fw: Add watchdog

Will reset after 1 second spent in app mainloop.
This commit is contained in:
Markus Koch 2024-09-22 10:52:05 +02:00
parent b66134f097
commit 40ba2ea4dc
2 changed files with 44 additions and 12 deletions

View File

@ -1,6 +1,7 @@
#include "lib/system.h" #include "lib/system.h"
#include "lib/hal.h" #include "lib/hal.h"
#include <stdlib.h> #include <stdlib.h>
#include <util/delay.h>
#define TIMER_INMENU TIMER_APP0 #define TIMER_INMENU TIMER_APP0
#define TIMER_AUX TIMER_APP1 #define TIMER_AUX TIMER_APP1
@ -98,12 +99,34 @@ void system_test()
display_update(); display_update();
} }
// Note: This is only for watchdog testing.
// Never use _delay_ms(...) in your programs. Use timer_* functions instead.
void wdt_test()
{
int i = 50;
display_clear(0);
while (i--) {
led[0].b = !led[0].b;
display_update();
_delay_ms(100);
}
led[0].b = 0;
led[0].r = 1;
display_update();
_delay_ms(2000);
}
void app_init() void app_init()
{ {
return; return;
} }
#define APP_COUNT 3 void app_suspend()
{
return;
}
#define APP_COUNT 4
enum app_return app_mainloop() enum app_return app_mainloop()
{ {
static int app_sel = 0; static int app_sel = 0;
@ -136,6 +159,9 @@ enum app_return app_mainloop()
case 2: case 2:
system_test(); system_test();
break; break;
case 3:
wdt_test();
break;
default: default:
app_sel = 0; app_sel = 0;
break; break;

View File

@ -1,4 +1,5 @@
#include <avr/io.h> #include <avr/io.h>
#include <avr/wdt.h>
#include <util/delay.h> #include <util/delay.h>
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
@ -7,6 +8,7 @@
#include "pm.h" #include "pm.h"
extern void app_init(); extern void app_init();
extern void app_suspend();
extern enum app_return app_mainloop(); extern enum app_return app_mainloop();
struct cRGB led[STRIPLEN]; struct cRGB led[STRIPLEN];
@ -39,6 +41,7 @@ void boot_animation(enum BOOT_COLOR color_offset)
_delay_ms(10); _delay_ms(10);
display_clear(); display_clear();
for(int i = 0; i<STRIPLEN; ++i) { for(int i = 0; i<STRIPLEN; ++i) {
wdt_reset();
*(&led[i].r + color_offset) = 0x10; *(&led[i].r + color_offset) = 0x10;
display_update(); display_update();
_delay_ms(15); _delay_ms(15);
@ -52,27 +55,30 @@ void system_boot()
{ {
system_init(); system_init();
pm_led_on_auto(); pm_led_on_auto();
wdt_enable(WDTO_1S);
boot_animation(GREEN); boot_animation(GREEN);
app_init(); app_init();
} }
void system_suspend()
{
app_suspend();
boot_animation(RED);
wdt_disable();
pm_suspend();
}
int main() int main()
{ {
enum app_return ar; enum app_return ar;
while (1) {
system_boot(); system_boot();
while ((ar = app_mainloop()) == RUN) {
while (1) { wdt_reset();
while (1) {
ar = app_mainloop();
if (ar != RUN)
break;
// TODO: Handle reset instead of suspend
// TODO: wdt reset
} }
boot_animation(RED); // TODO: Handle reset instead of suspend
pm_suspend(); system_suspend();
system_boot();
} }
pm_reset(); pm_reset();