|
| 1 | +//go:build py32 |
| 2 | + |
| 3 | +package machine |
| 4 | + |
| 5 | +import ( |
| 6 | + "device/py32" |
| 7 | + "runtime/interrupt" |
| 8 | +) |
| 9 | + |
| 10 | +// Remember the clock used for baud rate calculations so Configure() can be |
| 11 | +// called without explicitly passing the clock. |
| 12 | +var py32UARTClockHz uint32 = 24_000_000 |
| 13 | + |
| 14 | +// UART implements a minimal USART1 driver for PY32 parts. |
| 15 | +type UART struct { |
| 16 | + Bus *py32.USART_Type |
| 17 | + Buffer *RingBuffer |
| 18 | + irq interrupt.Interrupt |
| 19 | +} |
| 20 | + |
| 21 | +var DefaultUART = &UART{Bus: py32.USART1, Buffer: NewRingBuffer()} |
| 22 | + |
| 23 | +// ConfigureWithClock initializes the UART using the provided peripheral clock |
| 24 | +// frequency (in Hz). This avoids assuming a fixed MCU clock. |
| 25 | +func (uart *UART) ConfigureWithClock(config UARTConfig, clockHz uint32) error { |
| 26 | + if config.BaudRate == 0 { |
| 27 | + config.BaudRate = 115200 |
| 28 | + } |
| 29 | + |
| 30 | + // Configure default pins if they weren't provided. |
| 31 | + if config.TX == 0 { |
| 32 | + ConfigureUARTPin(DEFAULT_UART_TX_PIN, DEFAULT_UART_TX_PIN_AF) |
| 33 | + } |
| 34 | + |
| 35 | + if config.RX == 0 { |
| 36 | + ConfigureUARTPin(DEFAULT_UART_RX_PIN, DEFAULT_UART_RX_PIN_AF) |
| 37 | + } |
| 38 | + |
| 39 | + // Enable peripheral clock. |
| 40 | + py32.RCC.APBENR2.SetBits(py32.RCC_APBENR2_USART1EN) |
| 41 | + |
| 42 | + // Reset control registers to a known state. |
| 43 | + uart.Bus.CR1.Set(0) |
| 44 | + uart.Bus.CR2.Set(0) |
| 45 | + uart.Bus.CR3.Set(0) |
| 46 | + |
| 47 | + // Oversampling by 16: BRR expects fck/baud. |
| 48 | + divider := (clockHz + (config.BaudRate / 2)) / config.BaudRate |
| 49 | + uart.Bus.BRR.Set(divider) |
| 50 | + |
| 51 | + // Enable transmitter, receiver, RX interrupt, and the peripheral. |
| 52 | + uart.Bus.CR1.Set(py32.USART_CR1_TE | py32.USART_CR1_RE | py32.USART_CR1_RXNEIE | py32.USART_CR1_UE) |
| 53 | + |
| 54 | + // Hook interrupt. |
| 55 | + uart.irq = interrupt.New(py32.IRQ_USART1, handleUartInterrupt) |
| 56 | + uart.irq.SetPriority(0xc0) |
| 57 | + uart.irq.Enable() |
| 58 | + |
| 59 | + return nil |
| 60 | +} |
| 61 | + |
| 62 | +// Configure uses the last stored clock (defaulting to 24 MHz). Call |
| 63 | +// ConfigureWithClock for explicit control. |
| 64 | +func (uart *UART) Configure(config UARTConfig) error { |
| 65 | + return uart.ConfigureWithClock(config, py32UARTClockHz) |
| 66 | +} |
| 67 | + |
| 68 | +// InitSerialWithClock configures the default Serial using the supplied |
| 69 | +// peripheral clock frequency. |
| 70 | +func InitSerialWithClock(clockHz uint32) { |
| 71 | + py32UARTClockHz = clockHz |
| 72 | + //Serial.ConfigureWithClock(UARTConfig{}, clockHz) |
| 73 | +} |
| 74 | + |
| 75 | +// Configure pin for use by UART |
| 76 | +func ConfigureUARTPin(pin Pin, af uint8) { |
| 77 | + pin.enableClock() |
| 78 | + port, n := pin.getPort() |
| 79 | + pos := (n % 16) * 2 |
| 80 | + |
| 81 | + // Alternate function mode is encoded as 0b10. |
| 82 | + port.MODER.ReplaceBits(2, gpioModeMask, pos) |
| 83 | + port.PUPDR.ReplaceBits(gpioPullUp, gpioPullMask, pos) |
| 84 | + port.OSPEEDR.ReplaceBits(gpioOutputSpeedHigh, gpioOutputSpeedMask, pos) |
| 85 | + pin.SetAltFunc(af) |
| 86 | +} |
| 87 | + |
| 88 | +func handleUartInterrupt(interrupt.Interrupt) { |
| 89 | + uart := DefaultUART |
| 90 | + data := uint8(uart.Bus.DR.Get()) |
| 91 | + uart.Receive(data) |
| 92 | +} |
| 93 | + |
| 94 | +func (uart *UART) writeByte(c byte) error { |
| 95 | + for uart.Bus.SR.Get()&py32.USART_SR_TXE == 0 { |
| 96 | + } |
| 97 | + uart.Bus.DR.Set(uint32(c)) |
| 98 | + return nil |
| 99 | +} |
| 100 | + |
| 101 | +func (uart *UART) flush() { |
| 102 | + for uart.Bus.SR.Get()&py32.USART_SR_TC == 0 { |
| 103 | + } |
| 104 | +} |
0 commit comments