Changeset - ddbcfaffc98a
[Not reviewed]
default
0 4 0
Ethan Zonca - 9 years ago 2016-10-19 17:29:22
ez@ethanzonca.com
Hopefully set up the RTC and prepare for entering sleep mode for one minute at a time
4 files changed with 57 insertions and 11 deletions:
0 comments (0 inline, 0 general)
inc/rtc.h
Show inline comments
 
#ifndef __rtc_H
 
#define __rtc_H
 
 
#include "stm32f0xx_hal.h"
 
 
 
void rtc_init(void);
 
RTC_HandleTypeDef* rtc_gethandle(void);
 
 
 
#endif /*__ rtc_H */
src/interrupts.c
Show inline comments
 
//
 
// Interrupts: all global ISRs
 
//
 
 
#include "stm32f0xx_hal.h"
 
#include "stm32f0xx.h"
 
#include "interrupts.h"
 
#include "uart.h"
 
#include "rtc.h"
 
#include "gpio.h"
 
 
extern TIM_HandleTypeDef htim1;
 
extern volatile uint8_t proceed;
 
 
void SysTick_Handler(void)
 
{
 
    HAL_IncTick();
 
    HAL_SYSTICK_IRQHandler();
 
}
 
 
void DMA1_Channel2_3_IRQHandler(void)
 
@@ -24,12 +25,23 @@ void DMA1_Channel2_3_IRQHandler(void)
 
}
 
 
void USART1_IRQHandler(void)
 
{
 
    HAL_UART_IRQHandler(uart_gethandle());
 
}
 
 
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
 
}
src/main.c
Show inline comments
 
@@ -8,24 +8,31 @@
 
#include "i2c.h"
 
#include "uart.h"
 
#include "gpio.h"
 
#include "wspr.h"
 
#include "rtc.h"
 
#include "gps.h"
 
 
 
// We have access to the 1PPS pin of the gps... could have trim routine for internal oscillator based on this when we have a fix
 
// 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?
 
    SYSTEM_GPSACQ, // RTC interrupted
 
    SYSTEM_WSPRTX, // Wait for timeslot and actually transmit the message
 
};
 
 
static void __calc_gridloc(char *dst, double lat, double lon);
 
static void ledpulse(void);
 
 
uint32_t statled_ontime = 0;
 
 
@@ -89,35 +96,40 @@ int main(void)
 
            gps_polltimer = HAL_GetTick();
 
        }
 
 
 
 
        switch(state)
 
        {
 
 
            // Idling: sleep and wait for RTC timeslot trigger
 
            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;
 
 
 
            // Attempt to acquire GPS fix
 
            case SYSTEM_GPSACQ:
 
            {
 
                blink_rate = BLINK_FAST;
 
                HAL_PWR_EnterSLEEPMode(0, PWR_SLEEPENTRY_WFI);
 
                HAL_PWR_EnterSLEEPMode(0, PWR_SLEEPENTRY_WFI);
 
                HAL_PWR_EnterSLEEPMode(0, PWR_SLEEPENTRY_WFI);
 
                HAL_PWR_EnterSLEEPMode(0, PWR_SLEEPENTRY_WFI);
 
src/rtc.c
Show inline comments
 
@@ -16,29 +16,30 @@ static void Error_Handler(void)
 
// Initialize RTC
 
void rtc_init(void)
 
{
 
        __HAL_RCC_RTC_ENABLE();
 
 
    
 
  RTC_TimeTypeDef sTime;
 
  RTC_DateTypeDef sDate;
 
  RTC_AlarmTypeDef sAlarm;
 
 
  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();
 
  }
 
 
  sTime.Hours = 0x0;
 
  sTime.Minutes = 0x0;
 
  sTime.Seconds = 0x0;
 
  sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
 
  sTime.StoreOperation = RTC_STOREOPERATION_RESET;
 
  if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD) != HAL_OK)
 
  {
 
@@ -46,31 +47,48 @@ void rtc_init(void)
 
  }
 
 
  sDate.WeekDay = RTC_WEEKDAY_MONDAY;
 
  sDate.Month = RTC_MONTH_JANUARY;
 
  sDate.Date = 0x1;
 
  sDate.Year = 0x0;
 
 
  if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BCD) != HAL_OK)
 
  {
 
    Error_Handler();
 
  }
 
 
 
    /**Enable the Alarm A 
 
    */
 
  sAlarm.AlarmTime.Hours = 0x0;
 
  sAlarm.AlarmTime.Minutes = 0x0;
 
  sAlarm.AlarmTime.Seconds = 0x0;
 
  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;
 
  sAlarm.Alarm = RTC_ALARM_A;
 
  if (HAL_RTC_SetAlarm(&hrtc, &sAlarm, RTC_FORMAT_BCD) != HAL_OK)
 
  {
 
    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;
 
}
 
 
0 comments (0 inline, 0 general)