1 /*
2 * Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7 #ifndef _HARDWARE_I2C_H
8 #define _HARDWARE_I2C_H
9
10 #include "pico.h"
11 #include "pico/time.h"
12 #include "hardware/structs/i2c.h"
13
14 // PICO_CONFIG: PARAM_ASSERTIONS_ENABLED_I2C, Enable/disable assertions in the I2C module, type=bool, default=0, group=hardware_i2c
15 #ifndef PARAM_ASSERTIONS_ENABLED_I2C
16 #define PARAM_ASSERTIONS_ENABLED_I2C 0
17 #endif
18
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22
23 /** \file hardware/i2c.h
24 * \defgroup hardware_i2c hardware_i2c
25 *
26 * I2C Controller API
27 *
28 * The I2C bus is a two-wire serial interface, consisting of a serial data line SDA and a serial clock SCL. These wires carry
29 * information between the devices connected to the bus. Each device is recognized by a unique address and can operate as
30 * either a “transmitter” or “receiver”, depending on the function of the device. Devices can also be considered as masters or
31 * slaves when performing data transfers. A master is a device that initiates a data transfer on the bus and generates the
32 * clock signals to permit that transfer. At that time, any device addressed is considered a slave.
33 *
34 * This API allows the controller to be set up as a master or a slave using the \ref i2c_set_slave_mode function.
35 *
36 * The external pins of each controller are connected to GPIO pins as defined in the GPIO muxing table in the datasheet. The muxing options
37 * give some IO flexibility, but each controller external pin should be connected to only one GPIO.
38 *
39 * Note that the controller does NOT support High speed mode or Ultra-fast speed mode, the fastest operation being fast mode plus
40 * at up to 1000Kb/s.
41 *
42 * See the datasheet for more information on the I2C controller and its usage.
43 *
44 * \subsection i2c_example Example
45 * \addtogroup hardware_i2c
46 * \include bus_scan.c
47 */
48
49 typedef struct i2c_inst i2c_inst_t;
50
51 /** The I2C identifiers for use in I2C functions.
52 *
53 * e.g. i2c_init(i2c0, 48000)
54 *
55 * \ingroup hardware_i2c
56 * @{
57 */
58 extern i2c_inst_t i2c0_inst;
59 extern i2c_inst_t i2c1_inst;
60
61 #define i2c0 (&i2c0_inst) ///< Identifier for I2C HW Block 0
62 #define i2c1 (&i2c1_inst) ///< Identifier for I2C HW Block 1
63
64 /** @} */
65
66 // ----------------------------------------------------------------------------
67 // Setup
68
69 /*! \brief Initialise the I2C HW block
70 * \ingroup hardware_i2c
71 *
72 * Put the I2C hardware into a known state, and enable it. Must be called
73 * before other functions. By default, the I2C is configured to operate as a
74 * master.
75 *
76 * The I2C bus frequency is set as close as possible to requested, and
77 * the return actual rate set is returned
78 *
79 * \param i2c Either \ref i2c0 or \ref i2c1
80 * \param baudrate Baudrate in Hz (e.g. 100kHz is 100000)
81 * \return Actual set baudrate
82 */
83 uint i2c_init(i2c_inst_t *i2c, uint baudrate);
84
85 /*! \brief Disable the I2C HW block
86 * \ingroup hardware_i2c
87 *
88 * \param i2c Either \ref i2c0 or \ref i2c1
89 *
90 * Disable the I2C again if it is no longer used. Must be reinitialised before
91 * being used again.
92 */
93 void i2c_deinit(i2c_inst_t *i2c);
94
95 /*! \brief Set I2C baudrate
96 * \ingroup hardware_i2c
97 *
98 * Set I2C bus frequency as close as possible to requested, and return actual
99 * rate set.
100 * Baudrate may not be as exactly requested due to clocking limitations.
101 *
102 * \param i2c Either \ref i2c0 or \ref i2c1
103 * \param baudrate Baudrate in Hz (e.g. 100kHz is 100000)
104 * \return Actual set baudrate
105 */
106 uint i2c_set_baudrate(i2c_inst_t *i2c, uint baudrate);
107
108 /*! \brief Set I2C port to slave mode
109 * \ingroup hardware_i2c
110 *
111 * \param i2c Either \ref i2c0 or \ref i2c1
112 * \param slave true to use slave mode, false to use master mode
113 * \param addr If \p slave is true, set the slave address to this value
114 */
115 void i2c_set_slave_mode(i2c_inst_t *i2c, bool slave, uint8_t addr);
116
117 // ----------------------------------------------------------------------------
118 // Generic input/output
119
120 struct i2c_inst {
121 i2c_hw_t *hw;
122 bool restart_on_next;
123 };
124
125 /*! \brief Convert I2c instance to hardware instance number
126 * \ingroup hardware_i2c
127 *
128 * \param i2c I2C instance
129 * \return Number of UART, 0 or 1.
130 */
i2c_hw_index(i2c_inst_t * i2c)131 static inline uint i2c_hw_index(i2c_inst_t *i2c) {
132 invalid_params_if(I2C, i2c != i2c0 && i2c != i2c1);
133 return i2c == i2c1 ? 1 : 0;
134 }
135
i2c_get_hw(i2c_inst_t * i2c)136 static inline i2c_hw_t *i2c_get_hw(i2c_inst_t *i2c) {
137 i2c_hw_index(i2c); // check it is a hw i2c
138 return i2c->hw;
139 }
140
141 /*! \brief Attempt to write specified number of bytes to address, blocking until the specified absolute time is reached.
142 * \ingroup hardware_i2c
143 *
144 * \param i2c Either \ref i2c0 or \ref i2c1
145 * \param addr Address of device to write to
146 * \param src Pointer to data to send
147 * \param len Length of data in bytes to send
148 * \param nostop If true, master retains control of the bus at the end of the transfer (no Stop is issued),
149 * and the next transfer will begin with a Restart rather than a Start.
150 * \param until The absolute time that the block will wait until the entire transaction is complete. Note, an individual timeout of
151 * this value divided by the length of data is applied for each byte transfer, so if the first or subsequent
152 * bytes fails to transfer within that sub timeout, the function will return with an error.
153 *
154 * \return Number of bytes written, or PICO_ERROR_GENERIC if address not acknowledged, no device present, or PICO_ERROR_TIMEOUT if a timeout occurred.
155 */
156 int i2c_write_blocking_until(i2c_inst_t *i2c, uint8_t addr, const uint8_t *src, size_t len, bool nostop, absolute_time_t until);
157
158 /*! \brief Attempt to read specified number of bytes from address, blocking until the specified absolute time is reached.
159 * \ingroup hardware_i2c
160 *
161 * \param i2c Either \ref i2c0 or \ref i2c1
162 * \param addr Address of device to read from
163 * \param dst Pointer to buffer to receive data
164 * \param len Length of data in bytes to receive
165 * \param nostop If true, master retains control of the bus at the end of the transfer (no Stop is issued),
166 * and the next transfer will begin with a Restart rather than a Start.
167 * \param until The absolute time that the block will wait until the entire transaction is complete.
168 * \return Number of bytes read, or PICO_ERROR_GENERIC if address not acknowledged, no device present, or PICO_ERROR_TIMEOUT if a timeout occurred.
169 */
170 int i2c_read_blocking_until(i2c_inst_t *i2c, uint8_t addr, uint8_t *dst, size_t len, bool nostop, absolute_time_t until);
171
172 /*! \brief Attempt to write specified number of bytes to address, with timeout
173 * \ingroup hardware_i2c
174 *
175 * \param i2c Either \ref i2c0 or \ref i2c1
176 * \param addr Address of device to write to
177 * \param src Pointer to data to send
178 * \param len Length of data in bytes to send
179 * \param nostop If true, master retains control of the bus at the end of the transfer (no Stop is issued),
180 * and the next transfer will begin with a Restart rather than a Start.
181 * \param timeout_us The time that the function will wait for the entire transaction to complete. Note, an individual timeout of
182 * this value divided by the length of data is applied for each byte transfer, so if the first or subsequent
183 * bytes fails to transfer within that sub timeout, the function will return with an error.
184 *
185 * \return Number of bytes written, or PICO_ERROR_GENERIC if address not acknowledged, no device present, or PICO_ERROR_TIMEOUT if a timeout occurred.
186 */
i2c_write_timeout_us(i2c_inst_t * i2c,uint8_t addr,const uint8_t * src,size_t len,bool nostop,uint timeout_us)187 static inline int i2c_write_timeout_us(i2c_inst_t *i2c, uint8_t addr, const uint8_t *src, size_t len, bool nostop, uint timeout_us) {
188 absolute_time_t t = make_timeout_time_us(timeout_us);
189 return i2c_write_blocking_until(i2c, addr, src, len, nostop, t);
190 }
191
192 int i2c_write_timeout_per_char_us(i2c_inst_t *i2c, uint8_t addr, const uint8_t *src, size_t len, bool nostop, uint timeout_per_char_us);
193
194 /*! \brief Attempt to read specified number of bytes from address, with timeout
195 * \ingroup hardware_i2c
196 *
197 * \param i2c Either \ref i2c0 or \ref i2c1
198 * \param addr Address of device to read from
199 * \param dst Pointer to buffer to receive data
200 * \param len Length of data in bytes to receive
201 * \param nostop If true, master retains control of the bus at the end of the transfer (no Stop is issued),
202 * and the next transfer will begin with a Restart rather than a Start.
203 * \param timeout_us The time that the function will wait for the entire transaction to complete
204 * \return Number of bytes read, or PICO_ERROR_GENERIC if address not acknowledged, no device present, or PICO_ERROR_TIMEOUT if a timeout occurred.
205 */
i2c_read_timeout_us(i2c_inst_t * i2c,uint8_t addr,uint8_t * dst,size_t len,bool nostop,uint timeout_us)206 static inline int i2c_read_timeout_us(i2c_inst_t *i2c, uint8_t addr, uint8_t *dst, size_t len, bool nostop, uint timeout_us) {
207 absolute_time_t t = make_timeout_time_us(timeout_us);
208 return i2c_read_blocking_until(i2c, addr, dst, len, nostop, t);
209 }
210
211 int i2c_read_timeout_per_char_us(i2c_inst_t *i2c, uint8_t addr, uint8_t *dst, size_t len, bool nostop, uint timeout_per_char_us);
212
213 /*! \brief Attempt to write specified number of bytes to address, blocking
214 * \ingroup hardware_i2c
215 *
216 * \param i2c Either \ref i2c0 or \ref i2c1
217 * \param addr Address of device to write to
218 * \param src Pointer to data to send
219 * \param len Length of data in bytes to send
220 * \param nostop If true, master retains control of the bus at the end of the transfer (no Stop is issued),
221 * and the next transfer will begin with a Restart rather than a Start.
222 * \return Number of bytes written, or PICO_ERROR_GENERIC if address not acknowledged, no device present.
223 */
224 int i2c_write_blocking(i2c_inst_t *i2c, uint8_t addr, const uint8_t *src, size_t len, bool nostop);
225
226 /*! \brief Attempt to read specified number of bytes from address, blocking
227 * \ingroup hardware_i2c
228 *
229 * \param i2c Either \ref i2c0 or \ref i2c1
230 * \param addr Address of device to read from
231 * \param dst Pointer to buffer to receive data
232 * \param len Length of data in bytes to receive
233 * \param nostop If true, master retains control of the bus at the end of the transfer (no Stop is issued),
234 * and the next transfer will begin with a Restart rather than a Start.
235 * \return Number of bytes read, or PICO_ERROR_GENERIC if address not acknowledged, no device present.
236 */
237 int i2c_read_blocking(i2c_inst_t *i2c, uint8_t addr, uint8_t *dst, size_t len, bool nostop);
238
239
240 /*! \brief Determine non-blocking write space available
241 * \ingroup hardware_i2c
242 *
243 * \param i2c Either \ref i2c0 or \ref i2c1
244 * \return 0 if no space is available in the I2C to write more data. If return is nonzero, at
245 * least that many bytes can be written without blocking.
246 */
i2c_get_write_available(i2c_inst_t * i2c)247 static inline size_t i2c_get_write_available(i2c_inst_t *i2c) {
248 const size_t IC_TX_BUFFER_DEPTH = 32;
249 return IC_TX_BUFFER_DEPTH - i2c_get_hw(i2c)->txflr;
250 }
251
252 /*! \brief Determine number of bytes received
253 * \ingroup hardware_i2c
254 *
255 * \param i2c Either \ref i2c0 or \ref i2c1
256 * \return 0 if no data available, if return is nonzero at
257 * least that many bytes can be read without blocking.
258 */
i2c_get_read_available(i2c_inst_t * i2c)259 static inline size_t i2c_get_read_available(i2c_inst_t *i2c) {
260 return i2c_get_hw(i2c)->rxflr;
261 }
262
263 /*! \brief Write direct to TX FIFO
264 * \ingroup hardware_i2c
265 *
266 * \param i2c Either \ref i2c0 or \ref i2c1
267 * \param src Data to send
268 * \param len Number of bytes to send
269 *
270 * Writes directly to the to I2C TX FIFO which us mainly useful for
271 * slave-mode operation.
272 */
i2c_write_raw_blocking(i2c_inst_t * i2c,const uint8_t * src,size_t len)273 static inline void i2c_write_raw_blocking(i2c_inst_t *i2c, const uint8_t *src, size_t len) {
274 for (size_t i = 0; i < len; ++i) {
275 // TODO NACK or STOP on end?
276 while (!i2c_get_write_available(i2c))
277 tight_loop_contents();
278 i2c_get_hw(i2c)->data_cmd = *src++;
279 }
280 }
281
282 /*! \brief Write direct to TX FIFO
283 * \ingroup hardware_i2c
284 *
285 * \param i2c Either \ref i2c0 or \ref i2c1
286 * \param dst Buffer to accept data
287 * \param len Number of bytes to send
288 *
289 * Reads directly from the I2C RX FIFO which us mainly useful for
290 * slave-mode operation.
291 */
i2c_read_raw_blocking(i2c_inst_t * i2c,uint8_t * dst,size_t len)292 static inline void i2c_read_raw_blocking(i2c_inst_t *i2c, uint8_t *dst, size_t len) {
293 for (size_t i = 0; i < len; ++i) {
294 while (!i2c_get_read_available(i2c))
295 tight_loop_contents();
296 *dst++ = i2c_get_hw(i2c)->data_cmd;
297 }
298 }
299
300 #ifdef __cplusplus
301 }
302 #endif
303
304 #endif
305