Add switch gpio, improve sleep scheduling

This commit is contained in:
jaseg 2019-07-06 15:47:01 +09:00
parent 2a6ce07104
commit fa4f717051

53
main.c
View file

@ -93,18 +93,6 @@ void rtc_set_alarm_rel_sec(uint32_t value) {
rtc_set_alarm_sec(rtc_time() + value); rtc_set_alarm_sec(rtc_time() + value);
} }
void switch_output_enable() {
uint32_t now = rtc_time();
if (now/300 % 24 == 0)
GPIOC->ODR |= 1<<14;
else
GPIOC->ODR &= ~(1<<14);
}
void switch_output_disable() {
GPIOC->ODR &= ~(1<<14);
}
int main(void){ int main(void){
/* We're starting out from HSI@8MHz */ /* We're starting out from HSI@8MHz */
SystemCoreClockUpdate(); SystemCoreClockUpdate();
@ -115,8 +103,7 @@ int main(void){
/* Turn on lots of neat things */ /* Turn on lots of neat things */
RCC->APB2ENR |= RCC_APB2ENR_IOPCEN; RCC->APB2ENR |= RCC_APB2ENR_IOPCEN;
RCC->APB1ENR |= RCC_APB1ENR_BKPEN | RCC_APB1ENR_PWREN; RCC->APB1ENR |= RCC_APB1ENR_BKPEN | RCC_APB1ENR_PWREN;
PWR->CR = PWR_CR_PDDS | PWR_CR_DBP; PWR->CR = PWR_CR_DBP;
SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk; /* Use deep sleep mode */
GPIOC->CRH |= GPIOC->CRH |=
(0<<GPIO_CRH_CNF13_Pos) | (2<<GPIO_CRH_MODE13_Pos)| /* PC13 - LED */ (0<<GPIO_CRH_CNF13_Pos) | (2<<GPIO_CRH_MODE13_Pos)| /* PC13 - LED */
@ -131,23 +118,39 @@ int main(void){
if (!(PWR->CSR & PWR_CSR_WUF)) /* This reset wasn't caused by the RTC alarm */ if (!(PWR->CSR & PWR_CSR_WUF)) /* This reset wasn't caused by the RTC alarm */
REBOOT_REGISTER++; REBOOT_REGISTER++;
uint32_t now = rtc_time();
bool switch_on = false;
if (REBOOT_REGISTER > 1) { /* We have rebooted since initial bring-up */ if (REBOOT_REGISTER > 1) { /* We have rebooted since initial bring-up */
/* Give status indication and active output as fail-safe */ /* Give status indication and active output as fail-safe */
GPIOC->ODR &= ~(1<<13); if ((now&3) == 0) {
for (int i=0; i<5000; i++) GPIOC->ODR &= ~(1<<13);
asm volatile ("nop"); for (int i=0; i<5000; i++)
GPIOC->ODR |= 1<<13; asm volatile ("nop");
GPIOC->ODR |= 1<<13;
}
switch_on = true;
switch_output_enable();
} else { } else {
uint32_t now = rtc_time(); switch_on = (now >= TARGET_DATE - (DAY_SECONDS*24) && now < TARGET_DATE);
if (now >= TARGET_DATE - (DAY_SECONDS*24) && now < TARGET_DATE)
switch_output_enable();
else
switch_output_disable();
} }
PWR->CR |= PWR_CR_CWUF; if (switch_on && now/300 % 24 == 0) {
GPIOC->ODR |= 1<<14;
/* Go to sleep mode to keep GPIO active */
PWR->CR &= ~PWR_CR_PDDS;
SCB->SCR &= ~(SCB_SCR_SLEEPDEEP_Msk); /* Use deep sleep mode */
} else {
GPIOC->ODR &= ~(1<<14);
/* Go to standby mode to reduce power consumption */
PWR->CR = PWR_CR_PDDS;
SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk; /* Use deep sleep mode */
}
PWR->CR |= PWR_CR_CWUF; /* This has 2 cycles latency, thus the NOPs */
asm volatile ("nop"); asm volatile ("nop");
asm volatile ("nop"); asm volatile ("nop");
asm volatile ("wfe"); asm volatile ("wfe");