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