tinygo-org / tinygo

Go compiler for small places. Microcontrollers, WebAssembly (WASM/WASI), and command-line tools. Based on LLVM.
https://tinygo.org
Other
14.73k stars 859 forks source link

Add STM32 Nucleo L476RG Support #4219

Closed pcting closed 2 months ago

pcting commented 2 months ago

Adding support for STM32 L476RG datasheet: https://www.st.com/resource/en/datasheet/stm32l476je.pdf board layout: https://www.st.com/resource/en/user_manual/um1724-stm32-nucleo64-boards-mb1136-stmicroelectronics.pdf

Running blinky3.go Notes:

$ tinygo flash  -target=nucleo-l476rg src/examples/blinky3/blinky3.go
xPack Open On-Chip Debugger 0.12.0+dev-01557-gdd1758272-dirty (2024-04-02-07:26)
Licensed under GNU GPL v2
For bug reports, read
    http://openocd.org/doc/doxygen/bugs.html
WARNING: interface/stlink-v2-1.cfg is deprecated, please switch to interface/stlink.cfg
Info : auto-selecting first available session transport "hla_swd". To override use 'transport select <transport>'.
Info : The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD
Info : clock speed 500 kHz
Info : STLINK V2J33M25 (API v2) VID:PID 0483:374B
Info : Target voltage: 3.258383
Info : [stm32l4x.cpu] Cortex-M4 r0p1 processor detected
Info : [stm32l4x.cpu] target has 6 breakpoints, 4 watchpoints
Info : [stm32l4x.cpu] Examination succeed
Info : starting gdb server for stm32l4x.cpu on 3333
Info : Listening on port 3333 for gdb connections
Info : Unable to match requested speed 500 kHz, using 480 kHz
Info : Unable to match requested speed 500 kHz, using 480 kHz
[stm32l4x.cpu] halted due to debug-request, current mode: Thread 
xPSR: 0x01000000 pc: 0x080031a4 msp: 0x20001000
** Programming Started **
Info : device idcode = 0x10076415 (STM32L47/L48xx - Rev 4 : 0x1007)
Info : RDP level 0 (0xAA)
Info : flash size = 1024 KiB
Info : flash mode : dual-bank
Info : Padding image section 0 at 0x0800c264 with 4 bytes (bank write end alignment)
Warn : Adding extra erase range, 0x0800c268 .. 0x0800c7ff
** Programming Finished **
** Resetting Target **
Info : Unable to match requested speed 500 kHz, using 480 kHz
Info : Unable to match requested speed 500 kHz, using 480 kHz
shutdown command invoked

it looks like the code flashes fine as the LED blinks every second as expected, but monitoring the serial port outputs garbage. i'm trying to figure out why.

building something similar using platformio:

 *  Executing task: platformio run --target upload 

Processing nucleo_l476rg (platform: ststm32; board: nucleo_l476rg; framework: arduino)
-------------------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/ststm32/nucleo_l476rg.html
PLATFORM: ST STM32 (17.3.0) > ST Nucleo L476RG
HARDWARE: STM32L476RGT6 80MHz, 96KB RAM, 1MB Flash
DEBUG: Current (stlink) On-board (stlink) External (blackmagic, cmsis-dap, jlink)
PACKAGES: 
 - framework-arduinoststm32 @ 4.20701.0 (2.7.1) 
 - framework-cmsis @ 2.50900.0 (5.9.0) 
 - tool-dfuutil @ 1.11.0 
 - tool-dfuutil-arduino @ 1.11.0 
 - tool-openocd @ 3.1200.0 (12.0) 
 - tool-stm32duino @ 1.0.1 
 - toolchain-gccarmnoneeabi @ 1.120301.0 (12.3.1)
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 14 compatible libraries
Scanning dependencies...
No dependencies
Building in release mode
Checking size .pio/build/nucleo_l476rg/firmware.elf
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [          ]   1.5% (used 1488 bytes from 98304 bytes)
Flash: [          ]   2.2% (used 23240 bytes from 1048576 bytes)
Configuring upload protocol...
AVAILABLE: blackmagic, cmsis-dap, jlink, mbed, stlink
CURRENT: upload_protocol = stlink
Uploading .pio/build/nucleo_l476rg/firmware.elf
xPack Open On-Chip Debugger 0.12.0-01004-g9ea7f3d64-dirty (2023-01-30-15:03)
Licensed under GNU GPL v2
For bug reports, read
        http://openocd.org/doc/doxygen/bugs.html
debug_level: 1

srst_only separate srst_nogate srst_open_drain connect_deassert_srst

Warn : target stm32l4x.cpu examination failed
[stm32l4x.cpu] halted due to debug-request, current mode: Thread 
xPSR: 0x01000000 pc: 0x08000ff8 msp: 0x20000800
** Programming Started **
Warn : Adding extra erase range, 0x08005c78 .. 0x08005fff
** Programming Finished **
** Verify Started **
** Verified OK **
** Resetting Target **
shutdown command invoked
=============================== [SUCCESS] Took 5.81 seconds ===============================
 *  Terminal will be reused by tasks, press any key to close it. 
pcting commented 2 months ago

occasionally i see this error:

$ tinygo flash  -target=nucleo-l476rg src/examples/blinky3/blinky3.go
xPack Open On-Chip Debugger 0.12.0+dev-01557-gdd1758272-dirty (2024-04-02-07:26)
Licensed under GNU GPL v2
For bug reports, read
    http://openocd.org/doc/doxygen/bugs.html
WARNING: interface/stlink-v2-1.cfg is deprecated, please switch to interface/stlink.cfg
Info : auto-selecting first available session transport "hla_swd". To override use 'transport select <transport>'.
Info : The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD
Info : clock speed 500 kHz
Info : STLINK V2J33M25 (API v2) VID:PID 0483:374B
Info : Target voltage: 3.255227
Info : [stm32l4x.cpu] Cortex-M4 r0p1 processor detected
Info : [stm32l4x.cpu] target has 6 breakpoints, 4 watchpoints
Info : [stm32l4x.cpu] Examination succeed
Info : starting gdb server for stm32l4x.cpu on 3333
Info : Listening on port 3333 for gdb connections
Info : Unable to match requested speed 500 kHz, using 480 kHz
Info : Unable to match requested speed 500 kHz, using 480 kHz
Error: timed out while waiting for target halted
** Unable to reset target **
shutdown command invoked

error: failed to flash /tmp/tinygo3652766739/main.hex: exit status 1

the solutions

Option 1: Flash Twice Holding Reset

  1. while holding down the reset button down, flash the device
  2. release the reset button and flash again.

Option 2: Flash With platform.io

  1. create a "hello world" app in platform.io and flash it.
deadprogram commented 2 months ago

Hello @pcting thank you for adding this new board/processor.

Did you actually mean to add the additional example? I'm pretty sure that is actually not needed. It would be better to just add the new board to the smoketests located here: https://github.com/tinygo-org/tinygo/blob/dev/GNUmakefile#L696-L731

pcting commented 2 months ago

Hello @pcting thank you for adding this new board/processor.

Did you actually mean to add the additional example? I'm pretty sure that is actually not needed. It would be better to just add the new board to the smoketests located here: https://github.com/tinygo-org/tinygo/blob/dev/GNUmakefile#L696-L731

ah, i wasn't aware of the smoketest target. thanks! i'll remove the new blinky3.go example

pcting commented 2 months ago

@deadprogram i've cleaned up the unneeded example. let me know if anything else needs changing. thanks

pcting commented 2 months ago

i think i need to revisit the arduino pin mapping before we merge

pcting commented 2 months ago

i fixed the pin mapping; it should be good now

deadprogram commented 2 months ago

Thanks again @pcting and @sago35 now merging.