mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-05 03:27:48 +00:00
Improvements for stm32wle targets :
- board/stm32: Add STM32 Nucleo WL55JC board - stm32/stm32wl: Set 48Mhz HSE32/PLL as default Clock, SPI implementation
This commit is contained in:
committed by
Ron Evans
parent
718746dd21
commit
b0fce80b50
@@ -9,8 +9,8 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
LED1 = PB5
|
||||
LED = LED1 // Default LED
|
||||
// We assume a LED is connected on PB5
|
||||
LED = PB5 // Default LED
|
||||
)
|
||||
|
||||
// SubGhz (SPI3)
|
||||
@@ -46,6 +46,11 @@ var (
|
||||
RxAltFuncSelector: AF7_USART1_2,
|
||||
}
|
||||
DefaultUART = UART0
|
||||
|
||||
// SPI
|
||||
SPI3 = SPI{
|
||||
Bus: stm32.SPI3,
|
||||
}
|
||||
)
|
||||
|
||||
func init() {
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
//go:build nucleowl55jc
|
||||
// +build nucleowl55jc
|
||||
|
||||
package machine
|
||||
|
||||
import (
|
||||
"device/stm32"
|
||||
"runtime/interrupt"
|
||||
)
|
||||
|
||||
const (
|
||||
LED_BLUE = PB15
|
||||
LED_GREEN = PB9
|
||||
LED_RED = PB11
|
||||
LED = LED_RED
|
||||
|
||||
BTN1 = PA0
|
||||
BTN2 = PA1
|
||||
BTN3 = PC6
|
||||
BUTTON = BTN1
|
||||
|
||||
// SubGhz (SPI3)
|
||||
SPI0_NSS_PIN = PA4
|
||||
SPI0_SCK_PIN = PA5
|
||||
SPI0_SDO_PIN = PA6
|
||||
SPI0_SDI_PIN = PA7
|
||||
|
||||
//MCU USART1
|
||||
UART1_TX_PIN = PB6
|
||||
UART1_RX_PIN = PB7
|
||||
|
||||
//MCU USART2
|
||||
UART2_RX_PIN = PA3
|
||||
UART2_TX_PIN = PA2
|
||||
|
||||
// DEFAULT USART
|
||||
UART_RX_PIN = UART2_RX_PIN
|
||||
UART_TX_PIN = UART2_TX_PIN
|
||||
)
|
||||
|
||||
var (
|
||||
// STM32 UART2 is connected to the embedded STLINKV3 Virtual Com Port
|
||||
UART0 = &_UART0
|
||||
_UART0 = UART{
|
||||
Buffer: NewRingBuffer(),
|
||||
Bus: stm32.USART2,
|
||||
TxAltFuncSelector: 7,
|
||||
RxAltFuncSelector: 7,
|
||||
}
|
||||
|
||||
// UART1 is free
|
||||
UART1 = &_UART1
|
||||
_UART1 = UART{
|
||||
Buffer: NewRingBuffer(),
|
||||
Bus: stm32.USART1,
|
||||
TxAltFuncSelector: 7,
|
||||
RxAltFuncSelector: 7,
|
||||
}
|
||||
|
||||
DefaultUART = UART0
|
||||
|
||||
// SPI
|
||||
SPI3 = SPI{
|
||||
Bus: stm32.SPI3,
|
||||
}
|
||||
)
|
||||
|
||||
func init() {
|
||||
// Enable UARTs Interrupts
|
||||
UART0.Interrupt = interrupt.New(stm32.IRQ_USART2, _UART0.handleInterrupt)
|
||||
UART1.Interrupt = interrupt.New(stm32.IRQ_USART1, _UART1.handleInterrupt)
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
//go:build stm32 && !stm32f7x2 && !stm32l5x2 && !stm32wle5
|
||||
// +build stm32,!stm32f7x2,!stm32l5x2,!stm32wle5
|
||||
//go:build stm32 && !stm32f7x2 && !stm32l5x2
|
||||
// +build stm32,!stm32f7x2,!stm32l5x2
|
||||
|
||||
package machine
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ package machine
|
||||
|
||||
import (
|
||||
"device/stm32"
|
||||
"math/bits"
|
||||
"runtime/interrupt"
|
||||
"runtime/volatile"
|
||||
"unsafe"
|
||||
@@ -28,12 +29,15 @@ const (
|
||||
AF15_EVENTOUT = 15
|
||||
)
|
||||
|
||||
func CPUFrequency() uint32 {
|
||||
return 4e6
|
||||
}
|
||||
const (
|
||||
SYSCLK = 48e6
|
||||
APB1_TIM_FREQ = SYSCLK
|
||||
APB2_TIM_FREQ = SYSCLK
|
||||
)
|
||||
|
||||
const APB1_TIM_FREQ = 4e6
|
||||
const APB2_TIM_FREQ = 4e6
|
||||
func CPUFrequency() uint32 {
|
||||
return SYSCLK
|
||||
}
|
||||
|
||||
const (
|
||||
PA0 = portA + 0
|
||||
@@ -221,6 +225,64 @@ func (p Pin) registerInterrupt() interrupt.Interrupt {
|
||||
return interrupt.Interrupt{}
|
||||
}
|
||||
|
||||
// -- SPI ----------------------------------------------------------------------
|
||||
|
||||
type SPI struct {
|
||||
Bus *stm32.SPI_Type
|
||||
AltFuncSelector uint8
|
||||
}
|
||||
|
||||
func (spi SPI) config8Bits() {
|
||||
// Set rx threshold to 8-bits, so RXNE flag is set for 1 byte
|
||||
// (common STM32 SPI implementation does 8-bit transfers only)
|
||||
spi.Bus.CR2.SetBits(stm32.SPI_CR2_FRXTH)
|
||||
}
|
||||
|
||||
func (spi SPI) configurePins(config SPIConfig) {
|
||||
config.SCK.ConfigureAltFunc(PinConfig{Mode: PinModeSPICLK}, spi.AltFuncSelector)
|
||||
config.SDO.ConfigureAltFunc(PinConfig{Mode: PinModeSPISDO}, spi.AltFuncSelector)
|
||||
config.SDI.ConfigureAltFunc(PinConfig{Mode: PinModeSPISDI}, spi.AltFuncSelector)
|
||||
}
|
||||
|
||||
func (spi SPI) getBaudRate(config SPIConfig) uint32 {
|
||||
var clock uint32
|
||||
|
||||
// We keep this switch and separate management of SPI Clocks
|
||||
// for future improvement of system/bus clocks and prescalers
|
||||
switch spi.Bus {
|
||||
case stm32.SPI1:
|
||||
clock = CPUFrequency()
|
||||
case stm32.SPI2, stm32.SPI3:
|
||||
clock = CPUFrequency()
|
||||
}
|
||||
|
||||
// limit requested frequency to bus frequency and min frequency (DIV256)
|
||||
freq := config.Frequency
|
||||
if min := clock / 256; freq < min {
|
||||
freq = min
|
||||
} else if freq > clock {
|
||||
freq = clock
|
||||
}
|
||||
|
||||
// calculate the exact clock divisor (freq=clock/div -> div=clock/freq).
|
||||
// truncation is fine, since it produces a less-than-or-equal divisor, and
|
||||
// thus a greater-than-or-equal frequency.
|
||||
// divisors only come in consecutive powers of 2, so we can use log2 (or,
|
||||
// equivalently, bits.Len - 1) to convert to respective enum value.
|
||||
div := bits.Len32(clock/freq) - 1
|
||||
|
||||
// but DIV1 (2^0) is not permitted, as the least divisor is DIV2 (2^1), so
|
||||
// subtract 1 from the log2 value, keeping a lower bound of 0
|
||||
if div < 0 {
|
||||
div = 0
|
||||
} else if div > 0 {
|
||||
div--
|
||||
}
|
||||
|
||||
// finally, shift the enumerated value into position for SPI CR1
|
||||
return uint32(div) << stm32.SPI_CR1_BR_Pos
|
||||
}
|
||||
|
||||
//---------- UART related code
|
||||
|
||||
// Configure the UART.
|
||||
|
||||
+2
-2
@@ -1,5 +1,5 @@
|
||||
//go:build !baremetal || (stm32 && !stm32f7x2 && !stm32l5x2 && !stm32wle5) || fe310 || k210 || atmega
|
||||
// +build !baremetal stm32,!stm32f7x2,!stm32l5x2,!stm32wle5 fe310 k210 atmega
|
||||
//go:build !baremetal || (stm32 && !stm32f7x2 && !stm32l5x2) || fe310 || k210 || atmega
|
||||
// +build !baremetal stm32,!stm32f7x2,!stm32l5x2 fe310 k210 atmega
|
||||
|
||||
package machine
|
||||
|
||||
|
||||
Reference in New Issue
Block a user