mirror of https://github.com/ARMmbed/mbed-os.git
STM32: Refactor lp_ticker.c + rtc_api.c + sleep.c + rtc_api_hal.h files
parent
08509e75df
commit
ab28a5e5cb
|
@ -116,4 +116,8 @@ struct i2c_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
|
||||
|
|
|
@ -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
|
|
@ -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
|
|
@ -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
|
|
@ -116,5 +116,8 @@ struct i2c_s {
|
|||
}
|
||||
#endif
|
||||
|
||||
/* STM32F1 HAL doesn't provide this API called in rtc_api.c */
|
||||
#define __HAL_RCC_RTC_CLKPRESCALER(__RTCCLKSource__)
|
||||
|
||||
#endif
|
||||
|
||||
|
|
|
@ -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 + 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;
|
||||
|
||||
// Change the RTC current date/time
|
||||
HAL_RTC_SetDate(&RtcHandle, &dateStruct, FORMAT_BIN);
|
||||
HAL_RTC_SetTime(&RtcHandle, &timeStruct, FORMAT_BIN);
|
||||
}
|
||||
|
||||
#endif
|
|
@ -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
|
|
@ -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
|
|
@ -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
|
|
@ -116,5 +116,8 @@ struct i2c_s {
|
|||
}
|
||||
#endif
|
||||
|
||||
/* STM32F3 HAL doesn't provide this API called in rtc_api.c */
|
||||
#define __HAL_RCC_RTC_CLKPRESCALER(__RTCCLKSource__)
|
||||
|
||||
#endif
|
||||
|
||||
|
|
|
@ -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
|
|
@ -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 <stdint.h>
|
||||
#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
|
|
@ -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
|
|
@ -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
|
|
@ -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 <stdint.h>
|
||||
#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
|
|
@ -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
|
|
@ -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
|
|
@ -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
|
|
@ -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 <stdint.h>
|
||||
#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
|
|
@ -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
|
|
@ -116,5 +116,8 @@ struct i2c_s {
|
|||
}
|
||||
#endif
|
||||
|
||||
/* STM32L0 HAL doesn't provide this API called in rtc_api.c */
|
||||
#define RTC_WKUP_IRQn RTC_IRQn
|
||||
|
||||
#endif
|
||||
|
||||
|
|
|
@ -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
|
|
@ -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 <stdint.h>
|
||||
#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
|
|
@ -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
|
|
@ -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
|
|
@ -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
|
|
@ -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 <stdint.h>
|
||||
#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
|
|
@ -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
|
|
@ -116,5 +116,8 @@ struct i2c_s {
|
|||
}
|
||||
#endif
|
||||
|
||||
/* STM32L4 HAL doesn't provide this API called in rtc_api.c */
|
||||
#define __HAL_RCC_RTC_CLKPRESCALER(__RTCCLKSource__)
|
||||
|
||||
#endif
|
||||
|
||||
|
|
|
@ -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
|
|
@ -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
|
|
@ -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 <stdint.h>
|
||||
#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
|
|
@ -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
|
|
@ -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
|
|
@ -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.");
|
||||
}
|
||||
// Connect LSI to RTC
|
||||
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
|
||||
PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
|
||||
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) {
|
||||
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
|
||||
{
|
||||
error("Cannot initialize RTC with LSI\n");
|
||||
}
|
||||
#endif
|
||||
|
||||
__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("PeriphClkInitStruct RTC failed with LSI\n");
|
||||
}
|
||||
|
||||
#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 */
|
|
@ -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
|
Loading…
Reference in New Issue