Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 

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