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