diff --git a/cores/arduino/board.h b/cores/arduino/board.h index 55ae8c1f62..1cc7cd3d33 100644 --- a/cores/arduino/board.h +++ b/cores/arduino/board.h @@ -12,6 +12,7 @@ #include "digital_io.h" #include "dwt.h" #include "hw_config.h" +#include "otp.h" #include "timer.h" #include "uart.h" diff --git a/cores/arduino/stm32/clock.h b/cores/arduino/stm32/clock.h index 8841640261..23ee45055a 100644 --- a/cores/arduino/stm32/clock.h +++ b/cores/arduino/stm32/clock.h @@ -1,39 +1,15 @@ -/** - ****************************************************************************** - * @file clock.h - * @author WI6LABS - * @version V1.0.0 - * @date 01-August-2016 - * @brief Header for clock.c module - ****************************************************************************** - * @attention - * - *

© COPYRIGHT(c) 2016 STMicroelectronics

- * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. Neither the name of STMicroelectronics nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - ****************************************************************************** - */ +/* + ******************************************************************************* + * Copyright (c) 2016-2021, STMicroelectronics + * All rights reserved. + * + * This software component is licensed by ST under BSD 3-Clause license, + * the "License"; You may not use this file except in compliance with the + * License. You may obtain a copy of the License at: + * opensource.org/licenses/BSD-3-Clause + * + ******************************************************************************* + */ /* Define to prevent recursive inclusion -------------------------------------*/ #ifndef __CLOCK_H @@ -62,6 +38,8 @@ uint32_t getCurrentMillis(void); uint32_t getCurrentMicros(void); void enableClock(sourceClock_t source); +void configHSECapacitorTuning(void); + #ifdef __cplusplus } #endif diff --git a/cores/arduino/stm32/lock_resource.h b/cores/arduino/stm32/lock_resource.h index 4b6a07afaf..1c201bfaca 100644 --- a/cores/arduino/stm32/lock_resource.h +++ b/cores/arduino/stm32/lock_resource.h @@ -1,27 +1,22 @@ -/** - ****************************************************************************** - * @file lock_resource.h - * @author MCD Application Team - * @brief Header for lock_resource.c - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2019 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under BSD 3-Clause license, - * the "License"; You may not use this file except in compliance with the - * License. You may obtain a copy of the License at: - * opensource.org/licenses/BSD-3-Clause - * - ****************************************************************************** - */ +/* + ******************************************************************************* + * Copyright (c) 2019-2021, STMicroelectronics + * All rights reserved. + * + * This software component is licensed by ST under BSD 3-Clause license, + * the "License"; You may not use this file except in compliance with the + * License. You may obtain a copy of the License at: + * opensource.org/licenses/BSD-3-Clause + * + ******************************************************************************* + */ /* Define to prevent recursive inclusion -------------------------------------*/ #ifndef _LOCK_RESOURCE_H #define _LOCK_RESOURCE_H /* Includes ------------------------------------------------------------------*/ #include "stm32_def.h" +#include "stm32yyxx_ll_hsem.h" #ifdef __cplusplus extern "C" { @@ -35,15 +30,163 @@ typedef enum { } LockResource_Status_t; /* Exported constants --------------------------------------------------------*/ -#define LOCK_RESOURCE_TIMEOUT 100U /* timeout in ms */ +#if defined(STM32WBxx) +/* + * HW semaphore Complement ID list defined in hw_conf.h from STM32WB. + * They could be used also for H7 dualcore targets. + */ +/* + * Index of the semaphore used by CPU2 to prevent the CPU1 to either write or + * erase data in flash. The CPU1 shall not either write or erase in flash when + * this semaphore is taken by the CPU2. When the CPU1 needs to either write or + * erase in flash, it shall first get the semaphore and release it just + * after writing a raw (64bits data) or erasing one sector. + * On v1.4.0 and older CPU2 wireless firmware, this semaphore is unused and + * CPU2 is using PES bit. By default, CPU2 is using the PES bit to protect its + * timing. The CPU1 may request the CPU2 to use the semaphore instead of the + * PES bit by sending the system command SHCI_C2_SetFlashActivityControl() + */ +#define CFG_HW_BLOCK_FLASH_REQ_BY_CPU2_SEMID 7U -/* Exported macro ------------------------------------------------------------*/ -#define PERIPH_LOCK(__Periph__) Periph_Lock(__Periph__, LOCK_RESOURCE_TIMEOUT) -#define PERIPH_UNLOCK(__Periph__) Periph_Unlock(__Periph__) +/* + * Index of the semaphore used by CPU1 to prevent the CPU2 to either write or + * erase data in flash. In order to protect its timing, the CPU1 may get this + * semaphore to prevent the CPU2 to either write or erase in flash + * (as this will stall both CPUs) + * The PES bit shall not be used as this may stall the CPU2 in some cases. + */ +#define CFG_HW_BLOCK_FLASH_REQ_BY_CPU1_SEMID 6U -/* Exported functions ------------------------------------------------------- */ -LockResource_Status_t Periph_Lock(void *Peripheral, uint32_t Timeout); -void Periph_Unlock(void *Peripheral); +/* + * Index of the semaphore used to manage the CLK48 clock configuration + * When the USB is required, this semaphore shall be taken before configuring + * the CLK48 for USB and should be released after the application switch OFF + * the clock when the USB is not used anymore. When using the RNG, it is good + * enough to use CFG_HW_RNG_SEMID to control CLK48. + * More details in AN5289 + */ +#define CFG_HW_CLK48_CONFIG_SEMID 5U +#define CFG_HW_RCC_CRRCR_CCIPR_SEMID CFG_HW_CLK48_CONFIG_SEMID + +/* Index of the semaphore used to manage the entry Stop Mode procedure */ +#define CFG_HW_ENTRY_STOP_MODE_SEMID 4U +#define CFG_HW_ENTRY_STOP_MODE_MASK_SEMID (1U << CFG_HW_ENTRY_STOP_MODE_SEMID) + +/* Index of the semaphore used to access the RCC */ +#define CFG_HW_RCC_SEMID 3U + +/* Index of the semaphore used to access the FLASH */ +#define CFG_HW_FLASH_SEMID 2U + +/* Index of the semaphore used to access the PKA */ +#define CFG_HW_PKA_SEMID 1U + +/* Index of the semaphore used to access the RNG */ +#define CFG_HW_RNG_SEMID 0U + +/* Index of the semaphore used to access GPIO */ +#define CFG_HW_GPIO_SEMID 8U + +/* Index of the semaphore used to access the EXTI */ +#define CFG_HW_EXTI_SEMID 9U + +#elif defined(STM32MP1xx) +/* + * HW semaphore from STM32MP1 + * EXTI and GPIO are inherited from STM32MP1 Linux. + * Other SEMID are not used by linux and must not be used here, + * but reserved for MPU. + */ +/* Index of the semaphore used to access GPIO */ +#define CFG_HW_GPIO_SEMID 0U + +/* Index of the semaphore used to access the EXTI */ +#define CFG_HW_EXTI_SEMID 1U +#endif /* STM32WBxx */ + +/* Fake semaphore ID definition for compilation purpose only */ +#ifndef HSEM_SEMID_MAX +#define HSEM_SEMID_MAX 0U +#endif +#ifndef CFG_HW_BLOCK_FLASH_REQ_BY_CPU2_SEMID +#define CFG_HW_BLOCK_FLASH_REQ_BY_CPU2_SEMID (HSEM_SEMID_MAX +1) +#endif +#ifndef CFG_HW_BLOCK_FLASH_REQ_BY_CPU1_SEMID +#define CFG_HW_BLOCK_FLASH_REQ_BY_CPU1_SEMID (HSEM_SEMID_MAX +1) +#endif +#ifndef CFG_HW_CLK48_CONFIG_SEMID +#define CFG_HW_CLK48_CONFIG_SEMID (HSEM_SEMID_MAX +1) +#endif +#ifndef CFG_HW_RCC_CRRCR_CCIPR_SEMID +#define CFG_HW_RCC_CRRCR_CCIPR_SEMID (HSEM_SEMID_MAX +1) +#endif +#ifndef CFG_HW_ENTRY_STOP_MODE_SEMID +#define CFG_HW_ENTRY_STOP_MODE_SEMID (HSEM_SEMID_MAX +1) +#endif +#ifndef CFG_HW_RCC_SEMID +#define CFG_HW_RCC_SEMID (HSEM_SEMID_MAX +1) +#endif +#ifndef CFG_HW_FLASH_SEMID +#define CFG_HW_FLASH_SEMID (HSEM_SEMID_MAX +1) +#endif +#ifndef CFG_HW_PKA_SEMID +#define CFG_HW_PKA_SEMID (HSEM_SEMID_MAX +1) +#endif +#ifndef CFG_HW_RNG_SEMID +#define CFG_HW_RNG_SEMID (HSEM_SEMID_MAX +1) +#endif +#ifndef CFG_HW_GPIO_SEMID +#define CFG_HW_GPIO_SEMID (HSEM_SEMID_MAX +1) +#endif +#ifndef CFG_HW_EXTI_SEMID +#define CFG_HW_EXTI_SEMID (HSEM_SEMID_MAX +1) +#endif + +/* Hardware Semaphore wait forever value */ +#define HSEM_LOCK_WAIT_FOREVER 0xFFFFFFFFU +/* Hardware Semaphore default retry value */ +#ifndef HSEM_LOCK_DEFAULT_RETRY +#define HSEM_LOCK_DEFAULT_RETRY 0xFFFFU +#endif + +/* + * @brief hsem_lock function is used for register protection of shared Peripheral + * and shall be called before accessing registers of this shared Peripheral + * If Semaphore id is already taken, the function will busy loop waiting for it to + * be released, but give up after @retry downcounter have elapsed + * @param semID: Semaphore id used to identify which peripheral to protect + * @param retry: number of retry + * @retval None + */ +static inline void hsem_lock(uint32_t semID, uint32_t retry) +{ +#if defined(STM32MP1xx) || defined(STM32WBxx) + if ((semID) <= HSEM_SEMID_MAX) { + while (LL_HSEM_1StepLock(HSEM, semID)) { + if (retry != HSEM_LOCK_WAIT_FOREVER) { + retry--; + if (retry == 0) { + Error_Handler(); + } + } + } + } +#endif /* STM32MP1xx || STM32WBxx */ +} + +/* + * @brief hsem_unlock released a previously-acquired semaphore + * @param semID Semaphore id used to identify which peripheral to release + * @retval None + */ +static inline void hsem_unlock(uint32_t semID) +{ +#if defined(STM32MP1xx) || defined(STM32WBxx) + if ((semID) <= HSEM_SEMID_MAX) { + LL_HSEM_ReleaseLock(HSEM, semID, 0); + } +#endif /* STM32MP1xx || STM32WBxx */ +} #ifdef __cplusplus } // extern "C" diff --git a/cores/arduino/stm32/otp.h b/cores/arduino/stm32/otp.h new file mode 100644 index 0000000000..0826098a84 --- /dev/null +++ b/cores/arduino/stm32/otp.h @@ -0,0 +1,61 @@ +/* + ******************************************************************************* + * Copyright (c) 2021, STMicroelectronics + * All rights reserved. + * + * This software component is licensed by ST under BSD 3-Clause license, + * the "License"; You may not use this file except in compliance with the + * License. You may obtain a copy of the License at: + * opensource.org/licenses/BSD-3-Clause + * + ******************************************************************************* + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __OTP_H +#define __OTP_H + +#include "stm32_def.h" + +#ifdef OTP_AREA_BASE +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef STM32WBxx +/* + * See AN5042: Precise HSE frequency and startup time tuning + * for STM32 wireless MCUs. Each OTP structure type is indicated + * by its index (one byte).The index used for the BT structure is 0. + */ +typedef struct __packed { + uint8_t bd_address[6]; + uint8_t hse_tuning; + uint8_t id; +} OTP_BT_t; +#endif /* STM32WBxx */ + +/* Exported functions --------------------------------------------------------*/ + +/** + * @brief This API return the address (64 bits aligned) of the ID parameter in the OTP + * It returns the first ID declaration found from the higher address down to the base address + * The user shall fill the OTP from the base address to the top of the OTP so that the more recent + * declaration is returned by the API + * The OTP manager handles only 64 bits parameter + * | Id | Parameter | + * | 8bits | 58bits | + * | MSB | LSB | + * + * @param id: ID of the parameter to read from OTP + * @retval Address of the ID in the OTP - returns 0 when no ID found + */ +uint8_t *OTP_Read(uint8_t id); + +#ifdef __cplusplus +} +#endif +#endif /* OTP_AREA_BASE */ +#endif /*__OTP_H */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/cores/arduino/stm32/pinconfig.h b/cores/arduino/stm32/pinconfig.h index 743d11bf90..1cb0ca781a 100644 --- a/cores/arduino/stm32/pinconfig.h +++ b/cores/arduino/stm32/pinconfig.h @@ -1,43 +1,24 @@ /* ******************************************************************************* - * Copyright (c) 2018, STMicroelectronics + * Copyright (c) 2018-2021, STMicroelectronics * All rights reserved. * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: + * This software component is licensed by ST under BSD 3-Clause license, + * the "License"; You may not use this file except in compliance with the + * License. You may obtain a copy of the License at: + * opensource.org/licenses/BSD-3-Clause * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. Neither the name of STMicroelectronics nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************* + * * Based on mbed-os/target/TARGET_STM/TARGET_STMYY/pin_device.h */ #ifndef _PINCONFIG_H #define _PINCONFIG_H #include "PinAF_STM32F1.h" +#include "lock_resource.h" #include "stm32yyxx_ll_gpio.h" -#if defined(STM32MP1xx) - #include "lock_resource.h" -#endif - static inline void pin_DisconnectDebug(PinName pin) { #ifdef STM32F1xx @@ -49,13 +30,10 @@ static inline void pin_DisconnectDebug(PinName pin) static inline void pin_PullConfig(GPIO_TypeDef *gpio, uint32_t ll_pin, uint32_t pull_config) { -#if defined(STM32MP1xx) - PERIPH_LOCK(gpio); -#endif #ifdef STM32F1xx uint32_t function = LL_GPIO_GetPinMode(gpio, ll_pin); #endif - + hsem_lock(CFG_HW_GPIO_SEMID, HSEM_LOCK_DEFAULT_RETRY); switch (pull_config) { case GPIO_PULLUP: #ifdef STM32F1xx @@ -84,16 +62,11 @@ static inline void pin_PullConfig(GPIO_TypeDef *gpio, uint32_t ll_pin, uint32_t #endif break; } -#if defined(STM32MP1xx) - PERIPH_UNLOCK(gpio); -#endif + hsem_unlock(CFG_HW_GPIO_SEMID); } static inline void pin_SetAFPin(GPIO_TypeDef *gpio, PinName pin, uint32_t afnum) { -#if defined(STM32MP1xx) - PERIPH_LOCK(gpio); -#endif #ifdef STM32F1xx UNUSED(gpio); UNUSED(pin); @@ -101,14 +74,13 @@ static inline void pin_SetAFPin(GPIO_TypeDef *gpio, PinName pin, uint32_t afnum) #else uint32_t ll_pin = STM_LL_GPIO_PIN(pin); + hsem_lock(CFG_HW_GPIO_SEMID, HSEM_LOCK_DEFAULT_RETRY); if (STM_PIN(pin) > 7) { LL_GPIO_SetAFPin_8_15(gpio, ll_pin, afnum); } else { LL_GPIO_SetAFPin_0_7(gpio, ll_pin, afnum); } -#endif -#if defined(STM32MP1xx) - PERIPH_UNLOCK(gpio); + hsem_unlock(CFG_HW_GPIO_SEMID); #endif } diff --git a/cores/arduino/stm32/stm32yyxx_hal_conf.h b/cores/arduino/stm32/stm32yyxx_hal_conf.h index 7d3cd4897c..0d89681cd0 100644 --- a/cores/arduino/stm32/stm32yyxx_hal_conf.h +++ b/cores/arduino/stm32/stm32yyxx_hal_conf.h @@ -11,6 +11,7 @@ #define HAL_GPIO_MODULE_ENABLED #define HAL_PWR_MODULE_ENABLED #define HAL_RCC_MODULE_ENABLED +#define HAL_HSEM_MODULE_ENABLED /* * Optional HAL modules, can be enabled/disabled using @@ -133,7 +134,6 @@ HAL_HASH_MODULE_ENABLED HAL_HCD_MODULE_ENABLED HAL_HRTIM_MODULE_ENABLED - HAL_HSEM_MODULE_ENABLED HAL_ICACHE_MODULE_ENABLED HAL_IPCC_MODULE_ENABLED HAL_IRDA_MODULE_ENABLED diff --git a/libraries/SrcWrapper/src/stm32/analog.cpp b/libraries/SrcWrapper/src/stm32/analog.cpp index 852b99d8de..7c82481530 100644 --- a/libraries/SrcWrapper/src/stm32/analog.cpp +++ b/libraries/SrcWrapper/src/stm32/analog.cpp @@ -1,44 +1,20 @@ -/** - ****************************************************************************** - * @file analog.c - * @author WI6LABS - * @version V1.0.0 - * @date 01-August-2016 - * @brief provide analog services (ADC + PWM) - * - ****************************************************************************** - * @attention - * - *

© COPYRIGHT(c) 2016 STMicroelectronics

- * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. Neither the name of STMicroelectronics nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - ****************************************************************************** - */ -#include "stm32_def.h" +/* + ******************************************************************************* + * Copyright (c) 2016-2021, STMicroelectronics + * All rights reserved. + * + * This software component is licensed by ST under BSD 3-Clause license, + * the "License"; You may not use this file except in compliance with the + * License. You may obtain a copy of the License at: + * opensource.org/licenses/BSD-3-Clause + * + ******************************************************************************* + */ #include "analog.h" -#include "PinAF_STM32F1.h" +#include "lock_resource.h" #include "stm32yyxx_ll_adc.h" +#include "PinAF_STM32F1.h" + #ifdef __cplusplus extern "C" { @@ -628,8 +604,10 @@ void HAL_ADC_MspInit(ADC_HandleTypeDef *hadc) SystemClock_Config (variant.cpp) */ #if defined(__HAL_RCC_ADC_CONFIG) && !defined(STM32F1xx) && \ !defined(STM32H7xx) && !defined(STM32MP1xx) + hsem_lock(CFG_HW_RCC_CRRCR_CCIPR_SEMID, HSEM_LOCK_DEFAULT_RETRY); /* ADC Periph interface clock configuration */ __HAL_RCC_ADC_CONFIG(RCC_ADCCLKSOURCE_SYSCLK); + hsem_unlock(CFG_HW_RCC_CRRCR_CCIPR_SEMID); #endif /* Configure ADC GPIO pin */ diff --git a/libraries/SrcWrapper/src/stm32/clock.c b/libraries/SrcWrapper/src/stm32/clock.c index bea7adcdbc..82b497eb7b 100644 --- a/libraries/SrcWrapper/src/stm32/clock.c +++ b/libraries/SrcWrapper/src/stm32/clock.c @@ -1,43 +1,21 @@ -/** - ****************************************************************************** - * @file clock.c - * @author WI6LABS - * @version V1.0.0 - * @date 01-August-2016 - * @brief provide clock services for time purpose - * - ****************************************************************************** - * @attention - * - *

© COPYRIGHT(c) 2016 STMicroelectronics

- * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. Neither the name of STMicroelectronics nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - ****************************************************************************** - */ +/* + ******************************************************************************* + * Copyright (c) 2016-2021, STMicroelectronics + * All rights reserved. + * + * This software component is licensed by ST under BSD 3-Clause license, + * the "License"; You may not use this file except in compliance with the + * License. You may obtain a copy of the License at: + * opensource.org/licenses/BSD-3-Clause + * + ******************************************************************************* + */ #include "backup.h" #include "clock.h" +#include "lock_resource.h" +#include "otp.h" #include "stm32yyxx_ll_cortex.h" +#include "stm32yyxx_ll_rcc.h" #ifdef __cplusplus extern "C" { @@ -154,12 +132,26 @@ void enableClock(sourceClock_t source) break; } if (RCC_OscInitStruct.OscillatorType != RCC_OSCILLATORTYPE_NONE) { + hsem_lock(CFG_HW_RCC_SEMID, HSEM_LOCK_DEFAULT_RETRY); if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); } + hsem_unlock(CFG_HW_RCC_SEMID); } } +void configHSECapacitorTuning(void) +{ +#if defined(OTP_AREA_BASE) && defined(STM32WBxx) + OTP_BT_t *p_otp; + /* Read HSE_Tuning from OTP with index 0 */ + p_otp = (OTP_BT_t *) OTP_Read(0); + if ((p_otp) && (!LL_RCC_HSE_IsReady())) { + LL_RCC_HSE_SetCapacitorTuning(p_otp->hse_tuning); + } +#endif +} + #ifdef __cplusplus } #endif diff --git a/libraries/SrcWrapper/src/stm32/hw_config.c b/libraries/SrcWrapper/src/stm32/hw_config.c index 3434d6da96..4e070edc3f 100644 --- a/libraries/SrcWrapper/src/stm32/hw_config.c +++ b/libraries/SrcWrapper/src/stm32/hw_config.c @@ -1,44 +1,19 @@ -/** - ****************************************************************************** - * @file hw_config.c - * @author WI6LABS - * @version V1.0.0 - * @date 01-August-2016 - * @brief provide some hw interface for the Arduino interface - * - ****************************************************************************** - * @attention - * - *

© COPYRIGHT(c) 2016 STMicroelectronics

- * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. Neither the name of STMicroelectronics nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - ****************************************************************************** - */ -#include "stm32_def.h" +/* + ******************************************************************************* + * Copyright (c) 2016-2021, STMicroelectronics + * All rights reserved. + * + * This software component is licensed by ST under BSD 3-Clause license, + * the "License"; You may not use this file except in compliance with the + * License. You may obtain a copy of the License at: + * opensource.org/licenses/BSD-3-Clause + * + ******************************************************************************* + */ +#include "dwt.h" #include "hw_config.h" +#include "clock.h" #include "usbd_if.h" -#include "dwt.h" #ifdef __cplusplus extern "C" { @@ -59,17 +34,18 @@ void hw_config_init(void) /* Initialize the HAL */ HAL_Init(); +#ifdef HSEM_BASE + __HAL_RCC_HSEM_CLK_ENABLE(); +#endif + + configHSECapacitorTuning(); + /* Configure the system clock */ SystemClock_Config(); #if defined (USBCON) && defined(USBD_USE_CDC) USBD_CDC_init(); #endif - -#if defined (STM32MP1xx) - __HAL_RCC_HSEM_CLK_ENABLE(); -#endif - } #ifdef __cplusplus } diff --git a/libraries/SrcWrapper/src/stm32/interrupt.cpp b/libraries/SrcWrapper/src/stm32/interrupt.cpp index 5d7cfb3005..277a6a3c44 100644 --- a/libraries/SrcWrapper/src/stm32/interrupt.cpp +++ b/libraries/SrcWrapper/src/stm32/interrupt.cpp @@ -36,9 +36,7 @@ ****************************************************************************** */ #include "interrupt.h" -#if defined(STM32MP1xx) - #include "lock_resource.h" -#endif +#include "lock_resource.h" #if !defined(HAL_EXTI_MODULE_DISABLED) /* Private Types */ @@ -179,15 +177,11 @@ void stm32_interrupt_enable(GPIO_TypeDef *port, uint16_t pin, callback_function_ GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; -#if defined(STM32MP1xx) - PERIPH_LOCK(port); -#endif + hsem_lock(CFG_HW_GPIO_SEMID, HSEM_LOCK_DEFAULT_RETRY); HAL_GPIO_Init(port, &GPIO_InitStruct); -#if defined(STM32MP1xx) - PERIPH_UNLOCK(port); -#endif + hsem_unlock(CFG_HW_GPIO_SEMID); gpio_irq_conf[id].callback = callback; diff --git a/libraries/SrcWrapper/src/stm32/lock_resource.c b/libraries/SrcWrapper/src/stm32/lock_resource.c deleted file mode 100644 index 2f8f85d5da..0000000000 --- a/libraries/SrcWrapper/src/stm32/lock_resource.c +++ /dev/null @@ -1,94 +0,0 @@ -/** - ****************************************************************************** - * @file lock_resource.c - * @author MCD Application Team - * @brief This sample code provides hardware semaphore using HSEM for - * synchronization and mutual exclusion between heterogeneous processors - * and those not operating under a single, shared operating system. - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2019 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under BSD 3-Clause license, - * the "License"; You may not use this file except in compliance with the - * License. You may obtain a copy of the License at: - * opensource.org/licenses/BSD-3-Clause - * - ****************************************************************************** - */ - -/* Includes ------------------------------------------------------------------*/ -#include "lock_resource.h" - -#if defined(HAL_HSEM_MODULE_ENABLED) \ - && defined(STM32MP1xx) - -/* Private macro -------------------------------------------------------------*/ - -/* - * This macro provides a semaphore id for a dedicated peripheral. - * This macro shall be configured by user according its needs and aligned with remote processors - * (e.g. same semaphore id shall be used between processors for a dedicated peripheral) - */ -#define GET_HSEM_SEM_INDEX(__Peripheral__) (uint8_t)(((GPIO_TypeDef *)(__Peripheral__) == (GPIOA))? 0U :\ - ((GPIO_TypeDef *)(__Peripheral__) == (GPIOB))? 0U :\ - ((GPIO_TypeDef *)(__Peripheral__) == (GPIOC))? 0U :\ - ((GPIO_TypeDef *)(__Peripheral__) == (GPIOD))? 0U :\ - ((GPIO_TypeDef *)(__Peripheral__) == (GPIOE))? 0U :\ - ((GPIO_TypeDef *)(__Peripheral__) == (GPIOF))? 0U :\ - ((GPIO_TypeDef *)(__Peripheral__) == (GPIOG))? 0U :\ - ((GPIO_TypeDef *)(__Peripheral__) == (GPIOH))? 0U :\ - ((GPIO_TypeDef *)(__Peripheral__) == (GPIOI))? 0U :\ - ((GPIO_TypeDef *)(__Peripheral__) == (GPIOJ))? 0U :\ - ((GPIO_TypeDef *)(__Peripheral__) == (GPIOK))? 0U :\ - ((GPIO_TypeDef *)(__Peripheral__) == (GPIOZ))? 0U :\ - ((EXTI_TypeDef *)(__Peripheral__) == (EXTI))? 1U : HSEM_SEMID_MAX + 1U) - -/* Private user code ---------------------------------------------------------*/ - - -/** - * @brief Periph_Lock function is used for register protection of shared @Peripheral - * and shall be called before accessing registers of this shared @Peripheral - * If Semaphore id is already taken, the function will busy loop waiting for it to - * be released, but give up after @Timeout msecs have elapsed. - * @param Peripheral: used to identify which peripheral to protect. - * Semaphore id deduced from this peripheral. - * Timeout: timeout value in msecs - * @retval Return Status - */ -LockResource_Status_t Periph_Lock(void *Peripheral, uint32_t Timeout) -{ - uint32_t tickstart = 0U; - LockResource_Status_t ret = LOCK_RESOURCE_STATUS_OK; - - /* Init tickstart for timeout management*/ - tickstart = HAL_GetTick(); - - /* Try to Take HSEM assigned to the Peripheral */ - while (HAL_HSEM_FastTake(GET_HSEM_SEM_INDEX(Peripheral)) != HAL_OK) { - - if ((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout)) { - ret = LOCK_RESOURCE_STATUS_TIMEOUT; - Error_Handler(); - } - } - - return ret; -} - -/** - * @brief The Periph_Unlock released a previously-acquired semaphore which we want to unlock - * @param Peripheral: used to identify which peripheral and the related semaphore - * @retval None - */ -void Periph_Unlock(void *Peripheral) -{ - /* Release HSEM */ - HAL_HSEM_Release(GET_HSEM_SEM_INDEX(Peripheral), 0); - -} - -#endif // defined(HAL_HSEM_MODULE_ENABLED) diff --git a/libraries/SrcWrapper/src/stm32/otp.c b/libraries/SrcWrapper/src/stm32/otp.c new file mode 100644 index 0000000000..ec223f833f --- /dev/null +++ b/libraries/SrcWrapper/src/stm32/otp.c @@ -0,0 +1,36 @@ +/* + ******************************************************************************* + * Copyright (c) 2021, STMicroelectronics + * All rights reserved. + * + * This software component is licensed by ST under BSD 3-Clause license, + * the "License"; You may not use this file except in compliance with the + * License. You may obtain a copy of the License at: + * opensource.org/licenses/BSD-3-Clause + * + ******************************************************************************* + */ + +#include "otp.h" + +#ifdef OTP_AREA_BASE + +uint8_t *OTP_Read(uint8_t id) +{ + uint8_t *p_id; + + p_id = (uint8_t *)(OTP_AREA_END_ADDR - 7) ; + + while (((*(p_id + 7)) != id) && (p_id != (uint8_t *)OTP_AREA_BASE)) { + p_id -= 8 ; + } + + if ((*(p_id + 7)) != id) { + p_id = 0 ; + } + + return p_id ; +} + +#endif /* OTP_AREA_BASE */ +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/libraries/SrcWrapper/src/stm32/pinmap.c b/libraries/SrcWrapper/src/stm32/pinmap.c index 31c81f2850..5dac46f6ff 100644 --- a/libraries/SrcWrapper/src/stm32/pinmap.c +++ b/libraries/SrcWrapper/src/stm32/pinmap.c @@ -103,9 +103,9 @@ void pin_function(PinName pin, int function) __HAL_RCC_PWR_CLK_ENABLE(); HAL_PWREx_EnableVddIO2(); #endif /* STM32L5xx */ -#if defined(STM32MP1xx) - PERIPH_LOCK(gpio); -#endif + + hsem_lock(CFG_HW_GPIO_SEMID, HSEM_LOCK_DEFAULT_RETRY); + /* Set default speed to high. * For most families there are dedicated registers so it is * not so important, register can be set at any time. @@ -169,9 +169,7 @@ void pin_function(PinName pin, int function) pin_DisconnectDebug(pin); -#if defined(STM32MP1xx) - PERIPH_UNLOCK(gpio); -#endif + hsem_unlock(CFG_HW_GPIO_SEMID); } void pinmap_pinout(PinName pin, const PinMap *map) diff --git a/libraries/SrcWrapper/src/stm32/uart.c b/libraries/SrcWrapper/src/stm32/uart.c index 2c61c3158b..32668b4b11 100644 --- a/libraries/SrcWrapper/src/stm32/uart.c +++ b/libraries/SrcWrapper/src/stm32/uart.c @@ -1,39 +1,17 @@ -/** - ****************************************************************************** - * @file uart.c - * @author WI6LABS, fpistm - * @brief provide the UART interface - * - ****************************************************************************** - * @attention - * - *

© COPYRIGHT(c) 2016 STMicroelectronics

- * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. Neither the name of STMicroelectronics nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - ****************************************************************************** - */ +/* + ******************************************************************************* + * Copyright (c) 2016-2021, STMicroelectronics + * All rights reserved. + * + * This software component is licensed by ST under BSD 3-Clause license, + * the "License"; You may not use this file except in compliance with the + * License. You may obtain a copy of the License at: + * opensource.org/licenses/BSD-3-Clause + * + ******************************************************************************* + */ #include "core_debug.h" +#include "lock_resource.h" #include "uart.h" #include "Arduino.h" #include "PinAF_STM32F1.h" @@ -528,6 +506,7 @@ void uart_config_lowpower(serial_t *obj) /* Ensure HSI clock is enable */ enableClock(HSI_CLOCK); + hsem_lock(CFG_HW_RCC_CRRCR_CCIPR_SEMID, HSEM_LOCK_DEFAULT_RETRY); /* Configure HSI as source clock for low power wakeup clock */ switch (obj->index) { #if defined(USART1_BASE) @@ -573,6 +552,7 @@ void uart_config_lowpower(serial_t *obj) break; #endif } + hsem_unlock(CFG_HW_RCC_CRRCR_CCIPR_SEMID); } #endif diff --git a/system/STM32MP1xx/stm32mp1xx_hal_conf_default.h b/system/STM32MP1xx/stm32mp1xx_hal_conf_default.h index 4ae9a394f6..34bd1b88b2 100644 --- a/system/STM32MP1xx/stm32mp1xx_hal_conf_default.h +++ b/system/STM32MP1xx/stm32mp1xx_hal_conf_default.h @@ -40,7 +40,6 @@ #define HAL_RTC_MODULE_DISABLED /* RTC MODULE on Cortex-M side is not supported. Linux on Cortex-A will handle this. */ #define HAL_ETH_MODULE_DISABLED /* ETH module is also not intended to be used */ -#define HAL_HSEM_MODULE_ENABLED #define HAL_MDMA_MODULE_ENABLED /* Some other modules (e.g. USART) require this */ #if defined(ARDUINO_STM32MP157A_DK1) || defined(ARDUINO_STM32MP157C_DK2) diff --git a/variants/MKR_SHARKY/variant.h b/variants/MKR_SHARKY/variant.h index e6fc21a0f3..fb4b4757af 100644 --- a/variants/MKR_SHARKY/variant.h +++ b/variants/MKR_SHARKY/variant.h @@ -95,7 +95,6 @@ extern "C" { #define PIN_SERIAL_TX PB6 /* Extra HAL configuration */ -#define HAL_HSEM_MODULE_ENABLED #define PREFETCH_ENABLE 1U // Only 512k provided for cpu1, so defined the FLASH_PAGE_NUMBER diff --git a/variants/PNUCLEO_WB55RG/otp.h b/variants/PNUCLEO_WB55RG/otp.h deleted file mode 100644 index 0574fa9e81..0000000000 --- a/variants/PNUCLEO_WB55RG/otp.h +++ /dev/null @@ -1,46 +0,0 @@ -/** - ****************************************************************************** - * @file otp.h - * @author MCD Application Team - * @brief OTP manager interface - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2019 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under BSD 3-Clause license, - * the "License"; You may not use this file except in compliance with the - * License. You may obtain a copy of the License at: - * opensource.org/licenses/BSD-3-Clause - * - ****************************************************************************** - */ - - -/* Define to prevent recursive inclusion -------------------------------------*/ -#ifndef __OTP_H -#define __OTP_H - -#ifdef __cplusplus -extern "C" { -#endif - -typedef struct __packed { - uint8_t bd_address[6]; - uint8_t hse_tuning; - uint8_t id; -} OTP_ID0_t; - -/* Exported constants --------------------------------------------------------*/ -#define CFG_OTP_BASE_ADDRESS OTP_AREA_BASE - -#define CFG_OTP_END_ADDRESS OTP_AREA_END_ADDR - -#ifdef __cplusplus -} -#endif - -#endif /*__OTP_H */ - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/variants/PNUCLEO_WB55RG/variant.cpp b/variants/PNUCLEO_WB55RG/variant.cpp index ef7a2de156..e07ddd74aa 100644 --- a/variants/PNUCLEO_WB55RG/variant.cpp +++ b/variants/PNUCLEO_WB55RG/variant.cpp @@ -17,7 +17,7 @@ */ #include "pins_arduino.h" -#include "otp.h" +#include "lock_resource.h" #ifdef __cplusplus extern "C" { @@ -89,36 +89,6 @@ const PinName digitalPin[] = { extern "C" { #endif -static uint8_t *OTP_Read(uint8_t id) -{ - uint8_t *p_id; - - p_id = (uint8_t *)(CFG_OTP_END_ADDRESS - 7) ; - - while (((*(p_id + 7)) != id) && (p_id != (uint8_t *)CFG_OTP_BASE_ADDRESS)) { - p_id -= 8 ; - } - - if ((*(p_id + 7)) != id) { - p_id = 0 ; - } - - return p_id ; -} - -static void Config_HSE(void) -{ - OTP_ID0_t *p_otp; - - /** - * Read HSE_Tuning from OTP - */ - p_otp = (OTP_ID0_t *) OTP_Read(0); - if (p_otp) { - LL_RCC_HSE_SetCapacitorTuning(p_otp->hse_tuning); - } -} - /** * @brief System Clock Configuration * @param None @@ -130,11 +100,15 @@ WEAK void SystemClock_Config(void) RCC_ClkInitTypeDef RCC_ClkInitStruct = {}; RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {}; - Config_HSE(); + /* This prevents concurrent access to RCC registers by CPU2 (M0+) */ + hsem_lock(CFG_HW_RCC_SEMID, HSEM_LOCK_DEFAULT_RETRY); __HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_LOW); __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); + /* This prevents the CPU2 (M0+) to disable the HSI48 oscillator */ + hsem_lock(CFG_HW_CLK48_CONFIG_SEMID, HSEM_LOCK_DEFAULT_RETRY); + /* Initializes the CPU, AHB and APB busses clocks */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_HSI48 | RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_LSE; @@ -182,6 +156,7 @@ WEAK void SystemClock_Config(void) /* Select HSI as system clock source after Wake Up from Stop mode */ LL_RCC_SetClkAfterWakeFromStop(LL_RCC_STOP_WAKEUPCLOCK_HSI); + hsem_unlock(CFG_HW_RCC_SEMID); } #ifdef __cplusplus