Gentoo is the operating system I use on a daily basis. One of the classes I'm taking right now is a microprocessor and embedded systems development course. In this course, we're developing software for ARM processors in C and assembly. The boards we use in the lab are STM32F407G Discovery boards. # ARM Toolchain The recommended way to install cross-development toolchains on Gentoo is to install and set up the `sys-devel/crossdev` package. `crossdev` uses a local ebuild repository / overlay to create cross-development toolchains by setting the environment variables for `emerge` to download the necessary packages into that repository. To ensure that all stages are built (which includes everything from the binutils to the full compiler), you should use the `-s4` flag. [(1)](https://forums.gentoo.org/viewtopic-t-1044098.html) There are some additional packages you should install: * `app-eselect/eselect-repository` - This tool will allow us to create an overlay that `crossdev` can use. * `dev-embedded/openocd` - OpenOCD stands for Open On-Chip Debugger. It's a tool which allows debugging and flashing tools on your host system to communicate with microcontrollers through their hardware debug interface (like JTAG, SWD, ST-Link, J-Link). * `dev-debug/gdb` - This is the GNU Debugger, which works best with GNU toolchains and binaries compiled with them. It's especially useful for embedded development because you can remotely debug programs when used together with OpenOCD. To ensure that it's installed with support for system architectures other than the host's architecture, install it with the `multitarget` USE flag set. ```bash $ emerge -av sys-devel/crossdev app-eselect/eselect-repository dev-embedded/openocd dev-debug/gdb $ eselect repository create crossdev $ crossdev --stable --target arm-none-eabi -s4 ``` The compilation process for all four stages of this toolchain will take some time. # Development Environments ## STM32CubeIDE The STM32CubeIDE is the official development environment for STM32 microcontrollers and the one that's used in the microprocessor course I'm taking. It technically isn't required, but for learners like me it does make the process a lot easier and fun. A companion program you'll also need to download is the STM32CubeProgrammer. It's a tool used to actually write compiled binaries to the memory of whatever development board you have connected. The installer for the STM32CubeIDE includes the other necessary programs to get the IDE up and running, namely the STLink server and udev rules. Something that put me off from installing the STM32CubeIDE on my Linux system for the longest time was the fact that there was no package available on Gentoo for it. The STM32CubeIDE has a `.deb` version and there are packages available in the AUR on Arch Linux. But, sadly, there's nothing special which is available for Gentoo. The only way to install the STM32CubeIDE on Gentoo is to run the self-extracting installation script as the root user. I've just never been comfortable running shell scripts from the Internet as the root user nor having it install files in my root filesystem. So, I set out to devise a solution which would allow running the IDE as a local user. Is it more work? Yes. Is it really that much more work doing it this way? Not really. ### Create A New User The one thing I noticed right away when I attempted to run the STM32CubeIDE as my actual user was the enormous amount of directories and files it created in my home directory, like: * `.stm32cubeide`. * `.stm32cubemx`. * `.stmcube`. * `.stmcufinder`. * `STM32Cube`. * `STM32CubeIDE`. * `STMicroeletronics`. * `st`. I spend the majority of my time in my home directory, so having it cluttered up with these folders would be irritating. I decided to create an entirely new user account just for the STM32 software. ```bash $ useradd -G plugdev,video,wheel -m -s /bin/bash nate-dev ``` * `-g plugdev,video,wheel` - The `plugdev` group will allow the user to interact with any STM32 microcontroller board that's plugged into the system. The `video` group should ensure that the user can run graphical applications. The `wheel` group allows the user to run commands as the root user with the password. * `-m` - A home directory will be created for the user. This is where all of the program files will be stored. * `-s /bin/bash` - The shell of the user is set to `bash`. * `nate-dev` - The new user's name. ```bash $ passwd nate-dev ``` ### Download STM32CubeIDE And STM32CubeProgrammer The first step is to download the [STM32CubeIDE](https://www.st.com/en/development-tools/stm32cubeide.html) and the [STM32CubeProgrammer](https://www.st.com/en/development-tools/stm32cubeprog.html) from ST's website. If you're on a Linux distribution that doesn't use the Advanced Package Tool (`apt`) or the Red Hat Package Manager (`rpm`) like me, you'll have to download the *STM32CubeIDE Generic Linux Installer* and the *STM32CubeProgrammer software for Linux* scripts. Click *Get latest* to the right of both. You need to have a myST account created and signed in to download both files. Ideally, you should download these files as the new user and save them directly to its home directory. ### Run And Extract The STM32CubeIDE Setup Script To run and extract the STM32CubeIDE setup shell script once downloaded: ```bash $ su - nate-stm32 $ cd # Download the installation scripts to the home directory $ unzip en.st*.zip $ sh st-stm32cubeide*.sh --target st-stm32cubeide ``` Accept the license agreement when prompted by the installation script. Decline installing the STLink server and udev rules (we're going to do that later). Use the default installation directory given by the script (assuming you're running the script as the user we created earlier). ### Extract The STLink Server and udev Rules The setup shell script should've saved its contents to a newly created `st-stm32cubeide` directory. ```bash $ cd st-stm32cubeide $ sh st-stlink-server.*.sh --noexec --target st-stlink-server $ sh st-stlink-udev-rules-*.sh --noexec --target st-stlink-udev-rules ``` ### Install The udev Rules There are two options for adding the udev rules for STLink to the system. You can copy the files from the `st-stlink-udev-rules` directory to: 1. `/run/udev/rules.d/` - The rules will take effect until the system shuts down. 2. `/etc/udev/rules.d/` - The rules will take effect across reboots. Whenever you change or update udev rules, you can run the following commands to reload the udev device manager with the new configuration: ```bash udevadm control --reload-rules udevadm trigger ``` ### Run The STM32CubeProgrammer Setup Script To run the graphical installer for the STM32CubeProgrammer: ```bash $ cd $ ./SetupSTM32CubeProgrammer*.linux ``` Install the STM32CubeProgrammer in the default installation directory. ### Modify `~/.bash_profile` Add the following lines to the `~/.bash_profile` file: ```bash # ... # Add the directories containing the necessary STM32 binaries to the PATH variable. export PATH="$PATH:$HOME/st/stm32cubeide_1.18.0/" # STM32CubeIDE export PATH="$PATH:$HOME/st-stm32cubeide/st-stlink-server/" # STLink server export PATH="$PATH:$HOME/STMicroelectronics/STM32Cube/STM32CubeProgrammer/bin" # STM32CubeProgrammer # ... ``` To make the changes to the `~/.bash_profile` effective, run: ```bash $ source ~/.bash_profile ``` ### Testing To test if the installation was successful, run `stm32cubeide` as the new user: ```bash $ su - nate-dev $ stm32cubeide ``` The STM32CubeIDE logo should appear and IDE will open. ### Alias To save yourself some time, you can create an alias in the shell configuration of your actual user which runs `stm32cubeide` as the new user: ```bash alias stm32cubeide="su - nate-dev -c stm32cubeide" ``` # Raspberry Pi The Raspberry Pi is a fun computer to learn embedded development and programming on. My biggest challenge with using it, however, was the fact that I needed to use a monitor and peripherals to interact with it when there was no way I could connect it to the same network as my laptop. I looked into connecting to the Raspberry Pi over USB, but the Raspberry Pi I'm using doesn't support it. I use ConnMan as the network manager on my Gentoo system. The reason is that it's easy to use and convenient. That's especially true when it comes to configuring tethering. The `/etc/connman/main.conf` file, which is the configuration file for ConnMan, wasn't created on my system automatically. That's because this file is optional by default. But, we need this file to be created and for an entry in it to be added so we can enable tethering for Ethernet. Tethering for Ethernet is disabled by default because it can potentially disrupt networks. [(1)](https://man.archlinux.org/man/connman.conf.5.en) To allow Ethernet to have tethering turned on, you need to add the following configuration `/etc/connman/main.conf`: ``` [General] TetheringTechnologies=ethernet ``` After making those changes to ConnMan's configuration, run these commands to enable Ethernet connectivity and turn tethering on: ```bash $ rc-service connman restart $ connmanctl enable ethernet $ connmanctl tether ethernet on ``` Any device plugged into the Ethernet port will now have an IP address automatically assigned to them by ConnMan within a private subnet (192.168.0.0/24). If you're connected to another network through some other interface like Wi-Fi, then any network requests made by a device that's connected through the Ethernet port will be forwarded through that interface. DNS requests will also be forwarded to upstream DNS servers that the host is using. To connect to the Raspberry Pi when it is running, plug it into your Ethernet port and use SSH. ```bash $ ssh <user>@192.168.0.2 ```