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