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.
 
 
 

6523 lines
211 KiB

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