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