From 64eee31918ca2270d236ee0bfc519d17094651d9 Mon Sep 17 00:00:00 2001 From: Francois Ramu Date: Tue, 9 May 2023 09:26:16 +0200 Subject: [PATCH 1/7] stm32 BSP LoRaWan with RTC instance of the stm32WL The Alarm B of the RTC is used for LoRaWan purpose. Use the LSE as clock source for the RTC instance. set the free running mix mode. Signed-off-by: Francois Ramu --- src/BSP/timer_if.c | 85 ++++++++++++++++------------------------------ src/BSP/timer_if.h | 8 +++++ 2 files changed, 37 insertions(+), 56 deletions(-) diff --git a/src/BSP/timer_if.c b/src/BSP/timer_if.c index e3497ea..9b32f3b 100644 --- a/src/BSP/timer_if.c +++ b/src/BSP/timer_if.c @@ -3,7 +3,7 @@ ****************************************************************************** * @file timer_if.c * @author MCD Application Team - * @brief Configure RTC Alarm, Tick and Calendar manager + * @brief Configure RTC Alarm (B), Tick and Calendar manager ****************************************************************************** * @attention * @@ -27,6 +27,7 @@ // #include "stm32_lpm.h" // #include "utilities_def.h" #include "stm32wlxx_ll_rtc.h" +#include "rtc.h" /* USER CODE BEGIN Includes */ @@ -36,7 +37,7 @@ /** * @brief RTC handle */ -extern RTC_HandleTypeDef hrtc; +extern RTC_HandleTypeDef RtcHandle; /** * @brief Timer driver callbacks handler @@ -104,17 +105,6 @@ const UTIL_SYSTIM_Driver_s UTIL_SYSTIMDriver = /* #define RTIF_DEBUG */ -/** - * @brief Map UTIL_TIMER_IRQ can be overridden in utilities_conf.h to Map on Task rather then Isr - */ -#ifndef UTIL_TIMER_IRQ_MAP_INIT -#define UTIL_TIMER_IRQ_MAP_INIT() -#endif /* UTIL_TIMER_IRQ_MAP_INIT */ - -#ifndef UTIL_TIMER_IRQ_MAP_PROCESS -#define UTIL_TIMER_IRQ_MAP_PROCESS() UTIL_TIMER_IRQ_Handler() -#endif /* UTIL_TIMER_IRQ_MAP_PROCESS */ - /* USER CODE BEGIN PD */ /* USER CODE END PD */ @@ -177,6 +167,14 @@ static uint32_t TIMER_IF_BkUp_Read_MSBticks(void); /* USER CODE BEGIN PFP */ +/* Function to attach to the RTC IRQ as a callback */ +void UTIL_TIMER_IRQ_MAP_PROCESS(void *data) +{ + UNUSED(data); + + UTIL_TIMER_IRQ_Handler(); +} + /* USER CODE END PFP */ /* Exported functions ---------------------------------------------------------*/ @@ -188,18 +186,12 @@ UTIL_TIMER_Status_t TIMER_IF_Init(void) /* USER CODE END TIMER_IF_Init */ if (RTC_Initialized == false) { - hrtc.IsEnabled.RtcFeatures = UINT32_MAX; - /*Init RTC*/ - MX_RTC_Init(); - /*Stop Timer */ + /* RTC is already Initialized by the LoRaWan::begin */ TIMER_IF_StopTimer(); - /** DeActivate the Alarm A enabled by STM32CubeMX during MX_RTC_Init() */ - HAL_RTC_DeactivateAlarm(&hrtc, RTC_ALARM_A); + /*overload RTC feature enable*/ - hrtc.IsEnabled.RtcFeatures = UINT32_MAX; + RtcHandle.IsEnabled.RtcFeatures = UINT32_MAX; - /*Enable Direct Read of the calendar registers (not through Shadow) */ - HAL_RTCEx_EnableBypassShadow(&hrtc); /*Initialize MSB ticks*/ TIMER_IF_BkUp_Write_MSBticks(0); @@ -223,22 +215,15 @@ UTIL_TIMER_Status_t TIMER_IF_StartTimer(uint32_t timeout) /* USER CODE BEGIN TIMER_IF_StartTimer */ /* USER CODE END TIMER_IF_StartTimer */ - RTC_AlarmTypeDef sAlarm = {0}; /*Stop timer if one is already started*/ TIMER_IF_StopTimer(); timeout += RtcTimerContext; TIMER_IF_DBG_PRINTF("Start timer: time=%d, alarm=%d\n\r", GetTimerTicks(), timeout); - /* starts timer*/ - sAlarm.BinaryAutoClr = RTC_ALARMSUBSECONDBIN_AUTOCLR_NO; - sAlarm.AlarmTime.SubSeconds = UINT32_MAX - timeout; - sAlarm.AlarmMask = RTC_ALARMMASK_NONE; - sAlarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDBINMASK_NONE; - sAlarm.Alarm = RTC_ALARM_A; - if (HAL_RTC_SetAlarm_IT(&hrtc, &sAlarm, RTC_FORMAT_BCD) != HAL_OK) - { - Error_Handler(); - } + + /* Program ALARM B on subsecond, mask is 32 (and fixed to RTC_ALARMMASK_NONE for calendar) */ + RTC_StartAlarm(RTC_ALARM_B, 0, 0, 0, 0, UINT32_MAX - timeout, RTC_HOURFORMAT12_PM, 32UL); + /* USER CODE BEGIN TIMER_IF_StartTimer_Last */ /* USER CODE END TIMER_IF_StartTimer_Last */ @@ -251,12 +236,12 @@ UTIL_TIMER_Status_t TIMER_IF_StopTimer(void) /* USER CODE BEGIN TIMER_IF_StopTimer */ /* USER CODE END TIMER_IF_StopTimer */ - /* Clear RTC Alarm Flag */ - __HAL_RTC_ALARM_CLEAR_FLAG(&hrtc, RTC_FLAG_ALRAF); - /* Disable the Alarm A interrupt */ - HAL_RTC_DeactivateAlarm(&hrtc, RTC_ALARM_A); + + /* Disable the Alarm B interrupt */ + RTC_StopAlarm(RTC_ALARM_B); + /*overload RTC feature enable*/ - hrtc.IsEnabled.RtcFeatures = UINT32_MAX; + RtcHandle.IsEnabled.RtcFeatures = UINT32_MAX; /* USER CODE BEGIN TIMER_IF_StopTimer_Last */ /* USER CODE END TIMER_IF_StopTimer_Last */ @@ -374,18 +359,6 @@ void TIMER_IF_DelayMs(uint32_t delay) /* USER CODE END TIMER_IF_DelayMs_Last */ } -void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc) -{ - (void)hrtc; // unused - /* USER CODE BEGIN HAL_RTC_AlarmAEventCallback */ - - /* USER CODE END HAL_RTC_AlarmAEventCallback */ - UTIL_TIMER_IRQ_MAP_PROCESS(); - /* USER CODE BEGIN HAL_RTC_AlarmAEventCallback_Last */ - - /* USER CODE END HAL_RTC_AlarmAEventCallback_Last */ -} - void HAL_RTCEx_SSRUEventCallback(RTC_HandleTypeDef *hrtc) { (void)hrtc; // unused @@ -431,7 +404,7 @@ void TIMER_IF_BkUp_Write_Seconds(uint32_t Seconds) /* USER CODE BEGIN TIMER_IF_BkUp_Write_Seconds */ /* USER CODE END TIMER_IF_BkUp_Write_Seconds */ - HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_SECONDS, Seconds); + HAL_RTCEx_BKUPWrite(&RtcHandle, RTC_BKP_SECONDS, Seconds); /* USER CODE BEGIN TIMER_IF_BkUp_Write_Seconds_Last */ /* USER CODE END TIMER_IF_BkUp_Write_Seconds_Last */ @@ -442,7 +415,7 @@ void TIMER_IF_BkUp_Write_SubSeconds(uint32_t SubSeconds) /* USER CODE BEGIN TIMER_IF_BkUp_Write_SubSeconds */ /* USER CODE END TIMER_IF_BkUp_Write_SubSeconds */ - HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_SUBSECONDS, SubSeconds); + HAL_RTCEx_BKUPWrite(&RtcHandle, RTC_BKP_SUBSECONDS, SubSeconds); /* USER CODE BEGIN TIMER_IF_BkUp_Write_SubSeconds_Last */ /* USER CODE END TIMER_IF_BkUp_Write_SubSeconds_Last */ @@ -454,7 +427,7 @@ uint32_t TIMER_IF_BkUp_Read_Seconds(void) /* USER CODE BEGIN TIMER_IF_BkUp_Read_Seconds */ /* USER CODE END TIMER_IF_BkUp_Read_Seconds */ - ret = HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_SECONDS); + ret = HAL_RTCEx_BKUPRead(&RtcHandle, RTC_BKP_SECONDS); /* USER CODE BEGIN TIMER_IF_BkUp_Read_Seconds_Last */ /* USER CODE END TIMER_IF_BkUp_Read_Seconds_Last */ @@ -467,7 +440,7 @@ uint32_t TIMER_IF_BkUp_Read_SubSeconds(void) /* USER CODE BEGIN TIMER_IF_BkUp_Read_SubSeconds */ /* USER CODE END TIMER_IF_BkUp_Read_SubSeconds */ - ret = HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_SUBSECONDS); + ret = HAL_RTCEx_BKUPRead(&RtcHandle, RTC_BKP_SUBSECONDS); /* USER CODE BEGIN TIMER_IF_BkUp_Read_SubSeconds_Last */ /* USER CODE END TIMER_IF_BkUp_Read_SubSeconds_Last */ @@ -484,7 +457,7 @@ static void TIMER_IF_BkUp_Write_MSBticks(uint32_t MSBticks) /* USER CODE BEGIN TIMER_IF_BkUp_Write_MSBticks */ /* USER CODE END TIMER_IF_BkUp_Write_MSBticks */ - HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_MSBTICKS, MSBticks); + HAL_RTCEx_BKUPWrite(&RtcHandle, RTC_BKP_MSBTICKS, MSBticks); /* USER CODE BEGIN TIMER_IF_BkUp_Write_MSBticks_Last */ /* USER CODE END TIMER_IF_BkUp_Write_MSBticks_Last */ @@ -496,7 +469,7 @@ static uint32_t TIMER_IF_BkUp_Read_MSBticks(void) /* USER CODE END TIMER_IF_BkUp_Read_MSBticks */ uint32_t MSBticks; - MSBticks = HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_MSBTICKS); + MSBticks = HAL_RTCEx_BKUPRead(&RtcHandle, RTC_BKP_MSBTICKS); return MSBticks; /* USER CODE BEGIN TIMER_IF_BkUp_Read_MSBticks_Last */ diff --git a/src/BSP/timer_if.h b/src/BSP/timer_if.h index a60ed36..f9659f1 100644 --- a/src/BSP/timer_if.h +++ b/src/BSP/timer_if.h @@ -32,6 +32,14 @@ extern "C" { #include "../STM32CubeWL/Utilities/misc/stm32_systime.h" /* USER CODE BEGIN Includes */ +/** + * @brief Map UTIL_TIMER_IRQ can be overridden in utilities_conf.h to Map on Task rather then Isr + */ +#ifndef UTIL_TIMER_IRQ_MAP_INIT +#define UTIL_TIMER_IRQ_MAP_INIT() +#endif /* UTIL_TIMER_IRQ_MAP_INIT */ + +void UTIL_TIMER_IRQ_MAP_PROCESS(void *data); /* USER CODE END Includes */ From a3d885a56f75c1d20ed77b42782d3201c70b8fe6 Mon Sep 17 00:00:00 2001 From: Francois Ramu Date: Fri, 2 Jun 2023 11:16:11 +0200 Subject: [PATCH 2/7] stm32duino LoraWan enabling the RTC alarm B Mask is set to ALL in case of MIX mode, so that interrupt is trigged on the sub-second basis (other calendar values do not care) Signed-off-by: Francois Ramu --- src/STM32LoRaWAN.cpp | 14 ++++++++++++++ src/STM32LoRaWAN.h | 3 ++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/STM32LoRaWAN.cpp b/src/STM32LoRaWAN.cpp index ecf506a..6b6bf3a 100644 --- a/src/STM32LoRaWAN.cpp +++ b/src/STM32LoRaWAN.cpp @@ -54,6 +54,9 @@ #error "Unexpected txpower constants" #endif +/* Get the RTC object for init */ +STM32RTC& rtc = STM32RTC::getInstance(); + STM32LoRaWAN* STM32LoRaWAN::instance; bool STM32LoRaWAN::begin(_lora_band band) @@ -62,6 +65,17 @@ bool STM32LoRaWAN::begin(_lora_band band) return failure("Only one STM32LoRaWAN instance can be used"); instance = this; + /* + * Init RTC as an object : + * use the MIX mode = free running BCD calendar + binary mode for + * the sub-second counter RTC_SSR on 32 bit + */ + rtc.setClockSource(STM32RTC::LSE_CLOCK); + rtc.begin(true, STM32RTC::HOUR_24, STM32RTC::MODE_MIX); + /* Attach the callback function before enabling Interrupt */ + rtc.attachInterrupt(UTIL_TIMER_IRQ_MAP_PROCESS, STM32RTC::ALARM_B); + /* The subsecond alarm B is set during the StartTimerEvent */ + UTIL_TIMER_Init(); LoRaMacStatus_t res = LoRaMacInitialization(&LoRaMacPrimitives, &LoRaMacCallbacks, (LoRaMacRegion_t)band); diff --git a/src/STM32LoRaWAN.h b/src/STM32LoRaWAN.h index f993baa..831d003 100644 --- a/src/STM32LoRaWAN.h +++ b/src/STM32LoRaWAN.h @@ -41,8 +41,9 @@ #include "Arduino.h" #include "STM32CubeWL/LoRaWAN/Mac/LoRaMac.h" -#include "STM32CubeWL/Utilities/timer/stm32_timer.h" #include "BSP/mw_log_conf.h" +#include "BSP/timer_if.h" +#include "STM32RTC.h" /** From 37238b776950889b4685694206705ce59a848e3f Mon Sep 17 00:00:00 2001 From: Francois Ramu Date: Wed, 7 Jun 2023 14:11:13 +0200 Subject: [PATCH 3/7] STM32LoRaWan set the setRTCMode before the rtc.begin to set MIX mode The RTC.begin does not propose the RTCmode parameter. This is configured with the setRTCMode method before .begin The RTC irq (for Alarm B) is handled directly by the STM32RTC library Signed-off-by: Francois Ramu --- src/BSP/rtc.c | 11 ----------- src/STM32LoRaWAN.cpp | 3 ++- 2 files changed, 2 insertions(+), 12 deletions(-) diff --git a/src/BSP/rtc.c b/src/BSP/rtc.c index a3dccee..20b5e1d 100644 --- a/src/BSP/rtc.c +++ b/src/BSP/rtc.c @@ -140,17 +140,6 @@ void HAL_RTC_MspDeInit(RTC_HandleTypeDef* rtcHandle) } } -void RTC_Alarm_IRQHandler(void) -{ - /* USER CODE BEGIN RTC_Alarm_IRQn 0 */ - - /* USER CODE END RTC_Alarm_IRQn 0 */ - HAL_RTC_AlarmIRQHandler(&hrtc); - /* USER CODE BEGIN RTC_Alarm_IRQn 1 */ - - /* USER CODE END RTC_Alarm_IRQn 1 */ -} - /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ diff --git a/src/STM32LoRaWAN.cpp b/src/STM32LoRaWAN.cpp index 6b6bf3a..1f89778 100644 --- a/src/STM32LoRaWAN.cpp +++ b/src/STM32LoRaWAN.cpp @@ -71,7 +71,8 @@ bool STM32LoRaWAN::begin(_lora_band band) * the sub-second counter RTC_SSR on 32 bit */ rtc.setClockSource(STM32RTC::LSE_CLOCK); - rtc.begin(true, STM32RTC::HOUR_24, STM32RTC::MODE_MIX); + rtc.setRTCMode(STM32RTC::MODE_MIX); + rtc.begin(true, STM32RTC::HOUR_24); /* Attach the callback function before enabling Interrupt */ rtc.attachInterrupt(UTIL_TIMER_IRQ_MAP_PROCESS, STM32RTC::ALARM_B); /* The subsecond alarm B is set during the StartTimerEvent */ From 0368da757b2625ed47463e6021215a395703d13d Mon Sep 17 00:00:00 2001 From: Francois Ramu Date: Fri, 9 Jun 2023 10:20:52 +0200 Subject: [PATCH 4/7] simplify the BSP to keep are timer_f and include the RTC library Include the STM32RTC library to all the BSP as only few functions remain in the BSP as the RTC is instantiated by the STM32RTC library Signed-off-by: Francois Ramu --- src/BSP/rtc.c | 145 --------------------------------------------- src/BSP/rtc.h | 53 ----------------- src/BSP/timer_if.c | 52 ++++++++++++++-- src/BSP/timer_if.h | 1 + 4 files changed, 49 insertions(+), 202 deletions(-) delete mode 100644 src/BSP/rtc.c delete mode 100644 src/BSP/rtc.h diff --git a/src/BSP/rtc.c b/src/BSP/rtc.c deleted file mode 100644 index 20b5e1d..0000000 --- a/src/BSP/rtc.c +++ /dev/null @@ -1,145 +0,0 @@ -/* USER CODE BEGIN Header */ -/** - ****************************************************************************** - * @file rtc.c - * @brief This file provides code for the configuration - * of the RTC instances. - ****************************************************************************** - * @attention - * - * Copyright (c) 2022 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 - * - ****************************************************************************** - */ -/* USER CODE END Header */ -/* Includes ------------------------------------------------------------------*/ -#include "rtc.h" - -/* USER CODE BEGIN 0 */ - -/* USER CODE END 0 */ - -RTC_HandleTypeDef hrtc; - -/* RTC init function */ -void MX_RTC_Init(void) -{ - - /* USER CODE BEGIN RTC_Init 0 */ - - /* USER CODE END RTC_Init 0 */ - - RTC_AlarmTypeDef sAlarm = {0}; - - /* USER CODE BEGIN RTC_Init 1 */ - - /* USER CODE END RTC_Init 1 */ - - /** Initialize RTC Only - */ - hrtc.Instance = RTC; - hrtc.Init.AsynchPrediv = RTC_PREDIV_A; - hrtc.Init.OutPut = RTC_OUTPUT_DISABLE; - hrtc.Init.OutPutRemap = RTC_OUTPUT_REMAP_NONE; - hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH; - hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN; - hrtc.Init.OutPutPullUp = RTC_OUTPUT_PULLUP_NONE; - hrtc.Init.BinMode = RTC_BINARY_ONLY; - if (HAL_RTC_Init(&hrtc) != HAL_OK) - { - Error_Handler(); - } - - /* USER CODE BEGIN Check_RTC_BKUP */ - - /* USER CODE END Check_RTC_BKUP */ - - /** Initialize RTC and set the Time and Date - */ - if (HAL_RTCEx_SetSSRU_IT(&hrtc) != HAL_OK) - { - Error_Handler(); - } - - /** Enable the Alarm A - */ - sAlarm.BinaryAutoClr = RTC_ALARMSUBSECONDBIN_AUTOCLR_NO; - sAlarm.AlarmTime.SubSeconds = 0x0; - sAlarm.AlarmMask = RTC_ALARMMASK_NONE; - sAlarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDBINMASK_NONE; - sAlarm.Alarm = RTC_ALARM_A; - if (HAL_RTC_SetAlarm_IT(&hrtc, &sAlarm, 0) != HAL_OK) - { - Error_Handler(); - } - /* USER CODE BEGIN RTC_Init 2 */ - - /* USER CODE END RTC_Init 2 */ - -} - -void HAL_RTC_MspInit(RTC_HandleTypeDef* rtcHandle) -{ - - RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0}; - if(rtcHandle->Instance==RTC) - { - /* USER CODE BEGIN RTC_MspInit 0 */ - - /* USER CODE END RTC_MspInit 0 */ - - /** Initializes the peripherals clocks - */ - PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC; - PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE; - - if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) - { - Error_Handler(); - } - - /* RTC clock enable */ - __HAL_RCC_RTC_ENABLE(); - __HAL_RCC_RTCAPB_CLK_ENABLE(); - - /* RTC interrupt Init */ - HAL_NVIC_SetPriority(TAMP_STAMP_LSECSS_SSRU_IRQn, 0, 0); - HAL_NVIC_EnableIRQ(TAMP_STAMP_LSECSS_SSRU_IRQn); - HAL_NVIC_SetPriority(RTC_Alarm_IRQn, 0, 0); - HAL_NVIC_EnableIRQ(RTC_Alarm_IRQn); - /* USER CODE BEGIN RTC_MspInit 1 */ - - /* USER CODE END RTC_MspInit 1 */ - } -} - -void HAL_RTC_MspDeInit(RTC_HandleTypeDef* rtcHandle) -{ - - if(rtcHandle->Instance==RTC) - { - /* USER CODE BEGIN RTC_MspDeInit 0 */ - - /* USER CODE END RTC_MspDeInit 0 */ - /* Peripheral clock disable */ - __HAL_RCC_RTC_DISABLE(); - __HAL_RCC_RTCAPB_CLK_DISABLE(); - - /* RTC interrupt Deinit */ - HAL_NVIC_DisableIRQ(TAMP_STAMP_LSECSS_SSRU_IRQn); - HAL_NVIC_DisableIRQ(RTC_Alarm_IRQn); - /* USER CODE BEGIN RTC_MspDeInit 1 */ - - /* USER CODE END RTC_MspDeInit 1 */ - } -} - -/* USER CODE BEGIN 1 */ - -/* USER CODE END 1 */ diff --git a/src/BSP/rtc.h b/src/BSP/rtc.h deleted file mode 100644 index a3c5be7..0000000 --- a/src/BSP/rtc.h +++ /dev/null @@ -1,53 +0,0 @@ -/* USER CODE BEGIN Header */ -/** - ****************************************************************************** - * @file rtc.h - * @brief This file contains all the function prototypes for - * the rtc.c file - ****************************************************************************** - * @attention - * - * Copyright (c) 2022 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 - * - ****************************************************************************** - */ -/* USER CODE END Header */ -/* Define to prevent recursive inclusion -------------------------------------*/ -#ifndef __RTC_H__ -#define __RTC_H__ - -#ifdef __cplusplus -extern "C" { -#endif - -/* Includes ------------------------------------------------------------------*/ -#include "main.h" - -/* USER CODE BEGIN Includes */ - -/* USER CODE END Includes */ - -extern RTC_HandleTypeDef hrtc; - -/* USER CODE BEGIN Private defines */ - -/* USER CODE END Private defines */ - -void MX_RTC_Init(void); - -/* USER CODE BEGIN Prototypes */ - -/* USER CODE END Prototypes */ - -#ifdef __cplusplus -} -#endif - -#endif /* __RTC_H__ */ - diff --git a/src/BSP/timer_if.c b/src/BSP/timer_if.c index 9b32f3b..cf3e279 100644 --- a/src/BSP/timer_if.c +++ b/src/BSP/timer_if.c @@ -27,18 +27,56 @@ // #include "stm32_lpm.h" // #include "utilities_def.h" #include "stm32wlxx_ll_rtc.h" -#include "rtc.h" /* USER CODE BEGIN Includes */ /* USER CODE END Includes */ /* External variables ---------------------------------------------------------*/ -/** - * @brief RTC handle - */ + extern RTC_HandleTypeDef RtcHandle; +/* HAL MSP function used for RTC_Init */ +void HAL_RTC_MspInit(RTC_HandleTypeDef* rtcHandle) +{ + + if(rtcHandle->Instance==RTC) + { + if (HAL_RTCEx_SetSSRU_IT(rtcHandle) != HAL_OK) + { + Error_Handler(); + } + + /* RTC interrupt Init */ + HAL_NVIC_SetPriority(TAMP_STAMP_LSECSS_SSRU_IRQn, 0, 0); + HAL_NVIC_EnableIRQ(TAMP_STAMP_LSECSS_SSRU_IRQn); + + HAL_NVIC_SetPriority(RTC_Alarm_IRQn, RTC_IRQ_PRIO, RTC_IRQ_SUBPRIO); + HAL_NVIC_EnableIRQ(RTC_Alarm_IRQn); + } +} + +void HAL_RTC_MspDeInit(RTC_HandleTypeDef* rtcHandle) +{ + + if(rtcHandle->Instance==RTC) + { + /* USER CODE BEGIN RTC_MspDeInit 0 */ + + /* USER CODE END RTC_MspDeInit 0 */ + /* Peripheral clock disable */ + __HAL_RCC_RTC_DISABLE(); + __HAL_RCC_RTCAPB_CLK_DISABLE(); + + /* RTC interrupt Deinit */ + HAL_NVIC_DisableIRQ(TAMP_STAMP_LSECSS_SSRU_IRQn); + HAL_NVIC_DisableIRQ(RTC_Alarm_IRQn); + /* USER CODE BEGIN RTC_MspDeInit 1 */ + + /* USER CODE END RTC_MspDeInit 1 */ + } +} + /** * @brief Timer driver callbacks handler */ @@ -187,6 +225,12 @@ UTIL_TIMER_Status_t TIMER_IF_Init(void) if (RTC_Initialized == false) { /* RTC is already Initialized by the LoRaWan::begin */ + RtcHandle.IsEnabled.RtcFeatures = UINT32_MAX; + + /** Enable the Alarm B just after the HAL_RTC_Init */ + RTC_StartAlarm(RTC_ALARM_B, 0, 0, 0, 0, 0, RTC_HOURFORMAT12_PM, 32UL); + + /*Stop Timer */ TIMER_IF_StopTimer(); /*overload RTC feature enable*/ diff --git a/src/BSP/timer_if.h b/src/BSP/timer_if.h index f9659f1..04ca538 100644 --- a/src/BSP/timer_if.h +++ b/src/BSP/timer_if.h @@ -46,6 +46,7 @@ void UTIL_TIMER_IRQ_MAP_PROCESS(void *data); /* Exported types ------------------------------------------------------------*/ /* USER CODE BEGIN ET */ + /* USER CODE END ET */ /* Exported constants --------------------------------------------------------*/ From ca47122419939fba03dd4f8c246b376519f56534 Mon Sep 17 00:00:00 2001 From: Francois Ramu Date: Tue, 20 Jun 2023 08:35:53 +0200 Subject: [PATCH 5/7] configure and set the AlarmB to use in MIX mode Directly call the RTC functions from the STM32duino RTC library Let the IsEnabled.RtcFeatures be handled by the RTC library --- src/BSP/timer_if.c | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/src/BSP/timer_if.c b/src/BSP/timer_if.c index cf3e279..cb490e5 100644 --- a/src/BSP/timer_if.c +++ b/src/BSP/timer_if.c @@ -46,6 +46,8 @@ void HAL_RTC_MspInit(RTC_HandleTypeDef* rtcHandle) { Error_Handler(); } + /* give init value for the RtcFeatures enable */ + rtcHandle->IsEnabled.RtcFeatures = 0; /* RTC interrupt Init */ HAL_NVIC_SetPriority(TAMP_STAMP_LSECSS_SSRU_IRQn, 0, 0); @@ -224,17 +226,8 @@ UTIL_TIMER_Status_t TIMER_IF_Init(void) /* USER CODE END TIMER_IF_Init */ if (RTC_Initialized == false) { - /* RTC is already Initialized by the LoRaWan::begin */ - RtcHandle.IsEnabled.RtcFeatures = UINT32_MAX; - - /** Enable the Alarm B just after the HAL_RTC_Init */ - RTC_StartAlarm(RTC_ALARM_B, 0, 0, 0, 0, 0, RTC_HOURFORMAT12_PM, 32UL); - - /*Stop Timer */ - TIMER_IF_StopTimer(); - - /*overload RTC feature enable*/ - RtcHandle.IsEnabled.RtcFeatures = UINT32_MAX; + /*Stop Timer : Disable the Alarm B interrupt */ + RTC_StopAlarm(RTC_ALARM_B); /*Initialize MSB ticks*/ TIMER_IF_BkUp_Write_MSBticks(0); @@ -259,14 +252,16 @@ UTIL_TIMER_Status_t TIMER_IF_StartTimer(uint32_t timeout) /* USER CODE BEGIN TIMER_IF_StartTimer */ /* USER CODE END TIMER_IF_StartTimer */ + /*Stop timer if one is already started*/ - TIMER_IF_StopTimer(); + RTC_StopAlarm(RTC_ALARM_B); + timeout += RtcTimerContext; - TIMER_IF_DBG_PRINTF("Start timer: time=%d, alarm=%d\n\r", GetTimerTicks(), timeout); + TIMER_IF_DBG_PRINTF("Start timer: time=%d, alarm=%d\n\r", GetTimerTicks(), timeout); - /* Program ALARM B on subsecond, mask is 32 (and fixed to RTC_ALARMMASK_NONE for calendar) */ - RTC_StartAlarm(RTC_ALARM_B, 0, 0, 0, 0, UINT32_MAX - timeout, RTC_HOURFORMAT12_PM, 32UL); + /* Program ALARM B on subsecond, mask is 32 (and fixed to RTC_ALARMMASK_ALL for calendar) */ + RTC_StartAlarm(RTC_ALARM_B, 0, 0, 0, 0, UINT32_MAX - timeout, RTC_HOURFORMAT12_PM, 31UL); /* USER CODE BEGIN TIMER_IF_StartTimer_Last */ @@ -284,8 +279,6 @@ UTIL_TIMER_Status_t TIMER_IF_StopTimer(void) /* Disable the Alarm B interrupt */ RTC_StopAlarm(RTC_ALARM_B); - /*overload RTC feature enable*/ - RtcHandle.IsEnabled.RtcFeatures = UINT32_MAX; /* USER CODE BEGIN TIMER_IF_StopTimer_Last */ /* USER CODE END TIMER_IF_StopTimer_Last */ From 5ade244e6e9791c2aadf169117843f5eaf1b505d Mon Sep 17 00:00:00 2001 From: Francois Ramu Date: Mon, 3 Jul 2023 13:19:08 +0200 Subject: [PATCH 6/7] with RTC configured in MIX mode, the SSR is 32-bit value Give the parameters a 32-bit value to match the SubSecond register of the RTC. --- src/BSP/timer_if.c | 2 +- src/BSP/timer_if.h | 2 +- src/STM32CubeWL/Utilities/misc/stm32_systime.c | 10 +++++----- src/STM32CubeWL/Utilities/misc/stm32_systime.h | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/BSP/timer_if.c b/src/BSP/timer_if.c index cb490e5..6d005b2 100644 --- a/src/BSP/timer_if.c +++ b/src/BSP/timer_if.c @@ -412,7 +412,7 @@ void HAL_RTCEx_SSRUEventCallback(RTC_HandleTypeDef *hrtc) /* USER CODE END HAL_RTCEx_SSRUEventCallback_Last */ } -uint32_t TIMER_IF_GetTime(uint16_t *mSeconds) +uint32_t TIMER_IF_GetTime(uint32_t *mSeconds) { uint32_t seconds = 0; /* USER CODE BEGIN TIMER_IF_GetTime */ diff --git a/src/BSP/timer_if.h b/src/BSP/timer_if.h index 04ca538..b280351 100644 --- a/src/BSP/timer_if.h +++ b/src/BSP/timer_if.h @@ -140,7 +140,7 @@ uint32_t TIMER_IF_Convert_Tick2ms(uint32_t tick); * @param[out] subSeconds in ticks * @return time seconds */ -uint32_t TIMER_IF_GetTime(uint16_t *subSeconds); +uint32_t TIMER_IF_GetTime(uint32_t *subSeconds); /** * @brief write seconds in backUp register diff --git a/src/STM32CubeWL/Utilities/misc/stm32_systime.c b/src/STM32CubeWL/Utilities/misc/stm32_systime.c index 4a7a1e5..0f2b5d0 100644 --- a/src/STM32CubeWL/Utilities/misc/stm32_systime.c +++ b/src/STM32CubeWL/Utilities/misc/stm32_systime.c @@ -233,7 +233,7 @@ void SysTimeSet( SysTime_t sysTime ) SysTime_t calendarTime = { .Seconds = 0, .SubSeconds = 0 }; - calendarTime.Seconds = UTIL_SYSTIMDriver.GetCalendarTime( ( uint16_t* )&calendarTime.SubSeconds ); + calendarTime.Seconds = UTIL_SYSTIMDriver.GetCalendarTime( ( uint32_t* )&calendarTime.SubSeconds ); // sysTime is UNIX epoch DeltaTime = SysTimeSub( sysTime, calendarTime ); @@ -248,9 +248,9 @@ SysTime_t SysTimeGet( void ) SysTime_t sysTime = { .Seconds = 0, .SubSeconds = 0 }; SysTime_t DeltaTime; - calendarTime.Seconds = UTIL_SYSTIMDriver.GetCalendarTime( ( uint16_t* )&calendarTime.SubSeconds ); + calendarTime.Seconds = UTIL_SYSTIMDriver.GetCalendarTime( ( uint32_t* )&calendarTime.SubSeconds ); - DeltaTime.SubSeconds = (int16_t)UTIL_SYSTIMDriver.BKUPRead_SubSeconds(); + DeltaTime.SubSeconds = (int32_t)UTIL_SYSTIMDriver.BKUPRead_SubSeconds(); DeltaTime.Seconds = UTIL_SYSTIMDriver.BKUPRead_Seconds(); sysTime = SysTimeAdd( DeltaTime, calendarTime ); @@ -263,7 +263,7 @@ SysTime_t SysTimeGetMcuTime( void ) { SysTime_t calendarTime = { .Seconds = 0, .SubSeconds = 0 }; - calendarTime.Seconds = UTIL_SYSTIMDriver.GetCalendarTime( ( uint16_t* )&calendarTime.SubSeconds ); + calendarTime.Seconds = UTIL_SYSTIMDriver.GetCalendarTime( ( uint32_t* )&calendarTime.SubSeconds ); return calendarTime; } @@ -284,7 +284,7 @@ SysTime_t SysTimeFromMs( uint32_t timeMs ) SysTime_t sysTime = { .Seconds = seconds, .SubSeconds = timeMs - seconds * 1000 }; SysTime_t DeltaTime = { 0 }; - DeltaTime.SubSeconds = (int16_t)UTIL_SYSTIMDriver.BKUPRead_SubSeconds(); + DeltaTime.SubSeconds = (int32_t)UTIL_SYSTIMDriver.BKUPRead_SubSeconds(); DeltaTime.Seconds = UTIL_SYSTIMDriver.BKUPRead_Seconds(); return SysTimeAdd( sysTime, DeltaTime ); } diff --git a/src/STM32CubeWL/Utilities/misc/stm32_systime.h b/src/STM32CubeWL/Utilities/misc/stm32_systime.h index 5e219d3..78b033f 100644 --- a/src/STM32CubeWL/Utilities/misc/stm32_systime.h +++ b/src/STM32CubeWL/Utilities/misc/stm32_systime.h @@ -133,7 +133,7 @@ typedef struct uint32_t (*BKUPRead_Seconds) ( void ); /*!< Get the timer differencebetween real time and rtc time */ void (*BKUPWrite_SubSeconds) ( uint32_t SubSeconds); /*!< Set the timer differencebetween real time and rtc time */ uint32_t (*BKUPRead_SubSeconds) ( void ); /*!< Get the timer differencebetween real time and rtc time */ - uint32_t (*GetCalendarTime)( uint16_t* SubSeconds ); /*!< Set the rtc time */ + uint32_t (*GetCalendarTime)( uint32_t* SubSeconds ); /*!< Set the rtc time */ } UTIL_SYSTIM_Driver_s; /** From d2da46d4adcde28d5484020f9f03b1377f5d1ad0 Mon Sep 17 00:00:00 2001 From: Francois Ramu Date: Mon, 3 Jul 2023 13:20:00 +0200 Subject: [PATCH 7/7] adjust the tick in MIX mode to 256Hz In Mix mode, the SSR is counting on a fqce APRE which is RTCCLK freq / 'PREDIV_A + 1) = 256Hz when RTC is clocked by LSE. This frqe gives a tick of 3.9ms (= 1/256 * 1000) --- src/BSP/timer_if.c | 15 ++++++--------- src/BSP/timer_if.h | 5 +++++ 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/BSP/timer_if.c b/src/BSP/timer_if.c index 6d005b2..40a7c76 100644 --- a/src/BSP/timer_if.c +++ b/src/BSP/timer_if.c @@ -358,11 +358,12 @@ uint32_t TIMER_IF_Convert_ms2Tick(uint32_t timeMilliSec) /* USER CODE BEGIN TIMER_IF_Convert_ms2Tick */ /* USER CODE END TIMER_IF_Convert_ms2Tick */ - ret = ((uint32_t)((((uint64_t) timeMilliSec) << RTC_N_PREDIV_S) / 1000)); + ret = ((uint64_t)timeMilliSec * MS_TO_TICK) / 1000; + /* USER CODE BEGIN TIMER_IF_Convert_ms2Tick_Last */ /* USER CODE END TIMER_IF_Convert_ms2Tick_Last */ - return ret; + return (uint32_t)ret; } uint32_t TIMER_IF_Convert_Tick2ms(uint32_t tick) @@ -371,7 +372,7 @@ uint32_t TIMER_IF_Convert_Tick2ms(uint32_t tick) /* USER CODE BEGIN TIMER_IF_Convert_Tick2ms */ /* USER CODE END TIMER_IF_Convert_Tick2ms */ - ret = ((uint32_t)((((uint64_t)(tick)) * 1000) >> RTC_N_PREDIV_S)); + ret = tick * TICK_TO_MS; /* USER CODE BEGIN TIMER_IF_Convert_Tick2ms_Last */ /* USER CODE END TIMER_IF_Convert_Tick2ms_Last */ @@ -424,12 +425,8 @@ uint32_t TIMER_IF_GetTime(uint32_t *mSeconds) ticks = (((uint64_t) timerValueMSB) << 32) + timerValueLsb; - seconds = (uint32_t)(ticks >> RTC_N_PREDIV_S); - - ticks = (uint32_t) ticks & RTC_PREDIV_S; - - *mSeconds = TIMER_IF_Convert_Tick2ms(ticks); - + seconds = ticks / MS_TO_TICK; + *mSeconds = (ticks * 1000) / MS_TO_TICK; /* USER CODE BEGIN TIMER_IF_GetTime_Last */ /* USER CODE END TIMER_IF_GetTime_Last */ diff --git a/src/BSP/timer_if.h b/src/BSP/timer_if.h index b280351..a286889 100644 --- a/src/BSP/timer_if.h +++ b/src/BSP/timer_if.h @@ -39,6 +39,11 @@ extern "C" { #define UTIL_TIMER_IRQ_MAP_INIT() #endif /* UTIL_TIMER_IRQ_MAP_INIT */ +/* With RTC clocked by LSE, the APRE freq is 256Hz: 1 tick is 3.9ms (APREDIV = 0x7F) */ +#define MS_TO_TICK 256 +/* Give one more (to adjust to x3.9 factor) */ +#define TICK_TO_MS ((1000/256) + 1) + void UTIL_TIMER_IRQ_MAP_PROCESS(void *data); /* USER CODE END Includes */