summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Lamparter <equinox@diac24.net>2012-09-20 05:16:46 +0200
committerDavid Lamparter <equinox@diac24.net>2012-09-20 05:18:52 +0200
commit2bae1f8a73bdd5fca71fd3e60807d6b618e675d1 (patch)
tree435a2c9595cf12542c9ede44f77f95432b955c1d
parentb42baac581ade81def3f40ab7256efdcba94ad5e (diff)
lightctrl: use WDT
the CPU seems to get stuck for no apparent reason, right in the middle of a line on the serial console... weird. let's enable the WDT as a temporary band-aid.
-rw-r--r--dali_ctl.c8
-rw-r--r--lightctrl.c21
-rw-r--r--wdt.c25
3 files changed, 54 insertions, 0 deletions
diff --git a/dali_ctl.c b/dali_ctl.c
index ed23ce1..b5eb66b 100644
--- a/dali_ctl.c
+++ b/dali_ctl.c
@@ -41,6 +41,8 @@ static void dali_search_byte(uint16_t cmd)
uint8_t bit = 0x80;
while (bit) {
+ wdt_reset();
+
dali_twice(cmd | (dali_s_byte & ~bit));
if (dali_compare()) {
_delay_ms(1);
@@ -92,6 +94,8 @@ static void dali_search(void)
do {
dali_search_single();
if (!dali_s_notfound) {
+ wdt_reset();
+
uart_puts("dali scan found\t\t");
uart_puthex(dali_sh);
uart_puthex(dali_sm);
@@ -130,10 +134,14 @@ static void dali_search(void)
dali_map[addr >> 3] |= 1 << (addr & 0x7);
} else
uart_puts(" error\n");
+
+ wdt_reset();
dali_send(DALI_C_WITHDRAW);
}
} while (!dali_s_notfound);
+ wdt_reset();
+
dali_twice(DALI_C_TERMINATE);
uart_puts("dali scan end\n");
diff --git a/lightctrl.c b/lightctrl.c
index 9e8d234..659ac26 100644
--- a/lightctrl.c
+++ b/lightctrl.c
@@ -5,6 +5,7 @@
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <avr/eeprom.h>
+#include <avr/wdt.h>
#include <util/delay.h>
const uint8_t __signature[3] __attribute__((section (".signature"), used)) =
@@ -27,6 +28,7 @@ const uint8_t __signature[3] __attribute__((section (".signature"), used)) =
#include "dali_ctl.c"
#include "dim.c"
#include "can.c"
+#include "wdt.c"
static void can_rx_exec(void)
{
@@ -65,6 +67,8 @@ static void can_rx_exec(void)
int main(void)
{
+ wdt_init();
+
DDRD |= (1 << D_LED1) | (1 << D_LED2 ) | (1 << D_TXD) | (1 << D_DALIO);
PORTD |= (1 << D_LED1) | (1 << D_TAST);
PORTD &= ~(1 << D_LED1);
@@ -76,8 +80,24 @@ int main(void)
sei();
+ uart_puts("\nreset from");
+ if (MCUSR & (1 << WDRF))
+ uart_puts(" WDT");
+ if (MCUSR & (1 << BORF))
+ uart_puts(" BO");
+ if (MCUSR & (1 << EXTRF))
+ uart_puts(" EXT");
+ if (MCUSR & (1 << PORF))
+ uart_puts(" POR");
+ MCUSR = 0;
+
+ uart_puts("\nwdt_generation ");
+ uart_puthex(wdt_generation);
+
uart_puts("\ndelay...\n");
+ wdt_reset();
_delay_ms(1000);
+ wdt_reset();
uart_puts("\ninit done\n");
can_init();
@@ -88,6 +108,7 @@ int main(void)
uint16_t ctr = 0;
while (1) {
+ wdt_reset();
if (canint) {
canint = false;
can_int();
diff --git a/wdt.c b/wdt.c
new file mode 100644
index 0000000..f815b71
--- /dev/null
+++ b/wdt.c
@@ -0,0 +1,25 @@
+
+static uint8_t wdt_generation __attribute__((section (".noinit")));
+
+static void wdt_init(void)
+{
+ uint8_t temp;
+ /* timed sequence. bit 4: WDCE, bit 3: WDE,
+ * 0x07 = WDP2 | WDP1 | WDP0 = 2s */
+ asm volatile ("\n\
+WDTCSR = 0x60\n\
+WDCE = 4\n\
+WDE = 3\n\
+ wdr \n\
+ \n\
+ ldi %[temp], (1 << WDCE) | (1 << WDE) \n\
+ sts WDTCSR, %[temp] \n\
+ \n\
+ ldi %[temp], (1 << WDE) | 0x07 \n\
+ sts WDTCSR, %[temp] \n\
+ " : [temp] "=r" (temp) : : "memory");
+
+ if (MCUSR & (1 << WDRF))
+ wdt_generation++;
+}
+