30#include <nuttx/config.h>
31#include <nuttx/i2c/i2c_master.h>
32#include <sdk/config.h>
36#define TWI_FREQ_100KHZ (100000)
37#define TWI_FREQ_400KHZ (400000)
38#define TWI_FREQ_1MHZ (1000000)
41#define TWI_ADDR_LEN_7_BIT (7)
42#define TWI_ADDR_LEN_10_BIT (10)
45#define BUFFER_LENGTH (32)
46#define TWI_TX_BUF_LEN BUFFER_LENGTH
47#define TWI_RX_BUF_LEN BUFFER_LENGTH
50#define WIRE_HAS_END (1)
53#define TWI_SUCCESS (0)
54#define TWI_DATA_TOO_LONG (1)
55#define TWI_NACK_ON_ADDRESS (2)
56#define TWI_NACK_ON_DATA (3)
57#define TWI_OTHER_ERROR (4)
59typedef void (*TWIReceiveHandler)(
int bytes);
60typedef void (*TWIRequestHandler)(void);
67 void begin(uint8_t address);
68 void begin(uint16_t address);
69 void begin(
int address) { begin((uint8_t)address); }
72 virtual int available(
void);
73 virtual int peek(
void);
74 virtual int read(
void);
75 virtual void flush(
void);
76 virtual size_t write(uint8_t value);
77 virtual size_t write(
const uint8_t* data,
size_t length);
79 uint8_t requestFrom(uint8_t address, uint8_t quantity, uint8_t sendStop);
80 uint8_t requestFrom(uint8_t address, uint8_t quantity) {
return requestFrom(address, quantity, (uint8_t)
true); }
81 uint8_t requestFrom(
int address,
int quantity,
int sendStop) {
return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)sendStop); }
82 uint8_t requestFrom(
int address,
int quantity) {
return requestFrom((uint8_t)address, (uint8_t)quantity); }
84 void beginTransmission(uint8_t address) { beginTransmission(address, TWI_ADDR_LEN_7_BIT); }
85 void beginTransmission(uint16_t address) { beginTransmission(address, TWI_ADDR_LEN_10_BIT); }
86 void beginTransmission(
int address) { beginTransmission((uint8_t)address); }
87 uint8_t endTransmission(
bool sendStop);
88 uint8_t endTransmission(
void) {
return endTransmission(
true); }
90 void setClock(uint32_t clock);
91 void setWireTimeout(uint32_t timeout = 25000,
bool reset_with_timeout =
false);
92 bool getWireTimeoutFlag(
void);
93 void clearWireTimeoutFlag(
void);
94 void onReceive(TWIReceiveHandler handler);
95 void onRequest(TWIRequestHandler handler);
97 inline size_t write(
unsigned long n) {
return write((uint8_t)n); }
98 inline size_t write(
long n) {
return write((uint8_t)n); }
99 inline size_t write(
unsigned int n) {
return write((uint8_t)n); }
100 inline size_t write(
int n) {
return write((uint8_t)n); }
104 void beginTransmission(uint16_t address, uint8_t length);
107 FAR
struct i2c_master_s* _dev;
111 uint16_t _tx_address;
112 uint8_t _tx_addr_len;
113 uint8_t _tx_buf[TWI_TX_BUF_LEN];
114 uint8_t _tx_buf_index;
116 uint8_t _rx_buf[TWI_RX_BUF_LEN];
117 uint8_t _rx_buf_index;
119 TWIReceiveHandler _on_receive;
120 TWIRequestHandler _on_request;
123#ifdef CONFIG_CXD56_I2C0
126#error Please enable I2C0 in NuttX
129#ifdef CONFIG_CXD56_I2C1