Changeset - f2f8cb2bebfa
[Not reviewed]
default
0 6 0
Ethan Zonca - 7 years ago 2019-09-02 13:57:33
ez@ethanzonca.com
1hz RTC interrupt and STOP mode works! Now idling at 0.8mA or so!
6 files changed with 43 insertions and 11 deletions:
0 comments (0 inline, 0 general)
drivers/STM32F0xx_HAL_Driver/Inc/stm32f0xx_hal.h
Show inline comments
 
@@ -504,48 +504,49 @@
 
 
/** @addtogroup HAL_Exported_Functions
 
  * @{
 
  */
 
 
/** @addtogroup HAL_Exported_Functions_Group1
 
  * @{
 
  */    
 
/* Initialization and de-initialization functions  ******************************/
 
HAL_StatusTypeDef HAL_Init(void);
 
HAL_StatusTypeDef HAL_DeInit(void);
 
void              HAL_MspInit(void);
 
void              HAL_MspDeInit(void);
 
HAL_StatusTypeDef HAL_InitTick (uint32_t TickPriority);
 
/**
 
  * @}
 
  */  
 
 
/** @addtogroup HAL_Exported_Functions_Group2
 
  * @{
 
  */    
 
 
/* Peripheral Control functions  ************************************************/
 
void              HAL_IncTick(void);
 
void 			  HAL_IncTickBy(uint32_t increment); // emz
 
void              HAL_Delay(__IO uint32_t Delay);
 
uint32_t          HAL_GetTick(void);
 
void              HAL_SuspendTick(void);
 
void              HAL_ResumeTick(void);
 
uint32_t          HAL_GetHalVersion(void);
 
uint32_t          HAL_GetREVID(void);
 
uint32_t          HAL_GetDEVID(void);
 
void              HAL_DBGMCU_EnableDBGStopMode(void);
 
void              HAL_DBGMCU_DisableDBGStopMode(void);
 
void              HAL_DBGMCU_EnableDBGStandbyMode(void);
 
void              HAL_DBGMCU_DisableDBGStandbyMode(void);
 
/**
 
  * @}
 
  */ 
 
 
/**
 
  * @}
 
  */
 
 
/**
 
  * @}
 
  */ 
 
 
/**
drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_hal.c
Show inline comments
 
@@ -267,48 +267,54 @@ HAL_StatusTypeDef HAL_DeInit(void)
 
      (+) Get the device identifier
 
      (+) Get the device revision identifier
 
      (+) Enable/Disable Debug module during Sleep mode
 
      (+) Enable/Disable Debug module during STOP mode
 
      (+) Enable/Disable Debug module during STANDBY mode
 
      
 
@endverbatim
 
  * @{
 
  */
 
 
/**
 
  * @brief This function is called to increment  a global variable "uwTick"
 
  *        used as application time base.
 
  * @note In the default implementation, this variable is incremented each 1ms
 
  *       in Systick ISR.
 
  * @note This function is declared as __weak to be overwritten in case of other 
 
  *       implementations in user file.
 
  * @retval None
 
  */
 
__weak void HAL_IncTick(void)
 
{
 
  uwTick++;
 
}
 
 
// emz
 
__weak void HAL_IncTickBy(uint32_t increment)
 
{
 
	uwTick += increment;
 
}
 
 
/**
 
  * @brief  Provides a tick value in millisecond.
 
  * @note   This function is declared as __weak  to be overwritten  in case of other 
 
  *       implementations in user file.
 
  * @retval tick value
 
  */
 
__weak uint32_t HAL_GetTick(void)
 
{
 
  return uwTick;
 
}
 
 
/**
 
  * @brief This function provides accurate delay (in milliseconds) based 
 
  *        on variable incremented.
 
  * @note In the default implementation , SysTick timer is the source of time base.
 
  *       It is used to generate interrupts at regular time intervals where uwTick
 
  *       is incremented.
 
  * @note ThiS function is declared as __weak to be overwritten in case of other
 
  *       implementations in user file.
 
  * @param Delay: specifies the delay time length, in milliseconds.
 
  * @retval None
 
  */
 
__weak void HAL_Delay(__IO uint32_t Delay)
 
{
inc/gpio.h
Show inline comments
 
#ifndef __gpio_H
 
#define __gpio_H
 
 
#include "stm32f0xx_hal.h"
 
 
 
enum _blinkrate
 
{
 
    BLINK_FAST = 50,
 
    BLINK_MED = 250, 
 
    BLINK_SLOW = 2000
 
    BLINK_SLOW = 2000,
 
	BLINK_DISABLE = 9999,
 
};
 
 
 
#define OSC_EN_Pin GPIO_PIN_1
 
#define OSC_EN_GPIO_Port GPIOF
 
#define OSC_NOTEN OSC_EN_GPIO_Port , OSC_EN_Pin
 
 
#define GPS_NEN_Pin GPIO_PIN_0
 
#define GPS_NEN_GPIO_Port GPIOF
 
#define GPS_NOTEN GPS_NEN_GPIO_Port , GPS_NEN_Pin
 
 
#define VBATT_SENSE_Pin GPIO_PIN_6
 
#define VBATT_SENSE_GPIO_Port GPIOA
 
 
#define LED_BLUE_Pin GPIO_PIN_0
 
#define LED_BLUE_GPIO_Port GPIOB
 
#define LED_BLUE LED_BLUE_GPIO_Port , LED_BLUE_Pin
 
 
#define TCXO_EN_Pin GPIO_PIN_8
 
#define TCXO_EN_GPIO_Port GPIOA
 
#define TCXO_EN TCXO_EN_GPIO_Port  , TCXO_EN_Pin
 
 
 
void gpio_init(void);
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);
 
RTC_HandleTypeDef* rtc_gethandle(void);
 
 
 
#endif /*__ rtc_H */
src/main.c
Show inline comments
 
@@ -33,124 +33,138 @@ enum _state
 
};
 
 
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;
 
    uint8_t state = SYSTEM_GPSACQ;
 
//DEBUG:
 
    uint8_t state = SYSTEM_IDLE;
 
//    uint8_t 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;
 
 
    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);
 
//    __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:
 
            {
 
                blink_rate = BLINK_SLOW;
 
            	// Don't blink normally
 
                blink_rate = 9999; //BLINK_SLOW;
 
 
                // Actually sleep for real: disable systick and sleep until RTC interrupt
 
//                HAL_SuspendTick();
 
                // If we haven't blinked for a while, blink now
 
                if(HAL_GetTick() - 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();
 
                }
 
 
                // Enter sleep mode: wait for interrupt
 
                //HAL_PWR_EnterSLEEPMode(0, PWR_SLEEPENTRY_WFI);
 
                __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?
 
                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())
 
@@ -220,55 +234,55 @@ int main(void)
 
            		{
 
            			// 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(HAL_GetTick() - led_timer > blink_rate)
 
			if((blink_rate != BLINK_DISABLE) && (HAL_GetTick() - led_timer > blink_rate))
 
			{
 
				ledpulse();
 
				led_timer = HAL_GetTick();
 
			}
 
 
			if(statled_ontime && HAL_GetTick() - statled_ontime > 10)
 
			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;
src/rtc.c
Show inline comments
 
@@ -56,52 +56,61 @@ void rtc_init(void)
 
	}
 
 
	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_ALL;
 
	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;
 
}
 
 
 
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)