Mbed OS Reference
Loading...
Searching...
No Matches
i2c_api.h
1
2/** \addtogroup hal */
3/** @{*/
4/* mbed Microcontroller Library
5 * Copyright (c) 2006-2015 ARM Limited
6 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20#ifndef MBED_I2C_API_H
21#define MBED_I2C_API_H
22
23#include "device.h"
24#include "pinmap.h"
25#include "hal/buffer.h"
26
27#if DEVICE_I2C_ASYNCH
28#include "hal/dma_api.h"
29#endif
30
31#if DEVICE_I2C
32
33/**
34 * @defgroup hal_I2CEvents I2C Events Macros
35 *
36 * @{
37 */
38
39/**
40 * Indicates that an unspecified error has occurred in the transfer. This usually means
41 * either an internal error in the Mbed MCU's I2C module, or something like an arbitration loss.
42 * Does not indicate a NACK.
43 */
44#define I2C_EVENT_ERROR (1 << 1)
45
46/**
47 * Indicates that the slave did not respond to the address byte of the transfer.
48 */
49#define I2C_EVENT_ERROR_NO_SLAVE (1 << 2)
50
51/**
52 * Indicates that the transfer completed successfully.
53 */
54#define I2C_EVENT_TRANSFER_COMPLETE (1 << 3)
55
56/**
57 * Indicates that a NACK was received after the address byte, but before the requested number of bytes
58 * could be transferred.
59 *
60 * Note: Not every manufacturer HAL is able to make a distinction between this flag and #I2C_EVENT_ERROR_NO_SLAVE.
61 * On a NACK, you might conceivably get one or both of these flags.
62 */
63#define I2C_EVENT_TRANSFER_EARLY_NACK (1 << 4)
64
65/**
66 * Use this macro to request all possible I2C events.
67 */
68#define I2C_EVENT_ALL (I2C_EVENT_ERROR | I2C_EVENT_TRANSFER_COMPLETE | I2C_EVENT_ERROR_NO_SLAVE | I2C_EVENT_TRANSFER_EARLY_NACK)
69
70/**@}*/
71
72/// Enumeration of states the I2C HAL can be in
74 MBED_HAL_I2C_STATE_IDLE, ///< Not in the middle of a transaction
75 MBED_HAL_I2C_STATE_STARTED, ///< start() has been called but not write_byte() or read_byte()
76 MBED_HAL_I2C_STATE_ADDRESSED, ///< write_byte() has been called after start()
77 MBED_HAL_I2C_STATE_BYTE_READ, ///< In the middle of a single-byte read
78 MBED_HAL_I2C_STATE_BYTE_WRITE, ///< In the middle of a single-byte read
79 MBED_HAL_I2C_STATE_HOLDING_BUS, ///< We just completed a transaction with the repeated option set, so we are still holding the bus
80 MBED_HAL_I2C_STATE_ASYNC_TRANSACTION, ///< In an async transaction
81};
82
83
84typedef struct {
85 struct i2c_s i2c; /**< Target specific I2C structure */
86 volatile enum mbed_hal_i2c_state state; ///< I2C state. Managed by the I2C class, available for use by the HAL.
87#if DEVICE_I2C_ASYNCH
88 struct buffer_s tx_buff; /**< Tx buffer */
89 struct buffer_s rx_buff; /**< Rx buffer */
90#endif
91} i2c_t;
92
93enum {
94 I2C_ERROR_NO_SLAVE = -1,
95 I2C_ERROR_BUS_BUSY = -2,
96 I2C_ERROR_INVALID_USAGE = -3, // Invalid usage of the I2C API, e.g. by mixing single-byte and transactional function calls.
97 I2C_ERROR_OTHER = -4, ///< Other internal error
98};
99
100typedef struct {
101 int peripheral;
102 PinName sda_pin;
103 int sda_function;
104 PinName scl_pin;
105 int scl_function;
107
108/**
109 * Describes the capabilities of an I2C peripheral
110 */
111typedef struct {
112 /// If true, generation of the start condition in single-byte mode is delayed until the address is transmitted.
113 /// If single-byte is not supported this is a don't care.
115
116 /// If true, transmission of the address in single-byte mode is delayed until the first data byte is read/written.
117 /// For write transactions, the ACK/NACK from the address byte will be reported in the status of the first data byte.
118 /// Note that this means there is no way to detect a NACK of the address for a single-byte read operation.
119 /// If single-byte is not supported this is a don't care.
121
122 bool supports_single_byte; ///< If true, the single-byte API is implemented. Otherwise, it is a no-op.
123
124 /// If true, the single-byte API supports a zero-length transfer (one that has only a start, then an address, then a stop).
125 /// If single-byte is not supported this is a don't care.
127
128 /// If true, the transaction-based API supports a zero-length transfer (one that has only a start, then an address, then a stop).
130
132
133#ifdef __cplusplus
134extern "C" {
135#endif
136
137/**
138 * \defgroup hal_GeneralI2C I2C Configuration Functions
139 *
140 * # Defined behavior
141 * * ::i2c_init initializes i2c_t control structure
142 * * ::i2c_init configures the pins used by I2C
143 * * ::i2c_free returns the pins owned by the I2C object to their reset state
144 * * ::i2c_frequency configure the I2C frequency
145 * * ::i2c_start sends START command
146 * * ::i2c_read reads `length` bytes from the I2C slave specified by `address` to the `data` buffer
147 * * ::i2c_read reads generates a stop condition on the bus at the end of the transfer if `stop` parameter is non-zero
148 * * ::i2c_read reads returns the number of symbols received from the bus
149 * * ::i2c_write writes `length` bytes to the I2C slave specified by `address` from the `data` buffer
150 * * ::i2c_write generates a stop condition on the bus at the end of the transfer if `stop` parameter is non-zero
151 * * ::i2c_write returns zero on success, error code otherwise
152 * * ::i2c_reset resets the I2C peripheral
153 * * ::i2c_byte_read reads and return one byte from the specfied I2C slave
154 * * ::i2c_byte_read uses `last` parameter to inform the slave that all bytes have been read
155 * * ::i2c_byte_write writes one byte to the specified I2C slave
156 * * ::i2c_byte_write returns 0 if NAK was received, 1 if ACK was received, 2 for timeout
157 * * ::i2c_slave_mode enables/disables I2S slave mode
158 * * ::i2c_slave_receive returns: 1 - read addresses, 2 - write to all slaves, 3 write addressed, 0 - the slave has not been addressed
159 * * ::i2c_slave_read reads `length` bytes from the I2C master to the `data` buffer
160 * * ::i2c_slave_read returns non-zero if a value is available, 0 otherwise
161 * * ::i2c_slave_write writes `length` bytes to the I2C master from the `data` buffer
162 * * ::i2c_slave_write returns non-zero if a value is available, 0 otherwise
163 * * ::i2c_slave_address configures I2C slave address
164 * * ::i2c_transfer_asynch starts I2C asynchronous transfer
165 * * ::i2c_transfer_asynch writes `tx_length` bytes to the I2C slave specified by `address` from the `tx` buffer
166 * * ::i2c_transfer_asynch reads `rx_length` bytes from the I2C slave specified by `address` to the `rx` buffer
167 * * ::i2c_transfer_asynch generates a stop condition on the bus at the end of the transfer if `stop` parameter is non-zero
168 * * The callback given to ::i2c_transfer_asynch is invoked when the transfer completes
169 * * ::i2c_transfer_asynch specifies the logical OR of events to be registered
170 * * The ::i2c_transfer_asynch function may use the `DMAUsage` hint to select the appropriate async algorithm
171 * * ::i2c_irq_handler_asynch returns event flags if a transfer termination condition was met, otherwise returns 0.
172 * * ::i2c_active returns non-zero if the I2C module is active or 0 if it is not
173 * * ::i2c_abort_asynch aborts an on-going async transfer
174 *
175 * # Undefined behavior
176 * * Calling ::i2c_init multiple times on the same `i2c_t`
177 * * Calling any function other than ::i2c_init on a non-initialized `i2c_t`
178 * * Initialising the `I2C` peripheral with invalid `SDA` and `SCL` pins.
179 * * Passing pins that cannot be on the same peripheral
180 * * Passing an invalid pointer as `obj` to any function
181 * * Use of a `null` pointer as an argument to any function.
182 * * Initialising the peripheral in slave mode if slave mode is not supported
183 * * Operating the peripheral in slave mode without first specifying and address using ::i2c_slave_address
184 * * Setting an address using i2c_slave_address after initialising the peripheral in master mode
185 * * Setting an address to an `I2C` reserved value
186 * * Specifying an invalid address when calling any `read` or `write` functions
187 * * Setting the length of the transfer or receive buffers to larger than the buffers are
188 * * Passing an invalid pointer as `handler`
189 * * Calling ::i2c_abort_asynch when no transfer is currently in progress
190 *
191 *
192 * @{
193 */
194
195/**
196 * \defgroup hal_GeneralI2C_tests I2C hal tests
197 * The I2C HAL tests ensure driver conformance to defined behaviour.
198 *
199 * To run the I2C hal tests use the command:
200 *
201 * mbed test -t <toolchain> -m <target> -n tests-mbed_hal_fpga_ci_test_shield-i2c
202 *
203 */
204
205/** Initialize the I2C peripheral. It sets the default parameters for I2C
206 * peripheral, and configures its specifieds pins.
207 *
208 * @param obj The I2C object
209 * @param pinmap Pinmap pointer to structure which holds static pinmap
210 */
211void i2c_init_direct(i2c_t *obj, const i2c_pinmap_t *pinmap);
212
213/** Initialize the I2C peripheral. It sets the default parameters for I2C
214 * peripheral, and configures its specifieds pins.
215 *
216 * @param obj The I2C object
217 * @param sda The sda pin
218 * @param scl The scl pin
219 */
220void i2c_init(i2c_t *obj, PinName sda, PinName scl);
221
222/** Release a I2C object
223 *
224 * Return the pins owned by the I2C object to their reset state
225 * @param obj The I2C object to deinitialize
226 */
227void i2c_free(i2c_t *obj);
228
229/** Configure the I2C frequency
230 *
231 * @param obj The I2C object
232 * @param hz Frequency in Hz
233 */
234void i2c_frequency(i2c_t *obj, int hz);
235
236/**
237 * @brief Blocking read of data
238 *
239 * This function may be called in these I2C states:
240 * - IDLE
241 * - HOLDING_BUS
242 * - BYTE_READ (to do a repeated start after a single byte transaction)
243 * - BYTE_WRITE (to do a repeated start after a single byte transaction)
244 *
245 * @param obj The I2C object
246 * @param address 8/11-bit address (last bit is 1)
247 * @param data The buffer for receiving
248 * @param length Number of bytes to read. Allowed to be 0 *only* if the \c supports_zero_length_transfer_transaction
249 * capability is true.
250 * @param stop Stop to be generated after the transfer is done
251 * @return Number of read bytes
252 */
253int i2c_read(i2c_t *obj, int address, char *data, int length, int stop);
254
255/**
256 * @brief Blocking write of data
257 *
258 * This function may be called in these I2C states:
259 * - IDLE
260 * - HOLDING_BUS
261 * - BYTE_READ (to do a repeated start after a single byte transaction)
262 * - BYTE_WRITE (to do a repeated start after a single byte transaction)
263 *
264 * @param obj The I2C object
265 * @param address 8/11-bit address (last bit is 0)
266 * @param data The buffer for sending
267 * @param length Number of bytes to write. Allowed to be 0 *only* if the \c supports_zero_length_transfer_transaction
268 * capability is true.
269 * @param stop Stop to be generated after the transfer is done
270 * @retval zero or non-zero - Number of written bytes
271 * @retval negative - I2C_ERROR_XXX status
272 */
273int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop);
274
275/**
276 * @brief Send START command
277 *
278 * The I2C class guarantees that this will only be called at appropriate times (when the bus is idle or
279 * during an existing single-byte transaction to do a repeated start).
280 *
281 * Note that some I2C peripherals (e.g. RP2xxx, newer STM32s) are not capable of sending a start condition
282 * until the address is known. On these MCUs, the start condition will not actually be sent until the first write_byte()
283 * call after the start().
284 *
285 * This function may be called in these I2C states:
286 * - IDLE
287 * - HOLDING_BUS
288 * - ADDRESSED (to do a repeated start after a zero length transaction, if supported)
289 * - BYTE_READ
290 * - BYTE_WRITE
291 *
292 * @param obj The I2C object
293 */
294int i2c_start(i2c_t *obj);
295
296/**
297 * @brief Send STOP command
298 *
299 * This function may be called in these I2C states:
300 * - ADDRESSED (to complete a zero length transaction, *only* allowed if the \c supports_zero_length_transfer_single_byte capability is enabled)
301 * - BYTE_READ
302 * - BYTE_WRITE
303 *
304 * @param obj The I2C object
305 */
306int i2c_stop(i2c_t *obj);
307
308/** Reset I2C peripheral. TODO: The action here. Most of the implementation sends stop()
309 *
310 * @param obj The I2C object
311 */
312void i2c_reset(i2c_t *obj);
313
314/**
315 * @brief Read one byte
316 *
317 * This function may be called in these I2C states:
318 * - ADDRESSED
319 * - BYTE_READ
320 *
321 * @param obj The I2C object
322 * @param last Acknoledge
323 * @return The read byte
324 */
325int i2c_byte_read(i2c_t *obj, int last);
326
327/**
328 * @brief Write one byte
329 *
330 * This function may be called in these I2C states:
331 * - STARTED (to send the address byte)
332 * - ADDRESSED
333 * - BYTE_WRITE
334 *
335 * @param obj The I2C object
336 * @param data Byte to be written
337 * @return 0 if NAK was received, 1 if ACK was received, 2 for timeout, or 3 for other error.
338 */
339int i2c_byte_write(i2c_t *obj, int data);
340
341/** Get the pins that support I2C SDA
342 *
343 * Return a PinMap array of pins that support I2C SDA in
344 * master mode. The array is terminated with {NC, NC, 0}.
345 *
346 * @return PinMap array
347 */
349
350/** Get the pins that support I2C SCL
351 *
352 * Return a PinMap array of pins that support I2C SCL in
353 * master mode. The array is terminated with {NC, NC, 0}.
354 *
355 * @return PinMap array
356 */
358
359/** Get the pins that support I2C SDA
360 *
361 * Return a PinMap array of pins that support I2C SDA in
362 * slave mode. The array is terminated with {NC, NC, 0}.
363 *
364 * @return PinMap array
365 */
367
368/** Get the pins that support I2C SCL
369 *
370 * Return a PinMap array of pins that support I2C SCL in
371 * slave mode. The array is terminated with {NC, NC, 0}.
372 *
373 * @return PinMap array
374 */
376
377/**
378 * Fills the given i2c_capabilities_t structure with the capabilities of the I2C peripheral.
379 *
380 * @returns Pointer to constant capabilities data
381 */
383
384/**@}*/
385
386#if DEVICE_I2CSLAVE
387
388/**
389 * \defgroup SynchI2C Synchronous I2C Hardware Abstraction Layer for slave
390 * @{
391 */
392
393/** Configure I2C as slave or master.
394 * @param obj The I2C object
395 * @param enable_slave Enable i2c hardware so you can receive events with ::i2c_slave_receive
396 */
397void i2c_slave_mode(i2c_t *obj, int enable_slave);
398
399/** Check to see if the I2C slave has been addressed.
400 * @param obj The I2C object
401 * @return The status - 1 - read addresses, 2 - write to all slaves,
402 * 3 write addressed, 0 - the slave has not been addressed
403 */
405
406/**
407 * @brief Read specified number of bytes from an I2C master.
408 *
409 * This function shall be called only after ::i2c_slave_receive() has returned 2 or 3.
410 *
411 * @param obj The I2C object
412 * @param data The buffer for receiving
413 * @param length Number of bytes to read. If the master writes more than this number
414 * of bytes, the additional bytes are dropped.
415 *
416 * @return Number of bytes read, or zero on error
417 */
418int i2c_slave_read(i2c_t *obj, char *data, int length);
419
420/** Configure I2C as slave or master.
421 * @param obj The I2C object
422 * @param data The buffer for sending
423 * @param length Number of bytes to write
424 * @return number of bytes actually written to the master, or negative value on error.
425 */
426int i2c_slave_write(i2c_t *obj, const char *data, int length);
427
428/** Configure I2C address.
429 * @param obj The I2C object
430 * @param idx Currently not used
431 * @param address The address to be set (8-bit, always a read address)
432 * @param mask Currently not used
433 */
434void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask);
435
436#endif
437
438/**@}*/
439
440#if DEVICE_I2C_ASYNCH
441
442/**
443 * \defgroup hal_AsynchI2C Asynchronous I2C Hardware Abstraction Layer
444 * @{
445 */
446
447/** Start I2C asynchronous transfer
448 *
449 * @param obj The I2C object
450 * @param tx The transmit buffer
451 * @param tx_length The number of bytes to transmit
452 * @param rx The receive buffer
453 * @param rx_length The number of bytes to receive
454 * @param address The address to be set - 8bit or 11bit
455 * @param stop If true, stop will be generated after the transfer is done
456 * @param handler The I2C IRQ handler to be set
457 * @param event Event mask for the transfer. See \ref hal_I2CEvents
458 * @param hint DMA hint usage
459 */
460void i2c_transfer_asynch(i2c_t *obj, const void *tx, size_t tx_length, void *rx, size_t rx_length, uint32_t address, uint32_t stop, uint32_t handler, uint32_t event, DMAUsage hint);
461
462/** The asynchronous IRQ handler
463 *
464 * @param obj The I2C object which holds the transfer information
465 * @return Event flags if a transfer termination condition was met, otherwise return 0.
466 */
468
469/** Attempts to determine if the I2C peripheral is already in use
470 *
471 * @param obj The I2C object
472 * @return Non-zero if the I2C module is active or zero if it is not
473 */
474uint8_t i2c_active(i2c_t *obj);
475
476/** Abort asynchronous transfer
477 *
478 * This function does not perform any check - that should happen in upper layers.
479 * @param obj The I2C object
480 */
482
483#endif
484
485/**@}*/
486
487#ifdef __cplusplus
488}
489#endif
490
491#endif
492
493#endif
494
495/** @}*/
void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask)
Configure I2C address.
int i2c_slave_receive(i2c_t *obj)
Check to see if the I2C slave has been addressed.
int i2c_slave_write(i2c_t *obj, const char *data, int length)
Configure I2C as slave or master.
int i2c_slave_read(i2c_t *obj, char *data, int length)
Read specified number of bytes from an I2C master.
void i2c_slave_mode(i2c_t *obj, int enable_slave)
Configure I2C as slave or master.
uint8_t i2c_active(i2c_t *obj)
Attempts to determine if the I2C peripheral is already in use.
uint32_t i2c_irq_handler_asynch(i2c_t *obj)
The asynchronous IRQ handler.
void i2c_abort_asynch(i2c_t *obj)
Abort asynchronous transfer.
void i2c_transfer_asynch(i2c_t *obj, const void *tx, size_t tx_length, void *rx, size_t rx_length, uint32_t address, uint32_t stop, uint32_t handler, uint32_t event, DMAUsage hint)
Start I2C asynchronous transfer.
void i2c_init(i2c_t *obj, PinName sda, PinName scl)
Initialize the I2C peripheral.
i2c_capabilities_t const * i2c_get_capabilities()
Fills the given i2c_capabilities_t structure with the capabilities of the I2C peripheral.
const PinMap * i2c_slave_sda_pinmap(void)
Get the pins that support I2C SDA.
const PinMap * i2c_slave_scl_pinmap(void)
Get the pins that support I2C SCL.
void i2c_init_direct(i2c_t *obj, const i2c_pinmap_t *pinmap)
Initialize the I2C peripheral.
int i2c_byte_write(i2c_t *obj, int data)
Write one byte.
const PinMap * i2c_master_scl_pinmap(void)
Get the pins that support I2C SCL.
int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)
Blocking write of data.
int i2c_start(i2c_t *obj)
Send START command.
int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
Blocking read of data.
int i2c_stop(i2c_t *obj)
Send STOP command.
void i2c_reset(i2c_t *obj)
Reset I2C peripheral.
void i2c_frequency(i2c_t *obj, int hz)
Configure the I2C frequency.
int i2c_byte_read(i2c_t *obj, int last)
Read one byte.
const PinMap * i2c_master_sda_pinmap(void)
Get the pins that support I2C SDA.
void i2c_free(i2c_t *obj)
Release a I2C object.
mbed_hal_i2c_state
Enumeration of states the I2C HAL can be in.
Definition i2c_api.h:73
DMAUsage
Enumeration of possible DMA usage hints.
Definition dma_api.h:32
@ MBED_HAL_I2C_STATE_ASYNC_TRANSACTION
In an async transaction.
Definition i2c_api.h:80
@ MBED_HAL_I2C_STATE_BYTE_READ
In the middle of a single-byte read.
Definition i2c_api.h:77
@ MBED_HAL_I2C_STATE_ADDRESSED
write_byte() has been called after start()
Definition i2c_api.h:76
@ MBED_HAL_I2C_STATE_IDLE
Not in the middle of a transaction.
Definition i2c_api.h:74
@ MBED_HAL_I2C_STATE_STARTED
start() has been called but not write_byte() or read_byte()
Definition i2c_api.h:75
@ MBED_HAL_I2C_STATE_BYTE_WRITE
In the middle of a single-byte read.
Definition i2c_api.h:78
@ MBED_HAL_I2C_STATE_HOLDING_BUS
We just completed a transaction with the repeated option set, so we are still holding the bus.
Definition i2c_api.h:79
@ I2C_ERROR_OTHER
Other internal error.
Definition i2c_api.h:97
Generic buffer structure.
Definition buffer.h:27
Describes the capabilities of an I2C peripheral.
Definition i2c_api.h:111
bool supports_single_byte
If true, the single-byte API is implemented. Otherwise, it is a no-op.
Definition i2c_api.h:122
bool supports_zero_length_transfer_single_byte
If true, the single-byte API supports a zero-length transfer (one that has only a start,...
Definition i2c_api.h:126
bool single_byte_start_cond_delayed
If true, generation of the start condition in single-byte mode is delayed until the address is transm...
Definition i2c_api.h:114
bool single_byte_address_delayed
If true, transmission of the address in single-byte mode is delayed until the first data byte is read...
Definition i2c_api.h:120
bool supports_zero_length_transfer_transaction
If true, the transaction-based API supports a zero-length transfer (one that has only a start,...
Definition i2c_api.h:129
enum mbed_hal_i2c_state state
I2C state. Managed by the I2C class, available for use by the HAL.
Definition i2c_api.h:86