-
Notifications
You must be signed in to change notification settings - Fork 0
/
uart.c
145 lines (128 loc) · 3.04 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
/* maple-libopencm3 - demo project
* Copyright (C) 2016 Zoltán Csahók
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "uart.h"
#include "cdcacm.h"
char rxbuf[UART_RX_BUFFER_SIZE];
volatile int start;
volatile int end;
volatile int size;
int uart_available(void)
{
return size;
}
void uart_receive(const char *buf, int len)
{
while (len > 0 && size < UART_RX_BUFFER_SIZE) {
rxbuf[end] = *buf;
++buf;
--len;
end = (end + 1) % (UART_RX_BUFFER_SIZE - 1);
++size;
}
}
char uart_getc(void)
{
while (size == 0); // wait for data
char c = rxbuf[start];
DISABLE_INTERRUPTS;
start = (start + 1) % (UART_RX_BUFFER_SIZE - 1);
--size;
ENABLE_INTERRUPTS;
return c;
}
void uart_send(const char *buffer, int n)
{
if (n <= 0) {
return;
}
// retry until it succeeds
while (0 == usbd_ep_write_packet(cdcacm_usbd_dev, 0x82, buffer, n));
}
void uart_putc(unsigned char c)
{
uart_send((const char *) &c, 1);
}
void uart_puts(const char *s)
{
const char *p = s;
while (1) {
if (p == s + UART_TX_SIZE || *p == 0) {
uart_send(s, p - s);
if (*p == 0) {
return;
}
s = p;
}
p++;
}
}
void uart_clean(void)
{
start = end = size = 0;
}
void uart_init(void)
{
uart_clean();
cdcacm_init();
}
void uart_print_n(int value, int base, int length)
{
#define BUFSZ 32 // 32 for signed int32 (worst case: negative and base 2)
char buf[BUFSZ];
char *p = buf;
if (value == 0) {
*p++ = '0';
} else {
char sign = 0;
if (value < 0) {
sign = '-';
value = -value;
}
while (value > 0) {
*p++ = "0123456789ABCDEF"[value % base];
value /= base;
}
if (sign) {
*p++ = sign;
}
}
if (length > BUFSZ) {
length = BUFSZ;
}
// append leading blanks
while (p < buf + length) {
*p++ = ' ';
}
// reverse buf
int bufsz = p - buf;
int i;
for (i = 0; i < bufsz / 2; ++i) {
char tmp = buf[i];
buf[i] = buf[bufsz - 1 - i];
buf[bufsz - 1 - i] = tmp;
}
uart_send(buf, bufsz);
}
void uart_print_d(int value)
{
uart_print_n(value, 10, 0);
}
void uart_print_x(int value)
{
uart_print_n(value, 16, 0);
}