Skip to content

Commit 4f3562b

Browse files
kbumsikfpistm
authored andcommitted
[MP1] Add GPIO locking
1 parent 169fa67 commit 4f3562b

File tree

6 files changed

+178
-0
lines changed

6 files changed

+178
-0
lines changed

cores/arduino/stm32/hw_config.c

+4
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ void hw_config_init(void)
6666
USBD_CDC_init();
6767
#endif
6868

69+
#if defined (STM32MP1xx)
70+
__HAL_RCC_HSEM_CLK_ENABLE();
71+
#endif
72+
6973
}
7074
#ifdef __cplusplus
7175
}

cores/arduino/stm32/lock_resource.c

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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>&copy; 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)

cores/arduino/stm32/lock_resource.h

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
******************************************************************************
3+
* @file lock_resource.h
4+
* @author MCD Application Team
5+
* @brief Header for lock_resource.c
6+
******************************************************************************
7+
* @attention
8+
*
9+
* <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
10+
* All rights reserved.</center></h2>
11+
*
12+
* This software component is licensed by ST under BSD 3-Clause license,
13+
* the "License"; You may not use this file except in compliance with the
14+
* License. You may obtain a copy of the License at:
15+
* opensource.org/licenses/BSD-3-Clause
16+
*
17+
******************************************************************************
18+
*/
19+
/* Define to prevent recursive inclusion -------------------------------------*/
20+
#ifndef _LOCK_RESOURCE_H
21+
#define _LOCK_RESOURCE_H
22+
23+
/* Includes ------------------------------------------------------------------*/
24+
#include "stm32_def.h"
25+
26+
27+
/* Exported types ------------------------------------------------------------*/
28+
typedef enum {
29+
LOCK_RESOURCE_STATUS_OK = 0x00U,
30+
LOCK_RESOURCE_STATUS_ERROR = 0x01U,
31+
LOCK_RESOURCE_STATUS_TIMEOUT = 0x02U
32+
} LockResource_Status_t;
33+
34+
/* Exported constants --------------------------------------------------------*/
35+
#define LOCK_RESOURCE_TIMEOUT 100U /* timeout in ms */
36+
37+
/* Exported macro ------------------------------------------------------------*/
38+
#define PERIPH_LOCK(__Periph__) Periph_Lock(__Periph__, LOCK_RESOURCE_TIMEOUT)
39+
#define PERIPH_UNLOCK(__Periph__) Periph_Unlock(__Periph__)
40+
41+
/* Exported functions ------------------------------------------------------- */
42+
LockResource_Status_t Periph_Lock(void *Peripheral, uint32_t Timeout);
43+
void Periph_Unlock(void *Peripheral);
44+
45+
46+
47+
48+
#endif /* _LOCK_RESOURCE_H */
49+
50+
51+
52+
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

cores/arduino/stm32/pinconfig.h

+16
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@
3434
#include "PinAF_STM32F1.h"
3535
#include "stm32yyxx_ll_gpio.h"
3636

37+
#if defined(STM32MP1xx)
38+
#include "lock_resource.h"
39+
#endif
40+
3741
static inline void pin_DisconnectDebug(PinName pin)
3842
{
3943
#ifdef STM32F1xx
@@ -45,6 +49,9 @@ static inline void pin_DisconnectDebug(PinName pin)
4549

4650
static inline void pin_PullConfig(GPIO_TypeDef *gpio, uint32_t ll_pin, uint32_t pull_config)
4751
{
52+
#if defined(STM32MP1xx)
53+
PERIPH_LOCK(gpio);
54+
#endif
4855
#ifdef STM32F1xx
4956
uint32_t function = LL_GPIO_GetPinMode(gpio, ll_pin);
5057
#endif
@@ -77,10 +84,16 @@ static inline void pin_PullConfig(GPIO_TypeDef *gpio, uint32_t ll_pin, uint32_t
7784
#endif
7885
break;
7986
}
87+
#if defined(STM32MP1xx)
88+
PERIPH_UNLOCK(gpio);
89+
#endif
8090
}
8191

8292
static inline void pin_SetAFPin(GPIO_TypeDef *gpio, PinName pin, uint32_t afnum)
8393
{
94+
#if defined(STM32MP1xx)
95+
PERIPH_LOCK(gpio);
96+
#endif
8497
#ifdef STM32F1xx
8598
UNUSED(gpio);
8699
UNUSED(pin);
@@ -94,6 +107,9 @@ static inline void pin_SetAFPin(GPIO_TypeDef *gpio, PinName pin, uint32_t afnum)
94107
LL_GPIO_SetAFPin_0_7(gpio, ll_pin, afnum);
95108
}
96109
#endif
110+
#if defined(STM32MP1xx)
111+
PERIPH_UNLOCK(gpio);
112+
#endif
97113
}
98114

99115
#endif

cores/arduino/stm32/pinmap.c

+11
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
#include "pinconfig.h"
1919
#include "stm32yyxx_ll_gpio.h"
2020

21+
#if defined(STM32MP1xx)
22+
#include "lock_resource.h"
23+
#endif
24+
2125
/* Map STM_PIN to LL */
2226
const uint32_t pin_map_ll[16] = {
2327
LL_GPIO_PIN_0,
@@ -70,6 +74,9 @@ void pin_function(PinName pin, int function)
7074
/* Enable GPIO clock */
7175
GPIO_TypeDef *gpio = set_GPIO_Port_Clock(port);
7276

77+
#if defined(STM32MP1xx)
78+
PERIPH_LOCK(gpio);
79+
#endif
7380
/* Set default speed to high.
7481
* For most families there are dedicated registers so it is
7582
* not so important, register can be set at any time.
@@ -128,6 +135,10 @@ void pin_function(PinName pin, int function)
128135
pin_PullConfig(gpio, ll_pin, STM_PIN_PUPD(function));
129136

130137
pin_DisconnectDebug(pin);
138+
139+
#if defined(STM32MP1xx)
140+
PERIPH_UNLOCK(gpio);
141+
#endif
131142
}
132143

133144
void pinmap_pinout(PinName pin, const PinMap *map)

system/STM32MP1xx/stm32mp1xx_hal_conf_default.h

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#define HAL_RTC_MODULE_DISABLED /* RTC MODULE on Cortex-M side is not supported.
4242
Linux on Cortex-A will handle this. */
4343
#define HAL_ETH_MODULE_DISABLED /* ETH module is also not intended to be used */
44+
#define HAL_HSEM_MODULE_ENABLED
4445
#define HAL_MDMA_MODULE_ENABLED /* Some other modules (e.g. USART) require this */
4546

4647
#if defined(ARDUINO_STM32MP157A_DK1) || defined(ARDUINO_STM32MP157C_DK2)

0 commit comments

Comments
 (0)