Skip to content

Instantly share code, notes, and snippets.

@jjwatt
Created March 3, 2020 03:23
Show Gist options
  • Save jjwatt/18c9d05b83e18a691243dd434d96eebb to your computer and use it in GitHub Desktop.
Save jjwatt/18c9d05b83e18a691243dd434d96eebb to your computer and use it in GitHub Desktop.
Getting OpenSTM32 System Workbench to work on Nix
{ pkgs ? import <nixpkgs> {} }:
(pkgs.buildFHSUserEnv {
name = "stupid-java-installer-bs-env";
targetPkgs = pkgs: (with pkgs;
[ bash
oraclejdk8
gtk2
gnome2.glib
xorg.libXext
xorg.libXtst
]);
runScript = "bash";
}).env
@jjwatt
Copy link
Author

jjwatt commented Mar 3, 2020

Not that I would necessarily recommend it yet. You need even less for the install: Just bash and oraclejdk8 iirc. The install*.run file that openstm32 gives you is kind of trash. It's one of those JRE and JAR files embedded in a badly written bash script. It does some weird stuff, and I eventually went from hacking on the ~500MiB file's script lines to just trying to get it to work without changing it. When I finally fixed some of the bugs in the *.run file, java told me corrupt jar, so I gave up on that route for now. I may actually try just building for stm32 with native Nix cross compilers/gcc toolchains for embedded arm and makefiles. platformio also looks nice, and it's open source. I didn't know about it until after I did this, and now I see that it even has a Nix package! So, I may be going that route instead, but this should get you off the ground if you absolutely need OpenSTM32. Additionally, this seemed to work for installing STM32CubeIDE, too. They seem to use the same lazy crazy shit. However, I haven't gotten the stm32cubeide binary to run yet. It might even do crazier shit like bundle a gtk...

@jjwatt
Copy link
Author

jjwatt commented Mar 3, 2020

Here's the error I'm currently getting trying to run stm32cubeide if anyone's interested (unrelated to openstm32):

stupid-java-installer-bs-env-chrootenv:jwatt@wisdom-like-silence:~/st/stm32cubeide_1.3.0$ cat configuration/1583206252113.log
!SESSION 2020-03-03 03:30:51.903 -----------------------------------------------
eclipse.buildId=Version 1.3.0
java.version=1.8.0_242
java.vendor=AdoptOpenJDK
BootLoader constants: OS=linux, ARCH=x86_64, WS=gtk, NL=en_US
Command-line arguments:  -os linux -ws gtk -arch x86_64

!ENTRY org.eclipse.osgi 4 0 2020-03-03 03:30:55.693
!MESSAGE Application error
!STACK 1
java.lang.UnsatisfiedLinkError: org.eclipse.swt.internal.gtk.OS._cachejvmptr()V
        at org.eclipse.swt.internal.gtk.OS._cachejvmptr(Native Method)
        at org.eclipse.swt.internal.gtk.OS.cachejvmptr(OS.java:494)
        at org.eclipse.swt.internal.gtk.OS.<clinit>(OS.java:93)
        at org.eclipse.swt.internal.Converter.wcsToMbcs(Converter.java:209)
        at org.eclipse.swt.internal.Converter.wcsToMbcs(Converter.java:155)
        at org.eclipse.swt.widgets.Display.<clinit>(Display.java:161)
        at org.eclipse.ui.internal.Workbench.createDisplay(Workbench.java:735)
        at org.eclipse.ui.PlatformUI.createDisplay(PlatformUI.java:162)
        at org.eclipse.ui.internal.ide.application.IDEApplication.createDisplay(IDEApplication.java:180)
        at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:123)
        at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:203)
        at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:137)
        at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:107)
        at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:400)
        at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:255)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:660)
        at org.eclipse.equinox.launcher.Main.basicRun(Main.java:597)
        at org.eclipse.equinox.launcher.Main.run(Main.java:1468)
        at org.eclipse.equinox.launcher.Main.main(Main.java:1441)

@jjwatt
Copy link
Author

jjwatt commented Mar 3, 2020

I know this should be in a blog or something, and maybe I will put it in one soon. But for now, I just wanna get the info out there. Still haven't tried to get stm32cubeide working, but I have tried platformio on NixOS. At first, I was having issues with permissions, but then noticed that a lot of things were working as root. I had nixos.stlink installed as root and as my user, but of course that didn't give me the udev rules I needed, and chmod on /dev/ttyACM0 only got me so far. Then, I realized that I needed to add stlink to the system not just to individual users in order to get the udev rules. I don't think this is spelled out too well in any of the NixOS documentation or wikis, but it makes logical sense. It just took me a little while to connect those dots. It will likely also help me with some other issues: e.g., I was having permission issues with cdrtools and ended up burning CDs as root. I think now I won't need to. The thing to do is in your /etc/nixos/configuration.nix under environment.systemPackages = with pkgs; [ ... ] add stlink to that list. I'm not sure if that's all that is required because there is also a services.udev.packages option in NixOS. I guess, technically, that's in the services.udev NixOS module in nixpkgs though. Anyway, I went ahead and added some packages that I definitely wanted udev rules from to that, and it looks like it worked!

services.udev.packages = [ pkgs.stlink pkgs.cdrtools pkgs.dvdplusrwtools ]

btw, I didn't even check to see if dvdplusrwtools provided udev rules yet. Heh. Anyway, it didn't hurt, and now I can do this as my user:

 jwatt@wisdom-like-silence  ~  st-info --probe                      
Found 1 stlink programmers
 serial: 303636464646353034393535363537
openocd: "\x30\x36\x36\x46\x46\x46\x35\x30\x34\x39\x35\x35\x36\x35\x37"
  flash: 524288 (pagesize: 131072)
   sram: 131072
 chipid: 0x0421
  descr: F446 device

The rules even added aliases for my devices, so:

 jwatt@wisdom-like-silence  ~  find /dev/ -iname 'ttyACM*' -or -iname 'stlinkv2-1*' | xargs ls -al
lrwxrwxrwx 1 root root         3 Mar  3 15:14 /dev/stlinkv2-1_ -> sdh
lrwxrwxrwx 1 root root        11 Mar  3 15:14 /dev/stlinkv2-1_0 -> bsg/9:0:0:0
lrwxrwxrwx 1 root root        15 Mar  3 15:14 /dev/stlinkv2-1_1 -> bus/usb/001/006
crw-rw-rw- 1 root dialout 166, 0 Mar  3 15:14 /dev/ttyACM0

The USB interface does 3 things (4 things if you count power). That's why you see a link to -> sdh. It actually shows the device's flash as a mountable disk. The other things it does is: A serial device and the coveted debugging link over stlinkv2(-1)! That means I can fire up a remote gdb on it and/or use other tools to read and write directly to memory locations and even step through code line by line on the actual device itself. I'm looking forward to that. Reminds me of some of the retro hacking I do, programming for old 8 and 16-bit computers, using their native monitors or monitors/debuggers built into emulators.

And, I'm betting that platformio and other stlink based tools (including debuggers!) won't have any complaints now, either. So, I'm gradually moving away from the "bundled" embedded junk (which is mostly opensource to give them credit) to something that is more my style: native, step-by-step, unixy, modular tool-driven development. A udemy course I'm using uses OpenSTM32 System Workbench (and MDK, etc which is Windows-only), so I wanted to get it working to dip my toes in before I dug myself into setting up my ideal system for embedded hacking. Now, I guess I'm somewhere in the middle with platformio and using OpenSTM32 here and there. My dev board is a ARM Cortex-M4F, STM32F446 Nucleo Board in case that helps people stumble onto this with Google searches.

@leo60228
Copy link

thank you for this! for future people, you can download the java tarball nixpkgs will want from https://www.oracle.com/java/technologies/javase/javase8u211-later-archive-downloads.html, and System Workbench for STM32 from https://www.openstm32.org/System%2BWorkbench%2Bfor%2BSTM32

both of these need logins, you can get working ones from http://bugmenot.com/

@mikeyj001
Copy link

@jjwatt did you ever get stm32cubeide to work? I need it now, and just started to create an expression. I don't know how long it will take for me to successfully create the package.

@mikeyj001
Copy link

I have finally gotten stm32cubeide to run. This involved downloading the zip and changing the shebang in all of the *.sh files. Then I had to patch the stm32cubeide executable and run it as root. I also patched the stlink server executable, but while it runs, the IDE can't link to it, so still working on it.

I installed the nixos stlink package, but while it connects, the IDE complains about it not being the newest version.

Udev might be the issue I'm facing, so I'll try adding the line that jjwatt added and see what happens.

I aim to make a derivation, but the initial issue is that the user has to log in to download the zip file, which I don't know how to handle. I can't get the URL for the download.

I might end up using platformio after all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment