Marco Zecchini
/
Example_RTOS
Rtos API example
mbed-os/hal/i2c_api.h@0:9fca2b23d0ba, 2019-02-23 (annotated)
- Committer:
- marcozecchini
- Date:
- Sat Feb 23 12:13:36 2019 +0000
- Revision:
- 0:9fca2b23d0ba
final commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
marcozecchini | 0:9fca2b23d0ba | 1 | |
marcozecchini | 0:9fca2b23d0ba | 2 | /** \addtogroup hal */ |
marcozecchini | 0:9fca2b23d0ba | 3 | /** @{*/ |
marcozecchini | 0:9fca2b23d0ba | 4 | /* mbed Microcontroller Library |
marcozecchini | 0:9fca2b23d0ba | 5 | * Copyright (c) 2006-2015 ARM Limited |
marcozecchini | 0:9fca2b23d0ba | 6 | * |
marcozecchini | 0:9fca2b23d0ba | 7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
marcozecchini | 0:9fca2b23d0ba | 8 | * you may not use this file except in compliance with the License. |
marcozecchini | 0:9fca2b23d0ba | 9 | * You may obtain a copy of the License at |
marcozecchini | 0:9fca2b23d0ba | 10 | * |
marcozecchini | 0:9fca2b23d0ba | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
marcozecchini | 0:9fca2b23d0ba | 12 | * |
marcozecchini | 0:9fca2b23d0ba | 13 | * Unless required by applicable law or agreed to in writing, software |
marcozecchini | 0:9fca2b23d0ba | 14 | * distributed under the License is distributed on an "AS IS" BASIS, |
marcozecchini | 0:9fca2b23d0ba | 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
marcozecchini | 0:9fca2b23d0ba | 16 | * See the License for the specific language governing permissions and |
marcozecchini | 0:9fca2b23d0ba | 17 | * limitations under the License. |
marcozecchini | 0:9fca2b23d0ba | 18 | */ |
marcozecchini | 0:9fca2b23d0ba | 19 | #ifndef MBED_I2C_API_H |
marcozecchini | 0:9fca2b23d0ba | 20 | #define MBED_I2C_API_H |
marcozecchini | 0:9fca2b23d0ba | 21 | |
marcozecchini | 0:9fca2b23d0ba | 22 | #include "device.h" |
marcozecchini | 0:9fca2b23d0ba | 23 | #include "hal/buffer.h" |
marcozecchini | 0:9fca2b23d0ba | 24 | |
marcozecchini | 0:9fca2b23d0ba | 25 | #if DEVICE_I2C_ASYNCH |
marcozecchini | 0:9fca2b23d0ba | 26 | #include "hal/dma_api.h" |
marcozecchini | 0:9fca2b23d0ba | 27 | #endif |
marcozecchini | 0:9fca2b23d0ba | 28 | |
marcozecchini | 0:9fca2b23d0ba | 29 | #if DEVICE_I2C |
marcozecchini | 0:9fca2b23d0ba | 30 | |
marcozecchini | 0:9fca2b23d0ba | 31 | /** |
marcozecchini | 0:9fca2b23d0ba | 32 | * @defgroup hal_I2CEvents I2C Events Macros |
marcozecchini | 0:9fca2b23d0ba | 33 | * |
marcozecchini | 0:9fca2b23d0ba | 34 | * @{ |
marcozecchini | 0:9fca2b23d0ba | 35 | */ |
marcozecchini | 0:9fca2b23d0ba | 36 | #define I2C_EVENT_ERROR (1 << 1) |
marcozecchini | 0:9fca2b23d0ba | 37 | #define I2C_EVENT_ERROR_NO_SLAVE (1 << 2) |
marcozecchini | 0:9fca2b23d0ba | 38 | #define I2C_EVENT_TRANSFER_COMPLETE (1 << 3) |
marcozecchini | 0:9fca2b23d0ba | 39 | #define I2C_EVENT_TRANSFER_EARLY_NACK (1 << 4) |
marcozecchini | 0:9fca2b23d0ba | 40 | #define I2C_EVENT_ALL (I2C_EVENT_ERROR | I2C_EVENT_TRANSFER_COMPLETE | I2C_EVENT_ERROR_NO_SLAVE | I2C_EVENT_TRANSFER_EARLY_NACK) |
marcozecchini | 0:9fca2b23d0ba | 41 | |
marcozecchini | 0:9fca2b23d0ba | 42 | /**@}*/ |
marcozecchini | 0:9fca2b23d0ba | 43 | |
marcozecchini | 0:9fca2b23d0ba | 44 | #if DEVICE_I2C_ASYNCH |
marcozecchini | 0:9fca2b23d0ba | 45 | /** Asynch I2C HAL structure |
marcozecchini | 0:9fca2b23d0ba | 46 | */ |
marcozecchini | 0:9fca2b23d0ba | 47 | typedef struct { |
marcozecchini | 0:9fca2b23d0ba | 48 | struct i2c_s i2c; /**< Target specific I2C structure */ |
marcozecchini | 0:9fca2b23d0ba | 49 | struct buffer_s tx_buff; /**< Tx buffer */ |
marcozecchini | 0:9fca2b23d0ba | 50 | struct buffer_s rx_buff; /**< Rx buffer */ |
marcozecchini | 0:9fca2b23d0ba | 51 | } i2c_t; |
marcozecchini | 0:9fca2b23d0ba | 52 | |
marcozecchini | 0:9fca2b23d0ba | 53 | #else |
marcozecchini | 0:9fca2b23d0ba | 54 | /** Non-asynch I2C HAL structure |
marcozecchini | 0:9fca2b23d0ba | 55 | */ |
marcozecchini | 0:9fca2b23d0ba | 56 | typedef struct i2c_s i2c_t; |
marcozecchini | 0:9fca2b23d0ba | 57 | |
marcozecchini | 0:9fca2b23d0ba | 58 | #endif |
marcozecchini | 0:9fca2b23d0ba | 59 | |
marcozecchini | 0:9fca2b23d0ba | 60 | enum { |
marcozecchini | 0:9fca2b23d0ba | 61 | I2C_ERROR_NO_SLAVE = -1, |
marcozecchini | 0:9fca2b23d0ba | 62 | I2C_ERROR_BUS_BUSY = -2 |
marcozecchini | 0:9fca2b23d0ba | 63 | }; |
marcozecchini | 0:9fca2b23d0ba | 64 | |
marcozecchini | 0:9fca2b23d0ba | 65 | #ifdef __cplusplus |
marcozecchini | 0:9fca2b23d0ba | 66 | extern "C" { |
marcozecchini | 0:9fca2b23d0ba | 67 | #endif |
marcozecchini | 0:9fca2b23d0ba | 68 | |
marcozecchini | 0:9fca2b23d0ba | 69 | /** |
marcozecchini | 0:9fca2b23d0ba | 70 | * \defgroup hal_GeneralI2C I2C Configuration Functions |
marcozecchini | 0:9fca2b23d0ba | 71 | * @{ |
marcozecchini | 0:9fca2b23d0ba | 72 | */ |
marcozecchini | 0:9fca2b23d0ba | 73 | |
marcozecchini | 0:9fca2b23d0ba | 74 | /** Initialize the I2C peripheral. It sets the default parameters for I2C |
marcozecchini | 0:9fca2b23d0ba | 75 | * peripheral, and configures its specifieds pins. |
marcozecchini | 0:9fca2b23d0ba | 76 | * |
marcozecchini | 0:9fca2b23d0ba | 77 | * @param obj The I2C object |
marcozecchini | 0:9fca2b23d0ba | 78 | * @param sda The sda pin |
marcozecchini | 0:9fca2b23d0ba | 79 | * @param scl The scl pin |
marcozecchini | 0:9fca2b23d0ba | 80 | */ |
marcozecchini | 0:9fca2b23d0ba | 81 | void i2c_init(i2c_t *obj, PinName sda, PinName scl); |
marcozecchini | 0:9fca2b23d0ba | 82 | |
marcozecchini | 0:9fca2b23d0ba | 83 | /** Configure the I2C frequency |
marcozecchini | 0:9fca2b23d0ba | 84 | * |
marcozecchini | 0:9fca2b23d0ba | 85 | * @param obj The I2C object |
marcozecchini | 0:9fca2b23d0ba | 86 | * @param hz Frequency in Hz |
marcozecchini | 0:9fca2b23d0ba | 87 | */ |
marcozecchini | 0:9fca2b23d0ba | 88 | void i2c_frequency(i2c_t *obj, int hz); |
marcozecchini | 0:9fca2b23d0ba | 89 | |
marcozecchini | 0:9fca2b23d0ba | 90 | /** Send START command |
marcozecchini | 0:9fca2b23d0ba | 91 | * |
marcozecchini | 0:9fca2b23d0ba | 92 | * @param obj The I2C object |
marcozecchini | 0:9fca2b23d0ba | 93 | */ |
marcozecchini | 0:9fca2b23d0ba | 94 | int i2c_start(i2c_t *obj); |
marcozecchini | 0:9fca2b23d0ba | 95 | |
marcozecchini | 0:9fca2b23d0ba | 96 | /** Send STOP command |
marcozecchini | 0:9fca2b23d0ba | 97 | * |
marcozecchini | 0:9fca2b23d0ba | 98 | * @param obj The I2C object |
marcozecchini | 0:9fca2b23d0ba | 99 | */ |
marcozecchini | 0:9fca2b23d0ba | 100 | int i2c_stop(i2c_t *obj); |
marcozecchini | 0:9fca2b23d0ba | 101 | |
marcozecchini | 0:9fca2b23d0ba | 102 | /** Blocking reading data |
marcozecchini | 0:9fca2b23d0ba | 103 | * |
marcozecchini | 0:9fca2b23d0ba | 104 | * @param obj The I2C object |
marcozecchini | 0:9fca2b23d0ba | 105 | * @param address 7-bit address (last bit is 1) |
marcozecchini | 0:9fca2b23d0ba | 106 | * @param data The buffer for receiving |
marcozecchini | 0:9fca2b23d0ba | 107 | * @param length Number of bytes to read |
marcozecchini | 0:9fca2b23d0ba | 108 | * @param stop Stop to be generated after the transfer is done |
marcozecchini | 0:9fca2b23d0ba | 109 | * @return Number of read bytes |
marcozecchini | 0:9fca2b23d0ba | 110 | */ |
marcozecchini | 0:9fca2b23d0ba | 111 | int i2c_read(i2c_t *obj, int address, char *data, int length, int stop); |
marcozecchini | 0:9fca2b23d0ba | 112 | |
marcozecchini | 0:9fca2b23d0ba | 113 | /** Blocking sending data |
marcozecchini | 0:9fca2b23d0ba | 114 | * |
marcozecchini | 0:9fca2b23d0ba | 115 | * @param obj The I2C object |
marcozecchini | 0:9fca2b23d0ba | 116 | * @param address 7-bit address (last bit is 0) |
marcozecchini | 0:9fca2b23d0ba | 117 | * @param data The buffer for sending |
marcozecchini | 0:9fca2b23d0ba | 118 | * @param length Number of bytes to write |
marcozecchini | 0:9fca2b23d0ba | 119 | * @param stop Stop to be generated after the transfer is done |
marcozecchini | 0:9fca2b23d0ba | 120 | * @return |
marcozecchini | 0:9fca2b23d0ba | 121 | * zero or non-zero - Number of written bytes |
marcozecchini | 0:9fca2b23d0ba | 122 | * negative - I2C_ERROR_XXX status |
marcozecchini | 0:9fca2b23d0ba | 123 | */ |
marcozecchini | 0:9fca2b23d0ba | 124 | int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop); |
marcozecchini | 0:9fca2b23d0ba | 125 | |
marcozecchini | 0:9fca2b23d0ba | 126 | /** Reset I2C peripheral. TODO: The action here. Most of the implementation sends stop() |
marcozecchini | 0:9fca2b23d0ba | 127 | * |
marcozecchini | 0:9fca2b23d0ba | 128 | * @param obj The I2C object |
marcozecchini | 0:9fca2b23d0ba | 129 | */ |
marcozecchini | 0:9fca2b23d0ba | 130 | void i2c_reset(i2c_t *obj); |
marcozecchini | 0:9fca2b23d0ba | 131 | |
marcozecchini | 0:9fca2b23d0ba | 132 | /** Read one byte |
marcozecchini | 0:9fca2b23d0ba | 133 | * |
marcozecchini | 0:9fca2b23d0ba | 134 | * @param obj The I2C object |
marcozecchini | 0:9fca2b23d0ba | 135 | * @param last Acknoledge |
marcozecchini | 0:9fca2b23d0ba | 136 | * @return The read byte |
marcozecchini | 0:9fca2b23d0ba | 137 | */ |
marcozecchini | 0:9fca2b23d0ba | 138 | int i2c_byte_read(i2c_t *obj, int last); |
marcozecchini | 0:9fca2b23d0ba | 139 | |
marcozecchini | 0:9fca2b23d0ba | 140 | /** Write one byte |
marcozecchini | 0:9fca2b23d0ba | 141 | * |
marcozecchini | 0:9fca2b23d0ba | 142 | * @param obj The I2C object |
marcozecchini | 0:9fca2b23d0ba | 143 | * @param data Byte to be written |
marcozecchini | 0:9fca2b23d0ba | 144 | * @return 0 if NAK was received, 1 if ACK was received, 2 for timeout. |
marcozecchini | 0:9fca2b23d0ba | 145 | */ |
marcozecchini | 0:9fca2b23d0ba | 146 | int i2c_byte_write(i2c_t *obj, int data); |
marcozecchini | 0:9fca2b23d0ba | 147 | |
marcozecchini | 0:9fca2b23d0ba | 148 | /**@}*/ |
marcozecchini | 0:9fca2b23d0ba | 149 | |
marcozecchini | 0:9fca2b23d0ba | 150 | #if DEVICE_I2CSLAVE |
marcozecchini | 0:9fca2b23d0ba | 151 | |
marcozecchini | 0:9fca2b23d0ba | 152 | /** |
marcozecchini | 0:9fca2b23d0ba | 153 | * \defgroup SynchI2C Synchronous I2C Hardware Abstraction Layer for slave |
marcozecchini | 0:9fca2b23d0ba | 154 | * @{ |
marcozecchini | 0:9fca2b23d0ba | 155 | */ |
marcozecchini | 0:9fca2b23d0ba | 156 | |
marcozecchini | 0:9fca2b23d0ba | 157 | /** Configure I2C as slave or master. |
marcozecchini | 0:9fca2b23d0ba | 158 | * @param obj The I2C object |
marcozecchini | 0:9fca2b23d0ba | 159 | * @param enable_slave Enable i2c hardware so you can receive events with ::i2c_slave_receive |
marcozecchini | 0:9fca2b23d0ba | 160 | * @return non-zero if a value is available |
marcozecchini | 0:9fca2b23d0ba | 161 | */ |
marcozecchini | 0:9fca2b23d0ba | 162 | void i2c_slave_mode(i2c_t *obj, int enable_slave); |
marcozecchini | 0:9fca2b23d0ba | 163 | |
marcozecchini | 0:9fca2b23d0ba | 164 | /** Check to see if the I2C slave has been addressed. |
marcozecchini | 0:9fca2b23d0ba | 165 | * @param obj The I2C object |
marcozecchini | 0:9fca2b23d0ba | 166 | * @return The status - 1 - read addresses, 2 - write to all slaves, |
marcozecchini | 0:9fca2b23d0ba | 167 | * 3 write addressed, 0 - the slave has not been addressed |
marcozecchini | 0:9fca2b23d0ba | 168 | */ |
marcozecchini | 0:9fca2b23d0ba | 169 | int i2c_slave_receive(i2c_t *obj); |
marcozecchini | 0:9fca2b23d0ba | 170 | |
marcozecchini | 0:9fca2b23d0ba | 171 | /** Configure I2C as slave or master. |
marcozecchini | 0:9fca2b23d0ba | 172 | * @param obj The I2C object |
marcozecchini | 0:9fca2b23d0ba | 173 | * @param data The buffer for receiving |
marcozecchini | 0:9fca2b23d0ba | 174 | * @param length Number of bytes to read |
marcozecchini | 0:9fca2b23d0ba | 175 | * @return non-zero if a value is available |
marcozecchini | 0:9fca2b23d0ba | 176 | */ |
marcozecchini | 0:9fca2b23d0ba | 177 | int i2c_slave_read(i2c_t *obj, char *data, int length); |
marcozecchini | 0:9fca2b23d0ba | 178 | |
marcozecchini | 0:9fca2b23d0ba | 179 | /** Configure I2C as slave or master. |
marcozecchini | 0:9fca2b23d0ba | 180 | * @param obj The I2C object |
marcozecchini | 0:9fca2b23d0ba | 181 | * @param data The buffer for sending |
marcozecchini | 0:9fca2b23d0ba | 182 | * @param length Number of bytes to write |
marcozecchini | 0:9fca2b23d0ba | 183 | * @return non-zero if a value is available |
marcozecchini | 0:9fca2b23d0ba | 184 | */ |
marcozecchini | 0:9fca2b23d0ba | 185 | int i2c_slave_write(i2c_t *obj, const char *data, int length); |
marcozecchini | 0:9fca2b23d0ba | 186 | |
marcozecchini | 0:9fca2b23d0ba | 187 | /** Configure I2C address. |
marcozecchini | 0:9fca2b23d0ba | 188 | * @param obj The I2C object |
marcozecchini | 0:9fca2b23d0ba | 189 | * @param idx Currently not used |
marcozecchini | 0:9fca2b23d0ba | 190 | * @param address The address to be set |
marcozecchini | 0:9fca2b23d0ba | 191 | * @param mask Currently not used |
marcozecchini | 0:9fca2b23d0ba | 192 | */ |
marcozecchini | 0:9fca2b23d0ba | 193 | void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask); |
marcozecchini | 0:9fca2b23d0ba | 194 | |
marcozecchini | 0:9fca2b23d0ba | 195 | #endif |
marcozecchini | 0:9fca2b23d0ba | 196 | |
marcozecchini | 0:9fca2b23d0ba | 197 | /**@}*/ |
marcozecchini | 0:9fca2b23d0ba | 198 | |
marcozecchini | 0:9fca2b23d0ba | 199 | #if DEVICE_I2C_ASYNCH |
marcozecchini | 0:9fca2b23d0ba | 200 | |
marcozecchini | 0:9fca2b23d0ba | 201 | /** |
marcozecchini | 0:9fca2b23d0ba | 202 | * \defgroup hal_AsynchI2C Asynchronous I2C Hardware Abstraction Layer |
marcozecchini | 0:9fca2b23d0ba | 203 | * @{ |
marcozecchini | 0:9fca2b23d0ba | 204 | */ |
marcozecchini | 0:9fca2b23d0ba | 205 | |
marcozecchini | 0:9fca2b23d0ba | 206 | /** Start I2C asynchronous transfer |
marcozecchini | 0:9fca2b23d0ba | 207 | * |
marcozecchini | 0:9fca2b23d0ba | 208 | * @param obj The I2C object |
marcozecchini | 0:9fca2b23d0ba | 209 | * @param tx The transmit buffer |
marcozecchini | 0:9fca2b23d0ba | 210 | * @param tx_length The number of bytes to transmit |
marcozecchini | 0:9fca2b23d0ba | 211 | * @param rx The receive buffer |
marcozecchini | 0:9fca2b23d0ba | 212 | * @param rx_length The number of bytes to receive |
marcozecchini | 0:9fca2b23d0ba | 213 | * @param address The address to be set - 7bit or 9bit |
marcozecchini | 0:9fca2b23d0ba | 214 | * @param stop If true, stop will be generated after the transfer is done |
marcozecchini | 0:9fca2b23d0ba | 215 | * @param handler The I2C IRQ handler to be set |
marcozecchini | 0:9fca2b23d0ba | 216 | * @param event Event mask for the transfer. See \ref hal_I2CEvents |
marcozecchini | 0:9fca2b23d0ba | 217 | * @param hint DMA hint usage |
marcozecchini | 0:9fca2b23d0ba | 218 | */ |
marcozecchini | 0:9fca2b23d0ba | 219 | 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); |
marcozecchini | 0:9fca2b23d0ba | 220 | |
marcozecchini | 0:9fca2b23d0ba | 221 | /** The asynchronous IRQ handler |
marcozecchini | 0:9fca2b23d0ba | 222 | * |
marcozecchini | 0:9fca2b23d0ba | 223 | * @param obj The I2C object which holds the transfer information |
marcozecchini | 0:9fca2b23d0ba | 224 | * @return Event flags if a transfer termination condition was met, otherwise return 0. |
marcozecchini | 0:9fca2b23d0ba | 225 | */ |
marcozecchini | 0:9fca2b23d0ba | 226 | uint32_t i2c_irq_handler_asynch(i2c_t *obj); |
marcozecchini | 0:9fca2b23d0ba | 227 | |
marcozecchini | 0:9fca2b23d0ba | 228 | /** Attempts to determine if the I2C peripheral is already in use |
marcozecchini | 0:9fca2b23d0ba | 229 | * |
marcozecchini | 0:9fca2b23d0ba | 230 | * @param obj The I2C object |
marcozecchini | 0:9fca2b23d0ba | 231 | * @return Non-zero if the I2C module is active or zero if it is not |
marcozecchini | 0:9fca2b23d0ba | 232 | */ |
marcozecchini | 0:9fca2b23d0ba | 233 | uint8_t i2c_active(i2c_t *obj); |
marcozecchini | 0:9fca2b23d0ba | 234 | |
marcozecchini | 0:9fca2b23d0ba | 235 | /** Abort asynchronous transfer |
marcozecchini | 0:9fca2b23d0ba | 236 | * |
marcozecchini | 0:9fca2b23d0ba | 237 | * This function does not perform any check - that should happen in upper layers. |
marcozecchini | 0:9fca2b23d0ba | 238 | * @param obj The I2C object |
marcozecchini | 0:9fca2b23d0ba | 239 | */ |
marcozecchini | 0:9fca2b23d0ba | 240 | void i2c_abort_asynch(i2c_t *obj); |
marcozecchini | 0:9fca2b23d0ba | 241 | |
marcozecchini | 0:9fca2b23d0ba | 242 | #endif |
marcozecchini | 0:9fca2b23d0ba | 243 | |
marcozecchini | 0:9fca2b23d0ba | 244 | /**@}*/ |
marcozecchini | 0:9fca2b23d0ba | 245 | |
marcozecchini | 0:9fca2b23d0ba | 246 | #ifdef __cplusplus |
marcozecchini | 0:9fca2b23d0ba | 247 | } |
marcozecchini | 0:9fca2b23d0ba | 248 | #endif |
marcozecchini | 0:9fca2b23d0ba | 249 | |
marcozecchini | 0:9fca2b23d0ba | 250 | #endif |
marcozecchini | 0:9fca2b23d0ba | 251 | |
marcozecchini | 0:9fca2b23d0ba | 252 | #endif |
marcozecchini | 0:9fca2b23d0ba | 253 | |
marcozecchini | 0:9fca2b23d0ba | 254 | /** @}*/ |