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