2024-02-04 19:59:12 +01:00
|
|
|
#include <avr/io.h>
|
2024-09-22 10:52:05 +02:00
|
|
|
#include <avr/wdt.h>
|
2024-02-04 19:59:12 +01:00
|
|
|
#include <util/delay.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "system.h"
|
|
|
|
#include "pm.h"
|
|
|
|
|
|
|
|
extern void app_init();
|
2024-09-22 10:52:05 +02:00
|
|
|
extern void app_suspend();
|
2024-02-04 19:59:12 +01:00
|
|
|
extern enum app_return app_mainloop();
|
|
|
|
|
|
|
|
struct cRGB led[STRIPLEN];
|
|
|
|
|
|
|
|
void display_update() {
|
|
|
|
ws2812_setleds(led, STRIPLEN);
|
|
|
|
}
|
|
|
|
|
|
|
|
void display_clear() {
|
|
|
|
memset(led, 0, STRIPLEN * 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
void system_init()
|
|
|
|
{
|
|
|
|
pm_disable_periphery();
|
|
|
|
button_init();
|
|
|
|
TIFR = 0xFF; // Clear all interrupt flags
|
|
|
|
pm_init();
|
|
|
|
pm_axl_on();
|
|
|
|
_delay_ms(50);
|
|
|
|
mma8653_init();
|
|
|
|
while (HAL_ASSERTED_N(BUTTON_N)) // Prevent boot while button is pressed
|
|
|
|
_delay_ms(25);
|
|
|
|
timers_init();
|
|
|
|
}
|
|
|
|
|
|
|
|
enum BOOT_COLOR {RED, BLUE, GREEN};
|
|
|
|
void boot_animation(enum BOOT_COLOR color_offset)
|
|
|
|
{
|
|
|
|
_delay_ms(10);
|
|
|
|
display_clear();
|
|
|
|
for(int i = 0; i<STRIPLEN; ++i) {
|
2024-09-22 10:52:05 +02:00
|
|
|
wdt_reset();
|
2024-02-04 19:59:12 +01:00
|
|
|
*(&led[i].r + color_offset) = 0x10;
|
|
|
|
display_update();
|
|
|
|
_delay_ms(15);
|
|
|
|
*(&led[i].r + color_offset) = 0x0;
|
|
|
|
}
|
|
|
|
display_update();
|
|
|
|
_delay_ms(10);
|
|
|
|
}
|
|
|
|
|
|
|
|
void system_boot()
|
|
|
|
{
|
|
|
|
system_init();
|
|
|
|
pm_led_on_auto();
|
2024-09-22 10:52:05 +02:00
|
|
|
wdt_enable(WDTO_1S);
|
2024-02-04 19:59:12 +01:00
|
|
|
boot_animation(GREEN);
|
|
|
|
app_init();
|
|
|
|
}
|
|
|
|
|
2024-09-22 10:52:05 +02:00
|
|
|
void system_suspend()
|
|
|
|
{
|
|
|
|
app_suspend();
|
|
|
|
boot_animation(RED);
|
|
|
|
wdt_disable();
|
|
|
|
pm_suspend();
|
|
|
|
}
|
|
|
|
|
2024-02-04 19:59:12 +01:00
|
|
|
int main()
|
|
|
|
{
|
|
|
|
enum app_return ar;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
system_boot();
|
2024-09-22 10:52:05 +02:00
|
|
|
while ((ar = app_mainloop()) == RUN) {
|
|
|
|
wdt_reset();
|
|
|
|
}
|
|
|
|
// TODO: Handle reset instead of suspend
|
|
|
|
system_suspend();
|
2024-02-04 19:59:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pm_reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void systick_cb()
|
|
|
|
{
|
|
|
|
pm_led_on_auto(); // Turn off LED DC-DC converter when USB power is connected
|
|
|
|
button_systick_cb();
|
|
|
|
}
|