From 3e45de7deade177b5caa921ee72a0e36300474c7 Mon Sep 17 00:00:00 2001 From: Joey Castillo Date: Sat, 28 Aug 2021 13:35:52 -0400 Subject: implement USB console --- watch-library/main.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) (limited to 'watch-library/main.c') diff --git a/watch-library/main.c b/watch-library/main.c index c23ceac6..886ab851 100755 --- a/watch-library/main.c +++ b/watch-library/main.c @@ -32,6 +32,7 @@ #include "hal_init.h" #include "atmel_start_pins.h" #include "watch.h" +#include "tusb.h" int main(void) { // ASF code. Initialize the MCU with configuration options from Atmel Studio. @@ -54,12 +55,27 @@ int main(void) { // Watch library code. Set initial parameters for the device and enable the RTC. _watch_init(); + // check if we are plugged into USB power. + watch_enable_digital_input(VBUS_DET); + watch_enable_pull_down(VBUS_DET); + // IF WE ARE: + if (watch_get_pin_level(VBUS_DET)) { + // Ramp up to 16 MHz (seems necessary for USB to work)... + hri_oscctrl_write_OSC16MCTRL_reg(OSCCTRL, OSCCTRL_OSC16MCTRL_ONDEMAND | OSCCTRL_OSC16MCTRL_FSEL_16 | OSCCTRL_OSC16MCTRL_ENABLE); + // ...and enable USB functionality. + _watch_enable_usb(); + } + watch_disable_digital_input(VBUS_DET); + // User code. Give the app a chance to enable and set up peripherals. app_setup(); while (1) { bool can_sleep = app_loop(); - if (can_sleep) { + if (hri_usbdevice_get_CTRLA_ENABLE_bit(USB)) { + // if USB is enabled, do not sleep, and handle any pending TinyUSB tasks. + tud_task(); + } else if (can_sleep) { app_prepare_for_sleep(); sleep(4); app_wake_from_sleep(); -- cgit v1.2.3 From ef0ed276a54fa304cf865b52d3665fa1f2f83b47 Mon Sep 17 00:00:00 2001 From: Joey Castillo Date: Sat, 28 Aug 2021 19:49:49 -0400 Subject: handle tinyusb device tasks on a timer, allows delays in app --- watch-library/main.c | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) (limited to 'watch-library/main.c') diff --git a/watch-library/main.c b/watch-library/main.c index 886ab851..3133c2d2 100755 --- a/watch-library/main.c +++ b/watch-library/main.c @@ -58,11 +58,8 @@ int main(void) { // check if we are plugged into USB power. watch_enable_digital_input(VBUS_DET); watch_enable_pull_down(VBUS_DET); - // IF WE ARE: if (watch_get_pin_level(VBUS_DET)) { - // Ramp up to 16 MHz (seems necessary for USB to work)... - hri_oscctrl_write_OSC16MCTRL_reg(OSCCTRL, OSCCTRL_OSC16MCTRL_ONDEMAND | OSCCTRL_OSC16MCTRL_FSEL_16 | OSCCTRL_OSC16MCTRL_ENABLE); - // ...and enable USB functionality. + // if so, enable USB functionality. _watch_enable_usb(); } watch_disable_digital_input(VBUS_DET); @@ -71,11 +68,10 @@ int main(void) { app_setup(); while (1) { + bool usb_enabled = hri_usbdevice_get_CTRLA_ENABLE_bit(USB); bool can_sleep = app_loop(); - if (hri_usbdevice_get_CTRLA_ENABLE_bit(USB)) { - // if USB is enabled, do not sleep, and handle any pending TinyUSB tasks. - tud_task(); - } else if (can_sleep) { + + if (can_sleep && !usb_enabled) { app_prepare_for_sleep(); sleep(4); app_wake_from_sleep(); -- cgit v1.2.3 From 74950b110ebed7baf5cd2fa9e86cbf6d40e0fcb1 Mon Sep 17 00:00:00 2001 From: Joey Castillo Date: Sun, 29 Aug 2021 15:42:37 -0400 Subject: enable USB earlier so app can log sooner --- watch-library/main.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'watch-library/main.c') diff --git a/watch-library/main.c b/watch-library/main.c index 3133c2d2..af34d850 100755 --- a/watch-library/main.c +++ b/watch-library/main.c @@ -38,6 +38,15 @@ int main(void) { // ASF code. Initialize the MCU with configuration options from Atmel Studio. init_mcu(); + // check if we are plugged into USB power. + watch_enable_digital_input(VBUS_DET); + watch_enable_pull_down(VBUS_DET); + if (watch_get_pin_level(VBUS_DET)) { + // if so, enable USB functionality. + _watch_enable_usb(); + } + watch_disable_digital_input(VBUS_DET); + // User code. Give the app a chance to initialize its data structures and state. app_init(); @@ -55,15 +64,6 @@ int main(void) { // Watch library code. Set initial parameters for the device and enable the RTC. _watch_init(); - // check if we are plugged into USB power. - watch_enable_digital_input(VBUS_DET); - watch_enable_pull_down(VBUS_DET); - if (watch_get_pin_level(VBUS_DET)) { - // if so, enable USB functionality. - _watch_enable_usb(); - } - watch_disable_digital_input(VBUS_DET); - // User code. Give the app a chance to enable and set up peripherals. app_setup(); -- cgit v1.2.3