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