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 #include "hardware/regs/dreq.h"
14
15 // PICO_CONFIG: PARAM_ASSERTIONS_ENABLED_HARDWARE_I2C, Enable/disable assertions in the hardware_i2c module, type=bool, default=0, group=hardware_i2c
16 #ifndef PARAM_ASSERTIONS_ENABLED_HARDWARE_I2C
17 #ifdef PARAM_ASSERTIONS_ENABLED_I2C // backwards compatibility with SDK < 2.0.0
18 #define PARAM_ASSERTIONS_ENABLED_HARDWARE_I2C PARAM_ASSERTIONS_ENABLED_I2C
19 #else
20 #define PARAM_ASSERTIONS_ENABLED_HARDWARE_I2C 0
21 #endif
22 #endif
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26
27 /** \file hardware/i2c.h
28 * \defgroup hardware_i2c hardware_i2c
29 *
30 * \brief I2C Controller API
31 *
32 * The I2C bus is a two-wire serial interface, consisting of a serial data line SDA and a serial clock SCL. These wires carry
33 * information between the devices connected to the bus. Each device is recognized by a unique 7-bit address and can operate as
34 * either a “transmitter” or “receiver”, depending on the function of the device. Devices can also be considered as masters or
35 * slaves when performing data transfers. A master is a device that initiates a data transfer on the bus and generates the
36 * clock signals to permit that transfer. The first byte in the data transfer always contains the 7-bit address and
37 * a read/write bit in the LSB position. This API takes care of toggling the read/write bit. After this, any device addressed
38 * is considered a slave.
39 *
40 * This API allows the controller to be set up as a master or a slave using the \ref i2c_set_slave_mode function.
41 *
42 * The external pins of each controller are connected to GPIO pins as defined in the GPIO muxing table in the datasheet. The muxing options
43 * give some IO flexibility, but each controller external pin should be connected to only one GPIO.
44 *
45 * Note that the controller does NOT support High speed mode or Ultra-fast speed mode, the fastest operation being fast mode plus
46 * at up to 1000Kb/s.
47 *
48 * See the datasheet for more information on the I2C controller and its usage.
49 *
50 * \subsection i2c_example Example
51 * \addtogroup hardware_i2c
52 * \include bus_scan.c
53 */
54
55 typedef struct i2c_inst i2c_inst_t;
56
57 // PICO_CONFIG: PICO_DEFAULT_I2C, Define the default I2C for a board, min=0, max=1, default=Usually provided via board header, group=hardware_i2c
58 // PICO_CONFIG: PICO_DEFAULT_I2C_SDA_PIN, Define the default I2C SDA pin, min=0, max=47 on RP2350B, 29 otherwise, default=Usually provided via board header, group=hardware_i2c
59 // PICO_CONFIG: PICO_DEFAULT_I2C_SCL_PIN, Define the default I2C SCL pin, min=0, max=47 on RP2350B, 29 otherwise, default=Usually provided via board header, group=hardware_i2c
60
61 /** The I2C identifiers for use in I2C functions.
62 *
63 * e.g. i2c_init(i2c0, 48000)
64 *
65 * \ingroup hardware_i2c
66 * @{
67 */
68 extern i2c_inst_t i2c0_inst;
69 extern i2c_inst_t i2c1_inst;
70
71 #define i2c0 (&i2c0_inst) ///< Identifier for I2C HW Block 0
72 #define i2c1 (&i2c1_inst) ///< Identifier for I2C HW Block 1
73
74 #if !defined(PICO_DEFAULT_I2C_INSTANCE) && defined(PICO_DEFAULT_I2C)
75 #define PICO_DEFAULT_I2C_INSTANCE() (__CONCAT(i2c,PICO_DEFAULT_I2C))
76 #endif
77
78 /**
79 * \def PICO_DEFAULT_I2C
80 * \ingroup hardware_i2c
81 * \hideinitializer
82 * \brief The default I2C instance number
83 */
84
85 /**
86 * \def PICO_DEFAULT_I2C_INSTANCE()
87 * \ingroup hardware_i2c
88 * \hideinitializer
89 * \brief Returns the default I2C instance based on the value of PICO_DEFAULT_I2C
90 */
91 #ifdef PICO_DEFAULT_I2C_INSTANCE
92 #define i2c_default PICO_DEFAULT_I2C_INSTANCE()
93 #endif
94
95 /** @} */
96
97 // ----------------------------------------------------------------------------
98 // Setup
99
100 /*! \brief Initialise the I2C HW block
101 * \ingroup hardware_i2c
102 *
103 * Put the I2C hardware into a known state, and enable it. Must be called
104 * before other functions. By default, the I2C is configured to operate as a
105 * master.
106 *
107 * The I2C bus frequency is set as close as possible to requested, and
108 * the actual rate set is returned
109 *
110 * \param i2c Either \ref i2c0 or \ref i2c1
111 * \param baudrate Baudrate in Hz (e.g. 100kHz is 100000)
112 * \return Actual set baudrate
113 */
114 uint i2c_init(i2c_inst_t *i2c, uint baudrate);
115
116 /*! \brief Disable the I2C HW block
117 * \ingroup hardware_i2c
118 *
119 * \param i2c Either \ref i2c0 or \ref i2c1
120 *
121 * Disable the I2C again if it is no longer used. Must be reinitialised before
122 * being used again.
123 */
124 void i2c_deinit(i2c_inst_t *i2c);
125
126 /*! \brief Set I2C baudrate
127 * \ingroup hardware_i2c
128 *
129 * Set I2C bus frequency as close as possible to requested, and return actual
130 * rate set.
131 * Baudrate may not be as exactly requested due to clocking limitations.
132 *
133 * \param i2c Either \ref i2c0 or \ref i2c1
134 * \param baudrate Baudrate in Hz (e.g. 100kHz is 100000)
135 * \return Actual set baudrate
136 */
137 uint i2c_set_baudrate(i2c_inst_t *i2c, uint baudrate);
138
139 /*! \brief Set I2C port to slave mode
140 * \ingroup hardware_i2c
141 *
142 * \param i2c Either \ref i2c0 or \ref i2c1
143 * \param slave true to use slave mode, false to use master mode
144 * \param addr If \p slave is true, set the slave address to this value
145 */
146 void i2c_set_slave_mode(i2c_inst_t *i2c, bool slave, uint8_t addr);
147
148 // ----------------------------------------------------------------------------
149 // Generic input/output
150
151 struct i2c_inst {
152 i2c_hw_t *hw;
153 bool restart_on_next;
154 };
155
156 /**
157 * \def I2C_NUM(i2c)
158 * \ingroup hardware_i2c
159 * \hideinitializer
160 * \brief Returns the I2C number for a I2C instance
161 *
162 * Note this macro is intended to resolve at compile time, and does no parameter checking
163 */
164 #ifndef I2C_NUM
165 static_assert(NUM_I2CS == 2, "");
166 #define I2C_NUM(i2c) ((i2c) == i2c1)
167 #endif
168
169 /**
170 * \def I2C_INSTANCE(i2c_num)
171 * \ingroup hardware_i2c
172 * \hideinitializer
173 * \brief Returns the I2C instance with the given I2C number
174 *
175 * Note this macro is intended to resolve at compile time, and does no parameter checking
176 */
177 #ifndef I2C_INSTANCE
178 static_assert(NUM_I2CS == 2, "");
179 #define I2C_INSTANCE(num) ((num) ? i2c1 : i2c0)
180 #endif
181
182 /**
183 * \def I2C_DREQ_NUM(i2c, is_tx)
184 * \ingroup hardware_i2c
185 * \hideinitializer
186 * \brief Returns the \ref dreq_num_t used for pacing DMA transfers to or from this I2C instance.
187 * If is_tx is true, then it is for transfers to the I2C instance else for transfers from the I2C instance.
188 *
189 * Note this macro is intended to resolve at compile time, and does no parameter checking
190 */
191 #ifndef I2C_DREQ_NUM
192 static_assert(DREQ_I2C0_RX == DREQ_I2C0_TX + 1, "");
193 static_assert(DREQ_I2C1_RX == DREQ_I2C1_TX + 1, "");
194 static_assert(DREQ_I2C1_TX == DREQ_I2C0_TX + 2, "");
195 #define I2C_DREQ_NUM(i2c,is_tx) (DREQ_I2C0_TX + I2C_NUM(i2c) * 2 + !(is_tx))
196 #endif
197
198 /*! \brief Convert I2C instance to hardware instance number
199 * \ingroup hardware_i2c
200 *
201 * \param i2c I2C instance
202 * \return Number of I2C, 0 or 1.
203 */
i2c_get_index(i2c_inst_t * i2c)204 static inline uint i2c_get_index(i2c_inst_t *i2c) {
205 invalid_params_if(HARDWARE_I2C, i2c != i2c0 && i2c != i2c1);
206 return I2C_NUM(i2c);
207 }
208
209 // backward compatibility
210 #define i2c_hw_index(i2c) i2c_get_index(i2c)
211
212 /*! \brief Return pointer to structure containing i2c hardware registers
213 * \ingroup hardware_i2c
214 *
215 * \param i2c I2C instance
216 * \return pointer to \ref i2c_hw_t
217 */
i2c_get_hw(i2c_inst_t * i2c)218 static inline i2c_hw_t *i2c_get_hw(i2c_inst_t *i2c) {
219 i2c_hw_index(i2c); // check it is a hw i2c
220 return i2c->hw;
221 }
222
223 /*! \brief Convert I2C hardware instance number to I2C instance
224 * \ingroup hardware_i2c
225 *
226 * \param num Number of I2C, 0 or 1
227 * \return I2C hardware instance
228 */
i2c_get_instance(uint num)229 static inline i2c_inst_t *i2c_get_instance(uint num) {
230 invalid_params_if(HARDWARE_I2C, num >= NUM_I2CS);
231 return I2C_INSTANCE(num);
232 }
233
234 /*! \brief Attempt to write specified number of bytes to address, blocking until the specified absolute time is reached.
235 * \ingroup hardware_i2c
236 *
237 * \param i2c Either \ref i2c0 or \ref i2c1
238 * \param addr 7-bit address of device to write to
239 * \param src Pointer to data to send
240 * \param len Length of data in bytes to send
241 * \param nostop If true, master retains control of the bus at the end of the transfer (no Stop is issued),
242 * and the next transfer will begin with a Restart rather than a Start.
243 * \param until The absolute time that the block will wait until the entire transaction is complete. Note, an individual timeout of
244 * this value divided by the length of data is applied for each byte transfer, so if the first or subsequent
245 * bytes fails to transfer within that sub timeout, the function will return with an error.
246 *
247 * \return Number of bytes written, or PICO_ERROR_GENERIC if address not acknowledged, no device present, or PICO_ERROR_TIMEOUT if a timeout occurred.
248 */
249 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);
250
251 /*! \brief Attempt to read specified number of bytes from address, blocking until the specified absolute time is reached.
252 * \ingroup hardware_i2c
253 *
254 * \param i2c Either \ref i2c0 or \ref i2c1
255 * \param addr 7-bit address of device to read from
256 * \param dst Pointer to buffer to receive data
257 * \param len Length of data in bytes to receive
258 * \param nostop If true, master retains control of the bus at the end of the transfer (no Stop is issued),
259 * and the next transfer will begin with a Restart rather than a Start.
260 * \param until The absolute time that the block will wait until the entire transaction is complete.
261 * \return Number of bytes read, or PICO_ERROR_GENERIC if address not acknowledged, no device present, or PICO_ERROR_TIMEOUT if a timeout occurred.
262 */
263 int i2c_read_blocking_until(i2c_inst_t *i2c, uint8_t addr, uint8_t *dst, size_t len, bool nostop, absolute_time_t until);
264
265 /*! \brief Attempt to write specified number of bytes to address, with timeout
266 * \ingroup hardware_i2c
267 *
268 * \param i2c Either \ref i2c0 or \ref i2c1
269 * \param addr 7-bit address of device to write to
270 * \param src Pointer to data to send
271 * \param len Length of data in bytes to send
272 * \param nostop If true, master retains control of the bus at the end of the transfer (no Stop is issued),
273 * and the next transfer will begin with a Restart rather than a Start.
274 * \param timeout_us The time that the function will wait for the entire transaction to complete. Note, an individual timeout of
275 * this value divided by the length of data is applied for each byte transfer, so if the first or subsequent
276 * bytes fails to transfer within that sub timeout, the function will return with an error.
277 *
278 * \return Number of bytes written, or PICO_ERROR_GENERIC if address not acknowledged, no device present, or PICO_ERROR_TIMEOUT if a timeout occurred.
279 */
i2c_write_timeout_us(i2c_inst_t * i2c,uint8_t addr,const uint8_t * src,size_t len,bool nostop,uint timeout_us)280 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) {
281 absolute_time_t t = make_timeout_time_us(timeout_us);
282 return i2c_write_blocking_until(i2c, addr, src, len, nostop, t);
283 }
284
285 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);
286
287 /*! \brief Attempt to read specified number of bytes from address, with timeout
288 * \ingroup hardware_i2c
289 *
290 * \param i2c Either \ref i2c0 or \ref i2c1
291 * \param addr 7-bit address of device to read from
292 * \param dst Pointer to buffer to receive data
293 * \param len Length of data in bytes to receive
294 * \param nostop If true, master retains control of the bus at the end of the transfer (no Stop is issued),
295 * and the next transfer will begin with a Restart rather than a Start.
296 * \param timeout_us The time that the function will wait for the entire transaction to complete
297 * \return Number of bytes read, or PICO_ERROR_GENERIC if address not acknowledged, no device present, or PICO_ERROR_TIMEOUT if a timeout occurred.
298 */
i2c_read_timeout_us(i2c_inst_t * i2c,uint8_t addr,uint8_t * dst,size_t len,bool nostop,uint timeout_us)299 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) {
300 absolute_time_t t = make_timeout_time_us(timeout_us);
301 return i2c_read_blocking_until(i2c, addr, dst, len, nostop, t);
302 }
303
304 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);
305
306 /*! \brief Attempt to write specified number of bytes to address, blocking
307 * \ingroup hardware_i2c
308 *
309 * \param i2c Either \ref i2c0 or \ref i2c1
310 * \param addr 7-bit address of device to write to
311 * \param src Pointer to data to send
312 * \param len Length of data in bytes to send
313 * \param nostop If true, master retains control of the bus at the end of the transfer (no Stop is issued),
314 * and the next transfer will begin with a Restart rather than a Start.
315 * \return Number of bytes written, or PICO_ERROR_GENERIC if address not acknowledged, no device present.
316 */
317 int i2c_write_blocking(i2c_inst_t *i2c, uint8_t addr, const uint8_t *src, size_t len, bool nostop);
318
319 /*! \brief Attempt to write specified number of bytes to address, blocking in burst mode
320 * \ingroup hardware_i2c
321 *
322 * This version of the function will not issue a stop and will not restart on the next write.
323 * This allows you to write consecutive bytes of data without having to resend a stop bit and
324 * (for example) without having to send address byte(s) repeatedly
325 *
326 * \param i2c Either \ref i2c0 or \ref i2c1
327 * \param addr 7-bit address of device to read from
328 * \param dst Pointer to buffer to receive data
329 * \param len Length of data in bytes to receive
330 * \return Number of bytes read, or PICO_ERROR_GENERIC if address not acknowledged or no device present.
331 */
332 int i2c_write_burst_blocking(i2c_inst_t *i2c, uint8_t addr, const uint8_t *src, size_t len);
333
334 /*! \brief Attempt to read specified number of bytes from address, blocking
335 * \ingroup hardware_i2c
336 *
337 * \param i2c Either \ref i2c0 or \ref i2c1
338 * \param addr 7-bit address of device to read from
339 * \param dst Pointer to buffer to receive data
340 * \param len Length of data in bytes to receive
341 * \param nostop If true, master retains control of the bus at the end of the transfer (no Stop is issued),
342 * and the next transfer will begin with a Restart rather than a Start.
343 * \return Number of bytes read, or PICO_ERROR_GENERIC if address not acknowledged or no device present.
344 */
345 int i2c_read_blocking(i2c_inst_t *i2c, uint8_t addr, uint8_t *dst, size_t len, bool nostop);
346
347 /*! \brief Attempt to read specified number of bytes from address, blocking in burst mode
348 * \ingroup hardware_i2c
349 *
350 * This version of the function will not issue a stop and will not restart on the next read.
351 * This allows you to read consecutive bytes of data without having to resend a stop bit and
352 * (for example) without having to send address byte(s) repeatedly
353 *
354 * \param i2c Either \ref i2c0 or \ref i2c1
355 * \param addr 7-bit address of device to read from
356 * \param dst Pointer to buffer to receive data
357 * \param len Length of data in bytes to receive
358 * \return Number of bytes read, or PICO_ERROR_GENERIC if address not acknowledged or no device present.
359 */
360 int i2c_read_burst_blocking(i2c_inst_t *i2c, uint8_t addr, uint8_t *dst, size_t len);
361
362 /*! \brief Determine non-blocking write space available
363 * \ingroup hardware_i2c
364 *
365 * \param i2c Either \ref i2c0 or \ref i2c1
366 * \return 0 if no space is available in the I2C to write more data. If return is nonzero, at
367 * least that many bytes can be written without blocking.
368 */
i2c_get_write_available(i2c_inst_t * i2c)369 static inline size_t i2c_get_write_available(i2c_inst_t *i2c) {
370 const size_t IC_TX_BUFFER_DEPTH = 16;
371 return IC_TX_BUFFER_DEPTH - i2c_get_hw(i2c)->txflr;
372 }
373
374 /*! \brief Determine number of bytes received
375 * \ingroup hardware_i2c
376 *
377 * \param i2c Either \ref i2c0 or \ref i2c1
378 * \return 0 if no data available, if return is nonzero at
379 * least that many bytes can be read without blocking.
380 */
i2c_get_read_available(i2c_inst_t * i2c)381 static inline size_t i2c_get_read_available(i2c_inst_t *i2c) {
382 return i2c_get_hw(i2c)->rxflr;
383 }
384
385 /*! \brief Write direct to TX FIFO
386 * \ingroup hardware_i2c
387 *
388 * \param i2c Either \ref i2c0 or \ref i2c1
389 * \param src Data to send
390 * \param len Number of bytes to send
391 *
392 * Writes directly to the I2C TX FIFO which is mainly useful for
393 * slave-mode operation.
394 */
i2c_write_raw_blocking(i2c_inst_t * i2c,const uint8_t * src,size_t len)395 static inline void i2c_write_raw_blocking(i2c_inst_t *i2c, const uint8_t *src, size_t len) {
396 for (size_t i = 0; i < len; ++i) {
397 // TODO NACK or STOP on end?
398 while (!i2c_get_write_available(i2c))
399 tight_loop_contents();
400 i2c_get_hw(i2c)->data_cmd = *src++;
401 }
402 }
403
404 /*! \brief Read direct from RX FIFO
405 * \ingroup hardware_i2c
406 *
407 * \param i2c Either \ref i2c0 or \ref i2c1
408 * \param dst Buffer to accept data
409 * \param len Number of bytes to read
410 *
411 * Reads directly from the I2C RX FIFO which is mainly useful for
412 * slave-mode operation.
413 */
i2c_read_raw_blocking(i2c_inst_t * i2c,uint8_t * dst,size_t len)414 static inline void i2c_read_raw_blocking(i2c_inst_t *i2c, uint8_t *dst, size_t len) {
415 for (size_t i = 0; i < len; ++i) {
416 while (!i2c_get_read_available(i2c))
417 tight_loop_contents();
418 *dst++ = (uint8_t)i2c_get_hw(i2c)->data_cmd;
419 }
420 }
421
422 /**
423 * \brief Pop a byte from I2C Rx FIFO.
424 * \ingroup hardware_i2c
425 *
426 * This function is non-blocking and assumes the Rx FIFO isn't empty.
427 *
428 * \param i2c I2C instance.
429 * \return uint8_t Byte value.
430 */
i2c_read_byte_raw(i2c_inst_t * i2c)431 static inline uint8_t i2c_read_byte_raw(i2c_inst_t *i2c) {
432 i2c_hw_t *hw = i2c_get_hw(i2c);
433 assert(hw->status & I2C_IC_STATUS_RFNE_BITS); // Rx FIFO must not be empty
434 return (uint8_t)hw->data_cmd;
435 }
436
437 /**
438 * \brief Push a byte into I2C Tx FIFO.
439 * \ingroup hardware_i2c
440 *
441 * This function is non-blocking and assumes the Tx FIFO isn't full.
442 *
443 * \param i2c I2C instance.
444 * \param value Byte value.
445 */
i2c_write_byte_raw(i2c_inst_t * i2c,uint8_t value)446 static inline void i2c_write_byte_raw(i2c_inst_t *i2c, uint8_t value) {
447 i2c_hw_t *hw = i2c_get_hw(i2c);
448 assert(hw->status & I2C_IC_STATUS_TFNF_BITS); // Tx FIFO must not be full
449 hw->data_cmd = value;
450 }
451
452 /*! \brief Return the DREQ to use for pacing transfers to/from a particular I2C instance
453 * \ingroup hardware_i2c
454 *
455 * \param i2c Either \ref i2c0 or \ref i2c1
456 * \param is_tx true for sending data to the I2C instance, false for receiving data from the I2C instance
457 */
i2c_get_dreq(i2c_inst_t * i2c,bool is_tx)458 static inline uint i2c_get_dreq(i2c_inst_t *i2c, bool is_tx) {
459 return I2C_DREQ_NUM(i2c, is_tx);
460 }
461
462 #ifdef __cplusplus
463 }
464 #endif
465
466 #endif
467