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