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