Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6f39b41

Browse files
authored
Merge pull request#2124 from fpistm/Serial_flush_timeout
feat(serial): add timeout support to flush()
2 parents0ffbf3e +96cba70 commit6f39b41

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

‎cores/arduino/HardwareSerial.cpp

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ void HardwareSerial::begin(unsigned long baud, byte config)
441441
voidHardwareSerial::end()
442442
{
443443
// wait for transmission of outgoing data
444-
flush();
444+
flush(TX_TIMEOUT);
445445

446446
uart_deinit(&_serial);
447447

@@ -487,20 +487,25 @@ int HardwareSerial::availableForWrite(void)
487487
return tail - head -1;
488488
}
489489

490-
voidHardwareSerial::flush()
490+
voidHardwareSerial::flush(uint32_t timeout)
491491
{
492492
// If we have never written a byte, no need to flush. This special
493493
// case is needed since there is no way to force the TXC (transmit
494494
// complete) bit to 1 during initialization
495-
if (!_written) {
496-
return;
497-
}
498-
499-
while ((_serial.tx_head != _serial.tx_tail)) {
500-
// nop, the interrupt handler will free up space for us
495+
if (_written) {
496+
uint32_t tickstart =HAL_GetTick();
497+
while ((_serial.tx_head != _serial.tx_tail)) {
498+
// the interrupt handler will free up space for us
499+
// Only manage timeout if any
500+
if ((timeout !=0) && ((HAL_GetTick() - tickstart) >= timeout)) {
501+
// clear any transmit data
502+
_serial.tx_head = _serial.tx_tail;
503+
break;
504+
}
505+
}
506+
// If we get here, nothing is queued anymore (DRIE is disabled) and
507+
// the hardware finished transmission (TXC is set).
501508
}
502-
// If we get here, nothing is queued anymore (DRIE is disabled) and
503-
// the hardware finished transmission (TXC is set).
504509
}
505510

506511
size_tHardwareSerial::write(constuint8_t *buffer,size_t size)

‎cores/arduino/HardwareSerial.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ class HardwareSerial : public Stream {
125125
virtualintpeek(void);
126126
virtualintread(void);
127127
intavailableForWrite(void);
128-
virtualvoidflush(void);
128+
virtualvoidflush(uint32_t timeout =0);
129129
virtualsize_twrite(uint8_t);
130130
inlinesize_twrite(unsignedlong n)
131131
{

‎cores/arduino/stm32/uart.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ struct serial_s {
8686
};
8787

8888
/* Exported constants --------------------------------------------------------*/
89+
#ifndefTX_TIMEOUT
8990
#defineTX_TIMEOUT 1000
91+
#endif
9092

9193
#if !defined(RCC_USART1CLKSOURCE_HSI)
9294
/* Some series like C0 have 2 derivated clock from HSI: HSIKER (for peripherals)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp