]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/RISC-V_RV32_SiFive_HiFive1_FreedomStudio/freedom-metal/src/tty.c
Update RISCC-V-RV32-SiFive_HiFive1_FreedomStudio project to latest tools and metal...
[freertos] / FreeRTOS / Demo / RISC-V_RV32_SiFive_HiFive1_FreedomStudio / freedom-metal / src / tty.c
1 /* Copyright 2018 SiFive, Inc */
2 /* SPDX-License-Identifier: Apache-2.0 */
3
4 #include <metal/uart.h>
5 #include <metal/tty.h>
6 #include <metal/machine.h>
7
8 #if defined(__METAL_DT_STDOUT_UART_HANDLE)
9 /* This implementation serves as a small shim that interfaces with the first
10  * UART on a system. */
11 int metal_tty_putc(int c)
12 {
13     if (c == '\n') {
14         metal_tty_putc_raw( '\r' );
15     }
16     return metal_tty_putc_raw( c );
17 }
18
19 int metal_tty_putc_raw(int c)
20 {
21     return metal_uart_putc(__METAL_DT_STDOUT_UART_HANDLE, c);
22 }
23
24 int metal_tty_getc(int *c)
25 {
26    do {
27         metal_uart_getc( __METAL_DT_STDOUT_UART_HANDLE, c );
28         /* -1 means no key pressed, getc waits */
29     } while( -1 == *c )
30         ;
31     return 0;
32 }
33
34 #ifndef __METAL_DT_STDOUT_UART_BAUD
35 #define __METAL_DT_STDOUT_UART_BAUD 115200
36 #endif
37
38 static void metal_tty_init(void) __attribute__((constructor));
39 static void metal_tty_init(void)
40 {
41     metal_uart_init(__METAL_DT_STDOUT_UART_HANDLE, __METAL_DT_STDOUT_UART_BAUD);
42 }
43 #else
44 /* This implementation of putc doesn't actually do anything, it's just there to
45  * provide a shim that eats all the characters so we can ensure that everything
46  * can link to metal_tty_putc. */
47 int nop_putc(int c) __attribute__((section(".text.metal.nop.putc")));
48 int nop_putc(int c) { return -1; }
49 int metal_tty_putc(int c) __attribute__((weak, alias("nop_putc")));
50 #pragma message("There is no default output device, metal_tty_putc() will throw away all input.")
51 #endif