Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: TYBLE16_simple_data_logger TYBLE16_MP3_Air
i2c_api.h
00001 00002 /** \addtogroup hal */ 00003 /** @{*/ 00004 /* mbed Microcontroller Library 00005 * Copyright (c) 2006-2015 ARM Limited 00006 * SPDX-License-Identifier: Apache-2.0 00007 * 00008 * Licensed under the Apache License, Version 2.0 (the "License"); 00009 * you may not use this file except in compliance with the License. 00010 * You may obtain a copy of the License at 00011 * 00012 * http://www.apache.org/licenses/LICENSE-2.0 00013 * 00014 * Unless required by applicable law or agreed to in writing, software 00015 * distributed under the License is distributed on an "AS IS" BASIS, 00016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00017 * See the License for the specific language governing permissions and 00018 * limitations under the License. 00019 */ 00020 #ifndef MBED_I2C_API_H 00021 #define MBED_I2C_API_H 00022 00023 #include "device.h" 00024 #include "pinmap.h" 00025 #include "hal/buffer.h" 00026 00027 #if DEVICE_I2C_ASYNCH 00028 #include "hal/dma_api.h" 00029 #endif 00030 00031 #if DEVICE_I2C 00032 00033 /** 00034 * @defgroup hal_I2CEvents I2C Events Macros 00035 * 00036 * @{ 00037 */ 00038 #define I2C_EVENT_ERROR (1 << 1) 00039 #define I2C_EVENT_ERROR_NO_SLAVE (1 << 2) 00040 #define I2C_EVENT_TRANSFER_COMPLETE (1 << 3) 00041 #define I2C_EVENT_TRANSFER_EARLY_NACK (1 << 4) 00042 #define I2C_EVENT_ALL (I2C_EVENT_ERROR | I2C_EVENT_TRANSFER_COMPLETE | I2C_EVENT_ERROR_NO_SLAVE | I2C_EVENT_TRANSFER_EARLY_NACK) 00043 00044 /**@}*/ 00045 00046 #if DEVICE_I2C_ASYNCH 00047 /** Asynch I2C HAL structure 00048 */ 00049 typedef struct { 00050 struct i2c_s i2c; /**< Target specific I2C structure */ 00051 struct buffer_s tx_buff; /**< Tx buffer */ 00052 struct buffer_s rx_buff; /**< Rx buffer */ 00053 } i2c_t; 00054 00055 #else 00056 /** Non-asynch I2C HAL structure 00057 */ 00058 typedef struct i2c_s i2c_t; 00059 00060 #endif 00061 00062 enum { 00063 I2C_ERROR_NO_SLAVE = -1, 00064 I2C_ERROR_BUS_BUSY = -2 00065 }; 00066 00067 typedef struct { 00068 int peripheral; 00069 PinName sda_pin; 00070 int sda_function; 00071 PinName scl_pin; 00072 int scl_function; 00073 } i2c_pinmap_t; 00074 00075 #ifdef __cplusplus 00076 extern "C" { 00077 #endif 00078 00079 /** 00080 * \defgroup hal_GeneralI2C I2C Configuration Functions 00081 * 00082 * # Defined behavior 00083 * * ::i2c_init initializes i2c_t control structure 00084 * * ::i2c_init configures the pins used by I2C 00085 * * ::i2c_free returns the pins owned by the I2C object to their reset state 00086 * * ::i2c_frequency configure the I2C frequency 00087 * * ::i2c_start sends START command 00088 * * ::i2c_read reads `length` bytes from the I2C slave specified by `address` to the `data` buffer 00089 * * ::i2c_read reads generates a stop condition on the bus at the end of the transfer if `stop` parameter is non-zero 00090 * * ::i2c_read reads returns the number of symbols received from the bus 00091 * * ::i2c_write writes `length` bytes to the I2C slave specified by `address` from the `data` buffer 00092 * * ::i2c_write generates a stop condition on the bus at the end of the transfer if `stop` parameter is non-zero 00093 * * ::i2c_write returns zero on success, error code otherwise 00094 * * ::i2c_reset resets the I2C peripheral 00095 * * ::i2c_byte_read reads and return one byte from the specfied I2C slave 00096 * * ::i2c_byte_read uses `last` parameter to inform the slave that all bytes have been read 00097 * * ::i2c_byte_write writes one byte to the specified I2C slave 00098 * * ::i2c_byte_write returns 0 if NAK was received, 1 if ACK was received, 2 for timeout 00099 * * ::i2c_slave_mode enables/disables I2S slave mode 00100 * * ::i2c_slave_receive returns: 1 - read addresses, 2 - write to all slaves, 3 write addressed, 0 - the slave has not been addressed 00101 * * ::i2c_slave_read reads `length` bytes from the I2C master to the `data` buffer 00102 * * ::i2c_slave_read returns non-zero if a value is available, 0 otherwise 00103 * * ::i2c_slave_write writes `length` bytes to the I2C master from the `data` buffer 00104 * * ::i2c_slave_write returns non-zero if a value is available, 0 otherwise 00105 * * ::i2c_slave_address configures I2C slave address 00106 * * ::i2c_transfer_asynch starts I2C asynchronous transfer 00107 * * ::i2c_transfer_asynch writes `tx_length` bytes to the I2C slave specified by `address` from the `tx` buffer 00108 * * ::i2c_transfer_asynch reads `rx_length` bytes from the I2C slave specified by `address` to the `rx` buffer 00109 * * ::i2c_transfer_asynch generates a stop condition on the bus at the end of the transfer if `stop` parameter is non-zero 00110 * * The callback given to ::i2c_transfer_asynch is invoked when the transfer completes 00111 * * ::i2c_transfer_asynch specifies the logical OR of events to be registered 00112 * * The ::i2c_transfer_asynch function may use the `DMAUsage` hint to select the appropriate async algorithm 00113 * * ::i2c_irq_handler_asynch returns event flags if a transfer termination condition was met, otherwise returns 0. 00114 * * ::i2c_active returns non-zero if the I2C module is active or 0 if it is not 00115 * * ::i2c_abort_asynch aborts an on-going async transfer 00116 * 00117 * # Undefined behavior 00118 * * Calling ::i2c_init multiple times on the same `i2c_t` 00119 * * Calling any function other than ::i2c_init on a non-initialized `i2c_t` 00120 * * Initialising the `I2C` peripheral with invalid `SDA` and `SCL` pins. 00121 * * Passing pins that cannot be on the same peripheral 00122 * * Passing an invalid pointer as `obj` to any function 00123 * * Use of a `null` pointer as an argument to any function. 00124 * * Initialising the peripheral in slave mode if slave mode is not supported 00125 * * Operating the peripheral in slave mode without first specifying and address using ::i2c_slave_address 00126 * * Setting an address using i2c_slave_address after initialising the peripheral in master mode 00127 * * Setting an address to an `I2C` reserved value 00128 * * Specifying an invalid address when calling any `read` or `write` functions 00129 * * Setting the length of the transfer or receive buffers to larger than the buffers are 00130 * * Passing an invalid pointer as `handler` 00131 * * Calling ::i2c_abort_async when no transfer is currently in progress 00132 * 00133 * 00134 * @{ 00135 */ 00136 00137 /** 00138 * \defgroup hal_GeneralI2C_tests I2C hal tests 00139 * The I2C HAL tests ensure driver conformance to defined behaviour. 00140 * 00141 * To run the I2C hal tests use the command: 00142 * 00143 * mbed test -t <toolchain> -m <target> -n tests-mbed_hal_fpga_ci_test_shield-i2c 00144 * 00145 */ 00146 00147 /** Initialize the I2C peripheral. It sets the default parameters for I2C 00148 * peripheral, and configures its specifieds pins. 00149 * 00150 * @param obj The I2C object 00151 * @param pinmap Pinmap pointer to structure which holds static pinmap 00152 */ 00153 void i2c_init_direct(i2c_t *obj, const i2c_pinmap_t *pinmap); 00154 00155 /** Initialize the I2C peripheral. It sets the default parameters for I2C 00156 * peripheral, and configures its specifieds pins. 00157 * 00158 * @param obj The I2C object 00159 * @param sda The sda pin 00160 * @param scl The scl pin 00161 */ 00162 void i2c_init(i2c_t *obj, PinName sda, PinName scl); 00163 00164 /** Release a I2C object 00165 * 00166 * Return the pins owned by the I2C object to their reset state 00167 * @param obj The I2C object to deinitialize 00168 */ 00169 void i2c_free(i2c_t *obj); 00170 00171 /** Configure the I2C frequency 00172 * 00173 * @param obj The I2C object 00174 * @param hz Frequency in Hz 00175 */ 00176 void i2c_frequency(i2c_t *obj, int hz); 00177 00178 /** Send START command 00179 * 00180 * @param obj The I2C object 00181 */ 00182 int i2c_start(i2c_t *obj); 00183 00184 /** Send STOP command 00185 * 00186 * @param obj The I2C object 00187 */ 00188 int i2c_stop(i2c_t *obj); 00189 00190 /** Blocking reading data 00191 * 00192 * @param obj The I2C object 00193 * @param address 7-bit address (last bit is 1) 00194 * @param data The buffer for receiving 00195 * @param length Number of bytes to read 00196 * @param stop Stop to be generated after the transfer is done 00197 * @return Number of read bytes 00198 */ 00199 int i2c_read(i2c_t *obj, int address, char *data, int length, int stop); 00200 00201 /** Blocking sending data 00202 * 00203 * @param obj The I2C object 00204 * @param address 7-bit address (last bit is 0) 00205 * @param data The buffer for sending 00206 * @param length Number of bytes to write 00207 * @param stop Stop to be generated after the transfer is done 00208 * @return 00209 * zero or non-zero - Number of written bytes 00210 * negative - I2C_ERROR_XXX status 00211 */ 00212 int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop); 00213 00214 /** Reset I2C peripheral. TODO: The action here. Most of the implementation sends stop() 00215 * 00216 * @param obj The I2C object 00217 */ 00218 void i2c_reset(i2c_t *obj); 00219 00220 /** Read one byte 00221 * 00222 * @param obj The I2C object 00223 * @param last Acknoledge 00224 * @return The read byte 00225 */ 00226 int i2c_byte_read(i2c_t *obj, int last); 00227 00228 /** Write one byte 00229 * 00230 * @param obj The I2C object 00231 * @param data Byte to be written 00232 * @return 0 if NAK was received, 1 if ACK was received, 2 for timeout. 00233 */ 00234 int i2c_byte_write(i2c_t *obj, int data); 00235 00236 /** Get the pins that support I2C SDA 00237 * 00238 * Return a PinMap array of pins that support I2C SDA in 00239 * master mode. The array is terminated with {NC, NC, 0}. 00240 * 00241 * @return PinMap array 00242 */ 00243 const PinMap *i2c_master_sda_pinmap(void); 00244 00245 /** Get the pins that support I2C SCL 00246 * 00247 * Return a PinMap array of pins that support I2C SCL in 00248 * master mode. The array is terminated with {NC, NC, 0}. 00249 * 00250 * @return PinMap array 00251 */ 00252 const PinMap *i2c_master_scl_pinmap(void); 00253 00254 /** Get the pins that support I2C SDA 00255 * 00256 * Return a PinMap array of pins that support I2C SDA in 00257 * slave mode. The array is terminated with {NC, NC, 0}. 00258 * 00259 * @return PinMap array 00260 */ 00261 const PinMap *i2c_slave_sda_pinmap(void); 00262 00263 /** Get the pins that support I2C SCL 00264 * 00265 * Return a PinMap array of pins that support I2C SCL in 00266 * slave mode. The array is terminated with {NC, NC, 0}. 00267 * 00268 * @return PinMap array 00269 */ 00270 const PinMap *i2c_slave_scl_pinmap(void); 00271 00272 /**@}*/ 00273 00274 #if DEVICE_I2CSLAVE 00275 00276 /** 00277 * \defgroup SynchI2C Synchronous I2C Hardware Abstraction Layer for slave 00278 * @{ 00279 */ 00280 00281 /** Configure I2C as slave or master. 00282 * @param obj The I2C object 00283 * @param enable_slave Enable i2c hardware so you can receive events with ::i2c_slave_receive 00284 * @return non-zero if a value is available 00285 */ 00286 void i2c_slave_mode(i2c_t *obj, int enable_slave); 00287 00288 /** Check to see if the I2C slave has been addressed. 00289 * @param obj The I2C object 00290 * @return The status - 1 - read addresses, 2 - write to all slaves, 00291 * 3 write addressed, 0 - the slave has not been addressed 00292 */ 00293 int i2c_slave_receive(i2c_t *obj); 00294 00295 /** Configure I2C as slave or master. 00296 * @param obj The I2C object 00297 * @param data The buffer for receiving 00298 * @param length Number of bytes to read 00299 * @return non-zero if a value is available 00300 */ 00301 int i2c_slave_read(i2c_t *obj, char *data, int length); 00302 00303 /** Configure I2C as slave or master. 00304 * @param obj The I2C object 00305 * @param data The buffer for sending 00306 * @param length Number of bytes to write 00307 * @return non-zero if a value is available 00308 */ 00309 int i2c_slave_write(i2c_t *obj, const char *data, int length); 00310 00311 /** Configure I2C address. 00312 * @param obj The I2C object 00313 * @param idx Currently not used 00314 * @param address The address to be set 00315 * @param mask Currently not used 00316 */ 00317 void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask); 00318 00319 #endif 00320 00321 /**@}*/ 00322 00323 #if DEVICE_I2C_ASYNCH 00324 00325 /** 00326 * \defgroup hal_AsynchI2C Asynchronous I2C Hardware Abstraction Layer 00327 * @{ 00328 */ 00329 00330 /** Start I2C asynchronous transfer 00331 * 00332 * @param obj The I2C object 00333 * @param tx The transmit buffer 00334 * @param tx_length The number of bytes to transmit 00335 * @param rx The receive buffer 00336 * @param rx_length The number of bytes to receive 00337 * @param address The address to be set - 7bit or 9bit 00338 * @param stop If true, stop will be generated after the transfer is done 00339 * @param handler The I2C IRQ handler to be set 00340 * @param event Event mask for the transfer. See \ref hal_I2CEvents 00341 * @param hint DMA hint usage 00342 */ 00343 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); 00344 00345 /** The asynchronous IRQ handler 00346 * 00347 * @param obj The I2C object which holds the transfer information 00348 * @return Event flags if a transfer termination condition was met, otherwise return 0. 00349 */ 00350 uint32_t i2c_irq_handler_asynch(i2c_t *obj); 00351 00352 /** Attempts to determine if the I2C peripheral is already in use 00353 * 00354 * @param obj The I2C object 00355 * @return Non-zero if the I2C module is active or zero if it is not 00356 */ 00357 uint8_t i2c_active(i2c_t *obj); 00358 00359 /** Abort asynchronous transfer 00360 * 00361 * This function does not perform any check - that should happen in upper layers. 00362 * @param obj The I2C object 00363 */ 00364 void i2c_abort_asynch(i2c_t *obj); 00365 00366 #endif 00367 00368 /**@}*/ 00369 00370 #ifdef __cplusplus 00371 } 00372 #endif 00373 00374 #endif 00375 00376 #endif 00377 00378 /** @}*/
Generated on Tue Jul 12 2022 13:54:24 by
1.7.2