Changeset - c396707da341
[Not reviewed]
default
0 5 0
Ethan Zonca - 6 years ago 2020-04-24 19:53:28
ez@ethanzonca.com
Add uncommitted changes
5 files changed with 48 insertions and 21 deletions:
0 comments (0 inline, 0 general)
.settings/language.settings.xml
Show inline comments
 
@@ -2,19 +2,19 @@
 
<project>
 
	<configuration id="ilg.gnuarmeclipse.managedbuild.cross.toolchain.base.1766192373" name="Default">
 
		<extension point="org.eclipse.cdt.core.LanguageSettingsProvider">
 
			<provider copy-of="extension" id="org.eclipse.cdt.ui.UserLanguageSettingsProvider"/>
 
			<provider-reference id="org.eclipse.cdt.core.ReferencedProjectsLanguageSettingsProvider" ref="shared-provider"/>
 
			<provider-reference id="org.eclipse.cdt.managedbuilder.core.MBSLanguageSettingsProvider" ref="shared-provider"/>
 
		</extension>
 
	</configuration>
 
	<configuration id="com.atollic.truestudio.exe.debug.1518366166.1945969626" name="testTS">
 
		<extension point="org.eclipse.cdt.core.LanguageSettingsProvider">
 
			<provider copy-of="extension" id="org.eclipse.cdt.ui.UserLanguageSettingsProvider"/>
 
			<provider-reference id="org.eclipse.cdt.managedbuilder.core.MBSLanguageSettingsProvider" ref="shared-provider"/>
 
			<provider class="com.atollic.truestudio.mbs.GCCSpecsDetectorAtollicArm" console="false" env-hash="-1492095810163919837" id="com.atollic.truestudio.mbs.provider" keep-relative-paths="false" name="Atollic ARM Tools Language Settings" parameter="${COMMAND} -E -P -v -dD &quot;${INPUTS}&quot;" prefer-non-shared="true">
 
			<provider class="com.atollic.truestudio.mbs.GCCSpecsDetectorAtollicArm" console="false" env-hash="246127282863129152" id="com.atollic.truestudio.mbs.provider" keep-relative-paths="false" name="Atollic ARM Tools Language Settings" parameter="${COMMAND} -E -P -v -dD &quot;${INPUTS}&quot;" prefer-non-shared="true">
 
				<language-scope id="org.eclipse.cdt.core.gcc"/>
 
				<language-scope id="org.eclipse.cdt.core.g++"/>
 
			</provider>
 
		</extension>
 
	</configuration>
 
</project>
inc/rtc.h
Show inline comments
 
#ifndef __rtc_H
 
#define __rtc_H
 
 
#include "stm32f0xx_hal.h"
 
 
 
void rtc_incseconds(void);
 
void rtc_init(void);
 
RTC_TimeTypeDef* rtc_time(void);
 
uint64_t rtc_timestamp(void);
 
uint32_t rtc_timestamp_seconds(void);
 
RTC_HandleTypeDef* rtc_gethandle(void);
 
 
 
#endif /*__ rtc_H */
src/interrupts.c
Show inline comments
 
@@ -29,26 +29,29 @@ 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)
 
{
 
	  rtc_incseconds();
 
 
	// Do something awesome or not
 
}
 
 
void DMA1_Channel1_IRQHandler(void)
 
{
 
    HAL_DMA_IRQHandler(adc_gethandle()->DMA_Handle);
 
}
 
src/main.c
Show inline comments
 
@@ -24,25 +24,25 @@
 
//  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);
 
static void __ledpulse(void);
 
static void __sleep_enter_stop(void);
 
 
uint32_t statled_ontime = 0;
 
 
 
int main(void)
 
{
 
    HAL_Init();
 
    HAL_Delay(1000); // startup delay before infinisleep
 
 
    sysclk_init();
 
    gpio_init();
 
@@ -52,79 +52,82 @@ int main(void)
 
 
    uint32_t led_timer = HAL_GetTick();
 
 
    led_blink(4);
 
 
    uint16_t blink_rate = BLINK_FAST;
 
    uint8_t state = SYSTEM_GPSACQ;
 
 
    // DEBUG EMZ FIXME
 
//    state = SYSTEM_IDLE;
 
 
    uint32_t gps_polltimer = 0;
 
    uint32_t fix_acq_starttime = 0;
 
    uint32_t nextwspr_time = 0;
 
    uint64_t fix_acq_starttime = 0;
 
    uint64_t nextwspr_time = 0;
 
    uint8_t nextwspr_time_valid = 0;
 
    uint64_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);
 
 
 
////////////////////////////////////////////
 
///// TODO: MUST synchro RTC to start of 1PPS second to make sure we actually start wspr on time with 1s granularity
 
/////////////////////////////////////////
 
    while (1)
 
    {
 
    	// Every 10 minutes, wake up and try to wspr
 
    	if(state == SYSTEM_IDLE && (rtc_timestamp() - last_wspr_tx_time > 60000 * 10))
 
    	if(state == SYSTEM_IDLE && (rtc_timestamp_seconds() - last_wspr_tx_time >= 60 * 10))
 
    	{
 
    		state = SYSTEM_GPSACQ;
 
    	}
 
 
        // Update fix status every 2 seconds, only if the GPS is powered on
 
        if(HAL_GetTick() - gps_polltimer > 2000)
 
        if(rtc_timestamp_seconds() - gps_polltimer >= 3)
 
        {
 
            gps_polltimer = rtc_timestamp_seconds();
 
 
            if(gps_ison())
 
            {
 
            	HAL_GPIO_WritePin(LED_BLUE, 1);
 
            	HAL_Delay(50);
 
            	HAL_GPIO_WritePin(LED_BLUE, 0);
 
 
 
            	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 = rtc_timestamp_seconds() + (60 - (gps_getdata()->second * 1));
 
                    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 = rtc_timestamp_seconds() + 60 + (60 - (gps_getdata()->second * 1));
 
                    nextwspr_time_valid = 1;
 
            	}
 
            }
 
            gps_polltimer = HAL_GetTick();
 
        }
 
 
 
 
        switch(state)
 
        {
 
            // Idling: sleep and wait for RTC timeslot trigger
 
            case SYSTEM_IDLE:
 
            {
 
            	// Don't blink with the usual method
 
                blink_rate = BLINK_DISABLE;
 
 
@@ -144,95 +147,95 @@ int main(void)
 
 
            } break;
 
 
 
            // Attempt to acquire GPS fix
 
            case SYSTEM_GPSACQ:
 
            {
 
                //blink_rate = BLINK_FAST;
 
                blink_rate = BLINK_DISABLE;
 
 
                if(!gps_ison())
 
                {
 
                	fix_acq_starttime = HAL_GetTick();
 
                	fix_acq_starttime = rtc_timestamp_seconds();
 
                    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();
 
                }
 
                // If no decent fix in 3 minutes
 
                else if(HAL_GetTick() - fix_acq_starttime > 60000 * 3)
 
                else if(rtc_timestamp_seconds() - fix_acq_starttime >= 60 * 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 = rtc_timestamp(); // repeat acq/tx cycle after big time delay
 
                    last_wspr_tx_time = rtc_timestamp_seconds(); // repeat acq/tx cycle after big time delay
 
                	state = SYSTEM_IDLE;
 
                }
 
                else
 
                {
 
                	// We're waiting for a GPS fix, might as well sleep and let the GPS get a fix.
 
            		// Enter stop mode for 1 second. Blink in the GPS code above.
 
                    __sleep_enter_stop();
 
                }
 
 
            } break;
 
 
 
            // Wait for wspr timeslot and start transmitting
 
            case SYSTEM_WSPRTX:
 
            {
 
            	//blink_rate = BLINK_MED;
 
                blink_rate = BLINK_DISABLE;
 
 
 
            	// If we're after the minute but not more than 2s after the minute, start tx
 
            	if(HAL_GetTick() >= nextwspr_time)
 
            	if(rtc_timestamp_seconds() >= nextwspr_time)
 
            	{
 
            		if(HAL_GetTick() < nextwspr_time + 2000)
 
            		if(rtc_timestamp_seconds() < nextwspr_time + 2)
 
            		{
 
            			volatile double latitude_flt = (double)gps_getdata()->latitude / 10000000.0;
 
            			volatile double longitude_flt = (double)gps_getdata()->longitude / 10000000.0;
 
            			volatile uint8_t grid_locator[7];
 
 
            			__calc_gridloc(grid_locator, latitude_flt, longitude_flt);
 
 
                        // TODO: Switch between alternate and standard packet
 
						wspr_transmit(grid_locator, packet_type);
 
                        packet_type = !packet_type; // alternate packet type
 
						last_wspr_tx_time = rtc_timestamp();
 
						last_wspr_tx_time = rtc_timestamp_seconds();
 
						state = SYSTEM_IDLE;
 
                        adc_stop();
 
            		}
 
            		else
 
            		{
 
            			// Window was missed, go back to idle, and try again after time delay
 
						last_wspr_tx_time = rtc_timestamp();
 
						last_wspr_tx_time = rtc_timestamp_seconds();
 
            			state = SYSTEM_IDLE;
 
                        adc_stop();
 
            		}
 
                    nextwspr_time_valid = 0; // invalidate wspr time
 
                }
 
            	else
 
            	{
 
                	HAL_GPIO_WritePin(LED_BLUE, 1);
 
                	HAL_Delay(50);
 
                	HAL_GPIO_WritePin(LED_BLUE, 0);
 
                	HAL_Delay(50);
 
                	HAL_GPIO_WritePin(LED_BLUE, 1);
 
@@ -241,25 +244,25 @@ int main(void)
 
 
            		// Enter stop mode for 1 second
 
                    __sleep_enter_stop();
 
            	}
 
 
            } break;
 
 
        }
 
 
		#ifndef LED_DISABLE
 
			if((blink_rate != BLINK_DISABLE) && (HAL_GetTick() - led_timer > blink_rate))
 
			{
 
				ledpulse();
 
				__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
 
 
    }
 
}
 
@@ -276,25 +279,25 @@ static void __sleep_enter_stop(void)
 
 
	// Calculate how long we were asleep
 
	uint32_t timedelta = rtc_timestamp() - start;
 
 
	// Increment systick by this value to keep all timing happy
 
	HAL_IncTickBy(timedelta);
 
	HAL_ResumeTick();
 
 
	// We have woken up! Clear wakeup flag
 
	__HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);
 
}
 
 
static void ledpulse(void)
 
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;
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 RTC_HandleTypeDef hrtc;
 
static volatile uint32_t seconds_ctr = 0;
 
 
 
void rtc_incseconds(void)
 
{
 
	//HAL_GPIO_TogglePin(LED_BLUE);
 
	seconds_ctr += 1;
 
}
 
 
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);
 
	}
 
}
 
 
@@ -85,40 +93,43 @@ void rtc_init(void)
 
	{
 
		Error_Handler();
 
	}
 
 
	HAL_NVIC_SetPriority(RTC_IRQn, 0, 0);
 
	HAL_NVIC_EnableIRQ(RTC_IRQn);
 
 
	HAL_RTC_WaitForSynchro(&hrtc);
 
 
 
}
 
 
RTC_TimeTypeDef time_last = {0};
 
static RTC_TimeTypeDef time_last = {0};
 
 
// Get the time from the RTC
 
RTC_TimeTypeDef* rtc_time(void)
 
{
 
	HAL_RTC_GetTime(&hrtc, &time_last, RTC_FORMAT_BCD);
 
	return &time_last;
 
}
 
 
// Calculate value in milliseconds from subseconds of the RTC
 
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)
 
 
// Calculate timestamp in milliseconds since Unix epoch
 
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;
 
@@ -145,24 +156,32 @@ uint64_t rtc_timestamp(void)
 
	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;
 
 
}
 
 
uint32_t rtc_timestamp_seconds(void)
 
{
 
	return seconds_ctr;
 
}
 
 
 
// Calibrate the RTC somehow
 
void rtc_cal(void)
 
{
 
	// Do something with hrtc.Instance->CALR; // this has a plus and minus component, see refman
 
}
 
 
 
// RTC accessor
 
RTC_HandleTypeDef* rtc_gethandle(void)
 
{
 
	return &hrtc;
 
}
 
 
0 comments (0 inline, 0 general)