@@ -17,12 +17,95 @@ void SerialClass::adrParse(uint8_t c) {
1717}
1818#endif
1919
20- void SerialClass::begin (unsigned long baudrate, uint16_t config) {
20+ bool SerialClass::validatePins (pin_size_t rx, pin_size_t tx) {
21+ // for each role (RX/TX) in each UART, fail validation if:
22+ // - a pin is specified and no such role exists on the board, or
23+ // - a pin is specified and is not among the supported pins for that role
24+ switch (this ->port ) {
25+ #if LT_HW_UART0
26+ case 0 :
27+ if (rx != PIN_INVALID
28+ #ifdef PINS_SERIAL0_RX
29+ && !ltArrayContains ((pin_size_t [])PINS_SERIAL0_RX, rx)
30+ #endif
31+ )
32+ return false ;
33+ if (tx != PIN_INVALID
34+ #ifdef PINS_SERIAL0_TX
35+ && !ltArrayContains ((pin_size_t [])PINS_SERIAL0_TX, tx)
36+ #endif
37+ )
38+ return false ;
39+ break ;
40+ #endif
41+
42+ #if LT_HW_UART1
43+ case 1 :
44+ if (rx != PIN_INVALID
45+ #ifdef PINS_SERIAL1_RX
46+ && !ltArrayContains ((pin_size_t [])PINS_SERIAL1_RX, rx)
47+ #endif
48+ )
49+ return false ;
50+ if (tx != PIN_INVALID
51+ #ifdef PINS_SERIAL1_TX
52+ && !ltArrayContains ((pin_size_t [])PINS_SERIAL1_TX, tx)
53+ #endif
54+ )
55+ return false ;
56+ break ;
57+ #endif
58+
59+ #if LT_HW_UART2
60+ case 2 :
61+ if (rx != PIN_INVALID
62+ #ifdef PINS_SERIAL2_RX
63+ && !ltArrayContains ((pin_size_t [])PINS_SERIAL2_RX, rx)
64+ #endif
65+ )
66+ return false ;
67+ if (tx != PIN_INVALID
68+ #ifdef PINS_SERIAL2_TX
69+ && !ltArrayContains ((pin_size_t [])PINS_SERIAL2_TX, tx)
70+ #endif
71+ )
72+ return false ;
73+ break ;
74+ #endif
75+
76+ default :
77+ return false ;
78+ }
79+ return true ;
80+ }
81+
82+ void SerialClass::begin (unsigned long baudrate, uint16_t config, pin_size_t rx, pin_size_t tx) {
83+ // let family code cleanup any resources
84+ this ->end ();
85+
86+ // allocate data structure and ring buffer
2187 if (!this ->data ) {
2288 this ->data = new SerialData ();
2389 this ->rxBuf = new SerialRingBuffer ();
2490 }
2591
92+ // use provided pins or those used previously
93+ if (rx == PIN_INVALID)
94+ rx = this ->rx ;
95+ if (tx == PIN_INVALID)
96+ tx = this ->tx ;
97+ // nothing to configure if RX and TX are invalid
98+ if (rx == PIN_INVALID && tx == PIN_INVALID)
99+ return ;
100+ // validate if provided pins are valid for this UART port
101+ if (!this ->validatePins (rx, tx)) {
102+ LT_E (" Serial pin numbers RX=%d,TX=%d invalid for port %u" , rx, tx, this ->port );
103+ return ;
104+ }
105+ // store in SerialClass for family code to use
106+ this ->rx = rx;
107+ this ->tx = tx;
108+
26109 this ->beginPrivate (baudrate, config);
27110 // if (this->baudrate != baudrate || this->config != config)
28111 this ->configure (baudrate, config);
0 commit comments