Skip to content

Commit 22bf045

Browse files
pctingdeadprogram
authored andcommitted
add stm32 nucleol476rg support
1 parent 39029cc commit 22bf045

File tree

6 files changed

+184
-0
lines changed

6 files changed

+184
-0
lines changed

GNUmakefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,8 @@ ifneq ($(STM32), 0)
708708
@$(MD5SUM) test.hex
709709
$(TINYGO) build -size short -o test.hex -target=nucleo-l432kc examples/blinky1
710710
@$(MD5SUM) test.hex
711+
$(TINYGO) build -size short -o test.hex -target=nucleo-l476rg examples/blinky1
712+
@$(MD5SUM) test.hex
711713
$(TINYGO) build -size short -o test.hex -target=nucleo-l552ze examples/blinky1
712714
@$(MD5SUM) test.hex
713715
$(TINYGO) build -size short -o test.hex -target=nucleo-wl55jc examples/blinky1

src/machine/board_nucleol476rg.go

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
//go:build nucleol476rg
2+
3+
// Schematic: https://www.st.com/resource/en/user_manual/um1724-stm32-nucleo64-boards-mb1136-stmicroelectronics.pdf
4+
// Datasheet: https://www.st.com/resource/en/datasheet/stm32l476je.pdf
5+
6+
package machine
7+
8+
import (
9+
"device/stm32"
10+
"runtime/interrupt"
11+
)
12+
13+
const (
14+
// Arduino Pins
15+
A0 = PA0
16+
A1 = PA1
17+
A2 = PA4
18+
A3 = PB0
19+
A4 = PC1
20+
A5 = PC0
21+
22+
D0 = PA3
23+
D1 = PA2
24+
D2 = PA10
25+
D3 = PB3
26+
D4 = PB5
27+
D5 = PB4
28+
D6 = PB10
29+
D7 = PA8
30+
D8 = PA9
31+
D9 = PC7
32+
D10 = PB6
33+
D11 = PA7
34+
D12 = PA6
35+
D13 = PA5
36+
D14 = PB9
37+
D15 = PB8
38+
)
39+
40+
// User LD2: the green LED is a user LED connected to ARDUINO® signal D13 corresponding
41+
// to STM32 I/O PA5 (pin 21) or PB13 (pin 34) depending on the STM32 target.
42+
const (
43+
LED = LED_BUILTIN
44+
LED_BUILTIN = LED_GREEN
45+
LED_GREEN = PA5
46+
)
47+
48+
const (
49+
// This board does not have a user button, so
50+
// use first GPIO pin by default
51+
BUTTON = PA0
52+
)
53+
54+
const (
55+
// UART pins
56+
// PA2 and PA3 are connected to the ST-Link Virtual Com Port (VCP)
57+
UART_TX_PIN = PA2
58+
UART_RX_PIN = PA3
59+
60+
// I2C pins
61+
// With default solder bridge settings:
62+
// PB8 / Arduino D5 / CN3 Pin 8 is SCL
63+
// PB7 / Arduino D4 / CN3 Pin 7 is SDA
64+
I2C0_SCL_PIN = PB8
65+
I2C0_SDA_PIN = PB9
66+
67+
// SPI pins
68+
SPI1_SCK_PIN = PA5
69+
SPI1_SDI_PIN = PA6
70+
SPI1_SDO_PIN = PA7
71+
SPI0_SCK_PIN = SPI1_SCK_PIN
72+
SPI0_SDI_PIN = SPI1_SDI_PIN
73+
SPI0_SDO_PIN = SPI1_SDO_PIN
74+
)
75+
76+
var (
77+
// USART2 is the hardware serial port connected to the onboard ST-LINK
78+
// debugger to be exposed as virtual COM port over USB on Nucleo boards.
79+
UART1 = &_UART1
80+
_UART1 = UART{
81+
Buffer: NewRingBuffer(),
82+
Bus: stm32.USART2,
83+
TxAltFuncSelector: AF7_USART1_2_3,
84+
RxAltFuncSelector: AF7_USART1_2_3,
85+
}
86+
DefaultUART = UART1
87+
88+
// I2C1 is documented, alias to I2C0 as well
89+
I2C1 = &I2C{
90+
Bus: stm32.I2C1,
91+
AltFuncSelector: AF4_I2C1_2_3,
92+
}
93+
I2C0 = I2C1
94+
95+
// SPI1 is documented, alias to SPI0 as well
96+
SPI1 = &SPI{
97+
Bus: stm32.SPI1,
98+
AltFuncSelector: AF5_SPI1_2,
99+
}
100+
SPI0 = SPI1
101+
)
102+
103+
func init() {
104+
UART1.Interrupt = interrupt.New(stm32.IRQ_USART2, _UART1.handleInterrupt)
105+
}

src/machine/machine_stm32l4x6.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//go:build stm32l4x6
2+
3+
package machine
4+
5+
// Peripheral abstraction layer for the stm32l4x6
6+
7+
func CPUFrequency() uint32 {
8+
return 80e6
9+
}
10+
11+
// Internal use: configured speed of the APB1 and APB2 timers, this should be kept
12+
// in sync with any changes to runtime package which configures the oscillators
13+
// and clock frequencies
14+
const APB1_TIM_FREQ = 80e6 // 80MHz
15+
const APB2_TIM_FREQ = 80e6 // 80MHz
16+
17+
//---------- I2C related code
18+
19+
// Gets the value for TIMINGR register
20+
func (i2c *I2C) getFreqRange() uint32 {
21+
// This is a 'magic' value calculated by STM32CubeMX
22+
// for 80MHz PCLK1.
23+
// TODO: Do calculations based on PCLK1
24+
return 0x10909CEC
25+
}

src/runtime/runtime_stm32l4x6.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//go:build stm32 && stm32l4x6
2+
3+
package runtime
4+
5+
import (
6+
"device/stm32"
7+
)
8+
9+
/*
10+
clock settings
11+
12+
+-------------+-----------+
13+
| LSE | 32.768khz |
14+
| SYSCLK | 80mhz |
15+
| HCLK | 80mhz |
16+
| APB1(PCLK1) | 80mhz |
17+
| APB2(PCLK2) | 80mhz |
18+
+-------------+-----------+
19+
*/
20+
const (
21+
HSE_STARTUP_TIMEOUT = 0x0500
22+
PLL_M = 1
23+
PLL_N = 40
24+
PLL_P = RCC_PLLP_DIV7
25+
PLL_Q = RCC_PLLQ_DIV2
26+
PLL_R = RCC_PLLR_DIV2
27+
28+
MSIRANGE = stm32.RCC_CR_MSIRANGE_Range4M
29+
)

targets/nucleo-l476rg.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"inherits": ["cortex-m4"],
3+
"build-tags": ["nucleol476rg", "stm32l476", "stm32l4x6", "stm32l4", "stm32"],
4+
"serial": "uart",
5+
"linkerscript": "targets/stm32l4x6.ld",
6+
"extra-files": [
7+
"src/device/stm32/stm32l4x6.s"
8+
],
9+
"flash-method": "openocd",
10+
"openocd-interface": "stlink-v2-1",
11+
"openocd-target": "stm32l4x"
12+
}

targets/stm32l4x6.ld

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
MEMORY
3+
{
4+
FLASH_TEXT (rx) : ORIGIN = 0x08000000, LENGTH = 1024K
5+
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 96K
6+
RAM2 (xrw) : ORIGIN = 0x10000000, LENGTH = 32K
7+
}
8+
9+
_stack_size = 4K;
10+
11+
INCLUDE "targets/arm.ld"

0 commit comments

Comments
 (0)