You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

7321 lines
230 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32l1xx_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 @ref 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 channel
  30. (+++) Enable the DMAx interface clock using
  31. (+++) Configure the DMA handle parameters
  32. (+++) Configure the DMA Tx or Rx channel
  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 channel
  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 @ref HAL_I2C_Init(), configures also the low level Hardware
  39. (GPIO, CLOCK, NVIC...etc) by calling the customized @ref HAL_I2C_MspInit() API.
  40. (#) To check if target device is ready for communication, use the function @ref 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 @ref HAL_I2C_Master_Transmit()
  46. (+) Receive in master mode an amount of data in blocking mode using @ref HAL_I2C_Master_Receive()
  47. (+) Transmit in slave mode an amount of data in blocking mode using @ref HAL_I2C_Slave_Transmit()
  48. (+) Receive in slave mode an amount of data in blocking mode using @ref 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 @ref HAL_I2C_Mem_Write()
  53. (+) Read an amount of data in blocking mode from a specific memory address using @ref 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 @ref HAL_I2C_Master_Transmit_IT()
  58. (+) At transmission end of transfer, @ref HAL_I2C_MasterTxCpltCallback() is executed and user can
  59. add his own code by customization of function pointer @ref HAL_I2C_MasterTxCpltCallback()
  60. (+) Receive in master mode an amount of data in non-blocking mode using @ref HAL_I2C_Master_Receive_IT()
  61. (+) At reception end of transfer, @ref HAL_I2C_MasterRxCpltCallback() is executed and user can
  62. add his own code by customization of function pointer @ref HAL_I2C_MasterRxCpltCallback()
  63. (+) Transmit in slave mode an amount of data in non-blocking mode using @ref HAL_I2C_Slave_Transmit_IT()
  64. (+) At transmission end of transfer, @ref HAL_I2C_SlaveTxCpltCallback() is executed and user can
  65. add his own code by customization of function pointer @ref HAL_I2C_SlaveTxCpltCallback()
  66. (+) Receive in slave mode an amount of data in non-blocking mode using @ref HAL_I2C_Slave_Receive_IT()
  67. (+) At reception end of transfer, @ref HAL_I2C_SlaveRxCpltCallback() is executed and user can
  68. add his own code by customization of function pointer @ref HAL_I2C_SlaveRxCpltCallback()
  69. (+) In case of transfer Error, @ref HAL_I2C_ErrorCallback() function is executed and user can
  70. add his own code by customization of function pointer @ref HAL_I2C_ErrorCallback()
  71. (+) Abort a master I2C process communication with Interrupt using @ref HAL_I2C_Master_Abort_IT()
  72. (+) End of abort process, @ref HAL_I2C_AbortCpltCallback() is executed and user can
  73. add his own code by customization of function pointer @ref 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 @ref I2C_XferOptions_definition and are listed below:
  82. (++) I2C_FIRST_AND_LAST_FRAME: No sequential usage, functionnal 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 @ref HAL_I2C_Master_Seq_Transmit_IT() then @ref HAL_I2C_Master_Seq_Transmit_IT()
  88. or @ref HAL_I2C_Master_Seq_Transmit_DMA() then @ref 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 oposite 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. (+) Differents sequential I2C interfaces are listed below:
  111. (++) Sequential transmit in master I2C mode an amount of data in non-blocking mode using @ref HAL_I2C_Master_Seq_Transmit_IT()
  112. or using @ref HAL_I2C_Master_Seq_Transmit_DMA()
  113. (+++) At transmission end of current frame transfer, @ref HAL_I2C_MasterTxCpltCallback() is executed and user can
  114. add his own code by customization of function pointer @ref HAL_I2C_MasterTxCpltCallback()
  115. (++) Sequential receive in master I2C mode an amount of data in non-blocking mode using @ref HAL_I2C_Master_Seq_Receive_IT()
  116. or using @ref HAL_I2C_Master_Seq_Receive_DMA()
  117. (+++) At reception end of current frame transfer, @ref HAL_I2C_MasterRxCpltCallback() is executed and user can
  118. add his own code by customization of function pointer @ref HAL_I2C_MasterRxCpltCallback()
  119. (++) Abort a master IT or DMA I2C process communication with Interrupt using @ref HAL_I2C_Master_Abort_IT()
  120. (+++) End of abort process, @ref HAL_I2C_AbortCpltCallback() is executed and user can
  121. add his own code by customization of function pointer @ref HAL_I2C_AbortCpltCallback()
  122. (++) Enable/disable the Address listen mode in slave I2C mode using @ref HAL_I2C_EnableListen_IT() @ref HAL_I2C_DisableListen_IT()
  123. (+++) When address slave I2C match, @ref 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 @ref HAL_I2C_ListenCpltCallback() is executed and user can
  126. add his own code by customization of function pointer @ref HAL_I2C_ListenCpltCallback()
  127. (++) Sequential transmit in slave I2C mode an amount of data in non-blocking mode using @ref HAL_I2C_Slave_Seq_Transmit_IT()
  128. or using @ref HAL_I2C_Slave_Seq_Transmit_DMA()
  129. (+++) At transmission end of current frame transfer, @ref HAL_I2C_SlaveTxCpltCallback() is executed and user can
  130. add his own code by customization of function pointer @ref HAL_I2C_SlaveTxCpltCallback()
  131. (++) Sequential receive in slave I2C mode an amount of data in non-blocking mode using @ref HAL_I2C_Slave_Seq_Receive_IT()
  132. or using @ref HAL_I2C_Slave_Seq_Receive_DMA()
  133. (+++) At reception end of current frame transfer, @ref HAL_I2C_SlaveRxCpltCallback() is executed and user can
  134. add his own code by customization of function pointer @ref HAL_I2C_SlaveRxCpltCallback()
  135. (++) In case of transfer Error, @ref HAL_I2C_ErrorCallback() function is executed and user can
  136. add his own code by customization of function pointer @ref 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. @ref HAL_I2C_Mem_Write_IT()
  142. (+) At Memory end of write transfer, @ref HAL_I2C_MemTxCpltCallback() is executed and user can
  143. add his own code by customization of function pointer @ref HAL_I2C_MemTxCpltCallback()
  144. (+) Read an amount of data in non-blocking mode with Interrupt from a specific memory address using
  145. @ref HAL_I2C_Mem_Read_IT()
  146. (+) At Memory end of read transfer, @ref HAL_I2C_MemRxCpltCallback() is executed and user can
  147. add his own code by customization of function pointer @ref HAL_I2C_MemRxCpltCallback()
  148. (+) In case of transfer Error, @ref HAL_I2C_ErrorCallback() function is executed and user can
  149. add his own code by customization of function pointer @ref 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. @ref HAL_I2C_Master_Transmit_DMA()
  155. (+) At transmission end of transfer, @ref HAL_I2C_MasterTxCpltCallback() is executed and user can
  156. add his own code by customization of function pointer @ref HAL_I2C_MasterTxCpltCallback()
  157. (+) Receive in master mode an amount of data in non-blocking mode (DMA) using
  158. @ref HAL_I2C_Master_Receive_DMA()
  159. (+) At reception end of transfer, @ref HAL_I2C_MasterRxCpltCallback() is executed and user can
  160. add his own code by customization of function pointer @ref HAL_I2C_MasterRxCpltCallback()
  161. (+) Transmit in slave mode an amount of data in non-blocking mode (DMA) using
  162. @ref HAL_I2C_Slave_Transmit_DMA()
  163. (+) At transmission end of transfer, @ref HAL_I2C_SlaveTxCpltCallback() is executed and user can
  164. add his own code by customization of function pointer @ref HAL_I2C_SlaveTxCpltCallback()
  165. (+) Receive in slave mode an amount of data in non-blocking mode (DMA) using
  166. @ref HAL_I2C_Slave_Receive_DMA()
  167. (+) At reception end of transfer, @ref HAL_I2C_SlaveRxCpltCallback() is executed and user can
  168. add his own code by customization of function pointer @ref HAL_I2C_SlaveRxCpltCallback()
  169. (+) In case of transfer Error, @ref HAL_I2C_ErrorCallback() function is executed and user can
  170. add his own code by customization of function pointer @ref HAL_I2C_ErrorCallback()
  171. (+) Abort a master I2C process communication with Interrupt using @ref HAL_I2C_Master_Abort_IT()
  172. (+) End of abort process, @ref HAL_I2C_AbortCpltCallback() is executed and user can
  173. add his own code by customization of function pointer @ref 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. @ref HAL_I2C_Mem_Write_DMA()
  179. (+) At Memory end of write transfer, @ref HAL_I2C_MemTxCpltCallback() is executed and user can
  180. add his own code by customization of function pointer @ref HAL_I2C_MemTxCpltCallback()
  181. (+) Read an amount of data in non-blocking mode with DMA from a specific memory address using
  182. @ref HAL_I2C_Mem_Read_DMA()
  183. (+) At Memory end of read transfer, @ref HAL_I2C_MemRxCpltCallback() is executed and user can
  184. add his own code by customization of function pointer @ref HAL_I2C_MemRxCpltCallback()
  185. (+) In case of transfer Error, @ref HAL_I2C_ErrorCallback() function is executed and user can
  186. add his own code by customization of function pointer @ref HAL_I2C_ErrorCallback()
  187. *** I2C HAL driver macros list ***
  188. ==================================
  189. [..]
  190. Below the list of most used macros in I2C HAL driver.
  191. (+) @ref __HAL_I2C_ENABLE: Enable the I2C peripheral
  192. (+) @ref __HAL_I2C_DISABLE: Disable the I2C peripheral
  193. (+) @ref __HAL_I2C_GET_FLAG: Checks whether the specified I2C flag is set or not
  194. (+) @ref __HAL_I2C_CLEAR_FLAG: Clear the specified I2C pending flag
  195. (+) @ref __HAL_I2C_ENABLE_IT: Enable the specified I2C interrupt
  196. (+) @ref __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 @ref HAL_I2C_RegisterCallback() or @ref HAL_I2C_RegisterAddrCallback()
  203. to register an interrupt callback.
  204. [..]
  205. Function @ref 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 : @ref HAL_I2C_RegisterAddrCallback().
  221. [..]
  222. Use function @ref HAL_I2C_UnRegisterCallback to reset a callback to the default
  223. weak function.
  224. @ref 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 : @ref HAL_I2C_UnRegisterAddrCallback().
  240. [..]
  241. By default, after the @ref HAL_I2C_Init() and when the state is @ref HAL_I2C_STATE_RESET
  242. all callbacks are set to the corresponding weak functions:
  243. examples @ref HAL_I2C_MasterTxCpltCallback(), @ref HAL_I2C_MasterRxCpltCallback().
  244. Exception done for MspInit and MspDeInit functions that are
  245. reset to the legacy weak functions in the @ref HAL_I2C_Init()/ @ref HAL_I2C_DeInit() only when
  246. these callbacks are null (not registered beforehand).
  247. If MspInit or MspDeInit are not null, the @ref HAL_I2C_Init()/ @ref 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 @ref HAL_I2C_STATE_READY state only.
  251. Exception done MspInit/MspDeInit functions that can be registered/unregistered
  252. in @ref HAL_I2C_STATE_READY or @ref 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 @ref HAL_I2C_RegisterCallback() before calling @ref HAL_I2C_DeInit()
  256. or @ref 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 "stm32l1xx_hal.h"
  279. /** @addtogroup STM32L1xx_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. /* Generate Start */
  1428. SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
  1429. /* Process Unlocked */
  1430. __HAL_UNLOCK(hi2c);
  1431. /* Note : The I2C interrupts must be enabled after unlocking current process
  1432. to avoid the risk of I2C interrupt handle execution before current
  1433. process unlock */
  1434. /* Enable EVT, BUF and ERR interrupt */
  1435. __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  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. /* Enable Acknowledge */
  1495. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  1496. /* Generate Start */
  1497. SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
  1498. /* Process Unlocked */
  1499. __HAL_UNLOCK(hi2c);
  1500. /* Note : The I2C interrupts must be enabled after unlocking current process
  1501. to avoid the risk of I2C interrupt handle execution before current
  1502. process unlock */
  1503. /* Enable EVT, BUF and ERR interrupt */
  1504. __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  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. /* Set the I2C DMA transfer complete callback */
  1667. hi2c->hdmatx->XferCpltCallback = I2C_DMAXferCplt;
  1668. /* Set the DMA error callback */
  1669. hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
  1670. /* Set the unused DMA callbacks to NULL */
  1671. hi2c->hdmatx->XferHalfCpltCallback = NULL;
  1672. hi2c->hdmatx->XferAbortCallback = NULL;
  1673. /* Enable the DMA channel */
  1674. dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)hi2c->pBuffPtr, (uint32_t)&hi2c->Instance->DR, hi2c->XferSize);
  1675. if (dmaxferstatus == HAL_OK)
  1676. {
  1677. /* Enable Acknowledge */
  1678. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  1679. /* Generate Start */
  1680. SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
  1681. /* Process Unlocked */
  1682. __HAL_UNLOCK(hi2c);
  1683. /* Note : The I2C interrupts must be enabled after unlocking current process
  1684. to avoid the risk of I2C interrupt handle execution before current
  1685. process unlock */
  1686. /* Enable EVT and ERR interrupt */
  1687. __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR);
  1688. /* Enable DMA Request */
  1689. SET_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
  1690. }
  1691. else
  1692. {
  1693. /* Update I2C state */
  1694. hi2c->State = HAL_I2C_STATE_READY;
  1695. hi2c->Mode = HAL_I2C_MODE_NONE;
  1696. /* Update I2C error code */
  1697. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
  1698. /* Process Unlocked */
  1699. __HAL_UNLOCK(hi2c);
  1700. return HAL_ERROR;
  1701. }
  1702. }
  1703. else
  1704. {
  1705. /* Enable Acknowledge */
  1706. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  1707. /* Generate Start */
  1708. SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
  1709. /* Process Unlocked */
  1710. __HAL_UNLOCK(hi2c);
  1711. /* Note : The I2C interrupts must be enabled after unlocking current process
  1712. to avoid the risk of I2C interrupt handle execution before current
  1713. process unlock */
  1714. /* Enable EVT, BUF and ERR interrupt */
  1715. __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  1716. }
  1717. return HAL_OK;
  1718. }
  1719. else
  1720. {
  1721. return HAL_BUSY;
  1722. }
  1723. }
  1724. /**
  1725. * @brief Receive in master mode an amount of data in non-blocking mode with DMA
  1726. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  1727. * the configuration information for the specified I2C.
  1728. * @param DevAddress Target device address: The device 7 bits address value
  1729. * in datasheet must be shifted to the left before calling the interface
  1730. * @param pData Pointer to data buffer
  1731. * @param Size Amount of data to be sent
  1732. * @retval HAL status
  1733. */
  1734. HAL_StatusTypeDef HAL_I2C_Master_Receive_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
  1735. {
  1736. __IO uint32_t count = 0U;
  1737. HAL_StatusTypeDef dmaxferstatus;
  1738. if (hi2c->State == HAL_I2C_STATE_READY)
  1739. {
  1740. /* Wait until BUSY flag is reset */
  1741. count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U);
  1742. do
  1743. {
  1744. count--;
  1745. if (count == 0U)
  1746. {
  1747. hi2c->PreviousState = I2C_STATE_NONE;
  1748. hi2c->State = HAL_I2C_STATE_READY;
  1749. hi2c->Mode = HAL_I2C_MODE_NONE;
  1750. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  1751. /* Process Unlocked */
  1752. __HAL_UNLOCK(hi2c);
  1753. return HAL_ERROR;
  1754. }
  1755. }
  1756. while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
  1757. /* Process Locked */
  1758. __HAL_LOCK(hi2c);
  1759. /* Check if the I2C is already enabled */
  1760. if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  1761. {
  1762. /* Enable I2C peripheral */
  1763. __HAL_I2C_ENABLE(hi2c);
  1764. }
  1765. /* Disable Pos */
  1766. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  1767. hi2c->State = HAL_I2C_STATE_BUSY_RX;
  1768. hi2c->Mode = HAL_I2C_MODE_MASTER;
  1769. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1770. /* Prepare transfer parameters */
  1771. hi2c->pBuffPtr = pData;
  1772. hi2c->XferCount = Size;
  1773. hi2c->XferSize = hi2c->XferCount;
  1774. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  1775. hi2c->Devaddress = DevAddress;
  1776. if (hi2c->XferSize > 0U)
  1777. {
  1778. /* Set the I2C DMA transfer complete callback */
  1779. hi2c->hdmarx->XferCpltCallback = I2C_DMAXferCplt;
  1780. /* Set the DMA error callback */
  1781. hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
  1782. /* Set the unused DMA callbacks to NULL */
  1783. hi2c->hdmarx->XferHalfCpltCallback = NULL;
  1784. hi2c->hdmarx->XferAbortCallback = NULL;
  1785. /* Enable the DMA channel */
  1786. dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->DR, (uint32_t)hi2c->pBuffPtr, hi2c->XferSize);
  1787. if (dmaxferstatus == HAL_OK)
  1788. {
  1789. /* Enable Acknowledge */
  1790. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  1791. /* Generate Start */
  1792. SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
  1793. /* Process Unlocked */
  1794. __HAL_UNLOCK(hi2c);
  1795. /* Note : The I2C interrupts must be enabled after unlocking current process
  1796. to avoid the risk of I2C interrupt handle execution before current
  1797. process unlock */
  1798. /* Enable EVT and ERR interrupt */
  1799. __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR);
  1800. /* Enable DMA Request */
  1801. SET_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
  1802. }
  1803. else
  1804. {
  1805. /* Update I2C state */
  1806. hi2c->State = HAL_I2C_STATE_READY;
  1807. hi2c->Mode = HAL_I2C_MODE_NONE;
  1808. /* Update I2C error code */
  1809. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
  1810. /* Process Unlocked */
  1811. __HAL_UNLOCK(hi2c);
  1812. return HAL_ERROR;
  1813. }
  1814. }
  1815. else
  1816. {
  1817. /* Enable Acknowledge */
  1818. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  1819. /* Generate Start */
  1820. SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
  1821. /* Process Unlocked */
  1822. __HAL_UNLOCK(hi2c);
  1823. /* Note : The I2C interrupts must be enabled after unlocking current process
  1824. to avoid the risk of I2C interrupt handle execution before current
  1825. process unlock */
  1826. /* Enable EVT, BUF and ERR interrupt */
  1827. __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  1828. }
  1829. return HAL_OK;
  1830. }
  1831. else
  1832. {
  1833. return HAL_BUSY;
  1834. }
  1835. }
  1836. /**
  1837. * @brief Transmit in slave mode an amount of data in non-blocking mode with DMA
  1838. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  1839. * the configuration information for the specified I2C.
  1840. * @param pData Pointer to data buffer
  1841. * @param Size Amount of data to be sent
  1842. * @retval HAL status
  1843. */
  1844. HAL_StatusTypeDef HAL_I2C_Slave_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
  1845. {
  1846. HAL_StatusTypeDef dmaxferstatus;
  1847. if (hi2c->State == HAL_I2C_STATE_READY)
  1848. {
  1849. if ((pData == NULL) || (Size == 0U))
  1850. {
  1851. return HAL_ERROR;
  1852. }
  1853. /* Process Locked */
  1854. __HAL_LOCK(hi2c);
  1855. /* Check if the I2C is already enabled */
  1856. if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  1857. {
  1858. /* Enable I2C peripheral */
  1859. __HAL_I2C_ENABLE(hi2c);
  1860. }
  1861. /* Disable Pos */
  1862. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  1863. hi2c->State = HAL_I2C_STATE_BUSY_TX;
  1864. hi2c->Mode = HAL_I2C_MODE_SLAVE;
  1865. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1866. /* Prepare transfer parameters */
  1867. hi2c->pBuffPtr = pData;
  1868. hi2c->XferCount = Size;
  1869. hi2c->XferSize = hi2c->XferCount;
  1870. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  1871. /* Set the I2C DMA transfer complete callback */
  1872. hi2c->hdmatx->XferCpltCallback = I2C_DMAXferCplt;
  1873. /* Set the DMA error callback */
  1874. hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
  1875. /* Set the unused DMA callbacks to NULL */
  1876. hi2c->hdmatx->XferHalfCpltCallback = NULL;
  1877. hi2c->hdmatx->XferAbortCallback = NULL;
  1878. /* Enable the DMA channel */
  1879. dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)hi2c->pBuffPtr, (uint32_t)&hi2c->Instance->DR, hi2c->XferSize);
  1880. if (dmaxferstatus == HAL_OK)
  1881. {
  1882. /* Enable Address Acknowledge */
  1883. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  1884. /* Process Unlocked */
  1885. __HAL_UNLOCK(hi2c);
  1886. /* Note : The I2C interrupts must be enabled after unlocking current process
  1887. to avoid the risk of I2C interrupt handle execution before current
  1888. process unlock */
  1889. /* Enable EVT and ERR interrupt */
  1890. __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR);
  1891. /* Enable DMA Request */
  1892. hi2c->Instance->CR2 |= I2C_CR2_DMAEN;
  1893. return HAL_OK;
  1894. }
  1895. else
  1896. {
  1897. /* Update I2C state */
  1898. hi2c->State = HAL_I2C_STATE_READY;
  1899. hi2c->Mode = HAL_I2C_MODE_NONE;
  1900. /* Update I2C error code */
  1901. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
  1902. /* Process Unlocked */
  1903. __HAL_UNLOCK(hi2c);
  1904. return HAL_ERROR;
  1905. }
  1906. }
  1907. else
  1908. {
  1909. return HAL_BUSY;
  1910. }
  1911. }
  1912. /**
  1913. * @brief Receive in slave mode an amount of data in non-blocking mode with DMA
  1914. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  1915. * the configuration information for the specified I2C.
  1916. * @param pData Pointer to data buffer
  1917. * @param Size Amount of data to be sent
  1918. * @retval HAL status
  1919. */
  1920. HAL_StatusTypeDef HAL_I2C_Slave_Receive_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
  1921. {
  1922. HAL_StatusTypeDef dmaxferstatus;
  1923. if (hi2c->State == HAL_I2C_STATE_READY)
  1924. {
  1925. if ((pData == NULL) || (Size == 0U))
  1926. {
  1927. return HAL_ERROR;
  1928. }
  1929. /* Process Locked */
  1930. __HAL_LOCK(hi2c);
  1931. /* Check if the I2C is already enabled */
  1932. if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  1933. {
  1934. /* Enable I2C peripheral */
  1935. __HAL_I2C_ENABLE(hi2c);
  1936. }
  1937. /* Disable Pos */
  1938. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  1939. hi2c->State = HAL_I2C_STATE_BUSY_RX;
  1940. hi2c->Mode = HAL_I2C_MODE_SLAVE;
  1941. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  1942. /* Prepare transfer parameters */
  1943. hi2c->pBuffPtr = pData;
  1944. hi2c->XferCount = Size;
  1945. hi2c->XferSize = hi2c->XferCount;
  1946. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  1947. /* Set the I2C DMA transfer complete callback */
  1948. hi2c->hdmarx->XferCpltCallback = I2C_DMAXferCplt;
  1949. /* Set the DMA error callback */
  1950. hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
  1951. /* Set the unused DMA callbacks to NULL */
  1952. hi2c->hdmarx->XferHalfCpltCallback = NULL;
  1953. hi2c->hdmarx->XferAbortCallback = NULL;
  1954. /* Enable the DMA channel */
  1955. dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->DR, (uint32_t)hi2c->pBuffPtr, hi2c->XferSize);
  1956. if (dmaxferstatus == HAL_OK)
  1957. {
  1958. /* Enable Address Acknowledge */
  1959. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  1960. /* Process Unlocked */
  1961. __HAL_UNLOCK(hi2c);
  1962. /* Note : The I2C interrupts must be enabled after unlocking current process
  1963. to avoid the risk of I2C interrupt handle execution before current
  1964. process unlock */
  1965. /* Enable EVT and ERR interrupt */
  1966. __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR);
  1967. /* Enable DMA Request */
  1968. SET_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
  1969. return HAL_OK;
  1970. }
  1971. else
  1972. {
  1973. /* Update I2C state */
  1974. hi2c->State = HAL_I2C_STATE_READY;
  1975. hi2c->Mode = HAL_I2C_MODE_NONE;
  1976. /* Update I2C error code */
  1977. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
  1978. /* Process Unlocked */
  1979. __HAL_UNLOCK(hi2c);
  1980. return HAL_ERROR;
  1981. }
  1982. }
  1983. else
  1984. {
  1985. return HAL_BUSY;
  1986. }
  1987. }
  1988. /**
  1989. * @brief Write an amount of data in blocking mode to a specific memory address
  1990. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  1991. * the configuration information for the specified I2C.
  1992. * @param DevAddress Target device address: The device 7 bits address value
  1993. * in datasheet must be shifted to the left before calling the interface
  1994. * @param MemAddress Internal memory address
  1995. * @param MemAddSize Size of internal memory address
  1996. * @param pData Pointer to data buffer
  1997. * @param Size Amount of data to be sent
  1998. * @param Timeout Timeout duration
  1999. * @retval HAL status
  2000. */
  2001. 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)
  2002. {
  2003. /* Init tickstart for timeout management*/
  2004. uint32_t tickstart = HAL_GetTick();
  2005. /* Check the parameters */
  2006. assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
  2007. if (hi2c->State == HAL_I2C_STATE_READY)
  2008. {
  2009. /* Wait until BUSY flag is reset */
  2010. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY_FLAG, tickstart) != HAL_OK)
  2011. {
  2012. return HAL_BUSY;
  2013. }
  2014. /* Process Locked */
  2015. __HAL_LOCK(hi2c);
  2016. /* Check if the I2C is already enabled */
  2017. if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  2018. {
  2019. /* Enable I2C peripheral */
  2020. __HAL_I2C_ENABLE(hi2c);
  2021. }
  2022. /* Disable Pos */
  2023. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  2024. hi2c->State = HAL_I2C_STATE_BUSY_TX;
  2025. hi2c->Mode = HAL_I2C_MODE_MEM;
  2026. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  2027. /* Prepare transfer parameters */
  2028. hi2c->pBuffPtr = pData;
  2029. hi2c->XferCount = Size;
  2030. hi2c->XferSize = hi2c->XferCount;
  2031. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  2032. /* Send Slave Address and Memory Address */
  2033. if (I2C_RequestMemoryWrite(hi2c, DevAddress, MemAddress, MemAddSize, Timeout, tickstart) != HAL_OK)
  2034. {
  2035. return HAL_ERROR;
  2036. }
  2037. while (hi2c->XferSize > 0U)
  2038. {
  2039. /* Wait until TXE flag is set */
  2040. if (I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
  2041. {
  2042. if (hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  2043. {
  2044. /* Generate Stop */
  2045. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  2046. }
  2047. return HAL_ERROR;
  2048. }
  2049. /* Write data to DR */
  2050. hi2c->Instance->DR = *hi2c->pBuffPtr;
  2051. /* Increment Buffer pointer */
  2052. hi2c->pBuffPtr++;
  2053. /* Update counter */
  2054. hi2c->XferSize--;
  2055. hi2c->XferCount--;
  2056. if ((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET) && (hi2c->XferSize != 0U))
  2057. {
  2058. /* Write data to DR */
  2059. hi2c->Instance->DR = *hi2c->pBuffPtr;
  2060. /* Increment Buffer pointer */
  2061. hi2c->pBuffPtr++;
  2062. /* Update counter */
  2063. hi2c->XferSize--;
  2064. hi2c->XferCount--;
  2065. }
  2066. }
  2067. /* Wait until BTF flag is set */
  2068. if (I2C_WaitOnBTFFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
  2069. {
  2070. if (hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  2071. {
  2072. /* Generate Stop */
  2073. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  2074. }
  2075. return HAL_ERROR;
  2076. }
  2077. /* Generate Stop */
  2078. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  2079. hi2c->State = HAL_I2C_STATE_READY;
  2080. hi2c->Mode = HAL_I2C_MODE_NONE;
  2081. /* Process Unlocked */
  2082. __HAL_UNLOCK(hi2c);
  2083. return HAL_OK;
  2084. }
  2085. else
  2086. {
  2087. return HAL_BUSY;
  2088. }
  2089. }
  2090. /**
  2091. * @brief Read an amount of data in blocking mode from a specific memory address
  2092. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2093. * the configuration information for the specified I2C.
  2094. * @param DevAddress Target device address: The device 7 bits address value
  2095. * in datasheet must be shifted to the left before calling the interface
  2096. * @param MemAddress Internal memory address
  2097. * @param MemAddSize Size of internal memory address
  2098. * @param pData Pointer to data buffer
  2099. * @param Size Amount of data to be sent
  2100. * @param Timeout Timeout duration
  2101. * @retval HAL status
  2102. */
  2103. 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)
  2104. {
  2105. /* Init tickstart for timeout management*/
  2106. uint32_t tickstart = HAL_GetTick();
  2107. /* Check the parameters */
  2108. assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
  2109. if (hi2c->State == HAL_I2C_STATE_READY)
  2110. {
  2111. /* Wait until BUSY flag is reset */
  2112. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY_FLAG, tickstart) != HAL_OK)
  2113. {
  2114. return HAL_BUSY;
  2115. }
  2116. /* Process Locked */
  2117. __HAL_LOCK(hi2c);
  2118. /* Check if the I2C is already enabled */
  2119. if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  2120. {
  2121. /* Enable I2C peripheral */
  2122. __HAL_I2C_ENABLE(hi2c);
  2123. }
  2124. /* Disable Pos */
  2125. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  2126. hi2c->State = HAL_I2C_STATE_BUSY_RX;
  2127. hi2c->Mode = HAL_I2C_MODE_MEM;
  2128. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  2129. /* Prepare transfer parameters */
  2130. hi2c->pBuffPtr = pData;
  2131. hi2c->XferCount = Size;
  2132. hi2c->XferSize = hi2c->XferCount;
  2133. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  2134. /* Send Slave Address and Memory Address */
  2135. if (I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, Timeout, tickstart) != HAL_OK)
  2136. {
  2137. return HAL_ERROR;
  2138. }
  2139. if (hi2c->XferSize == 0U)
  2140. {
  2141. /* Clear ADDR flag */
  2142. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  2143. /* Generate Stop */
  2144. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  2145. }
  2146. else if (hi2c->XferSize == 1U)
  2147. {
  2148. /* Disable Acknowledge */
  2149. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  2150. /* Clear ADDR flag */
  2151. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  2152. /* Generate Stop */
  2153. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  2154. }
  2155. else if (hi2c->XferSize == 2U)
  2156. {
  2157. /* Disable Acknowledge */
  2158. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  2159. /* Enable Pos */
  2160. SET_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  2161. /* Clear ADDR flag */
  2162. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  2163. }
  2164. else
  2165. {
  2166. /* Clear ADDR flag */
  2167. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  2168. }
  2169. while (hi2c->XferSize > 0U)
  2170. {
  2171. if (hi2c->XferSize <= 3U)
  2172. {
  2173. /* One byte */
  2174. if (hi2c->XferSize == 1U)
  2175. {
  2176. /* Wait until RXNE flag is set */
  2177. if (I2C_WaitOnRXNEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
  2178. {
  2179. return HAL_ERROR;
  2180. }
  2181. /* Read data from DR */
  2182. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
  2183. /* Increment Buffer pointer */
  2184. hi2c->pBuffPtr++;
  2185. /* Update counter */
  2186. hi2c->XferSize--;
  2187. hi2c->XferCount--;
  2188. }
  2189. /* Two bytes */
  2190. else if (hi2c->XferSize == 2U)
  2191. {
  2192. /* Wait until BTF flag is set */
  2193. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BTF, RESET, Timeout, tickstart) != HAL_OK)
  2194. {
  2195. return HAL_ERROR;
  2196. }
  2197. /* Generate Stop */
  2198. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  2199. /* Read data from DR */
  2200. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
  2201. /* Increment Buffer pointer */
  2202. hi2c->pBuffPtr++;
  2203. /* Update counter */
  2204. hi2c->XferSize--;
  2205. hi2c->XferCount--;
  2206. /* Read data from DR */
  2207. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
  2208. /* Increment Buffer pointer */
  2209. hi2c->pBuffPtr++;
  2210. /* Update counter */
  2211. hi2c->XferSize--;
  2212. hi2c->XferCount--;
  2213. }
  2214. /* 3 Last bytes */
  2215. else
  2216. {
  2217. /* Wait until BTF flag is set */
  2218. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BTF, RESET, Timeout, tickstart) != HAL_OK)
  2219. {
  2220. return HAL_ERROR;
  2221. }
  2222. /* Disable Acknowledge */
  2223. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  2224. /* Read data from DR */
  2225. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
  2226. /* Increment Buffer pointer */
  2227. hi2c->pBuffPtr++;
  2228. /* Update counter */
  2229. hi2c->XferSize--;
  2230. hi2c->XferCount--;
  2231. /* Wait until BTF flag is set */
  2232. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BTF, RESET, Timeout, tickstart) != HAL_OK)
  2233. {
  2234. return HAL_ERROR;
  2235. }
  2236. /* Generate Stop */
  2237. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  2238. /* Read data from DR */
  2239. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
  2240. /* Increment Buffer pointer */
  2241. hi2c->pBuffPtr++;
  2242. /* Update counter */
  2243. hi2c->XferSize--;
  2244. hi2c->XferCount--;
  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. }
  2254. else
  2255. {
  2256. /* Wait until RXNE flag is set */
  2257. if (I2C_WaitOnRXNEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
  2258. {
  2259. return HAL_ERROR;
  2260. }
  2261. /* Read data from DR */
  2262. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
  2263. /* Increment Buffer pointer */
  2264. hi2c->pBuffPtr++;
  2265. /* Update counter */
  2266. hi2c->XferSize--;
  2267. hi2c->XferCount--;
  2268. if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET)
  2269. {
  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. }
  2279. }
  2280. hi2c->State = HAL_I2C_STATE_READY;
  2281. hi2c->Mode = HAL_I2C_MODE_NONE;
  2282. /* Process Unlocked */
  2283. __HAL_UNLOCK(hi2c);
  2284. return HAL_OK;
  2285. }
  2286. else
  2287. {
  2288. return HAL_BUSY;
  2289. }
  2290. }
  2291. /**
  2292. * @brief Write an amount of data in non-blocking mode with Interrupt to a specific memory address
  2293. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2294. * the configuration information for the specified I2C.
  2295. * @param DevAddress Target device address: The device 7 bits address value
  2296. * in datasheet must be shifted to the left before calling the interface
  2297. * @param MemAddress Internal memory address
  2298. * @param MemAddSize Size of internal memory address
  2299. * @param pData Pointer to data buffer
  2300. * @param Size Amount of data to be sent
  2301. * @retval HAL status
  2302. */
  2303. 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)
  2304. {
  2305. __IO uint32_t count = 0U;
  2306. /* Check the parameters */
  2307. assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
  2308. if (hi2c->State == HAL_I2C_STATE_READY)
  2309. {
  2310. /* Wait until BUSY flag is reset */
  2311. count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U);
  2312. do
  2313. {
  2314. count--;
  2315. if (count == 0U)
  2316. {
  2317. hi2c->PreviousState = I2C_STATE_NONE;
  2318. hi2c->State = HAL_I2C_STATE_READY;
  2319. hi2c->Mode = HAL_I2C_MODE_NONE;
  2320. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  2321. /* Process Unlocked */
  2322. __HAL_UNLOCK(hi2c);
  2323. return HAL_ERROR;
  2324. }
  2325. }
  2326. while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
  2327. /* Process Locked */
  2328. __HAL_LOCK(hi2c);
  2329. /* Check if the I2C is already enabled */
  2330. if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  2331. {
  2332. /* Enable I2C peripheral */
  2333. __HAL_I2C_ENABLE(hi2c);
  2334. }
  2335. /* Disable Pos */
  2336. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  2337. hi2c->State = HAL_I2C_STATE_BUSY_TX;
  2338. hi2c->Mode = HAL_I2C_MODE_MEM;
  2339. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  2340. /* Prepare transfer parameters */
  2341. hi2c->pBuffPtr = pData;
  2342. hi2c->XferCount = Size;
  2343. hi2c->XferSize = hi2c->XferCount;
  2344. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  2345. hi2c->Devaddress = DevAddress;
  2346. hi2c->Memaddress = MemAddress;
  2347. hi2c->MemaddSize = MemAddSize;
  2348. hi2c->EventCount = 0U;
  2349. /* Generate Start */
  2350. SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
  2351. /* Process Unlocked */
  2352. __HAL_UNLOCK(hi2c);
  2353. /* Note : The I2C interrupts must be enabled after unlocking current process
  2354. to avoid the risk of I2C interrupt handle execution before current
  2355. process unlock */
  2356. /* Enable EVT, BUF and ERR interrupt */
  2357. __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  2358. return HAL_OK;
  2359. }
  2360. else
  2361. {
  2362. return HAL_BUSY;
  2363. }
  2364. }
  2365. /**
  2366. * @brief Read an amount of data in non-blocking mode with Interrupt from a specific memory address
  2367. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2368. * the configuration information for the specified I2C.
  2369. * @param DevAddress Target device address
  2370. * @param MemAddress Internal memory address
  2371. * @param MemAddSize Size of internal memory address
  2372. * @param pData Pointer to data buffer
  2373. * @param Size Amount of data to be sent
  2374. * @retval HAL status
  2375. */
  2376. 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)
  2377. {
  2378. __IO uint32_t count = 0U;
  2379. /* Check the parameters */
  2380. assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
  2381. if (hi2c->State == HAL_I2C_STATE_READY)
  2382. {
  2383. /* Wait until BUSY flag is reset */
  2384. count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U);
  2385. do
  2386. {
  2387. count--;
  2388. if (count == 0U)
  2389. {
  2390. hi2c->PreviousState = I2C_STATE_NONE;
  2391. hi2c->State = HAL_I2C_STATE_READY;
  2392. hi2c->Mode = HAL_I2C_MODE_NONE;
  2393. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  2394. /* Process Unlocked */
  2395. __HAL_UNLOCK(hi2c);
  2396. return HAL_ERROR;
  2397. }
  2398. }
  2399. while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
  2400. /* Process Locked */
  2401. __HAL_LOCK(hi2c);
  2402. /* Check if the I2C is already enabled */
  2403. if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  2404. {
  2405. /* Enable I2C peripheral */
  2406. __HAL_I2C_ENABLE(hi2c);
  2407. }
  2408. /* Disable Pos */
  2409. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  2410. hi2c->State = HAL_I2C_STATE_BUSY_RX;
  2411. hi2c->Mode = HAL_I2C_MODE_MEM;
  2412. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  2413. /* Prepare transfer parameters */
  2414. hi2c->pBuffPtr = pData;
  2415. hi2c->XferCount = Size;
  2416. hi2c->XferSize = hi2c->XferCount;
  2417. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  2418. hi2c->Devaddress = DevAddress;
  2419. hi2c->Memaddress = MemAddress;
  2420. hi2c->MemaddSize = MemAddSize;
  2421. hi2c->EventCount = 0U;
  2422. /* Enable Acknowledge */
  2423. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  2424. /* Generate Start */
  2425. SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
  2426. /* Process Unlocked */
  2427. __HAL_UNLOCK(hi2c);
  2428. if (hi2c->XferSize > 0U)
  2429. {
  2430. /* Note : The I2C interrupts must be enabled after unlocking current process
  2431. to avoid the risk of I2C interrupt handle execution before current
  2432. process unlock */
  2433. /* Enable EVT, BUF and ERR interrupt */
  2434. __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  2435. }
  2436. return HAL_OK;
  2437. }
  2438. else
  2439. {
  2440. return HAL_BUSY;
  2441. }
  2442. }
  2443. /**
  2444. * @brief Write an amount of data in non-blocking mode with DMA to a specific memory address
  2445. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2446. * the configuration information for the specified I2C.
  2447. * @param DevAddress Target device address: The device 7 bits address value
  2448. * in datasheet must be shifted to the left before calling the interface
  2449. * @param MemAddress Internal memory address
  2450. * @param MemAddSize Size of internal memory address
  2451. * @param pData Pointer to data buffer
  2452. * @param Size Amount of data to be sent
  2453. * @retval HAL status
  2454. */
  2455. 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)
  2456. {
  2457. __IO uint32_t count = 0U;
  2458. HAL_StatusTypeDef dmaxferstatus;
  2459. /* Init tickstart for timeout management*/
  2460. uint32_t tickstart = HAL_GetTick();
  2461. /* Check the parameters */
  2462. assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
  2463. if (hi2c->State == HAL_I2C_STATE_READY)
  2464. {
  2465. /* Wait until BUSY flag is reset */
  2466. count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U);
  2467. do
  2468. {
  2469. count--;
  2470. if (count == 0U)
  2471. {
  2472. hi2c->PreviousState = I2C_STATE_NONE;
  2473. hi2c->State = HAL_I2C_STATE_READY;
  2474. hi2c->Mode = HAL_I2C_MODE_NONE;
  2475. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  2476. /* Process Unlocked */
  2477. __HAL_UNLOCK(hi2c);
  2478. return HAL_ERROR;
  2479. }
  2480. }
  2481. while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
  2482. /* Process Locked */
  2483. __HAL_LOCK(hi2c);
  2484. /* Check if the I2C is already enabled */
  2485. if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  2486. {
  2487. /* Enable I2C peripheral */
  2488. __HAL_I2C_ENABLE(hi2c);
  2489. }
  2490. /* Disable Pos */
  2491. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  2492. hi2c->State = HAL_I2C_STATE_BUSY_TX;
  2493. hi2c->Mode = HAL_I2C_MODE_MEM;
  2494. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  2495. /* Prepare transfer parameters */
  2496. hi2c->pBuffPtr = pData;
  2497. hi2c->XferCount = Size;
  2498. hi2c->XferSize = hi2c->XferCount;
  2499. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  2500. if (hi2c->XferSize > 0U)
  2501. {
  2502. /* Set the I2C DMA transfer complete callback */
  2503. hi2c->hdmatx->XferCpltCallback = I2C_DMAXferCplt;
  2504. /* Set the DMA error callback */
  2505. hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
  2506. /* Set the unused DMA callbacks to NULL */
  2507. hi2c->hdmatx->XferHalfCpltCallback = NULL;
  2508. hi2c->hdmatx->XferAbortCallback = NULL;
  2509. /* Enable the DMA channel */
  2510. dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)hi2c->pBuffPtr, (uint32_t)&hi2c->Instance->DR, hi2c->XferSize);
  2511. if (dmaxferstatus == HAL_OK)
  2512. {
  2513. /* Send Slave Address and Memory Address */
  2514. if (I2C_RequestMemoryWrite(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG, tickstart) != HAL_OK)
  2515. {
  2516. /* Abort the ongoing DMA */
  2517. dmaxferstatus = HAL_DMA_Abort_IT(hi2c->hdmatx);
  2518. /* Prevent unused argument(s) compilation and MISRA warning */
  2519. UNUSED(dmaxferstatus);
  2520. /* Clear directly Complete callback as no XferAbortCallback is used to finalize Abort treatment */
  2521. if (hi2c->hdmatx != NULL)
  2522. {
  2523. hi2c->hdmatx->XferCpltCallback = NULL;
  2524. }
  2525. /* Disable Acknowledge */
  2526. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  2527. hi2c->XferSize = 0U;
  2528. hi2c->XferCount = 0U;
  2529. /* Disable I2C peripheral to prevent dummy data in buffer */
  2530. __HAL_I2C_DISABLE(hi2c);
  2531. return HAL_ERROR;
  2532. }
  2533. /* Clear ADDR flag */
  2534. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  2535. /* Process Unlocked */
  2536. __HAL_UNLOCK(hi2c);
  2537. /* Note : The I2C interrupts must be enabled after unlocking current process
  2538. to avoid the risk of I2C interrupt handle execution before current
  2539. process unlock */
  2540. /* Enable ERR interrupt */
  2541. __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_ERR);
  2542. /* Enable DMA Request */
  2543. SET_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
  2544. return HAL_OK;
  2545. }
  2546. else
  2547. {
  2548. /* Update I2C state */
  2549. hi2c->State = HAL_I2C_STATE_READY;
  2550. hi2c->Mode = HAL_I2C_MODE_NONE;
  2551. /* Update I2C error code */
  2552. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
  2553. /* Process Unlocked */
  2554. __HAL_UNLOCK(hi2c);
  2555. return HAL_ERROR;
  2556. }
  2557. }
  2558. else
  2559. {
  2560. /* Update I2C state */
  2561. hi2c->State = HAL_I2C_STATE_READY;
  2562. hi2c->Mode = HAL_I2C_MODE_NONE;
  2563. /* Update I2C error code */
  2564. hi2c->ErrorCode |= HAL_I2C_ERROR_SIZE;
  2565. /* Process Unlocked */
  2566. __HAL_UNLOCK(hi2c);
  2567. return HAL_ERROR;
  2568. }
  2569. }
  2570. else
  2571. {
  2572. return HAL_BUSY;
  2573. }
  2574. }
  2575. /**
  2576. * @brief Reads an amount of data in non-blocking mode with DMA from a specific memory address.
  2577. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2578. * the configuration information for the specified I2C.
  2579. * @param DevAddress Target device address: The device 7 bits address value
  2580. * in datasheet must be shifted to the left before calling the interface
  2581. * @param MemAddress Internal memory address
  2582. * @param MemAddSize Size of internal memory address
  2583. * @param pData Pointer to data buffer
  2584. * @param Size Amount of data to be read
  2585. * @retval HAL status
  2586. */
  2587. 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)
  2588. {
  2589. /* Init tickstart for timeout management*/
  2590. uint32_t tickstart = HAL_GetTick();
  2591. __IO uint32_t count = 0U;
  2592. HAL_StatusTypeDef dmaxferstatus;
  2593. /* Check the parameters */
  2594. assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
  2595. if (hi2c->State == HAL_I2C_STATE_READY)
  2596. {
  2597. /* Wait until BUSY flag is reset */
  2598. count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U);
  2599. do
  2600. {
  2601. count--;
  2602. if (count == 0U)
  2603. {
  2604. hi2c->PreviousState = I2C_STATE_NONE;
  2605. hi2c->State = HAL_I2C_STATE_READY;
  2606. hi2c->Mode = HAL_I2C_MODE_NONE;
  2607. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  2608. /* Process Unlocked */
  2609. __HAL_UNLOCK(hi2c);
  2610. return HAL_ERROR;
  2611. }
  2612. }
  2613. while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
  2614. /* Process Locked */
  2615. __HAL_LOCK(hi2c);
  2616. /* Check if the I2C is already enabled */
  2617. if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  2618. {
  2619. /* Enable I2C peripheral */
  2620. __HAL_I2C_ENABLE(hi2c);
  2621. }
  2622. /* Disable Pos */
  2623. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  2624. hi2c->State = HAL_I2C_STATE_BUSY_RX;
  2625. hi2c->Mode = HAL_I2C_MODE_MEM;
  2626. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  2627. /* Prepare transfer parameters */
  2628. hi2c->pBuffPtr = pData;
  2629. hi2c->XferCount = Size;
  2630. hi2c->XferSize = hi2c->XferCount;
  2631. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  2632. if (hi2c->XferSize > 0U)
  2633. {
  2634. /* Set the I2C DMA transfer complete callback */
  2635. hi2c->hdmarx->XferCpltCallback = I2C_DMAXferCplt;
  2636. /* Set the DMA error callback */
  2637. hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
  2638. /* Set the unused DMA callbacks to NULL */
  2639. hi2c->hdmarx->XferHalfCpltCallback = NULL;
  2640. hi2c->hdmarx->XferAbortCallback = NULL;
  2641. /* Enable the DMA channel */
  2642. dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->DR, (uint32_t)hi2c->pBuffPtr, hi2c->XferSize);
  2643. if (dmaxferstatus == HAL_OK)
  2644. {
  2645. /* Send Slave Address and Memory Address */
  2646. if (I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG, tickstart) != HAL_OK)
  2647. {
  2648. /* Abort the ongoing DMA */
  2649. dmaxferstatus = HAL_DMA_Abort_IT(hi2c->hdmarx);
  2650. /* Prevent unused argument(s) compilation and MISRA warning */
  2651. UNUSED(dmaxferstatus);
  2652. /* Clear directly Complete callback as no XferAbortCallback is used to finalize Abort treatment */
  2653. if (hi2c->hdmarx != NULL)
  2654. {
  2655. hi2c->hdmarx->XferCpltCallback = NULL;
  2656. }
  2657. /* Disable Acknowledge */
  2658. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  2659. hi2c->XferSize = 0U;
  2660. hi2c->XferCount = 0U;
  2661. /* Disable I2C peripheral to prevent dummy data in buffer */
  2662. __HAL_I2C_DISABLE(hi2c);
  2663. return HAL_ERROR;
  2664. }
  2665. if (hi2c->XferSize == 1U)
  2666. {
  2667. /* Disable Acknowledge */
  2668. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  2669. }
  2670. else
  2671. {
  2672. /* Enable Last DMA bit */
  2673. SET_BIT(hi2c->Instance->CR2, I2C_CR2_LAST);
  2674. }
  2675. /* Clear ADDR flag */
  2676. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  2677. /* Process Unlocked */
  2678. __HAL_UNLOCK(hi2c);
  2679. /* Note : The I2C interrupts must be enabled after unlocking current process
  2680. to avoid the risk of I2C interrupt handle execution before current
  2681. process unlock */
  2682. /* Enable ERR interrupt */
  2683. __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_ERR);
  2684. /* Enable DMA Request */
  2685. hi2c->Instance->CR2 |= I2C_CR2_DMAEN;
  2686. }
  2687. else
  2688. {
  2689. /* Update I2C state */
  2690. hi2c->State = HAL_I2C_STATE_READY;
  2691. hi2c->Mode = HAL_I2C_MODE_NONE;
  2692. /* Update I2C error code */
  2693. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
  2694. /* Process Unlocked */
  2695. __HAL_UNLOCK(hi2c);
  2696. return HAL_ERROR;
  2697. }
  2698. }
  2699. else
  2700. {
  2701. /* Send Slave Address and Memory Address */
  2702. if (I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG, tickstart) != HAL_OK)
  2703. {
  2704. return HAL_ERROR;
  2705. }
  2706. /* Clear ADDR flag */
  2707. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  2708. /* Generate Stop */
  2709. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  2710. hi2c->State = HAL_I2C_STATE_READY;
  2711. /* Process Unlocked */
  2712. __HAL_UNLOCK(hi2c);
  2713. }
  2714. return HAL_OK;
  2715. }
  2716. else
  2717. {
  2718. return HAL_BUSY;
  2719. }
  2720. }
  2721. /**
  2722. * @brief Checks if target device is ready for communication.
  2723. * @note This function is used with Memory devices
  2724. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2725. * the configuration information for the specified I2C.
  2726. * @param DevAddress Target device address: The device 7 bits address value
  2727. * in datasheet must be shifted to the left before calling the interface
  2728. * @param Trials Number of trials
  2729. * @param Timeout Timeout duration
  2730. * @retval HAL status
  2731. */
  2732. HAL_StatusTypeDef HAL_I2C_IsDeviceReady(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint32_t Trials, uint32_t Timeout)
  2733. {
  2734. /* Get tick */
  2735. uint32_t tickstart = HAL_GetTick();
  2736. uint32_t I2C_Trials = 1U;
  2737. FlagStatus tmp1;
  2738. FlagStatus tmp2;
  2739. if (hi2c->State == HAL_I2C_STATE_READY)
  2740. {
  2741. /* Wait until BUSY flag is reset */
  2742. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY_FLAG, tickstart) != HAL_OK)
  2743. {
  2744. return HAL_BUSY;
  2745. }
  2746. /* Process Locked */
  2747. __HAL_LOCK(hi2c);
  2748. /* Check if the I2C is already enabled */
  2749. if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  2750. {
  2751. /* Enable I2C peripheral */
  2752. __HAL_I2C_ENABLE(hi2c);
  2753. }
  2754. /* Disable Pos */
  2755. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  2756. hi2c->State = HAL_I2C_STATE_BUSY;
  2757. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  2758. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  2759. do
  2760. {
  2761. /* Generate Start */
  2762. SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
  2763. /* Wait until SB flag is set */
  2764. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout, tickstart) != HAL_OK)
  2765. {
  2766. if (READ_BIT(hi2c->Instance->CR1, I2C_CR1_START) == I2C_CR1_START)
  2767. {
  2768. hi2c->ErrorCode = HAL_I2C_WRONG_START;
  2769. }
  2770. return HAL_TIMEOUT;
  2771. }
  2772. /* Send slave address */
  2773. hi2c->Instance->DR = I2C_7BIT_ADD_WRITE(DevAddress);
  2774. /* Wait until ADDR or AF flag are set */
  2775. /* Get tick */
  2776. tickstart = HAL_GetTick();
  2777. tmp1 = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ADDR);
  2778. tmp2 = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF);
  2779. while ((hi2c->State != HAL_I2C_STATE_TIMEOUT) && (tmp1 == RESET) && (tmp2 == RESET))
  2780. {
  2781. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  2782. {
  2783. hi2c->State = HAL_I2C_STATE_TIMEOUT;
  2784. }
  2785. tmp1 = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ADDR);
  2786. tmp2 = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF);
  2787. }
  2788. hi2c->State = HAL_I2C_STATE_READY;
  2789. /* Check if the ADDR flag has been set */
  2790. if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ADDR) == SET)
  2791. {
  2792. /* Generate Stop */
  2793. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  2794. /* Clear ADDR Flag */
  2795. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  2796. /* Wait until BUSY flag is reset */
  2797. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY_FLAG, tickstart) != HAL_OK)
  2798. {
  2799. return HAL_ERROR;
  2800. }
  2801. hi2c->State = HAL_I2C_STATE_READY;
  2802. /* Process Unlocked */
  2803. __HAL_UNLOCK(hi2c);
  2804. return HAL_OK;
  2805. }
  2806. else
  2807. {
  2808. /* Generate Stop */
  2809. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  2810. /* Clear AF Flag */
  2811. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  2812. /* Wait until BUSY flag is reset */
  2813. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY_FLAG, tickstart) != HAL_OK)
  2814. {
  2815. return HAL_ERROR;
  2816. }
  2817. }
  2818. /* Increment Trials */
  2819. I2C_Trials++;
  2820. }
  2821. while (I2C_Trials < Trials);
  2822. hi2c->State = HAL_I2C_STATE_READY;
  2823. /* Process Unlocked */
  2824. __HAL_UNLOCK(hi2c);
  2825. return HAL_ERROR;
  2826. }
  2827. else
  2828. {
  2829. return HAL_BUSY;
  2830. }
  2831. }
  2832. /**
  2833. * @brief Sequential transmit in master I2C mode an amount of data in non-blocking mode with Interrupt.
  2834. * @note This interface allow to manage repeated start condition when a direction change during transfer
  2835. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2836. * the configuration information for the specified I2C.
  2837. * @param DevAddress Target device address: The device 7 bits address value
  2838. * in datasheet must be shifted to the left before calling the interface
  2839. * @param pData Pointer to data buffer
  2840. * @param Size Amount of data to be sent
  2841. * @param XferOptions Options of Transfer, value of @ref I2C_XferOptions_definition
  2842. * @retval HAL status
  2843. */
  2844. HAL_StatusTypeDef HAL_I2C_Master_Seq_Transmit_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
  2845. {
  2846. __IO uint32_t Prev_State = 0x00U;
  2847. __IO uint32_t count = 0x00U;
  2848. /* Check the parameters */
  2849. assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
  2850. if (hi2c->State == HAL_I2C_STATE_READY)
  2851. {
  2852. /* Check Busy Flag only if FIRST call of Master interface */
  2853. if ((READ_BIT(hi2c->Instance->CR1, I2C_CR1_STOP) == I2C_CR1_STOP) || (XferOptions == I2C_FIRST_AND_LAST_FRAME) || (XferOptions == I2C_FIRST_FRAME))
  2854. {
  2855. /* Wait until BUSY flag is reset */
  2856. count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U);
  2857. do
  2858. {
  2859. count--;
  2860. if (count == 0U)
  2861. {
  2862. hi2c->PreviousState = I2C_STATE_NONE;
  2863. hi2c->State = HAL_I2C_STATE_READY;
  2864. hi2c->Mode = HAL_I2C_MODE_NONE;
  2865. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  2866. /* Process Unlocked */
  2867. __HAL_UNLOCK(hi2c);
  2868. return HAL_ERROR;
  2869. }
  2870. }
  2871. while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
  2872. }
  2873. /* Process Locked */
  2874. __HAL_LOCK(hi2c);
  2875. /* Check if the I2C is already enabled */
  2876. if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  2877. {
  2878. /* Enable I2C peripheral */
  2879. __HAL_I2C_ENABLE(hi2c);
  2880. }
  2881. /* Disable Pos */
  2882. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  2883. hi2c->State = HAL_I2C_STATE_BUSY_TX;
  2884. hi2c->Mode = HAL_I2C_MODE_MASTER;
  2885. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  2886. /* Prepare transfer parameters */
  2887. hi2c->pBuffPtr = pData;
  2888. hi2c->XferCount = Size;
  2889. hi2c->XferSize = hi2c->XferCount;
  2890. hi2c->XferOptions = XferOptions;
  2891. hi2c->Devaddress = DevAddress;
  2892. Prev_State = hi2c->PreviousState;
  2893. /* If transfer direction not change and there is no request to start another frame, do not generate Restart Condition */
  2894. /* Mean Previous state is same as current state */
  2895. if ((Prev_State != I2C_STATE_MASTER_BUSY_TX) || (IS_I2C_TRANSFER_OTHER_OPTIONS_REQUEST(XferOptions) == 1))
  2896. {
  2897. /* Generate Start */
  2898. SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
  2899. }
  2900. /* Process Unlocked */
  2901. __HAL_UNLOCK(hi2c);
  2902. /* Note : The I2C interrupts must be enabled after unlocking current process
  2903. to avoid the risk of I2C interrupt handle execution before current
  2904. process unlock */
  2905. /* Enable EVT, BUF and ERR interrupt */
  2906. __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  2907. return HAL_OK;
  2908. }
  2909. else
  2910. {
  2911. return HAL_BUSY;
  2912. }
  2913. }
  2914. /**
  2915. * @brief Sequential transmit in master I2C mode an amount of data in non-blocking mode with DMA.
  2916. * @note This interface allow to manage repeated start condition when a direction change during transfer
  2917. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  2918. * the configuration information for the specified I2C.
  2919. * @param DevAddress Target device address: The device 7 bits address value
  2920. * in datasheet must be shifted to the left before calling the interface
  2921. * @param pData Pointer to data buffer
  2922. * @param Size Amount of data to be sent
  2923. * @param XferOptions Options of Transfer, value of @ref I2C_XferOptions_definition
  2924. * @retval HAL status
  2925. */
  2926. HAL_StatusTypeDef HAL_I2C_Master_Seq_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
  2927. {
  2928. __IO uint32_t Prev_State = 0x00U;
  2929. __IO uint32_t count = 0x00U;
  2930. HAL_StatusTypeDef dmaxferstatus;
  2931. /* Check the parameters */
  2932. assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
  2933. if (hi2c->State == HAL_I2C_STATE_READY)
  2934. {
  2935. /* Check Busy Flag only if FIRST call of Master interface */
  2936. if ((READ_BIT(hi2c->Instance->CR1, I2C_CR1_STOP) == I2C_CR1_STOP) || (XferOptions == I2C_FIRST_AND_LAST_FRAME) || (XferOptions == I2C_FIRST_FRAME))
  2937. {
  2938. /* Wait until BUSY flag is reset */
  2939. count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U);
  2940. do
  2941. {
  2942. count--;
  2943. if (count == 0U)
  2944. {
  2945. hi2c->PreviousState = I2C_STATE_NONE;
  2946. hi2c->State = HAL_I2C_STATE_READY;
  2947. hi2c->Mode = HAL_I2C_MODE_NONE;
  2948. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  2949. /* Process Unlocked */
  2950. __HAL_UNLOCK(hi2c);
  2951. return HAL_ERROR;
  2952. }
  2953. }
  2954. while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
  2955. }
  2956. /* Process Locked */
  2957. __HAL_LOCK(hi2c);
  2958. /* Check if the I2C is already enabled */
  2959. if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  2960. {
  2961. /* Enable I2C peripheral */
  2962. __HAL_I2C_ENABLE(hi2c);
  2963. }
  2964. /* Disable Pos */
  2965. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  2966. hi2c->State = HAL_I2C_STATE_BUSY_TX;
  2967. hi2c->Mode = HAL_I2C_MODE_MASTER;
  2968. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  2969. /* Prepare transfer parameters */
  2970. hi2c->pBuffPtr = pData;
  2971. hi2c->XferCount = Size;
  2972. hi2c->XferSize = hi2c->XferCount;
  2973. hi2c->XferOptions = XferOptions;
  2974. hi2c->Devaddress = DevAddress;
  2975. Prev_State = hi2c->PreviousState;
  2976. if (hi2c->XferSize > 0U)
  2977. {
  2978. /* Set the I2C DMA transfer complete callback */
  2979. hi2c->hdmatx->XferCpltCallback = I2C_DMAXferCplt;
  2980. /* Set the DMA error callback */
  2981. hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
  2982. /* Set the unused DMA callbacks to NULL */
  2983. hi2c->hdmatx->XferHalfCpltCallback = NULL;
  2984. hi2c->hdmatx->XferAbortCallback = NULL;
  2985. /* Enable the DMA channel */
  2986. dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)hi2c->pBuffPtr, (uint32_t)&hi2c->Instance->DR, hi2c->XferSize);
  2987. if (dmaxferstatus == HAL_OK)
  2988. {
  2989. /* Enable Acknowledge */
  2990. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  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. /* If XferOptions is not associated to a new frame, mean no start bit is request, enable directly the DMA request */
  3004. /* In other cases, DMA request is enabled after Slave address treatment in IRQHandler */
  3005. if ((XferOptions == I2C_NEXT_FRAME) || (XferOptions == I2C_LAST_FRAME) || (XferOptions == I2C_LAST_FRAME_NO_STOP))
  3006. {
  3007. /* Enable DMA Request */
  3008. SET_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
  3009. }
  3010. /* Enable EVT and ERR interrupt */
  3011. __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR);
  3012. }
  3013. else
  3014. {
  3015. /* Update I2C state */
  3016. hi2c->State = HAL_I2C_STATE_READY;
  3017. hi2c->Mode = HAL_I2C_MODE_NONE;
  3018. /* Update I2C error code */
  3019. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
  3020. /* Process Unlocked */
  3021. __HAL_UNLOCK(hi2c);
  3022. return HAL_ERROR;
  3023. }
  3024. }
  3025. else
  3026. {
  3027. /* Enable Acknowledge */
  3028. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  3029. /* If transfer direction not change and there is no request to start another frame, do not generate Restart Condition */
  3030. /* Mean Previous state is same as current state */
  3031. if ((Prev_State != I2C_STATE_MASTER_BUSY_TX) || (IS_I2C_TRANSFER_OTHER_OPTIONS_REQUEST(XferOptions) == 1))
  3032. {
  3033. /* Generate Start */
  3034. SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
  3035. }
  3036. /* Process Unlocked */
  3037. __HAL_UNLOCK(hi2c);
  3038. /* Note : The I2C interrupts must be enabled after unlocking current process
  3039. to avoid the risk of I2C interrupt handle execution before current
  3040. process unlock */
  3041. /* Enable EVT, BUF and ERR interrupt */
  3042. __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  3043. }
  3044. return HAL_OK;
  3045. }
  3046. else
  3047. {
  3048. return HAL_BUSY;
  3049. }
  3050. }
  3051. /**
  3052. * @brief Sequential receive in master I2C mode an amount of data in non-blocking mode with Interrupt
  3053. * @note This interface allow to manage repeated start condition when a direction change during transfer
  3054. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3055. * the configuration information for the specified I2C.
  3056. * @param DevAddress Target device address: The device 7 bits address value
  3057. * in datasheet must be shifted to the left before calling the interface
  3058. * @param pData Pointer to data buffer
  3059. * @param Size Amount of data to be sent
  3060. * @param XferOptions Options of Transfer, value of @ref I2C_XferOptions_definition
  3061. * @retval HAL status
  3062. */
  3063. HAL_StatusTypeDef HAL_I2C_Master_Seq_Receive_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
  3064. {
  3065. __IO uint32_t Prev_State = 0x00U;
  3066. __IO uint32_t count = 0U;
  3067. uint32_t enableIT = (I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  3068. /* Check the parameters */
  3069. assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
  3070. if (hi2c->State == HAL_I2C_STATE_READY)
  3071. {
  3072. /* Check Busy Flag only if FIRST call of Master interface */
  3073. if ((READ_BIT(hi2c->Instance->CR1, I2C_CR1_STOP) == I2C_CR1_STOP) || (XferOptions == I2C_FIRST_AND_LAST_FRAME) || (XferOptions == I2C_FIRST_FRAME))
  3074. {
  3075. /* Wait until BUSY flag is reset */
  3076. count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U);
  3077. do
  3078. {
  3079. count--;
  3080. if (count == 0U)
  3081. {
  3082. hi2c->PreviousState = I2C_STATE_NONE;
  3083. hi2c->State = HAL_I2C_STATE_READY;
  3084. hi2c->Mode = HAL_I2C_MODE_NONE;
  3085. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  3086. /* Process Unlocked */
  3087. __HAL_UNLOCK(hi2c);
  3088. return HAL_ERROR;
  3089. }
  3090. }
  3091. while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
  3092. }
  3093. /* Process Locked */
  3094. __HAL_LOCK(hi2c);
  3095. /* Check if the I2C is already enabled */
  3096. if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  3097. {
  3098. /* Enable I2C peripheral */
  3099. __HAL_I2C_ENABLE(hi2c);
  3100. }
  3101. /* Disable Pos */
  3102. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  3103. hi2c->State = HAL_I2C_STATE_BUSY_RX;
  3104. hi2c->Mode = HAL_I2C_MODE_MASTER;
  3105. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  3106. /* Prepare transfer parameters */
  3107. hi2c->pBuffPtr = pData;
  3108. hi2c->XferCount = Size;
  3109. hi2c->XferSize = hi2c->XferCount;
  3110. hi2c->XferOptions = XferOptions;
  3111. hi2c->Devaddress = DevAddress;
  3112. Prev_State = hi2c->PreviousState;
  3113. if ((hi2c->XferCount == 2U) && ((XferOptions == I2C_LAST_FRAME) || (XferOptions == I2C_LAST_FRAME_NO_STOP)))
  3114. {
  3115. if (Prev_State == I2C_STATE_MASTER_BUSY_RX)
  3116. {
  3117. /* Disable Acknowledge */
  3118. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  3119. /* Enable Pos */
  3120. SET_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  3121. /* Remove Enabling of IT_BUF, mean RXNE treatment, treat the 2 bytes through BTF */
  3122. enableIT &= ~I2C_IT_BUF;
  3123. }
  3124. else
  3125. {
  3126. /* Enable Acknowledge */
  3127. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  3128. }
  3129. }
  3130. else
  3131. {
  3132. /* Enable Acknowledge */
  3133. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  3134. }
  3135. /* If transfer direction not change and there is no request to start another frame, do not generate Restart Condition */
  3136. /* Mean Previous state is same as current state */
  3137. if ((Prev_State != I2C_STATE_MASTER_BUSY_RX) || (IS_I2C_TRANSFER_OTHER_OPTIONS_REQUEST(XferOptions) == 1))
  3138. {
  3139. /* Generate Start */
  3140. SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
  3141. }
  3142. /* Process Unlocked */
  3143. __HAL_UNLOCK(hi2c);
  3144. /* Note : The I2C interrupts must be enabled after unlocking current process
  3145. to avoid the risk of I2C interrupt handle execution before current
  3146. process unlock */
  3147. /* Enable interrupts */
  3148. __HAL_I2C_ENABLE_IT(hi2c, enableIT);
  3149. return HAL_OK;
  3150. }
  3151. else
  3152. {
  3153. return HAL_BUSY;
  3154. }
  3155. }
  3156. /**
  3157. * @brief Sequential receive in master mode an amount of data in non-blocking mode with DMA
  3158. * @note This interface allow to manage repeated start condition when a direction change during transfer
  3159. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3160. * the configuration information for the specified I2C.
  3161. * @param DevAddress Target device address: The device 7 bits address value
  3162. * in datasheet must be shifted to the left before calling the interface
  3163. * @param pData Pointer to data buffer
  3164. * @param Size Amount of data to be sent
  3165. * @param XferOptions Options of Transfer, value of @ref I2C_XferOptions_definition
  3166. * @retval HAL status
  3167. */
  3168. HAL_StatusTypeDef HAL_I2C_Master_Seq_Receive_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
  3169. {
  3170. __IO uint32_t Prev_State = 0x00U;
  3171. __IO uint32_t count = 0U;
  3172. uint32_t enableIT = (I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  3173. HAL_StatusTypeDef dmaxferstatus;
  3174. /* Check the parameters */
  3175. assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
  3176. if (hi2c->State == HAL_I2C_STATE_READY)
  3177. {
  3178. /* Check Busy Flag only if FIRST call of Master interface */
  3179. if ((READ_BIT(hi2c->Instance->CR1, I2C_CR1_STOP) == I2C_CR1_STOP) || (XferOptions == I2C_FIRST_AND_LAST_FRAME) || (XferOptions == I2C_FIRST_FRAME))
  3180. {
  3181. /* Wait until BUSY flag is reset */
  3182. count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U);
  3183. do
  3184. {
  3185. count--;
  3186. if (count == 0U)
  3187. {
  3188. hi2c->PreviousState = I2C_STATE_NONE;
  3189. hi2c->State = HAL_I2C_STATE_READY;
  3190. hi2c->Mode = HAL_I2C_MODE_NONE;
  3191. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  3192. /* Process Unlocked */
  3193. __HAL_UNLOCK(hi2c);
  3194. return HAL_ERROR;
  3195. }
  3196. }
  3197. while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
  3198. }
  3199. /* Process Locked */
  3200. __HAL_LOCK(hi2c);
  3201. /* Check if the I2C is already enabled */
  3202. if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  3203. {
  3204. /* Enable I2C peripheral */
  3205. __HAL_I2C_ENABLE(hi2c);
  3206. }
  3207. /* Disable Pos */
  3208. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  3209. /* Clear Last DMA bit */
  3210. CLEAR_BIT(hi2c->Instance->CR2, I2C_CR2_LAST);
  3211. hi2c->State = HAL_I2C_STATE_BUSY_RX;
  3212. hi2c->Mode = HAL_I2C_MODE_MASTER;
  3213. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  3214. /* Prepare transfer parameters */
  3215. hi2c->pBuffPtr = pData;
  3216. hi2c->XferCount = Size;
  3217. hi2c->XferSize = hi2c->XferCount;
  3218. hi2c->XferOptions = XferOptions;
  3219. hi2c->Devaddress = DevAddress;
  3220. Prev_State = hi2c->PreviousState;
  3221. if (hi2c->XferSize > 0U)
  3222. {
  3223. if ((hi2c->XferCount == 2U) && ((XferOptions == I2C_LAST_FRAME) || (XferOptions == I2C_LAST_FRAME_NO_STOP)))
  3224. {
  3225. if (Prev_State == I2C_STATE_MASTER_BUSY_RX)
  3226. {
  3227. /* Disable Acknowledge */
  3228. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  3229. /* Enable Pos */
  3230. SET_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  3231. /* Enable Last DMA bit */
  3232. SET_BIT(hi2c->Instance->CR2, I2C_CR2_LAST);
  3233. }
  3234. else
  3235. {
  3236. /* Enable Acknowledge */
  3237. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  3238. }
  3239. }
  3240. else
  3241. {
  3242. /* Enable Acknowledge */
  3243. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  3244. if ((XferOptions == I2C_LAST_FRAME) || (XferOptions == I2C_OTHER_AND_LAST_FRAME) || (XferOptions == I2C_LAST_FRAME_NO_STOP))
  3245. {
  3246. /* Enable Last DMA bit */
  3247. SET_BIT(hi2c->Instance->CR2, I2C_CR2_LAST);
  3248. }
  3249. }
  3250. /* Set the I2C DMA transfer complete callback */
  3251. hi2c->hdmarx->XferCpltCallback = I2C_DMAXferCplt;
  3252. /* Set the DMA error callback */
  3253. hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
  3254. /* Set the unused DMA callbacks to NULL */
  3255. hi2c->hdmarx->XferHalfCpltCallback = NULL;
  3256. hi2c->hdmarx->XferAbortCallback = NULL;
  3257. /* Enable the DMA channel */
  3258. dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->DR, (uint32_t)hi2c->pBuffPtr, hi2c->XferSize);
  3259. if (dmaxferstatus == HAL_OK)
  3260. {
  3261. /* If transfer direction not change and there is no request to start another frame, do not generate Restart Condition */
  3262. /* Mean Previous state is same as current state */
  3263. if ((Prev_State != I2C_STATE_MASTER_BUSY_RX) || (IS_I2C_TRANSFER_OTHER_OPTIONS_REQUEST(XferOptions) == 1))
  3264. {
  3265. /* Generate Start */
  3266. SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
  3267. /* Update interrupt for only EVT and ERR */
  3268. enableIT = (I2C_IT_EVT | I2C_IT_ERR);
  3269. }
  3270. else
  3271. {
  3272. /* Update interrupt for only ERR */
  3273. enableIT = I2C_IT_ERR;
  3274. }
  3275. /* Process Unlocked */
  3276. __HAL_UNLOCK(hi2c);
  3277. /* Note : The I2C interrupts must be enabled after unlocking current process
  3278. to avoid the risk of I2C interrupt handle execution before current
  3279. process unlock */
  3280. /* If XferOptions is not associated to a new frame, mean no start bit is request, enable directly the DMA request */
  3281. /* In other cases, DMA request is enabled after Slave address treatment in IRQHandler */
  3282. if ((XferOptions == I2C_NEXT_FRAME) || (XferOptions == I2C_LAST_FRAME) || (XferOptions == I2C_LAST_FRAME_NO_STOP))
  3283. {
  3284. /* Enable DMA Request */
  3285. SET_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
  3286. }
  3287. /* Enable EVT and ERR interrupt */
  3288. __HAL_I2C_ENABLE_IT(hi2c, enableIT);
  3289. }
  3290. else
  3291. {
  3292. /* Update I2C state */
  3293. hi2c->State = HAL_I2C_STATE_READY;
  3294. hi2c->Mode = HAL_I2C_MODE_NONE;
  3295. /* Update I2C error code */
  3296. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
  3297. /* Process Unlocked */
  3298. __HAL_UNLOCK(hi2c);
  3299. return HAL_ERROR;
  3300. }
  3301. }
  3302. else
  3303. {
  3304. /* Enable Acknowledge */
  3305. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  3306. /* If transfer direction not change and there is no request to start another frame, do not generate Restart Condition */
  3307. /* Mean Previous state is same as current state */
  3308. if ((Prev_State != I2C_STATE_MASTER_BUSY_RX) || (IS_I2C_TRANSFER_OTHER_OPTIONS_REQUEST(XferOptions) == 1))
  3309. {
  3310. /* Generate Start */
  3311. SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
  3312. }
  3313. /* Process Unlocked */
  3314. __HAL_UNLOCK(hi2c);
  3315. /* Note : The I2C interrupts must be enabled after unlocking current process
  3316. to avoid the risk of I2C interrupt handle execution before current
  3317. process unlock */
  3318. /* Enable interrupts */
  3319. __HAL_I2C_ENABLE_IT(hi2c, enableIT);
  3320. }
  3321. return HAL_OK;
  3322. }
  3323. else
  3324. {
  3325. return HAL_BUSY;
  3326. }
  3327. }
  3328. /**
  3329. * @brief Sequential transmit in slave mode an amount of data in non-blocking mode with Interrupt
  3330. * @note This interface allow to manage repeated start condition when a direction change during transfer
  3331. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3332. * the configuration information for the specified I2C.
  3333. * @param pData Pointer to data buffer
  3334. * @param Size Amount of data to be sent
  3335. * @param XferOptions Options of Transfer, value of @ref I2C_XferOptions_definition
  3336. * @retval HAL status
  3337. */
  3338. HAL_StatusTypeDef HAL_I2C_Slave_Seq_Transmit_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
  3339. {
  3340. /* Check the parameters */
  3341. assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
  3342. if (((uint32_t)hi2c->State & (uint32_t)HAL_I2C_STATE_LISTEN) == (uint32_t)HAL_I2C_STATE_LISTEN)
  3343. {
  3344. if ((pData == NULL) || (Size == 0U))
  3345. {
  3346. return HAL_ERROR;
  3347. }
  3348. /* Process Locked */
  3349. __HAL_LOCK(hi2c);
  3350. /* Check if the I2C is already enabled */
  3351. if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  3352. {
  3353. /* Enable I2C peripheral */
  3354. __HAL_I2C_ENABLE(hi2c);
  3355. }
  3356. /* Disable Pos */
  3357. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  3358. hi2c->State = HAL_I2C_STATE_BUSY_TX_LISTEN;
  3359. hi2c->Mode = HAL_I2C_MODE_SLAVE;
  3360. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  3361. /* Prepare transfer parameters */
  3362. hi2c->pBuffPtr = pData;
  3363. hi2c->XferCount = Size;
  3364. hi2c->XferSize = hi2c->XferCount;
  3365. hi2c->XferOptions = XferOptions;
  3366. /* Clear ADDR flag */
  3367. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  3368. /* Process Unlocked */
  3369. __HAL_UNLOCK(hi2c);
  3370. /* Note : The I2C interrupts must be enabled after unlocking current process
  3371. to avoid the risk of I2C interrupt handle execution before current
  3372. process unlock */
  3373. /* Enable EVT, BUF and ERR interrupt */
  3374. __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  3375. return HAL_OK;
  3376. }
  3377. else
  3378. {
  3379. return HAL_BUSY;
  3380. }
  3381. }
  3382. /**
  3383. * @brief Sequential transmit in slave mode an amount of data in non-blocking mode with DMA
  3384. * @note This interface allow to manage repeated start condition when a direction change during transfer
  3385. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3386. * the configuration information for the specified I2C.
  3387. * @param pData Pointer to data buffer
  3388. * @param Size Amount of data to be sent
  3389. * @param XferOptions Options of Transfer, value of @ref I2C_XferOptions_definition
  3390. * @retval HAL status
  3391. */
  3392. HAL_StatusTypeDef HAL_I2C_Slave_Seq_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
  3393. {
  3394. HAL_StatusTypeDef dmaxferstatus;
  3395. /* Check the parameters */
  3396. assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
  3397. if (((uint32_t)hi2c->State & (uint32_t)HAL_I2C_STATE_LISTEN) == (uint32_t)HAL_I2C_STATE_LISTEN)
  3398. {
  3399. if ((pData == NULL) || (Size == 0U))
  3400. {
  3401. return HAL_ERROR;
  3402. }
  3403. /* Process Locked */
  3404. __HAL_LOCK(hi2c);
  3405. /* Disable Interrupts, to prevent preemption during treatment in case of multicall */
  3406. __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR);
  3407. /* I2C cannot manage full duplex exchange so disable previous IT enabled if any */
  3408. /* and then toggle the HAL slave RX state to TX state */
  3409. if (hi2c->State == HAL_I2C_STATE_BUSY_RX_LISTEN)
  3410. {
  3411. if ((hi2c->Instance->CR2 & I2C_CR2_DMAEN) == I2C_CR2_DMAEN)
  3412. {
  3413. /* Abort DMA Xfer if any */
  3414. if (hi2c->hdmarx != NULL)
  3415. {
  3416. CLEAR_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
  3417. /* Set the I2C DMA Abort callback :
  3418. will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
  3419. hi2c->hdmarx->XferAbortCallback = I2C_DMAAbort;
  3420. /* Abort DMA RX */
  3421. if (HAL_DMA_Abort_IT(hi2c->hdmarx) != HAL_OK)
  3422. {
  3423. /* Call Directly XferAbortCallback function in case of error */
  3424. hi2c->hdmarx->XferAbortCallback(hi2c->hdmarx);
  3425. }
  3426. }
  3427. }
  3428. }
  3429. else if (hi2c->State == HAL_I2C_STATE_BUSY_TX_LISTEN)
  3430. {
  3431. if ((hi2c->Instance->CR2 & I2C_CR2_DMAEN) == I2C_CR2_DMAEN)
  3432. {
  3433. CLEAR_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
  3434. /* Abort DMA Xfer if any */
  3435. if (hi2c->hdmatx != NULL)
  3436. {
  3437. /* Set the I2C DMA Abort callback :
  3438. will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
  3439. hi2c->hdmatx->XferAbortCallback = I2C_DMAAbort;
  3440. /* Abort DMA TX */
  3441. if (HAL_DMA_Abort_IT(hi2c->hdmatx) != HAL_OK)
  3442. {
  3443. /* Call Directly XferAbortCallback function in case of error */
  3444. hi2c->hdmatx->XferAbortCallback(hi2c->hdmatx);
  3445. }
  3446. }
  3447. }
  3448. }
  3449. else
  3450. {
  3451. /* Nothing to do */
  3452. }
  3453. /* Check if the I2C is already enabled */
  3454. if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  3455. {
  3456. /* Enable I2C peripheral */
  3457. __HAL_I2C_ENABLE(hi2c);
  3458. }
  3459. /* Disable Pos */
  3460. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  3461. hi2c->State = HAL_I2C_STATE_BUSY_TX_LISTEN;
  3462. hi2c->Mode = HAL_I2C_MODE_SLAVE;
  3463. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  3464. /* Prepare transfer parameters */
  3465. hi2c->pBuffPtr = pData;
  3466. hi2c->XferCount = Size;
  3467. hi2c->XferSize = hi2c->XferCount;
  3468. hi2c->XferOptions = XferOptions;
  3469. /* Set the I2C DMA transfer complete callback */
  3470. hi2c->hdmatx->XferCpltCallback = I2C_DMAXferCplt;
  3471. /* Set the DMA error callback */
  3472. hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
  3473. /* Set the unused DMA callbacks to NULL */
  3474. hi2c->hdmatx->XferHalfCpltCallback = NULL;
  3475. hi2c->hdmatx->XferAbortCallback = NULL;
  3476. /* Enable the DMA channel */
  3477. dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)hi2c->pBuffPtr, (uint32_t)&hi2c->Instance->DR, hi2c->XferSize);
  3478. if (dmaxferstatus == HAL_OK)
  3479. {
  3480. /* Enable Address Acknowledge */
  3481. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  3482. /* Clear ADDR flag */
  3483. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  3484. /* Process Unlocked */
  3485. __HAL_UNLOCK(hi2c);
  3486. /* Note : The I2C interrupts must be enabled after unlocking current process
  3487. to avoid the risk of I2C interrupt handle execution before current
  3488. process unlock */
  3489. /* Enable EVT and ERR interrupt */
  3490. __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR);
  3491. /* Enable DMA Request */
  3492. hi2c->Instance->CR2 |= I2C_CR2_DMAEN;
  3493. return HAL_OK;
  3494. }
  3495. else
  3496. {
  3497. /* Update I2C state */
  3498. hi2c->State = HAL_I2C_STATE_READY;
  3499. hi2c->Mode = HAL_I2C_MODE_NONE;
  3500. /* Update I2C error code */
  3501. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
  3502. /* Process Unlocked */
  3503. __HAL_UNLOCK(hi2c);
  3504. return HAL_ERROR;
  3505. }
  3506. }
  3507. else
  3508. {
  3509. return HAL_BUSY;
  3510. }
  3511. }
  3512. /**
  3513. * @brief Sequential receive in slave mode an amount of data in non-blocking mode with Interrupt
  3514. * @note This interface allow to manage repeated start condition when a direction change during transfer
  3515. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3516. * the configuration information for the specified I2C.
  3517. * @param pData Pointer to data buffer
  3518. * @param Size Amount of data to be sent
  3519. * @param XferOptions Options of Transfer, value of @ref I2C_XferOptions_definition
  3520. * @retval HAL status
  3521. */
  3522. HAL_StatusTypeDef HAL_I2C_Slave_Seq_Receive_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
  3523. {
  3524. /* Check the parameters */
  3525. assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
  3526. if (((uint32_t)hi2c->State & (uint32_t)HAL_I2C_STATE_LISTEN) == (uint32_t)HAL_I2C_STATE_LISTEN)
  3527. {
  3528. if ((pData == NULL) || (Size == 0U))
  3529. {
  3530. return HAL_ERROR;
  3531. }
  3532. /* Process Locked */
  3533. __HAL_LOCK(hi2c);
  3534. /* Check if the I2C is already enabled */
  3535. if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  3536. {
  3537. /* Enable I2C peripheral */
  3538. __HAL_I2C_ENABLE(hi2c);
  3539. }
  3540. /* Disable Pos */
  3541. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  3542. hi2c->State = HAL_I2C_STATE_BUSY_RX_LISTEN;
  3543. hi2c->Mode = HAL_I2C_MODE_SLAVE;
  3544. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  3545. /* Prepare transfer parameters */
  3546. hi2c->pBuffPtr = pData;
  3547. hi2c->XferCount = Size;
  3548. hi2c->XferSize = hi2c->XferCount;
  3549. hi2c->XferOptions = XferOptions;
  3550. /* Clear ADDR flag */
  3551. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  3552. /* Process Unlocked */
  3553. __HAL_UNLOCK(hi2c);
  3554. /* Note : The I2C interrupts must be enabled after unlocking current process
  3555. to avoid the risk of I2C interrupt handle execution before current
  3556. process unlock */
  3557. /* Enable EVT, BUF and ERR interrupt */
  3558. __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  3559. return HAL_OK;
  3560. }
  3561. else
  3562. {
  3563. return HAL_BUSY;
  3564. }
  3565. }
  3566. /**
  3567. * @brief Sequential receive in slave mode an amount of data in non-blocking mode with DMA
  3568. * @note This interface allow to manage repeated start condition when a direction change during transfer
  3569. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3570. * the configuration information for the specified I2C.
  3571. * @param pData Pointer to data buffer
  3572. * @param Size Amount of data to be sent
  3573. * @param XferOptions Options of Transfer, value of @ref I2C_XferOptions_definition
  3574. * @retval HAL status
  3575. */
  3576. HAL_StatusTypeDef HAL_I2C_Slave_Seq_Receive_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
  3577. {
  3578. HAL_StatusTypeDef dmaxferstatus;
  3579. /* Check the parameters */
  3580. assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions));
  3581. if (((uint32_t)hi2c->State & (uint32_t)HAL_I2C_STATE_LISTEN) == (uint32_t)HAL_I2C_STATE_LISTEN)
  3582. {
  3583. if ((pData == NULL) || (Size == 0U))
  3584. {
  3585. return HAL_ERROR;
  3586. }
  3587. /* Process Locked */
  3588. __HAL_LOCK(hi2c);
  3589. /* Disable Interrupts, to prevent preemption during treatment in case of multicall */
  3590. __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR);
  3591. /* I2C cannot manage full duplex exchange so disable previous IT enabled if any */
  3592. /* and then toggle the HAL slave RX state to TX state */
  3593. if (hi2c->State == HAL_I2C_STATE_BUSY_RX_LISTEN)
  3594. {
  3595. if ((hi2c->Instance->CR2 & I2C_CR2_DMAEN) == I2C_CR2_DMAEN)
  3596. {
  3597. /* Abort DMA Xfer if any */
  3598. if (hi2c->hdmarx != NULL)
  3599. {
  3600. CLEAR_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
  3601. /* Set the I2C DMA Abort callback :
  3602. will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
  3603. hi2c->hdmarx->XferAbortCallback = I2C_DMAAbort;
  3604. /* Abort DMA RX */
  3605. if (HAL_DMA_Abort_IT(hi2c->hdmarx) != HAL_OK)
  3606. {
  3607. /* Call Directly XferAbortCallback function in case of error */
  3608. hi2c->hdmarx->XferAbortCallback(hi2c->hdmarx);
  3609. }
  3610. }
  3611. }
  3612. }
  3613. else if (hi2c->State == HAL_I2C_STATE_BUSY_TX_LISTEN)
  3614. {
  3615. if ((hi2c->Instance->CR2 & I2C_CR2_DMAEN) == I2C_CR2_DMAEN)
  3616. {
  3617. CLEAR_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
  3618. /* Abort DMA Xfer if any */
  3619. if (hi2c->hdmatx != NULL)
  3620. {
  3621. /* Set the I2C DMA Abort callback :
  3622. will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
  3623. hi2c->hdmatx->XferAbortCallback = I2C_DMAAbort;
  3624. /* Abort DMA TX */
  3625. if (HAL_DMA_Abort_IT(hi2c->hdmatx) != HAL_OK)
  3626. {
  3627. /* Call Directly XferAbortCallback function in case of error */
  3628. hi2c->hdmatx->XferAbortCallback(hi2c->hdmatx);
  3629. }
  3630. }
  3631. }
  3632. }
  3633. else
  3634. {
  3635. /* Nothing to do */
  3636. }
  3637. /* Check if the I2C is already enabled */
  3638. if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  3639. {
  3640. /* Enable I2C peripheral */
  3641. __HAL_I2C_ENABLE(hi2c);
  3642. }
  3643. /* Disable Pos */
  3644. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  3645. hi2c->State = HAL_I2C_STATE_BUSY_RX_LISTEN;
  3646. hi2c->Mode = HAL_I2C_MODE_SLAVE;
  3647. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  3648. /* Prepare transfer parameters */
  3649. hi2c->pBuffPtr = pData;
  3650. hi2c->XferCount = Size;
  3651. hi2c->XferSize = hi2c->XferCount;
  3652. hi2c->XferOptions = XferOptions;
  3653. /* Set the I2C DMA transfer complete callback */
  3654. hi2c->hdmarx->XferCpltCallback = I2C_DMAXferCplt;
  3655. /* Set the DMA error callback */
  3656. hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
  3657. /* Set the unused DMA callbacks to NULL */
  3658. hi2c->hdmarx->XferHalfCpltCallback = NULL;
  3659. hi2c->hdmarx->XferAbortCallback = NULL;
  3660. /* Enable the DMA channel */
  3661. dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->DR, (uint32_t)hi2c->pBuffPtr, hi2c->XferSize);
  3662. if (dmaxferstatus == HAL_OK)
  3663. {
  3664. /* Enable Address Acknowledge */
  3665. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  3666. /* Clear ADDR flag */
  3667. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  3668. /* Process Unlocked */
  3669. __HAL_UNLOCK(hi2c);
  3670. /* Enable DMA Request */
  3671. SET_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
  3672. /* Note : The I2C interrupts must be enabled after unlocking current process
  3673. to avoid the risk of I2C interrupt handle execution before current
  3674. process unlock */
  3675. /* Enable EVT and ERR interrupt */
  3676. __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR);
  3677. return HAL_OK;
  3678. }
  3679. else
  3680. {
  3681. /* Update I2C state */
  3682. hi2c->State = HAL_I2C_STATE_READY;
  3683. hi2c->Mode = HAL_I2C_MODE_NONE;
  3684. /* Update I2C error code */
  3685. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
  3686. /* Process Unlocked */
  3687. __HAL_UNLOCK(hi2c);
  3688. return HAL_ERROR;
  3689. }
  3690. }
  3691. else
  3692. {
  3693. return HAL_BUSY;
  3694. }
  3695. }
  3696. /**
  3697. * @brief Enable the Address listen mode with Interrupt.
  3698. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3699. * the configuration information for the specified I2C.
  3700. * @retval HAL status
  3701. */
  3702. HAL_StatusTypeDef HAL_I2C_EnableListen_IT(I2C_HandleTypeDef *hi2c)
  3703. {
  3704. if (hi2c->State == HAL_I2C_STATE_READY)
  3705. {
  3706. hi2c->State = HAL_I2C_STATE_LISTEN;
  3707. /* Check if the I2C is already enabled */
  3708. if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
  3709. {
  3710. /* Enable I2C peripheral */
  3711. __HAL_I2C_ENABLE(hi2c);
  3712. }
  3713. /* Enable Address Acknowledge */
  3714. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  3715. /* Enable EVT and ERR interrupt */
  3716. __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR);
  3717. return HAL_OK;
  3718. }
  3719. else
  3720. {
  3721. return HAL_BUSY;
  3722. }
  3723. }
  3724. /**
  3725. * @brief Disable the Address listen mode with Interrupt.
  3726. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3727. * the configuration information for the specified I2C.
  3728. * @retval HAL status
  3729. */
  3730. HAL_StatusTypeDef HAL_I2C_DisableListen_IT(I2C_HandleTypeDef *hi2c)
  3731. {
  3732. /* Declaration of tmp to prevent undefined behavior of volatile usage */
  3733. uint32_t tmp;
  3734. /* Disable Address listen mode only if a transfer is not ongoing */
  3735. if (hi2c->State == HAL_I2C_STATE_LISTEN)
  3736. {
  3737. tmp = (uint32_t)(hi2c->State) & I2C_STATE_MSK;
  3738. hi2c->PreviousState = tmp | (uint32_t)(hi2c->Mode);
  3739. hi2c->State = HAL_I2C_STATE_READY;
  3740. hi2c->Mode = HAL_I2C_MODE_NONE;
  3741. /* Disable Address Acknowledge */
  3742. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  3743. /* Disable EVT and ERR interrupt */
  3744. __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR);
  3745. return HAL_OK;
  3746. }
  3747. else
  3748. {
  3749. return HAL_BUSY;
  3750. }
  3751. }
  3752. /**
  3753. * @brief Abort a master I2C IT or DMA process communication with Interrupt.
  3754. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3755. * the configuration information for the specified I2C.
  3756. * @param DevAddress Target device address: The device 7 bits address value
  3757. * in datasheet must be shifted to the left before calling the interface
  3758. * @retval HAL status
  3759. */
  3760. HAL_StatusTypeDef HAL_I2C_Master_Abort_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress)
  3761. {
  3762. /* Declaration of temporary variables to prevent undefined behavior of volatile usage */
  3763. HAL_I2C_ModeTypeDef CurrentMode = hi2c->Mode;
  3764. /* Prevent unused argument(s) compilation warning */
  3765. UNUSED(DevAddress);
  3766. /* Abort Master transfer during Receive or Transmit process */
  3767. if ((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET) && (CurrentMode == HAL_I2C_MODE_MASTER))
  3768. {
  3769. /* Process Locked */
  3770. __HAL_LOCK(hi2c);
  3771. hi2c->PreviousState = I2C_STATE_NONE;
  3772. hi2c->State = HAL_I2C_STATE_ABORT;
  3773. /* Disable Acknowledge */
  3774. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  3775. /* Generate Stop */
  3776. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  3777. hi2c->XferCount = 0U;
  3778. /* Disable EVT, BUF and ERR interrupt */
  3779. __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  3780. /* Process Unlocked */
  3781. __HAL_UNLOCK(hi2c);
  3782. /* Call the corresponding callback to inform upper layer of End of Transfer */
  3783. I2C_ITError(hi2c);
  3784. return HAL_OK;
  3785. }
  3786. else
  3787. {
  3788. /* Wrong usage of abort function */
  3789. /* This function should be used only in case of abort monitored by master device */
  3790. /* Or periphal is not in busy state, mean there is no active sequence to be abort */
  3791. return HAL_ERROR;
  3792. }
  3793. }
  3794. /**
  3795. * @}
  3796. */
  3797. /** @defgroup I2C_IRQ_Handler_and_Callbacks IRQ Handler and Callbacks
  3798. * @{
  3799. */
  3800. /**
  3801. * @brief This function handles I2C event interrupt request.
  3802. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3803. * the configuration information for the specified I2C.
  3804. * @retval None
  3805. */
  3806. void HAL_I2C_EV_IRQHandler(I2C_HandleTypeDef *hi2c)
  3807. {
  3808. uint32_t sr1itflags;
  3809. uint32_t sr2itflags = 0U;
  3810. uint32_t itsources = READ_REG(hi2c->Instance->CR2);
  3811. uint32_t CurrentXferOptions = hi2c->XferOptions;
  3812. HAL_I2C_ModeTypeDef CurrentMode = hi2c->Mode;
  3813. HAL_I2C_StateTypeDef CurrentState = hi2c->State;
  3814. /* Master or Memory mode selected */
  3815. if ((CurrentMode == HAL_I2C_MODE_MASTER) || (CurrentMode == HAL_I2C_MODE_MEM))
  3816. {
  3817. sr2itflags = READ_REG(hi2c->Instance->SR2);
  3818. sr1itflags = READ_REG(hi2c->Instance->SR1);
  3819. /* Exit IRQ event until Start Bit detected in case of Other frame requested */
  3820. if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_SB) == RESET) && (IS_I2C_TRANSFER_OTHER_OPTIONS_REQUEST(CurrentXferOptions) == 1U))
  3821. {
  3822. return;
  3823. }
  3824. /* SB Set ----------------------------------------------------------------*/
  3825. if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_SB) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_EVT) != RESET))
  3826. {
  3827. /* Convert OTHER_xxx XferOptions if any */
  3828. I2C_ConvertOtherXferOptions(hi2c);
  3829. I2C_Master_SB(hi2c);
  3830. }
  3831. /* ADD10 Set -------------------------------------------------------------*/
  3832. else if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_ADD10) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_EVT) != RESET))
  3833. {
  3834. I2C_Master_ADD10(hi2c);
  3835. }
  3836. /* ADDR Set --------------------------------------------------------------*/
  3837. else if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_ADDR) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_EVT) != RESET))
  3838. {
  3839. I2C_Master_ADDR(hi2c);
  3840. }
  3841. /* I2C in mode Transmitter -----------------------------------------------*/
  3842. else if (I2C_CHECK_FLAG(sr2itflags, I2C_FLAG_TRA) != RESET)
  3843. {
  3844. /* Do not check buffer and BTF flag if a Xfer DMA is on going */
  3845. if (READ_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN) != I2C_CR2_DMAEN)
  3846. {
  3847. /* TXE set and BTF reset -----------------------------------------------*/
  3848. 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))
  3849. {
  3850. I2C_MasterTransmit_TXE(hi2c);
  3851. }
  3852. /* BTF set -------------------------------------------------------------*/
  3853. else if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_BTF) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_EVT) != RESET))
  3854. {
  3855. if (CurrentMode == HAL_I2C_MODE_MASTER)
  3856. {
  3857. I2C_MasterTransmit_BTF(hi2c);
  3858. }
  3859. else /* HAL_I2C_MODE_MEM */
  3860. {
  3861. I2C_MemoryTransmit_TXE_BTF(hi2c);
  3862. }
  3863. }
  3864. else
  3865. {
  3866. /* Do nothing */
  3867. }
  3868. }
  3869. }
  3870. /* I2C in mode Receiver --------------------------------------------------*/
  3871. else
  3872. {
  3873. /* Do not check buffer and BTF flag if a Xfer DMA is on going */
  3874. if (READ_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN) != I2C_CR2_DMAEN)
  3875. {
  3876. /* RXNE set and BTF reset -----------------------------------------------*/
  3877. 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))
  3878. {
  3879. I2C_MasterReceive_RXNE(hi2c);
  3880. }
  3881. /* BTF set -------------------------------------------------------------*/
  3882. else if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_BTF) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_EVT) != RESET))
  3883. {
  3884. I2C_MasterReceive_BTF(hi2c);
  3885. }
  3886. else
  3887. {
  3888. /* Do nothing */
  3889. }
  3890. }
  3891. }
  3892. }
  3893. /* Slave mode selected */
  3894. else
  3895. {
  3896. /* If an error is detected, read only SR1 register to prevent */
  3897. /* a clear of ADDR flags by reading SR2 after reading SR1 in Error treatment */
  3898. if (hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
  3899. {
  3900. sr1itflags = READ_REG(hi2c->Instance->SR1);
  3901. }
  3902. else
  3903. {
  3904. sr2itflags = READ_REG(hi2c->Instance->SR2);
  3905. sr1itflags = READ_REG(hi2c->Instance->SR1);
  3906. }
  3907. /* ADDR set --------------------------------------------------------------*/
  3908. if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_ADDR) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_EVT) != RESET))
  3909. {
  3910. /* Now time to read SR2, this will clear ADDR flag automatically */
  3911. if (hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
  3912. {
  3913. sr2itflags = READ_REG(hi2c->Instance->SR2);
  3914. }
  3915. I2C_Slave_ADDR(hi2c, sr2itflags);
  3916. }
  3917. /* STOPF set --------------------------------------------------------------*/
  3918. else if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_STOPF) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_EVT) != RESET))
  3919. {
  3920. I2C_Slave_STOPF(hi2c);
  3921. }
  3922. /* I2C in mode Transmitter -----------------------------------------------*/
  3923. else if ((CurrentState == HAL_I2C_STATE_BUSY_TX) || (CurrentState == HAL_I2C_STATE_BUSY_TX_LISTEN))
  3924. {
  3925. /* TXE set and BTF reset -----------------------------------------------*/
  3926. 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))
  3927. {
  3928. I2C_SlaveTransmit_TXE(hi2c);
  3929. }
  3930. /* BTF set -------------------------------------------------------------*/
  3931. else if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_BTF) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_EVT) != RESET))
  3932. {
  3933. I2C_SlaveTransmit_BTF(hi2c);
  3934. }
  3935. else
  3936. {
  3937. /* Do nothing */
  3938. }
  3939. }
  3940. /* I2C in mode Receiver --------------------------------------------------*/
  3941. else
  3942. {
  3943. /* RXNE set and BTF reset ----------------------------------------------*/
  3944. 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))
  3945. {
  3946. I2C_SlaveReceive_RXNE(hi2c);
  3947. }
  3948. /* BTF set -------------------------------------------------------------*/
  3949. else if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_BTF) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_EVT) != RESET))
  3950. {
  3951. I2C_SlaveReceive_BTF(hi2c);
  3952. }
  3953. else
  3954. {
  3955. /* Do nothing */
  3956. }
  3957. }
  3958. }
  3959. }
  3960. /**
  3961. * @brief This function handles I2C error interrupt request.
  3962. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  3963. * the configuration information for the specified I2C.
  3964. * @retval None
  3965. */
  3966. void HAL_I2C_ER_IRQHandler(I2C_HandleTypeDef *hi2c)
  3967. {
  3968. HAL_I2C_ModeTypeDef tmp1;
  3969. uint32_t tmp2;
  3970. HAL_I2C_StateTypeDef tmp3;
  3971. uint32_t tmp4;
  3972. uint32_t sr1itflags = READ_REG(hi2c->Instance->SR1);
  3973. uint32_t itsources = READ_REG(hi2c->Instance->CR2);
  3974. uint32_t error = HAL_I2C_ERROR_NONE;
  3975. HAL_I2C_ModeTypeDef CurrentMode = hi2c->Mode;
  3976. /* I2C Bus error interrupt occurred ----------------------------------------*/
  3977. if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_BERR) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_ERR) != RESET))
  3978. {
  3979. error |= HAL_I2C_ERROR_BERR;
  3980. /* Clear BERR flag */
  3981. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_BERR);
  3982. }
  3983. /* I2C Arbitration Lost error interrupt occurred ---------------------------*/
  3984. if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_ARLO) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_ERR) != RESET))
  3985. {
  3986. error |= HAL_I2C_ERROR_ARLO;
  3987. /* Clear ARLO flag */
  3988. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ARLO);
  3989. }
  3990. /* I2C Acknowledge failure error interrupt occurred ------------------------*/
  3991. if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_AF) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_ERR) != RESET))
  3992. {
  3993. tmp1 = CurrentMode;
  3994. tmp2 = hi2c->XferCount;
  3995. tmp3 = hi2c->State;
  3996. tmp4 = hi2c->PreviousState;
  3997. if ((tmp1 == HAL_I2C_MODE_SLAVE) && (tmp2 == 0U) && \
  3998. ((tmp3 == HAL_I2C_STATE_BUSY_TX) || (tmp3 == HAL_I2C_STATE_BUSY_TX_LISTEN) || \
  3999. ((tmp3 == HAL_I2C_STATE_LISTEN) && (tmp4 == I2C_STATE_SLAVE_BUSY_TX))))
  4000. {
  4001. I2C_Slave_AF(hi2c);
  4002. }
  4003. else
  4004. {
  4005. /* Clear AF flag */
  4006. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  4007. error |= HAL_I2C_ERROR_AF;
  4008. /* Do not generate a STOP in case of Slave receive non acknowledge during transfer (mean not at the end of transfer) */
  4009. if ((CurrentMode == HAL_I2C_MODE_MASTER) || (CurrentMode == HAL_I2C_MODE_MEM))
  4010. {
  4011. /* Generate Stop */
  4012. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  4013. }
  4014. }
  4015. }
  4016. /* I2C Over-Run/Under-Run interrupt occurred -------------------------------*/
  4017. if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_OVR) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_ERR) != RESET))
  4018. {
  4019. error |= HAL_I2C_ERROR_OVR;
  4020. /* Clear OVR flag */
  4021. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_OVR);
  4022. }
  4023. /* Call the Error Callback in case of Error detected -----------------------*/
  4024. if (error != HAL_I2C_ERROR_NONE)
  4025. {
  4026. hi2c->ErrorCode |= error;
  4027. I2C_ITError(hi2c);
  4028. }
  4029. }
  4030. /**
  4031. * @brief Master Tx Transfer completed callback.
  4032. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4033. * the configuration information for the specified I2C.
  4034. * @retval None
  4035. */
  4036. __weak void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef *hi2c)
  4037. {
  4038. /* Prevent unused argument(s) compilation warning */
  4039. UNUSED(hi2c);
  4040. /* NOTE : This function should not be modified, when the callback is needed,
  4041. the HAL_I2C_MasterTxCpltCallback could be implemented in the user file
  4042. */
  4043. }
  4044. /**
  4045. * @brief Master Rx Transfer completed callback.
  4046. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4047. * the configuration information for the specified I2C.
  4048. * @retval None
  4049. */
  4050. __weak void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *hi2c)
  4051. {
  4052. /* Prevent unused argument(s) compilation warning */
  4053. UNUSED(hi2c);
  4054. /* NOTE : This function should not be modified, when the callback is needed,
  4055. the HAL_I2C_MasterRxCpltCallback could be implemented in the user file
  4056. */
  4057. }
  4058. /** @brief Slave Tx Transfer completed callback.
  4059. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4060. * the configuration information for the specified I2C.
  4061. * @retval None
  4062. */
  4063. __weak void HAL_I2C_SlaveTxCpltCallback(I2C_HandleTypeDef *hi2c)
  4064. {
  4065. /* Prevent unused argument(s) compilation warning */
  4066. UNUSED(hi2c);
  4067. /* NOTE : This function should not be modified, when the callback is needed,
  4068. the HAL_I2C_SlaveTxCpltCallback could be implemented in the user file
  4069. */
  4070. }
  4071. /**
  4072. * @brief Slave Rx Transfer completed callback.
  4073. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4074. * the configuration information for the specified I2C.
  4075. * @retval None
  4076. */
  4077. __weak void HAL_I2C_SlaveRxCpltCallback(I2C_HandleTypeDef *hi2c)
  4078. {
  4079. /* Prevent unused argument(s) compilation warning */
  4080. UNUSED(hi2c);
  4081. /* NOTE : This function should not be modified, when the callback is needed,
  4082. the HAL_I2C_SlaveRxCpltCallback could be implemented in the user file
  4083. */
  4084. }
  4085. /**
  4086. * @brief Slave Address Match callback.
  4087. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4088. * the configuration information for the specified I2C.
  4089. * @param TransferDirection Master request Transfer Direction (Write/Read), value of @ref I2C_XferDirection_definition
  4090. * @param AddrMatchCode Address Match Code
  4091. * @retval None
  4092. */
  4093. __weak void HAL_I2C_AddrCallback(I2C_HandleTypeDef *hi2c, uint8_t TransferDirection, uint16_t AddrMatchCode)
  4094. {
  4095. /* Prevent unused argument(s) compilation warning */
  4096. UNUSED(hi2c);
  4097. UNUSED(TransferDirection);
  4098. UNUSED(AddrMatchCode);
  4099. /* NOTE : This function should not be modified, when the callback is needed,
  4100. the HAL_I2C_AddrCallback() could be implemented in the user file
  4101. */
  4102. }
  4103. /**
  4104. * @brief Listen Complete callback.
  4105. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4106. * the configuration information for the specified I2C.
  4107. * @retval None
  4108. */
  4109. __weak void HAL_I2C_ListenCpltCallback(I2C_HandleTypeDef *hi2c)
  4110. {
  4111. /* Prevent unused argument(s) compilation warning */
  4112. UNUSED(hi2c);
  4113. /* NOTE : This function should not be modified, when the callback is needed,
  4114. the HAL_I2C_ListenCpltCallback() could be implemented in the user file
  4115. */
  4116. }
  4117. /**
  4118. * @brief Memory Tx Transfer completed callback.
  4119. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4120. * the configuration information for the specified I2C.
  4121. * @retval None
  4122. */
  4123. __weak void HAL_I2C_MemTxCpltCallback(I2C_HandleTypeDef *hi2c)
  4124. {
  4125. /* Prevent unused argument(s) compilation warning */
  4126. UNUSED(hi2c);
  4127. /* NOTE : This function should not be modified, when the callback is needed,
  4128. the HAL_I2C_MemTxCpltCallback could be implemented in the user file
  4129. */
  4130. }
  4131. /**
  4132. * @brief Memory Rx Transfer completed callback.
  4133. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4134. * the configuration information for the specified I2C.
  4135. * @retval None
  4136. */
  4137. __weak void HAL_I2C_MemRxCpltCallback(I2C_HandleTypeDef *hi2c)
  4138. {
  4139. /* Prevent unused argument(s) compilation warning */
  4140. UNUSED(hi2c);
  4141. /* NOTE : This function should not be modified, when the callback is needed,
  4142. the HAL_I2C_MemRxCpltCallback could be implemented in the user file
  4143. */
  4144. }
  4145. /**
  4146. * @brief I2C error callback.
  4147. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4148. * the configuration information for the specified I2C.
  4149. * @retval None
  4150. */
  4151. __weak void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c)
  4152. {
  4153. /* Prevent unused argument(s) compilation warning */
  4154. UNUSED(hi2c);
  4155. /* NOTE : This function should not be modified, when the callback is needed,
  4156. the HAL_I2C_ErrorCallback could be implemented in the user file
  4157. */
  4158. }
  4159. /**
  4160. * @brief I2C abort callback.
  4161. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4162. * the configuration information for the specified I2C.
  4163. * @retval None
  4164. */
  4165. __weak void HAL_I2C_AbortCpltCallback(I2C_HandleTypeDef *hi2c)
  4166. {
  4167. /* Prevent unused argument(s) compilation warning */
  4168. UNUSED(hi2c);
  4169. /* NOTE : This function should not be modified, when the callback is needed,
  4170. the HAL_I2C_AbortCpltCallback could be implemented in the user file
  4171. */
  4172. }
  4173. /**
  4174. * @}
  4175. */
  4176. /** @defgroup I2C_Exported_Functions_Group3 Peripheral State, Mode and Error functions
  4177. * @brief Peripheral State, Mode and Error functions
  4178. *
  4179. @verbatim
  4180. ===============================================================================
  4181. ##### Peripheral State, Mode and Error functions #####
  4182. ===============================================================================
  4183. [..]
  4184. This subsection permit to get in run-time the status of the peripheral
  4185. and the data flow.
  4186. @endverbatim
  4187. * @{
  4188. */
  4189. /**
  4190. * @brief Return the I2C handle state.
  4191. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4192. * the configuration information for the specified I2C.
  4193. * @retval HAL state
  4194. */
  4195. HAL_I2C_StateTypeDef HAL_I2C_GetState(I2C_HandleTypeDef *hi2c)
  4196. {
  4197. /* Return I2C handle state */
  4198. return hi2c->State;
  4199. }
  4200. /**
  4201. * @brief Returns the I2C Master, Slave, Memory or no mode.
  4202. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4203. * the configuration information for I2C module
  4204. * @retval HAL mode
  4205. */
  4206. HAL_I2C_ModeTypeDef HAL_I2C_GetMode(I2C_HandleTypeDef *hi2c)
  4207. {
  4208. return hi2c->Mode;
  4209. }
  4210. /**
  4211. * @brief Return the I2C error code.
  4212. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4213. * the configuration information for the specified I2C.
  4214. * @retval I2C Error Code
  4215. */
  4216. uint32_t HAL_I2C_GetError(I2C_HandleTypeDef *hi2c)
  4217. {
  4218. return hi2c->ErrorCode;
  4219. }
  4220. /**
  4221. * @}
  4222. */
  4223. /**
  4224. * @}
  4225. */
  4226. /** @addtogroup I2C_Private_Functions
  4227. * @{
  4228. */
  4229. /**
  4230. * @brief Handle TXE flag for Master
  4231. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4232. * the configuration information for I2C module
  4233. * @retval None
  4234. */
  4235. static void I2C_MasterTransmit_TXE(I2C_HandleTypeDef *hi2c)
  4236. {
  4237. /* Declaration of temporary variables to prevent undefined behavior of volatile usage */
  4238. HAL_I2C_StateTypeDef CurrentState = hi2c->State;
  4239. HAL_I2C_ModeTypeDef CurrentMode = hi2c->Mode;
  4240. uint32_t CurrentXferOptions = hi2c->XferOptions;
  4241. if ((hi2c->XferSize == 0U) && (CurrentState == HAL_I2C_STATE_BUSY_TX))
  4242. {
  4243. /* Call TxCpltCallback() directly if no stop mode is set */
  4244. if ((CurrentXferOptions != I2C_FIRST_AND_LAST_FRAME) && (CurrentXferOptions != I2C_LAST_FRAME) && (CurrentXferOptions != I2C_NO_OPTION_FRAME))
  4245. {
  4246. __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  4247. hi2c->PreviousState = I2C_STATE_MASTER_BUSY_TX;
  4248. hi2c->Mode = HAL_I2C_MODE_NONE;
  4249. hi2c->State = HAL_I2C_STATE_READY;
  4250. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  4251. hi2c->MasterTxCpltCallback(hi2c);
  4252. #else
  4253. HAL_I2C_MasterTxCpltCallback(hi2c);
  4254. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  4255. }
  4256. else /* Generate Stop condition then Call TxCpltCallback() */
  4257. {
  4258. /* Disable EVT, BUF and ERR interrupt */
  4259. __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  4260. /* Generate Stop */
  4261. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  4262. hi2c->PreviousState = I2C_STATE_NONE;
  4263. hi2c->State = HAL_I2C_STATE_READY;
  4264. if (hi2c->Mode == HAL_I2C_MODE_MEM)
  4265. {
  4266. hi2c->Mode = HAL_I2C_MODE_NONE;
  4267. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  4268. hi2c->MemTxCpltCallback(hi2c);
  4269. #else
  4270. HAL_I2C_MemTxCpltCallback(hi2c);
  4271. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  4272. }
  4273. else
  4274. {
  4275. hi2c->Mode = HAL_I2C_MODE_NONE;
  4276. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  4277. hi2c->MasterTxCpltCallback(hi2c);
  4278. #else
  4279. HAL_I2C_MasterTxCpltCallback(hi2c);
  4280. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  4281. }
  4282. }
  4283. }
  4284. else if ((CurrentState == HAL_I2C_STATE_BUSY_TX) || \
  4285. ((CurrentMode == HAL_I2C_MODE_MEM) && (CurrentState == HAL_I2C_STATE_BUSY_RX)))
  4286. {
  4287. if (hi2c->XferCount == 0U)
  4288. {
  4289. /* Disable BUF interrupt */
  4290. __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_BUF);
  4291. }
  4292. else
  4293. {
  4294. if (hi2c->Mode == HAL_I2C_MODE_MEM)
  4295. {
  4296. I2C_MemoryTransmit_TXE_BTF(hi2c);
  4297. }
  4298. else
  4299. {
  4300. /* Write data to DR */
  4301. hi2c->Instance->DR = *hi2c->pBuffPtr;
  4302. /* Increment Buffer pointer */
  4303. hi2c->pBuffPtr++;
  4304. /* Update counter */
  4305. hi2c->XferCount--;
  4306. }
  4307. }
  4308. }
  4309. else
  4310. {
  4311. /* Do nothing */
  4312. }
  4313. }
  4314. /**
  4315. * @brief Handle BTF flag for Master transmitter
  4316. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4317. * the configuration information for I2C module
  4318. * @retval None
  4319. */
  4320. static void I2C_MasterTransmit_BTF(I2C_HandleTypeDef *hi2c)
  4321. {
  4322. /* Declaration of temporary variables to prevent undefined behavior of volatile usage */
  4323. uint32_t CurrentXferOptions = hi2c->XferOptions;
  4324. if (hi2c->State == HAL_I2C_STATE_BUSY_TX)
  4325. {
  4326. if (hi2c->XferCount != 0U)
  4327. {
  4328. /* Write data to DR */
  4329. hi2c->Instance->DR = *hi2c->pBuffPtr;
  4330. /* Increment Buffer pointer */
  4331. hi2c->pBuffPtr++;
  4332. /* Update counter */
  4333. hi2c->XferCount--;
  4334. }
  4335. else
  4336. {
  4337. /* Call TxCpltCallback() directly if no stop mode is set */
  4338. if ((CurrentXferOptions != I2C_FIRST_AND_LAST_FRAME) && (CurrentXferOptions != I2C_LAST_FRAME) && (CurrentXferOptions != I2C_NO_OPTION_FRAME))
  4339. {
  4340. __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  4341. hi2c->PreviousState = I2C_STATE_MASTER_BUSY_TX;
  4342. hi2c->Mode = HAL_I2C_MODE_NONE;
  4343. hi2c->State = HAL_I2C_STATE_READY;
  4344. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  4345. hi2c->MasterTxCpltCallback(hi2c);
  4346. #else
  4347. HAL_I2C_MasterTxCpltCallback(hi2c);
  4348. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  4349. }
  4350. else /* Generate Stop condition then Call TxCpltCallback() */
  4351. {
  4352. /* Disable EVT, BUF and ERR interrupt */
  4353. __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  4354. /* Generate Stop */
  4355. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  4356. hi2c->PreviousState = I2C_STATE_NONE;
  4357. hi2c->State = HAL_I2C_STATE_READY;
  4358. hi2c->Mode = HAL_I2C_MODE_NONE;
  4359. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  4360. hi2c->MasterTxCpltCallback(hi2c);
  4361. #else
  4362. HAL_I2C_MasterTxCpltCallback(hi2c);
  4363. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  4364. }
  4365. }
  4366. }
  4367. else
  4368. {
  4369. /* Do nothing */
  4370. }
  4371. }
  4372. /**
  4373. * @brief Handle TXE and BTF flag for Memory transmitter
  4374. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4375. * the configuration information for I2C module
  4376. * @retval None
  4377. */
  4378. static void I2C_MemoryTransmit_TXE_BTF(I2C_HandleTypeDef *hi2c)
  4379. {
  4380. /* Declaration of temporary variables to prevent undefined behavior of volatile usage */
  4381. HAL_I2C_StateTypeDef CurrentState = hi2c->State;
  4382. if (hi2c->EventCount == 0U)
  4383. {
  4384. /* If Memory address size is 8Bit */
  4385. if (hi2c->MemaddSize == I2C_MEMADD_SIZE_8BIT)
  4386. {
  4387. /* Send Memory Address */
  4388. hi2c->Instance->DR = I2C_MEM_ADD_LSB(hi2c->Memaddress);
  4389. hi2c->EventCount += 2U;
  4390. }
  4391. /* If Memory address size is 16Bit */
  4392. else
  4393. {
  4394. /* Send MSB of Memory Address */
  4395. hi2c->Instance->DR = I2C_MEM_ADD_MSB(hi2c->Memaddress);
  4396. hi2c->EventCount++;
  4397. }
  4398. }
  4399. else if (hi2c->EventCount == 1U)
  4400. {
  4401. /* Send LSB of Memory Address */
  4402. hi2c->Instance->DR = I2C_MEM_ADD_LSB(hi2c->Memaddress);
  4403. hi2c->EventCount++;
  4404. }
  4405. else if (hi2c->EventCount == 2U)
  4406. {
  4407. if (CurrentState == HAL_I2C_STATE_BUSY_RX)
  4408. {
  4409. /* Generate Restart */
  4410. hi2c->Instance->CR1 |= I2C_CR1_START;
  4411. }
  4412. else if ((hi2c->XferCount > 0U) && (CurrentState == HAL_I2C_STATE_BUSY_TX))
  4413. {
  4414. /* Write data to DR */
  4415. hi2c->Instance->DR = *hi2c->pBuffPtr;
  4416. /* Increment Buffer pointer */
  4417. hi2c->pBuffPtr++;
  4418. /* Update counter */
  4419. hi2c->XferCount--;
  4420. }
  4421. else if ((hi2c->XferCount == 0U) && (CurrentState == HAL_I2C_STATE_BUSY_TX))
  4422. {
  4423. /* Generate Stop condition then Call TxCpltCallback() */
  4424. /* Disable EVT, BUF and ERR interrupt */
  4425. __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  4426. /* Generate Stop */
  4427. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  4428. hi2c->PreviousState = I2C_STATE_NONE;
  4429. hi2c->State = HAL_I2C_STATE_READY;
  4430. hi2c->Mode = HAL_I2C_MODE_NONE;
  4431. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  4432. hi2c->MemTxCpltCallback(hi2c);
  4433. #else
  4434. HAL_I2C_MemTxCpltCallback(hi2c);
  4435. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  4436. }
  4437. else
  4438. {
  4439. /* Do nothing */
  4440. }
  4441. }
  4442. else
  4443. {
  4444. /* Do nothing */
  4445. }
  4446. }
  4447. /**
  4448. * @brief Handle RXNE flag for Master
  4449. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4450. * the configuration information for I2C module
  4451. * @retval None
  4452. */
  4453. static void I2C_MasterReceive_RXNE(I2C_HandleTypeDef *hi2c)
  4454. {
  4455. if (hi2c->State == HAL_I2C_STATE_BUSY_RX)
  4456. {
  4457. uint32_t tmp;
  4458. tmp = hi2c->XferCount;
  4459. if (tmp > 3U)
  4460. {
  4461. /* Read data from DR */
  4462. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
  4463. /* Increment Buffer pointer */
  4464. hi2c->pBuffPtr++;
  4465. /* Update counter */
  4466. hi2c->XferCount--;
  4467. if (hi2c->XferCount == (uint16_t)3)
  4468. {
  4469. /* Disable BUF interrupt, this help to treat correctly the last 4 bytes
  4470. on BTF subroutine */
  4471. /* Disable BUF interrupt */
  4472. __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_BUF);
  4473. }
  4474. }
  4475. else if ((hi2c->XferOptions != I2C_FIRST_AND_NEXT_FRAME) && ((tmp == 1U) || (tmp == 0U)))
  4476. {
  4477. if (I2C_WaitOnSTOPRequestThroughIT(hi2c) == HAL_OK)
  4478. {
  4479. /* Disable Acknowledge */
  4480. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  4481. /* Disable EVT, BUF and ERR interrupt */
  4482. __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  4483. /* Read data from DR */
  4484. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
  4485. /* Increment Buffer pointer */
  4486. hi2c->pBuffPtr++;
  4487. /* Update counter */
  4488. hi2c->XferCount--;
  4489. hi2c->State = HAL_I2C_STATE_READY;
  4490. if (hi2c->Mode == HAL_I2C_MODE_MEM)
  4491. {
  4492. hi2c->Mode = HAL_I2C_MODE_NONE;
  4493. hi2c->PreviousState = I2C_STATE_NONE;
  4494. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  4495. hi2c->MemRxCpltCallback(hi2c);
  4496. #else
  4497. HAL_I2C_MemRxCpltCallback(hi2c);
  4498. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  4499. }
  4500. else
  4501. {
  4502. hi2c->Mode = HAL_I2C_MODE_NONE;
  4503. hi2c->PreviousState = I2C_STATE_MASTER_BUSY_RX;
  4504. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  4505. hi2c->MasterRxCpltCallback(hi2c);
  4506. #else
  4507. HAL_I2C_MasterRxCpltCallback(hi2c);
  4508. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  4509. }
  4510. }
  4511. else
  4512. {
  4513. /* Disable EVT, BUF and ERR interrupt */
  4514. __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  4515. /* Read data from DR */
  4516. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
  4517. /* Increment Buffer pointer */
  4518. hi2c->pBuffPtr++;
  4519. /* Update counter */
  4520. hi2c->XferCount--;
  4521. hi2c->State = HAL_I2C_STATE_READY;
  4522. hi2c->Mode = HAL_I2C_MODE_NONE;
  4523. /* Call user error callback */
  4524. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  4525. hi2c->ErrorCallback(hi2c);
  4526. #else
  4527. HAL_I2C_ErrorCallback(hi2c);
  4528. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  4529. }
  4530. }
  4531. else
  4532. {
  4533. /* Do nothing */
  4534. }
  4535. }
  4536. }
  4537. /**
  4538. * @brief Handle BTF flag for Master receiver
  4539. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4540. * the configuration information for I2C module
  4541. * @retval None
  4542. */
  4543. static void I2C_MasterReceive_BTF(I2C_HandleTypeDef *hi2c)
  4544. {
  4545. /* Declaration of temporary variables to prevent undefined behavior of volatile usage */
  4546. uint32_t CurrentXferOptions = hi2c->XferOptions;
  4547. if (hi2c->XferCount == 4U)
  4548. {
  4549. /* Disable BUF interrupt, this help to treat correctly the last 2 bytes
  4550. on BTF subroutine if there is a reception delay between N-1 and N byte */
  4551. __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_BUF);
  4552. /* Read data from DR */
  4553. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
  4554. /* Increment Buffer pointer */
  4555. hi2c->pBuffPtr++;
  4556. /* Update counter */
  4557. hi2c->XferCount--;
  4558. }
  4559. else if (hi2c->XferCount == 3U)
  4560. {
  4561. /* Disable BUF interrupt, this help to treat correctly the last 2 bytes
  4562. on BTF subroutine if there is a reception delay between N-1 and N byte */
  4563. __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_BUF);
  4564. if ((CurrentXferOptions != I2C_NEXT_FRAME) && (CurrentXferOptions != I2C_FIRST_AND_NEXT_FRAME))
  4565. {
  4566. /* Disable Acknowledge */
  4567. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  4568. }
  4569. /* Read data from DR */
  4570. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
  4571. /* Increment Buffer pointer */
  4572. hi2c->pBuffPtr++;
  4573. /* Update counter */
  4574. hi2c->XferCount--;
  4575. }
  4576. else if (hi2c->XferCount == 2U)
  4577. {
  4578. /* Prepare next transfer or stop current transfer */
  4579. if ((CurrentXferOptions == I2C_FIRST_FRAME) || (CurrentXferOptions == I2C_LAST_FRAME_NO_STOP))
  4580. {
  4581. /* Disable Acknowledge */
  4582. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  4583. }
  4584. else if ((CurrentXferOptions == I2C_NEXT_FRAME) || (CurrentXferOptions == I2C_FIRST_AND_NEXT_FRAME))
  4585. {
  4586. /* Enable Acknowledge */
  4587. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  4588. }
  4589. else if (CurrentXferOptions != I2C_LAST_FRAME_NO_STOP)
  4590. {
  4591. /* Generate Stop */
  4592. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  4593. }
  4594. else
  4595. {
  4596. /* Do nothing */
  4597. }
  4598. /* Read data from DR */
  4599. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
  4600. /* Increment Buffer pointer */
  4601. hi2c->pBuffPtr++;
  4602. /* Update counter */
  4603. hi2c->XferCount--;
  4604. /* Read data from DR */
  4605. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
  4606. /* Increment Buffer pointer */
  4607. hi2c->pBuffPtr++;
  4608. /* Update counter */
  4609. hi2c->XferCount--;
  4610. /* Disable EVT and ERR interrupt */
  4611. __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR);
  4612. hi2c->State = HAL_I2C_STATE_READY;
  4613. if (hi2c->Mode == HAL_I2C_MODE_MEM)
  4614. {
  4615. hi2c->Mode = HAL_I2C_MODE_NONE;
  4616. hi2c->PreviousState = I2C_STATE_NONE;
  4617. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  4618. hi2c->MemRxCpltCallback(hi2c);
  4619. #else
  4620. HAL_I2C_MemRxCpltCallback(hi2c);
  4621. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  4622. }
  4623. else
  4624. {
  4625. hi2c->Mode = HAL_I2C_MODE_NONE;
  4626. hi2c->PreviousState = I2C_STATE_MASTER_BUSY_RX;
  4627. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  4628. hi2c->MasterRxCpltCallback(hi2c);
  4629. #else
  4630. HAL_I2C_MasterRxCpltCallback(hi2c);
  4631. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  4632. }
  4633. }
  4634. else
  4635. {
  4636. /* Read data from DR */
  4637. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
  4638. /* Increment Buffer pointer */
  4639. hi2c->pBuffPtr++;
  4640. /* Update counter */
  4641. hi2c->XferCount--;
  4642. }
  4643. }
  4644. /**
  4645. * @brief Handle SB flag for Master
  4646. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4647. * the configuration information for I2C module
  4648. * @retval None
  4649. */
  4650. static void I2C_Master_SB(I2C_HandleTypeDef *hi2c)
  4651. {
  4652. if (hi2c->Mode == HAL_I2C_MODE_MEM)
  4653. {
  4654. if (hi2c->EventCount == 0U)
  4655. {
  4656. /* Send slave address */
  4657. hi2c->Instance->DR = I2C_7BIT_ADD_WRITE(hi2c->Devaddress);
  4658. }
  4659. else
  4660. {
  4661. hi2c->Instance->DR = I2C_7BIT_ADD_READ(hi2c->Devaddress);
  4662. }
  4663. }
  4664. else
  4665. {
  4666. if (hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_7BIT)
  4667. {
  4668. /* Send slave 7 Bits address */
  4669. if (hi2c->State == HAL_I2C_STATE_BUSY_TX)
  4670. {
  4671. hi2c->Instance->DR = I2C_7BIT_ADD_WRITE(hi2c->Devaddress);
  4672. }
  4673. else
  4674. {
  4675. hi2c->Instance->DR = I2C_7BIT_ADD_READ(hi2c->Devaddress);
  4676. }
  4677. if (((hi2c->hdmatx != NULL) && (hi2c->hdmatx->XferCpltCallback != NULL))
  4678. || ((hi2c->hdmarx != NULL) && (hi2c->hdmarx->XferCpltCallback != NULL)))
  4679. {
  4680. /* Enable DMA Request */
  4681. SET_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
  4682. }
  4683. }
  4684. else
  4685. {
  4686. if (hi2c->EventCount == 0U)
  4687. {
  4688. /* Send header of slave address */
  4689. hi2c->Instance->DR = I2C_10BIT_HEADER_WRITE(hi2c->Devaddress);
  4690. }
  4691. else if (hi2c->EventCount == 1U)
  4692. {
  4693. /* Send header of slave address */
  4694. hi2c->Instance->DR = I2C_10BIT_HEADER_READ(hi2c->Devaddress);
  4695. }
  4696. else
  4697. {
  4698. /* Do nothing */
  4699. }
  4700. }
  4701. }
  4702. }
  4703. /**
  4704. * @brief Handle ADD10 flag for Master
  4705. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4706. * the configuration information for I2C module
  4707. * @retval None
  4708. */
  4709. static void I2C_Master_ADD10(I2C_HandleTypeDef *hi2c)
  4710. {
  4711. /* Send slave address */
  4712. hi2c->Instance->DR = I2C_10BIT_ADDRESS(hi2c->Devaddress);
  4713. if ((hi2c->hdmatx != NULL) || (hi2c->hdmarx != NULL))
  4714. {
  4715. if ((hi2c->hdmatx->XferCpltCallback != NULL) || (hi2c->hdmarx->XferCpltCallback != NULL))
  4716. {
  4717. /* Enable DMA Request */
  4718. SET_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
  4719. }
  4720. }
  4721. }
  4722. /**
  4723. * @brief Handle ADDR flag for Master
  4724. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4725. * the configuration information for I2C module
  4726. * @retval None
  4727. */
  4728. static void I2C_Master_ADDR(I2C_HandleTypeDef *hi2c)
  4729. {
  4730. /* Declaration of temporary variable to prevent undefined behavior of volatile usage */
  4731. HAL_I2C_ModeTypeDef CurrentMode = hi2c->Mode;
  4732. uint32_t CurrentXferOptions = hi2c->XferOptions;
  4733. uint32_t Prev_State = hi2c->PreviousState;
  4734. if (hi2c->State == HAL_I2C_STATE_BUSY_RX)
  4735. {
  4736. if ((hi2c->EventCount == 0U) && (CurrentMode == HAL_I2C_MODE_MEM))
  4737. {
  4738. /* Clear ADDR flag */
  4739. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  4740. }
  4741. else if ((hi2c->EventCount == 0U) && (hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_10BIT))
  4742. {
  4743. /* Clear ADDR flag */
  4744. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  4745. /* Generate Restart */
  4746. SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
  4747. hi2c->EventCount++;
  4748. }
  4749. else
  4750. {
  4751. if (hi2c->XferCount == 0U)
  4752. {
  4753. /* Clear ADDR flag */
  4754. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  4755. /* Generate Stop */
  4756. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  4757. }
  4758. else if (hi2c->XferCount == 1U)
  4759. {
  4760. if (CurrentXferOptions == I2C_NO_OPTION_FRAME)
  4761. {
  4762. /* Disable Acknowledge */
  4763. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  4764. if ((hi2c->Instance->CR2 & I2C_CR2_DMAEN) == I2C_CR2_DMAEN)
  4765. {
  4766. /* Disable Acknowledge */
  4767. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  4768. /* Clear ADDR flag */
  4769. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  4770. }
  4771. else
  4772. {
  4773. /* Clear ADDR flag */
  4774. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  4775. /* Generate Stop */
  4776. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  4777. }
  4778. }
  4779. /* Prepare next transfer or stop current transfer */
  4780. else if ((CurrentXferOptions != I2C_FIRST_AND_LAST_FRAME) && (CurrentXferOptions != I2C_LAST_FRAME) \
  4781. && ((Prev_State != I2C_STATE_MASTER_BUSY_RX) || (CurrentXferOptions == I2C_FIRST_FRAME)))
  4782. {
  4783. if ((CurrentXferOptions != I2C_NEXT_FRAME) && (CurrentXferOptions != I2C_FIRST_AND_NEXT_FRAME) && (CurrentXferOptions != I2C_LAST_FRAME_NO_STOP))
  4784. {
  4785. /* Disable Acknowledge */
  4786. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  4787. }
  4788. else
  4789. {
  4790. /* Enable Acknowledge */
  4791. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  4792. }
  4793. /* Clear ADDR flag */
  4794. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  4795. }
  4796. else
  4797. {
  4798. /* Disable Acknowledge */
  4799. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  4800. /* Clear ADDR flag */
  4801. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  4802. /* Generate Stop */
  4803. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  4804. }
  4805. }
  4806. else if (hi2c->XferCount == 2U)
  4807. {
  4808. if ((CurrentXferOptions != I2C_NEXT_FRAME) && (CurrentXferOptions != I2C_FIRST_AND_NEXT_FRAME) && (CurrentXferOptions != I2C_LAST_FRAME_NO_STOP))
  4809. {
  4810. /* Disable Acknowledge */
  4811. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  4812. /* Enable Pos */
  4813. SET_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
  4814. }
  4815. else
  4816. {
  4817. /* Enable Acknowledge */
  4818. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  4819. }
  4820. 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)))
  4821. {
  4822. /* Enable Last DMA bit */
  4823. SET_BIT(hi2c->Instance->CR2, I2C_CR2_LAST);
  4824. }
  4825. /* Clear ADDR flag */
  4826. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  4827. }
  4828. else
  4829. {
  4830. /* Enable Acknowledge */
  4831. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  4832. 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)))
  4833. {
  4834. /* Enable Last DMA bit */
  4835. SET_BIT(hi2c->Instance->CR2, I2C_CR2_LAST);
  4836. }
  4837. /* Clear ADDR flag */
  4838. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  4839. }
  4840. /* Reset Event counter */
  4841. hi2c->EventCount = 0U;
  4842. }
  4843. }
  4844. else
  4845. {
  4846. /* Clear ADDR flag */
  4847. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  4848. }
  4849. }
  4850. /**
  4851. * @brief Handle TXE flag for Slave
  4852. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4853. * the configuration information for I2C module
  4854. * @retval None
  4855. */
  4856. static void I2C_SlaveTransmit_TXE(I2C_HandleTypeDef *hi2c)
  4857. {
  4858. /* Declaration of temporary variables to prevent undefined behavior of volatile usage */
  4859. HAL_I2C_StateTypeDef CurrentState = hi2c->State;
  4860. if (hi2c->XferCount != 0U)
  4861. {
  4862. /* Write data to DR */
  4863. hi2c->Instance->DR = *hi2c->pBuffPtr;
  4864. /* Increment Buffer pointer */
  4865. hi2c->pBuffPtr++;
  4866. /* Update counter */
  4867. hi2c->XferCount--;
  4868. if ((hi2c->XferCount == 0U) && (CurrentState == HAL_I2C_STATE_BUSY_TX_LISTEN))
  4869. {
  4870. /* Last Byte is received, disable Interrupt */
  4871. __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_BUF);
  4872. /* Set state at HAL_I2C_STATE_LISTEN */
  4873. hi2c->PreviousState = I2C_STATE_SLAVE_BUSY_TX;
  4874. hi2c->State = HAL_I2C_STATE_LISTEN;
  4875. /* Call the corresponding callback to inform upper layer of End of Transfer */
  4876. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  4877. hi2c->SlaveTxCpltCallback(hi2c);
  4878. #else
  4879. HAL_I2C_SlaveTxCpltCallback(hi2c);
  4880. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  4881. }
  4882. }
  4883. }
  4884. /**
  4885. * @brief Handle BTF flag for Slave transmitter
  4886. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4887. * the configuration information for I2C module
  4888. * @retval None
  4889. */
  4890. static void I2C_SlaveTransmit_BTF(I2C_HandleTypeDef *hi2c)
  4891. {
  4892. if (hi2c->XferCount != 0U)
  4893. {
  4894. /* Write data to DR */
  4895. hi2c->Instance->DR = *hi2c->pBuffPtr;
  4896. /* Increment Buffer pointer */
  4897. hi2c->pBuffPtr++;
  4898. /* Update counter */
  4899. hi2c->XferCount--;
  4900. }
  4901. }
  4902. /**
  4903. * @brief Handle RXNE flag for Slave
  4904. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4905. * the configuration information for I2C module
  4906. * @retval None
  4907. */
  4908. static void I2C_SlaveReceive_RXNE(I2C_HandleTypeDef *hi2c)
  4909. {
  4910. /* Declaration of temporary variables to prevent undefined behavior of volatile usage */
  4911. HAL_I2C_StateTypeDef CurrentState = hi2c->State;
  4912. if (hi2c->XferCount != 0U)
  4913. {
  4914. /* Read data from DR */
  4915. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
  4916. /* Increment Buffer pointer */
  4917. hi2c->pBuffPtr++;
  4918. /* Update counter */
  4919. hi2c->XferCount--;
  4920. if ((hi2c->XferCount == 0U) && (CurrentState == HAL_I2C_STATE_BUSY_RX_LISTEN))
  4921. {
  4922. /* Last Byte is received, disable Interrupt */
  4923. __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_BUF);
  4924. /* Set state at HAL_I2C_STATE_LISTEN */
  4925. hi2c->PreviousState = I2C_STATE_SLAVE_BUSY_RX;
  4926. hi2c->State = HAL_I2C_STATE_LISTEN;
  4927. /* Call the corresponding callback to inform upper layer of End of Transfer */
  4928. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  4929. hi2c->SlaveRxCpltCallback(hi2c);
  4930. #else
  4931. HAL_I2C_SlaveRxCpltCallback(hi2c);
  4932. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  4933. }
  4934. }
  4935. }
  4936. /**
  4937. * @brief Handle BTF flag for Slave receiver
  4938. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4939. * the configuration information for I2C module
  4940. * @retval None
  4941. */
  4942. static void I2C_SlaveReceive_BTF(I2C_HandleTypeDef *hi2c)
  4943. {
  4944. if (hi2c->XferCount != 0U)
  4945. {
  4946. /* Read data from DR */
  4947. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
  4948. /* Increment Buffer pointer */
  4949. hi2c->pBuffPtr++;
  4950. /* Update counter */
  4951. hi2c->XferCount--;
  4952. }
  4953. }
  4954. /**
  4955. * @brief Handle ADD flag for Slave
  4956. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  4957. * the configuration information for I2C module
  4958. * @param IT2Flags Interrupt2 flags to handle.
  4959. * @retval None
  4960. */
  4961. static void I2C_Slave_ADDR(I2C_HandleTypeDef *hi2c, uint32_t IT2Flags)
  4962. {
  4963. uint8_t TransferDirection = I2C_DIRECTION_RECEIVE;
  4964. uint16_t SlaveAddrCode;
  4965. if (((uint32_t)hi2c->State & (uint32_t)HAL_I2C_STATE_LISTEN) == (uint32_t)HAL_I2C_STATE_LISTEN)
  4966. {
  4967. /* Disable BUF interrupt, BUF enabling is manage through slave specific interface */
  4968. __HAL_I2C_DISABLE_IT(hi2c, (I2C_IT_BUF));
  4969. /* Transfer Direction requested by Master */
  4970. if (I2C_CHECK_FLAG(IT2Flags, I2C_FLAG_TRA) == RESET)
  4971. {
  4972. TransferDirection = I2C_DIRECTION_TRANSMIT;
  4973. }
  4974. if (I2C_CHECK_FLAG(IT2Flags, I2C_FLAG_DUALF) == RESET)
  4975. {
  4976. SlaveAddrCode = (uint16_t)hi2c->Init.OwnAddress1;
  4977. }
  4978. else
  4979. {
  4980. SlaveAddrCode = (uint16_t)hi2c->Init.OwnAddress2;
  4981. }
  4982. /* Process Unlocked */
  4983. __HAL_UNLOCK(hi2c);
  4984. /* Call Slave Addr callback */
  4985. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  4986. hi2c->AddrCallback(hi2c, TransferDirection, SlaveAddrCode);
  4987. #else
  4988. HAL_I2C_AddrCallback(hi2c, TransferDirection, SlaveAddrCode);
  4989. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  4990. }
  4991. else
  4992. {
  4993. /* Clear ADDR flag */
  4994. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ADDR);
  4995. /* Process Unlocked */
  4996. __HAL_UNLOCK(hi2c);
  4997. }
  4998. }
  4999. /**
  5000. * @brief Handle STOPF flag for Slave
  5001. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  5002. * the configuration information for I2C module
  5003. * @retval None
  5004. */
  5005. static void I2C_Slave_STOPF(I2C_HandleTypeDef *hi2c)
  5006. {
  5007. /* Declaration of temporary variable to prevent undefined behavior of volatile usage */
  5008. HAL_I2C_StateTypeDef CurrentState = hi2c->State;
  5009. /* Disable EVT, BUF and ERR interrupt */
  5010. __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  5011. /* Clear STOPF flag */
  5012. __HAL_I2C_CLEAR_STOPFLAG(hi2c);
  5013. /* Disable Acknowledge */
  5014. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  5015. /* If a DMA is ongoing, Update handle size context */
  5016. if ((hi2c->Instance->CR2 & I2C_CR2_DMAEN) == I2C_CR2_DMAEN)
  5017. {
  5018. if ((CurrentState == HAL_I2C_STATE_BUSY_RX) || (CurrentState == HAL_I2C_STATE_BUSY_RX_LISTEN))
  5019. {
  5020. hi2c->XferCount = (uint16_t)(__HAL_DMA_GET_COUNTER(hi2c->hdmarx));
  5021. if (hi2c->XferCount != 0U)
  5022. {
  5023. /* Set ErrorCode corresponding to a Non-Acknowledge */
  5024. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  5025. }
  5026. /* Disable, stop the current DMA */
  5027. CLEAR_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
  5028. /* Abort DMA Xfer if any */
  5029. if (HAL_DMA_GetState(hi2c->hdmarx) != HAL_DMA_STATE_READY)
  5030. {
  5031. /* Set the I2C DMA Abort callback :
  5032. will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
  5033. hi2c->hdmarx->XferAbortCallback = I2C_DMAAbort;
  5034. /* Abort DMA RX */
  5035. if (HAL_DMA_Abort_IT(hi2c->hdmarx) != HAL_OK)
  5036. {
  5037. /* Call Directly XferAbortCallback function in case of error */
  5038. hi2c->hdmarx->XferAbortCallback(hi2c->hdmarx);
  5039. }
  5040. }
  5041. }
  5042. else
  5043. {
  5044. hi2c->XferCount = (uint16_t)(__HAL_DMA_GET_COUNTER(hi2c->hdmatx));
  5045. if (hi2c->XferCount != 0U)
  5046. {
  5047. /* Set ErrorCode corresponding to a Non-Acknowledge */
  5048. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  5049. }
  5050. /* Disable, stop the current DMA */
  5051. CLEAR_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
  5052. /* Abort DMA Xfer if any */
  5053. if (HAL_DMA_GetState(hi2c->hdmatx) != HAL_DMA_STATE_READY)
  5054. {
  5055. /* Set the I2C DMA Abort callback :
  5056. will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
  5057. hi2c->hdmatx->XferAbortCallback = I2C_DMAAbort;
  5058. /* Abort DMA TX */
  5059. if (HAL_DMA_Abort_IT(hi2c->hdmatx) != HAL_OK)
  5060. {
  5061. /* Call Directly XferAbortCallback function in case of error */
  5062. hi2c->hdmatx->XferAbortCallback(hi2c->hdmatx);
  5063. }
  5064. }
  5065. }
  5066. }
  5067. /* All data are not transferred, so set error code accordingly */
  5068. if (hi2c->XferCount != 0U)
  5069. {
  5070. /* Store Last receive data if any */
  5071. if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET)
  5072. {
  5073. /* Read data from DR */
  5074. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
  5075. /* Increment Buffer pointer */
  5076. hi2c->pBuffPtr++;
  5077. /* Update counter */
  5078. hi2c->XferCount--;
  5079. }
  5080. /* Store Last receive data if any */
  5081. if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET)
  5082. {
  5083. /* Read data from DR */
  5084. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
  5085. /* Increment Buffer pointer */
  5086. hi2c->pBuffPtr++;
  5087. /* Update counter */
  5088. hi2c->XferCount--;
  5089. }
  5090. if (hi2c->XferCount != 0U)
  5091. {
  5092. /* Set ErrorCode corresponding to a Non-Acknowledge */
  5093. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  5094. }
  5095. }
  5096. if (hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
  5097. {
  5098. /* Call the corresponding callback to inform upper layer of End of Transfer */
  5099. I2C_ITError(hi2c);
  5100. }
  5101. else
  5102. {
  5103. if (CurrentState == HAL_I2C_STATE_BUSY_RX_LISTEN)
  5104. {
  5105. /* Set state at HAL_I2C_STATE_LISTEN */
  5106. hi2c->PreviousState = I2C_STATE_NONE;
  5107. hi2c->State = HAL_I2C_STATE_LISTEN;
  5108. /* Call the corresponding callback to inform upper layer of End of Transfer */
  5109. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  5110. hi2c->SlaveRxCpltCallback(hi2c);
  5111. #else
  5112. HAL_I2C_SlaveRxCpltCallback(hi2c);
  5113. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  5114. }
  5115. if (hi2c->State == HAL_I2C_STATE_LISTEN)
  5116. {
  5117. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  5118. hi2c->PreviousState = I2C_STATE_NONE;
  5119. hi2c->State = HAL_I2C_STATE_READY;
  5120. hi2c->Mode = HAL_I2C_MODE_NONE;
  5121. /* Call the Listen Complete callback, to inform upper layer of the end of Listen usecase */
  5122. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  5123. hi2c->ListenCpltCallback(hi2c);
  5124. #else
  5125. HAL_I2C_ListenCpltCallback(hi2c);
  5126. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  5127. }
  5128. else
  5129. {
  5130. if ((hi2c->PreviousState == I2C_STATE_SLAVE_BUSY_RX) || (CurrentState == HAL_I2C_STATE_BUSY_RX))
  5131. {
  5132. hi2c->PreviousState = I2C_STATE_NONE;
  5133. hi2c->State = HAL_I2C_STATE_READY;
  5134. hi2c->Mode = HAL_I2C_MODE_NONE;
  5135. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  5136. hi2c->SlaveRxCpltCallback(hi2c);
  5137. #else
  5138. HAL_I2C_SlaveRxCpltCallback(hi2c);
  5139. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  5140. }
  5141. }
  5142. }
  5143. }
  5144. /**
  5145. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  5146. * the configuration information for I2C module
  5147. * @retval None
  5148. */
  5149. static void I2C_Slave_AF(I2C_HandleTypeDef *hi2c)
  5150. {
  5151. /* Declaration of temporary variables to prevent undefined behavior of volatile usage */
  5152. HAL_I2C_StateTypeDef CurrentState = hi2c->State;
  5153. uint32_t CurrentXferOptions = hi2c->XferOptions;
  5154. if (((CurrentXferOptions == I2C_FIRST_AND_LAST_FRAME) || (CurrentXferOptions == I2C_LAST_FRAME)) && \
  5155. (CurrentState == HAL_I2C_STATE_LISTEN))
  5156. {
  5157. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  5158. /* Disable EVT, BUF and ERR interrupt */
  5159. __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  5160. /* Clear AF flag */
  5161. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  5162. /* Disable Acknowledge */
  5163. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  5164. hi2c->PreviousState = I2C_STATE_NONE;
  5165. hi2c->State = HAL_I2C_STATE_READY;
  5166. hi2c->Mode = HAL_I2C_MODE_NONE;
  5167. /* Call the Listen Complete callback, to inform upper layer of the end of Listen usecase */
  5168. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  5169. hi2c->ListenCpltCallback(hi2c);
  5170. #else
  5171. HAL_I2C_ListenCpltCallback(hi2c);
  5172. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  5173. }
  5174. else if (CurrentState == HAL_I2C_STATE_BUSY_TX)
  5175. {
  5176. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  5177. hi2c->PreviousState = I2C_STATE_SLAVE_BUSY_TX;
  5178. hi2c->State = HAL_I2C_STATE_READY;
  5179. hi2c->Mode = HAL_I2C_MODE_NONE;
  5180. /* Disable EVT, BUF and ERR interrupt */
  5181. __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  5182. /* Clear AF flag */
  5183. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  5184. /* Disable Acknowledge */
  5185. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  5186. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  5187. hi2c->SlaveTxCpltCallback(hi2c);
  5188. #else
  5189. HAL_I2C_SlaveTxCpltCallback(hi2c);
  5190. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  5191. }
  5192. else
  5193. {
  5194. /* Clear AF flag only */
  5195. /* State Listen, but XferOptions == FIRST or NEXT */
  5196. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  5197. }
  5198. }
  5199. /**
  5200. * @brief I2C interrupts error process
  5201. * @param hi2c I2C handle.
  5202. * @retval None
  5203. */
  5204. static void I2C_ITError(I2C_HandleTypeDef *hi2c)
  5205. {
  5206. /* Declaration of temporary variable to prevent undefined behavior of volatile usage */
  5207. HAL_I2C_StateTypeDef CurrentState = hi2c->State;
  5208. HAL_I2C_ModeTypeDef CurrentMode = hi2c->Mode;
  5209. uint32_t CurrentError;
  5210. if (((CurrentMode == HAL_I2C_MODE_MASTER) || (CurrentMode == HAL_I2C_MODE_MEM)) && (CurrentState == HAL_I2C_STATE_BUSY_RX))
  5211. {
  5212. /* Disable Pos bit in I2C CR1 when error occurred in Master/Mem Receive IT Process */
  5213. hi2c->Instance->CR1 &= ~I2C_CR1_POS;
  5214. }
  5215. if (((uint32_t)CurrentState & (uint32_t)HAL_I2C_STATE_LISTEN) == (uint32_t)HAL_I2C_STATE_LISTEN)
  5216. {
  5217. /* keep HAL_I2C_STATE_LISTEN */
  5218. hi2c->PreviousState = I2C_STATE_NONE;
  5219. hi2c->State = HAL_I2C_STATE_LISTEN;
  5220. }
  5221. else
  5222. {
  5223. /* If state is an abort treatment on going, don't change state */
  5224. /* This change will be do later */
  5225. if ((READ_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN) != I2C_CR2_DMAEN) && (CurrentState != HAL_I2C_STATE_ABORT))
  5226. {
  5227. hi2c->State = HAL_I2C_STATE_READY;
  5228. hi2c->Mode = HAL_I2C_MODE_NONE;
  5229. }
  5230. hi2c->PreviousState = I2C_STATE_NONE;
  5231. }
  5232. /* Abort DMA transfer */
  5233. if (READ_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN) == I2C_CR2_DMAEN)
  5234. {
  5235. hi2c->Instance->CR2 &= ~I2C_CR2_DMAEN;
  5236. if (hi2c->hdmatx->State != HAL_DMA_STATE_READY)
  5237. {
  5238. /* Set the DMA Abort callback :
  5239. will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
  5240. hi2c->hdmatx->XferAbortCallback = I2C_DMAAbort;
  5241. if (HAL_DMA_Abort_IT(hi2c->hdmatx) != HAL_OK)
  5242. {
  5243. /* Disable I2C peripheral to prevent dummy data in buffer */
  5244. __HAL_I2C_DISABLE(hi2c);
  5245. hi2c->State = HAL_I2C_STATE_READY;
  5246. /* Call Directly XferAbortCallback function in case of error */
  5247. hi2c->hdmatx->XferAbortCallback(hi2c->hdmatx);
  5248. }
  5249. }
  5250. else
  5251. {
  5252. /* Set the DMA Abort callback :
  5253. will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
  5254. hi2c->hdmarx->XferAbortCallback = I2C_DMAAbort;
  5255. if (HAL_DMA_Abort_IT(hi2c->hdmarx) != HAL_OK)
  5256. {
  5257. /* Store Last receive data if any */
  5258. if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET)
  5259. {
  5260. /* Read data from DR */
  5261. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
  5262. /* Increment Buffer pointer */
  5263. hi2c->pBuffPtr++;
  5264. }
  5265. /* Disable I2C peripheral to prevent dummy data in buffer */
  5266. __HAL_I2C_DISABLE(hi2c);
  5267. hi2c->State = HAL_I2C_STATE_READY;
  5268. /* Call Directly hi2c->hdmarx->XferAbortCallback function in case of error */
  5269. hi2c->hdmarx->XferAbortCallback(hi2c->hdmarx);
  5270. }
  5271. }
  5272. }
  5273. else if (hi2c->State == HAL_I2C_STATE_ABORT)
  5274. {
  5275. hi2c->State = HAL_I2C_STATE_READY;
  5276. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  5277. /* Store Last receive data if any */
  5278. if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET)
  5279. {
  5280. /* Read data from DR */
  5281. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
  5282. /* Increment Buffer pointer */
  5283. hi2c->pBuffPtr++;
  5284. }
  5285. /* Disable I2C peripheral to prevent dummy data in buffer */
  5286. __HAL_I2C_DISABLE(hi2c);
  5287. /* Call the corresponding callback to inform upper layer of End of Transfer */
  5288. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  5289. hi2c->AbortCpltCallback(hi2c);
  5290. #else
  5291. HAL_I2C_AbortCpltCallback(hi2c);
  5292. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  5293. }
  5294. else
  5295. {
  5296. /* Store Last receive data if any */
  5297. if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET)
  5298. {
  5299. /* Read data from DR */
  5300. *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
  5301. /* Increment Buffer pointer */
  5302. hi2c->pBuffPtr++;
  5303. }
  5304. /* Call user error callback */
  5305. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  5306. hi2c->ErrorCallback(hi2c);
  5307. #else
  5308. HAL_I2C_ErrorCallback(hi2c);
  5309. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  5310. }
  5311. /* STOP Flag is not set after a NACK reception, BusError, ArbitrationLost, OverRun */
  5312. CurrentError = hi2c->ErrorCode;
  5313. if (((CurrentError & HAL_I2C_ERROR_BERR) == HAL_I2C_ERROR_BERR) || \
  5314. ((CurrentError & HAL_I2C_ERROR_ARLO) == HAL_I2C_ERROR_ARLO) || \
  5315. ((CurrentError & HAL_I2C_ERROR_AF) == HAL_I2C_ERROR_AF) || \
  5316. ((CurrentError & HAL_I2C_ERROR_OVR) == HAL_I2C_ERROR_OVR))
  5317. {
  5318. /* Disable EVT, BUF and ERR interrupt */
  5319. __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
  5320. }
  5321. /* So may inform upper layer that listen phase is stopped */
  5322. /* during NACK error treatment */
  5323. CurrentState = hi2c->State;
  5324. if (((hi2c->ErrorCode & HAL_I2C_ERROR_AF) == HAL_I2C_ERROR_AF) && (CurrentState == HAL_I2C_STATE_LISTEN))
  5325. {
  5326. hi2c->XferOptions = I2C_NO_OPTION_FRAME;
  5327. hi2c->PreviousState = I2C_STATE_NONE;
  5328. hi2c->State = HAL_I2C_STATE_READY;
  5329. hi2c->Mode = HAL_I2C_MODE_NONE;
  5330. /* Call the Listen Complete callback, to inform upper layer of the end of Listen usecase */
  5331. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  5332. hi2c->ListenCpltCallback(hi2c);
  5333. #else
  5334. HAL_I2C_ListenCpltCallback(hi2c);
  5335. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  5336. }
  5337. }
  5338. /**
  5339. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  5340. * the configuration information for I2C module
  5341. * @param DevAddress Target device address: The device 7 bits address value
  5342. * in datasheet must be shifted to the left before calling the interface
  5343. * @param Timeout Timeout duration
  5344. * @param Tickstart Tick start value
  5345. * @retval HAL status
  5346. */
  5347. static HAL_StatusTypeDef I2C_MasterRequestWrite(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint32_t Timeout, uint32_t Tickstart)
  5348. {
  5349. /* Declaration of temporary variable to prevent undefined behavior of volatile usage */
  5350. uint32_t CurrentXferOptions = hi2c->XferOptions;
  5351. /* Generate Start condition if first transfer */
  5352. if ((CurrentXferOptions == I2C_FIRST_AND_LAST_FRAME) || (CurrentXferOptions == I2C_FIRST_FRAME) || (CurrentXferOptions == I2C_NO_OPTION_FRAME))
  5353. {
  5354. /* Generate Start */
  5355. SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
  5356. }
  5357. else if (hi2c->PreviousState == I2C_STATE_MASTER_BUSY_RX)
  5358. {
  5359. /* Generate ReStart */
  5360. SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
  5361. }
  5362. else
  5363. {
  5364. /* Do nothing */
  5365. }
  5366. /* Wait until SB flag is set */
  5367. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout, Tickstart) != HAL_OK)
  5368. {
  5369. if (READ_BIT(hi2c->Instance->CR1, I2C_CR1_START) == I2C_CR1_START)
  5370. {
  5371. hi2c->ErrorCode = HAL_I2C_WRONG_START;
  5372. }
  5373. return HAL_TIMEOUT;
  5374. }
  5375. if (hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_7BIT)
  5376. {
  5377. /* Send slave address */
  5378. hi2c->Instance->DR = I2C_7BIT_ADD_WRITE(DevAddress);
  5379. }
  5380. else
  5381. {
  5382. /* Send header of slave address */
  5383. hi2c->Instance->DR = I2C_10BIT_HEADER_WRITE(DevAddress);
  5384. /* Wait until ADD10 flag is set */
  5385. if (I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADD10, Timeout, Tickstart) != HAL_OK)
  5386. {
  5387. return HAL_ERROR;
  5388. }
  5389. /* Send slave address */
  5390. hi2c->Instance->DR = I2C_10BIT_ADDRESS(DevAddress);
  5391. }
  5392. /* Wait until ADDR flag is set */
  5393. if (I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, Timeout, Tickstart) != HAL_OK)
  5394. {
  5395. return HAL_ERROR;
  5396. }
  5397. return HAL_OK;
  5398. }
  5399. /**
  5400. * @brief Master sends target device address for read request.
  5401. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  5402. * the configuration information for I2C module
  5403. * @param DevAddress Target device address: The device 7 bits address value
  5404. * in datasheet must be shifted to the left before calling the interface
  5405. * @param Timeout Timeout duration
  5406. * @param Tickstart Tick start value
  5407. * @retval HAL status
  5408. */
  5409. static HAL_StatusTypeDef I2C_MasterRequestRead(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint32_t Timeout, uint32_t Tickstart)
  5410. {
  5411. /* Declaration of temporary variable to prevent undefined behavior of volatile usage */
  5412. uint32_t CurrentXferOptions = hi2c->XferOptions;
  5413. /* Enable Acknowledge */
  5414. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  5415. /* Generate Start condition if first transfer */
  5416. if ((CurrentXferOptions == I2C_FIRST_AND_LAST_FRAME) || (CurrentXferOptions == I2C_FIRST_FRAME) || (CurrentXferOptions == I2C_NO_OPTION_FRAME))
  5417. {
  5418. /* Generate Start */
  5419. SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
  5420. }
  5421. else if (hi2c->PreviousState == I2C_STATE_MASTER_BUSY_TX)
  5422. {
  5423. /* Generate ReStart */
  5424. SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
  5425. }
  5426. else
  5427. {
  5428. /* Do nothing */
  5429. }
  5430. /* Wait until SB flag is set */
  5431. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout, Tickstart) != HAL_OK)
  5432. {
  5433. if (READ_BIT(hi2c->Instance->CR1, I2C_CR1_START) == I2C_CR1_START)
  5434. {
  5435. hi2c->ErrorCode = HAL_I2C_WRONG_START;
  5436. }
  5437. return HAL_TIMEOUT;
  5438. }
  5439. if (hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_7BIT)
  5440. {
  5441. /* Send slave address */
  5442. hi2c->Instance->DR = I2C_7BIT_ADD_READ(DevAddress);
  5443. }
  5444. else
  5445. {
  5446. /* Send header of slave address */
  5447. hi2c->Instance->DR = I2C_10BIT_HEADER_WRITE(DevAddress);
  5448. /* Wait until ADD10 flag is set */
  5449. if (I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADD10, Timeout, Tickstart) != HAL_OK)
  5450. {
  5451. return HAL_ERROR;
  5452. }
  5453. /* Send slave address */
  5454. hi2c->Instance->DR = I2C_10BIT_ADDRESS(DevAddress);
  5455. /* Wait until ADDR flag is set */
  5456. if (I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, Timeout, Tickstart) != HAL_OK)
  5457. {
  5458. return HAL_ERROR;
  5459. }
  5460. /* Clear ADDR flag */
  5461. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  5462. /* Generate Restart */
  5463. SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
  5464. /* Wait until SB flag is set */
  5465. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout, Tickstart) != HAL_OK)
  5466. {
  5467. if (READ_BIT(hi2c->Instance->CR1, I2C_CR1_START) == I2C_CR1_START)
  5468. {
  5469. hi2c->ErrorCode = HAL_I2C_WRONG_START;
  5470. }
  5471. return HAL_TIMEOUT;
  5472. }
  5473. /* Send header of slave address */
  5474. hi2c->Instance->DR = I2C_10BIT_HEADER_READ(DevAddress);
  5475. }
  5476. /* Wait until ADDR flag is set */
  5477. if (I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, Timeout, Tickstart) != HAL_OK)
  5478. {
  5479. return HAL_ERROR;
  5480. }
  5481. return HAL_OK;
  5482. }
  5483. /**
  5484. * @brief Master sends target device address followed by internal memory address for write request.
  5485. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  5486. * the configuration information for I2C module
  5487. * @param DevAddress Target device address: The device 7 bits address value
  5488. * in datasheet must be shifted to the left before calling the interface
  5489. * @param MemAddress Internal memory address
  5490. * @param MemAddSize Size of internal memory address
  5491. * @param Timeout Timeout duration
  5492. * @param Tickstart Tick start value
  5493. * @retval HAL status
  5494. */
  5495. static HAL_StatusTypeDef I2C_RequestMemoryWrite(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout, uint32_t Tickstart)
  5496. {
  5497. /* Generate Start */
  5498. SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
  5499. /* Wait until SB flag is set */
  5500. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout, Tickstart) != HAL_OK)
  5501. {
  5502. if (READ_BIT(hi2c->Instance->CR1, I2C_CR1_START) == I2C_CR1_START)
  5503. {
  5504. hi2c->ErrorCode = HAL_I2C_WRONG_START;
  5505. }
  5506. return HAL_TIMEOUT;
  5507. }
  5508. /* Send slave address */
  5509. hi2c->Instance->DR = I2C_7BIT_ADD_WRITE(DevAddress);
  5510. /* Wait until ADDR flag is set */
  5511. if (I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, Timeout, Tickstart) != HAL_OK)
  5512. {
  5513. return HAL_ERROR;
  5514. }
  5515. /* Clear ADDR flag */
  5516. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  5517. /* Wait until TXE flag is set */
  5518. if (I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, Tickstart) != HAL_OK)
  5519. {
  5520. if (hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  5521. {
  5522. /* Generate Stop */
  5523. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  5524. }
  5525. return HAL_ERROR;
  5526. }
  5527. /* If Memory address size is 8Bit */
  5528. if (MemAddSize == I2C_MEMADD_SIZE_8BIT)
  5529. {
  5530. /* Send Memory Address */
  5531. hi2c->Instance->DR = I2C_MEM_ADD_LSB(MemAddress);
  5532. }
  5533. /* If Memory address size is 16Bit */
  5534. else
  5535. {
  5536. /* Send MSB of Memory Address */
  5537. hi2c->Instance->DR = I2C_MEM_ADD_MSB(MemAddress);
  5538. /* Wait until TXE flag is set */
  5539. if (I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, Tickstart) != HAL_OK)
  5540. {
  5541. if (hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  5542. {
  5543. /* Generate Stop */
  5544. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  5545. }
  5546. return HAL_ERROR;
  5547. }
  5548. /* Send LSB of Memory Address */
  5549. hi2c->Instance->DR = I2C_MEM_ADD_LSB(MemAddress);
  5550. }
  5551. return HAL_OK;
  5552. }
  5553. /**
  5554. * @brief Master sends target device address followed by internal memory address for read request.
  5555. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  5556. * the configuration information for I2C module
  5557. * @param DevAddress Target device address: The device 7 bits address value
  5558. * in datasheet must be shifted to the left before calling the interface
  5559. * @param MemAddress Internal memory address
  5560. * @param MemAddSize Size of internal memory address
  5561. * @param Timeout Timeout duration
  5562. * @param Tickstart Tick start value
  5563. * @retval HAL status
  5564. */
  5565. static HAL_StatusTypeDef I2C_RequestMemoryRead(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout, uint32_t Tickstart)
  5566. {
  5567. /* Enable Acknowledge */
  5568. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  5569. /* Generate Start */
  5570. SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
  5571. /* Wait until SB flag is set */
  5572. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout, Tickstart) != HAL_OK)
  5573. {
  5574. if (READ_BIT(hi2c->Instance->CR1, I2C_CR1_START) == I2C_CR1_START)
  5575. {
  5576. hi2c->ErrorCode = HAL_I2C_WRONG_START;
  5577. }
  5578. return HAL_TIMEOUT;
  5579. }
  5580. /* Send slave address */
  5581. hi2c->Instance->DR = I2C_7BIT_ADD_WRITE(DevAddress);
  5582. /* Wait until ADDR flag is set */
  5583. if (I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, Timeout, Tickstart) != HAL_OK)
  5584. {
  5585. return HAL_ERROR;
  5586. }
  5587. /* Clear ADDR flag */
  5588. __HAL_I2C_CLEAR_ADDRFLAG(hi2c);
  5589. /* Wait until TXE flag is set */
  5590. if (I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, Tickstart) != HAL_OK)
  5591. {
  5592. if (hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  5593. {
  5594. /* Generate Stop */
  5595. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  5596. }
  5597. return HAL_ERROR;
  5598. }
  5599. /* If Memory address size is 8Bit */
  5600. if (MemAddSize == I2C_MEMADD_SIZE_8BIT)
  5601. {
  5602. /* Send Memory Address */
  5603. hi2c->Instance->DR = I2C_MEM_ADD_LSB(MemAddress);
  5604. }
  5605. /* If Memory address size is 16Bit */
  5606. else
  5607. {
  5608. /* Send MSB of Memory Address */
  5609. hi2c->Instance->DR = I2C_MEM_ADD_MSB(MemAddress);
  5610. /* Wait until TXE flag is set */
  5611. if (I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, Tickstart) != HAL_OK)
  5612. {
  5613. if (hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  5614. {
  5615. /* Generate Stop */
  5616. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  5617. }
  5618. return HAL_ERROR;
  5619. }
  5620. /* Send LSB of Memory Address */
  5621. hi2c->Instance->DR = I2C_MEM_ADD_LSB(MemAddress);
  5622. }
  5623. /* Wait until TXE flag is set */
  5624. if (I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, Tickstart) != HAL_OK)
  5625. {
  5626. if (hi2c->ErrorCode == HAL_I2C_ERROR_AF)
  5627. {
  5628. /* Generate Stop */
  5629. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  5630. }
  5631. return HAL_ERROR;
  5632. }
  5633. /* Generate Restart */
  5634. SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
  5635. /* Wait until SB flag is set */
  5636. if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout, Tickstart) != HAL_OK)
  5637. {
  5638. if (READ_BIT(hi2c->Instance->CR1, I2C_CR1_START) == I2C_CR1_START)
  5639. {
  5640. hi2c->ErrorCode = HAL_I2C_WRONG_START;
  5641. }
  5642. return HAL_TIMEOUT;
  5643. }
  5644. /* Send slave address */
  5645. hi2c->Instance->DR = I2C_7BIT_ADD_READ(DevAddress);
  5646. /* Wait until ADDR flag is set */
  5647. if (I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, Timeout, Tickstart) != HAL_OK)
  5648. {
  5649. return HAL_ERROR;
  5650. }
  5651. return HAL_OK;
  5652. }
  5653. /**
  5654. * @brief DMA I2C process complete callback.
  5655. * @param hdma DMA handle
  5656. * @retval None
  5657. */
  5658. static void I2C_DMAXferCplt(DMA_HandleTypeDef *hdma)
  5659. {
  5660. I2C_HandleTypeDef *hi2c = (I2C_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent; /* Derogation MISRAC2012-Rule-11.5 */
  5661. /* Declaration of temporary variable to prevent undefined behavior of volatile usage */
  5662. HAL_I2C_StateTypeDef CurrentState = hi2c->State;
  5663. HAL_I2C_ModeTypeDef CurrentMode = hi2c->Mode;
  5664. uint32_t CurrentXferOptions = hi2c->XferOptions;
  5665. /* Disable EVT and ERR interrupt */
  5666. __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR);
  5667. /* Clear Complete callback */
  5668. if (hi2c->hdmatx != NULL)
  5669. {
  5670. hi2c->hdmatx->XferCpltCallback = NULL;
  5671. }
  5672. if (hi2c->hdmarx != NULL)
  5673. {
  5674. hi2c->hdmarx->XferCpltCallback = NULL;
  5675. }
  5676. 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)))
  5677. {
  5678. /* Disable DMA Request */
  5679. CLEAR_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
  5680. hi2c->XferCount = 0U;
  5681. if (CurrentState == HAL_I2C_STATE_BUSY_TX_LISTEN)
  5682. {
  5683. /* Set state at HAL_I2C_STATE_LISTEN */
  5684. hi2c->PreviousState = I2C_STATE_SLAVE_BUSY_TX;
  5685. hi2c->State = HAL_I2C_STATE_LISTEN;
  5686. /* Call the corresponding callback to inform upper layer of End of Transfer */
  5687. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  5688. hi2c->SlaveTxCpltCallback(hi2c);
  5689. #else
  5690. HAL_I2C_SlaveTxCpltCallback(hi2c);
  5691. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  5692. }
  5693. else if (CurrentState == HAL_I2C_STATE_BUSY_RX_LISTEN)
  5694. {
  5695. /* Set state at HAL_I2C_STATE_LISTEN */
  5696. hi2c->PreviousState = I2C_STATE_SLAVE_BUSY_RX;
  5697. hi2c->State = HAL_I2C_STATE_LISTEN;
  5698. /* Call the corresponding callback to inform upper layer of End of Transfer */
  5699. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  5700. hi2c->SlaveRxCpltCallback(hi2c);
  5701. #else
  5702. HAL_I2C_SlaveRxCpltCallback(hi2c);
  5703. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  5704. }
  5705. else
  5706. {
  5707. /* Do nothing */
  5708. }
  5709. /* Enable EVT and ERR interrupt to treat end of transfer in IRQ handler */
  5710. __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR);
  5711. }
  5712. /* Check current Mode, in case of treatment DMA handler have been preempted by a prior interrupt */
  5713. else if (hi2c->Mode != HAL_I2C_MODE_NONE)
  5714. {
  5715. if (hi2c->XferCount == (uint16_t)1)
  5716. {
  5717. /* Disable Acknowledge */
  5718. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  5719. }
  5720. /* Disable EVT and ERR interrupt */
  5721. __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR);
  5722. /* Prepare next transfer or stop current transfer */
  5723. if ((CurrentXferOptions == I2C_NO_OPTION_FRAME) || (CurrentXferOptions == I2C_FIRST_AND_LAST_FRAME) || (CurrentXferOptions == I2C_OTHER_AND_LAST_FRAME) || (CurrentXferOptions == I2C_LAST_FRAME))
  5724. {
  5725. /* Generate Stop */
  5726. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  5727. }
  5728. /* Disable Last DMA */
  5729. CLEAR_BIT(hi2c->Instance->CR2, I2C_CR2_LAST);
  5730. /* Disable DMA Request */
  5731. CLEAR_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
  5732. hi2c->XferCount = 0U;
  5733. /* Check if Errors has been detected during transfer */
  5734. if (hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
  5735. {
  5736. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  5737. hi2c->ErrorCallback(hi2c);
  5738. #else
  5739. HAL_I2C_ErrorCallback(hi2c);
  5740. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  5741. }
  5742. else
  5743. {
  5744. hi2c->State = HAL_I2C_STATE_READY;
  5745. if (hi2c->Mode == HAL_I2C_MODE_MEM)
  5746. {
  5747. hi2c->Mode = HAL_I2C_MODE_NONE;
  5748. hi2c->PreviousState = I2C_STATE_NONE;
  5749. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  5750. hi2c->MemRxCpltCallback(hi2c);
  5751. #else
  5752. HAL_I2C_MemRxCpltCallback(hi2c);
  5753. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  5754. }
  5755. else
  5756. {
  5757. hi2c->Mode = HAL_I2C_MODE_NONE;
  5758. hi2c->PreviousState = I2C_STATE_MASTER_BUSY_RX;
  5759. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  5760. hi2c->MasterRxCpltCallback(hi2c);
  5761. #else
  5762. HAL_I2C_MasterRxCpltCallback(hi2c);
  5763. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  5764. }
  5765. }
  5766. }
  5767. else
  5768. {
  5769. /* Do nothing */
  5770. }
  5771. }
  5772. /**
  5773. * @brief DMA I2C communication error callback.
  5774. * @param hdma DMA handle
  5775. * @retval None
  5776. */
  5777. static void I2C_DMAError(DMA_HandleTypeDef *hdma)
  5778. {
  5779. I2C_HandleTypeDef *hi2c = (I2C_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent; /* Derogation MISRAC2012-Rule-11.5 */
  5780. /* Clear Complete callback */
  5781. if (hi2c->hdmatx != NULL)
  5782. {
  5783. hi2c->hdmatx->XferCpltCallback = NULL;
  5784. }
  5785. if (hi2c->hdmarx != NULL)
  5786. {
  5787. hi2c->hdmarx->XferCpltCallback = NULL;
  5788. }
  5789. /* Disable Acknowledge */
  5790. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  5791. hi2c->XferCount = 0U;
  5792. hi2c->State = HAL_I2C_STATE_READY;
  5793. hi2c->Mode = HAL_I2C_MODE_NONE;
  5794. hi2c->ErrorCode |= HAL_I2C_ERROR_DMA;
  5795. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  5796. hi2c->ErrorCallback(hi2c);
  5797. #else
  5798. HAL_I2C_ErrorCallback(hi2c);
  5799. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  5800. }
  5801. /**
  5802. * @brief DMA I2C communication abort callback
  5803. * (To be called at end of DMA Abort procedure).
  5804. * @param hdma DMA handle.
  5805. * @retval None
  5806. */
  5807. static void I2C_DMAAbort(DMA_HandleTypeDef *hdma)
  5808. {
  5809. __IO uint32_t count = 0U;
  5810. I2C_HandleTypeDef *hi2c = (I2C_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent; /* Derogation MISRAC2012-Rule-11.5 */
  5811. /* Declaration of temporary variable to prevent undefined behavior of volatile usage */
  5812. HAL_I2C_StateTypeDef CurrentState = hi2c->State;
  5813. /* During abort treatment, check that there is no pending STOP request */
  5814. /* Wait until STOP flag is reset */
  5815. count = I2C_TIMEOUT_FLAG * (SystemCoreClock / 25U / 1000U);
  5816. do
  5817. {
  5818. if (count == 0U)
  5819. {
  5820. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  5821. break;
  5822. }
  5823. count--;
  5824. }
  5825. while (READ_BIT(hi2c->Instance->CR1, I2C_CR1_STOP) == I2C_CR1_STOP);
  5826. /* Clear Complete callback */
  5827. if (hi2c->hdmatx != NULL)
  5828. {
  5829. hi2c->hdmatx->XferCpltCallback = NULL;
  5830. }
  5831. if (hi2c->hdmarx != NULL)
  5832. {
  5833. hi2c->hdmarx->XferCpltCallback = NULL;
  5834. }
  5835. /* Disable Acknowledge */
  5836. CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  5837. hi2c->XferCount = 0U;
  5838. /* Reset XferAbortCallback */
  5839. if (hi2c->hdmatx != NULL)
  5840. {
  5841. hi2c->hdmatx->XferAbortCallback = NULL;
  5842. }
  5843. if (hi2c->hdmarx != NULL)
  5844. {
  5845. hi2c->hdmarx->XferAbortCallback = NULL;
  5846. }
  5847. /* Disable I2C peripheral to prevent dummy data in buffer */
  5848. __HAL_I2C_DISABLE(hi2c);
  5849. /* Check if come from abort from user */
  5850. if (hi2c->State == HAL_I2C_STATE_ABORT)
  5851. {
  5852. hi2c->State = HAL_I2C_STATE_READY;
  5853. hi2c->Mode = HAL_I2C_MODE_NONE;
  5854. hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
  5855. /* Call the corresponding callback to inform upper layer of End of Transfer */
  5856. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  5857. hi2c->AbortCpltCallback(hi2c);
  5858. #else
  5859. HAL_I2C_AbortCpltCallback(hi2c);
  5860. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  5861. }
  5862. else
  5863. {
  5864. if (((uint32_t)CurrentState & (uint32_t)HAL_I2C_STATE_LISTEN) == (uint32_t)HAL_I2C_STATE_LISTEN)
  5865. {
  5866. /* Renable I2C peripheral */
  5867. __HAL_I2C_ENABLE(hi2c);
  5868. /* Enable Acknowledge */
  5869. SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
  5870. /* keep HAL_I2C_STATE_LISTEN */
  5871. hi2c->PreviousState = I2C_STATE_NONE;
  5872. hi2c->State = HAL_I2C_STATE_LISTEN;
  5873. }
  5874. else
  5875. {
  5876. hi2c->State = HAL_I2C_STATE_READY;
  5877. hi2c->Mode = HAL_I2C_MODE_NONE;
  5878. }
  5879. /* Call the corresponding callback to inform upper layer of End of Transfer */
  5880. #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
  5881. hi2c->ErrorCallback(hi2c);
  5882. #else
  5883. HAL_I2C_ErrorCallback(hi2c);
  5884. #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
  5885. }
  5886. }
  5887. /**
  5888. * @brief This function handles I2C Communication Timeout.
  5889. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  5890. * the configuration information for I2C module
  5891. * @param Flag specifies the I2C flag to check.
  5892. * @param Status The new Flag status (SET or RESET).
  5893. * @param Timeout Timeout duration
  5894. * @param Tickstart Tick start value
  5895. * @retval HAL status
  5896. */
  5897. static HAL_StatusTypeDef I2C_WaitOnFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, FlagStatus Status, uint32_t Timeout, uint32_t Tickstart)
  5898. {
  5899. /* Wait until flag is set */
  5900. while (__HAL_I2C_GET_FLAG(hi2c, Flag) == Status)
  5901. {
  5902. /* Check for the Timeout */
  5903. if (Timeout != HAL_MAX_DELAY)
  5904. {
  5905. if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
  5906. {
  5907. hi2c->PreviousState = I2C_STATE_NONE;
  5908. hi2c->State = HAL_I2C_STATE_READY;
  5909. hi2c->Mode = HAL_I2C_MODE_NONE;
  5910. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  5911. /* Process Unlocked */
  5912. __HAL_UNLOCK(hi2c);
  5913. return HAL_ERROR;
  5914. }
  5915. }
  5916. }
  5917. return HAL_OK;
  5918. }
  5919. /**
  5920. * @brief This function handles I2C Communication Timeout for Master addressing phase.
  5921. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  5922. * the configuration information for I2C module
  5923. * @param Flag specifies the I2C flag to check.
  5924. * @param Timeout Timeout duration
  5925. * @param Tickstart Tick start value
  5926. * @retval HAL status
  5927. */
  5928. static HAL_StatusTypeDef I2C_WaitOnMasterAddressFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, uint32_t Timeout, uint32_t Tickstart)
  5929. {
  5930. while (__HAL_I2C_GET_FLAG(hi2c, Flag) == RESET)
  5931. {
  5932. if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET)
  5933. {
  5934. /* Generate Stop */
  5935. SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
  5936. /* Clear AF Flag */
  5937. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  5938. hi2c->PreviousState = I2C_STATE_NONE;
  5939. hi2c->State = HAL_I2C_STATE_READY;
  5940. hi2c->Mode = HAL_I2C_MODE_NONE;
  5941. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  5942. /* Process Unlocked */
  5943. __HAL_UNLOCK(hi2c);
  5944. return HAL_ERROR;
  5945. }
  5946. /* Check for the Timeout */
  5947. if (Timeout != HAL_MAX_DELAY)
  5948. {
  5949. if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
  5950. {
  5951. hi2c->PreviousState = I2C_STATE_NONE;
  5952. hi2c->State = HAL_I2C_STATE_READY;
  5953. hi2c->Mode = HAL_I2C_MODE_NONE;
  5954. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  5955. /* Process Unlocked */
  5956. __HAL_UNLOCK(hi2c);
  5957. return HAL_ERROR;
  5958. }
  5959. }
  5960. }
  5961. return HAL_OK;
  5962. }
  5963. /**
  5964. * @brief This function handles I2C Communication Timeout for specific usage of TXE flag.
  5965. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  5966. * the configuration information for the specified I2C.
  5967. * @param Timeout Timeout duration
  5968. * @param Tickstart Tick start value
  5969. * @retval HAL status
  5970. */
  5971. static HAL_StatusTypeDef I2C_WaitOnTXEFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart)
  5972. {
  5973. while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXE) == RESET)
  5974. {
  5975. /* Check if a NACK is detected */
  5976. if (I2C_IsAcknowledgeFailed(hi2c) != HAL_OK)
  5977. {
  5978. return HAL_ERROR;
  5979. }
  5980. /* Check for the Timeout */
  5981. if (Timeout != HAL_MAX_DELAY)
  5982. {
  5983. if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
  5984. {
  5985. hi2c->PreviousState = I2C_STATE_NONE;
  5986. hi2c->State = HAL_I2C_STATE_READY;
  5987. hi2c->Mode = HAL_I2C_MODE_NONE;
  5988. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  5989. /* Process Unlocked */
  5990. __HAL_UNLOCK(hi2c);
  5991. return HAL_ERROR;
  5992. }
  5993. }
  5994. }
  5995. return HAL_OK;
  5996. }
  5997. /**
  5998. * @brief This function handles I2C Communication Timeout for specific usage of BTF flag.
  5999. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  6000. * the configuration information for the specified I2C.
  6001. * @param Timeout Timeout duration
  6002. * @param Tickstart Tick start value
  6003. * @retval HAL status
  6004. */
  6005. static HAL_StatusTypeDef I2C_WaitOnBTFFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart)
  6006. {
  6007. while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == RESET)
  6008. {
  6009. /* Check if a NACK is detected */
  6010. if (I2C_IsAcknowledgeFailed(hi2c) != HAL_OK)
  6011. {
  6012. return HAL_ERROR;
  6013. }
  6014. /* Check for the Timeout */
  6015. if (Timeout != HAL_MAX_DELAY)
  6016. {
  6017. if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
  6018. {
  6019. hi2c->PreviousState = I2C_STATE_NONE;
  6020. hi2c->State = HAL_I2C_STATE_READY;
  6021. hi2c->Mode = HAL_I2C_MODE_NONE;
  6022. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  6023. /* Process Unlocked */
  6024. __HAL_UNLOCK(hi2c);
  6025. return HAL_ERROR;
  6026. }
  6027. }
  6028. }
  6029. return HAL_OK;
  6030. }
  6031. /**
  6032. * @brief This function handles I2C Communication Timeout for specific usage of STOP flag.
  6033. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  6034. * the configuration information for the specified I2C.
  6035. * @param Timeout Timeout duration
  6036. * @param Tickstart Tick start value
  6037. * @retval HAL status
  6038. */
  6039. static HAL_StatusTypeDef I2C_WaitOnSTOPFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart)
  6040. {
  6041. while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == RESET)
  6042. {
  6043. /* Check if a NACK is detected */
  6044. if (I2C_IsAcknowledgeFailed(hi2c) != HAL_OK)
  6045. {
  6046. return HAL_ERROR;
  6047. }
  6048. /* Check for the Timeout */
  6049. if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
  6050. {
  6051. hi2c->PreviousState = I2C_STATE_NONE;
  6052. hi2c->State = HAL_I2C_STATE_READY;
  6053. hi2c->Mode = HAL_I2C_MODE_NONE;
  6054. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  6055. /* Process Unlocked */
  6056. __HAL_UNLOCK(hi2c);
  6057. return HAL_ERROR;
  6058. }
  6059. }
  6060. return HAL_OK;
  6061. }
  6062. /**
  6063. * @brief This function handles I2C Communication Timeout for specific usage of STOP request through Interrupt.
  6064. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  6065. * the configuration information for the specified I2C.
  6066. * @retval HAL status
  6067. */
  6068. static HAL_StatusTypeDef I2C_WaitOnSTOPRequestThroughIT(I2C_HandleTypeDef *hi2c)
  6069. {
  6070. __IO uint32_t count = 0U;
  6071. /* Wait until STOP flag is reset */
  6072. count = I2C_TIMEOUT_STOP_FLAG * (SystemCoreClock / 25U / 1000U);
  6073. do
  6074. {
  6075. count--;
  6076. if (count == 0U)
  6077. {
  6078. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  6079. return HAL_ERROR;
  6080. }
  6081. }
  6082. while (READ_BIT(hi2c->Instance->CR1, I2C_CR1_STOP) == I2C_CR1_STOP);
  6083. return HAL_OK;
  6084. }
  6085. /**
  6086. * @brief This function handles I2C Communication Timeout for specific usage of RXNE flag.
  6087. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  6088. * the configuration information for the specified I2C.
  6089. * @param Timeout Timeout duration
  6090. * @param Tickstart Tick start value
  6091. * @retval HAL status
  6092. */
  6093. static HAL_StatusTypeDef I2C_WaitOnRXNEFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart)
  6094. {
  6095. while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == RESET)
  6096. {
  6097. /* Check if a STOPF is detected */
  6098. if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET)
  6099. {
  6100. /* Clear STOP Flag */
  6101. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF);
  6102. hi2c->PreviousState = I2C_STATE_NONE;
  6103. hi2c->State = HAL_I2C_STATE_READY;
  6104. hi2c->Mode = HAL_I2C_MODE_NONE;
  6105. hi2c->ErrorCode |= HAL_I2C_ERROR_NONE;
  6106. /* Process Unlocked */
  6107. __HAL_UNLOCK(hi2c);
  6108. return HAL_ERROR;
  6109. }
  6110. /* Check for the Timeout */
  6111. if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
  6112. {
  6113. hi2c->PreviousState = I2C_STATE_NONE;
  6114. hi2c->State = HAL_I2C_STATE_READY;
  6115. hi2c->Mode = HAL_I2C_MODE_NONE;
  6116. hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
  6117. /* Process Unlocked */
  6118. __HAL_UNLOCK(hi2c);
  6119. return HAL_ERROR;
  6120. }
  6121. }
  6122. return HAL_OK;
  6123. }
  6124. /**
  6125. * @brief This function handles Acknowledge failed detection during an I2C Communication.
  6126. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  6127. * the configuration information for the specified I2C.
  6128. * @retval HAL status
  6129. */
  6130. static HAL_StatusTypeDef I2C_IsAcknowledgeFailed(I2C_HandleTypeDef *hi2c)
  6131. {
  6132. if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET)
  6133. {
  6134. /* Clear NACKF Flag */
  6135. __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
  6136. hi2c->PreviousState = I2C_STATE_NONE;
  6137. hi2c->State = HAL_I2C_STATE_READY;
  6138. hi2c->Mode = HAL_I2C_MODE_NONE;
  6139. hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
  6140. /* Process Unlocked */
  6141. __HAL_UNLOCK(hi2c);
  6142. return HAL_ERROR;
  6143. }
  6144. return HAL_OK;
  6145. }
  6146. /**
  6147. * @brief Convert I2Cx OTHER_xxx XferOptions to functionnal XferOptions.
  6148. * @param hi2c I2C handle.
  6149. * @retval None
  6150. */
  6151. static void I2C_ConvertOtherXferOptions(I2C_HandleTypeDef *hi2c)
  6152. {
  6153. /* if user set XferOptions to I2C_OTHER_FRAME */
  6154. /* it request implicitly to generate a restart condition */
  6155. /* set XferOptions to I2C_FIRST_FRAME */
  6156. if (hi2c->XferOptions == I2C_OTHER_FRAME)
  6157. {
  6158. hi2c->XferOptions = I2C_FIRST_FRAME;
  6159. }
  6160. /* else if user set XferOptions to I2C_OTHER_AND_LAST_FRAME */
  6161. /* it request implicitly to generate a restart condition */
  6162. /* then generate a stop condition at the end of transfer */
  6163. /* set XferOptions to I2C_FIRST_AND_LAST_FRAME */
  6164. else if (hi2c->XferOptions == I2C_OTHER_AND_LAST_FRAME)
  6165. {
  6166. hi2c->XferOptions = I2C_FIRST_AND_LAST_FRAME;
  6167. }
  6168. else
  6169. {
  6170. /* Nothing to do */
  6171. }
  6172. }
  6173. /**
  6174. * @}
  6175. */
  6176. #endif /* HAL_I2C_MODULE_ENABLED */
  6177. /**
  6178. * @}
  6179. */
  6180. /**
  6181. * @}
  6182. */
  6183. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/