mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-11 06:23:39 +00:00
nrf: implement UART interface
Signed-off-by: Ron Evans <ron@hybridgroup.com>
This commit is contained in:
committed by
Ayke van Laethem
parent
3540fd9638
commit
ef2ac09561
@@ -3,6 +3,7 @@
|
||||
package machine
|
||||
|
||||
import (
|
||||
"device/arm"
|
||||
"device/nrf"
|
||||
)
|
||||
|
||||
@@ -35,3 +36,63 @@ func (p GPIO) Set(high bool) {
|
||||
func (p GPIO) Get() bool {
|
||||
return (nrf.P0.IN>>p.Pin)&1 != 0
|
||||
}
|
||||
|
||||
// UART
|
||||
var (
|
||||
// UART0 is the hardware serial port on the NRF.
|
||||
UART0 = &UART{}
|
||||
)
|
||||
|
||||
// Configure the UART.
|
||||
func (uart UART) Configure(config UARTConfig) {
|
||||
// Default baud rate to 115200.
|
||||
if config.BaudRate == 0 {
|
||||
config.BaudRate = 115200
|
||||
}
|
||||
|
||||
uart.SetBaudRate(config.BaudRate)
|
||||
|
||||
// Set TX and RX pins from board.
|
||||
nrf.UART0.PSELTXD = UART_TX_PIN
|
||||
nrf.UART0.PSELRXD = UART_RX_PIN
|
||||
|
||||
nrf.UART0.ENABLE = nrf.UART_ENABLE_ENABLE_Enabled
|
||||
nrf.UART0.TASKS_STARTTX = 1
|
||||
nrf.UART0.TASKS_STARTRX = 1
|
||||
nrf.UART0.INTENSET = nrf.UART_INTENSET_RXDRDY_Msk
|
||||
|
||||
// Enable RX IRQ.
|
||||
arm.EnableIRQ(nrf.IRQ_UARTE0_UART0)
|
||||
}
|
||||
|
||||
// SetBaudRate sets the communication speed for the UART.
|
||||
func (uart UART) SetBaudRate(br uint32) {
|
||||
// Magic: calculate 'baudrate' register from the input number.
|
||||
// Every value listed in the datasheet will be converted to the
|
||||
// correct register value, except for 192600. I suspect the value
|
||||
// listed in the nrf52 datasheet (0x0EBED000) is incorrectly rounded
|
||||
// and should be 0x0EBEE000, as the nrf51 datasheet lists the
|
||||
// nonrounded value 0x0EBEDFA4.
|
||||
// Some background:
|
||||
// https://devzone.nordicsemi.com/f/nordic-q-a/391/uart-baudrate-register-values/2046#2046
|
||||
rate := uint32((uint64(br/400)*uint64(400*0xffffffff/16000000) + 0x800) & 0xffffff000)
|
||||
|
||||
nrf.UART0.BAUDRATE = nrf.RegValue(rate)
|
||||
}
|
||||
|
||||
// WriteByte writes a byte of data to the UART.
|
||||
func (uart UART) WriteByte(c byte) error {
|
||||
nrf.UART0.EVENTS_TXDRDY = 0
|
||||
nrf.UART0.TXD = nrf.RegValue(c)
|
||||
for nrf.UART0.EVENTS_TXDRDY == 0 {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
//go:export UARTE0_UART0_IRQHandler
|
||||
func handleUART0() {
|
||||
if nrf.UART0.EVENTS_RXDRDY != 0 {
|
||||
bufferPut(byte(nrf.UART0.RXD))
|
||||
nrf.UART0.EVENTS_RXDRDY = 0x0
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user