STM32H735xx HAL User Manual
|
00001 /** 00002 ****************************************************************************** 00003 * @file stm32h7xx_hal_i2c.c 00004 * @author MCD Application Team 00005 * @brief I2C HAL module driver. 00006 * This file provides firmware functions to manage the following 00007 * functionalities of the Inter Integrated Circuit (I2C) peripheral: 00008 * + Initialization and de-initialization functions 00009 * + IO operation functions 00010 * + Peripheral State and Errors functions 00011 * 00012 ****************************************************************************** 00013 * @attention 00014 * 00015 * Copyright (c) 2017 STMicroelectronics. 00016 * All rights reserved. 00017 * 00018 * This software is licensed under terms that can be found in the LICENSE file 00019 * in the root directory of this software component. 00020 * If no LICENSE file comes with this software, it is provided AS-IS. 00021 * 00022 ****************************************************************************** 00023 @verbatim 00024 ============================================================================== 00025 ##### How to use this driver ##### 00026 ============================================================================== 00027 [..] 00028 The I2C HAL driver can be used as follows: 00029 00030 (#) Declare a I2C_HandleTypeDef handle structure, for example: 00031 I2C_HandleTypeDef hi2c; 00032 00033 (#)Initialize the I2C low level resources by implementing the HAL_I2C_MspInit() API: 00034 (##) Enable the I2Cx interface clock 00035 (##) I2C pins configuration 00036 (+++) Enable the clock for the I2C GPIOs 00037 (+++) Configure I2C pins as alternate function open-drain 00038 (##) NVIC configuration if you need to use interrupt process 00039 (+++) Configure the I2Cx interrupt priority 00040 (+++) Enable the NVIC I2C IRQ Channel 00041 (##) DMA Configuration if you need to use DMA process 00042 (+++) Declare a DMA_HandleTypeDef handle structure for 00043 the transmit or receive stream or channel depends on Instance 00044 (+++) Enable the DMAx interface clock using 00045 (+++) Configure the DMA handle parameters 00046 (+++) Configure the DMA Tx or Rx stream or channel depends on Instance 00047 (+++) Associate the initialized DMA handle to the hi2c DMA Tx or Rx handle 00048 (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on 00049 the DMA Tx or Rx stream or channel depends on Instance 00050 00051 (#) Configure the Communication Clock Timing, Own Address1, Master Addressing mode, Dual Addressing mode, 00052 Own Address2, Own Address2 Mask, General call and Nostretch mode in the hi2c Init structure. 00053 00054 (#) Initialize the I2C registers by calling the HAL_I2C_Init(), configures also the low level Hardware 00055 (GPIO, CLOCK, NVIC...etc) by calling the customized HAL_I2C_MspInit(&hi2c) API. 00056 00057 (#) To check if target device is ready for communication, use the function HAL_I2C_IsDeviceReady() 00058 00059 (#) For I2C IO and IO MEM operations, three operation modes are available within this driver : 00060 00061 *** Polling mode IO operation *** 00062 ================================= 00063 [..] 00064 (+) Transmit in master mode an amount of data in blocking mode using HAL_I2C_Master_Transmit() 00065 (+) Receive in master mode an amount of data in blocking mode using HAL_I2C_Master_Receive() 00066 (+) Transmit in slave mode an amount of data in blocking mode using HAL_I2C_Slave_Transmit() 00067 (+) Receive in slave mode an amount of data in blocking mode using HAL_I2C_Slave_Receive() 00068 00069 *** Polling mode IO MEM operation *** 00070 ===================================== 00071 [..] 00072 (+) Write an amount of data in blocking mode to a specific memory address using HAL_I2C_Mem_Write() 00073 (+) Read an amount of data in blocking mode from a specific memory address using HAL_I2C_Mem_Read() 00074 00075 00076 *** Interrupt mode IO operation *** 00077 =================================== 00078 [..] 00079 (+) Transmit in master mode an amount of data in non-blocking mode using HAL_I2C_Master_Transmit_IT() 00080 (+) At transmission end of transfer, HAL_I2C_MasterTxCpltCallback() is executed and users can 00081 add their own code by customization of function pointer HAL_I2C_MasterTxCpltCallback() 00082 (+) Receive in master mode an amount of data in non-blocking mode using HAL_I2C_Master_Receive_IT() 00083 (+) At reception end of transfer, HAL_I2C_MasterRxCpltCallback() is executed and users can 00084 add their own code by customization of function pointer HAL_I2C_MasterRxCpltCallback() 00085 (+) Transmit in slave mode an amount of data in non-blocking mode using HAL_I2C_Slave_Transmit_IT() 00086 (+) At transmission end of transfer, HAL_I2C_SlaveTxCpltCallback() is executed and users can 00087 add their own code by customization of function pointer HAL_I2C_SlaveTxCpltCallback() 00088 (+) Receive in slave mode an amount of data in non-blocking mode using HAL_I2C_Slave_Receive_IT() 00089 (+) At reception end of transfer, HAL_I2C_SlaveRxCpltCallback() is executed and users can 00090 add their own code by customization of function pointer HAL_I2C_SlaveRxCpltCallback() 00091 (+) In case of transfer Error, HAL_I2C_ErrorCallback() function is executed and users can 00092 add their own code by customization of function pointer HAL_I2C_ErrorCallback() 00093 (+) Abort a master I2C process communication with Interrupt using HAL_I2C_Master_Abort_IT() 00094 (+) End of abort process, HAL_I2C_AbortCpltCallback() is executed and users can 00095 add their own code by customization of function pointer HAL_I2C_AbortCpltCallback() 00096 (+) Discard a slave I2C process communication using __HAL_I2C_GENERATE_NACK() macro. 00097 This action will inform Master to generate a Stop condition to discard the communication. 00098 00099 00100 *** Interrupt mode or DMA mode IO sequential operation *** 00101 ========================================================== 00102 [..] 00103 (@) These interfaces allow to manage a sequential transfer with a repeated start condition 00104 when a direction change during transfer 00105 [..] 00106 (+) A specific option field manage the different steps of a sequential transfer 00107 (+) Option field values are defined through I2C_XFEROPTIONS and are listed below: 00108 (++) I2C_FIRST_AND_LAST_FRAME: No sequential usage, functional is same as associated interfaces in 00109 no sequential mode 00110 (++) I2C_FIRST_FRAME: Sequential usage, this option allow to manage a sequence with start condition, address 00111 and data to transfer without a final stop condition 00112 (++) I2C_FIRST_AND_NEXT_FRAME: Sequential usage (Master only), this option allow to manage a sequence with 00113 start condition, address and data to transfer without a final stop condition, 00114 an then permit a call the same master sequential interface several times 00115 (like HAL_I2C_Master_Seq_Transmit_IT() then HAL_I2C_Master_Seq_Transmit_IT() 00116 or HAL_I2C_Master_Seq_Transmit_DMA() then HAL_I2C_Master_Seq_Transmit_DMA()) 00117 (++) I2C_NEXT_FRAME: Sequential usage, this option allow to manage a sequence with a restart condition, address 00118 and with new data to transfer if the direction change or manage only the new data to 00119 transfer 00120 if no direction change and without a final stop condition in both cases 00121 (++) I2C_LAST_FRAME: Sequential usage, this option allow to manage a sequance with a restart condition, address 00122 and with new data to transfer if the direction change or manage only the new data to 00123 transfer 00124 if no direction change and with a final stop condition in both cases 00125 (++) I2C_LAST_FRAME_NO_STOP: Sequential usage (Master only), this option allow to manage a restart condition 00126 after several call of the same master sequential interface several times 00127 (link with option I2C_FIRST_AND_NEXT_FRAME). 00128 Usage can, transfer several bytes one by one using 00129 HAL_I2C_Master_Seq_Transmit_IT 00130 or HAL_I2C_Master_Seq_Receive_IT 00131 or HAL_I2C_Master_Seq_Transmit_DMA 00132 or HAL_I2C_Master_Seq_Receive_DMA 00133 with option I2C_FIRST_AND_NEXT_FRAME then I2C_NEXT_FRAME. 00134 Then usage of this option I2C_LAST_FRAME_NO_STOP at the last Transmit or 00135 Receive sequence permit to call the opposite interface Receive or Transmit 00136 without stopping the communication and so generate a restart condition. 00137 (++) I2C_OTHER_FRAME: Sequential usage (Master only), this option allow to manage a restart condition after 00138 each call of the same master sequential 00139 interface. 00140 Usage can, transfer several bytes one by one with a restart with slave address between 00141 each bytes using 00142 HAL_I2C_Master_Seq_Transmit_IT 00143 or HAL_I2C_Master_Seq_Receive_IT 00144 or HAL_I2C_Master_Seq_Transmit_DMA 00145 or HAL_I2C_Master_Seq_Receive_DMA 00146 with option I2C_FIRST_FRAME then I2C_OTHER_FRAME. 00147 Then usage of this option I2C_OTHER_AND_LAST_FRAME at the last frame to help automatic 00148 generation of STOP condition. 00149 00150 (+) Different sequential I2C interfaces are listed below: 00151 (++) Sequential transmit in master I2C mode an amount of data in non-blocking mode using 00152 HAL_I2C_Master_Seq_Transmit_IT() or using HAL_I2C_Master_Seq_Transmit_DMA() 00153 (+++) At transmission end of current frame transfer, HAL_I2C_MasterTxCpltCallback() is executed and 00154 users can add their own code by customization of function pointer HAL_I2C_MasterTxCpltCallback() 00155 (++) Sequential receive in master I2C mode an amount of data in non-blocking mode using 00156 HAL_I2C_Master_Seq_Receive_IT() or using HAL_I2C_Master_Seq_Receive_DMA() 00157 (+++) At reception end of current frame transfer, HAL_I2C_MasterRxCpltCallback() is executed and users can 00158 add their own code by customization of function pointer HAL_I2C_MasterRxCpltCallback() 00159 (++) Abort a master IT or DMA I2C process communication with Interrupt using HAL_I2C_Master_Abort_IT() 00160 (+++) End of abort process, HAL_I2C_AbortCpltCallback() is executed and users can 00161 add their own code by customization of function pointer HAL_I2C_AbortCpltCallback() 00162 (++) Enable/disable the Address listen mode in slave I2C mode using HAL_I2C_EnableListen_IT() 00163 HAL_I2C_DisableListen_IT() 00164 (+++) When address slave I2C match, HAL_I2C_AddrCallback() is executed and users can 00165 add their own code to check the Address Match Code and the transmission direction request by master 00166 (Write/Read). 00167 (+++) At Listen mode end HAL_I2C_ListenCpltCallback() is executed and users can 00168 add their own code by customization of function pointer HAL_I2C_ListenCpltCallback() 00169 (++) Sequential transmit in slave I2C mode an amount of data in non-blocking mode using 00170 HAL_I2C_Slave_Seq_Transmit_IT() or using HAL_I2C_Slave_Seq_Transmit_DMA() 00171 (+++) At transmission end of current frame transfer, HAL_I2C_SlaveTxCpltCallback() is executed and 00172 users can add their own code by customization of function pointer HAL_I2C_SlaveTxCpltCallback() 00173 (++) Sequential receive in slave I2C mode an amount of data in non-blocking mode using 00174 HAL_I2C_Slave_Seq_Receive_IT() or using HAL_I2C_Slave_Seq_Receive_DMA() 00175 (+++) At reception end of current frame transfer, HAL_I2C_SlaveRxCpltCallback() is executed and users can 00176 add their own code by customization of function pointer HAL_I2C_SlaveRxCpltCallback() 00177 (++) In case of transfer Error, HAL_I2C_ErrorCallback() function is executed and users can 00178 add their own code by customization of function pointer HAL_I2C_ErrorCallback() 00179 (++) Discard a slave I2C process communication using __HAL_I2C_GENERATE_NACK() macro. 00180 This action will inform Master to generate a Stop condition to discard the communication. 00181 00182 *** Interrupt mode IO MEM operation *** 00183 ======================================= 00184 [..] 00185 (+) Write an amount of data in non-blocking mode with Interrupt to a specific memory address using 00186 HAL_I2C_Mem_Write_IT() 00187 (+) At Memory end of write transfer, HAL_I2C_MemTxCpltCallback() is executed and users can 00188 add their own code by customization of function pointer HAL_I2C_MemTxCpltCallback() 00189 (+) Read an amount of data in non-blocking mode with Interrupt from a specific memory address using 00190 HAL_I2C_Mem_Read_IT() 00191 (+) At Memory end of read transfer, HAL_I2C_MemRxCpltCallback() is executed and users can 00192 add their own code by customization of function pointer HAL_I2C_MemRxCpltCallback() 00193 (+) In case of transfer Error, HAL_I2C_ErrorCallback() function is executed and users can 00194 add their own code by customization of function pointer HAL_I2C_ErrorCallback() 00195 00196 *** DMA mode IO operation *** 00197 ============================== 00198 [..] 00199 (+) Transmit in master mode an amount of data in non-blocking mode (DMA) using 00200 HAL_I2C_Master_Transmit_DMA() 00201 (+) At transmission end of transfer, HAL_I2C_MasterTxCpltCallback() is executed and users can 00202 add their own code by customization of function pointer HAL_I2C_MasterTxCpltCallback() 00203 (+) Receive in master mode an amount of data in non-blocking mode (DMA) using 00204 HAL_I2C_Master_Receive_DMA() 00205 (+) At reception end of transfer, HAL_I2C_MasterRxCpltCallback() is executed and users can 00206 add their own code by customization of function pointer HAL_I2C_MasterRxCpltCallback() 00207 (+) Transmit in slave mode an amount of data in non-blocking mode (DMA) using 00208 HAL_I2C_Slave_Transmit_DMA() 00209 (+) At transmission end of transfer, HAL_I2C_SlaveTxCpltCallback() is executed and users can 00210 add their own code by customization of function pointer HAL_I2C_SlaveTxCpltCallback() 00211 (+) Receive in slave mode an amount of data in non-blocking mode (DMA) using 00212 HAL_I2C_Slave_Receive_DMA() 00213 (+) At reception end of transfer, HAL_I2C_SlaveRxCpltCallback() is executed and users can 00214 add their own code by customization of function pointer HAL_I2C_SlaveRxCpltCallback() 00215 (+) In case of transfer Error, HAL_I2C_ErrorCallback() function is executed and users can 00216 add their own code by customization of function pointer HAL_I2C_ErrorCallback() 00217 (+) Abort a master I2C process communication with Interrupt using HAL_I2C_Master_Abort_IT() 00218 (+) End of abort process, HAL_I2C_AbortCpltCallback() is executed and users can 00219 add their own code by customization of function pointer HAL_I2C_AbortCpltCallback() 00220 (+) Discard a slave I2C process communication using __HAL_I2C_GENERATE_NACK() macro. 00221 This action will inform Master to generate a Stop condition to discard the communication. 00222 00223 *** DMA mode IO MEM operation *** 00224 ================================= 00225 [..] 00226 (+) Write an amount of data in non-blocking mode with DMA to a specific memory address using 00227 HAL_I2C_Mem_Write_DMA() 00228 (+) At Memory end of write transfer, HAL_I2C_MemTxCpltCallback() is executed and users can 00229 add their own code by customization of function pointer HAL_I2C_MemTxCpltCallback() 00230 (+) Read an amount of data in non-blocking mode with DMA from a specific memory address using 00231 HAL_I2C_Mem_Read_DMA() 00232 (+) At Memory end of read transfer, HAL_I2C_MemRxCpltCallback() is executed and users can 00233 add their own code by customization of function pointer HAL_I2C_MemRxCpltCallback() 00234 (+) In case of transfer Error, HAL_I2C_ErrorCallback() function is executed and users can 00235 add their own code by customization of function pointer HAL_I2C_ErrorCallback() 00236 00237 00238 *** I2C HAL driver macros list *** 00239 ================================== 00240 [..] 00241 Below the list of most used macros in I2C HAL driver. 00242 00243 (+) __HAL_I2C_ENABLE: Enable the I2C peripheral 00244 (+) __HAL_I2C_DISABLE: Disable the I2C peripheral 00245 (+) __HAL_I2C_GENERATE_NACK: Generate a Non-Acknowledge I2C peripheral in Slave mode 00246 (+) __HAL_I2C_GET_FLAG: Check whether the specified I2C flag is set or not 00247 (+) __HAL_I2C_CLEAR_FLAG: Clear the specified I2C pending flag 00248 (+) __HAL_I2C_ENABLE_IT: Enable the specified I2C interrupt 00249 (+) __HAL_I2C_DISABLE_IT: Disable the specified I2C interrupt 00250 00251 *** Callback registration *** 00252 ============================================= 00253 [..] 00254 The compilation flag USE_HAL_I2C_REGISTER_CALLBACKS when set to 1 00255 allows the user to configure dynamically the driver callbacks. 00256 Use Functions HAL_I2C_RegisterCallback() or HAL_I2C_RegisterAddrCallback() 00257 to register an interrupt callback. 00258 [..] 00259 Function HAL_I2C_RegisterCallback() allows to register following callbacks: 00260 (+) MasterTxCpltCallback : callback for Master transmission end of transfer. 00261 (+) MasterRxCpltCallback : callback for Master reception end of transfer. 00262 (+) SlaveTxCpltCallback : callback for Slave transmission end of transfer. 00263 (+) SlaveRxCpltCallback : callback for Slave reception end of transfer. 00264 (+) ListenCpltCallback : callback for end of listen mode. 00265 (+) MemTxCpltCallback : callback for Memory transmission end of transfer. 00266 (+) MemRxCpltCallback : callback for Memory reception end of transfer. 00267 (+) ErrorCallback : callback for error detection. 00268 (+) AbortCpltCallback : callback for abort completion process. 00269 (+) MspInitCallback : callback for Msp Init. 00270 (+) MspDeInitCallback : callback for Msp DeInit. 00271 This function takes as parameters the HAL peripheral handle, the Callback ID 00272 and a pointer to the user callback function. 00273 [..] 00274 For specific callback AddrCallback use dedicated register callbacks : HAL_I2C_RegisterAddrCallback(). 00275 [..] 00276 Use function HAL_I2C_UnRegisterCallback to reset a callback to the default 00277 weak function. 00278 HAL_I2C_UnRegisterCallback takes as parameters the HAL peripheral handle, 00279 and the Callback ID. 00280 This function allows to reset following callbacks: 00281 (+) MasterTxCpltCallback : callback for Master transmission end of transfer. 00282 (+) MasterRxCpltCallback : callback for Master reception end of transfer. 00283 (+) SlaveTxCpltCallback : callback for Slave transmission end of transfer. 00284 (+) SlaveRxCpltCallback : callback for Slave reception end of transfer. 00285 (+) ListenCpltCallback : callback for end of listen mode. 00286 (+) MemTxCpltCallback : callback for Memory transmission end of transfer. 00287 (+) MemRxCpltCallback : callback for Memory reception end of transfer. 00288 (+) ErrorCallback : callback for error detection. 00289 (+) AbortCpltCallback : callback for abort completion process. 00290 (+) MspInitCallback : callback for Msp Init. 00291 (+) MspDeInitCallback : callback for Msp DeInit. 00292 [..] 00293 For callback AddrCallback use dedicated register callbacks : HAL_I2C_UnRegisterAddrCallback(). 00294 [..] 00295 By default, after the HAL_I2C_Init() and when the state is HAL_I2C_STATE_RESET 00296 all callbacks are set to the corresponding weak functions: 00297 examples HAL_I2C_MasterTxCpltCallback(), HAL_I2C_MasterRxCpltCallback(). 00298 Exception done for MspInit and MspDeInit functions that are 00299 reset to the legacy weak functions in the HAL_I2C_Init()/ HAL_I2C_DeInit() only when 00300 these callbacks are null (not registered beforehand). 00301 If MspInit or MspDeInit are not null, the HAL_I2C_Init()/ HAL_I2C_DeInit() 00302 keep and use the user MspInit/MspDeInit callbacks (registered beforehand) whatever the state. 00303 [..] 00304 Callbacks can be registered/unregistered in HAL_I2C_STATE_READY state only. 00305 Exception done MspInit/MspDeInit functions that can be registered/unregistered 00306 in HAL_I2C_STATE_READY or HAL_I2C_STATE_RESET state, 00307 thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit. 00308 Then, the user first registers the MspInit/MspDeInit user callbacks 00309 using HAL_I2C_RegisterCallback() before calling HAL_I2C_DeInit() 00310 or HAL_I2C_Init() function. 00311 [..] 00312 When the compilation flag USE_HAL_I2C_REGISTER_CALLBACKS is set to 0 or 00313 not defined, the callback registration feature is not available and all callbacks 00314 are set to the corresponding weak functions. 00315 00316 [..] 00317 (@) You can refer to the I2C HAL driver header file for more useful macros 00318 00319 @endverbatim 00320 */ 00321 00322 /* Includes ------------------------------------------------------------------*/ 00323 #include "stm32h7xx_hal.h" 00324 00325 /** @addtogroup STM32H7xx_HAL_Driver 00326 * @{ 00327 */ 00328 00329 /** @defgroup I2C I2C 00330 * @brief I2C HAL module driver 00331 * @{ 00332 */ 00333 00334 #ifdef HAL_I2C_MODULE_ENABLED 00335 00336 /* Private typedef -----------------------------------------------------------*/ 00337 /* Private define ------------------------------------------------------------*/ 00338 00339 /** @defgroup I2C_Private_Define I2C Private Define 00340 * @{ 00341 */ 00342 #define TIMING_CLEAR_MASK (0xF0FFFFFFU) /*!< I2C TIMING clear register Mask */ 00343 #define I2C_TIMEOUT_ADDR (10000U) /*!< 10 s */ 00344 #define I2C_TIMEOUT_BUSY (25U) /*!< 25 ms */ 00345 #define I2C_TIMEOUT_DIR (25U) /*!< 25 ms */ 00346 #define I2C_TIMEOUT_RXNE (25U) /*!< 25 ms */ 00347 #define I2C_TIMEOUT_STOPF (25U) /*!< 25 ms */ 00348 #define I2C_TIMEOUT_TC (25U) /*!< 25 ms */ 00349 #define I2C_TIMEOUT_TCR (25U) /*!< 25 ms */ 00350 #define I2C_TIMEOUT_TXIS (25U) /*!< 25 ms */ 00351 #define I2C_TIMEOUT_FLAG (25U) /*!< 25 ms */ 00352 00353 #define MAX_NBYTE_SIZE 255U 00354 #define SLAVE_ADDR_SHIFT 7U 00355 #define SLAVE_ADDR_MSK 0x06U 00356 00357 /* Private define for @ref PreviousState usage */ 00358 #define I2C_STATE_MSK ((uint32_t)((uint32_t)((uint32_t)HAL_I2C_STATE_BUSY_TX | \ 00359 (uint32_t)HAL_I2C_STATE_BUSY_RX) & \ 00360 (uint32_t)(~((uint32_t)HAL_I2C_STATE_READY)))) 00361 /*!< Mask State define, keep only RX and TX bits */ 00362 #define I2C_STATE_NONE ((uint32_t)(HAL_I2C_MODE_NONE)) 00363 /*!< Default Value */ 00364 #define I2C_STATE_MASTER_BUSY_TX ((uint32_t)(((uint32_t)HAL_I2C_STATE_BUSY_TX & I2C_STATE_MSK) | \ 00365 (uint32_t)HAL_I2C_MODE_MASTER)) 00366 /*!< Master Busy TX, combinaison of State LSB and Mode enum */ 00367 #define I2C_STATE_MASTER_BUSY_RX ((uint32_t)(((uint32_t)HAL_I2C_STATE_BUSY_RX & I2C_STATE_MSK) | \ 00368 (uint32_t)HAL_I2C_MODE_MASTER)) 00369 /*!< Master Busy RX, combinaison of State LSB and Mode enum */ 00370 #define I2C_STATE_SLAVE_BUSY_TX ((uint32_t)(((uint32_t)HAL_I2C_STATE_BUSY_TX & I2C_STATE_MSK) | \ 00371 (uint32_t)HAL_I2C_MODE_SLAVE)) 00372 /*!< Slave Busy TX, combinaison of State LSB and Mode enum */ 00373 #define I2C_STATE_SLAVE_BUSY_RX ((uint32_t)(((uint32_t)HAL_I2C_STATE_BUSY_RX & I2C_STATE_MSK) | \ 00374 (uint32_t)HAL_I2C_MODE_SLAVE)) 00375 /*!< Slave Busy RX, combinaison of State LSB and Mode enum */ 00376 #define I2C_STATE_MEM_BUSY_TX ((uint32_t)(((uint32_t)HAL_I2C_STATE_BUSY_TX & I2C_STATE_MSK) | \ 00377 (uint32_t)HAL_I2C_MODE_MEM)) 00378 /*!< Memory Busy TX, combinaison of State LSB and Mode enum */ 00379 #define I2C_STATE_MEM_BUSY_RX ((uint32_t)(((uint32_t)HAL_I2C_STATE_BUSY_RX & I2C_STATE_MSK) | \ 00380 (uint32_t)HAL_I2C_MODE_MEM)) 00381 /*!< Memory Busy RX, combinaison of State LSB and Mode enum */ 00382 00383 00384 /* Private define to centralize the enable/disable of Interrupts */ 00385 #define I2C_XFER_TX_IT (uint16_t)(0x0001U) /*!< Bit field can be combinated with 00386 @ref I2C_XFER_LISTEN_IT */ 00387 #define I2C_XFER_RX_IT (uint16_t)(0x0002U) /*!< Bit field can be combinated with 00388 @ref I2C_XFER_LISTEN_IT */ 00389 #define I2C_XFER_LISTEN_IT (uint16_t)(0x8000U) /*!< Bit field can be combinated with @ref I2C_XFER_TX_IT 00390 and @ref I2C_XFER_RX_IT */ 00391 00392 #define I2C_XFER_ERROR_IT (uint16_t)(0x0010U) /*!< Bit definition to manage addition of global Error 00393 and NACK treatment */ 00394 #define I2C_XFER_CPLT_IT (uint16_t)(0x0020U) /*!< Bit definition to manage only STOP evenement */ 00395 #define I2C_XFER_RELOAD_IT (uint16_t)(0x0040U) /*!< Bit definition to manage only Reload of NBYTE */ 00396 00397 /* Private define Sequential Transfer Options default/reset value */ 00398 #define I2C_NO_OPTION_FRAME (0xFFFF0000U) 00399 /** 00400 * @} 00401 */ 00402 00403 /* Private macro -------------------------------------------------------------*/ 00404 /* Macro to get remaining data to transfer on DMA side */ 00405 #define I2C_GET_DMA_REMAIN_DATA(__HANDLE__) __HAL_DMA_GET_COUNTER(__HANDLE__) 00406 00407 /* Private variables ---------------------------------------------------------*/ 00408 /* Private function prototypes -----------------------------------------------*/ 00409 00410 /** @defgroup I2C_Private_Functions I2C Private Functions 00411 * @{ 00412 */ 00413 /* Private functions to handle DMA transfer */ 00414 static void I2C_DMAMasterTransmitCplt(DMA_HandleTypeDef *hdma); 00415 static void I2C_DMAMasterReceiveCplt(DMA_HandleTypeDef *hdma); 00416 static void I2C_DMASlaveTransmitCplt(DMA_HandleTypeDef *hdma); 00417 static void I2C_DMASlaveReceiveCplt(DMA_HandleTypeDef *hdma); 00418 static void I2C_DMAError(DMA_HandleTypeDef *hdma); 00419 static void I2C_DMAAbort(DMA_HandleTypeDef *hdma); 00420 00421 /* Private functions to handle IT transfer */ 00422 static void I2C_ITAddrCplt(I2C_HandleTypeDef *hi2c, uint32_t ITFlags); 00423 static void I2C_ITMasterSeqCplt(I2C_HandleTypeDef *hi2c); 00424 static void I2C_ITSlaveSeqCplt(I2C_HandleTypeDef *hi2c); 00425 static void I2C_ITMasterCplt(I2C_HandleTypeDef *hi2c, uint32_t ITFlags); 00426 static void I2C_ITSlaveCplt(I2C_HandleTypeDef *hi2c, uint32_t ITFlags); 00427 static void I2C_ITListenCplt(I2C_HandleTypeDef *hi2c, uint32_t ITFlags); 00428 static void I2C_ITError(I2C_HandleTypeDef *hi2c, uint32_t ErrorCode); 00429 00430 /* Private functions to handle IT transfer */ 00431 static HAL_StatusTypeDef I2C_RequestMemoryWrite(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, 00432 uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout, 00433 uint32_t Tickstart); 00434 static HAL_StatusTypeDef I2C_RequestMemoryRead(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, 00435 uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout, 00436 uint32_t Tickstart); 00437 00438 /* Private functions for I2C transfer IRQ handler */ 00439 static HAL_StatusTypeDef I2C_Master_ISR_IT(struct __I2C_HandleTypeDef *hi2c, uint32_t ITFlags, 00440 uint32_t ITSources); 00441 static HAL_StatusTypeDef I2C_Slave_ISR_IT(struct __I2C_HandleTypeDef *hi2c, uint32_t ITFlags, 00442 uint32_t ITSources); 00443 static HAL_StatusTypeDef I2C_Master_ISR_DMA(struct __I2C_HandleTypeDef *hi2c, uint32_t ITFlags, 00444 uint32_t ITSources); 00445 static HAL_StatusTypeDef I2C_Slave_ISR_DMA(struct __I2C_HandleTypeDef *hi2c, uint32_t ITFlags, 00446 uint32_t ITSources); 00447 00448 /* Private functions to handle flags during polling transfer */ 00449 static HAL_StatusTypeDef I2C_WaitOnFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, FlagStatus Status, 00450 uint32_t Timeout, uint32_t Tickstart); 00451 static HAL_StatusTypeDef I2C_WaitOnTXISFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, 00452 uint32_t Tickstart); 00453 static HAL_StatusTypeDef I2C_WaitOnRXNEFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, 00454 uint32_t Tickstart); 00455 static HAL_StatusTypeDef I2C_WaitOnSTOPFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, 00456 uint32_t Tickstart); 00457 static HAL_StatusTypeDef I2C_IsErrorOccurred(I2C_HandleTypeDef *hi2c, uint32_t Timeout, 00458 uint32_t Tickstart); 00459 00460 /* Private functions to centralize the enable/disable of Interrupts */ 00461 static void I2C_Enable_IRQ(I2C_HandleTypeDef *hi2c, uint16_t InterruptRequest); 00462 static void I2C_Disable_IRQ(I2C_HandleTypeDef *hi2c, uint16_t InterruptRequest); 00463 00464 /* Private function to treat different error callback */ 00465 static void I2C_TreatErrorCallback(I2C_HandleTypeDef *hi2c); 00466 00467 /* Private function to flush TXDR register */ 00468 static void I2C_Flush_TXDR(I2C_HandleTypeDef *hi2c); 00469 00470 /* Private function to handle start, restart or stop a transfer */ 00471 static void I2C_TransferConfig(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t Size, uint32_t Mode, 00472 uint32_t Request); 00473 00474 /* Private function to Convert Specific options */ 00475 static void I2C_ConvertOtherXferOptions(I2C_HandleTypeDef *hi2c); 00476 /** 00477 * @} 00478 */ 00479 00480 /* Exported functions --------------------------------------------------------*/ 00481 00482 /** @defgroup I2C_Exported_Functions I2C Exported Functions 00483 * @{ 00484 */ 00485 00486 /** @defgroup I2C_Exported_Functions_Group1 Initialization and de-initialization functions 00487 * @brief Initialization and Configuration functions 00488 * 00489 @verbatim 00490 =============================================================================== 00491 ##### Initialization and de-initialization functions ##### 00492 =============================================================================== 00493 [..] This subsection provides a set of functions allowing to initialize and 00494 deinitialize the I2Cx peripheral: 00495 00496 (+) User must Implement HAL_I2C_MspInit() function in which he configures 00497 all related peripherals resources (CLOCK, GPIO, DMA, IT and NVIC ). 00498 00499 (+) Call the function HAL_I2C_Init() to configure the selected device with 00500 the selected configuration: 00501 (++) Clock Timing 00502 (++) Own Address 1 00503 (++) Addressing mode (Master, Slave) 00504 (++) Dual Addressing mode 00505 (++) Own Address 2 00506 (++) Own Address 2 Mask 00507 (++) General call mode 00508 (++) Nostretch mode 00509 00510 (+) Call the function HAL_I2C_DeInit() to restore the default configuration 00511 of the selected I2Cx peripheral. 00512 00513 @endverbatim 00514 * @{ 00515 */ 00516 00517 /** 00518 * @brief Initializes the I2C according to the specified parameters 00519 * in the I2C_InitTypeDef and initialize the associated handle. 00520 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains 00521 * the configuration information for the specified I2C. 00522 * @retval HAL status 00523 */ 00524 HAL_StatusTypeDef HAL_I2C_Init(I2C_HandleTypeDef *hi2c) 00525 { 00526 /* Check the I2C handle allocation */ 00527 if (hi2c == NULL) 00528 { 00529 return HAL_ERROR; 00530 } 00531 00532 /* Check the parameters */ 00533 assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance)); 00534 assert_param(IS_I2C_OWN_ADDRESS1(hi2c->Init.OwnAddress1)); 00535 assert_param(IS_I2C_ADDRESSING_MODE(hi2c->Init.AddressingMode)); 00536 assert_param(IS_I2C_DUAL_ADDRESS(hi2c->Init.DualAddressMode)); 00537 assert_param(IS_I2C_OWN_ADDRESS2(hi2c->Init.OwnAddress2)); 00538 assert_param(IS_I2C_OWN_ADDRESS2_MASK(hi2c->Init.OwnAddress2Masks)); 00539 assert_param(IS_I2C_GENERAL_CALL(hi2c->Init.GeneralCallMode)); 00540 assert_param(IS_I2C_NO_STRETCH(hi2c->Init.NoStretchMode)); 00541 00542 if (hi2c->State == HAL_I2C_STATE_RESET) 00543 { 00544 /* Allocate lock resource and initialize it */ 00545 hi2c->Lock = HAL_UNLOCKED; 00546 00547 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1) 00548 /* Init the I2C Callback settings */ 00549 hi2c->MasterTxCpltCallback = HAL_I2C_MasterTxCpltCallback; /* Legacy weak MasterTxCpltCallback */ 00550 hi2c->MasterRxCpltCallback = HAL_I2C_MasterRxCpltCallback; /* Legacy weak MasterRxCpltCallback */ 00551 hi2c->SlaveTxCpltCallback = HAL_I2C_SlaveTxCpltCallback; /* Legacy weak SlaveTxCpltCallback */ 00552 hi2c->SlaveRxCpltCallback = HAL_I2C_SlaveRxCpltCallback; /* Legacy weak SlaveRxCpltCallback */ 00553 hi2c->ListenCpltCallback = HAL_I2C_ListenCpltCallback; /* Legacy weak ListenCpltCallback */ 00554 hi2c->MemTxCpltCallback = HAL_I2C_MemTxCpltCallback; /* Legacy weak MemTxCpltCallback */ 00555 hi2c->MemRxCpltCallback = HAL_I2C_MemRxCpltCallback; /* Legacy weak MemRxCpltCallback */ 00556 hi2c->ErrorCallback = HAL_I2C_ErrorCallback; /* Legacy weak ErrorCallback */ 00557 hi2c->AbortCpltCallback = HAL_I2C_AbortCpltCallback; /* Legacy weak AbortCpltCallback */ 00558 hi2c->AddrCallback = HAL_I2C_AddrCallback; /* Legacy weak AddrCallback */ 00559 00560 if (hi2c->MspInitCallback == NULL) 00561 { 00562 hi2c->MspInitCallback = HAL_I2C_MspInit; /* Legacy weak MspInit */ 00563 } 00564 00565 /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */ 00566 hi2c->MspInitCallback(hi2c); 00567 #else 00568 /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */ 00569 HAL_I2C_MspInit(hi2c); 00570 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */ 00571 } 00572 00573 hi2c->State = HAL_I2C_STATE_BUSY; 00574 00575 /* Disable the selected I2C peripheral */ 00576 __HAL_I2C_DISABLE(hi2c); 00577 00578 /*---------------------------- I2Cx TIMINGR Configuration ------------------*/ 00579 /* Configure I2Cx: Frequency range */ 00580 hi2c->Instance->TIMINGR = hi2c->Init.Timing & TIMING_CLEAR_MASK; 00581 00582 /*---------------------------- I2Cx OAR1 Configuration ---------------------*/ 00583 /* Disable Own Address1 before set the Own Address1 configuration */ 00584 hi2c->Instance->OAR1 &= ~I2C_OAR1_OA1EN; 00585 00586 /* Configure I2Cx: Own Address1 and ack own address1 mode */ 00587 if (hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_7BIT) 00588 { 00589 hi2c->Instance->OAR1 = (I2C_OAR1_OA1EN | hi2c->Init.OwnAddress1); 00590 } 00591 else /* I2C_ADDRESSINGMODE_10BIT */ 00592 { 00593 hi2c->Instance->OAR1 = (I2C_OAR1_OA1EN | I2C_OAR1_OA1MODE | hi2c->Init.OwnAddress1); 00594 } 00595 00596 /*---------------------------- I2Cx CR2 Configuration ----------------------*/ 00597 /* Configure I2Cx: Addressing Master mode */ 00598 if (hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_10BIT) 00599 { 00600 hi2c->Instance->CR2 = (I2C_CR2_ADD10); 00601 } 00602 /* Enable the AUTOEND by default, and enable NACK (should be disable only during Slave process */ 00603 hi2c->Instance->CR2 |= (I2C_CR2_AUTOEND | I2C_CR2_NACK); 00604 00605 /*---------------------------- I2Cx OAR2 Configuration ---------------------*/ 00606 /* Disable Own Address2 before set the Own Address2 configuration */ 00607 hi2c->Instance->OAR2 &= ~I2C_DUALADDRESS_ENABLE; 00608 00609 /* Configure I2Cx: Dual mode and Own Address2 */ 00610 hi2c->Instance->OAR2 = (hi2c->Init.DualAddressMode | hi2c->Init.OwnAddress2 | \ 00611 (hi2c->Init.OwnAddress2Masks << 8)); 00612 00613 /*---------------------------- I2Cx CR1 Configuration ----------------------*/ 00614 /* Configure I2Cx: Generalcall and NoStretch mode */ 00615 hi2c->Instance->CR1 = (hi2c->Init.GeneralCallMode | hi2c->Init.NoStretchMode); 00616 00617 /* Enable the selected I2C peripheral */ 00618 __HAL_I2C_ENABLE(hi2c); 00619 00620 hi2c->ErrorCode = HAL_I2C_ERROR_NONE; 00621 hi2c->State = HAL_I2C_STATE_READY; 00622 hi2c->PreviousState = I2C_STATE_NONE; 00623 hi2c->Mode = HAL_I2C_MODE_NONE; 00624 00625 return HAL_OK; 00626 } 00627 00628 /** 00629 * @brief DeInitialize the I2C peripheral. 00630 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains 00631 * the configuration information for the specified I2C. 00632 * @retval HAL status 00633 */ 00634 HAL_StatusTypeDef HAL_I2C_DeInit(I2C_HandleTypeDef *hi2c) 00635 { 00636 /* Check the I2C handle allocation */ 00637 if (hi2c == NULL) 00638 { 00639 return HAL_ERROR; 00640 } 00641 00642 /* Check the parameters */ 00643 assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance)); 00644 00645 hi2c->State = HAL_I2C_STATE_BUSY; 00646 00647 /* Disable the I2C Peripheral Clock */ 00648 __HAL_I2C_DISABLE(hi2c); 00649 00650 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1) 00651 if (hi2c->MspDeInitCallback == NULL) 00652 { 00653 hi2c->MspDeInitCallback = HAL_I2C_MspDeInit; /* Legacy weak MspDeInit */ 00654 } 00655 00656 /* DeInit the low level hardware: GPIO, CLOCK, NVIC */ 00657 hi2c->MspDeInitCallback(hi2c); 00658 #else 00659 /* DeInit the low level hardware: GPIO, CLOCK, NVIC */ 00660 HAL_I2C_MspDeInit(hi2c); 00661 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */ 00662 00663 hi2c->ErrorCode = HAL_I2C_ERROR_NONE; 00664 hi2c->State = HAL_I2C_STATE_RESET; 00665 hi2c->PreviousState = I2C_STATE_NONE; 00666 hi2c->Mode = HAL_I2C_MODE_NONE; 00667 00668 /* Release Lock */ 00669 __HAL_UNLOCK(hi2c); 00670 00671 return HAL_OK; 00672 } 00673 00674 /** 00675 * @brief Initialize the I2C MSP. 00676 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains 00677 * the configuration information for the specified I2C. 00678 * @retval None 00679 */ 00680 __weak void HAL_I2C_MspInit(I2C_HandleTypeDef *hi2c) 00681 { 00682 /* Prevent unused argument(s) compilation warning */ 00683 UNUSED(hi2c); 00684 00685 /* NOTE : This function should not be modified, when the callback is needed, 00686 the HAL_I2C_MspInit could be implemented in the user file 00687 */ 00688 } 00689 00690 /** 00691 * @brief DeInitialize the I2C MSP. 00692 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains 00693 * the configuration information for the specified I2C. 00694 * @retval None 00695 */ 00696 __weak void HAL_I2C_MspDeInit(I2C_HandleTypeDef *hi2c) 00697 { 00698 /* Prevent unused argument(s) compilation warning */ 00699 UNUSED(hi2c); 00700 00701 /* NOTE : This function should not be modified, when the callback is needed, 00702 the HAL_I2C_MspDeInit could be implemented in the user file 00703 */ 00704 } 00705 00706 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1) 00707 /** 00708 * @brief Register a User I2C Callback 00709 * To be used instead of the weak predefined callback 00710 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains 00711 * the configuration information for the specified I2C. 00712 * @param CallbackID ID of the callback to be registered 00713 * This parameter can be one of the following values: 00714 * @arg @ref HAL_I2C_MASTER_TX_COMPLETE_CB_ID Master Tx Transfer completed callback ID 00715 * @arg @ref HAL_I2C_MASTER_RX_COMPLETE_CB_ID Master Rx Transfer completed callback ID 00716 * @arg @ref HAL_I2C_SLAVE_TX_COMPLETE_CB_ID Slave Tx Transfer completed callback ID 00717 * @arg @ref HAL_I2C_SLAVE_RX_COMPLETE_CB_ID Slave Rx Transfer completed callback ID 00718 * @arg @ref HAL_I2C_LISTEN_COMPLETE_CB_ID Listen Complete callback ID 00719 * @arg @ref HAL_I2C_MEM_TX_COMPLETE_CB_ID Memory Tx Transfer callback ID 00720 * @arg @ref HAL_I2C_MEM_RX_COMPLETE_CB_ID Memory Rx Transfer completed callback ID 00721 * @arg @ref HAL_I2C_ERROR_CB_ID Error callback ID 00722 * @arg @ref HAL_I2C_ABORT_CB_ID Abort callback ID 00723 * @arg @ref HAL_I2C_MSPINIT_CB_ID MspInit callback ID 00724 * @arg @ref HAL_I2C_MSPDEINIT_CB_ID MspDeInit callback ID 00725 * @param pCallback pointer to the Callback function 00726 * @retval HAL status 00727 */ 00728 HAL_StatusTypeDef HAL_I2C_RegisterCallback(I2C_HandleTypeDef *hi2c, HAL_I2C_CallbackIDTypeDef CallbackID, 00729 pI2C_CallbackTypeDef pCallback) 00730 { 00731 HAL_StatusTypeDef status = HAL_OK; 00732 00733 if (pCallback == NULL) 00734 { 00735 /* Update the error code */ 00736 hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK; 00737 00738 return HAL_ERROR; 00739 } 00740 /* Process locked */ 00741 __HAL_LOCK(hi2c); 00742 00743 if (HAL_I2C_STATE_READY == hi2c->State) 00744 { 00745 switch (CallbackID) 00746 { 00747 case HAL_I2C_MASTER_TX_COMPLETE_CB_ID : 00748 hi2c->MasterTxCpltCallback = pCallback; 00749 break; 00750 00751 case HAL_I2C_MASTER_RX_COMPLETE_CB_ID : 00752 hi2c->MasterRxCpltCallback = pCallback; 00753 break; 00754 00755 case HAL_I2C_SLAVE_TX_COMPLETE_CB_ID : 00756 hi2c->SlaveTxCpltCallback = pCallback; 00757 break; 00758 00759 case HAL_I2C_SLAVE_RX_COMPLETE_CB_ID : 00760 hi2c->SlaveRxCpltCallback = pCallback; 00761 break; 00762 00763 case HAL_I2C_LISTEN_COMPLETE_CB_ID : 00764 hi2c->ListenCpltCallback = pCallback; 00765 break; 00766 00767 case HAL_I2C_MEM_TX_COMPLETE_CB_ID : 00768 hi2c->MemTxCpltCallback = pCallback; 00769 break; 00770 00771 case HAL_I2C_MEM_RX_COMPLETE_CB_ID : 00772 hi2c->MemRxCpltCallback = pCallback; 00773 break; 00774 00775 case HAL_I2C_ERROR_CB_ID : 00776 hi2c->ErrorCallback = pCallback; 00777 break; 00778 00779 case HAL_I2C_ABORT_CB_ID : 00780 hi2c->AbortCpltCallback = pCallback; 00781 break; 00782 00783 case HAL_I2C_MSPINIT_CB_ID : 00784 hi2c->MspInitCallback = pCallback; 00785 break; 00786 00787 case HAL_I2C_MSPDEINIT_CB_ID : 00788 hi2c->MspDeInitCallback = pCallback; 00789 break; 00790 00791 default : 00792 /* Update the error code */ 00793 hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK; 00794 00795 /* Return error status */ 00796 status = HAL_ERROR; 00797 break; 00798 } 00799 } 00800 else if (HAL_I2C_STATE_RESET == hi2c->State) 00801 { 00802 switch (CallbackID) 00803 { 00804 case HAL_I2C_MSPINIT_CB_ID : 00805 hi2c->MspInitCallback = pCallback; 00806 break; 00807 00808 case HAL_I2C_MSPDEINIT_CB_ID : 00809 hi2c->MspDeInitCallback = pCallback; 00810 break; 00811 00812 default : 00813 /* Update the error code */ 00814 hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK; 00815 00816 /* Return error status */ 00817 status = HAL_ERROR; 00818 break; 00819 } 00820 } 00821 else 00822 { 00823 /* Update the error code */ 00824 hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK; 00825 00826 /* Return error status */ 00827 status = HAL_ERROR; 00828 } 00829 00830 /* Release Lock */ 00831 __HAL_UNLOCK(hi2c); 00832 return status; 00833 } 00834 00835 /** 00836 * @brief Unregister an I2C Callback 00837 * I2C callback is redirected to the weak predefined callback 00838 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains 00839 * the configuration information for the specified I2C. 00840 * @param CallbackID ID of the callback to be unregistered 00841 * This parameter can be one of the following values: 00842 * This parameter can be one of the following values: 00843 * @arg @ref HAL_I2C_MASTER_TX_COMPLETE_CB_ID Master Tx Transfer completed callback ID 00844 * @arg @ref HAL_I2C_MASTER_RX_COMPLETE_CB_ID Master Rx Transfer completed callback ID 00845 * @arg @ref HAL_I2C_SLAVE_TX_COMPLETE_CB_ID Slave Tx Transfer completed callback ID 00846 * @arg @ref HAL_I2C_SLAVE_RX_COMPLETE_CB_ID Slave Rx Transfer completed callback ID 00847 * @arg @ref HAL_I2C_LISTEN_COMPLETE_CB_ID Listen Complete callback ID 00848 * @arg @ref HAL_I2C_MEM_TX_COMPLETE_CB_ID Memory Tx Transfer callback ID 00849 * @arg @ref HAL_I2C_MEM_RX_COMPLETE_CB_ID Memory Rx Transfer completed callback ID 00850 * @arg @ref HAL_I2C_ERROR_CB_ID Error callback ID 00851 * @arg @ref HAL_I2C_ABORT_CB_ID Abort callback ID 00852 * @arg @ref HAL_I2C_MSPINIT_CB_ID MspInit callback ID 00853 * @arg @ref HAL_I2C_MSPDEINIT_CB_ID MspDeInit callback ID 00854 * @retval HAL status 00855 */ 00856 HAL_StatusTypeDef HAL_I2C_UnRegisterCallback(I2C_HandleTypeDef *hi2c, HAL_I2C_CallbackIDTypeDef CallbackID) 00857 { 00858 HAL_StatusTypeDef status = HAL_OK; 00859 00860 /* Process locked */ 00861 __HAL_LOCK(hi2c); 00862 00863 if (HAL_I2C_STATE_READY == hi2c->State) 00864 { 00865 switch (CallbackID) 00866 { 00867 case HAL_I2C_MASTER_TX_COMPLETE_CB_ID : 00868 hi2c->MasterTxCpltCallback = HAL_I2C_MasterTxCpltCallback; /* Legacy weak MasterTxCpltCallback */ 00869 break; 00870 00871 case HAL_I2C_MASTER_RX_COMPLETE_CB_ID : 00872 hi2c->MasterRxCpltCallback = HAL_I2C_MasterRxCpltCallback; /* Legacy weak MasterRxCpltCallback */ 00873 break; 00874 00875 case HAL_I2C_SLAVE_TX_COMPLETE_CB_ID : 00876 hi2c->SlaveTxCpltCallback = HAL_I2C_SlaveTxCpltCallback; /* Legacy weak SlaveTxCpltCallback */ 00877 break; 00878 00879 case HAL_I2C_SLAVE_RX_COMPLETE_CB_ID : 00880 hi2c->SlaveRxCpltCallback = HAL_I2C_SlaveRxCpltCallback; /* Legacy weak SlaveRxCpltCallback */ 00881 break; 00882 00883 case HAL_I2C_LISTEN_COMPLETE_CB_ID : 00884 hi2c->ListenCpltCallback = HAL_I2C_ListenCpltCallback; /* Legacy weak ListenCpltCallback */ 00885 break; 00886 00887 case HAL_I2C_MEM_TX_COMPLETE_CB_ID : 00888 hi2c->MemTxCpltCallback = HAL_I2C_MemTxCpltCallback; /* Legacy weak MemTxCpltCallback */ 00889 break; 00890 00891 case HAL_I2C_MEM_RX_COMPLETE_CB_ID : 00892 hi2c->MemRxCpltCallback = HAL_I2C_MemRxCpltCallback; /* Legacy weak MemRxCpltCallback */ 00893 break; 00894 00895 case HAL_I2C_ERROR_CB_ID : 00896 hi2c->ErrorCallback = HAL_I2C_ErrorCallback; /* Legacy weak ErrorCallback */ 00897 break; 00898 00899 case HAL_I2C_ABORT_CB_ID : 00900 hi2c->AbortCpltCallback = HAL_I2C_AbortCpltCallback; /* Legacy weak AbortCpltCallback */ 00901 break; 00902 00903 case HAL_I2C_MSPINIT_CB_ID : 00904 hi2c->MspInitCallback = HAL_I2C_MspInit; /* Legacy weak MspInit */ 00905 break; 00906 00907 case HAL_I2C_MSPDEINIT_CB_ID : 00908 hi2c->MspDeInitCallback = HAL_I2C_MspDeInit; /* Legacy weak MspDeInit */ 00909 break; 00910 00911 default : 00912 /* Update the error code */ 00913 hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK; 00914 00915 /* Return error status */ 00916 status = HAL_ERROR; 00917 break; 00918 } 00919 } 00920 else if (HAL_I2C_STATE_RESET == hi2c->State) 00921 { 00922 switch (CallbackID) 00923 { 00924 case HAL_I2C_MSPINIT_CB_ID : 00925 hi2c->MspInitCallback = HAL_I2C_MspInit; /* Legacy weak MspInit */ 00926 break; 00927 00928 case HAL_I2C_MSPDEINIT_CB_ID : 00929 hi2c->MspDeInitCallback = HAL_I2C_MspDeInit; /* Legacy weak MspDeInit */ 00930 break; 00931 00932 default : 00933 /* Update the error code */ 00934 hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK; 00935 00936 /* Return error status */ 00937 status = HAL_ERROR; 00938 break; 00939 } 00940 } 00941 else 00942 { 00943 /* Update the error code */ 00944 hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK; 00945 00946 /* Return error status */ 00947 status = HAL_ERROR; 00948 } 00949 00950 /* Release Lock */ 00951 __HAL_UNLOCK(hi2c); 00952 return status; 00953 } 00954 00955 /** 00956 * @brief Register the Slave Address Match I2C Callback 00957 * To be used instead of the weak HAL_I2C_AddrCallback() predefined callback 00958 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains 00959 * the configuration information for the specified I2C. 00960 * @param pCallback pointer to the Address Match Callback function 00961 * @retval HAL status 00962 */ 00963 HAL_StatusTypeDef HAL_I2C_RegisterAddrCallback(I2C_HandleTypeDef *hi2c, pI2C_AddrCallbackTypeDef pCallback) 00964 { 00965 HAL_StatusTypeDef status = HAL_OK; 00966 00967 if (pCallback == NULL) 00968 { 00969 /* Update the error code */ 00970 hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK; 00971 00972 return HAL_ERROR; 00973 } 00974 /* Process locked */ 00975 __HAL_LOCK(hi2c); 00976 00977 if (HAL_I2C_STATE_READY == hi2c->State) 00978 { 00979 hi2c->AddrCallback = pCallback; 00980 } 00981 else 00982 { 00983 /* Update the error code */ 00984 hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK; 00985 00986 /* Return error status */ 00987 status = HAL_ERROR; 00988 } 00989 00990 /* Release Lock */ 00991 __HAL_UNLOCK(hi2c); 00992 return status; 00993 } 00994 00995 /** 00996 * @brief UnRegister the Slave Address Match I2C Callback 00997 * Info Ready I2C Callback is redirected to the weak HAL_I2C_AddrCallback() predefined callback 00998 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains 00999 * the configuration information for the specified I2C. 01000 * @retval HAL status 01001 */ 01002 HAL_StatusTypeDef HAL_I2C_UnRegisterAddrCallback(I2C_HandleTypeDef *hi2c) 01003 { 01004 HAL_StatusTypeDef status = HAL_OK; 01005 01006 /* Process locked */ 01007 __HAL_LOCK(hi2c); 01008 01009 if (HAL_I2C_STATE_READY == hi2c->State) 01010 { 01011 hi2c->AddrCallback = HAL_I2C_AddrCallback; /* Legacy weak AddrCallback */ 01012 } 01013 else 01014 { 01015 /* Update the error code */ 01016 hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK; 01017 01018 /* Return error status */ 01019 status = HAL_ERROR; 01020 } 01021 01022 /* Release Lock */ 01023 __HAL_UNLOCK(hi2c); 01024 return status; 01025 } 01026 01027 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */ 01028 01029 /** 01030 * @} 01031 */ 01032 01033 /** @defgroup I2C_Exported_Functions_Group2 Input and Output operation functions 01034 * @brief Data transfers functions 01035 * 01036 @verbatim 01037 =============================================================================== 01038 ##### IO operation functions ##### 01039 =============================================================================== 01040 [..] 01041 This subsection provides a set of functions allowing to manage the I2C data 01042 transfers. 01043 01044 (#) There are two modes of transfer: 01045 (++) Blocking mode : The communication is performed in the polling mode. 01046 The status of all data processing is returned by the same function 01047 after finishing transfer. 01048 (++) No-Blocking mode : The communication is performed using Interrupts 01049 or DMA. These functions return the status of the transfer startup. 01050 The end of the data processing will be indicated through the 01051 dedicated I2C IRQ when using Interrupt mode or the DMA IRQ when 01052 using DMA mode. 01053 01054 (#) Blocking mode functions are : 01055 (++) HAL_I2C_Master_Transmit() 01056 (++) HAL_I2C_Master_Receive() 01057 (++) HAL_I2C_Slave_Transmit() 01058 (++) HAL_I2C_Slave_Receive() 01059 (++) HAL_I2C_Mem_Write() 01060 (++) HAL_I2C_Mem_Read() 01061 (++) HAL_I2C_IsDeviceReady() 01062 01063 (#) No-Blocking mode functions with Interrupt are : 01064 (++) HAL_I2C_Master_Transmit_IT() 01065 (++) HAL_I2C_Master_Receive_IT() 01066 (++) HAL_I2C_Slave_Transmit_IT() 01067 (++) HAL_I2C_Slave_Receive_IT() 01068 (++) HAL_I2C_Mem_Write_IT() 01069 (++) HAL_I2C_Mem_Read_IT() 01070 (++) HAL_I2C_Master_Seq_Transmit_IT() 01071 (++) HAL_I2C_Master_Seq_Receive_IT() 01072 (++) HAL_I2C_Slave_Seq_Transmit_IT() 01073 (++) HAL_I2C_Slave_Seq_Receive_IT() 01074 (++) HAL_I2C_EnableListen_IT() 01075 (++) HAL_I2C_DisableListen_IT() 01076 (++) HAL_I2C_Master_Abort_IT() 01077 01078 (#) No-Blocking mode functions with DMA are : 01079 (++) HAL_I2C_Master_Transmit_DMA() 01080 (++) HAL_I2C_Master_Receive_DMA() 01081 (++) HAL_I2C_Slave_Transmit_DMA() 01082 (++) HAL_I2C_Slave_Receive_DMA() 01083 (++) HAL_I2C_Mem_Write_DMA() 01084 (++) HAL_I2C_Mem_Read_DMA() 01085 (++) HAL_I2C_Master_Seq_Transmit_DMA() 01086 (++) HAL_I2C_Master_Seq_Receive_DMA() 01087 (++) HAL_I2C_Slave_Seq_Transmit_DMA() 01088 (++) HAL_I2C_Slave_Seq_Receive_DMA() 01089 01090 (#) A set of Transfer Complete Callbacks are provided in non Blocking mode: 01091 (++) HAL_I2C_MasterTxCpltCallback() 01092 (++) HAL_I2C_MasterRxCpltCallback() 01093 (++) HAL_I2C_SlaveTxCpltCallback() 01094 (++) HAL_I2C_SlaveRxCpltCallback() 01095 (++) HAL_I2C_MemTxCpltCallback() 01096 (++) HAL_I2C_MemRxCpltCallback() 01097 (++) HAL_I2C_AddrCallback() 01098 (++) HAL_I2C_ListenCpltCallback() 01099 (++) HAL_I2C_ErrorCallback() 01100 (++) HAL_I2C_AbortCpltCallback() 01101 01102 @endverbatim 01103 * @{ 01104 */ 01105 01106 /** 01107 * @brief Transmits in master mode an amount of data in blocking mode. 01108 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains 01109 * the configuration information for the specified I2C. 01110 * @param DevAddress Target device address: The device 7 bits address value 01111 * in datasheet must be shifted to the left before calling the interface 01112 * @param pData Pointer to data buffer 01113 * @param Size Amount of data to be sent 01114 * @param Timeout Timeout duration 01115 * @retval HAL status 01116 */ 01117 HAL_StatusTypeDef HAL_I2C_Master_Transmit(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, 01118 uint16_t Size, uint32_t Timeout) 01119 { 01120 uint32_t tickstart; 01121 01122 if (hi2c->State == HAL_I2C_STATE_READY) 01123 { 01124 /* Process Locked */ 01125 __HAL_LOCK(hi2c); 01126 01127 /* Init tickstart for timeout management*/ 01128 tickstart = HAL_GetTick(); 01129 01130 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY, tickstart) != HAL_OK) 01131 { 01132 return HAL_ERROR; 01133 } 01134 01135 hi2c->State = HAL_I2C_STATE_BUSY_TX; 01136 hi2c->Mode = HAL_I2C_MODE_MASTER; 01137 hi2c->ErrorCode = HAL_I2C_ERROR_NONE; 01138 01139 /* Prepare transfer parameters */ 01140 hi2c->pBuffPtr = pData; 01141 hi2c->XferCount = Size; 01142 hi2c->XferISR = NULL; 01143 01144 /* Send Slave Address */ 01145 /* Set NBYTES to write and reload if hi2c->XferCount > MAX_NBYTE_SIZE and generate RESTART */ 01146 if (hi2c->XferCount > MAX_NBYTE_SIZE) 01147 { 01148 hi2c->XferSize = MAX_NBYTE_SIZE; 01149 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_RELOAD_MODE, 01150 I2C_GENERATE_START_WRITE); 01151 } 01152 else 01153 { 01154 hi2c->XferSize = hi2c->XferCount; 01155 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_AUTOEND_MODE, 01156 I2C_GENERATE_START_WRITE); 01157 } 01158 01159 while (hi2c->XferCount > 0U) 01160 { 01161 /* Wait until TXIS flag is set */ 01162 if (I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK) 01163 { 01164 return HAL_ERROR; 01165 } 01166 /* Write data to TXDR */ 01167 hi2c->Instance->TXDR = *hi2c->pBuffPtr; 01168 01169 /* Increment Buffer pointer */ 01170 hi2c->pBuffPtr++; 01171 01172 hi2c->XferCount--; 01173 hi2c->XferSize--; 01174 01175 if ((hi2c->XferCount != 0U) && (hi2c->XferSize == 0U)) 01176 { 01177 /* Wait until TCR flag is set */ 01178 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, Timeout, tickstart) != HAL_OK) 01179 { 01180 return HAL_ERROR; 01181 } 01182 01183 if (hi2c->XferCount > MAX_NBYTE_SIZE) 01184 { 01185 hi2c->XferSize = MAX_NBYTE_SIZE; 01186 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_RELOAD_MODE, 01187 I2C_NO_STARTSTOP); 01188 } 01189 else 01190 { 01191 hi2c->XferSize = hi2c->XferCount; 01192 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_AUTOEND_MODE, 01193 I2C_NO_STARTSTOP); 01194 } 01195 } 01196 } 01197 01198 /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */ 01199 /* Wait until STOPF flag is set */ 01200 if (I2C_WaitOnSTOPFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK) 01201 { 01202 return HAL_ERROR; 01203 } 01204 01205 /* Clear STOP Flag */ 01206 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF); 01207 01208 /* Clear Configuration Register 2 */ 01209 I2C_RESET_CR2(hi2c); 01210 01211 hi2c->State = HAL_I2C_STATE_READY; 01212 hi2c->Mode = HAL_I2C_MODE_NONE; 01213 01214 /* Process Unlocked */ 01215 __HAL_UNLOCK(hi2c); 01216 01217 return HAL_OK; 01218 } 01219 else 01220 { 01221 return HAL_BUSY; 01222 } 01223 } 01224 01225 /** 01226 * @brief Receives in master mode an amount of data in blocking mode. 01227 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains 01228 * the configuration information for the specified I2C. 01229 * @param DevAddress Target device address: The device 7 bits address value 01230 * in datasheet must be shifted to the left before calling the interface 01231 * @param pData Pointer to data buffer 01232 * @param Size Amount of data to be sent 01233 * @param Timeout Timeout duration 01234 * @retval HAL status 01235 */ 01236 HAL_StatusTypeDef HAL_I2C_Master_Receive(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, 01237 uint16_t Size, uint32_t Timeout) 01238 { 01239 uint32_t tickstart; 01240 01241 if (hi2c->State == HAL_I2C_STATE_READY) 01242 { 01243 /* Process Locked */ 01244 __HAL_LOCK(hi2c); 01245 01246 /* Init tickstart for timeout management*/ 01247 tickstart = HAL_GetTick(); 01248 01249 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY, tickstart) != HAL_OK) 01250 { 01251 return HAL_ERROR; 01252 } 01253 01254 hi2c->State = HAL_I2C_STATE_BUSY_RX; 01255 hi2c->Mode = HAL_I2C_MODE_MASTER; 01256 hi2c->ErrorCode = HAL_I2C_ERROR_NONE; 01257 01258 /* Prepare transfer parameters */ 01259 hi2c->pBuffPtr = pData; 01260 hi2c->XferCount = Size; 01261 hi2c->XferISR = NULL; 01262 01263 /* Send Slave Address */ 01264 /* Set NBYTES to write and reload if hi2c->XferCount > MAX_NBYTE_SIZE and generate RESTART */ 01265 if (hi2c->XferCount > MAX_NBYTE_SIZE) 01266 { 01267 hi2c->XferSize = MAX_NBYTE_SIZE; 01268 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_RELOAD_MODE, 01269 I2C_GENERATE_START_READ); 01270 } 01271 else 01272 { 01273 hi2c->XferSize = hi2c->XferCount; 01274 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_AUTOEND_MODE, 01275 I2C_GENERATE_START_READ); 01276 } 01277 01278 while (hi2c->XferCount > 0U) 01279 { 01280 /* Wait until RXNE flag is set */ 01281 if (I2C_WaitOnRXNEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK) 01282 { 01283 return HAL_ERROR; 01284 } 01285 01286 /* Read data from RXDR */ 01287 *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->RXDR; 01288 01289 /* Increment Buffer pointer */ 01290 hi2c->pBuffPtr++; 01291 01292 hi2c->XferSize--; 01293 hi2c->XferCount--; 01294 01295 if ((hi2c->XferCount != 0U) && (hi2c->XferSize == 0U)) 01296 { 01297 /* Wait until TCR flag is set */ 01298 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, Timeout, tickstart) != HAL_OK) 01299 { 01300 return HAL_ERROR; 01301 } 01302 01303 if (hi2c->XferCount > MAX_NBYTE_SIZE) 01304 { 01305 hi2c->XferSize = MAX_NBYTE_SIZE; 01306 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_RELOAD_MODE, 01307 I2C_NO_STARTSTOP); 01308 } 01309 else 01310 { 01311 hi2c->XferSize = hi2c->XferCount; 01312 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_AUTOEND_MODE, 01313 I2C_NO_STARTSTOP); 01314 } 01315 } 01316 } 01317 01318 /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */ 01319 /* Wait until STOPF flag is set */ 01320 if (I2C_WaitOnSTOPFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK) 01321 { 01322 return HAL_ERROR; 01323 } 01324 01325 /* Clear STOP Flag */ 01326 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF); 01327 01328 /* Clear Configuration Register 2 */ 01329 I2C_RESET_CR2(hi2c); 01330 01331 hi2c->State = HAL_I2C_STATE_READY; 01332 hi2c->Mode = HAL_I2C_MODE_NONE; 01333 01334 /* Process Unlocked */ 01335 __HAL_UNLOCK(hi2c); 01336 01337 return HAL_OK; 01338 } 01339 else 01340 { 01341 return HAL_BUSY; 01342 } 01343 } 01344 01345 /** 01346 * @brief Transmits in slave mode an amount of data in blocking mode. 01347 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains 01348 * the configuration information for the specified I2C. 01349 * @param pData Pointer to data buffer 01350 * @param Size Amount of data to be sent 01351 * @param Timeout Timeout duration 01352 * @retval HAL status 01353 */ 01354 HAL_StatusTypeDef HAL_I2C_Slave_Transmit(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, 01355 uint32_t Timeout) 01356 { 01357 uint32_t tickstart; 01358 01359 if (hi2c->State == HAL_I2C_STATE_READY) 01360 { 01361 if ((pData == NULL) || (Size == 0U)) 01362 { 01363 hi2c->ErrorCode = HAL_I2C_ERROR_INVALID_PARAM; 01364 return HAL_ERROR; 01365 } 01366 /* Process Locked */ 01367 __HAL_LOCK(hi2c); 01368 01369 /* Init tickstart for timeout management*/ 01370 tickstart = HAL_GetTick(); 01371 01372 hi2c->State = HAL_I2C_STATE_BUSY_TX; 01373 hi2c->Mode = HAL_I2C_MODE_SLAVE; 01374 hi2c->ErrorCode = HAL_I2C_ERROR_NONE; 01375 01376 /* Prepare transfer parameters */ 01377 hi2c->pBuffPtr = pData; 01378 hi2c->XferCount = Size; 01379 hi2c->XferISR = NULL; 01380 01381 /* Enable Address Acknowledge */ 01382 hi2c->Instance->CR2 &= ~I2C_CR2_NACK; 01383 01384 /* Wait until ADDR flag is set */ 01385 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, Timeout, tickstart) != HAL_OK) 01386 { 01387 /* Disable Address Acknowledge */ 01388 hi2c->Instance->CR2 |= I2C_CR2_NACK; 01389 return HAL_ERROR; 01390 } 01391 01392 /* Clear ADDR flag */ 01393 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ADDR); 01394 01395 /* If 10bit addressing mode is selected */ 01396 if (hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_10BIT) 01397 { 01398 /* Wait until ADDR flag is set */ 01399 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, Timeout, tickstart) != HAL_OK) 01400 { 01401 /* Disable Address Acknowledge */ 01402 hi2c->Instance->CR2 |= I2C_CR2_NACK; 01403 return HAL_ERROR; 01404 } 01405 01406 /* Clear ADDR flag */ 01407 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ADDR); 01408 } 01409 01410 /* Wait until DIR flag is set Transmitter mode */ 01411 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_DIR, RESET, Timeout, tickstart) != HAL_OK) 01412 { 01413 /* Disable Address Acknowledge */ 01414 hi2c->Instance->CR2 |= I2C_CR2_NACK; 01415 return HAL_ERROR; 01416 } 01417 01418 while (hi2c->XferCount > 0U) 01419 { 01420 /* Wait until TXIS flag is set */ 01421 if (I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK) 01422 { 01423 /* Disable Address Acknowledge */ 01424 hi2c->Instance->CR2 |= I2C_CR2_NACK; 01425 return HAL_ERROR; 01426 } 01427 01428 /* Write data to TXDR */ 01429 hi2c->Instance->TXDR = *hi2c->pBuffPtr; 01430 01431 /* Increment Buffer pointer */ 01432 hi2c->pBuffPtr++; 01433 01434 hi2c->XferCount--; 01435 } 01436 01437 /* Wait until AF flag is set */ 01438 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_AF, RESET, Timeout, tickstart) != HAL_OK) 01439 { 01440 /* Disable Address Acknowledge */ 01441 hi2c->Instance->CR2 |= I2C_CR2_NACK; 01442 return HAL_ERROR; 01443 } 01444 01445 /* Flush TX register */ 01446 I2C_Flush_TXDR(hi2c); 01447 01448 /* Clear AF flag */ 01449 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF); 01450 01451 /* Wait until STOP flag is set */ 01452 if (I2C_WaitOnSTOPFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK) 01453 { 01454 /* Disable Address Acknowledge */ 01455 hi2c->Instance->CR2 |= I2C_CR2_NACK; 01456 01457 return HAL_ERROR; 01458 } 01459 01460 /* Clear STOP flag */ 01461 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF); 01462 01463 /* Wait until BUSY flag is reset */ 01464 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, Timeout, tickstart) != HAL_OK) 01465 { 01466 /* Disable Address Acknowledge */ 01467 hi2c->Instance->CR2 |= I2C_CR2_NACK; 01468 return HAL_ERROR; 01469 } 01470 01471 /* Disable Address Acknowledge */ 01472 hi2c->Instance->CR2 |= I2C_CR2_NACK; 01473 01474 hi2c->State = HAL_I2C_STATE_READY; 01475 hi2c->Mode = HAL_I2C_MODE_NONE; 01476 01477 /* Process Unlocked */ 01478 __HAL_UNLOCK(hi2c); 01479 01480 return HAL_OK; 01481 } 01482 else 01483 { 01484 return HAL_BUSY; 01485 } 01486 } 01487 01488 /** 01489 * @brief Receive in slave mode an amount of data in blocking mode 01490 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains 01491 * the configuration information for the specified I2C. 01492 * @param pData Pointer to data buffer 01493 * @param Size Amount of data to be sent 01494 * @param Timeout Timeout duration 01495 * @retval HAL status 01496 */ 01497 HAL_StatusTypeDef HAL_I2C_Slave_Receive(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, 01498 uint32_t Timeout) 01499 { 01500 uint32_t tickstart; 01501 01502 if (hi2c->State == HAL_I2C_STATE_READY) 01503 { 01504 if ((pData == NULL) || (Size == 0U)) 01505 { 01506 hi2c->ErrorCode = HAL_I2C_ERROR_INVALID_PARAM; 01507 return HAL_ERROR; 01508 } 01509 /* Process Locked */ 01510 __HAL_LOCK(hi2c); 01511 01512 /* Init tickstart for timeout management*/ 01513 tickstart = HAL_GetTick(); 01514 01515 hi2c->State = HAL_I2C_STATE_BUSY_RX; 01516 hi2c->Mode = HAL_I2C_MODE_SLAVE; 01517 hi2c->ErrorCode = HAL_I2C_ERROR_NONE; 01518 01519 /* Prepare transfer parameters */ 01520 hi2c->pBuffPtr = pData; 01521 hi2c->XferCount = Size; 01522 hi2c->XferSize = hi2c->XferCount; 01523 hi2c->XferISR = NULL; 01524 01525 /* Enable Address Acknowledge */ 01526 hi2c->Instance->CR2 &= ~I2C_CR2_NACK; 01527 01528 /* Wait until ADDR flag is set */ 01529 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, Timeout, tickstart) != HAL_OK) 01530 { 01531 /* Disable Address Acknowledge */ 01532 hi2c->Instance->CR2 |= I2C_CR2_NACK; 01533 return HAL_ERROR; 01534 } 01535 01536 /* Clear ADDR flag */ 01537 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ADDR); 01538 01539 /* Wait until DIR flag is reset Receiver mode */ 01540 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_DIR, SET, Timeout, tickstart) != HAL_OK) 01541 { 01542 /* Disable Address Acknowledge */ 01543 hi2c->Instance->CR2 |= I2C_CR2_NACK; 01544 return HAL_ERROR; 01545 } 01546 01547 while (hi2c->XferCount > 0U) 01548 { 01549 /* Wait until RXNE flag is set */ 01550 if (I2C_WaitOnRXNEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK) 01551 { 01552 /* Disable Address Acknowledge */ 01553 hi2c->Instance->CR2 |= I2C_CR2_NACK; 01554 01555 /* Store Last receive data if any */ 01556 if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET) 01557 { 01558 /* Read data from RXDR */ 01559 *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->RXDR; 01560 01561 /* Increment Buffer pointer */ 01562 hi2c->pBuffPtr++; 01563 01564 hi2c->XferCount--; 01565 hi2c->XferSize--; 01566 } 01567 01568 return HAL_ERROR; 01569 } 01570 01571 /* Read data from RXDR */ 01572 *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->RXDR; 01573 01574 /* Increment Buffer pointer */ 01575 hi2c->pBuffPtr++; 01576 01577 hi2c->XferCount--; 01578 hi2c->XferSize--; 01579 } 01580 01581 /* Wait until STOP flag is set */ 01582 if (I2C_WaitOnSTOPFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK) 01583 { 01584 /* Disable Address Acknowledge */ 01585 hi2c->Instance->CR2 |= I2C_CR2_NACK; 01586 return HAL_ERROR; 01587 } 01588 01589 /* Clear STOP flag */ 01590 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF); 01591 01592 /* Wait until BUSY flag is reset */ 01593 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, Timeout, tickstart) != HAL_OK) 01594 { 01595 /* Disable Address Acknowledge */ 01596 hi2c->Instance->CR2 |= I2C_CR2_NACK; 01597 return HAL_ERROR; 01598 } 01599 01600 /* Disable Address Acknowledge */ 01601 hi2c->Instance->CR2 |= I2C_CR2_NACK; 01602 01603 hi2c->State = HAL_I2C_STATE_READY; 01604 hi2c->Mode = HAL_I2C_MODE_NONE; 01605 01606 /* Process Unlocked */ 01607 __HAL_UNLOCK(hi2c); 01608 01609 return HAL_OK; 01610 } 01611 else 01612 { 01613 return HAL_BUSY; 01614 } 01615 } 01616 01617 /** 01618 * @brief Transmit in master mode an amount of data in non-blocking mode with Interrupt 01619 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains 01620 * the configuration information for the specified I2C. 01621 * @param DevAddress Target device address: The device 7 bits address value 01622 * in datasheet must be shifted to the left before calling the interface 01623 * @param pData Pointer to data buffer 01624 * @param Size Amount of data to be sent 01625 * @retval HAL status 01626 */ 01627 HAL_StatusTypeDef HAL_I2C_Master_Transmit_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, 01628 uint16_t Size) 01629 { 01630 uint32_t xfermode; 01631 01632 if (hi2c->State == HAL_I2C_STATE_READY) 01633 { 01634 if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET) 01635 { 01636 return HAL_BUSY; 01637 } 01638 01639 /* Process Locked */ 01640 __HAL_LOCK(hi2c); 01641 01642 hi2c->State = HAL_I2C_STATE_BUSY_TX; 01643 hi2c->Mode = HAL_I2C_MODE_MASTER; 01644 hi2c->ErrorCode = HAL_I2C_ERROR_NONE; 01645 01646 /* Prepare transfer parameters */ 01647 hi2c->pBuffPtr = pData; 01648 hi2c->XferCount = Size; 01649 hi2c->XferOptions = I2C_NO_OPTION_FRAME; 01650 hi2c->XferISR = I2C_Master_ISR_IT; 01651 01652 if (hi2c->XferCount > MAX_NBYTE_SIZE) 01653 { 01654 hi2c->XferSize = MAX_NBYTE_SIZE; 01655 xfermode = I2C_RELOAD_MODE; 01656 } 01657 else 01658 { 01659 hi2c->XferSize = hi2c->XferCount; 01660 xfermode = I2C_AUTOEND_MODE; 01661 } 01662 01663 /* Send Slave Address */ 01664 /* Set NBYTES to write and reload if hi2c->XferCount > MAX_NBYTE_SIZE */ 01665 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, xfermode, I2C_GENERATE_START_WRITE); 01666 01667 /* Process Unlocked */ 01668 __HAL_UNLOCK(hi2c); 01669 01670 /* Note : The I2C interrupts must be enabled after unlocking current process 01671 to avoid the risk of I2C interrupt handle execution before current 01672 process unlock */ 01673 01674 /* Enable ERR, TC, STOP, NACK, TXI interrupt */ 01675 /* possible to enable all of these */ 01676 /* I2C_IT_ERRI | I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI | 01677 I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */ 01678 I2C_Enable_IRQ(hi2c, I2C_XFER_TX_IT); 01679 01680 return HAL_OK; 01681 } 01682 else 01683 { 01684 return HAL_BUSY; 01685 } 01686 } 01687 01688 /** 01689 * @brief Receive in master mode an amount of data in non-blocking mode with Interrupt 01690 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains 01691 * the configuration information for the specified I2C. 01692 * @param DevAddress Target device address: The device 7 bits address value 01693 * in datasheet must be shifted to the left before calling the interface 01694 * @param pData Pointer to data buffer 01695 * @param Size Amount of data to be sent 01696 * @retval HAL status 01697 */ 01698 HAL_StatusTypeDef HAL_I2C_Master_Receive_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, 01699 uint16_t Size) 01700 { 01701 uint32_t xfermode; 01702 01703 if (hi2c->State == HAL_I2C_STATE_READY) 01704 { 01705 if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET) 01706 { 01707 return HAL_BUSY; 01708 } 01709 01710 /* Process Locked */ 01711 __HAL_LOCK(hi2c); 01712 01713 hi2c->State = HAL_I2C_STATE_BUSY_RX; 01714 hi2c->Mode = HAL_I2C_MODE_MASTER; 01715 hi2c->ErrorCode = HAL_I2C_ERROR_NONE; 01716 01717 /* Prepare transfer parameters */ 01718 hi2c->pBuffPtr = pData; 01719 hi2c->XferCount = Size; 01720 hi2c->XferOptions = I2C_NO_OPTION_FRAME; 01721 hi2c->XferISR = I2C_Master_ISR_IT; 01722 01723 if (hi2c->XferCount > MAX_NBYTE_SIZE) 01724 { 01725 hi2c->XferSize = MAX_NBYTE_SIZE; 01726 xfermode = I2C_RELOAD_MODE; 01727 } 01728 else 01729 { 01730 hi2c->XferSize = hi2c->XferCount; 01731 xfermode = I2C_AUTOEND_MODE; 01732 } 01733 01734 /* Send Slave Address */ 01735 /* Set NBYTES to write and reload if hi2c->XferCount > MAX_NBYTE_SIZE */ 01736 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, xfermode, I2C_GENERATE_START_READ); 01737 01738 /* Process Unlocked */ 01739 __HAL_UNLOCK(hi2c); 01740 01741 /* Note : The I2C interrupts must be enabled after unlocking current process 01742 to avoid the risk of I2C interrupt handle execution before current 01743 process unlock */ 01744 01745 /* Enable ERR, TC, STOP, NACK, RXI interrupt */ 01746 /* possible to enable all of these */ 01747 /* I2C_IT_ERRI | I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI | 01748 I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */ 01749 I2C_Enable_IRQ(hi2c, I2C_XFER_RX_IT); 01750 01751 return HAL_OK; 01752 } 01753 else 01754 { 01755 return HAL_BUSY; 01756 } 01757 } 01758 01759 /** 01760 * @brief Transmit in slave mode an amount of data in non-blocking mode with Interrupt 01761 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains 01762 * the configuration information for the specified I2C. 01763 * @param pData Pointer to data buffer 01764 * @param Size Amount of data to be sent 01765 * @retval HAL status 01766 */ 01767 HAL_StatusTypeDef HAL_I2C_Slave_Transmit_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size) 01768 { 01769 if (hi2c->State == HAL_I2C_STATE_READY) 01770 { 01771 /* Process Locked */ 01772 __HAL_LOCK(hi2c); 01773 01774 hi2c->State = HAL_I2C_STATE_BUSY_TX; 01775 hi2c->Mode = HAL_I2C_MODE_SLAVE; 01776 hi2c->ErrorCode = HAL_I2C_ERROR_NONE; 01777 01778 /* Enable Address Acknowledge */ 01779 hi2c->Instance->CR2 &= ~I2C_CR2_NACK; 01780 01781 /* Prepare transfer parameters */ 01782 hi2c->pBuffPtr = pData; 01783 hi2c->XferCount = Size; 01784 hi2c->XferSize = hi2c->XferCount; 01785 hi2c->XferOptions = I2C_NO_OPTION_FRAME; 01786 hi2c->XferISR = I2C_Slave_ISR_IT; 01787 01788 /* Process Unlocked */ 01789 __HAL_UNLOCK(hi2c); 01790 01791 /* Note : The I2C interrupts must be enabled after unlocking current process 01792 to avoid the risk of I2C interrupt handle execution before current 01793 process unlock */ 01794 01795 /* Enable ERR, TC, STOP, NACK, TXI interrupt */ 01796 /* possible to enable all of these */ 01797 /* I2C_IT_ERRI | I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI | 01798 I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */ 01799 I2C_Enable_IRQ(hi2c, I2C_XFER_TX_IT | I2C_XFER_LISTEN_IT); 01800 01801 return HAL_OK; 01802 } 01803 else 01804 { 01805 return HAL_BUSY; 01806 } 01807 } 01808 01809 /** 01810 * @brief Receive in slave mode an amount of data in non-blocking mode with Interrupt 01811 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains 01812 * the configuration information for the specified I2C. 01813 * @param pData Pointer to data buffer 01814 * @param Size Amount of data to be sent 01815 * @retval HAL status 01816 */ 01817 HAL_StatusTypeDef HAL_I2C_Slave_Receive_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size) 01818 { 01819 if (hi2c->State == HAL_I2C_STATE_READY) 01820 { 01821 /* Process Locked */ 01822 __HAL_LOCK(hi2c); 01823 01824 hi2c->State = HAL_I2C_STATE_BUSY_RX; 01825 hi2c->Mode = HAL_I2C_MODE_SLAVE; 01826 hi2c->ErrorCode = HAL_I2C_ERROR_NONE; 01827 01828 /* Enable Address Acknowledge */ 01829 hi2c->Instance->CR2 &= ~I2C_CR2_NACK; 01830 01831 /* Prepare transfer parameters */ 01832 hi2c->pBuffPtr = pData; 01833 hi2c->XferCount = Size; 01834 hi2c->XferSize = hi2c->XferCount; 01835 hi2c->XferOptions = I2C_NO_OPTION_FRAME; 01836 hi2c->XferISR = I2C_Slave_ISR_IT; 01837 01838 /* Process Unlocked */ 01839 __HAL_UNLOCK(hi2c); 01840 01841 /* Note : The I2C interrupts must be enabled after unlocking current process 01842 to avoid the risk of I2C interrupt handle execution before current 01843 process unlock */ 01844 01845 /* Enable ERR, TC, STOP, NACK, RXI interrupt */ 01846 /* possible to enable all of these */ 01847 /* I2C_IT_ERRI | I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI | 01848 I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */ 01849 I2C_Enable_IRQ(hi2c, I2C_XFER_RX_IT | I2C_XFER_LISTEN_IT); 01850 01851 return HAL_OK; 01852 } 01853 else 01854 { 01855 return HAL_BUSY; 01856 } 01857 } 01858 01859 /** 01860 * @brief Transmit in master mode an amount of data in non-blocking mode with DMA 01861 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains 01862 * the configuration information for the specified I2C. 01863 * @param DevAddress Target device address: The device 7 bits address value 01864 * in datasheet must be shifted to the left before calling the interface 01865 * @param pData Pointer to data buffer 01866 * @param Size Amount of data to be sent 01867 * @retval HAL status 01868 */ 01869 HAL_StatusTypeDef HAL_I2C_Master_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, 01870 uint16_t Size) 01871 { 01872 uint32_t xfermode; 01873 HAL_StatusTypeDef dmaxferstatus; 01874 01875 if (hi2c->State == HAL_I2C_STATE_READY) 01876 { 01877 if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET) 01878 { 01879 return HAL_BUSY; 01880 } 01881 01882 /* Process Locked */ 01883 __HAL_LOCK(hi2c); 01884 01885 hi2c->State = HAL_I2C_STATE_BUSY_TX; 01886 hi2c->Mode = HAL_I2C_MODE_MASTER; 01887 hi2c->ErrorCode = HAL_I2C_ERROR_NONE; 01888 01889 /* Prepare transfer parameters */ 01890 hi2c->pBuffPtr = pData; 01891 hi2c->XferCount = Size; 01892 hi2c->XferOptions = I2C_NO_OPTION_FRAME; 01893 hi2c->XferISR = I2C_Master_ISR_DMA; 01894 01895 if (hi2c->XferCount > MAX_NBYTE_SIZE) 01896 { 01897 hi2c->XferSize = MAX_NBYTE_SIZE; 01898 xfermode = I2C_RELOAD_MODE; 01899 } 01900 else 01901 { 01902 hi2c->XferSize = hi2c->XferCount; 01903 xfermode = I2C_AUTOEND_MODE; 01904 } 01905 01906 if (hi2c->XferSize > 0U) 01907 { 01908 if (hi2c->hdmatx != NULL) 01909 { 01910 /* Set the I2C DMA transfer complete callback */ 01911 hi2c->hdmatx->XferCpltCallback = I2C_DMAMasterTransmitCplt; 01912 01913 /* Set the DMA error callback */ 01914 hi2c->hdmatx->XferErrorCallback = I2C_DMAError; 01915 01916 /* Set the unused DMA callbacks to NULL */ 01917 hi2c->hdmatx->XferHalfCpltCallback = NULL; 01918 hi2c->hdmatx->XferAbortCallback = NULL; 01919 01920 /* Enable the DMA stream or channel depends on Instance */ 01921 dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)pData, (uint32_t)&hi2c->Instance->TXDR, 01922 hi2c->XferSize); 01923 } 01924 else 01925 { 01926 /* Update I2C state */ 01927 hi2c->State = HAL_I2C_STATE_READY; 01928 hi2c->Mode = HAL_I2C_MODE_NONE; 01929 01930 /* Update I2C error code */ 01931 hi2c->ErrorCode |= HAL_I2C_ERROR_DMA_PARAM; 01932 01933 /* Process Unlocked */ 01934 __HAL_UNLOCK(hi2c); 01935 01936 return HAL_ERROR; 01937 } 01938 01939 if (dmaxferstatus == HAL_OK) 01940 { 01941 /* Send Slave Address */ 01942 /* Set NBYTES to write and reload if hi2c->XferCount > MAX_NBYTE_SIZE and generate RESTART */ 01943 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, xfermode, I2C_GENERATE_START_WRITE); 01944 01945 /* Update XferCount value */ 01946 hi2c->XferCount -= hi2c->XferSize; 01947 01948 /* Process Unlocked */ 01949 __HAL_UNLOCK(hi2c); 01950 01951 /* Note : The I2C interrupts must be enabled after unlocking current process 01952 to avoid the risk of I2C interrupt handle execution before current 01953 process unlock */ 01954 /* Enable ERR and NACK interrupts */ 01955 I2C_Enable_IRQ(hi2c, I2C_XFER_ERROR_IT); 01956 01957 /* Enable DMA Request */ 01958 hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN; 01959 } 01960 else 01961 { 01962 /* Update I2C state */ 01963 hi2c->State = HAL_I2C_STATE_READY; 01964 hi2c->Mode = HAL_I2C_MODE_NONE; 01965 01966 /* Update I2C error code */ 01967 hi2c->ErrorCode |= HAL_I2C_ERROR_DMA; 01968 01969 /* Process Unlocked */ 01970 __HAL_UNLOCK(hi2c); 01971 01972 return HAL_ERROR; 01973 } 01974 } 01975 else 01976 { 01977 /* Update Transfer ISR function pointer */ 01978 hi2c->XferISR = I2C_Master_ISR_IT; 01979 01980 /* Send Slave Address */ 01981 /* Set NBYTES to write and generate START condition */ 01982 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_AUTOEND_MODE, 01983 I2C_GENERATE_START_WRITE); 01984 01985 /* Process Unlocked */ 01986 __HAL_UNLOCK(hi2c); 01987 01988 /* Note : The I2C interrupts must be enabled after unlocking current process 01989 to avoid the risk of I2C interrupt handle execution before current 01990 process unlock */ 01991 /* Enable ERR, TC, STOP, NACK, TXI interrupt */ 01992 /* possible to enable all of these */ 01993 /* I2C_IT_ERRI | I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI | 01994 I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */ 01995 I2C_Enable_IRQ(hi2c, I2C_XFER_TX_IT); 01996 } 01997 01998 return HAL_OK; 01999 } 02000 else 02001 { 02002 return HAL_BUSY; 02003 } 02004 } 02005 02006 /** 02007 * @brief Receive in master mode an amount of data in non-blocking mode with DMA 02008 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains 02009 * the configuration information for the specified I2C. 02010 * @param DevAddress Target device address: The device 7 bits address value 02011 * in datasheet must be shifted to the left before calling the interface 02012 * @param pData Pointer to data buffer 02013 * @param Size Amount of data to be sent 02014 * @retval HAL status 02015 */ 02016 HAL_StatusTypeDef HAL_I2C_Master_Receive_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, 02017 uint16_t Size) 02018 { 02019 uint32_t xfermode; 02020 HAL_StatusTypeDef dmaxferstatus; 02021 02022 if (hi2c->State == HAL_I2C_STATE_READY) 02023 { 02024 if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET) 02025 { 02026 return HAL_BUSY; 02027 } 02028 02029 /* Process Locked */ 02030 __HAL_LOCK(hi2c); 02031 02032 hi2c->State = HAL_I2C_STATE_BUSY_RX; 02033 hi2c->Mode = HAL_I2C_MODE_MASTER; 02034 hi2c->ErrorCode = HAL_I2C_ERROR_NONE; 02035 02036 /* Prepare transfer parameters */ 02037 hi2c->pBuffPtr = pData; 02038 hi2c->XferCount = Size; 02039 hi2c->XferOptions = I2C_NO_OPTION_FRAME; 02040 hi2c->XferISR = I2C_Master_ISR_DMA; 02041 02042 if (hi2c->XferCount > MAX_NBYTE_SIZE) 02043 { 02044 hi2c->XferSize = MAX_NBYTE_SIZE; 02045 xfermode = I2C_RELOAD_MODE; 02046 } 02047 else 02048 { 02049 hi2c->XferSize = hi2c->XferCount; 02050 xfermode = I2C_AUTOEND_MODE; 02051 } 02052 02053 if (hi2c->XferSize > 0U) 02054 { 02055 if (hi2c->hdmarx != NULL) 02056 { 02057 /* Set the I2C DMA transfer complete callback */ 02058 hi2c->hdmarx->XferCpltCallback = I2C_DMAMasterReceiveCplt; 02059 02060 /* Set the DMA error callback */ 02061 hi2c->hdmarx->XferErrorCallback = I2C_DMAError; 02062 02063 /* Set the unused DMA callbacks to NULL */ 02064 hi2c->hdmarx->XferHalfCpltCallback = NULL; 02065 hi2c->hdmarx->XferAbortCallback = NULL; 02066 02067 /* Enable the DMA stream or channel depends on Instance */ 02068 dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->RXDR, (uint32_t)pData, 02069 hi2c->XferSize); 02070 } 02071 else 02072 { 02073 /* Update I2C state */ 02074 hi2c->State = HAL_I2C_STATE_READY; 02075 hi2c->Mode = HAL_I2C_MODE_NONE; 02076 02077 /* Update I2C error code */ 02078 hi2c->ErrorCode |= HAL_I2C_ERROR_DMA_PARAM; 02079 02080 /* Process Unlocked */ 02081 __HAL_UNLOCK(hi2c); 02082 02083 return HAL_ERROR; 02084 } 02085 02086 if (dmaxferstatus == HAL_OK) 02087 { 02088 /* Send Slave Address */ 02089 /* Set NBYTES to read and reload if hi2c->XferCount > MAX_NBYTE_SIZE and generate RESTART */ 02090 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, xfermode, I2C_GENERATE_START_READ); 02091 02092 /* Update XferCount value */ 02093 hi2c->XferCount -= hi2c->XferSize; 02094 02095 /* Process Unlocked */ 02096 __HAL_UNLOCK(hi2c); 02097 02098 /* Note : The I2C interrupts must be enabled after unlocking current process 02099 to avoid the risk of I2C interrupt handle execution before current 02100 process unlock */ 02101 /* Enable ERR and NACK interrupts */ 02102 I2C_Enable_IRQ(hi2c, I2C_XFER_ERROR_IT); 02103 02104 /* Enable DMA Request */ 02105 hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN; 02106 } 02107 else 02108 { 02109 /* Update I2C state */ 02110 hi2c->State = HAL_I2C_STATE_READY; 02111 hi2c->Mode = HAL_I2C_MODE_NONE; 02112 02113 /* Update I2C error code */ 02114 hi2c->ErrorCode |= HAL_I2C_ERROR_DMA; 02115 02116 /* Process Unlocked */ 02117 __HAL_UNLOCK(hi2c); 02118 02119 return HAL_ERROR; 02120 } 02121 } 02122 else 02123 { 02124 /* Update Transfer ISR function pointer */ 02125 hi2c->XferISR = I2C_Master_ISR_IT; 02126 02127 /* Send Slave Address */ 02128 /* Set NBYTES to read and generate START condition */ 02129 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_AUTOEND_MODE, 02130 I2C_GENERATE_START_READ); 02131 02132 /* Process Unlocked */ 02133 __HAL_UNLOCK(hi2c); 02134 02135 /* Note : The I2C interrupts must be enabled after unlocking current process 02136 to avoid the risk of I2C interrupt handle execution before current 02137 process unlock */ 02138 /* Enable ERR, TC, STOP, NACK, TXI interrupt */ 02139 /* possible to enable all of these */ 02140 /* I2C_IT_ERRI | I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI | 02141 I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */ 02142 I2C_Enable_IRQ(hi2c, I2C_XFER_TX_IT); 02143 } 02144 02145 return HAL_OK; 02146 } 02147 else 02148 { 02149 return HAL_BUSY; 02150 } 02151 } 02152 02153 /** 02154 * @brief Transmit in slave mode an amount of data in non-blocking mode with DMA 02155 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains 02156 * the configuration information for the specified I2C. 02157 * @param pData Pointer to data buffer 02158 * @param Size Amount of data to be sent 02159 * @retval HAL status 02160 */ 02161 HAL_StatusTypeDef HAL_I2C_Slave_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size) 02162 { 02163 HAL_StatusTypeDef dmaxferstatus; 02164 02165 if (hi2c->State == HAL_I2C_STATE_READY) 02166 { 02167 if ((pData == NULL) || (Size == 0U)) 02168 { 02169 hi2c->ErrorCode = HAL_I2C_ERROR_INVALID_PARAM; 02170 return HAL_ERROR; 02171 } 02172 /* Process Locked */ 02173 __HAL_LOCK(hi2c); 02174 02175 hi2c->State = HAL_I2C_STATE_BUSY_TX; 02176 hi2c->Mode = HAL_I2C_MODE_SLAVE; 02177 hi2c->ErrorCode = HAL_I2C_ERROR_NONE; 02178 02179 /* Prepare transfer parameters */ 02180 hi2c->pBuffPtr = pData; 02181 hi2c->XferCount = Size; 02182 hi2c->XferSize = hi2c->XferCount; 02183 hi2c->XferOptions = I2C_NO_OPTION_FRAME; 02184 hi2c->XferISR = I2C_Slave_ISR_DMA; 02185 02186 if (hi2c->hdmatx != NULL) 02187 { 02188 /* Set the I2C DMA transfer complete callback */ 02189 hi2c->hdmatx->XferCpltCallback = I2C_DMASlaveTransmitCplt; 02190 02191 /* Set the DMA error callback */ 02192 hi2c->hdmatx->XferErrorCallback = I2C_DMAError; 02193 02194 /* Set the unused DMA callbacks to NULL */ 02195 hi2c->hdmatx->XferHalfCpltCallback = NULL; 02196 hi2c->hdmatx->XferAbortCallback = NULL; 02197 02198 /* Enable the DMA stream or channel depends on Instance */ 02199 dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)pData, (uint32_t)&hi2c->Instance->TXDR, 02200 hi2c->XferSize); 02201 } 02202 else 02203 { 02204 /* Update I2C state */ 02205 hi2c->State = HAL_I2C_STATE_LISTEN; 02206 hi2c->Mode = HAL_I2C_MODE_NONE; 02207 02208 /* Update I2C error code */ 02209 hi2c->ErrorCode |= HAL_I2C_ERROR_DMA_PARAM; 02210 02211 /* Process Unlocked */ 02212 __HAL_UNLOCK(hi2c); 02213 02214 return HAL_ERROR; 02215 } 02216 02217 if (dmaxferstatus == HAL_OK) 02218 { 02219 /* Enable Address Acknowledge */ 02220 hi2c->Instance->CR2 &= ~I2C_CR2_NACK; 02221 02222 /* Process Unlocked */ 02223 __HAL_UNLOCK(hi2c); 02224 02225 /* Note : The I2C interrupts must be enabled after unlocking current process 02226 to avoid the risk of I2C interrupt handle execution before current 02227 process unlock */ 02228 /* Enable ERR, STOP, NACK, ADDR interrupts */ 02229 I2C_Enable_IRQ(hi2c, I2C_XFER_LISTEN_IT); 02230 02231 /* Enable DMA Request */ 02232 hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN; 02233 } 02234 else 02235 { 02236 /* Update I2C state */ 02237 hi2c->State = HAL_I2C_STATE_LISTEN; 02238 hi2c->Mode = HAL_I2C_MODE_NONE; 02239 02240 /* Update I2C error code */ 02241 hi2c->ErrorCode |= HAL_I2C_ERROR_DMA; 02242 02243 /* Process Unlocked */ 02244 __HAL_UNLOCK(hi2c); 02245 02246 return HAL_ERROR; 02247 } 02248 02249 return HAL_OK; 02250 } 02251 else 02252 { 02253 return HAL_BUSY; 02254 } 02255 } 02256 02257 /** 02258 * @brief Receive in slave mode an amount of data in non-blocking mode with DMA 02259 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains 02260 * the configuration information for the specified I2C. 02261 * @param pData Pointer to data buffer 02262 * @param Size Amount of data to be sent 02263 * @retval HAL status 02264 */ 02265 HAL_StatusTypeDef HAL_I2C_Slave_Receive_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size) 02266 { 02267 HAL_StatusTypeDef dmaxferstatus; 02268 02269 if (hi2c->State == HAL_I2C_STATE_READY) 02270 { 02271 if ((pData == NULL) || (Size == 0U)) 02272 { 02273 hi2c->ErrorCode = HAL_I2C_ERROR_INVALID_PARAM; 02274 return HAL_ERROR; 02275 } 02276 /* Process Locked */ 02277 __HAL_LOCK(hi2c); 02278 02279 hi2c->State = HAL_I2C_STATE_BUSY_RX; 02280 hi2c->Mode = HAL_I2C_MODE_SLAVE; 02281 hi2c->ErrorCode = HAL_I2C_ERROR_NONE; 02282 02283 /* Prepare transfer parameters */ 02284 hi2c->pBuffPtr = pData; 02285 hi2c->XferCount = Size; 02286 hi2c->XferSize = hi2c->XferCount; 02287 hi2c->XferOptions = I2C_NO_OPTION_FRAME; 02288 hi2c->XferISR = I2C_Slave_ISR_DMA; 02289 02290 if (hi2c->hdmarx != NULL) 02291 { 02292 /* Set the I2C DMA transfer complete callback */ 02293 hi2c->hdmarx->XferCpltCallback = I2C_DMASlaveReceiveCplt; 02294 02295 /* Set the DMA error callback */ 02296 hi2c->hdmarx->XferErrorCallback = I2C_DMAError; 02297 02298 /* Set the unused DMA callbacks to NULL */ 02299 hi2c->hdmarx->XferHalfCpltCallback = NULL; 02300 hi2c->hdmarx->XferAbortCallback = NULL; 02301 02302 /* Enable the DMA stream or channel depends on Instance */ 02303 dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->RXDR, (uint32_t)pData, 02304 hi2c->XferSize); 02305 } 02306 else 02307 { 02308 /* Update I2C state */ 02309 hi2c->State = HAL_I2C_STATE_LISTEN; 02310 hi2c->Mode = HAL_I2C_MODE_NONE; 02311 02312 /* Update I2C error code */ 02313 hi2c->ErrorCode |= HAL_I2C_ERROR_DMA_PARAM; 02314 02315 /* Process Unlocked */ 02316 __HAL_UNLOCK(hi2c); 02317 02318 return HAL_ERROR; 02319 } 02320 02321 if (dmaxferstatus == HAL_OK) 02322 { 02323 /* Enable Address Acknowledge */ 02324 hi2c->Instance->CR2 &= ~I2C_CR2_NACK; 02325 02326 /* Process Unlocked */ 02327 __HAL_UNLOCK(hi2c); 02328 02329 /* Note : The I2C interrupts must be enabled after unlocking current process 02330 to avoid the risk of I2C interrupt handle execution before current 02331 process unlock */ 02332 /* Enable ERR, STOP, NACK, ADDR interrupts */ 02333 I2C_Enable_IRQ(hi2c, I2C_XFER_LISTEN_IT); 02334 02335 /* Enable DMA Request */ 02336 hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN; 02337 } 02338 else 02339 { 02340 /* Update I2C state */ 02341 hi2c->State = HAL_I2C_STATE_LISTEN; 02342 hi2c->Mode = HAL_I2C_MODE_NONE; 02343 02344 /* Update I2C error code */ 02345 hi2c->ErrorCode |= HAL_I2C_ERROR_DMA; 02346 02347 /* Process Unlocked */ 02348 __HAL_UNLOCK(hi2c); 02349 02350 return HAL_ERROR; 02351 } 02352 02353 return HAL_OK; 02354 } 02355 else 02356 { 02357 return HAL_BUSY; 02358 } 02359 } 02360 /** 02361 * @brief Write an amount of data in blocking mode to a specific memory address 02362 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains 02363 * the configuration information for the specified I2C. 02364 * @param DevAddress Target device address: The device 7 bits address value 02365 * in datasheet must be shifted to the left before calling the interface 02366 * @param MemAddress Internal memory address 02367 * @param MemAddSize Size of internal memory address 02368 * @param pData Pointer to data buffer 02369 * @param Size Amount of data to be sent 02370 * @param Timeout Timeout duration 02371 * @retval HAL status 02372 */ 02373 HAL_StatusTypeDef HAL_I2C_Mem_Write(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, 02374 uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout) 02375 { 02376 uint32_t tickstart; 02377 02378 /* Check the parameters */ 02379 assert_param(IS_I2C_MEMADD_SIZE(MemAddSize)); 02380 02381 if (hi2c->State == HAL_I2C_STATE_READY) 02382 { 02383 if ((pData == NULL) || (Size == 0U)) 02384 { 02385 hi2c->ErrorCode = HAL_I2C_ERROR_INVALID_PARAM; 02386 return HAL_ERROR; 02387 } 02388 02389 /* Process Locked */ 02390 __HAL_LOCK(hi2c); 02391 02392 /* Init tickstart for timeout management*/ 02393 tickstart = HAL_GetTick(); 02394 02395 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY, tickstart) != HAL_OK) 02396 { 02397 return HAL_ERROR; 02398 } 02399 02400 hi2c->State = HAL_I2C_STATE_BUSY_TX; 02401 hi2c->Mode = HAL_I2C_MODE_MEM; 02402 hi2c->ErrorCode = HAL_I2C_ERROR_NONE; 02403 02404 /* Prepare transfer parameters */ 02405 hi2c->pBuffPtr = pData; 02406 hi2c->XferCount = Size; 02407 hi2c->XferISR = NULL; 02408 02409 /* Send Slave Address and Memory Address */ 02410 if (I2C_RequestMemoryWrite(hi2c, DevAddress, MemAddress, MemAddSize, Timeout, tickstart) != HAL_OK) 02411 { 02412 /* Process Unlocked */ 02413 __HAL_UNLOCK(hi2c); 02414 return HAL_ERROR; 02415 } 02416 02417 /* Set NBYTES to write and reload if hi2c->XferCount > MAX_NBYTE_SIZE */ 02418 if (hi2c->XferCount > MAX_NBYTE_SIZE) 02419 { 02420 hi2c->XferSize = MAX_NBYTE_SIZE; 02421 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_RELOAD_MODE, I2C_NO_STARTSTOP); 02422 } 02423 else 02424 { 02425 hi2c->XferSize = hi2c->XferCount; 02426 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP); 02427 } 02428 02429 do 02430 { 02431 /* Wait until TXIS flag is set */ 02432 if (I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK) 02433 { 02434 return HAL_ERROR; 02435 } 02436 02437 /* Write data to TXDR */ 02438 hi2c->Instance->TXDR = *hi2c->pBuffPtr; 02439 02440 /* Increment Buffer pointer */ 02441 hi2c->pBuffPtr++; 02442 02443 hi2c->XferCount--; 02444 hi2c->XferSize--; 02445 02446 if ((hi2c->XferCount != 0U) && (hi2c->XferSize == 0U)) 02447 { 02448 /* Wait until TCR flag is set */ 02449 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, Timeout, tickstart) != HAL_OK) 02450 { 02451 return HAL_ERROR; 02452 } 02453 02454 if (hi2c->XferCount > MAX_NBYTE_SIZE) 02455 { 02456 hi2c->XferSize = MAX_NBYTE_SIZE; 02457 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_RELOAD_MODE, 02458 I2C_NO_STARTSTOP); 02459 } 02460 else 02461 { 02462 hi2c->XferSize = hi2c->XferCount; 02463 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_AUTOEND_MODE, 02464 I2C_NO_STARTSTOP); 02465 } 02466 } 02467 02468 } while (hi2c->XferCount > 0U); 02469 02470 /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */ 02471 /* Wait until STOPF flag is reset */ 02472 if (I2C_WaitOnSTOPFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK) 02473 { 02474 return HAL_ERROR; 02475 } 02476 02477 /* Clear STOP Flag */ 02478 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF); 02479 02480 /* Clear Configuration Register 2 */ 02481 I2C_RESET_CR2(hi2c); 02482 02483 hi2c->State = HAL_I2C_STATE_READY; 02484 hi2c->Mode = HAL_I2C_MODE_NONE; 02485 02486 /* Process Unlocked */ 02487 __HAL_UNLOCK(hi2c); 02488 02489 return HAL_OK; 02490 } 02491 else 02492 { 02493 return HAL_BUSY; 02494 } 02495 } 02496 02497 /** 02498 * @brief Read an amount of data in blocking mode from a specific memory address 02499 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains 02500 * the configuration information for the specified I2C. 02501 * @param DevAddress Target device address: The device 7 bits address value 02502 * in datasheet must be shifted to the left before calling the interface 02503 * @param MemAddress Internal memory address 02504 * @param MemAddSize Size of internal memory address 02505 * @param pData Pointer to data buffer 02506 * @param Size Amount of data to be sent 02507 * @param Timeout Timeout duration 02508 * @retval HAL status 02509 */ 02510 HAL_StatusTypeDef HAL_I2C_Mem_Read(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, 02511 uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout) 02512 { 02513 uint32_t tickstart; 02514 02515 /* Check the parameters */ 02516 assert_param(IS_I2C_MEMADD_SIZE(MemAddSize)); 02517 02518 if (hi2c->State == HAL_I2C_STATE_READY) 02519 { 02520 if ((pData == NULL) || (Size == 0U)) 02521 { 02522 hi2c->ErrorCode = HAL_I2C_ERROR_INVALID_PARAM; 02523 return HAL_ERROR; 02524 } 02525 02526 /* Process Locked */ 02527 __HAL_LOCK(hi2c); 02528 02529 /* Init tickstart for timeout management*/ 02530 tickstart = HAL_GetTick(); 02531 02532 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY, tickstart) != HAL_OK) 02533 { 02534 return HAL_ERROR; 02535 } 02536 02537 hi2c->State = HAL_I2C_STATE_BUSY_RX; 02538 hi2c->Mode = HAL_I2C_MODE_MEM; 02539 hi2c->ErrorCode = HAL_I2C_ERROR_NONE; 02540 02541 /* Prepare transfer parameters */ 02542 hi2c->pBuffPtr = pData; 02543 hi2c->XferCount = Size; 02544 hi2c->XferISR = NULL; 02545 02546 /* Send Slave Address and Memory Address */ 02547 if (I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, Timeout, tickstart) != HAL_OK) 02548 { 02549 /* Process Unlocked */ 02550 __HAL_UNLOCK(hi2c); 02551 return HAL_ERROR; 02552 } 02553 02554 /* Send Slave Address */ 02555 /* Set NBYTES to write and reload if hi2c->XferCount > MAX_NBYTE_SIZE and generate RESTART */ 02556 if (hi2c->XferCount > MAX_NBYTE_SIZE) 02557 { 02558 hi2c->XferSize = MAX_NBYTE_SIZE; 02559 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_RELOAD_MODE, 02560 I2C_GENERATE_START_READ); 02561 } 02562 else 02563 { 02564 hi2c->XferSize = hi2c->XferCount; 02565 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_AUTOEND_MODE, 02566 I2C_GENERATE_START_READ); 02567 } 02568 02569 do 02570 { 02571 /* Wait until RXNE flag is set */ 02572 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_RXNE, RESET, Timeout, tickstart) != HAL_OK) 02573 { 02574 return HAL_ERROR; 02575 } 02576 02577 /* Read data from RXDR */ 02578 *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->RXDR; 02579 02580 /* Increment Buffer pointer */ 02581 hi2c->pBuffPtr++; 02582 02583 hi2c->XferSize--; 02584 hi2c->XferCount--; 02585 02586 if ((hi2c->XferCount != 0U) && (hi2c->XferSize == 0U)) 02587 { 02588 /* Wait until TCR flag is set */ 02589 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, Timeout, tickstart) != HAL_OK) 02590 { 02591 return HAL_ERROR; 02592 } 02593 02594 if (hi2c->XferCount > MAX_NBYTE_SIZE) 02595 { 02596 hi2c->XferSize = MAX_NBYTE_SIZE; 02597 I2C_TransferConfig(hi2c, DevAddress, (uint8_t) hi2c->XferSize, I2C_RELOAD_MODE, 02598 I2C_NO_STARTSTOP); 02599 } 02600 else 02601 { 02602 hi2c->XferSize = hi2c->XferCount; 02603 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_AUTOEND_MODE, 02604 I2C_NO_STARTSTOP); 02605 } 02606 } 02607 } while (hi2c->XferCount > 0U); 02608 02609 /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */ 02610 /* Wait until STOPF flag is reset */ 02611 if (I2C_WaitOnSTOPFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK) 02612 { 02613 return HAL_ERROR; 02614 } 02615 02616 /* Clear STOP Flag */ 02617 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF); 02618 02619 /* Clear Configuration Register 2 */ 02620 I2C_RESET_CR2(hi2c); 02621 02622 hi2c->State = HAL_I2C_STATE_READY; 02623 hi2c->Mode = HAL_I2C_MODE_NONE; 02624 02625 /* Process Unlocked */ 02626 __HAL_UNLOCK(hi2c); 02627 02628 return HAL_OK; 02629 } 02630 else 02631 { 02632 return HAL_BUSY; 02633 } 02634 } 02635 /** 02636 * @brief Write an amount of data in non-blocking mode with Interrupt to a specific memory address 02637 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains 02638 * the configuration information for the specified I2C. 02639 * @param DevAddress Target device address: The device 7 bits address value 02640 * in datasheet must be shifted to the left before calling the interface 02641 * @param MemAddress Internal memory address 02642 * @param MemAddSize Size of internal memory address 02643 * @param pData Pointer to data buffer 02644 * @param Size Amount of data to be sent 02645 * @retval HAL status 02646 */ 02647 HAL_StatusTypeDef HAL_I2C_Mem_Write_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, 02648 uint16_t MemAddSize, uint8_t *pData, uint16_t Size) 02649 { 02650 uint32_t tickstart; 02651 uint32_t xfermode; 02652 02653 /* Check the parameters */ 02654 assert_param(IS_I2C_MEMADD_SIZE(MemAddSize)); 02655 02656 if (hi2c->State == HAL_I2C_STATE_READY) 02657 { 02658 if ((pData == NULL) || (Size == 0U)) 02659 { 02660 hi2c->ErrorCode = HAL_I2C_ERROR_INVALID_PARAM; 02661 return HAL_ERROR; 02662 } 02663 02664 if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET) 02665 { 02666 return HAL_BUSY; 02667 } 02668 02669 /* Process Locked */ 02670 __HAL_LOCK(hi2c); 02671 02672 /* Init tickstart for timeout management*/ 02673 tickstart = HAL_GetTick(); 02674 02675 hi2c->State = HAL_I2C_STATE_BUSY_TX; 02676 hi2c->Mode = HAL_I2C_MODE_MEM; 02677 hi2c->ErrorCode = HAL_I2C_ERROR_NONE; 02678 02679 /* Prepare transfer parameters */ 02680 hi2c->pBuffPtr = pData; 02681 hi2c->XferCount = Size; 02682 hi2c->XferOptions = I2C_NO_OPTION_FRAME; 02683 hi2c->XferISR = I2C_Master_ISR_IT; 02684 02685 if (hi2c->XferCount > MAX_NBYTE_SIZE) 02686 { 02687 hi2c->XferSize = MAX_NBYTE_SIZE; 02688 xfermode = I2C_RELOAD_MODE; 02689 } 02690 else 02691 { 02692 hi2c->XferSize = hi2c->XferCount; 02693 xfermode = I2C_AUTOEND_MODE; 02694 } 02695 02696 /* Send Slave Address and Memory Address */ 02697 if (I2C_RequestMemoryWrite(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG, tickstart) 02698 != HAL_OK) 02699 { 02700 /* Process Unlocked */ 02701 __HAL_UNLOCK(hi2c); 02702 return HAL_ERROR; 02703 } 02704 02705 /* Set NBYTES to write and reload if hi2c->XferCount > MAX_NBYTE_SIZE and generate RESTART */ 02706 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, xfermode, I2C_NO_STARTSTOP); 02707 02708 /* Process Unlocked */ 02709 __HAL_UNLOCK(hi2c); 02710 02711 /* Note : The I2C interrupts must be enabled after unlocking current process 02712 to avoid the risk of I2C interrupt handle execution before current 02713 process unlock */ 02714 02715 /* Enable ERR, TC, STOP, NACK, TXI interrupt */ 02716 /* possible to enable all of these */ 02717 /* I2C_IT_ERRI | I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI | 02718 I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */ 02719 I2C_Enable_IRQ(hi2c, I2C_XFER_TX_IT); 02720 02721 return HAL_OK; 02722 } 02723 else 02724 { 02725 return HAL_BUSY; 02726 } 02727 } 02728 02729 /** 02730 * @brief Read an amount of data in non-blocking mode with Interrupt from a specific memory address 02731 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains 02732 * the configuration information for the specified I2C. 02733 * @param DevAddress Target device address: The device 7 bits address value 02734 * in datasheet must be shifted to the left before calling the interface 02735 * @param MemAddress Internal memory address 02736 * @param MemAddSize Size of internal memory address 02737 * @param pData Pointer to data buffer 02738 * @param Size Amount of data to be sent 02739 * @retval HAL status 02740 */ 02741 HAL_StatusTypeDef HAL_I2C_Mem_Read_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, 02742 uint16_t MemAddSize, uint8_t *pData, uint16_t Size) 02743 { 02744 uint32_t tickstart; 02745 uint32_t xfermode; 02746 02747 /* Check the parameters */ 02748 assert_param(IS_I2C_MEMADD_SIZE(MemAddSize)); 02749 02750 if (hi2c->State == HAL_I2C_STATE_READY) 02751 { 02752 if ((pData == NULL) || (Size == 0U)) 02753 { 02754 hi2c->ErrorCode = HAL_I2C_ERROR_INVALID_PARAM; 02755 return HAL_ERROR; 02756 } 02757 02758 if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET) 02759 { 02760 return HAL_BUSY; 02761 } 02762 02763 /* Process Locked */ 02764 __HAL_LOCK(hi2c); 02765 02766 /* Init tickstart for timeout management*/ 02767 tickstart = HAL_GetTick(); 02768 02769 hi2c->State = HAL_I2C_STATE_BUSY_RX; 02770 hi2c->Mode = HAL_I2C_MODE_MEM; 02771 hi2c->ErrorCode = HAL_I2C_ERROR_NONE; 02772 02773 /* Prepare transfer parameters */ 02774 hi2c->pBuffPtr = pData; 02775 hi2c->XferCount = Size; 02776 hi2c->XferOptions = I2C_NO_OPTION_FRAME; 02777 hi2c->XferISR = I2C_Master_ISR_IT; 02778 02779 if (hi2c->XferCount > MAX_NBYTE_SIZE) 02780 { 02781 hi2c->XferSize = MAX_NBYTE_SIZE; 02782 xfermode = I2C_RELOAD_MODE; 02783 } 02784 else 02785 { 02786 hi2c->XferSize = hi2c->XferCount; 02787 xfermode = I2C_AUTOEND_MODE; 02788 } 02789 02790 /* Send Slave Address and Memory Address */ 02791 if (I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG, tickstart) != HAL_OK) 02792 { 02793 /* Process Unlocked */ 02794 __HAL_UNLOCK(hi2c); 02795 return HAL_ERROR; 02796 } 02797 02798 /* Set NBYTES to write and reload if hi2c->XferCount > MAX_NBYTE_SIZE and generate RESTART */ 02799 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, xfermode, I2C_GENERATE_START_READ); 02800 02801 /* Process Unlocked */ 02802 __HAL_UNLOCK(hi2c); 02803 02804 /* Note : The I2C interrupts must be enabled after unlocking current process 02805 to avoid the risk of I2C interrupt handle execution before current 02806 process unlock */ 02807 02808 /* Enable ERR, TC, STOP, NACK, RXI interrupt */ 02809 /* possible to enable all of these */ 02810 /* I2C_IT_ERRI | I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI | 02811 I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */ 02812 I2C_Enable_IRQ(hi2c, I2C_XFER_RX_IT); 02813 02814 return HAL_OK; 02815 } 02816 else 02817 { 02818 return HAL_BUSY; 02819 } 02820 } 02821 /** 02822 * @brief Write an amount of data in non-blocking mode with DMA to a specific memory address 02823 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains 02824 * the configuration information for the specified I2C. 02825 * @param DevAddress Target device address: The device 7 bits address value 02826 * in datasheet must be shifted to the left before calling the interface 02827 * @param MemAddress Internal memory address 02828 * @param MemAddSize Size of internal memory address 02829 * @param pData Pointer to data buffer 02830 * @param Size Amount of data to be sent 02831 * @retval HAL status 02832 */ 02833 HAL_StatusTypeDef HAL_I2C_Mem_Write_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, 02834 uint16_t MemAddSize, uint8_t *pData, uint16_t Size) 02835 { 02836 uint32_t tickstart; 02837 uint32_t xfermode; 02838 HAL_StatusTypeDef dmaxferstatus; 02839 02840 /* Check the parameters */ 02841 assert_param(IS_I2C_MEMADD_SIZE(MemAddSize)); 02842 02843 if (hi2c->State == HAL_I2C_STATE_READY) 02844 { 02845 if ((pData == NULL) || (Size == 0U)) 02846 { 02847 hi2c->ErrorCode = HAL_I2C_ERROR_INVALID_PARAM; 02848 return HAL_ERROR; 02849 } 02850 02851 if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET) 02852 { 02853 return HAL_BUSY; 02854 } 02855 02856 /* Process Locked */ 02857 __HAL_LOCK(hi2c); 02858 02859 /* Init tickstart for timeout management*/ 02860 tickstart = HAL_GetTick(); 02861 02862 hi2c->State = HAL_I2C_STATE_BUSY_TX; 02863 hi2c->Mode = HAL_I2C_MODE_MEM; 02864 hi2c->ErrorCode = HAL_I2C_ERROR_NONE; 02865 02866 /* Prepare transfer parameters */ 02867 hi2c->pBuffPtr = pData; 02868 hi2c->XferCount = Size; 02869 hi2c->XferOptions = I2C_NO_OPTION_FRAME; 02870 hi2c->XferISR = I2C_Master_ISR_DMA; 02871 02872 if (hi2c->XferCount > MAX_NBYTE_SIZE) 02873 { 02874 hi2c->XferSize = MAX_NBYTE_SIZE; 02875 xfermode = I2C_RELOAD_MODE; 02876 } 02877 else 02878 { 02879 hi2c->XferSize = hi2c->XferCount; 02880 xfermode = I2C_AUTOEND_MODE; 02881 } 02882 02883 /* Send Slave Address and Memory Address */ 02884 if (I2C_RequestMemoryWrite(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG, tickstart) 02885 != HAL_OK) 02886 { 02887 /* Process Unlocked */ 02888 __HAL_UNLOCK(hi2c); 02889 return HAL_ERROR; 02890 } 02891 02892 02893 if (hi2c->hdmatx != NULL) 02894 { 02895 /* Set the I2C DMA transfer complete callback */ 02896 hi2c->hdmatx->XferCpltCallback = I2C_DMAMasterTransmitCplt; 02897 02898 /* Set the DMA error callback */ 02899 hi2c->hdmatx->XferErrorCallback = I2C_DMAError; 02900 02901 /* Set the unused DMA callbacks to NULL */ 02902 hi2c->hdmatx->XferHalfCpltCallback = NULL; 02903 hi2c->hdmatx->XferAbortCallback = NULL; 02904 02905 /* Enable the DMA stream or channel depends on Instance */ 02906 dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)pData, (uint32_t)&hi2c->Instance->TXDR, 02907 hi2c->XferSize); 02908 } 02909 else 02910 { 02911 /* Update I2C state */ 02912 hi2c->State = HAL_I2C_STATE_READY; 02913 hi2c->Mode = HAL_I2C_MODE_NONE; 02914 02915 /* Update I2C error code */ 02916 hi2c->ErrorCode |= HAL_I2C_ERROR_DMA_PARAM; 02917 02918 /* Process Unlocked */ 02919 __HAL_UNLOCK(hi2c); 02920 02921 return HAL_ERROR; 02922 } 02923 02924 if (dmaxferstatus == HAL_OK) 02925 { 02926 /* Send Slave Address */ 02927 /* Set NBYTES to write and reload if hi2c->XferCount > MAX_NBYTE_SIZE and generate RESTART */ 02928 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, xfermode, I2C_NO_STARTSTOP); 02929 02930 /* Update XferCount value */ 02931 hi2c->XferCount -= hi2c->XferSize; 02932 02933 /* Process Unlocked */ 02934 __HAL_UNLOCK(hi2c); 02935 02936 /* Note : The I2C interrupts must be enabled after unlocking current process 02937 to avoid the risk of I2C interrupt handle execution before current 02938 process unlock */ 02939 /* Enable ERR and NACK interrupts */ 02940 I2C_Enable_IRQ(hi2c, I2C_XFER_ERROR_IT); 02941 02942 /* Enable DMA Request */ 02943 hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN; 02944 } 02945 else 02946 { 02947 /* Update I2C state */ 02948 hi2c->State = HAL_I2C_STATE_READY; 02949 hi2c->Mode = HAL_I2C_MODE_NONE; 02950 02951 /* Update I2C error code */ 02952 hi2c->ErrorCode |= HAL_I2C_ERROR_DMA; 02953 02954 /* Process Unlocked */ 02955 __HAL_UNLOCK(hi2c); 02956 02957 return HAL_ERROR; 02958 } 02959 02960 return HAL_OK; 02961 } 02962 else 02963 { 02964 return HAL_BUSY; 02965 } 02966 } 02967 02968 /** 02969 * @brief Reads an amount of data in non-blocking mode with DMA from a specific memory address. 02970 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains 02971 * the configuration information for the specified I2C. 02972 * @param DevAddress Target device address: The device 7 bits address value 02973 * in datasheet must be shifted to the left before calling the interface 02974 * @param MemAddress Internal memory address 02975 * @param MemAddSize Size of internal memory address 02976 * @param pData Pointer to data buffer 02977 * @param Size Amount of data to be read 02978 * @retval HAL status 02979 */ 02980 HAL_StatusTypeDef HAL_I2C_Mem_Read_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, 02981 uint16_t MemAddSize, uint8_t *pData, uint16_t Size) 02982 { 02983 uint32_t tickstart; 02984 uint32_t xfermode; 02985 HAL_StatusTypeDef dmaxferstatus; 02986 02987 /* Check the parameters */ 02988 assert_param(IS_I2C_MEMADD_SIZE(MemAddSize)); 02989 02990 if (hi2c->State == HAL_I2C_STATE_READY) 02991 { 02992 if ((pData == NULL) || (Size == 0U)) 02993 { 02994 hi2c->ErrorCode = HAL_I2C_ERROR_INVALID_PARAM; 02995 return HAL_ERROR; 02996 } 02997 02998 if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET) 02999 { 03000 return HAL_BUSY; 03001 } 03002 03003 /* Process Locked */ 03004 __HAL_LOCK(hi2c); 03005 03006 /* Init tickstart for timeout management*/ 03007 tickstart = HAL_GetTick(); 03008 03009 hi2c->State = HAL_I2C_STATE_BUSY_RX; 03010 hi2c->Mode = HAL_I2C_MODE_MEM; 03011 hi2c->ErrorCode = HAL_I2C_ERROR_NONE; 03012 03013 /* Prepare transfer parameters */ 03014 hi2c->pBuffPtr = pData; 03015 hi2c->XferCount = Size; 03016 hi2c->XferOptions = I2C_NO_OPTION_FRAME; 03017 hi2c->XferISR = I2C_Master_ISR_DMA; 03018 03019 if (hi2c->XferCount > MAX_NBYTE_SIZE) 03020 { 03021 hi2c->XferSize = MAX_NBYTE_SIZE; 03022 xfermode = I2C_RELOAD_MODE; 03023 } 03024 else 03025 { 03026 hi2c->XferSize = hi2c->XferCount; 03027 xfermode = I2C_AUTOEND_MODE; 03028 } 03029 03030 /* Send Slave Address and Memory Address */ 03031 if (I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG, tickstart) != HAL_OK) 03032 { 03033 /* Process Unlocked */ 03034 __HAL_UNLOCK(hi2c); 03035 return HAL_ERROR; 03036 } 03037 03038 if (hi2c->hdmarx != NULL) 03039 { 03040 /* Set the I2C DMA transfer complete callback */ 03041 hi2c->hdmarx->XferCpltCallback = I2C_DMAMasterReceiveCplt; 03042 03043 /* Set the DMA error callback */ 03044 hi2c->hdmarx->XferErrorCallback = I2C_DMAError; 03045 03046 /* Set the unused DMA callbacks to NULL */ 03047 hi2c->hdmarx->XferHalfCpltCallback = NULL; 03048 hi2c->hdmarx->XferAbortCallback = NULL; 03049 03050 /* Enable the DMA stream or channel depends on Instance */ 03051 dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->RXDR, (uint32_t)pData, 03052 hi2c->XferSize); 03053 } 03054 else 03055 { 03056 /* Update I2C state */ 03057 hi2c->State = HAL_I2C_STATE_READY; 03058 hi2c->Mode = HAL_I2C_MODE_NONE; 03059 03060 /* Update I2C error code */ 03061 hi2c->ErrorCode |= HAL_I2C_ERROR_DMA_PARAM; 03062 03063 /* Process Unlocked */ 03064 __HAL_UNLOCK(hi2c); 03065 03066 return HAL_ERROR; 03067 } 03068 03069 if (dmaxferstatus == HAL_OK) 03070 { 03071 /* Set NBYTES to write and reload if hi2c->XferCount > MAX_NBYTE_SIZE and generate RESTART */ 03072 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, xfermode, I2C_GENERATE_START_READ); 03073 03074 /* Update XferCount value */ 03075 hi2c->XferCount -= hi2c->XferSize; 03076 03077 /* Process Unlocked */ 03078 __HAL_UNLOCK(hi2c); 03079 03080 /* Note : The I2C interrupts must be enabled after unlocking current process 03081 to avoid the risk of I2C interrupt handle execution before current 03082 process unlock */ 03083 /* Enable ERR and NACK interrupts */ 03084 I2C_Enable_IRQ(hi2c, I2C_XFER_ERROR_IT); 03085 03086 /* Enable DMA Request */ 03087 hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN; 03088 } 03089 else 03090 { 03091 /* Update I2C state */ 03092 hi2c->State = HAL_I2C_STATE_READY; 03093 hi2c->Mode = HAL_I2C_MODE_NONE; 03094 03095 /* Update I2C error code */ 03096 hi2c->ErrorCode |= HAL_I2C_ERROR_DMA; 03097 03098 /* Process Unlocked */ 03099 __HAL_UNLOCK(hi2c); 03100 03101 return HAL_ERROR; 03102 } 03103 03104 return HAL_OK; 03105 } 03106 else 03107 { 03108 return HAL_BUSY; 03109 } 03110 } 03111 03112 /** 03113 * @brief Checks if target device is ready for communication. 03114 * @note This function is used with Memory devices 03115 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains 03116 * the configuration information for the specified I2C. 03117 * @param DevAddress Target device address: The device 7 bits address value 03118 * in datasheet must be shifted to the left before calling the interface 03119 * @param Trials Number of trials 03120 * @param Timeout Timeout duration 03121 * @retval HAL status 03122 */ 03123 HAL_StatusTypeDef HAL_I2C_IsDeviceReady(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint32_t Trials, 03124 uint32_t Timeout) 03125 { 03126 uint32_t tickstart; 03127 03128 __IO uint32_t I2C_Trials = 0UL; 03129 03130 FlagStatus tmp1; 03131 FlagStatus tmp2; 03132 03133 if (hi2c->State == HAL_I2C_STATE_READY) 03134 { 03135 if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) == SET) 03136 { 03137 return HAL_BUSY; 03138 } 03139 03140 /* Process Locked */ 03141 __HAL_LOCK(hi2c); 03142 03143 hi2c->State = HAL_I2C_STATE_BUSY; 03144 hi2c->ErrorCode = HAL_I2C_ERROR_NONE; 03145 03146 do 03147 { 03148 /* Generate Start */ 03149 hi2c->Instance->CR2 = I2C_GENERATE_START(hi2c->Init.AddressingMode, DevAddress); 03150 03151 /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */ 03152 /* Wait until STOPF flag is set or a NACK flag is set*/ 03153 tickstart = HAL_GetTick(); 03154 03155 tmp1 = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF); 03156 tmp2 = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF); 03157 03158 while ((tmp1 == RESET) && (tmp2 == RESET)) 03159 { 03160 if (Timeout != HAL_MAX_DELAY) 03161 { 03162 if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U)) 03163 { 03164 /* Update I2C state */ 03165 hi2c->State = HAL_I2C_STATE_READY; 03166 03167 /* Update I2C error code */ 03168 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT; 03169 03170 /* Process Unlocked */ 03171 __HAL_UNLOCK(hi2c); 03172 03173 return HAL_ERROR; 03174 } 03175 } 03176 03177 tmp1 = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF); 03178 tmp2 = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF); 03179 } 03180 03181 /* Check if the NACKF flag has not been set */ 03182 if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == RESET) 03183 { 03184 /* Wait until STOPF flag is reset */ 03185 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_STOPF, RESET, Timeout, tickstart) != HAL_OK) 03186 { 03187 return HAL_ERROR; 03188 } 03189 03190 /* Clear STOP Flag */ 03191 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF); 03192 03193 /* Device is ready */ 03194 hi2c->State = HAL_I2C_STATE_READY; 03195 03196 /* Process Unlocked */ 03197 __HAL_UNLOCK(hi2c); 03198 03199 return HAL_OK; 03200 } 03201 else 03202 { 03203 /* Wait until STOPF flag is reset */ 03204 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_STOPF, RESET, Timeout, tickstart) != HAL_OK) 03205 { 03206 return HAL_ERROR; 03207 } 03208 03209 /* Clear NACK Flag */ 03210 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF); 03211 03212 /* Clear STOP Flag, auto generated with autoend*/ 03213 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF); 03214 } 03215 03216 /* Check if the maximum allowed number of trials has been reached */ 03217 if (I2C_Trials == Trials) 03218 { 03219 /* Generate Stop */ 03220 hi2c->Instance->CR2 |= I2C_CR2_STOP; 03221 03222 /* Wait until STOPF flag is reset */ 03223 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_STOPF, RESET, Timeout, tickstart) != HAL_OK) 03224 { 03225 return HAL_ERROR; 03226 } 03227 03228 /* Clear STOP Flag */ 03229 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF); 03230 } 03231 03232 /* Increment Trials */ 03233 I2C_Trials++; 03234 } while (I2C_Trials < Trials); 03235 03236 /* Update I2C state */ 03237 hi2c->State = HAL_I2C_STATE_READY; 03238 03239 /* Update I2C error code */ 03240 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT; 03241 03242 /* Process Unlocked */ 03243 __HAL_UNLOCK(hi2c); 03244 03245 return HAL_ERROR; 03246 } 03247 else 03248 { 03249 return HAL_BUSY; 03250 } 03251 } 03252 03253 /** 03254 * @brief Sequential transmit in master I2C mode an amount of data in non-blocking mode with Interrupt. 03255 * @note This interface allow to manage repeated start condition when a direction change during transfer 03256 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains 03257 * the configuration information for the specified I2C. 03258 * @param DevAddress Target device address: The device 7 bits address value 03259 * in datasheet must be shifted to the left before calling the interface 03260 * @param pData Pointer to data buffer 03261 * @param Size Amount of data to be sent 03262 * @param XferOptions Options of Transfer, value of @ref I2C_XFEROPTIONS 03263 * @retval HAL status 03264 */ 03265 HAL_StatusTypeDef HAL_I2C_Master_Seq_Transmit_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, 03266 uint16_t Size, uint32_t XferOptions) 03267 { 03268 uint32_t xfermode; 03269 uint32_t xferrequest = I2C_GENERATE_START_WRITE; 03270 03271 /* Check the parameters */ 03272 assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions)); 03273 03274 if (hi2c->State == HAL_I2C_STATE_READY) 03275 { 03276 /* Process Locked */ 03277 __HAL_LOCK(hi2c); 03278 03279 hi2c->State = HAL_I2C_STATE_BUSY_TX; 03280 hi2c->Mode = HAL_I2C_MODE_MASTER; 03281 hi2c->ErrorCode = HAL_I2C_ERROR_NONE; 03282 03283 /* Prepare transfer parameters */ 03284 hi2c->pBuffPtr = pData; 03285 hi2c->XferCount = Size; 03286 hi2c->XferOptions = XferOptions; 03287 hi2c->XferISR = I2C_Master_ISR_IT; 03288 03289 /* If hi2c->XferCount > MAX_NBYTE_SIZE, use reload mode */ 03290 if (hi2c->XferCount > MAX_NBYTE_SIZE) 03291 { 03292 hi2c->XferSize = MAX_NBYTE_SIZE; 03293 xfermode = I2C_RELOAD_MODE; 03294 } 03295 else 03296 { 03297 hi2c->XferSize = hi2c->XferCount; 03298 xfermode = hi2c->XferOptions; 03299 } 03300 03301 /* If transfer direction not change and there is no request to start another frame, 03302 do not generate Restart Condition */ 03303 /* Mean Previous state is same as current state */ 03304 if ((hi2c->PreviousState == I2C_STATE_MASTER_BUSY_TX) && \ 03305 (IS_I2C_TRANSFER_OTHER_OPTIONS_REQUEST(XferOptions) == 0)) 03306 { 03307 xferrequest = I2C_NO_STARTSTOP; 03308 } 03309 else 03310 { 03311 /* Convert OTHER_xxx XferOptions if any */ 03312 I2C_ConvertOtherXferOptions(hi2c); 03313 03314 /* Update xfermode accordingly if no reload is necessary */ 03315 if (hi2c->XferCount <= MAX_NBYTE_SIZE) 03316 { 03317 xfermode = hi2c->XferOptions; 03318 } 03319 } 03320 03321 /* Send Slave Address and set NBYTES to write */ 03322 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, xfermode, xferrequest); 03323 03324 /* Process Unlocked */ 03325 __HAL_UNLOCK(hi2c); 03326 03327 /* Note : The I2C interrupts must be enabled after unlocking current process 03328 to avoid the risk of I2C interrupt handle execution before current 03329 process unlock */ 03330 I2C_Enable_IRQ(hi2c, I2C_XFER_TX_IT); 03331 03332 return HAL_OK; 03333 } 03334 else 03335 { 03336 return HAL_BUSY; 03337 } 03338 } 03339 03340 /** 03341 * @brief Sequential transmit in master I2C mode an amount of data in non-blocking mode with DMA. 03342 * @note This interface allow to manage repeated start condition when a direction change during transfer 03343 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains 03344 * the configuration information for the specified I2C. 03345 * @param DevAddress Target device address: The device 7 bits address value 03346 * in datasheet must be shifted to the left before calling the interface 03347 * @param pData Pointer to data buffer 03348 * @param Size Amount of data to be sent 03349 * @param XferOptions Options of Transfer, value of @ref I2C_XFEROPTIONS 03350 * @retval HAL status 03351 */ 03352 HAL_StatusTypeDef HAL_I2C_Master_Seq_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, 03353 uint16_t Size, uint32_t XferOptions) 03354 { 03355 uint32_t xfermode; 03356 uint32_t xferrequest = I2C_GENERATE_START_WRITE; 03357 HAL_StatusTypeDef dmaxferstatus; 03358 03359 /* Check the parameters */ 03360 assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions)); 03361 03362 if (hi2c->State == HAL_I2C_STATE_READY) 03363 { 03364 /* Process Locked */ 03365 __HAL_LOCK(hi2c); 03366 03367 hi2c->State = HAL_I2C_STATE_BUSY_TX; 03368 hi2c->Mode = HAL_I2C_MODE_MASTER; 03369 hi2c->ErrorCode = HAL_I2C_ERROR_NONE; 03370 03371 /* Prepare transfer parameters */ 03372 hi2c->pBuffPtr = pData; 03373 hi2c->XferCount = Size; 03374 hi2c->XferOptions = XferOptions; 03375 hi2c->XferISR = I2C_Master_ISR_DMA; 03376 03377 /* If hi2c->XferCount > MAX_NBYTE_SIZE, use reload mode */ 03378 if (hi2c->XferCount > MAX_NBYTE_SIZE) 03379 { 03380 hi2c->XferSize = MAX_NBYTE_SIZE; 03381 xfermode = I2C_RELOAD_MODE; 03382 } 03383 else 03384 { 03385 hi2c->XferSize = hi2c->XferCount; 03386 xfermode = hi2c->XferOptions; 03387 } 03388 03389 /* If transfer direction not change and there is no request to start another frame, 03390 do not generate Restart Condition */ 03391 /* Mean Previous state is same as current state */ 03392 if ((hi2c->PreviousState == I2C_STATE_MASTER_BUSY_TX) && \ 03393 (IS_I2C_TRANSFER_OTHER_OPTIONS_REQUEST(XferOptions) == 0)) 03394 { 03395 xferrequest = I2C_NO_STARTSTOP; 03396 } 03397 else 03398 { 03399 /* Convert OTHER_xxx XferOptions if any */ 03400 I2C_ConvertOtherXferOptions(hi2c); 03401 03402 /* Update xfermode accordingly if no reload is necessary */ 03403 if (hi2c->XferCount <= MAX_NBYTE_SIZE) 03404 { 03405 xfermode = hi2c->XferOptions; 03406 } 03407 } 03408 03409 if (hi2c->XferSize > 0U) 03410 { 03411 if (hi2c->hdmatx != NULL) 03412 { 03413 /* Set the I2C DMA transfer complete callback */ 03414 hi2c->hdmatx->XferCpltCallback = I2C_DMAMasterTransmitCplt; 03415 03416 /* Set the DMA error callback */ 03417 hi2c->hdmatx->XferErrorCallback = I2C_DMAError; 03418 03419 /* Set the unused DMA callbacks to NULL */ 03420 hi2c->hdmatx->XferHalfCpltCallback = NULL; 03421 hi2c->hdmatx->XferAbortCallback = NULL; 03422 03423 /* Enable the DMA stream or channel depends on Instance */ 03424 dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)pData, (uint32_t)&hi2c->Instance->TXDR, 03425 hi2c->XferSize); 03426 } 03427 else 03428 { 03429 /* Update I2C state */ 03430 hi2c->State = HAL_I2C_STATE_READY; 03431 hi2c->Mode = HAL_I2C_MODE_NONE; 03432 03433 /* Update I2C error code */ 03434 hi2c->ErrorCode |= HAL_I2C_ERROR_DMA_PARAM; 03435 03436 /* Process Unlocked */ 03437 __HAL_UNLOCK(hi2c); 03438 03439 return HAL_ERROR; 03440 } 03441 03442 if (dmaxferstatus == HAL_OK) 03443 { 03444 /* Send Slave Address and set NBYTES to write */ 03445 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, xfermode, xferrequest); 03446 03447 /* Update XferCount value */ 03448 hi2c->XferCount -= hi2c->XferSize; 03449 03450 /* Process Unlocked */ 03451 __HAL_UNLOCK(hi2c); 03452 03453 /* Note : The I2C interrupts must be enabled after unlocking current process 03454 to avoid the risk of I2C interrupt handle execution before current 03455 process unlock */ 03456 /* Enable ERR and NACK interrupts */ 03457 I2C_Enable_IRQ(hi2c, I2C_XFER_ERROR_IT); 03458 03459 /* Enable DMA Request */ 03460 hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN; 03461 } 03462 else 03463 { 03464 /* Update I2C state */ 03465 hi2c->State = HAL_I2C_STATE_READY; 03466 hi2c->Mode = HAL_I2C_MODE_NONE; 03467 03468 /* Update I2C error code */ 03469 hi2c->ErrorCode |= HAL_I2C_ERROR_DMA; 03470 03471 /* Process Unlocked */ 03472 __HAL_UNLOCK(hi2c); 03473 03474 return HAL_ERROR; 03475 } 03476 } 03477 else 03478 { 03479 /* Update Transfer ISR function pointer */ 03480 hi2c->XferISR = I2C_Master_ISR_IT; 03481 03482 /* Send Slave Address */ 03483 /* Set NBYTES to write and generate START condition */ 03484 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_AUTOEND_MODE, 03485 I2C_GENERATE_START_WRITE); 03486 03487 /* Process Unlocked */ 03488 __HAL_UNLOCK(hi2c); 03489 03490 /* Note : The I2C interrupts must be enabled after unlocking current process 03491 to avoid the risk of I2C interrupt handle execution before current 03492 process unlock */ 03493 /* Enable ERR, TC, STOP, NACK, TXI interrupt */ 03494 /* possible to enable all of these */ 03495 /* I2C_IT_ERRI | I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI | 03496 I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */ 03497 I2C_Enable_IRQ(hi2c, I2C_XFER_TX_IT); 03498 } 03499 03500 return HAL_OK; 03501 } 03502 else 03503 { 03504 return HAL_BUSY; 03505 } 03506 } 03507 03508 /** 03509 * @brief Sequential receive in master I2C mode an amount of data in non-blocking mode with Interrupt 03510 * @note This interface allow to manage repeated start condition when a direction change during transfer 03511 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains 03512 * the configuration information for the specified I2C. 03513 * @param DevAddress Target device address: The device 7 bits address value 03514 * in datasheet must be shifted to the left before calling the interface 03515 * @param pData Pointer to data buffer 03516 * @param Size Amount of data to be sent 03517 * @param XferOptions Options of Transfer, value of @ref I2C_XFEROPTIONS 03518 * @retval HAL status 03519 */ 03520 HAL_StatusTypeDef HAL_I2C_Master_Seq_Receive_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, 03521 uint16_t Size, uint32_t XferOptions) 03522 { 03523 uint32_t xfermode; 03524 uint32_t xferrequest = I2C_GENERATE_START_READ; 03525 03526 /* Check the parameters */ 03527 assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions)); 03528 03529 if (hi2c->State == HAL_I2C_STATE_READY) 03530 { 03531 /* Process Locked */ 03532 __HAL_LOCK(hi2c); 03533 03534 hi2c->State = HAL_I2C_STATE_BUSY_RX; 03535 hi2c->Mode = HAL_I2C_MODE_MASTER; 03536 hi2c->ErrorCode = HAL_I2C_ERROR_NONE; 03537 03538 /* Prepare transfer parameters */ 03539 hi2c->pBuffPtr = pData; 03540 hi2c->XferCount = Size; 03541 hi2c->XferOptions = XferOptions; 03542 hi2c->XferISR = I2C_Master_ISR_IT; 03543 03544 /* If hi2c->XferCount > MAX_NBYTE_SIZE, use reload mode */ 03545 if (hi2c->XferCount > MAX_NBYTE_SIZE) 03546 { 03547 hi2c->XferSize = MAX_NBYTE_SIZE; 03548 xfermode = I2C_RELOAD_MODE; 03549 } 03550 else 03551 { 03552 hi2c->XferSize = hi2c->XferCount; 03553 xfermode = hi2c->XferOptions; 03554 } 03555 03556 /* If transfer direction not change and there is no request to start another frame, 03557 do not generate Restart Condition */ 03558 /* Mean Previous state is same as current state */ 03559 if ((hi2c->PreviousState == I2C_STATE_MASTER_BUSY_RX) && \ 03560 (IS_I2C_TRANSFER_OTHER_OPTIONS_REQUEST(XferOptions) == 0)) 03561 { 03562 xferrequest = I2C_NO_STARTSTOP; 03563 } 03564 else 03565 { 03566 /* Convert OTHER_xxx XferOptions if any */ 03567 I2C_ConvertOtherXferOptions(hi2c); 03568 03569 /* Update xfermode accordingly if no reload is necessary */ 03570 if (hi2c->XferCount <= MAX_NBYTE_SIZE) 03571 { 03572 xfermode = hi2c->XferOptions; 03573 } 03574 } 03575 03576 /* Send Slave Address and set NBYTES to read */ 03577 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, xfermode, xferrequest); 03578 03579 /* Process Unlocked */ 03580 __HAL_UNLOCK(hi2c); 03581 03582 /* Note : The I2C interrupts must be enabled after unlocking current process 03583 to avoid the risk of I2C interrupt handle execution before current 03584 process unlock */ 03585 I2C_Enable_IRQ(hi2c, I2C_XFER_RX_IT); 03586 03587 return HAL_OK; 03588 } 03589 else 03590 { 03591 return HAL_BUSY; 03592 } 03593 } 03594 03595 /** 03596 * @brief Sequential receive in master I2C mode an amount of data in non-blocking mode with DMA 03597 * @note This interface allow to manage repeated start condition when a direction change during transfer 03598 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains 03599 * the configuration information for the specified I2C. 03600 * @param DevAddress Target device address: The device 7 bits address value 03601 * in datasheet must be shifted to the left before calling the interface 03602 * @param pData Pointer to data buffer 03603 * @param Size Amount of data to be sent 03604 * @param XferOptions Options of Transfer, value of @ref I2C_XFEROPTIONS 03605 * @retval HAL status 03606 */ 03607 HAL_StatusTypeDef HAL_I2C_Master_Seq_Receive_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, 03608 uint16_t Size, uint32_t XferOptions) 03609 { 03610 uint32_t xfermode; 03611 uint32_t xferrequest = I2C_GENERATE_START_READ; 03612 HAL_StatusTypeDef dmaxferstatus; 03613 03614 /* Check the parameters */ 03615 assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions)); 03616 03617 if (hi2c->State == HAL_I2C_STATE_READY) 03618 { 03619 /* Process Locked */ 03620 __HAL_LOCK(hi2c); 03621 03622 hi2c->State = HAL_I2C_STATE_BUSY_RX; 03623 hi2c->Mode = HAL_I2C_MODE_MASTER; 03624 hi2c->ErrorCode = HAL_I2C_ERROR_NONE; 03625 03626 /* Prepare transfer parameters */ 03627 hi2c->pBuffPtr = pData; 03628 hi2c->XferCount = Size; 03629 hi2c->XferOptions = XferOptions; 03630 hi2c->XferISR = I2C_Master_ISR_DMA; 03631 03632 /* If hi2c->XferCount > MAX_NBYTE_SIZE, use reload mode */ 03633 if (hi2c->XferCount > MAX_NBYTE_SIZE) 03634 { 03635 hi2c->XferSize = MAX_NBYTE_SIZE; 03636 xfermode = I2C_RELOAD_MODE; 03637 } 03638 else 03639 { 03640 hi2c->XferSize = hi2c->XferCount; 03641 xfermode = hi2c->XferOptions; 03642 } 03643 03644 /* If transfer direction not change and there is no request to start another frame, 03645 do not generate Restart Condition */ 03646 /* Mean Previous state is same as current state */ 03647 if ((hi2c->PreviousState == I2C_STATE_MASTER_BUSY_RX) && \ 03648 (IS_I2C_TRANSFER_OTHER_OPTIONS_REQUEST(XferOptions) == 0)) 03649 { 03650 xferrequest = I2C_NO_STARTSTOP; 03651 } 03652 else 03653 { 03654 /* Convert OTHER_xxx XferOptions if any */ 03655 I2C_ConvertOtherXferOptions(hi2c); 03656 03657 /* Update xfermode accordingly if no reload is necessary */ 03658 if (hi2c->XferCount <= MAX_NBYTE_SIZE) 03659 { 03660 xfermode = hi2c->XferOptions; 03661 } 03662 } 03663 03664 if (hi2c->XferSize > 0U) 03665 { 03666 if (hi2c->hdmarx != NULL) 03667 { 03668 /* Set the I2C DMA transfer complete callback */ 03669 hi2c->hdmarx->XferCpltCallback = I2C_DMAMasterReceiveCplt; 03670 03671 /* Set the DMA error callback */ 03672 hi2c->hdmarx->XferErrorCallback = I2C_DMAError; 03673 03674 /* Set the unused DMA callbacks to NULL */ 03675 hi2c->hdmarx->XferHalfCpltCallback = NULL; 03676 hi2c->hdmarx->XferAbortCallback = NULL; 03677 03678 /* Enable the DMA stream or channel depends on Instance */ 03679 dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->RXDR, (uint32_t)pData, 03680 hi2c->XferSize); 03681 } 03682 else 03683 { 03684 /* Update I2C state */ 03685 hi2c->State = HAL_I2C_STATE_READY; 03686 hi2c->Mode = HAL_I2C_MODE_NONE; 03687 03688 /* Update I2C error code */ 03689 hi2c->ErrorCode |= HAL_I2C_ERROR_DMA_PARAM; 03690 03691 /* Process Unlocked */ 03692 __HAL_UNLOCK(hi2c); 03693 03694 return HAL_ERROR; 03695 } 03696 03697 if (dmaxferstatus == HAL_OK) 03698 { 03699 /* Send Slave Address and set NBYTES to read */ 03700 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, xfermode, xferrequest); 03701 03702 /* Update XferCount value */ 03703 hi2c->XferCount -= hi2c->XferSize; 03704 03705 /* Process Unlocked */ 03706 __HAL_UNLOCK(hi2c); 03707 03708 /* Note : The I2C interrupts must be enabled after unlocking current process 03709 to avoid the risk of I2C interrupt handle execution before current 03710 process unlock */ 03711 /* Enable ERR and NACK interrupts */ 03712 I2C_Enable_IRQ(hi2c, I2C_XFER_ERROR_IT); 03713 03714 /* Enable DMA Request */ 03715 hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN; 03716 } 03717 else 03718 { 03719 /* Update I2C state */ 03720 hi2c->State = HAL_I2C_STATE_READY; 03721 hi2c->Mode = HAL_I2C_MODE_NONE; 03722 03723 /* Update I2C error code */ 03724 hi2c->ErrorCode |= HAL_I2C_ERROR_DMA; 03725 03726 /* Process Unlocked */ 03727 __HAL_UNLOCK(hi2c); 03728 03729 return HAL_ERROR; 03730 } 03731 } 03732 else 03733 { 03734 /* Update Transfer ISR function pointer */ 03735 hi2c->XferISR = I2C_Master_ISR_IT; 03736 03737 /* Send Slave Address */ 03738 /* Set NBYTES to read and generate START condition */ 03739 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)hi2c->XferSize, I2C_AUTOEND_MODE, 03740 I2C_GENERATE_START_READ); 03741 03742 /* Process Unlocked */ 03743 __HAL_UNLOCK(hi2c); 03744 03745 /* Note : The I2C interrupts must be enabled after unlocking current process 03746 to avoid the risk of I2C interrupt handle execution before current 03747 process unlock */ 03748 /* Enable ERR, TC, STOP, NACK, TXI interrupt */ 03749 /* possible to enable all of these */ 03750 /* I2C_IT_ERRI | I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI | 03751 I2C_IT_ADDRI | I2C_IT_RXI | I2C_IT_TXI */ 03752 I2C_Enable_IRQ(hi2c, I2C_XFER_TX_IT); 03753 } 03754 03755 return HAL_OK; 03756 } 03757 else 03758 { 03759 return HAL_BUSY; 03760 } 03761 } 03762 03763 /** 03764 * @brief Sequential transmit in slave/device I2C mode an amount of data in non-blocking mode with Interrupt 03765 * @note This interface allow to manage repeated start condition when a direction change during transfer 03766 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains 03767 * the configuration information for the specified I2C. 03768 * @param pData Pointer to data buffer 03769 * @param Size Amount of data to be sent 03770 * @param XferOptions Options of Transfer, value of @ref I2C_XFEROPTIONS 03771 * @retval HAL status 03772 */ 03773 HAL_StatusTypeDef HAL_I2C_Slave_Seq_Transmit_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, 03774 uint32_t XferOptions) 03775 { 03776 /* Check the parameters */ 03777 assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions)); 03778 03779 if (((uint32_t)hi2c->State & (uint32_t)HAL_I2C_STATE_LISTEN) == (uint32_t)HAL_I2C_STATE_LISTEN) 03780 { 03781 if ((pData == NULL) || (Size == 0U)) 03782 { 03783 hi2c->ErrorCode = HAL_I2C_ERROR_INVALID_PARAM; 03784 return HAL_ERROR; 03785 } 03786 03787 /* Disable Interrupts, to prevent preemption during treatment in case of multicall */ 03788 I2C_Disable_IRQ(hi2c, I2C_XFER_LISTEN_IT | I2C_XFER_TX_IT); 03789 03790 /* Process Locked */ 03791 __HAL_LOCK(hi2c); 03792 03793 /* I2C cannot manage full duplex exchange so disable previous IT enabled if any */ 03794 /* and then toggle the HAL slave RX state to TX state */ 03795 if (hi2c->State == HAL_I2C_STATE_BUSY_RX_LISTEN) 03796 { 03797 /* Disable associated Interrupts */ 03798 I2C_Disable_IRQ(hi2c, I2C_XFER_RX_IT); 03799 03800 /* Abort DMA Xfer if any */ 03801 if ((hi2c->Instance->CR1 & I2C_CR1_RXDMAEN) == I2C_CR1_RXDMAEN) 03802 { 03803 hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN; 03804 03805 if (hi2c->hdmarx != NULL) 03806 { 03807 /* Set the I2C DMA Abort callback : 03808 will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */ 03809 hi2c->hdmarx->XferAbortCallback = I2C_DMAAbort; 03810 03811 /* Abort DMA RX */ 03812 if (HAL_DMA_Abort_IT(hi2c->hdmarx) != HAL_OK) 03813 { 03814 /* Call Directly XferAbortCallback function in case of error */ 03815 hi2c->hdmarx->XferAbortCallback(hi2c->hdmarx); 03816 } 03817 } 03818 } 03819 } 03820 03821 hi2c->State = HAL_I2C_STATE_BUSY_TX_LISTEN; 03822 hi2c->Mode = HAL_I2C_MODE_SLAVE; 03823 hi2c->ErrorCode = HAL_I2C_ERROR_NONE; 03824 03825 /* Enable Address Acknowledge */ 03826 hi2c->Instance->CR2 &= ~I2C_CR2_NACK; 03827 03828 /* Prepare transfer parameters */ 03829 hi2c->pBuffPtr = pData; 03830 hi2c->XferCount = Size; 03831 hi2c->XferSize = hi2c->XferCount; 03832 hi2c->XferOptions = XferOptions; 03833 hi2c->XferISR = I2C_Slave_ISR_IT; 03834 03835 if (I2C_GET_DIR(hi2c) == I2C_DIRECTION_RECEIVE) 03836 { 03837 /* Clear ADDR flag after prepare the transfer parameters */ 03838 /* This action will generate an acknowledge to the Master */ 03839 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ADDR); 03840 } 03841 03842 /* Process Unlocked */ 03843 __HAL_UNLOCK(hi2c); 03844 03845 /* Note : The I2C interrupts must be enabled after unlocking current process 03846 to avoid the risk of I2C interrupt handle execution before current 03847 process unlock */ 03848 /* REnable ADDR interrupt */ 03849 I2C_Enable_IRQ(hi2c, I2C_XFER_TX_IT | I2C_XFER_LISTEN_IT); 03850 03851 return HAL_OK; 03852 } 03853 else 03854 { 03855 return HAL_ERROR; 03856 } 03857 } 03858 03859 /** 03860 * @brief Sequential transmit in slave/device I2C mode an amount of data in non-blocking mode with DMA 03861 * @note This interface allow to manage repeated start condition when a direction change during transfer 03862 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains 03863 * the configuration information for the specified I2C. 03864 * @param pData Pointer to data buffer 03865 * @param Size Amount of data to be sent 03866 * @param XferOptions Options of Transfer, value of @ref I2C_XFEROPTIONS 03867 * @retval HAL status 03868 */ 03869 HAL_StatusTypeDef HAL_I2C_Slave_Seq_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, 03870 uint32_t XferOptions) 03871 { 03872 HAL_StatusTypeDef dmaxferstatus; 03873 03874 /* Check the parameters */ 03875 assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions)); 03876 03877 if (((uint32_t)hi2c->State & (uint32_t)HAL_I2C_STATE_LISTEN) == (uint32_t)HAL_I2C_STATE_LISTEN) 03878 { 03879 if ((pData == NULL) || (Size == 0U)) 03880 { 03881 hi2c->ErrorCode = HAL_I2C_ERROR_INVALID_PARAM; 03882 return HAL_ERROR; 03883 } 03884 03885 /* Process Locked */ 03886 __HAL_LOCK(hi2c); 03887 03888 /* Disable Interrupts, to prevent preemption during treatment in case of multicall */ 03889 I2C_Disable_IRQ(hi2c, I2C_XFER_LISTEN_IT | I2C_XFER_TX_IT); 03890 03891 /* I2C cannot manage full duplex exchange so disable previous IT enabled if any */ 03892 /* and then toggle the HAL slave RX state to TX state */ 03893 if (hi2c->State == HAL_I2C_STATE_BUSY_RX_LISTEN) 03894 { 03895 /* Disable associated Interrupts */ 03896 I2C_Disable_IRQ(hi2c, I2C_XFER_RX_IT); 03897 03898 if ((hi2c->Instance->CR1 & I2C_CR1_RXDMAEN) == I2C_CR1_RXDMAEN) 03899 { 03900 /* Abort DMA Xfer if any */ 03901 if (hi2c->hdmarx != NULL) 03902 { 03903 hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN; 03904 03905 /* Set the I2C DMA Abort callback : 03906 will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */ 03907 hi2c->hdmarx->XferAbortCallback = I2C_DMAAbort; 03908 03909 /* Abort DMA RX */ 03910 if (HAL_DMA_Abort_IT(hi2c->hdmarx) != HAL_OK) 03911 { 03912 /* Call Directly XferAbortCallback function in case of error */ 03913 hi2c->hdmarx->XferAbortCallback(hi2c->hdmarx); 03914 } 03915 } 03916 } 03917 } 03918 else if (hi2c->State == HAL_I2C_STATE_BUSY_TX_LISTEN) 03919 { 03920 if ((hi2c->Instance->CR1 & I2C_CR1_TXDMAEN) == I2C_CR1_TXDMAEN) 03921 { 03922 hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN; 03923 03924 /* Abort DMA Xfer if any */ 03925 if (hi2c->hdmatx != NULL) 03926 { 03927 /* Set the I2C DMA Abort callback : 03928 will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */ 03929 hi2c->hdmatx->XferAbortCallback = I2C_DMAAbort; 03930 03931 /* Abort DMA TX */ 03932 if (HAL_DMA_Abort_IT(hi2c->hdmatx) != HAL_OK) 03933 { 03934 /* Call Directly XferAbortCallback function in case of error */ 03935 hi2c->hdmatx->XferAbortCallback(hi2c->hdmatx); 03936 } 03937 } 03938 } 03939 } 03940 else 03941 { 03942 /* Nothing to do */ 03943 } 03944 03945 hi2c->State = HAL_I2C_STATE_BUSY_TX_LISTEN; 03946 hi2c->Mode = HAL_I2C_MODE_SLAVE; 03947 hi2c->ErrorCode = HAL_I2C_ERROR_NONE; 03948 03949 /* Enable Address Acknowledge */ 03950 hi2c->Instance->CR2 &= ~I2C_CR2_NACK; 03951 03952 /* Prepare transfer parameters */ 03953 hi2c->pBuffPtr = pData; 03954 hi2c->XferCount = Size; 03955 hi2c->XferSize = hi2c->XferCount; 03956 hi2c->XferOptions = XferOptions; 03957 hi2c->XferISR = I2C_Slave_ISR_DMA; 03958 03959 if (hi2c->hdmatx != NULL) 03960 { 03961 /* Set the I2C DMA transfer complete callback */ 03962 hi2c->hdmatx->XferCpltCallback = I2C_DMASlaveTransmitCplt; 03963 03964 /* Set the DMA error callback */ 03965 hi2c->hdmatx->XferErrorCallback = I2C_DMAError; 03966 03967 /* Set the unused DMA callbacks to NULL */ 03968 hi2c->hdmatx->XferHalfCpltCallback = NULL; 03969 hi2c->hdmatx->XferAbortCallback = NULL; 03970 03971 /* Enable the DMA stream or channel depends on Instance */ 03972 dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)pData, (uint32_t)&hi2c->Instance->TXDR, 03973 hi2c->XferSize); 03974 } 03975 else 03976 { 03977 /* Update I2C state */ 03978 hi2c->State = HAL_I2C_STATE_LISTEN; 03979 hi2c->Mode = HAL_I2C_MODE_NONE; 03980 03981 /* Update I2C error code */ 03982 hi2c->ErrorCode |= HAL_I2C_ERROR_DMA_PARAM; 03983 03984 /* Process Unlocked */ 03985 __HAL_UNLOCK(hi2c); 03986 03987 return HAL_ERROR; 03988 } 03989 03990 if (dmaxferstatus == HAL_OK) 03991 { 03992 /* Update XferCount value */ 03993 hi2c->XferCount -= hi2c->XferSize; 03994 03995 /* Reset XferSize */ 03996 hi2c->XferSize = 0; 03997 } 03998 else 03999 { 04000 /* Update I2C state */ 04001 hi2c->State = HAL_I2C_STATE_LISTEN; 04002 hi2c->Mode = HAL_I2C_MODE_NONE; 04003 04004 /* Update I2C error code */ 04005 hi2c->ErrorCode |= HAL_I2C_ERROR_DMA; 04006 04007 /* Process Unlocked */ 04008 __HAL_UNLOCK(hi2c); 04009 04010 return HAL_ERROR; 04011 } 04012 04013 if (I2C_GET_DIR(hi2c) == I2C_DIRECTION_RECEIVE) 04014 { 04015 /* Clear ADDR flag after prepare the transfer parameters */ 04016 /* This action will generate an acknowledge to the Master */ 04017 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ADDR); 04018 } 04019 04020 /* Process Unlocked */ 04021 __HAL_UNLOCK(hi2c); 04022 04023 /* Enable DMA Request */ 04024 hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN; 04025 04026 /* Note : The I2C interrupts must be enabled after unlocking current process 04027 to avoid the risk of I2C interrupt handle execution before current 04028 process unlock */ 04029 /* Enable ERR, STOP, NACK, ADDR interrupts */ 04030 I2C_Enable_IRQ(hi2c, I2C_XFER_LISTEN_IT); 04031 04032 return HAL_OK; 04033 } 04034 else 04035 { 04036 return HAL_ERROR; 04037 } 04038 } 04039 04040 /** 04041 * @brief Sequential receive in slave/device I2C mode an amount of data in non-blocking mode with Interrupt 04042 * @note This interface allow to manage repeated start condition when a direction change during transfer 04043 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains 04044 * the configuration information for the specified I2C. 04045 * @param pData Pointer to data buffer 04046 * @param Size Amount of data to be sent 04047 * @param XferOptions Options of Transfer, value of @ref I2C_XFEROPTIONS 04048 * @retval HAL status 04049 */ 04050 HAL_StatusTypeDef HAL_I2C_Slave_Seq_Receive_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, 04051 uint32_t XferOptions) 04052 { 04053 /* Check the parameters */ 04054 assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions)); 04055 04056 if (((uint32_t)hi2c->State & (uint32_t)HAL_I2C_STATE_LISTEN) == (uint32_t)HAL_I2C_STATE_LISTEN) 04057 { 04058 if ((pData == NULL) || (Size == 0U)) 04059 { 04060 hi2c->ErrorCode = HAL_I2C_ERROR_INVALID_PARAM; 04061 return HAL_ERROR; 04062 } 04063 04064 /* Disable Interrupts, to prevent preemption during treatment in case of multicall */ 04065 I2C_Disable_IRQ(hi2c, I2C_XFER_LISTEN_IT | I2C_XFER_RX_IT); 04066 04067 /* Process Locked */ 04068 __HAL_LOCK(hi2c); 04069 04070 /* I2C cannot manage full duplex exchange so disable previous IT enabled if any */ 04071 /* and then toggle the HAL slave TX state to RX state */ 04072 if (hi2c->State == HAL_I2C_STATE_BUSY_TX_LISTEN) 04073 { 04074 /* Disable associated Interrupts */ 04075 I2C_Disable_IRQ(hi2c, I2C_XFER_TX_IT); 04076 04077 if ((hi2c->Instance->CR1 & I2C_CR1_TXDMAEN) == I2C_CR1_TXDMAEN) 04078 { 04079 hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN; 04080 04081 /* Abort DMA Xfer if any */ 04082 if (hi2c->hdmatx != NULL) 04083 { 04084 /* Set the I2C DMA Abort callback : 04085 will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */ 04086 hi2c->hdmatx->XferAbortCallback = I2C_DMAAbort; 04087 04088 /* Abort DMA TX */ 04089 if (HAL_DMA_Abort_IT(hi2c->hdmatx) != HAL_OK) 04090 { 04091 /* Call Directly XferAbortCallback function in case of error */ 04092 hi2c->hdmatx->XferAbortCallback(hi2c->hdmatx); 04093 } 04094 } 04095 } 04096 } 04097 04098 hi2c->State = HAL_I2C_STATE_BUSY_RX_LISTEN; 04099 hi2c->Mode = HAL_I2C_MODE_SLAVE; 04100 hi2c->ErrorCode = HAL_I2C_ERROR_NONE; 04101 04102 /* Enable Address Acknowledge */ 04103 hi2c->Instance->CR2 &= ~I2C_CR2_NACK; 04104 04105 /* Prepare transfer parameters */ 04106 hi2c->pBuffPtr = pData; 04107 hi2c->XferCount = Size; 04108 hi2c->XferSize = hi2c->XferCount; 04109 hi2c->XferOptions = XferOptions; 04110 hi2c->XferISR = I2C_Slave_ISR_IT; 04111 04112 if (I2C_GET_DIR(hi2c) == I2C_DIRECTION_TRANSMIT) 04113 { 04114 /* Clear ADDR flag after prepare the transfer parameters */ 04115 /* This action will generate an acknowledge to the Master */ 04116 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ADDR); 04117 } 04118 04119 /* Process Unlocked */ 04120 __HAL_UNLOCK(hi2c); 04121 04122 /* Note : The I2C interrupts must be enabled after unlocking current process 04123 to avoid the risk of I2C interrupt handle execution before current 04124 process unlock */ 04125 /* REnable ADDR interrupt */ 04126 I2C_Enable_IRQ(hi2c, I2C_XFER_RX_IT | I2C_XFER_LISTEN_IT); 04127 04128 return HAL_OK; 04129 } 04130 else 04131 { 04132 return HAL_ERROR; 04133 } 04134 } 04135 04136 /** 04137 * @brief Sequential receive in slave/device I2C mode an amount of data in non-blocking mode with DMA 04138 * @note This interface allow to manage repeated start condition when a direction change during transfer 04139 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains 04140 * the configuration information for the specified I2C. 04141 * @param pData Pointer to data buffer 04142 * @param Size Amount of data to be sent 04143 * @param XferOptions Options of Transfer, value of @ref I2C_XFEROPTIONS 04144 * @retval HAL status 04145 */ 04146 HAL_StatusTypeDef HAL_I2C_Slave_Seq_Receive_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, 04147 uint32_t XferOptions) 04148 { 04149 HAL_StatusTypeDef dmaxferstatus; 04150 04151 /* Check the parameters */ 04152 assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions)); 04153 04154 if (((uint32_t)hi2c->State & (uint32_t)HAL_I2C_STATE_LISTEN) == (uint32_t)HAL_I2C_STATE_LISTEN) 04155 { 04156 if ((pData == NULL) || (Size == 0U)) 04157 { 04158 hi2c->ErrorCode = HAL_I2C_ERROR_INVALID_PARAM; 04159 return HAL_ERROR; 04160 } 04161 04162 /* Disable Interrupts, to prevent preemption during treatment in case of multicall */ 04163 I2C_Disable_IRQ(hi2c, I2C_XFER_LISTEN_IT | I2C_XFER_RX_IT); 04164 04165 /* Process Locked */ 04166 __HAL_LOCK(hi2c); 04167 04168 /* I2C cannot manage full duplex exchange so disable previous IT enabled if any */ 04169 /* and then toggle the HAL slave TX state to RX state */ 04170 if (hi2c->State == HAL_I2C_STATE_BUSY_TX_LISTEN) 04171 { 04172 /* Disable associated Interrupts */ 04173 I2C_Disable_IRQ(hi2c, I2C_XFER_TX_IT); 04174 04175 if ((hi2c->Instance->CR1 & I2C_CR1_TXDMAEN) == I2C_CR1_TXDMAEN) 04176 { 04177 /* Abort DMA Xfer if any */ 04178 if (hi2c->hdmatx != NULL) 04179 { 04180 hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN; 04181 04182 /* Set the I2C DMA Abort callback : 04183 will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */ 04184 hi2c->hdmatx->XferAbortCallback = I2C_DMAAbort; 04185 04186 /* Abort DMA TX */ 04187 if (HAL_DMA_Abort_IT(hi2c->hdmatx) != HAL_OK) 04188 { 04189 /* Call Directly XferAbortCallback function in case of error */ 04190 hi2c->hdmatx->XferAbortCallback(hi2c->hdmatx); 04191 } 04192 } 04193 } 04194 } 04195 else if (hi2c->State == HAL_I2C_STATE_BUSY_RX_LISTEN) 04196 { 04197 if ((hi2c->Instance->CR1 & I2C_CR1_RXDMAEN) == I2C_CR1_RXDMAEN) 04198 { 04199 hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN; 04200 04201 /* Abort DMA Xfer if any */ 04202 if (hi2c->hdmarx != NULL) 04203 { 04204 /* Set the I2C DMA Abort callback : 04205 will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */ 04206 hi2c->hdmarx->XferAbortCallback = I2C_DMAAbort; 04207 04208 /* Abort DMA RX */ 04209 if (HAL_DMA_Abort_IT(hi2c->hdmarx) != HAL_OK) 04210 { 04211 /* Call Directly XferAbortCallback function in case of error */ 04212 hi2c->hdmarx->XferAbortCallback(hi2c->hdmarx); 04213 } 04214 } 04215 } 04216 } 04217 else 04218 { 04219 /* Nothing to do */ 04220 } 04221 04222 hi2c->State = HAL_I2C_STATE_BUSY_RX_LISTEN; 04223 hi2c->Mode = HAL_I2C_MODE_SLAVE; 04224 hi2c->ErrorCode = HAL_I2C_ERROR_NONE; 04225 04226 /* Enable Address Acknowledge */ 04227 hi2c->Instance->CR2 &= ~I2C_CR2_NACK; 04228 04229 /* Prepare transfer parameters */ 04230 hi2c->pBuffPtr = pData; 04231 hi2c->XferCount = Size; 04232 hi2c->XferSize = hi2c->XferCount; 04233 hi2c->XferOptions = XferOptions; 04234 hi2c->XferISR = I2C_Slave_ISR_DMA; 04235 04236 if (hi2c->hdmarx != NULL) 04237 { 04238 /* Set the I2C DMA transfer complete callback */ 04239 hi2c->hdmarx->XferCpltCallback = I2C_DMASlaveReceiveCplt; 04240 04241 /* Set the DMA error callback */ 04242 hi2c->hdmarx->XferErrorCallback = I2C_DMAError; 04243 04244 /* Set the unused DMA callbacks to NULL */ 04245 hi2c->hdmarx->XferHalfCpltCallback = NULL; 04246 hi2c->hdmarx->XferAbortCallback = NULL; 04247 04248 /* Enable the DMA stream or channel depends on Instance */ 04249 dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->RXDR, 04250 (uint32_t)pData, hi2c->XferSize); 04251 } 04252 else 04253 { 04254 /* Update I2C state */ 04255 hi2c->State = HAL_I2C_STATE_LISTEN; 04256 hi2c->Mode = HAL_I2C_MODE_NONE; 04257 04258 /* Update I2C error code */ 04259 hi2c->ErrorCode |= HAL_I2C_ERROR_DMA_PARAM; 04260 04261 /* Process Unlocked */ 04262 __HAL_UNLOCK(hi2c); 04263 04264 return HAL_ERROR; 04265 } 04266 04267 if (dmaxferstatus == HAL_OK) 04268 { 04269 /* Update XferCount value */ 04270 hi2c->XferCount -= hi2c->XferSize; 04271 04272 /* Reset XferSize */ 04273 hi2c->XferSize = 0; 04274 } 04275 else 04276 { 04277 /* Update I2C state */ 04278 hi2c->State = HAL_I2C_STATE_LISTEN; 04279 hi2c->Mode = HAL_I2C_MODE_NONE; 04280 04281 /* Update I2C error code */ 04282 hi2c->ErrorCode |= HAL_I2C_ERROR_DMA; 04283 04284 /* Process Unlocked */ 04285 __HAL_UNLOCK(hi2c); 04286 04287 return HAL_ERROR; 04288 } 04289 04290 if (I2C_GET_DIR(hi2c) == I2C_DIRECTION_TRANSMIT) 04291 { 04292 /* Clear ADDR flag after prepare the transfer parameters */ 04293 /* This action will generate an acknowledge to the Master */ 04294 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ADDR); 04295 } 04296 04297 /* Process Unlocked */ 04298 __HAL_UNLOCK(hi2c); 04299 04300 /* Enable DMA Request */ 04301 hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN; 04302 04303 /* Note : The I2C interrupts must be enabled after unlocking current process 04304 to avoid the risk of I2C interrupt handle execution before current 04305 process unlock */ 04306 /* REnable ADDR interrupt */ 04307 I2C_Enable_IRQ(hi2c, I2C_XFER_RX_IT | I2C_XFER_LISTEN_IT); 04308 04309 return HAL_OK; 04310 } 04311 else 04312 { 04313 return HAL_ERROR; 04314 } 04315 } 04316 04317 /** 04318 * @brief Enable the Address listen mode with Interrupt. 04319 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains 04320 * the configuration information for the specified I2C. 04321 * @retval HAL status 04322 */ 04323 HAL_StatusTypeDef HAL_I2C_EnableListen_IT(I2C_HandleTypeDef *hi2c) 04324 { 04325 if (hi2c->State == HAL_I2C_STATE_READY) 04326 { 04327 hi2c->State = HAL_I2C_STATE_LISTEN; 04328 hi2c->XferISR = I2C_Slave_ISR_IT; 04329 04330 /* Enable the Address Match interrupt */ 04331 I2C_Enable_IRQ(hi2c, I2C_XFER_LISTEN_IT); 04332 04333 return HAL_OK; 04334 } 04335 else 04336 { 04337 return HAL_BUSY; 04338 } 04339 } 04340 04341 /** 04342 * @brief Disable the Address listen mode with Interrupt. 04343 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains 04344 * the configuration information for the specified I2C 04345 * @retval HAL status 04346 */ 04347 HAL_StatusTypeDef HAL_I2C_DisableListen_IT(I2C_HandleTypeDef *hi2c) 04348 { 04349 /* Declaration of tmp to prevent undefined behavior of volatile usage */ 04350 uint32_t tmp; 04351 04352 /* Disable Address listen mode only if a transfer is not ongoing */ 04353 if (hi2c->State == HAL_I2C_STATE_LISTEN) 04354 { 04355 tmp = (uint32_t)(hi2c->State) & I2C_STATE_MSK; 04356 hi2c->PreviousState = tmp | (uint32_t)(hi2c->Mode); 04357 hi2c->State = HAL_I2C_STATE_READY; 04358 hi2c->Mode = HAL_I2C_MODE_NONE; 04359 hi2c->XferISR = NULL; 04360 04361 /* Disable the Address Match interrupt */ 04362 I2C_Disable_IRQ(hi2c, I2C_XFER_LISTEN_IT); 04363 04364 return HAL_OK; 04365 } 04366 else 04367 { 04368 return HAL_BUSY; 04369 } 04370 } 04371 04372 /** 04373 * @brief Abort a master I2C IT or DMA process communication with Interrupt. 04374 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains 04375 * the configuration information for the specified I2C. 04376 * @param DevAddress Target device address: The device 7 bits address value 04377 * in datasheet must be shifted to the left before calling the interface 04378 * @retval HAL status 04379 */ 04380 HAL_StatusTypeDef HAL_I2C_Master_Abort_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress) 04381 { 04382 if (hi2c->Mode == HAL_I2C_MODE_MASTER) 04383 { 04384 /* Process Locked */ 04385 __HAL_LOCK(hi2c); 04386 04387 /* Disable Interrupts and Store Previous state */ 04388 if (hi2c->State == HAL_I2C_STATE_BUSY_TX) 04389 { 04390 I2C_Disable_IRQ(hi2c, I2C_XFER_TX_IT); 04391 hi2c->PreviousState = I2C_STATE_MASTER_BUSY_TX; 04392 } 04393 else if (hi2c->State == HAL_I2C_STATE_BUSY_RX) 04394 { 04395 I2C_Disable_IRQ(hi2c, I2C_XFER_RX_IT); 04396 hi2c->PreviousState = I2C_STATE_MASTER_BUSY_RX; 04397 } 04398 else 04399 { 04400 /* Do nothing */ 04401 } 04402 04403 /* Set State at HAL_I2C_STATE_ABORT */ 04404 hi2c->State = HAL_I2C_STATE_ABORT; 04405 04406 /* Set NBYTES to 1 to generate a dummy read on I2C peripheral */ 04407 /* Set AUTOEND mode, this will generate a NACK then STOP condition to abort the current transfer */ 04408 I2C_TransferConfig(hi2c, DevAddress, 1, I2C_AUTOEND_MODE, I2C_GENERATE_STOP); 04409 04410 /* Process Unlocked */ 04411 __HAL_UNLOCK(hi2c); 04412 04413 /* Note : The I2C interrupts must be enabled after unlocking current process 04414 to avoid the risk of I2C interrupt handle execution before current 04415 process unlock */ 04416 I2C_Enable_IRQ(hi2c, I2C_XFER_CPLT_IT); 04417 04418 return HAL_OK; 04419 } 04420 else 04421 { 04422 /* Wrong usage of abort function */ 04423 /* This function should be used only in case of abort monitored by master device */ 04424 return HAL_ERROR; 04425 } 04426 } 04427 04428 /** 04429 * @} 04430 */ 04431 04432 /** @defgroup I2C_IRQ_Handler_and_Callbacks IRQ Handler and Callbacks 04433 * @{ 04434 */ 04435 04436 /** 04437 * @brief This function handles I2C event interrupt request. 04438 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains 04439 * the configuration information for the specified I2C. 04440 * @retval None 04441 */ 04442 void HAL_I2C_EV_IRQHandler(I2C_HandleTypeDef *hi2c) 04443 { 04444 /* Get current IT Flags and IT sources value */ 04445 uint32_t itflags = READ_REG(hi2c->Instance->ISR); 04446 uint32_t itsources = READ_REG(hi2c->Instance->CR1); 04447 04448 /* I2C events treatment -------------------------------------*/ 04449 if (hi2c->XferISR != NULL) 04450 { 04451 hi2c->XferISR(hi2c, itflags, itsources); 04452 } 04453 } 04454 04455 /** 04456 * @brief This function handles I2C error interrupt request. 04457 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains 04458 * the configuration information for the specified I2C. 04459 * @retval None 04460 */ 04461 void HAL_I2C_ER_IRQHandler(I2C_HandleTypeDef *hi2c) 04462 { 04463 uint32_t itflags = READ_REG(hi2c->Instance->ISR); 04464 uint32_t itsources = READ_REG(hi2c->Instance->CR1); 04465 uint32_t tmperror; 04466 04467 /* I2C Bus error interrupt occurred ------------------------------------*/ 04468 if ((I2C_CHECK_FLAG(itflags, I2C_FLAG_BERR) != RESET) && \ 04469 (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_ERRI) != RESET)) 04470 { 04471 hi2c->ErrorCode |= HAL_I2C_ERROR_BERR; 04472 04473 /* Clear BERR flag */ 04474 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_BERR); 04475 } 04476 04477 /* I2C Over-Run/Under-Run interrupt occurred ----------------------------------------*/ 04478 if ((I2C_CHECK_FLAG(itflags, I2C_FLAG_OVR) != RESET) && \ 04479 (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_ERRI) != RESET)) 04480 { 04481 hi2c->ErrorCode |= HAL_I2C_ERROR_OVR; 04482 04483 /* Clear OVR flag */ 04484 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_OVR); 04485 } 04486 04487 /* I2C Arbitration Loss error interrupt occurred -------------------------------------*/ 04488 if ((I2C_CHECK_FLAG(itflags, I2C_FLAG_ARLO) != RESET) && \ 04489 (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_ERRI) != RESET)) 04490 { 04491 hi2c->ErrorCode |= HAL_I2C_ERROR_ARLO; 04492 04493 /* Clear ARLO flag */ 04494 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ARLO); 04495 } 04496 04497 /* Store current volatile hi2c->ErrorCode, misra rule */ 04498 tmperror = hi2c->ErrorCode; 04499 04500 /* Call the Error Callback in case of Error detected */ 04501 if ((tmperror & (HAL_I2C_ERROR_BERR | HAL_I2C_ERROR_OVR | HAL_I2C_ERROR_ARLO)) != HAL_I2C_ERROR_NONE) 04502 { 04503 I2C_ITError(hi2c, tmperror); 04504 } 04505 } 04506 04507 /** 04508 * @brief Master Tx Transfer completed callback. 04509 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains 04510 * the configuration information for the specified I2C. 04511 * @retval None 04512 */ 04513 __weak void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef *hi2c) 04514 { 04515 /* Prevent unused argument(s) compilation warning */ 04516 UNUSED(hi2c); 04517 04518 /* NOTE : This function should not be modified, when the callback is needed, 04519 the HAL_I2C_MasterTxCpltCallback could be implemented in the user file 04520 */ 04521 } 04522 04523 /** 04524 * @brief Master Rx Transfer completed callback. 04525 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains 04526 * the configuration information for the specified I2C. 04527 * @retval None 04528 */ 04529 __weak void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *hi2c) 04530 { 04531 /* Prevent unused argument(s) compilation warning */ 04532 UNUSED(hi2c); 04533 04534 /* NOTE : This function should not be modified, when the callback is needed, 04535 the HAL_I2C_MasterRxCpltCallback could be implemented in the user file 04536 */ 04537 } 04538 04539 /** @brief Slave Tx Transfer completed callback. 04540 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains 04541 * the configuration information for the specified I2C. 04542 * @retval None 04543 */ 04544 __weak void HAL_I2C_SlaveTxCpltCallback(I2C_HandleTypeDef *hi2c) 04545 { 04546 /* Prevent unused argument(s) compilation warning */ 04547 UNUSED(hi2c); 04548 04549 /* NOTE : This function should not be modified, when the callback is needed, 04550 the HAL_I2C_SlaveTxCpltCallback could be implemented in the user file 04551 */ 04552 } 04553 04554 /** 04555 * @brief Slave Rx Transfer completed callback. 04556 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains 04557 * the configuration information for the specified I2C. 04558 * @retval None 04559 */ 04560 __weak void HAL_I2C_SlaveRxCpltCallback(I2C_HandleTypeDef *hi2c) 04561 { 04562 /* Prevent unused argument(s) compilation warning */ 04563 UNUSED(hi2c); 04564 04565 /* NOTE : This function should not be modified, when the callback is needed, 04566 the HAL_I2C_SlaveRxCpltCallback could be implemented in the user file 04567 */ 04568 } 04569 04570 /** 04571 * @brief Slave Address Match callback. 04572 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains 04573 * the configuration information for the specified I2C. 04574 * @param TransferDirection Master request Transfer Direction (Write/Read), value of @ref I2C_XFERDIRECTION 04575 * @param AddrMatchCode Address Match Code 04576 * @retval None 04577 */ 04578 __weak void HAL_I2C_AddrCallback(I2C_HandleTypeDef *hi2c, uint8_t TransferDirection, uint16_t AddrMatchCode) 04579 { 04580 /* Prevent unused argument(s) compilation warning */ 04581 UNUSED(hi2c); 04582 UNUSED(TransferDirection); 04583 UNUSED(AddrMatchCode); 04584 04585 /* NOTE : This function should not be modified, when the callback is needed, 04586 the HAL_I2C_AddrCallback() could be implemented in the user file 04587 */ 04588 } 04589 04590 /** 04591 * @brief Listen Complete callback. 04592 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains 04593 * the configuration information for the specified I2C. 04594 * @retval None 04595 */ 04596 __weak void HAL_I2C_ListenCpltCallback(I2C_HandleTypeDef *hi2c) 04597 { 04598 /* Prevent unused argument(s) compilation warning */ 04599 UNUSED(hi2c); 04600 04601 /* NOTE : This function should not be modified, when the callback is needed, 04602 the HAL_I2C_ListenCpltCallback() could be implemented in the user file 04603 */ 04604 } 04605 04606 /** 04607 * @brief Memory Tx Transfer completed callback. 04608 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains 04609 * the configuration information for the specified I2C. 04610 * @retval None 04611 */ 04612 __weak void HAL_I2C_MemTxCpltCallback(I2C_HandleTypeDef *hi2c) 04613 { 04614 /* Prevent unused argument(s) compilation warning */ 04615 UNUSED(hi2c); 04616 04617 /* NOTE : This function should not be modified, when the callback is needed, 04618 the HAL_I2C_MemTxCpltCallback could be implemented in the user file 04619 */ 04620 } 04621 04622 /** 04623 * @brief Memory Rx Transfer completed callback. 04624 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains 04625 * the configuration information for the specified I2C. 04626 * @retval None 04627 */ 04628 __weak void HAL_I2C_MemRxCpltCallback(I2C_HandleTypeDef *hi2c) 04629 { 04630 /* Prevent unused argument(s) compilation warning */ 04631 UNUSED(hi2c); 04632 04633 /* NOTE : This function should not be modified, when the callback is needed, 04634 the HAL_I2C_MemRxCpltCallback could be implemented in the user file 04635 */ 04636 } 04637 04638 /** 04639 * @brief I2C error callback. 04640 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains 04641 * the configuration information for the specified I2C. 04642 * @retval None 04643 */ 04644 __weak void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c) 04645 { 04646 /* Prevent unused argument(s) compilation warning */ 04647 UNUSED(hi2c); 04648 04649 /* NOTE : This function should not be modified, when the callback is needed, 04650 the HAL_I2C_ErrorCallback could be implemented in the user file 04651 */ 04652 } 04653 04654 /** 04655 * @brief I2C abort callback. 04656 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains 04657 * the configuration information for the specified I2C. 04658 * @retval None 04659 */ 04660 __weak void HAL_I2C_AbortCpltCallback(I2C_HandleTypeDef *hi2c) 04661 { 04662 /* Prevent unused argument(s) compilation warning */ 04663 UNUSED(hi2c); 04664 04665 /* NOTE : This function should not be modified, when the callback is needed, 04666 the HAL_I2C_AbortCpltCallback could be implemented in the user file 04667 */ 04668 } 04669 04670 /** 04671 * @} 04672 */ 04673 04674 /** @defgroup I2C_Exported_Functions_Group3 Peripheral State, Mode and Error functions 04675 * @brief Peripheral State, Mode and Error functions 04676 * 04677 @verbatim 04678 =============================================================================== 04679 ##### Peripheral State, Mode and Error functions ##### 04680 =============================================================================== 04681 [..] 04682 This subsection permit to get in run-time the status of the peripheral 04683 and the data flow. 04684 04685 @endverbatim 04686 * @{ 04687 */ 04688 04689 /** 04690 * @brief Return the I2C handle state. 04691 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains 04692 * the configuration information for the specified I2C. 04693 * @retval HAL state 04694 */ 04695 HAL_I2C_StateTypeDef HAL_I2C_GetState(I2C_HandleTypeDef *hi2c) 04696 { 04697 /* Return I2C handle state */ 04698 return hi2c->State; 04699 } 04700 04701 /** 04702 * @brief Returns the I2C Master, Slave, Memory or no mode. 04703 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains 04704 * the configuration information for I2C module 04705 * @retval HAL mode 04706 */ 04707 HAL_I2C_ModeTypeDef HAL_I2C_GetMode(I2C_HandleTypeDef *hi2c) 04708 { 04709 return hi2c->Mode; 04710 } 04711 04712 /** 04713 * @brief Return the I2C error code. 04714 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains 04715 * the configuration information for the specified I2C. 04716 * @retval I2C Error Code 04717 */ 04718 uint32_t HAL_I2C_GetError(I2C_HandleTypeDef *hi2c) 04719 { 04720 return hi2c->ErrorCode; 04721 } 04722 04723 /** 04724 * @} 04725 */ 04726 04727 /** 04728 * @} 04729 */ 04730 04731 /** @addtogroup I2C_Private_Functions 04732 * @{ 04733 */ 04734 04735 /** 04736 * @brief Interrupt Sub-Routine which handle the Interrupt Flags Master Mode with Interrupt. 04737 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains 04738 * the configuration information for the specified I2C. 04739 * @param ITFlags Interrupt flags to handle. 04740 * @param ITSources Interrupt sources enabled. 04741 * @retval HAL status 04742 */ 04743 static HAL_StatusTypeDef I2C_Master_ISR_IT(struct __I2C_HandleTypeDef *hi2c, uint32_t ITFlags, 04744 uint32_t ITSources) 04745 { 04746 uint16_t devaddress; 04747 uint32_t tmpITFlags = ITFlags; 04748 04749 /* Process Locked */ 04750 __HAL_LOCK(hi2c); 04751 04752 if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_AF) != RESET) && \ 04753 (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_NACKI) != RESET)) 04754 { 04755 /* Clear NACK Flag */ 04756 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF); 04757 04758 /* Set corresponding Error Code */ 04759 /* No need to generate STOP, it is automatically done */ 04760 /* Error callback will be send during stop flag treatment */ 04761 hi2c->ErrorCode |= HAL_I2C_ERROR_AF; 04762 04763 /* Flush TX register */ 04764 I2C_Flush_TXDR(hi2c); 04765 } 04766 else if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_RXNE) != RESET) && \ 04767 (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_RXI) != RESET)) 04768 { 04769 /* Remove RXNE flag on temporary variable as read done */ 04770 tmpITFlags &= ~I2C_FLAG_RXNE; 04771 04772 /* Read data from RXDR */ 04773 *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->RXDR; 04774 04775 /* Increment Buffer pointer */ 04776 hi2c->pBuffPtr++; 04777 04778 hi2c->XferSize--; 04779 hi2c->XferCount--; 04780 } 04781 else if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_TXIS) != RESET) && \ 04782 (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_TXI) != RESET)) 04783 { 04784 /* Write data to TXDR */ 04785 hi2c->Instance->TXDR = *hi2c->pBuffPtr; 04786 04787 /* Increment Buffer pointer */ 04788 hi2c->pBuffPtr++; 04789 04790 hi2c->XferSize--; 04791 hi2c->XferCount--; 04792 } 04793 else if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_TCR) != RESET) && \ 04794 (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_TCI) != RESET)) 04795 { 04796 if ((hi2c->XferCount != 0U) && (hi2c->XferSize == 0U)) 04797 { 04798 devaddress = (uint16_t)(hi2c->Instance->CR2 & I2C_CR2_SADD); 04799 04800 if (hi2c->XferCount > MAX_NBYTE_SIZE) 04801 { 04802 hi2c->XferSize = MAX_NBYTE_SIZE; 04803 I2C_TransferConfig(hi2c, devaddress, (uint8_t)hi2c->XferSize, I2C_RELOAD_MODE, I2C_NO_STARTSTOP); 04804 } 04805 else 04806 { 04807 hi2c->XferSize = hi2c->XferCount; 04808 if (hi2c->XferOptions != I2C_NO_OPTION_FRAME) 04809 { 04810 I2C_TransferConfig(hi2c, devaddress, (uint8_t)hi2c->XferSize, 04811 hi2c->XferOptions, I2C_NO_STARTSTOP); 04812 } 04813 else 04814 { 04815 I2C_TransferConfig(hi2c, devaddress, (uint8_t)hi2c->XferSize, 04816 I2C_AUTOEND_MODE, I2C_NO_STARTSTOP); 04817 } 04818 } 04819 } 04820 else 04821 { 04822 /* Call TxCpltCallback() if no stop mode is set */ 04823 if (I2C_GET_STOP_MODE(hi2c) != I2C_AUTOEND_MODE) 04824 { 04825 /* Call I2C Master Sequential complete process */ 04826 I2C_ITMasterSeqCplt(hi2c); 04827 } 04828 else 04829 { 04830 /* Wrong size Status regarding TCR flag event */ 04831 /* Call the corresponding callback to inform upper layer of End of Transfer */ 04832 I2C_ITError(hi2c, HAL_I2C_ERROR_SIZE); 04833 } 04834 } 04835 } 04836 else if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_TC) != RESET) && \ 04837 (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_TCI) != RESET)) 04838 { 04839 if (hi2c->XferCount == 0U) 04840 { 04841 if (I2C_GET_STOP_MODE(hi2c) != I2C_AUTOEND_MODE) 04842 { 04843 /* Generate a stop condition in case of no transfer option */ 04844 if (hi2c->XferOptions == I2C_NO_OPTION_FRAME) 04845 { 04846 /* Generate Stop */ 04847 hi2c->Instance->CR2 |= I2C_CR2_STOP; 04848 } 04849 else 04850 { 04851 /* Call I2C Master Sequential complete process */ 04852 I2C_ITMasterSeqCplt(hi2c); 04853 } 04854 } 04855 } 04856 else 04857 { 04858 /* Wrong size Status regarding TC flag event */ 04859 /* Call the corresponding callback to inform upper layer of End of Transfer */ 04860 I2C_ITError(hi2c, HAL_I2C_ERROR_SIZE); 04861 } 04862 } 04863 else 04864 { 04865 /* Nothing to do */ 04866 } 04867 04868 if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_STOPF) != RESET) && \ 04869 (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_STOPI) != RESET)) 04870 { 04871 /* Call I2C Master complete process */ 04872 I2C_ITMasterCplt(hi2c, tmpITFlags); 04873 } 04874 04875 /* Process Unlocked */ 04876 __HAL_UNLOCK(hi2c); 04877 04878 return HAL_OK; 04879 } 04880 04881 /** 04882 * @brief Interrupt Sub-Routine which handle the Interrupt Flags Slave Mode with Interrupt. 04883 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains 04884 * the configuration information for the specified I2C. 04885 * @param ITFlags Interrupt flags to handle. 04886 * @param ITSources Interrupt sources enabled. 04887 * @retval HAL status 04888 */ 04889 static HAL_StatusTypeDef I2C_Slave_ISR_IT(struct __I2C_HandleTypeDef *hi2c, uint32_t ITFlags, 04890 uint32_t ITSources) 04891 { 04892 uint32_t tmpoptions = hi2c->XferOptions; 04893 uint32_t tmpITFlags = ITFlags; 04894 04895 /* Process locked */ 04896 __HAL_LOCK(hi2c); 04897 04898 /* Check if STOPF is set */ 04899 if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_STOPF) != RESET) && \ 04900 (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_STOPI) != RESET)) 04901 { 04902 /* Call I2C Slave complete process */ 04903 I2C_ITSlaveCplt(hi2c, tmpITFlags); 04904 } 04905 04906 if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_AF) != RESET) && \ 04907 (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_NACKI) != RESET)) 04908 { 04909 /* Check that I2C transfer finished */ 04910 /* if yes, normal use case, a NACK is sent by the MASTER when Transfer is finished */ 04911 /* Mean XferCount == 0*/ 04912 /* So clear Flag NACKF only */ 04913 if (hi2c->XferCount == 0U) 04914 { 04915 if ((hi2c->State == HAL_I2C_STATE_LISTEN) && (tmpoptions == I2C_FIRST_AND_LAST_FRAME)) 04916 /* Same action must be done for (tmpoptions == I2C_LAST_FRAME) which removed for 04917 Warning[Pa134]: left and right operands are identical */ 04918 { 04919 /* Call I2C Listen complete process */ 04920 I2C_ITListenCplt(hi2c, tmpITFlags); 04921 } 04922 else if ((hi2c->State == HAL_I2C_STATE_BUSY_TX_LISTEN) && (tmpoptions != I2C_NO_OPTION_FRAME)) 04923 { 04924 /* Clear NACK Flag */ 04925 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF); 04926 04927 /* Flush TX register */ 04928 I2C_Flush_TXDR(hi2c); 04929 04930 /* Last Byte is Transmitted */ 04931 /* Call I2C Slave Sequential complete process */ 04932 I2C_ITSlaveSeqCplt(hi2c); 04933 } 04934 else 04935 { 04936 /* Clear NACK Flag */ 04937 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF); 04938 } 04939 } 04940 else 04941 { 04942 /* if no, error use case, a Non-Acknowledge of last Data is generated by the MASTER*/ 04943 /* Clear NACK Flag */ 04944 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF); 04945 04946 /* Set ErrorCode corresponding to a Non-Acknowledge */ 04947 hi2c->ErrorCode |= HAL_I2C_ERROR_AF; 04948 04949 if ((tmpoptions == I2C_FIRST_FRAME) || (tmpoptions == I2C_NEXT_FRAME)) 04950 { 04951 /* Call the corresponding callback to inform upper layer of End of Transfer */ 04952 I2C_ITError(hi2c, hi2c->ErrorCode); 04953 } 04954 } 04955 } 04956 else if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_RXNE) != RESET) && \ 04957 (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_RXI) != RESET)) 04958 { 04959 if (hi2c->XferCount > 0U) 04960 { 04961 /* Read data from RXDR */ 04962 *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->RXDR; 04963 04964 /* Increment Buffer pointer */ 04965 hi2c->pBuffPtr++; 04966 04967 hi2c->XferSize--; 04968 hi2c->XferCount--; 04969 } 04970 04971 if ((hi2c->XferCount == 0U) && \ 04972 (tmpoptions != I2C_NO_OPTION_FRAME)) 04973 { 04974 /* Call I2C Slave Sequential complete process */ 04975 I2C_ITSlaveSeqCplt(hi2c); 04976 } 04977 } 04978 else if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_ADDR) != RESET) && \ 04979 (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_ADDRI) != RESET)) 04980 { 04981 I2C_ITAddrCplt(hi2c, tmpITFlags); 04982 } 04983 else if ((I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_TXIS) != RESET) && \ 04984 (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_TXI) != RESET)) 04985 { 04986 /* Write data to TXDR only if XferCount not reach "0" */ 04987 /* A TXIS flag can be set, during STOP treatment */ 04988 /* Check if all Data have already been sent */ 04989 /* If it is the case, this last write in TXDR is not sent, correspond to a dummy TXIS event */ 04990 if (hi2c->XferCount > 0U) 04991 { 04992 /* Write data to TXDR */ 04993 hi2c->Instance->TXDR = *hi2c->pBuffPtr; 04994 04995 /* Increment Buffer pointer */ 04996 hi2c->pBuffPtr++; 04997 04998 hi2c->XferCount--; 04999 hi2c->XferSize--; 05000 } 05001 else 05002 { 05003 if ((tmpoptions == I2C_NEXT_FRAME) || (tmpoptions == I2C_FIRST_FRAME)) 05004 { 05005 /* Last Byte is Transmitted */ 05006 /* Call I2C Slave Sequential complete process */ 05007 I2C_ITSlaveSeqCplt(hi2c); 05008 } 05009 } 05010 } 05011 else 05012 { 05013 /* Nothing to do */ 05014 } 05015 05016 /* Process Unlocked */ 05017 __HAL_UNLOCK(hi2c); 05018 05019 return HAL_OK; 05020 } 05021 05022 /** 05023 * @brief Interrupt Sub-Routine which handle the Interrupt Flags Master Mode with DMA. 05024 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains 05025 * the configuration information for the specified I2C. 05026 * @param ITFlags Interrupt flags to handle. 05027 * @param ITSources Interrupt sources enabled. 05028 * @retval HAL status 05029 */ 05030 static HAL_StatusTypeDef I2C_Master_ISR_DMA(struct __I2C_HandleTypeDef *hi2c, uint32_t ITFlags, 05031 uint32_t ITSources) 05032 { 05033 uint16_t devaddress; 05034 uint32_t xfermode; 05035 05036 /* Process Locked */ 05037 __HAL_LOCK(hi2c); 05038 05039 if ((I2C_CHECK_FLAG(ITFlags, I2C_FLAG_AF) != RESET) && \ 05040 (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_NACKI) != RESET)) 05041 { 05042 /* Clear NACK Flag */ 05043 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF); 05044 05045 /* Set corresponding Error Code */ 05046 hi2c->ErrorCode |= HAL_I2C_ERROR_AF; 05047 05048 /* No need to generate STOP, it is automatically done */ 05049 /* But enable STOP interrupt, to treat it */ 05050 /* Error callback will be send during stop flag treatment */ 05051 I2C_Enable_IRQ(hi2c, I2C_XFER_CPLT_IT); 05052 05053 /* Flush TX register */ 05054 I2C_Flush_TXDR(hi2c); 05055 } 05056 else if ((I2C_CHECK_FLAG(ITFlags, I2C_FLAG_TCR) != RESET) && \ 05057 (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_TCI) != RESET)) 05058 { 05059 /* Disable TC interrupt */ 05060 __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_TCI); 05061 05062 if (hi2c->XferCount != 0U) 05063 { 05064 /* Recover Slave address */ 05065 devaddress = (uint16_t)(hi2c->Instance->CR2 & I2C_CR2_SADD); 05066 05067 /* Prepare the new XferSize to transfer */ 05068 if (hi2c->XferCount > MAX_NBYTE_SIZE) 05069 { 05070 hi2c->XferSize = MAX_NBYTE_SIZE; 05071 xfermode = I2C_RELOAD_MODE; 05072 } 05073 else 05074 { 05075 hi2c->XferSize = hi2c->XferCount; 05076 if (hi2c->XferOptions != I2C_NO_OPTION_FRAME) 05077 { 05078 xfermode = hi2c->XferOptions; 05079 } 05080 else 05081 { 05082 xfermode = I2C_AUTOEND_MODE; 05083 } 05084 } 05085 05086 /* Set the new XferSize in Nbytes register */ 05087 I2C_TransferConfig(hi2c, devaddress, (uint8_t)hi2c->XferSize, xfermode, I2C_NO_STARTSTOP); 05088 05089 /* Update XferCount value */ 05090 hi2c->XferCount -= hi2c->XferSize; 05091 05092 /* Enable DMA Request */ 05093 if (hi2c->State == HAL_I2C_STATE_BUSY_RX) 05094 { 05095 hi2c->Instance->CR1 |= I2C_CR1_RXDMAEN; 05096 } 05097 else 05098 { 05099 hi2c->Instance->CR1 |= I2C_CR1_TXDMAEN; 05100 } 05101 } 05102 else 05103 { 05104 /* Call TxCpltCallback() if no stop mode is set */ 05105 if (I2C_GET_STOP_MODE(hi2c) != I2C_AUTOEND_MODE) 05106 { 05107 /* Call I2C Master Sequential complete process */ 05108 I2C_ITMasterSeqCplt(hi2c); 05109 } 05110 else 05111 { 05112 /* Wrong size Status regarding TCR flag event */ 05113 /* Call the corresponding callback to inform upper layer of End of Transfer */ 05114 I2C_ITError(hi2c, HAL_I2C_ERROR_SIZE); 05115 } 05116 } 05117 } 05118 else if ((I2C_CHECK_FLAG(ITFlags, I2C_FLAG_TC) != RESET) && \ 05119 (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_TCI) != RESET)) 05120 { 05121 if (hi2c->XferCount == 0U) 05122 { 05123 if (I2C_GET_STOP_MODE(hi2c) != I2C_AUTOEND_MODE) 05124 { 05125 /* Generate a stop condition in case of no transfer option */ 05126 if (hi2c->XferOptions == I2C_NO_OPTION_FRAME) 05127 { 05128 /* Generate Stop */ 05129 hi2c->Instance->CR2 |= I2C_CR2_STOP; 05130 } 05131 else 05132 { 05133 /* Call I2C Master Sequential complete process */ 05134 I2C_ITMasterSeqCplt(hi2c); 05135 } 05136 } 05137 } 05138 else 05139 { 05140 /* Wrong size Status regarding TC flag event */ 05141 /* Call the corresponding callback to inform upper layer of End of Transfer */ 05142 I2C_ITError(hi2c, HAL_I2C_ERROR_SIZE); 05143 } 05144 } 05145 else if ((I2C_CHECK_FLAG(ITFlags, I2C_FLAG_STOPF) != RESET) && \ 05146 (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_STOPI) != RESET)) 05147 { 05148 /* Call I2C Master complete process */ 05149 I2C_ITMasterCplt(hi2c, ITFlags); 05150 } 05151 else 05152 { 05153 /* Nothing to do */ 05154 } 05155 05156 /* Process Unlocked */ 05157 __HAL_UNLOCK(hi2c); 05158 05159 return HAL_OK; 05160 } 05161 05162 /** 05163 * @brief Interrupt Sub-Routine which handle the Interrupt Flags Slave Mode with DMA. 05164 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains 05165 * the configuration information for the specified I2C. 05166 * @param ITFlags Interrupt flags to handle. 05167 * @param ITSources Interrupt sources enabled. 05168 * @retval HAL status 05169 */ 05170 static HAL_StatusTypeDef I2C_Slave_ISR_DMA(struct __I2C_HandleTypeDef *hi2c, uint32_t ITFlags, 05171 uint32_t ITSources) 05172 { 05173 uint32_t tmpoptions = hi2c->XferOptions; 05174 uint32_t treatdmanack = 0U; 05175 HAL_I2C_StateTypeDef tmpstate; 05176 05177 /* Process locked */ 05178 __HAL_LOCK(hi2c); 05179 05180 /* Check if STOPF is set */ 05181 if ((I2C_CHECK_FLAG(ITFlags, I2C_FLAG_STOPF) != RESET) && \ 05182 (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_STOPI) != RESET)) 05183 { 05184 /* Call I2C Slave complete process */ 05185 I2C_ITSlaveCplt(hi2c, ITFlags); 05186 } 05187 05188 if ((I2C_CHECK_FLAG(ITFlags, I2C_FLAG_AF) != RESET) && \ 05189 (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_NACKI) != RESET)) 05190 { 05191 /* Check that I2C transfer finished */ 05192 /* if yes, normal use case, a NACK is sent by the MASTER when Transfer is finished */ 05193 /* Mean XferCount == 0 */ 05194 /* So clear Flag NACKF only */ 05195 if ((I2C_CHECK_IT_SOURCE(ITSources, I2C_CR1_TXDMAEN) != RESET) || 05196 (I2C_CHECK_IT_SOURCE(ITSources, I2C_CR1_RXDMAEN) != RESET)) 05197 { 05198 /* Split check of hdmarx, for MISRA compliance */ 05199 if (hi2c->hdmarx != NULL) 05200 { 05201 if (I2C_CHECK_IT_SOURCE(ITSources, I2C_CR1_RXDMAEN) != RESET) 05202 { 05203 if (I2C_GET_DMA_REMAIN_DATA(hi2c->hdmarx) == 0U) 05204 { 05205 treatdmanack = 1U; 05206 } 05207 } 05208 } 05209 05210 /* Split check of hdmatx, for MISRA compliance */ 05211 if (hi2c->hdmatx != NULL) 05212 { 05213 if (I2C_CHECK_IT_SOURCE(ITSources, I2C_CR1_TXDMAEN) != RESET) 05214 { 05215 if (I2C_GET_DMA_REMAIN_DATA(hi2c->hdmatx) == 0U) 05216 { 05217 treatdmanack = 1U; 05218 } 05219 } 05220 } 05221 05222 if (treatdmanack == 1U) 05223 { 05224 if ((hi2c->State == HAL_I2C_STATE_LISTEN) && (tmpoptions == I2C_FIRST_AND_LAST_FRAME)) 05225 /* Same action must be done for (tmpoptions == I2C_LAST_FRAME) which removed for 05226 Warning[Pa134]: left and right operands are identical */ 05227 { 05228 /* Call I2C Listen complete process */ 05229 I2C_ITListenCplt(hi2c, ITFlags); 05230 } 05231 else if ((hi2c->State == HAL_I2C_STATE_BUSY_TX_LISTEN) && (tmpoptions != I2C_NO_OPTION_FRAME)) 05232 { 05233 /* Clear NACK Flag */ 05234 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF); 05235 05236 /* Flush TX register */ 05237 I2C_Flush_TXDR(hi2c); 05238 05239 /* Last Byte is Transmitted */ 05240 /* Call I2C Slave Sequential complete process */ 05241 I2C_ITSlaveSeqCplt(hi2c); 05242 } 05243 else 05244 { 05245 /* Clear NACK Flag */ 05246 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF); 05247 } 05248 } 05249 else 05250 { 05251 /* if no, error use case, a Non-Acknowledge of last Data is generated by the MASTER*/ 05252 /* Clear NACK Flag */ 05253 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF); 05254 05255 /* Set ErrorCode corresponding to a Non-Acknowledge */ 05256 hi2c->ErrorCode |= HAL_I2C_ERROR_AF; 05257 05258 /* Store current hi2c->State, solve MISRA2012-Rule-13.5 */ 05259 tmpstate = hi2c->State; 05260 05261 if ((tmpoptions == I2C_FIRST_FRAME) || (tmpoptions == I2C_NEXT_FRAME)) 05262 { 05263 if ((tmpstate == HAL_I2C_STATE_BUSY_TX) || (tmpstate == HAL_I2C_STATE_BUSY_TX_LISTEN)) 05264 { 05265 hi2c->PreviousState = I2C_STATE_SLAVE_BUSY_TX; 05266 } 05267 else if ((tmpstate == HAL_I2C_STATE_BUSY_RX) || (tmpstate == HAL_I2C_STATE_BUSY_RX_LISTEN)) 05268 { 05269 hi2c->PreviousState = I2C_STATE_SLAVE_BUSY_RX; 05270 } 05271 else 05272 { 05273 /* Do nothing */ 05274 } 05275 05276 /* Call the corresponding callback to inform upper layer of End of Transfer */ 05277 I2C_ITError(hi2c, hi2c->ErrorCode); 05278 } 05279 } 05280 } 05281 else 05282 { 05283 /* Only Clear NACK Flag, no DMA treatment is pending */ 05284 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF); 05285 } 05286 } 05287 else if ((I2C_CHECK_FLAG(ITFlags, I2C_FLAG_ADDR) != RESET) && \ 05288 (I2C_CHECK_IT_SOURCE(ITSources, I2C_IT_ADDRI) != RESET)) 05289 { 05290 I2C_ITAddrCplt(hi2c, ITFlags); 05291 } 05292 else 05293 { 05294 /* Nothing to do */ 05295 } 05296 05297 /* Process Unlocked */ 05298 __HAL_UNLOCK(hi2c); 05299 05300 return HAL_OK; 05301 } 05302 05303 /** 05304 * @brief Master sends target device address followed by internal memory address for write request. 05305 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains 05306 * the configuration information for the specified I2C. 05307 * @param DevAddress Target device address: The device 7 bits address value 05308 * in datasheet must be shifted to the left before calling the interface 05309 * @param MemAddress Internal memory address 05310 * @param MemAddSize Size of internal memory address 05311 * @param Timeout Timeout duration 05312 * @param Tickstart Tick start value 05313 * @retval HAL status 05314 */ 05315 static HAL_StatusTypeDef I2C_RequestMemoryWrite(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, 05316 uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout, 05317 uint32_t Tickstart) 05318 { 05319 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)MemAddSize, I2C_RELOAD_MODE, I2C_GENERATE_START_WRITE); 05320 05321 /* Wait until TXIS flag is set */ 05322 if (I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout, Tickstart) != HAL_OK) 05323 { 05324 return HAL_ERROR; 05325 } 05326 05327 /* If Memory address size is 8Bit */ 05328 if (MemAddSize == I2C_MEMADD_SIZE_8BIT) 05329 { 05330 /* Send Memory Address */ 05331 hi2c->Instance->TXDR = I2C_MEM_ADD_LSB(MemAddress); 05332 } 05333 /* If Memory address size is 16Bit */ 05334 else 05335 { 05336 /* Send MSB of Memory Address */ 05337 hi2c->Instance->TXDR = I2C_MEM_ADD_MSB(MemAddress); 05338 05339 /* Wait until TXIS flag is set */ 05340 if (I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout, Tickstart) != HAL_OK) 05341 { 05342 return HAL_ERROR; 05343 } 05344 05345 /* Send LSB of Memory Address */ 05346 hi2c->Instance->TXDR = I2C_MEM_ADD_LSB(MemAddress); 05347 } 05348 05349 /* Wait until TCR flag is set */ 05350 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TCR, RESET, Timeout, Tickstart) != HAL_OK) 05351 { 05352 return HAL_ERROR; 05353 } 05354 05355 return HAL_OK; 05356 } 05357 05358 /** 05359 * @brief Master sends target device address followed by internal memory address for read request. 05360 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains 05361 * the configuration information for the specified I2C. 05362 * @param DevAddress Target device address: The device 7 bits address value 05363 * in datasheet must be shifted to the left before calling the interface 05364 * @param MemAddress Internal memory address 05365 * @param MemAddSize Size of internal memory address 05366 * @param Timeout Timeout duration 05367 * @param Tickstart Tick start value 05368 * @retval HAL status 05369 */ 05370 static HAL_StatusTypeDef I2C_RequestMemoryRead(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, 05371 uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout, 05372 uint32_t Tickstart) 05373 { 05374 I2C_TransferConfig(hi2c, DevAddress, (uint8_t)MemAddSize, I2C_SOFTEND_MODE, I2C_GENERATE_START_WRITE); 05375 05376 /* Wait until TXIS flag is set */ 05377 if (I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout, Tickstart) != HAL_OK) 05378 { 05379 return HAL_ERROR; 05380 } 05381 05382 /* If Memory address size is 8Bit */ 05383 if (MemAddSize == I2C_MEMADD_SIZE_8BIT) 05384 { 05385 /* Send Memory Address */ 05386 hi2c->Instance->TXDR = I2C_MEM_ADD_LSB(MemAddress); 05387 } 05388 /* If Memory address size is 16Bit */ 05389 else 05390 { 05391 /* Send MSB of Memory Address */ 05392 hi2c->Instance->TXDR = I2C_MEM_ADD_MSB(MemAddress); 05393 05394 /* Wait until TXIS flag is set */ 05395 if (I2C_WaitOnTXISFlagUntilTimeout(hi2c, Timeout, Tickstart) != HAL_OK) 05396 { 05397 return HAL_ERROR; 05398 } 05399 05400 /* Send LSB of Memory Address */ 05401 hi2c->Instance->TXDR = I2C_MEM_ADD_LSB(MemAddress); 05402 } 05403 05404 /* Wait until TC flag is set */ 05405 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TC, RESET, Timeout, Tickstart) != HAL_OK) 05406 { 05407 return HAL_ERROR; 05408 } 05409 05410 return HAL_OK; 05411 } 05412 05413 /** 05414 * @brief I2C Address complete process callback. 05415 * @param hi2c I2C handle. 05416 * @param ITFlags Interrupt flags to handle. 05417 * @retval None 05418 */ 05419 static void I2C_ITAddrCplt(I2C_HandleTypeDef *hi2c, uint32_t ITFlags) 05420 { 05421 uint8_t transferdirection; 05422 uint16_t slaveaddrcode; 05423 uint16_t ownadd1code; 05424 uint16_t ownadd2code; 05425 05426 /* Prevent unused argument(s) compilation warning */ 05427 UNUSED(ITFlags); 05428 05429 /* In case of Listen state, need to inform upper layer of address match code event */ 05430 if (((uint32_t)hi2c->State & (uint32_t)HAL_I2C_STATE_LISTEN) == (uint32_t)HAL_I2C_STATE_LISTEN) 05431 { 05432 transferdirection = I2C_GET_DIR(hi2c); 05433 slaveaddrcode = I2C_GET_ADDR_MATCH(hi2c); 05434 ownadd1code = I2C_GET_OWN_ADDRESS1(hi2c); 05435 ownadd2code = I2C_GET_OWN_ADDRESS2(hi2c); 05436 05437 /* If 10bits addressing mode is selected */ 05438 if (hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_10BIT) 05439 { 05440 if ((slaveaddrcode & SLAVE_ADDR_MSK) == ((ownadd1code >> SLAVE_ADDR_SHIFT) & SLAVE_ADDR_MSK)) 05441 { 05442 slaveaddrcode = ownadd1code; 05443 hi2c->AddrEventCount++; 05444 if (hi2c->AddrEventCount == 2U) 05445 { 05446 /* Reset Address Event counter */ 05447 hi2c->AddrEventCount = 0U; 05448 05449 /* Clear ADDR flag */ 05450 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ADDR); 05451 05452 /* Process Unlocked */ 05453 __HAL_UNLOCK(hi2c); 05454 05455 /* Call Slave Addr callback */ 05456 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1) 05457 hi2c->AddrCallback(hi2c, transferdirection, slaveaddrcode); 05458 #else 05459 HAL_I2C_AddrCallback(hi2c, transferdirection, slaveaddrcode); 05460 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */ 05461 } 05462 } 05463 else 05464 { 05465 slaveaddrcode = ownadd2code; 05466 05467 /* Disable ADDR Interrupts */ 05468 I2C_Disable_IRQ(hi2c, I2C_XFER_LISTEN_IT); 05469 05470 /* Process Unlocked */ 05471 __HAL_UNLOCK(hi2c); 05472 05473 /* Call Slave Addr callback */ 05474 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1) 05475 hi2c->AddrCallback(hi2c, transferdirection, slaveaddrcode); 05476 #else 05477 HAL_I2C_AddrCallback(hi2c, transferdirection, slaveaddrcode); 05478 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */ 05479 } 05480 } 05481 /* else 7 bits addressing mode is selected */ 05482 else 05483 { 05484 /* Disable ADDR Interrupts */ 05485 I2C_Disable_IRQ(hi2c, I2C_XFER_LISTEN_IT); 05486 05487 /* Process Unlocked */ 05488 __HAL_UNLOCK(hi2c); 05489 05490 /* Call Slave Addr callback */ 05491 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1) 05492 hi2c->AddrCallback(hi2c, transferdirection, slaveaddrcode); 05493 #else 05494 HAL_I2C_AddrCallback(hi2c, transferdirection, slaveaddrcode); 05495 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */ 05496 } 05497 } 05498 /* Else clear address flag only */ 05499 else 05500 { 05501 /* Clear ADDR flag */ 05502 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ADDR); 05503 05504 /* Process Unlocked */ 05505 __HAL_UNLOCK(hi2c); 05506 } 05507 } 05508 05509 /** 05510 * @brief I2C Master sequential complete process. 05511 * @param hi2c I2C handle. 05512 * @retval None 05513 */ 05514 static void I2C_ITMasterSeqCplt(I2C_HandleTypeDef *hi2c) 05515 { 05516 /* Reset I2C handle mode */ 05517 hi2c->Mode = HAL_I2C_MODE_NONE; 05518 05519 /* No Generate Stop, to permit restart mode */ 05520 /* The stop will be done at the end of transfer, when I2C_AUTOEND_MODE enable */ 05521 if (hi2c->State == HAL_I2C_STATE_BUSY_TX) 05522 { 05523 hi2c->State = HAL_I2C_STATE_READY; 05524 hi2c->PreviousState = I2C_STATE_MASTER_BUSY_TX; 05525 hi2c->XferISR = NULL; 05526 05527 /* Disable Interrupts */ 05528 I2C_Disable_IRQ(hi2c, I2C_XFER_TX_IT); 05529 05530 /* Process Unlocked */ 05531 __HAL_UNLOCK(hi2c); 05532 05533 /* Call the corresponding callback to inform upper layer of End of Transfer */ 05534 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1) 05535 hi2c->MasterTxCpltCallback(hi2c); 05536 #else 05537 HAL_I2C_MasterTxCpltCallback(hi2c); 05538 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */ 05539 } 05540 /* hi2c->State == HAL_I2C_STATE_BUSY_RX */ 05541 else 05542 { 05543 hi2c->State = HAL_I2C_STATE_READY; 05544 hi2c->PreviousState = I2C_STATE_MASTER_BUSY_RX; 05545 hi2c->XferISR = NULL; 05546 05547 /* Disable Interrupts */ 05548 I2C_Disable_IRQ(hi2c, I2C_XFER_RX_IT); 05549 05550 /* Process Unlocked */ 05551 __HAL_UNLOCK(hi2c); 05552 05553 /* Call the corresponding callback to inform upper layer of End of Transfer */ 05554 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1) 05555 hi2c->MasterRxCpltCallback(hi2c); 05556 #else 05557 HAL_I2C_MasterRxCpltCallback(hi2c); 05558 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */ 05559 } 05560 } 05561 05562 /** 05563 * @brief I2C Slave sequential complete process. 05564 * @param hi2c I2C handle. 05565 * @retval None 05566 */ 05567 static void I2C_ITSlaveSeqCplt(I2C_HandleTypeDef *hi2c) 05568 { 05569 uint32_t tmpcr1value = READ_REG(hi2c->Instance->CR1); 05570 05571 /* Reset I2C handle mode */ 05572 hi2c->Mode = HAL_I2C_MODE_NONE; 05573 05574 /* If a DMA is ongoing, Update handle size context */ 05575 if (I2C_CHECK_IT_SOURCE(tmpcr1value, I2C_CR1_TXDMAEN) != RESET) 05576 { 05577 /* Disable DMA Request */ 05578 hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN; 05579 } 05580 else if (I2C_CHECK_IT_SOURCE(tmpcr1value, I2C_CR1_RXDMAEN) != RESET) 05581 { 05582 /* Disable DMA Request */ 05583 hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN; 05584 } 05585 else 05586 { 05587 /* Do nothing */ 05588 } 05589 05590 if (hi2c->State == HAL_I2C_STATE_BUSY_TX_LISTEN) 05591 { 05592 /* Remove HAL_I2C_STATE_SLAVE_BUSY_TX, keep only HAL_I2C_STATE_LISTEN */ 05593 hi2c->State = HAL_I2C_STATE_LISTEN; 05594 hi2c->PreviousState = I2C_STATE_SLAVE_BUSY_TX; 05595 05596 /* Disable Interrupts */ 05597 I2C_Disable_IRQ(hi2c, I2C_XFER_TX_IT); 05598 05599 /* Process Unlocked */ 05600 __HAL_UNLOCK(hi2c); 05601 05602 /* Call the corresponding callback to inform upper layer of End of Transfer */ 05603 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1) 05604 hi2c->SlaveTxCpltCallback(hi2c); 05605 #else 05606 HAL_I2C_SlaveTxCpltCallback(hi2c); 05607 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */ 05608 } 05609 05610 else if (hi2c->State == HAL_I2C_STATE_BUSY_RX_LISTEN) 05611 { 05612 /* Remove HAL_I2C_STATE_SLAVE_BUSY_RX, keep only HAL_I2C_STATE_LISTEN */ 05613 hi2c->State = HAL_I2C_STATE_LISTEN; 05614 hi2c->PreviousState = I2C_STATE_SLAVE_BUSY_RX; 05615 05616 /* Disable Interrupts */ 05617 I2C_Disable_IRQ(hi2c, I2C_XFER_RX_IT); 05618 05619 /* Process Unlocked */ 05620 __HAL_UNLOCK(hi2c); 05621 05622 /* Call the corresponding callback to inform upper layer of End of Transfer */ 05623 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1) 05624 hi2c->SlaveRxCpltCallback(hi2c); 05625 #else 05626 HAL_I2C_SlaveRxCpltCallback(hi2c); 05627 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */ 05628 } 05629 else 05630 { 05631 /* Nothing to do */ 05632 } 05633 } 05634 05635 /** 05636 * @brief I2C Master complete process. 05637 * @param hi2c I2C handle. 05638 * @param ITFlags Interrupt flags to handle. 05639 * @retval None 05640 */ 05641 static void I2C_ITMasterCplt(I2C_HandleTypeDef *hi2c, uint32_t ITFlags) 05642 { 05643 uint32_t tmperror; 05644 uint32_t tmpITFlags = ITFlags; 05645 __IO uint32_t tmpreg; 05646 05647 /* Clear STOP Flag */ 05648 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF); 05649 05650 /* Disable Interrupts and Store Previous state */ 05651 if (hi2c->State == HAL_I2C_STATE_BUSY_TX) 05652 { 05653 I2C_Disable_IRQ(hi2c, I2C_XFER_TX_IT); 05654 hi2c->PreviousState = I2C_STATE_MASTER_BUSY_TX; 05655 } 05656 else if (hi2c->State == HAL_I2C_STATE_BUSY_RX) 05657 { 05658 I2C_Disable_IRQ(hi2c, I2C_XFER_RX_IT); 05659 hi2c->PreviousState = I2C_STATE_MASTER_BUSY_RX; 05660 } 05661 else 05662 { 05663 /* Do nothing */ 05664 } 05665 05666 /* Clear Configuration Register 2 */ 05667 I2C_RESET_CR2(hi2c); 05668 05669 /* Reset handle parameters */ 05670 hi2c->XferISR = NULL; 05671 hi2c->XferOptions = I2C_NO_OPTION_FRAME; 05672 05673 if (I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_AF) != RESET) 05674 { 05675 /* Clear NACK Flag */ 05676 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF); 05677 05678 /* Set acknowledge error code */ 05679 hi2c->ErrorCode |= HAL_I2C_ERROR_AF; 05680 } 05681 05682 /* Fetch Last receive data if any */ 05683 if ((hi2c->State == HAL_I2C_STATE_ABORT) && (I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_RXNE) != RESET)) 05684 { 05685 /* Read data from RXDR */ 05686 tmpreg = (uint8_t)hi2c->Instance->RXDR; 05687 UNUSED(tmpreg); 05688 } 05689 05690 /* Flush TX register */ 05691 I2C_Flush_TXDR(hi2c); 05692 05693 /* Store current volatile hi2c->ErrorCode, misra rule */ 05694 tmperror = hi2c->ErrorCode; 05695 05696 /* Call the corresponding callback to inform upper layer of End of Transfer */ 05697 if ((hi2c->State == HAL_I2C_STATE_ABORT) || (tmperror != HAL_I2C_ERROR_NONE)) 05698 { 05699 /* Call the corresponding callback to inform upper layer of End of Transfer */ 05700 I2C_ITError(hi2c, hi2c->ErrorCode); 05701 } 05702 /* hi2c->State == HAL_I2C_STATE_BUSY_TX */ 05703 else if (hi2c->State == HAL_I2C_STATE_BUSY_TX) 05704 { 05705 hi2c->State = HAL_I2C_STATE_READY; 05706 hi2c->PreviousState = I2C_STATE_NONE; 05707 05708 if (hi2c->Mode == HAL_I2C_MODE_MEM) 05709 { 05710 hi2c->Mode = HAL_I2C_MODE_NONE; 05711 05712 /* Process Unlocked */ 05713 __HAL_UNLOCK(hi2c); 05714 05715 /* Call the corresponding callback to inform upper layer of End of Transfer */ 05716 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1) 05717 hi2c->MemTxCpltCallback(hi2c); 05718 #else 05719 HAL_I2C_MemTxCpltCallback(hi2c); 05720 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */ 05721 } 05722 else 05723 { 05724 hi2c->Mode = HAL_I2C_MODE_NONE; 05725 05726 /* Process Unlocked */ 05727 __HAL_UNLOCK(hi2c); 05728 05729 /* Call the corresponding callback to inform upper layer of End of Transfer */ 05730 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1) 05731 hi2c->MasterTxCpltCallback(hi2c); 05732 #else 05733 HAL_I2C_MasterTxCpltCallback(hi2c); 05734 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */ 05735 } 05736 } 05737 /* hi2c->State == HAL_I2C_STATE_BUSY_RX */ 05738 else if (hi2c->State == HAL_I2C_STATE_BUSY_RX) 05739 { 05740 hi2c->State = HAL_I2C_STATE_READY; 05741 hi2c->PreviousState = I2C_STATE_NONE; 05742 05743 if (hi2c->Mode == HAL_I2C_MODE_MEM) 05744 { 05745 hi2c->Mode = HAL_I2C_MODE_NONE; 05746 05747 /* Process Unlocked */ 05748 __HAL_UNLOCK(hi2c); 05749 05750 /* Call the corresponding callback to inform upper layer of End of Transfer */ 05751 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1) 05752 hi2c->MemRxCpltCallback(hi2c); 05753 #else 05754 HAL_I2C_MemRxCpltCallback(hi2c); 05755 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */ 05756 } 05757 else 05758 { 05759 hi2c->Mode = HAL_I2C_MODE_NONE; 05760 05761 /* Process Unlocked */ 05762 __HAL_UNLOCK(hi2c); 05763 05764 /* Call the corresponding callback to inform upper layer of End of Transfer */ 05765 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1) 05766 hi2c->MasterRxCpltCallback(hi2c); 05767 #else 05768 HAL_I2C_MasterRxCpltCallback(hi2c); 05769 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */ 05770 } 05771 } 05772 else 05773 { 05774 /* Nothing to do */ 05775 } 05776 } 05777 05778 /** 05779 * @brief I2C Slave complete process. 05780 * @param hi2c I2C handle. 05781 * @param ITFlags Interrupt flags to handle. 05782 * @retval None 05783 */ 05784 static void I2C_ITSlaveCplt(I2C_HandleTypeDef *hi2c, uint32_t ITFlags) 05785 { 05786 uint32_t tmpcr1value = READ_REG(hi2c->Instance->CR1); 05787 uint32_t tmpITFlags = ITFlags; 05788 HAL_I2C_StateTypeDef tmpstate = hi2c->State; 05789 05790 /* Clear STOP Flag */ 05791 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF); 05792 05793 /* Disable Interrupts and Store Previous state */ 05794 if ((tmpstate == HAL_I2C_STATE_BUSY_TX) || (tmpstate == HAL_I2C_STATE_BUSY_TX_LISTEN)) 05795 { 05796 I2C_Disable_IRQ(hi2c, I2C_XFER_LISTEN_IT | I2C_XFER_TX_IT); 05797 hi2c->PreviousState = I2C_STATE_SLAVE_BUSY_TX; 05798 } 05799 else if ((tmpstate == HAL_I2C_STATE_BUSY_RX) || (tmpstate == HAL_I2C_STATE_BUSY_RX_LISTEN)) 05800 { 05801 I2C_Disable_IRQ(hi2c, I2C_XFER_LISTEN_IT | I2C_XFER_RX_IT); 05802 hi2c->PreviousState = I2C_STATE_SLAVE_BUSY_RX; 05803 } 05804 else 05805 { 05806 /* Do nothing */ 05807 } 05808 05809 /* Disable Address Acknowledge */ 05810 hi2c->Instance->CR2 |= I2C_CR2_NACK; 05811 05812 /* Clear Configuration Register 2 */ 05813 I2C_RESET_CR2(hi2c); 05814 05815 /* Flush TX register */ 05816 I2C_Flush_TXDR(hi2c); 05817 05818 /* If a DMA is ongoing, Update handle size context */ 05819 if (I2C_CHECK_IT_SOURCE(tmpcr1value, I2C_CR1_TXDMAEN) != RESET) 05820 { 05821 /* Disable DMA Request */ 05822 hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN; 05823 05824 if (hi2c->hdmatx != NULL) 05825 { 05826 hi2c->XferCount = (uint16_t)I2C_GET_DMA_REMAIN_DATA(hi2c->hdmatx); 05827 } 05828 } 05829 else if (I2C_CHECK_IT_SOURCE(tmpcr1value, I2C_CR1_RXDMAEN) != RESET) 05830 { 05831 /* Disable DMA Request */ 05832 hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN; 05833 05834 if (hi2c->hdmarx != NULL) 05835 { 05836 hi2c->XferCount = (uint16_t)I2C_GET_DMA_REMAIN_DATA(hi2c->hdmarx); 05837 } 05838 } 05839 else 05840 { 05841 /* Do nothing */ 05842 } 05843 05844 /* Store Last receive data if any */ 05845 if (I2C_CHECK_FLAG(tmpITFlags, I2C_FLAG_RXNE) != RESET) 05846 { 05847 /* Remove RXNE flag on temporary variable as read done */ 05848 tmpITFlags &= ~I2C_FLAG_RXNE; 05849 05850 /* Read data from RXDR */ 05851 *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->RXDR; 05852 05853 /* Increment Buffer pointer */ 05854 hi2c->pBuffPtr++; 05855 05856 if ((hi2c->XferSize > 0U)) 05857 { 05858 hi2c->XferSize--; 05859 hi2c->XferCount--; 05860 } 05861 } 05862 05863 /* All data are not transferred, so set error code accordingly */ 05864 if (hi2c->XferCount != 0U) 05865 { 05866 /* Set ErrorCode corresponding to a Non-Acknowledge */ 05867 hi2c->ErrorCode |= HAL_I2C_ERROR_AF; 05868 } 05869 05870 hi2c->Mode = HAL_I2C_MODE_NONE; 05871 hi2c->XferISR = NULL; 05872 05873 if (hi2c->ErrorCode != HAL_I2C_ERROR_NONE) 05874 { 05875 /* Call the corresponding callback to inform upper layer of End of Transfer */ 05876 I2C_ITError(hi2c, hi2c->ErrorCode); 05877 05878 /* Call the Listen Complete callback, to inform upper layer of the end of Listen usecase */ 05879 if (hi2c->State == HAL_I2C_STATE_LISTEN) 05880 { 05881 /* Call I2C Listen complete process */ 05882 I2C_ITListenCplt(hi2c, tmpITFlags); 05883 } 05884 } 05885 else if (hi2c->XferOptions != I2C_NO_OPTION_FRAME) 05886 { 05887 /* Call the Sequential Complete callback, to inform upper layer of the end of Transfer */ 05888 I2C_ITSlaveSeqCplt(hi2c); 05889 05890 hi2c->XferOptions = I2C_NO_OPTION_FRAME; 05891 hi2c->State = HAL_I2C_STATE_READY; 05892 hi2c->PreviousState = I2C_STATE_NONE; 05893 05894 /* Process Unlocked */ 05895 __HAL_UNLOCK(hi2c); 05896 05897 /* Call the Listen Complete callback, to inform upper layer of the end of Listen usecase */ 05898 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1) 05899 hi2c->ListenCpltCallback(hi2c); 05900 #else 05901 HAL_I2C_ListenCpltCallback(hi2c); 05902 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */ 05903 } 05904 /* Call the corresponding callback to inform upper layer of End of Transfer */ 05905 else if (hi2c->State == HAL_I2C_STATE_BUSY_RX) 05906 { 05907 hi2c->State = HAL_I2C_STATE_READY; 05908 hi2c->PreviousState = I2C_STATE_NONE; 05909 05910 /* Process Unlocked */ 05911 __HAL_UNLOCK(hi2c); 05912 05913 /* Call the corresponding callback to inform upper layer of End of Transfer */ 05914 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1) 05915 hi2c->SlaveRxCpltCallback(hi2c); 05916 #else 05917 HAL_I2C_SlaveRxCpltCallback(hi2c); 05918 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */ 05919 } 05920 else 05921 { 05922 hi2c->State = HAL_I2C_STATE_READY; 05923 hi2c->PreviousState = I2C_STATE_NONE; 05924 05925 /* Process Unlocked */ 05926 __HAL_UNLOCK(hi2c); 05927 05928 /* Call the corresponding callback to inform upper layer of End of Transfer */ 05929 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1) 05930 hi2c->SlaveTxCpltCallback(hi2c); 05931 #else 05932 HAL_I2C_SlaveTxCpltCallback(hi2c); 05933 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */ 05934 } 05935 } 05936 05937 /** 05938 * @brief I2C Listen complete process. 05939 * @param hi2c I2C handle. 05940 * @param ITFlags Interrupt flags to handle. 05941 * @retval None 05942 */ 05943 static void I2C_ITListenCplt(I2C_HandleTypeDef *hi2c, uint32_t ITFlags) 05944 { 05945 /* Reset handle parameters */ 05946 hi2c->XferOptions = I2C_NO_OPTION_FRAME; 05947 hi2c->PreviousState = I2C_STATE_NONE; 05948 hi2c->State = HAL_I2C_STATE_READY; 05949 hi2c->Mode = HAL_I2C_MODE_NONE; 05950 hi2c->XferISR = NULL; 05951 05952 /* Store Last receive data if any */ 05953 if (I2C_CHECK_FLAG(ITFlags, I2C_FLAG_RXNE) != RESET) 05954 { 05955 /* Read data from RXDR */ 05956 *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->RXDR; 05957 05958 /* Increment Buffer pointer */ 05959 hi2c->pBuffPtr++; 05960 05961 if ((hi2c->XferSize > 0U)) 05962 { 05963 hi2c->XferSize--; 05964 hi2c->XferCount--; 05965 05966 /* Set ErrorCode corresponding to a Non-Acknowledge */ 05967 hi2c->ErrorCode |= HAL_I2C_ERROR_AF; 05968 } 05969 } 05970 05971 /* Disable all Interrupts*/ 05972 I2C_Disable_IRQ(hi2c, I2C_XFER_LISTEN_IT | I2C_XFER_RX_IT | I2C_XFER_TX_IT); 05973 05974 /* Clear NACK Flag */ 05975 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF); 05976 05977 /* Process Unlocked */ 05978 __HAL_UNLOCK(hi2c); 05979 05980 /* Call the Listen Complete callback, to inform upper layer of the end of Listen usecase */ 05981 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1) 05982 hi2c->ListenCpltCallback(hi2c); 05983 #else 05984 HAL_I2C_ListenCpltCallback(hi2c); 05985 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */ 05986 } 05987 05988 /** 05989 * @brief I2C interrupts error process. 05990 * @param hi2c I2C handle. 05991 * @param ErrorCode Error code to handle. 05992 * @retval None 05993 */ 05994 static void I2C_ITError(I2C_HandleTypeDef *hi2c, uint32_t ErrorCode) 05995 { 05996 HAL_I2C_StateTypeDef tmpstate = hi2c->State; 05997 uint32_t tmppreviousstate; 05998 05999 /* Reset handle parameters */ 06000 hi2c->Mode = HAL_I2C_MODE_NONE; 06001 hi2c->XferOptions = I2C_NO_OPTION_FRAME; 06002 hi2c->XferCount = 0U; 06003 06004 /* Set new error code */ 06005 hi2c->ErrorCode |= ErrorCode; 06006 06007 /* Disable Interrupts */ 06008 if ((tmpstate == HAL_I2C_STATE_LISTEN) || 06009 (tmpstate == HAL_I2C_STATE_BUSY_TX_LISTEN) || 06010 (tmpstate == HAL_I2C_STATE_BUSY_RX_LISTEN)) 06011 { 06012 /* Disable all interrupts, except interrupts related to LISTEN state */ 06013 I2C_Disable_IRQ(hi2c, I2C_XFER_RX_IT | I2C_XFER_TX_IT); 06014 06015 /* keep HAL_I2C_STATE_LISTEN if set */ 06016 hi2c->State = HAL_I2C_STATE_LISTEN; 06017 hi2c->XferISR = I2C_Slave_ISR_IT; 06018 } 06019 else 06020 { 06021 /* Disable all interrupts */ 06022 I2C_Disable_IRQ(hi2c, I2C_XFER_LISTEN_IT | I2C_XFER_RX_IT | I2C_XFER_TX_IT); 06023 06024 /* If state is an abort treatment on going, don't change state */ 06025 /* This change will be do later */ 06026 if (hi2c->State != HAL_I2C_STATE_ABORT) 06027 { 06028 /* Set HAL_I2C_STATE_READY */ 06029 hi2c->State = HAL_I2C_STATE_READY; 06030 } 06031 hi2c->XferISR = NULL; 06032 } 06033 06034 /* Abort DMA TX transfer if any */ 06035 tmppreviousstate = hi2c->PreviousState; 06036 if ((hi2c->hdmatx != NULL) && ((tmppreviousstate == I2C_STATE_MASTER_BUSY_TX) || \ 06037 (tmppreviousstate == I2C_STATE_SLAVE_BUSY_TX))) 06038 { 06039 if ((hi2c->Instance->CR1 & I2C_CR1_TXDMAEN) == I2C_CR1_TXDMAEN) 06040 { 06041 hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN; 06042 } 06043 06044 if (HAL_DMA_GetState(hi2c->hdmatx) != HAL_DMA_STATE_READY) 06045 { 06046 /* Set the I2C DMA Abort callback : 06047 will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */ 06048 hi2c->hdmatx->XferAbortCallback = I2C_DMAAbort; 06049 06050 /* Process Unlocked */ 06051 __HAL_UNLOCK(hi2c); 06052 06053 /* Abort DMA TX */ 06054 if (HAL_DMA_Abort_IT(hi2c->hdmatx) != HAL_OK) 06055 { 06056 /* Call Directly XferAbortCallback function in case of error */ 06057 hi2c->hdmatx->XferAbortCallback(hi2c->hdmatx); 06058 } 06059 } 06060 else 06061 { 06062 I2C_TreatErrorCallback(hi2c); 06063 } 06064 } 06065 /* Abort DMA RX transfer if any */ 06066 else if ((hi2c->hdmarx != NULL) && ((tmppreviousstate == I2C_STATE_MASTER_BUSY_RX) || \ 06067 (tmppreviousstate == I2C_STATE_SLAVE_BUSY_RX))) 06068 { 06069 if ((hi2c->Instance->CR1 & I2C_CR1_RXDMAEN) == I2C_CR1_RXDMAEN) 06070 { 06071 hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN; 06072 } 06073 06074 if (HAL_DMA_GetState(hi2c->hdmarx) != HAL_DMA_STATE_READY) 06075 { 06076 /* Set the I2C DMA Abort callback : 06077 will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */ 06078 hi2c->hdmarx->XferAbortCallback = I2C_DMAAbort; 06079 06080 /* Process Unlocked */ 06081 __HAL_UNLOCK(hi2c); 06082 06083 /* Abort DMA RX */ 06084 if (HAL_DMA_Abort_IT(hi2c->hdmarx) != HAL_OK) 06085 { 06086 /* Call Directly hi2c->hdmarx->XferAbortCallback function in case of error */ 06087 hi2c->hdmarx->XferAbortCallback(hi2c->hdmarx); 06088 } 06089 } 06090 else 06091 { 06092 I2C_TreatErrorCallback(hi2c); 06093 } 06094 } 06095 else 06096 { 06097 I2C_TreatErrorCallback(hi2c); 06098 } 06099 } 06100 06101 /** 06102 * @brief I2C Error callback treatment. 06103 * @param hi2c I2C handle. 06104 * @retval None 06105 */ 06106 static void I2C_TreatErrorCallback(I2C_HandleTypeDef *hi2c) 06107 { 06108 if (hi2c->State == HAL_I2C_STATE_ABORT) 06109 { 06110 hi2c->State = HAL_I2C_STATE_READY; 06111 hi2c->PreviousState = I2C_STATE_NONE; 06112 06113 /* Process Unlocked */ 06114 __HAL_UNLOCK(hi2c); 06115 06116 /* Call the corresponding callback to inform upper layer of End of Transfer */ 06117 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1) 06118 hi2c->AbortCpltCallback(hi2c); 06119 #else 06120 HAL_I2C_AbortCpltCallback(hi2c); 06121 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */ 06122 } 06123 else 06124 { 06125 hi2c->PreviousState = I2C_STATE_NONE; 06126 06127 /* Process Unlocked */ 06128 __HAL_UNLOCK(hi2c); 06129 06130 /* Call the corresponding callback to inform upper layer of End of Transfer */ 06131 #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1) 06132 hi2c->ErrorCallback(hi2c); 06133 #else 06134 HAL_I2C_ErrorCallback(hi2c); 06135 #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */ 06136 } 06137 } 06138 06139 /** 06140 * @brief I2C Tx data register flush process. 06141 * @param hi2c I2C handle. 06142 * @retval None 06143 */ 06144 static void I2C_Flush_TXDR(I2C_HandleTypeDef *hi2c) 06145 { 06146 /* If a pending TXIS flag is set */ 06147 /* Write a dummy data in TXDR to clear it */ 06148 if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXIS) != RESET) 06149 { 06150 hi2c->Instance->TXDR = 0x00U; 06151 } 06152 06153 /* Flush TX register if not empty */ 06154 if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXE) == RESET) 06155 { 06156 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_TXE); 06157 } 06158 } 06159 06160 /** 06161 * @brief DMA I2C master transmit process complete callback. 06162 * @param hdma DMA handle 06163 * @retval None 06164 */ 06165 static void I2C_DMAMasterTransmitCplt(DMA_HandleTypeDef *hdma) 06166 { 06167 /* Derogation MISRAC2012-Rule-11.5 */ 06168 I2C_HandleTypeDef *hi2c = (I2C_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent); 06169 06170 /* Disable DMA Request */ 06171 hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN; 06172 06173 /* If last transfer, enable STOP interrupt */ 06174 if (hi2c->XferCount == 0U) 06175 { 06176 /* Enable STOP interrupt */ 06177 I2C_Enable_IRQ(hi2c, I2C_XFER_CPLT_IT); 06178 } 06179 /* else prepare a new DMA transfer and enable TCReload interrupt */ 06180 else 06181 { 06182 /* Update Buffer pointer */ 06183 hi2c->pBuffPtr += hi2c->XferSize; 06184 06185 /* Set the XferSize to transfer */ 06186 if (hi2c->XferCount > MAX_NBYTE_SIZE) 06187 { 06188 hi2c->XferSize = MAX_NBYTE_SIZE; 06189 } 06190 else 06191 { 06192 hi2c->XferSize = hi2c->XferCount; 06193 } 06194 06195 /* Enable the DMA stream or channel depends on Instance */ 06196 if (HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)hi2c->pBuffPtr, (uint32_t)&hi2c->Instance->TXDR, 06197 hi2c->XferSize) != HAL_OK) 06198 { 06199 /* Call the corresponding callback to inform upper layer of End of Transfer */ 06200 I2C_ITError(hi2c, HAL_I2C_ERROR_DMA); 06201 } 06202 else 06203 { 06204 /* Enable TC interrupts */ 06205 I2C_Enable_IRQ(hi2c, I2C_XFER_RELOAD_IT); 06206 } 06207 } 06208 } 06209 06210 /** 06211 * @brief DMA I2C slave transmit process complete callback. 06212 * @param hdma DMA handle 06213 * @retval None 06214 */ 06215 static void I2C_DMASlaveTransmitCplt(DMA_HandleTypeDef *hdma) 06216 { 06217 /* Derogation MISRAC2012-Rule-11.5 */ 06218 I2C_HandleTypeDef *hi2c = (I2C_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent); 06219 uint32_t tmpoptions = hi2c->XferOptions; 06220 06221 if ((tmpoptions == I2C_NEXT_FRAME) || (tmpoptions == I2C_FIRST_FRAME)) 06222 { 06223 /* Disable DMA Request */ 06224 hi2c->Instance->CR1 &= ~I2C_CR1_TXDMAEN; 06225 06226 /* Last Byte is Transmitted */ 06227 /* Call I2C Slave Sequential complete process */ 06228 I2C_ITSlaveSeqCplt(hi2c); 06229 } 06230 else 06231 { 06232 /* No specific action, Master fully manage the generation of STOP condition */ 06233 /* Mean that this generation can arrive at any time, at the end or during DMA process */ 06234 /* So STOP condition should be manage through Interrupt treatment */ 06235 } 06236 } 06237 06238 /** 06239 * @brief DMA I2C master receive process complete callback. 06240 * @param hdma DMA handle 06241 * @retval None 06242 */ 06243 static void I2C_DMAMasterReceiveCplt(DMA_HandleTypeDef *hdma) 06244 { 06245 /* Derogation MISRAC2012-Rule-11.5 */ 06246 I2C_HandleTypeDef *hi2c = (I2C_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent); 06247 06248 /* Disable DMA Request */ 06249 hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN; 06250 06251 /* If last transfer, enable STOP interrupt */ 06252 if (hi2c->XferCount == 0U) 06253 { 06254 /* Enable STOP interrupt */ 06255 I2C_Enable_IRQ(hi2c, I2C_XFER_CPLT_IT); 06256 } 06257 /* else prepare a new DMA transfer and enable TCReload interrupt */ 06258 else 06259 { 06260 /* Update Buffer pointer */ 06261 hi2c->pBuffPtr += hi2c->XferSize; 06262 06263 /* Set the XferSize to transfer */ 06264 if (hi2c->XferCount > MAX_NBYTE_SIZE) 06265 { 06266 hi2c->XferSize = MAX_NBYTE_SIZE; 06267 } 06268 else 06269 { 06270 hi2c->XferSize = hi2c->XferCount; 06271 } 06272 06273 /* Enable the DMA stream or channel depends on Instance */ 06274 if (HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->RXDR, (uint32_t)hi2c->pBuffPtr, 06275 hi2c->XferSize) != HAL_OK) 06276 { 06277 /* Call the corresponding callback to inform upper layer of End of Transfer */ 06278 I2C_ITError(hi2c, HAL_I2C_ERROR_DMA); 06279 } 06280 else 06281 { 06282 /* Enable TC interrupts */ 06283 I2C_Enable_IRQ(hi2c, I2C_XFER_RELOAD_IT); 06284 } 06285 } 06286 } 06287 06288 /** 06289 * @brief DMA I2C slave receive process complete callback. 06290 * @param hdma DMA handle 06291 * @retval None 06292 */ 06293 static void I2C_DMASlaveReceiveCplt(DMA_HandleTypeDef *hdma) 06294 { 06295 /* Derogation MISRAC2012-Rule-11.5 */ 06296 I2C_HandleTypeDef *hi2c = (I2C_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent); 06297 uint32_t tmpoptions = hi2c->XferOptions; 06298 06299 if ((I2C_GET_DMA_REMAIN_DATA(hi2c->hdmarx) == 0U) && \ 06300 (tmpoptions != I2C_NO_OPTION_FRAME)) 06301 { 06302 /* Disable DMA Request */ 06303 hi2c->Instance->CR1 &= ~I2C_CR1_RXDMAEN; 06304 06305 /* Call I2C Slave Sequential complete process */ 06306 I2C_ITSlaveSeqCplt(hi2c); 06307 } 06308 else 06309 { 06310 /* No specific action, Master fully manage the generation of STOP condition */ 06311 /* Mean that this generation can arrive at any time, at the end or during DMA process */ 06312 /* So STOP condition should be manage through Interrupt treatment */ 06313 } 06314 } 06315 06316 /** 06317 * @brief DMA I2C communication error callback. 06318 * @param hdma DMA handle 06319 * @retval None 06320 */ 06321 static void I2C_DMAError(DMA_HandleTypeDef *hdma) 06322 { 06323 uint32_t treatdmaerror = 0U; 06324 /* Derogation MISRAC2012-Rule-11.5 */ 06325 I2C_HandleTypeDef *hi2c = (I2C_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent); 06326 06327 if (hi2c->hdmatx != NULL) 06328 { 06329 if (I2C_GET_DMA_REMAIN_DATA(hi2c->hdmatx) == 0U) 06330 { 06331 treatdmaerror = 1U; 06332 } 06333 } 06334 06335 if (hi2c->hdmarx != NULL) 06336 { 06337 if (I2C_GET_DMA_REMAIN_DATA(hi2c->hdmarx) == 0U) 06338 { 06339 treatdmaerror = 1U; 06340 } 06341 } 06342 06343 /* Check if a FIFO error is detected, if true normal use case, so no specific action to perform */ 06344 if (!((HAL_DMA_GetError(hdma) == HAL_DMA_ERROR_FE)) && (treatdmaerror != 0U)) 06345 { 06346 /* Disable Acknowledge */ 06347 hi2c->Instance->CR2 |= I2C_CR2_NACK; 06348 06349 /* Call the corresponding callback to inform upper layer of End of Transfer */ 06350 I2C_ITError(hi2c, HAL_I2C_ERROR_DMA); 06351 } 06352 } 06353 06354 /** 06355 * @brief DMA I2C communication abort callback 06356 * (To be called at end of DMA Abort procedure). 06357 * @param hdma DMA handle. 06358 * @retval None 06359 */ 06360 static void I2C_DMAAbort(DMA_HandleTypeDef *hdma) 06361 { 06362 /* Derogation MISRAC2012-Rule-11.5 */ 06363 I2C_HandleTypeDef *hi2c = (I2C_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent); 06364 06365 /* Reset AbortCpltCallback */ 06366 if (hi2c->hdmatx != NULL) 06367 { 06368 hi2c->hdmatx->XferAbortCallback = NULL; 06369 } 06370 if (hi2c->hdmarx != NULL) 06371 { 06372 hi2c->hdmarx->XferAbortCallback = NULL; 06373 } 06374 06375 I2C_TreatErrorCallback(hi2c); 06376 } 06377 06378 /** 06379 * @brief This function handles I2C Communication Timeout. It waits 06380 * until a flag is no longer in the specified status. 06381 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains 06382 * the configuration information for the specified I2C. 06383 * @param Flag Specifies the I2C flag to check. 06384 * @param Status The actual Flag status (SET or RESET). 06385 * @param Timeout Timeout duration 06386 * @param Tickstart Tick start value 06387 * @retval HAL status 06388 */ 06389 static HAL_StatusTypeDef I2C_WaitOnFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, FlagStatus Status, 06390 uint32_t Timeout, uint32_t Tickstart) 06391 { 06392 while (__HAL_I2C_GET_FLAG(hi2c, Flag) == Status) 06393 { 06394 /* Check for the Timeout */ 06395 if (Timeout != HAL_MAX_DELAY) 06396 { 06397 if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U)) 06398 { 06399 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT; 06400 hi2c->State = HAL_I2C_STATE_READY; 06401 hi2c->Mode = HAL_I2C_MODE_NONE; 06402 06403 /* Process Unlocked */ 06404 __HAL_UNLOCK(hi2c); 06405 return HAL_ERROR; 06406 } 06407 } 06408 } 06409 return HAL_OK; 06410 } 06411 06412 /** 06413 * @brief This function handles I2C Communication Timeout for specific usage of TXIS flag. 06414 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains 06415 * the configuration information for the specified I2C. 06416 * @param Timeout Timeout duration 06417 * @param Tickstart Tick start value 06418 * @retval HAL status 06419 */ 06420 static HAL_StatusTypeDef I2C_WaitOnTXISFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, 06421 uint32_t Tickstart) 06422 { 06423 while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXIS) == RESET) 06424 { 06425 /* Check if an error is detected */ 06426 if (I2C_IsErrorOccurred(hi2c, Timeout, Tickstart) != HAL_OK) 06427 { 06428 return HAL_ERROR; 06429 } 06430 06431 /* Check for the Timeout */ 06432 if (Timeout != HAL_MAX_DELAY) 06433 { 06434 if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U)) 06435 { 06436 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT; 06437 hi2c->State = HAL_I2C_STATE_READY; 06438 hi2c->Mode = HAL_I2C_MODE_NONE; 06439 06440 /* Process Unlocked */ 06441 __HAL_UNLOCK(hi2c); 06442 06443 return HAL_ERROR; 06444 } 06445 } 06446 } 06447 return HAL_OK; 06448 } 06449 06450 /** 06451 * @brief This function handles I2C Communication Timeout for specific usage of STOP flag. 06452 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains 06453 * the configuration information for the specified I2C. 06454 * @param Timeout Timeout duration 06455 * @param Tickstart Tick start value 06456 * @retval HAL status 06457 */ 06458 static HAL_StatusTypeDef I2C_WaitOnSTOPFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, 06459 uint32_t Tickstart) 06460 { 06461 while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == RESET) 06462 { 06463 /* Check if an error is detected */ 06464 if (I2C_IsErrorOccurred(hi2c, Timeout, Tickstart) != HAL_OK) 06465 { 06466 return HAL_ERROR; 06467 } 06468 06469 /* Check for the Timeout */ 06470 if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U)) 06471 { 06472 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT; 06473 hi2c->State = HAL_I2C_STATE_READY; 06474 hi2c->Mode = HAL_I2C_MODE_NONE; 06475 06476 /* Process Unlocked */ 06477 __HAL_UNLOCK(hi2c); 06478 06479 return HAL_ERROR; 06480 } 06481 } 06482 return HAL_OK; 06483 } 06484 06485 /** 06486 * @brief This function handles I2C Communication Timeout for specific usage of RXNE flag. 06487 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains 06488 * the configuration information for the specified I2C. 06489 * @param Timeout Timeout duration 06490 * @param Tickstart Tick start value 06491 * @retval HAL status 06492 */ 06493 static HAL_StatusTypeDef I2C_WaitOnRXNEFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, 06494 uint32_t Tickstart) 06495 { 06496 while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == RESET) 06497 { 06498 /* Check if an error is detected */ 06499 if (I2C_IsErrorOccurred(hi2c, Timeout, Tickstart) != HAL_OK) 06500 { 06501 return HAL_ERROR; 06502 } 06503 06504 /* Check if a STOPF is detected */ 06505 if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET) 06506 { 06507 /* Check if an RXNE is pending */ 06508 /* Store Last receive data if any */ 06509 if ((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET) && (hi2c->XferSize > 0U)) 06510 { 06511 /* Return HAL_OK */ 06512 /* The Reading of data from RXDR will be done in caller function */ 06513 return HAL_OK; 06514 } 06515 else 06516 { 06517 if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET) 06518 { 06519 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF); 06520 hi2c->ErrorCode = HAL_I2C_ERROR_AF; 06521 } 06522 else 06523 { 06524 hi2c->ErrorCode = HAL_I2C_ERROR_NONE; 06525 } 06526 06527 /* Clear STOP Flag */ 06528 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF); 06529 06530 /* Clear Configuration Register 2 */ 06531 I2C_RESET_CR2(hi2c); 06532 06533 hi2c->State = HAL_I2C_STATE_READY; 06534 hi2c->Mode = HAL_I2C_MODE_NONE; 06535 06536 /* Process Unlocked */ 06537 __HAL_UNLOCK(hi2c); 06538 06539 return HAL_ERROR; 06540 } 06541 } 06542 06543 /* Check for the Timeout */ 06544 if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U)) 06545 { 06546 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT; 06547 hi2c->State = HAL_I2C_STATE_READY; 06548 06549 /* Process Unlocked */ 06550 __HAL_UNLOCK(hi2c); 06551 06552 return HAL_ERROR; 06553 } 06554 } 06555 return HAL_OK; 06556 } 06557 06558 /** 06559 * @brief This function handles errors detection during an I2C Communication. 06560 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains 06561 * the configuration information for the specified I2C. 06562 * @param Timeout Timeout duration 06563 * @param Tickstart Tick start value 06564 * @retval HAL status 06565 */ 06566 static HAL_StatusTypeDef I2C_IsErrorOccurred(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart) 06567 { 06568 HAL_StatusTypeDef status = HAL_OK; 06569 uint32_t itflag = hi2c->Instance->ISR; 06570 uint32_t error_code = 0; 06571 uint32_t tickstart = Tickstart; 06572 uint32_t tmp1; 06573 HAL_I2C_ModeTypeDef tmp2; 06574 06575 if (HAL_IS_BIT_SET(itflag, I2C_FLAG_AF)) 06576 { 06577 /* Clear NACKF Flag */ 06578 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF); 06579 06580 /* Wait until STOP Flag is set or timeout occurred */ 06581 /* AutoEnd should be initiate after AF */ 06582 while ((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == RESET) && (status == HAL_OK)) 06583 { 06584 /* Check for the Timeout */ 06585 if (Timeout != HAL_MAX_DELAY) 06586 { 06587 if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U)) 06588 { 06589 tmp1 = (uint32_t)(hi2c->Instance->CR2 & I2C_CR2_STOP); 06590 tmp2 = hi2c->Mode; 06591 06592 /* In case of I2C still busy, try to regenerate a STOP manually */ 06593 if ((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET) && \ 06594 (tmp1 != I2C_CR2_STOP) && \ 06595 (tmp2 != HAL_I2C_MODE_SLAVE)) 06596 { 06597 /* Generate Stop */ 06598 hi2c->Instance->CR2 |= I2C_CR2_STOP; 06599 06600 /* Update Tick with new reference */ 06601 tickstart = HAL_GetTick(); 06602 } 06603 06604 while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == RESET) 06605 { 06606 /* Check for the Timeout */ 06607 if ((HAL_GetTick() - tickstart) > I2C_TIMEOUT_STOPF) 06608 { 06609 hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT; 06610 hi2c->State = HAL_I2C_STATE_READY; 06611 hi2c->Mode = HAL_I2C_MODE_NONE; 06612 06613 /* Process Unlocked */ 06614 __HAL_UNLOCK(hi2c); 06615 06616 status = HAL_ERROR; 06617 } 06618 } 06619 } 06620 } 06621 } 06622 06623 /* In case STOP Flag is detected, clear it */ 06624 if (status == HAL_OK) 06625 { 06626 /* Clear STOP Flag */ 06627 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF); 06628 } 06629 06630 error_code |= HAL_I2C_ERROR_AF; 06631 06632 status = HAL_ERROR; 06633 } 06634 06635 /* Refresh Content of Status register */ 06636 itflag = hi2c->Instance->ISR; 06637 06638 /* Then verify if an additional errors occurs */ 06639 /* Check if a Bus error occurred */ 06640 if (HAL_IS_BIT_SET(itflag, I2C_FLAG_BERR)) 06641 { 06642 error_code |= HAL_I2C_ERROR_BERR; 06643 06644 /* Clear BERR flag */ 06645 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_BERR); 06646 06647 status = HAL_ERROR; 06648 } 06649 06650 /* Check if an Over-Run/Under-Run error occurred */ 06651 if (HAL_IS_BIT_SET(itflag, I2C_FLAG_OVR)) 06652 { 06653 error_code |= HAL_I2C_ERROR_OVR; 06654 06655 /* Clear OVR flag */ 06656 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_OVR); 06657 06658 status = HAL_ERROR; 06659 } 06660 06661 /* Check if an Arbitration Loss error occurred */ 06662 if (HAL_IS_BIT_SET(itflag, I2C_FLAG_ARLO)) 06663 { 06664 error_code |= HAL_I2C_ERROR_ARLO; 06665 06666 /* Clear ARLO flag */ 06667 __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ARLO); 06668 06669 status = HAL_ERROR; 06670 } 06671 06672 if (status != HAL_OK) 06673 { 06674 /* Flush TX register */ 06675 I2C_Flush_TXDR(hi2c); 06676 06677 /* Clear Configuration Register 2 */ 06678 I2C_RESET_CR2(hi2c); 06679 06680 hi2c->ErrorCode |= error_code; 06681 hi2c->State = HAL_I2C_STATE_READY; 06682 hi2c->Mode = HAL_I2C_MODE_NONE; 06683 06684 /* Process Unlocked */ 06685 __HAL_UNLOCK(hi2c); 06686 } 06687 06688 return status; 06689 } 06690 06691 /** 06692 * @brief Handles I2Cx communication when starting transfer or during transfer (TC or TCR flag are set). 06693 * @param hi2c I2C handle. 06694 * @param DevAddress Specifies the slave address to be programmed. 06695 * @param Size Specifies the number of bytes to be programmed. 06696 * This parameter must be a value between 0 and 255. 06697 * @param Mode New state of the I2C START condition generation. 06698 * This parameter can be one of the following values: 06699 * @arg @ref I2C_RELOAD_MODE Enable Reload mode . 06700 * @arg @ref I2C_AUTOEND_MODE Enable Automatic end mode. 06701 * @arg @ref I2C_SOFTEND_MODE Enable Software end mode. 06702 * @param Request New state of the I2C START condition generation. 06703 * This parameter can be one of the following values: 06704 * @arg @ref I2C_NO_STARTSTOP Don't Generate stop and start condition. 06705 * @arg @ref I2C_GENERATE_STOP Generate stop condition (Size should be set to 0). 06706 * @arg @ref I2C_GENERATE_START_READ Generate Restart for read request. 06707 * @arg @ref I2C_GENERATE_START_WRITE Generate Restart for write request. 06708 * @retval None 06709 */ 06710 static void I2C_TransferConfig(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t Size, uint32_t Mode, 06711 uint32_t Request) 06712 { 06713 /* Check the parameters */ 06714 assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance)); 06715 assert_param(IS_TRANSFER_MODE(Mode)); 06716 assert_param(IS_TRANSFER_REQUEST(Request)); 06717 06718 /* Declaration of tmp to prevent undefined behavior of volatile usage */ 06719 uint32_t tmp = ((uint32_t)(((uint32_t)DevAddress & I2C_CR2_SADD) | \ 06720 (((uint32_t)Size << I2C_CR2_NBYTES_Pos) & I2C_CR2_NBYTES) | \ 06721 (uint32_t)Mode | (uint32_t)Request) & (~0x80000000U)); 06722 06723 /* update CR2 register */ 06724 MODIFY_REG(hi2c->Instance->CR2, \ 06725 ((I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RELOAD | I2C_CR2_AUTOEND | \ 06726 (I2C_CR2_RD_WRN & (uint32_t)(Request >> (31U - I2C_CR2_RD_WRN_Pos))) | \ 06727 I2C_CR2_START | I2C_CR2_STOP)), tmp); 06728 } 06729 06730 /** 06731 * @brief Manage the enabling of Interrupts. 06732 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains 06733 * the configuration information for the specified I2C. 06734 * @param InterruptRequest Value of @ref I2C_Interrupt_configuration_definition. 06735 * @retval None 06736 */ 06737 static void I2C_Enable_IRQ(I2C_HandleTypeDef *hi2c, uint16_t InterruptRequest) 06738 { 06739 uint32_t tmpisr = 0U; 06740 06741 if ((hi2c->XferISR == I2C_Master_ISR_DMA) || \ 06742 (hi2c->XferISR == I2C_Slave_ISR_DMA)) 06743 { 06744 if ((InterruptRequest & I2C_XFER_LISTEN_IT) == I2C_XFER_LISTEN_IT) 06745 { 06746 /* Enable ERR, STOP, NACK and ADDR interrupts */ 06747 tmpisr |= I2C_IT_ADDRI | I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_ERRI; 06748 } 06749 06750 if (InterruptRequest == I2C_XFER_ERROR_IT) 06751 { 06752 /* Enable ERR and NACK interrupts */ 06753 tmpisr |= I2C_IT_ERRI | I2C_IT_NACKI; 06754 } 06755 06756 if (InterruptRequest == I2C_XFER_CPLT_IT) 06757 { 06758 /* Enable STOP interrupts */ 06759 tmpisr |= (I2C_IT_STOPI | I2C_IT_TCI); 06760 } 06761 06762 if (InterruptRequest == I2C_XFER_RELOAD_IT) 06763 { 06764 /* Enable TC interrupts */ 06765 tmpisr |= I2C_IT_TCI; 06766 } 06767 } 06768 else 06769 { 06770 if ((InterruptRequest & I2C_XFER_LISTEN_IT) == I2C_XFER_LISTEN_IT) 06771 { 06772 /* Enable ERR, STOP, NACK, and ADDR interrupts */ 06773 tmpisr |= I2C_IT_ADDRI | I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_ERRI; 06774 } 06775 06776 if ((InterruptRequest & I2C_XFER_TX_IT) == I2C_XFER_TX_IT) 06777 { 06778 /* Enable ERR, TC, STOP, NACK and RXI interrupts */ 06779 tmpisr |= I2C_IT_ERRI | I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_TXI; 06780 } 06781 06782 if ((InterruptRequest & I2C_XFER_RX_IT) == I2C_XFER_RX_IT) 06783 { 06784 /* Enable ERR, TC, STOP, NACK and TXI interrupts */ 06785 tmpisr |= I2C_IT_ERRI | I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_RXI; 06786 } 06787 06788 if (InterruptRequest == I2C_XFER_CPLT_IT) 06789 { 06790 /* Enable STOP interrupts */ 06791 tmpisr |= I2C_IT_STOPI; 06792 } 06793 } 06794 06795 /* Enable interrupts only at the end */ 06796 /* to avoid the risk of I2C interrupt handle execution before */ 06797 /* all interrupts requested done */ 06798 __HAL_I2C_ENABLE_IT(hi2c, tmpisr); 06799 } 06800 06801 /** 06802 * @brief Manage the disabling of Interrupts. 06803 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains 06804 * the configuration information for the specified I2C. 06805 * @param InterruptRequest Value of @ref I2C_Interrupt_configuration_definition. 06806 * @retval None 06807 */ 06808 static void I2C_Disable_IRQ(I2C_HandleTypeDef *hi2c, uint16_t InterruptRequest) 06809 { 06810 uint32_t tmpisr = 0U; 06811 06812 if ((InterruptRequest & I2C_XFER_TX_IT) == I2C_XFER_TX_IT) 06813 { 06814 /* Disable TC and TXI interrupts */ 06815 tmpisr |= I2C_IT_TCI | I2C_IT_TXI; 06816 06817 if (((uint32_t)hi2c->State & (uint32_t)HAL_I2C_STATE_LISTEN) != (uint32_t)HAL_I2C_STATE_LISTEN) 06818 { 06819 /* Disable NACK and STOP interrupts */ 06820 tmpisr |= I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_ERRI; 06821 } 06822 } 06823 06824 if ((InterruptRequest & I2C_XFER_RX_IT) == I2C_XFER_RX_IT) 06825 { 06826 /* Disable TC and RXI interrupts */ 06827 tmpisr |= I2C_IT_TCI | I2C_IT_RXI; 06828 06829 if (((uint32_t)hi2c->State & (uint32_t)HAL_I2C_STATE_LISTEN) != (uint32_t)HAL_I2C_STATE_LISTEN) 06830 { 06831 /* Disable NACK and STOP interrupts */ 06832 tmpisr |= I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_ERRI; 06833 } 06834 } 06835 06836 if ((InterruptRequest & I2C_XFER_LISTEN_IT) == I2C_XFER_LISTEN_IT) 06837 { 06838 /* Disable ADDR, NACK and STOP interrupts */ 06839 tmpisr |= I2C_IT_ADDRI | I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_ERRI; 06840 } 06841 06842 if (InterruptRequest == I2C_XFER_ERROR_IT) 06843 { 06844 /* Enable ERR and NACK interrupts */ 06845 tmpisr |= I2C_IT_ERRI | I2C_IT_NACKI; 06846 } 06847 06848 if (InterruptRequest == I2C_XFER_CPLT_IT) 06849 { 06850 /* Enable STOP interrupts */ 06851 tmpisr |= I2C_IT_STOPI; 06852 } 06853 06854 if (InterruptRequest == I2C_XFER_RELOAD_IT) 06855 { 06856 /* Enable TC interrupts */ 06857 tmpisr |= I2C_IT_TCI; 06858 } 06859 06860 /* Disable interrupts only at the end */ 06861 /* to avoid a breaking situation like at "t" time */ 06862 /* all disable interrupts request are not done */ 06863 __HAL_I2C_DISABLE_IT(hi2c, tmpisr); 06864 } 06865 06866 /** 06867 * @brief Convert I2Cx OTHER_xxx XferOptions to functional XferOptions. 06868 * @param hi2c I2C handle. 06869 * @retval None 06870 */ 06871 static void I2C_ConvertOtherXferOptions(I2C_HandleTypeDef *hi2c) 06872 { 06873 /* if user set XferOptions to I2C_OTHER_FRAME */ 06874 /* it request implicitly to generate a restart condition */ 06875 /* set XferOptions to I2C_FIRST_FRAME */ 06876 if (hi2c->XferOptions == I2C_OTHER_FRAME) 06877 { 06878 hi2c->XferOptions = I2C_FIRST_FRAME; 06879 } 06880 /* else if user set XferOptions to I2C_OTHER_AND_LAST_FRAME */ 06881 /* it request implicitly to generate a restart condition */ 06882 /* then generate a stop condition at the end of transfer */ 06883 /* set XferOptions to I2C_FIRST_AND_LAST_FRAME */ 06884 else if (hi2c->XferOptions == I2C_OTHER_AND_LAST_FRAME) 06885 { 06886 hi2c->XferOptions = I2C_FIRST_AND_LAST_FRAME; 06887 } 06888 else 06889 { 06890 /* Nothing to do */ 06891 } 06892 } 06893 06894 /** 06895 * @} 06896 */ 06897 06898 #endif /* HAL_I2C_MODULE_ENABLED */ 06899 /** 06900 * @} 06901 */ 06902 06903 /** 06904 * @} 06905 */