stm32: add blues wireless swan

This commit is contained in:
Kenneth Bell
2022-01-09 17:03:04 +00:00
committed by Ron Evans
parent 21c76c0cb0
commit 14ce531498
11 changed files with 421 additions and 249 deletions
+63
View File
@@ -0,0 +1,63 @@
//go:build swan
// +build swan
package machine
import (
"device/stm32"
"runtime/interrupt"
)
const (
// LED on the SWAN
LED = PE2
// UART pins
// PA9 and PA10 are connected to the SWAN Tx/Rx
UART_TX_PIN = PA9
UART_RX_PIN = PA10
// I2C pins
// PB6 is SCL
// PB7 is SDA
I2C0_SCL_PIN = PB6
I2C0_SDA_PIN = PB7
// SPI pins
SPI1_SCK_PIN = PD1
SPI1_SDI_PIN = PB14
SPI1_SDO_PIN = PB15
SPI0_SCK_PIN = SPI1_SCK_PIN
SPI0_SDI_PIN = SPI1_SDI_PIN
SPI0_SDO_PIN = SPI1_SDO_PIN
)
var (
// USART1 is connected to the TX/RX pins
UART1 = &_UART1
_UART1 = UART{
Buffer: NewRingBuffer(),
Bus: stm32.USART1,
TxAltFuncSelector: 7,
RxAltFuncSelector: 7,
}
DefaultUART = UART1
// I2C1 is documented, alias to I2C0 as well
I2C1 = &I2C{
Bus: stm32.I2C1,
AltFuncSelector: 4,
}
I2C0 = I2C1
// SPI1 is documented, alias to SPI0 as well
SPI1 = &SPI{
Bus: stm32.SPI2,
AltFuncSelector: 5,
}
SPI0 = SPI1
)
func init() {
UART1.Interrupt = interrupt.New(stm32.IRQ_USART1, _UART1.handleInterrupt)
}
+56 -16
View File
@@ -117,6 +117,25 @@ const (
PE15 = portE + 15
)
// IRQs are defined here as they vary in the SVDs, but do have consistent mapping
// to Timer Interrupts.
const (
irq_TIM1_BRK_TIM15 = 24
irq_TIM1_UP_TIM16 = 25
irq_TIM1_TRG_COM_TIM17 = 26
irq_TIM1_CC = 27
irq_TIM2 = 28
irq_TIM3 = 29
irq_TIM4 = 30
irq_TIM5 = 50
irq_TIM6 = 54
irq_TIM7 = 55
irq_TIM8_BRK = 43
irq_TIM8_UP = 44
irq_TIM8_TRG_COM = 45
irq_TIM8_CC = 46
)
func (p Pin) getPort() *stm32.GPIO_Type {
switch p / 16 {
case 0:
@@ -185,8 +204,6 @@ func enableAltFuncClock(bus unsafe.Pointer) {
stm32.RCC.APB1ENR1.SetBits(stm32.RCC_APB1ENR1_TIM2EN)
case unsafe.Pointer(stm32.LPTIM2): // LPTIM2 clock enable
stm32.RCC.APB1ENR2.SetBits(stm32.RCC_APB1ENR2_LPTIM2EN)
case unsafe.Pointer(stm32.I2C4): // I2C4 clock enable
stm32.RCC.APB1ENR2.SetBits(stm32.RCC_APB1ENR2_I2C4EN)
case unsafe.Pointer(stm32.LPUART1): // LPUART1 clock enable
stm32.RCC.APB1ENR2.SetBits(stm32.RCC_APB1ENR2_LPUART1EN)
case unsafe.Pointer(stm32.TIM16): // TIM16 clock enable
@@ -258,6 +275,29 @@ func (p Pin) registerInterrupt() interrupt.Interrupt {
return interrupt.Interrupt{}
}
//---------- UART related code
// Configure the UART.
func (uart *UART) configurePins(config UARTConfig) {
// enable the alternate functions on the TX and RX pins
config.TX.ConfigureAltFunc(PinConfig{Mode: PinModeUARTTX}, uart.TxAltFuncSelector)
config.RX.ConfigureAltFunc(PinConfig{Mode: PinModeUARTRX}, uart.RxAltFuncSelector)
}
// UART baudrate calc based on the bus and clockspeed
// NOTE: keep this in sync with the runtime/runtime_stm32l5x2.go clock init code
func (uart *UART) getBaudRateDivisor(baudRate uint32) uint32 {
return (CPUFrequency() / baudRate)
}
// Register names vary by ST processor, these are for STM L5
func (uart *UART) setRegisters() {
uart.rxReg = &uart.Bus.RDR
uart.txReg = &uart.Bus.TDR
uart.statusReg = &uart.Bus.ISR
uart.txEmptyFlag = stm32.USART_ISR_TXE
}
//---------- SPI related types and code
// SPI on the STM32Fxxx using MODER / alternate function pins
@@ -445,19 +485,19 @@ var (
func (t *TIM) registerUPInterrupt() interrupt.Interrupt {
switch t {
case &TIM1:
return interrupt.New(stm32.IRQ_TIM1_UP_TIM16, TIM1.handleUPInterrupt)
return interrupt.New(irq_TIM1_UP_TIM16, TIM1.handleUPInterrupt)
case &TIM2:
return interrupt.New(stm32.IRQ_TIM2, TIM2.handleUPInterrupt)
return interrupt.New(irq_TIM2, TIM2.handleUPInterrupt)
case &TIM3:
return interrupt.New(stm32.IRQ_TIM3, TIM3.handleUPInterrupt)
return interrupt.New(irq_TIM3, TIM3.handleUPInterrupt)
case &TIM6:
return interrupt.New(stm32.IRQ_TIM6_DACUNDER, TIM6.handleUPInterrupt)
return interrupt.New(irq_TIM6, TIM6.handleUPInterrupt)
case &TIM7:
return interrupt.New(stm32.IRQ_TIM7, TIM7.handleUPInterrupt)
return interrupt.New(irq_TIM7, TIM7.handleUPInterrupt)
case &TIM15:
return interrupt.New(stm32.IRQ_TIM1_BRK_TIM15, TIM15.handleUPInterrupt)
return interrupt.New(irq_TIM1_BRK_TIM15, TIM15.handleUPInterrupt)
case &TIM16:
return interrupt.New(stm32.IRQ_TIM1_UP_TIM16, TIM16.handleUPInterrupt)
return interrupt.New(irq_TIM1_UP_TIM16, TIM16.handleUPInterrupt)
}
return interrupt.Interrupt{}
@@ -466,19 +506,19 @@ func (t *TIM) registerUPInterrupt() interrupt.Interrupt {
func (t *TIM) registerOCInterrupt() interrupt.Interrupt {
switch t {
case &TIM1:
return interrupt.New(stm32.IRQ_TIM1_CC, TIM1.handleUPInterrupt)
return interrupt.New(irq_TIM1_CC, TIM1.handleUPInterrupt)
case &TIM2:
return interrupt.New(stm32.IRQ_TIM2, TIM2.handleOCInterrupt)
return interrupt.New(irq_TIM2, TIM2.handleOCInterrupt)
case &TIM3:
return interrupt.New(stm32.IRQ_TIM3, TIM3.handleOCInterrupt)
return interrupt.New(irq_TIM3, TIM3.handleOCInterrupt)
case &TIM6:
return interrupt.New(stm32.IRQ_TIM6_DACUNDER, TIM6.handleOCInterrupt)
return interrupt.New(irq_TIM6, TIM6.handleOCInterrupt)
case &TIM7:
return interrupt.New(stm32.IRQ_TIM7, TIM7.handleOCInterrupt)
return interrupt.New(irq_TIM7, TIM7.handleOCInterrupt)
case &TIM15:
return interrupt.New(stm32.IRQ_TIM1_BRK_TIM15, TIM15.handleOCInterrupt)
return interrupt.New(irq_TIM1_BRK_TIM15, TIM15.handleOCInterrupt)
case &TIM16:
return interrupt.New(stm32.IRQ_TIM1_UP_TIM16, TIM16.handleOCInterrupt)
return interrupt.New(irq_TIM1_UP_TIM16, TIM16.handleOCInterrupt)
}
return interrupt.Interrupt{}
+1 -27
View File
@@ -1,13 +1,10 @@
//go:build stm32l4x2
// +build stm32l4x2
package machine
// Peripheral abstraction layer for the stm32l4x2
import (
"device/stm32"
)
func CPUFrequency() uint32 {
return 80000000
}
@@ -18,29 +15,6 @@ func CPUFrequency() uint32 {
const APB1_TIM_FREQ = 80e6 // 80MHz
const APB2_TIM_FREQ = 80e6 // 80MHz
//---------- UART related code
// Configure the UART.
func (uart *UART) configurePins(config UARTConfig) {
// enable the alternate functions on the TX and RX pins
config.TX.ConfigureAltFunc(PinConfig{Mode: PinModeUARTTX}, uart.TxAltFuncSelector)
config.RX.ConfigureAltFunc(PinConfig{Mode: PinModeUARTRX}, uart.RxAltFuncSelector)
}
// UART baudrate calc based on the bus and clockspeed
// NOTE: keep this in sync with the runtime/runtime_stm32l5x2.go clock init code
func (uart *UART) getBaudRateDivisor(baudRate uint32) uint32 {
return (CPUFrequency() / baudRate)
}
// Register names vary by ST processor, these are for STM L5
func (uart *UART) setRegisters() {
uart.rxReg = &uart.Bus.RDR
uart.txReg = &uart.Bus.TDR
uart.statusReg = &uart.Bus.ISR
uart.txEmptyFlag = stm32.USART_ISR_TXE
}
//---------- I2C related code
// Gets the value for TIMINGR register
+26
View File
@@ -0,0 +1,26 @@
//go:build stm32l4x5
// +build stm32l4x5
package machine
// Peripheral abstraction layer for the stm32l4x5
func CPUFrequency() uint32 {
return 120e6
}
// Internal use: configured speed of the APB1 and APB2 timers, this should be kept
// in sync with any changes to runtime package which configures the oscillators
// and clock frequencies
const APB1_TIM_FREQ = 120e6 // 120MHz
const APB2_TIM_FREQ = 120e6 // 120MHz
//---------- I2C related code
// Gets the value for TIMINGR register
func (i2c *I2C) getFreqRange() uint32 {
// This is a 'magic' value calculated by STM32CubeMX
// for 120MHz PCLK1.
// TODO: Do calculations based on PCLK1
return 0x307075B1
}