stm32h7xx_hal_hsem.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /**
  2. ******************************************************************************
  3. * @file stm32h7xx_hal_hsem.c
  4. * @author MCD Application Team
  5. * @brief HSEM HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the semaphore peripheral:
  8. * + Semaphore Take function (2-Step Procedure) , non blocking
  9. * + Semaphore FastTake function (1-Step Procedure) , non blocking
  10. * + Semaphore Status check
  11. * + Semaphore Clear Key Set and Get
  12. * + Release and release all functions
  13. * + Semaphore notification enabling and disabling and callnack functions
  14. * + IRQ handler management
  15. *
  16. *
  17. ******************************************************************************
  18. * @attention
  19. *
  20. * Copyright (c) 2017 STMicroelectronics.
  21. * All rights reserved.
  22. *
  23. * This software is licensed under terms that can be found in the LICENSE file
  24. * in the root directory of this software component.
  25. * If no LICENSE file comes with this software, it is provided AS-IS.
  26. *
  27. ******************************************************************************
  28. @verbatim
  29. ==============================================================================
  30. ##### How to use this driver #####
  31. ==============================================================================
  32. [..]
  33. (#)Take a semaphore In 2-Step mode Using function HAL_HSEM_Take. This function takes as parameters :
  34. (++) the semaphore ID from 0 to 31
  35. (++) the process ID from 0 to 255
  36. (#) Fast Take semaphore In 1-Step mode Using function HAL_HSEM_FastTake. This function takes as parameter :
  37. (++) the semaphore ID from 0_ID to 31. Note that the process ID value is implicitly assumed as zero
  38. (#) Check if a semaphore is Taken using function HAL_HSEM_IsSemTaken. This function takes as parameter :
  39. (++) the semaphore ID from 0_ID to 31
  40. (++) It returns 1 if the given semaphore is taken otherwise (Free) zero
  41. (#)Release a semaphore using function with HAL_HSEM_Release. This function takes as parameters :
  42. (++) the semaphore ID from 0 to 31
  43. (++) the process ID from 0 to 255:
  44. (++) Note: If ProcessID and MasterID match, semaphore is freed, and an interrupt
  45. may be generated when enabled (notification activated). If ProcessID or MasterID does not match,
  46. semaphore remains taken (locked)
  47. (#)Release all semaphores at once taken by a given Master using function HAL_HSEM_Release_All
  48. This function takes as parameters :
  49. (++) the Release Key (value from 0 to 0xFFFF) can be Set or Get respectively by
  50. HAL_HSEM_SetClearKey() or HAL_HSEM_GetClearKey functions
  51. (++) the Master ID:
  52. (++) Note: If the Key and MasterID match, all semaphores taken by the given CPU that corresponds
  53. to MasterID will be freed, and an interrupt may be generated when enabled (notification activated). If the
  54. Key or the MasterID doesn't match, semaphores remains taken (locked)
  55. (#)Semaphores Release all key functions:
  56. (++) HAL_HSEM_SetClearKey() to set semaphore release all Key
  57. (++) HAL_HSEM_GetClearKey() to get release all Key
  58. (#)Semaphores notification functions :
  59. (++) HAL_HSEM_ActivateNotification to activate a notification callback on
  60. a given semaphores Mask (bitfield). When one or more semaphores defined by the mask are released
  61. the callback HAL_HSEM_FreeCallback will be asserted giving as parameters a mask of the released
  62. semaphores (bitfield).
  63. (++) HAL_HSEM_DeactivateNotification to deactivate the notification of a given semaphores Mask (bitfield).
  64. (++) See the description of the macro __HAL_HSEM_SEMID_TO_MASK to check how to calculate a semaphore mask
  65. Used by the notification functions
  66. *** HSEM HAL driver macros list ***
  67. =============================================
  68. [..] Below the list of most used macros in HSEM HAL driver.
  69. (+) __HAL_HSEM_SEMID_TO_MASK: Helper macro to convert a Semaphore ID to a Mask.
  70. [..] Example of use :
  71. [..] mask = __HAL_HSEM_SEMID_TO_MASK(8) | __HAL_HSEM_SEMID_TO_MASK(21) | __HAL_HSEM_SEMID_TO_MASK(25).
  72. [..] All next macros take as parameter a semaphore Mask (bitfiled) that can be constructed using __HAL_HSEM_SEMID_TO_MASK as the above example.
  73. (+) __HAL_HSEM_ENABLE_IT: Enable the specified semaphores Mask interrupts.
  74. (+) __HAL_HSEM_DISABLE_IT: Disable the specified semaphores Mask interrupts.
  75. (+) __HAL_HSEM_GET_IT: Checks whether the specified semaphore interrupt has occurred or not.
  76. (+) __HAL_HSEM_GET_FLAG: Get the semaphores status release flags.
  77. (+) __HAL_HSEM_CLEAR_FLAG: Clear the semaphores status release flags.
  78. @endverbatim
  79. ******************************************************************************
  80. */
  81. /* Includes ------------------------------------------------------------------*/
  82. #include "stm32h7xx_hal.h"
  83. /** @addtogroup STM32H7xx_HAL_Driver
  84. * @{
  85. */
  86. /** @defgroup HSEM HSEM
  87. * @brief HSEM HAL module driver
  88. * @{
  89. */
  90. #ifdef HAL_HSEM_MODULE_ENABLED
  91. /* Private typedef -----------------------------------------------------------*/
  92. /* Private define ------------------------------------------------------------*/
  93. #if defined(DUAL_CORE)
  94. /** @defgroup HSEM_Private_Constants HSEM Private Constants
  95. * @{
  96. */
  97. #ifndef HSEM_R_MASTERID
  98. #define HSEM_R_MASTERID HSEM_R_COREID
  99. #endif
  100. #ifndef HSEM_RLR_MASTERID
  101. #define HSEM_RLR_MASTERID HSEM_RLR_COREID
  102. #endif
  103. #ifndef HSEM_CR_MASTERID
  104. #define HSEM_CR_MASTERID HSEM_CR_COREID
  105. #endif
  106. /**
  107. * @}
  108. */
  109. #endif /* DUAL_CORE */
  110. /* Private macro -------------------------------------------------------------*/
  111. /* Private variables ---------------------------------------------------------*/
  112. /* Private function prototypes -----------------------------------------------*/
  113. /* Private functions ---------------------------------------------------------*/
  114. /* Exported functions --------------------------------------------------------*/
  115. /** @defgroup HSEM_Exported_Functions HSEM Exported Functions
  116. * @{
  117. */
  118. /** @defgroup HSEM_Exported_Functions_Group1 Take and Release functions
  119. * @brief HSEM Take and Release functions
  120. *
  121. @verbatim
  122. ==============================================================================
  123. ##### HSEM Take and Release functions #####
  124. ==============================================================================
  125. [..] This section provides functions allowing to:
  126. (+) Take a semaphore with 2 Step method
  127. (+) Fast Take a semaphore with 1 Step method
  128. (+) Check semaphore state Taken or not
  129. (+) Release a semaphore
  130. (+) Release all semaphore at once
  131. @endverbatim
  132. * @{
  133. */
  134. /**
  135. * @brief Take a semaphore in 2 Step mode.
  136. * @param SemID: semaphore ID from 0 to 31
  137. * @param ProcessID: Process ID from 0 to 255
  138. * @retval HAL status
  139. */
  140. HAL_StatusTypeDef HAL_HSEM_Take(uint32_t SemID, uint32_t ProcessID)
  141. {
  142. /* Check the parameters */
  143. assert_param(IS_HSEM_SEMID(SemID));
  144. assert_param(IS_HSEM_PROCESSID(ProcessID));
  145. #if USE_MULTI_CORE_SHARED_CODE != 0U
  146. /* First step write R register with MasterID, processID and take bit=1*/
  147. HSEM->R[SemID] = ((ProcessID & HSEM_R_PROCID) | ((HAL_GetCurrentCPUID() << POSITION_VAL(HSEM_R_MASTERID)) & HSEM_R_MASTERID) | HSEM_R_LOCK);
  148. /* second step : read the R register . Take achieved if MasterID and processID match and take bit set to 1 */
  149. if (HSEM->R[SemID] == ((ProcessID & HSEM_R_PROCID) | ((HAL_GetCurrentCPUID() << POSITION_VAL(HSEM_R_MASTERID)) & HSEM_R_MASTERID) | HSEM_R_LOCK))
  150. {
  151. /*take success when MasterID and ProcessID match and take bit set*/
  152. return HAL_OK;
  153. }
  154. #else
  155. /* First step write R register with MasterID, processID and take bit=1*/
  156. HSEM->R[SemID] = (ProcessID | HSEM_CR_COREID_CURRENT | HSEM_R_LOCK);
  157. /* second step : read the R register . Take achieved if MasterID and processID match and take bit set to 1 */
  158. if (HSEM->R[SemID] == (ProcessID | HSEM_CR_COREID_CURRENT | HSEM_R_LOCK))
  159. {
  160. /*take success when MasterID and ProcessID match and take bit set*/
  161. return HAL_OK;
  162. }
  163. #endif
  164. /* Semaphore take fails*/
  165. return HAL_ERROR;
  166. }
  167. /**
  168. * @brief Fast Take a semaphore with 1 Step mode.
  169. * @param SemID: semaphore ID from 0 to 31
  170. * @retval HAL status
  171. */
  172. HAL_StatusTypeDef HAL_HSEM_FastTake(uint32_t SemID)
  173. {
  174. /* Check the parameters */
  175. assert_param(IS_HSEM_SEMID(SemID));
  176. #if USE_MULTI_CORE_SHARED_CODE != 0U
  177. /* Read the RLR register to take the semaphore */
  178. if (HSEM->RLR[SemID] == (((HAL_GetCurrentCPUID() << POSITION_VAL(HSEM_R_MASTERID)) & HSEM_RLR_MASTERID) | HSEM_RLR_LOCK))
  179. {
  180. /*take success when MasterID match and take bit set*/
  181. return HAL_OK;
  182. }
  183. #else
  184. /* Read the RLR register to take the semaphore */
  185. if (HSEM->RLR[SemID] == (HSEM_CR_COREID_CURRENT | HSEM_RLR_LOCK))
  186. {
  187. /*take success when MasterID match and take bit set*/
  188. return HAL_OK;
  189. }
  190. #endif
  191. /* Semaphore take fails */
  192. return HAL_ERROR;
  193. }
  194. /**
  195. * @brief Check semaphore state Taken or not.
  196. * @param SemID: semaphore ID
  197. * @retval HAL HSEM state
  198. */
  199. uint32_t HAL_HSEM_IsSemTaken(uint32_t SemID)
  200. {
  201. return (((HSEM->R[SemID] & HSEM_R_LOCK) != 0U) ? 1UL : 0UL);
  202. }
  203. /**
  204. * @brief Release a semaphore.
  205. * @param SemID: semaphore ID from 0 to 31
  206. * @param ProcessID: Process ID from 0 to 255
  207. * @retval None
  208. */
  209. void HAL_HSEM_Release(uint32_t SemID, uint32_t ProcessID)
  210. {
  211. /* Check the parameters */
  212. assert_param(IS_HSEM_SEMID(SemID));
  213. assert_param(IS_HSEM_PROCESSID(ProcessID));
  214. /* Clear the semaphore by writing to the R register : the MasterID , the processID and take bit = 0 */
  215. #if USE_MULTI_CORE_SHARED_CODE != 0U
  216. HSEM->R[SemID] = (ProcessID | ((HAL_GetCurrentCPUID() << POSITION_VAL(HSEM_R_MASTERID)) & HSEM_R_MASTERID));
  217. #else
  218. HSEM->R[SemID] = (ProcessID | HSEM_CR_COREID_CURRENT);
  219. #endif
  220. }
  221. /**
  222. * @brief Release All semaphore used by a given Master .
  223. * @param Key: Semaphore Key , value from 0 to 0xFFFF
  224. * @param CoreID: CoreID of the CPU that is using semaphores to be released
  225. * @retval None
  226. */
  227. void HAL_HSEM_ReleaseAll(uint32_t Key, uint32_t CoreID)
  228. {
  229. assert_param(IS_HSEM_KEY(Key));
  230. assert_param(IS_HSEM_COREID(CoreID));
  231. HSEM->CR = ((Key << HSEM_CR_KEY_Pos) | (CoreID << HSEM_CR_COREID_Pos));
  232. }
  233. /**
  234. * @}
  235. */
  236. /** @defgroup HSEM_Exported_Functions_Group2 HSEM Set and Get Key functions
  237. * @brief HSEM Set and Get Key functions.
  238. *
  239. @verbatim
  240. ==============================================================================
  241. ##### HSEM Set and Get Key functions #####
  242. ==============================================================================
  243. [..] This section provides functions allowing to:
  244. (+) Set semaphore Key
  245. (+) Get semaphore Key
  246. @endverbatim
  247. * @{
  248. */
  249. /**
  250. * @brief Set semaphore Key .
  251. * @param Key: Semaphore Key , value from 0 to 0xFFFF
  252. * @retval None
  253. */
  254. void HAL_HSEM_SetClearKey(uint32_t Key)
  255. {
  256. assert_param(IS_HSEM_KEY(Key));
  257. MODIFY_REG(HSEM->KEYR, HSEM_KEYR_KEY, (Key << HSEM_KEYR_KEY_Pos));
  258. }
  259. /**
  260. * @brief Get semaphore Key .
  261. * @retval Semaphore Key , value from 0 to 0xFFFF
  262. */
  263. uint32_t HAL_HSEM_GetClearKey(void)
  264. {
  265. return (HSEM->KEYR >> HSEM_KEYR_KEY_Pos);
  266. }
  267. /**
  268. * @}
  269. */
  270. /** @defgroup HSEM_Exported_Functions_Group3 HSEM IRQ handler management
  271. * @brief HSEM Notification functions.
  272. *
  273. @verbatim
  274. ==============================================================================
  275. ##### HSEM IRQ handler management and Notification functions #####
  276. ==============================================================================
  277. [..] This section provides HSEM IRQ handler and Notification function.
  278. @endverbatim
  279. * @{
  280. */
  281. /**
  282. * @brief Activate Semaphore release Notification for a given Semaphores Mask .
  283. * @param SemMask: Mask of Released semaphores
  284. * @retval Semaphore Key
  285. */
  286. void HAL_HSEM_ActivateNotification(uint32_t SemMask)
  287. {
  288. #if USE_MULTI_CORE_SHARED_CODE != 0U
  289. /*enable the semaphore mask interrupts */
  290. if (HAL_GetCurrentCPUID() == HSEM_CPU1_COREID)
  291. {
  292. /*Use interrupt line 0 for CPU1 Master */
  293. HSEM->C1IER |= SemMask;
  294. }
  295. else /* HSEM_CPU2_COREID */
  296. {
  297. /*Use interrupt line 1 for CPU2 Master*/
  298. HSEM->C2IER |= SemMask;
  299. }
  300. #else
  301. HSEM_COMMON->IER |= SemMask;
  302. #endif
  303. }
  304. /**
  305. * @brief Deactivate Semaphore release Notification for a given Semaphores Mask .
  306. * @param SemMask: Mask of Released semaphores
  307. * @retval Semaphore Key
  308. */
  309. void HAL_HSEM_DeactivateNotification(uint32_t SemMask)
  310. {
  311. #if USE_MULTI_CORE_SHARED_CODE != 0U
  312. /*enable the semaphore mask interrupts */
  313. if (HAL_GetCurrentCPUID() == HSEM_CPU1_COREID)
  314. {
  315. /*Use interrupt line 0 for CPU1 Master */
  316. HSEM->C1IER &= ~SemMask;
  317. }
  318. else /* HSEM_CPU2_COREID */
  319. {
  320. /*Use interrupt line 1 for CPU2 Master*/
  321. HSEM->C2IER &= ~SemMask;
  322. }
  323. #else
  324. HSEM_COMMON->IER &= ~SemMask;
  325. #endif
  326. }
  327. /**
  328. * @brief This function handles HSEM interrupt request
  329. * @retval None
  330. */
  331. void HAL_HSEM_IRQHandler(void)
  332. {
  333. uint32_t statusreg;
  334. #if USE_MULTI_CORE_SHARED_CODE != 0U
  335. if (HAL_GetCurrentCPUID() == HSEM_CPU1_COREID)
  336. {
  337. /* Get the list of masked freed semaphores*/
  338. statusreg = HSEM->C1MISR; /*Use interrupt line 0 for CPU1 Master*/
  339. /*Disable Interrupts*/
  340. HSEM->C1IER &= ~((uint32_t)statusreg);
  341. /*Clear Flags*/
  342. HSEM->C1ICR = ((uint32_t)statusreg);
  343. }
  344. else /* HSEM_CPU2_COREID */
  345. {
  346. /* Get the list of masked freed semaphores*/
  347. statusreg = HSEM->C2MISR;/*Use interrupt line 1 for CPU2 Master*/
  348. /*Disable Interrupts*/
  349. HSEM->C2IER &= ~((uint32_t)statusreg);
  350. /*Clear Flags*/
  351. HSEM->C2ICR = ((uint32_t)statusreg);
  352. }
  353. #else
  354. /* Get the list of masked freed semaphores*/
  355. statusreg = HSEM_COMMON->MISR;
  356. /*Disable Interrupts*/
  357. HSEM_COMMON->IER &= ~((uint32_t)statusreg);
  358. /*Clear Flags*/
  359. HSEM_COMMON->ICR = ((uint32_t)statusreg);
  360. #endif
  361. /* Call FreeCallback */
  362. HAL_HSEM_FreeCallback(statusreg);
  363. }
  364. /**
  365. * @brief Semaphore Released Callback.
  366. * @param SemMask: Mask of Released semaphores
  367. * @retval None
  368. */
  369. __weak void HAL_HSEM_FreeCallback(uint32_t SemMask)
  370. {
  371. /* Prevent unused argument(s) compilation warning */
  372. UNUSED(SemMask);
  373. /* NOTE : This function should not be modified, when the callback is needed,
  374. the HAL_HSEM_FreeCallback can be implemented in the user file
  375. */
  376. }
  377. /**
  378. * @}
  379. */
  380. /**
  381. * @}
  382. */
  383. #endif /* HAL_HSEM_MODULE_ENABLED */
  384. /**
  385. * @}
  386. */
  387. /**
  388. * @}
  389. */