summaryrefslogtreecommitdiff
path: root/app/platform/stm32f0-gcc/retarget.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'app/platform/stm32f0-gcc/retarget.cpp')
-rw-r--r--app/platform/stm32f0-gcc/retarget.cpp82
1 files changed, 69 insertions, 13 deletions
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 <cstdlib>
#include <cerrno>
#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<void *>(USART1), },
+ { FileType::kUART, O_NONBLOCK, reinterpret_cast<void *>(USART1), },
+ { FileType::kUARTDirect, 0, reinterpret_cast<void *>(USART1), },
+ { FileType::kUART, O_NONBLOCK, reinterpret_cast<void *>(USART3), },
+ { FileType::kUART, O_NONBLOCK, reinterpret_cast<void *>(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<unsigned>(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<USART_TypeDef*>(file->extra), buf, nbytes);
+ break;
+ case FileType::kUARTDirect:
+ ret = UARTReadDirect(reinterpret_cast<USART_TypeDef*>(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<USART_TypeDef*>(file->extra), buf, nbytes);
+ break;
+ case FileType::kUARTDirect:
+ ret = UARTWriteDirect(reinterpret_cast<USART_TypeDef*>(file->extra), buf, nbytes);
+ break;
+ }
+ return ret;
}
-static void dputchar2(char c, void* fd)
+static void dputchar(char c, void* fd)
{
write(reinterpret_cast<int>(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<void *>(fd), format, args);
+ const int ret = vfctprintf(dputchar, reinterpret_cast<void *>(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<void *>(fd), format, args);
+ return vfctprintf(dputchar, reinterpret_cast<void *>(fd), format, args);
}
extern "C" void putchar_(char c)