JM9 XRCU Board 0.1.1
Libraries API Reference
Loading...
Searching...
No Matches
Wire.h
1/*
2 TwoWire.h - TWI/I2C library for Arduino & Wiring
3 Copyright (c) 2006 Nicholas Zambetti. All right reserved.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19 Modified 2012 by Todd Krein (todd@krein.org) to implement repeated starts
20*/
21
22/*
23 Change Log
24
25 Software I2C implementations added. ----- 2 Sept 2024 by David Choi <yhei.choi@gmail.com>
26
27
28 This comment should be regarded as a notice stating that a person changed this file and the date of any change.
29 This notice is required under section 2b of the GNU Lesser General Public License v2.1.
30 */
31
32#ifndef TwoWire_h
33#define TwoWire_h
34
35#include <functional>
36
37#include "Stream.h"
38#include "Arduino.h"
39extern "C" {
40#include "utility/twi.h"
41}
42#include "Soft_i2c_embed/Soft_wire_embed.h"
43
44// Minimal buffer length. Buffers length will be increased when needed,
45// but TX buffer is limited to a maximum to avoid too much stack consumption
46// Note: Buffer length and max buffer length are limited by uin16_t type
47#define BUFFER_LENGTH 32
48#if !defined(WIRE_MAX_TX_BUFF_LENGTH)
49 #define WIRE_MAX_TX_BUFF_LENGTH 1024U
50#endif
51
52// WIRE_HAS_END means Wire has end()
53#define WIRE_HAS_END 1
54
55class TwoWire : public Stream {
56 public:
57 typedef std::function<void(int)> cb_function_receive_t;
58 typedef std::function<void(void)> cb_function_request_t;
59
60 private:
61 uint8_t *rxBuffer;
62 uint16_t rxBufferAllocated;
63 uint16_t rxBufferIndex;
64 uint16_t rxBufferLength;
65
66 uint8_t txAddress;
67 uint8_t *txBuffer;
68 uint16_t txBufferAllocated;
69 uint16_t txDataSize;
70
71 uint8_t transmitting;
72
73 uint8_t ownAddress;
74 i2c_t _i2c;
75
76 bool begin_is_called;
77 bool useSoftTwi;
78 Soft_wire sw_i2c;
79
80 std::function<void(int)> user_onReceive;
81 std::function<void(void)> user_onRequest;
82
83 static void onRequestService(i2c_t *);
84 static void onReceiveService(i2c_t *);
85
86 void allocateRxBuffer(size_t length);
87 size_t allocateTxBuffer(size_t length);
88
89 void resetRxBuffer(void);
90 void resetTxBuffer(void);
91 void recoverBus(void);
92
93 public:
94 TwoWire();
95 TwoWire(uint32_t sda, uint32_t scl);
96 ~TwoWire();
97 // setSCL/SDA have to be called before begin()
98 void setSCL(uint32_t scl)
99 {
100 _i2c.scl = digitalPinToPinName(scl);
101 };
102 void setSDA(uint32_t sda)
103 {
104 _i2c.sda = digitalPinToPinName(sda);
105 };
106 void setSCL(PinName scl)
107 {
108 _i2c.scl = scl;
109 };
110 void setSDA(PinName sda)
111 {
112 _i2c.sda = sda;
113 };
114 void begin(bool generalCall = false);
115 void begin(uint32_t, uint32_t);
116 void begin(uint8_t, bool generalCall = false, bool NoStretchMode = false);
117 void begin(int, bool generalCall = false, bool NoStretchMode = false);
118 void end();
119 void setClock(uint32_t);
120 void setWireTimeout(uint32_t timeout = 25000U, bool reset_with_timeout = false);
121 void beginTransmission(uint8_t);
122 void beginTransmission(int);
123 uint8_t endTransmission(void);
124 uint8_t endTransmission(uint8_t);
125 uint8_t requestFrom(uint8_t, uint8_t);
126 uint8_t requestFrom(uint8_t, uint8_t, uint8_t);
127 uint8_t requestFrom(uint8_t, size_t, bool);
128 uint8_t requestFrom(uint8_t, uint8_t, uint32_t, uint8_t, uint8_t);
129 uint8_t requestFrom(int, int);
130 uint8_t requestFrom(int, int, int);
131 virtual size_t write(uint8_t);
132 virtual size_t write(const uint8_t *, size_t);
133 virtual int available(void);
134 virtual int read(void);
135 virtual int peek(void);
136 virtual void flush(void);
137
138 void onReceive(cb_function_receive_t callback);
139 void onRequest(cb_function_request_t callback);
140
141 inline size_t write(unsigned long n)
142 {
143 return write((uint8_t)n);
144 }
145 inline size_t write(long n)
146 {
147 return write((uint8_t)n);
148 }
149 inline size_t write(unsigned int n)
150 {
151 return write((uint8_t)n);
152 }
153 inline size_t write(int n)
154 {
155 return write((uint8_t)n);
156 }
157 using Print::write;
158
159 // Could be used to mix Arduino API and STM32Cube HAL API (ex: DMA). Use at your own risk.
160 I2C_HandleTypeDef *getHandle(void)
161 {
162 return &(_i2c.handle);
163 }
164
170 void setUseSoftTwi (const bool use);
171
177 bool isUseSoftTwi ();
178
184 void switchToPort (const uint32_t port);
185
192 void switchToPinsSCLSDA (const uint32_t scl, const uint32_t sda);
193
200 void switchToPinsSCLSDA (const PinName scl, const PinName sda);
201
207 bool isSlavePresent (const uint8_t slave_address);
208
215 uint8_t scanSlaves (const uint8_t starting_address = 0x01);
216};
217
218
219
220extern TwoWire Wire;
221
222#endif
Definition Wire.h:55
bool isUseSoftTwi()
Checks whether this object is currently using software or hardware TWI.
Definition Wire.cpp:624
void setUseSoftTwi(const bool use)
Sets the flag whether to use software or hardware TWI.
Definition Wire.cpp:620
void switchToPinsSCLSDA(const uint32_t scl, const uint32_t sda)
Switches the SCL and SDA pins used by this object.
Definition Wire.cpp:633
uint8_t scanSlaves(const uint8_t starting_address=0x01)
Scans the slaves connected to the TWI bus of selected port.
Definition Wire.cpp:652
~TwoWire()
TwoWire destructor.
Definition Wire.cpp:66
bool isSlavePresent(const uint8_t slave_address)
Scans if the slave of a specific address is connected to the TWI bus of selected port.
Definition Wire.cpp:646
void switchToPort(const uint32_t port)
Switches the port used by this object.
Definition Wire.cpp:628