-
Notifications
You must be signed in to change notification settings - Fork 0
/
serial.c
145 lines (119 loc) · 2.5 KB
/
serial.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
#include <avr/io.h>
#include <stdio.h>
#define ESC 27
#define BYTE 8
/*
* Initialize the serial port.
*/
void serial_init() {
uint16_t baud_setting;
UCSR0A = _BV(U2X0);
baud_setting = 16; //115200 baud
// assign the baud_setting
UBRR0H = baud_setting >> 8;
UBRR0L = baud_setting;
// enable transmit and receive
UCSR0B |= (1 << TXEN0) | (1 << RXEN0);
}
/*
* Return 1 if a character is available else return 0.
*/
uint8_t byte_available() {
return (UCSR0A & (1 << RXC0)) ? 1 : 0;
}
/*
* Unbuffered read
* Return 255 if no character is available otherwise return available character.
*/
uint8_t read_byte() {
if (UCSR0A & (1 << RXC0)) return UDR0;
return 255;
}
/*
* Unbuffered write
*
* b byte to write.
*/
uint8_t write_byte(uint8_t b) {
//loop until the send buffer is empty
while (((1 << UDRIE0) & UCSR0B) || !(UCSR0A & (1 << UDRE0))) {}
//write out the byte
UDR0 = b;
return 1;
}
//Print a string
void print_string(char* s)
{
while(*s != '\0')
write_byte(*s++);
return;
}
//Print an 8-bit or 16-bit unsigned integer
void print_int(uint16_t i)
{
char str[10];
sprintf(str, "%u", i);
print_string(str);
}
//Print a 32-bit unsigned integer
void print_int32(uint32_t i) {
char buf[32] = {0};
int ndx = 31;
write_byte(0x1B);
write_byte('[');
write_byte('1');
write_byte('i');
for (; i && ndx; --ndx, i /= 10)
buf[ndx] = "0123456789"[i % 10];
while (ndx <= 31) write_byte(buf[ndx++]);
return;
}
//Print an 8-bit or 16-bit unsigned integer in hex format
void print_hex(uint16_t i)
{
char str[10];
sprintf(str, "%#x", i);
print_string(str);
}
//Print a 32-bit unsigned integer in hex format
void print_hex32(uint32_t i) {
char buf[32] = {0};
int ndx = 31;
write_byte(0x1B);
write_byte('[');
write_byte('1');
write_byte('i');
write_byte('0');
write_byte('x');
for (; i && ndx; --ndx, i /= 16)
buf[ndx] = "0123456789abcdef"[i % 16];
while (ndx <= 31) write_byte(buf[ndx++]);
return;
}
//Set the cursor position
void set_cursor(uint8_t row, uint8_t col)
{
//<ESC>[{ROW};{COLUMN}H
write_byte(ESC);
write_byte('[');
print_int(row);
write_byte(';');
print_int(col);
write_byte('H');
}
//Set the foreground color
void set_color(uint8_t color)
{
write_byte(ESC);
write_byte('[');
print_int(color);
write_byte('m');
}
//Erase all text on the screen
void clear_screen(void)
{
write_byte(ESC);
print_string("[2J");
set_cursor(0, 0);
return;
}