Files
luban-lite-t3e-pro/bsp/artinchip/drv_bare/printf/stdio_port.c

69 lines
1.3 KiB
C
Raw Normal View History

2023-08-30 16:21:18 +08:00
#include <stdio.h>
#include <stddef.h>
#include <unistd.h>
#include <uart.h>
#define PORT console_uart_id
static int console_uart_id;
2023-11-30 19:48:02 +08:00
static int enable_uart_id;
2023-08-30 16:21:18 +08:00
void stdio_set_uart(int id)
{
console_uart_id = id;
2023-11-30 19:48:02 +08:00
enable_uart_id = id;
}
void stdio_unset_uart(int id)
{
if (id == enable_uart_id)
enable_uart_id = -1;
2023-08-30 16:21:18 +08:00
}
int putchar_port(int c)
{
2023-11-30 19:48:02 +08:00
if (enable_uart_id == console_uart_id) {
if (c == '\n')
uart_putchar(PORT, '\r');
2023-08-30 16:21:18 +08:00
2023-11-30 19:48:02 +08:00
if (uart_putchar(PORT, c) < 0)
return -1;
return c;
} else {
return 0;
}
2023-08-30 16:21:18 +08:00
}
int puts_port(const char *s)
{
int cnt, c;
cnt = 0;
for (;;) {
c = *s;
if (c == 0)
break;
if (putchar_port(c) < 0)
break;
cnt++;
s++;
}
2023-11-09 20:19:51 +08:00
putchar_port('\n');
2023-08-30 16:21:18 +08:00
return cnt;
}
int getchar_port(void)
{
2023-11-30 19:48:02 +08:00
if (enable_uart_id == console_uart_id) {
return uart_getchar(PORT);
} else {
return 0;
}
2023-08-30 16:21:18 +08:00
}
int getc(FILE *stream) __attribute__((alias("getchar_port")));
int getchar(void) __attribute__ ((alias("getchar_port")));
int puts(const char *s) __attribute__ ((alias("puts_port")));
int putc(int c, FILE *stream) __attribute__((alias("putchar_port")));
int putchar(int c) __attribute__ ((alias("putchar_port")));