#include "uart.h" #include #include #include #include #include #include #include #include #include int _write(int file, char *ptr, int len); void usart_setup(void) { /* Setup GPIO pin GPIO_USART1_RE_TX on GPIO port B for transmit. */ gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART1_TX); usart_set_baudrate(USART1, 115200); usart_set_databits(USART1, 8); usart_set_stopbits(USART1, USART_STOPBITS_1); usart_set_parity(USART1, USART_PARITY_NONE); usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE); usart_set_mode(USART1, USART_MODE_TX); usart_enable(USART1); } int _write(int file, char *ptr, int len) { if (file == STDOUT_FILENO || file == STDERR_FILENO) { for (int i = 0; i < len; i++) { if (ptr[i] == '\n') { usart_send_blocking(USART1, '\r'); } usart_send_blocking(USART1, ptr[i]); } return len; } errno = EIO; return -1; } int _close(int file); int _close(int file) { (void)file; return -1; } int _fstat(int file, struct stat *st); int _fstat(int file, struct stat *st) { (void)file; st->st_mode = S_IFCHR; return 0; } int _isatty(int file); int _isatty(int file) { (void)file; return 1; } int _lseek(int file, int ptr, int dir); int _lseek(int file, int ptr, int dir) { (void)file; (void)ptr; (void)dir; return 0; } // Optional — stubbed read (e.g. for scanf), returns 0 bytes int _read(int file, char *ptr, int len); int _read(int file, char *ptr, int len) { (void)file; (void)ptr; (void)len; return 0; }