i2c

package
v0.0.0-...-168ccc2 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2021 License: BSD-3-Clause Imports: 9 Imported by: 0

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

View Source
const (
	I2C       = Mode(0)
	SMBusDev  = Mode(i2c.SMBUS)
	SMBusHost = Mode(i2c.SMBTYPE | i2c.SMBUS)
)

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

func NewAltDriver(p *Periph) *AltDriver

NewAltDriver provides convenient way to create heap allocated AltDriver struct.

func (*AltDriver) ISR

func (d *AltDriver) ISR()

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

func (d *AltDriver) SetIntMode(en bool)

SetIntMode enables/disables interrupt mode.

func (*AltDriver) Unlock

func (d *AltDriver) Unlock()

Unlock must be used after recovering from error.

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

func (*AltMasterConnDMA) Write

func (c *AltMasterConnDMA) 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.

type Config

type Config struct {
	Speed int       // Clock speed [Hz]
	Duty  DutyCycle // Duty cycle used in fast mode.
	Mode  Mode      // I2C, SMBusDev, SMBusHost.
}

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

func (*Driver) Unlock

func (d *Driver) Unlock()

Unlock must be used after recovering from error.

type DutyCycle

type DutyCycle byte

DutyCycle describes SCL low time to high time.

const (
	Duty2_1  DutyCycle = iota // SCL low/high = 2/1.
	Duty16_9                  // SCL low/high = 16/9.
)

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
)

func (Error) Error

func (e Error) Error() string

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 Mode

type Mode byte

Mode

type Periph

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

Periph represents I2C peripheral.

func (*Periph) Bus

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

Bus returns a bus to which p is connected.

func (*Periph) Disable

func (p *Periph) Disable()

func (*Periph) DisableClock

func (p *Periph) DisableClock()

DisableClock disables clock for p..

func (*Periph) Enable

func (p *Periph) Enable()

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

func (p *Periph) Reset()

Reset resets p.

func (*Periph) Setup

func (p *Periph) Setup(cfg *Config)

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.

func (*Periph) SoftReset

func (p *Periph) SoftReset()

func (*Periph) Speed

func (p *Periph) Speed() int

Speed returns actual clock speed set.

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.

const (
	NOAS StopMode = 0      // Manual mode (use SetStopRead, StopWrite).
	ASRD StopMode = 1 << 1 // Any read is finished by sending stop condition.
	ASWR StopMode = 1 << 2 // Any write is finished by sending stop condition.

)

Jump to

Keyboard shortcuts

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