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