diff options
author | Oxore <oxore@protonmail.com> | 2023-03-07 19:10:25 +0300 |
---|---|---|
committer | Oxore <oxore@protonmail.com> | 2023-03-07 19:10:25 +0300 |
commit | 3282a756679019fd723346cc3bc584cc582eb669 (patch) | |
tree | a1992e7851fee8ca07929bb8c9f0877a7401d70c | |
parent | ac69a640ba9dde383b9c3ddca59c3d6d14ea5b2e (diff) |
Begin implementing serial descriptors
-rw-r--r-- | CMakeLists.txt | 1 | ||||
-rw-r--r-- | app/platform/stm32f0-gcc/retarget.cpp | 82 | ||||
-rw-r--r-- | app/platform/stm32f0-gcc/uart.cpp | 33 | ||||
-rw-r--r-- | app/platform/stm32f0-gcc/uart.h | 12 | ||||
-rw-r--r-- | third_party/CMSIS/Device/Include/cmsis_gcc.h | 4 |
5 files changed, 117 insertions, 15 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index c4a7808..224e421 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -74,6 +74,7 @@ set(nixie_clock_sources "app/main.c" "app/stm32f0xx_it.c" "app/system_stm32f0xx.c" + "app/platform/stm32f0-gcc/uart.cpp" "app/platform/stm32f0-gcc/retarget.cpp" "app/platform/stm32f0-gcc/freertos/port.c" "app/platform/stm32f0-gcc/startup/handlers_cm.c" 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) diff --git a/app/platform/stm32f0-gcc/uart.cpp b/app/platform/stm32f0-gcc/uart.cpp new file mode 100644 index 0000000..0fa6527 --- /dev/null +++ b/app/platform/stm32f0-gcc/uart.cpp @@ -0,0 +1,33 @@ +#include "uart.h" + +size_t UARTWrite(USART_TypeDef* uart, const void *data, size_t nbytes) +{ + if (uart == nullptr) return 0; + (void) data; + (void) nbytes; + return 0; +} + +size_t UARTWriteDirect(USART_TypeDef* uart, const void *data, size_t nbytes) +{ + if (uart == nullptr) return 0; + (void) data; + (void) nbytes; + return 0; +} + +size_t UARTRead(USART_TypeDef* uart, void *data, size_t nbytes) +{ + if (uart == nullptr) return 0; + (void) data; + (void) nbytes; + return 0; +} + +size_t UARTReadDirect(USART_TypeDef* uart, void *data, size_t nbytes) +{ + if (uart == nullptr) return 0; + (void) data; + (void) nbytes; + return 0; +} diff --git a/app/platform/stm32f0-gcc/uart.h b/app/platform/stm32f0-gcc/uart.h new file mode 100644 index 0000000..176f847 --- /dev/null +++ b/app/platform/stm32f0-gcc/uart.h @@ -0,0 +1,12 @@ +#pragma once +#include <cstddef> +#include "stm32f0xx.h" + +// Non-blocking buffered write +size_t UARTWrite(USART_TypeDef*, const void *data, size_t nbytes); +// Blocking write directly into data register +size_t UARTWriteDirect(USART_TypeDef*, const void *data, size_t nbytes); +// Non-blocking buffered read +size_t UARTRead(USART_TypeDef*, void *data, size_t nbytes); +// Blocking read directly from data register +size_t UARTReadDirect(USART_TypeDef*, void *data, size_t nbytes); diff --git a/third_party/CMSIS/Device/Include/cmsis_gcc.h b/third_party/CMSIS/Device/Include/cmsis_gcc.h index d868f2e..11d9594 100644 --- a/third_party/CMSIS/Device/Include/cmsis_gcc.h +++ b/third_party/CMSIS/Device/Include/cmsis_gcc.h @@ -147,7 +147,7 @@ __attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __get_xPSR(void) */
__attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __get_PSP(void)
{
- register uint32_t result;
+ uint32_t result;
__ASM volatile ("MRS %0, psp\n" : "=r" (result) );
return(result);
@@ -172,7 +172,7 @@ __attribute__( ( always_inline ) ) __STATIC_INLINE void __set_PSP(uint32_t topOf */
__attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __get_MSP(void)
{
- register uint32_t result;
+ uint32_t result;
__ASM volatile ("MRS %0, msp\n" : "=r" (result) );
return(result);
|