This projects adds real-time capabilities to Raspberry Pi by connecting it to STM32 F1 microcontroller via SPI. GPIO on Raspberry Pi is somewhat limited - there are no analog inputs and outputs, and the number of pins may quickly become to small for nontrivial applications. Furthermore, if we use Linux with Python, we are not working in a real-time environment. To avoid these limitations I’ve decided to connect Raspberry with 32-bit microcontroller. Since I had a board with STM32F103VE available, and it suited my requirements, it was a natural choice. Raspberry Pi can now send commands to the microcontroller, which executes them in real-time. With this extension we have additional 80 I/O pins available, with powerful hardware support of STM32 (timers, counters, AD/DA converters, ...).
The following set of commands is currently implemented as Python functions on Raspberry Pi and C functions on STM32:
Of course one can add additional commands for unused ports and peripheral devices if needed.
The download file contains all sources and binary for STM32F103. You can also download it at the end of this page.
Project consists of the following files and directories:
bin/
- contains compiler outputs (.o files)dist/
- contains linker output file rtrpi.elf to be downloaded to the targetlib/
- contains CMSIS library and CRC functions for STM32src/
- contains all C sources for STM32srcRPi/
- contains Python module to be used on Raspberry Pibuild.py
- build scriptbuilder.py
- utilities used by build scriptlicense.txt
- license filertRPi.iyaml
- unit test specification for microcontroller code in iSystem’s testIDEA format.rtRPi.xjrf
- winIDEA workspace used for debugging code on microcontrollerstm32f10x_intflash_intsram.ld
- linker configuration filereadme.mkd
- this text in markdown format.project
- Eclipse CDT project file.cproject
- Eclipse CDT project fileConnect the following pins:
Signal RPi pin STM32F103VE, LQFP100 pin ---------------------------------------------- MISO 19 31 (SPI1_MISO) MOSI 21 32 (SPI1_MOSI) SCLK 23 30 (31 (SPI1_SCK) CE 24 29 (SPI1_NSS)
Compiled elf file is included in the downloadable distribution (file dist/rtrpi.elf). All you need is a tool to program the flash on your microcontroller. Exact instructions depend on the tool used, so please consult your tool manual for details. Instructions described here refer to iSystem tools. Since this is a relatively simple project, the cheap iTag is fine.
Hardware | Hardware
, tab Communication
and select your debugger. Close the dialog.Debug | Download
, then disconnect the debugger.Microcontroller is ready.
SPI is not enabled by default on Raspberry, and we also need to install Python module for SPI control. For details please see:
Copy file srcRPi/stm32f1.py to Raspberry Pi.
Import module stm32f1.py and use its classes. It is recommended to use file srcRPi/spiTest.py as an example.
# Run motor for i in range(-100, 101, 10): rtrpi.setMotorSpeed(0, i) time.sleep(0.5) # Read distance from ultrasonic sensor print rtrpi.readDistance(0) # Read analog values ch0, ch1 = rtrpi.readADCValues() # Digital I/O rtrpi.writeDigitalBit(0, outBitNo, 1) if rtrpi.readDigitalBit(0, inBitNo) != 1: print 'Off'
Function Pin name Pin no. Description --------------------------------------------------------------------- ADC channel 0 PA.2 25 ADC123_IN2 ADC channel 1 PA.3 26 ADC123_IN3 Ultrasonic sensor PA.0 23 TIM5_CH1, Echo PA.1 24 GPIO out, Trigger Motor 0 PB.5 91 GPIO out, direction bit PB.6 92 TIM4_CH1, PWM output Motor 1 PB.2 37 GPIO out, direction bit PB.7 93 TIM4_CH2, PWM output Digital inputs PE[0..15] Digital outputs PC[0..15]
If you’d like to improve or change the existing functionality, you’ll also need the following software on PC:
$ cd <rtRPi unzip dir> $ ./build.py all
Eclipse project has this already configured as external build.
Testing
If you are using iTag, you can easily implement and run unit tests for functions on STM32. From winIDEA start testIDEA with menu command Test | testIDEA and open file rtRPi.iyaml. It already contains tests for CRC functions, and you can easily add your own tests. Please note that tests are run on STM32, so it must be attached to debugger during testing. The download file does not need to be modified, because no code instrumentation is performed.
This software is licensed under MIT open source license. See file license.txt for details.
I work for iSystem Labs d.o.o.
Contact: markok3.14 at Google mail.
© Marko Klopčič