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.
 
 
 

372 lines
12 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32wbxx_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. @verbatim
  18. ==============================================================================
  19. ##### How to use this driver #####
  20. ==============================================================================
  21. [..]
  22. (#)Take a semaphore In 2-Step mode Using function HAL_HSEM_Take. This function takes as parameters :
  23. (++) the semaphore ID from 0 to 31
  24. (++) the process ID from 0 to 255
  25. (#) Fast Take semaphore In 1-Step mode Using function HAL_HSEM_FastTake. This function takes as parameter :
  26. (++) the semaphore ID from 0_ID to 31. Note that the process ID value is implicitly assumed as zero
  27. (#) Check if a semaphore is Taken using function HAL_HSEM_IsSemTaken. This function takes as parameter :
  28. (++) the semaphore ID from 0_ID to 31
  29. (++) It returns 1 if the given semaphore is taken otherwise (Free) zero
  30. (#)Release a semaphore using function with HAL_HSEM_Release. This function takes as parameters :
  31. (++) the semaphore ID from 0 to 31
  32. (++) the process ID from 0 to 255:
  33. (++) Note: If ProcessID and MasterID match, semaphore is freed, and an interrupt
  34. may be generated when enabled (notification activated). If ProcessID or MasterID does not match,
  35. semaphore remains taken (locked)
  36. (#)Release all semaphores at once taken by a given Master using function HAL_HSEM_Release_All
  37. This function takes as parameters :
  38. (++) the Release Key (value from 0 to 0xFFFF) can be Set or Get respectively by
  39. HAL_HSEM_SetClearKey() or HAL_HSEM_GetClearKey functions
  40. (++) the Master ID:
  41. (++) Note: If the Key and MasterID match, all semaphores taken by the given CPU that corresponds
  42. to MasterID will be freed, and an interrupt may be generated when enabled (notification activated). If the
  43. Key or the MasterID doesn't match, semaphores remains taken (locked)
  44. (#)Semaphores Release all key functions:
  45. (++) HAL_HSEM_SetClearKey() to set semaphore release all Key
  46. (++) HAL_HSEM_GetClearKey() to get release all Key
  47. (#)Semaphores notification functions :
  48. (++) HAL_HSEM_ActivateNotification to activate a notification callback on
  49. a given semaphores Mask (bitfield). When one or more semaphores defined by the mask are released
  50. the callback HAL_HSEM_FreeCallback will be asserted giving as parameters a mask of the released
  51. semaphores (bitfield).
  52. (++) HAL_HSEM_DeactivateNotification to deactivate the notification of a given semaphores Mask (bitfield).
  53. (++) See the description of the macro __HAL_HSEM_SEMID_TO_MASK to check how to calculate a semaphore mask
  54. Used by the notification functions
  55. *** HSEM HAL driver macros list ***
  56. =============================================
  57. [..] Below the list of most used macros in HSEM HAL driver.
  58. (+) __HAL_HSEM_SEMID_TO_MASK: Helper macro to convert a Semaphore ID to a Mask.
  59. [..] Example of use :
  60. [..] mask = __HAL_HSEM_SEMID_TO_MASK(8) | __HAL_HSEM_SEMID_TO_MASK(21) | __HAL_HSEM_SEMID_TO_MASK(25).
  61. [..] All next macros take as parameter a semaphore Mask (bitfiled) that can be constructed using __HAL_HSEM_SEMID_TO_MASK as the above example.
  62. (+) __HAL_HSEM_ENABLE_IT: Enable the specified semaphores Mask interrupts.
  63. (+) __HAL_HSEM_DISABLE_IT: Disable the specified semaphores Mask interrupts.
  64. (+) __HAL_HSEM_GET_IT: Checks whether the specified semaphore interrupt has occurred or not.
  65. (+) __HAL_HSEM_GET_FLAG: Get the semaphores status release flags.
  66. (+) __HAL_HSEM_CLEAR_FLAG: Clear the semaphores status release flags.
  67. @endverbatim
  68. ******************************************************************************
  69. * @attention
  70. *
  71. * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
  72. * All rights reserved.</center></h2>
  73. *
  74. * This software component is licensed by ST under BSD 3-Clause license,
  75. * the "License"; You may not use this file except in compliance with the
  76. * License. You may obtain a copy of the License at:
  77. * opensource.org/licenses/BSD-3-Clause
  78. *
  79. ******************************************************************************
  80. */
  81. /* Includes ------------------------------------------------------------------*/
  82. #include "stm32wbxx_hal.h"
  83. /** @addtogroup STM32WBxx_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. /* First step write R register with MasterID, processID and take bit=1*/
  146. HSEM->R[SemID] = (ProcessID | HSEM_CR_COREID_CURRENT | HSEM_R_LOCK);
  147. /* second step : read the R register . Take achieved if MasterID and processID match and take bit set to 1 */
  148. if (HSEM->R[SemID] == (ProcessID | HSEM_CR_COREID_CURRENT | HSEM_R_LOCK))
  149. {
  150. /*take success when MasterID and ProcessID match and take bit set*/
  151. return HAL_OK;
  152. }
  153. /* Semaphore take fails*/
  154. return HAL_ERROR;
  155. }
  156. /**
  157. * @brief Fast Take a semaphore with 1 Step mode.
  158. * @param SemID: semaphore ID from 0 to 31
  159. * @retval HAL status
  160. */
  161. HAL_StatusTypeDef HAL_HSEM_FastTake(uint32_t SemID)
  162. {
  163. /* Check the parameters */
  164. assert_param(IS_HSEM_SEMID(SemID));
  165. /* Read the RLR register to take the semaphore */
  166. if (HSEM->RLR[SemID] == (HSEM_CR_COREID_CURRENT | HSEM_RLR_LOCK))
  167. {
  168. /*take success when MasterID match and take bit set*/
  169. return HAL_OK;
  170. }
  171. /* Semaphore take fails */
  172. return HAL_ERROR;
  173. }
  174. /**
  175. * @brief Check semaphore state Taken or not.
  176. * @param SemID: semaphore ID
  177. * @retval HAL HSEM state
  178. */
  179. uint32_t HAL_HSEM_IsSemTaken(uint32_t SemID)
  180. {
  181. return (((HSEM->R[SemID] & HSEM_R_LOCK) != 0U) ? 1UL : 0UL);
  182. }
  183. /**
  184. * @brief Release a semaphore.
  185. * @param SemID: semaphore ID from 0 to 31
  186. * @param ProcessID: Process ID from 0 to 255
  187. * @retval None
  188. */
  189. void HAL_HSEM_Release(uint32_t SemID, uint32_t ProcessID)
  190. {
  191. /* Check the parameters */
  192. assert_param(IS_HSEM_SEMID(SemID));
  193. assert_param(IS_HSEM_PROCESSID(ProcessID));
  194. /* Clear the semaphore by writing to the R register : the MasterID , the processID and take bit = 0 */
  195. HSEM->R[SemID] = (ProcessID | HSEM_CR_COREID_CURRENT);
  196. }
  197. /**
  198. * @brief Release All semaphore used by a given Master .
  199. * @param Key: Semaphore Key , value from 0 to 0xFFFF
  200. * @param CoreID: CoreID of the CPU that is using semaphores to be released
  201. * @retval None
  202. */
  203. void HAL_HSEM_ReleaseAll(uint32_t Key, uint32_t CoreID)
  204. {
  205. assert_param(IS_HSEM_KEY(Key));
  206. assert_param(IS_HSEM_COREID(CoreID));
  207. HSEM->CR = ((Key << HSEM_CR_KEY_Pos) | (CoreID << HSEM_CR_COREID_Pos));
  208. }
  209. /**
  210. * @}
  211. */
  212. /** @defgroup HSEM_Exported_Functions_Group2 HSEM Set and Get Key functions
  213. * @brief HSEM Set and Get Key functions.
  214. *
  215. @verbatim
  216. ==============================================================================
  217. ##### HSEM Set and Get Key functions #####
  218. ==============================================================================
  219. [..] This section provides functions allowing to:
  220. (+) Set semaphore Key
  221. (+) Get semaphore Key
  222. @endverbatim
  223. * @{
  224. */
  225. /**
  226. * @brief Set semaphore Key .
  227. * @param Key: Semaphore Key , value from 0 to 0xFFFF
  228. * @retval None
  229. */
  230. void HAL_HSEM_SetClearKey(uint32_t Key)
  231. {
  232. assert_param(IS_HSEM_KEY(Key));
  233. MODIFY_REG(HSEM->KEYR, HSEM_KEYR_KEY, (Key << HSEM_KEYR_KEY_Pos));
  234. }
  235. /**
  236. * @brief Get semaphore Key .
  237. * @retval Semaphore Key , value from 0 to 0xFFFF
  238. */
  239. uint32_t HAL_HSEM_GetClearKey(void)
  240. {
  241. return (HSEM->KEYR >> HSEM_KEYR_KEY_Pos);
  242. }
  243. /**
  244. * @}
  245. */
  246. /** @defgroup HSEM_Exported_Functions_Group3 HSEM IRQ handler management
  247. * @brief HSEM Notification functions.
  248. *
  249. @verbatim
  250. ==============================================================================
  251. ##### HSEM IRQ handler management and Notification functions #####
  252. ==============================================================================
  253. [..] This section provides HSEM IRQ handler and Notification function.
  254. @endverbatim
  255. * @{
  256. */
  257. /**
  258. * @brief Activate Semaphore release Notification for a given Semaphores Mask .
  259. * @param SemMask: Mask of Released semaphores
  260. * @retval Semaphore Key
  261. */
  262. void HAL_HSEM_ActivateNotification(uint32_t SemMask)
  263. {
  264. HSEM_COMMON->IER |= SemMask;
  265. }
  266. /**
  267. * @brief Deactivate Semaphore release Notification for a given Semaphores Mask .
  268. * @param SemMask: Mask of Released semaphores
  269. * @retval Semaphore Key
  270. */
  271. void HAL_HSEM_DeactivateNotification(uint32_t SemMask)
  272. {
  273. HSEM_COMMON->IER &= ~SemMask;
  274. }
  275. /**
  276. * @brief This function handles HSEM interrupt request
  277. * @retval None
  278. */
  279. void HAL_HSEM_IRQHandler(void)
  280. {
  281. uint32_t statusreg;
  282. /* Get the list of masked freed semaphores*/
  283. statusreg = HSEM_COMMON->MISR;
  284. /*Disable Interrupts*/
  285. HSEM_COMMON->IER &= ~((uint32_t)statusreg);
  286. /*Clear Flags*/
  287. HSEM_COMMON->ICR = ((uint32_t)statusreg);
  288. /* Call FreeCallback */
  289. HAL_HSEM_FreeCallback(statusreg);
  290. }
  291. /**
  292. * @brief Semaphore Released Callback.
  293. * @param SemMask: Mask of Released semaphores
  294. * @retval None
  295. */
  296. __weak void HAL_HSEM_FreeCallback(uint32_t SemMask)
  297. {
  298. /* Prevent unused argument(s) compilation warning */
  299. UNUSED(SemMask);
  300. /* NOTE : This function should not be modified, when the callback is needed,
  301. the HAL_HSEM_FreeCallback can be implemented in the user file
  302. */
  303. }
  304. /**
  305. * @}
  306. */
  307. /**
  308. * @}
  309. */
  310. #endif /* HAL_HSEM_MODULE_ENABLED */
  311. /**
  312. * @}
  313. */
  314. /**
  315. * @}
  316. */
  317. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/