NetBurner 3.5.8
PDF Version
Wire.h
1/*NB_REVISION*/
2
3/*NB_COPYRIGHT*/
4
8
228
229#ifndef _WIRE_H
230#define _WIRE_H
231#include <sim.h> /*on-chip register definitions*/
232#include <nbrtos.h>
233#include <string.h>
234#include <pins.h>
235
236
241typedef void(slave_rx_handler)(int num_rx);
242
249typedef void(slave_rq_handler)();
250
255const int wire_success = 0;
256const int wire_data_too_big = 1;
257const int wire_NACK_on_address = 2;
258const int wire_NACK_on_data = 3;
259const int wire_other_error = 4;
260const int wire_timeout = 5;
262
285{
286 #include <wire_guts.h> // Processor specific code
287
288 static const uint32_t I2C_RX_BUFFER_SIZE = 256;
289 uint8_t InRxBuf[I2C_RX_BUFFER_SIZE];
290 size_t m_rx_get_pos;
291 size_t m_rx_put_pos;
292
293 static const uint32_t I2C_TX_BUFFER_SIZE = 256;
294 uint8_t OutTxBuf[I2C_TX_BUFFER_SIZE];
295 size_t m_tx_get_pos;
296 size_t m_tx_put_pos;
297
298 // These carry in-class initialisers so that an object whose constructor
299 // could not bind a hardware module still has defined state.
300 uint32_t m_ClockBaud = 100000;
301 uint32_t m_TimeOut_usec = 1000;
302 uint32_t m_nBegins = 0;
303 uint8_t m_slave_addr = 0;
304 uint8_t m_target_addr = 0;
305 uint32_t m_flags = 0;
306 uint32_t m_NoHang_TimeOut = 2; // Ticks
307 uint8_t m_numAddressBytes = 1; // Register address width, 0 - 3, default 1
308
309 // False until a constructor successfully binds this object to a hardware
310 // I2C module. CoreConstructor() is the only thing that sets it true, so it
311 // is the single test for "is this object usable" on every platform.
312 bool m_bound = false;
313
314 bool flagset(uint32_t f) { return ((m_flags & f) != 0); }
315 void setflag(uint32_t f) { m_flags |= f; }
316 void clrflag(uint32_t f) { m_flags &= (~f); }
317
318 // Queue the register address, most significant byte first, using the width
319 // set by setNumAddressBytes(). A width of 0 queues nothing.
320 void sendRegAddress(uint32_t reg)
321 {
322 for (int i = (int)m_numAddressBytes - 1; i >= 0; i--)
323 {
324 write((uint8_t)(reg >> (8 * i)));
325 }
326 }
327
328 public:
335 TwoWire(void);
336
371 TwoWire(int mod);
372
390 TwoWire(PinIO scl, PinIO sda);
391
401 TwoWire(int sclpin_num, int sdapin_num);
402
409
449 bool isValid() const { return m_bound; }
450
462 void setClock(uint32_t clockbaud) { m_ClockBaud = clockbaud; }
463
478 void begin(uint8_t slave_addr = 0);
479
489 void end();
490
534 void beginTransmission(uint8_t address);
535
555 int endTransmission(bool bStop = true);
556
571 size_t write(const uint8_t *pdata, int len);
572
587 size_t write(uint8_t v) { return write((uint8_t *)&v, 1); };
588
600 size_t write(const char *pStr, int len = -1)
601 {
602 if (len == -1) len = strlen(pStr);
603 return write((uint8_t *)pStr, len);
604 }
605
673 int requestFrom(uint8_t address, size_t quantity, bool stop = true);
674
685 size_t available();
686
698 size_t read(uint8_t *pbuf, size_t maxlen);
699
705 uint8_t read()
706 {
707 uint8_t v = 0;
708 read(&v, 1);
709 return v;
710 };
711
716
729
742
756 void setWireTimeout(int timeout_usec = 0, bool reset_on_timeout = false);
757
766
779
781
786
828 void reset();
829
912 bool ping(uint8_t addr);
913
968 int writeRegN(uint8_t dAddr, uint32_t reg, const uint8_t *buf, uint32_t blen);
969
1011 inline void setNumAddressBytes(uint8_t numAddressBytes = 1) { m_numAddressBytes = numAddressBytes; }
1012
1035 int readRegN(uint8_t dAddr, uint32_t reg, uint8_t *buf, uint32_t blen);
1036
1050 inline int writeReg8(uint8_t dAddr, uint32_t reg, uint8_t data) { return writeRegN(dAddr, reg, &data, 1); }
1051
1065 inline int readReg8(uint8_t dAddr, uint32_t reg, uint8_t &data) { return readRegN(dAddr, reg, &data, 1); }
1066
1068};
1069
1071extern class TwoWire Wire;
1072
1116{
1117 TwoWire &theWire;
1118 uint8_t the_addr;
1119
1120 public:
1127 TwoWireObject(TwoWire &w, uint8_t addr) : theWire{w}, the_addr{addr} {};
1128
1134 TwoWireObject(uint8_t addr) : theWire{Wire}, the_addr{addr} {};
1135
1139 void reset() { theWire.reset(); }
1140
1151 bool ping() { return theWire.ping(the_addr); }
1152
1166 int writeRegN(uint32_t reg, const uint8_t *buf, uint32_t blen) { return theWire.writeRegN(the_addr, reg, buf, blen); }
1167
1177 int readRegN(uint32_t reg, uint8_t *buf, uint32_t blen) { return theWire.readRegN(the_addr, reg, buf, blen); }
1178
1187 inline int writeReg8(uint32_t reg, uint8_t data) { return writeRegN(reg, &data, 1); }
1188
1197 inline int readReg8(uint32_t reg, uint8_t &data) { return readRegN(reg, &data, 1); }
1198};
1199
1200#endif
1201
GPIO Pin Class.
Definition coldfire/cpu/MCF5441X/include/cpu_pins.h:44
Arduino Wire-compatible I2C controller/target interface.
Definition Wire.h:285
void setClock(uint32_t clockbaud)
Set the I2C clock speed.
Definition Wire.h:462
size_t write(const char *pStr, int len=-1)
Queue a string for transmission.
Definition Wire.h:600
void end()
Shut down the I2C interface.
void beginTransmission(uint8_t address)
Begin a write transaction to a target device.
int readReg8(uint8_t dAddr, uint32_t reg, uint8_t &data)
Read a single byte from a device register.
Definition Wire.h:1065
int requestFrom(uint8_t address, size_t quantity, bool stop=true)
Request bytes from a target device.
void begin(uint8_t slave_addr=0)
Initialize the I2C interface.
TwoWire(int mod)
Bind to a specific hardware I2C module, leaving the pins to the caller.
TwoWire(int sclpin_num, int sdapin_num)
Construct a TwoWire using Pins[] array index numbers.
void onRequest(slave_rq_handler *handler)
Register a callback for target (slave) request events.
bool isValid() const
Report whether this object is bound to a hardware I2C module.
Definition Wire.h:449
~TwoWire()
Destructor.
int writeRegN(uint8_t dAddr, uint32_t reg, const uint8_t *buf, uint32_t blen)
Write a number of bytes to a device register.
void onReceive(slave_rx_handler *handler)
Register a callback for target (slave) receive events.
int readRegN(uint8_t dAddr, uint32_t reg, uint8_t *buf, uint32_t blen)
Read a number of bytes from a device register.
int writeReg8(uint8_t dAddr, uint32_t reg, uint8_t data)
Write a single byte to a device register.
Definition Wire.h:1050
uint8_t read()
Read the next byte from the receive buffer.
Definition Wire.h:705
size_t available()
Check how many bytes are available to read.
TwoWire(PinIO scl, PinIO sda)
Construct a TwoWire using specific SCL/SDA pins.
size_t write(const uint8_t *pdata, int len)
Queue data bytes for transmission.
void setWireTimeout(int timeout_usec=0, bool reset_on_timeout=false)
Set the transaction completion timeout.
void clearTimeout()
Clear the transaction completion timeout.
int endTransmission(bool bStop=true)
End a write transaction and send data on the bus.
void setNumAddressBytes(uint8_t numAddressBytes=1)
Specify the register address size for a read or write transaction.
Definition Wire.h:1011
void reset()
Reset the I2C peripheral and return the object to an idle state.
size_t read(uint8_t *pbuf, size_t maxlen)
Read available data into a buffer (non-standard extension).
size_t write(uint8_t v)
Queue a single byte for transmission.
Definition Wire.h:587
bool getWireTimeoutFlag()
Check whether a timeout has occurred.
bool ping(uint8_t addr)
Check whether a device is present at the given address.
TwoWire(void)
Construct a TwoWire using the platform default I2C module.
int readReg8(uint32_t reg, uint8_t &data)
Read an 8-bit value from a target device register.
Definition Wire.h:1197
void reset()
Reset the I2C system and unhang the bus.
Definition Wire.h:1139
TwoWireObject(TwoWire &w, uint8_t addr)
Construct a TwoWireObject with a specific TwoWire instance.
Definition Wire.h:1127
TwoWireObject(uint8_t addr)
Construct a TwoWireObject using the global Wire instance.
Definition Wire.h:1134
bool ping()
Check to see if the bound device address exists on this bus.
Definition Wire.h:1151
int writeReg8(uint32_t reg, uint8_t data)
Write an 8-bit value to a target device register.
Definition Wire.h:1187
int readRegN(uint32_t reg, uint8_t *buf, uint32_t blen)
Read a number of 8-bit values from the specified register address.
Definition Wire.h:1177
int writeRegN(uint32_t reg, const uint8_t *buf, uint32_t blen)
Write a number of 8-bit values to the specified register address.
Definition Wire.h:1166
class TwoWire Wire
Pre-declared global TwoWire instance using platform default pins.
Definition I2C/Wire/DocExamples/src/main.cpp:24
void slave_rq_handler()
Callback type for target (slave) request events.
Definition Wire.h:249
void slave_rx_handler(int num_rx)
Callback type for target (slave) receive events.
Definition Wire.h:241
const int wire_success
Transaction completed successfully.
Definition Wire.h:255
const int wire_timeout
Transaction timed out (see setWireTimeout()).
Definition Wire.h:260
const int wire_NACK_on_address
Target device did not acknowledge its address.
Definition Wire.h:257
const int wire_data_too_big
Data exceeds the 256-byte transmit buffer.
Definition Wire.h:256
const int wire_NACK_on_data
Target device did not acknowledge a data byte.
Definition Wire.h:258
const int wire_other_error
Unspecified bus error.
Definition Wire.h:259