firmware/main: Rework main loop skeleton

Basically if VBUS is present, we enable USB and we don't go to
sleep to make sure we react in time.

Also add skeleton to read and handle button press events.

Signed-off-by: Sylvain Munaut <tnt@246tNt.com>
This commit is contained in:
Sylvain Munaut 2023-03-16 10:07:57 +01:00
parent ebc8a810c7
commit 84470dd095
1 changed files with 73 additions and 21 deletions

View File

@ -18,11 +18,15 @@
#include "utils.h"
#include "usb_dev.h"
/* ------------------------------------------------------------------------ */
/* USB */
/* ------------------------------------------------------------------------ */
extern const struct usb_stack_descriptors app_stack_desc;
static void
serial_no_init()
usb_serial_no_init(void)
{
uint8_t buf[8];
char *id, *desc;
@ -42,6 +46,44 @@ serial_no_init()
desc[2 + (i << 1)] = id[i];
}
static bool
usb_run(void)
{
/* Check current power state */
bool powered = pmu_is_vbus_present();
bool usb_running = pmu_usb_state();
/* Power USB on/off accordingly */
if (powered && !usb_running)
{
/* Re-enable USB clock */
pmu_usb_enable();
/* Stack init (got reset) */
usb_init(&app_stack_desc);
usb_dfu_rt_init();
usb_dev_init();
usb_connect();
/* We're up ! */
usb_running = true;
}
else if (!powered & usb_running)
{
/* Shutdown USB clock (and reset USB core) */
pmu_usb_disable();
/* USB not active, don't try to access it */
usb_running = false;
}
/* Run USB if need be */
if (usb_running)
usb_poll();
return usb_running;
}
void
usb_dfu_rt_cb_reboot(void)
{
@ -52,36 +94,46 @@ usb_dfu_rt_cb_reboot(void)
pmu_sys_reboot(1);
}
//#define USB 1
/* ------------------------------------------------------------------------ */
/* Main */
/* ------------------------------------------------------------------------ */
void main()
{
/* SPI */
spi_init();
serial_no_init();
usb_serial_no_init();
/* Clear events */
pmu_get_buttons();
/* Enable LEDs */
led_init();
led_start();
/* Enable USB directly */
//#define USB
#ifdef USB
pmu_usb_enable();
usb_init(&app_stack_desc);
usb_dfu_rt_init();
usb_dev_init();
usb_connect();
#endif
/* Main loop */
while (1)
{
#ifdef USB
/* USB poll */
usb_poll();
led_poll(false);
#else
led_poll(true);
#endif
/* Run USB */
bool usb_running = usb_run();
/* Check buttons */
uint8_t btn = pmu_get_buttons();
if (btn & BTN_1_SHORT) {
/* FIXME */
} else if (btn & BTN_1_LONG) {
/* FIXME */
}
if (btn & BTN_2_SHORT) {
/* FIXME */
} else if (btn & BTN_2_LONG) {
/* FIXME */
}
/* LED */
led_poll(!usb_running);
}
}