Documentation ¶
Overview ¶
Package i2c provides interface to use I2C peripheral.
I2C peripheral can be used through driver. Only one driver can be used with one peripheral.
In case of operation as master device, driver implements virtual connections to slave devices.
Driver supports multiple concurrent master connections. First read or write on inactive connection starts an I2C transaction and the connection becomes active until the transaction end. Peripheral supports only one active connection at the same time. Starting a subsequent transaction in other connection is blocked until the current transaction will end.
Active connection supports both read and write transactions. There is no need to terminate write transaction before subsequent read transaction but read transaction must be terminated before subsequent write transaction.
Index ¶
- Constants
- type AltDriver
- type AltDriverDMA
- func (d *AltDriverDMA) DMAISR(ch *dma.Channel)
- func (d *AltDriverDMA) I2CISR()
- func (d *AltDriverDMA) MasterConn(addr int16, stopm StopMode) AltMasterConnDMA
- func (d *AltDriverDMA) NewMasterConn(addr int16, stopm StopMode) *AltMasterConnDMA
- func (d *AltDriverDMA) SetIntMode(i2cen, dmaen bool)
- func (d *AltDriverDMA) Unlock()
- type AltMasterConn
- func (c *AltMasterConn) Read(buf []byte) (int, error)
- func (c *AltMasterConn) SetStopMode(stopm StopMode)
- func (c *AltMasterConn) SetStopRead()
- func (c *AltMasterConn) StopWrite()
- func (c *AltMasterConn) UnlockDriver()
- func (c *AltMasterConn) Write(buf []byte) (int, error)
- func (c *AltMasterConn) WriteByte(b byte) error
- type AltMasterConnDMA
- type Config
- type Driver
- type DutyCycle
- type Error
- type MasterConn
- func (c *MasterConn) Read(buf []byte) (int, error)
- func (c *MasterConn) SetStopMode(stopm StopMode)
- func (c *MasterConn) SetStopRead()
- func (c *MasterConn) StopWrite()
- func (c *MasterConn) UnlockDriver()
- func (c *MasterConn) Write(buf []byte) (int, error)
- func (c *MasterConn) WriteByte(b byte) error
- type Mode
- type Periph
- type StopMode
Constants ¶
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AltDriver ¶
type AltDriver struct { P *Periph // contains filtered or unexported fields }
AltDriver implements polling and interrupt driven driver to I2C peripheral. Default mode is polling. Interrupt mode avoids active polling of event flags at cost of system call to sleep until interupt occurs. ISR is very short: it only informs thread part of AltDriver that some event occured. The all remaining porcessing of events/data for both modes is handled by the same code that is executed in thread mode.
func NewAltDriver ¶
NewAltDriver provides convenient way to create heap allocated AltDriver struct.
func (*AltDriver) MasterConn ¶
func (d *AltDriver) MasterConn(addr int16, stopm StopMode) AltMasterConn
MasterConn returns initialized AltMasterConn struct that can be used to communicate with the slave device. Addr is the I2C address of the slave.
func (*AltDriver) NewMasterConn ¶
func (d *AltDriver) NewMasterConn(addr int16, stopm StopMode) *AltMasterConn
NewMasterConn is like MasterConn but returns pointer to heap allocated AltMasterConn struct.
func (*AltDriver) SetIntMode ¶
SetIntMode enables/disables interrupt mode.
type AltDriverDMA ¶
type AltDriverDMA struct { P *Periph RxDMA *dma.Channel TxDMA *dma.Channel // contains filtered or unexported fields }
AltDriverDMA uses DMA to implement polling and interrupt driven driver to I2C peripheral. Default mode is polling.
func NewAltDriverDMA ¶
func NewAltDriverDMA(p *Periph, rxch, txch *dma.Channel) *AltDriverDMA
NewAltDriverDMA provides convenient way to create heap allocated AltDriverDMA struct.
func (*AltDriverDMA) DMAISR ¶
func (d *AltDriverDMA) DMAISR(ch *dma.Channel)
func (*AltDriverDMA) I2CISR ¶
func (d *AltDriverDMA) I2CISR()
func (*AltDriverDMA) MasterConn ¶
func (d *AltDriverDMA) MasterConn(addr int16, stopm StopMode) AltMasterConnDMA
MasterConn returns initialized AltMasterConnDMA struct that can be used to communicate with the slave device. Addr is the I2C address of the slave. See MasterConnDMA.SetStopMode for description of stopm.
func (*AltDriverDMA) NewMasterConn ¶
func (d *AltDriverDMA) NewMasterConn(addr int16, stopm StopMode) *AltMasterConnDMA
NewMasterConn is like MasterConn but returns pointer to heap allocated AltMasterConnDMA struct.
func (*AltDriverDMA) SetIntMode ¶
func (d *AltDriverDMA) SetIntMode(i2cen, dmaen bool)
func (*AltDriverDMA) Unlock ¶
func (d *AltDriverDMA) Unlock()
Unlock must be used after recovering from an error.
type AltMasterConn ¶
type AltMasterConn struct {
// contains filtered or unexported fields
}
AltMasterConn can be used by I2c master. It represents a virtual connection to the slave device.
func (*AltMasterConn) Read ¶
func (c *AltMasterConn) Read(buf []byte) (int, error)
Read reads data from slave device into buf. If len(buf) == 0 Read does nothing, especially it does not: activate inactiv connection, interrupt previous write transaction, deactivate connection if SetStopRead was called before.
func (*AltMasterConn) SetStopMode ¶
func (c *AltMasterConn) SetStopMode(stopm StopMode)
SetStopMode allows to enable/disable auto-stop mode for read and/or write operations. See StopMode for more information.
func (*AltMasterConn) SetStopRead ¶
func (c *AltMasterConn) SetStopRead()
SetStopRead sets an internal flag which causes that subsequent read finishes transaction and deactivates connection. It can be called at any time, but if called after first read in current transaction, the subsequent read must read at least 2 bytes to properly generate stop condition on I2C bus.
func (*AltMasterConn) StopWrite ¶
func (c *AltMasterConn) StopWrite()
StopWrite terminates current write transaction and deactivates connection.
func (*AltMasterConn) UnlockDriver ¶
func (c *AltMasterConn) UnlockDriver()
func (*AltMasterConn) Write ¶
func (c *AltMasterConn) Write(buf []byte) (int, error)
Write sends data from buf to slave device. If len(buf) == 0 Write does nothing, especially it does not activate inactiv connection nor interrupt previous read transaction.
func (*AltMasterConn) WriteByte ¶
func (c *AltMasterConn) WriteByte(b byte) error
type AltMasterConnDMA ¶
type AltMasterConnDMA struct {
// contains filtered or unexported fields
}
func (*AltMasterConnDMA) Read ¶
func (c *AltMasterConnDMA) Read(buf []byte) (int, error)
Read reads data from slave device into buf. If len(buf) == 0 Read does nothing, especially it does not: activate inactiv connection, interrupt previous write transaction, deactivate connection if SetStopRead was called before.
func (*AltMasterConnDMA) SetStopRead ¶
func (c *AltMasterConnDMA) SetStopRead()
SetStopRead sets an internal flag which causes that subsequent read finishes transaction and deactivates connection. It can be called at any time, but if called after first read in current transaction, the subsequent read must read at least 2 bytes to properly generate stop condition on I2C bus.
func (*AltMasterConnDMA) StopWrite ¶
func (c *AltMasterConnDMA) StopWrite()
StopWrite terminates current write transaction and deactivates connection.
func (*AltMasterConnDMA) UnlockDriver ¶
func (c *AltMasterConnDMA) UnlockDriver()
type Driver ¶
type Driver struct { P *Periph RxDMA *dma.Channel TxDMA *dma.Channel // contains filtered or unexported fields }
Driver implements interrupt driven driver to I2C peripheral. To use the Driver the Periph field must be set and the I2CISR method must be setup as I2C interrupt handler for both (event and error) Periph's IRQs.
Setting the RxDMA or/and TxDMA fields enables using DMA for Rx or/and Tx data transfer. If DMA is enabled for some direction the DMAISR method must be setup as DMA interrupt handler for this direction.
func (*Driver) I2CISR ¶
func (d *Driver) I2CISR()
I2CISR is I2C (event and error) interrupt handler.
func (*Driver) MasterConn ¶
func (d *Driver) MasterConn(addr int16, stopm StopMode) MasterConn
MasterConn returns initialized MasterConn struct that can be used to communicate with the slave device. Addr is the I2C address of the slave.
func (*Driver) NewMasterConn ¶
func (d *Driver) NewMasterConn(addr int16, stopm StopMode) *MasterConn
NewMasterConn is like MasterConn but returns pointer to heap allocated MasterConn struct.
type Error ¶
type Error int16
const ( BusErr Error = 1 << 0 ArbLost Error = 1 << 1 AckFail Error = 1 << 2 Overrun Error = 1 << 3 PECErr Error = 1 << 4 Timeout Error = 1 << 6 SMBAlert Error = 1 << 7 BadEvent Error = 1 << 8 SoftTimeout Error = 1 << 9 BelatedStop Error = 1 << 10 ActiveRead Error = 1 << 11 // Write when active read transaction. DMAErr Error = 1 << 12 )
type MasterConn ¶
type MasterConn struct {
// contains filtered or unexported fields
}
func (*MasterConn) Read ¶
func (c *MasterConn) Read(buf []byte) (int, error)
Read reads data from slave device into buf. If len(buf) == 0 Read does nothing, especially it does not: activate inactiv connection, interrupt previous write transaction, deactivate connection if SetStopRead was called before.
func (*MasterConn) SetStopMode ¶
func (c *MasterConn) SetStopMode(stopm StopMode)
SetStopMode allows to enable/disable auto-stop mode for read and/or write operations. See StopMode for more information.
func (*MasterConn) SetStopRead ¶
func (c *MasterConn) SetStopRead()
SetStopRead sets an internal flag which causes that subsequent read finishes transaction and deactivates connection. It can be called at any time, but if called after first read in current transaction, the subsequent read must read at least 2 bytes to properly generate stop condition on I2C bus.
func (*MasterConn) StopWrite ¶
func (c *MasterConn) StopWrite()
StopWrite terminates current write transaction and deactivates connection.
func (*MasterConn) UnlockDriver ¶
func (c *MasterConn) UnlockDriver()
func (*MasterConn) Write ¶
func (c *MasterConn) Write(buf []byte) (int, error)
Write sends data from buf to slave device. If len(buf) == 0 Write does nothing, especially it does not activate inactiv connection nor interrupt previous read transaction.
func (*MasterConn) WriteByte ¶
func (c *MasterConn) WriteByte(b byte) error
type Periph ¶
type Periph struct {
// contains filtered or unexported fields
}
Periph represents I2C peripheral.
func (*Periph) EnableClock ¶
EnableClock enables clock for p. lp determines whether the clock remains on in low power (sleep) mode.
func (*Periph) Setup ¶
Setup configures p. It ensures that configured SCL clock speed <= cfg.Speed.
STM32 generates I2C SCL as follow:
SCL = PCLK / CCR / idiv
where PCLK is peripheral (bus) clock, CCR is 12-bit value from CCR register, idiv is internal divider equal to:
2 for standard mode, 3 for fast mode and 2/1 duty cycle, 25 for fast mode and 16/9 duty cycle.
For 36 MHz PCLK (typical for 72 MHz F10x) maximum valid SCL can be:
standard mode: 36 MHz / 180 / 2 = 100 kHz, fast mode 2/1: 36 MHz / 30 / 3 = 400 kHz, fast mode 16/9: 36 MHz / 4 / 25 = 360 kHz.
To obtain 400 kHz SCL in 16/9 fast mode the PCLK must be configured to multiple of 10 MHz.
type StopMode ¶
type StopMode byte
StopMode determines auto-stop behavior of master connection. If auto-stop mode is enabled for read (ASRD) or write (ASWR) then ay read/write operation is finished by sending stop condition on the I2C bus and leaves connection inactive. This mode improves ability to sharing I2C bus between multiple tasks but at the same time can degrade performance. It is not recommended to disable auto-stop mode for read operations.