NetBurner 3.5.0
PDF Version
 
bb_i2c.h
Go to the documentation of this file.
1#ifndef __BB_I2C_H
2#define __BB_I2C_H
3/*NB_REVISION*/
4
5/*NB_COPYRIGHT*/
6
24#include <predef.h>
25#include <nbrtos.h>
26#include <pins.h>
27
28#ifndef WIRE_DEFAULT_IINSTACE
29#define WIRE_DEFAULT_INSTANCE Wire0
30#endif
31#define Wire WIRE_DEFAULT_INSTANCE
32
33
44class BBI2C {
45public:
56
57private:
58 enum HwCmd_t {
59 CMD_TXD = 0,
60 CMD_RXN = 1,
61 CMD_STOP = 2,
62 CMD_DISCARDN = 3,
63 CMD_START = 4,
64 CMD_START_NAK = 5,
65 CMD_START_HS = 6,
66 CMD_START_HS_NAK = 7,
67 };
68 enum TxnStat {
69 TXN_RDY,
70 TXN_SETUP,
71 TXN_BUS_BUSY,
72 TXN_RESTART_FOR_READ,
73 TXN_BEGIN_RX,
74 TXN_RX_IN_PROGRESS,
75 TXN_RX_QUEUED,
76 TXN_RX_STOPPING,
77 TXN_BEGIN_TX,
78 TXN_TX_IN_PROGRESS,
79 TXN_TX_QUEUED,
80 TXN_IN_PROGRESS,
81 TXN_STOPPING,
82 TXN_WAITING
83 };
84#ifdef I2C_DEBUG
85 struct IsrLogEntry {
86 uint32_t isrNum;
87 TxnStat oldState;
88 TxnStat newState;
89 const uint8_t *buf;
90 uint32_t len;
91 uint32_t lenToQueue;
92 uint32_t tick;
93 uint32_t tickFrac;
94 int line;
95
96 void Log(const I2C &i2c, TxnStat state, int line);
97 void Dump();
98 };
99 friend struct IsrLogEntry;
100 struct IsrLog {
101 IsrLogEntry entry[ISR_LOG_LEN];
102 uint32_t head;
103 uint32_t tail;
104 IsrLog();
105 void Log(const I2C &i2c, TxnStat state, int line);
106 void Dump();
107 };
108 friend struct IsrLog;
109#endif
110 PinIO scl, sda;
111
112 uint32_t delayCount;
113 uint8_t *dataBuf;
114 uint32_t dataLen;
115 uint32_t rxLenToQueue;
116 uint16_t maxDelay_ms;
117 uint16_t delayCycles_ms;// how many delay counts per millisecond
118 uint16_t currPinLowCount;// how many millisecond counts we've been low for
119 uint16_t reqPinLowCount;// how many milliseconds counts we need
120 TxnStat txnState;
121 Result_t txnResult;
122
123 OS_SEM txnSem;
124
125 uint32_t isrCnt;
126#ifdef I2C_DEBUG
127 IsrLog log;
128#endif
129
130
131 uint8_t iadrAddressSize; // Number of address register bytes, 0 - 3
132 void isr();
133 int isr_rx(uint32_t sr);
134 int isr_tx(uint32_t sr);
135#ifdef I2C_DEBUG
136 void Log(TxnStat state, int line);
137 void DumpLog();
138#endif
139
140 Result_t start(uint8_t deviceAddr);
141 Result_t restart(uint8_t deviceAddr);
142
143 Result_t stop();
144 Result_t write8(uint8_t dat);
145 Result_t read8(uint8_t &dat, bool bLast);
146 Result_t queueRx();
147
148
149public:
155 BBI2C(PinIO scl, PinIO sda);
156
157 // Copy constructor
158 BBI2C(const BBI2C & rhs)
159 : scl(rhs.scl), sda(rhs.sda), txnState(rhs.txnState)
160 {}
161
168 void setup(uint32_t maxBusSpeed);
169
174 void resetBus();
175
190 inline void setNumAddressBytes(uint8_t numAddressBytes = 1) { iadrAddressSize = numAddressBytes; }
191
201 virtual Result_t writeReg8(uint8_t devAddr, uint32_t reg, uint8_t data);
202 virtual Result_t writeReg16(uint8_t devAddr, uint32_t reg, uint16_t data, int byteOrder);
203 virtual Result_t writeReg32(uint8_t devAddr, uint32_t reg, uint32_t data, int byteOrder);
204
214 virtual Result_t readReg8(uint8_t devAddr, uint32_t reg, uint8_t &data);
215 virtual Result_t readReg16(uint8_t devAddr, uint32_t reg, uint16_t &data, int byteOrder);
216 virtual Result_t readReg32(uint8_t devAddr, uint32_t reg, uint32_t &data, int byteOrder);
217
232 virtual Result_t writeRegN(uint8_t devAddr, uint32_t reg, uint8_t *buf, uint32_t blen);
233
244 virtual Result_t readRegN(uint8_t devAddr, uint32_t reg, uint8_t *buf, uint32_t blen);
245
246 // Static implementations of the C++ class that can be used if you prefer a C style interface
247 static Result_t writeReg8(int module, uint8_t devAddr, uint32_t reg, uint8_t dat);
248 static Result_t readReg8(int module, uint8_t devAddr, uint32_t reg, uint8_t &dat);
249 static Result_t writeRegN(int module, uint8_t devAddr, uint32_t reg, uint8_t *buf, uint32_t blen);
250 static Result_t readRegN(int module, uint8_t devAddr, uint32_t reg, uint8_t *buf, uint32_t blen);
251
252 // These functions are for internal use only.
253 void dump(uint32_t line);
260 void SetMaxBusDelay(uint16_t milliseconds);
261 uint32_t GetBusSpeed() { return 132000000/delayCount; }
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:44
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:47
@ I2C_RES_ACK
Acknowledged.
Definition bb_i2c.h:48
@ I2C_RES_ERR
Unknown Error.
Definition bb_i2c.h:54
@ I2C_RES_BUSY
Bus is busy.
Definition bb_i2c.h:51
@ I2C_RES_NACK
Not acknowledged.
Definition bb_i2c.h:49
@ I2C_RES_IN_PROGRESS
Another transaction is currently in progress.
Definition bb_i2c.h:53
@ I2C_RES_ARG
Bad argument.
Definition bb_i2c.h:52
@ I2C_RES_ARB_LST
Arbitration listening.
Definition bb_i2c.h:50
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:190
void resetBus()
Reset the I2C bus.
I2C Peripheral Class.
Definition i2c.h:216
GPIO Pin Class.
Definition coldfire/cpu/MCF5441X/include/cpu_pins.h:14
Wire Interface Class for I2C.
Definition i2c.h:65
NetBurner Real-Time Operating System (NBRTOS) API.
Semaphores are used to control access to shared resources or or to communicate between tasks in a mul...
Definition nbrtos.h:386