Skip to content

Commit 10b869e

Browse files
Fix XIAO RP2040, allow swapping HW units 0/1 (#606)
Add plumbing to allow `Wire`, `Serial1`, `SPI1` to map to the 2nd hardware unit for devices where the PCB layout only brings out the 2nd port. Fix the Seeedstudio XAIO pins Fixes #594
1 parent c863dad commit 10b869e

File tree

5 files changed

+83
-21
lines changed

5 files changed

+83
-21
lines changed

cores/rp2040/SerialUART.cpp

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -350,18 +350,6 @@ SerialUART::operator bool() {
350350
return _running;
351351
}
352352

353-
#if defined(PIN_SERIAL1_RTS)
354-
SerialUART Serial1(uart0, PIN_SERIAL1_TX, PIN_SERIAL1_RX, PIN_SERIAL1_RTS, PIN_SERIAL1_CTS);
355-
#else
356-
SerialUART Serial1(uart0, PIN_SERIAL1_TX, PIN_SERIAL1_RX);
357-
#endif
358-
359-
#if defined(PIN_SERIAL2_RTS)
360-
SerialUART Serial2(uart1, PIN_SERIAL2_TX, PIN_SERIAL2_RX, PIN_SERIAL2_RTS, PIN_SERIAL2_CTS);
361-
#else
362-
SerialUART Serial2(uart1, PIN_SERIAL2_TX, PIN_SERIAL2_RX);
363-
#endif
364-
365353
void arduino::serialEvent1Run(void) {
366354
if (serialEvent1 && Serial1.available()) {
367355
serialEvent1();
@@ -406,10 +394,38 @@ void __not_in_flash_func(SerialUART::_handleIRQ)(bool inIRQ) {
406394
}
407395
}
408396

397+
#ifndef __SERIAL1_DEVICE
398+
#define __SERIAL1_DEVICE uart0
399+
#endif
400+
#ifndef __SERIAL2_DEVICE
401+
#define __SERIAL2_DEVICE uart1
402+
#endif
403+
404+
#if defined(PIN_SERIAL1_RTS)
405+
SerialUART Serial1(__SERIAL1_DEVICE, PIN_SERIAL1_TX, PIN_SERIAL1_RX, PIN_SERIAL1_RTS, PIN_SERIAL1_CTS);
406+
#else
407+
SerialUART Serial1(__SERIAL1_DEVICE, PIN_SERIAL1_TX, PIN_SERIAL1_RX);
408+
#endif
409+
410+
#if defined(PIN_SERIAL2_RTS)
411+
SerialUART Serial2(__SERIAL2_DEVICE, PIN_SERIAL2_TX, PIN_SERIAL2_RX, PIN_SERIAL2_RTS, PIN_SERIAL2_CTS);
412+
#else
413+
SerialUART Serial2(__SERIAL2_DEVICE, PIN_SERIAL2_TX, PIN_SERIAL2_RX);
414+
#endif
415+
416+
409417
static void __not_in_flash_func(_uart0IRQ)() {
410-
Serial1._handleIRQ();
418+
if (__SERIAL1_DEVICE == uart0) {
419+
Serial1._handleIRQ();
420+
} else {
421+
Serial2._handleIRQ();
422+
}
411423
}
412424

413425
static void __not_in_flash_func(_uart1IRQ)() {
414-
Serial2._handleIRQ();
426+
if (__SERIAL2_DEVICE == uart1) {
427+
Serial2._handleIRQ();
428+
} else {
429+
Serial1._handleIRQ();
430+
}
415431
}

libraries/SPI/SPI.cpp renamed to libraries/SPI/src/SPI.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,5 +307,12 @@ void SPIClassRP2040::setClockDivider(uint8_t uc_div) {
307307
(void) uc_div; // no-op
308308
}
309309

310-
SPIClassRP2040 SPI(spi0, PIN_SPI0_MISO, PIN_SPI0_SS, PIN_SPI0_SCK, PIN_SPI0_MOSI);
311-
SPIClassRP2040 SPI1(spi1, PIN_SPI1_MISO, PIN_SPI1_SS, PIN_SPI1_SCK, PIN_SPI1_MOSI);
310+
#ifndef __SPI0_DEVICE
311+
#define __SPI0_DEVICE spi0
312+
#endif
313+
#ifndef __SPI1_DEVICE
314+
#define __SPI1_DEVICE spi1
315+
#endif
316+
317+
SPIClassRP2040 SPI(__SPI0_DEVICE, PIN_SPI0_MISO, PIN_SPI0_SS, PIN_SPI0_SCK, PIN_SPI0_MOSI);
318+
SPIClassRP2040 SPI1(__SPI1_DEVICE, PIN_SPI1_MISO, PIN_SPI1_SS, PIN_SPI1_SCK, PIN_SPI1_MOSI);
File renamed without changes.

libraries/Wire/src/Wire.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -377,5 +377,12 @@ void TwoWire::onRequest(void(*function)(void)) {
377377
_onRequestCallback = function;
378378
}
379379

380-
TwoWire Wire(i2c0, PIN_WIRE0_SDA, PIN_WIRE0_SCL);
381-
TwoWire Wire1(i2c1, PIN_WIRE1_SDA, PIN_WIRE1_SCL);
380+
#ifndef __WIRE0_DEVICE
381+
#define __WIRE0_DEVICE i2c0
382+
#endif
383+
#ifndef __WIRE1_DEVICE
384+
#define __WIRE1_DEVICE i2c1
385+
#endif
386+
387+
TwoWire Wire(__WIRE0_DEVICE, PIN_WIRE0_SDA, PIN_WIRE0_SCL);
388+
TwoWire Wire1(__WIRE1_DEVICE, PIN_WIRE1_SDA, PIN_WIRE1_SCL);

variants/seeed_xiao_rp2040/pins_arduino.h

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,27 @@
88
#define PIN_LED_R (17u)
99
#define PIN_LED_G (16u)
1010
#define PIN_LED_B (25u)
11+
#define LED_BUILTIN PIN_LED
12+
13+
// Digital pins
14+
static const uint8_t D0 = (26u);
15+
static const uint8_t D1 = (27u);
16+
static const uint8_t D2 = (28u);
17+
static const uint8_t D3 = (29u);
18+
static const uint8_t D4 = (6u);
19+
static const uint8_t D5 = (7u);
20+
static const uint8_t D6 = (0u);
21+
static const uint8_t D7 = (1u);
22+
static const uint8_t D8 = (2u);
23+
static const uint8_t D9 = (4u);
24+
static const uint8_t D10 = (3u);
25+
26+
// Analog pins
27+
static const uint8_t A0 = (26u);
28+
static const uint8_t A1 = (27u);
29+
static const uint8_t A2 = (28u);
30+
static const uint8_t A3 = (29u);
31+
#define ADC_RESOLUTION 12
1132

1233
// NeoPixel
1334
#define PIN_NEOPIXEL (12u)
@@ -26,23 +47,34 @@
2647
#define PIN_SPI0_MOSI (3u)
2748
#define PIN_SPI0_SCK (2u)
2849
#define PIN_SPI0_SS (31u) // not pinned out
50+
static const uint8_t SS = PIN_SPI0_SS; // SPI Slave SS not used. Set here only for reference.
51+
static const uint8_t MOSI = PIN_SPI0_MOSI;
52+
static const uint8_t MISO = PIN_SPI0_MISO;
53+
static const uint8_t SCK = PIN_SPI0_SCK;
2954

3055
// Not pinned out
3156
#define PIN_SPI1_MISO (31u)
3257
#define PIN_SPI1_MOSI (31u)
3358
#define PIN_SPI1_SCK (31u)
3459
#define PIN_SPI1_SS (31u)
60+
#define SPI_MISO (PIN_SPI1_MISO)
61+
#define SPI_MOSI (PIN_SPI1_MOSI)
62+
#define SPI_SCK (PIN_SPI1_SCK)
3563

3664
// Wire
65+
#define __WIRE0_DEVICE (i2c1)
3766
#define PIN_WIRE0_SDA (6u)
3867
#define PIN_WIRE0_SCL (7u)
68+
#define SDA PIN_WIRE0_SDA
69+
#define SCL PIN_WIRE0_SCL
70+
#define I2C_SDA (SDA)
71+
#define I2C_SCL (SCL)
3972

4073
// Wire1 not pinned out
74+
#define __WIRE1_DEVICE (i2c0)
4175
#define PIN_WIRE1_SDA (31u)
4276
#define PIN_WIRE1_SCL (31u)
4377

44-
#define SERIAL_HOWMANY (2u)
78+
#define SERIAL_HOWMANY (1u)
4579
#define SPI_HOWMANY (1u)
4680
#define WIRE_HOWMANY (1u)
47-
48-
#include "../generic/common.h"

0 commit comments

Comments
 (0)