Skip to content

Commit 1094c08

Browse files
committed
Add support for UARTSerial::write from critical section
1 parent d30ae07 commit 1094c08

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

drivers/UARTSerial.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,23 @@ void UARTSerial::sigio(Callback<void()> func)
134134
core_util_critical_section_exit();
135135
}
136136

137+
/* Special synchronous write designed to work from critical section, such
138+
* as in mbed_error_vfprintf.
139+
*/
140+
ssize_t UARTSerial::write_unbuffered(const char *buf_ptr, size_t length)
141+
{
142+
while (!_txbuf.empty()) {
143+
tx_irq();
144+
}
145+
146+
for (size_t data_written = 0; data_written < length; data_written++) {
147+
SerialBase::_base_putc(*buf_ptr++);
148+
data_written++;
149+
}
150+
151+
return length;
152+
}
153+
137154
ssize_t UARTSerial::write(const void *buffer, size_t length)
138155
{
139156
size_t data_written = 0;
@@ -143,6 +160,10 @@ ssize_t UARTSerial::write(const void *buffer, size_t length)
143160
return 0;
144161
}
145162

163+
if (core_util_in_critical_section()) {
164+
return write_unbuffered(buf_ptr, length);
165+
}
166+
146167
api_lock();
147168

148169
// Unlike read, we should write the whole thing if blocking. POSIX only

drivers/UARTSerial.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,9 @@ class UARTSerial : private SerialBase, public FileHandle, private NonCopyable<UA
238238
/** Release mutex */
239239
virtual void api_unlock(void);
240240

241+
/** Unbuffered write - invoked when write called from critical section */
242+
ssize_t write_unbuffered(const char *buf_ptr, size_t length);
243+
241244
/** Software serial buffers
242245
* By default buffer size is 256 for TX and 256 for RX. Configurable through mbed_app.json
243246
*/

0 commit comments

Comments
 (0)