From 3282a756679019fd723346cc3bc584cc582eb669 Mon Sep 17 00:00:00 2001 From: Oxore Date: Tue, 7 Mar 2023 19:10:25 +0300 Subject: Begin implementing serial descriptors --- app/platform/stm32f0-gcc/retarget.cpp | 82 +++++++++++++++++++++++++++++------ 1 file changed, 69 insertions(+), 13 deletions(-) (limited to 'app/platform/stm32f0-gcc/retarget.cpp') diff --git a/app/platform/stm32f0-gcc/retarget.cpp b/app/platform/stm32f0-gcc/retarget.cpp index 524840e..8b8160f 100644 --- a/app/platform/stm32f0-gcc/retarget.cpp +++ b/app/platform/stm32f0-gcc/retarget.cpp @@ -4,11 +4,40 @@ #include #include #include "third_party/printf/printf.h" +#include "fcntl.h" +#include "uart.h" #ifdef errno #undef errno #endif +#if defined(__GNUC__) +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wold-style-cast" +#endif + +enum class FileType { + kUART, + kUARTDirect, +}; + +struct File +{ + FileType type; + int flags; + void *extra; +}; + +static constexpr size_t kFilesCount = 5; + +static File files[5] { + { FileType::kUART, O_NONBLOCK, reinterpret_cast(USART1), }, + { FileType::kUART, O_NONBLOCK, reinterpret_cast(USART1), }, + { FileType::kUARTDirect, 0, reinterpret_cast(USART1), }, + { FileType::kUART, O_NONBLOCK, reinterpret_cast(USART3), }, + { FileType::kUART, O_NONBLOCK, reinterpret_cast(USART4), }, +}; + int errno{}; extern "C" [[noreturn]] void vApplicationMallocFailedHook(void) @@ -45,25 +74,52 @@ extern "C" [[noreturn]] void __assert_func ( /* NOTREACHED */ } +static File* FileOfDescriptor(int fd) +{ + if (fd < 0 || static_cast(fd) >= kFilesCount) + return nullptr; + return files + fd; +} + extern "C" ssize_t read(int fd, void *buf, size_t nbytes) { - (void) fd; - (void) buf; - (void) nbytes; - errno = ENOSYS; - return -1; + File* file = FileOfDescriptor(fd); + if (file == nullptr) { + errno = EBADF; + return -1; + } + size_t ret{}; + switch (file->type) { + case FileType::kUART: + ret = UARTRead(reinterpret_cast(file->extra), buf, nbytes); + break; + case FileType::kUARTDirect: + ret = UARTReadDirect(reinterpret_cast(file->extra), buf, nbytes); + break; + } + return ret; } extern "C" ssize_t write(int fd, const void *buf, size_t nbytes) { - (void) fd; - (void) buf; - (void) nbytes; - errno = ENOSYS; - return -1; + File* file = FileOfDescriptor(fd); + if (file == nullptr) { + errno = EBADF; + return -1; + } + size_t ret{}; + switch (file->type) { + case FileType::kUART: + ret = UARTWrite(reinterpret_cast(file->extra), buf, nbytes); + break; + case FileType::kUARTDirect: + ret = UARTWriteDirect(reinterpret_cast(file->extra), buf, nbytes); + break; + } + return ret; } -static void dputchar2(char c, void* fd) +static void dputchar(char c, void* fd) { write(reinterpret_cast(fd), &c, 1); } @@ -77,14 +133,14 @@ extern "C" int dprintf(int fd, const char * format, ...) { va_list args; va_start(args, format); - const int ret = vfctprintf(dputchar2, reinterpret_cast(fd), format, args); + const int ret = vfctprintf(dputchar, reinterpret_cast(fd), format, args); va_end(args); return ret; } extern "C" int vdprintf(int fd, const char * format, va_list args) { - return vfctprintf(dputchar2, reinterpret_cast(fd), format, args); + return vfctprintf(dputchar, reinterpret_cast(fd), format, args); } extern "C" void putchar_(char c) -- cgit v1.2.3