Skip to content

Commit 7a66456

Browse files
fpistmxC0000005
authored andcommitted
[U(S)ART] Use RX/TX pins defined in the variant for specified peripheral
Allow to use defined PIN_SERIALx_RX and PIN_SERIALx_TX in variant.h if user instanciate a hardware Serial using peripheral name. Example: HardwareSerial mySerial(USART3); will use PIN_SERIAL3_RX and PIN_SERIAL3_TX if defined. Signed-off-by: Frederic.Pillon <[email protected]>
1 parent 27144d4 commit 7a66456

File tree

2 files changed

+105
-4
lines changed

2 files changed

+105
-4
lines changed

cores/arduino/HardwareSerial.cpp

Lines changed: 96 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,104 @@ HardwareSerial::HardwareSerial(void* peripheral)
166166
// If Serial is defined in variant set
167167
// the Rx/Tx pins for com port if defined
168168
#if defined(Serial) && defined(PIN_SERIAL_RX) && defined(PIN_SERIAL_TX)
169-
if (this == &Serial)
170-
{
169+
if (this == &Serial) {
171170
setRx(PIN_SERIAL_RX);
172171
setTx(PIN_SERIAL_TX);
173-
}
174-
else
172+
} else
173+
#endif
174+
#if defined(PIN_SERIAL1_RX) && defined(PIN_SERIAL1_TX) && defined(USART1_BASE)
175+
if (peripheral == USART1) {
176+
setRx(PIN_SERIAL1_RX);
177+
setTx(PIN_SERIAL1_TX);
178+
} else
179+
#endif
180+
#if defined(PIN_SERIAL2_RX) && defined(PIN_SERIAL2_TX) && defined(USART2_BASE)
181+
if (peripheral == USART2) {
182+
setRx(PIN_SERIAL2_RX);
183+
setTx(PIN_SERIAL2_TX);
184+
} else
185+
#endif
186+
#if defined(PIN_SERIAL3_RX) && defined(PIN_SERIAL3_TX) && defined(USART3_BASE)
187+
if (peripheral == USART3) {
188+
setRx(PIN_SERIAL3_RX);
189+
setTx(PIN_SERIAL3_TX);
190+
}else
191+
#endif
192+
#if defined(PIN_SERIAL4_RX) && defined(PIN_SERIAL4_TX) &&\
193+
(defined(USART4_BASE) || defined(UART4_BASE))
194+
#if defined(USART4_BASE)
195+
if (peripheral == USART4)
196+
#elif defined(UART4_BASE)
197+
if (peripheral == UART4)
198+
#endif
199+
{
200+
setRx(PIN_SERIAL4_RX);
201+
setTx(PIN_SERIAL4_TX);
202+
} else
203+
#endif
204+
#if defined(PIN_SERIAL5_RX) && defined(PIN_SERIAL5_TX) &&\
205+
(defined(USART5_BASE) || defined(UART5_BASE))
206+
#if defined(USART5_BASE)
207+
if (peripheral == USART5)
208+
#elif defined(UART5_BASE)
209+
if (peripheral == UART5)
210+
#endif
211+
{
212+
setRx(PIN_SERIAL5_RX);
213+
setTx(PIN_SERIAL5_TX);
214+
} else
215+
#endif
216+
#if defined(PIN_SERIAL6_RX) && defined(PIN_SERIAL6_TX) && defined(USART6_BASE)
217+
if (peripheral == USART6)
218+
{
219+
setRx(PIN_SERIAL6_RX);
220+
setTx(PIN_SERIAL6_TX);
221+
} else
222+
#endif
223+
#if defined(PIN_SERIAL7_RX) && defined(PIN_SERIAL7_TX) &&\
224+
(defined(USART7_BASE) || defined(UART7_BASE))
225+
#if defined(USART7_BASE)
226+
if (peripheral == USART7)
227+
#elif defined(UART7_BASE)
228+
if (peripheral == UART7)
229+
#endif
230+
{
231+
setRx(PIN_SERIAL7_RX);
232+
setTx(PIN_SERIAL7_TX);
233+
} else
234+
#endif
235+
#if defined(PIN_SERIAL8_RX) && defined(PIN_SERIAL8_TX) &&\
236+
(defined(USART8_BASE) || defined(UART8_BASE))
237+
#if defined(USART8_BASE)
238+
if (peripheral == USART8)
239+
#elif defined(UART8_BASE)
240+
if (peripheral == UART8)
241+
#endif
242+
{
243+
setRx(PIN_SERIAL8_RX);
244+
setTx(PIN_SERIAL8_TX);
245+
} else
246+
#endif
247+
#if defined(PIN_SERIAL9_RX) && defined(PIN_SERIAL9_TX) && defined(UART9)
248+
if (peripheral == UART9)
249+
{
250+
setRx(PIN_SERIAL9_RX);
251+
setTx(PIN_SERIAL9_TX);
252+
} else
253+
#endif
254+
#if defined(PIN_SERIAL10_RX) && defined(PIN_SERIAL10_TX) && defined(UART10)
255+
if (peripheral == UART10)
256+
{
257+
setRx(PIN_SERIAL10_RX);
258+
setTx(PIN_SERIAL10_TX);
259+
} else
260+
#endif
261+
#if defined(PIN_SERIALLP1_RX) && defined(PIN_SERIALLP1_TX) && defined(LPUART1_BASE)
262+
if (peripheral == LPUART1)
263+
{
264+
setRx(PIN_SERIALLP1_RX);
265+
setTx(PIN_SERIALLP1_TX);
266+
} else
175267
#endif
176268
// else get the pins of the first peripheral occurence in PinMap
177269
{

variants/board_template/variant.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,15 @@ enum {
135135
#define PIN_SERIAL_RX x
136136
#define PIN_SERIAL_TX x
137137

138+
// Optional PIN_SERIALn_RX and PIN_SERIALn_TX where 'n' is the U(S)ART number
139+
// Used when user instanciate a hardware Serial using its peripheral name.
140+
// Example: HardwareSerial mySerial(USART3);
141+
// will use PIN_SERIAL3_RX and PIN_SERIAL3_TX if defined.
142+
//#define PIN_SERIALn_RX x // For U(S)ARTn RX
143+
//#define PIN_SERIALn_TX x // For U(S)ARTn TX
144+
//#define PIN_SERIALLP1_RX x // For LPUART1 RX
145+
//#define PIN_SERIALLP1_TX x // For LPUART1 TX
146+
138147
#ifdef __cplusplus
139148
} // extern "C"
140149
#endif

0 commit comments

Comments
 (0)