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