webradio/firmware/pm.c

85 lines
1.1 KiB
C
Raw Normal View History

2020-07-30 19:57:46 +02:00
#include <avr/io.h>
#include <avr/sleep.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include "pm.h"
2020-07-31 18:23:58 +02:00
#include "batmon.h"
2020-07-30 19:57:46 +02:00
static void disable_periphery()
{
cli();
WDTCSR = 0;
TWCR = 0;
SPCR = 0;
ADCSRA = 0;
DDRB = 0;
DDRC = 0;
DDRD = 0;
PORTB = 0;
PORTC = 0;
PORTD = (1 << 2); // Pullup for INT0
}
void suspend()
{
disable_periphery();
EICRA = 0;
EIMSK = (1 << INT0);
SMCR = (1 << SM1) | (1 << SE);
sei();
sleep_cpu();
cli();
_delay_ms(50);
while (!(PIND & (1 << 2)));
_delay_ms(50);
}
ISR(INT0_vect)
{
return;
}
void reset()
{
disable_periphery();
// WDTCSR = (1 << WDE) | (1 << WDP2);
// For some reason, the WD reset locks up my chip,
// so let's just jump to the reset vector directly.
asm("ldi R16,0x00");
asm("push R16");
asm("push R16");
asm("push R16");
asm("push R16");
asm("ret");
while (1);
}
char* post()
{
_delay_ms(500);
2020-07-31 18:23:58 +02:00
if (!batmon_ok_to_boot())
return PSTR("Low battery.");
2020-07-30 19:57:46 +02:00
return 0;
}
2020-07-31 19:07:11 +02:00
void pm_init()
{
PORT_SBCPOW(DDR) |= PIN_SBCPOW;
pm_sbc_off();
}
void pm_sbc_on()
{
PORT_SBCPOW(PORT) |= PIN_SBCPOW;
}
void pm_sbc_off()
{
PORT_SBCPOW(PORT) &= ~PIN_SBCPOW;
}