From 7f8a1d6341476f5ba7e7520f08d906d592acb4f3 Mon Sep 17 00:00:00 2001 From: jeromecoutant Date: Wed, 23 Nov 2016 16:41:32 +0100 Subject: [PATCH 1/2] STM32: Refactor lp_ticker.c + rtc_api.c + sleep.c + rtc_api_hal.h files --- .../TARGET_STM32F0/common_objects.h | 4 + targets/TARGET_STM/TARGET_STM32F0/lp_ticker.c | 83 ----- targets/TARGET_STM/TARGET_STM32F0/rtc_api.c | 294 ----------------- targets/TARGET_STM/TARGET_STM32F0/sleep.c | 59 ---- .../TARGET_STM32F1/common_objects.h | 3 + targets/TARGET_STM/TARGET_STM32F1/rtc_api.c | 199 ----------- targets/TARGET_STM/TARGET_STM32F1/sleep.c | 55 ---- targets/TARGET_STM/TARGET_STM32F2/rtc_api.c | 222 ------------- targets/TARGET_STM/TARGET_STM32F2/sleep.c | 61 ---- .../TARGET_STM32F3/common_objects.h | 3 + targets/TARGET_STM/TARGET_STM32F3/rtc_api.c | 296 ----------------- .../TARGET_STM/TARGET_STM32F3/rtc_api_hal.h | 79 ----- targets/TARGET_STM/TARGET_STM32F4/lp_ticker.c | 83 ----- targets/TARGET_STM/TARGET_STM32F4/rtc_api.c | 298 ----------------- .../TARGET_STM/TARGET_STM32F4/rtc_api_hal.h | 79 ----- targets/TARGET_STM/TARGET_STM32F4/sleep.c | 60 ---- targets/TARGET_STM/TARGET_STM32F7/lp_ticker.c | 83 ----- targets/TARGET_STM/TARGET_STM32F7/rtc_api.c | 305 ----------------- .../TARGET_STM/TARGET_STM32F7/rtc_api_hal.h | 79 ----- targets/TARGET_STM/TARGET_STM32F7/sleep.c | 59 ---- .../TARGET_STM32L0/common_objects.h | 3 + targets/TARGET_STM/TARGET_STM32L0/lp_ticker.c | 83 ----- .../TARGET_STM/TARGET_STM32L0/rtc_api_hal.h | 79 ----- targets/TARGET_STM/TARGET_STM32L0/sleep.c | 60 ---- targets/TARGET_STM/TARGET_STM32L1/lp_ticker.c | 83 ----- targets/TARGET_STM/TARGET_STM32L1/rtc_api.c | 308 ----------------- .../TARGET_STM/TARGET_STM32L1/rtc_api_hal.h | 79 ----- targets/TARGET_STM/TARGET_STM32L1/sleep.c | 100 ------ .../TARGET_STM32L4/common_objects.h | 3 + targets/TARGET_STM/TARGET_STM32L4/lp_ticker.c | 83 ----- targets/TARGET_STM/TARGET_STM32L4/rtc_api.c | 310 ------------------ .../TARGET_STM/TARGET_STM32L4/rtc_api_hal.h | 79 ----- targets/TARGET_STM/TARGET_STM32L4/sleep.c | 75 ----- .../{TARGET_STM32F3 => }/lp_ticker.c | 12 +- .../TARGET_STM/{TARGET_STM32L0 => }/rtc_api.c | 144 ++++---- .../{TARGET_STM32F0 => }/rtc_api_hal.h | 0 .../TARGET_STM/{TARGET_STM32F3 => }/sleep.c | 18 +- 37 files changed, 114 insertions(+), 3809 deletions(-) delete mode 100644 targets/TARGET_STM/TARGET_STM32F0/lp_ticker.c delete mode 100644 targets/TARGET_STM/TARGET_STM32F0/rtc_api.c delete mode 100644 targets/TARGET_STM/TARGET_STM32F0/sleep.c delete mode 100644 targets/TARGET_STM/TARGET_STM32F1/rtc_api.c delete mode 100644 targets/TARGET_STM/TARGET_STM32F1/sleep.c delete mode 100644 targets/TARGET_STM/TARGET_STM32F2/rtc_api.c delete mode 100644 targets/TARGET_STM/TARGET_STM32F2/sleep.c delete mode 100644 targets/TARGET_STM/TARGET_STM32F3/rtc_api.c delete mode 100644 targets/TARGET_STM/TARGET_STM32F3/rtc_api_hal.h delete mode 100644 targets/TARGET_STM/TARGET_STM32F4/lp_ticker.c delete mode 100644 targets/TARGET_STM/TARGET_STM32F4/rtc_api.c delete mode 100644 targets/TARGET_STM/TARGET_STM32F4/rtc_api_hal.h delete mode 100644 targets/TARGET_STM/TARGET_STM32F4/sleep.c delete mode 100644 targets/TARGET_STM/TARGET_STM32F7/lp_ticker.c delete mode 100644 targets/TARGET_STM/TARGET_STM32F7/rtc_api.c delete mode 100644 targets/TARGET_STM/TARGET_STM32F7/rtc_api_hal.h delete mode 100644 targets/TARGET_STM/TARGET_STM32F7/sleep.c delete mode 100644 targets/TARGET_STM/TARGET_STM32L0/lp_ticker.c delete mode 100644 targets/TARGET_STM/TARGET_STM32L0/rtc_api_hal.h delete mode 100644 targets/TARGET_STM/TARGET_STM32L0/sleep.c delete mode 100644 targets/TARGET_STM/TARGET_STM32L1/lp_ticker.c delete mode 100755 targets/TARGET_STM/TARGET_STM32L1/rtc_api.c delete mode 100644 targets/TARGET_STM/TARGET_STM32L1/rtc_api_hal.h delete mode 100644 targets/TARGET_STM/TARGET_STM32L1/sleep.c delete mode 100644 targets/TARGET_STM/TARGET_STM32L4/lp_ticker.c delete mode 100644 targets/TARGET_STM/TARGET_STM32L4/rtc_api.c delete mode 100644 targets/TARGET_STM/TARGET_STM32L4/rtc_api_hal.h delete mode 100644 targets/TARGET_STM/TARGET_STM32L4/sleep.c rename targets/TARGET_STM/{TARGET_STM32F3 => }/lp_ticker.c (97%) rename targets/TARGET_STM/{TARGET_STM32L0 => }/rtc_api.c (84%) rename targets/TARGET_STM/{TARGET_STM32F0 => }/rtc_api_hal.h (100%) rename targets/TARGET_STM/{TARGET_STM32F3 => }/sleep.c (89%) diff --git a/targets/TARGET_STM/TARGET_STM32F0/common_objects.h b/targets/TARGET_STM/TARGET_STM32F0/common_objects.h index 3bbfc82717d..be9e9b5675b 100644 --- a/targets/TARGET_STM/TARGET_STM32F0/common_objects.h +++ b/targets/TARGET_STM/TARGET_STM32F0/common_objects.h @@ -89,4 +89,8 @@ struct serial_s { } #endif +/* STM32F0 HAL doesn't provide this API called in rtc_api.c */ +#define __HAL_RCC_RTC_CLKPRESCALER(__RTCCLKSource__) +#define RTC_WKUP_IRQn RTC_IRQn + #endif diff --git a/targets/TARGET_STM/TARGET_STM32F0/lp_ticker.c b/targets/TARGET_STM/TARGET_STM32F0/lp_ticker.c deleted file mode 100644 index 07034e41af7..00000000000 --- a/targets/TARGET_STM/TARGET_STM32F0/lp_ticker.c +++ /dev/null @@ -1,83 +0,0 @@ -/* mbed Microcontroller Library - ******************************************************************************* - * Copyright (c) 2016, 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: - * - * 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 "device.h" - -#if DEVICE_LOWPOWERTIMER - -#include "ticker_api.h" -#include "lp_ticker_api.h" -#include "rtc_api.h" -#include "rtc_api_hal.h" - -static uint8_t lp_ticker_inited = 0; - -void lp_ticker_init(void) -{ - if (lp_ticker_inited) return; - lp_ticker_inited = 1; - - rtc_init(); - rtc_set_irq_handler((uint32_t) lp_ticker_irq_handler); -} - -uint32_t lp_ticker_read(void) -{ - uint32_t usecs; - time_t time; - - lp_ticker_init(); - - do { - time = rtc_read(); - usecs = rtc_read_subseconds(); - } while (time != rtc_read()); - - return (time * 1000000) + usecs; -} - -void lp_ticker_set_interrupt(timestamp_t timestamp) -{ - uint32_t delta; - - delta = timestamp - lp_ticker_read(); - rtc_set_wake_up_timer(delta); - } - -void lp_ticker_disable_interrupt(void) -{ - rtc_deactivate_wake_up_timer(); -} - -void lp_ticker_clear_interrupt(void) -{ - -} - -#endif diff --git a/targets/TARGET_STM/TARGET_STM32F0/rtc_api.c b/targets/TARGET_STM/TARGET_STM32F0/rtc_api.c deleted file mode 100644 index bf6724c2974..00000000000 --- a/targets/TARGET_STM/TARGET_STM32F0/rtc_api.c +++ /dev/null @@ -1,294 +0,0 @@ -/* mbed Microcontroller Library - ******************************************************************************* - * Copyright (c) 2016, 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: - * - * 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 "rtc_api.h" -#include "rtc_api_hal.h" - -#if DEVICE_RTC - -#include "mbed_error.h" - -#if RTC_LSI -static int rtc_inited = 0; -#endif - -static RTC_HandleTypeDef RtcHandle; - -#if RTC_LSI - #define RTC_CLOCK LSI_VALUE -#else - #define RTC_CLOCK LSE_VALUE -#endif - -#if DEVICE_LOWPOWERTIMER - #define RTC_ASYNCH_PREDIV ((RTC_CLOCK - 1) / 0x8000) - #define RTC_SYNCH_PREDIV (RTC_CLOCK / (RTC_ASYNCH_PREDIV + 1) - 1) -#else - #define RTC_ASYNCH_PREDIV (0x007F) - #define RTC_SYNCH_PREDIV (RTC_CLOCK / (RTC_ASYNCH_PREDIV + 1) - 1) -#endif - -#if DEVICE_LOWPOWERTIMER - static void (*irq_handler)(void); - static void RTC_IRQHandler(void); -#endif - -void rtc_init(void) { - RCC_OscInitTypeDef RCC_OscInitStruct; - -#if RTC_LSI - if (rtc_inited) return; - rtc_inited = 1; -#endif - - RtcHandle.Instance = RTC; - -#if !RTC_LSI - // Enable LSE Oscillator - RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE; - RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; // Mandatory, otherwise the PLL is reconfigured! - RCC_OscInitStruct.LSEState = RCC_LSE_ON; // External 32.768 kHz clock on OSC_IN/OSC_OUT - RCC_OscInitStruct.LSIState = RCC_LSI_OFF; - if (HAL_RCC_OscConfig(&RCC_OscInitStruct) == HAL_OK) { // Check if LSE has started correctly - // Connect LSE to RTC - __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSE); - } else { - error("Cannot initialize RTC with LSE\n"); - } -#else - // Enable Power clock - __PWR_CLK_ENABLE(); - - // Enable access to Backup domain - HAL_PWR_EnableBkUpAccess(); - - // Reset Backup domain - __HAL_RCC_BACKUPRESET_FORCE(); - __HAL_RCC_BACKUPRESET_RELEASE(); - - // Enable LSI clock - RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE; - RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; // Mandatory, otherwise the PLL is reconfigured! - RCC_OscInitStruct.LSEState = RCC_LSE_OFF; - RCC_OscInitStruct.LSIState = RCC_LSI_ON; - if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { - error("Cannot initialize RTC with LSI\n"); - } - // Connect LSI to RTC - __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSI); -#endif - - // Enable RTC - __HAL_RCC_RTC_ENABLE(); - - RtcHandle.Init.HourFormat = RTC_HOURFORMAT_24; - RtcHandle.Init.AsynchPrediv = RTC_ASYNCH_PREDIV; - RtcHandle.Init.SynchPrediv = RTC_SYNCH_PREDIV; - RtcHandle.Init.OutPut = RTC_OUTPUT_DISABLE; - RtcHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH; - RtcHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN; - - if (HAL_RTC_Init(&RtcHandle) != HAL_OK) { - error("RTC error: RTC initialization failed."); - } - -#if DEVICE_LOWPOWERTIMER -#if RTC_LSI - rtc_write(0); -#else - if (!rtc_isenabled()) { - rtc_write(0); - } -#endif - NVIC_ClearPendingIRQ(RTC_IRQn); - NVIC_DisableIRQ(RTC_IRQn); - NVIC_SetVector(RTC_IRQn, (uint32_t)RTC_IRQHandler); - NVIC_EnableIRQ(RTC_IRQn); -#endif -} - -void rtc_free(void) { -#if RTC_LSI - // Enable Power clock - __PWR_CLK_ENABLE(); - - // Enable access to Backup domain - HAL_PWR_EnableBkUpAccess(); - - // Reset Backup domain - __HAL_RCC_BACKUPRESET_FORCE(); - __HAL_RCC_BACKUPRESET_RELEASE(); - - // Disable access to Backup domain - HAL_PWR_DisableBkUpAccess(); -#endif - - // Disable LSI and LSE clocks - RCC_OscInitTypeDef RCC_OscInitStruct; - RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE; - RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; - RCC_OscInitStruct.LSIState = RCC_LSI_OFF; - RCC_OscInitStruct.LSEState = RCC_LSE_OFF; - HAL_RCC_OscConfig(&RCC_OscInitStruct); - -#if RTC_LSI - rtc_inited = 0; -#endif -} - -int rtc_isenabled(void) { -#if RTC_LSI - return rtc_inited; -#else - if ((RTC->ISR & RTC_ISR_INITS) == RTC_ISR_INITS) { - return 1; - } else { - return 0; - } -#endif -} - -/* - RTC Registers - RTC_WeekDay 1=monday, 2=tuesday, ..., 7=sunday - RTC_Month 1=january, 2=february, ..., 12=december - RTC_Date day of the month 1-31 - RTC_Year year 0-99 - struct tm - tm_sec seconds after the minute 0-61 - tm_min minutes after the hour 0-59 - tm_hour hours since midnight 0-23 - tm_mday day of the month 1-31 - tm_mon months since January 0-11 - tm_year years since 1900 - tm_wday days since Sunday 0-6 - tm_yday days since January 1 0-365 - tm_isdst Daylight Saving Time flag -*/ -time_t rtc_read(void) { - RTC_DateTypeDef dateStruct; - RTC_TimeTypeDef timeStruct; - struct tm timeinfo; - - RtcHandle.Instance = RTC; - - // Read actual date and time - // Warning: the time must be read first! - HAL_RTC_GetTime(&RtcHandle, &timeStruct, FORMAT_BIN); - HAL_RTC_GetDate(&RtcHandle, &dateStruct, FORMAT_BIN); - - // Setup a tm structure based on the RTC - timeinfo.tm_wday = dateStruct.WeekDay; - timeinfo.tm_mon = dateStruct.Month - 1; - timeinfo.tm_mday = dateStruct.Date; - timeinfo.tm_year = dateStruct.Year + 68; - timeinfo.tm_hour = timeStruct.Hours; - timeinfo.tm_min = timeStruct.Minutes; - timeinfo.tm_sec = timeStruct.Seconds; - timeinfo.tm_isdst = -1; - - // Convert to timestamp - time_t t = mktime(&timeinfo); - - return t; -} - -void rtc_write(time_t t) { - RTC_DateTypeDef dateStruct; - RTC_TimeTypeDef timeStruct; - - RtcHandle.Instance = RTC; - - // Convert the time into a tm - struct tm *timeinfo = localtime(&t); - - // Fill RTC structures - dateStruct.WeekDay = timeinfo->tm_wday; - dateStruct.Month = timeinfo->tm_mon + 1; - dateStruct.Date = timeinfo->tm_mday; - dateStruct.Year = timeinfo->tm_year - 68; - timeStruct.Hours = timeinfo->tm_hour; - timeStruct.Minutes = timeinfo->tm_min; - timeStruct.Seconds = timeinfo->tm_sec; - timeStruct.TimeFormat = RTC_HOURFORMAT_24; - timeStruct.DayLightSaving = RTC_DAYLIGHTSAVING_NONE; - timeStruct.StoreOperation = RTC_STOREOPERATION_RESET; - - // Change the RTC current date/time - HAL_RTC_SetDate(&RtcHandle, &dateStruct, FORMAT_BIN); - HAL_RTC_SetTime(&RtcHandle, &timeStruct, FORMAT_BIN); -} - -#if DEVICE_LOWPOWERTIMER - -static void RTC_IRQHandler(void) -{ - HAL_RTCEx_WakeUpTimerIRQHandler(&RtcHandle); -} - -void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc) -{ - if (irq_handler) { - // Fire the user callback - irq_handler(); - } -} - -void rtc_set_irq_handler(uint32_t handler) -{ - irq_handler = (void (*)(void))handler; -} - -uint32_t rtc_read_subseconds(void) -{ - return 1000000.f * ((double)(RTC_SYNCH_PREDIV - RTC->SSR) / (RTC_SYNCH_PREDIV + 1)); -} - -void rtc_set_wake_up_timer(uint32_t delta) -{ - uint32_t wake_up_counter = delta / (2000000 / RTC_CLOCK); - - if (HAL_RTCEx_SetWakeUpTimer_IT(&RtcHandle, wake_up_counter, - RTC_WAKEUPCLOCK_RTCCLK_DIV2) != HAL_OK) { - error("Set wake up timer failed\n"); - } -} - -void rtc_deactivate_wake_up_timer(void) -{ - HAL_RTCEx_DeactivateWakeUpTimer(&RtcHandle); -} - -void rtc_synchronize(void) -{ - HAL_RTC_WaitForSynchro(&RtcHandle); -} -#endif // DEVICE_LOWPOWERTIMER - -#endif diff --git a/targets/TARGET_STM/TARGET_STM32F0/sleep.c b/targets/TARGET_STM/TARGET_STM32F0/sleep.c deleted file mode 100644 index 888a74974f8..00000000000 --- a/targets/TARGET_STM/TARGET_STM32F0/sleep.c +++ /dev/null @@ -1,59 +0,0 @@ -/* mbed Microcontroller Library - ******************************************************************************* - * Copyright (c) 2015, 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: - * - * 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 "sleep_api.h" -#include "rtc_api_hal.h" - -#if DEVICE_SLEEP - -#include "cmsis.h" - - -void sleep(void) { - // Stop HAL systick - HAL_SuspendTick(); - // Request to enter SLEEP mode - HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI); - // Restart HAL systick - HAL_ResumeTick(); -} - -void deepsleep(void) { - // Request to enter STOP mode with regulator in low power mode - HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI); - - // After wake-up from STOP reconfigure the PLL - SetSysClock(); - -#if DEVICE_LOWPOWERTIMER - rtc_synchronize(); -#endif -} - -#endif diff --git a/targets/TARGET_STM/TARGET_STM32F1/common_objects.h b/targets/TARGET_STM/TARGET_STM32F1/common_objects.h index e39a16c0198..472fbd5fd62 100644 --- a/targets/TARGET_STM/TARGET_STM32F1/common_objects.h +++ b/targets/TARGET_STM/TARGET_STM32F1/common_objects.h @@ -89,5 +89,8 @@ struct spi_s { } #endif +/* STM32F1 HAL doesn't provide this API called in rtc_api.c */ +#define __HAL_RCC_RTC_CLKPRESCALER(__RTCCLKSource__) + #endif diff --git a/targets/TARGET_STM/TARGET_STM32F1/rtc_api.c b/targets/TARGET_STM/TARGET_STM32F1/rtc_api.c deleted file mode 100644 index 82c527a058a..00000000000 --- a/targets/TARGET_STM/TARGET_STM32F1/rtc_api.c +++ /dev/null @@ -1,199 +0,0 @@ -/* mbed Microcontroller Library - ******************************************************************************* - * Copyright (c) 2014, 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: - * - * 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 "rtc_api.h" - -#if DEVICE_RTC - -#include "mbed_error.h" - -static int rtc_inited = 0; - -static RTC_HandleTypeDef RtcHandle; - -void rtc_init(void) -{ - RCC_OscInitTypeDef RCC_OscInitStruct; - - if (rtc_inited) return; - rtc_inited = 1; - - RtcHandle.Instance = RTC; - -#if !RTC_LSI - // Enable LSE Oscillator - RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE; - RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; // Mandatory, otherwise the PLL is reconfigured! - RCC_OscInitStruct.LSEState = RCC_LSE_ON; // External 32.768 kHz clock on OSC_IN/OSC_OUT - RCC_OscInitStruct.LSIState = RCC_LSI_OFF; - if (HAL_RCC_OscConfig(&RCC_OscInitStruct) == HAL_OK) { // Check if LSE has started correctly - // Connect LSE to RTC - __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSE); - } else { - error("Cannot initialize RTC with LSE\n"); - } -#else - // Enable Power clock - __HAL_RCC_PWR_CLK_ENABLE(); - - // Enable access to Backup domain - HAL_PWR_EnableBkUpAccess(); - - // Reset Backup domain - __HAL_RCC_BACKUPRESET_FORCE(); - __HAL_RCC_BACKUPRESET_RELEASE(); - - // Enable LSI clock - RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE; - RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; // Mandatory, otherwise the PLL is reconfigured! - RCC_OscInitStruct.LSEState = RCC_LSE_OFF; - RCC_OscInitStruct.LSIState = RCC_LSI_ON; - if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { - error("Cannot initialize RTC with LSI\n"); - } - // Connect LSI to RTC - __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSI); - // This value is LSI typical value. To be measured precisely using a timer input capture for example. -#endif - - // Enable RTC - __HAL_RCC_RTC_ENABLE(); - - RtcHandle.Init.AsynchPrediv = RTC_AUTO_1_SECOND; - - if (HAL_RTC_Init(&RtcHandle) != HAL_OK) { - error("RTC error: RTC initialization failed."); - } -} - -void rtc_free(void) -{ -#if RTC_LSI - // Enable Power clock - __PWR_CLK_ENABLE(); - - // Enable access to Backup domain - HAL_PWR_EnableBkUpAccess(); - - // Reset Backup domain - __HAL_RCC_BACKUPRESET_FORCE(); - __HAL_RCC_BACKUPRESET_RELEASE(); - - // Disable access to Backup domain - HAL_PWR_DisableBkUpAccess(); -#endif - - // Disable LSI and LSE clocks - RCC_OscInitTypeDef RCC_OscInitStruct; - RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE; - RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; - RCC_OscInitStruct.LSIState = RCC_LSI_OFF; - RCC_OscInitStruct.LSEState = RCC_LSE_OFF; - HAL_RCC_OscConfig(&RCC_OscInitStruct); - - rtc_inited = 0; -} - -int rtc_isenabled(void) -{ - return rtc_inited; -} - -/* - RTC Registers - RTC_WeekDay 1=monday, 2=tuesday, ..., 7=sunday - RTC_Month 1=january, 2=february, ..., 12=december - RTC_Date day of the month 1-31 - RTC_Year year 0-99 - struct tm - tm_sec seconds after the minute 0-61 - tm_min minutes after the hour 0-59 - tm_hour hours since midnight 0-23 - tm_mday day of the month 1-31 - tm_mon months since January 0-11 - tm_year years since 1900 - tm_wday days since Sunday 0-6 - tm_yday days since January 1 0-365 - tm_isdst Daylight Saving Time flag -*/ -time_t rtc_read(void) -{ - RTC_DateTypeDef dateStruct; - RTC_TimeTypeDef timeStruct; - struct tm timeinfo; - - RtcHandle.Instance = RTC; - - // Read actual date and time - // Warning: the time must be read first! - HAL_RTC_GetTime(&RtcHandle, &timeStruct, FORMAT_BIN); - HAL_RTC_GetDate(&RtcHandle, &dateStruct, FORMAT_BIN); - - // Setup a tm structure based on the RTC - timeinfo.tm_wday = dateStruct.WeekDay; - timeinfo.tm_mon = dateStruct.Month - 1; - timeinfo.tm_mday = dateStruct.Date; - timeinfo.tm_year = dateStruct.Year; - timeinfo.tm_hour = timeStruct.Hours; - timeinfo.tm_min = timeStruct.Minutes; - timeinfo.tm_sec = timeStruct.Seconds; - // Daylight Saving Time information is not available - timeinfo.tm_isdst = -1; - - // Convert to timestamp - time_t t = mktime(&timeinfo); - - return t; -} - -void rtc_write(time_t t) -{ - RTC_DateTypeDef dateStruct; - RTC_TimeTypeDef timeStruct; - - RtcHandle.Instance = RTC; - - // Convert the time into a tm - struct tm *timeinfo = localtime(&t); - - // Fill RTC structures - dateStruct.WeekDay = timeinfo->tm_wday; - dateStruct.Month = timeinfo->tm_mon + 1; - dateStruct.Date = timeinfo->tm_mday; - dateStruct.Year = timeinfo->tm_year; - timeStruct.Hours = timeinfo->tm_hour; - timeStruct.Minutes = timeinfo->tm_min; - timeStruct.Seconds = timeinfo->tm_sec; - - // Change the RTC current date/time - HAL_RTC_SetDate(&RtcHandle, &dateStruct, FORMAT_BIN); - HAL_RTC_SetTime(&RtcHandle, &timeStruct, FORMAT_BIN); -} - -#endif diff --git a/targets/TARGET_STM/TARGET_STM32F1/sleep.c b/targets/TARGET_STM/TARGET_STM32F1/sleep.c deleted file mode 100644 index eedcdb67842..00000000000 --- a/targets/TARGET_STM/TARGET_STM32F1/sleep.c +++ /dev/null @@ -1,55 +0,0 @@ -/* mbed Microcontroller Library - ******************************************************************************* - * Copyright (c) 2014, 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: - * - * 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 "sleep_api.h" - -#if DEVICE_SLEEP - -#include "cmsis.h" -#include "hal_tick.h" - -void sleep(void) { - // Stop HAL systick - HAL_SuspendTick(); - // Request to enter SLEEP mode - HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI); - // Restart HAL systick - HAL_ResumeTick(); -} - -void deepsleep(void) -{ - // Request to enter STOP mode with regulator in low power mode - HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI); - - // After wake-up from STOP reconfigure the PLL - SetSysClock(); -} - -#endif diff --git a/targets/TARGET_STM/TARGET_STM32F2/rtc_api.c b/targets/TARGET_STM/TARGET_STM32F2/rtc_api.c deleted file mode 100644 index 563d5391e83..00000000000 --- a/targets/TARGET_STM/TARGET_STM32F2/rtc_api.c +++ /dev/null @@ -1,222 +0,0 @@ -/* mbed Microcontroller Library - ******************************************************************************* - * Copyright (c) 2016, 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: - * - * 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 "rtc_api.h" - -#if DEVICE_RTC - -#include "mbed_error.h" - -#if RTC_LSI -static int rtc_inited = 0; -#endif - -static RTC_HandleTypeDef RtcHandle; - -void rtc_init(void) -{ - RCC_OscInitTypeDef RCC_OscInitStruct; - uint32_t rtc_freq = 0; - -#if RTC_LSI - rtc_inited = 1; -#endif - - RtcHandle.Instance = RTC; - -#if !RTC_LSI - // Enable LSE Oscillator - RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE; - RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; /* Mandatory, otherwise the PLL is reconfigured! */ - RCC_OscInitStruct.LSEState = RCC_LSE_ON; /* External 32.768 kHz clock on OSC_IN/OSC_OUT */ - if (HAL_RCC_OscConfig(&RCC_OscInitStruct) == HAL_OK) { - // Connect LSE to RTC - __HAL_RCC_RTC_CLKPRESCALER(RCC_RTCCLKSOURCE_LSE); - __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSE); - rtc_freq = LSE_VALUE; - } else { - error("RTC error: LSE clock initialization failed."); - } -#else - // Enable Power clock - __PWR_CLK_ENABLE(); - - // Enable access to Backup domain - HAL_PWR_EnableBkUpAccess(); - - // Reset Backup domain - __HAL_RCC_BACKUPRESET_FORCE(); - __HAL_RCC_BACKUPRESET_RELEASE(); - - // Enable LSI clock - RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE; - RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; // Mandatory, otherwise the PLL is reconfigured! - RCC_OscInitStruct.LSEState = RCC_LSE_OFF; - RCC_OscInitStruct.LSIState = RCC_LSI_ON; - if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { - error("RTC error: LSI clock initialization failed."); - } - // Connect LSI to RTC - __HAL_RCC_RTC_CLKPRESCALER(RCC_RTCCLKSOURCE_LSI); - __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSI); - // [TODO] This value is LSI typical value. To be measured precisely using a timer input capture - rtc_freq = LSI_VALUE; -#endif - - // Enable RTC - __HAL_RCC_RTC_ENABLE(); - - RtcHandle.Init.HourFormat = RTC_HOURFORMAT_24; - RtcHandle.Init.AsynchPrediv = 127; - RtcHandle.Init.SynchPrediv = (rtc_freq / 128) - 1; - RtcHandle.Init.OutPut = RTC_OUTPUT_DISABLE; - RtcHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH; - RtcHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN; - - if (HAL_RTC_Init(&RtcHandle) != HAL_OK) { - error("RTC error: RTC initialization failed."); - } -} - -void rtc_free(void) -{ -#if RTC_LSI - // Enable Power clock - __PWR_CLK_ENABLE(); - - // Enable access to Backup domain - HAL_PWR_EnableBkUpAccess(); - - // Reset Backup domain - __HAL_RCC_BACKUPRESET_FORCE(); - __HAL_RCC_BACKUPRESET_RELEASE(); - - // Disable access to Backup domain - HAL_PWR_DisableBkUpAccess(); -#endif - - // Disable LSI and LSE clocks - RCC_OscInitTypeDef RCC_OscInitStruct; - RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE; - RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; - RCC_OscInitStruct.LSIState = RCC_LSI_OFF; - RCC_OscInitStruct.LSEState = RCC_LSE_OFF; - HAL_RCC_OscConfig(&RCC_OscInitStruct); -#if RTC_LSI - rtc_inited = 0; -#endif -} - -int rtc_isenabled(void) -{ -#if RTC_LSI - return rtc_inited; -#else - if ((RTC->ISR & RTC_ISR_INITS) == RTC_ISR_INITS) { - return 1; - } else { - return 0; - } -#endif -} - -/* - RTC Registers - RTC_WeekDay 1=monday, 2=tuesday, ..., 7=sunday - RTC_Month 1=january, 2=february, ..., 12=december - RTC_Date day of the month 1-31 - RTC_Year year 0-99 - struct tm - tm_sec seconds after the minute 0-61 - tm_min minutes after the hour 0-59 - tm_hour hours since midnight 0-23 - tm_mday day of the month 1-31 - tm_mon months since January 0-11 - tm_year years since 1900 - tm_wday days since Sunday 0-6 - tm_yday days since January 1 0-365 - tm_isdst Daylight Saving Time flag -*/ -time_t rtc_read(void) -{ - RTC_DateTypeDef dateStruct; - RTC_TimeTypeDef timeStruct; - struct tm timeinfo; - - RtcHandle.Instance = RTC; - - // Read actual date and time - // Warning: the time must be read first! - HAL_RTC_GetTime(&RtcHandle, &timeStruct, FORMAT_BIN); - HAL_RTC_GetDate(&RtcHandle, &dateStruct, FORMAT_BIN); - - // Setup a tm structure based on the RTC - timeinfo.tm_wday = dateStruct.WeekDay; - timeinfo.tm_mon = dateStruct.Month - 1; - timeinfo.tm_mday = dateStruct.Date; - timeinfo.tm_year = dateStruct.Year + 68; - timeinfo.tm_hour = timeStruct.Hours; - timeinfo.tm_min = timeStruct.Minutes; - timeinfo.tm_sec = timeStruct.Seconds; - timeinfo.tm_isdst = -1; - - // Convert to timestamp - time_t t = mktime(&timeinfo); - - return t; -} - -void rtc_write(time_t t) -{ - RTC_DateTypeDef dateStruct; - RTC_TimeTypeDef timeStruct; - - RtcHandle.Instance = RTC; - - // Convert the time into a tm - struct tm *timeinfo = localtime(&t); - - // Fill RTC structures - dateStruct.WeekDay = timeinfo->tm_wday; - dateStruct.Month = timeinfo->tm_mon + 1; - dateStruct.Date = timeinfo->tm_mday; - dateStruct.Year = timeinfo->tm_year - 68; - timeStruct.Hours = timeinfo->tm_hour; - timeStruct.Minutes = timeinfo->tm_min; - timeStruct.Seconds = timeinfo->tm_sec; - timeStruct.TimeFormat = RTC_HOURFORMAT_24; - timeStruct.DayLightSaving = RTC_DAYLIGHTSAVING_NONE; - timeStruct.StoreOperation = RTC_STOREOPERATION_RESET; - - // Change the RTC current date/time - HAL_RTC_SetDate(&RtcHandle, &dateStruct, FORMAT_BIN); - HAL_RTC_SetTime(&RtcHandle, &timeStruct, FORMAT_BIN); -} - -#endif diff --git a/targets/TARGET_STM/TARGET_STM32F2/sleep.c b/targets/TARGET_STM/TARGET_STM32F2/sleep.c deleted file mode 100644 index a9f995b2322..00000000000 --- a/targets/TARGET_STM/TARGET_STM32F2/sleep.c +++ /dev/null @@ -1,61 +0,0 @@ -/* mbed Microcontroller Library - ******************************************************************************* - * Copyright (c) 2016, 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: - * - * 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 "sleep_api.h" - -#if DEVICE_SLEEP - -#include "cmsis.h" - -static TIM_HandleTypeDef TimMasterHandle; - -void sleep(void) -{ - TimMasterHandle.Instance = TIM5; - - // Disable HAL tick interrupt - __HAL_TIM_DISABLE_IT(&TimMasterHandle, TIM_IT_CC2); - - // Request to enter SLEEP mode - HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI); - - // Enable HAL tick interrupt - __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC2); -} - -void deepsleep(void) -{ - // Request to enter STOP mode with regulator in low power mode - HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI); - - // After wake-up from STOP reconfigure the PLL - SetSysClock(); -} - -#endif diff --git a/targets/TARGET_STM/TARGET_STM32F3/common_objects.h b/targets/TARGET_STM/TARGET_STM32F3/common_objects.h index fd235f0781f..3f9c4c838f1 100644 --- a/targets/TARGET_STM/TARGET_STM32F3/common_objects.h +++ b/targets/TARGET_STM/TARGET_STM32F3/common_objects.h @@ -89,5 +89,8 @@ struct serial_s { } #endif +/* STM32F3 HAL doesn't provide this API called in rtc_api.c */ +#define __HAL_RCC_RTC_CLKPRESCALER(__RTCCLKSource__) + #endif diff --git a/targets/TARGET_STM/TARGET_STM32F3/rtc_api.c b/targets/TARGET_STM/TARGET_STM32F3/rtc_api.c deleted file mode 100644 index c82d6f1ec0a..00000000000 --- a/targets/TARGET_STM/TARGET_STM32F3/rtc_api.c +++ /dev/null @@ -1,296 +0,0 @@ -/* mbed Microcontroller Library - ******************************************************************************* - * Copyright (c) 2016, 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: - * - * 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 "rtc_api.h" -#include "rtc_api_hal.h" - -#if DEVICE_RTC - -#include "mbed_error.h" - -#if RTC_LSI - static int rtc_inited = 0; -#endif - -static RTC_HandleTypeDef RtcHandle; - -#if RTC_LSI - #define RTC_CLOCK LSI_VALUE -#else - #define RTC_CLOCK LSE_VALUE -#endif - -#if DEVICE_LOWPOWERTIMER - #define RTC_ASYNCH_PREDIV ((RTC_CLOCK - 1) / 0x8000) - #define RTC_SYNCH_PREDIV (RTC_CLOCK / (RTC_ASYNCH_PREDIV + 1) - 1) -#else - #define RTC_ASYNCH_PREDIV (0x007F) - #define RTC_SYNCH_PREDIV (RTC_CLOCK / (RTC_ASYNCH_PREDIV + 1) - 1) -#endif - -#if DEVICE_LOWPOWERTIMER - static void (*irq_handler)(void); - static void RTC_IRQHandler(); -#endif - -void rtc_init(void) -{ - RCC_OscInitTypeDef RCC_OscInitStruct; - -#if RTC_LSI - if (rtc_inited) return; - rtc_inited = 1; -#endif - - RtcHandle.Instance = RTC; - -#if !RTC_LSI - // Enable LSE Oscillator - RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE; - RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; /* Mandatory, otherwise the PLL is reconfigured! */ - RCC_OscInitStruct.LSEState = RCC_LSE_ON; /* External 32.768 kHz clock on OSC_IN/OSC_OUT */ - if (HAL_RCC_OscConfig(&RCC_OscInitStruct) == HAL_OK) { - // Connect LSE to RTC - __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSE); - } else { - error("RTC error: LSE clock initialization failed."); - } -#else - // Enable Power clock - __PWR_CLK_ENABLE(); - - // Enable access to Backup domain - HAL_PWR_EnableBkUpAccess(); - - // Reset Backup domain - __HAL_RCC_BACKUPRESET_FORCE(); - __HAL_RCC_BACKUPRESET_RELEASE(); - - // Enable LSI clock - RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE; - RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; // Mandatory, otherwise the PLL is reconfigured! - RCC_OscInitStruct.LSEState = RCC_LSE_OFF; - RCC_OscInitStruct.LSIState = RCC_LSI_ON; - if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { - error("RTC error: LSI clock initialization failed."); - } - // Connect LSI to RTC - __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSI); -#endif - - // Enable RTC - __HAL_RCC_RTC_ENABLE(); - - RtcHandle.Init.HourFormat = RTC_HOURFORMAT_24; - RtcHandle.Init.AsynchPrediv = RTC_ASYNCH_PREDIV; - RtcHandle.Init.SynchPrediv = RTC_SYNCH_PREDIV; - RtcHandle.Init.OutPut = RTC_OUTPUT_DISABLE; - RtcHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH; - RtcHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN; - - if (HAL_RTC_Init(&RtcHandle) != HAL_OK) { - error("RTC error: RTC initialization failed."); - } - -#if DEVICE_LOWPOWERTIMER -#if RTC_LSI - rtc_write(0); -#else - if (!rtc_isenabled()) { - rtc_write(0); - } -#endif - NVIC_ClearPendingIRQ(RTC_WKUP_IRQn); - NVIC_DisableIRQ(RTC_WKUP_IRQn); - NVIC_SetVector(RTC_WKUP_IRQn, (uint32_t)RTC_IRQHandler); - NVIC_EnableIRQ(RTC_WKUP_IRQn); -#endif -} - -void rtc_free(void) -{ -#if RTC_LSI - // Enable Power clock - __PWR_CLK_ENABLE(); - - // Enable access to Backup domain - HAL_PWR_EnableBkUpAccess(); - - // Reset Backup domain - __HAL_RCC_BACKUPRESET_FORCE(); - __HAL_RCC_BACKUPRESET_RELEASE(); - - // Disable access to Backup domain - HAL_PWR_DisableBkUpAccess(); -#endif - - // Disable LSI and LSE clocks - RCC_OscInitTypeDef RCC_OscInitStruct; - RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE; - RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; - RCC_OscInitStruct.LSIState = RCC_LSI_OFF; - RCC_OscInitStruct.LSEState = RCC_LSE_OFF; - HAL_RCC_OscConfig(&RCC_OscInitStruct); - -#if RTC_LSI - rtc_inited = 0; -#endif -} - -int rtc_isenabled(void) -{ -#if RTC_LSI - return rtc_inited; -#else - if ((RTC->ISR & RTC_ISR_INITS) == RTC_ISR_INITS) return 1; - else return 0; -#endif -} - -/* - RTC Registers - RTC_WeekDay 1=monday, 2=tuesday, ..., 7=sunday - RTC_Month 1=january, 2=february, ..., 12=december - RTC_Date day of the month 1-31 - RTC_Year year 0-99 - struct tm - tm_sec seconds after the minute 0-61 - tm_min minutes after the hour 0-59 - tm_hour hours since midnight 0-23 - tm_mday day of the month 1-31 - tm_mon months since January 0-11 - tm_year years since 1900 - tm_wday days since Sunday 0-6 - tm_yday days since January 1 0-365 - tm_isdst Daylight Saving Time flag -*/ -time_t rtc_read(void) -{ - RTC_DateTypeDef dateStruct; - RTC_TimeTypeDef timeStruct; - struct tm timeinfo; - - RtcHandle.Instance = RTC; - - // Read actual date and time - // Warning: the time must be read first! - HAL_RTC_GetTime(&RtcHandle, &timeStruct, FORMAT_BIN); - HAL_RTC_GetDate(&RtcHandle, &dateStruct, FORMAT_BIN); - - // Setup a tm structure based on the RTC - timeinfo.tm_wday = dateStruct.WeekDay; - timeinfo.tm_mon = dateStruct.Month - 1; - timeinfo.tm_mday = dateStruct.Date; - timeinfo.tm_year = dateStruct.Year + 68; - timeinfo.tm_hour = timeStruct.Hours; - timeinfo.tm_min = timeStruct.Minutes; - timeinfo.tm_sec = timeStruct.Seconds; - // Daylight Saving Time information is not available - timeinfo.tm_isdst = -1; - - // Convert to timestamp - time_t t = mktime(&timeinfo); - - return t; -} - -void rtc_write(time_t t) -{ - RTC_DateTypeDef dateStruct; - RTC_TimeTypeDef timeStruct; - - RtcHandle.Instance = RTC; - - // Convert the time into a tm - struct tm *timeinfo = localtime(&t); - - // Fill RTC structures - dateStruct.WeekDay = timeinfo->tm_wday; - dateStruct.Month = timeinfo->tm_mon + 1; - dateStruct.Date = timeinfo->tm_mday; - dateStruct.Year = timeinfo->tm_year - 68; - timeStruct.Hours = timeinfo->tm_hour; - timeStruct.Minutes = timeinfo->tm_min; - timeStruct.Seconds = timeinfo->tm_sec; - timeStruct.TimeFormat = RTC_HOURFORMAT_24; - timeStruct.DayLightSaving = RTC_DAYLIGHTSAVING_NONE; - timeStruct.StoreOperation = RTC_STOREOPERATION_RESET; - - // Change the RTC current date/time - HAL_RTC_SetDate(&RtcHandle, &dateStruct, FORMAT_BIN); - HAL_RTC_SetTime(&RtcHandle, &timeStruct, FORMAT_BIN); -} - -#if DEVICE_LOWPOWERTIMER - -static void RTC_IRQHandler() -{ - HAL_RTCEx_WakeUpTimerIRQHandler(&RtcHandle); -} - -void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc) -{ - if (irq_handler) { - // Fire the user callback - irq_handler(); - } -} - -void rtc_set_irq_handler(uint32_t handler) -{ - irq_handler = (void (*)(void))handler; -} - -uint32_t rtc_read_subseconds(void) -{ - return 1000000.f * ((double)(RTC_SYNCH_PREDIV - RTC->SSR) / (RTC_SYNCH_PREDIV + 1)); -} - -void rtc_set_wake_up_timer(uint32_t delta) -{ - uint32_t wake_up_counter = delta / (2000000 / RTC_CLOCK); - - if (HAL_RTCEx_SetWakeUpTimer_IT(&RtcHandle, wake_up_counter, - RTC_WAKEUPCLOCK_RTCCLK_DIV2) != HAL_OK) { - error("Set wake up timer failed\n"); - } -} - -void rtc_deactivate_wake_up_timer(void) -{ - HAL_RTCEx_DeactivateWakeUpTimer(&RtcHandle); -} - -void rtc_synchronize(void) -{ - HAL_RTC_WaitForSynchro(&RtcHandle); -} -#endif // DEVICE_LOWPOWERTIMER - -#endif diff --git a/targets/TARGET_STM/TARGET_STM32F3/rtc_api_hal.h b/targets/TARGET_STM/TARGET_STM32F3/rtc_api_hal.h deleted file mode 100644 index 14a345438d3..00000000000 --- a/targets/TARGET_STM/TARGET_STM32F3/rtc_api_hal.h +++ /dev/null @@ -1,79 +0,0 @@ -/* mbed Microcontroller Library -******************************************************************************* -* Copyright (c) 2016, 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: -* -* 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. -******************************************************************************* -*/ - -#ifndef MBED_RTC_API_HAL_H -#define MBED_RTC_API_HAL_H - -#include -#include "rtc_api.h" - -#ifdef __cplusplus -extern "C" { -#endif -/* - * Extend rtc_api.h - */ - -/** Set the given function as handler of wakeup timer event. - * - * @param handler The function to set as handler - */ -void rtc_set_irq_handler(uint32_t handler); - -/** Read the subsecond register. - * - * @return The remaining time as microseconds (0-999999) - */ -uint32_t rtc_read_subseconds(void); - -/** Program a wake up timer event in delta microseconds. - * - * @param delta The time to wait - */ -void rtc_set_wake_up_timer(uint32_t delta); - -/** Disable the wake up timer event. - * - * The wake up timer use auto reload, you have to deactivate it manually. - */ -void rtc_deactivate_wake_up_timer(void); - -/** Synchronise the RTC shadow registers. - * - * Must be called after a deepsleep. - */ -void rtc_synchronize(void); - - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/targets/TARGET_STM/TARGET_STM32F4/lp_ticker.c b/targets/TARGET_STM/TARGET_STM32F4/lp_ticker.c deleted file mode 100644 index 4415f74c9c9..00000000000 --- a/targets/TARGET_STM/TARGET_STM32F4/lp_ticker.c +++ /dev/null @@ -1,83 +0,0 @@ -/* mbed Microcontroller Library - ******************************************************************************* - * Copyright (c) 2016, 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: - * - * 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 "device.h" - -#if DEVICE_LOWPOWERTIMER - -#include "ticker_api.h" -#include "lp_ticker_api.h" -#include "rtc_api.h" -#include "rtc_api_hal.h" - -static uint8_t lp_ticker_inited = 0; - -void lp_ticker_init(void) -{ - if (lp_ticker_inited) return; - lp_ticker_inited = 1; - - rtc_init(); - rtc_set_irq_handler((uint32_t) lp_ticker_irq_handler); -} - -uint32_t lp_ticker_read(void) -{ - uint32_t usecs; - time_t time; - - lp_ticker_init(); - - do { - time = rtc_read(); - usecs = rtc_read_subseconds(); - } while (time != rtc_read()); - - return (time * 1000000) + usecs; -} - -void lp_ticker_set_interrupt(timestamp_t timestamp) -{ - uint32_t delta; - - delta = timestamp - lp_ticker_read(); - rtc_set_wake_up_timer(delta); -} - -void lp_ticker_disable_interrupt(void) -{ - rtc_deactivate_wake_up_timer(); -} - -void lp_ticker_clear_interrupt(void) -{ - -} - -#endif \ No newline at end of file diff --git a/targets/TARGET_STM/TARGET_STM32F4/rtc_api.c b/targets/TARGET_STM/TARGET_STM32F4/rtc_api.c deleted file mode 100644 index 6e126d0ce2e..00000000000 --- a/targets/TARGET_STM/TARGET_STM32F4/rtc_api.c +++ /dev/null @@ -1,298 +0,0 @@ -/* mbed Microcontroller Library - ******************************************************************************* - * Copyright (c) 2016, 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: - * - * 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 "rtc_api.h" -#include "rtc_api_hal.h" - -#if DEVICE_RTC - -#include "mbed_error.h" - -#if RTC_LSI -static int rtc_inited = 0; -#endif - -static RTC_HandleTypeDef RtcHandle; - -#if RTC_LSI - #define RTC_CLOCK LSI_VALUE -#else - #define RTC_CLOCK LSE_VALUE -#endif - -#if DEVICE_LOWPOWERTIMER - #define RTC_ASYNCH_PREDIV ((RTC_CLOCK - 1) / 0x8000) - #define RTC_SYNCH_PREDIV (RTC_CLOCK / (RTC_ASYNCH_PREDIV + 1) - 1) -#else - #define RTC_ASYNCH_PREDIV (0x007F) - #define RTC_SYNCH_PREDIV (RTC_CLOCK / (RTC_ASYNCH_PREDIV + 1) - 1) -#endif - -#if DEVICE_LOWPOWERTIMER - static void (*irq_handler)(void); - static void RTC_IRQHandler(); -#endif - -void rtc_init(void) -{ - RCC_OscInitTypeDef RCC_OscInitStruct; - -#if RTC_LSI - if (rtc_inited) return; - rtc_inited = 1; -#endif - - RtcHandle.Instance = RTC; - -#if !RTC_LSI - // Enable LSE Oscillator - RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE; - RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; /* Mandatory, otherwise the PLL is reconfigured! */ - RCC_OscInitStruct.LSEState = RCC_LSE_ON; /* External 32.768 kHz clock on OSC_IN/OSC_OUT */ - if (HAL_RCC_OscConfig(&RCC_OscInitStruct) == HAL_OK) { - // Connect LSE to RTC - __HAL_RCC_RTC_CLKPRESCALER(RCC_RTCCLKSOURCE_LSE); - __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSE); - } else { - error("RTC error: LSE clock initialization failed."); - } -#else - // Enable Power clock - __PWR_CLK_ENABLE(); - - // Enable access to Backup domain - HAL_PWR_EnableBkUpAccess(); - - // Reset Backup domain - __HAL_RCC_BACKUPRESET_FORCE(); - __HAL_RCC_BACKUPRESET_RELEASE(); - - // Enable LSI clock - RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE; - RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; // Mandatory, otherwise the PLL is reconfigured! - RCC_OscInitStruct.LSEState = RCC_LSE_OFF; - RCC_OscInitStruct.LSIState = RCC_LSI_ON; - if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { - error("RTC error: LSI clock initialization failed."); - } - // Connect LSI to RTC - __HAL_RCC_RTC_CLKPRESCALER(RCC_RTCCLKSOURCE_LSI); - __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSI); -#endif - - // Enable RTC - __HAL_RCC_RTC_ENABLE(); - - RtcHandle.Init.HourFormat = RTC_HOURFORMAT_24; - RtcHandle.Init.AsynchPrediv = RTC_ASYNCH_PREDIV; - RtcHandle.Init.SynchPrediv = RTC_SYNCH_PREDIV; - RtcHandle.Init.OutPut = RTC_OUTPUT_DISABLE; - RtcHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH; - RtcHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN; - - if (HAL_RTC_Init(&RtcHandle) != HAL_OK) { - error("RTC error: RTC initialization failed."); - } - -#if DEVICE_LOWPOWERTIMER -#if RTC_LSI - rtc_write(0); -#else - if (!rtc_isenabled()) { - rtc_write(0); - } -#endif - NVIC_ClearPendingIRQ(RTC_WKUP_IRQn); - NVIC_DisableIRQ(RTC_WKUP_IRQn); - NVIC_SetVector(RTC_WKUP_IRQn, (uint32_t)RTC_IRQHandler); - NVIC_EnableIRQ(RTC_WKUP_IRQn); -#endif -} - -void rtc_free(void) -{ -#if RTC_LSI - // Enable Power clock - __PWR_CLK_ENABLE(); - - // Enable access to Backup domain - HAL_PWR_EnableBkUpAccess(); - - // Reset Backup domain - __HAL_RCC_BACKUPRESET_FORCE(); - __HAL_RCC_BACKUPRESET_RELEASE(); - - // Disable access to Backup domain - HAL_PWR_DisableBkUpAccess(); -#endif - - // Disable LSI and LSE clocks - RCC_OscInitTypeDef RCC_OscInitStruct; - RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE; - RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; - RCC_OscInitStruct.LSIState = RCC_LSI_OFF; - RCC_OscInitStruct.LSEState = RCC_LSE_OFF; - HAL_RCC_OscConfig(&RCC_OscInitStruct); - -#if RTC_LSI - rtc_inited = 0; -#endif -} - -int rtc_isenabled(void) -{ -#if RTC_LSI - return rtc_inited; -#else - if ((RTC->ISR & RTC_ISR_INITS) == RTC_ISR_INITS) return 1; - else return 0; -#endif -} - -/* - RTC Registers - RTC_WeekDay 1=monday, 2=tuesday, ..., 7=sunday - RTC_Month 1=january, 2=february, ..., 12=december - RTC_Date day of the month 1-31 - RTC_Year year 0-99 - struct tm - tm_sec seconds after the minute 0-61 - tm_min minutes after the hour 0-59 - tm_hour hours since midnight 0-23 - tm_mday day of the month 1-31 - tm_mon months since January 0-11 - tm_year years since 1900 - tm_wday days since Sunday 0-6 - tm_yday days since January 1 0-365 - tm_isdst Daylight Saving Time flag -*/ -time_t rtc_read(void) -{ - RTC_DateTypeDef dateStruct; - RTC_TimeTypeDef timeStruct; - struct tm timeinfo; - - RtcHandle.Instance = RTC; - - // Read actual date and time - // Warning: the time must be read first! - HAL_RTC_GetTime(&RtcHandle, &timeStruct, FORMAT_BIN); - HAL_RTC_GetDate(&RtcHandle, &dateStruct, FORMAT_BIN); - - // Setup a tm structure based on the RTC - timeinfo.tm_wday = dateStruct.WeekDay; - timeinfo.tm_mon = dateStruct.Month - 1; - timeinfo.tm_mday = dateStruct.Date; - timeinfo.tm_year = dateStruct.Year + 68; - timeinfo.tm_hour = timeStruct.Hours; - timeinfo.tm_min = timeStruct.Minutes; - timeinfo.tm_sec = timeStruct.Seconds; - // Daylight Saving Time information is not available - timeinfo.tm_isdst = -1; - - // Convert to timestamp - time_t t = mktime(&timeinfo); - - return t; -} - -void rtc_write(time_t t) -{ - RTC_DateTypeDef dateStruct; - RTC_TimeTypeDef timeStruct; - - RtcHandle.Instance = RTC; - - // Convert the time into a tm - struct tm *timeinfo = localtime(&t); - - // Fill RTC structures - dateStruct.WeekDay = timeinfo->tm_wday; - dateStruct.Month = timeinfo->tm_mon + 1; - dateStruct.Date = timeinfo->tm_mday; - dateStruct.Year = timeinfo->tm_year - 68; - timeStruct.Hours = timeinfo->tm_hour; - timeStruct.Minutes = timeinfo->tm_min; - timeStruct.Seconds = timeinfo->tm_sec; - timeStruct.TimeFormat = RTC_HOURFORMAT_24; - timeStruct.DayLightSaving = RTC_DAYLIGHTSAVING_NONE; - timeStruct.StoreOperation = RTC_STOREOPERATION_RESET; - - // Change the RTC current date/time - HAL_RTC_SetDate(&RtcHandle, &dateStruct, FORMAT_BIN); - HAL_RTC_SetTime(&RtcHandle, &timeStruct, FORMAT_BIN); -} - -#if DEVICE_LOWPOWERTIMER - -static void RTC_IRQHandler() -{ - HAL_RTCEx_WakeUpTimerIRQHandler(&RtcHandle); -} - -void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc) -{ - if (irq_handler) { - // Fire the user callback - irq_handler(); - } -} - -void rtc_set_irq_handler(uint32_t handler) -{ - irq_handler = (void (*)(void))handler; -} - -uint32_t rtc_read_subseconds(void) -{ - return 1000000.f * ((double)(RTC_SYNCH_PREDIV - RTC->SSR) / (RTC_SYNCH_PREDIV + 1)); -} - -void rtc_set_wake_up_timer(uint32_t delta) -{ - uint32_t wake_up_counter = delta / (2000000 / RTC_CLOCK); - - if (HAL_RTCEx_SetWakeUpTimer_IT(&RtcHandle, wake_up_counter, - RTC_WAKEUPCLOCK_RTCCLK_DIV2) != HAL_OK) { - error("Set wake up timer failed\n"); - } -} - -void rtc_deactivate_wake_up_timer(void) -{ - HAL_RTCEx_DeactivateWakeUpTimer(&RtcHandle); -} - -void rtc_synchronize(void) -{ - HAL_RTC_WaitForSynchro(&RtcHandle); -} -#endif // DEVICE_LOWPOWERTIMER - -#endif diff --git a/targets/TARGET_STM/TARGET_STM32F4/rtc_api_hal.h b/targets/TARGET_STM/TARGET_STM32F4/rtc_api_hal.h deleted file mode 100644 index 7954d406335..00000000000 --- a/targets/TARGET_STM/TARGET_STM32F4/rtc_api_hal.h +++ /dev/null @@ -1,79 +0,0 @@ -/* mbed Microcontroller Library -******************************************************************************* -* Copyright (c) 2016, 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: -* -* 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. -******************************************************************************* -*/ - -#ifndef MBED_RTC_API_HAL_H -#define MBED_RTC_API_HAL_H - -#include -#include "rtc_api.h" - -#ifdef __cplusplus -extern "C" { -#endif -/* - * Extend rtc_api.h - */ - -/** Set the given function as handler of wakeup timer event. - * - * @param handler The function to set as handler - */ -void rtc_set_irq_handler(uint32_t handler); - -/** Read the subsecond register. - * - * @return The remaining time as microseconds (0-999999) - */ -uint32_t rtc_read_subseconds(void); - -/** Program a wake up timer event in delta microseconds. - * - * @param delta The time to wait - */ -void rtc_set_wake_up_timer(uint32_t delta); - -/** Disable the wake up timer event. - * - * The wake up timer use auto reload, you have to deactivate it manually. - */ -void rtc_deactivate_wake_up_timer(void); - -/** Synchronise the RTC shadow registers. - * - * Must be called after a deepsleep. - */ -void rtc_synchronize(void); - - -#ifdef __cplusplus -} -#endif - -#endif \ No newline at end of file diff --git a/targets/TARGET_STM/TARGET_STM32F4/sleep.c b/targets/TARGET_STM/TARGET_STM32F4/sleep.c deleted file mode 100644 index d14fc098fb7..00000000000 --- a/targets/TARGET_STM/TARGET_STM32F4/sleep.c +++ /dev/null @@ -1,60 +0,0 @@ -/* mbed Microcontroller Library - ******************************************************************************* - * Copyright (c) 2014, 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: - * - * 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 "sleep_api.h" -#include "rtc_api_hal.h" - -#if DEVICE_SLEEP - -#include "cmsis.h" - - -void sleep(void) { - // Stop HAL systick - HAL_SuspendTick(); - // Request to enter SLEEP mode - HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI); - // Restart HAL systick - HAL_ResumeTick(); -} - -void deepsleep(void) -{ - // Request to enter STOP mode with regulator in low power mode - HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI); - - // After wake-up from STOP reconfigure the PLL - SetSysClock(); - -#if DEVICE_LOWPOWERTIMER - rtc_synchronize(); -#endif -} - -#endif diff --git a/targets/TARGET_STM/TARGET_STM32F7/lp_ticker.c b/targets/TARGET_STM/TARGET_STM32F7/lp_ticker.c deleted file mode 100644 index 4415f74c9c9..00000000000 --- a/targets/TARGET_STM/TARGET_STM32F7/lp_ticker.c +++ /dev/null @@ -1,83 +0,0 @@ -/* mbed Microcontroller Library - ******************************************************************************* - * Copyright (c) 2016, 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: - * - * 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 "device.h" - -#if DEVICE_LOWPOWERTIMER - -#include "ticker_api.h" -#include "lp_ticker_api.h" -#include "rtc_api.h" -#include "rtc_api_hal.h" - -static uint8_t lp_ticker_inited = 0; - -void lp_ticker_init(void) -{ - if (lp_ticker_inited) return; - lp_ticker_inited = 1; - - rtc_init(); - rtc_set_irq_handler((uint32_t) lp_ticker_irq_handler); -} - -uint32_t lp_ticker_read(void) -{ - uint32_t usecs; - time_t time; - - lp_ticker_init(); - - do { - time = rtc_read(); - usecs = rtc_read_subseconds(); - } while (time != rtc_read()); - - return (time * 1000000) + usecs; -} - -void lp_ticker_set_interrupt(timestamp_t timestamp) -{ - uint32_t delta; - - delta = timestamp - lp_ticker_read(); - rtc_set_wake_up_timer(delta); -} - -void lp_ticker_disable_interrupt(void) -{ - rtc_deactivate_wake_up_timer(); -} - -void lp_ticker_clear_interrupt(void) -{ - -} - -#endif \ No newline at end of file diff --git a/targets/TARGET_STM/TARGET_STM32F7/rtc_api.c b/targets/TARGET_STM/TARGET_STM32F7/rtc_api.c deleted file mode 100644 index 40e7ede75f0..00000000000 --- a/targets/TARGET_STM/TARGET_STM32F7/rtc_api.c +++ /dev/null @@ -1,305 +0,0 @@ -/* mbed Microcontroller Library - ******************************************************************************* - * Copyright (c) 2015, 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: - * - * 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 "rtc_api.h" -#include "rtc_api_hal.h" - -#if DEVICE_RTC - -#include "mbed_error.h" - -#if RTC_LSI -static int rtc_inited = 0; -#endif - -static RTC_HandleTypeDef RtcHandle; - -#if RTC_LSI - #define RTC_CLOCK LSI_VALUE -#else - #define RTC_CLOCK LSE_VALUE -#endif - -#if DEVICE_LOWPOWERTIMER - #define RTC_ASYNCH_PREDIV ((RTC_CLOCK - 1) / 0x8000) - #define RTC_SYNCH_PREDIV (RTC_CLOCK / (RTC_ASYNCH_PREDIV + 1) - 1) -#else - #define RTC_ASYNCH_PREDIV (0x007F) - #define RTC_SYNCH_PREDIV (RTC_CLOCK / (RTC_ASYNCH_PREDIV + 1) - 1) -#endif - -#if DEVICE_LOWPOWERTIMER - static void (*irq_handler)(void); - static void RTC_IRQHandler(void); -#endif - -void rtc_init(void) -{ - RCC_OscInitTypeDef RCC_OscInitStruct; - -#if RTC_LSI - if (rtc_inited) return; - rtc_inited = 1; -#endif - - RtcHandle.Instance = RTC; - -#if !RTC_LSI - // Enable LSE Oscillator - RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE; - RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; // Mandatory, otherwise the PLL is reconfigured! - RCC_OscInitStruct.LSEState = RCC_LSE_ON; // External 32.768 kHz clock on OSC_IN/OSC_OUT - RCC_OscInitStruct.LSIState = RCC_LSI_OFF; - if (HAL_RCC_OscConfig(&RCC_OscInitStruct) == HAL_OK) { // Check if LSE has started correctly - // Connect LSE to RTC - __HAL_RCC_RTC_CLKPRESCALER(RCC_RTCCLKSOURCE_LSE); - __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSE); - } else { - error("Cannot initialize RTC with LSE\n"); - } -#else - // Enable Power clock - __PWR_CLK_ENABLE(); - - // Enable access to Backup domain - HAL_PWR_EnableBkUpAccess(); - - // Reset Backup domain - __HAL_RCC_BACKUPRESET_FORCE(); - __HAL_RCC_BACKUPRESET_RELEASE(); - - // Enable LSI clock - RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE; - RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; // Mandatory, otherwise the PLL is reconfigured! - RCC_OscInitStruct.LSEState = RCC_LSE_OFF; - RCC_OscInitStruct.LSIState = RCC_LSI_ON; - if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { - error("Cannot initialize RTC with LSI\n"); - } - // Connect LSI to RTC - __HAL_RCC_RTC_CLKPRESCALER(RCC_RTCCLKSOURCE_LSI); - __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSI); -#endif - - // Enable RTC - __HAL_RCC_RTC_ENABLE(); - - RtcHandle.Init.HourFormat = RTC_HOURFORMAT_24; - RtcHandle.Init.AsynchPrediv = RTC_ASYNCH_PREDIV; - RtcHandle.Init.SynchPrediv = RTC_SYNCH_PREDIV; - RtcHandle.Init.OutPut = RTC_OUTPUT_DISABLE; - RtcHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH; - RtcHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN; - - if (HAL_RTC_Init(&RtcHandle) != HAL_OK) { - error("RTC error: RTC initialization failed."); - } - -#if DEVICE_LOWPOWERTIMER -#if RTC_LSI - rtc_write(0); -#else - if (!rtc_isenabled()) { - rtc_write(0); - } -#endif - NVIC_ClearPendingIRQ(RTC_WKUP_IRQn); - NVIC_DisableIRQ(RTC_WKUP_IRQn); - NVIC_SetVector(RTC_WKUP_IRQn, (uint32_t)RTC_IRQHandler); - NVIC_EnableIRQ(RTC_WKUP_IRQn); -#endif -} - -void rtc_free(void) -{ -#if RTC_LSI - // Enable Power clock - __PWR_CLK_ENABLE(); - - // Enable access to Backup domain - HAL_PWR_EnableBkUpAccess(); - - // Reset Backup domain - __HAL_RCC_BACKUPRESET_FORCE(); - __HAL_RCC_BACKUPRESET_RELEASE(); - - // Disable access to Backup domain - HAL_PWR_DisableBkUpAccess(); -#endif - - // Disable LSI and LSE clocks - RCC_OscInitTypeDef RCC_OscInitStruct; - RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE; - RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; - RCC_OscInitStruct.LSIState = RCC_LSI_OFF; - RCC_OscInitStruct.LSEState = RCC_LSE_OFF; - HAL_RCC_OscConfig(&RCC_OscInitStruct); - -#if RTC_LSI - rtc_inited = 0; -#endif -} - -int rtc_isenabled(void) -{ -#if RTC_LSI - return rtc_inited; -#else - if ((RTC->ISR & RTC_ISR_INITS) == RTC_ISR_INITS) { - return 1; - } else { - return 0; - } -#endif -} - -/* - RTC Registers - RTC_WeekDay 1=monday, 2=tuesday, ..., 7=sunday - RTC_Month 1=january, 2=february, ..., 12=december - RTC_Date day of the month 1-31 - RTC_Year year 0-99 - struct tm - tm_sec seconds after the minute 0-61 - tm_min minutes after the hour 0-59 - tm_hour hours since midnight 0-23 - tm_mday day of the month 1-31 - tm_mon months since January 0-11 - tm_year years since 1900 - tm_wday days since Sunday 0-6 - tm_yday days since January 1 0-365 - tm_isdst Daylight Saving Time flag -*/ -time_t rtc_read(void) -{ - RTC_DateTypeDef dateStruct; - RTC_TimeTypeDef timeStruct; - struct tm timeinfo; - - RtcHandle.Instance = RTC; - - // Read actual date and time - // Warning: the time must be read first! - HAL_RTC_GetTime(&RtcHandle, &timeStruct, FORMAT_BIN); - HAL_RTC_GetDate(&RtcHandle, &dateStruct, FORMAT_BIN); - - // Setup a tm structure based on the RTC - timeinfo.tm_wday = dateStruct.WeekDay; - timeinfo.tm_mon = dateStruct.Month - 1; - timeinfo.tm_mday = dateStruct.Date; - timeinfo.tm_year = dateStruct.Year + 68; - timeinfo.tm_hour = timeStruct.Hours; - timeinfo.tm_min = timeStruct.Minutes; - timeinfo.tm_sec = timeStruct.Seconds; - // Daylight Saving Time information is not available - timeinfo.tm_isdst = -1; - - // Convert to timestamp - time_t t = mktime(&timeinfo); - - return t; -} - -void rtc_write(time_t t) -{ - RTC_DateTypeDef dateStruct; - RTC_TimeTypeDef timeStruct; - - RtcHandle.Instance = RTC; - - // Enable write access to Backup domain - HAL_PWR_EnableBkUpAccess(); - - // Convert the time into a tm - struct tm *timeinfo = localtime(&t); - - // Fill RTC structures - dateStruct.WeekDay = timeinfo->tm_wday; - dateStruct.Month = timeinfo->tm_mon + 1; - dateStruct.Date = timeinfo->tm_mday; - dateStruct.Year = timeinfo->tm_year - 68; - timeStruct.Hours = timeinfo->tm_hour; - timeStruct.Minutes = timeinfo->tm_min; - timeStruct.Seconds = timeinfo->tm_sec; - timeStruct.TimeFormat = RTC_HOURFORMAT_24; - timeStruct.DayLightSaving = RTC_DAYLIGHTSAVING_NONE; - timeStruct.StoreOperation = RTC_STOREOPERATION_RESET; - - // Change the RTC current date/time - HAL_RTC_SetDate(&RtcHandle, &dateStruct, FORMAT_BIN); - HAL_RTC_SetTime(&RtcHandle, &timeStruct, FORMAT_BIN); -} - -#if DEVICE_LOWPOWERTIMER - -static void RTC_IRQHandler(void) -{ - HAL_RTCEx_WakeUpTimerIRQHandler(&RtcHandle); -} - -void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc) -{ - if (irq_handler) { - // Fire the user callback - irq_handler(); - } -} - -void rtc_set_irq_handler(uint32_t handler) -{ - irq_handler = (void (*)(void))handler; -} - -uint32_t rtc_read_subseconds(void) -{ - return 1000000.f * ((double)(RTC_SYNCH_PREDIV - RTC->SSR) / (RTC_SYNCH_PREDIV + 1)); -} - -void rtc_set_wake_up_timer(uint32_t delta) -{ - uint32_t wake_up_counter = delta / (2000000 / RTC_CLOCK); - - if (HAL_RTCEx_SetWakeUpTimer_IT(&RtcHandle, wake_up_counter, - RTC_WAKEUPCLOCK_RTCCLK_DIV2) != HAL_OK) { - error("Set wake up timer failed\n"); - } -} - -void rtc_deactivate_wake_up_timer(void) -{ - HAL_RTCEx_DeactivateWakeUpTimer(&RtcHandle); -} - -void rtc_synchronize(void) -{ - HAL_RTC_WaitForSynchro(&RtcHandle); -} -#endif // DEVICE_LOWPOWERTIMER - -#endif diff --git a/targets/TARGET_STM/TARGET_STM32F7/rtc_api_hal.h b/targets/TARGET_STM/TARGET_STM32F7/rtc_api_hal.h deleted file mode 100644 index 14a345438d3..00000000000 --- a/targets/TARGET_STM/TARGET_STM32F7/rtc_api_hal.h +++ /dev/null @@ -1,79 +0,0 @@ -/* mbed Microcontroller Library -******************************************************************************* -* Copyright (c) 2016, 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: -* -* 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. -******************************************************************************* -*/ - -#ifndef MBED_RTC_API_HAL_H -#define MBED_RTC_API_HAL_H - -#include -#include "rtc_api.h" - -#ifdef __cplusplus -extern "C" { -#endif -/* - * Extend rtc_api.h - */ - -/** Set the given function as handler of wakeup timer event. - * - * @param handler The function to set as handler - */ -void rtc_set_irq_handler(uint32_t handler); - -/** Read the subsecond register. - * - * @return The remaining time as microseconds (0-999999) - */ -uint32_t rtc_read_subseconds(void); - -/** Program a wake up timer event in delta microseconds. - * - * @param delta The time to wait - */ -void rtc_set_wake_up_timer(uint32_t delta); - -/** Disable the wake up timer event. - * - * The wake up timer use auto reload, you have to deactivate it manually. - */ -void rtc_deactivate_wake_up_timer(void); - -/** Synchronise the RTC shadow registers. - * - * Must be called after a deepsleep. - */ -void rtc_synchronize(void); - - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/targets/TARGET_STM/TARGET_STM32F7/sleep.c b/targets/TARGET_STM/TARGET_STM32F7/sleep.c deleted file mode 100644 index d5a69116240..00000000000 --- a/targets/TARGET_STM/TARGET_STM32F7/sleep.c +++ /dev/null @@ -1,59 +0,0 @@ -/* mbed Microcontroller Library - ******************************************************************************* - * Copyright (c) 2015, 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: - * - * 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 "sleep_api.h" -#include "rtc_api_hal.h" - -#if DEVICE_SLEEP - -#include "cmsis.h" - -void sleep(void) { - // Stop HAL systick - HAL_SuspendTick(); - // Request to enter SLEEP mode - HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI); - // Restart HAL systick - HAL_ResumeTick(); -} - -void deepsleep(void) -{ - // Request to enter STOP mode with regulator in low power mode - HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI); - - // After wake-up from STOP need to reconfigure the system clock - SetSysClock(); - -#if DEVICE_LOWPOWERTIMER - rtc_synchronize(); -#endif -} - -#endif diff --git a/targets/TARGET_STM/TARGET_STM32L0/common_objects.h b/targets/TARGET_STM/TARGET_STM32L0/common_objects.h index fd235f0781f..82f2f0f2658 100644 --- a/targets/TARGET_STM/TARGET_STM32L0/common_objects.h +++ b/targets/TARGET_STM/TARGET_STM32L0/common_objects.h @@ -89,5 +89,8 @@ struct serial_s { } #endif +/* STM32L0 HAL doesn't provide this API called in rtc_api.c */ +#define RTC_WKUP_IRQn RTC_IRQn + #endif diff --git a/targets/TARGET_STM/TARGET_STM32L0/lp_ticker.c b/targets/TARGET_STM/TARGET_STM32L0/lp_ticker.c deleted file mode 100644 index 4415f74c9c9..00000000000 --- a/targets/TARGET_STM/TARGET_STM32L0/lp_ticker.c +++ /dev/null @@ -1,83 +0,0 @@ -/* mbed Microcontroller Library - ******************************************************************************* - * Copyright (c) 2016, 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: - * - * 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 "device.h" - -#if DEVICE_LOWPOWERTIMER - -#include "ticker_api.h" -#include "lp_ticker_api.h" -#include "rtc_api.h" -#include "rtc_api_hal.h" - -static uint8_t lp_ticker_inited = 0; - -void lp_ticker_init(void) -{ - if (lp_ticker_inited) return; - lp_ticker_inited = 1; - - rtc_init(); - rtc_set_irq_handler((uint32_t) lp_ticker_irq_handler); -} - -uint32_t lp_ticker_read(void) -{ - uint32_t usecs; - time_t time; - - lp_ticker_init(); - - do { - time = rtc_read(); - usecs = rtc_read_subseconds(); - } while (time != rtc_read()); - - return (time * 1000000) + usecs; -} - -void lp_ticker_set_interrupt(timestamp_t timestamp) -{ - uint32_t delta; - - delta = timestamp - lp_ticker_read(); - rtc_set_wake_up_timer(delta); -} - -void lp_ticker_disable_interrupt(void) -{ - rtc_deactivate_wake_up_timer(); -} - -void lp_ticker_clear_interrupt(void) -{ - -} - -#endif \ No newline at end of file diff --git a/targets/TARGET_STM/TARGET_STM32L0/rtc_api_hal.h b/targets/TARGET_STM/TARGET_STM32L0/rtc_api_hal.h deleted file mode 100644 index 14a345438d3..00000000000 --- a/targets/TARGET_STM/TARGET_STM32L0/rtc_api_hal.h +++ /dev/null @@ -1,79 +0,0 @@ -/* mbed Microcontroller Library -******************************************************************************* -* Copyright (c) 2016, 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: -* -* 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. -******************************************************************************* -*/ - -#ifndef MBED_RTC_API_HAL_H -#define MBED_RTC_API_HAL_H - -#include -#include "rtc_api.h" - -#ifdef __cplusplus -extern "C" { -#endif -/* - * Extend rtc_api.h - */ - -/** Set the given function as handler of wakeup timer event. - * - * @param handler The function to set as handler - */ -void rtc_set_irq_handler(uint32_t handler); - -/** Read the subsecond register. - * - * @return The remaining time as microseconds (0-999999) - */ -uint32_t rtc_read_subseconds(void); - -/** Program a wake up timer event in delta microseconds. - * - * @param delta The time to wait - */ -void rtc_set_wake_up_timer(uint32_t delta); - -/** Disable the wake up timer event. - * - * The wake up timer use auto reload, you have to deactivate it manually. - */ -void rtc_deactivate_wake_up_timer(void); - -/** Synchronise the RTC shadow registers. - * - * Must be called after a deepsleep. - */ -void rtc_synchronize(void); - - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/targets/TARGET_STM/TARGET_STM32L0/sleep.c b/targets/TARGET_STM/TARGET_STM32L0/sleep.c deleted file mode 100644 index ad469cc8c33..00000000000 --- a/targets/TARGET_STM/TARGET_STM32L0/sleep.c +++ /dev/null @@ -1,60 +0,0 @@ -/* mbed Microcontroller Library - ******************************************************************************* - * Copyright (c) 2015, 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: - * - * 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 "sleep_api.h" -#include "rtc_api_hal.h" - -#if DEVICE_SLEEP - -#include "cmsis.h" - - -void sleep(void) { - // Stop HAL systick - HAL_SuspendTick(); - // Request to enter SLEEP mode - HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI); - // Restart HAL systick - HAL_ResumeTick(); -} - -void deepsleep(void) -{ - // Request to enter STOP mode with regulator in low power mode - HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI); - - // After wake-up from STOP reconfigure the PLL - SetSysClock(); - -#if DEVICE_LOWPOWERTIMER - rtc_synchronize(); -#endif -} - -#endif diff --git a/targets/TARGET_STM/TARGET_STM32L1/lp_ticker.c b/targets/TARGET_STM/TARGET_STM32L1/lp_ticker.c deleted file mode 100644 index 4415f74c9c9..00000000000 --- a/targets/TARGET_STM/TARGET_STM32L1/lp_ticker.c +++ /dev/null @@ -1,83 +0,0 @@ -/* mbed Microcontroller Library - ******************************************************************************* - * Copyright (c) 2016, 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: - * - * 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 "device.h" - -#if DEVICE_LOWPOWERTIMER - -#include "ticker_api.h" -#include "lp_ticker_api.h" -#include "rtc_api.h" -#include "rtc_api_hal.h" - -static uint8_t lp_ticker_inited = 0; - -void lp_ticker_init(void) -{ - if (lp_ticker_inited) return; - lp_ticker_inited = 1; - - rtc_init(); - rtc_set_irq_handler((uint32_t) lp_ticker_irq_handler); -} - -uint32_t lp_ticker_read(void) -{ - uint32_t usecs; - time_t time; - - lp_ticker_init(); - - do { - time = rtc_read(); - usecs = rtc_read_subseconds(); - } while (time != rtc_read()); - - return (time * 1000000) + usecs; -} - -void lp_ticker_set_interrupt(timestamp_t timestamp) -{ - uint32_t delta; - - delta = timestamp - lp_ticker_read(); - rtc_set_wake_up_timer(delta); -} - -void lp_ticker_disable_interrupt(void) -{ - rtc_deactivate_wake_up_timer(); -} - -void lp_ticker_clear_interrupt(void) -{ - -} - -#endif \ No newline at end of file diff --git a/targets/TARGET_STM/TARGET_STM32L1/rtc_api.c b/targets/TARGET_STM/TARGET_STM32L1/rtc_api.c deleted file mode 100755 index 612ff04c735..00000000000 --- a/targets/TARGET_STM/TARGET_STM32L1/rtc_api.c +++ /dev/null @@ -1,308 +0,0 @@ -/* mbed Microcontroller Library - ******************************************************************************* - * Copyright (c) 2016, 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: - * - * 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 "rtc_api.h" -#include "rtc_api_hal.h" - -#if DEVICE_RTC - -#include "mbed_error.h" - -#if RTC_LSI -static int rtc_inited = 0; -#endif - -static RTC_HandleTypeDef RtcHandle; - -#if RTC_LSI - #define RTC_CLOCK LSI_VALUE -#else - #define RTC_CLOCK LSE_VALUE -#endif - -#if DEVICE_LOWPOWERTIMER - #define RTC_ASYNCH_PREDIV ((RTC_CLOCK - 1) / 0x8000) - #define RTC_SYNCH_PREDIV (RTC_CLOCK / (RTC_ASYNCH_PREDIV + 1) - 1) -#else - #define RTC_ASYNCH_PREDIV (0x007F) - #define RTC_SYNCH_PREDIV (RTC_CLOCK / (RTC_ASYNCH_PREDIV + 1) - 1) -#endif - -#if DEVICE_LOWPOWERTIMER - static void (*irq_handler)(void); - static void RTC_IRQHandler(void); -#endif - -void rtc_init(void) -{ - RCC_OscInitTypeDef RCC_OscInitStruct; - -#if RTC_LSI - if (rtc_inited) return; - rtc_inited = 1; -#endif - - RtcHandle.Instance = RTC; - -#if !RTC_LSI - // Enable LSE Oscillator - RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE; - RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; // Mandatory, otherwise the PLL is reconfigured! - RCC_OscInitStruct.LSEState = RCC_LSE_ON; // External 32.768 kHz clock on OSC_IN/OSC_OUT - RCC_OscInitStruct.LSIState = RCC_LSI_OFF; - if (HAL_RCC_OscConfig(&RCC_OscInitStruct) == HAL_OK) { // Check if LSE has started correctly - // Connect LSE to RTC - __HAL_RCC_RTC_CLKPRESCALER(RCC_RTCCLKSOURCE_LSE); - __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSE); - } else { - error("Cannot initialize RTC with LSE\n"); - } -#else - // Enable Power clock - __PWR_CLK_ENABLE(); - - // Enable access to Backup domain - HAL_PWR_EnableBkUpAccess(); - - // Reset Backup domain - __HAL_RCC_BACKUPRESET_FORCE(); - __HAL_RCC_BACKUPRESET_RELEASE(); - - // Enable LSI clock - RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE; - RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; // Mandatory, otherwise the PLL is reconfigured! - RCC_OscInitStruct.LSEState = RCC_LSE_OFF; - RCC_OscInitStruct.LSIState = RCC_LSI_ON; - if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { - error("Cannot initialize RTC with LSI\n"); - } - // Connect LSI to RTC - __HAL_RCC_RTC_CLKPRESCALER(RCC_RTCCLKSOURCE_LSI); - __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSI); -#endif - - // Enable RTC - __HAL_RCC_RTC_ENABLE(); - - RtcHandle.Init.HourFormat = RTC_HOURFORMAT_24; -#ifdef TARGET_MOTE_L152RC - /* SubSecond resolution of 16384Hz */ - RtcHandle.Init.AsynchPrediv = 1; - RtcHandle.Init.SynchPrediv = (RTC_CLOCK / 2) - 1; -#else - RtcHandle.Init.AsynchPrediv = RTC_ASYNCH_PREDIV; - RtcHandle.Init.SynchPrediv = RTC_SYNCH_PREDIV; -#endif - RtcHandle.Init.OutPut = RTC_OUTPUT_DISABLE; - RtcHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH; - RtcHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN; - - if (HAL_RTC_Init(&RtcHandle) != HAL_OK) { - error("RTC error: RTC initialization failed."); - } - -#if DEVICE_LOWPOWERTIMER -#if RTC_LSI - rtc_write(0); -#else - if (!rtc_isenabled()) { - rtc_write(0); - } -#endif - NVIC_ClearPendingIRQ(RTC_WKUP_IRQn); - NVIC_DisableIRQ(RTC_WKUP_IRQn); - NVIC_SetVector(RTC_WKUP_IRQn, (uint32_t)RTC_IRQHandler); - NVIC_EnableIRQ(RTC_WKUP_IRQn); -#endif -} - -void rtc_free(void) -{ -#if RTC_LSI - // Enable Power clock - __PWR_CLK_ENABLE(); - - // Enable access to Backup domain - HAL_PWR_EnableBkUpAccess(); - - // Reset Backup domain - __HAL_RCC_BACKUPRESET_FORCE(); - __HAL_RCC_BACKUPRESET_RELEASE(); - - // Disable access to Backup domain - HAL_PWR_DisableBkUpAccess(); -#endif - - // Disable LSI and LSE clocks - RCC_OscInitTypeDef RCC_OscInitStruct; - RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE; - RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; - RCC_OscInitStruct.LSIState = RCC_LSI_OFF; - RCC_OscInitStruct.LSEState = RCC_LSE_OFF; - HAL_RCC_OscConfig(&RCC_OscInitStruct); - -#if RTC_LSI - rtc_inited = 0; -#endif -} - -int rtc_isenabled(void) -{ -#if RTC_LSI - return rtc_inited; -#else - if ((RTC->ISR & RTC_ISR_INITS) == RTC_ISR_INITS) { - return 1; - } else { - return 0; - } -#endif -} - -/* - RTC Registers - RTC_WeekDay 1=monday, 2=tuesday, ..., 7=sunday - RTC_Month 1=january, 2=february, ..., 12=december - RTC_Date day of the month 1-31 - RTC_Year year 0-99 - struct tm - tm_sec seconds after the minute 0-61 - tm_min minutes after the hour 0-59 - tm_hour hours since midnight 0-23 - tm_mday day of the month 1-31 - tm_mon months since January 0-11 - tm_year years since 1900 - tm_wday days since Sunday 0-6 - tm_yday days since January 1 0-365 - tm_isdst Daylight Saving Time flag -*/ -time_t rtc_read(void) -{ - RTC_DateTypeDef dateStruct; - RTC_TimeTypeDef timeStruct; - struct tm timeinfo; - - RtcHandle.Instance = RTC; - - // Read actual date and time - // Warning: the time must be read first! - HAL_RTC_GetTime(&RtcHandle, &timeStruct, FORMAT_BIN); - HAL_RTC_GetDate(&RtcHandle, &dateStruct, FORMAT_BIN); - - // Setup a tm structure based on the RTC - timeinfo.tm_wday = dateStruct.WeekDay; - timeinfo.tm_mon = dateStruct.Month - 1; - timeinfo.tm_mday = dateStruct.Date; - timeinfo.tm_year = dateStruct.Year + 68; - timeinfo.tm_hour = timeStruct.Hours; - timeinfo.tm_min = timeStruct.Minutes; - timeinfo.tm_sec = timeStruct.Seconds; - // Daylight Saving Time information is not available - timeinfo.tm_isdst = -1; - - // Convert to timestamp - time_t t = mktime(&timeinfo); - - return t; -} - -void rtc_write(time_t t) -{ - RTC_DateTypeDef dateStruct; - RTC_TimeTypeDef timeStruct; - - RtcHandle.Instance = RTC; - - // Convert the time into a tm - struct tm *timeinfo = localtime(&t); - - // Fill RTC structures - dateStruct.WeekDay = timeinfo->tm_wday; - dateStruct.Month = timeinfo->tm_mon + 1; - dateStruct.Date = timeinfo->tm_mday; - dateStruct.Year = timeinfo->tm_year - 68; - timeStruct.Hours = timeinfo->tm_hour; - timeStruct.Minutes = timeinfo->tm_min; - timeStruct.Seconds = timeinfo->tm_sec; - timeStruct.TimeFormat = RTC_HOURFORMAT_24; - timeStruct.DayLightSaving = RTC_DAYLIGHTSAVING_NONE; - timeStruct.StoreOperation = RTC_STOREOPERATION_RESET; - - // Change the RTC current date/time - HAL_RTC_SetDate(&RtcHandle, &dateStruct, FORMAT_BIN); - HAL_RTC_SetTime(&RtcHandle, &timeStruct, FORMAT_BIN); -} - -#if DEVICE_LOWPOWERTIMER - -static void RTC_IRQHandler(void) -{ - HAL_RTCEx_WakeUpTimerIRQHandler(&RtcHandle); -} - -void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc) -{ - if (irq_handler) { - // Fire the user callback - irq_handler(); - } -} - -void rtc_set_irq_handler(uint32_t handler) -{ - irq_handler = (void (*)(void))handler; -} - -uint32_t rtc_read_subseconds(void) -{ - return 1000000.f * ((double)(RTC_SYNCH_PREDIV - RTC->SSR) / (RTC_SYNCH_PREDIV + 1)); -} - -void rtc_set_wake_up_timer(uint32_t delta) -{ - uint32_t wake_up_counter = delta / (2000000 / RTC_CLOCK); - - if (HAL_RTCEx_SetWakeUpTimer_IT(&RtcHandle, wake_up_counter, - RTC_WAKEUPCLOCK_RTCCLK_DIV2) != HAL_OK) { - error("Set wake up timer failed\n"); - } -} - -void rtc_deactivate_wake_up_timer(void) -{ - HAL_RTCEx_DeactivateWakeUpTimer(&RtcHandle); -} - -void rtc_synchronize(void) -{ - HAL_RTC_WaitForSynchro(&RtcHandle); -} -#endif // DEVICE_LOWPOWERTIMER - -#endif diff --git a/targets/TARGET_STM/TARGET_STM32L1/rtc_api_hal.h b/targets/TARGET_STM/TARGET_STM32L1/rtc_api_hal.h deleted file mode 100644 index 14a345438d3..00000000000 --- a/targets/TARGET_STM/TARGET_STM32L1/rtc_api_hal.h +++ /dev/null @@ -1,79 +0,0 @@ -/* mbed Microcontroller Library -******************************************************************************* -* Copyright (c) 2016, 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: -* -* 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. -******************************************************************************* -*/ - -#ifndef MBED_RTC_API_HAL_H -#define MBED_RTC_API_HAL_H - -#include -#include "rtc_api.h" - -#ifdef __cplusplus -extern "C" { -#endif -/* - * Extend rtc_api.h - */ - -/** Set the given function as handler of wakeup timer event. - * - * @param handler The function to set as handler - */ -void rtc_set_irq_handler(uint32_t handler); - -/** Read the subsecond register. - * - * @return The remaining time as microseconds (0-999999) - */ -uint32_t rtc_read_subseconds(void); - -/** Program a wake up timer event in delta microseconds. - * - * @param delta The time to wait - */ -void rtc_set_wake_up_timer(uint32_t delta); - -/** Disable the wake up timer event. - * - * The wake up timer use auto reload, you have to deactivate it manually. - */ -void rtc_deactivate_wake_up_timer(void); - -/** Synchronise the RTC shadow registers. - * - * Must be called after a deepsleep. - */ -void rtc_synchronize(void); - - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/targets/TARGET_STM/TARGET_STM32L1/sleep.c b/targets/TARGET_STM/TARGET_STM32L1/sleep.c deleted file mode 100644 index 99b6ef9ed9a..00000000000 --- a/targets/TARGET_STM/TARGET_STM32L1/sleep.c +++ /dev/null @@ -1,100 +0,0 @@ -/* mbed Microcontroller Library - ******************************************************************************* - * Copyright (c) 2014, 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: - * - * 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 "sleep_api.h" -#include "hal_tick.h" -#include "rtc_api_hal.h" - -#if DEVICE_SLEEP - -#include "cmsis.h" - -static TIM_HandleTypeDef TimMasterHandle; - -void sleep(void) { - // Stop HAL systick - HAL_SuspendTick(); - // Request to enter SLEEP mode - HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI); - // Restart HAL systick - HAL_ResumeTick(); -} - - -void deepsleep(void) -{ -#if defined(TARGET_MOTE_L152RC) - int8_t STOPEntry = PWR_STOPENTRY_WFI; -#endif - - // Stop HAL systick - TimMasterHandle.Instance = TIM_MST; - -#if defined(TARGET_MOTE_L152RC) - /* Select the regulator state in Stop mode: Set PDDS and LPSDSR bit according to PWR_Regulator value */ - MODIFY_REG(PWR->CR, (PWR_CR_PDDS | PWR_CR_LPSDSR), PWR_LOWPOWERREGULATOR_ON); - - /* Set SLEEPDEEP bit of Cortex System Control Register */ - SET_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SLEEPDEEP_Msk)); - - /* Select Stop mode entry --------------------------------------------------*/ - if(STOPEntry == PWR_STOPENTRY_WFI) - { - /* Request Wait For Interrupt */ - __WFI(); - } - else - { - /* Request Wait For Event */ - __SEV(); - __WFE(); - __WFE(); - } - __NOP(); - __NOP(); - __NOP(); - /* Reset SLEEPDEEP bit of Cortex System Control Register */ - CLEAR_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SLEEPDEEP_Msk)); -#else - // Request to enter STOP mode with regulator in low power mode - HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI); -#endif - - // After wake-up from STOP reconfigure the PLL - SetSysClock(); - - // Restart HAL systick - HAL_ResumeTick(); - -#if DEVICE_LOWPOWERTIMER - rtc_synchronize(); -#endif -} - -#endif diff --git a/targets/TARGET_STM/TARGET_STM32L4/common_objects.h b/targets/TARGET_STM/TARGET_STM32L4/common_objects.h index fd235f0781f..fe83786dc8d 100644 --- a/targets/TARGET_STM/TARGET_STM32L4/common_objects.h +++ b/targets/TARGET_STM/TARGET_STM32L4/common_objects.h @@ -89,5 +89,8 @@ struct serial_s { } #endif +/* STM32L4 HAL doesn't provide this API called in rtc_api.c */ +#define __HAL_RCC_RTC_CLKPRESCALER(__RTCCLKSource__) + #endif diff --git a/targets/TARGET_STM/TARGET_STM32L4/lp_ticker.c b/targets/TARGET_STM/TARGET_STM32L4/lp_ticker.c deleted file mode 100644 index 4415f74c9c9..00000000000 --- a/targets/TARGET_STM/TARGET_STM32L4/lp_ticker.c +++ /dev/null @@ -1,83 +0,0 @@ -/* mbed Microcontroller Library - ******************************************************************************* - * Copyright (c) 2016, 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: - * - * 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 "device.h" - -#if DEVICE_LOWPOWERTIMER - -#include "ticker_api.h" -#include "lp_ticker_api.h" -#include "rtc_api.h" -#include "rtc_api_hal.h" - -static uint8_t lp_ticker_inited = 0; - -void lp_ticker_init(void) -{ - if (lp_ticker_inited) return; - lp_ticker_inited = 1; - - rtc_init(); - rtc_set_irq_handler((uint32_t) lp_ticker_irq_handler); -} - -uint32_t lp_ticker_read(void) -{ - uint32_t usecs; - time_t time; - - lp_ticker_init(); - - do { - time = rtc_read(); - usecs = rtc_read_subseconds(); - } while (time != rtc_read()); - - return (time * 1000000) + usecs; -} - -void lp_ticker_set_interrupt(timestamp_t timestamp) -{ - uint32_t delta; - - delta = timestamp - lp_ticker_read(); - rtc_set_wake_up_timer(delta); -} - -void lp_ticker_disable_interrupt(void) -{ - rtc_deactivate_wake_up_timer(); -} - -void lp_ticker_clear_interrupt(void) -{ - -} - -#endif \ No newline at end of file diff --git a/targets/TARGET_STM/TARGET_STM32L4/rtc_api.c b/targets/TARGET_STM/TARGET_STM32L4/rtc_api.c deleted file mode 100644 index c34d806f714..00000000000 --- a/targets/TARGET_STM/TARGET_STM32L4/rtc_api.c +++ /dev/null @@ -1,310 +0,0 @@ -/* mbed Microcontroller Library - ******************************************************************************* - * Copyright (c) 2015, 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: - * - * 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 "rtc_api.h" -#include "rtc_api_hal.h" - -#if DEVICE_RTC - -#include "mbed_error.h" - -#if RTC_LSI -static int rtc_inited = 0; -#endif - -static RTC_HandleTypeDef RtcHandle; - -#if RTC_LSI - #define RTC_CLOCK LSI_VALUE -#else - #define RTC_CLOCK LSE_VALUE -#endif - -#if DEVICE_LOWPOWERTIMER - #define RTC_ASYNCH_PREDIV ((RTC_CLOCK - 1) / 0x8000) - #define RTC_SYNCH_PREDIV (RTC_CLOCK / (RTC_ASYNCH_PREDIV + 1) - 1) -#else - #define RTC_ASYNCH_PREDIV (0x007F) - #define RTC_SYNCH_PREDIV (RTC_CLOCK / (RTC_ASYNCH_PREDIV + 1) - 1) -#endif - -#if DEVICE_LOWPOWERTIMER - static void (*irq_handler)(void); - static void RTC_IRQHandler(void); -#endif - -void rtc_init(void) -{ - RCC_OscInitTypeDef RCC_OscInitStruct = {0}; - RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0}; - -#if RTC_LSI - if (rtc_inited) return; - rtc_inited = 1; -#endif - - RtcHandle.Instance = RTC; - -#if !RTC_LSI - // Enable LSE Oscillator - RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE; - RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; // Mandatory, otherwise the PLL is reconfigured! - RCC_OscInitStruct.LSEState = RCC_LSE_ON; // External 32.768 kHz clock on OSC_IN/OSC_OUT - RCC_OscInitStruct.LSIState = RCC_LSI_OFF; - if (HAL_RCC_OscConfig(&RCC_OscInitStruct) == HAL_OK) { // Check if LSE has started correctly - // Connect LSE to RTC - PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC; - PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE; - HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct); - } else { - error("Cannot initialize RTC with LSE\n"); - } -#else - // Enable Power clock - __HAL_RCC_PWR_CLK_ENABLE(); - - // Enable access to Backup domain - HAL_PWR_EnableBkUpAccess(); - - // Reset Backup domain - __HAL_RCC_BACKUPRESET_FORCE(); - __HAL_RCC_BACKUPRESET_RELEASE(); - - // Enable LSI clock - RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE; - RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; // Mandatory, otherwise the PLL is reconfigured! - RCC_OscInitStruct.LSEState = RCC_LSE_OFF; - RCC_OscInitStruct.LSIState = RCC_LSI_ON; - if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { - error("Cannot initialize RTC with LSI\n"); - } - // Connect LSI to RTC - PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC; - PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI; - if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) { - error("Cannot initialize RTC with LSI\n"); - } -#endif - - // Check if RTC is already initialized - if ((RTC->ISR & RTC_ISR_INITS) == RTC_ISR_INITS) return; - - // Enable RTC - __HAL_RCC_RTC_ENABLE(); - - RtcHandle.Init.HourFormat = RTC_HOURFORMAT_24; - RtcHandle.Init.AsynchPrediv = RTC_ASYNCH_PREDIV; - RtcHandle.Init.SynchPrediv = RTC_SYNCH_PREDIV; - RtcHandle.Init.OutPut = RTC_OUTPUT_DISABLE; - RtcHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH; - RtcHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN; - - if (HAL_RTC_Init(&RtcHandle) != HAL_OK) { - error("Cannot initialize RTC\n"); - } - -#if DEVICE_LOWPOWERTIMER -#if RTC_LSI - rtc_write(0); -#else - if (!rtc_isenabled()) { - rtc_write(0); - } -#endif - NVIC_ClearPendingIRQ(RTC_WKUP_IRQn); - NVIC_DisableIRQ(RTC_WKUP_IRQn); - NVIC_SetVector(RTC_WKUP_IRQn, (uint32_t)RTC_IRQHandler); - NVIC_EnableIRQ(RTC_WKUP_IRQn); -#endif -} - -void rtc_free(void) -{ -#if RTC_LSI - // Enable Power clock - __HAL_RCC_PWR_CLK_ENABLE(); - - // Enable access to Backup domain - HAL_PWR_EnableBkUpAccess(); - - // Reset Backup domain - __HAL_RCC_BACKUPRESET_FORCE(); - __HAL_RCC_BACKUPRESET_RELEASE(); - - // Disable access to Backup domain - HAL_PWR_DisableBkUpAccess(); -#endif - - // Disable LSI and LSE clocks - RCC_OscInitTypeDef RCC_OscInitStruct; - RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE; - RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; - RCC_OscInitStruct.LSIState = RCC_LSI_OFF; - RCC_OscInitStruct.LSEState = RCC_LSE_OFF; - HAL_RCC_OscConfig(&RCC_OscInitStruct); - -#if RTC_LSI - rtc_inited = 0; -#endif -} - -int rtc_isenabled(void) -{ -#if RTC_LSI - return rtc_inited; -#else - if ((RTC->ISR & RTC_ISR_INITS) == RTC_ISR_INITS) { - return 1; - } else { - return 0; - } -#endif -} - -/* - RTC Registers - RTC_WeekDay 1=monday, 2=tuesday, ..., 7=sunday - RTC_Month 1=january, 2=february, ..., 12=december - RTC_Date day of the month 1-31 - RTC_Year year 0-99 - struct tm - tm_sec seconds after the minute 0-61 - tm_min minutes after the hour 0-59 - tm_hour hours since midnight 0-23 - tm_mday day of the month 1-31 - tm_mon months since January 0-11 - tm_year years since 1900 - tm_wday days since Sunday 0-6 - tm_yday days since January 1 0-365 - tm_isdst Daylight Saving Time flag -*/ -time_t rtc_read(void) -{ - RTC_DateTypeDef dateStruct; - RTC_TimeTypeDef timeStruct; - struct tm timeinfo; - - RtcHandle.Instance = RTC; - - // Read actual date and time - // Warning: the time must be read first! - HAL_RTC_GetTime(&RtcHandle, &timeStruct, RTC_FORMAT_BIN); - HAL_RTC_GetDate(&RtcHandle, &dateStruct, RTC_FORMAT_BIN); - - // Setup a tm structure based on the RTC - timeinfo.tm_wday = dateStruct.WeekDay; - timeinfo.tm_mon = dateStruct.Month - 1; - timeinfo.tm_mday = dateStruct.Date; - timeinfo.tm_year = dateStruct.Year + 68; - timeinfo.tm_hour = timeStruct.Hours; - timeinfo.tm_min = timeStruct.Minutes; - timeinfo.tm_sec = timeStruct.Seconds; - // Daylight Saving Time information is not available - timeinfo.tm_isdst = -1; - - // Convert to timestamp - time_t t = mktime(&timeinfo); - - return t; -} - -void rtc_write(time_t t) -{ - RTC_DateTypeDef dateStruct; - RTC_TimeTypeDef timeStruct; - - RtcHandle.Instance = RTC; - - // Convert the time into a tm - struct tm *timeinfo = localtime(&t); - - // Fill RTC structures - dateStruct.WeekDay = timeinfo->tm_wday; - dateStruct.Month = timeinfo->tm_mon + 1; - dateStruct.Date = timeinfo->tm_mday; - dateStruct.Year = timeinfo->tm_year - 68; - timeStruct.Hours = timeinfo->tm_hour; - timeStruct.Minutes = timeinfo->tm_min; - timeStruct.Seconds = timeinfo->tm_sec; - timeStruct.TimeFormat = RTC_HOURFORMAT_24; - timeStruct.DayLightSaving = RTC_DAYLIGHTSAVING_NONE; - timeStruct.StoreOperation = RTC_STOREOPERATION_RESET; - - // Change the RTC current date/time - HAL_RTC_SetDate(&RtcHandle, &dateStruct, RTC_FORMAT_BIN); - HAL_RTC_SetTime(&RtcHandle, &timeStruct, RTC_FORMAT_BIN); -} - -#if DEVICE_LOWPOWERTIMER - -static void RTC_IRQHandler(void) -{ - HAL_RTCEx_WakeUpTimerIRQHandler(&RtcHandle); -} - -void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc) -{ - if (irq_handler) { - // Fire the user callback - irq_handler(); - } -} - -void rtc_set_irq_handler(uint32_t handler) -{ - irq_handler = (void (*)(void))handler; -} - -uint32_t rtc_read_subseconds(void) -{ - return 1000000.f * ((double)(RTC_SYNCH_PREDIV - RTC->SSR) / (RTC_SYNCH_PREDIV + 1)); -} - -void rtc_set_wake_up_timer(uint32_t delta) -{ - uint32_t wake_up_counter = delta / (2000000 / RTC_CLOCK); - - if (HAL_RTCEx_SetWakeUpTimer_IT(&RtcHandle, wake_up_counter, - RTC_WAKEUPCLOCK_RTCCLK_DIV2) != HAL_OK) { - error("Set wake up timer failed\n"); - } -} - -void rtc_deactivate_wake_up_timer(void) -{ - HAL_RTCEx_DeactivateWakeUpTimer(&RtcHandle); -} - -void rtc_synchronize(void) -{ - HAL_RTC_WaitForSynchro(&RtcHandle); -} -#endif // DEVICE_LOWPOWERTIMER - -#endif diff --git a/targets/TARGET_STM/TARGET_STM32L4/rtc_api_hal.h b/targets/TARGET_STM/TARGET_STM32L4/rtc_api_hal.h deleted file mode 100644 index 14a345438d3..00000000000 --- a/targets/TARGET_STM/TARGET_STM32L4/rtc_api_hal.h +++ /dev/null @@ -1,79 +0,0 @@ -/* mbed Microcontroller Library -******************************************************************************* -* Copyright (c) 2016, 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: -* -* 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. -******************************************************************************* -*/ - -#ifndef MBED_RTC_API_HAL_H -#define MBED_RTC_API_HAL_H - -#include -#include "rtc_api.h" - -#ifdef __cplusplus -extern "C" { -#endif -/* - * Extend rtc_api.h - */ - -/** Set the given function as handler of wakeup timer event. - * - * @param handler The function to set as handler - */ -void rtc_set_irq_handler(uint32_t handler); - -/** Read the subsecond register. - * - * @return The remaining time as microseconds (0-999999) - */ -uint32_t rtc_read_subseconds(void); - -/** Program a wake up timer event in delta microseconds. - * - * @param delta The time to wait - */ -void rtc_set_wake_up_timer(uint32_t delta); - -/** Disable the wake up timer event. - * - * The wake up timer use auto reload, you have to deactivate it manually. - */ -void rtc_deactivate_wake_up_timer(void); - -/** Synchronise the RTC shadow registers. - * - * Must be called after a deepsleep. - */ -void rtc_synchronize(void); - - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/targets/TARGET_STM/TARGET_STM32L4/sleep.c b/targets/TARGET_STM/TARGET_STM32L4/sleep.c deleted file mode 100644 index 542cdc768f5..00000000000 --- a/targets/TARGET_STM/TARGET_STM32L4/sleep.c +++ /dev/null @@ -1,75 +0,0 @@ -/* mbed Microcontroller Library - ******************************************************************************* - * Copyright (c) 2015, 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: - * - * 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 "sleep_api.h" -#include "rtc_api_hal.h" - -#if DEVICE_SLEEP - -#include "cmsis.h" - -void sleep(void) { - // Stop HAL systick - HAL_SuspendTick(); - // Request to enter SLEEP mode - HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI); - // Restart HAL systick - HAL_ResumeTick(); -} - -void deepsleep(void) -{ - // Stop HAL systick - HAL_SuspendTick(); - - // Request to enter STOP mode 1 with regulator in low power mode - if (__HAL_RCC_PWR_IS_CLK_ENABLED()) { - HAL_PWREx_EnableLowPowerRunMode(); - HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI); - HAL_PWREx_DisableLowPowerRunMode(); - } else { - __HAL_RCC_PWR_CLK_ENABLE(); - HAL_PWREx_EnableLowPowerRunMode(); - HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI); - HAL_PWREx_DisableLowPowerRunMode(); - __HAL_RCC_PWR_CLK_DISABLE(); - } - - // After wake-up from STOP reconfigure the PLL - SetSysClock(); - - // Restart HAL systick - HAL_ResumeTick(); - -#if DEVICE_LOWPOWERTIMER - rtc_synchronize(); -#endif -} - -#endif diff --git a/targets/TARGET_STM/TARGET_STM32F3/lp_ticker.c b/targets/TARGET_STM/lp_ticker.c similarity index 97% rename from targets/TARGET_STM/TARGET_STM32F3/lp_ticker.c rename to targets/TARGET_STM/lp_ticker.c index 564905fa9ce..0f0ba90353d 100644 --- a/targets/TARGET_STM/TARGET_STM32F3/lp_ticker.c +++ b/targets/TARGET_STM/lp_ticker.c @@ -42,23 +42,23 @@ void lp_ticker_init(void) { if (lp_ticker_inited) return; lp_ticker_inited = 1; - + rtc_init(); rtc_set_irq_handler((uint32_t) lp_ticker_irq_handler); } uint32_t lp_ticker_read(void) { - uint32_t usecs; - time_t time; + uint32_t usecs = 0; + time_t time = 0; lp_ticker_init(); - + do { time = rtc_read(); usecs = rtc_read_subseconds(); } while (time != rtc_read()); - + return (time * 1000000) + usecs; } @@ -77,7 +77,7 @@ void lp_ticker_disable_interrupt(void) void lp_ticker_clear_interrupt(void) { - + } #endif diff --git a/targets/TARGET_STM/TARGET_STM32L0/rtc_api.c b/targets/TARGET_STM/rtc_api.c similarity index 84% rename from targets/TARGET_STM/TARGET_STM32L0/rtc_api.c rename to targets/TARGET_STM/rtc_api.c index 82f4c055e01..15a1d0b31cd 100644 --- a/targets/TARGET_STM/TARGET_STM32L0/rtc_api.c +++ b/targets/TARGET_STM/rtc_api.c @@ -27,17 +27,12 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************* */ -#include "rtc_api.h" -#include "rtc_api_hal.h" - #if DEVICE_RTC +#include "rtc_api.h" +#include "rtc_api_hal.h" #include "mbed_error.h" -#if RTC_LSI -static int rtc_inited = 0; -#endif - static RTC_HandleTypeDef RtcHandle; #if RTC_LSI @@ -51,7 +46,7 @@ static RTC_HandleTypeDef RtcHandle; #define RTC_SYNCH_PREDIV (RTC_CLOCK / (RTC_ASYNCH_PREDIV + 1) - 1) #else #define RTC_ASYNCH_PREDIV (0x007F) - #define RTC_SYNCH_PREDIV (RTC_CLOCK / (RTC_ASYNCH_PREDIV + 1) - 1) + #define RTC_SYNCH_PREDIV (RTC_CLOCK / (RTC_ASYNCH_PREDIV + 1) - 1) #endif #if DEVICE_LOWPOWERTIMER @@ -64,83 +59,97 @@ void rtc_init(void) RCC_OscInitTypeDef RCC_OscInitStruct; RCC_PeriphCLKInitTypeDef PeriphClkInitStruct; -#if RTC_LSI - if (rtc_inited) return; - rtc_inited = 1; -#endif - - RtcHandle.Instance = RTC; - - // Note : Due to a change inside stm32l0xx_hal_rcc.c (v1.2 to v1.5) the bit DBP of the register - // PWR_CR is now reset by the fonction HAL_RCC_OscConfig(). - // Enable Power clock - __PWR_CLK_ENABLE(); - // Enable access to Backup domain HAL_PWR_EnableBkUpAccess(); + RtcHandle.Instance = RTC; + #if !RTC_LSI - // Enable LSE Oscillator RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; // Mandatory, otherwise the PLL is reconfigured! - RCC_OscInitStruct.LSEState = RCC_LSE_ON; // External 32.768 kHz clock on OSC_IN/OSC_OUT - if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { - error("Cannot initialize RTC with LSE\n"); + RCC_OscInitStruct.LSEState = RCC_LSE_ON; + RCC_OscInitStruct.LSIState = RCC_LSI_OFF; + + if (HAL_RCC_OscConfig(&RCC_OscInitStruct) == HAL_OK) + { + __HAL_RCC_RTC_CLKPRESCALER(RCC_RTCCLKSOURCE_LSE); + __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSE); } - // Connect LSE to RTC + else + { + error("Cannot initialize RTC with LSE\n"); + } + PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC; PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE; - if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) { - error("Cannot initialize RTC with LSI\n"); + if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) + { + error("PeriphClkInitStruct RTC failed with LSE\n"); } -#else + +#else /* !RTC_LSI */ + + __PWR_CLK_ENABLE(); + // Reset Backup domain __HAL_RCC_BACKUPRESET_FORCE(); __HAL_RCC_BACKUPRESET_RELEASE(); // Enable LSI clock - RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE; + RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; // Mandatory, otherwise the PLL is reconfigured! RCC_OscInitStruct.LSEState = RCC_LSE_OFF; RCC_OscInitStruct.LSIState = RCC_LSI_ON; - if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { - error("RTC error: LSI clock initialization failed."); + if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) + { + error("Cannot initialize RTC with LSI\n"); } - // Connect LSI to RTC + + __HAL_RCC_RTC_CLKPRESCALER(RCC_RTCCLKSOURCE_LSI); + __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSI); + PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC; PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI; - if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) { - error("Cannot initialize RTC with LSI\n"); + if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) + { + error("PeriphClkInitStruct RTC failed with LSI\n"); } -#endif + +#endif /* !RTC_LSI */ // Enable RTC __HAL_RCC_RTC_ENABLE(); +#if TARGET_STM32F1 + RtcHandle.Init.AsynchPrediv = RTC_AUTO_1_SECOND; +#else /* TARGET_STM32F1 */ RtcHandle.Init.HourFormat = RTC_HOURFORMAT_24; RtcHandle.Init.AsynchPrediv = RTC_ASYNCH_PREDIV; RtcHandle.Init.SynchPrediv = RTC_SYNCH_PREDIV; RtcHandle.Init.OutPut = RTC_OUTPUT_DISABLE; RtcHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH; RtcHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN; +#endif /* TARGET_STM32F1 */ if (HAL_RTC_Init(&RtcHandle) != HAL_OK) { error("RTC error: RTC initialization failed."); } #if DEVICE_LOWPOWERTIMER -#if RTC_LSI - rtc_write(0); -#else - if (!rtc_isenabled()) { + +#if !RTC_LSI + if (!rtc_isenabled()) +#endif /* !RTC_LSI */ + { rtc_write(0); } -#endif - NVIC_ClearPendingIRQ(RTC_IRQn); - NVIC_DisableIRQ(RTC_IRQn); - NVIC_SetVector(RTC_IRQn, (uint32_t)RTC_IRQHandler); - NVIC_EnableIRQ(RTC_IRQn); -#endif + + NVIC_ClearPendingIRQ(RTC_WKUP_IRQn); + NVIC_DisableIRQ(RTC_WKUP_IRQn); + NVIC_SetVector(RTC_WKUP_IRQn, (uint32_t)RTC_IRQHandler); + NVIC_EnableIRQ(RTC_WKUP_IRQn); + +#endif /* DEVICE_LOWPOWERTIMER */ } void rtc_free(void) @@ -167,23 +176,6 @@ void rtc_free(void) RCC_OscInitStruct.LSIState = RCC_LSI_OFF; RCC_OscInitStruct.LSEState = RCC_LSE_OFF; HAL_RCC_OscConfig(&RCC_OscInitStruct); - -#if RTC_LSI - rtc_inited = 0; -#endif -} - -int rtc_isenabled(void) -{ -#if RTC_LSI - return rtc_inited; -#else - if ((RTC->ISR & RTC_ISR_INITS) == RTC_ISR_INITS) { - return 1; - } else { - return 0; - } -#endif } /* @@ -225,7 +217,7 @@ time_t rtc_read(void) timeinfo.tm_min = timeStruct.Minutes; timeinfo.tm_sec = timeStruct.Seconds; // Daylight Saving Time information is not available - timeinfo.tm_isdst = -1; + timeinfo.tm_isdst = -1; // Convert to timestamp time_t t = mktime(&timeinfo); @@ -251,15 +243,34 @@ void rtc_write(time_t t) timeStruct.Hours = timeinfo->tm_hour; timeStruct.Minutes = timeinfo->tm_min; timeStruct.Seconds = timeinfo->tm_sec; + +#if !(TARGET_STM32F1) timeStruct.TimeFormat = RTC_HOURFORMAT_24; timeStruct.DayLightSaving = RTC_DAYLIGHTSAVING_NONE; timeStruct.StoreOperation = RTC_STOREOPERATION_RESET; +#endif /* TARGET_STM32F1 */ // Change the RTC current date/time HAL_RTC_SetDate(&RtcHandle, &dateStruct, FORMAT_BIN); HAL_RTC_SetTime(&RtcHandle, &timeStruct, FORMAT_BIN); } +int rtc_isenabled(void) +{ +#if DEVICE_LOWPOWERTIMER + if ((RTC->ISR & RTC_ISR_INITS) == RTC_ISR_INITS) + { + return 1; + } + else + { + return 0; + } +#else /* DEVICE_LOWPOWERTIMER */ + return 1; +#endif /* DEVICE_LOWPOWERTIMER */ +} + #if DEVICE_LOWPOWERTIMER static void RTC_IRQHandler(void) @@ -288,7 +299,7 @@ uint32_t rtc_read_subseconds(void) void rtc_set_wake_up_timer(uint32_t delta) { uint32_t wake_up_counter = delta / (2000000 / RTC_CLOCK); - + if (HAL_RTCEx_SetWakeUpTimer_IT(&RtcHandle, wake_up_counter, RTC_WAKEUPCLOCK_RTCCLK_DIV2) != HAL_OK) { error("Set wake up timer failed\n"); @@ -304,6 +315,7 @@ void rtc_synchronize(void) { HAL_RTC_WaitForSynchro(&RtcHandle); } -#endif // DEVICE_LOWPOWERTIMER +#endif /* DEVICE_LOWPOWERTIMER */ -#endif + +#endif /* DEVICE_RTC */ diff --git a/targets/TARGET_STM/TARGET_STM32F0/rtc_api_hal.h b/targets/TARGET_STM/rtc_api_hal.h similarity index 100% rename from targets/TARGET_STM/TARGET_STM32F0/rtc_api_hal.h rename to targets/TARGET_STM/rtc_api_hal.h diff --git a/targets/TARGET_STM/TARGET_STM32F3/sleep.c b/targets/TARGET_STM/sleep.c similarity index 89% rename from targets/TARGET_STM/TARGET_STM32F3/sleep.c rename to targets/TARGET_STM/sleep.c index 0c028275dff..efea541276d 100644 --- a/targets/TARGET_STM/TARGET_STM32F3/sleep.c +++ b/targets/TARGET_STM/sleep.c @@ -1,6 +1,6 @@ /* mbed Microcontroller Library ******************************************************************************* - * Copyright (c) 2014, STMicroelectronics + * Copyright (c) 2016, STMicroelectronics * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -35,7 +35,8 @@ #include "cmsis.h" -void sleep(void) { +void sleep(void) +{ // Stop HAL systick HAL_SuspendTick(); // Request to enter SLEEP mode @@ -44,15 +45,24 @@ void sleep(void) { HAL_ResumeTick(); } - void deepsleep(void) { + // Stop HAL systick + HAL_SuspendTick(); + // Request to enter STOP mode with regulator in low power mode +#if TARGET_STM32L4 + HAL_PWREx_EnterSTOP2Mode(PWR_STOPENTRY_WFI); +#else /* TARGET_STM32L4 */ HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI); +#endif /* TARGET_STM32L4 */ + + // Restart HAL systick + HAL_ResumeTick(); // After wake-up from STOP reconfigure the PLL SetSysClock(); - + #if DEVICE_LOWPOWERTIMER rtc_synchronize(); #endif From 51bebd5b538fe221bf39af106ebd9828d4c37342 Mon Sep 17 00:00:00 2001 From: jeromecoutant Date: Tue, 6 Dec 2016 09:54:24 +0100 Subject: [PATCH 2/2] Run astyle --- targets/TARGET_STM/lp_ticker.c | 4 +-- targets/TARGET_STM/rtc_api.c | 49 ++++++++++++++-------------------- 2 files changed, 22 insertions(+), 31 deletions(-) diff --git a/targets/TARGET_STM/lp_ticker.c b/targets/TARGET_STM/lp_ticker.c index 0f0ba90353d..c123793247a 100644 --- a/targets/TARGET_STM/lp_ticker.c +++ b/targets/TARGET_STM/lp_ticker.c @@ -55,8 +55,8 @@ uint32_t lp_ticker_read(void) lp_ticker_init(); do { - time = rtc_read(); - usecs = rtc_read_subseconds(); + time = rtc_read(); + usecs = rtc_read_subseconds(); } while (time != rtc_read()); return (time * 1000000) + usecs; diff --git a/targets/TARGET_STM/rtc_api.c b/targets/TARGET_STM/rtc_api.c index 15a1d0b31cd..acb3af96e82 100644 --- a/targets/TARGET_STM/rtc_api.c +++ b/targets/TARGET_STM/rtc_api.c @@ -36,22 +36,22 @@ static RTC_HandleTypeDef RtcHandle; #if RTC_LSI - #define RTC_CLOCK LSI_VALUE +#define RTC_CLOCK LSI_VALUE #else - #define RTC_CLOCK LSE_VALUE +#define RTC_CLOCK LSE_VALUE #endif #if DEVICE_LOWPOWERTIMER - #define RTC_ASYNCH_PREDIV ((RTC_CLOCK - 1) / 0x8000) - #define RTC_SYNCH_PREDIV (RTC_CLOCK / (RTC_ASYNCH_PREDIV + 1) - 1) +#define RTC_ASYNCH_PREDIV ((RTC_CLOCK - 1) / 0x8000) +#define RTC_SYNCH_PREDIV (RTC_CLOCK / (RTC_ASYNCH_PREDIV + 1) - 1) #else - #define RTC_ASYNCH_PREDIV (0x007F) - #define RTC_SYNCH_PREDIV (RTC_CLOCK / (RTC_ASYNCH_PREDIV + 1) - 1) +#define RTC_ASYNCH_PREDIV (0x007F) +#define RTC_SYNCH_PREDIV (RTC_CLOCK / (RTC_ASYNCH_PREDIV + 1) - 1) #endif #if DEVICE_LOWPOWERTIMER - static void (*irq_handler)(void); - static void RTC_IRQHandler(void); +static void (*irq_handler)(void); +static void RTC_IRQHandler(void); #endif void rtc_init(void) @@ -70,20 +70,16 @@ void rtc_init(void) RCC_OscInitStruct.LSEState = RCC_LSE_ON; RCC_OscInitStruct.LSIState = RCC_LSI_OFF; - if (HAL_RCC_OscConfig(&RCC_OscInitStruct) == HAL_OK) - { + if (HAL_RCC_OscConfig(&RCC_OscInitStruct) == HAL_OK) { __HAL_RCC_RTC_CLKPRESCALER(RCC_RTCCLKSOURCE_LSE); __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSE); - } - else - { - error("Cannot initialize RTC with LSE\n"); + } else { + error("Cannot initialize RTC with LSE\n"); } PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC; PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE; - if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) - { + if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) { error("PeriphClkInitStruct RTC failed with LSE\n"); } @@ -100,8 +96,7 @@ void rtc_init(void) RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; // Mandatory, otherwise the PLL is reconfigured! RCC_OscInitStruct.LSEState = RCC_LSE_OFF; RCC_OscInitStruct.LSIState = RCC_LSI_ON; - if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) - { + if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { error("Cannot initialize RTC with LSI\n"); } @@ -110,8 +105,7 @@ void rtc_init(void) PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC; PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI; - if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) - { + if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) { error("PeriphClkInitStruct RTC failed with LSI\n"); } @@ -258,16 +252,13 @@ void rtc_write(time_t t) int rtc_isenabled(void) { #if DEVICE_LOWPOWERTIMER - if ((RTC->ISR & RTC_ISR_INITS) == RTC_ISR_INITS) - { - return 1; - } - else - { - return 0; - } + if ((RTC->ISR & RTC_ISR_INITS) == RTC_ISR_INITS) { + return 1; + } else { + return 0; + } #else /* DEVICE_LOWPOWERTIMER */ - return 1; + return 1; #endif /* DEVICE_LOWPOWERTIMER */ }