From 391833f71473d32fc0e436bc9c1246968ee6086d Mon Sep 17 00:00:00 2001 From: Delan Azabani Date: Sun, 26 Nov 2023 20:11:35 +0800 Subject: [PATCH] firmware: pinout v2: move sunk to uart1, move sunm to pio uart --- debug1.patch | 34 +++++++++++++++ debug2.patch | 28 ++++++++++++ doc/firmware.md | 5 ++- platformio.ini | 2 +- src/config.h | 75 ++++++++++++++++++++++----------- src/main.cc | 69 ++++++++++++++++-------------- src/menu.cc | 11 ++--- src/panic.h | 1 + src/pinout.cc | 110 ++++++++++++++++++++++++++++++++++++++++++++++++ src/pinout.h | 35 +++++++++++++++ src/settings.h | 2 + src/sunk.cc | 5 ++- src/sunk.h | 1 + 13 files changed, 310 insertions(+), 68 deletions(-) create mode 100644 debug1.patch create mode 100644 debug2.patch create mode 100644 src/pinout.cc create mode 100644 src/pinout.h diff --git a/debug1.patch b/debug1.patch new file mode 100644 index 0000000..b6e310c --- /dev/null +++ b/debug1.patch @@ -0,0 +1,34 @@ +diff --git a/.pio/libdeps/pico/Adafruit TinyUSB Library/src/Adafruit_TinyUSB.h b/.pio/libdeps/pico/Adafruit TinyUSB Library/src/Adafruit_TinyUSB.h +index 1d0808c..25b5f95 100644 +--- a/.pio/libdeps/pico/Adafruit TinyUSB Library/src/Adafruit_TinyUSB.h ++++ b/.pio/libdeps/pico/Adafruit TinyUSB Library/src/Adafruit_TinyUSB.h +@@ -25,6 +25,9 @@ + #ifndef ADAFRUIT_TINYUSB_H_ + #define ADAFRUIT_TINYUSB_H_ + ++#include ++extern HardwareSerial *TinyUSB_Serial_Debug; ++ + // Error message for Core that must select TinyUSB via menu + #if !defined(USE_TINYUSB) && ( defined(ARDUINO_ARCH_SAMD) || \ + (defined(ARDUINO_ARCH_RP2040) && !defined(ARDUINO_ARCH_MBED)) ) +diff --git a/.pio/libdeps/pico/Adafruit TinyUSB Library/src/arduino/ports/rp2040/Adafruit_TinyUSB_rp2040.cpp b/.pio/libdeps/pico/Adafruit TinyUSB Library/src/arduino/ports/rp2040/Adafruit_TinyUSB_rp2040.cpp +index 8af08a4..b0449dd 100644 +--- a/.pio/libdeps/pico/Adafruit TinyUSB Library/src/arduino/ports/rp2040/Adafruit_TinyUSB_rp2040.cpp ++++ b/.pio/libdeps/pico/Adafruit TinyUSB Library/src/arduino/ports/rp2040/Adafruit_TinyUSB_rp2040.cpp +@@ -141,13 +141,14 @@ void TinyUSB_Device_Task(void) { + + // Debug log with Serial1 + #if CFG_TUSB_DEBUG ++HardwareSerial *TinyUSB_Serial_Debug = nullptr; + int serial1_printf(const char *__restrict format, ...) { + char buf[256]; + int len; + va_list ap; + va_start(ap, format); + len = vsnprintf(buf, sizeof(buf), format, ap); +- Serial1.write(buf); ++ if (TinyUSB_Serial_Debug) TinyUSB_Serial_Debug->write(buf); + va_end(ap); + return len; + } diff --git a/debug2.patch b/debug2.patch new file mode 100644 index 0000000..bfb72dc --- /dev/null +++ b/debug2.patch @@ -0,0 +1,28 @@ +diff --git a/cores/rp2040/debug_internal.h b/cores/rp2040/debug_internal.h +index 81026fb..e8b206f 100644 +--- a/cores/rp2040/debug_internal.h ++++ b/cores/rp2040/debug_internal.h +@@ -20,6 +20,9 @@ + + #pragma once + ++typedef int SerialPrintf(const char *format, ...) __attribute__ ((format (printf, 1, 2))); ++extern SerialPrintf *DEBUG_RP2040_PRINTF; ++ + #if !defined(DEBUG_RP2040_PORT) + #define DEBUGV(...) do { } while(0) + #define DEBUGCORE(...) do { } while(0) +diff --git a/cores/rp2040/main.cpp b/cores/rp2040/main.cpp +index fb1ed21..a8e98f9 100644 +--- a/cores/rp2040/main.cpp ++++ b/cores/rp2040/main.cpp +@@ -24,6 +24,9 @@ + #include + #include + ++static int dummyDebugPrintf(const char *, ...) { return -1; } ++SerialPrintf *DEBUG_RP2040_PRINTF = dummyDebugPrintf; ++ + RP2040 rp2040; + extern "C" { + volatile bool __otherCoreIdled = false; diff --git a/doc/firmware.md b/doc/firmware.md index 2e0153c..371e19a 100644 --- a/doc/firmware.md +++ b/doc/firmware.md @@ -6,9 +6,12 @@ firmware ## how to build the firmware 1. fix the -DCFG_TUSB_CONFIG_FILE in platformio.ini for your absolute path -2. apply patches to <.pio/libdeps/pico/Adafruit TinyUSB Library/src/host/usbh.c> (version 2.0.1) +2. apply patches to [Adafruit TinyUSB Library](https://github.com/adafruit/Adafruit_TinyUSB_Arduino) (version 2.0.1) 1. `git apply tinyusb1.patch` — [tinyusb1.patch](tinyusb1.patch) fixes a race condition in enumeration when two devices are connected at the same time (hathach/tinyusb#1786, upstreamed in 2.0.2 as hathach/tinyusb#1960) 2. `git apply tinyusb2.patch` — [tinyusb2.patch](tinyusb2.patch) improves compatibility with Microsoft Wired Keyboard 600 (045E:0750) and 400 (045E:0752) when CFG_TUSB_DEBUG < 2 by adding a delay in tuh_control_xfer + 3. `git apply debug1.patch` — [debug1.patch](debug1.patch) +3. apply patches to [framework-arduinopico](https://github.com/earlephilhower/arduino-pico) (version 1.30101.0) + 1. `git -C ~/.platformio/packages/framework-arduinopico apply $PWD/debug2.patch` — [debug2.patch](debug2.patch) ### linux users diff --git a/platformio.ini b/platformio.ini index 48cb036..9ae3ee2 100644 --- a/platformio.ini +++ b/platformio.ini @@ -20,7 +20,7 @@ lib_deps = https://github.com/adafruit/Adafruit_SSD1306.git#2.5.7 https://github.com/adafruit/Adafruit_TinyUSB_Arduino.git#2.0.1 https://github.com/sekigon-gonnoc/Pico-PIO-USB.git#0.5.2 -build_flags = -DUSB3SUN_VERSION=\"1.5\" -DUSE_TINYUSB -DCFG_TUSB_CONFIG_FILE=\"/home/delan/code/usb3sun/tusb_config.h\" ; -DCFG_TUSB_DEBUG=3 +build_flags = -DUSB3SUN_VERSION=\"1.5\" -DUSE_TINYUSB -DCFG_TUSB_CONFIG_FILE=\"/home/delan/code/usb3sun/tusb_config.h\" ; -DCFG_TUSB_DEBUG=3 -DDEBUG_RP2040_WIRE -DDEBUG_RP2040_SPI -DDEBUG_RP2040_CORE board_build.f_cpu = 120000000L board_build.filesystem_size = 64k ; build_type = debug diff --git a/src/config.h b/src/config.h index 6dcfb3f..4cbfac9 100644 --- a/src/config.h +++ b/src/config.h @@ -1,7 +1,13 @@ #define DISPLAY_ROTATION 0 // 0|1|2|3 where 0 means GND pin is bottom left +// FIXME no longer used #define SUNM_BAUD 9600 // 1200|2400|4800|9600 where higher is smoother and more responsive -#define DEBUG_LOGGING // allow any logging to Serial or Serial1 +#define DEBUG_LOGGING // allow any logging +#define DEBUG_OVER_CDC // log to Serial (USB CDC); excludes TinyUSB debugging +#define DEBUG_OVER_UART // log to Serial1 (UART0); includes TinyUSB debugging + // (ignored in pinout v1 when SUNK_ENABLE is defined) +#define DEBUG_UART_BAUD 115200 // only 115200 works with stock picoprobe firmware + // #define DEBUG_TIMINGS // log time spent on critical operations // #define BUZZER_VERBOSE // log buzzer state changes for debugging // #define SUNK_VERBOSE // log keyboard tx for debugging @@ -16,49 +22,68 @@ #define USB1_DP 4 // GP# number for USB root port 1 D+ #define USB1_DM (USB_DP+1) // GP# number for USB root port 1 D- (always D+ GP# + 1) #define BUZZER_PIN 28 // GP# number for positive of passive piezo buzzer +#define PINOUT_V2_PIN 7 // GP# number for detecting pinout v2 (rev A4+) // the following must be GP# numbers valid for... +#define DEBUG_UART_TX 0 // ...UART0 TX: connect to picoprobe GP5 +#define DEBUG_UART_RX 1 // ...UART0 RX: connect to picoprobe GP4 #define DISPLAY_SCL 17 // ...I2C0 SCL: SCL pin of SSD1306 #define DISPLAY_SDA 16 // ...I2C0 SDA: SDA pin of SSD1306 +// pinout v1/v2 config // the following must be GP# numbers valid for... - // pin 1: 0 V (purple) - // pin 2: 0 V (brown) - // pin 3: +5 Vdc (blue) -#define SUN_MTX 8 // ...UART1 TX: pin 4: mouse tx (gray) -#define SUN_MRX 9 // ...UART1 RX: n/c: mouse rx -#define SUN_KRX 13 // ...UART0 RX: pin 5: keyboard rx (red) -#define SUN_KTX 12 // ...UART0 TX: pin 6: keyboard tx (green) -#define POWER_KEY 15 // ...any: pin 7: power key (yellow) - // pin 8: +5 Vdc (orange) + // pin 1: 0 V (purple) + // pin 2: 0 V (brown) + // pin 3: +5 Vdc (blue) + // n/c: mouse rx +#define SUN_MRX_V1 9 // ...UART1 RX (required by SerialUART api) + // pin 4: mouse tx (gray) +#define SUN_MTX_V1 8 // ...UART1 TX +#define SUN_MTX_V2 6 // ...any + // pin 5: keyboard rx (red) +#define SUN_KRX_V1 13 // ...UART0 RX +#define SUN_KRX_V2 9 // ...UART1 RX + // pin 6: keyboard tx (green) +#define SUN_KTX_V1 12 // ...UART0 TX +#define SUN_KTX_V2 8 // ...UART1 TX + // pin 7: power key (yellow) +#define POWER_KEY 15 // ...any + // pin 8: +5 Vdc (orange) +#define DEBUG_UART Serial1 // UART0 +#define SUNK_UART_V1 Serial1 // UART0 +#define SUNK_UART_V2 Serial2 // UART1 +#define SUNM_UART_V1 Serial2 // UART1 -// send output over Serial1 instead of Serial (disables Sun keyboard interface) -// #define PICOPROBE_ENABLE -#if !defined(PICOPROBE_ENABLE) #define SUNK_ENABLE -#endif -#define PICOPROBE_BAUD 115200 // values other than 9600 may require PuTTY/minicom +#define SUNM_ENABLE +// FIXME broken in pinout v2 // fake Sun host for loopback testing (disables Sun mouse interface) // #define FAKE_SUN_ENABLE #if !defined(FAKE_SUN_ENABLE) #define SUNM_ENABLE #endif +// FIXME broken in pinout v2 // the following must be GP# numbers valid for... #define FAKE_SUN_KRX 8 // ...UART1 TX: connect to SUN_KRX #define FAKE_SUN_KTX 9 // ...UART1 RX: connect to SUN_KTX -#define PICOPROBE_TX 0 // ...UART0 TX: connect to picoprobe GP5 -#define PICOPROBE_RX 1 // ...UART0 RX: connect to picoprobe GP4 -#ifdef PICOPROBE_ENABLE -#define Sprint(...) (Serial1.print(__VA_ARGS__), Serial1.flush()) -#define Sprintln(...) (Serial1.println(__VA_ARGS__), Serial1.flush()) -#define Sprintf(...) (Serial1.printf(__VA_ARGS__), Serial1.flush()) -#elif defined(DEBUG_LOGGING) -#define Sprint(...) (Serial.print(__VA_ARGS__), Serial.flush()) -#define Sprintln(...) (Serial.println(__VA_ARGS__), Serial.flush()) -#define Sprintf(...) (Serial.printf(__VA_ARGS__), Serial.flush()) +#if defined(DEBUG_LOGGING) +#define DEBUG_PRINT_FLUSH(port, method, ...) (port.method(__VA_ARGS__), port.flush()) +#if defined(DEBUG_OVER_CDC) && defined(DEBUG_OVER_UART) +#define Sprint(...) do { DEBUG_PRINT_FLUSH(Serial, print, __VA_ARGS__); if (pinout.canDebugOverUart) DEBUG_PRINT_FLUSH(DEBUG_UART, print, __VA_ARGS__); } while (0) +#define Sprintln(...) do { DEBUG_PRINT_FLUSH(Serial, println, __VA_ARGS__); if (pinout.canDebugOverUart) DEBUG_PRINT_FLUSH(DEBUG_UART, println, __VA_ARGS__); } while (0) +#define Sprintf(...) do { DEBUG_PRINT_FLUSH(Serial, printf, __VA_ARGS__); if (pinout.canDebugOverUart) DEBUG_PRINT_FLUSH(DEBUG_UART, printf, __VA_ARGS__); } while (0) +#elif defined(DEBUG_OVER_CDC) +#define Sprint(...) DEBUG_PRINT_FLUSH(Serial, print, __VA_ARGS__) +#define Sprintln(...) DEBUG_PRINT_FLUSH(Serial, println, __VA_ARGS__) +#define Sprintf(...) DEBUG_PRINT_FLUSH(Serial, printf, __VA_ARGS__) +#elif defined(DEBUG_OVER_UART) +#define Sprint(...) do { if (pinout.canDebugOverUart) DEBUG_PRINT_FLUSH(DEBUG_UART, print, __VA_ARGS__); } while (0) +#define Sprintln(...) do { if (pinout.canDebugOverUart) DEBUG_PRINT_FLUSH(DEBUG_UART, println, __VA_ARGS__); } while (0) +#define Sprintf(...) do { if (pinout.canDebugOverUart) DEBUG_PRINT_FLUSH(DEBUG_UART, printf, __VA_ARGS__); } while (0) +#endif #else #define Sprint(...) do {} while (0) #define Sprintln(...) do {} while (0) diff --git a/src/main.cc b/src/main.cc index b217237..2672072 100644 --- a/src/main.cc +++ b/src/main.cc @@ -5,6 +5,7 @@ #include #include #include +#include extern "C" { #include @@ -18,6 +19,7 @@ extern "C" { #include "buzzer.h" #include "display.h" #include "menu.h" +#include "pinout.h" #include "settings.h" #include "state.h" #include "sunk.h" @@ -51,6 +53,7 @@ struct { std::atomic wait = true; +Pinout pinout; State state; Buzzer buzzer; Settings settings; @@ -149,6 +152,14 @@ void setup() { pinMode(LED_BUILTIN, OUTPUT); digitalWrite(LED_BUILTIN, HIGH); + // check for pinout v2 (active high) + pinMode(PINOUT_V2_PIN, INPUT_PULLDOWN); + if (digitalRead(PINOUT_V2_PIN) == HIGH) { + pinout.v2(); + } else { + pinout.v1(); + } + // needs to be done manually when using FreeRTOS and/or TinyUSB Serial.begin(115200); @@ -168,31 +179,14 @@ void setup() { Settings::begin(); settings.readAll(); + pinout.beginSun(); -#if defined(PICOPROBE_ENABLE) - Serial1.end(); // needed under CFG_TUSB_DEBUG - Serial1.setPinout(PICOPROBE_TX, PICOPROBE_RX); - Serial1.setFIFOSize(4096); - Serial1.begin(115200, SERIAL_8N1); -#elif defined(SUNK_ENABLE) - // gpio invert must be set *after* setPinout/begin - Serial1.setPinout(SUN_KTX, SUN_KRX); - Serial1.begin(1200, SERIAL_8N1); - gpio_set_outover(SUN_KTX, GPIO_OVERRIDE_INVERT); - gpio_set_inover(SUN_KRX, GPIO_OVERRIDE_INVERT); -#endif #if defined(FAKE_SUN_ENABLE) // gpio invert must be set *after* setPinout/begin Serial2.setPinout(FAKE_SUN_KRX, FAKE_SUN_KTX); Serial2.begin(1200, SERIAL_8N1); gpio_set_outover(FAKE_SUN_KRX, GPIO_OVERRIDE_INVERT); gpio_set_inover(FAKE_SUN_KTX, GPIO_OVERRIDE_INVERT); -#elif defined(SUNM_ENABLE) - // gpio invert must be set *after* setPinout/begin - Serial2.setPinout(SUN_MTX, SUN_MRX); - Serial2.begin(settings.mouseBaudReal(), SERIAL_8N1); - gpio_set_outover(SUN_MTX, GPIO_OVERRIDE_INVERT); - gpio_set_inover(SUN_MRX, GPIO_OVERRIDE_INVERT); #endif for (int i = 0; i < splash_height; i += 2) { @@ -266,18 +260,18 @@ void loop() { } #ifdef SUNK_ENABLE -void serialEvent1() { - while (Serial1.available() > 0) { - uint8_t command = Serial1.read(); +void sunkEvent() { + while (pinout.sunk->available() > 0) { + uint8_t command = pinout.sunk->read(); Sprintf("sun keyboard: rx command %02Xh\n", command); switch (command) { case SUNK_RESET: // self test fail: - // Serial.write(0x7E); - // Serial.write(0x01); - Serial1.write(SUNK_RESET_RESPONSE); - Serial1.write(0x04); - Serial1.write(0x7F); // TODO optional make code + // pinout.sunk->write(0x7E); + // pinout.sunk->write(0x01); + pinout.sunk->write(SUNK_RESET_RESPONSE); + pinout.sunk->write(0x04); + pinout.sunk->write(0x7F); // TODO optional make code break; case SUNK_BELL_ON: state.bell = true; @@ -294,8 +288,8 @@ void serialEvent1() { state.clickEnabled = false; break; case SUNK_LED: { - while (Serial1.peek() == -1) delay(1); - uint8_t status = Serial1.read(); + while (pinout.sunk->peek() == -1) delay(1); + uint8_t status = pinout.sunk->read(); Sprintf("sun keyboard: led status %02Xh\n", status); state.num = status & 1 << 0; state.compose = status & 1 << 1; @@ -307,14 +301,26 @@ void serialEvent1() { rp2040.fifo.push_nb((uint32_t)Message::UHID_LED_FROM_STATE); } break; case SUNK_LAYOUT: { - Serial1.write(SUNK_LAYOUT_RESPONSE); + pinout.sunk->write(SUNK_LAYOUT_RESPONSE); // UNITED STATES (TODO alternate layouts) uint8_t layout = 0b00000000; - Serial1.write(&layout, 1); + pinout.sunk->write(&layout, 1); } break; } } } + +void serialEvent1() { +#if defined(SUNK_ENABLE) + sunkEvent(); +#endif +} + +void serialEvent2() { +#if defined(SUNK_ENABLE) + sunkEvent(); +#endif +} #endif #ifdef FAKE_SUN_ENABLE @@ -358,6 +364,7 @@ void setup1() { pio_usb_configuration_t pio_cfg = PIO_USB_DEFAULT_CONFIG; pio_cfg.pin_dp = USB0_DP; + pio_cfg.sm_tx = 1; // tuh_configure -> pico pio hcd_configure -> memcpy to static global USBHost.configure_pio_usb(1, &pio_cfg); @@ -598,7 +605,7 @@ void tuh_hid_report_received_cb(uint8_t dev_addr, uint8_t instance, uint8_t cons (uint8_t) mreport->x, (uint8_t) -mreport->y, 0, 0, }; #ifdef SUNM_ENABLE - size_t len = Serial2.write(result, sizeof(result) / sizeof(*result)); + size_t len = pinout.sunm->write(result, sizeof(result) / sizeof(*result)); #ifdef SUNM_VERBOSE Sprintf("sun mouse: tx %02Xh %02Xh %02Xh %02Xh %02Xh = %zu\n", result[0], result[1], result[2], result[3], result[4], len); diff --git a/src/menu.cc b/src/menu.cc index 28a13f9..63c131a 100644 --- a/src/menu.cc +++ b/src/menu.cc @@ -8,6 +8,7 @@ #include "bindings.h" #include "display.h" #include "hostid.h" +#include "pinout.h" #include "settings.h" #include "state.h" #include "sunk.h" @@ -158,10 +159,7 @@ void MenuView::sel(uint8_t usbkSelector) { case (size_t)MenuItem::MouseBaud: ++settings.mouseBaud(); settings.write(settings.mouseBaud_field); - Serial2.end(); - Serial2.begin(settings.mouseBaudReal(), SERIAL_8N1); - gpio_set_outover(SUN_MTX, GPIO_OVERRIDE_INVERT); - gpio_set_outover(SUN_MRX, GPIO_OVERRIDE_INVERT); + pinout.restartSunm(); break; } break; @@ -181,10 +179,7 @@ void MenuView::sel(uint8_t usbkSelector) { case (size_t)MenuItem::MouseBaud: --settings.mouseBaud(); settings.write(settings.mouseBaud_field); - Serial2.end(); - Serial2.begin(settings.mouseBaudReal(), SERIAL_8N1); - gpio_set_outover(SUN_MTX, GPIO_OVERRIDE_INVERT); - gpio_set_outover(SUN_MRX, GPIO_OVERRIDE_INVERT); + pinout.restartSunm(); break; } break; diff --git a/src/panic.h b/src/panic.h index c0316b6..8b1ea5d 100644 --- a/src/panic.h +++ b/src/panic.h @@ -2,6 +2,7 @@ #define USB3SUN_PANIC_H #include "config.h" +#include "pinout.h" #include diff --git a/src/pinout.cc b/src/pinout.cc new file mode 100644 index 0000000..f512e39 --- /dev/null +++ b/src/pinout.cc @@ -0,0 +1,110 @@ +#include "config.h" +#include "pinout.h" +#include "settings.h" + +#include + +// TODO add Print::vprintf in ArduinoCore-API Print.h +#if defined(DEBUG_LOGGING) +static int printfDebug(const char *format, ...) { + char buf[256]; + int len; + va_list ap; + va_start(ap, format); + len = vsnprintf(buf, sizeof(buf), format, ap); +#if defined(DEBUG_OVER_CDC) + Serial.write(buf); + Serial.flush(); +#endif +#if defined(DEBUG_OVER_UART) + DEBUG_UART.write(buf); + DEBUG_UART.flush(); +#endif + va_end(ap); + return len; +} +#endif + +Pinout::Pinout() : sunmV2(SUN_MTX_V2, SerialPIO::NOPIN) {} + +void Pinout::v1() { +#if defined(DEBUG_LOGGING) +#if defined(DEBUG_OVER_CDC) + debugOverCdc(); +#endif +#if defined(DEBUG_OVER_UART) && !defined(SUNK_ENABLE) + debugOverUart(); +#endif +#endif + Sprintln("pinout: v1"); +} + +void Pinout::v2() { + version = 2; + sunk = &SUNK_UART_V2; + sunm = &sunmV2; + sunkTx = SUN_KTX_V2; + sunkRx = SUN_KRX_V2; + sunmTx = SUN_MTX_V2; + sunkUart = &SUNK_UART_V2; +#if defined(DEBUG_LOGGING) +#if defined(DEBUG_OVER_CDC) + debugOverCdc(); +#endif +#if defined(DEBUG_OVER_UART) + debugOverUart(); +#endif +#endif + Sprintln("pinout: v2"); +} + +void Pinout::beginSun() { +#if defined(SUNK_ENABLE) + sunk->end(); + sunkUart->setPinout(sunkTx, sunkRx); + sunk->begin(1200, SERIAL_8N1); + // gpio invert must be set *after* setPinout/begin + gpio_set_outover(sunkTx, GPIO_OVERRIDE_INVERT); + gpio_set_inover(sunkRx, GPIO_OVERRIDE_INVERT); +#endif +#if defined(SUNM_ENABLE) + sunm->end(); + switch (version) { + case 1: + sunmV1.setPinout(SUN_MTX_V1, SUN_MRX_V1); + break; + case 2: + // do nothing + break; + } + sunm->begin(settings.mouseBaudReal(), SERIAL_8N1); + // gpio invert must be set *after* setPinout/begin + gpio_set_outover(sunmTx, GPIO_OVERRIDE_INVERT); +#endif +} + +void Pinout::restartSunm() { +#if defined(SUNM_ENABLE) + sunm->end(); + sunm->begin(settings.mouseBaudReal(), SERIAL_8N1); + // gpio invert must be set *after* setPinout/begin + gpio_set_outover(sunmTx, GPIO_OVERRIDE_INVERT); +#endif +} + +void Pinout::debugOverCdc() { + Serial.begin(115200); + DEBUG_RP2040_PRINTF = printfDebug; +} + +void Pinout::debugOverUart() { + DEBUG_UART.end(); + DEBUG_UART.setPinout(DEBUG_UART_TX, DEBUG_UART_RX); + DEBUG_UART.setFIFOSize(4096); + DEBUG_UART.begin(DEBUG_UART_BAUD, SERIAL_8N1); + DEBUG_RP2040_PRINTF = printfDebug; +#if CFG_TUSB_DEBUG + TinyUSB_Serial_Debug = &DEBUG_UART; +#endif + canDebugOverUart = true; +} diff --git a/src/pinout.h b/src/pinout.h new file mode 100644 index 0000000..27cb02a --- /dev/null +++ b/src/pinout.h @@ -0,0 +1,35 @@ +#ifndef USB3SUN_PINOUT_H +#define USB3SUN_PINOUT_H + +#include "config.h" + +#include +#include + +struct Pinout { + Pinout(); + void v1(); + void v2(); + void beginSun(); + void restartSunm(); + + int version = 1; + bool canDebugOverUart = false; + HardwareSerial *sunk = &SUNK_UART_V1; + HardwareSerial *sunm = &SUNM_UART_V1; + +private: + pin_size_t sunkTx = SUN_KTX_V1; + pin_size_t sunkRx = SUN_KRX_V1; + pin_size_t sunmTx = SUN_MTX_V1; + SerialUART *sunkUart = &SUNK_UART_V1; + SerialUART &sunmV1 = SUNM_UART_V1; + SerialPIO sunmV2; + + void debugOverCdc(); + void debugOverUart(); +}; + +extern Pinout pinout; + +#endif diff --git a/src/settings.h b/src/settings.h index fd24df7..0a6b884 100644 --- a/src/settings.h +++ b/src/settings.h @@ -6,6 +6,8 @@ #include #include +#include "pinout.h" + #define SETTING(_name, _version, _type, ...) \ struct _name##Setting { \ static const unsigned currentVersion = _version; \ diff --git a/src/sunk.cc b/src/sunk.cc index 3275586..2f28f44 100644 --- a/src/sunk.cc +++ b/src/sunk.cc @@ -6,6 +6,7 @@ #include "bindings.h" #include "buzzer.h" +#include "pinout.h" void sunkSend(bool make, uint8_t code) { static int activeCount = 0; @@ -21,7 +22,7 @@ void sunkSend(bool make, uint8_t code) { #ifdef SUNK_VERBOSE Sprintf("sun keyboard: tx command %02Xh\n", code); #endif - Serial1.write(code); + pinout.sunk->write(code); #endif if (activeCount <= 0) { @@ -30,7 +31,7 @@ void sunkSend(bool make, uint8_t code) { #ifdef SUNK_VERBOSE Sprintf("sun keyboard: idle\n"); #endif - Serial1.write(SUNK_IDLE); + pinout.sunk->write(SUNK_IDLE); #endif } diff --git a/src/sunk.h b/src/sunk.h index cdb7ee6..22806c8 100644 --- a/src/sunk.h +++ b/src/sunk.h @@ -8,6 +8,7 @@ #include #include "bindings.h" +#include "pinout.h" // internal flags (not part of real keycode) #define SUNK_SEND_SHIFT 0x100