1. Download the software package
The software package and example code we provide are available on CooCox website, which can be downloaded using the wget command:
$wget http://www.coocox.org/Embedded_Pi/softw ... 07.tar.bz2

The tools folder includes the software for Raspberry Pi, and the example folder includes the example code provided by CooCox.
2. Build up the development environment
As the Raspberry Pi uses the UART interface for programming the Embedded Pi, we need to install the PySerial library for UART, which is located in the “tools” folder:
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
3. Create a new project folder LED_Blinky under the directory “home/pi/”:
pi@raspberrypi ~ / EmbeddedPi $cd ..
pi@raspberrypi ~ $mkdir LED_Blinky
pi@raspberrypi ~ $cd LED_Blinky

Copy the files start_coide.c and stm32_f103_gcc.ld from the tools folder to the LED_Blinky folder:
pi@raspberrypi ~ / LED_Blinky $ cp ../EmbeddedPi/example/startup_coide.c .
pi@raspberrypi ~ / LED_Blinky $ cp ../EmbeddedPi/example/stm32_f103_gcc.ld .

4. Create new files led.c and reg.h, and paste the code below to the files respectively, then save the files. The code is used to make the LED on Embedded Pi blink.
//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);
}
}
So the project folder will look like the figure below:

5. Compile the project
1) Convert each .c file to an objective file with .o extension
$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 all the objective files
$ld -Tstm32_f103_gcc.ld –o led.elf led.o startup_coide.o
3) Generate .bin file for the Embedded Pi
$objcopy –Obinary led.elf led.bin
List all the files in the folder LED_Blinky:

At this point, the Raspberry Pi has compiled and generated the led.bin file for the Embedded Pi successfully, next we shall prepare for the programming.
6. Hardware connection
1) Short-circuit both JP2 and JP3 to set the Embedded Pi to the ST-Adapter Mode (For operation mode configuration, refer to section 3.12 of Embedded Pi User Manual)
2) Connect the Embedded Pi to the Raspberry Pi with the 26-PIN flat cable in correct direction
3) Connect the UART interface of the Embedded Pi and Raspberry Pi, as the yellow lines in the figure below:

7. Configure the UART interface as a serial console
The UART interface of Raspberry Pi is occupied by the operating system by default for outputting kernel information, thus we need to configure the UART interface to function as a serial console for communication with the Embedded Pi.
(This is based on the guide here).
Backup the /boot/cmdline.txt file before you edit it just in case of screw-ups:
$sudo cp /boot/cmdline.txt /boot/cmdline_backup.txt
Edit the file:
$sudo vi /boot/cmdline.txt
Modify dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 $ to dwc_otg.lpm_enable=0 console=tty1 $, and save.
You also need to edit this file:
$sudo vi /etc/inittab
Move the cursor to the start of the last line in the file, and press "i" to select insert and cursor and then press "#" to comment out the line:
#T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100
Press ESC and enter ":wq" to save and exit, then restart the Raspberry Pi:
$sudo reboot
8. Download the led.bin to the Embedded Pi
Copy the stm32loader.py file, which is mainly used for programming the Embedded Pi, from the tools folder to the LED_Blinky folder.

Regarding the Embedded Pi, press BOOT0 and hold it there, and press RESET button for 1 second and release, then release BOOT0, the STM32 will enter ISP mode (Refer to section 3.8.1 of Embedded Pi User Manual).
Go back to the Raspberry Pi, and program the .bin file to the Embedded Pi:
pi@raspberrypi ~ /LED_Blinky$ sudo python stm32loader.py –e –w –v led.bin
If a message of “None” always appears, redo the above operation of setting the Embedded Pi to ISP mode.
If an error message like “NACK” appears, retry the download command.
A message of “Verification OK” will appear after successful programming, as shown in the figure below:

Reset the STM32 and the LED on the Embedded Pi will blink.