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.
Fork of mbed-dev by
i2c_api.h
00001 00002 /** \addtogroup hal */ 00003 /** @{*/ 00004 /* mbed Microcontroller Library 00005 * Copyright (c) 2006-2015 ARM Limited 00006 * 00007 * Licensed under the Apache License, Version 2.0 (the "License"); 00008 * you may not use this file except in compliance with the License. 00009 * You may obtain a copy of the License at 00010 * 00011 * http://www.apache.org/licenses/LICENSE-2.0 00012 * 00013 * Unless required by applicable law or agreed to in writing, software 00014 * distributed under the License is distributed on an "AS IS" BASIS, 00015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00016 * See the License for the specific language governing permissions and 00017 * limitations under the License. 00018 */ 00019 #ifndef MBED_I2C_API_H 00020 #define MBED_I2C_API_H 00021 00022 #include "device.h" 00023 #include "hal/buffer.h" 00024 00025 #if DEVICE_I2C_ASYNCH 00026 #include "hal/dma_api.h" 00027 #endif 00028 00029 #if DEVICE_I2C 00030 00031 /** 00032 * @defgroup hal_I2CEvents I2C Events Macros 00033 * 00034 * @{ 00035 */ 00036 #define I2C_EVENT_ERROR (1 << 1) 00037 #define I2C_EVENT_ERROR_NO_SLAVE (1 << 2) 00038 #define I2C_EVENT_TRANSFER_COMPLETE (1 << 3) 00039 #define I2C_EVENT_TRANSFER_EARLY_NACK (1 << 4) 00040 #define I2C_EVENT_ALL (I2C_EVENT_ERROR | I2C_EVENT_TRANSFER_COMPLETE | I2C_EVENT_ERROR_NO_SLAVE | I2C_EVENT_TRANSFER_EARLY_NACK) 00041 00042 /**@}*/ 00043 00044 #if DEVICE_I2C_ASYNCH 00045 /** Asynch I2C HAL structure 00046 */ 00047 typedef struct { 00048 struct i2c_s i2c; /**< Target specific I2C structure */ 00049 struct buffer_s tx_buff; /**< Tx buffer */ 00050 struct buffer_s rx_buff; /**< Rx buffer */ 00051 } i2c_t; 00052 00053 #else 00054 /** Non-asynch I2C HAL structure 00055 */ 00056 typedef struct i2c_s i2c_t; 00057 00058 #endif 00059 00060 enum { 00061 I2C_ERROR_NO_SLAVE = -1, 00062 I2C_ERROR_BUS_BUSY = -2 00063 }; 00064 00065 #ifdef __cplusplus 00066 extern "C" { 00067 #endif 00068 00069 /** 00070 * \defgroup hal_GeneralI2C I2C Configuration Functions 00071 * @{ 00072 */ 00073 00074 /** Initialize the I2C peripheral. It sets the default parameters for I2C 00075 * peripheral, and configures its specifieds pins. 00076 * 00077 * @param obj The I2C object 00078 * @param sda The sda pin 00079 * @param scl The scl pin 00080 */ 00081 void i2c_init(i2c_t *obj, PinName sda, PinName scl); 00082 00083 /** Configure the I2C frequency 00084 * 00085 * @param obj The I2C object 00086 * @param hz Frequency in Hz 00087 */ 00088 void i2c_frequency(i2c_t *obj, int hz); 00089 00090 /** Send START command 00091 * 00092 * @param obj The I2C object 00093 */ 00094 int i2c_start(i2c_t *obj); 00095 00096 /** Send STOP command 00097 * 00098 * @param obj The I2C object 00099 */ 00100 int i2c_stop(i2c_t *obj); 00101 00102 /** Blocking reading data 00103 * 00104 * @param obj The I2C object 00105 * @param address 7-bit address (last bit is 1) 00106 * @param data The buffer for receiving 00107 * @param length Number of bytes to read 00108 * @param stop Stop to be generated after the transfer is done 00109 * @return Number of read bytes 00110 */ 00111 int i2c_read(i2c_t *obj, int address, char *data, int length, int stop); 00112 00113 /** Blocking sending data 00114 * 00115 * @param obj The I2C object 00116 * @param address 7-bit address (last bit is 0) 00117 * @param data The buffer for sending 00118 * @param length Number of bytes to write 00119 * @param stop Stop to be generated after the transfer is done 00120 * @return 00121 * zero or non-zero - Number of written bytes 00122 * negative - I2C_ERROR_XXX status 00123 */ 00124 int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop); 00125 00126 /** Reset I2C peripheral. TODO: The action here. Most of the implementation sends stop() 00127 * 00128 * @param obj The I2C object 00129 */ 00130 void i2c_reset(i2c_t *obj); 00131 00132 /** Read one byte 00133 * 00134 * @param obj The I2C object 00135 * @param last Acknoledge 00136 * @return The read byte 00137 */ 00138 int i2c_byte_read(i2c_t *obj, int last); 00139 00140 /** Write one byte 00141 * 00142 * @param obj The I2C object 00143 * @param data Byte to be written 00144 * @return 0 if NAK was received, 1 if ACK was received, 2 for timeout. 00145 */ 00146 int i2c_byte_write(i2c_t *obj, int data); 00147 00148 /**@}*/ 00149 00150 #if DEVICE_I2CSLAVE 00151 00152 /** 00153 * \defgroup SynchI2C Synchronous I2C Hardware Abstraction Layer for slave 00154 * @{ 00155 */ 00156 00157 /** Configure I2C as slave or master. 00158 * @param obj The I2C object 00159 * @param enable_slave Enable i2c hardware so you can receive events with ::i2c_slave_receive 00160 * @return non-zero if a value is available 00161 */ 00162 void i2c_slave_mode(i2c_t *obj, int enable_slave); 00163 00164 /** Check to see if the I2C slave has been addressed. 00165 * @param obj The I2C object 00166 * @return The status - 1 - read addresses, 2 - write to all slaves, 00167 * 3 write addressed, 0 - the slave has not been addressed 00168 */ 00169 int i2c_slave_receive(i2c_t *obj); 00170 00171 /** Configure I2C as slave or master. 00172 * @param obj The I2C object 00173 * @param data The buffer for receiving 00174 * @param length Number of bytes to read 00175 * @return non-zero if a value is available 00176 */ 00177 int i2c_slave_read(i2c_t *obj, char *data, int length); 00178 00179 /** Configure I2C as slave or master. 00180 * @param obj The I2C object 00181 * @param data The buffer for sending 00182 * @param length Number of bytes to write 00183 * @return non-zero if a value is available 00184 */ 00185 int i2c_slave_write(i2c_t *obj, const char *data, int length); 00186 00187 /** Configure I2C address. 00188 * @param obj The I2C object 00189 * @param idx Currently not used 00190 * @param address The address to be set 00191 * @param mask Currently not used 00192 */ 00193 void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask); 00194 00195 #endif 00196 00197 /**@}*/ 00198 00199 #if DEVICE_I2C_ASYNCH 00200 00201 /** 00202 * \defgroup hal_AsynchI2C Asynchronous I2C Hardware Abstraction Layer 00203 * @{ 00204 */ 00205 00206 /** Start I2C asynchronous transfer 00207 * 00208 * @param obj The I2C object 00209 * @param tx The transmit buffer 00210 * @param tx_length The number of bytes to transmit 00211 * @param rx The receive buffer 00212 * @param rx_length The number of bytes to receive 00213 * @param address The address to be set - 7bit or 9bit 00214 * @param stop If true, stop will be generated after the transfer is done 00215 * @param handler The I2C IRQ handler to be set 00216 * @param event Event mask for the transfer. See \ref hal_I2CEvents 00217 * @param hint DMA hint usage 00218 */ 00219 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); 00220 00221 /** The asynchronous IRQ handler 00222 * 00223 * @param obj The I2C object which holds the transfer information 00224 * @return Event flags if a transfer termination condition was met, otherwise return 0. 00225 */ 00226 uint32_t i2c_irq_handler_asynch(i2c_t *obj); 00227 00228 /** Attempts to determine if the I2C peripheral is already in use 00229 * 00230 * @param obj The I2C object 00231 * @return Non-zero if the I2C module is active or zero if it is not 00232 */ 00233 uint8_t i2c_active(i2c_t *obj); 00234 00235 /** Abort asynchronous transfer 00236 * 00237 * This function does not perform any check - that should happen in upper layers. 00238 * @param obj The I2C object 00239 */ 00240 void i2c_abort_asynch(i2c_t *obj); 00241 00242 #endif 00243 00244 /**@}*/ 00245 00246 #ifdef __cplusplus 00247 } 00248 #endif 00249 00250 #endif 00251 00252 #endif 00253 00254 /** @}*/
Generated on Wed Jul 27 2022 09:32:04 by
