Changeset - 7f3f8f2d539f
[Not reviewed]
default
0 4 0
Ethan Zonca - 7 years ago 2019-09-02 20:06:26
ez@ethanzonca.com
Add support for RTC-based full unix timestamp with subsecond support
4 files changed with 83 insertions and 14 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_TimeTypeDef* rtc_time(void);
 
uint64_t rtc_timestamp(void);
 
RTC_HandleTypeDef* rtc_gethandle(void);
 
 
 
#endif /*__ rtc_H */
src/main.c
Show inline comments
 
@@ -10,183 +10,197 @@
 
#include "gpio.h"
 
#include "wspr.h"
 
#include "rtc.h"
 
#include "gps.h"
 
#include "config.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
 
// Probable 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();
 
    HAL_Delay(1000); // startup delay before infinisleep
 
 
    sysclk_init();
 
    gpio_init();
 
    rtc_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;
 
//DEBUG:
 
//    uint8_t state = SYSTEM_IDLE;
 
 
    // DEBUG EMZ FIXME
 
    state = SYSTEM_IDLE;
 
 
    uint32_t gps_polltimer = 0;
 
    uint32_t fix_acq_starttime = 0;
 
    uint32_t nextwspr_time = 0;
 
    uint8_t nextwspr_time_valid = 0;
 
    uint32_t last_wspr_tx_time = 0;
 
    uint64_t idle_blink_last = 0;
 
 
    uint8_t packet_type = 0;
 
 
    // Transmit pilot tone to test TX on bootup
 
    HAL_Delay(1000);
 
    wspr_pilot_tone();
 
    adc_stop();
 
    HAL_Delay(1000);
 
 
//    __DBGMCU_CLK_ENABLE() ; // (RCC->APB2ENR |= (RCC_APB2ENR_DBGMCUEN))
 
//    HAL_EnableDBGStopMode();  //  SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
 
 
    uint32_t idle_blink_last = 0;
 
 
    while (1)
 
    {
 
    	// TODO: Disable GPIO port clocking when not needed!
 
 
    	// 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, only if the GPS is powered on
 
        if(HAL_GetTick() - gps_polltimer > 2000)
 
        {
 
            if(gps_ison())
 
            {
 
            	gps_update_data();
 
 
            	// If odd minute
 
            	if(gps_getdata()->minute % 2)
 
            	{
 
            		// Wait until even minute plus one second, coming soon
 
            		nextwspr_time = HAL_GetTick() + (60000 - (gps_getdata()->second * 1000));
 
                    nextwspr_time_valid = 1;
 
 
            	}
 
            	// If even minute
 
            	else
 
            	{
 
            		// Wait until odd minute, one minute and some change away
 
            		nextwspr_time = HAL_GetTick() + 60000 + (60000 - (gps_getdata()->second * 1000));
 
                    nextwspr_time_valid = 1;
 
            	}
 
            }
 
            gps_polltimer = HAL_GetTick();
 
        }
 
 
 
 
        switch(state)
 
        {
 
 
            // Idling: sleep and wait for RTC timeslot trigger
 
            case SYSTEM_IDLE:
 
            {
 
            	// Don't blink normally
 
                blink_rate = 9999; //BLINK_SLOW;
 
            	// Don't blink with the usual method
 
                blink_rate = BLINK_DISABLE;
 
 
                // If we haven't blinked for a while, blink now
 
                if(HAL_GetTick() - idle_blink_last > 10 * 1000)
 
                if(rtc_timestamp() - idle_blink_last > 10 * 1000)
 
                {
 
                	HAL_GPIO_WritePin(LED_BLUE, 1);
 
                	HAL_Delay(20);
 
                	HAL_GPIO_WritePin(LED_BLUE, 0);
 
                	idle_blink_last = HAL_GetTick();
 
                	idle_blink_last = rtc_timestamp();
 
                }
 
 
 
                // Enter STOP mode for one second using RTC for wakeup
 
                // 1s might not be accurate because we're entering STOP
 
                // mode any time between 1s interrupt ticks...
 
 
                // TODO: Calculate full RTC timestamp before entering
 
                // and after entering, find delta, this is the amount
 
                // we use to increment SYSTICK
 
 
 
                // Enter sleep mode: wait for interrupt
 
                //HAL_PWR_EnterSLEEPMode(0, PWR_SLEEPENTRY_WFI);
 
 
                uint64_t start = rtc_timestamp();
 
 
                __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);
 
                HAL_SuspendTick();
 
 
        		HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);
 
 
        		// We probably stopped for a second
 
        		HAL_IncTickBy(1000); // maybe check the RTC before and after this, increment tick by the delta?
 
        		uint32_t timedelta = rtc_timestamp() - start;
 
        		HAL_IncTickBy(timedelta);
 
        		//HAL_IncTickBy(1000); // maybe check the RTC before and after this, increment tick by the delta?
 
                HAL_ResumeTick();
 
 
                // We have woken up! Clear wakeup flag
 
        		__HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);
 
 
                // 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
 
 
 //               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
 
                }
 
 
                // TODO: Move GPS processing into here from above!
 
 
 
                // If 3d fix with a decent enough precision
 
                if( ((gps_getdata()->fixtype == 2) || (gps_getdata()->fixtype == 3)) && gps_getdata()->pdop < 10 && nextwspr_time_valid == 1)
 
                {
 
                    // 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;
 
                    adc_start();
 
@@ -226,83 +240,85 @@ int main(void)
 
                        // TODO: Switch between alternate and standard packet
 
						wspr_transmit(grid_locator, packet_type);
 
                        packet_type = !packet_type; // alternate packet type
 
						last_wspr_tx_time = HAL_GetTick();
 
						state = SYSTEM_IDLE;
 
                        adc_stop();
 
            		}
 
            		else
 
            		{
 
            			// Window was missed, go back to idle, and try again after time delay
 
						last_wspr_tx_time = HAL_GetTick();
 
            			state = SYSTEM_IDLE;
 
                        adc_stop();
 
            		}
 
                    nextwspr_time_valid = 0; // invalidate wspr time
 
                }
 
            	else
 
            	{
 
                    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);
 
            	}
 
 
                // Schedule next wakeup (maybe 2mins prior to timeslot if no osc trim)
 
                // Next wakeup should enter SYSTEM_GPSACQ state...
 
 
            } break;
 
 
        }
 
 
		#ifndef LED_DISABLE
 
			if((blink_rate != BLINK_DISABLE) && (HAL_GetTick() - led_timer > blink_rate))
 
			{
 
				ledpulse();
 
				led_timer = HAL_GetTick();
 
			}
 
 
			if((blink_rate != BLINK_DISABLE) && (statled_ontime && HAL_GetTick() - statled_ontime > 10))
 
			{
 
				HAL_GPIO_WritePin(LED_BLUE, 0);
 
				statled_ontime = 0;
 
			}
 
		#endif
 
 
    }
 
}
 
 
 
 
static void ledpulse(void)
 
{
 
    HAL_GPIO_WritePin(LED_BLUE, 1);
 
	statled_ontime = HAL_GetTick();
 
}
 
 
static void __calc_gridloc(char *dst, double lat, double lon)
 
{
 
	int o1, o2, o3;
 
	int a1, a2, a3;
 
	double remainder;
 
	// longitude
 
	remainder = lon + 180.0;
 
	o1 = (int)(remainder / 20.0);
 
	remainder = remainder - (double)o1 * 20.0;
 
	o2 = (int)(remainder / 2.0);
 
	remainder = remainder - 2.0 * (double)o2;
 
	o3 = (int)(12.0 * remainder);
 
 
	// latitude
 
	remainder = lat + 90.0;
 
	a1 = (int)(remainder / 10.0);
 
	remainder = remainder - (double)a1 * 10.0;
 
	a2 = (int)(remainder);
 
	remainder = remainder - (double)a2;
 
	a3 = (int)(24.0 * remainder);
 
	dst[0] = (char)o1 + 'A';
 
	dst[1] = (char)a1 + 'A';
 
	dst[2] = (char)o2 + '0';
 
	dst[3] = (char)a2 + '0';
 
	dst[4] = (char)o3 + 'A';
 
	dst[5] = (char)a3 + 'A';
 
	dst[6] = (char)0;
 
}
 
src/rtc.c
Show inline comments
 
//
 
// RTC: configure real-time clock
 
//
 
 
#include "stm32f0xx_hal.h"
 
#include "rtc.h"
 
#include "gpio.h"
 
 
 
RTC_HandleTypeDef hrtc;
 
 
static void Error_Handler(void)
 
{
 
	volatile uint8_t crap = 1;
 
 
	for(uint16_t i=0; i<300; i++)
 
	{
 
		HAL_GPIO_TogglePin(LED_BLUE);
 
		HAL_Delay(100);
 
	}
 
}
 
 
// Initialize RTC
 
void rtc_init(void)
 
{
 
	__HAL_RCC_RTC_ENABLE();
 
 
    
 
	RTC_TimeTypeDef sTime;
 
	RTC_DateTypeDef sDate;
 
	RTC_AlarmTypeDef sAlarm;
 
 
	HAL_PWR_EnableBkUpAccess();
 
 
	hrtc.Instance = RTC;
 
	hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
 
	hrtc.Init.AsynchPrediv = 124;
 
	hrtc.Init.SynchPrediv = 322; // if this has enough bits should be 1.0018Hz based on 40kHz LSI
 
	hrtc.Init.AsynchPrediv = 127; // Note that both these dividers actually divide by their value + 1
 
	hrtc.Init.SynchPrediv = 311; // About 1Hz with 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 = 0x01;
 
	sDate.Year = 0x19;
 
 
	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;
 
 
	// Alarm will trigger on the Xth second of every minute
 
	sAlarm.AlarmMask = RTC_ALARMMASK_ALL; // Trigger every second for now
 
	sAlarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_SS14; //RTC_ALARMSUBSECONDMASK_ALL;
 
	sAlarm.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_DATE;
 
	sAlarm.AlarmDateWeekDay = RTC_WEEKDAY_MONDAY;
 
	sAlarm.Alarm = RTC_ALARM_A;
 
	if (HAL_RTC_SetAlarm_IT(&hrtc, &sAlarm, RTC_FORMAT_BCD) != HAL_OK)
 
	{
 
		Error_Handler();
 
	}
 
 
	HAL_NVIC_SetPriority(RTC_IRQn, 0, 0);
 
	HAL_NVIC_EnableIRQ(RTC_IRQn);
 
 
	HAL_RTC_WaitForSynchro(&hrtc);
 
 
 
}
 
 
RTC_TimeTypeDef time_last = {0};
 
 
RTC_TimeTypeDef* rtc_time(void)
 
{
 
	HAL_RTC_GetTime(&hrtc, &time_last, RTC_FORMAT_BCD);
 
	return &time_last;
 
}
 
 
uint16_t __rtc_millis(uint32_t sub_seconds)
 
{
 
	// Subsecond counter counts down from SynchPrediv value to zero
 
	return (hrtc.Init.SynchPrediv - sub_seconds) * 999 / hrtc.Init.SynchPrediv; // 0 - 999 mS
 
}
 
 
#define JULIAN_DATE_BASE 2440588 // Unix epoch time in Julian calendar (UnixTime = 00:00:00 01.01.1970 => JDN = 2440588)
 
 
uint64_t rtc_timestamp(void)
 
{
 
	RTC_TimeTypeDef time = {0};
 
	RTC_DateTypeDef date = {0};
 
 
	HAL_RTC_GetTime(&hrtc, &time, RTC_FORMAT_BCD);
 
	HAL_RTC_GetDate(&hrtc, &date, RTC_FORMAT_BCD);
 
 
	// Thanks to LonelyWolf: https://github.com/LonelyWolf/stm32/blob/master/stm32l-dosfs/RTC.c
 
	// Convert Date/Time structures to epoch time
 
	uint8_t  a;
 
	uint16_t y;
 
	uint8_t  m;
 
	uint64_t JDN;
 
 
	// These hardcore math's are taken from http://en.wikipedia.org/wiki/Julian_day
 
 
	// Calculate some coefficients
 
	a = (14 - date.Month) / 12;
 
	y = (date.Year + 2000) + 4800 - a; // years since 1 March, 4801 BC
 
	m = date.Month + (12 * a) - 3; // since 1 March, 4801 BC
 
 
	// Gregorian calendar date compute
 
	JDN  = date.Date;
 
	JDN += (153 * m + 2) / 5;
 
	JDN += 365 * y;
 
	JDN += y / 4;
 
	JDN += -y / 100;
 
	JDN += y / 400;
 
	JDN  = JDN - 32045;
 
	JDN  = JDN - JULIAN_DATE_BASE;    // Calculate from base date
 
	JDN *= 86400;                     // Days to seconds
 
	JDN += time.Hours * 3600;    // ... and today seconds
 
	JDN += time.Minutes * 60;
 
	JDN += time.Seconds;
 
 
	JDN *= 1000; // Prepare to add ms
 
 
	JDN += __rtc_millis(time.SubSeconds);
 
 
	return JDN;
 
 
}
 
 
 
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;
 
}
 
 
src/wspr.c
Show inline comments
 
@@ -209,98 +209,98 @@ void wspr_transmit(uint8_t* grid_locator
 

	
 
        // Encode
 
        uint8_t powers[] = {0, 3, 7, 10, 13, 17, 20, 23, 27, 30, 33, 37, 40, 43, 47, 50, 53, 57, 60};
 
        dbm = powers[chunk];
 

	
 

	
 
    }
 
    else
 
    {
 
        call[0] = call_orig[0];
 
        call[1] = call_orig[1];
 
        call[2] = call_orig[2];
 
        call[3] = call_orig[3];
 
        call[4] = call_orig[4];
 
        call[5] = call_orig[5];
 
        call[6] = call_orig[6];
 

	
 
        dbm = DBM_ORIG; 
 
    }
 

	
 
    // Start timer for WSPR
 
    __TIM1_CLK_ENABLE();
 
    htim1.Instance = TIM1;
 
    htim1.Init.Prescaler = 512 / 4; // FIXED gives 64us ticks from 2mhz clock // gives 64uS ticks from 8MHz ahbclk
 
    htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
 
    htim1.Init.Period = ctc; // Count up to this value (how many 64uS ticks per symbol)
 
    htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
 
    htim1.Init.RepetitionCounter = 0;
 
    HAL_TIM_Base_Init(&htim1);
 
    HAL_TIM_Base_Start_IT(&htim1);
 
    HAL_NVIC_SetPriority(TIM1_BRK_UP_TRG_COM_IRQn, 0, 0);
 
    HAL_NVIC_EnableIRQ(TIM1_BRK_UP_TRG_COM_IRQn);
 

	
 

	
 

	
 
    // TODO: Bring up TCXO sooner! Gotta let it warm up or something
 

	
 
    HAL_GPIO_WritePin(OSC_NOTEN, 0);
 
    HAL_GPIO_WritePin(TCXO_EN, 1);
 
    HAL_Delay(100);
 

	
 
    // Bring up the chip
 
    i2c_init();
 
    si5351_init(i2c_get(), SI5351_CRYSTAL_LOAD_8PF, 0);
 
    si5351_set_correction(0);
 
    //si5351_set_pll(SI5351_PLL_FIXED, SI5351_PLLA);
 
    //si5351_set_ms_source(SI5351_CLK0, SI5351_PLLA);
 
    si5351_set_freq(WSPR_DEFAULT_FREQ * 100, 0, SI5351_CLK0);
 
//    si5351_drive_strength(SI5351_CLK0, SI5351_DRIVE_8MA); // Set for max power if desired (8ma max)
 
    si5351_drive_strength(SI5351_CLK0, SI5351_DRIVE_6MA); // Set for max power if desired (8ma max)
 
    si5351_drive_strength(SI5351_CLK0, SI5351_DRIVE_8MA); // Set for max power if desired (8ma max)
 
//    si5351_drive_strength(SI5351_CLK0, SI5351_DRIVE_6MA); // Set for max power if desired (8ma max)
 
//    si5351_drive_strength(SI5351_CLK0, SI5351_DRIVE_2MA); // Set for max power if desired (8ma max)
 
    si5351_output_enable(SI5351_CLK0, 1);
 
    //si5351_pll_reset(SI5351_PLLA);
 

	
 
    // Make sure the other outputs of the SI5351 are disabled
 
    si5351_output_enable(SI5351_CLK1, 0); // Disable the clock initially
 
    si5351_output_enable(SI5351_CLK2, 0); // Disable the clock initially
 

	
 
    // disable clock powers
 
    si5351_set_clock_pwr(SI5351_CLK1, 0);
 
    si5351_set_clock_pwr(SI5351_CLK2, 0);
 

	
 

	
 
    // Encode message to transmit
 
    wspr_encode(call, loc, dbm, tx_buffer);
 

	
 
    // Key transmitter
 
    si5351_output_enable(SI5351_CLK0, 1);
 

	
 
    // Loop through and transmit symbols TODO: Do this from an ISR or ISR-triggered main loop function call (optimal)
 
    uint8_t i;
 
    for(i=0; i<symbol_count; i++)
 
    {
 
        uint32_t freq2 = (freq * 100) + (tx_buffer[i] * tone_spacing);
 
        si5351_set_freq(freq2, 0, SI5351_CLK0);
 
        HAL_GPIO_TogglePin(LED_BLUE);
 

	
 
        proceed = 0;
 
        while(!proceed);
 
    }
 

	
 
    // Disable transmitter
 
    si5351_output_enable(SI5351_CLK0, 0);
 

	
 
    HAL_GPIO_WritePin(OSC_NOTEN, 1);
 
    HAL_GPIO_WritePin(TCXO_EN, 0);
 

	
 
    i2c_deinit();
 

	
 
    // Disable timer
 
    HAL_NVIC_DisableIRQ(TIM1_BRK_UP_TRG_COM_IRQn);
 
    HAL_TIM_Base_Stop_IT(&htim1);
 
    HAL_TIM_Base_DeInit(&htim1);
 

	
 
    __TIM1_CLK_DISABLE();
 

	
 

	
 
}
0 comments (0 inline, 0 general)