NetBurner 3.5.8
PDF Version
bb_i2c.h
1#ifndef __BB_I2C_H
2#define __BB_I2C_H
3/*NB_REVISION*/
4
5/*NB_COPYRIGHT*/
6
10
128
129#include <predef.h>
130#include <nbrtos.h>
131#include <pins.h>
132
153class BBI2C {
154public:
165
166private:
167 enum TxnStat {
168 TXN_RDY,
169 TXN_SETUP,
170 TXN_BUS_BUSY,
171 TXN_RESTART_FOR_READ,
172 TXN_BEGIN_RX,
173 TXN_RX_IN_PROGRESS,
174 TXN_RX_QUEUED,
175 TXN_RX_STOPPING,
176 TXN_BEGIN_TX,
177 TXN_TX_IN_PROGRESS,
178 TXN_TX_QUEUED,
179 TXN_IN_PROGRESS,
180 TXN_STOPPING,
181 TXN_WAITING
182 };
183
184 PinIO scl, sda;
185
186 uint32_t delayCount; // Half bit period, in the units spinDly() counts
187 TxnStat txnState;
188 Result_t txnResult;
189 uint8_t iadrAddressSize; // Number of address register bytes, 0 - 3
190
191public:
192 Result_t start();
193 Result_t restart();
194
195 Result_t stop();
196 Result_t write8(uint8_t dat);
197 Result_t read8(uint8_t &dat, bool bLast);
198
199
200public:
210 BBI2C(PinIO scl, PinIO sda);
211
220 BBI2C(const BBI2C & rhs)
221 : scl(rhs.scl), sda(rhs.sda), delayCount(rhs.delayCount),
222 txnState(rhs.txnState), txnResult(rhs.txnResult),
223 iadrAddressSize(rhs.iadrAddressSize)
224 {}
225
236 void setup(uint32_t maxBusSpeed);
237
242 void resetBus();
243
258 inline void setNumAddressBytes(uint8_t numAddressBytes = 1) { iadrAddressSize = numAddressBytes; }
259
269 virtual Result_t writeReg8(uint8_t devAddr, uint32_t reg, uint8_t data);
270
282 virtual Result_t writeReg16(uint8_t devAddr, uint32_t reg, uint16_t data, int byteOrder);
283
295 virtual Result_t writeReg32(uint8_t devAddr, uint32_t reg, uint32_t data, int byteOrder);
296
306 virtual Result_t readReg8(uint8_t devAddr, uint32_t reg, uint8_t &data);
307 virtual Result_t readReg16(uint8_t devAddr, uint32_t reg, uint16_t &data, int byteOrder);
308 virtual Result_t readReg32(uint8_t devAddr, uint32_t reg, uint32_t &data, int byteOrder);
309
324 virtual Result_t writeRegN(uint8_t devAddr, uint32_t reg, uint8_t *buf, uint32_t blen);
325
336 virtual Result_t readRegN(uint8_t devAddr, uint32_t reg, uint8_t *buf, uint32_t blen);
337
338 // Static implementations of the C++ class that can be used if you prefer a C style interface
339 static Result_t writeReg8(int module, uint8_t devAddr, uint32_t reg, uint8_t dat);
340 static Result_t readReg8(int module, uint8_t devAddr, uint32_t reg, uint8_t &dat);
341 static Result_t writeRegN(int module, uint8_t devAddr, uint32_t reg, uint8_t *buf, uint32_t blen);
342 static Result_t readRegN(int module, uint8_t devAddr, uint32_t reg, uint8_t *buf, uint32_t blen);
343
344 // These functions are for internal use only.
345 void dump(uint32_t line);
363 [[deprecated("SetMaxBusDelay has no effect; bus operations already time out, use resetBus() to recover a hung bus")]]
364 void SetMaxBusDelay(uint16_t milliseconds);
365
381 uint32_t GetBusSpeed();
382
383 virtual Result_t ping(uint8_t devAddr, bool bAsWrite = false);
384
385}; // end class BBI2C
386
387#endif /* ----- #ifndef __BB_I2C_H ----- */
388
BBI2C(PinIO scl, PinIO sda)
Construct a software I2C bus on the specified GPIO pins.
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:156
@ I2C_RES_ACK
Acknowledged.
Definition bb_i2c.h:157
@ I2C_RES_ERR
Unknown Error.
Definition bb_i2c.h:163
@ I2C_RES_BUSY
Bus is busy.
Definition bb_i2c.h:160
@ I2C_RES_NACK
Not acknowledged.
Definition bb_i2c.h:158
@ I2C_RES_IN_PROGRESS
Another transaction is currently in progress.
Definition bb_i2c.h:162
@ I2C_RES_ARG
Bad argument.
Definition bb_i2c.h:161
@ I2C_RES_ARB_LST
Arbitration listening.
Definition bb_i2c.h:159
virtual Result_t writeReg8(uint8_t devAddr, uint32_t reg, uint8_t data)
Write an 8-bit value to a I2C slave device register.
uint32_t GetBusSpeed()
Return the bus speed the software timing is currently set to.
BBI2C(const BBI2C &rhs)
Copy constructor.
Definition bb_i2c.h:220
virtual Result_t writeReg16(uint8_t devAddr, uint32_t reg, uint16_t data, int byteOrder)
Write a 16-bit value to an I2C 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)
Configure the pins and prepare the software I2C bus for use.
virtual Result_t writeReg32(uint8_t devAddr, uint32_t reg, uint32_t data, int byteOrder)
Write a 32-bit value to an I2C device register.
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:258
void resetBus()
Reset the I2C bus.
GPIO Pin Class.
Definition coldfire/cpu/MCF5441X/include/cpu_pins.h:44