uart

package
v0.2.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 24, 2020 License: BSD-3-Clause Imports: 10 Imported by: 0

Documentation

Overview

Package uart provides access to the registers of UART peripheral. It also provides the driver that implements io.ReadWriter interface.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Baudrate

type Baudrate uint32
const (
	Baud1200   Baudrate = 0x0004F000 // Actual rate: 1205 baud.
	Baud2400   Baudrate = 0x0009D000 // Actual rate: 2396 baud.
	Baud4800   Baudrate = 0x0013B000 // Actual rate: 4808 baud.
	Baud9600   Baudrate = 0x00275000 // Actual rate: 9598 baud.
	Baud14400  Baudrate = 0x003B0000 // Actual rate: 14414 baud.
	Baud19200  Baudrate = 0x004EA000 // Actual rate: 19208 baud.
	Baud28800  Baudrate = 0x0075F000 // Actual rate: 28829 baud.
	Baud31250  Baudrate = 0x00800000
	Baud38400  Baudrate = 0x009D5000 // Actual rate: 38462 baud.
	Baud56000  Baudrate = 0x00E50000 // Actual rate: 55944 baud.
	Baud57600  Baudrate = 0x00EBF000 // Actual rate: 57762 baud.
	Baud76800  Baudrate = 0x013A9000 // Actual rate: 76923 baud.
	Baud115200 Baudrate = 0x01D7E000 // Actual rate: 115942 baud.
	Baud230400 Baudrate = 0x03AFB000 // Actual rate: 231884 baud.
	Baud250000 Baudrate = 0x04000000
	Baud460800 Baudrate = 0x075F7000 // Actual rate: 470588 baud.
	Baud921600 Baudrate = 0x0EBED000 // Actual rate: 941176 baud.
	Baud1M     Baudrate = 0x10000000
)

func Baud

func Baud(baud int) Baudrate

func (Baudrate) Baud

func (br Baudrate) Baud() int

type Config added in v0.1.2

type Config uint32
const (
	HWFC   Config = 0x01 << 0 // Hardware flow control
	PARITY Config = 0x07 << 1 // Parity
	STOP2  Config = 0x01 << 4 // Two stop bits
)

type Driver

type Driver struct {
	// contains filtered or unexported fields
}

Driver is an interrupt based driver for UART peripheral. It is optimized for error free links (it is fast and efficient but has some limitations when it comes to reporting Rx errors).

Reading from UART you may encounter errors detected by the hardware (there is no hardware error detection on writing). This driver treats all Rx hardware errors as asynchronous events but at least the ErrOverrun is in fact synchronous. So the Rx errors other than ErrTimeout are simply imformative about the connection quallity or the reading performance. You cannot determine which data has been affected (use other techniques to ensure data integrity.

Set the read timeout to ensure wake-up in case of missing data. The hardware may not detect some Rx errors or the error can be signaled before you try to read affected data because of the hardware and software buffering. This means that the reader can not rely on waking up in case of Rx error. Consider also that the remote party can reset unexpectedly and depending on the protocol used it can be quiet waiting for data request or initialization sequence.

The write operation can block only if the hardware flow control is enabled. In this case you can use write timeout to detect problems with the remote party or RTS/CTS signaling.

The driver supports one reading goroutine and one writing goroutine that both can work concurrently with the driver.

func NewDriver

func NewDriver(p *Periph) *Driver

NewDriver returns a new driver for p.

func (*Driver) Disable

func (d *Driver) Disable()

Disable disables UART peripheral.

func (*Driver) DisableRx

func (d *Driver) DisableRx() (rxbuf []byte)

DisableRx disables the UART receiver. The receive buffer is returned and no longer referenced by driver. You can use the STOPRX and STARTRX tasks directly if you want to temporary disable the receiver leaving the driver intact.

func (*Driver) Enable

func (d *Driver) Enable()

Enable enables UART peripheral.

func (*Driver) EnableRx

func (d *Driver) EnableRx(rxbuf []byte)

EnableRx enables the UART receiver. If rxbuf is not nil the Driver uses the provided slice to buffer received data. Othewrise it allocates a small buffer itself. At least 2-byte buffer is required, which is effectively one byte buffer because the other byte always remains unused for efficient checking of an empty state. You cannot rely on 6-byte hardware buffer as extension of the software buffer because for the performance reasons the ISR will not return until it has read all bytes from hardware. If the software buffer is full the ISR simply drops read bytes until there is no more data to read. EnableRx panics if the receiving is already enabled or rxbuf is too short.

func (*Driver) IRQ added in v0.1.2

func (d *Driver) IRQ() rtos.IRQ

IRQ returns the interrupt assigned to UART peripheral used by driver.

func (*Driver) ISR

func (d *Driver) ISR()

ISR handles UART interrupts. It supports the reading thread to run in parallel on another CPU.

func (*Driver) Len

func (d *Driver) Len() int

Len returns the number of bytes that are ready to read from Rx buffer.

func (*Driver) Periph added in v0.1.2

func (d *Driver) Periph() *Periph

func (*Driver) Read

func (d *Driver) Read(p []byte) (n int, err error)

Read reads up to len(p) bytes into p. It returns number of bytes read and an error if detected. Read blocks only if the internal buffer is empty (d.Len() > 0 ensure non-blocking read).

func (*Driver) ReadByte

func (d *Driver) ReadByte() (b byte, err error)

ReadByte reads one byte and returns error if detected. ReadByte blocks only if the internal buffer is empty (d.Len() > 0 ensure non-blocking read).

func (*Driver) SetBaudrate added in v0.1.2

func (d *Driver) SetBaudrate(br Baudrate)

SetBaudrate sets Tx and Rx baudrate.

func (*Driver) SetConfig added in v0.1.2

func (d *Driver) SetConfig(cfg Config)

SetConfig allows to configure UART peripheral to use hardware flow controll, add and check parity bit, and use two stop bits instead default one.

func (*Driver) SetReadTimeout added in v0.1.2

func (d *Driver) SetReadTimeout(timeout time.Duration)

SetReadTimeout sets the read timeout used by Read* functions.

func (*Driver) SetWriteTimeout added in v0.1.2

func (d *Driver) SetWriteTimeout(timeout time.Duration)

SetWriteTimeout sets the write timeout used by Write* functions.

func (*Driver) UsePin added in v0.1.2

func (d *Driver) UsePin(pin gpio.Pin, s Signal)

UsePin configurs the specified pin to be used as signal s.

func (*Driver) Write

func (d *Driver) Write(p []byte) (int, error)

Write sends bytes from p to the remote party. It return the number of bytes sent and error if detected. It does not provide any guarantee that the bytes sent were received by the remote party.

func (*Driver) WriteByte

func (d *Driver) WriteByte(b byte) (err error)

WriteByte sends one byte to the remote party and returns an error if detected// WriteByte can block if the hardware flow control is used. It does not provide any guarantee that the byte sent was received by the remote party.

func (*Driver) WriteString

func (d *Driver) WriteString(s string) (n int, err error)

WriteString works like Write.

type DriverError

type DriverError uint8
const (
	// ErrBufOverflow is returned if one or more received bytes has been dropped
	// because of the lack of free space in the driver's receive buffer.
	ErrBufOverflow DriverError = iota + 1

	// ErrTimeout is returned if timeout occured. It means that the read/write
	// operation has been interrupted. In case of write you can not determine
	// the exact number of bytes sent to the remote party.
	ErrTimeout
)

func (DriverError) Error

func (e DriverError) Error() string

Error implements error interface.

type ErrorBits added in v0.1.2

type ErrorBits uint8

ErrorBits is a bitfield that lists detected errors.

const (
	EOVERRUN ErrorBits = 1 << 0
	EPARITY  ErrorBits = 1 << 1
	EFRAMING ErrorBits = 1 << 2
	EBREAK   ErrorBits = 1 << 3

	EALL = EOVERRUN | EPARITY | EFRAMING | EBREAK
)

func (ErrorBits) Error added in v0.1.2

func (e ErrorBits) Error() string

type Event

type Event uint8
const (
	CTS    Event = 0  // CTS is activated (set low). nRF51v3+.
	NCTS   Event = 1  // CTS is deactivated (set high). nRF51v3+.
	RXDRDY Event = 2  // Data received in RXD.
	TXDRDY Event = 7  // Data sent from TXD.
	ERROR  Event = 9  // Error detected.
	RXTO   Event = 17 // Receiver timeout.
)

type Periph

type Periph struct {
	te.Regs
	// contains filtered or unexported fields
}

func UART added in v0.1.2

func UART(n int) *Periph

func (*Periph) ClearERRORSRC

func (p *Periph) ClearERRORSRC(e ErrorBits)

ClearERRORSRC clears specfied error flags.

func (*Periph) Event

func (p *Periph) Event(e Event) *te.Event

func (*Periph) LoadBAUDRATE

func (p *Periph) LoadBAUDRATE() Baudrate

LoadBAUDRATE returns configured baudrate.

func (*Periph) LoadCONFIG added in v0.1.2

func (p *Periph) LoadCONFIG() Config

LoadCONFIG returns configuration. nRF52.

func (*Periph) LoadENABLE

func (p *Periph) LoadENABLE() bool

LoadENABLE reports whether the UART peripheral is enabled.

func (*Periph) LoadERRORSRC

func (p *Periph) LoadERRORSRC() ErrorBits

LoadERRORSRC returns error flags.

func (*Periph) LoadPSEL

func (p *Periph) LoadPSEL(s Signal) (psel gpio.PSEL, en bool)

LoadPSEL returns configuration of signal s.

func (*Periph) LoadRXD

func (p *Periph) LoadRXD() byte

LoadRXD returns data received in previous transfers.

func (*Periph) LoadSHORTS

func (p *Periph) LoadSHORTS() Shorts

func (*Periph) StoreBAUDRATE

func (p *Periph) StoreBAUDRATE(br Baudrate)

StoreBAUDRATE stores baudrate.

func (*Periph) StoreCONFIG added in v0.1.2

func (p *Periph) StoreCONFIG(cfg Config)

StoreCONFIG stores baudrate. nRF52.

func (*Periph) StoreENABLE

func (p *Periph) StoreENABLE(en bool)

StoreENABLE enables or disables UART peripheral.

func (*Periph) StorePSEL

func (p *Periph) StorePSEL(s Signal, psel gpio.PSEL, en bool)

StorePSEL configures signal s.

func (*Periph) StoreSHORTS

func (p *Periph) StoreSHORTS(s Shorts)

func (*Periph) StoreTXD

func (p *Periph) StoreTXD(b byte)

StoreTX stores data to be transferred.

func (*Periph) Task

func (p *Periph) Task(t Task) *te.Task

type Shorts

type Shorts uint32
const (
	CTS_STARTRX Shorts = 1 << 3
	NCTS_STOPRX Shorts = 1 << 4
)

type Signal

type Signal byte
const (
	RTSn Signal = 0
	TXD  Signal = 1
	CTSn Signal = 2
	RXD  Signal = 3
)

type Task

type Task uint8
const (
	STARTRX Task = 0 // Start UART receiver.
	STOPRX  Task = 1 // Stop UART receiver.
	STARTTX Task = 2 // Start UART transmitter.
	STOPTX  Task = 3 // Stop UART transmitter.
	SUSPEND Task = 7 // Suspend UART. nRF52.
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL