NetBurner 3.5.7
PDF Version
Wire.h
1/*NB_REVISION*/
2
3/*NB_COPYRIGHT*/
4
60#ifndef _WIRE_H
61#define _WIRE_H
62#include <sim.h> /*on-chip register definitions*/
63#include <nbrtos.h>
64#include <string.h>
65#include <pins.h>
66
67
72typedef void(slave_rx_handler)(int num_rx);
73
80typedef void(slave_rq_handler)();
81
86const int wire_success = 0;
87const int wire_data_too_big = 1;
88const int wire_NACK_on_address = 2;
89const int wire_NACK_on_data = 3;
90const int wire_other_error = 4;
91const int wire_timeout = 5;
255{
256 #include <wire_guts.h> // Processor specific code
257
258 static const uint32_t I2C_RX_BUFFER_SIZE = 256;
259 uint8_t InRxBuf[I2C_RX_BUFFER_SIZE];
260 size_t m_rx_get_pos;
261 size_t m_rx_put_pos;
262
263 static const uint32_t I2C_TX_BUFFER_SIZE = 256;
264 uint8_t OutTxBuf[I2C_TX_BUFFER_SIZE];
265 size_t m_tx_get_pos;
266 size_t m_tx_put_pos;
267
268 uint32_t m_ClockBaud;
269 uint32_t m_TimeOut_usec;
270 uint32_t m_nBegins;
271 uint8_t m_slave_addr;
272 uint8_t m_target_addr;
273 uint32_t m_flags;
274 uint32_t m_NoHang_TimeOut; // Ticks
275
276 bool flagset(uint32_t f) { return ((m_flags & f) != 0); }
277 void setflag(uint32_t f) { m_flags |= f; }
278 void clrflag(uint32_t f) { m_flags &= (~f); }
279
280 public:
287 TwoWire(void);
288
299 TwoWire(int mod);
300
314 TwoWire(PinIO scl, PinIO sda);
315
325 TwoWire(int sclpin_num, int sdapin_num);
326
333
345 void setClock(uint32_t clockbaud) { m_ClockBaud = clockbaud; }
346
361 void begin(uint8_t slave_addr = 0);
362
372 void end();
373
417 void beginTransmission(uint8_t address);
418
438 int endTransmission(bool bStop = true);
439
454 size_t write(const uint8_t *pdata, int len);
455
465 size_t write(char v) { return write((uint8_t *)&v, 1); };
466
476 size_t write(uint8_t v) { return write((uint8_t *)&v, 1); };
477
489 size_t write(const char *pStr, int len = -1)
490 {
491 if (len == -1) len = strlen(pStr);
492 return write((uint8_t *)pStr, len);
493 }
494
532 int requestFrom(uint8_t address, size_t quantity, bool stop = true);
533
544 size_t available();
545
557 size_t read(uint8_t *pbuf, size_t maxlen);
558
564 uint8_t read()
565 {
566 uint8_t v = 0;
567 read(&v, 1);
568 return v;
569 };
570
588
602
616 void setWireTimeout(int timeout_usec = 0, bool reset_on_timeout = false);
617
626
639
657 void reset();
658
692 bool ping(uint8_t addr);
693
741 int writeRegN(uint8_t dAddr, uint32_t reg, const uint8_t *buf, uint32_t blen);
742
758 int readRegN(uint8_t dAddr, uint32_t reg, uint8_t *buf, uint32_t blen);
759
773 inline int writeReg8(uint8_t dAddr, uint32_t reg, uint8_t data) { return writeRegN(dAddr, reg, &data, 1); }
774
788 inline int readReg8(uint8_t dAddr, uint32_t reg, uint8_t &data) { return readRegN(dAddr, reg, &data, 1); }
789
791};
792
794extern class TwoWire Wire;
795
839{
840 TwoWire &theWire;
841 uint8_t the_addr;
842
843 public:
850 TwoWireObject(TwoWire &w, uint8_t addr) : theWire{w}, the_addr{addr} {};
851
857 TwoWireObject(uint8_t addr) : theWire{Wire}, the_addr{addr} {};
858
862 void reset() { theWire.reset(); }
863
868 bool ping() { return theWire.ping(the_addr); }
869
883 int writeRegN(uint32_t reg, const uint8_t *buf, uint32_t blen) { return theWire.writeRegN(the_addr, reg, buf, blen); }
884
894 int readRegN(uint32_t reg, uint8_t *buf, uint32_t blen) { return theWire.readRegN(the_addr, reg, buf, blen); }
895
904 inline int writeReg8(uint32_t reg, uint8_t data) { return writeRegN(reg, &data, 1); }
905
914 inline int readReg8(uint32_t reg, uint8_t &data) { return readRegN(reg, &data, 1); }
915};
916
917#endif
918
GPIO Pin Class.
Definition coldfire/cpu/MCF5441X/include/cpu_pins.h:15
Arduino Wire-compatible I2C controller/target interface.
Definition Wire.h:255
void setClock(uint32_t clockbaud)
Set the I2C clock speed.
Definition Wire.h:345
size_t write(const char *pStr, int len=-1)
Queue a string for transmission.
Definition Wire.h:489
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:788
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)
Construct a TwoWire using a specific hardware I2C module.
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.
~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:773
uint8_t read()
Read the next byte from the receive buffer.
Definition Wire.h:564
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 reset()
Reset the I2C bus and recover from a hung 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:476
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.
size_t write(char v)
Queue a single char for transmission.
Definition Wire.h:465
Convenience wrapper binding a TwoWire instance and device address.
Definition Wire.h:839
int readReg8(uint32_t reg, uint8_t &data)
Read an 8-bit value from a target device register.
Definition Wire.h:914
void reset()
Reset the I2C system and unhang the bus.
Definition Wire.h:862
TwoWireObject(TwoWire &w, uint8_t addr)
Construct a TwoWireObject with a specific TwoWire instance.
Definition Wire.h:850
TwoWireObject(uint8_t addr)
Construct a TwoWireObject using the global Wire instance.
Definition Wire.h:857
bool ping()
Check to see if the bound device address exists on this bus.
Definition Wire.h:868
int writeReg8(uint32_t reg, uint8_t data)
Write an 8-bit value to a target device register.
Definition Wire.h:904
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:894
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:883
class TwoWire Wire
Pre-declared global TwoWire instance using platform default pins.
Definition I2C/Wire/PAC193X/src/main.cpp:15
void slave_rq_handler()
Callback type for target (slave) request events.
Definition Wire.h:80
void slave_rx_handler(int num_rx)
Callback type for target (slave) receive events.
Definition Wire.h:72
const int wire_success
Transaction completed successfully.
Definition Wire.h:86
const int wire_timeout
Transaction timed out (see setWireTimeout())
Definition Wire.h:91
const int wire_NACK_on_address
Target device did not acknowledge its address.
Definition Wire.h:88
const int wire_data_too_big
Data exceeds the 256-byte transmit buffer.
Definition Wire.h:87
const int wire_NACK_on_data
Target device did not acknowledge a data byte.
Definition Wire.h:89
const int wire_other_error
Unspecified bus error.
Definition Wire.h:90