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