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