|
| 1 | +/** |
| 2 | + ****************************************************************************** |
| 3 | + * @file lock_resource.c |
| 4 | + * @author MCD Application Team |
| 5 | + * @brief This sample code provides hardware semaphore using HSEM for |
| 6 | + * synchronization and mutual exclusion between heterogeneous processors |
| 7 | + * and those not operating under a single, shared operating system. |
| 8 | + ****************************************************************************** |
| 9 | + * @attention |
| 10 | + * |
| 11 | + * <h2><center>© Copyright (c) 2019 STMicroelectronics. |
| 12 | + * All rights reserved.</center></h2> |
| 13 | + * |
| 14 | + * This software component is licensed by ST under BSD 3-Clause license, |
| 15 | + * the "License"; You may not use this file except in compliance with the |
| 16 | + * License. You may obtain a copy of the License at: |
| 17 | + * opensource.org/licenses/BSD-3-Clause |
| 18 | + * |
| 19 | + ****************************************************************************** |
| 20 | + */ |
| 21 | + |
| 22 | +/* Includes ------------------------------------------------------------------*/ |
| 23 | +#include "lock_resource.h" |
| 24 | + |
| 25 | +#if defined(HAL_HSEM_MODULE_ENABLED) \ |
| 26 | + && defined(STM32MP1xx) |
| 27 | + |
| 28 | +/* Private macro -------------------------------------------------------------*/ |
| 29 | + |
| 30 | +/* |
| 31 | + * This macro provides a semaphore id for a dedicated peripheral. |
| 32 | + * This macro shall be configured by user according its needs and aligned with remote processors |
| 33 | + * (e.g. same semaphore id shall be used between processors for a dedicated peripheral) |
| 34 | + */ |
| 35 | +#define GET_HSEM_SEM_INDEX(__Peripheral__) (uint8_t)(((GPIO_TypeDef *)(__Peripheral__) == (GPIOA))? 0U :\ |
| 36 | + ((GPIO_TypeDef *)(__Peripheral__) == (GPIOB))? 0U :\ |
| 37 | + ((GPIO_TypeDef *)(__Peripheral__) == (GPIOC))? 0U :\ |
| 38 | + ((GPIO_TypeDef *)(__Peripheral__) == (GPIOD))? 0U :\ |
| 39 | + ((GPIO_TypeDef *)(__Peripheral__) == (GPIOE))? 0U :\ |
| 40 | + ((GPIO_TypeDef *)(__Peripheral__) == (GPIOF))? 0U :\ |
| 41 | + ((GPIO_TypeDef *)(__Peripheral__) == (GPIOG))? 0U :\ |
| 42 | + ((GPIO_TypeDef *)(__Peripheral__) == (GPIOH))? 0U :\ |
| 43 | + ((GPIO_TypeDef *)(__Peripheral__) == (GPIOI))? 0U :\ |
| 44 | + ((GPIO_TypeDef *)(__Peripheral__) == (GPIOJ))? 0U :\ |
| 45 | + ((GPIO_TypeDef *)(__Peripheral__) == (GPIOK))? 0U :\ |
| 46 | + ((GPIO_TypeDef *)(__Peripheral__) == (GPIOZ))? 0U :\ |
| 47 | + ((EXTI_TypeDef *)(__Peripheral__) == (EXTI))? 1U : HSEM_SEMID_MAX + 1U) |
| 48 | + |
| 49 | +/* Private user code ---------------------------------------------------------*/ |
| 50 | + |
| 51 | + |
| 52 | +/** |
| 53 | + * @brief Periph_Lock function is used for register protection of shared @Peripheral |
| 54 | + * and shall be called before accessing registers of this shared @Peripheral |
| 55 | + * If Semaphore id is already taken, the function will busy loop waiting for it to |
| 56 | + * be released, but give up after @Timeout msecs have elapsed. |
| 57 | + * @param Peripheral: used to identify which peripheral to protect. |
| 58 | + * Semaphore id deduced from this peripheral. |
| 59 | + * Timeout: timeout value in msecs |
| 60 | + * @retval Return Status |
| 61 | + */ |
| 62 | +LockResource_Status_t Periph_Lock(void *Peripheral, uint32_t Timeout) |
| 63 | +{ |
| 64 | + uint32_t tickstart = 0U; |
| 65 | + LockResource_Status_t ret = LOCK_RESOURCE_STATUS_OK; |
| 66 | + |
| 67 | + /* Init tickstart for timeout management*/ |
| 68 | + tickstart = HAL_GetTick(); |
| 69 | + |
| 70 | + /* Try to Take HSEM assigned to the Peripheral */ |
| 71 | + while (HAL_HSEM_FastTake(GET_HSEM_SEM_INDEX(Peripheral)) != HAL_OK) { |
| 72 | + |
| 73 | + if ((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout)) { |
| 74 | + ret = LOCK_RESOURCE_STATUS_TIMEOUT; |
| 75 | + Error_Handler(); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + return ret; |
| 80 | +} |
| 81 | + |
| 82 | +/** |
| 83 | + * @brief The Periph_Unlock released a previously-acquired semaphore which we want to unlock |
| 84 | + * @param Peripheral: used to identify which peripheral and the related semaphore |
| 85 | + * @retval None |
| 86 | + */ |
| 87 | +void Periph_Unlock(void *Peripheral) |
| 88 | +{ |
| 89 | + /* Release HSEM */ |
| 90 | + HAL_HSEM_Release(GET_HSEM_SEM_INDEX(Peripheral), 0); |
| 91 | + |
| 92 | +} |
| 93 | + |
| 94 | +#endif // defined(HAL_HSEM_MODULE_ENABLED) |
0 commit comments