usart

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2024 License: BSD-3-Clause Imports: 9 Imported by: 0

Documentation

Overview

Package spi provides unified way to configure/use USART peripherals. It also provides a driver for synchronous I/O.

Index

Constants

View Source
const (
	Word7b  = Config(W7b) // L4 or newer
	Word8b  = Config(0)
	Word9b  = Config(W9b)
	ParEven = Config(PCE)
	ParOdd  = Config(PCE | OPS)

	Stop0b5  = Config(S05) << 1
	Stop1b5  = Config(S15) << 1
	Stop2b   = Config(S2) << 1
	SwapRxTx = Config(Swap) << 1     // L4 or newer
	InvRx    = Config(RxInv) << 1    // L4 or newer
	InvTx    = Config(TxInv) << 1    // L4 or newer
	InvData  = Config(DataInv) << 1  // L4 or newer
	InvBits  = Config(MSBFirst) << 1 // L4 or newer
	AutoBR   = Config(ABREn) << 1    // L4 or newer

	IR         = Config(IREn) >> 1
	LowPowerIR = Config(IREn|IRLP) >> 1
	HalfDuplex = Config(HDSEL) >> 1
	SCNACK     = Config(NACK) >> 1
	SmartCard  = Config(SCEN) >> 1
	RTS        = Config(RTSE) >> 1
	CTS        = Config(CTSE) >> 1
)
View Source
const (
	Idle       = Event(1 << 4) // IDLE line detected
	RxNotEmpty = Event(1 << 5) // read data register not empty
	TxDone     = Event(1 << 6) // transmission complete
	TxEmpty    = Event(1 << 7) // transmit data register empty
	LINBreak   = Event(1 << 8) // LIN break detection flag
	CTST       = Event(1 << 9) // CTS toggle

	EvAll = Idle | RxNotEmpty | TxDone | TxEmpty | LINBreak | CTST
)
View Source
const (
	ErrParity  = Error(1 << 0) // Parity error.
	ErrFraming = Error(1 << 1) // Framing error.
	ErrNoise   = Error(1 << 2) // Noise error flag.
	ErrOverrun = Error(1 << 3) // Overrun error.

	ErrAll = ErrParity | ErrFraming | ErrNoise | ErrOverrun
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Conf1 added in v0.7.0

type Conf1 uint32
const (
	UE  Conf1 = ue  // UART enable
	RE  Conf1 = re  // receiver enable
	TE  Conf1 = te  // transmiter enable
	PCE Conf1 = pce // parity control enable (default even)
	OPS Conf1 = ps  // odd parity select
	W9b Conf1 = m0  // 9 bit data word instead of 8 bit
	W7b Conf1 = m1  // 7 bit data word instead of 8 bit
)

type Conf2 added in v0.7.0

type Conf2 uint32
const (
	S05      Conf2 = stop0b5  // use 0.5 stop bits insted of 1
	S2       Conf2 = stop2b   // use 2 stop bits instead of 1
	S15      Conf2 = stop1b5  // use 1.5 stop bits instead of 1
	Swap     Conf2 = swap     // swap Tx/Rx pins
	RxInv    Conf2 = rxinv    // invert Rx signal
	TxInv    Conf2 = txinv    // invert Tx signal
	DataInv  Conf2 = datainv  // invert data bits for Tx and Rx
	MSBFirst Conf2 = msbfirst // most significant bit first
	ABREn    Conf2 = abren    // automatic baud rate detection
)

type Conf3 added in v0.7.0

type Conf3 uint32
const (
	IREn   Conf3 = iren   // IrDA mode
	IRLP   Conf3 = irlp   // IrDA low-power
	HDSEL  Conf3 = hdsel  // half-duplex operation
	NACK   Conf3 = nack   // Smart Card auto-retry mode in case of NACK
	SCEN   Conf3 = scen   // Smart Card mode
	DMAR   Conf3 = dmar   // RxNotEmpty event generates DMA request
	DMAT   Conf3 = dmat   // TxEmpty event generates DMA request
	RTSE   Conf3 = rtse   // hardware RTS signal
	CTSE   Conf3 = ctse   // hardware CTS signal
	OneBit Conf3 = onebit // one-bit sampling (noise detection disabled)
)

type Config added in v0.7.0

type Config uint32

type Driver added in v0.7.0

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

func NewDriver added in v0.7.0

func NewDriver(p *Periph, rxdma, txdma dma.Channel) *Driver

NewDriver returns a new driver for p.

func (*Driver) Disable added in v0.7.0

func (d *Driver) Disable()

Disable disables UART.

func (*Driver) DisableRx added in v0.7.0

func (d *Driver) DisableRx()

DisableRx disables USART receiver and frees memory allocated for the internal ring buffer.

func (*Driver) DisableTx added in v0.7.0

func (d *Driver) DisableTx()

DisableTx disables Tx part of the USART peripheral.

func (*Driver) DiscardRx added in v0.12.0

func (d *Driver) DiscardRx()

DiscardRx discards all rceived data.

func (*Driver) Enable added in v0.7.0

func (d *Driver) Enable()

Enable enables UART.

func (*Driver) EnableRx added in v0.7.0

func (d *Driver) EnableRx(bufLen int)

EnableRx enables the UART receive. It allocates an internal ring buffer of bufLen size. In most cases bufLen = 64 is good choise (minimum is 2).

EnableRx setups Rx DMA channel in circular mode and enables it to continuous reception of data. Driver assumes that it has exclusive access to the underlying USART peripheral and Rx DMA channel between EnableRx and DisableRx.

The Rx algorithm and the DMA configuration is optimized for speed. The DMA is allowed to copy any received data to the internal ring buffer. If there is no room for new data the DMA overwrites the oldest. The detection of buffer overflow is weak (some cases may not be detected).

You can not rely on the error reporting at all. The detected UART errors are asynchronous with the received data and can be used only as an indicator of poor connection quality. The ErrBufOverflow indicates that the rxbuf is too small or the data processing is too slow. You cannot determine which data has been affected (use other techniques to ensure data integrity).

As the DMA reads any received data it does not make much sense to enable hardware RTS signaling unless the DMA is very busy.

func (*Driver) EnableTx added in v0.7.0

func (d *Driver) EnableTx()

EnableTx enables Tx part of the USART peripheral and setups Tx DMA channel. Driver assumes that it has exclusive access to the underlying USART peripheral and Tx DMA channel between EnableTx and DisableTx.

func (*Driver) Periph added in v0.7.0

func (d *Driver) Periph() *Periph

func (*Driver) Read added in v0.7.0

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

Read implements io.Reader interface.

func (*Driver) ReadByte added in v0.12.0

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

func (*Driver) RxDMA added in v0.7.0

func (d *Driver) RxDMA() dma.Channel

func (*Driver) RxISR added in v0.7.0

func (d *Driver) RxISR()

RxISR is an interrupt handler for the UART interrupt. The UART interrupt is only need for reception. No other interrupt is need for Rx-only use case.

func (*Driver) SetBaudrate added in v0.7.0

func (d *Driver) SetBaudrate(baudrate int)

func (*Driver) SetConfig added in v0.7.0

func (d *Driver) SetConfig(conf Config)

SetConfig configures UART.

func (*Driver) SetReadTimeout added in v0.12.0

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

SetReadTimeout sets the read timeout used by Read* functions.

func (*Driver) SetWriteTimeout added in v0.12.0

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

SetWriteTimeout sets the write timeout used by Write* functions.

func (*Driver) Setup added in v0.7.0

func (d *Driver) Setup(conf Config, baudrate int)

Setup enables clock source, resets UART, configures and enables it. You still need to enable Tx and/or Rx before use it.

func (*Driver) TxDMA added in v0.7.0

func (d *Driver) TxDMA() dma.Channel

func (*Driver) TxDMAISR added in v0.7.0

func (d *Driver) TxDMAISR()

TxDMAISR is an interrupt handler for Tx DMA channel IRQ. The DMA interrupt is only needed to receive data. No other interrupt is need for Tx-only use case.

func (*Driver) UsePin added in v0.7.0

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

UsePin is a helper function that can be used to configure GPIO pins as required by USART peripheral. Only certain pins can be used (see datasheet).

func (*Driver) Write added in v0.7.0

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

Write implements io.Writer interface.

func (*Driver) WriteByte added in v0.12.0

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

WriteByte implements io.ByteWriter interface.

func (*Driver) WriteString added in v0.7.0

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

WriteString implements io.StringWriter interface.

type DriverError added in v0.7.0

type DriverError uint8
const (
	ErrBufOverflow DriverError = iota + 1
	ErrTimeout
)

func (DriverError) Error added in v0.7.0

func (e DriverError) Error() string

type Error

type Error uint8

Error is a bitmask that describes errors that can be detected by USART hardware when receiving data.

func (Error) Error

func (e Error) Error() string

type Event

type Event uint16

Event is a bitmask that describes events in USART peripheral.

type Periph

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

Periph represents USART peripheral.

func (*Periph) Baudrate added in v0.7.0

func (p *Periph) Baudrate() int

Baudrate returns the current UART speed sym/s.

func (*Periph) Bus

func (p *Periph) Bus() bus.Bus

Bus returns a bus to which p is connected to.

func (*Periph) Clear

func (p *Periph) Clear(ev Event, err Error)

Clear clears specified events and errors. For MCUs that have no USART_ICR register (older than L4, F7 series) only RxNotEmpty, TxDone, LINBreak and CTS events can be cleared this way. Other events can be cleared only by specific sequence of reading status register and read or write data register.

func (*Periph) Conf1 added in v0.7.0

func (p *Periph) Conf1() Conf1

func (*Periph) Conf2 added in v0.7.0

func (p *Periph) Conf2() Conf2

func (*Periph) Conf3 added in v0.7.0

func (p *Periph) Conf3() Conf3

func (*Periph) DisableClock

func (p *Periph) DisableClock()

DisableClock disables clock for p.

func (*Periph) DisableErrIRQ added in v0.7.0

func (p *Periph) DisableErrIRQ()

DisableErrIRQ disables generating of IRQ by ErrNoise, ErrOverrun, ErrFraming errors when DMA is used to handle incoming data.

func (*Periph) DisableIRQ added in v0.7.0

func (p *Periph) DisableIRQ(e Event)

DisableIRQ disables generating interrupts by specified events.

func (*Periph) EnableClock

func (p *Periph) EnableClock(lp bool)

EnableClock enables clock for p. lp determines whether the clock remains on in low power (sleep) mode.

func (*Periph) EnableErrIRQ added in v0.7.0

func (p *Periph) EnableErrIRQ()

EnableErrIRQ enables generating interrupts by ErrNoise, ErrOverrun, ErrFraming errors when DMA is used to handle incoming data.

func (*Periph) EnableIRQ added in v0.7.0

func (p *Periph) EnableIRQ(e Event)

EnableIRQ enables generating interrupts by specified events.

func (*Periph) Load added in v0.7.0

func (p *Periph) Load() uint32

Load loads a word from Rx data register.

func (*Periph) Reset

func (p *Periph) Reset()

Reset resets p.

func (*Periph) SetBaudrate added in v0.7.0

func (p *Periph) SetBaudrate(baud int)

SetBaudrate sets the UART speed sym/s.

func (*Periph) SetConf1 added in v0.7.0

func (p *Periph) SetConf1(cfg Conf1)

func (*Periph) SetConf2 added in v0.7.0

func (p *Periph) SetConf2(cfg Conf2)

func (*Periph) SetConf3 added in v0.7.0

func (p *Periph) SetConf3(cfg Conf3)

func (*Periph) Status

func (p *Periph) Status() (Event, Error)

Status return the events and errors which have occurred since the last Clear.

func (*Periph) Store added in v0.7.0

func (p *Periph) Store(word uint32)

Store stores a word in Tx data register.

type Signal added in v0.7.0

type Signal uint8
const (
	RTSn Signal = iota
	TXD
	CTSn
	RXD
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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