Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 

7568 рядки
236 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_i2c.c
  4. * @author MCD Application Team
  5. * @brief I2C HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the Inter Integrated Circuit (I2C) peripheral:
  8. * + Initialization and de-initialization functions
  9. * + IO operation functions
  10. * + Peripheral State, Mode and Error functions
  11. *
  12. ******************************************************************************
  13. * @attention
  14. *
  15. * Copyright (c) 2016 STMicroelectronics.
  16. * All rights reserved.
  17. *
  18. * This software is licensed under terms that can be found in the LICENSE file
  19. * in the root directory of this software component.
  20. * If no LICENSE file comes with this software, it is provided AS-IS.
  21. *
  22. ******************************************************************************
  23. @verbatim
  24. ==============================================================================
  25. ##### How to use this driver #####
  26. ==============================================================================
  27. [..]
  28. The I2C HAL driver can be used as follows:
  29. (#) Declare a I2C_HandleTypeDef handle structure, for example:
  30. I2C_HandleTypeDef hi2c;
  31. (#)Initialize the I2C low level resources by implementing the HAL_I2C_MspInit() API:
  32. (##) Enable the I2Cx interface clock
  33. (##) I2C pins configuration
  34. (+++) Enable the clock for the I2C GPIOs
  35. (+++) Configure I2C pins as alternate function open-drain
  36. (##) NVIC configuration if you need to use interrupt process
  37. (+++) Configure the I2Cx interrupt priority
  38. (+++) Enable the NVIC I2C IRQ Channel
  39. (##) DMA Configuration if you need to use DMA process
  40. (+++) Declare a DMA_HandleTypeDef handle structure for the transmit or receive stream
  41. (+++) Enable the DMAx interface clock using
  42. (+++) Configure the DMA handle parameters
  43. (+++) Configure the DMA Tx or Rx stream
  44. (+++) Associate the initialized DMA handle to the hi2c DMA Tx or Rx handle
  45. (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on
  46. the DMA Tx or Rx stream
  47. (#) Configure the Communication Speed, Duty cycle, Addressing mode, Own Address1,
  48. Dual Addressing mode, Own Address2, General call and Nostretch mode in the hi2c Init structure.
  49. (#) Initialize the I2C registers by calling the HAL_I2C_Init(), configures also the low level Hardware
  50. (GPIO, CLOCK, NVIC...etc) by calling the customized HAL_I2C_MspInit() API.
  51. (#) To check if target device is ready for communication, use the function HAL_I2C_IsDeviceReady()
  52. (#) For I2C IO and IO MEM operations, three operation modes are available within this driver :
  53. *** Polling mode IO operation ***
  54. =================================
  55. [..]
  56. (+) Transmit in master mode an amount of data in blocking mode using HAL_I2C_Master_Transmit()
  57. (+) Receive in master mode an amount of data in blocking mode using HAL_I2C_Master_Receive()
  58. (+) Transmit in slave mode an amount of data in blocking mode using HAL_I2C_Slave_Transmit()
  59. (+) Receive in slave mode an amount of data in blocking mode using HAL_I2C_Slave_Receive()
  60. *** Polling mode IO MEM operation ***
  61. =====================================
  62. [..]
  63. (+) Write an amount of data in blocking mode to a specific memory address using HAL_I2C_Mem_Write()
  64. (+) Read an amount of data in blocking mode from a specific memory address using HAL_I2C_Mem_Read()
  65. *** Interrupt mode IO operation ***
  66. ===================================
  67. [..]
  68. (+) Transmit in master mode an amount of data in non-blocking mode using HAL_I2C_Master_Transmit_IT()
  69. (+) At transmission end of transfer, HAL_I2C_MasterTxCpltCallback() is executed and user can
  70. add his own code by customization of function pointer HAL_I2C_MasterTxCpltCallback()
  71. (+) Receive in master mode an amount of data in non-blocking mode using HAL_I2C_Master_Receive_IT()
  72. (+) At reception end of transfer, HAL_I2C_MasterRxCpltCallback() is executed and user can
  73. add his own code by customization of function pointer HAL_I2C_MasterRxCpltCallback()
  74. (+) Transmit in slave mode an amount of data in non-blocking mode using HAL_I2C_Slave_Transmit_IT()
  75. (+) At transmission end of transfer, HAL_I2C_SlaveTxCpltCallback() is executed and user can
  76. add his own code by customization of function pointer HAL_I2C_SlaveTxCpltCallback()
  77. (+) Receive in slave mode an amount of data in non-blocking mode using HAL_I2C_Slave_Receive_IT()
  78. (+) At reception end of transfer, HAL_I2C_SlaveRxCpltCallback() is executed and user can
  79. add his own code by customization of function pointer HAL_I2C_SlaveRxCpltCallback()
  80. (+) In case of transfer Error, HAL_I2C_ErrorCallback() function is executed and user can
  81. add his own code by customization of function pointer HAL_I2C_ErrorCallback()
  82. (+) Abort a master or memory I2C process communication with Interrupt using HAL_I2C_Master_Abort_IT()
  83. (+) End of abort process, HAL_I2C_AbortCpltCallback() is executed and user can
  84. add his own code by customization of function pointer HAL_I2C_AbortCpltCallback()
  85. *** Interrupt mode or DMA mode IO sequential operation ***
  86. ==========================================================
  87. [..]
  88. (@) These interfaces allow to manage a sequential transfer with a repeated start condition
  89. when a direction change during transfer
  90. [..]
  91. (+) A specific option field manage the different steps of a sequential transfer
  92. (+) Option field values are defined through I2C_XferOptions_definition and are listed below:
  93. (++) I2C_FIRST_AND_LAST_FRAME: No sequential usage, functional is same as associated interfaces in no sequential mode
  94. (++) I2C_FIRST_FRAME: Sequential usage, this option allow to manage a sequence with start condition, address
  95. and data to transfer without a final stop condition
  96. (++) I2C_FIRST_AND_NEXT_FRAME: Sequential usage (Master only), this option allow to manage a sequence with start condition, address
  97. and data to transfer without a final stop condition, an then permit a call the same master sequential interface
  98. several times (like HAL_I2C_Master_Seq_Transmit_IT() then HAL_I2C_Master_Seq_Transmit_IT()
  99. or HAL_I2C_Master_Seq_Transmit_DMA() then HAL_I2C_Master_Seq_Transmit_DMA())
  100. (++) I2C_NEXT_FRAME: Sequential usage, this option allow to manage a sequence with a restart condition, address
  101. and with new data to transfer if the direction change or manage only the new data to transfer
  102. if no direction change and without a final stop condition in both cases
  103. (++) I2C_LAST_FRAME: Sequential usage, this option allow to manage a sequance with a restart condition, address
  104. and with new data to transfer if the direction change or manage only the new data to transfer
  105. if no direction change and with a final stop condition in both cases
  106. (++) I2C_LAST_FRAME_NO_STOP: Sequential usage (Master only), this option allow to manage a restart condition after several call of the same master sequential
  107. interface several times (link with option I2C_FIRST_AND_NEXT_FRAME).
  108. Usage can, transfer several bytes one by one using HAL_I2C_Master_Seq_Transmit_IT(option I2C_FIRST_AND_NEXT_FRAME then I2C_NEXT_FRAME)
  109. or HAL_I2C_Master_Seq_Receive_IT(option I2C_FIRST_AND_NEXT_FRAME then I2C_NEXT_FRAME)
  110. or HAL_I2C_Master_Seq_Transmit_DMA(option I2C_FIRST_AND_NEXT_FRAME then I2C_NEXT_FRAME)
  111. or HAL_I2C_Master_Seq_Receive_DMA(option I2C_FIRST_AND_NEXT_FRAME then I2C_NEXT_FRAME).
  112. Then usage of this option I2C_LAST_FRAME_NO_STOP at the last Transmit or Receive sequence permit to call the opposite interface Receive or Transmit
  113. without stopping the communication and so generate a restart condition.
  114. (++) I2C_OTHER_FRAME: Sequential usage (Master only), this option allow to manage a restart condition after each call of the same master sequential
  115. interface.
  116. Usage can, transfer several bytes one by one with a restart with slave address between each bytes using HAL_I2C_Master_Seq_Transmit_IT(option I2C_FIRST_FRAME then I2C_OTHER_FRAME)
  117. or HAL_I2C_Master_Seq_Receive_IT(option I2C_FIRST_FRAME then I2C_OTHER_FRAME)
  118. or HAL_I2C_Master_Seq_Transmit_DMA(option I2C_FIRST_FRAME then I2C_OTHER_FRAME)
  119. or HAL_I2C_Master_Seq_Receive_DMA(option I2C_FIRST_FRAME then I2C_OTHER_FRAME).
  120. Then usage of this option I2C_OTHER_AND_LAST_FRAME at the last frame to help automatic generation of STOP condition.
  121. (+) Different sequential I2C interfaces are listed below:
  122. (++) Sequential transmit in master I2C mode an amount of data in non-blocking mode using HAL_I2C_Master_Seq_Transmit_IT()
  123. or using HAL_I2C_Master_Seq_Transmit_DMA()
  124. (+++) At transmission end of current frame transfer, HAL_I2C_MasterTxCpltCallback() is executed and user can
  125. add his own code by customization of function pointer HAL_I2C_MasterTxCpltCallback()
  126. (++) Sequential receive in master I2C mode an amount of data in non-blocking mode using HAL_I2C_Master_Seq_Receive_IT()
  127. or using HAL_I2C_Master_Seq_Receive_DMA()
  128. (+++) At reception end of current frame transfer, HAL_I2C_MasterRxCpltCallback() is executed and user can
  129. add his own code by customization of function pointer HAL_I2C_MasterRxCpltCallback()
  130. (++) Abort a master or memory IT or DMA I2C process communication with Interrupt using HAL_I2C_Master_Abort_IT()
  131. (+++) End of abort process, HAL_I2C_AbortCpltCallback() is executed and user can
  132. add his own code by customization of function pointer HAL_I2C_AbortCpltCallback()
  133. (++) Enable/disable the Address listen mode in slave I2C mode using HAL_I2C_EnableListen_IT() HAL_I2C_DisableListen_IT()
  134. (+++) When address slave I2C match, HAL_I2C_AddrCallback() is executed and user can
  135. add his own code to check the Address Match Code and the transmission direction request by master (Write/Read).
  136. (+++) At Listen mode end HAL_I2C_ListenCpltCallback() is executed and user can
  137. add his own code by customization of function pointer HAL_I2C_ListenCpltCallback()
  138. (++) Sequential transmit in slave I2C mode an amount of data in non-blocking mode using HAL_I2C_Slave_Seq_Transmit_IT()
  139. or using HAL_I2C_Slave_Seq_Transmit_DMA()
  140. (+++) At transmission end of current frame transfer, HAL_I2C_SlaveTxCpltCallback() is executed and user can
  141. add his own code by customization of function pointer HAL_I2C_SlaveTxCpltCallback()
  142. (++) Sequential receive in slave I2C mode an amount of data in non-blocking mode using HAL_I2C_Slave_Seq_Receive_IT()
  143. or using HAL_I2C_Slave_Seq_Receive_DMA()
  144. (+++) At reception end of current frame transfer, HAL_I2C_SlaveRxCpltCallback() is executed and user can
  145. add his own code by customization of function pointer HAL_I2C_SlaveRxCpltCallback()
  146. (++) In case of transfer Error, HAL_I2C_ErrorCallback() function is executed and user can
  147. add his own code by customization of function pointer HAL_I2C_ErrorCallback()
  148. *** Interrupt mode IO MEM operation ***
  149. =======================================
  150. [..]
  151. (+) Write an amount of data in non-blocking mode with Interrupt to a specific memory address using
  152. HAL_I2C_Mem_Write_IT()
  153. (+) At Memory end of write transfer, HAL_I2C_MemTxCpltCallback() is executed and user can
  154. add his own code by customization of function pointer HAL_I2C_MemTxCpltCallback()
  155. (+) Read an amount of data in non-blocking mode with Interrupt from a specific memory address using
  156. HAL_I2C_Mem_Read_IT()
  157. (+) At Memory end of read transfer, HAL_I2C_MemRxCpltCallback() is executed and user can
  158. add his own code by customization of function pointer HAL_I2C_MemRxCpltCallback()
  159. (+) In case of transfer Error, HAL_I2C_ErrorCallback() function is executed and user can
  160. add his own code by customization of function pointer HAL_I2C_ErrorCallback()
  161. *** DMA mode IO operation ***
  162. ==============================
  163. [..]
  164. (+) Transmit in master mode an amount of data in non-blocking mode (DMA) using
  165. HAL_I2C_Master_Transmit_DMA()
  166. (+) At transmission end of transfer, HAL_I2C_MasterTxCpltCallback() is executed and user can
  167. add his own code by customization of function pointer HAL_I2C_MasterTxCpltCallback()
  168. (+) Receive in master mode an amount of data in non-blocking mode (DMA) using
  169. HAL_I2C_Master_Receive_DMA()
  170. (+) At reception end of transfer, HAL_I2C_MasterRxCpltCallback() is executed and user can
  171. add his own code by customization of function pointer HAL_I2C_MasterRxCpltCallback()
  172. (+) Transmit in slave mode an amount of data in non-blocking mode (DMA) using
  173. HAL_I2C_Slave_Transmit_DMA()
  174. (+) At transmission end of transfer, HAL_I2C_SlaveTxCpltCallback() is executed and user can
  175. add his own code by customization of function pointer HAL_I2C_SlaveTxCpltCallback()
  176. (+) Receive in slave mode an amount of data in non-blocking mode (DMA) using
  177. HAL_I2C_Slave_Receive_DMA()
  178. (+) At reception end of transfer, HAL_I2C_SlaveRxCpltCallback() is executed and user can
  179. add his own code by customization of function pointer HAL_I2C_SlaveRxCpltCallback()
  180. (+) In case of transfer Error, HAL_I2C_ErrorCallback() function is executed and user can
  181. add his own code by customization of function pointer HAL_I2C_ErrorCallback()
  182. (+) Abort a master or memory I2C process communication with Interrupt using HAL_I2C_Master_Abort_IT()
  183. (+) End of abort process, HAL_I2C_AbortCpltCallback() is executed and user can
  184. add his own code by customization of function pointer HAL_I2C_AbortCpltCallback()
  185. *** DMA mode IO MEM operation ***
  186. =================================
  187. [..]
  188. (+) Write an amount of data in non-blocking mode with DMA to a specific memory address using
  189. HAL_I2C_Mem_Write_DMA()
  190. (+) At Memory end of write transfer, HAL_I2C_MemTxCpltCallback() is executed and user can
  191. add his own code by customization of function pointer HAL_I2C_MemTxCpltCallback()
  192. (+) Read an amount of data in non-blocking mode with DMA from a specific memory address using
  193. HAL_I2C_Mem_Read_DMA()
  194. (+) At Memory end of read transfer, HAL_I2C_MemRxCpltCallback() is executed and user can
  195. add his own code by customization of function pointer HAL_I2C_MemRxCpltCallback()
  196. (+) In case of transfer Error, HAL_I2C_ErrorCallback() function is executed and user can
  197. add his own code by customization of function pointer HAL_I2C_ErrorCallback()
  198. *** I2C HAL driver macros list ***
  199. ==================================
  200. [..]
  201. Below the list of most used macros in I2C HAL driver.
  202. (+) __HAL_I2C_ENABLE: Enable the I2C peripheral
  203. (+) __HAL_I2C_DISABLE: Disable the I2C peripheral
  204. (+) __HAL_I2C_GET_FLAG: Checks whether the specified I2C flag is set or not
  205. (+) __HAL_I2C_CLEAR_FLAG: Clear the specified I2C pending flag
  206. (+) __HAL_I2C_ENABLE_IT: Enable the specified I2C interrupt
  207. (+) __HAL_I2C_DISABLE_IT: Disable the specified I2C interrupt
  208. *** Callback registration ***
  209. =============================================
  210. [..]
  211. The compilation flag USE_HAL_I2C_REGISTER_CALLBACKS when set to 1
  212. allows the user to configure dynamically the driver callbacks.
  213. Use Functions HAL_I2C_RegisterCallback() or HAL_I2C_RegisterAddrCallback()
  214. to register an interrupt callback.
  215. [..]
  216. Function HAL_I2C_RegisterCallback() allows to register following callbacks:
  217. (+) MasterTxCpltCallback : callback for Master transmission end of transfer.
  218. (+) MasterRxCpltCallback : callback for Master reception end of transfer.
  219. (+) SlaveTxCpltCallback : callback for Slave transmission end of transfer.
  220. (+) SlaveRxCpltCallback : callback for Slave reception end of transfer.
  221. (+) ListenCpltCallback : callback for end of listen mode.
  222. (+) MemTxCpltCallback : callback for Memory transmission end of transfer.
  223. (+) MemRxCpltCallback : callback for Memory reception end of transfer.
  224. (+) ErrorCallback : callback for error detection.
  225. (+) AbortCpltCallback : callback for abort completion process.
  226. (+) MspInitCallback : callback for Msp Init.
  227. (+) MspDeInitCallback : callback for Msp DeInit.
  228. This function takes as parameters the HAL peripheral handle, the Callback ID
  229. and a pointer to the user callback function.
  230. [..]
  231. For specific callback AddrCallback use dedicated register callbacks : HAL_I2C_RegisterAddrCallback().
  232. [..]
  233. Use function HAL_I2C_UnRegisterCallback to reset a callback to the default
  234. weak function.
  235. HAL_I2C_UnRegisterCallback takes as parameters the HAL peripheral handle,
  236. and the Callback ID.
  237. This function allows to reset following callbacks:
  238. (+) MasterTxCpltCallback : callback for Master transmission end of transfer.
  239. (+) MasterRxCpltCallback : callback for Master reception end of transfer.
  240. (+) SlaveTxCpltCallback : callback for Slave transmission end of transfer.
  241. (+) SlaveRxCpltCallback : callback for Slave reception end of transfer.
  242. (+) ListenCpltCallback : callback for end of listen mode.
  243. (+) MemTxCpltCallback : callback for Memory transmission end of transfer.
  244. (+) MemRxCpltCallback : callback for Memory reception end of transfer.
  245. (+) ErrorCallback : callback for error detection.
  246. (+) AbortCpltCallback : callback for abort completion process.
  247. (+) MspInitCallback : callback for Msp Init.
  248. (+) MspDeInitCallback : callback for Msp DeInit.
  249. [..]
  250. For callback AddrCallback use dedicated register callbacks : HAL_I2C_UnRegisterAddrCallback().
  251. [..]
  252. By default, after the HAL_I2C_Init() and when the state is HAL_I2C_STATE_RESET
  253. all callbacks are set to the corresponding weak functions:
  254. examples HAL_I2C_MasterTxCpltCallback(), HAL_I2C_MasterRxCpltCallback().
  255. Exception done for MspInit and MspDeInit functions that are
  256. reset to the legacy weak functions in the HAL_I2C_Init()/ HAL_I2C_DeInit() only when
  257. these callbacks are null (not registered beforehand).
  258. If MspInit or MspDeInit are not null, the HAL_I2C_Init()/ HAL_I2C_DeInit()
  259. keep and use the user MspInit/MspDeInit callbacks (registered beforehand) whatever the state.
  260. [..]
  261. Callbacks can be registered/unregistered in HAL_I2C_STATE_READY state only.
  262. Exception done MspInit/MspDeInit functions that can be registered/unregistered
  263. in HAL_I2C_STATE_READY or HAL_I2C_STATE_RESET state,
  264. thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
  265. Then, the user first registers the MspInit/MspDeInit user callbacks
  266. using HAL_I2C_RegisterCallback() before calling HAL_I2C_DeInit()
  267. or HAL_I2C_Init() function.
  268. [..]
  269. When the compilation flag USE_HAL_I2C_REGISTER_CALLBACKS is set to 0 or
  270. not defined, the callback registration feature is not available and all callbacks
  271. are set to the corresponding weak functions.
  272. [..]
  273. (@) You can refer to the I2C HAL driver header file for more useful macros
  274. @endverbatim
  275. */
  276. /* Includes ------------------------------------------------------------------*/
  277. #include "stm32f4xx_hal.h"
  278. /** @addtogroup STM32F4xx_HAL_Driver
  279. * @{
  280. */
  281. /** @defgroup I2C I2C
  282. * @brief I2C HAL module driver
  283. * @{
  284. */
  285. #ifdef HAL_I2C_MODULE_ENABLED
  286. /* Private typedef -----------------------------------------------------------*/
  287. /* Private define ------------------------------------------------------------*/
  288. /** @defgroup I2C_Private_Define I2C Private Define
  289. * @{
  290. */
  291. #define I2C_TIMEOUT_FLAG 35U /*!< Timeout 35 ms */
  292. #define I2C_TIMEOUT_BUSY_FLAG 25U /*!< Timeout 25 ms */
  293. #define I2C_TIMEOUT_STOP_FLAG 5U /*!< Timeout 5 ms */
  294. #define I2C_NO_OPTION_FRAME 0xFFFF0000U /*!< XferOptions default value */
  295. /* Private define for @ref PreviousState usage */
  296. #define I2C_STATE_MSK ((uint32_t)((uint32_t)((uint32_t)HAL_I2C_STATE_BUSY_TX | (uint32_t)HAL_I2C_STATE_BUSY_RX) & (uint32_t)(~((uint32_t)HAL_I2C_STATE_READY)))) /*!< Mask State define, keep only RX and TX bits */
  297. #define I2C_STATE_NONE ((uint32_t)(HAL_I2C_MODE_NONE)) /*!< Default Value */
  298. #define I2C_STATE_MASTER_BUSY_TX ((uint32_t)(((uint32_t)HAL_I2C_STATE_BUSY_TX & I2C_STATE_MSK) | (uint32_t)HAL_I2C_MODE_MASTER)) /*!< Master Busy TX, combinaison of State LSB and Mode enum */
  299. #define I2C_STATE_MASTER_BUSY_RX ((uint32_t)(((uint32_t)HAL_I2C_STATE_BUSY_RX & I2C_STATE_MSK) | (uint32_t)HAL_I2C_MODE_MASTER)) /*!< Master Busy RX, combinaison of State LSB and Mode enum */
  300. #define I2C_STATE_SLAVE_BUSY_TX ((uint32_t)(((uint32_t)HAL_I2C_STATE_BUSY_TX & I2C_STATE_MSK) | (uint32_t)HAL_I2C_MODE_SLAVE)) /*!< Slave Busy TX, combinaison of State LSB and Mode enum */
  301. #define I2C_STATE_SLAVE_BUSY_RX ((uint32_t)(((uint32_t)HAL_I2C_STATE_BUSY_RX & I2C_STATE_MSK) | (uint32_t)HAL_I2C_MODE_SLAVE)) /*!< Slave Busy RX, combinaison of State LSB and Mode enum */
  302. /**
  303. * @}
  304. */
  305. /* Private macro -------------------------------------------------------------*/
  306. /** @addtogroup I2C_Private_Macros
  307. * @{
  308. */
  309. /* Macro to get remaining data to transfer on DMA side */
  310. #define I2C_GET_DMA_REMAIN_DATA(__HANDLE__) __HAL_DMA_GET_COUNTER(__HANDLE__)
  311. /**
  312. * @}
  313. */
  314. /* Private variables ---------------------------------------------------------*/
  315. /* Private function prototypes -----------------------------------------------*/
  316. /** @defgroup I2C_Private_Functions I2C Private Functions
  317. * @{
  318. */
  319. /* Private functions to handle DMA transfer */
  320. static void I2C_DMAXferCplt(DMA_HandleTypeDef *hdma);
  321. static void I2C_DMAError(DMA_HandleTypeDef *hdma);
  322. static void I2C_DMAAbort(DMA_HandleTypeDef *hdma);
  323. static void I2C_ITError(I2C_HandleTypeDef *hi2c);
  324. static HAL_StatusTypeDef I2C_MasterRequestWrite(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint32_t Timeout, uint32_t Tickstart);
  325. static HAL_StatusTypeDef I2C_MasterRequestRead(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint32_t Timeout, uint32_t Tickstart);
  326. static HAL_StatusTypeDef I2C_RequestMemoryWrite(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout, uint32_t Tickstart);
  327. static HAL_StatusTypeDef I2C_RequestMemoryRead(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout, uint32_t Tickstart);
  328. /* Private functions to handle flags during polling transfer */
  329. static HAL_StatusTypeDef I2C_WaitOnFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, FlagStatus Status, uint32_t Timeout, uint32_t Tickstart);
  330. static HAL_StatusTypeDef I2C_WaitOnMasterAddressFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, uint32_t Timeout, uint32_t Tickstart);
  331. static HAL_StatusTypeDef I2C_WaitOnTXEFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart);
  332. static HAL_StatusTypeDef I2C_WaitOnBTFFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart);
  333. static HAL_StatusTypeDef I2C_WaitOnRXNEFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart);
  334. static HAL_StatusTypeDef I2C_WaitOnSTOPFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart);
  335. static HAL_StatusTypeDef I2C_WaitOnSTOPRequestThroughIT(I2C_HandleTypeDef *hi2c);
  336. static HAL_StatusTypeDef I2C_IsAcknowledgeFailed(I2C_HandleTypeDef *hi2c);
  337. /* Private functions for I2C transfer IRQ handler */
  338. static void I2C_MasterTransmit_TXE(I2C_HandleTypeDef *hi2c);
  339. static void I2C_MasterTransmit_BTF(I2C_HandleTypeDef *hi2c);
  340. static void I2C_MasterReceive_RXNE(I2C_HandleTypeDef *hi2c);
  341. static void I2C_MasterReceive_BTF(I2C_HandleTypeDef *hi2c);
  342. static void I2C_Master_SB(I2C_HandleTypeDef *hi2c);
  343. static void I2C_Master_ADD10(I2C_HandleTypeDef *hi2c);
  344. static void I2C_Master_ADDR(I2C_HandleTypeDef *hi2c);
  345. static void I2C_SlaveTransmit_TXE(I2C_HandleTypeDef *hi2c);
  346. static void I2C_SlaveTransmit_BTF(I2C_HandleTypeDef *hi2c);
  347. static void I2C_SlaveReceive_RXNE(I2C_HandleTypeDef *hi2c);
  348. static void I2C_SlaveReceive_BTF(I2C_HandleTypeDef *hi2c);
  349. static void I2C_Slave_ADDR(I2C_HandleTypeDef *hi2c, uint32_t IT2Flags);
  350. static void I2C_Slave_STOPF(I2C_HandleTypeDef *hi2c);
  351. static void I2C_Slave_AF(I2C_HandleTypeDef *hi2c);
  352. static void I2C_MemoryTransmit_TXE_BTF(I2C_HandleTypeDef *hi2c);
  353. /* Private function to Convert Specific options */
  354. static void I2C_ConvertOtherXferOptions(I2C_HandleTypeDef *hi2c);
  355. /* Private function to flush DR register */
  356. static void I2C_Flush_DR(I2C_HandleTypeDef *hi2c);
  357. /**
  358. * @}
  359. */
  360. /* Exported functions --------------------------------------------------------*/
  361. /** @defgroup I2C_Exported_Functions I2C Exported Functions
  362. * @{
  363. */
  364. /** @defgroup I2C_Exported_Functions_Group1 Initialization and de-initialization functions
  365. * @brief Initialization and Configuration functions
  366. *
  367. @verbatim
  368. ===============================================================================
  369. ##### Initialization and de-initialization functions #####
  370. ===============================================================================
  371. [..] This subsection provides a set of functions allowing to initialize and
  372. deinitialize the I2Cx peripheral:
  373. (+) User must Implement HAL_I2C_MspInit() function in which he configures
  374. all related peripherals resources (CLOCK, GPIO, DMA, IT and NVIC).
  375. (+) Call the function HAL_I2C_Init() to configure the selected device with
  376. the selected configuration:
  377. (++) Communication Speed
  378. (++) Duty cycle
  379. (++) Addressing mode
  380. (++) Own Address 1
  381. (++) Dual Addressing mode
  382. (++) Own Address 2
  383. (++) General call mode
  384. (++) Nostretch mode
  385. (+) Call the function HAL_I2C_DeInit() to restore the default configuration
  386. of the selected I2Cx peripheral.
  387. @endverbatim
  388. * @{
  389. */
  390. /**
  391. * @brief Initializes the I2C according to the specified parameters
  392. * in the I2C_InitTypeDef and initialize the associated handle.
  393. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  394. * the configuration information for the specified I2C.
  395. * @retval HAL status
  396. */
  397. HAL_StatusTypeDef HAL_I2C_Init(I2C_HandleTypeDef *hi2c)
  398. {
  399. uint32_t freqrange;
  400. uint32_t pclk1;
  401. /* Check the I2C handle allocation */
  402. if (hi2c == NULL)
  403. {
  404. return HAL_ERROR;
  405. }
  406. /* Check the parameters */
  407. assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
  408. assert_param(IS_I2C_CLOCK_SPEED(hi2c->Init.ClockSpeed));
  409. assert_param(IS_I2C_DUTY_CYCLE(hi2c->Init.DutyCycle));
  410. assert_param(IS_I2C_OWN_ADDRESS1(hi2c->Init.OwnAddress1));
  411. assert_param(IS_I2C_ADDRESSING_MODE(hi2c->Init.AddressingMode));
  412. assert_param(IS_I2C_DUAL_ADDRESS(hi2c->Init.DualAddressMode));
  413. assert_param(IS_I2C_OWN_ADDRESS2(hi2c->Init.OwnAddress2));
  414. assert_param(IS_I2C_GENERAL_CALL(hi2c->Init.GeneralCallMode));
  415. assert_param(IS_I2C_NO_STRETCH(hi2c->Init.NoStretchMode));
  416. if (hi2c->State == HAL_I2C_STATE_RESET)
  417. {
  418. /* Allocate lock resource and initialize it */
  419. hi2c->Lock = HAL_UNLOCKED;
  420. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  421. /* Init the I2C Callback settings */
  422. hi2c->MasterTxCpltCallback = HAL_I2C_MasterTxCpltCallback; /* Legacy weak MasterTxCpltCallback */
  423. hi2c->MasterRxCpltCallback = HAL_I2C_MasterRxCpltCallback; /* Legacy weak MasterRxCpltCallback */
  424. hi2c->SlaveTxCpltCallback = HAL_I2C_SlaveTxCpltCallback; /* Legacy weak SlaveTxCpltCallback */
  425. hi2c->SlaveRxCpltCallback = HAL_I2C_SlaveRxCpltCallback; /* Legacy weak SlaveRxCpltCallback */
  426. hi2c->ListenCpltCallback = HAL_I2C_ListenCpltCallback; /* Legacy weak ListenCpltCallback */
  427. hi2c->MemTxCpltCallback = HAL_I2C_MemTxCpltCallback; /* Legacy weak MemTxCpltCallback */
  428. hi2c->MemRxCpltCallback = HAL_I2C_MemRxCpltCallback; /* Legacy weak MemRxCpltCallback */
  429. hi2c->ErrorCallback = HAL_I2C_ErrorCallback; /* Legacy weak ErrorCallback */
  430. hi2c->AbortCpltCallback = HAL_I2C_AbortCpltCallback; /* Legacy weak AbortCpltCallback */
  431. hi2c->AddrCallback = HAL_I2C_AddrCallback; /* Legacy weak AddrCallback */
  432. if (hi2c->MspInitCallback == NULL)
  433. {
  434. hi2c->MspInitCallback = HAL_I2C_MspInit; /* Legacy weak MspInit */
  435. }
  436. /* Init the low level hardware : GPIO, CLOCK, NVIC */
  437. hi2c->MspInitCallback(hi2c);
  438. #else
  439. /* Init the low level hardware : GPIO, CLOCK, NVIC */
  440. HAL_I2C_MspInit(hi2c);
  441. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  442. }
  443. hi2c->State = HAL_I2C_STATE_BUSY;
  444. /* Disable the selected I2C peripheral */
  445. __HAL_I2C_DISABLE(hi2c);
  446. /*Reset I2C*/
  447. hi2c->Instance->CR1 |= I2C_CR1_SWRST;
  448. hi2c->Instance->CR1 &= ~I2C_CR1_SWRST;
  449. /* Get PCLK1 frequency */
  450. pclk1 = HAL_RCC_GetPCLK1Freq();
  451. /* Check the minimum allowed PCLK1 frequency */
  452. if (I2C_MIN_PCLK_FREQ(pclk1, hi2c->Init.ClockSpeed) == 1U)
  453. {
  454. return HAL_ERROR;
  455. }
  456. /* Calculate frequency range */
  457. freqrange = I2C_FREQRANGE(pclk1);
  458. /*---------------------------- I2Cx CR2 Configuration ----------------------*/
  459. /* Configure I2Cx: Frequency range */
  460. MODIFY_REG(hi2c->Instance->CR2, I2C_CR2_FREQ, freqrange);
  461. /*---------------------------- I2Cx TRISE Configuration --------------------*/
  462. /* Configure I2Cx: Rise Time */
  463. MODIFY_REG(hi2c->Instance->TRISE, I2C_TRISE_TRISE, I2C_RISE_TIME(freqrange, hi2c->Init.ClockSpeed));
  464. /*---------------------------- I2Cx CCR Configuration ----------------------*/
  465. /* Configure I2Cx: Speed */
  466. MODIFY_REG(hi2c->Instance->CCR, (I2C_CCR_FS | I2C_CCR_DUTY | I2C_CCR_CCR), I2C_SPEED(pclk1, hi2c->Init.ClockSpeed, hi2c->Init.DutyCycle));
  467. /*---------------------------- I2Cx CR1 Configuration ----------------------*/
  468. /* Configure I2Cx: Generalcall and NoStretch mode */
  469. MODIFY_REG(hi2c->Instance->CR1, (I2C_CR1_ENGC | I2C_CR1_NOSTRETCH), (hi2c->Init.GeneralCallMode | hi2c->Init.NoStretchMode));
  470. /*---------------------------- I2Cx OAR1 Configuration ---------------------*/
  471. /* Configure I2Cx: Own Address1 and addressing mode */
  472. MODIFY_REG(hi2c->Instance->OAR1, (I2C_OAR1_ADDMODE | I2C_OAR1_ADD8_9 | I2C_OAR1_ADD1_7 | I2C_OAR1_ADD0), (hi2c->Init.AddressingMode | hi2c->Init.OwnAddress1));
  473. /*---------------------------- I2Cx OAR2 Configuration ---------------------*/
  474. /* Configure I2Cx: Dual mode and Own Address2 */
  475. MODIFY_REG(hi2c->Instance->OAR2, (I2C_OAR2_ENDUAL | I2C_OAR2_ADD2), (hi2c->Init.DualAddressMode | hi2c->Init.OwnAddress2));
  476. /* Enable the selected I2C peripheral */
  477. __HAL_I2C_ENABLE(hi2c);
  478. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  479. hi2c->State = HAL_I2C_STATE_READY;
  480. hi2c->PreviousState = I2C_STATE_NONE;
  481. hi2c->Mode = HAL_I2C_MODE_NONE;
  482. return HAL_OK;
  483. }
  484. /**
  485. * @brief DeInitialize the I2C peripheral.
  486. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  487. * the configuration information for the specified I2C.
  488. * @retval HAL status
  489. */
  490. HAL_StatusTypeDef HAL_I2C_DeInit(I2C_HandleTypeDef *hi2c)
  491. {
  492. /* Check the I2C handle allocation */
  493. if (hi2c == NULL)
  494. {
  495. return HAL_ERROR;
  496. }
  497. /* Check the parameters */
  498. assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
  499. hi2c->State = HAL_I2C_STATE_BUSY;
  500. /* Disable the I2C Peripheral Clock */
  501. __HAL_I2C_DISABLE(hi2c);
  502. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  503. if (hi2c->MspDeInitCallback == NULL)
  504. {
  505. hi2c->MspDeInitCallback = HAL_I2C_MspDeInit; /* Legacy weak MspDeInit */
  506. }
  507. /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
  508. hi2c->MspDeInitCallback(hi2c);
  509. #else
  510. /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
  511. HAL_I2C_MspDeInit(hi2c);
  512. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  513. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  514. hi2c->State = HAL_I2C_STATE_RESET;
  515. hi2c->PreviousState = I2C_STATE_NONE;
  516. hi2c->Mode = HAL_I2C_MODE_NONE;
  517. /* Release Lock */
  518. __HAL_UNLOCK(hi2c);
  519. return HAL_OK;
  520. }
  521. /**
  522. * @brief Initialize the I2C MSP.
  523. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  524. * the configuration information for the specified I2C.
  525. * @retval None
  526. */
  527. __weak void HAL_I2C_MspInit(I2C_HandleTypeDef *hi2c)
  528. {
  529. /* Prevent unused argument(s) compilation warning */
  530. UNUSED(hi2c);
  531. /* NOTE : This function should not be modified, when the callback is needed,
  532. the HAL_I2C_MspInit could be implemented in the user file
  533. */
  534. }
  535. /**
  536. * @brief DeInitialize the I2C MSP.
  537. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  538. * the configuration information for the specified I2C.
  539. * @retval None
  540. */
  541. __weak void HAL_I2C_MspDeInit(I2C_HandleTypeDef *hi2c)
  542. {
  543. /* Prevent unused argument(s) compilation warning */
  544. UNUSED(hi2c);
  545. /* NOTE : This function should not be modified, when the callback is needed,
  546. the HAL_I2C_MspDeInit could be implemented in the user file
  547. */
  548. }
  549. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  550. /**
  551. * @brief Register a User I2C Callback
  552. * To be used instead of the weak predefined callback
  553. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  554. * the configuration information for the specified I2C.
  555. * @param CallbackID ID of the callback to be registered
  556. * This parameter can be one of the following values:
  557. * @arg @ref HAL_I2C_MASTER_TX_COMPLETE_CB_ID Master Tx Transfer completed callback ID
  558. * @arg @ref HAL_I2C_MASTER_RX_COMPLETE_CB_ID Master Rx Transfer completed callback ID
  559. * @arg @ref HAL_I2C_SLAVE_TX_COMPLETE_CB_ID Slave Tx Transfer completed callback ID
  560. * @arg @ref HAL_I2C_SLAVE_RX_COMPLETE_CB_ID Slave Rx Transfer completed callback ID
  561. * @arg @ref HAL_I2C_LISTEN_COMPLETE_CB_ID Listen Complete callback ID
  562. * @arg @ref HAL_I2C_MEM_TX_COMPLETE_CB_ID Memory Tx Transfer callback ID
  563. * @arg @ref HAL_I2C_MEM_RX_COMPLETE_CB_ID Memory Rx Transfer completed callback ID
  564. * @arg @ref HAL_I2C_ERROR_CB_ID Error callback ID
  565. * @arg @ref HAL_I2C_ABORT_CB_ID Abort callback ID
  566. * @arg @ref HAL_I2C_MSPINIT_CB_ID MspInit callback ID
  567. * @arg @ref HAL_I2C_MSPDEINIT_CB_ID MspDeInit callback ID
  568. * @param pCallback pointer to the Callback function
  569. * @retval HAL status
  570. */
  571. HAL_StatusTypeDef HAL_I2C_RegisterCallback(I2C_HandleTypeDef *hi2c, HAL_I2C_CallbackIDTypeDef CallbackID, pI2C_CallbackTypeDef pCallback)
  572. {
  573. HAL_StatusTypeDef status = HAL_OK;
  574. if (pCallback == NULL)
  575. {
  576. /* Update the error code */
  577. hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK;
  578. return HAL_ERROR;
  579. }
  580. /* Process locked */
  581. __HAL_LOCK(hi2c);
  582. if (HAL_I2C_STATE_READY == hi2c->State)
  583. {
  584. switch (CallbackID)
  585. {
  586. case HAL_I2C_MASTER_TX_COMPLETE_CB_ID :
  587. hi2c->MasterTxCpltCallback = pCallback;
  588. break;
  589. case HAL_I2C_MASTER_RX_COMPLETE_CB_ID :
  590. hi2c->MasterRxCpltCallback = pCallback;
  591. break;
  592. case HAL_I2C_SLAVE_TX_COMPLETE_CB_ID :
  593. hi2c->SlaveTxCpltCallback = pCallback;
  594. break;
  595. case HAL_I2C_SLAVE_RX_COMPLETE_CB_ID :
  596. hi2c->SlaveRxCpltCallback = pCallback;
  597. break;
  598. case HAL_I2C_LISTEN_COMPLETE_CB_ID :
  599. hi2c->ListenCpltCallback = pCallback;
  600. break;
  601. case HAL_I2C_MEM_TX_COMPLETE_CB_ID :
  602. hi2c->MemTxCpltCallback = pCallback;
  603. break;
  604. case HAL_I2C_MEM_RX_COMPLETE_CB_ID :
  605. hi2c->MemRxCpltCallback = pCallback;
  606. break;
  607. case HAL_I2C_ERROR_CB_ID :
  608. hi2c->ErrorCallback = pCallback;
  609. break;
  610. case HAL_I2C_ABORT_CB_ID :
  611. hi2c->AbortCpltCallback = pCallback;
  612. break;
  613. case HAL_I2C_MSPINIT_CB_ID :
  614. hi2c->MspInitCallback = pCallback;
  615. break;
  616. case HAL_I2C_MSPDEINIT_CB_ID :
  617. hi2c->MspDeInitCallback = pCallback;
  618. break;
  619. default :
  620. /* Update the error code */
  621. hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK;
  622. /* Return error status */
  623. status = HAL_ERROR;
  624. break;
  625. }
  626. }
  627. else if (HAL_I2C_STATE_RESET == hi2c->State)
  628. {
  629. switch (CallbackID)
  630. {
  631. case HAL_I2C_MSPINIT_CB_ID :
  632. hi2c->MspInitCallback = pCallback;
  633. break;
  634. case HAL_I2C_MSPDEINIT_CB_ID :
  635. hi2c->MspDeInitCallback = pCallback;
  636. break;
  637. default :
  638. /* Update the error code */
  639. hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK;
  640. /* Return error status */
  641. status = HAL_ERROR;
  642. break;
  643. }
  644. }
  645. else
  646. {
  647. /* Update the error code */
  648. hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK;
  649. /* Return error status */
  650. status = HAL_ERROR;
  651. }
  652. /* Release Lock */
  653. __HAL_UNLOCK(hi2c);
  654. return status;
  655. }
  656. /**
  657. * @brief Unregister an I2C Callback
  658. * I2C callback is redirected to the weak predefined callback
  659. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  660. * the configuration information for the specified I2C.
  661. * @param CallbackID ID of the callback to be unregistered
  662. * This parameter can be one of the following values:
  663. * This parameter can be one of the following values:
  664. * @arg @ref HAL_I2C_MASTER_TX_COMPLETE_CB_ID Master Tx Transfer completed callback ID
  665. * @arg @ref HAL_I2C_MASTER_RX_COMPLETE_CB_ID Master Rx Transfer completed callback ID
  666. * @arg @ref HAL_I2C_SLAVE_TX_COMPLETE_CB_ID Slave Tx Transfer completed callback ID
  667. * @arg @ref HAL_I2C_SLAVE_RX_COMPLETE_CB_ID Slave Rx Transfer completed callback ID
  668. * @arg @ref HAL_I2C_LISTEN_COMPLETE_CB_ID Listen Complete callback ID
  669. * @arg @ref HAL_I2C_MEM_TX_COMPLETE_CB_ID Memory Tx Transfer callback ID
  670. * @arg @ref HAL_I2C_MEM_RX_COMPLETE_CB_ID Memory Rx Transfer completed callback ID
  671. * @arg @ref HAL_I2C_ERROR_CB_ID Error callback ID
  672. * @arg @ref HAL_I2C_ABORT_CB_ID Abort callback ID
  673. * @arg @ref HAL_I2C_MSPINIT_CB_ID MspInit callback ID
  674. * @arg @ref HAL_I2C_MSPDEINIT_CB_ID MspDeInit callback ID
  675. * @retval HAL status
  676. */
  677. HAL_StatusTypeDef HAL_I2C_UnRegisterCallback(I2C_HandleTypeDef *hi2c, HAL_I2C_CallbackIDTypeDef CallbackID)
  678. {
  679. HAL_StatusTypeDef status = HAL_OK;
  680. /* Process locked */
  681. __HAL_LOCK(hi2c);
  682. if (HAL_I2C_STATE_READY == hi2c->State)
  683. {
  684. switch (CallbackID)
  685. {
  686. case HAL_I2C_MASTER_TX_COMPLETE_CB_ID :
  687. hi2c->MasterTxCpltCallback = HAL_I2C_MasterTxCpltCallback; /* Legacy weak MasterTxCpltCallback */
  688. break;
  689. case HAL_I2C_MASTER_RX_COMPLETE_CB_ID :
  690. hi2c->MasterRxCpltCallback = HAL_I2C_MasterRxCpltCallback; /* Legacy weak MasterRxCpltCallback */
  691. break;
  692. case HAL_I2C_SLAVE_TX_COMPLETE_CB_ID :
  693. hi2c->SlaveTxCpltCallback = HAL_I2C_SlaveTxCpltCallback; /* Legacy weak SlaveTxCpltCallback */
  694. break;
  695. case HAL_I2C_SLAVE_RX_COMPLETE_CB_ID :
  696. hi2c->SlaveRxCpltCallback = HAL_I2C_SlaveRxCpltCallback; /* Legacy weak SlaveRxCpltCallback */
  697. break;
  698. case HAL_I2C_LISTEN_COMPLETE_CB_ID :
  699. hi2c->ListenCpltCallback = HAL_I2C_ListenCpltCallback; /* Legacy weak ListenCpltCallback */
  700. break;
  701. case HAL_I2C_MEM_TX_COMPLETE_CB_ID :
  702. hi2c->MemTxCpltCallback = HAL_I2C_MemTxCpltCallback; /* Legacy weak MemTxCpltCallback */
  703. break;
  704. case HAL_I2C_MEM_RX_COMPLETE_CB_ID :
  705. hi2c->MemRxCpltCallback = HAL_I2C_MemRxCpltCallback; /* Legacy weak MemRxCpltCallback */
  706. break;
  707. case HAL_I2C_ERROR_CB_ID :
  708. hi2c->ErrorCallback = HAL_I2C_ErrorCallback; /* Legacy weak ErrorCallback */
  709. break;
  710. case HAL_I2C_ABORT_CB_ID :
  711. hi2c->AbortCpltCallback = HAL_I2C_AbortCpltCallback; /* Legacy weak AbortCpltCallback */
  712. break;
  713. case HAL_I2C_MSPINIT_CB_ID :
  714. hi2c->MspInitCallback = HAL_I2C_MspInit; /* Legacy weak MspInit */
  715. break;
  716. case HAL_I2C_MSPDEINIT_CB_ID :
  717. hi2c->MspDeInitCallback = HAL_I2C_MspDeInit; /* Legacy weak MspDeInit */
  718. break;
  719. default :
  720. /* Update the error code */
  721. hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK;
  722. /* Return error status */
  723. status = HAL_ERROR;
  724. break;
  725. }
  726. }
  727. else if (HAL_I2C_STATE_RESET == hi2c->State)
  728. {
  729. switch (CallbackID)
  730. {
  731. case HAL_I2C_MSPINIT_CB_ID :
  732. hi2c->MspInitCallback = HAL_I2C_MspInit; /* Legacy weak MspInit */
  733. break;
  734. case HAL_I2C_MSPDEINIT_CB_ID :
  735. hi2c->MspDeInitCallback = HAL_I2C_MspDeInit; /* Legacy weak MspDeInit */
  736. break;
  737. default :
  738. /* Update the error code */
  739. hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK;
  740. /* Return error status */
  741. status = HAL_ERROR;
  742. break;
  743. }
  744. }
  745. else
  746. {
  747. /* Update the error code */
  748. hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK;
  749. /* Return error status */
  750. status = HAL_ERROR;
  751. }
  752. /* Release Lock */
  753. __HAL_UNLOCK(hi2c);
  754. return status;
  755. }
  756. /**
  757. * @brief Register the Slave Address Match I2C Callback
  758. * To be used instead of the weak HAL_I2C_AddrCallback() predefined callback
  759. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  760. * the configuration information for the specified I2C.
  761. * @param pCallback pointer to the Address Match Callback function
  762. * @retval HAL status
  763. */
  764. HAL_StatusTypeDef HAL_I2C_RegisterAddrCallback(I2C_HandleTypeDef *hi2c, pI2C_AddrCallbackTypeDef pCallback)
  765. {
  766. HAL_StatusTypeDef status = HAL_OK;
  767. if (pCallback == NULL)
  768. {
  769. /* Update the error code */
  770. hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK;
  771. return HAL_ERROR;
  772. }
  773. /* Process locked */
  774. __HAL_LOCK(hi2c);
  775. if (HAL_I2C_STATE_READY == hi2c->State)
  776. {
  777. hi2c->AddrCallback = pCallback;
  778. }
  779. else
  780. {
  781. /* Update the error code */
  782. hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK;
  783. /* Return error status */
  784. status = HAL_ERROR;
  785. }
  786. /* Release Lock */
  787. __HAL_UNLOCK(hi2c);
  788. return status;
  789. }
  790. /**
  791. * @brief UnRegister the Slave Address Match I2C Callback
  792. * Info Ready I2C Callback is redirected to the weak HAL_I2C_AddrCallback() predefined callback
  793. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  794. * the configuration information for the specified I2C.
  795. * @retval HAL status
  796. */
  797. HAL_StatusTypeDef HAL_I2C_UnRegisterAddrCallback(I2C_HandleTypeDef *hi2c)
  798. {
  799. HAL_StatusTypeDef status = HAL_OK;
  800. /* Process locked */
  801. __HAL_LOCK(hi2c);
  802. if (HAL_I2C_STATE_READY == hi2c->State)
  803. {
  804. hi2c->AddrCallback = HAL_I2C_AddrCallback; /* Legacy weak AddrCallback */
  805. }
  806. else
  807. {
  808. /* Update the error code */
  809. hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK;
  810. /* Return error status */
  811. status = HAL_ERROR;
  812. }
  813. /* Release Lock */
  814. __HAL_UNLOCK(hi2c);
  815. return status;
  816. }
  817. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  818. /**
  819. * @brief I2C data register flush process.
  820. * @param hi2c I2C handle.
  821. * @retval None
  822. */
  823. static void I2C_Flush_DR(I2C_HandleTypeDef *hi2c)
  824. {
  825. /* Write a dummy data in DR to clear TXE flag */
  826. if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXE) != RESET)
  827. {
  828. hi2c->Instance->DR = 0x00U;
  829. }
  830. }
  831. /**
  832. * @}
  833. */
  834. /** @defgroup I2C_Exported_Functions_Group2 Input and Output operation functions
  835. * @brief Data transfers functions
  836. *
  837. @verbatim
  838. ===============================================================================
  839. ##### IO operation functions #####
  840. ===============================================================================
  841. [..]
  842. This subsection provides a set of functions allowing to manage the I2C data
  843. transfers.
  844. (#) There are two modes of transfer:
  845. (++) Blocking mode : The communication is performed in the polling mode.
  846. The status of all data processing is returned by the same function
  847. after finishing transfer.
  848. (++) No-Blocking mode : The communication is performed using Interrupts
  849. or DMA. These functions return the status of the transfer startup.
  850. The end of the data processing will be indicated through the
  851. dedicated I2C IRQ when using Interrupt mode or the DMA IRQ when
  852. using DMA mode.
  853. (#) Blocking mode functions are :
  854. (++) HAL_I2C_Master_Transmit()
  855. (++) HAL_I2C_Master_Receive()
  856. (++) HAL_I2C_Slave_Transmit()
  857. (++) HAL_I2C_Slave_Receive()
  858. (++) HAL_I2C_Mem_Write()
  859. (++) HAL_I2C_Mem_Read()
  860. (++) HAL_I2C_IsDeviceReady()
  861. (#) No-Blocking mode functions with Interrupt are :
  862. (++) HAL_I2C_Master_Transmit_IT()
  863. (++) HAL_I2C_Master_Receive_IT()
  864. (++) HAL_I2C_Slave_Transmit_IT()
  865. (++) HAL_I2C_Slave_Receive_IT()
  866. (++) HAL_I2C_Mem_Write_IT()
  867. (++) HAL_I2C_Mem_Read_IT()
  868. (++) HAL_I2C_Master_Seq_Transmit_IT()
  869. (++) HAL_I2C_Master_Seq_Receive_IT()
  870. (++) HAL_I2C_Slave_Seq_Transmit_IT()
  871. (++) HAL_I2C_Slave_Seq_Receive_IT()
  872. (++) HAL_I2C_EnableListen_IT()
  873. (++) HAL_I2C_DisableListen_IT()
  874. (++) HAL_I2C_Master_Abort_IT()
  875. (#) No-Blocking mode functions with DMA are :
  876. (++) HAL_I2C_Master_Transmit_DMA()
  877. (++) HAL_I2C_Master_Receive_DMA()
  878. (++) HAL_I2C_Slave_Transmit_DMA()
  879. (++) HAL_I2C_Slave_Receive_DMA()
  880. (++) HAL_I2C_Mem_Write_DMA()
  881. (++) HAL_I2C_Mem_Read_DMA()
  882. (++) HAL_I2C_Master_Seq_Transmit_DMA()
  883. (++) HAL_I2C_Master_Seq_Receive_DMA()
  884. (++) HAL_I2C_Slave_Seq_Transmit_DMA()
  885. (++) HAL_I2C_Slave_Seq_Receive_DMA()
  886. (#) A set of Transfer Complete Callbacks are provided in non Blocking mode:
  887. (++) HAL_I2C_MasterTxCpltCallback()
  888. (++) HAL_I2C_MasterRxCpltCallback()
  889. (++) HAL_I2C_SlaveTxCpltCallback()
  890. (++) HAL_I2C_SlaveRxCpltCallback()
  891. (++) HAL_I2C_MemTxCpltCallback()
  892. (++) HAL_I2C_MemRxCpltCallback()
  893. (++) HAL_I2C_AddrCallback()
  894. (++) HAL_I2C_ListenCpltCallback()
  895. (++) HAL_I2C_ErrorCallback()
  896. (++) HAL_I2C_AbortCpltCallback()
  897. @endverbatim
  898. * @{
  899. */
  900. /**
  901. * @brief Transmits in master mode an amount of data in blocking mode.
  902. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  903. * the configuration information for the specified I2C.
  904. * @param DevAddress Target device address: The device 7 bits address value
  905. * in datasheet must be shifted to the left before calling the interface
  906. * @param pData Pointer to data buffer
  907. * @param Size Amount of data to be sent
  908. * @param Timeout Timeout duration
  909. * @retval HAL status
  910. */
  911. HAL_StatusTypeDef HAL_I2C_Master_Transmit(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  912. {
  913. /* Init tickstart for timeout management*/
  914. uint32_t tickstart = HAL_GetTick();
  915. if (hi2c->State == HAL_I2C_STATE_READY)
  916. {
  917. /* Wait until BUSY flag is reset */
  918. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY_FLAG, tickstart) != HAL_OK)
  919. {
  920. return HAL_BUSY;
  921. }
  922. /* Process Locked */
  923. __HAL_LOCK(hi2c);
  924. /* Check if the I2C is already enabled */
  925. if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  926. {
  927. /* Enable I2C peripheral */
  928. __HAL_I2C_ENABLE(hi2c);
  929. }
  930. /* Disable Pos */
  931. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  932. hi2c->State = HAL_I2C_STATE_BUSY_TX;
  933. hi2c->Mode = HAL_I2C_MODE_MASTER;
  934. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  935. /* Prepare transfer parameters */
  936. hi2c->pBuffPtr = pData;
  937. hi2c->XferCount = Size;
  938. hi2c->XferSize = hi2c->XferCount;
  939. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  940. /* Send Slave Address */
  941. if (I2C_MasterRequestWrite(hi2c, DevAddress, Timeout, tickstart) != HAL_OK)
  942. {
  943. return HAL_ERROR;
  944. }
  945. /* Clear ADDR flag */
  946. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  947. while (hi2c->XferSize > 0U)
  948. {
  949. /* Wait until TXE flag is set */
  950. if (I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
  951. {
  952. if (hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  953. {
  954. /* Generate Stop */
  955. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  956. }
  957. return HAL_ERROR;
  958. }
  959. /* Write data to DR */
  960. hi2c->Instance->DR = *hi2c->pBuffPtr;
  961. /* Increment Buffer pointer */
  962. hi2c->pBuffPtr++;
  963. /* Update counter */
  964. hi2c->XferCount--;
  965. hi2c->XferSize--;
  966. if ((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET) && (hi2c->XferSize != 0U))
  967. {
  968. /* Write data to DR */
  969. hi2c->Instance->DR = *hi2c->pBuffPtr;
  970. /* Increment Buffer pointer */
  971. hi2c->pBuffPtr++;
  972. /* Update counter */
  973. hi2c->XferCount--;
  974. hi2c->XferSize--;
  975. }
  976. /* Wait until BTF flag is set */
  977. if (I2C_WaitOnBTFFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
  978. {
  979. if (hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  980. {
  981. /* Generate Stop */
  982. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  983. }
  984. return HAL_ERROR;
  985. }
  986. }
  987. /* Generate Stop */
  988. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  989. hi2c->State = HAL_I2C_STATE_READY;
  990. hi2c->Mode = HAL_I2C_MODE_NONE;
  991. /* Process Unlocked */
  992. __HAL_UNLOCK(hi2c);
  993. return HAL_OK;
  994. }
  995. else
  996. {
  997. return HAL_BUSY;
  998. }
  999. }
  1000. /**
  1001. * @brief Receives in master mode an amount of data in blocking mode.
  1002. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  1003. * the configuration information for the specified I2C.
  1004. * @param DevAddress Target device address: The device 7 bits address value
  1005. * in datasheet must be shifted to the left before calling the interface
  1006. * @param pData Pointer to data buffer
  1007. * @param Size Amount of data to be sent
  1008. * @param Timeout Timeout duration
  1009. * @retval HAL status
  1010. */
  1011. HAL_StatusTypeDef HAL_I2C_Master_Receive(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  1012. {
  1013. /* Init tickstart for timeout management*/
  1014. uint32_t tickstart = HAL_GetTick();
  1015. if (hi2c->State == HAL_I2C_STATE_READY)
  1016. {
  1017. /* Wait until BUSY flag is reset */
  1018. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY_FLAG, tickstart) != HAL_OK)
  1019. {
  1020. return HAL_BUSY;
  1021. }
  1022. /* Process Locked */
  1023. __HAL_LOCK(hi2c);
  1024. /* Check if the I2C is already enabled */
  1025. if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  1026. {
  1027. /* Enable I2C peripheral */
  1028. __HAL_I2C_ENABLE(hi2c);
  1029. }
  1030. /* Disable Pos */
  1031. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  1032. hi2c->State = HAL_I2C_STATE_BUSY_RX;
  1033. hi2c->Mode = HAL_I2C_MODE_MASTER;
  1034. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1035. /* Prepare transfer parameters */
  1036. hi2c->pBuffPtr = pData;
  1037. hi2c->XferCount = Size;
  1038. hi2c->XferSize = hi2c->XferCount;
  1039. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  1040. /* Send Slave Address */
  1041. if (I2C_MasterRequestRead(hi2c, DevAddress, Timeout, tickstart) != HAL_OK)
  1042. {
  1043. return HAL_ERROR;
  1044. }
  1045. if (hi2c->XferSize == 0U)
  1046. {
  1047. /* Clear ADDR flag */
  1048. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  1049. /* Generate Stop */
  1050. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  1051. }
  1052. else if (hi2c->XferSize == 1U)
  1053. {
  1054. /* Disable Acknowledge */
  1055. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  1056. /* Clear ADDR flag */
  1057. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  1058. /* Generate Stop */
  1059. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  1060. }
  1061. else if (hi2c->XferSize == 2U)
  1062. {
  1063. /* Disable Acknowledge */
  1064. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  1065. /* Enable Pos */
  1066. SET_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  1067. /* Clear ADDR flag */
  1068. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  1069. }
  1070. else
  1071. {
  1072. /* Enable Acknowledge */
  1073. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  1074. /* Clear ADDR flag */
  1075. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  1076. }
  1077. while (hi2c->XferSize > 0U)
  1078. {
  1079. if (hi2c->XferSize <= 3U)
  1080. {
  1081. /* One byte */
  1082. if (hi2c->XferSize == 1U)
  1083. {
  1084. /* Wait until RXNE flag is set */
  1085. if (I2C_WaitOnRXNEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
  1086. {
  1087. return HAL_ERROR;
  1088. }
  1089. /* Read data from DR */
  1090. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
  1091. /* Increment Buffer pointer */
  1092. hi2c->pBuffPtr++;
  1093. /* Update counter */
  1094. hi2c->XferSize--;
  1095. hi2c->XferCount--;
  1096. }
  1097. /* Two bytes */
  1098. else if (hi2c->XferSize == 2U)
  1099. {
  1100. /* Wait until BTF flag is set */
  1101. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BTF, RESET, Timeout, tickstart) != HAL_OK)
  1102. {
  1103. return HAL_ERROR;
  1104. }
  1105. /* Generate Stop */
  1106. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  1107. /* Read data from DR */
  1108. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
  1109. /* Increment Buffer pointer */
  1110. hi2c->pBuffPtr++;
  1111. /* Update counter */
  1112. hi2c->XferSize--;
  1113. hi2c->XferCount--;
  1114. /* Read data from DR */
  1115. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
  1116. /* Increment Buffer pointer */
  1117. hi2c->pBuffPtr++;
  1118. /* Update counter */
  1119. hi2c->XferSize--;
  1120. hi2c->XferCount--;
  1121. }
  1122. /* 3 Last bytes */
  1123. else
  1124. {
  1125. /* Wait until BTF flag is set */
  1126. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BTF, RESET, Timeout, tickstart) != HAL_OK)
  1127. {
  1128. return HAL_ERROR;
  1129. }
  1130. /* Disable Acknowledge */
  1131. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  1132. /* Read data from DR */
  1133. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
  1134. /* Increment Buffer pointer */
  1135. hi2c->pBuffPtr++;
  1136. /* Update counter */
  1137. hi2c->XferSize--;
  1138. hi2c->XferCount--;
  1139. /* Wait until BTF flag is set */
  1140. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BTF, RESET, Timeout, tickstart) != HAL_OK)
  1141. {
  1142. return HAL_ERROR;
  1143. }
  1144. /* Generate Stop */
  1145. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  1146. /* Read data from DR */
  1147. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
  1148. /* Increment Buffer pointer */
  1149. hi2c->pBuffPtr++;
  1150. /* Update counter */
  1151. hi2c->XferSize--;
  1152. hi2c->XferCount--;
  1153. /* Read data from DR */
  1154. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
  1155. /* Increment Buffer pointer */
  1156. hi2c->pBuffPtr++;
  1157. /* Update counter */
  1158. hi2c->XferSize--;
  1159. hi2c->XferCount--;
  1160. }
  1161. }
  1162. else
  1163. {
  1164. /* Wait until RXNE flag is set */
  1165. if (I2C_WaitOnRXNEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
  1166. {
  1167. return HAL_ERROR;
  1168. }
  1169. /* Read data from DR */
  1170. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
  1171. /* Increment Buffer pointer */
  1172. hi2c->pBuffPtr++;
  1173. /* Update counter */
  1174. hi2c->XferSize--;
  1175. hi2c->XferCount--;
  1176. if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET)
  1177. {
  1178. if (hi2c->XferSize == 3U)
  1179. {
  1180. /* Disable Acknowledge */
  1181. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  1182. }
  1183. /* Read data from DR */
  1184. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
  1185. /* Increment Buffer pointer */
  1186. hi2c->pBuffPtr++;
  1187. /* Update counter */
  1188. hi2c->XferSize--;
  1189. hi2c->XferCount--;
  1190. }
  1191. }
  1192. }
  1193. hi2c->State = HAL_I2C_STATE_READY;
  1194. hi2c->Mode = HAL_I2C_MODE_NONE;
  1195. /* Process Unlocked */
  1196. __HAL_UNLOCK(hi2c);
  1197. return HAL_OK;
  1198. }
  1199. else
  1200. {
  1201. return HAL_BUSY;
  1202. }
  1203. }
  1204. /**
  1205. * @brief Transmits in slave mode an amount of data in blocking mode.
  1206. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  1207. * the configuration information for the specified I2C.
  1208. * @param pData Pointer to data buffer
  1209. * @param Size Amount of data to be sent
  1210. * @param Timeout Timeout duration
  1211. * @retval HAL status
  1212. */
  1213. HAL_StatusTypeDef HAL_I2C_Slave_Transmit(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  1214. {
  1215. /* Init tickstart for timeout management*/
  1216. uint32_t tickstart = HAL_GetTick();
  1217. if (hi2c->State == HAL_I2C_STATE_READY)
  1218. {
  1219. if ((pData == NULL) || (Size == 0U))
  1220. {
  1221. return HAL_ERROR;
  1222. }
  1223. /* Process Locked */
  1224. __HAL_LOCK(hi2c);
  1225. /* Check if the I2C is already enabled */
  1226. if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  1227. {
  1228. /* Enable I2C peripheral */
  1229. __HAL_I2C_ENABLE(hi2c);
  1230. }
  1231. /* Disable Pos */
  1232. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  1233. hi2c->State = HAL_I2C_STATE_BUSY_TX;
  1234. hi2c->Mode = HAL_I2C_MODE_SLAVE;
  1235. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1236. /* Prepare transfer parameters */
  1237. hi2c->pBuffPtr = pData;
  1238. hi2c->XferCount = Size;
  1239. hi2c->XferSize = hi2c->XferCount;
  1240. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  1241. /* Enable Address Acknowledge */
  1242. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  1243. /* Wait until ADDR flag is set */
  1244. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, Timeout, tickstart) != HAL_OK)
  1245. {
  1246. return HAL_ERROR;
  1247. }
  1248. /* Clear ADDR flag */
  1249. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  1250. /* If 10bit addressing mode is selected */
  1251. if (hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_10BIT)
  1252. {
  1253. /* Wait until ADDR flag is set */
  1254. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, Timeout, tickstart) != HAL_OK)
  1255. {
  1256. return HAL_ERROR;
  1257. }
  1258. /* Clear ADDR flag */
  1259. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  1260. }
  1261. while (hi2c->XferSize > 0U)
  1262. {
  1263. /* Wait until TXE flag is set */
  1264. if (I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
  1265. {
  1266. /* Disable Address Acknowledge */
  1267. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  1268. return HAL_ERROR;
  1269. }
  1270. /* Write data to DR */
  1271. hi2c->Instance->DR = *hi2c->pBuffPtr;
  1272. /* Increment Buffer pointer */
  1273. hi2c->pBuffPtr++;
  1274. /* Update counter */
  1275. hi2c->XferCount--;
  1276. hi2c->XferSize--;
  1277. if ((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET) && (hi2c->XferSize != 0U))
  1278. {
  1279. /* Write data to DR */
  1280. hi2c->Instance->DR = *hi2c->pBuffPtr;
  1281. /* Increment Buffer pointer */
  1282. hi2c->pBuffPtr++;
  1283. /* Update counter */
  1284. hi2c->XferCount--;
  1285. hi2c->XferSize--;
  1286. }
  1287. }
  1288. /* Wait until AF flag is set */
  1289. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_AF, RESET, Timeout, tickstart) != HAL_OK)
  1290. {
  1291. return HAL_ERROR;
  1292. }
  1293. /* Clear AF flag */
  1294. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  1295. /* Disable Address Acknowledge */
  1296. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  1297. hi2c->State = HAL_I2C_STATE_READY;
  1298. hi2c->Mode = HAL_I2C_MODE_NONE;
  1299. /* Process Unlocked */
  1300. __HAL_UNLOCK(hi2c);
  1301. return HAL_OK;
  1302. }
  1303. else
  1304. {
  1305. return HAL_BUSY;
  1306. }
  1307. }
  1308. /**
  1309. * @brief Receive in slave mode an amount of data in blocking mode
  1310. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  1311. * the configuration information for the specified I2C.
  1312. * @param pData Pointer to data buffer
  1313. * @param Size Amount of data to be sent
  1314. * @param Timeout Timeout duration
  1315. * @retval HAL status
  1316. */
  1317. HAL_StatusTypeDef HAL_I2C_Slave_Receive(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  1318. {
  1319. /* Init tickstart for timeout management*/
  1320. uint32_t tickstart = HAL_GetTick();
  1321. if (hi2c->State == HAL_I2C_STATE_READY)
  1322. {
  1323. if ((pData == NULL) || (Size == (uint16_t)0))
  1324. {
  1325. return HAL_ERROR;
  1326. }
  1327. /* Process Locked */
  1328. __HAL_LOCK(hi2c);
  1329. /* Check if the I2C is already enabled */
  1330. if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  1331. {
  1332. /* Enable I2C peripheral */
  1333. __HAL_I2C_ENABLE(hi2c);
  1334. }
  1335. /* Disable Pos */
  1336. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  1337. hi2c->State = HAL_I2C_STATE_BUSY_RX;
  1338. hi2c->Mode = HAL_I2C_MODE_SLAVE;
  1339. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1340. /* Prepare transfer parameters */
  1341. hi2c->pBuffPtr = pData;
  1342. hi2c->XferCount = Size;
  1343. hi2c->XferSize = hi2c->XferCount;
  1344. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  1345. /* Enable Address Acknowledge */
  1346. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  1347. /* Wait until ADDR flag is set */
  1348. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, Timeout, tickstart) != HAL_OK)
  1349. {
  1350. return HAL_ERROR;
  1351. }
  1352. /* Clear ADDR flag */
  1353. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  1354. while (hi2c->XferSize > 0U)
  1355. {
  1356. /* Wait until RXNE flag is set */
  1357. if (I2C_WaitOnRXNEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
  1358. {
  1359. /* Disable Address Acknowledge */
  1360. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  1361. return HAL_ERROR;
  1362. }
  1363. /* Read data from DR */
  1364. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
  1365. /* Increment Buffer pointer */
  1366. hi2c->pBuffPtr++;
  1367. /* Update counter */
  1368. hi2c->XferSize--;
  1369. hi2c->XferCount--;
  1370. if ((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET) && (hi2c->XferSize != 0U))
  1371. {
  1372. /* Read data from DR */
  1373. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
  1374. /* Increment Buffer pointer */
  1375. hi2c->pBuffPtr++;
  1376. /* Update counter */
  1377. hi2c->XferSize--;
  1378. hi2c->XferCount--;
  1379. }
  1380. }
  1381. /* Wait until STOP flag is set */
  1382. if (I2C_WaitOnSTOPFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
  1383. {
  1384. /* Disable Address Acknowledge */
  1385. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  1386. return HAL_ERROR;
  1387. }
  1388. /* Clear STOP flag */
  1389. __HAL_I2C_CLEAR_STOPFLAG(hi2c);
  1390. /* Disable Address Acknowledge */
  1391. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  1392. hi2c->State = HAL_I2C_STATE_READY;
  1393. hi2c->Mode = HAL_I2C_MODE_NONE;
  1394. /* Process Unlocked */
  1395. __HAL_UNLOCK(hi2c);
  1396. return HAL_OK;
  1397. }
  1398. else
  1399. {
  1400. return HAL_BUSY;
  1401. }
  1402. }
  1403. /**
  1404. * @brief Transmit in master mode an amount of data in non-blocking mode with Interrupt
  1405. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  1406. * the configuration information for the specified I2C.
  1407. * @param DevAddress Target device address: The device 7 bits address value
  1408. * in datasheet must be shifted to the left before calling the interface
  1409. * @param pData Pointer to data buffer
  1410. * @param Size Amount of data to be sent
  1411. * @retval HAL status
  1412. */
  1413. HAL_StatusTypeDef HAL_I2C_Master_Transmit_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
  1414. {
  1415. __IO uint32_t count = 0U;
  1416. if (hi2c->State == HAL_I2C_STATE_READY)
  1417. {
  1418. /* Wait until BUSY flag is reset */
  1419. count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U);
  1420. do
  1421. {
  1422. count--;
  1423. if (count == 0U)
  1424. {
  1425. hi2c->PreviousState = I2C_STATE_NONE;
  1426. hi2c->State = HAL_I2C_STATE_READY;
  1427. hi2c->Mode = HAL_I2C_MODE_NONE;
  1428. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  1429. return HAL_BUSY;
  1430. }
  1431. }
  1432. while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
  1433. /* Process Locked */
  1434. __HAL_LOCK(hi2c);
  1435. /* Check if the I2C is already enabled */
  1436. if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  1437. {
  1438. /* Enable I2C peripheral */
  1439. __HAL_I2C_ENABLE(hi2c);
  1440. }
  1441. /* Disable Pos */
  1442. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  1443. hi2c->State = HAL_I2C_STATE_BUSY_TX;
  1444. hi2c->Mode = HAL_I2C_MODE_MASTER;
  1445. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1446. /* Prepare transfer parameters */
  1447. hi2c->pBuffPtr = pData;
  1448. hi2c->XferCount = Size;
  1449. hi2c->XferSize = hi2c->XferCount;
  1450. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  1451. hi2c->Devaddress = DevAddress;
  1452. /* Process Unlocked */
  1453. __HAL_UNLOCK(hi2c);
  1454. /* Note : The I2C interrupts must be enabled after unlocking current process
  1455. to avoid the risk of I2C interrupt handle execution before current
  1456. process unlock */
  1457. /* Enable EVT, BUF and ERR interrupt */
  1458. __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  1459. /* Generate Start */
  1460. SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
  1461. return HAL_OK;
  1462. }
  1463. else
  1464. {
  1465. return HAL_BUSY;
  1466. }
  1467. }
  1468. /**
  1469. * @brief Receive in master mode an amount of data in non-blocking mode with Interrupt
  1470. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  1471. * the configuration information for the specified I2C.
  1472. * @param DevAddress Target device address: The device 7 bits address value
  1473. * in datasheet must be shifted to the left before calling the interface
  1474. * @param pData Pointer to data buffer
  1475. * @param Size Amount of data to be sent
  1476. * @retval HAL status
  1477. */
  1478. HAL_StatusTypeDef HAL_I2C_Master_Receive_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
  1479. {
  1480. __IO uint32_t count = 0U;
  1481. if (hi2c->State == HAL_I2C_STATE_READY)
  1482. {
  1483. /* Wait until BUSY flag is reset */
  1484. count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U);
  1485. do
  1486. {
  1487. count--;
  1488. if (count == 0U)
  1489. {
  1490. hi2c->PreviousState = I2C_STATE_NONE;
  1491. hi2c->State = HAL_I2C_STATE_READY;
  1492. hi2c->Mode = HAL_I2C_MODE_NONE;
  1493. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  1494. return HAL_BUSY;
  1495. }
  1496. }
  1497. while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
  1498. /* Process Locked */
  1499. __HAL_LOCK(hi2c);
  1500. /* Check if the I2C is already enabled */
  1501. if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  1502. {
  1503. /* Enable I2C peripheral */
  1504. __HAL_I2C_ENABLE(hi2c);
  1505. }
  1506. /* Disable Pos */
  1507. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  1508. hi2c->State = HAL_I2C_STATE_BUSY_RX;
  1509. hi2c->Mode = HAL_I2C_MODE_MASTER;
  1510. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1511. /* Prepare transfer parameters */
  1512. hi2c->pBuffPtr = pData;
  1513. hi2c->XferCount = Size;
  1514. hi2c->XferSize = hi2c->XferCount;
  1515. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  1516. hi2c->Devaddress = DevAddress;
  1517. /* Process Unlocked */
  1518. __HAL_UNLOCK(hi2c);
  1519. /* Note : The I2C interrupts must be enabled after unlocking current process
  1520. to avoid the risk of I2C interrupt handle execution before current
  1521. process unlock */
  1522. /* Enable EVT, BUF and ERR interrupt */
  1523. __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  1524. /* Enable Acknowledge */
  1525. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  1526. /* Generate Start */
  1527. SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
  1528. return HAL_OK;
  1529. }
  1530. else
  1531. {
  1532. return HAL_BUSY;
  1533. }
  1534. }
  1535. /**
  1536. * @brief Transmit in slave mode an amount of data in non-blocking mode with Interrupt
  1537. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  1538. * the configuration information for the specified I2C.
  1539. * @param pData Pointer to data buffer
  1540. * @param Size Amount of data to be sent
  1541. * @retval HAL status
  1542. */
  1543. HAL_StatusTypeDef HAL_I2C_Slave_Transmit_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
  1544. {
  1545. if (hi2c->State == HAL_I2C_STATE_READY)
  1546. {
  1547. if ((pData == NULL) || (Size == 0U))
  1548. {
  1549. return HAL_ERROR;
  1550. }
  1551. /* Process Locked */
  1552. __HAL_LOCK(hi2c);
  1553. /* Check if the I2C is already enabled */
  1554. if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  1555. {
  1556. /* Enable I2C peripheral */
  1557. __HAL_I2C_ENABLE(hi2c);
  1558. }
  1559. /* Disable Pos */
  1560. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  1561. hi2c->State = HAL_I2C_STATE_BUSY_TX;
  1562. hi2c->Mode = HAL_I2C_MODE_SLAVE;
  1563. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1564. /* Prepare transfer parameters */
  1565. hi2c->pBuffPtr = pData;
  1566. hi2c->XferCount = Size;
  1567. hi2c->XferSize = hi2c->XferCount;
  1568. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  1569. /* Enable Address Acknowledge */
  1570. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  1571. /* Process Unlocked */
  1572. __HAL_UNLOCK(hi2c);
  1573. /* Note : The I2C interrupts must be enabled after unlocking current process
  1574. to avoid the risk of I2C interrupt handle execution before current
  1575. process unlock */
  1576. /* Enable EVT, BUF and ERR interrupt */
  1577. __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  1578. return HAL_OK;
  1579. }
  1580. else
  1581. {
  1582. return HAL_BUSY;
  1583. }
  1584. }
  1585. /**
  1586. * @brief Receive in slave mode an amount of data in non-blocking mode with Interrupt
  1587. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  1588. * the configuration information for the specified I2C.
  1589. * @param pData Pointer to data buffer
  1590. * @param Size Amount of data to be sent
  1591. * @retval HAL status
  1592. */
  1593. HAL_StatusTypeDef HAL_I2C_Slave_Receive_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
  1594. {
  1595. if (hi2c->State == HAL_I2C_STATE_READY)
  1596. {
  1597. if ((pData == NULL) || (Size == 0U))
  1598. {
  1599. return HAL_ERROR;
  1600. }
  1601. /* Process Locked */
  1602. __HAL_LOCK(hi2c);
  1603. /* Check if the I2C is already enabled */
  1604. if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  1605. {
  1606. /* Enable I2C peripheral */
  1607. __HAL_I2C_ENABLE(hi2c);
  1608. }
  1609. /* Disable Pos */
  1610. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  1611. hi2c->State = HAL_I2C_STATE_BUSY_RX;
  1612. hi2c->Mode = HAL_I2C_MODE_SLAVE;
  1613. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1614. /* Prepare transfer parameters */
  1615. hi2c->pBuffPtr = pData;
  1616. hi2c->XferCount = Size;
  1617. hi2c->XferSize = hi2c->XferCount;
  1618. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  1619. /* Enable Address Acknowledge */
  1620. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  1621. /* Process Unlocked */
  1622. __HAL_UNLOCK(hi2c);
  1623. /* Note : The I2C interrupts must be enabled after unlocking current process
  1624. to avoid the risk of I2C interrupt handle execution before current
  1625. process unlock */
  1626. /* Enable EVT, BUF and ERR interrupt */
  1627. __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  1628. return HAL_OK;
  1629. }
  1630. else
  1631. {
  1632. return HAL_BUSY;
  1633. }
  1634. }
  1635. /**
  1636. * @brief Transmit in master mode an amount of data in non-blocking mode with DMA
  1637. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  1638. * the configuration information for the specified I2C.
  1639. * @param DevAddress Target device address: The device 7 bits address value
  1640. * in datasheet must be shifted to the left before calling the interface
  1641. * @param pData Pointer to data buffer
  1642. * @param Size Amount of data to be sent
  1643. * @retval HAL status
  1644. */
  1645. HAL_StatusTypeDef HAL_I2C_Master_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
  1646. {
  1647. __IO uint32_t count = 0U;
  1648. HAL_StatusTypeDef dmaxferstatus;
  1649. if (hi2c->State == HAL_I2C_STATE_READY)
  1650. {
  1651. /* Wait until BUSY flag is reset */
  1652. count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U);
  1653. do
  1654. {
  1655. count--;
  1656. if (count == 0U)
  1657. {
  1658. hi2c->PreviousState = I2C_STATE_NONE;
  1659. hi2c->State = HAL_I2C_STATE_READY;
  1660. hi2c->Mode = HAL_I2C_MODE_NONE;
  1661. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  1662. return HAL_BUSY;
  1663. }
  1664. }
  1665. while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
  1666. /* Process Locked */
  1667. __HAL_LOCK(hi2c);
  1668. /* Check if the I2C is already enabled */
  1669. if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  1670. {
  1671. /* Enable I2C peripheral */
  1672. __HAL_I2C_ENABLE(hi2c);
  1673. }
  1674. /* Disable Pos */
  1675. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  1676. hi2c->State = HAL_I2C_STATE_BUSY_TX;
  1677. hi2c->Mode = HAL_I2C_MODE_MASTER;
  1678. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1679. /* Prepare transfer parameters */
  1680. hi2c->pBuffPtr = pData;
  1681. hi2c->XferCount = Size;
  1682. hi2c->XferSize = hi2c->XferCount;
  1683. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  1684. hi2c->Devaddress = DevAddress;
  1685. if (hi2c->XferSize > 0U)
  1686. {
  1687. if (hi2c->hdmatx != NULL)
  1688. {
  1689. /* Set the I2C DMA transfer complete callback */
  1690. hi2c->hdmatx->XferCpltCallback = I2C_DMAXferCplt;
  1691. /* Set the DMA error callback */
  1692. hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
  1693. /* Set the unused DMA callbacks to NULL */
  1694. hi2c->hdmatx->XferHalfCpltCallback = NULL;
  1695. hi2c->hdmatx->XferM1CpltCallback = NULL;
  1696. hi2c->hdmatx->XferM1HalfCpltCallback = NULL;
  1697. hi2c->hdmatx->XferAbortCallback = NULL;
  1698. /* Enable the DMA stream */
  1699. dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)hi2c->pBuffPtr, (uint32_t)&hi2c->Instance->DR, hi2c->XferSize);
  1700. }
  1701. else
  1702. {
  1703. /* Update I2C state */
  1704. hi2c->State = HAL_I2C_STATE_READY;
  1705. hi2c->Mode = HAL_I2C_MODE_NONE;
  1706. /* Update I2C error code */
  1707. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA_PARAM;
  1708. /* Process Unlocked */
  1709. __HAL_UNLOCK(hi2c);
  1710. return HAL_ERROR;
  1711. }
  1712. if (dmaxferstatus == HAL_OK)
  1713. {
  1714. /* Process Unlocked */
  1715. __HAL_UNLOCK(hi2c);
  1716. /* Note : The I2C interrupts must be enabled after unlocking current process
  1717. to avoid the risk of I2C interrupt handle execution before current
  1718. process unlock */
  1719. /* Enable EVT and ERR interrupt */
  1720. __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR);
  1721. /* Enable DMA Request */
  1722. SET_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
  1723. /* Enable Acknowledge */
  1724. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  1725. /* Generate Start */
  1726. SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
  1727. }
  1728. else
  1729. {
  1730. /* Update I2C state */
  1731. hi2c->State = HAL_I2C_STATE_READY;
  1732. hi2c->Mode = HAL_I2C_MODE_NONE;
  1733. /* Update I2C error code */
  1734. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
  1735. /* Process Unlocked */
  1736. __HAL_UNLOCK(hi2c);
  1737. return HAL_ERROR;
  1738. }
  1739. }
  1740. else
  1741. {
  1742. /* Enable Acknowledge */
  1743. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  1744. /* Generate Start */
  1745. SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
  1746. /* Process Unlocked */
  1747. __HAL_UNLOCK(hi2c);
  1748. /* Note : The I2C interrupts must be enabled after unlocking current process
  1749. to avoid the risk of I2C interrupt handle execution before current
  1750. process unlock */
  1751. /* Enable EVT, BUF and ERR interrupt */
  1752. __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  1753. }
  1754. return HAL_OK;
  1755. }
  1756. else
  1757. {
  1758. return HAL_BUSY;
  1759. }
  1760. }
  1761. /**
  1762. * @brief Receive in master mode an amount of data in non-blocking mode with DMA
  1763. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  1764. * the configuration information for the specified I2C.
  1765. * @param DevAddress Target device address: The device 7 bits address value
  1766. * in datasheet must be shifted to the left before calling the interface
  1767. * @param pData Pointer to data buffer
  1768. * @param Size Amount of data to be sent
  1769. * @retval HAL status
  1770. */
  1771. HAL_StatusTypeDef HAL_I2C_Master_Receive_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
  1772. {
  1773. __IO uint32_t count = 0U;
  1774. HAL_StatusTypeDef dmaxferstatus;
  1775. if (hi2c->State == HAL_I2C_STATE_READY)
  1776. {
  1777. /* Wait until BUSY flag is reset */
  1778. count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U);
  1779. do
  1780. {
  1781. count--;
  1782. if (count == 0U)
  1783. {
  1784. hi2c->PreviousState = I2C_STATE_NONE;
  1785. hi2c->State = HAL_I2C_STATE_READY;
  1786. hi2c->Mode = HAL_I2C_MODE_NONE;
  1787. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  1788. return HAL_BUSY;
  1789. }
  1790. }
  1791. while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
  1792. /* Process Locked */
  1793. __HAL_LOCK(hi2c);
  1794. /* Check if the I2C is already enabled */
  1795. if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  1796. {
  1797. /* Enable I2C peripheral */
  1798. __HAL_I2C_ENABLE(hi2c);
  1799. }
  1800. /* Disable Pos */
  1801. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  1802. hi2c->State = HAL_I2C_STATE_BUSY_RX;
  1803. hi2c->Mode = HAL_I2C_MODE_MASTER;
  1804. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1805. /* Prepare transfer parameters */
  1806. hi2c->pBuffPtr = pData;
  1807. hi2c->XferCount = Size;
  1808. hi2c->XferSize = hi2c->XferCount;
  1809. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  1810. hi2c->Devaddress = DevAddress;
  1811. if (hi2c->XferSize > 0U)
  1812. {
  1813. if (hi2c->hdmarx != NULL)
  1814. {
  1815. /* Set the I2C DMA transfer complete callback */
  1816. hi2c->hdmarx->XferCpltCallback = I2C_DMAXferCplt;
  1817. /* Set the DMA error callback */
  1818. hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
  1819. /* Set the unused DMA callbacks to NULL */
  1820. hi2c->hdmarx->XferHalfCpltCallback = NULL;
  1821. hi2c->hdmarx->XferM1CpltCallback = NULL;
  1822. hi2c->hdmarx->XferM1HalfCpltCallback = NULL;
  1823. hi2c->hdmarx->XferAbortCallback = NULL;
  1824. /* Enable the DMA stream */
  1825. dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->DR, (uint32_t)hi2c->pBuffPtr, hi2c->XferSize);
  1826. }
  1827. else
  1828. {
  1829. /* Update I2C state */
  1830. hi2c->State = HAL_I2C_STATE_READY;
  1831. hi2c->Mode = HAL_I2C_MODE_NONE;
  1832. /* Update I2C error code */
  1833. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA_PARAM;
  1834. /* Process Unlocked */
  1835. __HAL_UNLOCK(hi2c);
  1836. return HAL_ERROR;
  1837. }
  1838. if (dmaxferstatus == HAL_OK)
  1839. {
  1840. /* Enable Acknowledge */
  1841. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  1842. /* Generate Start */
  1843. SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
  1844. /* Process Unlocked */
  1845. __HAL_UNLOCK(hi2c);
  1846. /* Note : The I2C interrupts must be enabled after unlocking current process
  1847. to avoid the risk of I2C interrupt handle execution before current
  1848. process unlock */
  1849. /* Enable EVT and ERR interrupt */
  1850. __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR);
  1851. /* Enable DMA Request */
  1852. SET_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
  1853. }
  1854. else
  1855. {
  1856. /* Update I2C state */
  1857. hi2c->State = HAL_I2C_STATE_READY;
  1858. hi2c->Mode = HAL_I2C_MODE_NONE;
  1859. /* Update I2C error code */
  1860. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
  1861. /* Process Unlocked */
  1862. __HAL_UNLOCK(hi2c);
  1863. return HAL_ERROR;
  1864. }
  1865. }
  1866. else
  1867. {
  1868. /* Process Unlocked */
  1869. __HAL_UNLOCK(hi2c);
  1870. /* Note : The I2C interrupts must be enabled after unlocking current process
  1871. to avoid the risk of I2C interrupt handle execution before current
  1872. process unlock */
  1873. /* Enable EVT, BUF and ERR interrupt */
  1874. __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  1875. /* Enable Acknowledge */
  1876. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  1877. /* Generate Start */
  1878. SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
  1879. }
  1880. return HAL_OK;
  1881. }
  1882. else
  1883. {
  1884. return HAL_BUSY;
  1885. }
  1886. }
  1887. /**
  1888. * @brief Transmit in slave mode an amount of data in non-blocking mode with DMA
  1889. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  1890. * the configuration information for the specified I2C.
  1891. * @param pData Pointer to data buffer
  1892. * @param Size Amount of data to be sent
  1893. * @retval HAL status
  1894. */
  1895. HAL_StatusTypeDef HAL_I2C_Slave_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
  1896. {
  1897. HAL_StatusTypeDef dmaxferstatus;
  1898. if (hi2c->State == HAL_I2C_STATE_READY)
  1899. {
  1900. if ((pData == NULL) || (Size == 0U))
  1901. {
  1902. return HAL_ERROR;
  1903. }
  1904. /* Process Locked */
  1905. __HAL_LOCK(hi2c);
  1906. /* Check if the I2C is already enabled */
  1907. if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  1908. {
  1909. /* Enable I2C peripheral */
  1910. __HAL_I2C_ENABLE(hi2c);
  1911. }
  1912. /* Disable Pos */
  1913. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  1914. hi2c->State = HAL_I2C_STATE_BUSY_TX;
  1915. hi2c->Mode = HAL_I2C_MODE_SLAVE;
  1916. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1917. /* Prepare transfer parameters */
  1918. hi2c->pBuffPtr = pData;
  1919. hi2c->XferCount = Size;
  1920. hi2c->XferSize = hi2c->XferCount;
  1921. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  1922. if (hi2c->hdmatx != NULL)
  1923. {
  1924. /* Set the I2C DMA transfer complete callback */
  1925. hi2c->hdmatx->XferCpltCallback = I2C_DMAXferCplt;
  1926. /* Set the DMA error callback */
  1927. hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
  1928. /* Set the unused DMA callbacks to NULL */
  1929. hi2c->hdmatx->XferHalfCpltCallback = NULL;
  1930. hi2c->hdmatx->XferM1CpltCallback = NULL;
  1931. hi2c->hdmatx->XferM1HalfCpltCallback = NULL;
  1932. hi2c->hdmatx->XferAbortCallback = NULL;
  1933. /* Enable the DMA stream */
  1934. dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)hi2c->pBuffPtr, (uint32_t)&hi2c->Instance->DR, hi2c->XferSize);
  1935. }
  1936. else
  1937. {
  1938. /* Update I2C state */
  1939. hi2c->State = HAL_I2C_STATE_LISTEN;
  1940. hi2c->Mode = HAL_I2C_MODE_NONE;
  1941. /* Update I2C error code */
  1942. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA_PARAM;
  1943. /* Process Unlocked */
  1944. __HAL_UNLOCK(hi2c);
  1945. return HAL_ERROR;
  1946. }
  1947. if (dmaxferstatus == HAL_OK)
  1948. {
  1949. /* Enable Address Acknowledge */
  1950. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  1951. /* Process Unlocked */
  1952. __HAL_UNLOCK(hi2c);
  1953. /* Note : The I2C interrupts must be enabled after unlocking current process
  1954. to avoid the risk of I2C interrupt handle execution before current
  1955. process unlock */
  1956. /* Enable EVT and ERR interrupt */
  1957. __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR);
  1958. /* Enable DMA Request */
  1959. hi2c->Instance->CR2 |= I2C_CR2_DMAEN;
  1960. return HAL_OK;
  1961. }
  1962. else
  1963. {
  1964. /* Update I2C state */
  1965. hi2c->State = HAL_I2C_STATE_READY;
  1966. hi2c->Mode = HAL_I2C_MODE_NONE;
  1967. /* Update I2C error code */
  1968. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
  1969. /* Process Unlocked */
  1970. __HAL_UNLOCK(hi2c);
  1971. return HAL_ERROR;
  1972. }
  1973. }
  1974. else
  1975. {
  1976. return HAL_BUSY;
  1977. }
  1978. }
  1979. /**
  1980. * @brief Receive in slave mode an amount of data in non-blocking mode with DMA
  1981. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  1982. * the configuration information for the specified I2C.
  1983. * @param pData Pointer to data buffer
  1984. * @param Size Amount of data to be sent
  1985. * @retval HAL status
  1986. */
  1987. HAL_StatusTypeDef HAL_I2C_Slave_Receive_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
  1988. {
  1989. HAL_StatusTypeDef dmaxferstatus;
  1990. if (hi2c->State == HAL_I2C_STATE_READY)
  1991. {
  1992. if ((pData == NULL) || (Size == 0U))
  1993. {
  1994. return HAL_ERROR;
  1995. }
  1996. /* Process Locked */
  1997. __HAL_LOCK(hi2c);
  1998. /* Check if the I2C is already enabled */
  1999. if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  2000. {
  2001. /* Enable I2C peripheral */
  2002. __HAL_I2C_ENABLE(hi2c);
  2003. }
  2004. /* Disable Pos */
  2005. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  2006. hi2c->State = HAL_I2C_STATE_BUSY_RX;
  2007. hi2c->Mode = HAL_I2C_MODE_SLAVE;
  2008. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  2009. /* Prepare transfer parameters */
  2010. hi2c->pBuffPtr = pData;
  2011. hi2c->XferCount = Size;
  2012. hi2c->XferSize = hi2c->XferCount;
  2013. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  2014. if (hi2c->hdmarx != NULL)
  2015. {
  2016. /* Set the I2C DMA transfer complete callback */
  2017. hi2c->hdmarx->XferCpltCallback = I2C_DMAXferCplt;
  2018. /* Set the DMA error callback */
  2019. hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
  2020. /* Set the unused DMA callbacks to NULL */
  2021. hi2c->hdmarx->XferHalfCpltCallback = NULL;
  2022. hi2c->hdmarx->XferM1CpltCallback = NULL;
  2023. hi2c->hdmarx->XferM1HalfCpltCallback = NULL;
  2024. hi2c->hdmarx->XferAbortCallback = NULL;
  2025. /* Enable the DMA stream */
  2026. dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->DR, (uint32_t)hi2c->pBuffPtr, hi2c->XferSize);
  2027. }
  2028. else
  2029. {
  2030. /* Update I2C state */
  2031. hi2c->State = HAL_I2C_STATE_LISTEN;
  2032. hi2c->Mode = HAL_I2C_MODE_NONE;
  2033. /* Update I2C error code */
  2034. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA_PARAM;
  2035. /* Process Unlocked */
  2036. __HAL_UNLOCK(hi2c);
  2037. return HAL_ERROR;
  2038. }
  2039. if (dmaxferstatus == HAL_OK)
  2040. {
  2041. /* Enable Address Acknowledge */
  2042. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  2043. /* Process Unlocked */
  2044. __HAL_UNLOCK(hi2c);
  2045. /* Note : The I2C interrupts must be enabled after unlocking current process
  2046. to avoid the risk of I2C interrupt handle execution before current
  2047. process unlock */
  2048. /* Enable EVT and ERR interrupt */
  2049. __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR);
  2050. /* Enable DMA Request */
  2051. SET_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
  2052. return HAL_OK;
  2053. }
  2054. else
  2055. {
  2056. /* Update I2C state */
  2057. hi2c->State = HAL_I2C_STATE_READY;
  2058. hi2c->Mode = HAL_I2C_MODE_NONE;
  2059. /* Update I2C error code */
  2060. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
  2061. /* Process Unlocked */
  2062. __HAL_UNLOCK(hi2c);
  2063. return HAL_ERROR;
  2064. }
  2065. }
  2066. else
  2067. {
  2068. return HAL_BUSY;
  2069. }
  2070. }
  2071. /**
  2072. * @brief Write an amount of data in blocking mode to a specific memory address
  2073. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2074. * the configuration information for the specified I2C.
  2075. * @param DevAddress Target device address: The device 7 bits address value
  2076. * in datasheet must be shifted to the left before calling the interface
  2077. * @param MemAddress Internal memory address
  2078. * @param MemAddSize Size of internal memory address
  2079. * @param pData Pointer to data buffer
  2080. * @param Size Amount of data to be sent
  2081. * @param Timeout Timeout duration
  2082. * @retval HAL status
  2083. */
  2084. HAL_StatusTypeDef HAL_I2C_Mem_Write(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  2085. {
  2086. /* Init tickstart for timeout management*/
  2087. uint32_t tickstart = HAL_GetTick();
  2088. /* Check the parameters */
  2089. assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
  2090. if (hi2c->State == HAL_I2C_STATE_READY)
  2091. {
  2092. /* Wait until BUSY flag is reset */
  2093. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY_FLAG, tickstart) != HAL_OK)
  2094. {
  2095. return HAL_BUSY;
  2096. }
  2097. /* Process Locked */
  2098. __HAL_LOCK(hi2c);
  2099. /* Check if the I2C is already enabled */
  2100. if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  2101. {
  2102. /* Enable I2C peripheral */
  2103. __HAL_I2C_ENABLE(hi2c);
  2104. }
  2105. /* Disable Pos */
  2106. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  2107. hi2c->State = HAL_I2C_STATE_BUSY_TX;
  2108. hi2c->Mode = HAL_I2C_MODE_MEM;
  2109. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  2110. /* Prepare transfer parameters */
  2111. hi2c->pBuffPtr = pData;
  2112. hi2c->XferCount = Size;
  2113. hi2c->XferSize = hi2c->XferCount;
  2114. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  2115. /* Send Slave Address and Memory Address */
  2116. if (I2C_RequestMemoryWrite(hi2c, DevAddress, MemAddress, MemAddSize, Timeout, tickstart) != HAL_OK)
  2117. {
  2118. return HAL_ERROR;
  2119. }
  2120. while (hi2c->XferSize > 0U)
  2121. {
  2122. /* Wait until TXE flag is set */
  2123. if (I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
  2124. {
  2125. if (hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  2126. {
  2127. /* Generate Stop */
  2128. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  2129. }
  2130. return HAL_ERROR;
  2131. }
  2132. /* Write data to DR */
  2133. hi2c->Instance->DR = *hi2c->pBuffPtr;
  2134. /* Increment Buffer pointer */
  2135. hi2c->pBuffPtr++;
  2136. /* Update counter */
  2137. hi2c->XferSize--;
  2138. hi2c->XferCount--;
  2139. if ((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET) && (hi2c->XferSize != 0U))
  2140. {
  2141. /* Write data to DR */
  2142. hi2c->Instance->DR = *hi2c->pBuffPtr;
  2143. /* Increment Buffer pointer */
  2144. hi2c->pBuffPtr++;
  2145. /* Update counter */
  2146. hi2c->XferSize--;
  2147. hi2c->XferCount--;
  2148. }
  2149. }
  2150. /* Wait until BTF flag is set */
  2151. if (I2C_WaitOnBTFFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
  2152. {
  2153. if (hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  2154. {
  2155. /* Generate Stop */
  2156. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  2157. }
  2158. return HAL_ERROR;
  2159. }
  2160. /* Generate Stop */
  2161. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  2162. hi2c->State = HAL_I2C_STATE_READY;
  2163. hi2c->Mode = HAL_I2C_MODE_NONE;
  2164. /* Process Unlocked */
  2165. __HAL_UNLOCK(hi2c);
  2166. return HAL_OK;
  2167. }
  2168. else
  2169. {
  2170. return HAL_BUSY;
  2171. }
  2172. }
  2173. /**
  2174. * @brief Read an amount of data in blocking mode from a specific memory address
  2175. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2176. * the configuration information for the specified I2C.
  2177. * @param DevAddress Target device address: The device 7 bits address value
  2178. * in datasheet must be shifted to the left before calling the interface
  2179. * @param MemAddress Internal memory address
  2180. * @param MemAddSize Size of internal memory address
  2181. * @param pData Pointer to data buffer
  2182. * @param Size Amount of data to be sent
  2183. * @param Timeout Timeout duration
  2184. * @retval HAL status
  2185. */
  2186. HAL_StatusTypeDef HAL_I2C_Mem_Read(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  2187. {
  2188. /* Init tickstart for timeout management*/
  2189. uint32_t tickstart = HAL_GetTick();
  2190. /* Check the parameters */
  2191. assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
  2192. if (hi2c->State == HAL_I2C_STATE_READY)
  2193. {
  2194. /* Wait until BUSY flag is reset */
  2195. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY_FLAG, tickstart) != HAL_OK)
  2196. {
  2197. return HAL_BUSY;
  2198. }
  2199. /* Process Locked */
  2200. __HAL_LOCK(hi2c);
  2201. /* Check if the I2C is already enabled */
  2202. if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  2203. {
  2204. /* Enable I2C peripheral */
  2205. __HAL_I2C_ENABLE(hi2c);
  2206. }
  2207. /* Disable Pos */
  2208. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  2209. hi2c->State = HAL_I2C_STATE_BUSY_RX;
  2210. hi2c->Mode = HAL_I2C_MODE_MEM;
  2211. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  2212. /* Prepare transfer parameters */
  2213. hi2c->pBuffPtr = pData;
  2214. hi2c->XferCount = Size;
  2215. hi2c->XferSize = hi2c->XferCount;
  2216. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  2217. /* Send Slave Address and Memory Address */
  2218. if (I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, Timeout, tickstart) != HAL_OK)
  2219. {
  2220. return HAL_ERROR;
  2221. }
  2222. if (hi2c->XferSize == 0U)
  2223. {
  2224. /* Clear ADDR flag */
  2225. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  2226. /* Generate Stop */
  2227. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  2228. }
  2229. else if (hi2c->XferSize == 1U)
  2230. {
  2231. /* Disable Acknowledge */
  2232. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  2233. /* Clear ADDR flag */
  2234. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  2235. /* Generate Stop */
  2236. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  2237. }
  2238. else if (hi2c->XferSize == 2U)
  2239. {
  2240. /* Disable Acknowledge */
  2241. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  2242. /* Enable Pos */
  2243. SET_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  2244. /* Clear ADDR flag */
  2245. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  2246. }
  2247. else
  2248. {
  2249. /* Clear ADDR flag */
  2250. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  2251. }
  2252. while (hi2c->XferSize > 0U)
  2253. {
  2254. if (hi2c->XferSize <= 3U)
  2255. {
  2256. /* One byte */
  2257. if (hi2c->XferSize == 1U)
  2258. {
  2259. /* Wait until RXNE flag is set */
  2260. if (I2C_WaitOnRXNEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
  2261. {
  2262. return HAL_ERROR;
  2263. }
  2264. /* Read data from DR */
  2265. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
  2266. /* Increment Buffer pointer */
  2267. hi2c->pBuffPtr++;
  2268. /* Update counter */
  2269. hi2c->XferSize--;
  2270. hi2c->XferCount--;
  2271. }
  2272. /* Two bytes */
  2273. else if (hi2c->XferSize == 2U)
  2274. {
  2275. /* Wait until BTF flag is set */
  2276. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BTF, RESET, Timeout, tickstart) != HAL_OK)
  2277. {
  2278. return HAL_ERROR;
  2279. }
  2280. /* Generate Stop */
  2281. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  2282. /* Read data from DR */
  2283. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
  2284. /* Increment Buffer pointer */
  2285. hi2c->pBuffPtr++;
  2286. /* Update counter */
  2287. hi2c->XferSize--;
  2288. hi2c->XferCount--;
  2289. /* Read data from DR */
  2290. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
  2291. /* Increment Buffer pointer */
  2292. hi2c->pBuffPtr++;
  2293. /* Update counter */
  2294. hi2c->XferSize--;
  2295. hi2c->XferCount--;
  2296. }
  2297. /* 3 Last bytes */
  2298. else
  2299. {
  2300. /* Wait until BTF flag is set */
  2301. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BTF, RESET, Timeout, tickstart) != HAL_OK)
  2302. {
  2303. return HAL_ERROR;
  2304. }
  2305. /* Disable Acknowledge */
  2306. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  2307. /* Read data from DR */
  2308. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
  2309. /* Increment Buffer pointer */
  2310. hi2c->pBuffPtr++;
  2311. /* Update counter */
  2312. hi2c->XferSize--;
  2313. hi2c->XferCount--;
  2314. /* Wait until BTF flag is set */
  2315. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BTF, RESET, Timeout, tickstart) != HAL_OK)
  2316. {
  2317. return HAL_ERROR;
  2318. }
  2319. /* Generate Stop */
  2320. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  2321. /* Read data from DR */
  2322. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
  2323. /* Increment Buffer pointer */
  2324. hi2c->pBuffPtr++;
  2325. /* Update counter */
  2326. hi2c->XferSize--;
  2327. hi2c->XferCount--;
  2328. /* Read data from DR */
  2329. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
  2330. /* Increment Buffer pointer */
  2331. hi2c->pBuffPtr++;
  2332. /* Update counter */
  2333. hi2c->XferSize--;
  2334. hi2c->XferCount--;
  2335. }
  2336. }
  2337. else
  2338. {
  2339. /* Wait until RXNE flag is set */
  2340. if (I2C_WaitOnRXNEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
  2341. {
  2342. return HAL_ERROR;
  2343. }
  2344. /* Read data from DR */
  2345. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
  2346. /* Increment Buffer pointer */
  2347. hi2c->pBuffPtr++;
  2348. /* Update counter */
  2349. hi2c->XferSize--;
  2350. hi2c->XferCount--;
  2351. if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET)
  2352. {
  2353. if (hi2c->XferSize == 3U)
  2354. {
  2355. /* Disable Acknowledge */
  2356. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  2357. }
  2358. /* Read data from DR */
  2359. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
  2360. /* Increment Buffer pointer */
  2361. hi2c->pBuffPtr++;
  2362. /* Update counter */
  2363. hi2c->XferSize--;
  2364. hi2c->XferCount--;
  2365. }
  2366. }
  2367. }
  2368. hi2c->State = HAL_I2C_STATE_READY;
  2369. hi2c->Mode = HAL_I2C_MODE_NONE;
  2370. /* Process Unlocked */
  2371. __HAL_UNLOCK(hi2c);
  2372. return HAL_OK;
  2373. }
  2374. else
  2375. {
  2376. return HAL_BUSY;
  2377. }
  2378. }
  2379. /**
  2380. * @brief Write an amount of data in non-blocking mode with Interrupt to a specific memory address
  2381. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2382. * the configuration information for the specified I2C.
  2383. * @param DevAddress Target device address: The device 7 bits address value
  2384. * in datasheet must be shifted to the left before calling the interface
  2385. * @param MemAddress Internal memory address
  2386. * @param MemAddSize Size of internal memory address
  2387. * @param pData Pointer to data buffer
  2388. * @param Size Amount of data to be sent
  2389. * @retval HAL status
  2390. */
  2391. HAL_StatusTypeDef HAL_I2C_Mem_Write_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size)
  2392. {
  2393. __IO uint32_t count = 0U;
  2394. /* Check the parameters */
  2395. assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
  2396. if (hi2c->State == HAL_I2C_STATE_READY)
  2397. {
  2398. /* Wait until BUSY flag is reset */
  2399. count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U);
  2400. do
  2401. {
  2402. count--;
  2403. if (count == 0U)
  2404. {
  2405. hi2c->PreviousState = I2C_STATE_NONE;
  2406. hi2c->State = HAL_I2C_STATE_READY;
  2407. hi2c->Mode = HAL_I2C_MODE_NONE;
  2408. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  2409. return HAL_BUSY;
  2410. }
  2411. }
  2412. while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
  2413. /* Process Locked */
  2414. __HAL_LOCK(hi2c);
  2415. /* Check if the I2C is already enabled */
  2416. if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  2417. {
  2418. /* Enable I2C peripheral */
  2419. __HAL_I2C_ENABLE(hi2c);
  2420. }
  2421. /* Disable Pos */
  2422. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  2423. hi2c->State = HAL_I2C_STATE_BUSY_TX;
  2424. hi2c->Mode = HAL_I2C_MODE_MEM;
  2425. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  2426. /* Prepare transfer parameters */
  2427. hi2c->pBuffPtr = pData;
  2428. hi2c->XferCount = Size;
  2429. hi2c->XferSize = hi2c->XferCount;
  2430. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  2431. hi2c->Devaddress = DevAddress;
  2432. hi2c->Memaddress = MemAddress;
  2433. hi2c->MemaddSize = MemAddSize;
  2434. hi2c->EventCount = 0U;
  2435. /* Generate Start */
  2436. SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
  2437. /* Process Unlocked */
  2438. __HAL_UNLOCK(hi2c);
  2439. /* Note : The I2C interrupts must be enabled after unlocking current process
  2440. to avoid the risk of I2C interrupt handle execution before current
  2441. process unlock */
  2442. /* Enable EVT, BUF and ERR interrupt */
  2443. __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  2444. return HAL_OK;
  2445. }
  2446. else
  2447. {
  2448. return HAL_BUSY;
  2449. }
  2450. }
  2451. /**
  2452. * @brief Read an amount of data in non-blocking mode with Interrupt from a specific memory address
  2453. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2454. * the configuration information for the specified I2C.
  2455. * @param DevAddress Target device address
  2456. * @param MemAddress Internal memory address
  2457. * @param MemAddSize Size of internal memory address
  2458. * @param pData Pointer to data buffer
  2459. * @param Size Amount of data to be sent
  2460. * @retval HAL status
  2461. */
  2462. HAL_StatusTypeDef HAL_I2C_Mem_Read_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size)
  2463. {
  2464. __IO uint32_t count = 0U;
  2465. /* Check the parameters */
  2466. assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
  2467. if (hi2c->State == HAL_I2C_STATE_READY)
  2468. {
  2469. /* Wait until BUSY flag is reset */
  2470. count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U);
  2471. do
  2472. {
  2473. count--;
  2474. if (count == 0U)
  2475. {
  2476. hi2c->PreviousState = I2C_STATE_NONE;
  2477. hi2c->State = HAL_I2C_STATE_READY;
  2478. hi2c->Mode = HAL_I2C_MODE_NONE;
  2479. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  2480. return HAL_BUSY;
  2481. }
  2482. }
  2483. while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
  2484. /* Process Locked */
  2485. __HAL_LOCK(hi2c);
  2486. /* Check if the I2C is already enabled */
  2487. if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  2488. {
  2489. /* Enable I2C peripheral */
  2490. __HAL_I2C_ENABLE(hi2c);
  2491. }
  2492. /* Disable Pos */
  2493. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  2494. hi2c->State = HAL_I2C_STATE_BUSY_RX;
  2495. hi2c->Mode = HAL_I2C_MODE_MEM;
  2496. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  2497. /* Prepare transfer parameters */
  2498. hi2c->pBuffPtr = pData;
  2499. hi2c->XferCount = Size;
  2500. hi2c->XferSize = hi2c->XferCount;
  2501. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  2502. hi2c->Devaddress = DevAddress;
  2503. hi2c->Memaddress = MemAddress;
  2504. hi2c->MemaddSize = MemAddSize;
  2505. hi2c->EventCount = 0U;
  2506. /* Enable Acknowledge */
  2507. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  2508. /* Generate Start */
  2509. SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
  2510. /* Process Unlocked */
  2511. __HAL_UNLOCK(hi2c);
  2512. if (hi2c->XferSize > 0U)
  2513. {
  2514. /* Note : The I2C interrupts must be enabled after unlocking current process
  2515. to avoid the risk of I2C interrupt handle execution before current
  2516. process unlock */
  2517. /* Enable EVT, BUF and ERR interrupt */
  2518. __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  2519. }
  2520. return HAL_OK;
  2521. }
  2522. else
  2523. {
  2524. return HAL_BUSY;
  2525. }
  2526. }
  2527. /**
  2528. * @brief Write an amount of data in non-blocking mode with DMA to a specific memory address
  2529. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2530. * the configuration information for the specified I2C.
  2531. * @param DevAddress Target device address: The device 7 bits address value
  2532. * in datasheet must be shifted to the left before calling the interface
  2533. * @param MemAddress Internal memory address
  2534. * @param MemAddSize Size of internal memory address
  2535. * @param pData Pointer to data buffer
  2536. * @param Size Amount of data to be sent
  2537. * @retval HAL status
  2538. */
  2539. HAL_StatusTypeDef HAL_I2C_Mem_Write_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size)
  2540. {
  2541. __IO uint32_t count = 0U;
  2542. HAL_StatusTypeDef dmaxferstatus;
  2543. /* Init tickstart for timeout management*/
  2544. uint32_t tickstart = HAL_GetTick();
  2545. /* Check the parameters */
  2546. assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
  2547. if (hi2c->State == HAL_I2C_STATE_READY)
  2548. {
  2549. /* Wait until BUSY flag is reset */
  2550. count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U);
  2551. do
  2552. {
  2553. count--;
  2554. if (count == 0U)
  2555. {
  2556. hi2c->PreviousState = I2C_STATE_NONE;
  2557. hi2c->State = HAL_I2C_STATE_READY;
  2558. hi2c->Mode = HAL_I2C_MODE_NONE;
  2559. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  2560. return HAL_BUSY;
  2561. }
  2562. }
  2563. while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
  2564. /* Process Locked */
  2565. __HAL_LOCK(hi2c);
  2566. /* Check if the I2C is already enabled */
  2567. if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  2568. {
  2569. /* Enable I2C peripheral */
  2570. __HAL_I2C_ENABLE(hi2c);
  2571. }
  2572. /* Disable Pos */
  2573. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  2574. hi2c->State = HAL_I2C_STATE_BUSY_TX;
  2575. hi2c->Mode = HAL_I2C_MODE_MEM;
  2576. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  2577. /* Prepare transfer parameters */
  2578. hi2c->pBuffPtr = pData;
  2579. hi2c->XferCount = Size;
  2580. hi2c->XferSize = hi2c->XferCount;
  2581. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  2582. hi2c->Devaddress = DevAddress;
  2583. hi2c->Memaddress = MemAddress;
  2584. hi2c->MemaddSize = MemAddSize;
  2585. hi2c->EventCount = 0U;
  2586. if (hi2c->XferSize > 0U)
  2587. {
  2588. if (hi2c->hdmatx != NULL)
  2589. {
  2590. /* Set the I2C DMA transfer complete callback */
  2591. hi2c->hdmatx->XferCpltCallback = I2C_DMAXferCplt;
  2592. /* Set the DMA error callback */
  2593. hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
  2594. /* Set the unused DMA callbacks to NULL */
  2595. hi2c->hdmatx->XferHalfCpltCallback = NULL;
  2596. hi2c->hdmatx->XferM1CpltCallback = NULL;
  2597. hi2c->hdmatx->XferM1HalfCpltCallback = NULL;
  2598. hi2c->hdmatx->XferAbortCallback = NULL;
  2599. /* Enable the DMA stream */
  2600. dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)hi2c->pBuffPtr, (uint32_t)&hi2c->Instance->DR, hi2c->XferSize);
  2601. }
  2602. else
  2603. {
  2604. /* Update I2C state */
  2605. hi2c->State = HAL_I2C_STATE_READY;
  2606. hi2c->Mode = HAL_I2C_MODE_NONE;
  2607. /* Update I2C error code */
  2608. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA_PARAM;
  2609. /* Process Unlocked */
  2610. __HAL_UNLOCK(hi2c);
  2611. return HAL_ERROR;
  2612. }
  2613. if (dmaxferstatus == HAL_OK)
  2614. {
  2615. /* Send Slave Address and Memory Address */
  2616. if (I2C_RequestMemoryWrite(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG, tickstart) != HAL_OK)
  2617. {
  2618. /* Abort the ongoing DMA */
  2619. dmaxferstatus = HAL_DMA_Abort_IT(hi2c->hdmatx);
  2620. /* Prevent unused argument(s) compilation and MISRA warning */
  2621. UNUSED(dmaxferstatus);
  2622. /* Set the unused I2C DMA transfer complete callback to NULL */
  2623. hi2c->hdmatx->XferCpltCallback = NULL;
  2624. /* Disable Acknowledge */
  2625. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  2626. hi2c->XferSize = 0U;
  2627. hi2c->XferCount = 0U;
  2628. /* Disable I2C peripheral to prevent dummy data in buffer */
  2629. __HAL_I2C_DISABLE(hi2c);
  2630. return HAL_ERROR;
  2631. }
  2632. /* Clear ADDR flag */
  2633. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  2634. /* Process Unlocked */
  2635. __HAL_UNLOCK(hi2c);
  2636. /* Note : The I2C interrupts must be enabled after unlocking current process
  2637. to avoid the risk of I2C interrupt handle execution before current
  2638. process unlock */
  2639. /* Enable ERR interrupt */
  2640. __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_ERR);
  2641. /* Enable DMA Request */
  2642. SET_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
  2643. return HAL_OK;
  2644. }
  2645. else
  2646. {
  2647. /* Update I2C state */
  2648. hi2c->State = HAL_I2C_STATE_READY;
  2649. hi2c->Mode = HAL_I2C_MODE_NONE;
  2650. /* Update I2C error code */
  2651. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
  2652. /* Process Unlocked */
  2653. __HAL_UNLOCK(hi2c);
  2654. return HAL_ERROR;
  2655. }
  2656. }
  2657. else
  2658. {
  2659. /* Update I2C state */
  2660. hi2c->State = HAL_I2C_STATE_READY;
  2661. hi2c->Mode = HAL_I2C_MODE_NONE;
  2662. /* Update I2C error code */
  2663. hi2c->ErrorCode |= HAL_I2C_ERROR_SIZE;
  2664. /* Process Unlocked */
  2665. __HAL_UNLOCK(hi2c);
  2666. return HAL_ERROR;
  2667. }
  2668. }
  2669. else
  2670. {
  2671. return HAL_BUSY;
  2672. }
  2673. }
  2674. /**
  2675. * @brief Reads an amount of data in non-blocking mode with DMA from a specific memory address.
  2676. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2677. * the configuration information for the specified I2C.
  2678. * @param DevAddress Target device address: The device 7 bits address value
  2679. * in datasheet must be shifted to the left before calling the interface
  2680. * @param MemAddress Internal memory address
  2681. * @param MemAddSize Size of internal memory address
  2682. * @param pData Pointer to data buffer
  2683. * @param Size Amount of data to be read
  2684. * @retval HAL status
  2685. */
  2686. HAL_StatusTypeDef HAL_I2C_Mem_Read_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size)
  2687. {
  2688. /* Init tickstart for timeout management*/
  2689. uint32_t tickstart = HAL_GetTick();
  2690. __IO uint32_t count = 0U;
  2691. HAL_StatusTypeDef dmaxferstatus;
  2692. /* Check the parameters */
  2693. assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
  2694. if (hi2c->State == HAL_I2C_STATE_READY)
  2695. {
  2696. /* Wait until BUSY flag is reset */
  2697. count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U);
  2698. do
  2699. {
  2700. count--;
  2701. if (count == 0U)
  2702. {
  2703. hi2c->PreviousState = I2C_STATE_NONE;
  2704. hi2c->State = HAL_I2C_STATE_READY;
  2705. hi2c->Mode = HAL_I2C_MODE_NONE;
  2706. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  2707. return HAL_BUSY;
  2708. }
  2709. }
  2710. while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
  2711. /* Process Locked */
  2712. __HAL_LOCK(hi2c);
  2713. /* Check if the I2C is already enabled */
  2714. if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  2715. {
  2716. /* Enable I2C peripheral */
  2717. __HAL_I2C_ENABLE(hi2c);
  2718. }
  2719. /* Disable Pos */
  2720. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  2721. hi2c->State = HAL_I2C_STATE_BUSY_RX;
  2722. hi2c->Mode = HAL_I2C_MODE_MEM;
  2723. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  2724. /* Prepare transfer parameters */
  2725. hi2c->pBuffPtr = pData;
  2726. hi2c->XferCount = Size;
  2727. hi2c->XferSize = hi2c->XferCount;
  2728. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  2729. hi2c->Devaddress = DevAddress;
  2730. hi2c->Memaddress = MemAddress;
  2731. hi2c->MemaddSize = MemAddSize;
  2732. hi2c->EventCount = 0U;
  2733. if (hi2c->XferSize > 0U)
  2734. {
  2735. if (hi2c->hdmarx != NULL)
  2736. {
  2737. /* Set the I2C DMA transfer complete callback */
  2738. hi2c->hdmarx->XferCpltCallback = I2C_DMAXferCplt;
  2739. /* Set the DMA error callback */
  2740. hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
  2741. /* Set the unused DMA callbacks to NULL */
  2742. hi2c->hdmarx->XferHalfCpltCallback = NULL;
  2743. hi2c->hdmarx->XferM1CpltCallback = NULL;
  2744. hi2c->hdmarx->XferM1HalfCpltCallback = NULL;
  2745. hi2c->hdmarx->XferAbortCallback = NULL;
  2746. /* Enable the DMA stream */
  2747. dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->DR, (uint32_t)hi2c->pBuffPtr, hi2c->XferSize);
  2748. }
  2749. else
  2750. {
  2751. /* Update I2C state */
  2752. hi2c->State = HAL_I2C_STATE_READY;
  2753. hi2c->Mode = HAL_I2C_MODE_NONE;
  2754. /* Update I2C error code */
  2755. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA_PARAM;
  2756. /* Process Unlocked */
  2757. __HAL_UNLOCK(hi2c);
  2758. return HAL_ERROR;
  2759. }
  2760. if (dmaxferstatus == HAL_OK)
  2761. {
  2762. /* Send Slave Address and Memory Address */
  2763. if (I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG, tickstart) != HAL_OK)
  2764. {
  2765. /* Abort the ongoing DMA */
  2766. dmaxferstatus = HAL_DMA_Abort_IT(hi2c->hdmarx);
  2767. /* Prevent unused argument(s) compilation and MISRA warning */
  2768. UNUSED(dmaxferstatus);
  2769. /* Set the unused I2C DMA transfer complete callback to NULL */
  2770. hi2c->hdmarx->XferCpltCallback = NULL;
  2771. /* Disable Acknowledge */
  2772. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  2773. hi2c->XferSize = 0U;
  2774. hi2c->XferCount = 0U;
  2775. /* Disable I2C peripheral to prevent dummy data in buffer */
  2776. __HAL_I2C_DISABLE(hi2c);
  2777. return HAL_ERROR;
  2778. }
  2779. if (hi2c->XferSize == 1U)
  2780. {
  2781. /* Disable Acknowledge */
  2782. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  2783. }
  2784. else
  2785. {
  2786. /* Enable Last DMA bit */
  2787. SET_BIT(hi2c->Instance->CR2, I2C_CR2_LAST);
  2788. }
  2789. /* Clear ADDR flag */
  2790. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  2791. /* Process Unlocked */
  2792. __HAL_UNLOCK(hi2c);
  2793. /* Note : The I2C interrupts must be enabled after unlocking current process
  2794. to avoid the risk of I2C interrupt handle execution before current
  2795. process unlock */
  2796. /* Enable ERR interrupt */
  2797. __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_ERR);
  2798. /* Enable DMA Request */
  2799. hi2c->Instance->CR2 |= I2C_CR2_DMAEN;
  2800. }
  2801. else
  2802. {
  2803. /* Update I2C state */
  2804. hi2c->State = HAL_I2C_STATE_READY;
  2805. hi2c->Mode = HAL_I2C_MODE_NONE;
  2806. /* Update I2C error code */
  2807. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
  2808. /* Process Unlocked */
  2809. __HAL_UNLOCK(hi2c);
  2810. return HAL_ERROR;
  2811. }
  2812. }
  2813. else
  2814. {
  2815. /* Send Slave Address and Memory Address */
  2816. if (I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG, tickstart) != HAL_OK)
  2817. {
  2818. return HAL_ERROR;
  2819. }
  2820. /* Clear ADDR flag */
  2821. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  2822. /* Generate Stop */
  2823. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  2824. hi2c->State = HAL_I2C_STATE_READY;
  2825. /* Process Unlocked */
  2826. __HAL_UNLOCK(hi2c);
  2827. }
  2828. return HAL_OK;
  2829. }
  2830. else
  2831. {
  2832. return HAL_BUSY;
  2833. }
  2834. }
  2835. /**
  2836. * @brief Checks if target device is ready for communication.
  2837. * @note This function is used with Memory devices
  2838. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2839. * the configuration information for the specified I2C.
  2840. * @param DevAddress Target device address: The device 7 bits address value
  2841. * in datasheet must be shifted to the left before calling the interface
  2842. * @param Trials Number of trials
  2843. * @param Timeout Timeout duration
  2844. * @retval HAL status
  2845. */
  2846. HAL_StatusTypeDef HAL_I2C_IsDeviceReady(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint32_t Trials, uint32_t Timeout)
  2847. {
  2848. /* Get tick */
  2849. uint32_t tickstart = HAL_GetTick();
  2850. uint32_t I2C_Trials = 0U;
  2851. FlagStatus tmp1;
  2852. FlagStatus tmp2;
  2853. if (hi2c->State == HAL_I2C_STATE_READY)
  2854. {
  2855. /* Wait until BUSY flag is reset */
  2856. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY_FLAG, tickstart) != HAL_OK)
  2857. {
  2858. return HAL_BUSY;
  2859. }
  2860. /* Process Locked */
  2861. __HAL_LOCK(hi2c);
  2862. /* Check if the I2C is already enabled */
  2863. if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  2864. {
  2865. /* Enable I2C peripheral */
  2866. __HAL_I2C_ENABLE(hi2c);
  2867. }
  2868. /* Disable Pos */
  2869. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  2870. hi2c->State = HAL_I2C_STATE_BUSY;
  2871. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  2872. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  2873. do
  2874. {
  2875. /* Generate Start */
  2876. SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
  2877. /* Wait until SB flag is set */
  2878. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout, tickstart) != HAL_OK)
  2879. {
  2880. if (READ_BIT(hi2c->Instance->CR1, I2C_CR1_START) == I2C_CR1_START)
  2881. {
  2882. hi2c->ErrorCode = HAL_I2C_WRONG_START;
  2883. }
  2884. return HAL_TIMEOUT;
  2885. }
  2886. /* Send slave address */
  2887. hi2c->Instance->DR = I2C_7BIT_ADD_WRITE(DevAddress);
  2888. /* Wait until ADDR or AF flag are set */
  2889. /* Get tick */
  2890. tickstart = HAL_GetTick();
  2891. tmp1 = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ADDR);
  2892. tmp2 = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF);
  2893. while ((hi2c->State != HAL_I2C_STATE_TIMEOUT) && (tmp1 == RESET) && (tmp2 == RESET))
  2894. {
  2895. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  2896. {
  2897. hi2c->State = HAL_I2C_STATE_TIMEOUT;
  2898. }
  2899. tmp1 = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ADDR);
  2900. tmp2 = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF);
  2901. }
  2902. hi2c->State = HAL_I2C_STATE_READY;
  2903. /* Check if the ADDR flag has been set */
  2904. if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ADDR) == SET)
  2905. {
  2906. /* Generate Stop */
  2907. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  2908. /* Clear ADDR Flag */
  2909. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  2910. /* Wait until BUSY flag is reset */
  2911. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY_FLAG, tickstart) != HAL_OK)
  2912. {
  2913. return HAL_ERROR;
  2914. }
  2915. hi2c->State = HAL_I2C_STATE_READY;
  2916. /* Process Unlocked */
  2917. __HAL_UNLOCK(hi2c);
  2918. return HAL_OK;
  2919. }
  2920. else
  2921. {
  2922. /* Generate Stop */
  2923. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  2924. /* Clear AF Flag */
  2925. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  2926. /* Wait until BUSY flag is reset */
  2927. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY_FLAG, tickstart) != HAL_OK)
  2928. {
  2929. return HAL_ERROR;
  2930. }
  2931. }
  2932. /* Increment Trials */
  2933. I2C_Trials++;
  2934. }
  2935. while (I2C_Trials < Trials);
  2936. hi2c->State = HAL_I2C_STATE_READY;
  2937. /* Process Unlocked */
  2938. __HAL_UNLOCK(hi2c);
  2939. return HAL_ERROR;
  2940. }
  2941. else
  2942. {
  2943. return HAL_BUSY;
  2944. }
  2945. }
  2946. /**
  2947. * @brief Sequential transmit in master I2C mode an amount of data in non-blocking mode with Interrupt.
  2948. * @note This interface allow to manage repeated start condition when a direction change during transfer
  2949. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2950. * the configuration information for the specified I2C.
  2951. * @param DevAddress Target device address: The device 7 bits address value
  2952. * in datasheet must be shifted to the left before calling the interface
  2953. * @param pData Pointer to data buffer
  2954. * @param Size Amount of data to be sent
  2955. * @param XferOptions Options of Transfer, value of @ref I2C_XferOptions_definition
  2956. * @retval HAL status
  2957. */
  2958. HAL_StatusTypeDef HAL_I2C_Master_Seq_Transmit_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
  2959. {
  2960. __IO uint32_t Prev_State = 0x00U;
  2961. __IO uint32_t count = 0x00U;
  2962. /* Check the parameters */
  2963. assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
  2964. if (hi2c->State == HAL_I2C_STATE_READY)
  2965. {
  2966. /* Check Busy Flag only if FIRST call of Master interface */
  2967. if ((READ_BIT(hi2c->Instance->CR1, I2C_CR1_STOP) == I2C_CR1_STOP) || (XferOptions == I2C_FIRST_AND_LAST_FRAME) || (XferOptions == I2C_FIRST_FRAME))
  2968. {
  2969. /* Wait until BUSY flag is reset */
  2970. count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U);
  2971. do
  2972. {
  2973. count--;
  2974. if (count == 0U)
  2975. {
  2976. hi2c->PreviousState = I2C_STATE_NONE;
  2977. hi2c->State = HAL_I2C_STATE_READY;
  2978. hi2c->Mode = HAL_I2C_MODE_NONE;
  2979. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  2980. return HAL_BUSY;
  2981. }
  2982. }
  2983. while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
  2984. }
  2985. /* Process Locked */
  2986. __HAL_LOCK(hi2c);
  2987. /* Check if the I2C is already enabled */
  2988. if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  2989. {
  2990. /* Enable I2C peripheral */
  2991. __HAL_I2C_ENABLE(hi2c);
  2992. }
  2993. /* Disable Pos */
  2994. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  2995. hi2c->State = HAL_I2C_STATE_BUSY_TX;
  2996. hi2c->Mode = HAL_I2C_MODE_MASTER;
  2997. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  2998. /* Prepare transfer parameters */
  2999. hi2c->pBuffPtr = pData;
  3000. hi2c->XferCount = Size;
  3001. hi2c->XferSize = hi2c->XferCount;
  3002. hi2c->XferOptions = XferOptions;
  3003. hi2c->Devaddress = DevAddress;
  3004. Prev_State = hi2c->PreviousState;
  3005. /* If transfer direction not change and there is no request to start another frame, do not generate Restart Condition */
  3006. /* Mean Previous state is same as current state */
  3007. if ((Prev_State != I2C_STATE_MASTER_BUSY_TX) || (IS_I2C_TRANSFER_OTHER_OPTIONS_REQUEST(XferOptions) == 1))
  3008. {
  3009. /* Generate Start */
  3010. SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
  3011. }
  3012. /* Process Unlocked */
  3013. __HAL_UNLOCK(hi2c);
  3014. /* Note : The I2C interrupts must be enabled after unlocking current process
  3015. to avoid the risk of I2C interrupt handle execution before current
  3016. process unlock */
  3017. /* Enable EVT, BUF and ERR interrupt */
  3018. __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  3019. return HAL_OK;
  3020. }
  3021. else
  3022. {
  3023. return HAL_BUSY;
  3024. }
  3025. }
  3026. /**
  3027. * @brief Sequential transmit in master I2C mode an amount of data in non-blocking mode with DMA.
  3028. * @note This interface allow to manage repeated start condition when a direction change during transfer
  3029. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3030. * the configuration information for the specified I2C.
  3031. * @param DevAddress Target device address: The device 7 bits address value
  3032. * in datasheet must be shifted to the left before calling the interface
  3033. * @param pData Pointer to data buffer
  3034. * @param Size Amount of data to be sent
  3035. * @param XferOptions Options of Transfer, value of @ref I2C_XferOptions_definition
  3036. * @retval HAL status
  3037. */
  3038. HAL_StatusTypeDef HAL_I2C_Master_Seq_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
  3039. {
  3040. __IO uint32_t Prev_State = 0x00U;
  3041. __IO uint32_t count = 0x00U;
  3042. HAL_StatusTypeDef dmaxferstatus;
  3043. /* Check the parameters */
  3044. assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
  3045. if (hi2c->State == HAL_I2C_STATE_READY)
  3046. {
  3047. /* Check Busy Flag only if FIRST call of Master interface */
  3048. if ((READ_BIT(hi2c->Instance->CR1, I2C_CR1_STOP) == I2C_CR1_STOP) || (XferOptions == I2C_FIRST_AND_LAST_FRAME) || (XferOptions == I2C_FIRST_FRAME))
  3049. {
  3050. /* Wait until BUSY flag is reset */
  3051. count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U);
  3052. do
  3053. {
  3054. count--;
  3055. if (count == 0U)
  3056. {
  3057. hi2c->PreviousState = I2C_STATE_NONE;
  3058. hi2c->State = HAL_I2C_STATE_READY;
  3059. hi2c->Mode = HAL_I2C_MODE_NONE;
  3060. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  3061. return HAL_BUSY;
  3062. }
  3063. }
  3064. while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
  3065. }
  3066. /* Process Locked */
  3067. __HAL_LOCK(hi2c);
  3068. /* Check if the I2C is already enabled */
  3069. if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  3070. {
  3071. /* Enable I2C peripheral */
  3072. __HAL_I2C_ENABLE(hi2c);
  3073. }
  3074. /* Disable Pos */
  3075. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  3076. hi2c->State = HAL_I2C_STATE_BUSY_TX;
  3077. hi2c->Mode = HAL_I2C_MODE_MASTER;
  3078. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  3079. /* Prepare transfer parameters */
  3080. hi2c->pBuffPtr = pData;
  3081. hi2c->XferCount = Size;
  3082. hi2c->XferSize = hi2c->XferCount;
  3083. hi2c->XferOptions = XferOptions;
  3084. hi2c->Devaddress = DevAddress;
  3085. Prev_State = hi2c->PreviousState;
  3086. if (hi2c->XferSize > 0U)
  3087. {
  3088. if (hi2c->hdmatx != NULL)
  3089. {
  3090. /* Set the I2C DMA transfer complete callback */
  3091. hi2c->hdmatx->XferCpltCallback = I2C_DMAXferCplt;
  3092. /* Set the DMA error callback */
  3093. hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
  3094. /* Set the unused DMA callbacks to NULL */
  3095. hi2c->hdmatx->XferHalfCpltCallback = NULL;
  3096. hi2c->hdmatx->XferAbortCallback = NULL;
  3097. /* Enable the DMA stream */
  3098. dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)hi2c->pBuffPtr, (uint32_t)&hi2c->Instance->DR, hi2c->XferSize);
  3099. }
  3100. else
  3101. {
  3102. /* Update I2C state */
  3103. hi2c->State = HAL_I2C_STATE_READY;
  3104. hi2c->Mode = HAL_I2C_MODE_NONE;
  3105. /* Update I2C error code */
  3106. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA_PARAM;
  3107. /* Process Unlocked */
  3108. __HAL_UNLOCK(hi2c);
  3109. return HAL_ERROR;
  3110. }
  3111. if (dmaxferstatus == HAL_OK)
  3112. {
  3113. /* Enable Acknowledge */
  3114. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  3115. /* If transfer direction not change and there is no request to start another frame, do not generate Restart Condition */
  3116. /* Mean Previous state is same as current state */
  3117. if ((Prev_State != I2C_STATE_MASTER_BUSY_TX) || (IS_I2C_TRANSFER_OTHER_OPTIONS_REQUEST(XferOptions) == 1))
  3118. {
  3119. /* Generate Start */
  3120. SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
  3121. }
  3122. /* Process Unlocked */
  3123. __HAL_UNLOCK(hi2c);
  3124. /* Note : The I2C interrupts must be enabled after unlocking current process
  3125. to avoid the risk of I2C interrupt handle execution before current
  3126. process unlock */
  3127. /* If XferOptions is not associated to a new frame, mean no start bit is request, enable directly the DMA request */
  3128. /* In other cases, DMA request is enabled after Slave address treatment in IRQHandler */
  3129. if ((XferOptions == I2C_NEXT_FRAME) || (XferOptions == I2C_LAST_FRAME) || (XferOptions == I2C_LAST_FRAME_NO_STOP))
  3130. {
  3131. /* Enable DMA Request */
  3132. SET_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
  3133. }
  3134. /* Enable EVT and ERR interrupt */
  3135. __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR);
  3136. }
  3137. else
  3138. {
  3139. /* Update I2C state */
  3140. hi2c->State = HAL_I2C_STATE_READY;
  3141. hi2c->Mode = HAL_I2C_MODE_NONE;
  3142. /* Update I2C error code */
  3143. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
  3144. /* Process Unlocked */
  3145. __HAL_UNLOCK(hi2c);
  3146. return HAL_ERROR;
  3147. }
  3148. }
  3149. else
  3150. {
  3151. /* Enable Acknowledge */
  3152. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  3153. /* If transfer direction not change and there is no request to start another frame, do not generate Restart Condition */
  3154. /* Mean Previous state is same as current state */
  3155. if ((Prev_State != I2C_STATE_MASTER_BUSY_TX) || (IS_I2C_TRANSFER_OTHER_OPTIONS_REQUEST(XferOptions) == 1))
  3156. {
  3157. /* Generate Start */
  3158. SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
  3159. }
  3160. /* Process Unlocked */
  3161. __HAL_UNLOCK(hi2c);
  3162. /* Note : The I2C interrupts must be enabled after unlocking current process
  3163. to avoid the risk of I2C interrupt handle execution before current
  3164. process unlock */
  3165. /* Enable EVT, BUF and ERR interrupt */
  3166. __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  3167. }
  3168. return HAL_OK;
  3169. }
  3170. else
  3171. {
  3172. return HAL_BUSY;
  3173. }
  3174. }
  3175. /**
  3176. * @brief Sequential receive in master I2C mode an amount of data in non-blocking mode with Interrupt
  3177. * @note This interface allow to manage repeated start condition when a direction change during transfer
  3178. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3179. * the configuration information for the specified I2C.
  3180. * @param DevAddress Target device address: The device 7 bits address value
  3181. * in datasheet must be shifted to the left before calling the interface
  3182. * @param pData Pointer to data buffer
  3183. * @param Size Amount of data to be sent
  3184. * @param XferOptions Options of Transfer, value of @ref I2C_XferOptions_definition
  3185. * @retval HAL status
  3186. */
  3187. HAL_StatusTypeDef HAL_I2C_Master_Seq_Receive_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
  3188. {
  3189. __IO uint32_t Prev_State = 0x00U;
  3190. __IO uint32_t count = 0U;
  3191. uint32_t enableIT = (I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  3192. /* Check the parameters */
  3193. assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
  3194. if (hi2c->State == HAL_I2C_STATE_READY)
  3195. {
  3196. /* Check Busy Flag only if FIRST call of Master interface */
  3197. if ((READ_BIT(hi2c->Instance->CR1, I2C_CR1_STOP) == I2C_CR1_STOP) || (XferOptions == I2C_FIRST_AND_LAST_FRAME) || (XferOptions == I2C_FIRST_FRAME))
  3198. {
  3199. /* Wait until BUSY flag is reset */
  3200. count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U);
  3201. do
  3202. {
  3203. count--;
  3204. if (count == 0U)
  3205. {
  3206. hi2c->PreviousState = I2C_STATE_NONE;
  3207. hi2c->State = HAL_I2C_STATE_READY;
  3208. hi2c->Mode = HAL_I2C_MODE_NONE;
  3209. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  3210. return HAL_BUSY;
  3211. }
  3212. }
  3213. while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
  3214. }
  3215. /* Process Locked */
  3216. __HAL_LOCK(hi2c);
  3217. /* Check if the I2C is already enabled */
  3218. if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  3219. {
  3220. /* Enable I2C peripheral */
  3221. __HAL_I2C_ENABLE(hi2c);
  3222. }
  3223. /* Disable Pos */
  3224. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  3225. hi2c->State = HAL_I2C_STATE_BUSY_RX;
  3226. hi2c->Mode = HAL_I2C_MODE_MASTER;
  3227. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  3228. /* Prepare transfer parameters */
  3229. hi2c->pBuffPtr = pData;
  3230. hi2c->XferCount = Size;
  3231. hi2c->XferSize = hi2c->XferCount;
  3232. hi2c->XferOptions = XferOptions;
  3233. hi2c->Devaddress = DevAddress;
  3234. Prev_State = hi2c->PreviousState;
  3235. if ((hi2c->XferCount == 2U) && ((XferOptions == I2C_LAST_FRAME) || (XferOptions == I2C_LAST_FRAME_NO_STOP)))
  3236. {
  3237. if (Prev_State == I2C_STATE_MASTER_BUSY_RX)
  3238. {
  3239. /* Disable Acknowledge */
  3240. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  3241. /* Enable Pos */
  3242. SET_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  3243. /* Remove Enabling of IT_BUF, mean RXNE treatment, treat the 2 bytes through BTF */
  3244. enableIT &= ~I2C_IT_BUF;
  3245. }
  3246. else
  3247. {
  3248. /* Enable Acknowledge */
  3249. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  3250. }
  3251. }
  3252. else
  3253. {
  3254. /* Enable Acknowledge */
  3255. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  3256. }
  3257. /* If transfer direction not change and there is no request to start another frame, do not generate Restart Condition */
  3258. /* Mean Previous state is same as current state */
  3259. if ((Prev_State != I2C_STATE_MASTER_BUSY_RX) || (IS_I2C_TRANSFER_OTHER_OPTIONS_REQUEST(XferOptions) == 1))
  3260. {
  3261. /* Generate Start */
  3262. SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
  3263. }
  3264. /* Process Unlocked */
  3265. __HAL_UNLOCK(hi2c);
  3266. /* Note : The I2C interrupts must be enabled after unlocking current process
  3267. to avoid the risk of I2C interrupt handle execution before current
  3268. process unlock */
  3269. /* Enable interrupts */
  3270. __HAL_I2C_ENABLE_IT(hi2c, enableIT);
  3271. return HAL_OK;
  3272. }
  3273. else
  3274. {
  3275. return HAL_BUSY;
  3276. }
  3277. }
  3278. /**
  3279. * @brief Sequential receive in master mode an amount of data in non-blocking mode with DMA
  3280. * @note This interface allow to manage repeated start condition when a direction change during transfer
  3281. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3282. * the configuration information for the specified I2C.
  3283. * @param DevAddress Target device address: The device 7 bits address value
  3284. * in datasheet must be shifted to the left before calling the interface
  3285. * @param pData Pointer to data buffer
  3286. * @param Size Amount of data to be sent
  3287. * @param XferOptions Options of Transfer, value of @ref I2C_XferOptions_definition
  3288. * @retval HAL status
  3289. */
  3290. HAL_StatusTypeDef HAL_I2C_Master_Seq_Receive_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
  3291. {
  3292. __IO uint32_t Prev_State = 0x00U;
  3293. __IO uint32_t count = 0U;
  3294. uint32_t enableIT = (I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  3295. HAL_StatusTypeDef dmaxferstatus;
  3296. /* Check the parameters */
  3297. assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
  3298. if (hi2c->State == HAL_I2C_STATE_READY)
  3299. {
  3300. /* Check Busy Flag only if FIRST call of Master interface */
  3301. if ((READ_BIT(hi2c->Instance->CR1, I2C_CR1_STOP) == I2C_CR1_STOP) || (XferOptions == I2C_FIRST_AND_LAST_FRAME) || (XferOptions == I2C_FIRST_FRAME))
  3302. {
  3303. /* Wait until BUSY flag is reset */
  3304. count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U);
  3305. do
  3306. {
  3307. count--;
  3308. if (count == 0U)
  3309. {
  3310. hi2c->PreviousState = I2C_STATE_NONE;
  3311. hi2c->State = HAL_I2C_STATE_READY;
  3312. hi2c->Mode = HAL_I2C_MODE_NONE;
  3313. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  3314. return HAL_BUSY;
  3315. }
  3316. }
  3317. while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
  3318. }
  3319. /* Process Locked */
  3320. __HAL_LOCK(hi2c);
  3321. /* Check if the I2C is already enabled */
  3322. if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  3323. {
  3324. /* Enable I2C peripheral */
  3325. __HAL_I2C_ENABLE(hi2c);
  3326. }
  3327. /* Disable Pos */
  3328. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  3329. /* Clear Last DMA bit */
  3330. CLEAR_BIT(hi2c->Instance->CR2, I2C_CR2_LAST);
  3331. hi2c->State = HAL_I2C_STATE_BUSY_RX;
  3332. hi2c->Mode = HAL_I2C_MODE_MASTER;
  3333. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  3334. /* Prepare transfer parameters */
  3335. hi2c->pBuffPtr = pData;
  3336. hi2c->XferCount = Size;
  3337. hi2c->XferSize = hi2c->XferCount;
  3338. hi2c->XferOptions = XferOptions;
  3339. hi2c->Devaddress = DevAddress;
  3340. Prev_State = hi2c->PreviousState;
  3341. if (hi2c->XferSize > 0U)
  3342. {
  3343. if ((hi2c->XferCount == 2U) && ((XferOptions == I2C_LAST_FRAME) || (XferOptions == I2C_LAST_FRAME_NO_STOP)))
  3344. {
  3345. if (Prev_State == I2C_STATE_MASTER_BUSY_RX)
  3346. {
  3347. /* Disable Acknowledge */
  3348. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  3349. /* Enable Pos */
  3350. SET_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  3351. /* Enable Last DMA bit */
  3352. SET_BIT(hi2c->Instance->CR2, I2C_CR2_LAST);
  3353. }
  3354. else
  3355. {
  3356. /* Enable Acknowledge */
  3357. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  3358. }
  3359. }
  3360. else
  3361. {
  3362. /* Enable Acknowledge */
  3363. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  3364. if ((XferOptions == I2C_LAST_FRAME) || (XferOptions == I2C_OTHER_AND_LAST_FRAME) || (XferOptions == I2C_LAST_FRAME_NO_STOP))
  3365. {
  3366. /* Enable Last DMA bit */
  3367. SET_BIT(hi2c->Instance->CR2, I2C_CR2_LAST);
  3368. }
  3369. }
  3370. if (hi2c->hdmarx != NULL)
  3371. {
  3372. /* Set the I2C DMA transfer complete callback */
  3373. hi2c->hdmarx->XferCpltCallback = I2C_DMAXferCplt;
  3374. /* Set the DMA error callback */
  3375. hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
  3376. /* Set the unused DMA callbacks to NULL */
  3377. hi2c->hdmarx->XferHalfCpltCallback = NULL;
  3378. hi2c->hdmarx->XferAbortCallback = NULL;
  3379. /* Enable the DMA stream */
  3380. dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->DR, (uint32_t)hi2c->pBuffPtr, hi2c->XferSize);
  3381. }
  3382. else
  3383. {
  3384. /* Update I2C state */
  3385. hi2c->State = HAL_I2C_STATE_READY;
  3386. hi2c->Mode = HAL_I2C_MODE_NONE;
  3387. /* Update I2C error code */
  3388. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA_PARAM;
  3389. /* Process Unlocked */
  3390. __HAL_UNLOCK(hi2c);
  3391. return HAL_ERROR;
  3392. }
  3393. if (dmaxferstatus == HAL_OK)
  3394. {
  3395. /* If transfer direction not change and there is no request to start another frame, do not generate Restart Condition */
  3396. /* Mean Previous state is same as current state */
  3397. if ((Prev_State != I2C_STATE_MASTER_BUSY_RX) || (IS_I2C_TRANSFER_OTHER_OPTIONS_REQUEST(XferOptions) == 1))
  3398. {
  3399. /* Generate Start */
  3400. SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
  3401. /* Update interrupt for only EVT and ERR */
  3402. enableIT = (I2C_IT_EVT | I2C_IT_ERR);
  3403. }
  3404. else
  3405. {
  3406. /* Update interrupt for only ERR */
  3407. enableIT = I2C_IT_ERR;
  3408. }
  3409. /* Process Unlocked */
  3410. __HAL_UNLOCK(hi2c);
  3411. /* Note : The I2C interrupts must be enabled after unlocking current process
  3412. to avoid the risk of I2C interrupt handle execution before current
  3413. process unlock */
  3414. /* If XferOptions is not associated to a new frame, mean no start bit is request, enable directly the DMA request */
  3415. /* In other cases, DMA request is enabled after Slave address treatment in IRQHandler */
  3416. if ((XferOptions == I2C_NEXT_FRAME) || (XferOptions == I2C_LAST_FRAME) || (XferOptions == I2C_LAST_FRAME_NO_STOP))
  3417. {
  3418. /* Enable DMA Request */
  3419. SET_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
  3420. }
  3421. /* Enable EVT and ERR interrupt */
  3422. __HAL_I2C_ENABLE_IT(hi2c, enableIT);
  3423. }
  3424. else
  3425. {
  3426. /* Update I2C state */
  3427. hi2c->State = HAL_I2C_STATE_READY;
  3428. hi2c->Mode = HAL_I2C_MODE_NONE;
  3429. /* Update I2C error code */
  3430. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
  3431. /* Process Unlocked */
  3432. __HAL_UNLOCK(hi2c);
  3433. return HAL_ERROR;
  3434. }
  3435. }
  3436. else
  3437. {
  3438. /* Enable Acknowledge */
  3439. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  3440. /* If transfer direction not change and there is no request to start another frame, do not generate Restart Condition */
  3441. /* Mean Previous state is same as current state */
  3442. if ((Prev_State != I2C_STATE_MASTER_BUSY_RX) || (IS_I2C_TRANSFER_OTHER_OPTIONS_REQUEST(XferOptions) == 1))
  3443. {
  3444. /* Generate Start */
  3445. SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
  3446. }
  3447. /* Process Unlocked */
  3448. __HAL_UNLOCK(hi2c);
  3449. /* Note : The I2C interrupts must be enabled after unlocking current process
  3450. to avoid the risk of I2C interrupt handle execution before current
  3451. process unlock */
  3452. /* Enable interrupts */
  3453. __HAL_I2C_ENABLE_IT(hi2c, enableIT);
  3454. }
  3455. return HAL_OK;
  3456. }
  3457. else
  3458. {
  3459. return HAL_BUSY;
  3460. }
  3461. }
  3462. /**
  3463. * @brief Sequential transmit in slave mode an amount of data in non-blocking mode with Interrupt
  3464. * @note This interface allow to manage repeated start condition when a direction change during transfer
  3465. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3466. * the configuration information for the specified I2C.
  3467. * @param pData Pointer to data buffer
  3468. * @param Size Amount of data to be sent
  3469. * @param XferOptions Options of Transfer, value of @ref I2C_XferOptions_definition
  3470. * @retval HAL status
  3471. */
  3472. HAL_StatusTypeDef HAL_I2C_Slave_Seq_Transmit_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
  3473. {
  3474. /* Check the parameters */
  3475. assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
  3476. if (((uint32_t)hi2c->State & (uint32_t)HAL_I2C_STATE_LISTEN) == (uint32_t)HAL_I2C_STATE_LISTEN)
  3477. {
  3478. if ((pData == NULL) || (Size == 0U))
  3479. {
  3480. return HAL_ERROR;
  3481. }
  3482. /* Process Locked */
  3483. __HAL_LOCK(hi2c);
  3484. /* Check if the I2C is already enabled */
  3485. if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  3486. {
  3487. /* Enable I2C peripheral */
  3488. __HAL_I2C_ENABLE(hi2c);
  3489. }
  3490. /* Disable Pos */
  3491. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  3492. hi2c->State = HAL_I2C_STATE_BUSY_TX_LISTEN;
  3493. hi2c->Mode = HAL_I2C_MODE_SLAVE;
  3494. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  3495. /* Prepare transfer parameters */
  3496. hi2c->pBuffPtr = pData;
  3497. hi2c->XferCount = Size;
  3498. hi2c->XferSize = hi2c->XferCount;
  3499. hi2c->XferOptions = XferOptions;
  3500. /* Clear ADDR flag */
  3501. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  3502. /* Process Unlocked */
  3503. __HAL_UNLOCK(hi2c);
  3504. /* Note : The I2C interrupts must be enabled after unlocking current process
  3505. to avoid the risk of I2C interrupt handle execution before current
  3506. process unlock */
  3507. /* Enable EVT, BUF and ERR interrupt */
  3508. __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  3509. return HAL_OK;
  3510. }
  3511. else
  3512. {
  3513. return HAL_BUSY;
  3514. }
  3515. }
  3516. /**
  3517. * @brief Sequential transmit in slave mode an amount of data in non-blocking mode with DMA
  3518. * @note This interface allow to manage repeated start condition when a direction change during transfer
  3519. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3520. * the configuration information for the specified I2C.
  3521. * @param pData Pointer to data buffer
  3522. * @param Size Amount of data to be sent
  3523. * @param XferOptions Options of Transfer, value of @ref I2C_XferOptions_definition
  3524. * @retval HAL status
  3525. */
  3526. HAL_StatusTypeDef HAL_I2C_Slave_Seq_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
  3527. {
  3528. HAL_StatusTypeDef dmaxferstatus;
  3529. /* Check the parameters */
  3530. assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
  3531. if (((uint32_t)hi2c->State & (uint32_t)HAL_I2C_STATE_LISTEN) == (uint32_t)HAL_I2C_STATE_LISTEN)
  3532. {
  3533. if ((pData == NULL) || (Size == 0U))
  3534. {
  3535. return HAL_ERROR;
  3536. }
  3537. /* Process Locked */
  3538. __HAL_LOCK(hi2c);
  3539. /* Disable Interrupts, to prevent preemption during treatment in case of multicall */
  3540. __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR);
  3541. /* I2C cannot manage full duplex exchange so disable previous IT enabled if any */
  3542. /* and then toggle the HAL slave RX state to TX state */
  3543. if (hi2c->State == HAL_I2C_STATE_BUSY_RX_LISTEN)
  3544. {
  3545. if ((hi2c->Instance->CR2 & I2C_CR2_DMAEN) == I2C_CR2_DMAEN)
  3546. {
  3547. /* Abort DMA Xfer if any */
  3548. if (hi2c->hdmarx != NULL)
  3549. {
  3550. CLEAR_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
  3551. /* Set the I2C DMA Abort callback :
  3552. will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
  3553. hi2c->hdmarx->XferAbortCallback = I2C_DMAAbort;
  3554. /* Abort DMA RX */
  3555. if (HAL_DMA_Abort_IT(hi2c->hdmarx) != HAL_OK)
  3556. {
  3557. /* Call Directly XferAbortCallback function in case of error */
  3558. hi2c->hdmarx->XferAbortCallback(hi2c->hdmarx);
  3559. }
  3560. }
  3561. }
  3562. }
  3563. else if (hi2c->State == HAL_I2C_STATE_BUSY_TX_LISTEN)
  3564. {
  3565. if ((hi2c->Instance->CR2 & I2C_CR2_DMAEN) == I2C_CR2_DMAEN)
  3566. {
  3567. CLEAR_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
  3568. /* Abort DMA Xfer if any */
  3569. if (hi2c->hdmatx != NULL)
  3570. {
  3571. /* Set the I2C DMA Abort callback :
  3572. will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
  3573. hi2c->hdmatx->XferAbortCallback = I2C_DMAAbort;
  3574. /* Abort DMA TX */
  3575. if (HAL_DMA_Abort_IT(hi2c->hdmatx) != HAL_OK)
  3576. {
  3577. /* Call Directly XferAbortCallback function in case of error */
  3578. hi2c->hdmatx->XferAbortCallback(hi2c->hdmatx);
  3579. }
  3580. }
  3581. }
  3582. }
  3583. else
  3584. {
  3585. /* Nothing to do */
  3586. }
  3587. /* Check if the I2C is already enabled */
  3588. if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  3589. {
  3590. /* Enable I2C peripheral */
  3591. __HAL_I2C_ENABLE(hi2c);
  3592. }
  3593. /* Disable Pos */
  3594. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  3595. hi2c->State = HAL_I2C_STATE_BUSY_TX_LISTEN;
  3596. hi2c->Mode = HAL_I2C_MODE_SLAVE;
  3597. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  3598. /* Prepare transfer parameters */
  3599. hi2c->pBuffPtr = pData;
  3600. hi2c->XferCount = Size;
  3601. hi2c->XferSize = hi2c->XferCount;
  3602. hi2c->XferOptions = XferOptions;
  3603. if (hi2c->hdmatx != NULL)
  3604. {
  3605. /* Set the I2C DMA transfer complete callback */
  3606. hi2c->hdmatx->XferCpltCallback = I2C_DMAXferCplt;
  3607. /* Set the DMA error callback */
  3608. hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
  3609. /* Set the unused DMA callbacks to NULL */
  3610. hi2c->hdmatx->XferHalfCpltCallback = NULL;
  3611. hi2c->hdmatx->XferAbortCallback = NULL;
  3612. /* Enable the DMA stream */
  3613. dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)hi2c->pBuffPtr, (uint32_t)&hi2c->Instance->DR, hi2c->XferSize);
  3614. }
  3615. else
  3616. {
  3617. /* Update I2C state */
  3618. hi2c->State = HAL_I2C_STATE_LISTEN;
  3619. hi2c->Mode = HAL_I2C_MODE_NONE;
  3620. /* Update I2C error code */
  3621. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA_PARAM;
  3622. /* Process Unlocked */
  3623. __HAL_UNLOCK(hi2c);
  3624. return HAL_ERROR;
  3625. }
  3626. if (dmaxferstatus == HAL_OK)
  3627. {
  3628. /* Enable Address Acknowledge */
  3629. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  3630. /* Clear ADDR flag */
  3631. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  3632. /* Process Unlocked */
  3633. __HAL_UNLOCK(hi2c);
  3634. /* Note : The I2C interrupts must be enabled after unlocking current process
  3635. to avoid the risk of I2C interrupt handle execution before current
  3636. process unlock */
  3637. /* Enable EVT and ERR interrupt */
  3638. __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR);
  3639. /* Enable DMA Request */
  3640. hi2c->Instance->CR2 |= I2C_CR2_DMAEN;
  3641. return HAL_OK;
  3642. }
  3643. else
  3644. {
  3645. /* Update I2C state */
  3646. hi2c->State = HAL_I2C_STATE_READY;
  3647. hi2c->Mode = HAL_I2C_MODE_NONE;
  3648. /* Update I2C error code */
  3649. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
  3650. /* Process Unlocked */
  3651. __HAL_UNLOCK(hi2c);
  3652. return HAL_ERROR;
  3653. }
  3654. }
  3655. else
  3656. {
  3657. return HAL_BUSY;
  3658. }
  3659. }
  3660. /**
  3661. * @brief Sequential receive in slave mode an amount of data in non-blocking mode with Interrupt
  3662. * @note This interface allow to manage repeated start condition when a direction change during transfer
  3663. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3664. * the configuration information for the specified I2C.
  3665. * @param pData Pointer to data buffer
  3666. * @param Size Amount of data to be sent
  3667. * @param XferOptions Options of Transfer, value of @ref I2C_XferOptions_definition
  3668. * @retval HAL status
  3669. */
  3670. HAL_StatusTypeDef HAL_I2C_Slave_Seq_Receive_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
  3671. {
  3672. /* Check the parameters */
  3673. assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
  3674. if (((uint32_t)hi2c->State & (uint32_t)HAL_I2C_STATE_LISTEN) == (uint32_t)HAL_I2C_STATE_LISTEN)
  3675. {
  3676. if ((pData == NULL) || (Size == 0U))
  3677. {
  3678. return HAL_ERROR;
  3679. }
  3680. /* Process Locked */
  3681. __HAL_LOCK(hi2c);
  3682. /* Check if the I2C is already enabled */
  3683. if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  3684. {
  3685. /* Enable I2C peripheral */
  3686. __HAL_I2C_ENABLE(hi2c);
  3687. }
  3688. /* Disable Pos */
  3689. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  3690. hi2c->State = HAL_I2C_STATE_BUSY_RX_LISTEN;
  3691. hi2c->Mode = HAL_I2C_MODE_SLAVE;
  3692. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  3693. /* Prepare transfer parameters */
  3694. hi2c->pBuffPtr = pData;
  3695. hi2c->XferCount = Size;
  3696. hi2c->XferSize = hi2c->XferCount;
  3697. hi2c->XferOptions = XferOptions;
  3698. /* Clear ADDR flag */
  3699. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  3700. /* Process Unlocked */
  3701. __HAL_UNLOCK(hi2c);
  3702. /* Note : The I2C interrupts must be enabled after unlocking current process
  3703. to avoid the risk of I2C interrupt handle execution before current
  3704. process unlock */
  3705. /* Enable EVT, BUF and ERR interrupt */
  3706. __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  3707. return HAL_OK;
  3708. }
  3709. else
  3710. {
  3711. return HAL_BUSY;
  3712. }
  3713. }
  3714. /**
  3715. * @brief Sequential receive in slave mode an amount of data in non-blocking mode with DMA
  3716. * @note This interface allow to manage repeated start condition when a direction change during transfer
  3717. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3718. * the configuration information for the specified I2C.
  3719. * @param pData Pointer to data buffer
  3720. * @param Size Amount of data to be sent
  3721. * @param XferOptions Options of Transfer, value of @ref I2C_XferOptions_definition
  3722. * @retval HAL status
  3723. */
  3724. HAL_StatusTypeDef HAL_I2C_Slave_Seq_Receive_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
  3725. {
  3726. HAL_StatusTypeDef dmaxferstatus;
  3727. /* Check the parameters */
  3728. assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
  3729. if (((uint32_t)hi2c->State & (uint32_t)HAL_I2C_STATE_LISTEN) == (uint32_t)HAL_I2C_STATE_LISTEN)
  3730. {
  3731. if ((pData == NULL) || (Size == 0U))
  3732. {
  3733. return HAL_ERROR;
  3734. }
  3735. /* Process Locked */
  3736. __HAL_LOCK(hi2c);
  3737. /* Disable Interrupts, to prevent preemption during treatment in case of multicall */
  3738. __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR);
  3739. /* I2C cannot manage full duplex exchange so disable previous IT enabled if any */
  3740. /* and then toggle the HAL slave RX state to TX state */
  3741. if (hi2c->State == HAL_I2C_STATE_BUSY_RX_LISTEN)
  3742. {
  3743. if ((hi2c->Instance->CR2 & I2C_CR2_DMAEN) == I2C_CR2_DMAEN)
  3744. {
  3745. /* Abort DMA Xfer if any */
  3746. if (hi2c->hdmarx != NULL)
  3747. {
  3748. CLEAR_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
  3749. /* Set the I2C DMA Abort callback :
  3750. will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
  3751. hi2c->hdmarx->XferAbortCallback = I2C_DMAAbort;
  3752. /* Abort DMA RX */
  3753. if (HAL_DMA_Abort_IT(hi2c->hdmarx) != HAL_OK)
  3754. {
  3755. /* Call Directly XferAbortCallback function in case of error */
  3756. hi2c->hdmarx->XferAbortCallback(hi2c->hdmarx);
  3757. }
  3758. }
  3759. }
  3760. }
  3761. else if (hi2c->State == HAL_I2C_STATE_BUSY_TX_LISTEN)
  3762. {
  3763. if ((hi2c->Instance->CR2 & I2C_CR2_DMAEN) == I2C_CR2_DMAEN)
  3764. {
  3765. CLEAR_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
  3766. /* Abort DMA Xfer if any */
  3767. if (hi2c->hdmatx != NULL)
  3768. {
  3769. /* Set the I2C DMA Abort callback :
  3770. will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
  3771. hi2c->hdmatx->XferAbortCallback = I2C_DMAAbort;
  3772. /* Abort DMA TX */
  3773. if (HAL_DMA_Abort_IT(hi2c->hdmatx) != HAL_OK)
  3774. {
  3775. /* Call Directly XferAbortCallback function in case of error */
  3776. hi2c->hdmatx->XferAbortCallback(hi2c->hdmatx);
  3777. }
  3778. }
  3779. }
  3780. }
  3781. else
  3782. {
  3783. /* Nothing to do */
  3784. }
  3785. /* Check if the I2C is already enabled */
  3786. if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  3787. {
  3788. /* Enable I2C peripheral */
  3789. __HAL_I2C_ENABLE(hi2c);
  3790. }
  3791. /* Disable Pos */
  3792. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  3793. hi2c->State = HAL_I2C_STATE_BUSY_RX_LISTEN;
  3794. hi2c->Mode = HAL_I2C_MODE_SLAVE;
  3795. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  3796. /* Prepare transfer parameters */
  3797. hi2c->pBuffPtr = pData;
  3798. hi2c->XferCount = Size;
  3799. hi2c->XferSize = hi2c->XferCount;
  3800. hi2c->XferOptions = XferOptions;
  3801. if (hi2c->hdmarx != NULL)
  3802. {
  3803. /* Set the I2C DMA transfer complete callback */
  3804. hi2c->hdmarx->XferCpltCallback = I2C_DMAXferCplt;
  3805. /* Set the DMA error callback */
  3806. hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
  3807. /* Set the unused DMA callbacks to NULL */
  3808. hi2c->hdmarx->XferHalfCpltCallback = NULL;
  3809. hi2c->hdmarx->XferAbortCallback = NULL;
  3810. /* Enable the DMA stream */
  3811. dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->DR, (uint32_t)hi2c->pBuffPtr, hi2c->XferSize);
  3812. }
  3813. else
  3814. {
  3815. /* Update I2C state */
  3816. hi2c->State = HAL_I2C_STATE_LISTEN;
  3817. hi2c->Mode = HAL_I2C_MODE_NONE;
  3818. /* Update I2C error code */
  3819. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA_PARAM;
  3820. /* Process Unlocked */
  3821. __HAL_UNLOCK(hi2c);
  3822. return HAL_ERROR;
  3823. }
  3824. if (dmaxferstatus == HAL_OK)
  3825. {
  3826. /* Enable Address Acknowledge */
  3827. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  3828. /* Clear ADDR flag */
  3829. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  3830. /* Process Unlocked */
  3831. __HAL_UNLOCK(hi2c);
  3832. /* Enable DMA Request */
  3833. SET_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
  3834. /* Note : The I2C interrupts must be enabled after unlocking current process
  3835. to avoid the risk of I2C interrupt handle execution before current
  3836. process unlock */
  3837. /* Enable EVT and ERR interrupt */
  3838. __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR);
  3839. return HAL_OK;
  3840. }
  3841. else
  3842. {
  3843. /* Update I2C state */
  3844. hi2c->State = HAL_I2C_STATE_READY;
  3845. hi2c->Mode = HAL_I2C_MODE_NONE;
  3846. /* Update I2C error code */
  3847. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
  3848. /* Process Unlocked */
  3849. __HAL_UNLOCK(hi2c);
  3850. return HAL_ERROR;
  3851. }
  3852. }
  3853. else
  3854. {
  3855. return HAL_BUSY;
  3856. }
  3857. }
  3858. /**
  3859. * @brief Enable the Address listen mode with Interrupt.
  3860. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3861. * the configuration information for the specified I2C.
  3862. * @retval HAL status
  3863. */
  3864. HAL_StatusTypeDef HAL_I2C_EnableListen_IT(I2C_HandleTypeDef *hi2c)
  3865. {
  3866. if (hi2c->State == HAL_I2C_STATE_READY)
  3867. {
  3868. hi2c->State = HAL_I2C_STATE_LISTEN;
  3869. /* Check if the I2C is already enabled */
  3870. if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  3871. {
  3872. /* Enable I2C peripheral */
  3873. __HAL_I2C_ENABLE(hi2c);
  3874. }
  3875. /* Enable Address Acknowledge */
  3876. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  3877. /* Enable EVT and ERR interrupt */
  3878. __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR);
  3879. return HAL_OK;
  3880. }
  3881. else
  3882. {
  3883. return HAL_BUSY;
  3884. }
  3885. }
  3886. /**
  3887. * @brief Disable the Address listen mode with Interrupt.
  3888. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3889. * the configuration information for the specified I2C.
  3890. * @retval HAL status
  3891. */
  3892. HAL_StatusTypeDef HAL_I2C_DisableListen_IT(I2C_HandleTypeDef *hi2c)
  3893. {
  3894. /* Declaration of tmp to prevent undefined behavior of volatile usage */
  3895. uint32_t tmp;
  3896. /* Disable Address listen mode only if a transfer is not ongoing */
  3897. if (hi2c->State == HAL_I2C_STATE_LISTEN)
  3898. {
  3899. tmp = (uint32_t)(hi2c->State) & I2C_STATE_MSK;
  3900. hi2c->PreviousState = tmp | (uint32_t)(hi2c->Mode);
  3901. hi2c->State = HAL_I2C_STATE_READY;
  3902. hi2c->Mode = HAL_I2C_MODE_NONE;
  3903. /* Disable Address Acknowledge */
  3904. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  3905. /* Disable EVT and ERR interrupt */
  3906. __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR);
  3907. return HAL_OK;
  3908. }
  3909. else
  3910. {
  3911. return HAL_BUSY;
  3912. }
  3913. }
  3914. /**
  3915. * @brief Abort a master or memory I2C IT or DMA process communication with Interrupt.
  3916. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3917. * the configuration information for the specified I2C.
  3918. * @param DevAddress Target device address: The device 7 bits address value
  3919. * in datasheet must be shifted to the left before calling the interface
  3920. * @retval HAL status
  3921. */
  3922. HAL_StatusTypeDef HAL_I2C_Master_Abort_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress)
  3923. {
  3924. /* Declaration of temporary variables to prevent undefined behavior of volatile usage */
  3925. HAL_I2C_ModeTypeDef CurrentMode = hi2c->Mode;
  3926. /* Prevent unused argument(s) compilation warning */
  3927. UNUSED(DevAddress);
  3928. /* Abort Master transfer during Receive or Transmit process */
  3929. if ((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET) && ((CurrentMode == HAL_I2C_MODE_MASTER) ||
  3930. (CurrentMode == HAL_I2C_MODE_MEM)))
  3931. {
  3932. /* Process Locked */
  3933. __HAL_LOCK(hi2c);
  3934. hi2c->PreviousState = I2C_STATE_NONE;
  3935. hi2c->State = HAL_I2C_STATE_ABORT;
  3936. /* Disable Acknowledge */
  3937. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  3938. /* Generate Stop */
  3939. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  3940. hi2c->XferCount = 0U;
  3941. /* Disable EVT, BUF and ERR interrupt */
  3942. __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  3943. /* Process Unlocked */
  3944. __HAL_UNLOCK(hi2c);
  3945. /* Call the corresponding callback to inform upper layer of End of Transfer */
  3946. I2C_ITError(hi2c);
  3947. return HAL_OK;
  3948. }
  3949. else
  3950. {
  3951. /* Wrong usage of abort function */
  3952. /* This function should be used only in case of abort monitored by master device */
  3953. /* Or periphal is not in busy state, mean there is no active sequence to be abort */
  3954. return HAL_ERROR;
  3955. }
  3956. }
  3957. /**
  3958. * @}
  3959. */
  3960. /** @defgroup I2C_IRQ_Handler_and_Callbacks IRQ Handler and Callbacks
  3961. * @{
  3962. */
  3963. /**
  3964. * @brief This function handles I2C event interrupt request.
  3965. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3966. * the configuration information for the specified I2C.
  3967. * @retval None
  3968. */
  3969. void HAL_I2C_EV_IRQHandler(I2C_HandleTypeDef *hi2c)
  3970. {
  3971. uint32_t sr1itflags;
  3972. uint32_t sr2itflags = 0U;
  3973. uint32_t itsources = READ_REG(hi2c->Instance->CR2);
  3974. uint32_t CurrentXferOptions = hi2c->XferOptions;
  3975. HAL_I2C_ModeTypeDef CurrentMode = hi2c->Mode;
  3976. HAL_I2C_StateTypeDef CurrentState = hi2c->State;
  3977. /* Master or Memory mode selected */
  3978. if ((CurrentMode == HAL_I2C_MODE_MASTER) || (CurrentMode == HAL_I2C_MODE_MEM))
  3979. {
  3980. sr2itflags = READ_REG(hi2c->Instance->SR2);
  3981. sr1itflags = READ_REG(hi2c->Instance->SR1);
  3982. /* Exit IRQ event until Start Bit detected in case of Other frame requested */
  3983. if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_SB) == RESET) && (IS_I2C_TRANSFER_OTHER_OPTIONS_REQUEST(CurrentXferOptions) == 1U))
  3984. {
  3985. return;
  3986. }
  3987. /* SB Set ----------------------------------------------------------------*/
  3988. if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_SB) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_EVT) != RESET))
  3989. {
  3990. /* Convert OTHER_xxx XferOptions if any */
  3991. I2C_ConvertOtherXferOptions(hi2c);
  3992. I2C_Master_SB(hi2c);
  3993. }
  3994. /* ADD10 Set -------------------------------------------------------------*/
  3995. else if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_ADD10) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_EVT) != RESET))
  3996. {
  3997. I2C_Master_ADD10(hi2c);
  3998. }
  3999. /* ADDR Set --------------------------------------------------------------*/
  4000. else if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_ADDR) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_EVT) != RESET))
  4001. {
  4002. I2C_Master_ADDR(hi2c);
  4003. }
  4004. /* I2C in mode Transmitter -----------------------------------------------*/
  4005. else if (I2C_CHECK_FLAG(sr2itflags, I2C_FLAG_TRA) != RESET)
  4006. {
  4007. /* Do not check buffer and BTF flag if a Xfer DMA is on going */
  4008. if (READ_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN) != I2C_CR2_DMAEN)
  4009. {
  4010. /* TXE set and BTF reset -----------------------------------------------*/
  4011. if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_TXE) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_BUF) != RESET) && (I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_BTF) == RESET))
  4012. {
  4013. I2C_MasterTransmit_TXE(hi2c);
  4014. }
  4015. /* BTF set -------------------------------------------------------------*/
  4016. else if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_BTF) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_EVT) != RESET))
  4017. {
  4018. if (CurrentState == HAL_I2C_STATE_BUSY_TX)
  4019. {
  4020. I2C_MasterTransmit_BTF(hi2c);
  4021. }
  4022. else /* HAL_I2C_MODE_MEM */
  4023. {
  4024. if (CurrentMode == HAL_I2C_MODE_MEM)
  4025. {
  4026. I2C_MemoryTransmit_TXE_BTF(hi2c);
  4027. }
  4028. }
  4029. }
  4030. else
  4031. {
  4032. /* Do nothing */
  4033. }
  4034. }
  4035. }
  4036. /* I2C in mode Receiver --------------------------------------------------*/
  4037. else
  4038. {
  4039. /* Do not check buffer and BTF flag if a Xfer DMA is on going */
  4040. if (READ_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN) != I2C_CR2_DMAEN)
  4041. {
  4042. /* RXNE set and BTF reset -----------------------------------------------*/
  4043. if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_RXNE) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_BUF) != RESET) && (I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_BTF) == RESET))
  4044. {
  4045. I2C_MasterReceive_RXNE(hi2c);
  4046. }
  4047. /* BTF set -------------------------------------------------------------*/
  4048. else if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_BTF) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_EVT) != RESET))
  4049. {
  4050. I2C_MasterReceive_BTF(hi2c);
  4051. }
  4052. else
  4053. {
  4054. /* Do nothing */
  4055. }
  4056. }
  4057. }
  4058. }
  4059. /* Slave mode selected */
  4060. else
  4061. {
  4062. /* If an error is detected, read only SR1 register to prevent */
  4063. /* a clear of ADDR flags by reading SR2 after reading SR1 in Error treatment */
  4064. if (hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
  4065. {
  4066. sr1itflags = READ_REG(hi2c->Instance->SR1);
  4067. }
  4068. else
  4069. {
  4070. sr2itflags = READ_REG(hi2c->Instance->SR2);
  4071. sr1itflags = READ_REG(hi2c->Instance->SR1);
  4072. }
  4073. /* ADDR set --------------------------------------------------------------*/
  4074. if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_ADDR) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_EVT) != RESET))
  4075. {
  4076. /* Now time to read SR2, this will clear ADDR flag automatically */
  4077. if (hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
  4078. {
  4079. sr2itflags = READ_REG(hi2c->Instance->SR2);
  4080. }
  4081. I2C_Slave_ADDR(hi2c, sr2itflags);
  4082. }
  4083. /* STOPF set --------------------------------------------------------------*/
  4084. else if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_STOPF) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_EVT) != RESET))
  4085. {
  4086. I2C_Slave_STOPF(hi2c);
  4087. }
  4088. /* I2C in mode Transmitter -----------------------------------------------*/
  4089. else if ((CurrentState == HAL_I2C_STATE_BUSY_TX) || (CurrentState == HAL_I2C_STATE_BUSY_TX_LISTEN))
  4090. {
  4091. /* TXE set and BTF reset -----------------------------------------------*/
  4092. if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_TXE) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_BUF) != RESET) && (I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_BTF) == RESET))
  4093. {
  4094. I2C_SlaveTransmit_TXE(hi2c);
  4095. }
  4096. /* BTF set -------------------------------------------------------------*/
  4097. else if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_BTF) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_EVT) != RESET))
  4098. {
  4099. I2C_SlaveTransmit_BTF(hi2c);
  4100. }
  4101. else
  4102. {
  4103. /* Do nothing */
  4104. }
  4105. }
  4106. /* I2C in mode Receiver --------------------------------------------------*/
  4107. else
  4108. {
  4109. /* RXNE set and BTF reset ----------------------------------------------*/
  4110. if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_RXNE) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_BUF) != RESET) && (I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_BTF) == RESET))
  4111. {
  4112. I2C_SlaveReceive_RXNE(hi2c);
  4113. }
  4114. /* BTF set -------------------------------------------------------------*/
  4115. else if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_BTF) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_EVT) != RESET))
  4116. {
  4117. I2C_SlaveReceive_BTF(hi2c);
  4118. }
  4119. else
  4120. {
  4121. /* Do nothing */
  4122. }
  4123. }
  4124. }
  4125. }
  4126. /**
  4127. * @brief This function handles I2C error interrupt request.
  4128. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4129. * the configuration information for the specified I2C.
  4130. * @retval None
  4131. */
  4132. void HAL_I2C_ER_IRQHandler(I2C_HandleTypeDef *hi2c)
  4133. {
  4134. HAL_I2C_ModeTypeDef tmp1;
  4135. uint32_t tmp2;
  4136. HAL_I2C_StateTypeDef tmp3;
  4137. uint32_t tmp4;
  4138. uint32_t sr1itflags = READ_REG(hi2c->Instance->SR1);
  4139. uint32_t itsources = READ_REG(hi2c->Instance->CR2);
  4140. uint32_t error = HAL_I2C_ERROR_NONE;
  4141. HAL_I2C_ModeTypeDef CurrentMode = hi2c->Mode;
  4142. /* I2C Bus error interrupt occurred ----------------------------------------*/
  4143. if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_BERR) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_ERR) != RESET))
  4144. {
  4145. error |= HAL_I2C_ERROR_BERR;
  4146. /* Clear BERR flag */
  4147. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_BERR);
  4148. }
  4149. /* I2C Arbitration Lost error interrupt occurred ---------------------------*/
  4150. if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_ARLO) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_ERR) != RESET))
  4151. {
  4152. error |= HAL_I2C_ERROR_ARLO;
  4153. /* Clear ARLO flag */
  4154. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ARLO);
  4155. }
  4156. /* I2C Acknowledge failure error interrupt occurred ------------------------*/
  4157. if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_AF) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_ERR) != RESET))
  4158. {
  4159. tmp1 = CurrentMode;
  4160. tmp2 = hi2c->XferCount;
  4161. tmp3 = hi2c->State;
  4162. tmp4 = hi2c->PreviousState;
  4163. if ((tmp1 == HAL_I2C_MODE_SLAVE) && (tmp2 == 0U) && \
  4164. ((tmp3 == HAL_I2C_STATE_BUSY_TX) || (tmp3 == HAL_I2C_STATE_BUSY_TX_LISTEN) || \
  4165. ((tmp3 == HAL_I2C_STATE_LISTEN) && (tmp4 == I2C_STATE_SLAVE_BUSY_TX))))
  4166. {
  4167. I2C_Slave_AF(hi2c);
  4168. }
  4169. else
  4170. {
  4171. /* Clear AF flag */
  4172. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  4173. error |= HAL_I2C_ERROR_AF;
  4174. /* Do not generate a STOP in case of Slave receive non acknowledge during transfer (mean not at the end of transfer) */
  4175. if ((CurrentMode == HAL_I2C_MODE_MASTER) || (CurrentMode == HAL_I2C_MODE_MEM))
  4176. {
  4177. /* Generate Stop */
  4178. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  4179. }
  4180. }
  4181. }
  4182. /* I2C Over-Run/Under-Run interrupt occurred -------------------------------*/
  4183. if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_OVR) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_ERR) != RESET))
  4184. {
  4185. error |= HAL_I2C_ERROR_OVR;
  4186. /* Clear OVR flag */
  4187. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_OVR);
  4188. }
  4189. /* Call the Error Callback in case of Error detected -----------------------*/
  4190. if (error != HAL_I2C_ERROR_NONE)
  4191. {
  4192. hi2c->ErrorCode |= error;
  4193. I2C_ITError(hi2c);
  4194. }
  4195. }
  4196. /**
  4197. * @brief Master Tx Transfer completed callback.
  4198. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4199. * the configuration information for the specified I2C.
  4200. * @retval None
  4201. */
  4202. __weak void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef *hi2c)
  4203. {
  4204. /* Prevent unused argument(s) compilation warning */
  4205. UNUSED(hi2c);
  4206. /* NOTE : This function should not be modified, when the callback is needed,
  4207. the HAL_I2C_MasterTxCpltCallback could be implemented in the user file
  4208. */
  4209. }
  4210. /**
  4211. * @brief Master Rx Transfer completed callback.
  4212. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4213. * the configuration information for the specified I2C.
  4214. * @retval None
  4215. */
  4216. __weak void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *hi2c)
  4217. {
  4218. /* Prevent unused argument(s) compilation warning */
  4219. UNUSED(hi2c);
  4220. /* NOTE : This function should not be modified, when the callback is needed,
  4221. the HAL_I2C_MasterRxCpltCallback could be implemented in the user file
  4222. */
  4223. }
  4224. /** @brief Slave Tx Transfer completed callback.
  4225. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4226. * the configuration information for the specified I2C.
  4227. * @retval None
  4228. */
  4229. __weak void HAL_I2C_SlaveTxCpltCallback(I2C_HandleTypeDef *hi2c)
  4230. {
  4231. /* Prevent unused argument(s) compilation warning */
  4232. UNUSED(hi2c);
  4233. /* NOTE : This function should not be modified, when the callback is needed,
  4234. the HAL_I2C_SlaveTxCpltCallback could be implemented in the user file
  4235. */
  4236. }
  4237. /**
  4238. * @brief Slave Rx Transfer completed callback.
  4239. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4240. * the configuration information for the specified I2C.
  4241. * @retval None
  4242. */
  4243. __weak void HAL_I2C_SlaveRxCpltCallback(I2C_HandleTypeDef *hi2c)
  4244. {
  4245. /* Prevent unused argument(s) compilation warning */
  4246. UNUSED(hi2c);
  4247. /* NOTE : This function should not be modified, when the callback is needed,
  4248. the HAL_I2C_SlaveRxCpltCallback could be implemented in the user file
  4249. */
  4250. }
  4251. /**
  4252. * @brief Slave Address Match callback.
  4253. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4254. * the configuration information for the specified I2C.
  4255. * @param TransferDirection Master request Transfer Direction (Write/Read), value of @ref I2C_XferDirection_definition
  4256. * @param AddrMatchCode Address Match Code
  4257. * @retval None
  4258. */
  4259. __weak void HAL_I2C_AddrCallback(I2C_HandleTypeDef *hi2c, uint8_t TransferDirection, uint16_t AddrMatchCode)
  4260. {
  4261. /* Prevent unused argument(s) compilation warning */
  4262. UNUSED(hi2c);
  4263. UNUSED(TransferDirection);
  4264. UNUSED(AddrMatchCode);
  4265. /* NOTE : This function should not be modified, when the callback is needed,
  4266. the HAL_I2C_AddrCallback() could be implemented in the user file
  4267. */
  4268. }
  4269. /**
  4270. * @brief Listen Complete callback.
  4271. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4272. * the configuration information for the specified I2C.
  4273. * @retval None
  4274. */
  4275. __weak void HAL_I2C_ListenCpltCallback(I2C_HandleTypeDef *hi2c)
  4276. {
  4277. /* Prevent unused argument(s) compilation warning */
  4278. UNUSED(hi2c);
  4279. /* NOTE : This function should not be modified, when the callback is needed,
  4280. the HAL_I2C_ListenCpltCallback() could be implemented in the user file
  4281. */
  4282. }
  4283. /**
  4284. * @brief Memory Tx Transfer completed callback.
  4285. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4286. * the configuration information for the specified I2C.
  4287. * @retval None
  4288. */
  4289. __weak void HAL_I2C_MemTxCpltCallback(I2C_HandleTypeDef *hi2c)
  4290. {
  4291. /* Prevent unused argument(s) compilation warning */
  4292. UNUSED(hi2c);
  4293. /* NOTE : This function should not be modified, when the callback is needed,
  4294. the HAL_I2C_MemTxCpltCallback could be implemented in the user file
  4295. */
  4296. }
  4297. /**
  4298. * @brief Memory Rx Transfer completed callback.
  4299. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4300. * the configuration information for the specified I2C.
  4301. * @retval None
  4302. */
  4303. __weak void HAL_I2C_MemRxCpltCallback(I2C_HandleTypeDef *hi2c)
  4304. {
  4305. /* Prevent unused argument(s) compilation warning */
  4306. UNUSED(hi2c);
  4307. /* NOTE : This function should not be modified, when the callback is needed,
  4308. the HAL_I2C_MemRxCpltCallback could be implemented in the user file
  4309. */
  4310. }
  4311. /**
  4312. * @brief I2C error callback.
  4313. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4314. * the configuration information for the specified I2C.
  4315. * @retval None
  4316. */
  4317. __weak void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c)
  4318. {
  4319. /* Prevent unused argument(s) compilation warning */
  4320. UNUSED(hi2c);
  4321. /* NOTE : This function should not be modified, when the callback is needed,
  4322. the HAL_I2C_ErrorCallback could be implemented in the user file
  4323. */
  4324. }
  4325. /**
  4326. * @brief I2C abort callback.
  4327. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4328. * the configuration information for the specified I2C.
  4329. * @retval None
  4330. */
  4331. __weak void HAL_I2C_AbortCpltCallback(I2C_HandleTypeDef *hi2c)
  4332. {
  4333. /* Prevent unused argument(s) compilation warning */
  4334. UNUSED(hi2c);
  4335. /* NOTE : This function should not be modified, when the callback is needed,
  4336. the HAL_I2C_AbortCpltCallback could be implemented in the user file
  4337. */
  4338. }
  4339. /**
  4340. * @}
  4341. */
  4342. /** @defgroup I2C_Exported_Functions_Group3 Peripheral State, Mode and Error functions
  4343. * @brief Peripheral State, Mode and Error functions
  4344. *
  4345. @verbatim
  4346. ===============================================================================
  4347. ##### Peripheral State, Mode and Error functions #####
  4348. ===============================================================================
  4349. [..]
  4350. This subsection permit to get in run-time the status of the peripheral
  4351. and the data flow.
  4352. @endverbatim
  4353. * @{
  4354. */
  4355. /**
  4356. * @brief Return the I2C handle state.
  4357. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4358. * the configuration information for the specified I2C.
  4359. * @retval HAL state
  4360. */
  4361. HAL_I2C_StateTypeDef HAL_I2C_GetState(I2C_HandleTypeDef *hi2c)
  4362. {
  4363. /* Return I2C handle state */
  4364. return hi2c->State;
  4365. }
  4366. /**
  4367. * @brief Returns the I2C Master, Slave, Memory or no mode.
  4368. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4369. * the configuration information for I2C module
  4370. * @retval HAL mode
  4371. */
  4372. HAL_I2C_ModeTypeDef HAL_I2C_GetMode(I2C_HandleTypeDef *hi2c)
  4373. {
  4374. return hi2c->Mode;
  4375. }
  4376. /**
  4377. * @brief Return the I2C error code.
  4378. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4379. * the configuration information for the specified I2C.
  4380. * @retval I2C Error Code
  4381. */
  4382. uint32_t HAL_I2C_GetError(I2C_HandleTypeDef *hi2c)
  4383. {
  4384. return hi2c->ErrorCode;
  4385. }
  4386. /**
  4387. * @}
  4388. */
  4389. /**
  4390. * @}
  4391. */
  4392. /** @addtogroup I2C_Private_Functions
  4393. * @{
  4394. */
  4395. /**
  4396. * @brief Handle TXE flag for Master
  4397. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4398. * the configuration information for I2C module
  4399. * @retval None
  4400. */
  4401. static void I2C_MasterTransmit_TXE(I2C_HandleTypeDef *hi2c)
  4402. {
  4403. /* Declaration of temporary variables to prevent undefined behavior of volatile usage */
  4404. HAL_I2C_StateTypeDef CurrentState = hi2c->State;
  4405. HAL_I2C_ModeTypeDef CurrentMode = hi2c->Mode;
  4406. uint32_t CurrentXferOptions = hi2c->XferOptions;
  4407. if ((hi2c->XferSize == 0U) && (CurrentState == HAL_I2C_STATE_BUSY_TX))
  4408. {
  4409. /* Call TxCpltCallback() directly if no stop mode is set */
  4410. if ((CurrentXferOptions != I2C_FIRST_AND_LAST_FRAME) && (CurrentXferOptions != I2C_LAST_FRAME) && (CurrentXferOptions != I2C_NO_OPTION_FRAME))
  4411. {
  4412. __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  4413. hi2c->PreviousState = I2C_STATE_MASTER_BUSY_TX;
  4414. hi2c->Mode = HAL_I2C_MODE_NONE;
  4415. hi2c->State = HAL_I2C_STATE_READY;
  4416. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  4417. hi2c->MasterTxCpltCallback(hi2c);
  4418. #else
  4419. HAL_I2C_MasterTxCpltCallback(hi2c);
  4420. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  4421. }
  4422. else /* Generate Stop condition then Call TxCpltCallback() */
  4423. {
  4424. /* Disable EVT, BUF and ERR interrupt */
  4425. __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  4426. /* Generate Stop */
  4427. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  4428. hi2c->PreviousState = I2C_STATE_NONE;
  4429. hi2c->State = HAL_I2C_STATE_READY;
  4430. if (hi2c->Mode == HAL_I2C_MODE_MEM)
  4431. {
  4432. hi2c->Mode = HAL_I2C_MODE_NONE;
  4433. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  4434. hi2c->MemTxCpltCallback(hi2c);
  4435. #else
  4436. HAL_I2C_MemTxCpltCallback(hi2c);
  4437. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  4438. }
  4439. else
  4440. {
  4441. hi2c->Mode = HAL_I2C_MODE_NONE;
  4442. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  4443. hi2c->MasterTxCpltCallback(hi2c);
  4444. #else
  4445. HAL_I2C_MasterTxCpltCallback(hi2c);
  4446. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  4447. }
  4448. }
  4449. }
  4450. else if ((CurrentState == HAL_I2C_STATE_BUSY_TX) || \
  4451. ((CurrentMode == HAL_I2C_MODE_MEM) && (CurrentState == HAL_I2C_STATE_BUSY_RX)))
  4452. {
  4453. if (hi2c->XferCount == 0U)
  4454. {
  4455. /* Disable BUF interrupt */
  4456. __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_BUF);
  4457. }
  4458. else
  4459. {
  4460. if (hi2c->Mode == HAL_I2C_MODE_MEM)
  4461. {
  4462. I2C_MemoryTransmit_TXE_BTF(hi2c);
  4463. }
  4464. else
  4465. {
  4466. /* Write data to DR */
  4467. hi2c->Instance->DR = *hi2c->pBuffPtr;
  4468. /* Increment Buffer pointer */
  4469. hi2c->pBuffPtr++;
  4470. /* Update counter */
  4471. hi2c->XferCount--;
  4472. }
  4473. }
  4474. }
  4475. else
  4476. {
  4477. /* Do nothing */
  4478. }
  4479. }
  4480. /**
  4481. * @brief Handle BTF flag for Master transmitter
  4482. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4483. * the configuration information for I2C module
  4484. * @retval None
  4485. */
  4486. static void I2C_MasterTransmit_BTF(I2C_HandleTypeDef *hi2c)
  4487. {
  4488. /* Declaration of temporary variables to prevent undefined behavior of volatile usage */
  4489. uint32_t CurrentXferOptions = hi2c->XferOptions;
  4490. if (hi2c->State == HAL_I2C_STATE_BUSY_TX)
  4491. {
  4492. if (hi2c->XferCount != 0U)
  4493. {
  4494. /* Write data to DR */
  4495. hi2c->Instance->DR = *hi2c->pBuffPtr;
  4496. /* Increment Buffer pointer */
  4497. hi2c->pBuffPtr++;
  4498. /* Update counter */
  4499. hi2c->XferCount--;
  4500. }
  4501. else
  4502. {
  4503. /* Call TxCpltCallback() directly if no stop mode is set */
  4504. if ((CurrentXferOptions != I2C_FIRST_AND_LAST_FRAME) && (CurrentXferOptions != I2C_LAST_FRAME) && (CurrentXferOptions != I2C_NO_OPTION_FRAME))
  4505. {
  4506. __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  4507. hi2c->PreviousState = I2C_STATE_MASTER_BUSY_TX;
  4508. hi2c->Mode = HAL_I2C_MODE_NONE;
  4509. hi2c->State = HAL_I2C_STATE_READY;
  4510. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  4511. hi2c->MasterTxCpltCallback(hi2c);
  4512. #else
  4513. HAL_I2C_MasterTxCpltCallback(hi2c);
  4514. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  4515. }
  4516. else /* Generate Stop condition then Call TxCpltCallback() */
  4517. {
  4518. /* Disable EVT, BUF and ERR interrupt */
  4519. __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  4520. /* Generate Stop */
  4521. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  4522. hi2c->PreviousState = I2C_STATE_NONE;
  4523. hi2c->State = HAL_I2C_STATE_READY;
  4524. if (hi2c->Mode == HAL_I2C_MODE_MEM)
  4525. {
  4526. hi2c->Mode = HAL_I2C_MODE_NONE;
  4527. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  4528. hi2c->MemTxCpltCallback(hi2c);
  4529. #else
  4530. HAL_I2C_MemTxCpltCallback(hi2c);
  4531. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  4532. }
  4533. else
  4534. {
  4535. hi2c->Mode = HAL_I2C_MODE_NONE;
  4536. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  4537. hi2c->MasterTxCpltCallback(hi2c);
  4538. #else
  4539. HAL_I2C_MasterTxCpltCallback(hi2c);
  4540. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  4541. }
  4542. }
  4543. }
  4544. }
  4545. else
  4546. {
  4547. /* Do nothing */
  4548. }
  4549. }
  4550. /**
  4551. * @brief Handle TXE and BTF flag for Memory transmitter
  4552. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4553. * the configuration information for I2C module
  4554. * @retval None
  4555. */
  4556. static void I2C_MemoryTransmit_TXE_BTF(I2C_HandleTypeDef *hi2c)
  4557. {
  4558. /* Declaration of temporary variables to prevent undefined behavior of volatile usage */
  4559. HAL_I2C_StateTypeDef CurrentState = hi2c->State;
  4560. if (hi2c->EventCount == 0U)
  4561. {
  4562. /* If Memory address size is 8Bit */
  4563. if (hi2c->MemaddSize == I2C_MEMADD_SIZE_8BIT)
  4564. {
  4565. /* Send Memory Address */
  4566. hi2c->Instance->DR = I2C_MEM_ADD_LSB(hi2c->Memaddress);
  4567. hi2c->EventCount += 2U;
  4568. }
  4569. /* If Memory address size is 16Bit */
  4570. else
  4571. {
  4572. /* Send MSB of Memory Address */
  4573. hi2c->Instance->DR = I2C_MEM_ADD_MSB(hi2c->Memaddress);
  4574. hi2c->EventCount++;
  4575. }
  4576. }
  4577. else if (hi2c->EventCount == 1U)
  4578. {
  4579. /* Send LSB of Memory Address */
  4580. hi2c->Instance->DR = I2C_MEM_ADD_LSB(hi2c->Memaddress);
  4581. hi2c->EventCount++;
  4582. }
  4583. else if (hi2c->EventCount == 2U)
  4584. {
  4585. if (CurrentState == HAL_I2C_STATE_BUSY_RX)
  4586. {
  4587. /* Generate Restart */
  4588. hi2c->Instance->CR1 |= I2C_CR1_START;
  4589. hi2c->EventCount++;
  4590. }
  4591. else if ((hi2c->XferCount > 0U) && (CurrentState == HAL_I2C_STATE_BUSY_TX))
  4592. {
  4593. /* Write data to DR */
  4594. hi2c->Instance->DR = *hi2c->pBuffPtr;
  4595. /* Increment Buffer pointer */
  4596. hi2c->pBuffPtr++;
  4597. /* Update counter */
  4598. hi2c->XferCount--;
  4599. }
  4600. else if ((hi2c->XferCount == 0U) && (CurrentState == HAL_I2C_STATE_BUSY_TX))
  4601. {
  4602. /* Generate Stop condition then Call TxCpltCallback() */
  4603. /* Disable EVT, BUF and ERR interrupt */
  4604. __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  4605. /* Generate Stop */
  4606. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  4607. hi2c->PreviousState = I2C_STATE_NONE;
  4608. hi2c->State = HAL_I2C_STATE_READY;
  4609. hi2c->Mode = HAL_I2C_MODE_NONE;
  4610. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  4611. hi2c->MemTxCpltCallback(hi2c);
  4612. #else
  4613. HAL_I2C_MemTxCpltCallback(hi2c);
  4614. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  4615. }
  4616. else
  4617. {
  4618. /* Do nothing */
  4619. }
  4620. }
  4621. else
  4622. {
  4623. /* Clear TXE and BTF flags */
  4624. I2C_Flush_DR(hi2c);
  4625. }
  4626. }
  4627. /**
  4628. * @brief Handle RXNE flag for Master
  4629. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4630. * the configuration information for I2C module
  4631. * @retval None
  4632. */
  4633. static void I2C_MasterReceive_RXNE(I2C_HandleTypeDef *hi2c)
  4634. {
  4635. if (hi2c->State == HAL_I2C_STATE_BUSY_RX)
  4636. {
  4637. uint32_t tmp;
  4638. uint32_t CurrentXferOptions;
  4639. CurrentXferOptions = hi2c->XferOptions;
  4640. tmp = hi2c->XferCount;
  4641. if (tmp > 3U)
  4642. {
  4643. /* Read data from DR */
  4644. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
  4645. /* Increment Buffer pointer */
  4646. hi2c->pBuffPtr++;
  4647. /* Update counter */
  4648. hi2c->XferCount--;
  4649. if (hi2c->XferCount == (uint16_t)3)
  4650. {
  4651. /* Disable BUF interrupt, this help to treat correctly the last 4 bytes
  4652. on BTF subroutine */
  4653. /* Disable BUF interrupt */
  4654. __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_BUF);
  4655. }
  4656. }
  4657. else if ((hi2c->XferOptions != I2C_FIRST_AND_NEXT_FRAME) && ((tmp == 1U) || (tmp == 0U)))
  4658. {
  4659. if (I2C_WaitOnSTOPRequestThroughIT(hi2c) == HAL_OK)
  4660. {
  4661. /* Disable Acknowledge */
  4662. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  4663. /* Disable EVT, BUF and ERR interrupt */
  4664. __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  4665. /* Read data from DR */
  4666. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
  4667. /* Increment Buffer pointer */
  4668. hi2c->pBuffPtr++;
  4669. /* Update counter */
  4670. hi2c->XferCount--;
  4671. hi2c->State = HAL_I2C_STATE_READY;
  4672. if (hi2c->Mode == HAL_I2C_MODE_MEM)
  4673. {
  4674. hi2c->Mode = HAL_I2C_MODE_NONE;
  4675. hi2c->PreviousState = I2C_STATE_NONE;
  4676. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  4677. hi2c->MemRxCpltCallback(hi2c);
  4678. #else
  4679. HAL_I2C_MemRxCpltCallback(hi2c);
  4680. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  4681. }
  4682. else
  4683. {
  4684. hi2c->Mode = HAL_I2C_MODE_NONE;
  4685. if ((CurrentXferOptions == I2C_FIRST_AND_LAST_FRAME) || (CurrentXferOptions == I2C_LAST_FRAME))
  4686. {
  4687. hi2c->PreviousState = I2C_STATE_NONE;
  4688. }
  4689. else
  4690. {
  4691. hi2c->PreviousState = I2C_STATE_MASTER_BUSY_RX;
  4692. }
  4693. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  4694. hi2c->MasterRxCpltCallback(hi2c);
  4695. #else
  4696. HAL_I2C_MasterRxCpltCallback(hi2c);
  4697. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  4698. }
  4699. }
  4700. else
  4701. {
  4702. /* Disable EVT, BUF and ERR interrupt */
  4703. __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  4704. /* Read data from DR */
  4705. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
  4706. /* Increment Buffer pointer */
  4707. hi2c->pBuffPtr++;
  4708. /* Update counter */
  4709. hi2c->XferCount--;
  4710. hi2c->State = HAL_I2C_STATE_READY;
  4711. hi2c->Mode = HAL_I2C_MODE_NONE;
  4712. /* Call user error callback */
  4713. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  4714. hi2c->ErrorCallback(hi2c);
  4715. #else
  4716. HAL_I2C_ErrorCallback(hi2c);
  4717. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  4718. }
  4719. }
  4720. else
  4721. {
  4722. /* Disable BUF interrupt, this help to treat correctly the last 2 bytes
  4723. on BTF subroutine if there is a reception delay between N-1 and N byte */
  4724. __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_BUF);
  4725. }
  4726. }
  4727. }
  4728. /**
  4729. * @brief Handle BTF flag for Master receiver
  4730. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4731. * the configuration information for I2C module
  4732. * @retval None
  4733. */
  4734. static void I2C_MasterReceive_BTF(I2C_HandleTypeDef *hi2c)
  4735. {
  4736. /* Declaration of temporary variables to prevent undefined behavior of volatile usage */
  4737. uint32_t CurrentXferOptions = hi2c->XferOptions;
  4738. if (hi2c->XferCount == 4U)
  4739. {
  4740. /* Disable BUF interrupt, this help to treat correctly the last 2 bytes
  4741. on BTF subroutine if there is a reception delay between N-1 and N byte */
  4742. __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_BUF);
  4743. /* Read data from DR */
  4744. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
  4745. /* Increment Buffer pointer */
  4746. hi2c->pBuffPtr++;
  4747. /* Update counter */
  4748. hi2c->XferCount--;
  4749. }
  4750. else if (hi2c->XferCount == 3U)
  4751. {
  4752. /* Disable BUF interrupt, this help to treat correctly the last 2 bytes
  4753. on BTF subroutine if there is a reception delay between N-1 and N byte */
  4754. __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_BUF);
  4755. if ((CurrentXferOptions != I2C_NEXT_FRAME) && (CurrentXferOptions != I2C_FIRST_AND_NEXT_FRAME))
  4756. {
  4757. /* Disable Acknowledge */
  4758. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  4759. }
  4760. /* Read data from DR */
  4761. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
  4762. /* Increment Buffer pointer */
  4763. hi2c->pBuffPtr++;
  4764. /* Update counter */
  4765. hi2c->XferCount--;
  4766. }
  4767. else if (hi2c->XferCount == 2U)
  4768. {
  4769. /* Prepare next transfer or stop current transfer */
  4770. if ((CurrentXferOptions == I2C_FIRST_FRAME) || (CurrentXferOptions == I2C_LAST_FRAME_NO_STOP))
  4771. {
  4772. /* Disable Acknowledge */
  4773. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  4774. }
  4775. else if ((CurrentXferOptions == I2C_NEXT_FRAME) || (CurrentXferOptions == I2C_FIRST_AND_NEXT_FRAME))
  4776. {
  4777. /* Enable Acknowledge */
  4778. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  4779. }
  4780. else
  4781. {
  4782. /* Generate Stop */
  4783. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  4784. }
  4785. /* Read data from DR */
  4786. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
  4787. /* Increment Buffer pointer */
  4788. hi2c->pBuffPtr++;
  4789. /* Update counter */
  4790. hi2c->XferCount--;
  4791. /* Read data from DR */
  4792. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
  4793. /* Increment Buffer pointer */
  4794. hi2c->pBuffPtr++;
  4795. /* Update counter */
  4796. hi2c->XferCount--;
  4797. /* Disable EVT and ERR interrupt */
  4798. __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR);
  4799. hi2c->State = HAL_I2C_STATE_READY;
  4800. if (hi2c->Mode == HAL_I2C_MODE_MEM)
  4801. {
  4802. hi2c->Mode = HAL_I2C_MODE_NONE;
  4803. hi2c->PreviousState = I2C_STATE_NONE;
  4804. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  4805. hi2c->MemRxCpltCallback(hi2c);
  4806. #else
  4807. HAL_I2C_MemRxCpltCallback(hi2c);
  4808. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  4809. }
  4810. else
  4811. {
  4812. hi2c->Mode = HAL_I2C_MODE_NONE;
  4813. if ((CurrentXferOptions == I2C_FIRST_AND_LAST_FRAME) || (CurrentXferOptions == I2C_LAST_FRAME))
  4814. {
  4815. hi2c->PreviousState = I2C_STATE_NONE;
  4816. }
  4817. else
  4818. {
  4819. hi2c->PreviousState = I2C_STATE_MASTER_BUSY_RX;
  4820. }
  4821. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  4822. hi2c->MasterRxCpltCallback(hi2c);
  4823. #else
  4824. HAL_I2C_MasterRxCpltCallback(hi2c);
  4825. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  4826. }
  4827. }
  4828. else
  4829. {
  4830. /* Read data from DR */
  4831. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
  4832. /* Increment Buffer pointer */
  4833. hi2c->pBuffPtr++;
  4834. /* Update counter */
  4835. hi2c->XferCount--;
  4836. }
  4837. }
  4838. /**
  4839. * @brief Handle SB flag for Master
  4840. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4841. * the configuration information for I2C module
  4842. * @retval None
  4843. */
  4844. static void I2C_Master_SB(I2C_HandleTypeDef *hi2c)
  4845. {
  4846. if (hi2c->Mode == HAL_I2C_MODE_MEM)
  4847. {
  4848. if (hi2c->EventCount == 0U)
  4849. {
  4850. /* Send slave address */
  4851. hi2c->Instance->DR = I2C_7BIT_ADD_WRITE(hi2c->Devaddress);
  4852. }
  4853. else
  4854. {
  4855. hi2c->Instance->DR = I2C_7BIT_ADD_READ(hi2c->Devaddress);
  4856. }
  4857. }
  4858. else
  4859. {
  4860. if (hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_7BIT)
  4861. {
  4862. /* Send slave 7 Bits address */
  4863. if (hi2c->State == HAL_I2C_STATE_BUSY_TX)
  4864. {
  4865. hi2c->Instance->DR = I2C_7BIT_ADD_WRITE(hi2c->Devaddress);
  4866. }
  4867. else
  4868. {
  4869. hi2c->Instance->DR = I2C_7BIT_ADD_READ(hi2c->Devaddress);
  4870. }
  4871. if (((hi2c->hdmatx != NULL) && (hi2c->hdmatx->XferCpltCallback != NULL))
  4872. || ((hi2c->hdmarx != NULL) && (hi2c->hdmarx->XferCpltCallback != NULL)))
  4873. {
  4874. /* Enable DMA Request */
  4875. SET_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
  4876. }
  4877. }
  4878. else
  4879. {
  4880. if (hi2c->EventCount == 0U)
  4881. {
  4882. /* Send header of slave address */
  4883. hi2c->Instance->DR = I2C_10BIT_HEADER_WRITE(hi2c->Devaddress);
  4884. }
  4885. else if (hi2c->EventCount == 1U)
  4886. {
  4887. /* Send header of slave address */
  4888. hi2c->Instance->DR = I2C_10BIT_HEADER_READ(hi2c->Devaddress);
  4889. }
  4890. else
  4891. {
  4892. /* Do nothing */
  4893. }
  4894. }
  4895. }
  4896. }
  4897. /**
  4898. * @brief Handle ADD10 flag for Master
  4899. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4900. * the configuration information for I2C module
  4901. * @retval None
  4902. */
  4903. static void I2C_Master_ADD10(I2C_HandleTypeDef *hi2c)
  4904. {
  4905. /* Send slave address */
  4906. hi2c->Instance->DR = I2C_10BIT_ADDRESS(hi2c->Devaddress);
  4907. if (((hi2c->hdmatx != NULL) && (hi2c->hdmatx->XferCpltCallback != NULL))
  4908. || ((hi2c->hdmarx != NULL) && (hi2c->hdmarx->XferCpltCallback != NULL)))
  4909. {
  4910. /* Enable DMA Request */
  4911. SET_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
  4912. }
  4913. }
  4914. /**
  4915. * @brief Handle ADDR flag for Master
  4916. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4917. * the configuration information for I2C module
  4918. * @retval None
  4919. */
  4920. static void I2C_Master_ADDR(I2C_HandleTypeDef *hi2c)
  4921. {
  4922. /* Declaration of temporary variable to prevent undefined behavior of volatile usage */
  4923. HAL_I2C_ModeTypeDef CurrentMode = hi2c->Mode;
  4924. uint32_t CurrentXferOptions = hi2c->XferOptions;
  4925. uint32_t Prev_State = hi2c->PreviousState;
  4926. if (hi2c->State == HAL_I2C_STATE_BUSY_RX)
  4927. {
  4928. if ((hi2c->EventCount == 0U) && (CurrentMode == HAL_I2C_MODE_MEM))
  4929. {
  4930. /* Clear ADDR flag */
  4931. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  4932. }
  4933. else if ((hi2c->EventCount == 0U) && (hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_10BIT))
  4934. {
  4935. /* Clear ADDR flag */
  4936. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  4937. /* Generate Restart */
  4938. SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
  4939. hi2c->EventCount++;
  4940. }
  4941. else
  4942. {
  4943. if (hi2c->XferCount == 0U)
  4944. {
  4945. /* Clear ADDR flag */
  4946. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  4947. /* Generate Stop */
  4948. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  4949. }
  4950. else if (hi2c->XferCount == 1U)
  4951. {
  4952. if (CurrentXferOptions == I2C_NO_OPTION_FRAME)
  4953. {
  4954. /* Disable Acknowledge */
  4955. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  4956. if ((hi2c->Instance->CR2 & I2C_CR2_DMAEN) == I2C_CR2_DMAEN)
  4957. {
  4958. /* Disable Acknowledge */
  4959. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  4960. /* Clear ADDR flag */
  4961. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  4962. }
  4963. else
  4964. {
  4965. /* Clear ADDR flag */
  4966. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  4967. /* Generate Stop */
  4968. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  4969. }
  4970. }
  4971. /* Prepare next transfer or stop current transfer */
  4972. else if ((CurrentXferOptions != I2C_FIRST_AND_LAST_FRAME) && (CurrentXferOptions != I2C_LAST_FRAME) \
  4973. && ((Prev_State != I2C_STATE_MASTER_BUSY_RX) || (CurrentXferOptions == I2C_FIRST_FRAME)))
  4974. {
  4975. if ((CurrentXferOptions != I2C_NEXT_FRAME) && (CurrentXferOptions != I2C_FIRST_AND_NEXT_FRAME) && (CurrentXferOptions != I2C_LAST_FRAME_NO_STOP))
  4976. {
  4977. /* Disable Acknowledge */
  4978. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  4979. }
  4980. else
  4981. {
  4982. /* Enable Acknowledge */
  4983. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  4984. }
  4985. /* Clear ADDR flag */
  4986. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  4987. }
  4988. else
  4989. {
  4990. /* Disable Acknowledge */
  4991. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  4992. /* Clear ADDR flag */
  4993. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  4994. /* Generate Stop */
  4995. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  4996. }
  4997. }
  4998. else if (hi2c->XferCount == 2U)
  4999. {
  5000. if ((CurrentXferOptions != I2C_NEXT_FRAME) && (CurrentXferOptions != I2C_FIRST_AND_NEXT_FRAME) && (CurrentXferOptions != I2C_LAST_FRAME_NO_STOP))
  5001. {
  5002. /* Disable Acknowledge */
  5003. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  5004. /* Enable Pos */
  5005. SET_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  5006. }
  5007. else
  5008. {
  5009. /* Enable Acknowledge */
  5010. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  5011. }
  5012. if (((hi2c->Instance->CR2 & I2C_CR2_DMAEN) == I2C_CR2_DMAEN) && ((CurrentXferOptions == I2C_NO_OPTION_FRAME) || (CurrentXferOptions == I2C_FIRST_FRAME) || (CurrentXferOptions == I2C_FIRST_AND_LAST_FRAME) || (CurrentXferOptions == I2C_LAST_FRAME_NO_STOP) || (CurrentXferOptions == I2C_LAST_FRAME)))
  5013. {
  5014. /* Enable Last DMA bit */
  5015. SET_BIT(hi2c->Instance->CR2, I2C_CR2_LAST);
  5016. }
  5017. /* Clear ADDR flag */
  5018. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  5019. }
  5020. else
  5021. {
  5022. /* Enable Acknowledge */
  5023. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  5024. if (((hi2c->Instance->CR2 & I2C_CR2_DMAEN) == I2C_CR2_DMAEN) && ((CurrentXferOptions == I2C_NO_OPTION_FRAME) || (CurrentXferOptions == I2C_FIRST_FRAME) || (CurrentXferOptions == I2C_FIRST_AND_LAST_FRAME) || (CurrentXferOptions == I2C_LAST_FRAME_NO_STOP) || (CurrentXferOptions == I2C_LAST_FRAME)))
  5025. {
  5026. /* Enable Last DMA bit */
  5027. SET_BIT(hi2c->Instance->CR2, I2C_CR2_LAST);
  5028. }
  5029. /* Clear ADDR flag */
  5030. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  5031. }
  5032. /* Reset Event counter */
  5033. hi2c->EventCount = 0U;
  5034. }
  5035. }
  5036. else
  5037. {
  5038. /* Clear ADDR flag */
  5039. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  5040. }
  5041. }
  5042. /**
  5043. * @brief Handle TXE flag for Slave
  5044. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  5045. * the configuration information for I2C module
  5046. * @retval None
  5047. */
  5048. static void I2C_SlaveTransmit_TXE(I2C_HandleTypeDef *hi2c)
  5049. {
  5050. /* Declaration of temporary variables to prevent undefined behavior of volatile usage */
  5051. HAL_I2C_StateTypeDef CurrentState = hi2c->State;
  5052. if (hi2c->XferCount != 0U)
  5053. {
  5054. /* Write data to DR */
  5055. hi2c->Instance->DR = *hi2c->pBuffPtr;
  5056. /* Increment Buffer pointer */
  5057. hi2c->pBuffPtr++;
  5058. /* Update counter */
  5059. hi2c->XferCount--;
  5060. if ((hi2c->XferCount == 0U) && (CurrentState == HAL_I2C_STATE_BUSY_TX_LISTEN))
  5061. {
  5062. /* Last Byte is received, disable Interrupt */
  5063. __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_BUF);
  5064. /* Set state at HAL_I2C_STATE_LISTEN */
  5065. hi2c->PreviousState = I2C_STATE_SLAVE_BUSY_TX;
  5066. hi2c->State = HAL_I2C_STATE_LISTEN;
  5067. /* Call the corresponding callback to inform upper layer of End of Transfer */
  5068. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  5069. hi2c->SlaveTxCpltCallback(hi2c);
  5070. #else
  5071. HAL_I2C_SlaveTxCpltCallback(hi2c);
  5072. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  5073. }
  5074. }
  5075. }
  5076. /**
  5077. * @brief Handle BTF flag for Slave transmitter
  5078. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  5079. * the configuration information for I2C module
  5080. * @retval None
  5081. */
  5082. static void I2C_SlaveTransmit_BTF(I2C_HandleTypeDef *hi2c)
  5083. {
  5084. if (hi2c->XferCount != 0U)
  5085. {
  5086. /* Write data to DR */
  5087. hi2c->Instance->DR = *hi2c->pBuffPtr;
  5088. /* Increment Buffer pointer */
  5089. hi2c->pBuffPtr++;
  5090. /* Update counter */
  5091. hi2c->XferCount--;
  5092. }
  5093. }
  5094. /**
  5095. * @brief Handle RXNE flag for Slave
  5096. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  5097. * the configuration information for I2C module
  5098. * @retval None
  5099. */
  5100. static void I2C_SlaveReceive_RXNE(I2C_HandleTypeDef *hi2c)
  5101. {
  5102. /* Declaration of temporary variables to prevent undefined behavior of volatile usage */
  5103. HAL_I2C_StateTypeDef CurrentState = hi2c->State;
  5104. if (hi2c->XferCount != 0U)
  5105. {
  5106. /* Read data from DR */
  5107. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
  5108. /* Increment Buffer pointer */
  5109. hi2c->pBuffPtr++;
  5110. /* Update counter */
  5111. hi2c->XferCount--;
  5112. if ((hi2c->XferCount == 0U) && (CurrentState == HAL_I2C_STATE_BUSY_RX_LISTEN))
  5113. {
  5114. /* Last Byte is received, disable Interrupt */
  5115. __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_BUF);
  5116. /* Set state at HAL_I2C_STATE_LISTEN */
  5117. hi2c->PreviousState = I2C_STATE_SLAVE_BUSY_RX;
  5118. hi2c->State = HAL_I2C_STATE_LISTEN;
  5119. /* Call the corresponding callback to inform upper layer of End of Transfer */
  5120. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  5121. hi2c->SlaveRxCpltCallback(hi2c);
  5122. #else
  5123. HAL_I2C_SlaveRxCpltCallback(hi2c);
  5124. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  5125. }
  5126. }
  5127. }
  5128. /**
  5129. * @brief Handle BTF flag for Slave receiver
  5130. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  5131. * the configuration information for I2C module
  5132. * @retval None
  5133. */
  5134. static void I2C_SlaveReceive_BTF(I2C_HandleTypeDef *hi2c)
  5135. {
  5136. if (hi2c->XferCount != 0U)
  5137. {
  5138. /* Read data from DR */
  5139. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
  5140. /* Increment Buffer pointer */
  5141. hi2c->pBuffPtr++;
  5142. /* Update counter */
  5143. hi2c->XferCount--;
  5144. }
  5145. }
  5146. /**
  5147. * @brief Handle ADD flag for Slave
  5148. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  5149. * the configuration information for I2C module
  5150. * @param IT2Flags Interrupt2 flags to handle.
  5151. * @retval None
  5152. */
  5153. static void I2C_Slave_ADDR(I2C_HandleTypeDef *hi2c, uint32_t IT2Flags)
  5154. {
  5155. uint8_t TransferDirection = I2C_DIRECTION_RECEIVE;
  5156. uint16_t SlaveAddrCode;
  5157. if (((uint32_t)hi2c->State & (uint32_t)HAL_I2C_STATE_LISTEN) == (uint32_t)HAL_I2C_STATE_LISTEN)
  5158. {
  5159. /* Disable BUF interrupt, BUF enabling is manage through slave specific interface */
  5160. __HAL_I2C_DISABLE_IT(hi2c, (I2C_IT_BUF));
  5161. /* Transfer Direction requested by Master */
  5162. if (I2C_CHECK_FLAG(IT2Flags, I2C_FLAG_TRA) == RESET)
  5163. {
  5164. TransferDirection = I2C_DIRECTION_TRANSMIT;
  5165. }
  5166. if (I2C_CHECK_FLAG(IT2Flags, I2C_FLAG_DUALF) == RESET)
  5167. {
  5168. SlaveAddrCode = (uint16_t)hi2c->Init.OwnAddress1;
  5169. }
  5170. else
  5171. {
  5172. SlaveAddrCode = (uint16_t)hi2c->Init.OwnAddress2;
  5173. }
  5174. /* Process Unlocked */
  5175. __HAL_UNLOCK(hi2c);
  5176. /* Call Slave Addr callback */
  5177. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  5178. hi2c->AddrCallback(hi2c, TransferDirection, SlaveAddrCode);
  5179. #else
  5180. HAL_I2C_AddrCallback(hi2c, TransferDirection, SlaveAddrCode);
  5181. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  5182. }
  5183. else
  5184. {
  5185. /* Clear ADDR flag */
  5186. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  5187. /* Process Unlocked */
  5188. __HAL_UNLOCK(hi2c);
  5189. }
  5190. }
  5191. /**
  5192. * @brief Handle STOPF flag for Slave
  5193. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  5194. * the configuration information for I2C module
  5195. * @retval None
  5196. */
  5197. static void I2C_Slave_STOPF(I2C_HandleTypeDef *hi2c)
  5198. {
  5199. /* Declaration of temporary variable to prevent undefined behavior of volatile usage */
  5200. HAL_I2C_StateTypeDef CurrentState = hi2c->State;
  5201. /* Disable EVT, BUF and ERR interrupt */
  5202. __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  5203. /* Clear STOPF flag */
  5204. __HAL_I2C_CLEAR_STOPFLAG(hi2c);
  5205. /* Disable Acknowledge */
  5206. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  5207. /* If a DMA is ongoing, Update handle size context */
  5208. if ((hi2c->Instance->CR2 & I2C_CR2_DMAEN) == I2C_CR2_DMAEN)
  5209. {
  5210. if ((CurrentState == HAL_I2C_STATE_BUSY_RX) || (CurrentState == HAL_I2C_STATE_BUSY_RX_LISTEN))
  5211. {
  5212. hi2c->XferCount = (uint16_t)(I2C_GET_DMA_REMAIN_DATA(hi2c->hdmarx));
  5213. if (hi2c->XferCount != 0U)
  5214. {
  5215. /* Set ErrorCode corresponding to a Non-Acknowledge */
  5216. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  5217. }
  5218. /* Disable, stop the current DMA */
  5219. CLEAR_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
  5220. /* Abort DMA Xfer if any */
  5221. if (HAL_DMA_GetState(hi2c->hdmarx) != HAL_DMA_STATE_READY)
  5222. {
  5223. /* Set the I2C DMA Abort callback :
  5224. will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
  5225. hi2c->hdmarx->XferAbortCallback = I2C_DMAAbort;
  5226. /* Abort DMA RX */
  5227. if (HAL_DMA_Abort_IT(hi2c->hdmarx) != HAL_OK)
  5228. {
  5229. /* Call Directly XferAbortCallback function in case of error */
  5230. hi2c->hdmarx->XferAbortCallback(hi2c->hdmarx);
  5231. }
  5232. }
  5233. }
  5234. else
  5235. {
  5236. hi2c->XferCount = (uint16_t)(I2C_GET_DMA_REMAIN_DATA(hi2c->hdmatx));
  5237. if (hi2c->XferCount != 0U)
  5238. {
  5239. /* Set ErrorCode corresponding to a Non-Acknowledge */
  5240. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  5241. }
  5242. /* Disable, stop the current DMA */
  5243. CLEAR_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
  5244. /* Abort DMA Xfer if any */
  5245. if (HAL_DMA_GetState(hi2c->hdmatx) != HAL_DMA_STATE_READY)
  5246. {
  5247. /* Set the I2C DMA Abort callback :
  5248. will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
  5249. hi2c->hdmatx->XferAbortCallback = I2C_DMAAbort;
  5250. /* Abort DMA TX */
  5251. if (HAL_DMA_Abort_IT(hi2c->hdmatx) != HAL_OK)
  5252. {
  5253. /* Call Directly XferAbortCallback function in case of error */
  5254. hi2c->hdmatx->XferAbortCallback(hi2c->hdmatx);
  5255. }
  5256. }
  5257. }
  5258. }
  5259. /* All data are not transferred, so set error code accordingly */
  5260. if (hi2c->XferCount != 0U)
  5261. {
  5262. /* Store Last receive data if any */
  5263. if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET)
  5264. {
  5265. /* Read data from DR */
  5266. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
  5267. /* Increment Buffer pointer */
  5268. hi2c->pBuffPtr++;
  5269. /* Update counter */
  5270. hi2c->XferCount--;
  5271. }
  5272. /* Store Last receive data if any */
  5273. if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET)
  5274. {
  5275. /* Read data from DR */
  5276. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
  5277. /* Increment Buffer pointer */
  5278. hi2c->pBuffPtr++;
  5279. /* Update counter */
  5280. hi2c->XferCount--;
  5281. }
  5282. if (hi2c->XferCount != 0U)
  5283. {
  5284. /* Set ErrorCode corresponding to a Non-Acknowledge */
  5285. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  5286. }
  5287. }
  5288. if (hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
  5289. {
  5290. /* Call the corresponding callback to inform upper layer of End of Transfer */
  5291. I2C_ITError(hi2c);
  5292. }
  5293. else
  5294. {
  5295. if (CurrentState == HAL_I2C_STATE_BUSY_RX_LISTEN)
  5296. {
  5297. /* Set state at HAL_I2C_STATE_LISTEN */
  5298. hi2c->PreviousState = I2C_STATE_NONE;
  5299. hi2c->State = HAL_I2C_STATE_LISTEN;
  5300. /* Call the corresponding callback to inform upper layer of End of Transfer */
  5301. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  5302. hi2c->SlaveRxCpltCallback(hi2c);
  5303. #else
  5304. HAL_I2C_SlaveRxCpltCallback(hi2c);
  5305. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  5306. }
  5307. if (hi2c->State == HAL_I2C_STATE_LISTEN)
  5308. {
  5309. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  5310. hi2c->PreviousState = I2C_STATE_NONE;
  5311. hi2c->State = HAL_I2C_STATE_READY;
  5312. hi2c->Mode = HAL_I2C_MODE_NONE;
  5313. /* Call the Listen Complete callback, to inform upper layer of the end of Listen usecase */
  5314. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  5315. hi2c->ListenCpltCallback(hi2c);
  5316. #else
  5317. HAL_I2C_ListenCpltCallback(hi2c);
  5318. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  5319. }
  5320. else
  5321. {
  5322. if ((hi2c->PreviousState == I2C_STATE_SLAVE_BUSY_RX) || (CurrentState == HAL_I2C_STATE_BUSY_RX))
  5323. {
  5324. hi2c->PreviousState = I2C_STATE_NONE;
  5325. hi2c->State = HAL_I2C_STATE_READY;
  5326. hi2c->Mode = HAL_I2C_MODE_NONE;
  5327. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  5328. hi2c->SlaveRxCpltCallback(hi2c);
  5329. #else
  5330. HAL_I2C_SlaveRxCpltCallback(hi2c);
  5331. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  5332. }
  5333. }
  5334. }
  5335. }
  5336. /**
  5337. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  5338. * the configuration information for I2C module
  5339. * @retval None
  5340. */
  5341. static void I2C_Slave_AF(I2C_HandleTypeDef *hi2c)
  5342. {
  5343. /* Declaration of temporary variables to prevent undefined behavior of volatile usage */
  5344. HAL_I2C_StateTypeDef CurrentState = hi2c->State;
  5345. uint32_t CurrentXferOptions = hi2c->XferOptions;
  5346. if (((CurrentXferOptions == I2C_FIRST_AND_LAST_FRAME) || (CurrentXferOptions == I2C_LAST_FRAME)) && \
  5347. (CurrentState == HAL_I2C_STATE_LISTEN))
  5348. {
  5349. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  5350. /* Disable EVT, BUF and ERR interrupt */
  5351. __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  5352. /* Clear AF flag */
  5353. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  5354. /* Disable Acknowledge */
  5355. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  5356. hi2c->PreviousState = I2C_STATE_NONE;
  5357. hi2c->State = HAL_I2C_STATE_READY;
  5358. hi2c->Mode = HAL_I2C_MODE_NONE;
  5359. /* Call the Listen Complete callback, to inform upper layer of the end of Listen usecase */
  5360. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  5361. hi2c->ListenCpltCallback(hi2c);
  5362. #else
  5363. HAL_I2C_ListenCpltCallback(hi2c);
  5364. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  5365. }
  5366. else if (CurrentState == HAL_I2C_STATE_BUSY_TX)
  5367. {
  5368. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  5369. hi2c->PreviousState = I2C_STATE_SLAVE_BUSY_TX;
  5370. hi2c->State = HAL_I2C_STATE_READY;
  5371. hi2c->Mode = HAL_I2C_MODE_NONE;
  5372. /* Disable EVT, BUF and ERR interrupt */
  5373. __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  5374. /* Clear AF flag */
  5375. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  5376. /* Disable Acknowledge */
  5377. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  5378. /* Clear TXE flag */
  5379. I2C_Flush_DR(hi2c);
  5380. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  5381. hi2c->SlaveTxCpltCallback(hi2c);
  5382. #else
  5383. HAL_I2C_SlaveTxCpltCallback(hi2c);
  5384. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  5385. }
  5386. else
  5387. {
  5388. /* Clear AF flag only */
  5389. /* State Listen, but XferOptions == FIRST or NEXT */
  5390. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  5391. }
  5392. }
  5393. /**
  5394. * @brief I2C interrupts error process
  5395. * @param hi2c I2C handle.
  5396. * @retval None
  5397. */
  5398. static void I2C_ITError(I2C_HandleTypeDef *hi2c)
  5399. {
  5400. /* Declaration of temporary variable to prevent undefined behavior of volatile usage */
  5401. HAL_I2C_StateTypeDef CurrentState = hi2c->State;
  5402. HAL_I2C_ModeTypeDef CurrentMode = hi2c->Mode;
  5403. uint32_t CurrentError;
  5404. if (((CurrentMode == HAL_I2C_MODE_MASTER) || (CurrentMode == HAL_I2C_MODE_MEM)) && (CurrentState == HAL_I2C_STATE_BUSY_RX))
  5405. {
  5406. /* Disable Pos bit in I2C CR1 when error occurred in Master/Mem Receive IT Process */
  5407. hi2c->Instance->CR1 &= ~I2C_CR1_POS;
  5408. }
  5409. if (((uint32_t)CurrentState & (uint32_t)HAL_I2C_STATE_LISTEN) == (uint32_t)HAL_I2C_STATE_LISTEN)
  5410. {
  5411. /* keep HAL_I2C_STATE_LISTEN */
  5412. hi2c->PreviousState = I2C_STATE_NONE;
  5413. hi2c->State = HAL_I2C_STATE_LISTEN;
  5414. }
  5415. else
  5416. {
  5417. /* If state is an abort treatment on going, don't change state */
  5418. /* This change will be do later */
  5419. if ((READ_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN) != I2C_CR2_DMAEN) && (CurrentState != HAL_I2C_STATE_ABORT))
  5420. {
  5421. hi2c->State = HAL_I2C_STATE_READY;
  5422. hi2c->Mode = HAL_I2C_MODE_NONE;
  5423. }
  5424. hi2c->PreviousState = I2C_STATE_NONE;
  5425. }
  5426. /* Abort DMA transfer */
  5427. if (READ_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN) == I2C_CR2_DMAEN)
  5428. {
  5429. hi2c->Instance->CR2 &= ~I2C_CR2_DMAEN;
  5430. if (hi2c->hdmatx->State != HAL_DMA_STATE_READY)
  5431. {
  5432. /* Set the DMA Abort callback :
  5433. will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
  5434. hi2c->hdmatx->XferAbortCallback = I2C_DMAAbort;
  5435. if (HAL_DMA_Abort_IT(hi2c->hdmatx) != HAL_OK)
  5436. {
  5437. /* Disable I2C peripheral to prevent dummy data in buffer */
  5438. __HAL_I2C_DISABLE(hi2c);
  5439. hi2c->State = HAL_I2C_STATE_READY;
  5440. /* Call Directly XferAbortCallback function in case of error */
  5441. hi2c->hdmatx->XferAbortCallback(hi2c->hdmatx);
  5442. }
  5443. }
  5444. else
  5445. {
  5446. /* Set the DMA Abort callback :
  5447. will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
  5448. hi2c->hdmarx->XferAbortCallback = I2C_DMAAbort;
  5449. if (HAL_DMA_Abort_IT(hi2c->hdmarx) != HAL_OK)
  5450. {
  5451. /* Store Last receive data if any */
  5452. if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET)
  5453. {
  5454. /* Read data from DR */
  5455. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
  5456. /* Increment Buffer pointer */
  5457. hi2c->pBuffPtr++;
  5458. }
  5459. /* Disable I2C peripheral to prevent dummy data in buffer */
  5460. __HAL_I2C_DISABLE(hi2c);
  5461. hi2c->State = HAL_I2C_STATE_READY;
  5462. /* Call Directly hi2c->hdmarx->XferAbortCallback function in case of error */
  5463. hi2c->hdmarx->XferAbortCallback(hi2c->hdmarx);
  5464. }
  5465. }
  5466. }
  5467. else if (hi2c->State == HAL_I2C_STATE_ABORT)
  5468. {
  5469. hi2c->State = HAL_I2C_STATE_READY;
  5470. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  5471. /* Store Last receive data if any */
  5472. if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET)
  5473. {
  5474. /* Read data from DR */
  5475. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
  5476. /* Increment Buffer pointer */
  5477. hi2c->pBuffPtr++;
  5478. }
  5479. /* Disable I2C peripheral to prevent dummy data in buffer */
  5480. __HAL_I2C_DISABLE(hi2c);
  5481. /* Call the corresponding callback to inform upper layer of End of Transfer */
  5482. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  5483. hi2c->AbortCpltCallback(hi2c);
  5484. #else
  5485. HAL_I2C_AbortCpltCallback(hi2c);
  5486. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  5487. }
  5488. else
  5489. {
  5490. /* Store Last receive data if any */
  5491. if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET)
  5492. {
  5493. /* Read data from DR */
  5494. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
  5495. /* Increment Buffer pointer */
  5496. hi2c->pBuffPtr++;
  5497. }
  5498. /* Call user error callback */
  5499. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  5500. hi2c->ErrorCallback(hi2c);
  5501. #else
  5502. HAL_I2C_ErrorCallback(hi2c);
  5503. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  5504. }
  5505. /* STOP Flag is not set after a NACK reception, BusError, ArbitrationLost, OverRun */
  5506. CurrentError = hi2c->ErrorCode;
  5507. if (((CurrentError & HAL_I2C_ERROR_BERR) == HAL_I2C_ERROR_BERR) || \
  5508. ((CurrentError & HAL_I2C_ERROR_ARLO) == HAL_I2C_ERROR_ARLO) || \
  5509. ((CurrentError & HAL_I2C_ERROR_AF) == HAL_I2C_ERROR_AF) || \
  5510. ((CurrentError & HAL_I2C_ERROR_OVR) == HAL_I2C_ERROR_OVR))
  5511. {
  5512. /* Disable EVT, BUF and ERR interrupt */
  5513. __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  5514. }
  5515. /* So may inform upper layer that listen phase is stopped */
  5516. /* during NACK error treatment */
  5517. CurrentState = hi2c->State;
  5518. if (((hi2c->ErrorCode & HAL_I2C_ERROR_AF) == HAL_I2C_ERROR_AF) && (CurrentState == HAL_I2C_STATE_LISTEN))
  5519. {
  5520. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  5521. hi2c->PreviousState = I2C_STATE_NONE;
  5522. hi2c->State = HAL_I2C_STATE_READY;
  5523. hi2c->Mode = HAL_I2C_MODE_NONE;
  5524. /* Call the Listen Complete callback, to inform upper layer of the end of Listen usecase */
  5525. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  5526. hi2c->ListenCpltCallback(hi2c);
  5527. #else
  5528. HAL_I2C_ListenCpltCallback(hi2c);
  5529. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  5530. }
  5531. }
  5532. /**
  5533. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  5534. * the configuration information for I2C module
  5535. * @param DevAddress Target device address: The device 7 bits address value
  5536. * in datasheet must be shifted to the left before calling the interface
  5537. * @param Timeout Timeout duration
  5538. * @param Tickstart Tick start value
  5539. * @retval HAL status
  5540. */
  5541. static HAL_StatusTypeDef I2C_MasterRequestWrite(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint32_t Timeout, uint32_t Tickstart)
  5542. {
  5543. /* Declaration of temporary variable to prevent undefined behavior of volatile usage */
  5544. uint32_t CurrentXferOptions = hi2c->XferOptions;
  5545. /* Generate Start condition if first transfer */
  5546. if ((CurrentXferOptions == I2C_FIRST_AND_LAST_FRAME) || (CurrentXferOptions == I2C_FIRST_FRAME) || (CurrentXferOptions == I2C_NO_OPTION_FRAME))
  5547. {
  5548. /* Generate Start */
  5549. SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
  5550. }
  5551. else if (hi2c->PreviousState == I2C_STATE_MASTER_BUSY_RX)
  5552. {
  5553. /* Generate ReStart */
  5554. SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
  5555. }
  5556. else
  5557. {
  5558. /* Do nothing */
  5559. }
  5560. /* Wait until SB flag is set */
  5561. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout, Tickstart) != HAL_OK)
  5562. {
  5563. if (READ_BIT(hi2c->Instance->CR1, I2C_CR1_START) == I2C_CR1_START)
  5564. {
  5565. hi2c->ErrorCode = HAL_I2C_WRONG_START;
  5566. }
  5567. return HAL_TIMEOUT;
  5568. }
  5569. if (hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_7BIT)
  5570. {
  5571. /* Send slave address */
  5572. hi2c->Instance->DR = I2C_7BIT_ADD_WRITE(DevAddress);
  5573. }
  5574. else
  5575. {
  5576. /* Send header of slave address */
  5577. hi2c->Instance->DR = I2C_10BIT_HEADER_WRITE(DevAddress);
  5578. /* Wait until ADD10 flag is set */
  5579. if (I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADD10, Timeout, Tickstart) != HAL_OK)
  5580. {
  5581. return HAL_ERROR;
  5582. }
  5583. /* Send slave address */
  5584. hi2c->Instance->DR = I2C_10BIT_ADDRESS(DevAddress);
  5585. }
  5586. /* Wait until ADDR flag is set */
  5587. if (I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, Timeout, Tickstart) != HAL_OK)
  5588. {
  5589. return HAL_ERROR;
  5590. }
  5591. return HAL_OK;
  5592. }
  5593. /**
  5594. * @brief Master sends target device address for read request.
  5595. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  5596. * the configuration information for I2C module
  5597. * @param DevAddress Target device address: The device 7 bits address value
  5598. * in datasheet must be shifted to the left before calling the interface
  5599. * @param Timeout Timeout duration
  5600. * @param Tickstart Tick start value
  5601. * @retval HAL status
  5602. */
  5603. static HAL_StatusTypeDef I2C_MasterRequestRead(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint32_t Timeout, uint32_t Tickstart)
  5604. {
  5605. /* Declaration of temporary variable to prevent undefined behavior of volatile usage */
  5606. uint32_t CurrentXferOptions = hi2c->XferOptions;
  5607. /* Enable Acknowledge */
  5608. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  5609. /* Generate Start condition if first transfer */
  5610. if ((CurrentXferOptions == I2C_FIRST_AND_LAST_FRAME) || (CurrentXferOptions == I2C_FIRST_FRAME) || (CurrentXferOptions == I2C_NO_OPTION_FRAME))
  5611. {
  5612. /* Generate Start */
  5613. SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
  5614. }
  5615. else if (hi2c->PreviousState == I2C_STATE_MASTER_BUSY_TX)
  5616. {
  5617. /* Generate ReStart */
  5618. SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
  5619. }
  5620. else
  5621. {
  5622. /* Do nothing */
  5623. }
  5624. /* Wait until SB flag is set */
  5625. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout, Tickstart) != HAL_OK)
  5626. {
  5627. if (READ_BIT(hi2c->Instance->CR1, I2C_CR1_START) == I2C_CR1_START)
  5628. {
  5629. hi2c->ErrorCode = HAL_I2C_WRONG_START;
  5630. }
  5631. return HAL_TIMEOUT;
  5632. }
  5633. if (hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_7BIT)
  5634. {
  5635. /* Send slave address */
  5636. hi2c->Instance->DR = I2C_7BIT_ADD_READ(DevAddress);
  5637. }
  5638. else
  5639. {
  5640. /* Send header of slave address */
  5641. hi2c->Instance->DR = I2C_10BIT_HEADER_WRITE(DevAddress);
  5642. /* Wait until ADD10 flag is set */
  5643. if (I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADD10, Timeout, Tickstart) != HAL_OK)
  5644. {
  5645. return HAL_ERROR;
  5646. }
  5647. /* Send slave address */
  5648. hi2c->Instance->DR = I2C_10BIT_ADDRESS(DevAddress);
  5649. /* Wait until ADDR flag is set */
  5650. if (I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, Timeout, Tickstart) != HAL_OK)
  5651. {
  5652. return HAL_ERROR;
  5653. }
  5654. /* Clear ADDR flag */
  5655. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  5656. /* Generate Restart */
  5657. SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
  5658. /* Wait until SB flag is set */
  5659. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout, Tickstart) != HAL_OK)
  5660. {
  5661. if (READ_BIT(hi2c->Instance->CR1, I2C_CR1_START) == I2C_CR1_START)
  5662. {
  5663. hi2c->ErrorCode = HAL_I2C_WRONG_START;
  5664. }
  5665. return HAL_TIMEOUT;
  5666. }
  5667. /* Send header of slave address */
  5668. hi2c->Instance->DR = I2C_10BIT_HEADER_READ(DevAddress);
  5669. }
  5670. /* Wait until ADDR flag is set */
  5671. if (I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, Timeout, Tickstart) != HAL_OK)
  5672. {
  5673. return HAL_ERROR;
  5674. }
  5675. return HAL_OK;
  5676. }
  5677. /**
  5678. * @brief Master sends target device address followed by internal memory address for write request.
  5679. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  5680. * the configuration information for I2C module
  5681. * @param DevAddress Target device address: The device 7 bits address value
  5682. * in datasheet must be shifted to the left before calling the interface
  5683. * @param MemAddress Internal memory address
  5684. * @param MemAddSize Size of internal memory address
  5685. * @param Timeout Timeout duration
  5686. * @param Tickstart Tick start value
  5687. * @retval HAL status
  5688. */
  5689. static HAL_StatusTypeDef I2C_RequestMemoryWrite(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout, uint32_t Tickstart)
  5690. {
  5691. /* Generate Start */
  5692. SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
  5693. /* Wait until SB flag is set */
  5694. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout, Tickstart) != HAL_OK)
  5695. {
  5696. if (READ_BIT(hi2c->Instance->CR1, I2C_CR1_START) == I2C_CR1_START)
  5697. {
  5698. hi2c->ErrorCode = HAL_I2C_WRONG_START;
  5699. }
  5700. return HAL_TIMEOUT;
  5701. }
  5702. /* Send slave address */
  5703. hi2c->Instance->DR = I2C_7BIT_ADD_WRITE(DevAddress);
  5704. /* Wait until ADDR flag is set */
  5705. if (I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, Timeout, Tickstart) != HAL_OK)
  5706. {
  5707. return HAL_ERROR;
  5708. }
  5709. /* Clear ADDR flag */
  5710. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  5711. /* Wait until TXE flag is set */
  5712. if (I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, Tickstart) != HAL_OK)
  5713. {
  5714. if (hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  5715. {
  5716. /* Generate Stop */
  5717. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  5718. }
  5719. return HAL_ERROR;
  5720. }
  5721. /* If Memory address size is 8Bit */
  5722. if (MemAddSize == I2C_MEMADD_SIZE_8BIT)
  5723. {
  5724. /* Send Memory Address */
  5725. hi2c->Instance->DR = I2C_MEM_ADD_LSB(MemAddress);
  5726. }
  5727. /* If Memory address size is 16Bit */
  5728. else
  5729. {
  5730. /* Send MSB of Memory Address */
  5731. hi2c->Instance->DR = I2C_MEM_ADD_MSB(MemAddress);
  5732. /* Wait until TXE flag is set */
  5733. if (I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, Tickstart) != HAL_OK)
  5734. {
  5735. if (hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  5736. {
  5737. /* Generate Stop */
  5738. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  5739. }
  5740. return HAL_ERROR;
  5741. }
  5742. /* Send LSB of Memory Address */
  5743. hi2c->Instance->DR = I2C_MEM_ADD_LSB(MemAddress);
  5744. }
  5745. return HAL_OK;
  5746. }
  5747. /**
  5748. * @brief Master sends target device address followed by internal memory address for read request.
  5749. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  5750. * the configuration information for I2C module
  5751. * @param DevAddress Target device address: The device 7 bits address value
  5752. * in datasheet must be shifted to the left before calling the interface
  5753. * @param MemAddress Internal memory address
  5754. * @param MemAddSize Size of internal memory address
  5755. * @param Timeout Timeout duration
  5756. * @param Tickstart Tick start value
  5757. * @retval HAL status
  5758. */
  5759. static HAL_StatusTypeDef I2C_RequestMemoryRead(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout, uint32_t Tickstart)
  5760. {
  5761. /* Enable Acknowledge */
  5762. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  5763. /* Generate Start */
  5764. SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
  5765. /* Wait until SB flag is set */
  5766. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout, Tickstart) != HAL_OK)
  5767. {
  5768. if (READ_BIT(hi2c->Instance->CR1, I2C_CR1_START) == I2C_CR1_START)
  5769. {
  5770. hi2c->ErrorCode = HAL_I2C_WRONG_START;
  5771. }
  5772. return HAL_TIMEOUT;
  5773. }
  5774. /* Send slave address */
  5775. hi2c->Instance->DR = I2C_7BIT_ADD_WRITE(DevAddress);
  5776. /* Wait until ADDR flag is set */
  5777. if (I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, Timeout, Tickstart) != HAL_OK)
  5778. {
  5779. return HAL_ERROR;
  5780. }
  5781. /* Clear ADDR flag */
  5782. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  5783. /* Wait until TXE flag is set */
  5784. if (I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, Tickstart) != HAL_OK)
  5785. {
  5786. if (hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  5787. {
  5788. /* Generate Stop */
  5789. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  5790. }
  5791. return HAL_ERROR;
  5792. }
  5793. /* If Memory address size is 8Bit */
  5794. if (MemAddSize == I2C_MEMADD_SIZE_8BIT)
  5795. {
  5796. /* Send Memory Address */
  5797. hi2c->Instance->DR = I2C_MEM_ADD_LSB(MemAddress);
  5798. }
  5799. /* If Memory address size is 16Bit */
  5800. else
  5801. {
  5802. /* Send MSB of Memory Address */
  5803. hi2c->Instance->DR = I2C_MEM_ADD_MSB(MemAddress);
  5804. /* Wait until TXE flag is set */
  5805. if (I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, Tickstart) != HAL_OK)
  5806. {
  5807. if (hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  5808. {
  5809. /* Generate Stop */
  5810. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  5811. }
  5812. return HAL_ERROR;
  5813. }
  5814. /* Send LSB of Memory Address */
  5815. hi2c->Instance->DR = I2C_MEM_ADD_LSB(MemAddress);
  5816. }
  5817. /* Wait until TXE flag is set */
  5818. if (I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, Tickstart) != HAL_OK)
  5819. {
  5820. if (hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  5821. {
  5822. /* Generate Stop */
  5823. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  5824. }
  5825. return HAL_ERROR;
  5826. }
  5827. /* Generate Restart */
  5828. SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
  5829. /* Wait until SB flag is set */
  5830. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout, Tickstart) != HAL_OK)
  5831. {
  5832. if (READ_BIT(hi2c->Instance->CR1, I2C_CR1_START) == I2C_CR1_START)
  5833. {
  5834. hi2c->ErrorCode = HAL_I2C_WRONG_START;
  5835. }
  5836. return HAL_TIMEOUT;
  5837. }
  5838. /* Send slave address */
  5839. hi2c->Instance->DR = I2C_7BIT_ADD_READ(DevAddress);
  5840. /* Wait until ADDR flag is set */
  5841. if (I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, Timeout, Tickstart) != HAL_OK)
  5842. {
  5843. return HAL_ERROR;
  5844. }
  5845. return HAL_OK;
  5846. }
  5847. /**
  5848. * @brief DMA I2C process complete callback.
  5849. * @param hdma DMA handle
  5850. * @retval None
  5851. */
  5852. static void I2C_DMAXferCplt(DMA_HandleTypeDef *hdma)
  5853. {
  5854. I2C_HandleTypeDef *hi2c = (I2C_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent; /* Derogation MISRAC2012-Rule-11.5 */
  5855. /* Declaration of temporary variable to prevent undefined behavior of volatile usage */
  5856. HAL_I2C_StateTypeDef CurrentState = hi2c->State;
  5857. HAL_I2C_ModeTypeDef CurrentMode = hi2c->Mode;
  5858. uint32_t CurrentXferOptions = hi2c->XferOptions;
  5859. /* Disable EVT and ERR interrupt */
  5860. __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR);
  5861. /* Clear Complete callback */
  5862. if (hi2c->hdmatx != NULL)
  5863. {
  5864. hi2c->hdmatx->XferCpltCallback = NULL;
  5865. }
  5866. if (hi2c->hdmarx != NULL)
  5867. {
  5868. hi2c->hdmarx->XferCpltCallback = NULL;
  5869. }
  5870. if ((((uint32_t)CurrentState & (uint32_t)HAL_I2C_STATE_BUSY_TX) == (uint32_t)HAL_I2C_STATE_BUSY_TX) || ((((uint32_t)CurrentState & (uint32_t)HAL_I2C_STATE_BUSY_RX) == (uint32_t)HAL_I2C_STATE_BUSY_RX) && (CurrentMode == HAL_I2C_MODE_SLAVE)))
  5871. {
  5872. /* Disable DMA Request */
  5873. CLEAR_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
  5874. hi2c->XferCount = 0U;
  5875. if (CurrentState == HAL_I2C_STATE_BUSY_TX_LISTEN)
  5876. {
  5877. /* Set state at HAL_I2C_STATE_LISTEN */
  5878. hi2c->PreviousState = I2C_STATE_SLAVE_BUSY_TX;
  5879. hi2c->State = HAL_I2C_STATE_LISTEN;
  5880. /* Call the corresponding callback to inform upper layer of End of Transfer */
  5881. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  5882. hi2c->SlaveTxCpltCallback(hi2c);
  5883. #else
  5884. HAL_I2C_SlaveTxCpltCallback(hi2c);
  5885. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  5886. }
  5887. else if (CurrentState == HAL_I2C_STATE_BUSY_RX_LISTEN)
  5888. {
  5889. /* Set state at HAL_I2C_STATE_LISTEN */
  5890. hi2c->PreviousState = I2C_STATE_SLAVE_BUSY_RX;
  5891. hi2c->State = HAL_I2C_STATE_LISTEN;
  5892. /* Call the corresponding callback to inform upper layer of End of Transfer */
  5893. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  5894. hi2c->SlaveRxCpltCallback(hi2c);
  5895. #else
  5896. HAL_I2C_SlaveRxCpltCallback(hi2c);
  5897. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  5898. }
  5899. else
  5900. {
  5901. /* Do nothing */
  5902. }
  5903. /* Enable EVT and ERR interrupt to treat end of transfer in IRQ handler */
  5904. __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR);
  5905. }
  5906. /* Check current Mode, in case of treatment DMA handler have been preempted by a prior interrupt */
  5907. else if (hi2c->Mode != HAL_I2C_MODE_NONE)
  5908. {
  5909. if (hi2c->XferCount == (uint16_t)1)
  5910. {
  5911. /* Disable Acknowledge */
  5912. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  5913. }
  5914. /* Disable EVT and ERR interrupt */
  5915. __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR);
  5916. /* Prepare next transfer or stop current transfer */
  5917. if ((CurrentXferOptions == I2C_NO_OPTION_FRAME) || (CurrentXferOptions == I2C_FIRST_AND_LAST_FRAME) || (CurrentXferOptions == I2C_OTHER_AND_LAST_FRAME) || (CurrentXferOptions == I2C_LAST_FRAME))
  5918. {
  5919. /* Generate Stop */
  5920. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  5921. }
  5922. /* Disable Last DMA */
  5923. CLEAR_BIT(hi2c->Instance->CR2, I2C_CR2_LAST);
  5924. /* Disable DMA Request */
  5925. CLEAR_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
  5926. hi2c->XferCount = 0U;
  5927. /* Check if Errors has been detected during transfer */
  5928. if (hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
  5929. {
  5930. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  5931. hi2c->ErrorCallback(hi2c);
  5932. #else
  5933. HAL_I2C_ErrorCallback(hi2c);
  5934. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  5935. }
  5936. else
  5937. {
  5938. hi2c->State = HAL_I2C_STATE_READY;
  5939. if (hi2c->Mode == HAL_I2C_MODE_MEM)
  5940. {
  5941. hi2c->Mode = HAL_I2C_MODE_NONE;
  5942. hi2c->PreviousState = I2C_STATE_NONE;
  5943. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  5944. hi2c->MemRxCpltCallback(hi2c);
  5945. #else
  5946. HAL_I2C_MemRxCpltCallback(hi2c);
  5947. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  5948. }
  5949. else
  5950. {
  5951. hi2c->Mode = HAL_I2C_MODE_NONE;
  5952. if ((CurrentXferOptions == I2C_FIRST_AND_LAST_FRAME) || (CurrentXferOptions == I2C_LAST_FRAME))
  5953. {
  5954. hi2c->PreviousState = I2C_STATE_NONE;
  5955. }
  5956. else
  5957. {
  5958. hi2c->PreviousState = I2C_STATE_MASTER_BUSY_RX;
  5959. }
  5960. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  5961. hi2c->MasterRxCpltCallback(hi2c);
  5962. #else
  5963. HAL_I2C_MasterRxCpltCallback(hi2c);
  5964. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  5965. }
  5966. }
  5967. }
  5968. else
  5969. {
  5970. /* Do nothing */
  5971. }
  5972. }
  5973. /**
  5974. * @brief DMA I2C communication error callback.
  5975. * @param hdma DMA handle
  5976. * @retval None
  5977. */
  5978. static void I2C_DMAError(DMA_HandleTypeDef *hdma)
  5979. {
  5980. I2C_HandleTypeDef *hi2c = (I2C_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent; /* Derogation MISRAC2012-Rule-11.5 */
  5981. /* Clear Complete callback */
  5982. if (hi2c->hdmatx != NULL)
  5983. {
  5984. hi2c->hdmatx->XferCpltCallback = NULL;
  5985. }
  5986. if (hi2c->hdmarx != NULL)
  5987. {
  5988. hi2c->hdmarx->XferCpltCallback = NULL;
  5989. }
  5990. /* Ignore DMA FIFO error */
  5991. if (HAL_DMA_GetError(hdma) != HAL_DMA_ERROR_FE)
  5992. {
  5993. /* Disable Acknowledge */
  5994. hi2c->Instance->CR1 &= ~I2C_CR1_ACK;
  5995. hi2c->XferCount = 0U;
  5996. hi2c->State = HAL_I2C_STATE_READY;
  5997. hi2c->Mode = HAL_I2C_MODE_NONE;
  5998. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
  5999. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  6000. hi2c->ErrorCallback(hi2c);
  6001. #else
  6002. HAL_I2C_ErrorCallback(hi2c);
  6003. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  6004. }
  6005. }
  6006. /**
  6007. * @brief DMA I2C communication abort callback
  6008. * (To be called at end of DMA Abort procedure).
  6009. * @param hdma DMA handle.
  6010. * @retval None
  6011. */
  6012. static void I2C_DMAAbort(DMA_HandleTypeDef *hdma)
  6013. {
  6014. __IO uint32_t count = 0U;
  6015. I2C_HandleTypeDef *hi2c = (I2C_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent; /* Derogation MISRAC2012-Rule-11.5 */
  6016. /* Declaration of temporary variable to prevent undefined behavior of volatile usage */
  6017. HAL_I2C_StateTypeDef CurrentState = hi2c->State;
  6018. /* During abort treatment, check that there is no pending STOP request */
  6019. /* Wait until STOP flag is reset */
  6020. count = I2C_TIMEOUT_FLAG * (SystemCoreClock / 25U / 1000U);
  6021. do
  6022. {
  6023. if (count == 0U)
  6024. {
  6025. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  6026. break;
  6027. }
  6028. count--;
  6029. }
  6030. while (READ_BIT(hi2c->Instance->CR1, I2C_CR1_STOP) == I2C_CR1_STOP);
  6031. /* Clear Complete callback */
  6032. if (hi2c->hdmatx != NULL)
  6033. {
  6034. hi2c->hdmatx->XferCpltCallback = NULL;
  6035. }
  6036. if (hi2c->hdmarx != NULL)
  6037. {
  6038. hi2c->hdmarx->XferCpltCallback = NULL;
  6039. }
  6040. /* Disable Acknowledge */
  6041. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  6042. hi2c->XferCount = 0U;
  6043. /* Reset XferAbortCallback */
  6044. if (hi2c->hdmatx != NULL)
  6045. {
  6046. hi2c->hdmatx->XferAbortCallback = NULL;
  6047. }
  6048. if (hi2c->hdmarx != NULL)
  6049. {
  6050. hi2c->hdmarx->XferAbortCallback = NULL;
  6051. }
  6052. /* Disable I2C peripheral to prevent dummy data in buffer */
  6053. __HAL_I2C_DISABLE(hi2c);
  6054. /* Check if come from abort from user */
  6055. if (hi2c->State == HAL_I2C_STATE_ABORT)
  6056. {
  6057. hi2c->State = HAL_I2C_STATE_READY;
  6058. hi2c->Mode = HAL_I2C_MODE_NONE;
  6059. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  6060. /* Call the corresponding callback to inform upper layer of End of Transfer */
  6061. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  6062. hi2c->AbortCpltCallback(hi2c);
  6063. #else
  6064. HAL_I2C_AbortCpltCallback(hi2c);
  6065. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  6066. }
  6067. else
  6068. {
  6069. if (((uint32_t)CurrentState & (uint32_t)HAL_I2C_STATE_LISTEN) == (uint32_t)HAL_I2C_STATE_LISTEN)
  6070. {
  6071. /* Renable I2C peripheral */
  6072. __HAL_I2C_ENABLE(hi2c);
  6073. /* Enable Acknowledge */
  6074. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  6075. /* keep HAL_I2C_STATE_LISTEN */
  6076. hi2c->PreviousState = I2C_STATE_NONE;
  6077. hi2c->State = HAL_I2C_STATE_LISTEN;
  6078. }
  6079. else
  6080. {
  6081. hi2c->State = HAL_I2C_STATE_READY;
  6082. hi2c->Mode = HAL_I2C_MODE_NONE;
  6083. }
  6084. /* Call the corresponding callback to inform upper layer of End of Transfer */
  6085. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  6086. hi2c->ErrorCallback(hi2c);
  6087. #else
  6088. HAL_I2C_ErrorCallback(hi2c);
  6089. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  6090. }
  6091. }
  6092. /**
  6093. * @brief This function handles I2C Communication Timeout.
  6094. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  6095. * the configuration information for I2C module
  6096. * @param Flag specifies the I2C flag to check.
  6097. * @param Status The new Flag status (SET or RESET).
  6098. * @param Timeout Timeout duration
  6099. * @param Tickstart Tick start value
  6100. * @retval HAL status
  6101. */
  6102. static HAL_StatusTypeDef I2C_WaitOnFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, FlagStatus Status, uint32_t Timeout, uint32_t Tickstart)
  6103. {
  6104. /* Wait until flag is set */
  6105. while (__HAL_I2C_GET_FLAG(hi2c, Flag) == Status)
  6106. {
  6107. /* Check for the Timeout */
  6108. if (Timeout != HAL_MAX_DELAY)
  6109. {
  6110. if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
  6111. {
  6112. if ((__HAL_I2C_GET_FLAG(hi2c, Flag) == Status))
  6113. {
  6114. hi2c->PreviousState = I2C_STATE_NONE;
  6115. hi2c->State = HAL_I2C_STATE_READY;
  6116. hi2c->Mode = HAL_I2C_MODE_NONE;
  6117. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  6118. /* Process Unlocked */
  6119. __HAL_UNLOCK(hi2c);
  6120. return HAL_ERROR;
  6121. }
  6122. }
  6123. }
  6124. }
  6125. return HAL_OK;
  6126. }
  6127. /**
  6128. * @brief This function handles I2C Communication Timeout for Master addressing phase.
  6129. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  6130. * the configuration information for I2C module
  6131. * @param Flag specifies the I2C flag to check.
  6132. * @param Timeout Timeout duration
  6133. * @param Tickstart Tick start value
  6134. * @retval HAL status
  6135. */
  6136. static HAL_StatusTypeDef I2C_WaitOnMasterAddressFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, uint32_t Timeout, uint32_t Tickstart)
  6137. {
  6138. while (__HAL_I2C_GET_FLAG(hi2c, Flag) == RESET)
  6139. {
  6140. if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET)
  6141. {
  6142. /* Generate Stop */
  6143. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  6144. /* Clear AF Flag */
  6145. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  6146. hi2c->PreviousState = I2C_STATE_NONE;
  6147. hi2c->State = HAL_I2C_STATE_READY;
  6148. hi2c->Mode = HAL_I2C_MODE_NONE;
  6149. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  6150. /* Process Unlocked */
  6151. __HAL_UNLOCK(hi2c);
  6152. return HAL_ERROR;
  6153. }
  6154. /* Check for the Timeout */
  6155. if (Timeout != HAL_MAX_DELAY)
  6156. {
  6157. if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
  6158. {
  6159. if ((__HAL_I2C_GET_FLAG(hi2c, Flag) == RESET))
  6160. {
  6161. hi2c->PreviousState = I2C_STATE_NONE;
  6162. hi2c->State = HAL_I2C_STATE_READY;
  6163. hi2c->Mode = HAL_I2C_MODE_NONE;
  6164. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  6165. /* Process Unlocked */
  6166. __HAL_UNLOCK(hi2c);
  6167. return HAL_ERROR;
  6168. }
  6169. }
  6170. }
  6171. }
  6172. return HAL_OK;
  6173. }
  6174. /**
  6175. * @brief This function handles I2C Communication Timeout for specific usage of TXE flag.
  6176. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  6177. * the configuration information for the specified I2C.
  6178. * @param Timeout Timeout duration
  6179. * @param Tickstart Tick start value
  6180. * @retval HAL status
  6181. */
  6182. static HAL_StatusTypeDef I2C_WaitOnTXEFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart)
  6183. {
  6184. while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXE) == RESET)
  6185. {
  6186. /* Check if a NACK is detected */
  6187. if (I2C_IsAcknowledgeFailed(hi2c) != HAL_OK)
  6188. {
  6189. return HAL_ERROR;
  6190. }
  6191. /* Check for the Timeout */
  6192. if (Timeout != HAL_MAX_DELAY)
  6193. {
  6194. if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
  6195. {
  6196. if ((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXE) == RESET))
  6197. {
  6198. hi2c->PreviousState = I2C_STATE_NONE;
  6199. hi2c->State = HAL_I2C_STATE_READY;
  6200. hi2c->Mode = HAL_I2C_MODE_NONE;
  6201. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  6202. /* Process Unlocked */
  6203. __HAL_UNLOCK(hi2c);
  6204. return HAL_ERROR;
  6205. }
  6206. }
  6207. }
  6208. }
  6209. return HAL_OK;
  6210. }
  6211. /**
  6212. * @brief This function handles I2C Communication Timeout for specific usage of BTF flag.
  6213. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  6214. * the configuration information for the specified I2C.
  6215. * @param Timeout Timeout duration
  6216. * @param Tickstart Tick start value
  6217. * @retval HAL status
  6218. */
  6219. static HAL_StatusTypeDef I2C_WaitOnBTFFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart)
  6220. {
  6221. while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == RESET)
  6222. {
  6223. /* Check if a NACK is detected */
  6224. if (I2C_IsAcknowledgeFailed(hi2c) != HAL_OK)
  6225. {
  6226. return HAL_ERROR;
  6227. }
  6228. /* Check for the Timeout */
  6229. if (Timeout != HAL_MAX_DELAY)
  6230. {
  6231. if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
  6232. {
  6233. if ((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == RESET))
  6234. {
  6235. hi2c->PreviousState = I2C_STATE_NONE;
  6236. hi2c->State = HAL_I2C_STATE_READY;
  6237. hi2c->Mode = HAL_I2C_MODE_NONE;
  6238. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  6239. /* Process Unlocked */
  6240. __HAL_UNLOCK(hi2c);
  6241. return HAL_ERROR;
  6242. }
  6243. }
  6244. }
  6245. }
  6246. return HAL_OK;
  6247. }
  6248. /**
  6249. * @brief This function handles I2C Communication Timeout for specific usage of STOP flag.
  6250. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  6251. * the configuration information for the specified I2C.
  6252. * @param Timeout Timeout duration
  6253. * @param Tickstart Tick start value
  6254. * @retval HAL status
  6255. */
  6256. static HAL_StatusTypeDef I2C_WaitOnSTOPFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart)
  6257. {
  6258. while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == RESET)
  6259. {
  6260. /* Check if a NACK is detected */
  6261. if (I2C_IsAcknowledgeFailed(hi2c) != HAL_OK)
  6262. {
  6263. return HAL_ERROR;
  6264. }
  6265. /* Check for the Timeout */
  6266. if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
  6267. {
  6268. if ((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == RESET))
  6269. {
  6270. hi2c->PreviousState = I2C_STATE_NONE;
  6271. hi2c->State = HAL_I2C_STATE_READY;
  6272. hi2c->Mode = HAL_I2C_MODE_NONE;
  6273. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  6274. /* Process Unlocked */
  6275. __HAL_UNLOCK(hi2c);
  6276. return HAL_ERROR;
  6277. }
  6278. }
  6279. }
  6280. return HAL_OK;
  6281. }
  6282. /**
  6283. * @brief This function handles I2C Communication Timeout for specific usage of STOP request through Interrupt.
  6284. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  6285. * the configuration information for the specified I2C.
  6286. * @retval HAL status
  6287. */
  6288. static HAL_StatusTypeDef I2C_WaitOnSTOPRequestThroughIT(I2C_HandleTypeDef *hi2c)
  6289. {
  6290. __IO uint32_t count = 0U;
  6291. /* Wait until STOP flag is reset */
  6292. count = I2C_TIMEOUT_STOP_FLAG * (SystemCoreClock / 25U / 1000U);
  6293. do
  6294. {
  6295. count--;
  6296. if (count == 0U)
  6297. {
  6298. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  6299. return HAL_ERROR;
  6300. }
  6301. }
  6302. while (READ_BIT(hi2c->Instance->CR1, I2C_CR1_STOP) == I2C_CR1_STOP);
  6303. return HAL_OK;
  6304. }
  6305. /**
  6306. * @brief This function handles I2C Communication Timeout for specific usage of RXNE flag.
  6307. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  6308. * the configuration information for the specified I2C.
  6309. * @param Timeout Timeout duration
  6310. * @param Tickstart Tick start value
  6311. * @retval HAL status
  6312. */
  6313. static HAL_StatusTypeDef I2C_WaitOnRXNEFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart)
  6314. {
  6315. while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == RESET)
  6316. {
  6317. /* Check if a STOPF is detected */
  6318. if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET)
  6319. {
  6320. /* Clear STOP Flag */
  6321. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  6322. hi2c->PreviousState = I2C_STATE_NONE;
  6323. hi2c->State = HAL_I2C_STATE_READY;
  6324. hi2c->Mode = HAL_I2C_MODE_NONE;
  6325. hi2c->ErrorCode |= HAL_I2C_ERROR_NONE;
  6326. /* Process Unlocked */
  6327. __HAL_UNLOCK(hi2c);
  6328. return HAL_ERROR;
  6329. }
  6330. /* Check for the Timeout */
  6331. if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
  6332. {
  6333. if ((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == RESET))
  6334. {
  6335. hi2c->PreviousState = I2C_STATE_NONE;
  6336. hi2c->State = HAL_I2C_STATE_READY;
  6337. hi2c->Mode = HAL_I2C_MODE_NONE;
  6338. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  6339. /* Process Unlocked */
  6340. __HAL_UNLOCK(hi2c);
  6341. return HAL_ERROR;
  6342. }
  6343. }
  6344. }
  6345. return HAL_OK;
  6346. }
  6347. /**
  6348. * @brief This function handles Acknowledge failed detection during an I2C Communication.
  6349. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  6350. * the configuration information for the specified I2C.
  6351. * @retval HAL status
  6352. */
  6353. static HAL_StatusTypeDef I2C_IsAcknowledgeFailed(I2C_HandleTypeDef *hi2c)
  6354. {
  6355. if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET)
  6356. {
  6357. /* Clear NACKF Flag */
  6358. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  6359. hi2c->PreviousState = I2C_STATE_NONE;
  6360. hi2c->State = HAL_I2C_STATE_READY;
  6361. hi2c->Mode = HAL_I2C_MODE_NONE;
  6362. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  6363. /* Process Unlocked */
  6364. __HAL_UNLOCK(hi2c);
  6365. return HAL_ERROR;
  6366. }
  6367. return HAL_OK;
  6368. }
  6369. /**
  6370. * @brief Convert I2Cx OTHER_xxx XferOptions to functional XferOptions.
  6371. * @param hi2c I2C handle.
  6372. * @retval None
  6373. */
  6374. static void I2C_ConvertOtherXferOptions(I2C_HandleTypeDef *hi2c)
  6375. {
  6376. /* if user set XferOptions to I2C_OTHER_FRAME */
  6377. /* it request implicitly to generate a restart condition */
  6378. /* set XferOptions to I2C_FIRST_FRAME */
  6379. if (hi2c->XferOptions == I2C_OTHER_FRAME)
  6380. {
  6381. hi2c->XferOptions = I2C_FIRST_FRAME;
  6382. }
  6383. /* else if user set XferOptions to I2C_OTHER_AND_LAST_FRAME */
  6384. /* it request implicitly to generate a restart condition */
  6385. /* then generate a stop condition at the end of transfer */
  6386. /* set XferOptions to I2C_FIRST_AND_LAST_FRAME */
  6387. else if (hi2c->XferOptions == I2C_OTHER_AND_LAST_FRAME)
  6388. {
  6389. hi2c->XferOptions = I2C_FIRST_AND_LAST_FRAME;
  6390. }
  6391. else
  6392. {
  6393. /* Nothing to do */
  6394. }
  6395. }
  6396. /**
  6397. * @}
  6398. */
  6399. #endif /* HAL_I2C_MODULE_ENABLED */
  6400. /**
  6401. * @}
  6402. */
  6403. /**
  6404. * @}
  6405. */