STM32 programming with Arduino

Post Reply
User avatar
clanger9
Posts: 203
Joined: Mon Oct 28, 2019 7:41 am
Location: Chester, UK
Been thanked: 1 time
Contact:

STM32 programming with Arduino

Post by clanger9 »

For the uninitiated (like me), here are some quick steps to get up and running with an STM32 board and the Arduino IDE. I had a couple of false starts trying to follow guides on the internet, so here's what worked for me:
  1. Get a suitable dev board. You can get a cheap "blue pill" board and a programming interface, but to make life easier you can spend a little more on an official STM32 dev board. The one suggested above by Matt seems ideal: https://www.st.com/content/st_com/en/pr ... 103rb.html . It has the advantage of an on-board ST-LINK programming interface and easy access to all the pins, so you can use it out of the box.
  2. Add the board into your Arduino IDE. Start the IDE and paste the following URL into Preferences -> Additional Boards Manager URLs

    Code: Select all

    https://github.com/stm32duino/BoardManagerFiles/raw/master/STM32/package_stm_index.json
  3. Tools -> Board: -> Boards manager... -> install "STM32 Cores"
  4. Connect your dev board to the USB port and select the right board in the menu. If you're using the board linked above, you want Board: "Nucleo-64" and Board part number: "Nucleo F103RB"
  5. Open the Blink example, click Upload and you should be rewarded with a flashing LED :)
If it doesn't work (e.g. you get upload errors), you can try re-flashing the firmware using STM's firmware upgrade tool:
https://www.st.com/en/evaluation-tools/ ... 103rb.html#
User avatar
johu
Site Admin
Posts: 5684
Joined: Thu Nov 08, 2018 10:52 pm
Location: Kassel/Germany
Has thanked: 153 times
Been thanked: 960 times
Contact:

Re: STM32 programming with Arduino

Post by johu »

I split this off the template thread as it is not related ;)
Support R/D and forum on Patreon: https://patreon.com/openinverter - Subscribe on odysee: https://odysee.com/@openinverter:9
jon volk
Posts: 572
Joined: Wed Apr 10, 2019 7:47 pm
Location: Connecticut
Been thanked: 2 times

Re: STM32 programming with Arduino

Post by jon volk »

I tried to make the transition from Arduino/Teensy to STM32 and retain the Arduino programming facade. It’ll create more anger and frustration than it’s worth for anything beyond a blinking an LED. The initial curve is steep, but diving into c/cpp is time well spent. It’ll probably prove just as frustrating at first, but the return and better understanding of the hardware is worth it IMO. Most questions can then be answered by reading the data sheet over trying to understand what someone was doing in a 6 year old GitHub library.

Your mileage may vary!
Formerly 92 E30 BMW Cabrio with Tesla power
User avatar
celeron55
Posts: 774
Joined: Thu Jul 04, 2019 3:04 pm
Location: Finland
Has thanked: 27 times
Been thanked: 110 times
Contact:

Re: STM32 programming with Arduino

Post by celeron55 »

Arduino on STM32 works for simple projects with mostly simple I/O and some logging via USB. It gets in the way when squeezing the most out of an STM32 in a large custom project.

The compatibility of Arduino libraries on STM32 is surprisingly high, as long as you only use ones that build up on Arduino's standard libraries. For example an MCP2515 library can work, because instead of assuming anything about the hardware, it uses the SPI library, which is correctly ported in the STM32 Arduino core.

If you use the integrated CANbus peripheral, advanced USB, PWM or such, you need a platform specific library or if that doesn't exist you can write it yourself. The STM32 hardware abstraction layer is loaded with the Arduino stuff so you can do anything that you could do with STM32 in any environment. Note that this is the official libraries compared to libopencm3 used by openinverter with is completely different and replaces those.

The board definitions and board manager can be painful when using custom boards (stm32 boards often are custom boards). Arduino-cli makes things easier as you can write scripts to always select the right board for you.

Overall, not really the "easy way", but one way among others to program STM32s with pros and cons.
User avatar
mdrobnak
Posts: 692
Joined: Thu Mar 05, 2020 5:08 pm
Location: Colorado, United States
Has thanked: 1 time
Been thanked: 5 times

Re: STM32 programming with Arduino

Post by mdrobnak »

celeron55 wrote: Fri Jan 08, 2021 11:06 pm The board definitions and board manager can be painful when using custom boards (stm32 boards often are custom boards). Arduino-cli makes things easier as you can write scripts to always select the right board for you.
Never used the CLI - but the board manager issue was definitely real for me. It's what made me skip out on Arduino in the end. I wanted to be able to just pick different targets (say Teensy, or STM32) and it just didn't work well in my experience. The CLI might have made it easier.

-Matt
User avatar
clanger9
Posts: 203
Joined: Mon Oct 28, 2019 7:41 am
Location: Chester, UK
Been thanked: 1 time
Contact:

Re: STM32 programming with Arduino

Post by clanger9 »

Thank for the pointers - yes, I don't expect the Arduino IDE to cut it for a serious project! It was more that I didn't want to tackle new hardware and software all in one go, but I'd still like to have a play with Johannes' template project.

I was pleased to find that the whole Arduino "Boards manager" thing is now much simpler than it used to be for STM32. Some of the guides on the internet refer to 3rd partly libraries or workarounds that didn't work for me. With the above steps, I got it up and running in minutes.

My plan is to try porting some of my (simple) Arduino projects across to STM32 hardware first to get used to it, and see how it performs compared to the Teensy (which I still really like...).

If the hardware checks out for me, I'll look into using the proper STM32 toolchain.
User avatar
mdrobnak
Posts: 692
Joined: Thu Mar 05, 2020 5:08 pm
Location: Colorado, United States
Has thanked: 1 time
Been thanked: 5 times

Re: STM32 programming with Arduino

Post by mdrobnak »

Ahh, that makes good sense. The platform definitely has quirks, but also has a lot of people using it so it is easy to find others to ask for help when things go very wrong hehe.

-Matt
User avatar
mackoffgrid
Posts: 93
Joined: Thu Jan 02, 2020 10:18 am
Location: Brisbane Australia
Has thanked: 4 times

Re: STM32 programming with Arduino

Post by mackoffgrid »

The stm32F103 in the form of the Bluepill with a price that rivaled the arduino Nano was a bit of a game changer. 32 bit processor, descent memory, grown up ADC and Timers - Great. The path for the stm32 to Arduino IDE was a bit tortuous and to this day I don't use the USB bootloader, preferring the built in serial bootloader.

I like the arduino IDE - platform to the chagrin of my peers :oops: - because it's FREE, cross platform, Simple for my aging mind, And - But, I am quite happy to launch into bare metal programming the stm32's to better use the dual ADCs and timers etc. Oh, and a pet feature I really appreciate, the Arduino IDE can work in portable mode which I make full use of - I like to compartmentalize projects.

This last year I have been using other processors in the stm32F1 series such as the F105 and F107 in various pin counts, I just tell Arduino IDE that they are Bluepills and use Bare Metal programming to access the extra pins and features, such as Dual CAN or extra timers.

I tried stm32CubeIDE and I do like GUI setup but I found the HAL's are a bit over whelming - it needs a text book and reference book like we used to get.
https://github.com/mackelec/SolarUte
meFDCAN Arduino Library 3 FDCAN port stm32G4xx
meCAN Arduino Library 2023 version 2/3 CAN port stm32F0xx, stm32F1xx, stm32F4xx, stm32L4xx
User avatar
SciroccoEV
Posts: 369
Joined: Thu Oct 10, 2019 1:50 pm
Location: Luton UK
Been thanked: 15 times

Re: STM32 programming with Arduino

Post by SciroccoEV »

When I was trying some test code, I used Visual Studio Code with the Platform IO plugin. Very nice contextual editor. You could build projects on the Arduino, Libopencm3, STMCube and serveral other frameworks. The only thing that defeated me was configuring it to output binary and hex files. I KNOW it can be done, but the documentation on how to configure it didn't seem to match up. I could still program from within the IDE using an STM-link, but not use the Wifi updater. Since I was only doing some basic diagnostics, I didn't try very hard to did into the problem.
User avatar
EV_Builder
Posts: 1199
Joined: Tue Apr 28, 2020 3:50 pm
Location: The Netherlands
Has thanked: 16 times
Been thanked: 33 times
Contact:

Re: STM32 programming with Arduino

Post by EV_Builder »

If you want a better Arduino IDE you can use Visual Studio free edition from Microsoft (2017) and install on top of that a package with the name: "Visual.Micro.Arduino.Studio_X_X_X_X_"

Works very nice and its not that expensive at all.
It improves our hobby output allot.
Converting an Porsche Panamera
see http://www.wdrautomatisering.nl for bespoke BMS modules.
User avatar
mackoffgrid
Posts: 93
Joined: Thu Jan 02, 2020 10:18 am
Location: Brisbane Australia
Has thanked: 4 times

Re: STM32 programming with Arduino

Post by mackoffgrid »

stm32F105, stm32L4, stm32F4 Dual CAN Arduino library

I've been using exoCan and my own "Bare Metal" code to drive Dual CAN in the stm32F105/107 for a while now. However with the recent chip shortage I've bee forced into upgrading to the stm32L4 controller which meant I had to write a library for the L4. I'll doing my future Dual CAN projects probably using the F4 series. Hence one library to unify the lot.

I've only just finished what I've done when I had to jump to a another project so I haven't implemented the F4 controller yet, and I don't write libraries professionally so it may be a little rough around the edges, ......

I thought this might be useful .

https://github.com/mackelec/meCAN
https://github.com/mackelec/SolarUte
meFDCAN Arduino Library 3 FDCAN port stm32G4xx
meCAN Arduino Library 2023 version 2/3 CAN port stm32F0xx, stm32F1xx, stm32F4xx, stm32L4xx
Post Reply