spi

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2022 License: BSD-3-Clause Imports: 10 Imported by: 0

Documentation

Overview

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

Index

Constants

View Source
const (
	CPHA0  = Config(0)    // Sample on leading edge.
	CPHA1  = Config(cpha) // Sample on trailing edge.
	CPOL0  = Config(0)    // Clock idle state is 0.
	CPOL1  = Config(cpol) // Clock idle state is 1.
	Slave  = Config(0)    // Slave mode.
	Master = Config(mstr) // Master mode.

	BR2   = Config(0)        // Baud rate = PCLK/2
	BR4   = Config(1 << brn) // Baud rate = PCLK/4.
	BR8   = Config(2 << brn) // Baud rate = PCLK/8.
	BR16  = Config(3 << brn) // Baud rate = PCLK/16.
	BR32  = Config(4 << brn) // Baud rate = PCLK/32.
	BR64  = Config(5 << brn) // Baud rate = PCLK/64.
	BR128 = Config(6 << brn) // Baud rate = PCLK/128.
	BR256 = Config(7 << brn) // Baud rate = PCLK/256.

	MSBF = Config(0)        // Most significant bit first.
	LSBF = Config(lsbfirst) // Least significant bit first.

	HardSS = Config(0)   // Hardware slave select.
	SoftSS = Config(ssm) // Software slave select (use ISSLow, ISSHigh).

	ISSLow  = Config(0)   // Set NSS internally to low (used with SoftSS).
	ISSHigh = Config(ssi) // Set NSS internally to high (used with SoftSS).

	SSInp = Config(0)          // Set NSS as input (used with HardSS).
	SSOut = Config(ssoe << 16) // set NSS as output (used with HardSS).

	ThreeWire = Config(0)        // Three-wire mode (SCK, MOSI, MISO).
	TwoWire   = Config(bidimode) // Two-wire mode (SCK, MOSI/MISO).

	TxRx   = Config(0)      // Three-wire transimt and receive.
	RxOnly = Config(rxonly) // Three-wire receive only.

	Rx = Config(0)      // Two-wire receive-only.
	Tx = Config(bidioe) // Two-wire transmit-only.

)
View Source
const (
	Err        = Event(1)         // Some hardware error occurs.
	RxNotEmpty = Event(rxne) << 1 // Receive buffer not empty.
	TxEmpty    = Event(txe) << 1  // Transmit buffer empty.
	Busy       = Event(bsy) << 1  // Periph is busy (not a real event).

)
View Source
const (
	ErrCRC     = Error(crcerr >> 3)
	ErrMode    = Error(modf >> 3)
	ErrOverrun = Error(ovr >> 3)
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Config added in v0.2.1

type Config uint32

type Driver

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

Driver implements DMA based driver to an SPI peripheral working in master mode.

func NewDriver

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

NewDriver provides convenient way to create heap allocated Driver struct.

func (*Driver) DMAISR

func (d *Driver) DMAISR(ch dma.Channel)

func (*Driver) Disable added in v0.2.1

func (d *Driver) Disable()

Disable waits for the peripheral idle state and calls d.Periph().Disable().

func (*Driver) Enable added in v0.2.1

func (d *Driver) Enable()

Enable calls d.Periph().Enable().

func (*Driver) Err

func (d *Driver) Err(clear bool) error

Err returns the saved error and clears it if the clear is true. If an error occurs during any command it is saved and the subsequent commands are ignored until the error is cleared.

func (*Driver) ISR

func (d *Driver) ISR()

func (*Driver) Periph

func (d *Driver) Periph() *Periph

Periph returns the SPI peripheral used by Driver.

func (*Driver) RxDMA

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

func (*Driver) SetBaudrate added in v0.2.1

func (d *Driver) SetBaudrate(baudrate int)

SetBaudrate calls d.Periph().SetBaudrate(baudrate).

func (*Driver) SetWordSize added in v0.2.1

func (d *Driver) SetWordSize(size int)

SetWordSize calls d.Periph().SetWordSize(size).

func (*Driver) Setup added in v0.2.1

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

Setup enables clock source, resets peripheral, then calls d.Periph().SetConfig(conf, baudrate) to configure it.

func (*Driver) TwoWireSetRx added in v0.2.1

func (d *Driver) TwoWireSetRx()

TwoWireSetRx can be used in two-wire mode to set data direction to recevie.

func (*Driver) TwoWireSetTx added in v0.2.1

func (d *Driver) TwoWireSetTx()

TwoWireSetRx can be used in two-wire mode to set data direction to transimit.

func (*Driver) TxDMA

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

func (*Driver) UsePinMaster added in v0.7.1

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

UsePinMaster is a helper function that can be used to configure GPIO pins as required by SPI master device. Only certain pins can be used (see datasheet).

func (*Driver) UsePinSlave added in v0.7.1

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

UsePinSlave is a helper function that can be used to configure GPIO pins as required by SPI slave device. Only certain pins can be used (see datasheet).

func (*Driver) WriteByteN added in v0.9.0

func (d *Driver) WriteByteN(b byte, n int)

func (*Driver) WriteRead

func (d *Driver) WriteRead(out, in []byte) int

func (*Driver) WriteRead16

func (d *Driver) WriteRead16(out, in []uint16) int

func (*Driver) WriteReadByte

func (d *Driver) WriteReadByte(b byte) byte

WriteReadByte writes and reads byte.

func (*Driver) WriteReadWord16

func (d *Driver) WriteReadWord16(w uint16) uint16

WriteReadWord16 writes and reads 16-bit word.

func (*Driver) WriteStringRead

func (d *Driver) WriteStringRead(out string, in []byte) int

func (*Driver) WriteWord16N added in v0.9.0

func (d *Driver) WriteWord16N(w uint16, n int)

type Error

type Error uint8

Error is a bitfield that encodes possible peripheral errors.

func (Error) Error

func (e Error) Error() string

type Event

type Event uint16

Event is a bitfield that encodes possible peripheral events.

type Periph

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

Periph represents SPI peripheral.

func (*Periph) BR

func (p *Periph) BR(baudrate int) Config

BR calculates the baud rate bits of configuration. BR guarantees that the returned bits corresponds to the value closest to but not greater than baudrate.

func (*Periph) Baudrate

func (p *Periph) Baudrate() int

Baudrate returns the currently configured baudrate.

func (*Periph) Bus

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

Bus returns a bus to which p is connected to.

func (*Periph) Config added in v0.2.1

func (p *Periph) Config() Config

Config returns the current p's configuration.

func (*Periph) Disable

func (p *Periph) Disable()

Disable disables p.

func (*Periph) DisableClock

func (p *Periph) DisableClock()

DisableClock disables clock for p.

func (*Periph) DisableDMA

func (p *Periph) DisableDMA(e Event)

DisableDMA disables generating of DMA requests by events e.

func (*Periph) DisableIRQ

func (p *Periph) DisableIRQ(e Event)

DisableIRQ disables generating of IRQ by events e.

func (*Periph) EditConfig added in v0.2.3

func (p *Periph) EditConfig(from, to Config)

EditConfig changes configuration.

func (*Periph) Enable

func (p *Periph) Enable()

Enable enables p.

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) EnableDMA

func (p *Periph) EnableDMA(e Event)

EnableDMA enables generating of DMA requests by events e.

func (*Periph) EnableIRQ

func (p *Periph) EnableIRQ(e Event)

EnableIRQ enables generating of IRQ by events e.

func (*Periph) Enabled

func (p *Periph) Enabled() bool

Enabled reports whether p is enabled.

func (*Periph) LoadByte

func (p *Periph) LoadByte() byte

LoadByte loads a byte from the data register. Use it only when 8-bit frame is configured.

func (*Periph) LoadWord16

func (p *Periph) LoadWord16() uint16

LoadWord16 loads a 16-bit word from the data register. Use it only when 16-bit word or data packing is configured.

func (*Periph) Reset

func (p *Periph) Reset()

Reset resets p.

func (*Periph) SetBaudrate added in v0.2.1

func (p *Periph) SetBaudrate(baudrate int)

SetBaudrate sets the baudrate (p must be disabled).

func (*Periph) SetConfig added in v0.2.1

func (p *Periph) SetConfig(conf Config, baudrate int)

SetConfig configures p. If baudrate > 0 it replaces the BR bits in conf with the ones calculated from baudrate. Notice that some configuration changes require disabled p.

func (*Periph) SetWordSize

func (p *Periph) SetWordSize(size int)

SetWordSize sets size of data word. All families support 8 and 16-bit words, F0, F3, L4 supports 4 to 16-bit. Some families require disabled peripheral before use this function. SetWordSize is mandatory for F0, F3, L4 because the default reset configuration does not work.

func (*Periph) Status

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

Status return current status of p.

func (*Periph) StoreByte

func (p *Periph) StoreByte(v byte)

StoreByte stores a byte to the data register. Use it only when 8-bit frame is configured.

func (*Periph) StoreWord16

func (p *Periph) StoreWord16(v uint16)

StoreWord16 stores a 16-bit word to the data register. Use it only when 16-bit word or data packing is configured.

func (*Periph) WordSize

func (p *Periph) WordSize() int

WordSize return currently used word size.

type Signal added in v0.3.0

type Signal uint8
const (
	SCK Signal = iota
	MOSI
	MISO
	NSS
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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