Skip to content

Commit e6bf53b

Browse files
committed
Rework hardware semaphore usage
This new implementation is mroe generic and support MP1 as well WB. H7 could be added easily later if dual core support is implemented. Signed-off-by: Frederic Pillon <frederic.pillon@st.com>
1 parent d999a60 commit e6bf53b

File tree

5 files changed

+157
-173
lines changed

5 files changed

+157
-173
lines changed

cores/arduino/stm32/lock_resource.h

+139-25
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,22 @@
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-
*/
1+
/*
2+
*******************************************************************************
3+
* Copyright (c) 2019-2021, STMicroelectronics
4+
* All rights reserved.
5+
*
6+
* This software component is licensed by ST under BSD 3-Clause license,
7+
* the "License"; You may not use this file except in compliance with the
8+
* License. You may obtain a copy of the License at:
9+
* opensource.org/licenses/BSD-3-Clause
10+
*
11+
*******************************************************************************
12+
*/
1913
/* Define to prevent recursive inclusion -------------------------------------*/
2014
#ifndef _LOCK_RESOURCE_H
2115
#define _LOCK_RESOURCE_H
2216

2317
/* Includes ------------------------------------------------------------------*/
2418
#include "stm32_def.h"
19+
#include "stm32yyxx_ll_hsem.h"
2520

2621
#ifdef __cplusplus
2722
extern "C" {
@@ -35,15 +30,134 @@ typedef enum {
3530
} LockResource_Status_t;
3631

3732
/* Exported constants --------------------------------------------------------*/
38-
#define LOCK_RESOURCE_TIMEOUT 100U /* timeout in ms */
33+
#if defined(STM32WBxx)
34+
/*
35+
* HW semaphore Complement ID list defined in hw_conf.h from STM32WB.
36+
* They could be used also for H7 dualcore targets.
37+
*/
38+
/*
39+
* Index of the semaphore used by CPU2 to prevent the CPU1 to either write or
40+
* erase data in flash. The CPU1 shall not either write or erase in flash when
41+
* this semaphore is taken by the CPU2. When the CPU1 needs to either write or
42+
* erase in flash, it shall first get the semaphore and release it just
43+
* after writing a raw (64bits data) or erasing one sector.
44+
* On v1.4.0 and older CPU2 wireless firmware, this semaphore is unused and
45+
* CPU2 is using PES bit. By default, CPU2 is using the PES bit to protect its
46+
* timing. The CPU1 may request the CPU2 to use the semaphore instead of the
47+
* PES bit by sending the system command SHCI_C2_SetFlashActivityControl()
48+
*/
49+
#define CFG_HW_BLOCK_FLASH_REQ_BY_CPU2_SEMID 7U
3950

40-
/* Exported macro ------------------------------------------------------------*/
41-
#define PERIPH_LOCK(__Periph__) Periph_Lock(__Periph__, LOCK_RESOURCE_TIMEOUT)
42-
#define PERIPH_UNLOCK(__Periph__) Periph_Unlock(__Periph__)
51+
/*
52+
* Index of the semaphore used by CPU1 to prevent the CPU2 to either write or
53+
* erase data in flash. In order to protect its timing, the CPU1 may get this
54+
* semaphore to prevent the CPU2 to either write or erase in flash
55+
* (as this will stall both CPUs)
56+
* The PES bit shall not be used as this may stall the CPU2 in some cases.
57+
*/
58+
#define CFG_HW_BLOCK_FLASH_REQ_BY_CPU1_SEMID 6U
4359

44-
/* Exported functions ------------------------------------------------------- */
45-
LockResource_Status_t Periph_Lock(void *Peripheral, uint32_t Timeout);
46-
void Periph_Unlock(void *Peripheral);
60+
/*
61+
* Index of the semaphore used to manage the CLK48 clock configuration
62+
* When the USB is required, this semaphore shall be taken before configuring
63+
* the CLK48 for USB and should be released after the application switch OFF
64+
* the clock when the USB is not used anymore. When using the RNG, it is good
65+
* enough to use CFG_HW_RNG_SEMID to control CLK48.
66+
* More details in AN5289
67+
*/
68+
#define CFG_HW_CLK48_CONFIG_SEMID 5U
69+
#define CFG_HW_RCC_CRRCR_CCIPR_SEMID CFG_HW_CLK48_CONFIG_SEMID
70+
71+
/* Index of the semaphore used to manage the entry Stop Mode procedure */
72+
#define CFG_HW_ENTRY_STOP_MODE_SEMID 4U
73+
#define CFG_HW_ENTRY_STOP_MODE_MASK_SEMID (1U << CFG_HW_ENTRY_STOP_MODE_SEMID)
74+
75+
/* Index of the semaphore used to access the RCC */
76+
#define CFG_HW_RCC_SEMID 3U
77+
78+
/* Index of the semaphore used to access the FLASH */
79+
#define CFG_HW_FLASH_SEMID 2U
80+
81+
/* Index of the semaphore used to access the PKA */
82+
#define CFG_HW_PKA_SEMID 1U
83+
84+
/* Index of the semaphore used to access the RNG */
85+
#define CFG_HW_RNG_SEMID 0U
86+
87+
/* Index of the semaphore used to access GPIO */
88+
#define CFG_HW_GPIO_SEMID 8U
89+
90+
/* Index of the semaphore used to access the EXTI */
91+
#define CFG_HW_EXTI_SEMID 9U
92+
93+
#elif defined(STM32MP1xx)
94+
/*
95+
* HW semaphore from STM32MP1
96+
* EXTI and GPIO are inherited from STM32MP1 Linux.
97+
* Other SEMID are not used by linux and must not be used here,
98+
* but reserved for MPU.
99+
*/
100+
/* Index of the semaphore used to access GPIO */
101+
#define CFG_HW_GPIO_SEMID 0U
102+
103+
/* Index of the semaphore used to access the EXTI */
104+
#define CFG_HW_EXTI_SEMID 1U
105+
#else
106+
/* Fake semaphore ID definition for compilation purpose only */
107+
#define CFG_HW_BLOCK_FLASH_REQ_BY_CPU2_SEMID 0U
108+
#define CFG_HW_BLOCK_FLASH_REQ_BY_CPU1_SEMID 0U
109+
#define CFG_HW_CLK48_CONFIG_SEMID 0U
110+
#define CFG_HW_RCC_CRRCR_CCIPR_SEMID 0U
111+
#define CFG_HW_ENTRY_STOP_MODE_SEMID 0U
112+
#define CFG_HW_RCC_SEMID 0U
113+
#define CFG_HW_FLASH_SEMID 0U
114+
#define CFG_HW_PKA_SEMID 0U
115+
#define CFG_HW_RNG_SEMID 0U
116+
#define CFG_HW_GPIO_SEMID 0U
117+
#define CFG_HW_EXTI_SEMID 0U
118+
#endif /* STM32WBxx */
119+
120+
/** Hardware Semaphore wait forever value */
121+
#define HSEM_LOCK_WAIT_FOREVER 0xFFFFFFFFU
122+
/** Hardware Semaphore default retry value */
123+
#ifndef HSEM_LOCK_DEFAULT_RETRY
124+
#define HSEM_LOCK_DEFAULT_RETRY 0xFFFFU
125+
#endif
126+
127+
/*
128+
* @brief hsem_lock function is used for register protection of shared Peripheral
129+
* and shall be called before accessing registers of this shared Peripheral
130+
* If Semaphore id is already taken, the function will busy loop waiting for it to
131+
* be released, but give up after @retry downcounter have elapsed
132+
* @param semID: Semaphore id used to identify which peripheral to protect
133+
* @param retry: number of retry
134+
* @retval None
135+
*/
136+
static inline void hsem_lock(uint32_t semID, uint32_t retry)
137+
{
138+
#if defined(STM32MP1xx) || defined(STM32WBxx)
139+
while (LL_HSEM_1StepLock(HSEM, semID)) {
140+
if (retry != HSEM_LOCK_WAIT_FOREVER) {
141+
retry--;
142+
if (retry == 0) {
143+
Error_Handler();
144+
}
145+
}
146+
}
147+
#endif /* STM32MP1xx || STM32WBxx */
148+
}
149+
150+
/*
151+
* @brief hsem_unlock released a previously-acquired semaphore
152+
* @param semID Semaphore id used to identify which peripheral to release
153+
* @retval None
154+
*/
155+
static inline void hsem_unlock(uint32_t semID)
156+
{
157+
#if defined(STM32MP1xx) || defined(STM32WBxx)
158+
LL_HSEM_ReleaseLock(HSEM, semID, 0);
159+
#endif /* STM32MP1xx || STM32WBxx */
160+
}
47161

48162
#ifdef __cplusplus
49163
} // extern "C"

cores/arduino/stm32/pinconfig.h

+11-39
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,24 @@
11
/*
22
*******************************************************************************
3-
* Copyright (c) 2018, STMicroelectronics
3+
* Copyright (c) 2018-2021, STMicroelectronics
44
* All rights reserved.
55
*
6-
* Redistribution and use in source and binary forms, with or without
7-
* modification, are permitted provided that the following conditions are met:
6+
* This software component is licensed by ST under BSD 3-Clause license,
7+
* the "License"; You may not use this file except in compliance with the
8+
* License. You may obtain a copy of the License at:
9+
* opensource.org/licenses/BSD-3-Clause
810
*
9-
* 1. Redistributions of source code must retain the above copyright notice,
10-
* this list of conditions and the following disclaimer.
11-
* 2. Redistributions in binary form must reproduce the above copyright notice,
12-
* this list of conditions and the following disclaimer in the documentation
13-
* and/or other materials provided with the distribution.
14-
* 3. Neither the name of STMicroelectronics nor the names of its contributors
15-
* may be used to endorse or promote products derived from this software
16-
* without specific prior written permission.
17-
*
18-
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19-
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20-
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21-
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22-
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23-
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24-
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25-
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26-
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27-
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2811
*******************************************************************************
12+
*
2913
* Based on mbed-os/target/TARGET_STM/TARGET_STMYY/pin_device.h
3014
*/
3115
#ifndef _PINCONFIG_H
3216
#define _PINCONFIG_H
3317

3418
#include "PinAF_STM32F1.h"
19+
#include "lock_resource.h"
3520
#include "stm32yyxx_ll_gpio.h"
3621

37-
#if defined(STM32MP1xx)
38-
#include "lock_resource.h"
39-
#endif
40-
4122
static inline void pin_DisconnectDebug(PinName pin)
4223
{
4324
#ifdef STM32F1xx
@@ -49,13 +30,10 @@ static inline void pin_DisconnectDebug(PinName pin)
4930

5031
static inline void pin_PullConfig(GPIO_TypeDef *gpio, uint32_t ll_pin, uint32_t pull_config)
5132
{
52-
#if defined(STM32MP1xx)
53-
PERIPH_LOCK(gpio);
54-
#endif
5533
#ifdef STM32F1xx
5634
uint32_t function = LL_GPIO_GetPinMode(gpio, ll_pin);
5735
#endif
58-
36+
hsem_lock(CFG_HW_GPIO_SEMID, HSEM_LOCK_DEFAULT_RETRY);
5937
switch (pull_config) {
6038
case GPIO_PULLUP:
6139
#ifdef STM32F1xx
@@ -84,31 +62,25 @@ static inline void pin_PullConfig(GPIO_TypeDef *gpio, uint32_t ll_pin, uint32_t
8462
#endif
8563
break;
8664
}
87-
#if defined(STM32MP1xx)
88-
PERIPH_UNLOCK(gpio);
89-
#endif
65+
hsem_unlock(CFG_HW_GPIO_SEMID);
9066
}
9167

9268
static inline void pin_SetAFPin(GPIO_TypeDef *gpio, PinName pin, uint32_t afnum)
9369
{
94-
#if defined(STM32MP1xx)
95-
PERIPH_LOCK(gpio);
96-
#endif
9770
#ifdef STM32F1xx
9871
UNUSED(gpio);
9972
UNUSED(pin);
10073
pin_SetF1AFPin(afnum);
10174
#else
10275
uint32_t ll_pin = STM_LL_GPIO_PIN(pin);
10376

77+
hsem_lock(CFG_HW_GPIO_SEMID, HSEM_LOCK_DEFAULT_RETRY);
10478
if (STM_PIN(pin) > 7) {
10579
LL_GPIO_SetAFPin_8_15(gpio, ll_pin, afnum);
10680
} else {
10781
LL_GPIO_SetAFPin_0_7(gpio, ll_pin, afnum);
10882
}
109-
#endif
110-
#if defined(STM32MP1xx)
111-
PERIPH_UNLOCK(gpio);
83+
hsem_unlock(CFG_HW_GPIO_SEMID);
11284
#endif
11385
}
11486

libraries/SrcWrapper/src/stm32/interrupt.cpp

+3-9
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@
3636
******************************************************************************
3737
*/
3838
#include "interrupt.h"
39-
#if defined(STM32MP1xx)
40-
#include "lock_resource.h"
41-
#endif
39+
#include "lock_resource.h"
4240
#if !defined(HAL_EXTI_MODULE_DISABLED)
4341

4442
/* Private Types */
@@ -179,15 +177,11 @@ void stm32_interrupt_enable(GPIO_TypeDef *port, uint16_t pin, callback_function_
179177

180178
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
181179

182-
#if defined(STM32MP1xx)
183-
PERIPH_LOCK(port);
184-
#endif
180+
hsem_lock(CFG_HW_GPIO_SEMID, HSEM_LOCK_DEFAULT_RETRY);
185181

186182
HAL_GPIO_Init(port, &GPIO_InitStruct);
187183

188-
#if defined(STM32MP1xx)
189-
PERIPH_UNLOCK(port);
190-
#endif
184+
hsem_unlock(CFG_HW_GPIO_SEMID);
191185

192186
gpio_irq_conf[id].callback = callback;
193187

0 commit comments

Comments
 (0)