9 /* Should we exit simulation on ctrl-c or pass it through? */
10 #define EXIT_ON_CTRL_C
13 /* Round to nearest */
14 #define BITWIDTH ((CLK_FREQUENCY+(BAUD/2))/BAUD)
17 * Our UART uses 16x oversampling, so at 50 MHz and 115200 baud
18 * each sample is: 50000000/(115200*16) = 27 clock cycles. This
19 * means each bit is off by 0.47% so for 8 bits plus a start and
20 * stop bit the errors add to be 4.7%.
22 static double error
= 0.05;
25 IDLE
, START_BIT
, BITS
, STOP_BIT
, ERROR
28 static enum state tx_state
= IDLE
;
29 static unsigned long tx_countbits
;
30 static unsigned char tx_bits
;
31 static unsigned char tx_byte
;
32 static unsigned char tx_prev
;
35 * Return an error if the transition is not close enough to the start or
36 * the end of an expected bit.
38 static bool is_error(unsigned long bits
)
40 double e
= 1.0 * tx_countbits
/ BITWIDTH
;
42 if ((e
<= (1.0-error
)) && (e
>= error
))
48 void uart_tx(unsigned char tx
)
54 tx_countbits
= BITWIDTH
;
63 if (is_error(tx_countbits
)) {
64 printf("START_BIT error %ld %ld\n", BITWIDTH
, tx_countbits
);
65 tx_countbits
= BITWIDTH
*2;
71 if (tx_countbits
== 0) {
73 tx_countbits
= BITWIDTH
;
79 if (tx_countbits
== BITWIDTH
/2) {
80 tx_byte
= tx_byte
| (tx
<< tx_bits
);
81 tx_bits
= tx_bits
+ 1;
85 if (is_error(tx_countbits
)) {
86 printf("BITS error %ld %ld\n", BITWIDTH
, tx_countbits
);
87 tx_countbits
= BITWIDTH
*2;
93 if (tx_countbits
== 0) {
97 tx_countbits
= BITWIDTH
;
105 if (is_error(tx_countbits
)) {
106 printf("STOP_BIT error %ld %ld\n", BITWIDTH
, tx_countbits
);
107 tx_countbits
= BITWIDTH
*2;
111 /* Go straight to idle */
112 write(STDOUT_FILENO
, &tx_byte
, 1);
116 if (tx_countbits
== 0) {
117 write(STDOUT_FILENO
, &tx_byte
, 1);
124 if (tx_countbits
== 0) {
134 static struct termios oldt
;
136 static void disable_raw_mode(void)
138 tcsetattr(STDIN_FILENO
, TCSANOW
, &oldt
);
141 static void enable_raw_mode(void)
143 static bool initialized
= false;
146 static struct termios newt
;
148 tcgetattr(STDIN_FILENO
, &oldt
);
151 #ifdef EXIT_ON_CTRL_C
152 newt
.c_lflag
|= ISIG
;
154 tcsetattr(STDIN_FILENO
, TCSANOW
, &newt
);
156 atexit(disable_raw_mode
);
160 static int nonblocking_read(unsigned char *c
)
163 unsigned long val
= 0;
164 struct pollfd fdset
[1];
168 memset(fdset
, 0, sizeof(fdset
));
170 fdset
[0].fd
= STDIN_FILENO
;
171 fdset
[0].events
= POLLIN
;
173 ret
= poll(fdset
, 1, 0);
177 ret
= read(STDIN_FILENO
, &val
, 1);
179 fprintf(stderr
, "%s: read of stdin returns %d\n", __func__
, ret
);
191 static enum state rx_state
= IDLE
;
192 static unsigned char rx_char
;
193 static unsigned long rx_countbits
;
194 static unsigned char rx_bit
;
195 static unsigned char rx
= 1;
197 /* Avoid calling poll() too much */
198 #define RX_INTERVAL 10000
199 static unsigned long rx_sometimes
;
201 unsigned char uart_rx(void)
207 if (rx_sometimes
++ >= RX_INTERVAL
) {
210 if (nonblocking_read(&c
)) {
211 rx_state
= START_BIT
;
213 rx_countbits
= BITWIDTH
;
223 if (rx_countbits
== 0) {
225 rx_countbits
= BITWIDTH
;
232 if (rx_countbits
== 0) {
238 rx
= (rx_char
>> rx_bit
) & 1;
240 rx_countbits
= BITWIDTH
;
246 if (rx_countbits
== 0) {