Miosix porting for Vaisala RS-41SG radiosonde
After having found and collected some Vaisala RS-41SG radiosondes, I decided to port the Miosix operating system for their on-board microcontroller: this allows to have a nice enviroment to develop applications using the sonde’s hardware, with little effort.
The on-board microcontroller is an ST Microelectronics STM32F100C8, with 64kB of flash memory and 8kB or RAM. I will not describe here the overall hardware configuration of the sonde, since an excellent description can be found here. I will also assume that the reader already has the miosix toolchain set up on his/her computer: if this is not the case, here an installation guide can be found.
Kernel usage:
The miosix kernel is structured so that both the kernel and application code are compiled and linked together to obtain the final executable. The first operation to do when developing an application using the kernel, is obtaining a fresh copy of the kernel sources. This can be done using git, in this way:
git clone https://miosix.org/git-public/miosix-kernel.git
cd miosix-kernel
git fetch origin
git checkout -b testing origin/testing
This will create a folder named miosix-kernel containing both the kernel sources and a stub for the main source file. The last command switches to the testing branch of the repository, which is the one containing the latest changes.
To configure the kernel for our target, one has to open the file Makefile.inc, located in miosix-kernel/miosix/config folder, and uncomment the 49th line removing the leading #. The final result should be:
#OPT_BOARD := stm32f303vc_stm32f3discovery
OPT_BOARD := stm32f100c8_vaisala_rs41
Then, at lines 562-564 of the same file, there is the possibily to configure the microntroller’s clock signal:
#CLOCK_FREQ := -DSYSCLK_FREQ_8MHz=8000000
CLOCK_FREQ := -DSYSCLK_FREQ_24MHz=24000000 -DRUN_WITH_HSI
#CLOCK_FREQ := -DSYSCLK_FREQ_24MHz=24000000 -DHSE_VALUE=24000000
The three options correspond to the following clock configurations:
- core clocked at 8MHz using the internal RC oscillator;
- core clocked at 24MHz using the internal RC oscillator for PLL time base;
- core clocked at 24MHz using the external 24MHz crystal.
By default, the microcontroller runs at 24MHz using the internal oscillator for PLL time base. On the other hand, when all the three options are commented out, the microcontroller is automatically configured to run at 8MHz using the internal oscillator.
Notice: if the third option is selected and the program is flashed onto a radiosonde recovered after some time, there is the possibily that the microcontroller will not execute the code. This is due to the fact that a long exposure to the outside elements, in particular high levels of humidity, can damage the external oscillator, which is a rather delicate component.
After having configured the Makefile.inc file, it’s time to edit the one named miosix_settings.h, located in the same folder and providing the kernel configuration options. Here, first of all we have to comment out the line 37 to remove the compilation error which will remind us that the file has not been yet edited. Then, is a good idea to commend out the lines 88, 93 and 101: this will disable the filesystem support - which in our case is useless since we do not have any kind of memory device - thus saving some code size.
After these configurations, is possible to compile the kernel and the application code by issuing the make command in the directory containing the main Makefile, which is also the one containing the main.cpp file.
Hardware interfaces:
The kernel’s board support package comes with a file providing handy labels (actually they are typedefs) for the microcontroller’s gpios; the labels are grouped in namespaces associated to their functionality. To use these labels, the application’s source code has to use the directive #include “interfaces-impl/hwmapping.h”.
• GPS module:
The Ublox UBX-6010 GPS module is connected to the microcontroller through a serial interface, plus a reset signal. The serial interface is connected to USART1 on pins PA9 and PA10 and the communication runs at 9600 baud, while the reset signal is connected to PA15 and has negated logic (to reset the GPS one has to pull the line to low state). There is no need to write a custom driver for the serial interface, since USART1 is already supported by the STM32Serial driver.
• SPI devices:
All the SPI devices - the radio transmitter and the (optional) eeprom and barometer - share the same bus, connected to the microcontroller’s SPI2 peripheral using pins PB13, PB14 and PB15. The chip select signals are distributed in this way: PA8 for the barometer, PB2 for the eeprom and PC13 for the radio transmitter.
• XDATA connector:
The sonde has an XDATA interface connector (Amphenol Minitek 98464-G61-10LF), located at its bottom, opposite to the antenna and below the turn-on button, and having this pinout:
Pinout of XDATA connector Pinout of connected ribbon cable, when using 89361-710LF mating connector
-------
GND | o o | XDATA_RX(PB11) > 1 GND
| | 2 XDATA_RX(PB11)
XDATA_TX(PB10) | o o | +3V_MCU 3 XDATA_TX(PB10)
- | 4 +3V_MCU
V_Boost| o o | VBAT 5 V_Boost
- | 6 VBAT
MCU_RST | o o | SWCLK(PA14) 7 MCU_RST
| | 8 SWCLK(PA14)
SWDIO(PA13) | o o | GND 9 SWDIO(PA13)
------- 10 GND
Two mating connectors are available, both from the same manufacturer of the sonde’s one:
- 89361-710LF for ribbon cables;
- 90311-010LF crimp-to-wire (requires also crimp contacts 77138-101LF);
Here, the SWD interface allows to re-program the internal microcontroller, and eventually to debug the application code via OpenOCD and gdb. The pins labelled XDATA_RX and XDATA_TX are connected to microntroller’s USART3 and used by the miosix serial interface, running at 19200 baud 8N1. The MCU_RST pin, if connected to GND, puts the microcontroller in reset state.
Flashing the new firmware for the first time:
The original firmware comes with the flash memory having both the readout protection enabled and with some sectors having write protection enabled. Thus, before being able to write a new binary image to the flash memory, it’s necessary to remove all the protections: the readout protection can be removed using OpenOCD through specific commands, which also perform a complete flash erase. As regards the write protection, it can be removed by using the ST Microelectronics’ tool ST-LINK Utility.
Note: there can be some issues affecting flash memory unlocking through OpenOCD, thus I strongly reccommend using the ST-LINK Utility.
Once the flash memory has been unlocked, the new firmware image can be written with any tool, being it OpenOCD, ST-LINK Utility, or any other one. The new firmware is correctly written to flash and executed when, resetting the sonde’s microcontroller, there is a short flash of the red led and the kernel’s boot log is printed on the serial interface (unless disabled in miosix’s configuration file).
Code examples:
Alternate blink of green and red leds.
Usage of C standard input/output through serial console.
Redirection of GPS sentences on serial console.
References:
[1] Miosix kernel official page
[2] Code size optimisations to reduce kernel’s binary size
[4] STM32F100xx Reference Manual