mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-11 06:23:39 +00:00
machine: make UART objects pointer receivers
This means that machine.UART0, machine.UART1, etc are of type *machine.UART, not machine.UART. This makes them easier to pass around and avoids surprises when they are passed around by value while they should be passed around by reference. There is a small code size impact in some cases, but it is relatively minor.
This commit is contained in:
committed by
Ron Evans
parent
7c949ad386
commit
aa5b8d0df7
@@ -137,12 +137,13 @@ type UART struct {
|
||||
|
||||
// UART
|
||||
var (
|
||||
// NRF_UART0 is the hardware UART on the NRF SoC.
|
||||
NRF_UART0 = UART{Buffer: NewRingBuffer()}
|
||||
// UART0 is the hardware UART on the NRF SoC.
|
||||
_NRF_UART0 = UART{Buffer: NewRingBuffer()}
|
||||
NRF_UART0 = &_NRF_UART0
|
||||
)
|
||||
|
||||
// Configure the UART.
|
||||
func (uart UART) Configure(config UARTConfig) {
|
||||
func (uart *UART) Configure(config UARTConfig) {
|
||||
// Default baud rate to 115200.
|
||||
if config.BaudRate == 0 {
|
||||
config.BaudRate = 115200
|
||||
@@ -164,13 +165,13 @@ func (uart UART) Configure(config UARTConfig) {
|
||||
nrf.UART0.INTENSET.Set(nrf.UART_INTENSET_RXDRDY_Msk)
|
||||
|
||||
// Enable RX IRQ.
|
||||
intr := interrupt.New(nrf.IRQ_UART0, NRF_UART0.handleInterrupt)
|
||||
intr := interrupt.New(nrf.IRQ_UART0, _NRF_UART0.handleInterrupt)
|
||||
intr.SetPriority(0xc0) // low priority
|
||||
intr.Enable()
|
||||
}
|
||||
|
||||
// SetBaudRate sets the communication speed for the UART.
|
||||
func (uart UART) SetBaudRate(br uint32) {
|
||||
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
|
||||
@@ -185,7 +186,7 @@ func (uart UART) SetBaudRate(br uint32) {
|
||||
}
|
||||
|
||||
// WriteByte writes a byte of data to the UART.
|
||||
func (uart UART) WriteByte(c byte) error {
|
||||
func (uart *UART) WriteByte(c byte) error {
|
||||
nrf.UART0.EVENTS_TXDRDY.Set(0)
|
||||
nrf.UART0.TXD.Set(uint32(c))
|
||||
for nrf.UART0.EVENTS_TXDRDY.Get() == 0 {
|
||||
|
||||
Reference in New Issue
Block a user