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

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