Skip to content

Commit

Permalink
fixup! stdio_semihosting: port to new interface
Browse files Browse the repository at this point in the history
  • Loading branch information
benpicco committed Feb 9, 2024
1 parent 3af8214 commit 796b633
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion sys/stdio_semihosting/stdio_semihosting.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ static size_t _semihosting_write(const uint8_t *buffer, size_t len)

static ssize_t _semihosting_read(uint8_t *buffer, size_t len)
{
if (!_semihosting_connected()) {
return 0;
}

uint32_t args[3] = {
STDIO_SEMIHOSTING_F_STDIN,
(uint32_t)buffer,
Expand All @@ -156,7 +160,7 @@ static bool _read_cb(void *arg)

static bool _init_done;
static void _init(void) {
if (!STDIO_SEMIHOSTING_RX) {
if (!STDIO_SEMIHOSTING_RX || !IS_USED(MODULE_STDIO_DISPATCH)) {
return;
}

Expand All @@ -183,8 +187,36 @@ static ssize_t _write(const void* buffer, size_t len)
if (!_semihosting_connected()) {
return len;
}
if (STDIO_SEMIHOSTING_RX && IS_USED(MODULE_STDIO_DISPATCH)
&& !_init_done) {
_init();
}

size_t remaining = _semihosting_write(buffer, len);
return len - remaining;
}

#ifndef MODULE_STDIO_DISPATCH
ssize_t stdio_read(void* buffer, size_t count)
{
if (!STDIO_SEMIHOSTING_RX) {
return -ENOTSUP;
}

uint32_t last_wakeup = ztimer_now(ZTIMER_MSEC);
ssize_t bytes_read = _semihosting_read(buffer, count);
while (bytes_read == 0) {
ztimer_periodic_wakeup(ZTIMER_MSEC, &last_wakeup,
STDIO_SEMIHOSTING_POLL_RATE_MS);
bytes_read = _semihosting_read(buffer, count);
}
return bytes_read;
}

int stdio_available(void)
{
return -ENOTSUP;
}
#endif

STDIO_PROVIDER(STDIO_SEMIHOSTING, _init, _detach, _write)

0 comments on commit 796b633

Please sign in to comment.