25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 

7558 satır
253 KiB

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