85 lines
1.1 KiB
C
85 lines
1.1 KiB
C
#include <avr/io.h>
|
|
#include <avr/sleep.h>
|
|
#include <util/delay.h>
|
|
#include <avr/interrupt.h>
|
|
#include <avr/pgmspace.h>
|
|
#include "pm.h"
|
|
#include "batmon.h"
|
|
|
|
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);
|
|
|
|
if (!batmon_ok_to_boot())
|
|
return PSTR("Low battery.");
|
|
|
|
return 0;
|
|
}
|
|
|
|
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;
|
|
}
|