Documentation
¶
Overview ¶
Package spi provides unified way to configure/use USART peripherals. It also provides a driver for synchronous I/O.
Index ¶
- Constants
- type Conf1
- type Conf2
- type Conf3
- type Config
- type Driver
- func (d *Driver) Disable()
- func (d *Driver) DisableRx()
- func (d *Driver) DisableTx()
- func (d *Driver) DiscardRx()
- func (d *Driver) Enable()
- func (d *Driver) EnableRx(bufLen int)
- func (d *Driver) EnableTx()
- func (d *Driver) Periph() *Periph
- func (d *Driver) Read(buf []byte) (n int, err error)
- func (d *Driver) ReadByte() (b byte, err error)
- func (d *Driver) RxDMA() dma.Channel
- func (d *Driver) RxISR()
- func (d *Driver) SetBaudrate(baudrate int)
- func (d *Driver) SetConfig(conf Config)
- func (d *Driver) SetReadTimeout(timeout time.Duration)
- func (d *Driver) SetWriteTimeout(timeout time.Duration)
- func (d *Driver) Setup(conf Config, baudrate int)
- func (d *Driver) TxDMA() dma.Channel
- func (d *Driver) TxDMAISR()
- func (d *Driver) UsePin(pin gpio.Pin, sig Signal)
- func (d *Driver) Write(p []byte) (int, error)
- func (d *Driver) WriteByte(b byte) error
- func (d *Driver) WriteString(s string) (n int, err error)
- type DriverError
- type Error
- type Event
- type Periph
- func (p *Periph) Baudrate() int
- func (p *Periph) Bus() bus.Bus
- func (p *Periph) Clear(ev Event, err Error)
- func (p *Periph) Conf1() Conf1
- func (p *Periph) Conf2() Conf2
- func (p *Periph) Conf3() Conf3
- func (p *Periph) DisableClock()
- func (p *Periph) DisableErrIRQ()
- func (p *Periph) DisableIRQ(e Event)
- func (p *Periph) EnableClock(lp bool)
- func (p *Periph) EnableErrIRQ()
- func (p *Periph) EnableIRQ(e Event)
- func (p *Periph) Load() uint32
- func (p *Periph) Reset()
- func (p *Periph) SetBaudrate(baud int)
- func (p *Periph) SetConf1(cfg Conf1)
- func (p *Periph) SetConf2(cfg Conf2)
- func (p *Periph) SetConf3(cfg Conf3)
- func (p *Periph) Status() (Event, Error)
- func (p *Periph) Store(word uint32)
- type Signal
Constants ¶
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 )
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 )
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 Driver ¶ added in v0.7.0
type Driver struct {
// contains filtered or unexported fields
}
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) EnableRx ¶ added in v0.7.0
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) 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 (*Driver) SetReadTimeout ¶ added in v0.12.0
SetReadTimeout sets the read timeout used by Read* functions.
func (*Driver) SetWriteTimeout ¶ added in v0.12.0
SetWriteTimeout sets the write timeout used by Write* functions.
func (*Driver) Setup ¶ added in v0.7.0
Setup enables clock source, resets UART, configures and enables it. You still need to enable Tx and/or Rx before use it.
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
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).
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.
type Periph ¶
type Periph struct {
// contains filtered or unexported fields
}
Periph represents USART peripheral.
func (*Periph) Clear ¶
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) 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
DisableIRQ disables generating interrupts by specified events.
func (*Periph) EnableClock ¶
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
EnableIRQ enables generating interrupts by specified events.
func (*Periph) SetBaudrate ¶ added in v0.7.0
SetBaudrate sets the UART speed sym/s.