stm32l0: use unified UART logic
Этот коммит содержится в:
родитель
b0b84c48ec
коммит
ce8ad3650a
3 изменённых файлов: 9 добавлений и 74 удалений
|
@ -1,4 +1,4 @@
|
||||||
// +build stm32,!stm32l0
|
// +build stm32
|
||||||
|
|
||||||
package machine
|
package machine
|
||||||
|
|
||||||
|
|
|
@ -6,7 +6,6 @@ package machine
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"device/stm32"
|
"device/stm32"
|
||||||
"runtime/interrupt"
|
|
||||||
"unsafe"
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -189,14 +188,6 @@ func enableAltFuncClock(bus unsafe.Pointer) {
|
||||||
|
|
||||||
//---------- UART related types and code
|
//---------- UART related types and code
|
||||||
|
|
||||||
// UART representation
|
|
||||||
type UART struct {
|
|
||||||
Buffer *RingBuffer
|
|
||||||
Bus *stm32.USART_Type
|
|
||||||
Interrupt interrupt.Interrupt
|
|
||||||
AltFuncSelector uint8
|
|
||||||
}
|
|
||||||
|
|
||||||
// Configure the UART.
|
// Configure the UART.
|
||||||
func (uart UART) configurePins(config UARTConfig) {
|
func (uart UART) configurePins(config UARTConfig) {
|
||||||
// enable the alternate functions on the TX and RX pins
|
// enable the alternate functions on the TX and RX pins
|
||||||
|
@ -222,6 +213,14 @@ func (uart UART) getBaudRateDivisor(baudRate uint32) uint32 {
|
||||||
return rate
|
return rate
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Register names vary by ST processor, these are for STM L0 family
|
||||||
|
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 related types and code
|
||||||
|
|
||||||
// SPI on the STM32Fxxx using MODER / alternate function pins
|
// SPI on the STM32Fxxx using MODER / alternate function pins
|
||||||
|
|
|
@ -1,64 +0,0 @@
|
||||||
// +build stm32,stm32l0
|
|
||||||
|
|
||||||
package machine
|
|
||||||
|
|
||||||
// Peripheral abstraction layer for UARTs on the stm32 family.
|
|
||||||
|
|
||||||
import (
|
|
||||||
"device/stm32"
|
|
||||||
"runtime/interrupt"
|
|
||||||
"unsafe"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Configure the UART.
|
|
||||||
func (uart UART) Configure(config UARTConfig) {
|
|
||||||
// Default baud rate to 115200.
|
|
||||||
if config.BaudRate == 0 {
|
|
||||||
config.BaudRate = 115200
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set the GPIO pins to defaults if they're not set
|
|
||||||
if config.TX == 0 && config.RX == 0 {
|
|
||||||
config.TX = UART_TX_PIN
|
|
||||||
config.RX = UART_RX_PIN
|
|
||||||
}
|
|
||||||
|
|
||||||
// Enable USART clock
|
|
||||||
enableAltFuncClock(unsafe.Pointer(uart.Bus))
|
|
||||||
|
|
||||||
uart.configurePins(config)
|
|
||||||
|
|
||||||
// Set baud rate
|
|
||||||
uart.SetBaudRate(config.BaudRate)
|
|
||||||
|
|
||||||
// Enable USART port, tx, rx and rx interrupts
|
|
||||||
uart.Bus.CR1.Set(stm32.USART_CR1_TE | stm32.USART_CR1_RE | stm32.USART_CR1_RXNEIE | stm32.USART_CR1_UE)
|
|
||||||
|
|
||||||
// Enable RX IRQ
|
|
||||||
uart.Interrupt.SetPriority(0xc0)
|
|
||||||
uart.Interrupt.Enable()
|
|
||||||
}
|
|
||||||
|
|
||||||
// handleInterrupt should be called from the appropriate interrupt handler for
|
|
||||||
// this UART instance.
|
|
||||||
func (uart *UART) handleInterrupt(interrupt.Interrupt) {
|
|
||||||
uart.Receive(byte((uart.Bus.RDR.Get() & 0xFF)))
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetBaudRate sets the communication speed for the UART. Defer to chip-specific
|
|
||||||
// routines for calculation
|
|
||||||
func (uart UART) SetBaudRate(br uint32) {
|
|
||||||
divider := uart.getBaudRateDivisor(br)
|
|
||||||
uart.Bus.BRR.Set(divider)
|
|
||||||
}
|
|
||||||
|
|
||||||
// WriteByte writes a byte of data to the UART.
|
|
||||||
func (uart UART) WriteByte(c byte) error {
|
|
||||||
|
|
||||||
uart.Bus.TDR.Set(uint32(c))
|
|
||||||
|
|
||||||
for !uart.Bus.ISR.HasBits(stm32.USART_ISR_TXE) {
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
Загрузка…
Создание таблицы
Сослаться в новой задаче