apple / swift-embedded-examples

A collection of example projects using Embedded Swift
Apache License 2.0
721 stars 49 forks source link

Serial (UART?) input #55

Open mredig opened 2 months ago

mredig commented 2 months ago

I've found that I'm able to print via Swift on my esp32c*, but I cannot figure out how to read serial input.

Is this built into Swift at all, or do I need to drop into c calls?

kubamracek commented 2 months ago

Even UART output is not really built into Embedded Swift -- the standard Swift functions that produce text output like print() are currently just redirected to putchar() calls, and we expect that the underlaying platform provides that. IIUC, the ESP SDK routes putchar into the JTAG-forwarded UART (on JTAG enabled boards).

So I think the solution here is to manually operate a UART from your code, especially if you want to use a different UART than the one that's used for normal logging from the various subsystems in ESP SDK. So, probably you're looking at library calls to the ESP SDK, or alternatively accessing the UART HW directly similarly to how this STM32 example code is set up: https://github.com/apple/swift-embedded-examples/blob/main/stm32-uart-echo/Sources/Application/Application.swift