-
Notifications
You must be signed in to change notification settings - Fork 6
/
uart.c
152 lines (131 loc) · 3.46 KB
/
uart.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
* Serial.
*
* Licensed under AGPLv3.
*/
#include <avr/io.h>
#include <avr/interrupt.h>
#include "timer1.h"
#ifndef NULL
# define NULL 0
#endif
void serial_init(void) {
uint16_t baud = 8; /* 115200 at 16 MHz, TODO: use F_CPU */
UBRR0H = baud >> 8;
UBRR0L = baud & 255;
UCSR0B = 0x98; /*(1 << RXEN0) | (1 << TXEN0) | (1 << RXCIE0);*/
UCSR0C = 0x06; /*(3 << UCSZ00);*/
}
static const long int timeout = 10000;
void serial_write1(char ch) {
int count = 0;
#if defined(__AVR_ATmega128__) || defined(__AVR_ATmega1280__)
while (!(UCSR0A & _BV(UDRE0)) && ++ count < timeout);
UDR0 = ch;
count = 0;
while (!(UCSR1A & _BV(UDRE1)) && ++ count < timeout);
UDR1 = ch;
#elif defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__)
while (!(UCSR0A & _BV(UDRE0)) && ++ count < timeout);
UDR0 = ch;
#else
/* m8,16,32,169,8515,8535,163 */
while (!(UCSRA & _BV(UDRE)) && ++ count < timeout);
UDR = ch;
#endif
}
static const char to_hex[16] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f',
};
void serial_write_hex16(uint16_t val) {
serial_write1(' ');
serial_write1('0');
serial_write1('x');
serial_write1(to_hex[(val >> 12) & 15]);
serial_write1(to_hex[(val >> 8) & 15]);
serial_write1(to_hex[(val >> 4) & 15]);
serial_write1(to_hex[(val >> 0) & 15]);
}
void serial_write_hex32(uint32_t val) {
serial_write1(' ');
serial_write1('0');
serial_write1('x');
serial_write1(to_hex[(val >> 28) & 15]);
serial_write1(to_hex[(val >> 24) & 15]);
serial_write1(to_hex[(val >> 20) & 15]);
serial_write1(to_hex[(val >> 16) & 15]);
serial_write1(to_hex[(val >> 12) & 15]);
serial_write1(to_hex[(val >> 8) & 15]);
serial_write1(to_hex[(val >> 4) & 15]);
serial_write1(to_hex[(val >> 0) & 15]);
}
void serial_write_eol(void) {
serial_write1('\r');
serial_write1('\n');
}
void serial_write_str(const char *str) {
while (*str)
serial_write1(*str ++);
}
void serial_write_dec8(uint8_t val) {
serial_write1(' ');
if (val > 99)
serial_write1(val / 100 + '0');
if (val > 9)
serial_write1((val / 10) % 10 + '0');
serial_write1(val % 10 + '0');
}
static void serial_write_dec32_nosp(uint32_t val) {
if (val > 999999999)
serial_write1(val / 1000000000 + '0');
if (val > 99999999)
serial_write1((val / 100000000) % 10 + '0');
if (val > 9999999)
serial_write1((val / 10000000) % 10 + '0');
if (val > 999999)
serial_write1((val / 1000000) % 10 + '0');
if (val > 99999)
serial_write1((val / 100000) % 10 + '0');
if (val > 9999)
serial_write1((val / 10000) % 10 + '0');
if (val > 999)
serial_write1((val / 1000) % 10 + '0');
if (val > 99)
serial_write1((val / 100) % 10 + '0');
if (val > 9)
serial_write1((val / 10) % 10 + '0');
serial_write1(val % 10 + '0');
}
void serial_write_dec32(uint32_t val) {
serial_write1(' ');
serial_write_dec32_nosp(val);
}
void serial_write_fp32(int32_t val, uint32_t unit) {
uint32_t u;
serial_write1(' ');
if (val < 0) {
serial_write1('-');
val = 0 - val;
}
/* Let's hope the /'s and %'s get optimised together */
serial_write_dec32_nosp(val / unit);
val = val % unit;
if (unit > 1 && likely(val))
serial_write1('.');
for (u = unit; u > 1 && likely(val); u /= 10) {
val *= 10;
serial_write1(val / unit + '0');
val = val % unit;
}
}
static void (*ch_handler)(char ch) = NULL;
void serial_set_handler(void (*handler)(char ch)) {
ch_handler = handler;
}
ISR(USART_RX_vect) {
uint8_t status = UCSR0A;
uint8_t ch = UDR0;
if (!unlikely(status & 0x14) && ch_handler)
ch_handler(ch);
}