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