diff --git a/hardware/mux_rpi/mux_rpi.ino b/hardware/mux_rpi/mux_rpi.ino index d74841d..7540828 100644 --- a/hardware/mux_rpi/mux_rpi.ino +++ b/hardware/mux_rpi/mux_rpi.ino @@ -23,24 +23,39 @@ bool ledState = false; void TC5_Handler(void); void timerConfigure(int sampleRate); +// General UART configuration struct +struct { + // Whether or not the USB is used for serial communications (stand alone builds) + bool Serial = true; + // Whether or not the i2c is used for serial communication (_controller builds) + bool uartI2C = false; + + // Use the generic uart setup with the rpi + bool Serial1 = true; + // Use uart0 on the rpi + bool rpiUART0 = true; + // Use uart5 on the rpi + bool rpiUART5 = true; +} uartConfig; + // Additional serial ports for communication <> RPI // https://learn.adafruit.com/using-atsamd21-sercom-to-add-more-spi-i2c-serial-ports?view=all#creating-a-new-serial // D11 (RX) -- GPIO 8 (TX) // D10 (TX) -- GPIO 10 (RX) // MISO (RX) -- GPIO 32 (TX) // MOSI (TX) -- GPIO 33 (RX) - Uart rpiUART0 (&sercom1, 10, 11, SERCOM_RX_PAD_0, UART_TX_PAD_2); Uart rpiUART5 (&sercom4, PIN_SPI_MISO, PIN_SPI_MOSI, SERCOM_RX_PAD_0, UART_TX_PAD_2); - void SERCOM1_Handler() { rpiUART0.IrqHandler(); } - void SERCOM4_Handler() { rpiUART5.IrqHandler(); } +// Misc uart functions (defined after setup/loop) +void setupUARTs(); + // Setup stuff needed for build void setup() { // Setup board LED @@ -50,11 +65,8 @@ void setup() { timerConfigure(TIMER_MILLIS); timerStartCounter(); - // Setup serial ports - Serial.begin(115200); - Serial1.begin(115200); - rpiUART0.begin(115200); - rpiUART5.begin(115200); + // Setup UARTS + setupUARTs(); // Assign SERCOM functionality pinPeripheral(10, PIO_SERCOM); @@ -64,23 +76,58 @@ void setup() { // Main function int serialIncomingByte; void loop() { - while (Serial.available()) { + while (uartConfig.Serial && Serial.available()) { uartBufferOutbound.push(Serial.read()); - Serial1.write(uartBufferOutbound.last()); - rpiUART0.write(uartBufferOutbound.last()); - rpiUART5.write(uartBufferOutbound.last()); + if (uartConfig.Serial1) { + Serial1.write(uartBufferOutbound.last()); + } + if (uartConfig.rpiUART0) { + rpiUART0.write(uartBufferOutbound.last()); + } + if (uartConfig.rpiUART5) { + rpiUART5.write(uartBufferOutbound.last()); + } } - while (Serial1.available()) { + while (uartConfig.Serial1 && Serial1.available()) { uartBufferInbound.push(Serial1.read()); - Serial.write(uartBufferInbound.last()); + if (uartConfig.Serial) { + Serial.write(uartBufferInbound.last()); + } } - while (rpiUART0.available()) { + while (uartConfig.rpiUART0 && rpiUART0.available()) { uartBufferInbound.push(rpiUART0.read()); - Serial.write(uartBufferInbound.last()); + if (uartConfig.Serial) { + Serial.write(uartBufferInbound.last()); + } } - while (rpiUART5.available()) { + while (uartConfig.rpiUART5 && rpiUART5.available()) { uartBufferInbound.push(rpiUART5.read()); - Serial.write(uartBufferInbound.last()); + if (uartConfig.Serial) { + Serial.write(uartBufferInbound.last()); + } + } +} + +// Method to setup / reset the uarts +void setupUARTs() { + // Close ALL serial ports and re-enable per config (config can change during runtime) + Serial.end(); + Serial1.end(); + rpiUART0.end(); + rpiUART5.end(); + + // Setup serial ports + if (uartConfig.Serial) { + Serial.begin(115200); + } + if (uartConfig.Serial1) { + Serial1.begin(115200); + } + if (uartConfig.rpiUART0) { + rpiUART0.begin(115200); + } + if (uartConfig.rpiUART5) { + rpiUART5.begin(115200); } }