Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 

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