Developer World
Spresense Arduino Library v3.2.0-77d75a4
Wire.h
1/*
2 Wire.h - Two Wire I/O for the Spresense SDK
3 Copyright (C) 2018 Sony Semiconductor Solutions Corp.
4 Copyright (c) 2017 Sony Corporation All right reserved.
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with this library; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19*/
20#ifndef Wire_h
21#define Wire_h
22
23/*
24 This header file maybe inclued in plain C file.
25 To avoid compiling error all C++ stuff should be ignored
26 */
27// #ifdef __cplusplus
28
29#include <stdbool.h>
30#include <nuttx/config.h>
31#include <nuttx/i2c/i2c_master.h>
32#include <sdk/config.h>
33#include "Stream.h"
34
35// I2C frequence supported
36#define TWI_FREQ_100KHZ (100000) // standard mode
37#define TWI_FREQ_400KHZ (400000) // fast mode
38#define TWI_FREQ_1MHZ (1000000) // fast mode plus
39
40// I2C address length supported
41#define TWI_ADDR_LEN_7_BIT (7)
42#define TWI_ADDR_LEN_10_BIT (10)
43
44// buffer
45#define BUFFER_LENGTH (32)
46#define TWI_TX_BUF_LEN BUFFER_LENGTH
47#define TWI_RX_BUF_LEN BUFFER_LENGTH
48
49// WIRE_HAS_END means Wire has end()
50#define WIRE_HAS_END (1)
51
52// return value
53#define TWI_SUCCESS (0) // success
54#define TWI_DATA_TOO_LONG (1) // data too long to fit in transmit buffer
55#define TWI_NACK_ON_ADDRESS (2) // received NACK on transmit of address
56#define TWI_NACK_ON_DATA (3) // received NACK on transmit of data
57#define TWI_OTHER_ERROR (4) // other error
58
59typedef void (*TWIReceiveHandler)(int bytes);
60typedef void (*TWIRequestHandler)(void);
61
62class TwoWire : public Stream
63{
64public:
65 TwoWire(int port = 0);
66 void begin(void); // master mode
67 void begin(uint8_t address); // slave mode, not supported in nuttx?
68 void begin(uint16_t address);
69 void begin(int address) { begin((uint8_t)address); }
70 void end(void);
71
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);
78
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); }
83
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); }
89
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);
96
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); }
101 using Print::write;
102
103private:
104 void beginTransmission(uint16_t address, uint8_t length);
105
106private:
107 FAR struct i2c_master_s* _dev;
108 int _port;
109 uint32_t _freq;
110 bool _transmitting;
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;
115 uint8_t _tx_buf_len;
116 uint8_t _rx_buf[TWI_RX_BUF_LEN];
117 uint8_t _rx_buf_index;
118 uint8_t _rx_buf_len;
119 TWIReceiveHandler _on_receive;
120 TWIRequestHandler _on_request;
121};
122
123#ifdef CONFIG_CXD56_I2C0
124extern TwoWire Wire;
125#else
126#error Please enable I2C0 in NuttX
127#endif
128
129#ifdef CONFIG_CXD56_I2C1
130extern TwoWire Wire1;
131#endif
132
133// #endif //__cplusplus
134#endif //Wire_h
Definition: Wire.h:63