blob: e610402839b4cdc34d4ecdb2c3228dd79442ca16 [file] [log] [blame]
Austin Schuh208337d2022-01-01 14:29:11 -08001/*
2 * Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <stdio.h>
8#include "hardware/uart.h"
9
10#if defined(__unix) || defined(__APPLE__)
11#define _XOPEN_SOURCE 600 /* for ONLCR */
12#define __BSD_VISIBLE 1 /* for ONLCR in *BSD */
13
14#include <unistd.h>
15#include <termios.h>
16#include <fcntl.h>
17#include <stdlib.h>
18
19#ifndef FNONBLOCK
20#define FNONBLOCK O_NONBLOCK
21#endif
22
23struct termios _tty;
24static tcflag_t _res_oflg = 0;
25static tcflag_t _res_lflg = 0;
26
27void _resettty(void) {
28 if (!isatty(STDIN_FILENO))
29 return;
30
31 /* reset tty: */
32 _tty.c_oflag = _res_oflg;
33 _tty.c_lflag = _res_lflg;
34 tcsetattr(STDIN_FILENO, TCSADRAIN, &_tty);
35}
36
37void _inittty(void) {
38 if (!isatty(STDIN_FILENO))
39 return;
40
41 /* save tty: */
42 tcgetattr(STDIN_FILENO, &_tty);
43 _res_oflg = _tty.c_oflag;
44 _res_lflg = _tty.c_lflag;
45
46 /* set raw: */
47 _tty.c_lflag &= ~(ICANON | ICRNL);// | ISIG);
48 //_tty.c_oflag &= ~ONLCR;
49 tcsetattr(STDIN_FILENO, TCSANOW, &_tty);
50
51 fcntl(STDIN_FILENO, F_SETFL, FNONBLOCK);
52 atexit(_resettty);
53}
54
55#else
56void _inittty() {}
57#endif
58
59typedef struct {
60 bool dummy;
61} uart_hw_t;
62
63uart_inst_t *const uart0;
64uart_inst_t *const uart1;
65
66static int _nextchar = EOF;
67
68static bool _peekchar() {
69 if (_nextchar == EOF) {
70 _nextchar = getchar();
71 }
72 return _nextchar != EOF;
73}
74
75uint uart_init(uart_inst_t *uart, uint baud_rate) {
76 _inittty();
77 return baud_rate;
78}
79
80size_t uart_is_writable(uart_inst_t *uart) {
81 return 1;
82}
83
84// If returns 0, no data is available to be read from UART.
85// If returns nonzero, at least that many bytes can be written without blocking.
86size_t uart_is_readable(uart_inst_t *uart) {
87 return _peekchar() ? 1 : 0;
88}
89
90// Write len bytes directly from src to the UART
91void uart_write_blocking(uart_inst_t *uart, const uint8_t *src, size_t len) {
92 for (size_t i = 0; i < len; i++) {
93 uart_putc(uart, src[i]);
94 }
95}
96
97// Read len bytes directly from the UART to dst
98void uart_read_blocking(uart_inst_t *uart, uint8_t *dst, size_t len) {
99 for (size_t i = 0; i < len; i++) {
100 dst[i] = uart_getc(uart);
101 }
102}
103
104// ----------------------------------------------------------------------------
105// UART-specific operations and aliases
106
107void uart_putc(uart_inst_t *uart, char c) {
108 putchar(c);
109}
110
111void uart_puts(uart_inst_t *uart, const char *s) {
112 puts(s);
113}
114
115char uart_getc(uart_inst_t *uart) {
116 while (!_peekchar()) {
117 tight_loop_contents();
118 }
119 char rc = (char) _nextchar;
120 _nextchar = EOF;
121 return rc;
122}
123
124void uart_default_tx_wait_blocking() {
125
126}