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.
 
 
 

6503 lines
210 KiB

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