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