@@ -108,115 +108,137 @@ void serialEventLP1() __attribute__((weak));
108
108
// Constructors ////////////////////////////////////////////////////////////////
109
109
HardwareSerial::HardwareSerial (uint32_t _rx, uint32_t _tx)
110
110
{
111
- _serial.pin_rx = digitalPinToPinName (_rx);
112
- _serial.pin_tx = digitalPinToPinName (_tx);
113
- init ();
111
+ init (digitalPinToPinName (_rx), digitalPinToPinName (_tx));
114
112
}
115
113
116
114
HardwareSerial::HardwareSerial (PinName _rx, PinName _tx)
117
115
{
118
- _serial.pin_rx = _rx;
119
- _serial.pin_tx = _tx;
120
- init ();
116
+ init (_rx, _tx);
121
117
}
122
118
123
- HardwareSerial::HardwareSerial (void *peripheral)
119
+ HardwareSerial::HardwareSerial (void *peripheral, bool halfDuplex )
124
120
{
121
+ // If PIN_SERIALy_RX is not defined assume half-duplex
122
+ _serial.pin_rx = NC;
125
123
// If Serial is defined in variant set
126
124
// the Rx/Tx pins for com port if defined
127
- #if defined(Serial) && defined(PIN_SERIAL_RX) && defined( PIN_SERIAL_TX)
125
+ #if defined(Serial) && defined(PIN_SERIAL_TX)
128
126
if ((void *)this == (void *)&Serial) {
127
+ #if defined(PIN_SERIAL_RX)
129
128
setRx (PIN_SERIAL_RX);
129
+ #endif
130
130
setTx (PIN_SERIAL_TX);
131
131
} else
132
132
#endif
133
- #if defined(PIN_SERIAL1_RX) && defined( PIN_SERIAL1_TX) && defined(USART1_BASE)
133
+ #if defined(PIN_SERIAL1_TX) && defined(USART1_BASE)
134
134
if (peripheral == USART1) {
135
+ #if defined(PIN_SERIAL1_RX)
135
136
setRx (PIN_SERIAL1_RX);
137
+ #endif
136
138
setTx (PIN_SERIAL1_TX);
137
139
} else
138
140
#endif
139
- #if defined(PIN_SERIAL2_RX) && defined( PIN_SERIAL2_TX) && defined(USART2_BASE)
141
+ #if defined(PIN_SERIAL2_TX) && defined(USART2_BASE)
140
142
if (peripheral == USART2) {
143
+ #if defined(PIN_SERIAL2_RX)
141
144
setRx (PIN_SERIAL2_RX);
145
+ #endif
142
146
setTx (PIN_SERIAL2_TX);
143
147
} else
144
148
#endif
145
- #if defined(PIN_SERIAL3_RX) && defined( PIN_SERIAL3_TX) && defined(USART3_BASE)
149
+ #if defined(PIN_SERIAL3_TX) && defined(USART3_BASE)
146
150
if (peripheral == USART3) {
151
+ #if defined(PIN_SERIAL3_RX)
147
152
setRx (PIN_SERIAL3_RX);
153
+ #endif
148
154
setTx (PIN_SERIAL3_TX);
149
155
} else
150
156
#endif
151
- #if defined(PIN_SERIAL4_RX) && defined( PIN_SERIAL4_TX) &&\
157
+ #if defined(PIN_SERIAL4_TX) &&\
152
158
(defined (USART4_BASE) || defined (UART4_BASE))
153
159
#if defined(USART4_BASE)
154
160
if (peripheral == USART4)
155
161
#elif defined(UART4_BASE)
156
162
if (peripheral == UART4)
157
163
#endif
158
164
{
165
+ #if defined(PIN_SERIAL4_RX)
159
166
setRx (PIN_SERIAL4_RX);
167
+ #endif
160
168
setTx (PIN_SERIAL4_TX);
161
169
} else
162
170
#endif
163
- #if defined(PIN_SERIAL5_RX) && defined( PIN_SERIAL5_TX) &&\
171
+ #if defined(PIN_SERIAL5_TX) &&\
164
172
(defined (USART5_BASE) || defined (UART5_BASE))
165
173
#if defined(USART5_BASE)
166
174
if (peripheral == USART5)
167
175
#elif defined(UART5_BASE)
168
176
if (peripheral == UART5)
169
177
#endif
170
178
{
179
+ #if defined(PIN_SERIAL5_RX)
171
180
setRx (PIN_SERIAL5_RX);
181
+ #endif
172
182
setTx (PIN_SERIAL5_TX);
173
183
} else
174
184
#endif
175
- #if defined(PIN_SERIAL6_RX) && defined( PIN_SERIAL6_TX) && defined(USART6_BASE)
185
+ #if defined(PIN_SERIAL6_TX) && defined(USART6_BASE)
176
186
if (peripheral == USART6) {
187
+ #if defined(PIN_SERIAL6_RX)
177
188
setRx (PIN_SERIAL6_RX);
189
+ #endif
178
190
setTx (PIN_SERIAL6_TX);
179
191
} else
180
192
#endif
181
- #if defined(PIN_SERIAL7_RX) && defined( PIN_SERIAL7_TX) &&\
193
+ #if defined(PIN_SERIAL7_TX) &&\
182
194
(defined (USART7_BASE) || defined (UART7_BASE))
183
195
#if defined(USART7_BASE)
184
196
if (peripheral == USART7)
185
197
#elif defined(UART7_BASE)
186
198
if (peripheral == UART7)
187
199
#endif
188
200
{
201
+ #if defined(PIN_SERIAL7_RX)
189
202
setRx (PIN_SERIAL7_RX);
203
+ #endif
190
204
setTx (PIN_SERIAL7_TX);
191
205
} else
192
206
#endif
193
- #if defined(PIN_SERIAL8_RX) && defined( PIN_SERIAL8_TX) &&\
207
+ #if defined(PIN_SERIAL8_TX) &&\
194
208
(defined (USART8_BASE) || defined (UART8_BASE))
195
209
#if defined(USART8_BASE)
196
210
if (peripheral == USART8)
197
211
#elif defined(UART8_BASE)
198
212
if (peripheral == UART8)
199
213
#endif
200
214
{
215
+ #if defined(PIN_SERIAL8_RX)
201
216
setRx (PIN_SERIAL8_RX);
217
+ #endif
202
218
setTx (PIN_SERIAL8_TX);
203
219
} else
204
220
#endif
205
- #if defined(PIN_SERIAL9_RX) && defined( PIN_SERIAL9_TX) && defined(UART9)
221
+ #if defined(PIN_SERIAL9_TX) && defined(UART9)
206
222
if (peripheral == UART9) {
223
+ #if defined(PIN_SERIAL9_RX)
207
224
setRx (PIN_SERIAL9_RX);
225
+ #endif
208
226
setTx (PIN_SERIAL9_TX);
209
227
} else
210
228
#endif
211
- #if defined(PIN_SERIAL10_RX) && defined( PIN_SERIAL10_TX) && defined(UART10)
229
+ #if defined(PIN_SERIAL10_TX) && defined(UART10)
212
230
if (peripheral == UART10) {
231
+ #if defined(PIN_SERIAL10_RX)
213
232
setRx (PIN_SERIAL10_RX);
233
+ #endif
214
234
setTx (PIN_SERIAL10_TX);
215
235
} else
216
236
#endif
217
- #if defined(PIN_SERIALLP1_RX) && defined( PIN_SERIALLP1_TX) && defined(LPUART1_BASE)
237
+ #if defined(PIN_SERIALLP1_TX) && defined(LPUART1_BASE)
218
238
if (peripheral == LPUART1) {
239
+ #if defined(PIN_SERIALLP1_RX)
219
240
setRx (PIN_SERIALLP1_RX);
241
+ #endif
220
242
setTx (PIN_SERIALLP1_TX);
221
243
} else
222
244
#endif
@@ -225,11 +247,30 @@ HardwareSerial::HardwareSerial(void *peripheral)
225
247
_serial.pin_rx = pinmap_pin (peripheral, PinMap_UART_RX);
226
248
_serial.pin_tx = pinmap_pin (peripheral, PinMap_UART_TX);
227
249
}
228
- init ();
250
+ if (halfDuplex) {
251
+ _serial.pin_rx = NC;
252
+ }
253
+ init (_serial.pin_rx , _serial.pin_tx );
229
254
}
230
255
231
- void HardwareSerial::init ( void )
256
+ HardwareSerial::HardwareSerial ( uint32_t _rxtx )
232
257
{
258
+ init (NC, digitalPinToPinName (_rxtx));
259
+ }
260
+
261
+ HardwareSerial::HardwareSerial (PinName _rxtx)
262
+ {
263
+ init (NC, _rxtx);
264
+ }
265
+
266
+ void HardwareSerial::init (PinName _rx, PinName _tx)
267
+ {
268
+ if (_rx == _tx) {
269
+ _serial.pin_rx = NC;
270
+ } else {
271
+ _serial.pin_rx = _rx;
272
+ }
273
+ _serial.pin_tx = _tx;
233
274
_serial.rx_buff = _rx_buffer;
234
275
_serial.rx_head = 0 ;
235
276
_serial.rx_tail = 0 ;
@@ -347,11 +388,7 @@ void HardwareSerial::begin(unsigned long baud, byte config)
347
388
}
348
389
349
390
uart_init (&_serial, (uint32_t )baud, databits, parity, stopbits);
350
- if (isHalfDuplex ()) {
351
- // Start half duplex communication in receive mode.
352
- _rx_enabled = true ;
353
- uart_enable_rx (&_serial);
354
- }
391
+ enableHalfDuplexRx ();
355
392
uart_attach_rx_callback (&_serial, _rx_complete_irq);
356
393
}
357
394
@@ -382,15 +419,7 @@ int HardwareSerial::peek(void)
382
419
383
420
int HardwareSerial::read (void )
384
421
{
385
- if (isHalfDuplex ()) {
386
- // In half duplex mode we have to wait for all TX characters to
387
- // be transmitted before we can receive data.
388
- flush ();
389
- if (!_rx_enabled) {
390
- _rx_enabled = true ;
391
- uart_enable_rx (&_serial);
392
- }
393
- }
422
+ enableHalfDuplexRx ();
394
423
// if the head isn't ahead of the tail, we don't have any characters
395
424
if (_serial.rx_head == _serial.rx_tail ) {
396
425
return -1 ;
@@ -475,4 +504,28 @@ void HardwareSerial::setTx(PinName _tx)
475
504
{
476
505
_serial.pin_tx = _tx;
477
506
}
507
+
508
+ void HardwareSerial::setHalfDuplex (void )
509
+ {
510
+ _serial.pin_rx = NC;
511
+ }
512
+
513
+ bool HardwareSerial::isHalfDuplex (void ) const
514
+ {
515
+ return _serial.pin_rx == NC;
516
+ }
517
+
518
+ void HardwareSerial::enableHalfDuplexRx (void )
519
+ {
520
+ if (isHalfDuplex ()) {
521
+ // In half-duplex mode we have to wait for all TX characters to
522
+ // be transmitted before we can receive data.
523
+ flush ();
524
+ if (!_rx_enabled) {
525
+ _rx_enabled = true ;
526
+ uart_enable_rx (&_serial);
527
+ }
528
+ }
529
+ }
530
+
478
531
#endif // HAL_UART_MODULE_ENABLED
0 commit comments