Tutorial: How to program the pyboard with Arduino

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Post Reply
appels
Posts: 13
Joined: Thu Dec 03, 2020 3:14 pm

Tutorial: How to program the pyboard with Arduino

Post by appels » Sun Dec 27, 2020 6:43 pm

Hi,

I needed to program the pyboard in with the Arduino framework, but to my surprise I couldn't find a single video, article, forum topic or pretty much anything that told me how to do this. And after a lot of trial and error and I mean a lot, I finally got it to work.
And because it took me so long, I don't want to screw over the next guy that needs to do this by putting him in the same situation.
That's why I'm making this small tutorial on how to do this.


If you want to use the Arduino IDE:

-> You will need the STM32CubeProgrammer, because Arduino will use it and if you don't have it you will get errors, so you might as well install it first. (https://www.st.com/en/development-tools ... eprog.html) (It is possible that when installing you will be asked to download Java Runtime Environment, if you don't already have it. You will be provided with the link during the installation, so just install that as well)
->Download Arduino (https://www.arduino.cc/en/software)
->Once installed open Arduino, go to "File", then "preferences" and type the following URL
'https://raw.githubusercontent.com/stm32 ... index.json'
in the box next to "Additional Boards Manager URLs"
->Then go to "Tools", "Board" and click on "Boards Manager"
->Once you're there, search in the box at the top for 'stm', then you should see the package "STM32 Cores".
->Install this package.
->Once installed close the Boards Manager
->Now we have to tweak the Board settings so the Arduino IDE knows which board you're going to hook up to your computer.
->Go to "Tools" and make sure that everything is set like this:
* Board = 'Generic STM32F4 series'
* Board Part Number = 'Generic F405RG'
* U(S)ART support = 'Enabled (generic 'Serial')'
* USB support = 'CDC (generic 'Serial' supersede U(S)ART)'
* USB speed = 'Low/Full Speed'
* Optimize = 'Smallest (-Os default)'
* C Runtime Library: 'Newlib Nano (default)'
* Upload method 'STM32CubeProgrammer (DFU)'
* You don't need to set the port, because STM32CubeProgrammer will take care of this and when the pyboard is in bootloader mode, it isn't presented as a Port anyway.
->Now comes the sketchy part.
->If you try to upload code like this, normally it will upload it succesfully to the pyboard, but it won't execute correctly.
->I found a solution to this problem, but we have to alter 2 files from the STM board support package we just downloaded.
->Go to "C:\Users\<username>\AppData\Local\Arduino15\packages\STM32\hardware\stm32\1.9.0\variants\Generic_F4x5RG"
->Once you're in this folder open "variant.cpp" with a text or code editor (I used Atom, but I think any editor will do)
->Then on line 161 change "FLASH_LATENCY_1" in to 'FLASH_LATENCY_5' and save and close the file
->Open variant.h and add the line '#define HSE_VALUE 12000000U' (I don't think it matters where you put it, but I would put it somewhere in the middle so it is in the ifndef guards. I put it on line 112)
->Also save and close this file.
->Now you should be able to load Arduino sketches on to the pyboard. (But don't use the 'X1', 'X2', 'Y5',... Pin names, because they aren't defined. You have to use 'PA1', 'PA2', 'PB4',... The MicroPython docs has an overview of these pin names:
https://docs.micropython.org/en/latest/ ... ckref.html)
->And also remember to put the board in to bootloader mode (also called dfu mode) everytime before uploading a sketch by connecting 'BOOT0' with 3V3 and hitting reset. (This is shown in the "preparation" section of this tutorial: https://github.com/micropython/micropyt ... are-Update)


If you want to use VS Code with PlatformIO:

->Download VS Code (https://code.visualstudio.com/)
->Once installed open VS Code and click on the extensions icon on the left hand side. (It looks like 3 blocks together and a fourth block closeby)
->search for 'platformio' and install it.
->Once installed you should see the home tab of PlatformIO on your screen, but if you don't then click on the little house in the bottom left corner that says "PlatformIO: Home".
->On the home tab, click on "+ New Project"
->Give it a name, select "STM32F405RG (128k RAM. 1024 Flash) (Generic)" as board, "Arduino" as framework and if you want a diferent location to save it then choose one.
->The project will create itself, this can take some time.
->When it's finished look in your project folder on the left hand side and open "platform.ini".
->We have to add two lines to this file, so we can upload the code correctly to the pyboard:
* 'upload_protocol = dfu'
* 'build_flags = -D PIO_FRAMEWORK_ARDUINO_ENABLE_CDC'
->Save and close this file.
->Now unfold the "src" folder in your project and open "main.cpp" by double clicking on it.
->Here you can write your Arduino code.
->To upload it: save it and then click on the check icon in the bottom left corner that says "PlatformIO: Build"
->Once it is compiled without errors you can upload your code by clicking on the arrow icon in the bottom left corner that says "PlatformIO: Upload" (But put your pyboard in dfu mode before uploading ofcourse)
->This will upload you code correctly, but your code won't work on the pyboard.
->I found a solution to this problem, but we have to alter 2 files from the STM board support package.
->Go to "C:\Users\<username>\.platformio\packages\framework-arduinoststm32\variants\Generic_F4x5RG"
->Once you're in this folder open "variant.cpp" with a text or code editor (I used Atom, but I think any editor will do)
->Then on line 161 change "FLASH_LATENCY_1" in to 'FLASH_LATENCY_5' and save and close the file
->Open variant.h and add the line '#define HSE_VALUE 12000000U' (I don't think it matters where you put it, but I would put it somewhere in the middle so it is in the ifndef guards. I put it on line 112)
->Also save and close this file.
->Now you should be able to load Arduino sketches on to the pyboard. (But don't use the 'X1', 'X2', 'Y5',... Pin names, because they aren't defined. You have to use 'PA1', 'PA2', 'PB4',... The MicroPython docs has an overview of these pin names:
https://docs.micropython.org/en/latest/ ... ckref.html)
->And also remember to put the board in to bootloader mode (also called dfu mode) everytime before uploading a sketch by connecting 'BOOT0' with 3V3 and hitting reset. (This is shown in the "preparation" section of this tutorial: https://github.com/micropython/micropyt ... are-Update)



I can't tell you exactly why this works only that it does. It also isn't some random code I came up with and put in there. I came to this solution, because I also have the Adafruit STM32F405 Feather Express, and with this board I could program it with Arduino right away. So I started looking through these files of both boards and looked for differences. I changed a lot of things in these files and multiple solutions worked, but after some tests, I concluded that I only needed to change these 2 things to let the pyboard work with Arduino.
But still I don't know exactly what I changed with these 2 adjustments, but the developers at Adafruit aren't stupid, so I think it will be alright.

I think it also would be worthwile to make a specific "pyboard board package" for the STM32duino github repository. You can pretty much base it on the generic one we use now and/or the Adafruit one and then add code for the specific pin names. I can't imagine it would be that difficult to make for an experienced developer and it would be really helpful for everybody who wants to use Arduino to program their pyboard, because you could just select it out of a list instead of doing all this.



References:
I got a lot of information from the Adafruit STM32F405 Feather Express Arduino setup page, because since they use the same MCU a lot of it is similar to the Pyboard. (https://learn.adafruit.com/adafruit-stm ... -ide-setup)
Also this forum question helped me come to this solution (https://community.platformio.org/t/stm3 ... ller/14671)

appels
Posts: 13
Joined: Thu Dec 03, 2020 3:14 pm

Re: Tutorial: How to program the pyboard with Arduino

Post by appels » Mon Dec 28, 2020 9:14 am

By the way in case it is helpfull:

I did this on a Windows PC
And I already used the Zadig utility to install the WinUSB driver for dfu mode (when I updated the MicroPython firmware on the pyboard)
Here is al link for the Zadig utility: https://zadig.akeo.ie/
And if you want more information about how to use it, search for 'pyboard firmware update'.

Post Reply