NetBurner 3.5.7
PDF Version
bb_i2c.h
1#ifndef __BB_I2C_H
2#define __BB_I2C_H
3/*NB_REVISION*/
4
5/*NB_COPYRIGHT*/
6
19#include <predef.h>
20#include <nbrtos.h>
21#include <pins.h>
22
23#ifndef WIRE_DEFAULT_IINSTACE
24#define WIRE_DEFAULT_INSTANCE Wire0
25#endif
26#define Wire WIRE_DEFAULT_INSTANCE
27
28
39class BBI2C {
40public:
51
52private:
53 enum HwCmd_t {
54 CMD_TXD = 0,
55 CMD_RXN = 1,
56 CMD_STOP = 2,
57 CMD_DISCARDN = 3,
58 CMD_START = 4,
59 CMD_START_NAK = 5,
60 CMD_START_HS = 6,
61 CMD_START_HS_NAK = 7,
62 };
63 enum TxnStat {
64 TXN_RDY,
65 TXN_SETUP,
66 TXN_BUS_BUSY,
67 TXN_RESTART_FOR_READ,
68 TXN_BEGIN_RX,
69 TXN_RX_IN_PROGRESS,
70 TXN_RX_QUEUED,
71 TXN_RX_STOPPING,
72 TXN_BEGIN_TX,
73 TXN_TX_IN_PROGRESS,
74 TXN_TX_QUEUED,
75 TXN_IN_PROGRESS,
76 TXN_STOPPING,
77 TXN_WAITING
78 };
79#ifdef I2C_DEBUG
80 struct IsrLogEntry {
81 uint32_t isrNum;
82 TxnStat oldState;
83 TxnStat newState;
84 const uint8_t *buf;
85 uint32_t len;
86 uint32_t lenToQueue;
87 uint32_t tick;
88 uint32_t tickFrac;
89 int line;
90
91 void Log(const I2C &i2c, TxnStat state, int line);
92 void Dump();
93 };
94 friend struct IsrLogEntry;
95 struct IsrLog {
96 IsrLogEntry entry[ISR_LOG_LEN];
97 uint32_t head;
98 uint32_t tail;
99 IsrLog();
100 void Log(const I2C &i2c, TxnStat state, int line);
101 void Dump();
102 };
103 friend struct IsrLog;
104#endif
105 PinIO scl, sda;
106
107 uint32_t delayCount;
108 uint8_t *dataBuf;
109 uint32_t dataLen;
110 uint32_t rxLenToQueue;
111 uint16_t maxDelay_ms;
112 uint16_t delayCycles_ms;// how many delay counts per millisecond
113 uint16_t currPinLowCount;// how many millisecond counts we've been low for
114 uint16_t reqPinLowCount;// how many milliseconds counts we need
115 TxnStat txnState;
116 Result_t txnResult;
117
118#ifndef NB_BARE_METAL
119 OS_SEM txnSem;
120#endif
121
122 uint32_t isrCnt;
123#ifdef I2C_DEBUG
124 IsrLog log;
125#endif
126
127
128 uint8_t iadrAddressSize; // Number of address register bytes, 0 - 3
129 void isr();
130 int isr_rx(uint32_t sr);
131 int isr_tx(uint32_t sr);
132#ifdef I2C_DEBUG
133 void Log(TxnStat state, int line);
134 void DumpLog();
135#endif
136
137public:
138 Result_t start();
139 Result_t restart();
140
141 Result_t stop();
142 Result_t write8(uint8_t dat);
143 Result_t read8(uint8_t &dat, bool bLast);
144 Result_t queueRx();
145
146
147public:
153 BBI2C(PinIO scl, PinIO sda);
154
155 // Copy constructor
156 BBI2C(const BBI2C & rhs)
157 : scl(rhs.scl), sda(rhs.sda), txnState(rhs.txnState)
158 {}
159
166 void setup(uint32_t maxBusSpeed);
167
172 void resetBus();
173
188 inline void setNumAddressBytes(uint8_t numAddressBytes = 1) { iadrAddressSize = numAddressBytes; }
189
199 virtual Result_t writeReg8(uint8_t devAddr, uint32_t reg, uint8_t data);
200 virtual Result_t writeReg16(uint8_t devAddr, uint32_t reg, uint16_t data, int byteOrder);
201 virtual Result_t writeReg32(uint8_t devAddr, uint32_t reg, uint32_t data, int byteOrder);
202
212 virtual Result_t readReg8(uint8_t devAddr, uint32_t reg, uint8_t &data);
213 virtual Result_t readReg16(uint8_t devAddr, uint32_t reg, uint16_t &data, int byteOrder);
214 virtual Result_t readReg32(uint8_t devAddr, uint32_t reg, uint32_t &data, int byteOrder);
215
230 virtual Result_t writeRegN(uint8_t devAddr, uint32_t reg, uint8_t *buf, uint32_t blen);
231
242 virtual Result_t readRegN(uint8_t devAddr, uint32_t reg, uint8_t *buf, uint32_t blen);
243
244 // Static implementations of the C++ class that can be used if you prefer a C style interface
245 static Result_t writeReg8(int module, uint8_t devAddr, uint32_t reg, uint8_t dat);
246 static Result_t readReg8(int module, uint8_t devAddr, uint32_t reg, uint8_t &dat);
247 static Result_t writeRegN(int module, uint8_t devAddr, uint32_t reg, uint8_t *buf, uint32_t blen);
248 static Result_t readRegN(int module, uint8_t devAddr, uint32_t reg, uint8_t *buf, uint32_t blen);
249
250 // These functions are for internal use only.
251 void dump(uint32_t line);
258 void SetMaxBusDelay(uint16_t milliseconds);
259 uint32_t GetBusSpeed() { return 132000000/delayCount; }
260
261 virtual Result_t ping(uint8_t devAddr, bool bAsWrite = false);
262
263 friend void LPI2C1_ISR();
264 friend void LPI2C2_ISR();
265 friend void LPI2C3_ISR();
266 friend void LPI2C4_ISR();
267
268 friend class WireIntf;
269
270}; // end class I2C
271
272#endif /* ----- #ifndef __BB_I2C_H ----- */
273
I2C Peripheral Class.
Definition bb_i2c.h:39
BBI2C(PinIO scl, PinIO sda)
Constructor for the I2C peripheral module.
virtual Result_t readRegN(uint8_t devAddr, uint32_t reg, uint8_t *buf, uint32_t blen)
Read a number of 8-bit values from an I2C slave at the specified register address.
Result_t
Definition bb_i2c.h:42
@ I2C_RES_ACK
Acknowledged.
Definition bb_i2c.h:43
@ I2C_RES_ERR
Unknown Error.
Definition bb_i2c.h:49
@ I2C_RES_BUSY
Bus is busy.
Definition bb_i2c.h:46
@ I2C_RES_NACK
Not acknowledged.
Definition bb_i2c.h:44
@ I2C_RES_IN_PROGRESS
Another transaction is currently in progress.
Definition bb_i2c.h:48
@ I2C_RES_ARG
Bad argument.
Definition bb_i2c.h:47
@ I2C_RES_ARB_LST
Arbitration listening.
Definition bb_i2c.h:45
virtual Result_t writeReg8(uint8_t devAddr, uint32_t reg, uint8_t data)
Write an 8-bit value to a I2C slave device register.
virtual Result_t writeRegN(uint8_t devAddr, uint32_t reg, uint8_t *buf, uint32_t blen)
Write a number of 8-bit values to an I2C slave to the specified register address.
virtual Result_t readReg8(uint8_t devAddr, uint32_t reg, uint8_t &data)
Read an 8-bit value form an I2C slave device register.
void setup(uint32_t maxBusSpeed)
Setup the I2C peripheral interface.
void SetMaxBusDelay(uint16_t milliseconds)
Set the maximum duration that the bus is allowed to be in use without being clocked before being forc...
void setNumAddressBytes(uint8_t numAddressBytes=1)
Specify the register address size for a read or write transaction. A number of bytes,...
Definition bb_i2c.h:188
void resetBus()
Reset the I2C bus.
GPIO Pin Class.
Definition coldfire/cpu/MCF5441X/include/cpu_pins.h:15
Counting semaphore for task synchronization and resource management.
Definition nbrtos.h:550