# HG changeset patch # User Ethan Zonca # Date 2016-10-19 17:29:22 # Node ID ddbcfaffc98a20e39287c26d1349b65dfd9dbc29 # Parent 2fa25ca2db210b8f4eb9c85a39c25664e04f78fa Hopefully set up the RTC and prepare for entering sleep mode for one minute at a time diff --git a/inc/rtc.h b/inc/rtc.h --- a/inc/rtc.h +++ b/inc/rtc.h @@ -1,7 +1,11 @@ #ifndef __rtc_H #define __rtc_H +#include "stm32f0xx_hal.h" + void rtc_init(void); +RTC_HandleTypeDef* rtc_gethandle(void); + #endif /*__ rtc_H */ diff --git a/src/interrupts.c b/src/interrupts.c --- a/src/interrupts.c +++ b/src/interrupts.c @@ -6,6 +6,7 @@ #include "stm32f0xx.h" #include "interrupts.h" #include "uart.h" +#include "rtc.h" #include "gpio.h" extern TIM_HandleTypeDef htim1; @@ -33,3 +34,14 @@ void TIM1_BRK_UP_TRG_COM_IRQHandler(void proceed = 1; HAL_TIM_IRQHandler(&htim1); } + +void RTC_IRQHandler(void) +{ + HAL_RTC_AlarmIRQHandler(rtc_gethandle()); +} + + +void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc) +{ + // Do something awesome or not +} diff --git a/src/main.c b/src/main.c --- a/src/main.c +++ b/src/main.c @@ -17,6 +17,13 @@ // Probabl wake up 1 minute early -- 0.45min possible +/- on wakeup time with 15min sync intervals +// TODO: Add JT9 message with more grid locator digits + altitude + vbatt + temp +// MSG13charmax: +// X: gridloc +// Y: altitude +// Z: temperature +// KD8TDF XXYYZZ // could use alt callsign thing + enum _state { SYSTEM_IDLE = 0, // awaiting RTC interrupt for wakeup TODO wake up before scheduled time to get fix? @@ -98,17 +105,22 @@ int main(void) case SYSTEM_IDLE: { blink_rate = BLINK_SLOW; - HAL_PWR_EnterSLEEPMode(0, PWR_SLEEPENTRY_WFI); - HAL_PWR_EnterSLEEPMode(0, PWR_SLEEPENTRY_WFI); - HAL_PWR_EnterSLEEPMode(0, PWR_SLEEPENTRY_WFI); + + // Actually sleep for real: disable systick and sleep until RTC interrupt + HAL_SuspendTick(); + + // Enter sleep mode: wait for interrupt HAL_PWR_EnterSLEEPMode(0, PWR_SLEEPENTRY_WFI); - // Wait for RTC wakeup interrupt - //wfi(); - //enter_sleep(); + // We have woken up! + + // This is hopefully the only timer that needs to stay alive in idle mode + last_wspr_tx_time += 0; // move this timer forward based on sleep length - // Somehow go to another state when we get an interrupt -// state = SYSTEM_GPSACQ; + HAL_ResumeTick(); + + // TODO: Eventually use GPS time to calibrate the RTC maybe + } break; diff --git a/src/rtc.c b/src/rtc.c --- a/src/rtc.c +++ b/src/rtc.c @@ -16,7 +16,7 @@ static void Error_Handler(void) // Initialize RTC void rtc_init(void) { - __HAL_RCC_RTC_ENABLE(); + __HAL_RCC_RTC_ENABLE(); RTC_TimeTypeDef sTime; @@ -25,11 +25,12 @@ void rtc_init(void) hrtc.Instance = RTC; hrtc.Init.HourFormat = RTC_HOURFORMAT_24; - hrtc.Init.AsynchPrediv = 127; - hrtc.Init.SynchPrediv = 255; + hrtc.Init.AsynchPrediv = 124; + hrtc.Init.SynchPrediv = 322; // if this has enough bits should be 1.0018Hz based on 40kHz LSI hrtc.Init.OutPut = RTC_OUTPUT_DISABLE; hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH; hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN; + if (HAL_RTC_Init(&hrtc) != HAL_OK) { Error_Handler(); @@ -55,6 +56,7 @@ void rtc_init(void) Error_Handler(); } + /**Enable the Alarm A */ sAlarm.AlarmTime.Hours = 0x0; @@ -63,7 +65,9 @@ void rtc_init(void) sAlarm.AlarmTime.SubSeconds = 0x0; sAlarm.AlarmTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE; sAlarm.AlarmTime.StoreOperation = RTC_STOREOPERATION_RESET; - sAlarm.AlarmMask = RTC_ALARMMASK_NONE; + + // Alarm will trigger on the Xth second of every minute + sAlarm.AlarmMask = RTC_ALARMMASK_DATEWEEKDAY | RTC_ALARMMASK_HOURS | RTC_ALARMMASK_MINUTES; sAlarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_ALL; sAlarm.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_DATE; sAlarm.AlarmDateWeekDay = 0x1; @@ -73,4 +77,18 @@ void rtc_init(void) Error_Handler(); } + HAL_NVIC_SetPriority(RTC_IRQn, 0, 0); + HAL_NVIC_EnableIRQ(RTC_IRQn); } + +void rtc_cal(void) +{ + // Do something with hrtc.Instance->CALR; // this has a plus and minus component, see refman +} + +RTC_HandleTypeDef* rtc_gethandle(void) +{ + return &hrtc; +} + +