001

Committer:
ganlikun
Date:
Sun Jun 12 14:02:44 2022 +0000
Revision:
0:13413ea9a877
00

Who changed what in which revision?

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