Skip to content

Commit cd9cd80

Browse files
committed
Make Serialx instance generic
Previously Serialx instance was defined by the variant. It was not enough generic and flexible. With this patch, all available Serialx instance are handled dynamically. Where 'x' is the U(S)ART number. Currently, STM32 MCU could have 9 U(S)ART. Serial is the generic name used in Arduino sketches. Serial is now mapped to the Serialx instance linked to the com port in the variant.h (mainly one linked to ST-Link) Moreover this will simplify USB integration to map the SerialUSB. Signed-off-by: Frederic.Pillon <frederic.pillon@st.com>
1 parent 28243f1 commit cd9cd80

File tree

37 files changed

+245
-659
lines changed

37 files changed

+245
-659
lines changed

cores/arduino/HardwareSerial.cpp

Lines changed: 115 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,126 @@
3030
#include "Arduino.h"
3131
#include "HardwareSerial.h"
3232

33+
// SerialEvent functions are weak, so when the user doesn't define them,
34+
// the linker just sets their address to 0 (which is checked below).
35+
36+
#if defined(HAVE_HWSERIAL1)
37+
HardwareSerial Serial1(USART1);
38+
void serialEvent1() __attribute__((weak));
39+
#endif
40+
41+
#if defined(HAVE_HWSERIAL2)
42+
HardwareSerial Serial2(USART2);
43+
void serialEvent2() __attribute__((weak));
44+
#endif
45+
46+
#if defined(HAVE_HWSERIAL3)
47+
HardwareSerial Serial3(USART3);
48+
void serialEvent3() __attribute__((weak));
49+
#endif
50+
51+
#if defined(HAVE_HWSERIAL4)
52+
#if defined(USART4)
53+
HardwareSerial Serial4(USART4);
54+
#else
55+
HardwareSerial Serial4(UART4);
56+
#endif
57+
void serialEvent4() __attribute__((weak));
58+
#endif
59+
60+
#if defined(HAVE_HWSERIAL5)
61+
#if defined(USART5)
62+
HardwareSerial Serial5(USART5);
63+
#else
64+
HardwareSerial Serial5(UART5);
65+
#endif
66+
void serialEvent5() __attribute__((weak));
67+
#endif
68+
69+
#if defined(HAVE_HWSERIAL6)
70+
HardwareSerial Serial6(USART6);
71+
void serialEvent6() __attribute__((weak));
72+
#endif
73+
74+
#if defined(HAVE_HWSERIAL7)
75+
#if defined(USART7)
76+
HardwareSerial Serial7(USART7);
77+
#else
78+
HardwareSerial Serial7(UART7);
79+
#endif
80+
void serialEvent7() __attribute__((weak));
81+
#endif
82+
83+
#if defined(HAVE_HWSERIAL8)
84+
#if defined(USART8)
85+
HardwareSerial Serial8(USART8);
86+
#else
87+
HardwareSerial Serial8(UART8);
88+
#endif
89+
void serialEvent8() __attribute__((weak));
90+
#endif
91+
92+
#if defined(HAVE_HWSERIAL9)
93+
HardwareSerial Serial9(UART9);
94+
void serialEvent9() __attribute__((weak));
95+
#endif
96+
97+
#if defined(HAVE_HWSERIAL10)
98+
HardwareSerial Serial10(UART10);
99+
void serialEvent10() __attribute__((weak));
100+
#endif
101+
102+
void serialEventRun(void)
103+
{
104+
#if defined(HAVE_HWSERIAL1)
105+
if (serialEvent1 && Serial1.available()) serialEvent1();
106+
#endif
107+
#if defined(HAVE_HWSERIAL2)
108+
if (serialEvent2 && Serial2.available()) serialEvent2();
109+
#endif
110+
#if defined(HAVE_HWSERIAL3)
111+
if (serialEvent3 && Serial3.available()) serialEvent3();
112+
#endif
113+
#if defined(HAVE_HWSERIAL4)
114+
if (serialEvent4 && Serial4.available()) serialEvent4();
115+
#endif
116+
#if defined(HAVE_HWSERIAL5)
117+
if (serialEvent5 && Serial5.available()) serialEvent5();
118+
#endif
119+
#if defined(HAVE_HWSERIAL6)
120+
if (serialEvent6 && Serial6.available()) serialEvent6();
121+
#endif
122+
#if defined(HAVE_HWSERIAL7)
123+
if (serialEvent7 && Serial7.available()) serialEvent7();
124+
#endif
125+
#if defined(HAVE_HWSERIAL8)
126+
if (serialEvent8 && Serial8.available()) serialEvent8();
127+
#endif
128+
#if defined(HAVE_HWSERIAL9)
129+
if (serialEvent9 && Serial9.available()) serialEvent9();
130+
#endif
131+
#if defined(HAVE_HWSERIAL10)
132+
if (serialEventl10 && Serial10.available()) serialEvent10();
133+
#endif
134+
}
33135

34136
// Constructors ////////////////////////////////////////////////////////////////
35137
HardwareSerial::HardwareSerial(PinName _rx, PinName _tx)
36138
{
37139
_serial.pin_rx = _rx;
38140
_serial.pin_tx = _tx;
141+
init();
142+
}
143+
144+
HardwareSerial::HardwareSerial(void* peripheral)
145+
{
146+
_serial.pin_rx = pinmap_pin(peripheral, PinMap_UART_RX);
147+
_serial.pin_tx = pinmap_pin(peripheral, PinMap_UART_TX);
148+
init();
149+
}
150+
151+
void HardwareSerial::init(void)
152+
{
39153
_serial.rx_buff = _rx_buffer;
40154
_serial.rx_head = 0;
41155
_serial.rx_tail = 0;
@@ -87,7 +201,7 @@ void HardwareSerial::begin(unsigned long baud, byte config)
87201

88202
_serial.baudrate = (uint32_t)baud;
89203

90-
// Manage databits
204+
// Manage databitshardware/arduino/avr/cores/arduino/HardwareSerial.cpp
91205
switch(config & 0x07) {
92206
case 0x02:
93207
databits = 6;

cores/arduino/HardwareSerial.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ class HardwareSerial : public Stream
102102

103103
public:
104104
HardwareSerial(PinName _rx, PinName _tx);
105+
HardwareSerial(void* peripheral);
105106
void begin(unsigned long baud) { begin(baud, SERIAL_8N1); }
106107
void begin(unsigned long, uint8_t);
107108
void end();
@@ -121,8 +122,21 @@ class HardwareSerial : public Stream
121122
// Interrupt handlers
122123
static void _rx_complete_irq(serial_t* obj);
123124
static int _tx_complete_irq(serial_t* obj);
125+
private:
126+
void init(void);
124127
};
125128

129+
extern HardwareSerial Serial1;
130+
extern HardwareSerial Serial2;
131+
extern HardwareSerial Serial3;
132+
extern HardwareSerial Serial4;
133+
extern HardwareSerial Serial5;
134+
extern HardwareSerial Serial6;
135+
extern HardwareSerial Serial7;
136+
extern HardwareSerial Serial8;
137+
extern HardwareSerial Serial9;
138+
extern HardwareSerial Serial10;
139+
126140
extern void serialEventRun(void) __attribute__((weak));
127141

128142
#endif

cores/arduino/stm32/uart.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,37 @@
4747
extern "C" {
4848
#endif
4949

50+
#if defined(USART1_BASE)
51+
#define HAVE_HWSERIAL1
52+
#endif
53+
#if defined(USART2_BASE)
54+
#define HAVE_HWSERIAL2
55+
#endif
56+
#if defined(USART3_BASE)
57+
#define HAVE_HWSERIAL3
58+
#endif
59+
#if defined(USART4_BASE) || defined(UART4_BASE)
60+
#define HAVE_HWSERIAL4
61+
#endif
62+
#if defined(USART5_BASE) || defined(UART5_BASE)
63+
#define HAVE_HWSERIAL5
64+
#endif
65+
#if defined(USART6_BASE)
66+
#define HAVE_HWSERIAL6
67+
#endif
68+
#if defined(USART7_BASE) || defined(UART7_BASE)
69+
#define HAVE_HWSERIAL7
70+
#endif
71+
#if defined(USART8_BASE) || defined(UART8_BASE)
72+
#define HAVE_HWSERIAL8
73+
#endif
74+
#if defined(UART9_BASE)
75+
#define HAVE_HWSERIAL9
76+
#endif
77+
#if defined(UART10_BASE)
78+
#define HAVE_HWSERIAL10
79+
#endif
80+
5081
/* Exported types ------------------------------------------------------------*/
5182
typedef struct serial_s serial_t;
5283

variants/DISCO_F100RB/variant.cpp

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -97,40 +97,6 @@ const PinName digitalPin[] = {
9797
}
9898
#endif
9999

100-
/*
101-
* UART objects
102-
*/
103-
104-
HardwareSerial Serial(PA_3, PA_2);
105-
#ifdef ENABLE_SERIAL1
106-
HardwareSerial Serial1(PA_10, PA_9);
107-
#endif
108-
#ifdef ENABLE_SERIAL2
109-
HardwareSerial Serial2(PB_11, PB_10);
110-
#endif
111-
112-
void serialEvent() __attribute__((weak));
113-
void serialEvent() { }
114-
#ifdef ENABLE_SERIAL1
115-
void serialEvent1() __attribute__((weak));
116-
void serialEvent1() { }
117-
#endif
118-
#ifdef ENABLE_SERIAL2
119-
void serialEvent2() __attribute__((weak));
120-
void serialEvent2() { }
121-
#endif
122-
123-
void serialEventRun(void)
124-
{
125-
if (Serial.available()) serialEvent();
126-
#ifdef ENABLE_SERIAL1
127-
if (Serial1.available()) serialEvent1();
128-
#endif
129-
#ifdef ENABLE_SERIAL2
130-
if (Serial2.available()) serialEvent2();
131-
#endif
132-
}
133-
134100
// ----------------------------------------------------------------------------
135101

136102
#ifdef __cplusplus

variants/DISCO_F100RB/variant.h

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -152,15 +152,12 @@ enum {
152152
//Do not use basic timer: OC is required
153153
#define TIMER_SERVO TIM17 //TODO: advanced-control timers don't work
154154

155+
// UART Definitions
155156
#define DEBUG_UART ((USART_TypeDef *) USART2)
156157

157158
// Serial Pin Firmata
158159
#define PIN_SERIAL_RX 8
159160
#define PIN_SERIAL_TX 7
160-
#define PIN_SERIAL1_RX 24
161-
#define PIN_SERIAL1_TX 23
162-
#define PIN_SERIAL2_RX 42
163-
#define PIN_SERIAL2_TX 41
164161

165162
#ifdef __cplusplus
166163
} // extern "C"
@@ -169,28 +166,26 @@ enum {
169166
* Arduino objects - C++ only
170167
*----------------------------------------------------------------------------*/
171168

172-
#ifdef __cplusplus
173-
extern HardwareSerial Serial;
174-
extern HardwareSerial Serial1;
175-
extern HardwareSerial Serial2;
176-
177-
// These serial port names are intended to allow libraries and architecture-neutral
178-
// sketches to automatically default to the correct port name for a particular type
179-
// of use. For example, a GPS module would normally connect to SERIAL_PORT_HARDWARE_OPEN,
180-
// the first hardware serial port whose RX/TX pins are not dedicated to another use.
181-
//
182-
// SERIAL_PORT_MONITOR Port which normally prints to the Arduino Serial Monitor
183-
//
184-
// SERIAL_PORT_USBVIRTUAL Port which is USB virtual serial
185-
//
186-
// SERIAL_PORT_LINUXBRIDGE Port which connects to a Linux system via Bridge library
187-
//
188-
// SERIAL_PORT_HARDWARE Hardware serial port, physical RX & TX pins.
189-
//
190-
// SERIAL_PORT_HARDWARE_OPEN Hardware serial ports which are open for use. Their RX & TX
191-
// pins are NOT connected to anything by default.
192-
#define SERIAL_PORT_MONITOR Serial
193-
#define SERIAL_PORT_HARDWARE Serial
194-
#endif
169+
#ifdef __cplusplus
170+
#define Serial Serial2 //Connected to ST-Link
171+
172+
// These serial port names are intended to allow libraries and architecture-neutral
173+
// sketches to automatically default to the correct port name for a particular type
174+
// of use. For example, a GPS module would normally connect to SERIAL_PORT_HARDWARE_OPEN,
175+
// the first hardware serial port whose RX/TX pins are not dedicated to another use.
176+
//
177+
// SERIAL_PORT_MONITOR Port which normally prints to the Arduino Serial Monitor
178+
//
179+
// SERIAL_PORT_USBVIRTUAL Port which is USB virtual serial
180+
//
181+
// SERIAL_PORT_LINUXBRIDGE Port which connects to a Linux system via Bridge library
182+
//
183+
// SERIAL_PORT_HARDWARE Hardware serial port, physical RX & TX pins.
184+
//
185+
// SERIAL_PORT_HARDWARE_OPEN Hardware serial ports which are open for use. Their RX & TX
186+
// pins are NOT connected to anything by default.
187+
#define SERIAL_PORT_MONITOR Serial
188+
#define SERIAL_PORT_HARDWARE Serial
189+
#endif
195190

196191
#endif /* _VARIANT_ARDUINO_STM32_ */

variants/DISCO_F407VG/variant.cpp

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -123,20 +123,6 @@ const PinName digitalPin[] = {
123123
}
124124
#endif
125125

126-
/*
127-
* UART objects
128-
*/
129-
130-
HardwareSerial Serial(PA_3, PA_2); // Could be connected to ST-Link
131-
132-
void serialEvent() __attribute__((weak));
133-
void serialEvent() { }
134-
135-
void serialEventRun(void)
136-
{
137-
if (Serial.available()) serialEvent();
138-
}
139-
140126
// ----------------------------------------------------------------------------
141127

142128
#ifdef __cplusplus

variants/DISCO_F407VG/variant.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ enum {
177177
#define TIMER_SERVO TIM7
178178
#define TIMER_UART_EMULATED TIM6
179179

180+
// UART Definitions
180181
#define DEBUG_UART ((USART_TypeDef *) USART2)
181182

182183
// UART Emulation
@@ -195,7 +196,7 @@ enum {
195196
*----------------------------------------------------------------------------*/
196197

197198
#ifdef __cplusplus
198-
extern HardwareSerial Serial;
199+
#define Serial Serial2 // Could be connected to ST-Link
199200

200201
// These serial port names are intended to allow libraries and architecture-neutral
201202
// sketches to automatically default to the correct port name for a particular type
@@ -212,8 +213,8 @@ extern HardwareSerial Serial;
212213
//
213214
// SERIAL_PORT_HARDWARE_OPEN Hardware serial ports which are open for use. Their RX & TX
214215
// pins are NOT connected to anything by default.
215-
#define SERIAL_PORT_MONITOR Serial // Require connections for ST-LINK VCP on U2 pin 12 and 13.
216-
// See UM §6.1.3 ST-LINK/V2-A VCP configuration)
216+
#define SERIAL_PORT_MONITOR Serial // Require connections for ST-LINK VCP on U2 pin 12 and 13.
217+
// See UM §6.1.3 ST-LINK/V2-A VCP configuration)
217218
#define SERIAL_PORT_HARDWARE_OPEN Serial
218219
#endif
219220

0 commit comments

Comments
 (0)