71 lines
1.6 KiB
C
71 lines
1.6 KiB
C
#include "uart.h"
|
|
#include <errno.h>
|
|
#include <libopencm3/stm32/gpio.h>
|
|
#include <libopencm3/stm32/usart.h>
|
|
#include <sys/stat.h>
|
|
#include <unistd.h>
|
|
|
|
int _write(int file, char *ptr, int len);
|
|
int _close(int file);
|
|
int _fstat(int file, struct stat *st);
|
|
int _isatty(int file);
|
|
int _lseek(int file, int ptr, int dir);
|
|
int _read(int file, char *ptr, int len);
|
|
|
|
void usart_setup(void) {
|
|
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) {
|
|
(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;
|
|
}
|
|
|
|
// Optional — stubbed read (e.g. for scanf), returns 0 bytes
|
|
int _read(int file, char *ptr, int len) {
|
|
(void)file;
|
|
(void)ptr;
|
|
(void)len;
|
|
return 0;
|
|
}
|