add basic UART handler

This commit is contained in:
ardnew
2020-08-29 14:32:24 -05:00
committed by Ron Evans
parent 19d5e05e37
commit a9a6d0ee63
3 changed files with 79 additions and 25 deletions
+29 -1
View File
@@ -2,6 +2,11 @@
package machine
import (
"device/stm32"
"runtime/interrupt"
)
const (
NUM_DIGITAL_IO_PINS = 39
NUM_ANALOG_IO_PINS = 7
@@ -113,7 +118,30 @@ const (
UART_TX_PIN = UART0_TX_PIN //
)
func initUART() {}
var (
UART1 = UART{
Buffer: NewRingBuffer(),
Bus: stm32.USART3,
AltFuncSelector: stm32.AF7_USART1_2_3,
}
UART2 = UART{
Buffer: NewRingBuffer(),
Bus: stm32.USART6,
AltFuncSelector: stm32.AF8_USART4_5_6,
}
UART3 = UART{
Buffer: NewRingBuffer(),
Bus: stm32.USART1,
AltFuncSelector: stm32.AF7_USART1_2_3,
}
UART0 = UART1
)
func initUART() {
UART1.Interrupt = interrupt.New(stm32.IRQ_USART3, UART1.handleInterrupt)
UART2.Interrupt = interrupt.New(stm32.IRQ_USART6, UART2.handleInterrupt)
UART3.Interrupt = interrupt.New(stm32.IRQ_USART1, UART3.handleInterrupt)
}
// -- SPI ----------------------------------------------------------------------
+16 -2
View File
@@ -22,8 +22,22 @@ type UART struct {
AltFuncSelector stm32.AltFunc
}
func (uart UART) configurePins(config UARTConfig) {}
func (uart UART) getBaudRateDivisor(baudRate uint32) uint32 { return 0 }
func (uart UART) configurePins(config UARTConfig) {
// enable the alternate functions on the TX and RX pins
config.TX.ConfigureAltFunc(PinConfig{Mode: PinModeUARTTX}, uart.AltFuncSelector)
config.RX.ConfigureAltFunc(PinConfig{Mode: PinModeUARTRX}, uart.AltFuncSelector)
}
func (uart UART) getBaudRateDivisor(baudRate uint32) uint32 {
var clock uint32
switch uart.Bus {
case stm32.USART1, stm32.USART6:
clock = CPUFrequency() / 2 // APB2 Frequency
case stm32.USART2, stm32.USART3, stm32.UART4, stm32.UART5:
clock = CPUFrequency() / 4 // APB1 Frequency
}
return clock / baudRate
}
// -- SPI ----------------------------------------------------------------------