stm32h7xx_hal_hsem.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /**
  2. ******************************************************************************
  3. * @file stm32h7xx_hal_hsem.c
  4. * @author MCD Application Team
  5. * @version V1.0.0
  6. * @date 21-April-2017
  7. * @brief HSEM HAL module driver.
  8. * This file provides firmware functions to manage the following
  9. * functionalities of the semaphore peripheral:
  10. * + Semaphore Take function (2-Step Procedure) , non blocking
  11. * + Semaphore FastTake function (1-Step Procedure) , non blocking
  12. * + Semaphore Status check
  13. * + Semaphore Clear Key Set and Get
  14. * + Release and release all functions
  15. * + Semaphore notification enabling and disabling and callnack functions
  16. * + IRQ handler management
  17. *
  18. *
  19. @verbatim
  20. ==============================================================================
  21. ##### How to use this driver #####
  22. ==============================================================================
  23. [..]
  24. (#)Take a semaphore In 2-Step mode Using function HAL_HSEM_Take. This function takes as parameters :
  25. (++) the semaphore ID from 0 to 31
  26. (++) the process ID from 0 to 255
  27. (#) Fast Take semaphore In 1-Step mode Using function HAL_HSEM_FastTake. This function takes as parameter :
  28. (++) the semaphore ID from 0_ID to 31. Note that the process ID value is implicitly assumed as zero
  29. (#) Check if a semaphore is Taken using function HAL_HSEM_IsSemTaken. This function takes as parameter :
  30. (++) the semaphore ID from 0_ID to 31
  31. (++) It returns 1 if the given semaphore is taken otherwise (Free) zero.
  32. (#)Release a semaphore using function with HAL_HSEM_Release. This function takes as parameters :
  33. (++) the semaphore ID from 0 to 31
  34. (++) the process ID from 0 to 255:
  35. (++) Note: If ProcessID and MasterID match, semaphore is freed, and an interrupt
  36. may be generated when enabled (notification activated). If ProcessID or MasterID does not match,
  37. semaphore remains taken (locked).
  38. (#)Release all semaphores at once taken by a given Master using function HAL_HSEM_Release_All
  39. This function takes as parameters :
  40. (++) the Release Key (value from 0 to 0xFFFF) can be Set or Get respectively by
  41. HAL_HSEM_SetClearKey() or HAL_HSEM_GetClearKey functions
  42. (++) the Master ID:
  43. (++) Note: If the Key and MasterID match, all semaphores taken by the given CPU that corresponds
  44. to MasterID will be freed, and an interrupt may be generated when enabled (notification activated). If the
  45. Key or the MasterID doesn't match, semaphores remains taken (locked).
  46. (#)Semaphores Release all key functions:
  47. (++) HAL_HSEM_SetClearKey() to set semaphore release all Key
  48. (++) HAL_HSEM_GetClearKey() to get release all Key
  49. (#)Semaphores notification functions :
  50. (++) HAL_HSEM_ActivateNotification to activate a notification callback on
  51. a given semaphores Mask (bitfield). When one or more semaphores defined by the mask are released
  52. the callback HAL_HSEM_FreeCallback will be asserted giving as parameters a mask of the released
  53. semaphores (bitfield).
  54. (++) HAL_HSEM_DeactivateNotification to deactivate the notification of a given semaphores Mask (bitfield).
  55. (++) See the description of the macro __HAL_HSEM_SEMID_TO_MASK to check how to calculate a semaphore mask
  56. Used by the notification functions
  57. *** HSEM HAL driver macros list ***
  58. =============================================
  59. [..] Below the list of most used macros in HSEM HAL driver.
  60. (+) __HAL_HSEM_SEMID_TO_MASK: Helper macro to convert a Semaphore ID to a Mask.
  61. [..] Example of use :
  62. [..] mask = __HAL_HSEM_SEMID_TO_MASK(8) | __HAL_HSEM_SEMID_TO_MASK(21) | __HAL_HSEM_SEMID_TO_MASK(25).
  63. [..] All next macros take as parameter a semaphore Mask (bitfiled) that can be constructed using __HAL_HSEM_SEMID_TO_MASK as the above example.
  64. (+) __HAL_HSEM_ENABLE_IT: Enable the specified semaphores Mask interrupts.
  65. (+) __HAL_HSEM_DISABLE_IT: Disable the specified semaphores Mask interrupts.
  66. (+) __HAL_HSEM_GET_IT: Checks whether the specified semaphore interrupt has occurred or not.
  67. (+) __HAL_HSEM_GET_FLAG: Get the semaphores status release flags.
  68. (+) __HAL_HSEM_CLEAR_FLAG: Clear the semaphores status release flags.
  69. @endverbatim
  70. ******************************************************************************
  71. * @attention
  72. *
  73. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  74. *
  75. * Redistribution and use in source and binary forms, with or without modification,
  76. * are permitted provided that the following conditions are met:
  77. * 1. Redistributions of source code must retain the above copyright notice,
  78. * this list of conditions and the following disclaimer.
  79. * 2. Redistributions in binary form must reproduce the above copyright notice,
  80. * this list of conditions and the following disclaimer in the documentation
  81. * and/or other materials provided with the distribution.
  82. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  83. * may be used to endorse or promote products derived from this software
  84. * without specific prior written permission.
  85. *
  86. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  87. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  88. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  89. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  90. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  91. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  92. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  93. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  94. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  95. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  96. *
  97. ******************************************************************************
  98. */
  99. /* Includes ------------------------------------------------------------------*/
  100. #include "stm32h7xx_hal.h"
  101. /** @addtogroup STM32H7xx_HAL_Driver
  102. * @{
  103. */
  104. /** @defgroup HSEM HSEM
  105. * @brief HSEM HAL module driver
  106. * @{
  107. */
  108. #ifdef HAL_HSEM_MODULE_ENABLED
  109. /* Private typedef -----------------------------------------------------------*/
  110. /* Private define ------------------------------------------------------------*/
  111. /* Private macro -------------------------------------------------------------*/
  112. /* Private variables ---------------------------------------------------------*/
  113. /* Private function prototypes -----------------------------------------------*/
  114. /* Private functions ---------------------------------------------------------*/
  115. /* Exported functions --------------------------------------------------------*/
  116. /** @defgroup HSEM_Exported_Functions HSEM Exported Functions
  117. * @{
  118. */
  119. /** @defgroup HSEM_Exported_Functions_Group1 Take and Release functions
  120. * @brief HSEM Take and Release functions
  121. *
  122. @verbatim
  123. ==============================================================================
  124. ##### HSEM Take and Release functions #####
  125. ==============================================================================
  126. [..] This section provides functions allowing to:
  127. (+) Take a semaphore with 2 Step method
  128. (+) Fast Take a semaphore with 1 Step method
  129. (+) Check semaphore state Taken or not
  130. (+) Release a semaphore
  131. (+) Release all semaphore at once
  132. @endverbatim
  133. * @{
  134. */
  135. /**
  136. * @brief Take a semaphore in 2 Step mode.
  137. * @param SemID: semaphore ID from 0 to 31
  138. * @param ProcessID: Process ID from 0 to 255
  139. * @retval HAL status
  140. */
  141. HAL_StatusTypeDef HAL_HSEM_Take(uint32_t SemID, uint32_t ProcessID)
  142. {
  143. /* Check the parameters */
  144. assert_param(IS_HSEM_SEMID(SemID));
  145. assert_param(IS_HSEM_PROCESSID(ProcessID));
  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. /* Semaphore take fails*/
  155. return HAL_ERROR;
  156. }
  157. /**
  158. * @brief Fast Take a semaphore with 1 Step mode.
  159. * @param SemID: semaphore ID from 0 to 31
  160. * @retval HAL status
  161. */
  162. HAL_StatusTypeDef HAL_HSEM_FastTake(uint32_t SemID)
  163. {
  164. /* Check the parameters */
  165. assert_param(IS_HSEM_SEMID(SemID));
  166. /* Read the RLR register to take the semaphore */
  167. if(HSEM->RLR[SemID] == (((HAL_GetCurrentCPUID() << POSITION_VAL(HSEM_R_MASTERID)) & HSEM_RLR_MASTERID) | HSEM_RLR_LOCK))
  168. {
  169. /*take success when MasterID match and take bit set*/
  170. return HAL_OK;
  171. }
  172. /* Semaphore take fails */
  173. return HAL_ERROR;
  174. }
  175. /**
  176. * @brief Check semaphore state Taken or not.
  177. * @param SemID: semaphore ID
  178. * @retval HAL HSEM state
  179. */
  180. uint32_t HAL_HSEM_IsSemTaken(uint32_t SemID)
  181. {
  182. return ((HSEM->R[SemID] & HSEM_R_LOCK) != 0U);
  183. }
  184. /**
  185. * @brief Release a semaphore.
  186. * @param SemID: semaphore ID from 0 to 31
  187. * @param ProcessID: Process ID from 0 to 255
  188. * @retval None
  189. */
  190. void HAL_HSEM_Release(uint32_t SemID, uint32_t ProcessID)
  191. {
  192. /* Check the parameters */
  193. assert_param(IS_HSEM_SEMID(SemID));
  194. assert_param(IS_HSEM_PROCESSID(ProcessID));
  195. /* Clear the semaphore by writing to the R register : the MasterID , the processID and take bit = 0 */
  196. HSEM->R[SemID] = ((ProcessID & HSEM_R_PROCID) | ((HAL_GetCurrentCPUID() << POSITION_VAL(HSEM_R_MASTERID)) & HSEM_R_MASTERID));
  197. }
  198. /**
  199. * @brief Release All semaphore used by a given Master .
  200. * @param Key: Semaphore Key , value from 0 to 0xFFFF
  201. * @param MasterID: MasterID of the CPU that is using semaphores to be Released
  202. * @retval None
  203. */
  204. void HAL_HSEM_ReleaseAll(uint32_t Key, uint32_t MasterID)
  205. {
  206. assert_param(IS_HSEM_KEY(Key));
  207. assert_param(IS_HSEM_MASTERID(MasterID));
  208. HSEM->CR = (((Key << POSITION_VAL(HSEM_KEYR_KEY)) & HSEM_CR_KEY ) | ((MasterID << POSITION_VAL(HSEM_CR_MASTERID)) & HSEM_CR_MASTERID));
  209. }
  210. /**
  211. * @}
  212. */
  213. /** @defgroup HSEM_Exported_Functions_Group2 HSEM Set and Get Key functions
  214. * @brief HSEM Set and Get Key functions.
  215. *
  216. @verbatim
  217. ==============================================================================
  218. ##### HSEM Set and Get Key functions #####
  219. ==============================================================================
  220. [..] This section provides functions allowing to:
  221. (+) Set semaphore Key
  222. (+) Get semaphore Key
  223. @endverbatim
  224. * @{
  225. */
  226. /**
  227. * @brief Set semaphore Key .
  228. * @param Key: Semaphore Key , value from 0 to 0xFFFF
  229. * @retval None
  230. */
  231. void HAL_HSEM_SetClearKey(uint32_t Key)
  232. {
  233. assert_param(IS_HSEM_KEY(Key));
  234. MODIFY_REG(HSEM->KEYR, HSEM_KEYR_KEY, (Key << POSITION_VAL(HSEM_KEYR_KEY)));
  235. }
  236. /**
  237. * @brief Get semaphore Key .
  238. * @retval Semaphore Key , value from 0 to 0xFFFF
  239. */
  240. uint32_t HAL_HSEM_GetClearKey(void)
  241. {
  242. return (HSEM->KEYR >> POSITION_VAL(HSEM_KEYR_KEY));
  243. }
  244. /**
  245. * @}
  246. */
  247. /** @defgroup HSEM_Exported_Functions_Group3 HSEM IRQ handler management
  248. * @brief HSEM Notification functions.
  249. *
  250. @verbatim
  251. ==============================================================================
  252. ##### HSEM IRQ handler management and Notification functions #####
  253. ==============================================================================
  254. [..] This section provides HSEM IRQ handler and Notification function.
  255. @endverbatim
  256. * @{
  257. */
  258. /**
  259. * @brief Activate Semaphore release Notification for a given Semaphores Mask .
  260. * @param SemMask: Mask of Released semaphores
  261. * @retval Semaphore Key
  262. */
  263. void HAL_HSEM_ActivateNotification(uint32_t SemMask)
  264. {
  265. /*Activate interrupt for CM7 Master */
  266. HSEM->IER |= SemMask;
  267. }
  268. /**
  269. * @brief Deactivate Semaphore release Notification for a given Semaphores Mask .
  270. * @param SemMask: Mask of Released semaphores
  271. * @retval Semaphore Key
  272. */
  273. void HAL_HSEM_DeactivateNotification(uint32_t SemMask)
  274. {
  275. /*Deactivate interrupt for CM7 Master */
  276. HSEM->IER &= ~SemMask;
  277. }
  278. /**
  279. * @brief This function handles HSEM interrupt request.
  280. * @retval None
  281. */
  282. void HAL_HSEM_IRQHandler(void)
  283. {
  284. uint32_t statusreg = 0U;
  285. /* Get the list of masked freed semaphores*/
  286. statusreg = HSEM->MISR;
  287. /*Disable Interrupts*/
  288. HSEM->IER &= ~((uint32_t)statusreg);
  289. /*Clear Flags*/
  290. HSEM->ICR |= ((uint32_t)statusreg);
  291. /* Call FreeCallback */
  292. HAL_HSEM_FreeCallback(statusreg);
  293. }
  294. /**
  295. * @brief Semaphore Released Callback.
  296. * @param SemMask: Mask of Released semaphores
  297. * @retval None
  298. */
  299. __weak void HAL_HSEM_FreeCallback(uint32_t SemMask)
  300. {
  301. /* Prevent unused argument(s) compilation warning */
  302. UNUSED(SemMask);
  303. /* NOTE : This function should not be modified, when the callback is needed,
  304. the HAL_HSEM_FreeCallback can be implemented in the user file
  305. */
  306. }
  307. /**
  308. * @}
  309. */
  310. /**
  311. * @}
  312. */
  313. #endif /* HAL_HSEM_MODULE_ENABLED */
  314. /**
  315. * @}
  316. */
  317. /**
  318. * @}
  319. */
  320. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/