NAMote72 Utility Application – Serial Terminal Monitor control for NAMote72 (note: this application replaces the previous na_mote1 test code application)
Dependencies: SX127x lib_gps lib_mma8451q lib_mpl3115a2 lib_sx9500 mbed
Fork of na_mote1 by
See wiki Page for a detailed
This is a link to the wiki page
Diff: rtc_alarm.c
- Revision:
- 10:a3e2c558c003
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rtc_alarm.c Thu Jun 25 22:29:28 2015 +0000 @@ -0,0 +1,166 @@ +/* mbed + * Copyright (c) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "rtc_alarm.h" +#include <stdio.h> + +/* + The MCU can be woken up from low-power mode by an RTC Alarm event, an RTC + Wakeup event, a tamper event, a time-stamp event, or a comparator event, + without depending on an external interrupt (Auto-wakeup mode). + +(+) RTC auto-wakeup (AWU) from the Stop mode + (++) To wake up from the Stop mode with an RTC alarm event, it is necessary to: + (+++) Configure the EXTI Line 17 to be sensitive to rising edges (Interrupt + or Event modes) and Enable the RTC Alarm Interrupt using the HAL_RTC_SetAlarm_IT() + function + (+++) Configure the RTC to generate the RTC alarm using the HAL_RTC_Init() + and HAL_RTC_SetTime() functions. + (++) To wake up from the Stop mode with an RTC Tamper or time stamp event, it + is necessary to: + (+++) Configure the EXTI Line 19 to be sensitive to rising edges (Interrupt or Event modes) and + Enable the RTC Tamper or time stamp Interrupt using the HAL_RTCEx_SetTamper_IT() + or HAL_RTCEx_SetTimeStamp_IT() functions. + (++) To wake up from the Stop mode with an RTC WakeUp event, it is necessary to: + (+++) Configure the EXTI Line 20 to be sensitive to rising edges (Interrupt or Event modes) and + Enable the RTC WakeUp Interrupt using the HAL_RTCEx_SetWakeUpTimer_IT() function. + (+++) Configure the RTC to generate the RTC WakeUp event using the HAL_RTCEx_SetWakeUpTimer() + function. +*/ + +extern RTC_HandleTypeDef RtcHandle; + +#if 0 +typedef struct +{ + RTC_TypeDef *Instance; /*!< Register base address */ + RTC_InitTypeDef Init; /*!< RTC required parameters */ + HAL_LockTypeDef Lock; /*!< RTC locking object */ + __IO HAL_RTCStateTypeDef State; /*!< Time communication state */ +} RTC_HandleTypeDef; + +HAL_RTCEx_WakeUpTimerIRQHandler(): + __HAL_RTC_WAKEUPTIMER_GET_IT(): + ->Instance->ISR + ->Instance->CR + HAL_RTCEx_WakeUpTimerEventCallback(): + (none) + __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(): + ->Instance->ISR + ->State + +HAL_RTCEx_SetWakeUpTimer_IT(): + __HAL_LOCK(): + ->Lock + ->State + __HAL_RTC_WRITEPROTECTION_DISABLE(): + __HAL_RTC_WAKEUPTIMER_DISABLE(): + __HAL_RTC_WAKEUPTIMER_GET_FLAG(): + __HAL_RTC_WRITEPROTECTION_ENABLE(): + __HAL_UNLOCK(): + ->Instance->WUTR + ->Instance->CR + __HAL_RTC_WAKEUPTIMER_ENABLE(): + +HAL_RTCEx_DeactivateWakeUpTimer(): + +#endif /* #if 0 */ + +static void rtc_wkup_irq(void) +{ + HAL_RTCEx_WakeUpTimerIRQHandler(&RtcHandle); +} + +void rtc_alarm_init() +{ + /* Enable Ultra low power mode */ + HAL_PWREx_EnableUltraLowPower(); + + /* Enable the fast wake up from Ultra low power mode */ + HAL_PWREx_EnableFastWakeUp(); + + NVIC_SetVector(RTC_WKUP_IRQn, (uint32_t)rtc_wkup_irq); + NVIC_EnableIRQ(RTC_WKUP_IRQn); +} + +void rtc_alarm_start(uint32_t seconds) +{ + /* Disable Wakeup Counter */ + HAL_RTCEx_DeactivateWakeUpTimer(&RtcHandle); + +/* +RTC_WAKEUPCLOCK_RTCCLK_DIV16 488.3us steps, upto 32s +RTC_WAKEUPCLOCK_RTCCLK_DIV8 244us steps +RTC_WAKEUPCLOCK_RTCCLK_DIV4 122us steps +RTC_WAKEUPCLOCK_RTCCLK_DIV2: 61.035us steps +RTC_WAKEUPCLOCK_CK_SPRE_16BITS 1s steps +RTC_WAKEUPCLOCK_CK_SPRE_17BITS 1s steps +*/ + + /* ## Setting the Wake up time ############################################*/ + /* see RM0038 section 20.3.4 */ + //HAL_RTCEx_SetWakeUpTimer_IT(&RtcHandle, seconds, RTC_WAKEUPCLOCK_RTCCLK_DIV16); + HAL_RTCEx_SetWakeUpTimer_IT(&RtcHandle, seconds, RTC_WAKEUPCLOCK_CK_SPRE_16BITS); + +} + +void rtc_print_subseconds() +{ + printf("ssr:%04x ", RtcHandle.Instance->SSR); +} + +time_t rtc_read_with_subseconds(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 + 100; + timeinfo.tm_hour = timeStruct.Hours; + timeinfo.tm_min = timeStruct.Minutes; + timeinfo.tm_sec = timeStruct.Seconds; + + // Convert to timestamp + time_t t = mktime(&timeinfo); + + t <<= 14; + printf("14:%08x ", t); + + //return (t << 14) | timeStruct.Seconds; + return t | timeStruct.SubSeconds; +} + +/** + * @brief RTC Wake Up callback + * @param None + * @retval None + */ +void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc) +{ + /* Clear Wake Up Flag */ + __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU); +} +