add watchdog (now working)

This commit is contained in:
King Kévin 2015-11-08 19:29:49 +01:00
parent 6066ef44d8
commit 6531f411a4
1 changed files with 21 additions and 19 deletions

View File

@ -46,10 +46,24 @@ void wdt_init(void)
{ {
MCUSR = 0; MCUSR = 0;
wdt_disable(); wdt_disable();
return;
} }
/* enable watchdog with interrupt and system reset (modified wdt_enable) */
#define wdt_set(value) \
__asm__ __volatile__ ( \
"in __tmp_reg__,__SREG__" "\n\t" \
"cli" "\n\t" \
"wdr" "\n\t" \
"sts %0,%1" "\n\t" \
"out __SREG__,__tmp_reg__" "\n\t" \
"sts %0,%2" "\n\t" \
: /* no outputs */ \
: "M" (_SFR_MEM_ADDR(_WD_CONTROL_REG)), \
"r" (_BV(_WD_CHANGE_BIT) | _BV(WDE) | _BV(WDIE)), \
"r" ((uint8_t) ((value & 0x08 ? _WD_PS3_MASK : 0x00) | _BV(WDE) | _BV(WDIE) | (value & 0x07)) ) \
: "r0" \
)
/* initialize GPIO */ /* initialize GPIO */
void io_init(void) void io_init(void)
{ {
@ -73,7 +87,7 @@ int main(void)
{ {
io_init(); // initialize IOs io_init(); // initialize IOs
printf(PSTR("welcome to the spar counter power meter monitor\n")); //printf(PSTR("welcome to the spar counter power meter monitor\n"));
/* configure nRF24 transceiver */ /* configure nRF24 transceiver */
nrf24_set_rx_addr(conf.rx_addr); // set device receiving address nrf24_set_rx_addr(conf.rx_addr); // set device receiving address
@ -98,24 +112,12 @@ int main(void)
bool action = false; // to know if we performed any king of action during which some other activity could have happened bool action = false; // to know if we performed any king of action during which some other activity could have happened
/* start watchdog */
/*
wdt_enable(WDTO_4S);
while (true);
WDTCSR = (1<<WDCE); // change settings
//WDTCSR = (0<<WDP3)|(1<<WDP2)|(1<<WDP1)|(1<<WDP0); // set time-out to 2s
WDTCSR = (1<<WDP3)|(0<<WDP2)|(0<<WDP1)|(1<<WDP0);
WDTCSR = (1<<WDCE); // change settings
WDTCSR = (1<<WDE)|(1<<WDIE); // enable interrupt than system reset mode
while (true);
*/
while (true) { // endless loop for micro-controller while (true) { // endless loop for micro-controller
action = false; // new cycle of actions action = false; // new cycle of actions
if (watchdog) { // watchdog interrupt trigger. clear before system reset if (watchdog) { // watchdog interrupt trigger. reset watchdog before system reset
WDTCSR = (1<<WDCE); // change settings wdt_set(WDTO_2S); // set to 4s (interrupt and reset mode)
WDTCSR = (1<<WDE)|(1<<WDIE); // enable interrupt than system reset mode sei(); // re-enable interrupts
watchdog = false; watchdog = false; // wait for next reset
} }
if (timer_seconds!=0 && timer_seconds==conf.request_period) { if (timer_seconds!=0 && timer_seconds==conf.request_period) {
action = true; // an action is performed action = true; // an action is performed