diff --git a/.idea/workspace.xml b/.idea/workspace.xml index b05a31a..27f10f1 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -28,8 +28,11 @@ - - + + + + + - + - - + @@ -122,8 +128,18 @@ 1742375125098 - + + + + + @@ -134,4 +150,8 @@ + + + \ No newline at end of file diff --git a/Core/Inc/retarget.h b/Core/Inc/retarget.h new file mode 100644 index 0000000..1892363 --- /dev/null +++ b/Core/Inc/retarget.h @@ -0,0 +1,22 @@ +#ifndef _RETARGET_H__ +#define _RETARGET_H__ + +#include "stm32f1xx_hal.h" +#include +#include + +void RetargetInit(UART_HandleTypeDef *huart); + +int _isatty(int fd); + +int _write(int fd, char *ptr, int len); + +int _close(int fd); + +int _lseek(int fd, int ptr, int dir); + +int _read(int fd, char *ptr, int len); + +int _fstat(int fd, struct stat *st); + +#endif //#ifndef _RETARGET_H__ \ No newline at end of file diff --git a/Core/Src/main.c b/Core/Src/main.c index 4b22c79..0748fa1 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -27,6 +27,7 @@ /* USER CODE BEGIN Includes */ #include +#include "retarget.h" #include "oled.h" #include "rc522.h" /* USER CODE END Includes */ @@ -119,13 +120,19 @@ int main(void) MX_USART1_UART_Init(); MX_SPI1_Init(); /* USER CODE BEGIN 2 */ + // Retarget uart + RetargetInit(&huart1); + + // Init RC522 MFRC_Init(); PCD_Reset(); + // Init OLED OLED_Init(); OLED_Clear(); OLED_ShowString(0,0,"Card scan...",12); + printf("startup\n"); /* USER CODE END 2 */ /* Infinite loop */ diff --git a/Core/Src/retarget.c b/Core/Src/retarget.c new file mode 100644 index 0000000..4a031d3 --- /dev/null +++ b/Core/Src/retarget.c @@ -0,0 +1,98 @@ +#include <_ansi.h> +#include <_syslist.h> +#include +#include +#include +#include +#include + +#if !defined(OS_USE_SEMIHOSTING) + +#define STDIN_FILENO 0 +#define STDOUT_FILENO 1 +#define STDERR_FILENO 2 + +UART_HandleTypeDef *gHuart; + +void RetargetInit(UART_HandleTypeDef *huart) +{ + gHuart = huart; + + /* Disable I/O buffering for STDOUT stream, so that + * chars are sent out as soon as they are printed. */ + setvbuf(stdout, NULL, _IONBF, 0); +} + +int _isatty(int fd) +{ + if (fd >= STDIN_FILENO && fd <= STDERR_FILENO) + return 1; + + errno = EBADF; + return 0; +} + +int _write(int fd, char *ptr, int len) +{ + HAL_StatusTypeDef hstatus; + + if (fd == STDOUT_FILENO || fd == STDERR_FILENO) + { + hstatus = HAL_UART_Transmit(gHuart, (uint8_t *) ptr, len, HAL_MAX_DELAY); + if (hstatus == HAL_OK) + return len; + else + return EIO; + } + errno = EBADF; + return -1; +} + +int _close(int fd) +{ + if (fd >= STDIN_FILENO && fd <= STDERR_FILENO) + return 0; + + errno = EBADF; + return -1; +} + +int _lseek(int fd, int ptr, int dir) +{ + (void) fd; + (void) ptr; + (void) dir; + + errno = EBADF; + return -1; +} + +int _read(int fd, char *ptr, int len) +{ + HAL_StatusTypeDef hstatus; + + if (fd == STDIN_FILENO) + { + hstatus = HAL_UART_Receive(gHuart, (uint8_t *) ptr, 1, HAL_MAX_DELAY); + if (hstatus == HAL_OK) + return 1; + else + return EIO; + } + errno = EBADF; + return -1; +} + +int _fstat(int fd, struct stat *st) +{ + if (fd >= STDIN_FILENO && fd <= STDERR_FILENO) + { + st->st_mode = S_IFCHR; + return 0; + } + + errno = EBADF; + return 0; +} + +#endif //#if !defined(OS_USE_SEMIHOSTING) \ No newline at end of file diff --git a/Core/Src/syscalls.c b/Core/Src/syscalls.c index 8884b5a..b749d9d 100644 --- a/Core/Src/syscalls.c +++ b/Core/Src/syscalls.c @@ -89,33 +89,33 @@ __attribute__((weak)) int _write(int file, char *ptr, int len) return len; } -int _close(int file) -{ - (void)file; - return -1; -} - - -int _fstat(int file, struct stat *st) -{ - (void)file; - st->st_mode = S_IFCHR; - return 0; -} - -int _isatty(int file) -{ - (void)file; - return 1; -} - -int _lseek(int file, int ptr, int dir) -{ - (void)file; - (void)ptr; - (void)dir; - return 0; -} +// int _close(int file) +// { +// (void)file; +// return -1; +// } +// +// +// int _fstat(int file, struct stat *st) +// { +// (void)file; +// st->st_mode = S_IFCHR; +// return 0; +// } +// +// int _isatty(int file) +// { +// (void)file; +// return 1; +// } +// +// int _lseek(int file, int ptr, int dir) +// { +// (void)file; +// (void)ptr; +// (void)dir; +// return 0; +// } int _open(char *path, int flags, ...) {