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