Overview:
Since the release of Embedded Pi many users have found programming Embedded Pi a bit difficult, specially Linux users as CooCox CoIDE is a Windows based program. CooCox team has addressed this issue and came up with an unique open source STM32 loader ISP tool (requires binary .bin file as input) which allows the Raspberry Pi users to program Embedded Pi STM32 MCU flash memory, using serial (UART) communication between Raspberry Pi and Embedded Pi, directly from Raspberry Pi Linux based Debian environment. Now Raspberry Pi users can use the in-built GCC compiler (or any other compiler) to develop embedded projects in C/C++, compile, link (generate binary .bin file) and download the program into the Embedded Pi STM32 MCU flash memory to test the functionality.
This document provides a step-by-step guide on how to write a simple LED Blinking C program using GCC compiler on Raspberry Pi, and later run the program on Embedded Pi by downloading into MCU flash memory using STM32 loader ISP tool.
How to program Embedded Pi using Raspberry Pi
Step 1: Download the resource kit from CooCox website and extract all the files:- To start first we need to download few resource files provided by CooCox team, which includes STM32 MCU startup code, STM32 ISP tool (written in Python), Serial library for Python (used by ISP tool) and LED blinky example. Use the below commands to download and extract all the files:
$wget http://www.coocox.org/Embedded_Pi/software_code/EmbeddedPi_20130807.tar.bz2
$tar –xjvf EmbeddedPi_30130807.tar.bz2
File Extracted Location: /home/pi/EmbeddedPi
Step 2: Install Python serial (UART) library for ISP Tool:
pi@raspberrypi ~ /EmbeddedPi $cd tools
pi@raspberrypi ~ / EmbeddedPi $tar –xzf pyserial-2.6.tar.gz
pi@raspberrypi ~ / EmbeddedPi $cd pyserial-2.6
pi@raspberrypi ~ / EmbeddedPi $sudo python setup.py install
Step 3: Create a directory named “LED_Blinky” and copy “startup_coide.c” and “stm32_f103_gcc.ld” files to this directory from “/home/pi/EmbeddedPi/example” directory:
pi@raspberrypi ~ / EmbeddedPi $cd ..
pi@raspberrypi ~ $mkdir LED_Blinky
pi@raspberrypi ~ $cd LED_Blinky
pi@raspberrypi ~ /LED_Blinky $cp .. /EmbeddedPi/example/startup_coide.c .
pi@raspberrypi ~ / LED_Blinky $cp .. /EmbeddedPi/example/stm32_f103_gcc.ld .
As shown below:
Step 4: Now create "led.c" & "reg.h" files and copy the below code to the respective files. You can use any editor of your choice:
//filename: reg.h: #ifndef _REGS_H_ #define _REGS_H_ #define RCC_APB2ENR *(volatile unsigned long *)0x40021018 #define GPIOB_CRL *(volatile unsigned long *)0x40010C00 #define GPIOB_CRH *(volatile unsigned long *)0x40010C04 #define GPIOB_IDR *(volatile unsigned long *)0x40010C08 #define GPIOB_ODR *(volatile unsigned long *)0x40010C0C #endif |
//filename: led.c: #include "reg.h" void delay_ms(unsigned short nms) { unsigned long i; while(nms--) for(i = 0; i < 1000; i++); } int main(void) { RCC_APB2ENR |= (1<<3); GPIOB_CRH = (2<<20) | (0<<22); GPIOB_ODR = 1<<13; //PB13 while(1) { GPIOB_ODR |= 1<<13; delay_ms(500); GPIOB_ODR &= ~(1<<13); delay_ms(500); } } |
Step 5: Now we will use GCC compile to compile all these files and generate a binary .bin file. It’s a 3 step process; compile, link and create .bin file:
1. Compile and convert the .c files into .o target files.
$ gcc -mcpu=cortex-m3-mthumb -Wall -g -O0 -nostartfiles -c startup_coide.c -o startup_coide.o
$ gcc -mcpu=cortex-m3-mthumb -Wall -g -O0 -nostartfiles -I. -c led.c -o led.o
2. Link the object files and create .elf file.
$ ld -Tstm32_f103_gcc.ld -o led.elf led.o startup_coide.o
3. Generate .bin file from .elf file.
$ objcopy -Obinary led.elf led.bin
Now we have successfully compiled all the source files and generated a binary “led.bin” file. This is the file we need to download into Embedded Pi flash memory using ISP tool. You can use the “ls” command to see all the generated files as below:
$ ls -l
Step 6: Prepare your hardware; connect Raspberry Pi & Embedded Pi as below (please refer to Embedded Pi User Manual for detail):
- Setup the Embedded Pi in ST-Adapter mode - connect JP2, JP3 connection.
- Connect the Embedded Pi and Raspberry Pi using 26 pin ribbon cable.
- Connect Raspberry Pi and Embedded Pi UART communication port as shown in "Yellow Lines" below:
Step 7: STM32 loader ISP tool requires Raspberry Pi UART port, but the Raspberry Pi UART interface is by default occupied by Raspberry Pi OS to output kernel information. So we need to configure the Raspberry Pi UART0 before we can use it for STM32 loader ISP tool to download the program into Embedded Pi. Please follow the below steps to configure Raspberry Pi UART port:
Note: It's better to backup the /boot/cmdline.txt file before you edit, just in case if something goes wrong ( $sudo cp /boot/cmdline.txt /boot/cmdline_backup.txt)
Edit the file as below:
$ sudo vi /boot/cmdline.txt
The file contained the following:
dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait
Delete any parameters involving the serial port "ttyAMA0", which in this example is:
console=ttyAMA0,115200 kgdboc=ttyAMA0,115200
Which gives:
dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait
Press ESC to return to the vi command mode and then type ":wq" (without the quotation marks) to save and exit (even if it appears vi is still in editing mode just type the ":wq" command). If you need to exit without saving type ":q".
You also need to edit this file:
$ sudo vi /etc/inittab
Search /ttyAMA0/ for the serial port usage by typing:
This should find the line of the file specifying the serial port (if there is one), move the cursor to the start of the line and press "i" to select insert and then press "#" to comment out the line. Now press ESC and enter ":wq" to save and exit.
Enter "sudo reboot" to restart the Raspberry Pi and now the UART will be available to use in another process.
Step 8: Download the "led.bin" file into the Embedded Pi using STM32 loader ISP tool.
1. Copy the “stm32loader.py” file from “/home/pi/EmbeddedPi/tools” directory to “/home/pi/LED_linky” project directory.
2. Setup the Embedded Pi STM32 in serial download mode - first press the Embedded Pi BOOT0 key (HIGH) and without releasing press the RESET button. After pressing RESET button, release the BOOT0 key.
3. Now use the below command to download the generated binary "led.bin" file (in Step 5) into the Embedded Pi flash memory:
pi@raspberrypi ~ / LED_Blinky $ sudo python stm32loader.py -e -w -v led.bin
Possible Outcomes:
a. If prompted with "NONE" on screen, please re-run the above Steps 8.2 [hold boot0, reset MCU].
b. If prompted with “NACK” on screen, please re-download the .bin file (Step 8.3).
c. If prompted with “Verification OK” on screen, indicates a successful download.
Now restart the Embedded Pi, the program should execute and LED will start blinking.