forked from esden/open-bldc-mk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uart.c
222 lines (195 loc) · 4.68 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/**
* @file uart.c
* @brief UART communication handling
* @author Olivier
*/
#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdint.h>
#include "uart.h"
uint8_t should_block;
#define UCSRACONFIG (1<<U2X)|(0<<MPCM)
#define UCSRBCONFIG (1<<RXCIE)|(1<<TXCIE)|(0<<UDRIE)|(1<<RXEN)|(1<<TXEN)|(0<<UCSZ2)
#define UCSRCCONFIG (1<<URSEL)|(0<<UMSEL)|(0<<UPM0)|(0<<UPM1)|(1<<UCSZ1)|(1<<UCSZ0)|(0<<UCPOL)
#define UBRRHCONFIG 0
#define UBRRLCONFIG 8
#define UART_QUEUE_LENGTH 64
struct UART_QUEUE {
char queue[UART_QUEUE_LENGTH];
uint8_t in;
uint8_t out;
uint8_t size;
};
volatile struct UART_QUEUE rx_queue;
volatile struct UART_QUEUE tx_queue;
/**
* Jump start byte transmission
*/
void start_transmission(void);
void clear_queues(void);
void uart_init(void) {
UCSRA = UCSRACONFIG;
UCSRB = UCSRBCONFIG;
UCSRC = UCSRCCONFIG;
UBRRH = UBRRHCONFIG;
UBRRL = UBRRLCONFIG;
uart_block(1);
//Init queues
clear_queues();
}
void clear_queues() {
uint8_t temp;
temp = SREG & (1 << SREG_I);
cli();
rx_queue.in = 0;
rx_queue.out = 0;
tx_queue.in = 0;
tx_queue.out = 0;
if (temp) {
sei();
}
}
uint8_t getch(char *byte) {
uint8_t temp;
uint8_t ret_val = UART_FAIL;
temp = SREG & (1 << SREG_I);
cli();
if (rx_queue.size) {
if (byte) {
*byte = rx_queue.queue[rx_queue.out++];
rx_queue.size--;
if (rx_queue.out >= UART_QUEUE_LENGTH) {
rx_queue.out = 0;
}
ret_val = UART_SUCCESS;
}
}
if (temp) {
sei();
}
return ret_val;
}
/**
* byte reception interruption handling
*/
ISR(USART_RXC_vect)
{
uint8_t receivedByte = UDR;
if (rx_queue.size < UART_QUEUE_LENGTH) {
rx_queue.queue[rx_queue.in++] = receivedByte;
rx_queue.size++;
if (rx_queue.in >= UART_QUEUE_LENGTH) {
rx_queue.in = 0;
}
} else {
// ignore it, queue is full
}
}
void start_transmission(void) {
uint8_t temp;
temp = SREG & (1 << SREG_I);
cli();
if (tx_queue.size) {
UDR = tx_queue.queue[tx_queue.out++];
tx_queue.size--;
if (tx_queue.out >= UART_QUEUE_LENGTH) {
tx_queue.out = 0;
}
}
if (temp) {
sei();
}
}
void uart_block(uint8_t sb) {
should_block = sb;
if (sb && UCSRB & (1 << TXCIE)) {
UCSRB &= ~(1 << TXCIE);
clear_queues();
}
UCSRB |= ((!sb) << TXCIE);
}
void write(char byte) {
if (should_block) {
while (!(UCSRA & (1 << UDRE)))
;
UDR = byte;
} else {
uint8_t temp;
temp = SREG & (1 << SREG_I);
cli();
if (tx_queue.size < UART_QUEUE_LENGTH) {
tx_queue.queue[tx_queue.in++] = byte;
tx_queue.size++;
if (tx_queue.in >= UART_QUEUE_LENGTH) {
tx_queue.in = 0;
}
}
if (temp) {
sei();
}
if (UCSRA & (1 << UDRE)) {
start_transmission();
}
}
}
/**
* byte transmission interruption handling
*/
ISR(USART_TXC_vect)
{
if (tx_queue.size) {
UDR = tx_queue.queue[tx_queue.out++];
tx_queue.size--;
if (tx_queue.out >= UART_QUEUE_LENGTH) {
tx_queue.out = 0;
}
}
}
void flush(void) {
uint8_t dummy;
while (UCSRA & (1 << RXC))
dummy = UDR;
}
void print_number(unsigned long n, uint8_t base) {
char buf[8 * sizeof(long) + 1]; // Assumes 8-bit chars plus zero byte.
char *str = &buf[sizeof(buf) - 1];
*str = '\0';
// prevent crash if called with base == 1
if (base < 2)
base = 10;
do {
unsigned long m = n;
n /= base;
char c = m - base * n;
*--str = c < 10 ? c + '0' : c + 'A' - 10;
} while (n);
while (*str)
write(*str++);
}
void print_float(double number, uint8_t digits) {
// Handle negative numbers
if (number < 0.0) {
print('-');
number = -number;
}
// Round correctly so that print(1.999, 2) prints as "2.00"
double rounding = 0.5;
for (uint8_t i = 0; i < digits; ++i)
rounding /= 10.0;
number += rounding;
// Extract the integer part of the number and print it
unsigned long int_part = (unsigned long) number;
double remainder = number - (double) int_part;
print(int_part);
// Print the decimal point, but only if there are digits beyond
if (digits > 0) {
print('.');
}
// Extract digits from the remainder one at a time
while (digits-- > 0) {
remainder *= 10.0;
int toPrint = (int) remainder;
print(toPrint);
remainder -= toPrint;
}
}