Skip to content

Commit 14ce531

Browse files
kenbelldeadprogram
authored andcommitted
stm32: add blues wireless swan
1 parent 21c76c0 commit 14ce531

File tree

11 files changed

+421
-249
lines changed

11 files changed

+421
-249
lines changed

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,8 @@ ifneq ($(STM32), 0)
462462
@$(MD5SUM) test.hex
463463
$(TINYGO) build -size short -o test.hex -target=lorae5 examples/blinky1
464464
@$(MD5SUM) test.hex
465+
$(TINYGO) build -size short -o test.hex -target=swan examples/blinky1
466+
@$(MD5SUM) test.hex
465467
endif
466468
ifneq ($(AVR), 0)
467469
$(TINYGO) build -size short -o test.hex -target=atmega1284p examples/serial

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ The following 79 microcontroller boards are currently supported:
7979
* [Arduino Zero](https://store.arduino.cc/usa/arduino-zero)
8080
* [BBC micro:bit](https://microbit.org/)
8181
* [BBC micro:bit v2](https://microbit.org/new-microbit/)
82+
* [blues wireless Swan](https://blues.io/products/swan/)
8283
* [Digispark](http://digistump.com/products/1)
8384
* [Dragino LoRaWAN GPS Tracker LGT-92](http://www.dragino.com/products/lora-lorawan-end-node/item/142-lgt-92.html)
8485
* [ESP32 - Core board](https://www.espressif.com/en/products/socs/esp32)

src/machine/board_swan.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//go:build swan
2+
// +build swan
3+
4+
package machine
5+
6+
import (
7+
"device/stm32"
8+
"runtime/interrupt"
9+
)
10+
11+
const (
12+
// LED on the SWAN
13+
LED = PE2
14+
15+
// UART pins
16+
// PA9 and PA10 are connected to the SWAN Tx/Rx
17+
UART_TX_PIN = PA9
18+
UART_RX_PIN = PA10
19+
20+
// I2C pins
21+
// PB6 is SCL
22+
// PB7 is SDA
23+
I2C0_SCL_PIN = PB6
24+
I2C0_SDA_PIN = PB7
25+
26+
// SPI pins
27+
SPI1_SCK_PIN = PD1
28+
SPI1_SDI_PIN = PB14
29+
SPI1_SDO_PIN = PB15
30+
SPI0_SCK_PIN = SPI1_SCK_PIN
31+
SPI0_SDI_PIN = SPI1_SDI_PIN
32+
SPI0_SDO_PIN = SPI1_SDO_PIN
33+
)
34+
35+
var (
36+
// USART1 is connected to the TX/RX pins
37+
UART1 = &_UART1
38+
_UART1 = UART{
39+
Buffer: NewRingBuffer(),
40+
Bus: stm32.USART1,
41+
TxAltFuncSelector: 7,
42+
RxAltFuncSelector: 7,
43+
}
44+
DefaultUART = UART1
45+
46+
// I2C1 is documented, alias to I2C0 as well
47+
I2C1 = &I2C{
48+
Bus: stm32.I2C1,
49+
AltFuncSelector: 4,
50+
}
51+
I2C0 = I2C1
52+
53+
// SPI1 is documented, alias to SPI0 as well
54+
SPI1 = &SPI{
55+
Bus: stm32.SPI2,
56+
AltFuncSelector: 5,
57+
}
58+
SPI0 = SPI1
59+
)
60+
61+
func init() {
62+
UART1.Interrupt = interrupt.New(stm32.IRQ_USART1, _UART1.handleInterrupt)
63+
}

src/machine/machine_stm32l4.go

Lines changed: 56 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,25 @@ const (
117117
PE15 = portE + 15
118118
)
119119

120+
// IRQs are defined here as they vary in the SVDs, but do have consistent mapping
121+
// to Timer Interrupts.
122+
const (
123+
irq_TIM1_BRK_TIM15 = 24
124+
irq_TIM1_UP_TIM16 = 25
125+
irq_TIM1_TRG_COM_TIM17 = 26
126+
irq_TIM1_CC = 27
127+
irq_TIM2 = 28
128+
irq_TIM3 = 29
129+
irq_TIM4 = 30
130+
irq_TIM5 = 50
131+
irq_TIM6 = 54
132+
irq_TIM7 = 55
133+
irq_TIM8_BRK = 43
134+
irq_TIM8_UP = 44
135+
irq_TIM8_TRG_COM = 45
136+
irq_TIM8_CC = 46
137+
)
138+
120139
func (p Pin) getPort() *stm32.GPIO_Type {
121140
switch p / 16 {
122141
case 0:
@@ -185,8 +204,6 @@ func enableAltFuncClock(bus unsafe.Pointer) {
185204
stm32.RCC.APB1ENR1.SetBits(stm32.RCC_APB1ENR1_TIM2EN)
186205
case unsafe.Pointer(stm32.LPTIM2): // LPTIM2 clock enable
187206
stm32.RCC.APB1ENR2.SetBits(stm32.RCC_APB1ENR2_LPTIM2EN)
188-
case unsafe.Pointer(stm32.I2C4): // I2C4 clock enable
189-
stm32.RCC.APB1ENR2.SetBits(stm32.RCC_APB1ENR2_I2C4EN)
190207
case unsafe.Pointer(stm32.LPUART1): // LPUART1 clock enable
191208
stm32.RCC.APB1ENR2.SetBits(stm32.RCC_APB1ENR2_LPUART1EN)
192209
case unsafe.Pointer(stm32.TIM16): // TIM16 clock enable
@@ -258,6 +275,29 @@ func (p Pin) registerInterrupt() interrupt.Interrupt {
258275
return interrupt.Interrupt{}
259276
}
260277

278+
//---------- UART related code
279+
280+
// Configure the UART.
281+
func (uart *UART) configurePins(config UARTConfig) {
282+
// enable the alternate functions on the TX and RX pins
283+
config.TX.ConfigureAltFunc(PinConfig{Mode: PinModeUARTTX}, uart.TxAltFuncSelector)
284+
config.RX.ConfigureAltFunc(PinConfig{Mode: PinModeUARTRX}, uart.RxAltFuncSelector)
285+
}
286+
287+
// UART baudrate calc based on the bus and clockspeed
288+
// NOTE: keep this in sync with the runtime/runtime_stm32l5x2.go clock init code
289+
func (uart *UART) getBaudRateDivisor(baudRate uint32) uint32 {
290+
return (CPUFrequency() / baudRate)
291+
}
292+
293+
// Register names vary by ST processor, these are for STM L5
294+
func (uart *UART) setRegisters() {
295+
uart.rxReg = &uart.Bus.RDR
296+
uart.txReg = &uart.Bus.TDR
297+
uart.statusReg = &uart.Bus.ISR
298+
uart.txEmptyFlag = stm32.USART_ISR_TXE
299+
}
300+
261301
//---------- SPI related types and code
262302

263303
// SPI on the STM32Fxxx using MODER / alternate function pins
@@ -445,19 +485,19 @@ var (
445485
func (t *TIM) registerUPInterrupt() interrupt.Interrupt {
446486
switch t {
447487
case &TIM1:
448-
return interrupt.New(stm32.IRQ_TIM1_UP_TIM16, TIM1.handleUPInterrupt)
488+
return interrupt.New(irq_TIM1_UP_TIM16, TIM1.handleUPInterrupt)
449489
case &TIM2:
450-
return interrupt.New(stm32.IRQ_TIM2, TIM2.handleUPInterrupt)
490+
return interrupt.New(irq_TIM2, TIM2.handleUPInterrupt)
451491
case &TIM3:
452-
return interrupt.New(stm32.IRQ_TIM3, TIM3.handleUPInterrupt)
492+
return interrupt.New(irq_TIM3, TIM3.handleUPInterrupt)
453493
case &TIM6:
454-
return interrupt.New(stm32.IRQ_TIM6_DACUNDER, TIM6.handleUPInterrupt)
494+
return interrupt.New(irq_TIM6, TIM6.handleUPInterrupt)
455495
case &TIM7:
456-
return interrupt.New(stm32.IRQ_TIM7, TIM7.handleUPInterrupt)
496+
return interrupt.New(irq_TIM7, TIM7.handleUPInterrupt)
457497
case &TIM15:
458-
return interrupt.New(stm32.IRQ_TIM1_BRK_TIM15, TIM15.handleUPInterrupt)
498+
return interrupt.New(irq_TIM1_BRK_TIM15, TIM15.handleUPInterrupt)
459499
case &TIM16:
460-
return interrupt.New(stm32.IRQ_TIM1_UP_TIM16, TIM16.handleUPInterrupt)
500+
return interrupt.New(irq_TIM1_UP_TIM16, TIM16.handleUPInterrupt)
461501
}
462502

463503
return interrupt.Interrupt{}
@@ -466,19 +506,19 @@ func (t *TIM) registerUPInterrupt() interrupt.Interrupt {
466506
func (t *TIM) registerOCInterrupt() interrupt.Interrupt {
467507
switch t {
468508
case &TIM1:
469-
return interrupt.New(stm32.IRQ_TIM1_CC, TIM1.handleUPInterrupt)
509+
return interrupt.New(irq_TIM1_CC, TIM1.handleUPInterrupt)
470510
case &TIM2:
471-
return interrupt.New(stm32.IRQ_TIM2, TIM2.handleOCInterrupt)
511+
return interrupt.New(irq_TIM2, TIM2.handleOCInterrupt)
472512
case &TIM3:
473-
return interrupt.New(stm32.IRQ_TIM3, TIM3.handleOCInterrupt)
513+
return interrupt.New(irq_TIM3, TIM3.handleOCInterrupt)
474514
case &TIM6:
475-
return interrupt.New(stm32.IRQ_TIM6_DACUNDER, TIM6.handleOCInterrupt)
515+
return interrupt.New(irq_TIM6, TIM6.handleOCInterrupt)
476516
case &TIM7:
477-
return interrupt.New(stm32.IRQ_TIM7, TIM7.handleOCInterrupt)
517+
return interrupt.New(irq_TIM7, TIM7.handleOCInterrupt)
478518
case &TIM15:
479-
return interrupt.New(stm32.IRQ_TIM1_BRK_TIM15, TIM15.handleOCInterrupt)
519+
return interrupt.New(irq_TIM1_BRK_TIM15, TIM15.handleOCInterrupt)
480520
case &TIM16:
481-
return interrupt.New(stm32.IRQ_TIM1_UP_TIM16, TIM16.handleOCInterrupt)
521+
return interrupt.New(irq_TIM1_UP_TIM16, TIM16.handleOCInterrupt)
482522
}
483523

484524
return interrupt.Interrupt{}

src/machine/machine_stm32l4x2.go

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1+
//go:build stm32l4x2
12
// +build stm32l4x2
23

34
package machine
45

56
// Peripheral abstraction layer for the stm32l4x2
67

7-
import (
8-
"device/stm32"
9-
)
10-
118
func CPUFrequency() uint32 {
129
return 80000000
1310
}
@@ -18,29 +15,6 @@ func CPUFrequency() uint32 {
1815
const APB1_TIM_FREQ = 80e6 // 80MHz
1916
const APB2_TIM_FREQ = 80e6 // 80MHz
2017

21-
//---------- UART related code
22-
23-
// Configure the UART.
24-
func (uart *UART) configurePins(config UARTConfig) {
25-
// enable the alternate functions on the TX and RX pins
26-
config.TX.ConfigureAltFunc(PinConfig{Mode: PinModeUARTTX}, uart.TxAltFuncSelector)
27-
config.RX.ConfigureAltFunc(PinConfig{Mode: PinModeUARTRX}, uart.RxAltFuncSelector)
28-
}
29-
30-
// UART baudrate calc based on the bus and clockspeed
31-
// NOTE: keep this in sync with the runtime/runtime_stm32l5x2.go clock init code
32-
func (uart *UART) getBaudRateDivisor(baudRate uint32) uint32 {
33-
return (CPUFrequency() / baudRate)
34-
}
35-
36-
// Register names vary by ST processor, these are for STM L5
37-
func (uart *UART) setRegisters() {
38-
uart.rxReg = &uart.Bus.RDR
39-
uart.txReg = &uart.Bus.TDR
40-
uart.statusReg = &uart.Bus.ISR
41-
uart.txEmptyFlag = stm32.USART_ISR_TXE
42-
}
43-
4418
//---------- I2C related code
4519

4620
// Gets the value for TIMINGR register

src/machine/machine_stm32l4x5.go

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

0 commit comments

Comments
 (0)