Documentation ¶
Overview ¶
Package lpi2c provides a high-level interface to the LPI2C peripheral.
Index ¶
- Constants
- type DER
- type MCCR
- type MCFGR0
- type MCFGR1
- type MCFGR2
- type MCFGR3
- type MCR
- type MDMR
- type MFCR
- type MFSR
- type MSR
- type Master
- func (d *Master) Clear(flags MSR)
- func (d *Master) DMAISR()
- func (d *Master) Err(clear bool) error
- func (d *Master) Flush()
- func (d *Master) ID() uint8
- func (d *Master) ISR()
- func (d *Master) Name() string
- func (d *Master) NewConn(a i2cbus.Addr) i2cbus.Conn
- func (d *Master) Periph() *Periph
- func (d *Master) Read(p []byte)
- func (d *Master) ReadByte() byte
- func (d *Master) SetID(id uint8)
- func (d *Master) SetName(s string)
- func (d *Master) Setup(sp Speed)
- func (d *Master) Status() MSR
- func (d *Master) UsePin(pin iomux.Pin, sig Signal) bool
- func (d *Master) Wait(flags MSR)
- func (d *Master) Write(p []byte)
- func (d *Master) WriteCmd(cmd int16)
- func (d *Master) WriteCmds(cmds []int16)
- func (d *Master) WriteString(s string)
- type MasterError
- type Periph
- type RDR
- type SAMR
- type SASR
- type SCFGR1
- type SCFGR2
- type SCR
- type SSR
- type STAR
- type Signal
- type Speed
Constants ¶
const ( FEATURE uint32 = 0xFFFF << 0 //+ Feature Specification Number FEATURE_2 uint32 = 0x02 << 0 // Master only, with standard feature set FEATURE_3 uint32 = 0x03 << 0 // Master and slave, with standard feature set MINOR uint32 = 0xFF << 16 //+ Minor Version Number MAJOR uint32 = 0xFF << 24 //+ Major Version Number )
VERID
const ( FEATUREn = 0 MINORn = 16 MAJORn = 24 )
const ( MTXFIFO uint32 = 0x0F << 0 //+ Master Transmit FIFO Size MRXFIFO uint32 = 0x0F << 8 //+ Master Receive FIFO Size )
PARAM
const ( MTXFIFOn = 0 MRXFIFOn = 8 )
const ( MENn = 0 MRSTn = 1 MDOZENn = 2 MDBGENn = 3 MRTFn = 8 MRRFn = 9 )
const ( MTDFn = 0 MRDFn = 1 MEPFn = 8 MSDFn = 9 MNDFn = 10 MALFn = 11 MFEFn = 12 MPLTFn = 13 MDMFn = 14 MBFn = 24 MBBFn = 25 )
const ( HRENn = 0 HRPOLn = 1 HRSELn = 2 CIRFIFOn = 8 RDMOn = 9 )
const ( MPRESCALEn = 0 MAUTOSTOPn = 8 MIGNACKn = 9 MTIMECFGn = 10 MATCFGn = 16 MPINCFGn = 24 )
const ( MBUSIDLEn = 0 MFILTSCLn = 16 MFILTSDAn = 24 )
const ( MATCH0n = 0 MATCH1n = 16 )
const ( CLKLOn = 0 CLKHIn = 8 SETHOLDn = 16 DATAVDn = 24 )
const ( TXWATERn = 0 RXWATERn = 16 )
const ( TXCOUNTn = 0 RXCOUNTn = 16 )
const ( DATA int16 = 0xFF << 0 //+ Transmit Data CMD int16 = 0x07 << 8 //+ Command Data Send int16 = 0x00 << 8 // Transmit DATA[7:0] Recv int16 = 0x01 << 8 // Receive (DATA[7:0] + 1) bytes Stop int16 = 0x02 << 8 // Generate STOP condition Discard int16 = 0x03 << 8 // Receive and discard (DATA[7:0] + 1) bytes Start int16 = 0x04 << 8 // Generate (repeated) START and transmit address in DATA[7:0] StartNACK int16 = 0x05 << 8 // Generate (repeated) START and transmit address in DATA[7:0]. This transfer expects a NACK to be returned. StartHS int16 = 0x06 << 8 // Generate (repeated) START and transmit address in DATA[7:0] using high speed mode StartHSNACK int16 = 0x07 << 8 // Generate (repeated) START and transmit address in DATA[7:0] using high speed mode. This transfer expects a NACK to be returned. )
const ( DATAn = 0 CMDn = 8 )
const ( SENn = 0 SRSTn = 1 SFILTENn = 4 SFILTDZn = 5 SRTFn = 8 SRRFn = 9 )
const ( STDFn = 0 SRDFn = 1 SAVFn = 2 STAFn = 3 SRSFn = 8 SSDFn = 9 SBEFn = 10 SFEFn = 11 SAM0Fn = 12 SAM1Fn = 13 SGCFn = 14 SSARFn = 15 SBFn = 24 SBBFn = 25 )
const ( TDDEn = 0 RDDEn = 1 AVDEn = 2 )
const ( SADRSTALLn = 0 SRXSTALLn = 1 STXDSTALLn = 2 SACKSTALLn = 3 SGCENn = 8 SSAENn = 9 STXCFGn = 10 SRXCFGn = 11 SIGNACKn = 12 SHSMENn = 13 SADDRCFGn = 16 )
const ( SCLKHOLDn = 0 SDATAVDn = 8 SFILTSCLn = 16 SFILTSDAn = 24 )
const ( ADDR0n = 1 ADDR1n = 17 )
const ( RADDRn = 0 ANVn = 14 )
const ( RXDATAn = 0 RXEMPTYn = 14 SOFn = 15 )
const MasterErrFlags = MNDF | MALF | MFEF | MPLTF
const (
PINLOWn = 8
)
const (
TXNACKn = 0
)
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type MCFGR1 ¶
type MCFGR1 uint32
const ( MPRESCALE MCFGR1 = 0x07 << 0 //+ Prescaler Div1 MCFGR1 = 0x00 << 0 // Divide by 1 Div2 MCFGR1 = 0x01 << 0 // Divide by 2 Div4 MCFGR1 = 0x02 << 0 // Divide by 4 Div8 MCFGR1 = 0x03 << 0 // Divide by 8 Div16 MCFGR1 = 0x04 << 0 // Divide by 16 Div32 MCFGR1 = 0x05 << 0 // Divide by 32 Div64 MCFGR1 = 0x06 << 0 // Divide by 64 Div128 MCFGR1 = 0x07 << 0 // Divide by 128 MAUTOSTOP MCFGR1 = 0x01 << 8 //+ Automatic STOP Generation MIGNACK MCFGR1 = 0x01 << 9 //+ IGNACK MTIMECFG MCFGR1 = 0x01 << 10 //+ Timeout Configuration MATCFG MCFGR1 = 0x07 << 16 //+ Match Configuration Disable MCFGR1 = 0x00 << 16 // Match is disabled D0eqM0_or_D0eqM1 MCFGR1 = 0x02 << 16 // Match is enabled (1st data word equals MATCH0 OR MATCH1) DXeqM0_or_DXeqM1 MCFGR1 = 0x03 << 16 // Match is enabled (any data word equals MATCH0 OR MATCH1) D0D1_eq_M0M1 MCFGR1 = 0x04 << 16 // Match is enabled (1st data word equals MATCH0 AND 2nd data word equals MATCH1) DXDX1_eq_M0M1 MCFGR1 = 0x05 << 16 // Match is enabled (any data word equals MATCH0 AND next data word equals MATCH1) D0andM0_eq_M0andM1 MCFGR1 = 0x06 << 16 // Match is enabled (1st data word AND MATCH1 equals MATCH0 AND MATCH1) DXandM0_eq_M0andM1 MCFGR1 = 0x07 << 16 // Match is enabled (any data word AND MATCH1 equals MATCH0 AND MATCH1) MPINCFG MCFGR1 = 0x07 << 24 //+ Pin Configuration OpenDrain2pin MCFGR1 = 0x00 << 24 // 2-pin open drain mode OutputOnly2pin MCFGR1 = 0x01 << 24 // 2-pin output only mode (ultra-fast mode) PushPull2pin MCFGR1 = 0x02 << 24 // 2-pin push-pull mode PushPull4pin MCFGR1 = 0x03 << 24 // 4-pin push-pull mode OpenDrain2pinSepSlave MCFGR1 = 0x04 << 24 // 2-pin open drain mode with separate LPI2C slave OutputOnly2pinSepSlave MCFGR1 = 0x05 << 24 // 2-pin output only mode (ultra-fast mode) with separate LPI2C slave PushPull2pinSepSlave MCFGR1 = 0x06 << 24 // 2-pin push-pull mode with separate LPI2C slave PushPull4pinInverted MCFGR1 = 0x07 << 24 // 4-pin push-pull mode (inverted outputs) )
type MSR ¶
type MSR uint32
const ( MTDF MSR = 0x01 << 0 //+ Transmit Data Flag MRDF MSR = 0x01 << 1 //+ Receive Data Flag MEPF MSR = 0x01 << 8 //+ End Packet Flag MSDF MSR = 0x01 << 9 //+ STOP Detect Flag MNDF MSR = 0x01 << 10 //+ NACK Detect Flag MALF MSR = 0x01 << 11 //+ Arbitration Lost Flag MFEF MSR = 0x01 << 12 //+ FIFO Error Flag MPLTF MSR = 0x01 << 13 //+ Pin Low Timeout Flag MDMF MSR = 0x01 << 14 //+ Data Match Flag MBF MSR = 0x01 << 24 //+ Master Busy Flag MBBF MSR = 0x01 << 25 //+ Bus Busy Flag )
type Master ¶
type Master struct { sync.Mutex // use with the low-level interface to share the driver // contains filtered or unexported fields }
A Master is a driver for the LPI2C peripheral. It provides two kinds of interfaces to communicate with slave devices on the I2C bus.
The first interface is a low-level one. It provides a set of methods to directly interract with the Data / Command FIFOs of the underlying LPI2C peripheral.
Example:
d.WriteCmds( lpi2c.Start|eepromAddr<<1|wr, lpi2c.Send|int16(memAddr), lpi2c.Start|eepromAddr<<1|rd, lpi2c.Recv|int16(len(buf) - 1), lpi2c.Stop, ) d.Read(buf) if err := d.Err(true); err != nil {
Write methods in the low-level interface are asynchronous, that is, they may return before all commands/data will be written to the FIFO. Therefore you must not modify the data/command buffer pass to the last write method until the return of the Flush method or another write method.
The read/write methods doesn't return errors. There is an Err method that allow to check and reset the LPI2C error flags at a convenient time. Even if you call Err after every method call the returned error is still asynchronous due to the asynchronous nature of the write methods and the delayed execution of commands by the LPI2C peripheral itself. You can use Wait to synchronise things but it seems that only the MSDF flag (Stop Condition) can be used to to synchronize errors.
The second interface is a connection oriented one that implements the i2cbus.Conn interface.
Example:
c := d.NewConn(eepromAddr) c.WriteByte(memAaddr) c.Read(buf) err := c.Close() if err != nil {
Both interfaces may be used concurently by multiple goroutines but in such a case users of the low-level interface must gain an exclusive access to the driver using the embedded mutex and wait for the Stop Condition before unlocking the Master.
func NewMaster ¶
NewMaster returns a new master-mode driver for p. If valid DMA channel is given, the DMA will be used for bigger data transfers.
func (*Master) Clear ¶
Clear allows to clear the MEPF, MSDF, MDMF in the MSR register. It is intended to be used together with the Wait method to wait for events signaled by these flags.
func (*Master) DMAISR ¶
func (d *Master) DMAISR()
DMAISR is a DMA interrupt handler for the DMA channel used by Master.
func (*Master) Err ¶
Err returns the content of the MSR register wrapped into the MasterError type if any error flag (see MasterErrFlags) is set. Othewrise it returns nil. If clear is true Err clears the Tx FIFO and the error flags in the MSR register and if the LPI2C peripheral is in the busy state (MSR[MBF] is set) it also releases the bus by writing the Stop command into Tx FIFO.
func (*Master) Flush ¶
func (d *Master) Flush()
Flush waits for the last command passed to the last WriteCmd call or last data byte passed to the last Write/WriteString call to be written to the Tx FIFO. Return from Flush doesn't mean the written commands/data were or even will be executed/sent.
func (*Master) ISR ¶
func (d *Master) ISR()
ISR is the interrupt handler for the LPI2C peripheral used by Master.
func (*Master) Name ¶
Name implements the i2cbus.Master interface. The default name is the name of the underlying peripheral (e.g. "LPI2C1") but can be changed using SetName.
func (*Master) Read ¶
Read reads len(p) data bytes from Rx FIFO. The read data is valid if Err returns nil.
func (*Master) SetID ¶
SetID sets the Master ID. Its three least significant bits are used for arbitration between competing masters while switching to the High Speed mode.
func (*Master) Status ¶
Status returns the current status of the LPSPI Master. It's intended do to be used with together with the Clear and Wait methods to check which of the events we were waiting for actually took place.
You won't read this in the RM:
In caes of repeated START the detection of NACK causes setting of both MSDF and MEPF flags. Usually MNDF flag is set before MSDF, MEPF but sometimes it happens in the reverse order. After NACK the SDA stays high, the SCL stays low which probably causes that the MBF and MBBF flags are set. The only way to clear MBF,MBBF is to write the Stop command into MTDR or reset the peripheral (disabling and reenabling it doesn't work). After the Stop command SCL is momentarily pulled low to allow releasing SDA and next SCL what corresponds to the Stop Condition on the bus. SM says that MBBF reflets the bus state. But it's not cleare how MBF and MBBF relate to each other.
func (*Master) UsePin ¶
UsePin is a helper function that can be used to configure IO pins as required by LPI2C peripheral. It configures the pin as open-drain output with the internal 22 kΩ pull-up resistor enadbled so you can avoid external pull-up resistors for low-capacitance bus and low-speed transfers.
Only certain pins can be used forLPI2C peripheral (see datasheet). UsePin returns true on succes or false if it isn't possible to use a pin as a sig.
func (*Master) Wait ¶
Wait waits for an event described by the MEPF, MSDF, MDMF, MTDF flags or an error. The MTDF flag allows to wait for an empty Tx FIFO. In most cases you should clear the flag you want to wait for.
func (*Master) Write ¶
Write is like WriteCmds but writes only Send commands with the provided data.
func (*Master) WriteCmd ¶
WriteCmd works like WriteCmds but writes only one command word into the Tx FIFO.
func (*Master) WriteCmds ¶
WriteCmds starts writing commands into the Tx FIFO in the background using interrupts and/or DMA. WriteCmd is no-op if len(cmds) == 0.
The LPI2C concept of a combined command and data FIFO greatly simplifies use of the I2C protocol. Thanks to this concept an I2C transaction or even multiple transactions can be prepared in advance as an array of commands and data, including receive transactions if the amount of data is known.
There is, however, a certain weakness of the LPI2C peripheral when it comes to receiving data of an unknown quantity or if the data should be slowly received in chunks of size less than the Rx FIFO. Such transfers may require issuing repeat start conditions after each chunk to avoid MSR[FEF] error. This is because the LPI2C periperal transmits NACK at the end of the Recv command if the Rx FIFO isn't full and there is no next Recv or Discard command in the Tx FIFO. Two or more consecutive Recv commands in the list passed to WriteCmds may also cause the FIFO error because there is no guarantee that they will all get into the Tx FIFO on time.
func (*Master) WriteString ¶
WriteString is like Write but writes bytes from string instead of slice.
type MasterError ¶
type MasterError struct {
Status MSR // value of the Master Status Register as read by Master.Err
}
MasterError contains value of the Master Status Register with one or more error flags set.
func (*MasterError) Error ¶
func (e *MasterError) Error() string
type Periph ¶
type Periph struct { VERID mmio.R32[uint32] // Version ID Register PARAM mmio.R32[uint32] // Parameter Register MCR mmio.R32[MCR] // Master Control Register MSR mmio.R32[MSR] // Master Status Register MIER mmio.R32[MSR] // Master Interrupt Enable Register MDER mmio.R32[DER] // Master DMA Enable Register MCFGR0 mmio.R32[MCFGR0] // Master Configuration Register 0 MCFGR1 mmio.R32[MCFGR1] // Master Configuration Register 1 MCFGR2 mmio.R32[MCFGR2] // Master Configuration Register 2 MCFGR3 mmio.R32[MCFGR3] // Master Configuration Register 3 MDMR mmio.R32[MDMR] // Master Data Match Register MCCR0 mmio.R32[MCCR] // Master Clock Configuration Register 0 MCCR1 mmio.R32[MCCR] // Master Clock Configuration Register 1 MFCR mmio.R32[MFCR] // Master FIFO Control Register MFSR mmio.R32[MFSR] // Master FIFO Status Register MTDR mmio.R16[int16] // Master Transmit Data Register MRDR mmio.R32[RDR] // Master Receive Data Register SCR mmio.R32[SCR] // Slave Control Register SSR mmio.R32[SSR] // Slave Status Register SIER mmio.R32[SSR] // Slave Interrupt Enable Register SDER mmio.R32[DER] // Slave DMA Enable Register SCFGR1 mmio.R32[SCFGR1] // Slave Configuration Register 1 SCFGR2 mmio.R32[SCFGR2] // Slave Configuration Register 2 SAMR mmio.R32[SAMR] // Slave Address Match Register SASR mmio.R32[SASR] // Slave Address Status Register STAR mmio.R32[STAR] // Slave Transmit ACK Register STDR mmio.R32[uint32] // Slave Transmit Data Register SRDR mmio.R32[RDR] // Slave Receive Data Register // contains filtered or unexported fields }
func (*Periph) EnableClock ¶
EnableClock enables the clock for the LPI2C peripheral. lp determines whether the clock remains on in low power WAIT mode.
type SCFGR1 ¶
type SCFGR1 uint32
const ( SADRSTALL SCFGR1 = 0x01 << 0 //+ Address SCL Stall SRXSTALL SCFGR1 = 0x01 << 1 //+ RX SCL Stall STXDSTALL SCFGR1 = 0x01 << 2 //+ TX Data SCL Stall SACKSTALL SCFGR1 = 0x01 << 3 //+ ACK SCL Stall SGCEN SCFGR1 = 0x01 << 8 //+ General Call Enable SSAEN SCFGR1 = 0x01 << 9 //+ SMBus Alert Enable STXCFG SCFGR1 = 0x01 << 10 //+ Transmit Flag Configuration SRXCFG SCFGR1 = 0x01 << 11 //+ Receive Data Configuration SIGNACK SCFGR1 = 0x01 << 12 //+ Ignore NACK SHSMEN SCFGR1 = 0x01 << 13 //+ High Speed Mode Enable SADDRCFG SCFGR1 = 0x07 << 16 //+ Address Configuration ADDRCFG_0 SCFGR1 = 0x00 << 16 // Address match 0 (7-bit) ADDRCFG_1 SCFGR1 = 0x01 << 16 // Address match 0 (10-bit) ADDRCFG_2 SCFGR1 = 0x02 << 16 // Address match 0 (7-bit) or Address match 1 (7-bit) ADDRCFG_3 SCFGR1 = 0x03 << 16 // Address match 0 (10-bit) or Address match 1 (10-bit) ADDRCFG_4 SCFGR1 = 0x04 << 16 // Address match 0 (7-bit) or Address match 1 (10-bit) ADDRCFG_5 SCFGR1 = 0x05 << 16 // Address match 0 (10-bit) or Address match 1 (7-bit) ADDRCFG_6 SCFGR1 = 0x06 << 16 // From Address match 0 (7-bit) to Address match 1 (7-bit) ADDRCFG_7 SCFGR1 = 0x07 << 16 // From Address match 0 (10-bit) to Address match 1 (10-bit) )
type SSR ¶
type SSR uint32
const ( STDF SSR = 0x01 << 0 //+ Transmit Data Flag SRDF SSR = 0x01 << 1 //+ Receive Data Flag SAVF SSR = 0x01 << 2 //+ Address Valid Flag STAF SSR = 0x01 << 3 //+ Transmit ACK Flag SRSF SSR = 0x01 << 8 //+ Repeated Start Flag SSDF SSR = 0x01 << 9 //+ STOP Detect Flag SBEF SSR = 0x01 << 10 //+ Bit Error Flag SFEF SSR = 0x01 << 11 //+ FIFO Error Flag SAM0F SSR = 0x01 << 12 //+ Address Match 0 Flag SAM1F SSR = 0x01 << 13 //+ Address Match 1 Flag SGCF SSR = 0x01 << 14 //+ General Call Flag SSARF SSR = 0x01 << 15 //+ SMBus Alert Response Flag SBF SSR = 0x01 << 24 //+ Slave Busy Flag SBBF SSR = 0x01 << 25 //+ Bus Busy Flag )
type Speed ¶
type Speed uint64
Speed encodes the timing configuration that determines the maximum communication speed (the actual speed depends also on the SCL rise time).
const ( Slow50k Speed = timingSlow // ≤58 kb/s (slow) and 0.83 Mb/s HS Std100k Speed = timingStd // ≤114 kb/s (standard) and 1.65 Mb/s HS Fast400k Speed = timingFast // ≤400 kb/s (Fast) and 1.65 Mb/s HS FastPlus1M Speed = timingPlus // ≤1 Mb/s (Fast+) and 1.65 Mb/s HS FastHS Speed = timingFastHS // ≤400 kb/s (Fast) and 3.33 Mb/s HS FastPlusHS Speed = timingPlusHS // ≤1 Mb/s (Fast+) and 3.33 Mb/s HS )