NetBurner 3.5.6
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 OS_SEM txnSem;
119
120 uint32_t isrCnt;
121#ifdef I2C_DEBUG
122 IsrLog log;
123#endif
124
125
126 uint8_t iadrAddressSize; // Number of address register bytes, 0 - 3
127 void isr();
128 int isr_rx(uint32_t sr);
129 int isr_tx(uint32_t sr);
130#ifdef I2C_DEBUG
131 void Log(TxnStat state, int line);
132 void DumpLog();
133#endif
134
135public:
136 Result_t start();
137 Result_t restart();
138
139 Result_t stop();
140 Result_t write8(uint8_t dat);
141 Result_t read8(uint8_t &dat, bool bLast);
142 Result_t queueRx();
143
144
145public:
151 BBI2C(PinIO scl, PinIO sda);
152
153 // Copy constructor
154 BBI2C(const BBI2C & rhs)
155 : scl(rhs.scl), sda(rhs.sda), txnState(rhs.txnState)
156 {}
157
164 void setup(uint32_t maxBusSpeed);
165
170 void resetBus();
171
186 inline void setNumAddressBytes(uint8_t numAddressBytes = 1) { iadrAddressSize = numAddressBytes; }
187
197 virtual Result_t writeReg8(uint8_t devAddr, uint32_t reg, uint8_t data);
198 virtual Result_t writeReg16(uint8_t devAddr, uint32_t reg, uint16_t data, int byteOrder);
199 virtual Result_t writeReg32(uint8_t devAddr, uint32_t reg, uint32_t data, int byteOrder);
200
210 virtual Result_t readReg8(uint8_t devAddr, uint32_t reg, uint8_t &data);
211 virtual Result_t readReg16(uint8_t devAddr, uint32_t reg, uint16_t &data, int byteOrder);
212 virtual Result_t readReg32(uint8_t devAddr, uint32_t reg, uint32_t &data, int byteOrder);
213
228 virtual Result_t writeRegN(uint8_t devAddr, uint32_t reg, uint8_t *buf, uint32_t blen);
229
240 virtual Result_t readRegN(uint8_t devAddr, uint32_t reg, uint8_t *buf, uint32_t blen);
241
242 // Static implementations of the C++ class that can be used if you prefer a C style interface
243 static Result_t writeReg8(int module, uint8_t devAddr, uint32_t reg, uint8_t dat);
244 static Result_t readReg8(int module, uint8_t devAddr, uint32_t reg, uint8_t &dat);
245 static Result_t writeRegN(int module, uint8_t devAddr, uint32_t reg, uint8_t *buf, uint32_t blen);
246 static Result_t readRegN(int module, uint8_t devAddr, uint32_t reg, uint8_t *buf, uint32_t blen);
247
248 // These functions are for internal use only.
249 void dump(uint32_t line);
256 void SetMaxBusDelay(uint16_t milliseconds);
257 uint32_t GetBusSpeed() { return 132000000/delayCount; }
258
259 virtual Result_t ping(uint8_t devAddr, bool bAsWrite = false);
260
261 friend void LPI2C1_ISR();
262 friend void LPI2C2_ISR();
263 friend void LPI2C3_ISR();
264 friend void LPI2C4_ISR();
265
266 friend class WireIntf;
267
268}; // end class I2C
269
270#endif /* ----- #ifndef __BB_I2C_H ----- */
271
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:186
void resetBus()
Reset the I2C bus.
I2C Peripheral Class.
Definition i2c.h:213
GPIO Pin Class.
Definition coldfire/cpu/MCF5441X/include/cpu_pins.h:15
Wire Interface Class for I2C.
Definition i2c.h:66