]> git.earman.xyz Git - sensor-watch.git/commitdiff
add alternate _write for debugging USB issues
authorJoey Castillo <jose.castillo@gmail.com>
Sat, 28 Aug 2021 23:36:00 +0000 (19:36 -0400)
committerJoey Castillo <jose.castillo@gmail.com>
Sat, 28 Aug 2021 23:44:52 +0000 (19:44 -0400)
watch-library/watch/tusb_config.h
watch-library/watch/watch_private.c
watch-library/watch/watch_uart.c

index fa981f3af4f11ebe81602ac440d3a56db8bbe2d8..a22b2b9976df18f26c0cfd300cb59edaeaacee45 100644 (file)
@@ -43,8 +43,8 @@
 
 #define CFG_TUSB_OS                 OPT_OS_NONE
 
-// Do not use TinyUSB debug, as our printf method prints stuff to the USB console.
-// if you must, modify _write in watch_private.c to use an alternate destination like a UART.
+// disable TinyUSB debug. our printf method prints stuff to the USB console, so you just get infinite noise.
+// if you need to debug tinyUSB issues, use the alternate _write function in watch_private.c to echo to the UART.
 #define CFG_TUSB_DEBUG           0
 
 /* USB DMA on some MCUs can only access a specific SRAM region with restriction on alignment.
index c9f0b3c0fafc6d218799d533b2a93b2b54834c83..550419a223e6f9dac5cf65fbb3e2909ba103dc2c 100644 (file)
@@ -106,6 +106,15 @@ int _read() {
     return 0;
 }
 
+// Alternate function that outputs to the debug UART. useful for debugging USB issues.
+// int _write(int file, char *ptr, int len) {
+//     (void)file;
+//     int pos = 0;
+//     while(pos < len) watch_debug_putc(ptr[pos++]);
+
+//     return 0;
+// }
+
 void USB_Handler(void) {
     tud_int_handler(0);
 }
index 68ac872e045ff6cfe0cb953338e4ef7396cde1f0..afebff1b7744901a216e02822659d8c6d229a02e 100644 (file)
 #include "peripheral_clk_config.h"
 
 void watch_enable_debug_uart(uint32_t baud) {
-    /// FIXME: UART baud rate calculation will be incorrect if plugged into USB / running at 16 MHz
-    uint64_t br = (uint64_t)65536 * (CONF_CPU_FREQUENCY - 16 * baud) / CONF_CPU_FREQUENCY;
+    uint8_t fsel = hri_oscctrl_get_OSC16MCTRL_FSEL_bf(OSCCTRL, OSCCTRL_OSC16MCTRL_MASK);
+    uint32_t freq = 0;
+    switch (fsel) {
+        case OSCCTRL_OSC16MCTRL_FSEL_4_Val:
+            freq = 4000000;
+            break;
+        case OSCCTRL_OSC16MCTRL_FSEL_8_Val:
+            freq = 8000000;
+            break;
+        case OSCCTRL_OSC16MCTRL_FSEL_12_Val:
+            freq = 12000000;
+            break;
+        case OSCCTRL_OSC16MCTRL_FSEL_16_Val:
+            freq = 16000000;
+            break;
+        default:
+            return;
+    }
+    uint64_t br = (uint64_t)65536 * ((freq * 4) - 16 * baud) / (freq * 4);
 
     gpio_set_pin_direction(D1, GPIO_DIRECTION_IN);
     gpio_set_pin_function(D1, PINMUX_PB00C_SERCOM3_PAD2);