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)
 
{
 
    HAL_DMA_IRQHandler(uart_get_txdma_handle());
 
    HAL_DMA_IRQHandler(uart_get_txdma_handle());
 
}
 
 
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
 
//
 
// WSPRHAB: Minimal high-altitude balloon tracker with WSPR telemetry
 
//
 
 
#include "stm32f0xx_hal.h"
 
#include "adc.h"
 
#include "system.h"
 
#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;
 
 
 
int main(void)
 
{
 
    HAL_Init();
 
 
    sysclk_init();
 
    rtc_init();
 
    gpio_init();
 
    adc_init();
 
    wspr_init();
 
 
    uint32_t led_timer = HAL_GetTick();
 
 
    led_blink(4);
 
 
    uint16_t blink_rate = BLINK_FAST;
 
    uint8_t state = SYSTEM_GPSACQ;
 
 
    uint32_t gps_polltimer = 0;
 
    uint32_t fix_acq_starttime = 0;
 
    uint32_t nextwspr_time = 0;
 
    uint32_t last_wspr_tx_time = 0;
 
 
    uint8_t fix_ok = 0;
 
    uint8_t numsats = 0;
 
 
 
    while (1)
 
    {
 
 
    	// Every 10 minutes, wake up and try to wspr
 
    	if(state == SYSTEM_IDLE && (HAL_GetTick() - last_wspr_tx_time > 60000 * 10))
 
    	{
 
    		state = SYSTEM_GPSACQ;
 
    	}
 
 
        // Update fix status every 2 seconds
 
        if(HAL_GetTick() - gps_polltimer > 2000)
 
        {
 
            if(gps_ison())
 
            {
 
            	gps_update_data();
 
 
            	// If odd minute
 
            	if(gps_getdata()->minute % 2)
 
            	{
 
            		// Wait until even minute, coming soon
 
            		nextwspr_time = HAL_GetTick() + (60000 - (gps_getdata()->second * 1000));
 
 
            	}
 
            	// If even minute
 
            	else
 
            	{
 
            		// Wait until odd minute, one minute and some change away
 
            		nextwspr_time = HAL_GetTick() + 60000 + (60000 - (gps_getdata()->second * 1000));
 
            	}
 
            }
 
            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);
 
 
 
                if(!gps_ison())
 
                {
 
                	fix_acq_starttime = HAL_GetTick();
 
                    gps_poweron(); // power on and initialize GPS module
 
                }
 
 
                // If 3d fix with a decent enough precision
 
                if( ((gps_getdata()->fixtype == 2) || (gps_getdata()->fixtype == 3)) && gps_getdata()->pdop < 10)
 
                {
 
                    // Disable GPS module
 
                    gps_poweroff();
 
 
                    // TODO: Set RTC from GPS time
 
 
                    // TODO: Set RTC for countdown to next transmission timeslot!
 
 
                    // TODO: Set wspr countdown timer for this transmission!
 
                    fix_acq_starttime = 0;
 
                    state = SYSTEM_WSPRTX;
 
                }
 
                // If no decent fix in 3 minutes
 
                else if(HAL_GetTick() - fix_acq_starttime > 60000 * 3)
 
                {
 
                	// Flash error code and go to idle, try again next time
 
                	led_blink(4);
 
                    gps_poweroff();
 
                    fix_acq_starttime = 0;
 
                    last_wspr_tx_time = HAL_GetTick(); // repeat acq/tx cycle after big time delay
 
                	state = SYSTEM_IDLE;
 
                }
 
            } break;
 
 
            
 
            // Wait for wspr timeslot and start transmitting
 
            case SYSTEM_WSPRTX:
src/rtc.c
Show inline comments
 
//
 
// RTC: configure real-time clock
 
//
 
 
#include "stm32f0xx_hal.h"
 
#include "rtc.h"
 
 
 
RTC_HandleTypeDef hrtc;
 
 
static void Error_Handler(void)
 
{
 
	volatile crap = 1;
 
}
 
 
// 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)
 
  {
 
    Error_Handler();
 
  }
 
 
  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)