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

1782 lines
54 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32h7xx_hal_pssi.c
  4. * @author MCD Application Team
  5. * @brief PSSI HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the Parallel Synchronous Slave Interface (PSSI) 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 PSSI HAL driver can be used as follows:
  18. (#) Declare a PSSI_HandleTypeDef handle structure, for example:
  19. PSSI_HandleTypeDef hpssi;
  20. (#) Initialize the PSSI low level resources by implementing the @ref HAL_PSSI_MspInit() API:
  21. (##) Enable the PSSIx interface clock
  22. (##) PSSI pins configuration
  23. (+++) Enable the clock for the PSSI GPIOs
  24. (+++) Configure PSSI pins as alternate function open-drain
  25. (##) NVIC configuration if you need to use interrupt process
  26. (+++) Configure the PSSIx interrupt priority
  27. (+++) Enable the NVIC PSSI IRQ Channel
  28. (##) DMA Configuration if you need to use DMA process
  29. (+++) Declare DMA_HandleTypeDef handles structure for the transmit and receive
  30. (+++) Enable the DMAx interface clock
  31. (+++) Configure the DMA handle parameters
  32. (+++) Configure the DMA Tx and Rx
  33. (+++) Associate the initialized DMA handle to the hpssi DMA Tx and Rx handle
  34. (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on
  35. the DMA Tx and Rx
  36. (#) Configure the Communication Bus Width, Control Signals, Input Polarity and Output Polarity
  37. in the hpssi Init structure.
  38. (#) Initialize the PSSI registers by calling the @ref HAL_PSSI_Init(), configure also the low level Hardware
  39. (GPIO, CLOCK, NVIC...etc) by calling the customized @ref HAL_PSSI_MspInit(&hpssi) API.
  40. (#) For PSSI IO operations, two operation modes are available within this driver :
  41. *** Polling mode IO operation ***
  42. =================================
  43. [..]
  44. (+) Transmit an amount of data by byte in blocking mode using @ref HAL_PSSI_Transmit()
  45. (+) Receive an amount of data by byte in blocking mode using @ref HAL_PSSI_Receive()
  46. *** DMA mode IO operation ***
  47. ==============================
  48. [..]
  49. (+) Transmit an amount of data in non-blocking mode (DMA) using
  50. @ref HAL_PSSI_Transmit_DMA()
  51. (+) At transmission end of transfer, @ref HAL_PSSI_TxCpltCallback() is executed and user can
  52. add his own code by customization of function pointer @ref HAL_PSSI_TxCpltCallback()
  53. (+) Receive an amount of data in non-blocking mode (DMA) using
  54. @ref HAL_PSSI_Receive_DMA()
  55. (+) At reception end of transfer, @ref HAL_PSSI_RxCpltCallback() is executed and user can
  56. add his own code by customization of function pointer @ref HAL_PSSI_RxCpltCallback()
  57. (+) In case of transfer Error, @ref HAL_PSSI_ErrorCallback() function is executed and user can
  58. add his own code by customization of function pointer @ref HAL_PSSI_ErrorCallback()
  59. (+) Abort a PSSI process communication with Interrupt using @ref HAL_PSSI_Abort_IT()
  60. (+) End of abort process, @ref HAL_PSSI_AbortCpltCallback() is executed and user can
  61. add his own code by customization of function pointer @ref HAL_PSSI_AbortCpltCallback()
  62. *** PSSI HAL driver macros list ***
  63. ==================================
  64. [..]
  65. Below the list of most used macros in PSSI HAL driver.
  66. (+) @ref HAL_PSSI_ENABLE : Enable the PSSI peripheral
  67. (+) @ref HAL_PSSI_DISABLE : Disable the PSSI peripheral
  68. (+) @ref HAL_PSSI_GET_FLAG : Check whether the specified PSSI flag is set or not
  69. (+) @ref HAL_PSSI_CLEAR_FLAG : Clear the specified PSSI pending flag
  70. (+) @ref HAL_PSSI_ENABLE_IT : Enable the specified PSSI interrupt
  71. (+) @ref HAL_PSSI_DISABLE_IT : Disable the specified PSSI interrupt
  72. *** Callback registration ***
  73. =============================================
  74. Use Functions @ref HAL_PSSI_RegisterCallback() or @ref HAL_PSSI_RegisterAddrCallback()
  75. to register an interrupt callback.
  76. Function @ref HAL_PSSI_RegisterCallback() allows to register following callbacks:
  77. (+) TxCpltCallback : callback for transmission end of transfer.
  78. (+) RxCpltCallback : callback for reception end of transfer.
  79. (+) ErrorCallback : callback for error detection.
  80. (+) AbortCpltCallback : callback for abort completion process.
  81. (+) MspInitCallback : callback for Msp Init.
  82. (+) MspDeInitCallback : callback for Msp DeInit.
  83. This function takes as parameters the HAL peripheral handle, the Callback ID
  84. and a pointer to the user callback function.
  85. Use function @ref HAL_PSSI_UnRegisterCallback to reset a callback to the default
  86. weak function.
  87. @ref HAL_PSSI_UnRegisterCallback takes as parameters the HAL peripheral handle,
  88. and the Callback ID.
  89. This function allows to reset following callbacks:
  90. (+) TxCpltCallback : callback for transmission end of transfer.
  91. (+) RxCpltCallback : callback for reception end of transfer.
  92. (+) ErrorCallback : callback for error detection.
  93. (+) AbortCpltCallback : callback for abort completion process.
  94. (+) MspInitCallback : callback for Msp Init.
  95. (+) MspDeInitCallback : callback for Msp DeInit.
  96. By default, after the @ref HAL_PSSI_Init() and when the state is @ref HAL_PSSI_STATE_RESET
  97. all callbacks are set to the corresponding weak functions:
  98. examples @ref HAL_PSSI_TxCpltCallback(), @ref HAL_PSSI_RxCpltCallback().
  99. Exception done for MspInit and MspDeInit functions that are
  100. reset to the legacy weak functions in the @ref HAL_PSSI_Init()/ @ref HAL_PSSI_DeInit() only when
  101. these callbacks are null (not registered beforehand).
  102. If MspInit or MspDeInit are not null, the @ref HAL_PSSI_Init()/ @ref HAL_PSSI_DeInit()
  103. keep and use the user MspInit/MspDeInit callbacks (registered beforehand) whatever the state.
  104. Callbacks can be registered/unregistered in @ref HAL_PSSI_STATE_READY state only.
  105. Exception done MspInit/MspDeInit functions that can be registered/unregistered
  106. in @ref HAL_PSSI_STATE_READY or @ref HAL_PSSI_STATE_RESET state,
  107. thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
  108. Then, the user first registers the MspInit/MspDeInit user callbacks
  109. using @ref HAL_PSSI_RegisterCallback() before calling @ref HAL_PSSI_DeInit()
  110. or @ref HAL_PSSI_Init() function.
  111. [..]
  112. (@) You can refer to the PSSI HAL driver header file for more useful macros
  113. @endverbatim
  114. ******************************************************************************
  115. * @attention
  116. *
  117. * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
  118. * All rights reserved.</center></h2>
  119. *
  120. * This software component is licensed by ST under BSD 3-Clause license,
  121. * the "License"; You may not use this file except in compliance with the
  122. * License. You may obtain a copy of the License at:
  123. * opensource.org/licenses/BSD-3-Clause
  124. *
  125. ******************************************************************************
  126. */
  127. /* Includes ------------------------------------------------------------------*/
  128. #include "stm32h7xx_hal.h"
  129. /** @addtogroup STM32H7xx_HAL_Driver
  130. * @{
  131. */
  132. /** @defgroup PSSI PSSI
  133. * @brief PSSI HAL module driver
  134. * @{
  135. */
  136. #ifdef HAL_PSSI_MODULE_ENABLED
  137. #if defined(PSSI)
  138. /* Private typedef -----------------------------------------------------------*/
  139. /* Private define ------------------------------------------------------------*/
  140. /** @defgroup PSSI_Private_Define PSSI Private Define
  141. * @{
  142. */
  143. /**
  144. * @}
  145. */
  146. /* Private macro -------------------------------------------------------------*/
  147. /* Private variables ---------------------------------------------------------*/
  148. /* Private function prototypes -----------------------------------------------*/
  149. /** @defgroup PSSI_Private_Functions PSSI Private Functions
  150. * @{
  151. */
  152. /* Private functions to handle DMA transfer */
  153. void PSSI_DMATransmitCplt(DMA_HandleTypeDef *hdma);
  154. void PSSI_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
  155. void PSSI_DMAError(DMA_HandleTypeDef *hdma);
  156. void PSSI_DMAAbort(DMA_HandleTypeDef *hdma);
  157. /* Private functions to handle IT transfer */
  158. static void PSSI_Error(PSSI_HandleTypeDef *hpssi, uint32_t ErrorCode);
  159. /* Private functions to handle flags during polling transfer */
  160. static HAL_StatusTypeDef PSSI_WaitOnStatusUntilTimeout(PSSI_HandleTypeDef *hpssi, uint32_t Flag, FlagStatus Status, uint32_t Timeout, uint32_t Tickstart);
  161. /**
  162. * @}
  163. */
  164. /* Exported functions --------------------------------------------------------*/
  165. /** @defgroup PSSI_Exported_Functions PSSI Exported Functions
  166. * @{
  167. */
  168. /** @defgroup PSSI_Exported_Functions_Group1 Initialization and de-initialization functions
  169. * @brief Initialization and Configuration functions
  170. *
  171. @verbatim
  172. ===============================================================================
  173. ##### Initialization and de-initialization functions #####
  174. ===============================================================================
  175. [..] This subsection provides a set of functions allowing to initialize and
  176. deinitialize the PSSIx peripheral:
  177. (+) User must implement HAL_PSSI_MspInit() function in which he configures
  178. all related peripherals resources (CLOCK, GPIO, DMA, IT and NVIC ).
  179. (+) Call the function HAL_PSSI_Init() to configure the selected device with
  180. the selected configuration:
  181. (++) Data Width
  182. (++) Control Signals
  183. (++) Input Clock polarity
  184. (++) Output Clock polarity
  185. (+) Call the function HAL_PSSI_DeInit() to restore the default configuration
  186. of the selected PSSIx peripheral.
  187. @endverbatim
  188. * @{
  189. */
  190. /**
  191. * @brief Initializes the PSSI according to the specified parameters
  192. * in the PSSI_InitTypeDef and initialize the associated handle.
  193. * @param hpssi Pointer to a PSSI_HandleTypeDef structure that contains
  194. * the configuration information for the specified PSSI.
  195. * @retval HAL status
  196. */
  197. HAL_StatusTypeDef HAL_PSSI_Init(PSSI_HandleTypeDef *hpssi)
  198. {
  199. /* Check the PSSI handle allocation */
  200. if (hpssi == NULL)
  201. {
  202. return HAL_ERROR;
  203. }
  204. /* Check the parameters */
  205. assert_param(IS_PSSI_ALL_INSTANCE(hpssi->Instance));
  206. assert_param(IS_PSSI_CONTROL_SIGNAL(hpssi->Init.ControlSignal));
  207. assert_param(IS_PSSI_BUSWIDTH(hpssi->Init.BusWidth));
  208. assert_param(IS_PSSI_CLOCK_POLARITY(hpssi->Init.ClockPolarity));
  209. assert_param(IS_PSSI_DE_POLARITY(hpssi->Init.DataEnablePolarity));
  210. assert_param(IS_PSSI_RDY_POLARITY(hpssi->Init.ReadyPolarity));
  211. if (hpssi->State == HAL_PSSI_STATE_RESET)
  212. {
  213. /* Allocate lock resource and initialize it */
  214. hpssi->Lock = HAL_UNLOCKED;
  215. /* Init the PSSI Callback settings */
  216. hpssi->TxCpltCallback = HAL_PSSI_TxCpltCallback; /* Legacy weak TxCpltCallback */
  217. hpssi->RxCpltCallback = HAL_PSSI_RxCpltCallback; /* Legacy weak RxCpltCallback */
  218. hpssi->ErrorCallback = HAL_PSSI_ErrorCallback; /* Legacy weak ErrorCallback */
  219. hpssi->AbortCpltCallback = HAL_PSSI_AbortCpltCallback; /* Legacy weak AbortCpltCallback */
  220. if (hpssi->MspInitCallback == NULL)
  221. {
  222. hpssi->MspInitCallback = HAL_PSSI_MspInit; /* Legacy weak MspInit */
  223. }
  224. /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */
  225. hpssi->MspInitCallback(hpssi);
  226. }
  227. hpssi->State = HAL_PSSI_STATE_BUSY;
  228. /* Disable the selected PSSI peripheral */
  229. HAL_PSSI_DISABLE(hpssi);
  230. /*---------------------------- PSSIx CR Configuration ----------------------*/
  231. /* Configure PSSIx: Control Signal and Bus Width*/
  232. MODIFY_REG(hpssi->Instance->CR,PSSI_CR_DERDYCFG|PSSI_CR_EDM|PSSI_CR_DEPOL|PSSI_CR_RDYPOL,
  233. hpssi->Init.ControlSignal|hpssi->Init.DataEnablePolarity|hpssi->Init.ReadyPolarity|hpssi->Init.BusWidth);
  234. hpssi->ErrorCode = HAL_PSSI_ERROR_NONE;
  235. hpssi->State = HAL_PSSI_STATE_READY;
  236. return HAL_OK;
  237. }
  238. /**
  239. * @brief DeInitialize the PSSI peripheral.
  240. * @param hpssi Pointer to a PSSI_HandleTypeDef structure that contains
  241. * the configuration information for the specified PSSI.
  242. * @retval HAL status
  243. */
  244. HAL_StatusTypeDef HAL_PSSI_DeInit(PSSI_HandleTypeDef *hpssi)
  245. {
  246. /* Check the PSSI handle allocation */
  247. if (hpssi == NULL)
  248. {
  249. return HAL_ERROR;
  250. }
  251. /* Check the parameters */
  252. assert_param(IS_PSSI_ALL_INSTANCE(hpssi->Instance));
  253. hpssi->State = HAL_PSSI_STATE_BUSY;
  254. /* Disable the PSSI Peripheral Clock */
  255. HAL_PSSI_DISABLE(hpssi);
  256. if (hpssi->MspDeInitCallback == NULL)
  257. {
  258. hpssi->MspDeInitCallback = HAL_PSSI_MspDeInit; /* Legacy weak MspDeInit */
  259. }
  260. /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
  261. hpssi->MspDeInitCallback(hpssi);
  262. hpssi->ErrorCode = HAL_PSSI_ERROR_NONE;
  263. hpssi->State = HAL_PSSI_STATE_RESET;
  264. /* Release Lock */
  265. __HAL_UNLOCK(hpssi);
  266. return HAL_OK;
  267. }
  268. /**
  269. * @brief Initialize the PSSI MSP.
  270. * @param hpssi Pointer to a PSSI_HandleTypeDef structure that contains
  271. * the configuration information for the specified PSSI.
  272. * @retval None
  273. */
  274. __weak void HAL_PSSI_MspInit(PSSI_HandleTypeDef *hpssi)
  275. {
  276. /* Prevent unused argument(s) compilation warning */
  277. UNUSED(hpssi);
  278. /* NOTE : This function should not be modified, when the callback is needed,
  279. the HAL_PSSI_MspInit can be implemented in the user file
  280. */
  281. }
  282. /**
  283. * @brief DeInitialize the PSSI MSP.
  284. * @param hpssi Pointer to a PSSI_HandleTypeDef structure that contains
  285. * the configuration information for the specified PSSI.
  286. * @retval None
  287. */
  288. __weak void HAL_PSSI_MspDeInit(PSSI_HandleTypeDef *hpssi)
  289. {
  290. /* Prevent unused argument(s) compilation warning */
  291. UNUSED(hpssi);
  292. /* NOTE : This function should not be modified, when the callback is needed,
  293. the HAL_PSSI_MspDeInit can be implemented in the user file
  294. */
  295. }
  296. /**
  297. * @brief Register a User PSSI Callback
  298. * To be used instead of the weak predefined callback
  299. * @param hpssi Pointer to a PSSI_HandleTypeDef structure that contains
  300. * the configuration information for the specified PSSI.
  301. * @param CallbackID ID of the callback to be registered
  302. * This parameter can be one of the following values:
  303. * @arg @ref HAL_PSSI_TX_COMPLETE_CB_ID Tx Transfer completed callback ID
  304. * @arg @ref HAL_PSSI_RX_COMPLETE_CB_ID Rx Transfer completed callback ID
  305. * @arg @ref HAL_PSSI_ERROR_CB_ID Error callback ID
  306. * @arg @ref HAL_PSSI_ABORT_CB_ID Abort callback ID
  307. * @arg @ref HAL_PSSI_MSPINIT_CB_ID MspInit callback ID
  308. * @arg @ref HAL_PSSI_MSPDEINIT_CB_ID MspDeInit callback ID
  309. * @param pCallback pointer to the Callback function
  310. * @retval HAL status
  311. */
  312. HAL_StatusTypeDef HAL_PSSI_RegisterCallback(PSSI_HandleTypeDef *hpssi, HAL_PSSI_CallbackIDTypeDef CallbackID, pPSSI_CallbackTypeDef pCallback)
  313. {
  314. HAL_StatusTypeDef status = HAL_OK;
  315. if (pCallback == NULL)
  316. {
  317. /* Update the error code */
  318. hpssi->ErrorCode |= HAL_PSSI_ERROR_INVALID_CALLBACK;
  319. return HAL_ERROR;
  320. }
  321. /* Process locked */
  322. __HAL_LOCK(hpssi);
  323. if (HAL_PSSI_STATE_READY == hpssi->State)
  324. {
  325. switch (CallbackID)
  326. {
  327. case HAL_PSSI_TX_COMPLETE_CB_ID :
  328. hpssi->TxCpltCallback = pCallback;
  329. break;
  330. case HAL_PSSI_RX_COMPLETE_CB_ID :
  331. hpssi->RxCpltCallback = pCallback;
  332. break;
  333. case HAL_PSSI_ERROR_CB_ID :
  334. hpssi->ErrorCallback = pCallback;
  335. break;
  336. case HAL_PSSI_ABORT_CB_ID :
  337. hpssi->AbortCpltCallback = pCallback;
  338. break;
  339. case HAL_PSSI_MSPINIT_CB_ID :
  340. hpssi->MspInitCallback = pCallback;
  341. break;
  342. case HAL_PSSI_MSPDEINIT_CB_ID :
  343. hpssi->MspDeInitCallback = pCallback;
  344. break;
  345. default :
  346. /* Update the error code */
  347. hpssi->ErrorCode |= HAL_PSSI_ERROR_INVALID_CALLBACK;
  348. /* Return error status */
  349. status = HAL_ERROR;
  350. break;
  351. }
  352. }
  353. else if (HAL_PSSI_STATE_RESET == hpssi->State)
  354. {
  355. switch (CallbackID)
  356. {
  357. case HAL_PSSI_MSPINIT_CB_ID :
  358. hpssi->MspInitCallback = pCallback;
  359. break;
  360. case HAL_PSSI_MSPDEINIT_CB_ID :
  361. hpssi->MspDeInitCallback = pCallback;
  362. break;
  363. default :
  364. /* Update the error code */
  365. hpssi->ErrorCode |= HAL_PSSI_ERROR_INVALID_CALLBACK;
  366. /* Return error status */
  367. status = HAL_ERROR;
  368. break;
  369. }
  370. }
  371. else
  372. {
  373. /* Update the error code */
  374. hpssi->ErrorCode |= HAL_PSSI_ERROR_INVALID_CALLBACK;
  375. /* Return error status */
  376. status = HAL_ERROR;
  377. }
  378. /* Release Lock */
  379. __HAL_UNLOCK(hpssi);
  380. return status;
  381. }
  382. /**
  383. * @brief Unregister an PSSI Callback
  384. * PSSI callback is redirected to the weak predefined callback
  385. * @param hpssi Pointer to a PSSI_HandleTypeDef structure that contains
  386. * the configuration information for the specified PSSI.
  387. * @param CallbackID ID of the callback to be unregistered
  388. * This parameter can be one of the following values:
  389. * @arg @ref HAL_PSSI_TX_COMPLETE_CB_ID Tx Transfer completed callback ID
  390. * @arg @ref HAL_PSSI_RX_COMPLETE_CB_ID Rx Transfer completed callback ID
  391. * @arg @ref HAL_PSSI_ERROR_CB_ID Error callback ID
  392. * @arg @ref HAL_PSSI_ABORT_CB_ID Abort callback ID
  393. * @arg @ref HAL_PSSI_MSPINIT_CB_ID MspInit callback ID
  394. * @arg @ref HAL_PSSI_MSPDEINIT_CB_ID MspDeInit callback ID
  395. * @retval HAL status
  396. */
  397. HAL_StatusTypeDef HAL_PSSI_UnRegisterCallback(PSSI_HandleTypeDef *hpssi, HAL_PSSI_CallbackIDTypeDef CallbackID)
  398. {
  399. HAL_StatusTypeDef status = HAL_OK;
  400. /* Process locked */
  401. __HAL_LOCK(hpssi);
  402. if (HAL_PSSI_STATE_READY == hpssi->State)
  403. {
  404. switch (CallbackID)
  405. {
  406. case HAL_PSSI_TX_COMPLETE_CB_ID :
  407. hpssi->TxCpltCallback = HAL_PSSI_TxCpltCallback; /* Legacy weak TxCpltCallback */
  408. break;
  409. case HAL_PSSI_RX_COMPLETE_CB_ID :
  410. hpssi->RxCpltCallback = HAL_PSSI_RxCpltCallback; /* Legacy weak RxCpltCallback */
  411. break;
  412. case HAL_PSSI_ERROR_CB_ID :
  413. hpssi->ErrorCallback = HAL_PSSI_ErrorCallback; /* Legacy weak ErrorCallback */
  414. break;
  415. case HAL_PSSI_ABORT_CB_ID :
  416. hpssi->AbortCpltCallback = HAL_PSSI_AbortCpltCallback; /* Legacy weak AbortCpltCallback */
  417. break;
  418. case HAL_PSSI_MSPINIT_CB_ID :
  419. hpssi->MspInitCallback = HAL_PSSI_MspInit; /* Legacy weak MspInit */
  420. break;
  421. case HAL_PSSI_MSPDEINIT_CB_ID :
  422. hpssi->MspDeInitCallback = HAL_PSSI_MspDeInit; /* Legacy weak MspDeInit */
  423. break;
  424. default :
  425. /* Update the error code */
  426. hpssi->ErrorCode |= HAL_PSSI_ERROR_INVALID_CALLBACK;
  427. /* Return error status */
  428. status = HAL_ERROR;
  429. break;
  430. }
  431. }
  432. else if (HAL_PSSI_STATE_RESET == hpssi->State)
  433. {
  434. switch (CallbackID)
  435. {
  436. case HAL_PSSI_MSPINIT_CB_ID :
  437. hpssi->MspInitCallback = HAL_PSSI_MspInit; /* Legacy weak MspInit */
  438. break;
  439. case HAL_PSSI_MSPDEINIT_CB_ID :
  440. hpssi->MspDeInitCallback = HAL_PSSI_MspDeInit; /* Legacy weak MspDeInit */
  441. break;
  442. default :
  443. /* Update the error code */
  444. hpssi->ErrorCode |= HAL_PSSI_ERROR_INVALID_CALLBACK;
  445. /* Return error status */
  446. status = HAL_ERROR;
  447. break;
  448. }
  449. }
  450. else
  451. {
  452. /* Update the error code */
  453. hpssi->ErrorCode |= HAL_PSSI_ERROR_INVALID_CALLBACK;
  454. /* Return error status */
  455. status = HAL_ERROR;
  456. }
  457. /* Release Lock */
  458. __HAL_UNLOCK(hpssi);
  459. return status;
  460. }
  461. /**
  462. * @}
  463. */
  464. /** @defgroup PSSI_Exported_Functions_Group2 Input and Output operation functions
  465. * @brief Data transfers functions
  466. *
  467. @verbatim
  468. ===============================================================================
  469. ##### IO operation functions #####
  470. ===============================================================================
  471. [..]
  472. This subsection provides a set of functions allowing to manage the PSSI data
  473. transfers.
  474. (#) There are two modes of transfer:
  475. (++) Blocking mode : The communication is performed in the polling mode.
  476. The status of all data processing is returned by the same function
  477. after finishing transfer.
  478. (++) No-Blocking mode : The communication is performed using DMA.
  479. These functions return the status of the transfer startup.
  480. The end of the data processing will be indicated through the
  481. dedicated the DMA IRQ .
  482. (#) Blocking mode functions are :
  483. (++) HAL_PSSI_Transmit()
  484. (++) HAL_PSSI_Receive()
  485. (#) No-Blocking mode functions with DMA are :
  486. (++) HAL_PSSI_Transmit_DMA()
  487. (++) HAL_PSSI_Receive_DMA()
  488. (#) A set of Transfer Complete Callbacks are provided in non Blocking mode:
  489. (++) HAL_PSSI_TxCpltCallback()
  490. (++) HAL_PSSI_RxCpltCallback()
  491. (++) HAL_PSSI_ErrorCallback()
  492. (++) HAL_PSSI_AbortCpltCallback()
  493. @endverbatim
  494. * @{
  495. */
  496. /**
  497. * @brief Transmits in master mode an amount of data in blocking mode.
  498. * @param hpssi Pointer to a PSSI_HandleTypeDef structure that contains
  499. * the configuration information for the specified PSSI.
  500. * @param pData Pointer to data buffer
  501. * @param Size Amount of data to be sent (in bytes)
  502. * @param Timeout Timeout duration
  503. * @retval HAL status
  504. */
  505. HAL_StatusTypeDef HAL_PSSI_Transmit(PSSI_HandleTypeDef *hpssi, uint8_t *pData, uint32_t Size, uint32_t Timeout)
  506. {
  507. uint32_t tickstart;
  508. uint32_t transfer_size = Size;
  509. if (((hpssi->Init.DataWidth == HAL_PSSI_8BITS) && (hpssi->Init.BusWidth != HAL_PSSI_8LINES)) ||
  510. ((hpssi->Init.DataWidth == HAL_PSSI_16BITS) && ((Size%2U) != 0U)) ||
  511. ((hpssi->Init.DataWidth == HAL_PSSI_32BITS) && ((Size%4U) != 0U)))
  512. {
  513. hpssi->ErrorCode = HAL_PSSI_ERROR_NOT_SUPPORTED;
  514. return HAL_ERROR;
  515. }
  516. if (hpssi->State == HAL_PSSI_STATE_READY)
  517. {
  518. /* Process Locked */
  519. __HAL_LOCK(hpssi);
  520. hpssi->State = HAL_PSSI_STATE_BUSY;
  521. hpssi->ErrorCode = HAL_PSSI_ERROR_NONE;
  522. /* Disable the selected PSSI peripheral */
  523. HAL_PSSI_DISABLE(hpssi);
  524. /* Configure transfer parameters */
  525. hpssi->Instance->CR |= PSSI_CR_OUTEN_OUTPUT |
  526. ((hpssi->Init.ClockPolarity == HAL_PSSI_RISING_EDGE)?0U:PSSI_CR_CKPOL);
  527. /* DMA Disable */
  528. hpssi->Instance->CR &= PSSI_CR_DMA_DISABLE;
  529. /* Enable the selected PSSI peripheral */
  530. HAL_PSSI_ENABLE(hpssi);
  531. if (hpssi->Init.DataWidth == HAL_PSSI_8BITS)
  532. {
  533. uint8_t *pbuffer = pData;
  534. while (transfer_size > 0U)
  535. {
  536. /* Init tickstart for timeout management*/
  537. tickstart = HAL_GetTick();
  538. /* Wait until Fifo is ready to transfer one byte flag is set */
  539. if (PSSI_WaitOnStatusUntilTimeout(hpssi, PSSI_FLAG_RTT1B, RESET, Timeout, tickstart) != HAL_OK)
  540. {
  541. hpssi->ErrorCode = HAL_PSSI_ERROR_TIMEOUT;
  542. hpssi->State = HAL_PSSI_STATE_READY;
  543. /* Process Unlocked */
  544. __HAL_UNLOCK(hpssi);
  545. return HAL_ERROR;
  546. }
  547. /* Write data to DR */
  548. *(__IO uint8_t *)(&hpssi->Instance->DR) = *(uint8_t *)pbuffer;
  549. /* Increment Buffer pointer */
  550. pbuffer++;
  551. transfer_size--;
  552. }
  553. }
  554. else if (hpssi->Init.DataWidth == HAL_PSSI_16BITS)
  555. {
  556. uint16_t *pbuffer = (uint16_t *)pData;
  557. while (transfer_size > 0U)
  558. {
  559. /* Init tickstart for timeout management*/
  560. tickstart = HAL_GetTick();
  561. /* Wait until Fifo is ready to transfer four bytes flag is set */
  562. if (PSSI_WaitOnStatusUntilTimeout(hpssi, PSSI_FLAG_RTT4B, RESET, Timeout, tickstart) != HAL_OK)
  563. {
  564. hpssi->ErrorCode = HAL_PSSI_ERROR_TIMEOUT;
  565. hpssi->State = HAL_PSSI_STATE_READY;
  566. /* Process Unlocked */
  567. __HAL_UNLOCK(hpssi);
  568. return HAL_ERROR;
  569. }
  570. /* Write data to DR */
  571. *(__IO uint16_t *)((uint32_t)(&hpssi->Instance->DR)) = *pbuffer;
  572. /* Increment Buffer pointer */
  573. pbuffer++;
  574. transfer_size -= 2U;
  575. }
  576. }
  577. else if (hpssi->Init.DataWidth == HAL_PSSI_32BITS)
  578. {
  579. uint32_t *pbuffer = (uint32_t *)pData;
  580. while (transfer_size > 0U)
  581. {
  582. /* Init tickstart for timeout management*/
  583. tickstart = HAL_GetTick();
  584. /* Wait until Fifo is ready to transfer four bytes flag is set */
  585. if (PSSI_WaitOnStatusUntilTimeout(hpssi, PSSI_FLAG_RTT4B, RESET, Timeout, tickstart) != HAL_OK)
  586. {
  587. hpssi->ErrorCode = HAL_PSSI_ERROR_TIMEOUT;
  588. hpssi->State = HAL_PSSI_STATE_READY;
  589. /* Process Unlocked */
  590. __HAL_UNLOCK(hpssi);
  591. return HAL_ERROR;
  592. }
  593. /* Write data to DR */
  594. *(__IO uint32_t *)(&hpssi->Instance->DR) = *pbuffer;
  595. /* Increment Buffer pointer */
  596. pbuffer++;
  597. transfer_size -= 4U;
  598. }
  599. }
  600. else
  601. {
  602. hpssi->ErrorCode = HAL_PSSI_ERROR_NOT_SUPPORTED;
  603. hpssi->State = HAL_PSSI_STATE_READY;
  604. /* Process Unlocked */
  605. __HAL_UNLOCK(hpssi);
  606. return HAL_ERROR;
  607. }
  608. /* Check Errors Flags */
  609. if (HAL_PSSI_GET_FLAG(hpssi, PSSI_FLAG_OVR_RIS) != 0U)
  610. {
  611. HAL_PSSI_CLEAR_FLAG(hpssi, PSSI_FLAG_OVR_RIS);
  612. HAL_PSSI_DISABLE(hpssi);
  613. hpssi->ErrorCode = HAL_PSSI_ERROR_UNDER_RUN;
  614. hpssi->State = HAL_PSSI_STATE_READY;
  615. /* Process Unlocked */
  616. __HAL_UNLOCK(hpssi);
  617. return HAL_ERROR;
  618. }
  619. hpssi->State = HAL_PSSI_STATE_READY;
  620. /* Process Unlocked */
  621. __HAL_UNLOCK(hpssi);
  622. return HAL_OK;
  623. }
  624. else
  625. {
  626. return HAL_BUSY;
  627. }
  628. }
  629. /**
  630. * @brief Receives an amount of data in blocking mode.
  631. * @param hpssi Pointer to a PSSI_HandleTypeDef structure that contains
  632. * the configuration information for the specified PSSI.
  633. * @param pData Pointer to data buffer
  634. * @param Size Amount of data to be received (in bytes)
  635. * @param Timeout Timeout duration
  636. * @retval HAL status
  637. */
  638. HAL_StatusTypeDef HAL_PSSI_Receive(PSSI_HandleTypeDef *hpssi, uint8_t *pData, uint32_t Size, uint32_t Timeout)
  639. {
  640. uint32_t tickstart;
  641. uint32_t transfer_size = Size;
  642. if (((hpssi->Init.DataWidth == HAL_PSSI_8BITS) && (hpssi->Init.BusWidth != HAL_PSSI_8LINES)) ||
  643. ((hpssi->Init.DataWidth == HAL_PSSI_16BITS) && ((Size%2U) != 0U)) ||
  644. ((hpssi->Init.DataWidth == HAL_PSSI_32BITS) && ((Size%4U) != 0U)))
  645. {
  646. hpssi->ErrorCode = HAL_PSSI_ERROR_NOT_SUPPORTED;
  647. return HAL_ERROR;
  648. }
  649. if (hpssi->State == HAL_PSSI_STATE_READY)
  650. {
  651. /* Process Locked */
  652. __HAL_LOCK(hpssi);
  653. hpssi->State = HAL_PSSI_STATE_BUSY;
  654. hpssi->ErrorCode = HAL_PSSI_ERROR_NONE;
  655. /* Disable the selected PSSI peripheral */
  656. HAL_PSSI_DISABLE(hpssi);
  657. /* Configure transfer parameters */
  658. hpssi->Instance->CR |= PSSI_CR_OUTEN_INPUT |((hpssi->Init.ClockPolarity == HAL_PSSI_FALLING_EDGE)?0U:PSSI_CR_CKPOL);
  659. /* DMA Disable */
  660. hpssi->Instance->CR &= PSSI_CR_DMA_DISABLE;
  661. /* Enable the selected PSSI peripheral */
  662. HAL_PSSI_ENABLE(hpssi);
  663. if (hpssi->Init.DataWidth == HAL_PSSI_8BITS)
  664. {
  665. uint8_t *pbuffer = pData;
  666. while (transfer_size > 0U)
  667. {
  668. /* Init tickstart for timeout management*/
  669. tickstart = HAL_GetTick();
  670. /* Wait until Fifo is ready to receive one byte flag is set */
  671. if (PSSI_WaitOnStatusUntilTimeout(hpssi, PSSI_FLAG_RTT1B, RESET, Timeout, tickstart) != HAL_OK)
  672. {
  673. hpssi->ErrorCode = HAL_PSSI_ERROR_TIMEOUT;
  674. hpssi->State = HAL_PSSI_STATE_READY;
  675. /* Process Unlocked */
  676. __HAL_UNLOCK(hpssi);
  677. return HAL_ERROR;
  678. }
  679. /* Read data from DR */
  680. *pbuffer = *(__IO uint8_t *)(&hpssi->Instance->DR);
  681. pbuffer++;
  682. transfer_size--;
  683. }
  684. }
  685. else if (hpssi->Init.DataWidth == HAL_PSSI_16BITS)
  686. {
  687. uint16_t *pbuffer = (uint16_t *)pData;
  688. while (transfer_size > 0U)
  689. {
  690. /* Init tickstart for timeout management*/
  691. tickstart = HAL_GetTick();
  692. /* Wait until Fifo is ready to receive four bytes flag is set */
  693. if (PSSI_WaitOnStatusUntilTimeout(hpssi, PSSI_FLAG_RTT4B, RESET, Timeout, tickstart) != HAL_OK)
  694. {
  695. hpssi->ErrorCode = HAL_PSSI_ERROR_TIMEOUT;
  696. hpssi->State = HAL_PSSI_STATE_READY;
  697. /* Process Unlocked */
  698. __HAL_UNLOCK(hpssi);
  699. return HAL_ERROR;
  700. }
  701. /* Read data from DR */
  702. *pbuffer = *(__IO uint16_t *)((uint32_t)&hpssi->Instance->DR);
  703. pbuffer++;
  704. transfer_size -= 2U;
  705. }
  706. }
  707. else if (hpssi->Init.DataWidth == HAL_PSSI_32BITS)
  708. {
  709. uint32_t *pbuffer = (uint32_t *)pData;
  710. while (transfer_size > 0U)
  711. {
  712. /* Init tickstart for timeout management*/
  713. tickstart = HAL_GetTick();
  714. /* Wait until Fifo is ready to receive four bytes flag is set */
  715. if (PSSI_WaitOnStatusUntilTimeout(hpssi, PSSI_FLAG_RTT4B, RESET, Timeout, tickstart) != HAL_OK)
  716. {
  717. hpssi->ErrorCode = HAL_PSSI_ERROR_TIMEOUT;
  718. hpssi->State = HAL_PSSI_STATE_READY;
  719. /* Process Unlocked */
  720. __HAL_UNLOCK(hpssi);
  721. return HAL_ERROR;
  722. }
  723. /* Read data from DR */
  724. *pbuffer = *(__IO uint32_t *)(&hpssi->Instance->DR);
  725. pbuffer++;
  726. transfer_size -= 4U;
  727. }
  728. }
  729. else
  730. {
  731. hpssi->ErrorCode = HAL_PSSI_ERROR_NOT_SUPPORTED;
  732. hpssi->State = HAL_PSSI_STATE_READY;
  733. /* Process Unlocked */
  734. __HAL_UNLOCK(hpssi);
  735. return HAL_ERROR;
  736. }
  737. /* Check Errors Flags */
  738. if (HAL_PSSI_GET_FLAG(hpssi, PSSI_FLAG_OVR_RIS) != 0U)
  739. {
  740. HAL_PSSI_CLEAR_FLAG(hpssi, PSSI_FLAG_OVR_RIS);
  741. hpssi->ErrorCode = HAL_PSSI_ERROR_OVER_RUN;
  742. __HAL_UNLOCK(hpssi);
  743. return HAL_ERROR;
  744. }
  745. hpssi->State = HAL_PSSI_STATE_READY;
  746. /* Process Unlocked */
  747. __HAL_UNLOCK(hpssi);
  748. return HAL_OK;
  749. }
  750. else
  751. {
  752. return HAL_BUSY;
  753. }
  754. }
  755. /**
  756. * @brief Transmit an amount of data in non-blocking mode with DMA
  757. * @param hpssi Pointer to a PSSI_HandleTypeDef structure that contains
  758. * the configuration information for the specified PSSI.
  759. * @param pData Pointer to data buffer
  760. * @param Size Amount of data to be sent (in bytes)
  761. * @retval HAL status
  762. */
  763. HAL_StatusTypeDef HAL_PSSI_Transmit_DMA(PSSI_HandleTypeDef *hpssi, uint32_t *pData, uint32_t Size)
  764. {
  765. HAL_StatusTypeDef dmaxferstatus;
  766. if (hpssi->State == HAL_PSSI_STATE_READY)
  767. {
  768. /* Process Locked */
  769. __HAL_LOCK(hpssi);
  770. hpssi->State = HAL_PSSI_STATE_BUSY_TX;
  771. hpssi->ErrorCode = HAL_PSSI_ERROR_NONE;
  772. /* Disable the selected PSSI peripheral */
  773. HAL_PSSI_DISABLE(hpssi);
  774. /* Prepare transfer parameters */
  775. hpssi->pBuffPtr = pData;
  776. hpssi->XferCount = Size;
  777. if (hpssi->XferCount > PSSI_MAX_NBYTE_SIZE)
  778. {
  779. hpssi->XferSize = PSSI_MAX_NBYTE_SIZE;
  780. }
  781. else
  782. {
  783. hpssi->XferSize = hpssi->XferCount;
  784. }
  785. if (hpssi->XferSize > 0U)
  786. {
  787. if (hpssi->hdmatx != NULL)
  788. {
  789. /* Configure BusWidth */
  790. if( hpssi->hdmatx->Init.PeriphDataAlignment == DMA_PDATAALIGN_BYTE)
  791. {
  792. MODIFY_REG(hpssi->Instance->CR,PSSI_CR_DMAEN|PSSI_CR_OUTEN|PSSI_CR_CKPOL,PSSI_CR_DMA_ENABLE | PSSI_CR_OUTEN_OUTPUT |
  793. ((hpssi->Init.ClockPolarity == HAL_PSSI_RISING_EDGE)?0U:PSSI_CR_CKPOL));
  794. }
  795. else
  796. {
  797. MODIFY_REG(hpssi->Instance->CR,PSSI_CR_DMAEN|PSSI_CR_OUTEN|PSSI_CR_CKPOL,PSSI_CR_DMA_ENABLE | hpssi->Init.BusWidth | PSSI_CR_OUTEN_OUTPUT |
  798. ((hpssi->Init.ClockPolarity == HAL_PSSI_RISING_EDGE)?0U:PSSI_CR_CKPOL));
  799. }
  800. /* Set the PSSI DMA transfer complete callback */
  801. hpssi->hdmatx->XferCpltCallback = PSSI_DMATransmitCplt;
  802. /* Set the DMA error callback */
  803. hpssi->hdmatx->XferErrorCallback = PSSI_DMAError;
  804. /* Set the unused DMA callbacks to NULL */
  805. hpssi->hdmatx->XferHalfCpltCallback = NULL;
  806. hpssi->hdmatx->XferAbortCallback = NULL;
  807. /* Enable the DMA */
  808. dmaxferstatus = HAL_DMA_Start_IT(hpssi->hdmatx, (uint32_t)pData, (uint32_t)&hpssi->Instance->DR, hpssi->XferSize);
  809. }
  810. else
  811. {
  812. /* Update PSSI state */
  813. hpssi->State = HAL_PSSI_STATE_READY;
  814. /* Update PSSI error code */
  815. hpssi->ErrorCode |= HAL_PSSI_ERROR_DMA;
  816. /* Process Unlocked */
  817. __HAL_UNLOCK(hpssi);
  818. return HAL_ERROR;
  819. }
  820. if (dmaxferstatus == HAL_OK)
  821. {
  822. /* Update XferCount value */
  823. hpssi->XferCount -= hpssi->XferSize;
  824. /* Process Unlocked */
  825. __HAL_UNLOCK(hpssi);
  826. /* Note : The PSSI interrupts must be enabled after unlocking current process
  827. to avoid the risk of PSSI interrupt handle execution before current
  828. process unlock */
  829. /* Enable ERR interrupt */
  830. HAL_PSSI_ENABLE_IT(hpssi, PSSI_FLAG_OVR_RIS);
  831. /* Enable DMA Request */
  832. hpssi->Instance->CR |= PSSI_CR_DMA_ENABLE;
  833. /* Enable the selected PSSI peripheral */
  834. HAL_PSSI_ENABLE(hpssi);
  835. }
  836. else
  837. {
  838. /* Update PSSI state */
  839. hpssi->State = HAL_PSSI_STATE_READY;
  840. /* Update PSSI error code */
  841. hpssi->ErrorCode |= HAL_PSSI_ERROR_DMA;
  842. /* Process Unlocked */
  843. __HAL_UNLOCK(hpssi);
  844. return HAL_ERROR;
  845. }
  846. }
  847. else
  848. {
  849. /* Process Unlocked */
  850. __HAL_UNLOCK(hpssi);
  851. /* Note : The PSSI interrupts must be enabled after unlocking current process
  852. to avoid the risk of PSSI interrupt handle execution before current
  853. process unlock */
  854. /* Enable ERRinterrupt */
  855. /* possible to enable all of these */
  856. HAL_PSSI_ENABLE_IT(hpssi, PSSI_FLAG_OVR_RIS);
  857. }
  858. return HAL_OK;
  859. }
  860. else
  861. {
  862. return HAL_BUSY;
  863. }
  864. }
  865. /**
  866. * @brief Receive an amount of data in non-blocking mode with DMA
  867. * @param hpssi Pointer to a PSSI_HandleTypeDef structure that contains
  868. * the configuration information for the specified PSSI.
  869. * @param pData Pointer to data buffer
  870. * @param Size Amount of data to be received (in bytes)
  871. * @retval HAL status
  872. */
  873. HAL_StatusTypeDef HAL_PSSI_Receive_DMA(PSSI_HandleTypeDef *hpssi, uint32_t *pData, uint32_t Size)
  874. {
  875. HAL_StatusTypeDef dmaxferstatus;
  876. if (hpssi->State == HAL_PSSI_STATE_READY)
  877. {
  878. /* Disable the selected PSSI peripheral */
  879. HAL_PSSI_DISABLE(hpssi);
  880. /* Process Locked */
  881. __HAL_LOCK(hpssi);
  882. hpssi->State = HAL_PSSI_STATE_BUSY_RX;
  883. hpssi->ErrorCode = HAL_PSSI_ERROR_NONE;
  884. /* Prepare transfer parameters */
  885. hpssi->pBuffPtr = pData;
  886. hpssi->XferCount = Size;
  887. if (hpssi->XferCount > PSSI_MAX_NBYTE_SIZE)
  888. {
  889. hpssi->XferSize = PSSI_MAX_NBYTE_SIZE;
  890. }
  891. else
  892. {
  893. hpssi->XferSize = hpssi->XferCount;
  894. }
  895. if (hpssi->XferSize > 0U)
  896. {
  897. if (hpssi->hdmarx != NULL)
  898. {
  899. /* Configure BusWidth */
  900. if( hpssi->hdmatx->Init.PeriphDataAlignment == DMA_PDATAALIGN_BYTE)
  901. {
  902. MODIFY_REG(hpssi->Instance->CR,PSSI_CR_DMAEN|PSSI_CR_OUTEN|PSSI_CR_CKPOL,PSSI_CR_DMA_ENABLE |
  903. ((hpssi->Init.ClockPolarity == HAL_PSSI_RISING_EDGE)?PSSI_CR_CKPOL:0U));
  904. }
  905. else
  906. {
  907. MODIFY_REG(hpssi->Instance->CR,PSSI_CR_DMAEN|PSSI_CR_OUTEN|PSSI_CR_CKPOL,PSSI_CR_DMA_ENABLE | hpssi->Init.BusWidth |
  908. ((hpssi->Init.ClockPolarity == HAL_PSSI_RISING_EDGE)?PSSI_CR_CKPOL:0U));
  909. }
  910. /* Set the PSSI DMA transfer complete callback */
  911. hpssi->hdmarx->XferCpltCallback = PSSI_DMAReceiveCplt;
  912. /* Set the DMA error callback */
  913. hpssi->hdmarx->XferErrorCallback = PSSI_DMAError;
  914. /* Set the unused DMA callbacks to NULL */
  915. hpssi->hdmarx->XferHalfCpltCallback = NULL;
  916. hpssi->hdmarx->XferAbortCallback = NULL;
  917. /* Enable the DMA */
  918. dmaxferstatus = HAL_DMA_Start_IT(hpssi->hdmarx, (uint32_t)&hpssi->Instance->DR, (uint32_t)pData, hpssi->XferSize);
  919. }
  920. else
  921. {
  922. /* Update PSSI state */
  923. hpssi->State = HAL_PSSI_STATE_READY;
  924. /* Update PSSI error code */
  925. hpssi->ErrorCode |= HAL_PSSI_ERROR_DMA;
  926. /* Process Unlocked */
  927. __HAL_UNLOCK(hpssi);
  928. return HAL_ERROR;
  929. }
  930. if (dmaxferstatus == HAL_OK)
  931. {
  932. /* Update XferCount value */
  933. hpssi->XferCount -= hpssi->XferSize;
  934. /* Process Unlocked */
  935. __HAL_UNLOCK(hpssi);
  936. /* Note : The PSSI interrupts must be enabled after unlocking current process
  937. to avoid the risk of PSSI interrupt handle execution before current
  938. process unlock */
  939. /* Enable ERR interrupt */
  940. HAL_PSSI_ENABLE_IT(hpssi, PSSI_FLAG_OVR_RIS);
  941. /* Enable DMA Request */
  942. hpssi->Instance->CR |= PSSI_CR_DMA_ENABLE;
  943. /* Enable the selected PSSI peripheral */
  944. HAL_PSSI_ENABLE(hpssi);
  945. }
  946. else
  947. {
  948. /* Update PSSI state */
  949. hpssi->State = HAL_PSSI_STATE_READY;
  950. /* Update PSSI error code */
  951. hpssi->ErrorCode |= HAL_PSSI_ERROR_DMA;
  952. /* Process Unlocked */
  953. __HAL_UNLOCK(hpssi);
  954. return HAL_ERROR;
  955. }
  956. }
  957. else
  958. {
  959. /* Process Unlocked */
  960. __HAL_UNLOCK(hpssi);
  961. /* Enable ERR,interrupt */
  962. HAL_PSSI_ENABLE_IT(hpssi, PSSI_FLAG_OVR_RIS);
  963. }
  964. return HAL_OK;
  965. }
  966. else
  967. {
  968. return HAL_BUSY;
  969. }
  970. }
  971. /**
  972. * @brief Abort a DMA process communication with Interrupt.
  973. * @param hpssi Pointer to a PSSI_HandleTypeDef structure that contains
  974. * the configuration information for the specified PSSI.
  975. * @retval HAL status
  976. */
  977. HAL_StatusTypeDef HAL_PSSI_Abort_DMA(PSSI_HandleTypeDef *hpssi)
  978. {
  979. /* Process Locked */
  980. __HAL_LOCK(hpssi);
  981. /* Disable Interrupts */
  982. HAL_PSSI_DISABLE_IT(hpssi, PSSI_FLAG_OVR_RIS);
  983. /* Set State at HAL_PSSI_STATE_ABORT */
  984. hpssi->State = HAL_PSSI_STATE_ABORT;
  985. /* Abort DMA TX transfer if any */
  986. if ((hpssi->Instance->CR & PSSI_CR_DMAEN) == PSSI_CR_DMAEN)
  987. {
  988. if (hpssi->State == HAL_PSSI_STATE_BUSY_TX)
  989. {
  990. hpssi->Instance->CR &= ~PSSI_CR_DMAEN;
  991. if (hpssi->hdmatx != NULL)
  992. {
  993. /* Set the PSSI DMA Abort callback :
  994. will lead to call HAL_PSSI_ErrorCallback() at end of DMA abort procedure */
  995. hpssi->hdmatx->XferAbortCallback = PSSI_DMAAbort;
  996. /* Abort DMA TX */
  997. if (HAL_DMA_Abort_IT(hpssi->hdmatx) != HAL_OK)
  998. {
  999. /* Call Directly XferAbortCallback function in case of error */
  1000. hpssi->hdmatx->XferAbortCallback(hpssi->hdmatx);
  1001. }
  1002. }
  1003. }
  1004. /* Abort DMA RX transfer if any */
  1005. else if (hpssi->State == HAL_PSSI_STATE_BUSY_RX)
  1006. {
  1007. hpssi->Instance->CR &= ~PSSI_CR_DMAEN;
  1008. if (hpssi->hdmarx != NULL)
  1009. {
  1010. /* Set the PSSI DMA Abort callback :
  1011. will lead to call HAL_PSSI_ErrorCallback() at end of DMA abort procedure */
  1012. hpssi->hdmarx->XferAbortCallback = PSSI_DMAAbort;
  1013. /* Abort DMA RX */
  1014. if (HAL_DMA_Abort_IT(hpssi->hdmarx) != HAL_OK)
  1015. {
  1016. /* Call Directly hpssi->hdma->XferAbortCallback function in case of error */
  1017. hpssi->hdmarx->XferAbortCallback(hpssi->hdmarx);
  1018. }
  1019. }
  1020. }
  1021. else
  1022. {
  1023. /* Call the error callback */
  1024. hpssi->ErrorCallback(hpssi);
  1025. }
  1026. }
  1027. /* Process Unlocked */
  1028. __HAL_UNLOCK(hpssi);
  1029. /* Note : The PSSI interrupts must be enabled after unlocking current process
  1030. to avoid the risk of PSSI interrupt handle execution before current
  1031. process unlock */
  1032. HAL_PSSI_ENABLE_IT(hpssi, PSSI_FLAG_OVR_RIS);
  1033. return HAL_OK;
  1034. }
  1035. /**
  1036. * @}
  1037. */
  1038. /** @defgroup PSSI_Exported_Functions_Group3 IRQ Handler and Callbacks
  1039. * @{
  1040. */
  1041. /**
  1042. * @brief This function handles PSSI event interrupt request.
  1043. * @param hpssi Pointer to a PSSI_HandleTypeDef structure that contains
  1044. * the configuration information for the specified PSSI.
  1045. * @retval None
  1046. */
  1047. void HAL_PSSI_IRQHandler(PSSI_HandleTypeDef *hpssi)
  1048. {
  1049. /* Overrun/ Underrun Errors */
  1050. if (HAL_PSSI_GET_FLAG(hpssi, PSSI_FLAG_OVR_MIS) != 0U)
  1051. {
  1052. /* Reset handle parameters */
  1053. hpssi->XferCount = 0U;
  1054. /* Disable all interrupts */
  1055. HAL_PSSI_DISABLE_IT(hpssi, PSSI_FLAG_OVR_RIS);
  1056. /* Abort DMA TX transfer if any */
  1057. if ((hpssi->Instance->CR & PSSI_CR_DMAEN) == PSSI_CR_DMAEN)
  1058. {
  1059. if (hpssi->State == HAL_PSSI_STATE_BUSY_TX)
  1060. {
  1061. /* Set new error code */
  1062. hpssi->ErrorCode |= HAL_PSSI_ERROR_UNDER_RUN;
  1063. hpssi->Instance->CR &= ~PSSI_CR_DMAEN;
  1064. if (hpssi->hdmatx != NULL)
  1065. {
  1066. /* Set the PSSI DMA Abort callback :
  1067. will lead to call HAL_PSSI_ErrorCallback() at end of DMA abort procedure */
  1068. hpssi->hdmatx->XferAbortCallback = PSSI_DMAAbort;
  1069. /* Process Unlocked */
  1070. __HAL_UNLOCK(hpssi);
  1071. /* Abort DMA TX */
  1072. if (HAL_DMA_Abort_IT(hpssi->hdmatx) != HAL_OK)
  1073. {
  1074. /* Call Directly XferAbortCallback function in case of error */
  1075. hpssi->hdmatx->XferAbortCallback(hpssi->hdmatx);
  1076. }
  1077. }
  1078. }
  1079. /* Abort DMA RX transfer if any */
  1080. else if (hpssi->State == HAL_PSSI_STATE_BUSY_RX)
  1081. {
  1082. /* Set new error code */
  1083. hpssi->ErrorCode |= HAL_PSSI_ERROR_OVER_RUN;
  1084. hpssi->Instance->CR &= ~PSSI_CR_DMAEN;
  1085. if (hpssi->hdmarx != NULL)
  1086. {
  1087. /* Set the PSSI DMA Abort callback :
  1088. will lead to call HAL_PSSI_ErrorCallback() at end of DMA abort procedure */
  1089. hpssi->hdmarx->XferAbortCallback = PSSI_DMAAbort;
  1090. /* Process Unlocked */
  1091. __HAL_UNLOCK(hpssi);
  1092. /* Abort DMA RX */
  1093. if (HAL_DMA_Abort_IT(hpssi->hdmarx) != HAL_OK)
  1094. {
  1095. /* Call Directly hpssi->hdma->XferAbortCallback function in case of error */
  1096. hpssi->hdmarx->XferAbortCallback(hpssi->hdmarx);
  1097. }
  1098. }
  1099. }
  1100. else
  1101. {
  1102. /* Call the corresponding callback to inform upper layer of the error */
  1103. hpssi->ErrorCallback(hpssi);
  1104. }
  1105. }
  1106. /* If state is an abort treatment on going, don't change state */
  1107. if (hpssi->State == HAL_PSSI_STATE_ABORT)
  1108. {
  1109. hpssi->State = HAL_PSSI_STATE_READY;
  1110. /* Process Unlocked */
  1111. __HAL_UNLOCK(hpssi);
  1112. /* Call the corresponding callback to inform upper layer of End of Transfer */
  1113. hpssi->AbortCpltCallback(hpssi);
  1114. }
  1115. else
  1116. {
  1117. /* Set HAL_PSSI_STATE_READY */
  1118. hpssi->State = HAL_PSSI_STATE_READY;
  1119. /* Process Unlocked */
  1120. __HAL_UNLOCK(hpssi);
  1121. /* Call the corresponding callback to inform upper layer of End of Transfer */
  1122. hpssi->ErrorCallback(hpssi);
  1123. }
  1124. }
  1125. }
  1126. /**
  1127. * @brief Tx Transfer complete callback.
  1128. * @param hpssi Pointer to a PSSI_HandleTypeDef structure that contains
  1129. * the configuration information for the specified PSSI.
  1130. * @retval None
  1131. */
  1132. __weak void HAL_PSSI_TxCpltCallback(PSSI_HandleTypeDef *hpssi)
  1133. {
  1134. /* Prevent unused argument(s) compilation warning */
  1135. UNUSED(hpssi);
  1136. /* NOTE : This function should not be modified, when the callback is needed,
  1137. the HAL_PSSI_TxCpltCallback can be implemented in the user file
  1138. */
  1139. }
  1140. /**
  1141. * @brief Rx Transfer complete callback.
  1142. * @param hpssi Pointer to a PSSI_HandleTypeDef structure that contains
  1143. * the configuration information for the specified PSSI.
  1144. * @retval None
  1145. */
  1146. __weak void HAL_PSSI_RxCpltCallback(PSSI_HandleTypeDef *hpssi)
  1147. {
  1148. /* Prevent unused argument(s) compilation warning */
  1149. UNUSED(hpssi);
  1150. /* NOTE : This function should not be modified, when the callback is needed,
  1151. the HAL_PSSI_RxCpltCallback can be implemented in the user file
  1152. */
  1153. }
  1154. /**
  1155. * @brief PSSI error callback.
  1156. * @param hpssi Pointer to a PSSI_HandleTypeDef structure that contains
  1157. * the configuration information for the specified PSSI.
  1158. * @retval None
  1159. */
  1160. __weak void HAL_PSSI_ErrorCallback(PSSI_HandleTypeDef *hpssi)
  1161. {
  1162. /* Prevent unused argument(s) compilation warning */
  1163. UNUSED(hpssi);
  1164. /* NOTE : This function should not be modified, when the callback is needed,
  1165. the HAL_PSSI_ErrorCallback could be implemented in the user file
  1166. */
  1167. }
  1168. /**
  1169. * @brief PSSI abort callback.
  1170. * @param hpssi Pointer to a PSSI_HandleTypeDef structure that contains
  1171. * the configuration information for the specified PSSI.
  1172. * @retval None
  1173. */
  1174. __weak void HAL_PSSI_AbortCpltCallback(PSSI_HandleTypeDef *hpssi)
  1175. {
  1176. /* Prevent unused argument(s) compilation warning */
  1177. UNUSED(hpssi);
  1178. /* NOTE : This function should not be modified, when the callback is needed,
  1179. the HAL_PSSI_AbortCpltCallback could be implemented in the user file
  1180. */
  1181. }
  1182. /**
  1183. * @}
  1184. */
  1185. /** @defgroup PSSI_Exported_Functions_Group4 Peripheral State, Mode and Error functions
  1186. * @brief Peripheral State, Mode and Error functions
  1187. *
  1188. @verbatim
  1189. ===============================================================================
  1190. ##### Peripheral State, Mode and Error functions #####
  1191. ===============================================================================
  1192. [..]
  1193. This subsection permit to get in run-time the status of the peripheral
  1194. and the data flow.
  1195. @endverbatim
  1196. * @{
  1197. */
  1198. /**
  1199. * @brief Return the PSSI handle state.
  1200. * @param hpssi Pointer to a PSSI_HandleTypeDef structure that contains
  1201. * the configuration information for the specified PSSI.
  1202. * @retval HAL state
  1203. */
  1204. HAL_PSSI_StateTypeDef HAL_PSSI_GetState(PSSI_HandleTypeDef *hpssi)
  1205. {
  1206. /* Return PSSI handle state */
  1207. return hpssi->State;
  1208. }
  1209. /**
  1210. * @brief Return the PSSI error code.
  1211. * @param hpssi Pointer to a PSSI_HandleTypeDef structure that contains
  1212. * the configuration information for the specified PSSI.
  1213. * @retval PSSI Error Code
  1214. */
  1215. uint32_t HAL_PSSI_GetError(PSSI_HandleTypeDef *hpssi)
  1216. {
  1217. return hpssi->ErrorCode;
  1218. }
  1219. /**
  1220. * @}
  1221. */
  1222. /**
  1223. * @}
  1224. */
  1225. /** @addtogroup PSSI_Private_Functions
  1226. * @{
  1227. */
  1228. /**
  1229. * @brief PSSI Errors process.
  1230. * @param hpssi PSSI handle.
  1231. * @param ErrorCode Error code to handle.
  1232. * @retval None
  1233. */
  1234. static void PSSI_Error(PSSI_HandleTypeDef *hpssi, uint32_t ErrorCode)
  1235. {
  1236. /* Reset handle parameters */
  1237. hpssi->XferCount = 0U;
  1238. /* Set new error code */
  1239. hpssi->ErrorCode |= ErrorCode;
  1240. /* Disable all interrupts */
  1241. HAL_PSSI_DISABLE_IT(hpssi, PSSI_FLAG_OVR_RIS);
  1242. /* Abort DMA TX transfer if any */
  1243. if ((hpssi->Instance->CR & PSSI_CR_DMAEN) == PSSI_CR_DMAEN)
  1244. {
  1245. if (hpssi->State == HAL_PSSI_STATE_BUSY_TX)
  1246. {
  1247. hpssi->Instance->CR &= ~PSSI_CR_DMAEN;
  1248. if (hpssi->hdmatx != NULL)
  1249. {
  1250. /* Set the PSSI DMA Abort callback :
  1251. will lead to call HAL_PSSI_ErrorCallback() at end of DMA abort procedure */
  1252. hpssi->hdmatx->XferAbortCallback = PSSI_DMAAbort;
  1253. /* Process Unlocked */
  1254. __HAL_UNLOCK(hpssi);
  1255. /* Abort DMA TX */
  1256. if (HAL_DMA_Abort_IT(hpssi->hdmatx) != HAL_OK)
  1257. {
  1258. /* Call Directly XferAbortCallback function in case of error */
  1259. hpssi->hdmatx->XferAbortCallback(hpssi->hdmatx);
  1260. }
  1261. }
  1262. }
  1263. /* Abort DMA RX transfer if any */
  1264. else if (hpssi->State == HAL_PSSI_STATE_BUSY_RX)
  1265. {
  1266. hpssi->Instance->CR &= ~PSSI_CR_DMAEN;
  1267. if (hpssi->hdmarx != NULL)
  1268. {
  1269. /* Set the PSSI DMA Abort callback :
  1270. will lead to call HAL_PSSI_ErrorCallback() at end of DMA abort procedure */
  1271. hpssi->hdmarx->XferAbortCallback = PSSI_DMAAbort;
  1272. /* Process Unlocked */
  1273. __HAL_UNLOCK(hpssi);
  1274. /* Abort DMA RX */
  1275. if (HAL_DMA_Abort_IT(hpssi->hdmarx) != HAL_OK)
  1276. {
  1277. /* Call Directly hpssi->hdma->XferAbortCallback function in case of error */
  1278. hpssi->hdmarx->XferAbortCallback(hpssi->hdmarx);
  1279. }
  1280. }
  1281. }
  1282. else
  1283. {
  1284. /*Nothing to do*/
  1285. }
  1286. }
  1287. /* If state is an abort treatment on going, don't change state */
  1288. if (hpssi->State == HAL_PSSI_STATE_ABORT)
  1289. {
  1290. hpssi->State = HAL_PSSI_STATE_READY;
  1291. /* Process Unlocked */
  1292. __HAL_UNLOCK(hpssi);
  1293. /* Call the corresponding callback to inform upper layer of End of Transfer */
  1294. hpssi->AbortCpltCallback(hpssi);
  1295. }
  1296. else
  1297. {
  1298. /* Set HAL_PSSI_STATE_READY */
  1299. hpssi->State = HAL_PSSI_STATE_READY;
  1300. /* Process Unlocked */
  1301. __HAL_UNLOCK(hpssi);
  1302. /* Call the corresponding callback to inform upper layer of End of Transfer */
  1303. hpssi->ErrorCallback(hpssi);
  1304. }
  1305. }
  1306. /**
  1307. * @brief DMA PSSI slave transmit process complete callback.
  1308. * @param hdma DMA handle
  1309. * @retval None
  1310. */
  1311. void PSSI_DMATransmitCplt(DMA_HandleTypeDef *hdma)
  1312. {
  1313. PSSI_HandleTypeDef *hpssi = (PSSI_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent); /* Derogation MISRAC2012-Rule-11.5 */
  1314. uint32_t tmperror;
  1315. /* Disable Interrupts */
  1316. HAL_PSSI_DISABLE_IT(hpssi, PSSI_FLAG_OVR_RIS);
  1317. /* Store current volatile hpssi->ErrorCode, misra rule */
  1318. tmperror = hpssi->ErrorCode;
  1319. /* Call the corresponding callback to inform upper layer of End of Transfer */
  1320. if ((hpssi->State == HAL_PSSI_STATE_ABORT) || (tmperror != HAL_PSSI_ERROR_NONE))
  1321. {
  1322. /* Call the corresponding callback to inform upper layer of End of Transfer */
  1323. PSSI_Error(hpssi, hpssi->ErrorCode);
  1324. }
  1325. /* hpssi->State == HAL_PSSI_STATE_BUSY_TX */
  1326. else
  1327. {
  1328. hpssi->State = HAL_PSSI_STATE_READY;
  1329. /* Process Unlocked */
  1330. __HAL_UNLOCK(hpssi);
  1331. /* Call the corresponding callback to inform upper layer of End of Transfer */
  1332. hpssi->TxCpltCallback(hpssi);
  1333. }
  1334. }
  1335. /**
  1336. * @brief DMA PSSI master receive process complete callback.
  1337. * @param hdma DMA handle
  1338. * @retval None
  1339. */
  1340. void PSSI_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
  1341. {
  1342. PSSI_HandleTypeDef *hpssi = (PSSI_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent); /* Derogation MISRAC2012-Rule-11.5 */
  1343. uint32_t tmperror;
  1344. /* Disable Interrupts */
  1345. HAL_PSSI_DISABLE_IT(hpssi, PSSI_FLAG_OVR_RIS);
  1346. /* Store current volatile hpssi->ErrorCode, misra rule */
  1347. tmperror = hpssi->ErrorCode;
  1348. /* Call the corresponding callback to inform upper layer of End of Transfer */
  1349. if ((hpssi->State == HAL_PSSI_STATE_ABORT) || (tmperror != HAL_PSSI_ERROR_NONE))
  1350. {
  1351. /* Call the corresponding callback to inform upper layer of End of Transfer */
  1352. PSSI_Error(hpssi, hpssi->ErrorCode);
  1353. }
  1354. /* hpssi->State == HAL_PSSI_STATE_BUSY_RX */
  1355. else
  1356. {
  1357. hpssi->State = HAL_PSSI_STATE_READY;
  1358. /* Process Unlocked */
  1359. __HAL_UNLOCK(hpssi);
  1360. /* Call the corresponding callback to inform upper layer of End of Transfer */
  1361. hpssi->RxCpltCallback(hpssi);
  1362. }
  1363. }
  1364. /**
  1365. * @brief DMA PSSI communication abort callback
  1366. * (To be called at end of DMA Abort procedure).
  1367. * @param hdma DMA handle.
  1368. * @retval None
  1369. */
  1370. void PSSI_DMAAbort(DMA_HandleTypeDef *hdma)
  1371. {
  1372. PSSI_HandleTypeDef *hpssi = (PSSI_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent); /* Derogation MISRAC2012-Rule-11.5 */
  1373. /* Reset AbortCpltCallback */
  1374. hpssi->hdmatx->XferAbortCallback = NULL;
  1375. hpssi->hdmarx->XferAbortCallback = NULL;
  1376. /* Check if come from abort from user */
  1377. if (hpssi->State == HAL_PSSI_STATE_ABORT)
  1378. {
  1379. hpssi->State = HAL_PSSI_STATE_READY;
  1380. /* Call the corresponding callback to inform upper layer of End of Transfer */
  1381. hpssi->AbortCpltCallback(hpssi);
  1382. }
  1383. else
  1384. {
  1385. /* Call the corresponding callback to inform upper layer of End of Transfer */
  1386. hpssi->ErrorCallback(hpssi);
  1387. }
  1388. }
  1389. /**
  1390. * @brief This function handles PSSI Communication Timeout.
  1391. * @param hpssi Pointer to a PSSI_HandleTypeDef structure that contains
  1392. * the configuration information for the specified PSSI.
  1393. * @param Flag Specifies the PSSI flag to check.
  1394. * @param Status The new Flag status (SET or RESET).
  1395. * @param Timeout Timeout duration
  1396. * @param Tickstart Tick start value
  1397. * @retval HAL status
  1398. */
  1399. static HAL_StatusTypeDef PSSI_WaitOnStatusUntilTimeout(PSSI_HandleTypeDef *hpssi, uint32_t Flag, FlagStatus Status, uint32_t Timeout, uint32_t Tickstart)
  1400. {
  1401. while ((HAL_PSSI_GET_STATUS(hpssi, Flag) & Flag) == (uint32_t)Status)
  1402. {
  1403. /* Check for the Timeout */
  1404. if (Timeout != HAL_MAX_DELAY)
  1405. {
  1406. if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
  1407. {
  1408. hpssi->ErrorCode |= HAL_PSSI_ERROR_TIMEOUT;
  1409. hpssi->State = HAL_PSSI_STATE_READY;
  1410. /* Process Unlocked */
  1411. __HAL_UNLOCK(hpssi);
  1412. return HAL_ERROR;
  1413. }
  1414. }
  1415. }
  1416. return HAL_OK;
  1417. }
  1418. void PSSI_DMAError(DMA_HandleTypeDef *hdma)
  1419. {
  1420. PSSI_HandleTypeDef *hpssi = (PSSI_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent); /* Derogation MISRAC2012-Rule-11.5 */
  1421. uint32_t tmperror;
  1422. /* Disable the selected PSSI peripheral */
  1423. HAL_PSSI_DISABLE(hpssi);
  1424. /* Disable Interrupts */
  1425. HAL_PSSI_DISABLE_IT(hpssi, PSSI_FLAG_OVR_RIS);
  1426. /* Store current volatile hpssi->ErrorCode, misra rule */
  1427. tmperror = hpssi->ErrorCode;
  1428. /* Call the corresponding callback to inform upper layer of End of Transfer */
  1429. if ((hpssi->State == HAL_PSSI_STATE_ABORT) || (tmperror != HAL_PSSI_ERROR_NONE))
  1430. {
  1431. /* Call the corresponding callback to inform upper layer of End of Transfer */
  1432. PSSI_Error(hpssi, hpssi->ErrorCode);
  1433. }
  1434. else
  1435. {
  1436. hpssi->State = HAL_PSSI_STATE_READY;
  1437. /* Process Unlocked */
  1438. __HAL_UNLOCK(hpssi);
  1439. /* Call the corresponding callback to inform upper layer of End of Transfer */
  1440. hpssi->ErrorCallback(hpssi);
  1441. }
  1442. }
  1443. /**
  1444. * @}
  1445. */
  1446. #endif /* PSSI */
  1447. #endif /* HAL_PSSI_MODULE_ENABLED */
  1448. /**
  1449. * @}
  1450. */
  1451. /**
  1452. * @}
  1453. */
  1454. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/