Wireless Programming and Debugging with STM32 and RPi
Technology articles on Steemit
Hello everyone! I noticed the lack of non-crypto-related technology articles on Steemit. I would like to fill the gap - follow me if you're interested in technology!
Wireless Programming and Debugging with STM32 and RPi
Are you tired of connecting programmer every time you need to change software in you robot? Do you remember when you had to disassemble half of your work just to repair some minor bug in a software? That's not the case anymore! With any microcontroller supporting SWD - Serial Wire Debug - and Raspberry Pi you can easily program and debug your uC using only WiFi connection!
But wait, wireless programming? You're joking...
Yeah, we were also surprised that there is almost nothing on the Internet how it can be achieved. And actually yes, we are joking, because the programming itself is not wireless. Microcontroller has to be connected to some device which will be used as programmer. But if we use it along with Raspberry Pi? And we all know that ssh connection with RPi is pretty easy. So, what do we need more? ;)
Resources
Here you can find all the sources which helped us while working on this feature:
- https://visualgdb.com/tutorials/arm/st-link/
- http://vedder.se/2012/12/debugging-the-stm32f4-using-openocd-gdb-and-eclipse/
- https://stackoverflow.com/questions/5535110/stm32-gdb-openocd-commands-and-initialization-for-flash-and-ram-debugging
- http://openocd.org/doc/html/index.html
- https://learn.adafruit.com/programming-microcontrollers-using-openocd-on-raspberry-pi/wiring-and-test
- https://learn.adafruit.com/programming-microcontrollers-using-openocd-on-raspberry-pi/compiling-openocd
Where the last page was the most vital for us. It describes exactly the same what we wanted to achieve, but with Arduino instead of STM32 microcontroller.
Let's start with electronics
First thing to do is to download the datasheet of your microcontroller and find which pins are enabled as SWD:
Now, take a closer look at the RPi pinout presented on the adafruit:
Note the difference in pin numbering! On the adafruit website authors use the numbers directly from the processor, not the header numbers as most of us used to.
Make sure you make the wiring correctly in your project!
OpenOCD
The Open On-Chip Debugger (OpenOCD) aims to provide debugging, in-system programming and boundary-scan testing for embedded target devices.
We use OpenOCD defined rules to enable STM32 programming directly from Raspberry Pi microprocessor.
Install OpenOCD on Raspberry Pi
Unfortunately OpenOCD isn't available from the repositories and we need to compile it from scratch. From the other side though, here you can find excellent tutorial how to do it ;)
Find interface and target
Interface describes our programmer - in our case Raspberry Pi. Let's type in Raspberry's console:
ls /usr/local/share/openocd/scripts/interface/
Now you should see the supported devices. We are interested in raspberrypi2-native.cfg
. Write down the name of the file (ok, you can always copy it later from here :wink:)
Note: don't bother if you have Raspberry Pi 3. In RPi 2 and RPi 3 the interfaces are exactly the same, so this file will work for both of them.
Target describes a microcontroller which we want to program. Let's see the supported ones:
ls /usr/local/share/openocd/scripts/target/
Impressive list, right? Find your microcontroller there and write down the filename. In our case stm32f0x.cfg
.
Prepare OpenOCD configuration file
Now it's time to write configuration file for our microcontroller. As a good starting point we took the one from adafruit:
source [find interface/raspberrypi2-native.cfg]
transport select swd
set CHIPNAME at91samd21g18
source [find target/at91samdXX.cfg]
# did not yet manage to make a working setup using srst
#reset_config srst_only
reset_config srst_nogate
adapter_nsrst_delay 100
adapter_nsrst_assert_width 100
init
targets
reset halt
In case of STM family we don't have to provide CHIPNAME
, instead it's crucial to set WORKAREASIZE
to 0x2000
. The modified file looks as follows:
source [find interface/raspberrypi2-native.cfg]
transport select swd
set WORKAREASIZE 0x2000
source [find target/stm32f0x.cfg]
reset_config srst_only srst_nogate
adapter_nsrst_delay 100
adapter_nsrst_assert_width 100
init
targets
reset halt
Find some cosy place on your SD card, create a file named openocd.cfg
and paste there the above code.
Verify electronics
To verify whether everything works smooth and nice, go to the directory where you placed openocd.cfg
file and type:
sudo openocd
Now, you should see something similar:
Eclipse
It's possible, but it's difficult, to flash and debug software directly from the shell. That's why it would be nice to configure Eclipse to be compatible with OpenOCD programming and debugging.
Ensure GDB is able to establish connection with OpenOCD
First of all we need to be sure that nothing blocks connection.
- On Raspberry Pi execute
sudo openocd
- On local computer launch arm-gdb by executing
arm-none-eabi-gdb
and thentarget remote 192.168.2.109:3333
where 192.168.2.109 is the IP address of Raspberry Pi
If everything is fine, you should see:
On local computer:
On Raspberry Pi:
The last test is to establish telnet connection from local computer and Raspberry Pi. Leave the openocd running on RPi and on local computer execute: telnet 192.168.2.109 4444
. Remember to change the IP! You should get the connection now. Type exit
to quit.
Install OpenOCD Eclipse plugin
To use OpenOCD remotely you need to install the GNU ARM C/C++ OpenOCD Debugging plugin. Here you'll find a great tutorial how to do it.
Set Run Configurations
To use wireless programming and debugging in an existing project you have change Run Configurations.
Open your project in eclipse and select Run -> Run Configurations...
In the new window right-click on the GDB OpenOCD Debugging and choose New
- Give a good name to the configuration and change the settings in Debugger tab:
Repeat the above step (with the same configuration) for Debug Configurations -> GDB OpenOCD Debugging
In the same window add the GDB Hardware Debugging Configuration with following settings:
Test it!
Write some mind blowing software, run sudo openocd
on Raspberry and run it!
You should be able trace the program and all the registers values in real-time ;) Have fun!
Are you interested in Robotics?
Check out our newest project!
Hi! I am a robot. I just upvoted you! I found similar content that readers might be interested in:
https://www.hackster.io/turtle-rover/wireless-programming-and-debugging-with-stm32-and-rpi-bddfa5
Hi! For those who are afraid that I've just copied some random content - I'm also the co-author of the Hackster article ;)
Congratulations @twardakm! You have completed some achievement on Steemit and have been rewarded with new badge(s) :
You got a First Reply
Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here
If you no longer want to receive notifications, reply to this comment with the word
STOP