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