aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKing Kévin <kingkevin@cuvoodoo.info>2015-11-08 19:29:49 +0100
committerKing Kévin <kingkevin@cuvoodoo.info>2015-11-08 19:29:49 +0100
commit6531f411a446f4212adb17d0edec72e63307b48b (patch)
treea2f0eb643ca8f765bad0852b52c9dc5d6c51f89e
parent6066ef44d8694068fd7b01f12474d65821f16c44 (diff)
add watchdog (now working)
-rw-r--r--arduino_nano/main.c40
1 files changed, 21 insertions, 19 deletions
diff --git a/arduino_nano/main.c b/arduino_nano/main.c
index f2fc8ba..c8b14fa 100644
--- a/arduino_nano/main.c
+++ b/arduino_nano/main.c
@@ -46,10 +46,24 @@ void wdt_init(void)
{
MCUSR = 0;
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 */
void io_init(void)
{
@@ -73,7 +87,7 @@ int main(void)
{
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 */
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
- /* 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
action = false; // new cycle of actions
- if (watchdog) { // watchdog interrupt trigger. clear before system reset
- WDTCSR = (1<<WDCE); // change settings
- WDTCSR = (1<<WDE)|(1<<WDIE); // enable interrupt than system reset mode
- watchdog = false;
+ if (watchdog) { // watchdog interrupt trigger. reset watchdog before system reset
+ wdt_set(WDTO_2S); // set to 4s (interrupt and reset mode)
+ sei(); // re-enable interrupts
+ watchdog = false; // wait for next reset
}
if (timer_seconds!=0 && timer_seconds==conf.request_period) {
action = true; // an action is performed