/
uart.c
260 lines (198 loc) · 4.95 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
Project: 1Sheeld Firmware
File: uart.cpp
Compiler: avr-gcc 3.4.2
Author: Integreight
Date: 2014.5
*/
#include "config.h"
#include "uart.h"
static volatile uint8_t UART0_RxBuf[UART0_RX0_BUFFER_SIZE];
static volatile uint8_t UART0_LastRxError;
static volatile uint16_t UART0_RxHead;
static volatile uint16_t UART0_RxTail;
static volatile uint8_t UART1_RxBuf[UART_RX1_BUFFER_SIZE];
static volatile uint8_t UART1_LastRxError;
static volatile uint16_t UART1_RxHead;
static volatile uint16_t UART1_RxTail;
void setupUartLeds()
{
SET_BIT(DDRA,6);
SET_BIT(DDRA,7);
SET_BIT(PORTA,6);
SET_BIT(PORTA,7);
TCCR2|=(1<<CS20)|(1<<CS21); // clock prescalar =32
}
void initUart(uint8_t serialPort, uint8_t baudrate){
UART1_RxHead = 0;
UART1_RxTail = 0;
UART0_RxHead = 0;
UART0_RxTail = 0;
switch(serialPort)
{
case 0:
UCSR0A=0x00;
UCSR0B=(1<<TXEN0)|(1<<RXEN0)|(1<<RXCIE0);
UCSR0C=(1<<URSEL0)|(1<<UCSZ00)|(1<<UCSZ01);
switch (baudrate)
{
case BAUD_9600: UBRR0L = 47;break;
case BAUD_14400: UBRR0L = 31;break;
case BAUD_19200: UBRR0L = 23;break;
case BAUD_28800: UBRR0L = 15;break;
case BAUD_38400: UBRR0L = 11;break;
case BAUD_57600: UBRR0L = 7;break;
case BAUD_115200: UBRR0L = 3;break;
default: UBRR0L = 3;break;
}
break;
case 1:
UCSR1A=0x00;
UCSR1B=(1<<TXEN1)|(1<<RXEN1)|(1<<RXCIE1)|(1<<TXCIE1);
UCSR1C=(1<<URSEL1)|(1<<UCSZ10)|(1<<UCSZ11);
UBRR1L= 3; // 115200 single speed
break;
default:
break;
}
}
void terminateUart(uint8_t serialPort)
{
if (serialPort==0)
{
UCSR0B &=0x07;
}
else
{
UCSR1B &=0x07;
}
}
void writeOnUart0(uint8_t data){
while((UCSR0A&(1<<UDRE0))==0)
{
}
UDR0=data;
}
void writeOnUart1(uint8_t data){
while((UCSR1A&(1<<UDRE1))==0)
{
}
UDR1=data;
}
int16_t readFromUart0(){
uint16_t tmptail;
uint8_t data;
if ( UART0_RxHead == UART0_RxTail ) {
return UART_NO_DATA; /* no data */
}
/* calculate /store buffer index */
tmptail = (UART0_RxTail + 1) & UART0_RX0_BUFFER_MASK;
UART0_RxTail = tmptail;
#ifdef PLUS_BOARD
if (((UART0_RxTail + 1) & UART0_RX0_BUFFER_MASK)== UART0_RxHead)
{
isArduinoRx0BufferEmpty = true;
}
#endif
/* get data from receive buffer */
data = UART0_RxBuf[tmptail];
return (UART0_LastRxError << 8) + data;
}
int16_t getAvailableDataCountOnUart0(){
return (UART0_RX0_BUFFER_SIZE + UART0_RxHead - UART0_RxTail) & UART0_RX0_BUFFER_MASK;
}
ISR (USART0_RXC_vect){
uint16_t tmphead;
uint8_t data;
uint8_t usr;
uint8_t lastRxError;
/* read UART status register and UART data register */
usr = UART0_STATUS;
data = UART0_DATA;
/* */
lastRxError = (usr & (_BV(FE0)|_BV(DOR0)) );
/* calculate buffer index */
tmphead = ( UART0_RxHead + 1) & UART0_RX0_BUFFER_MASK;
#ifdef PLUS_BOARD
if ((((tmphead + 128) & UART0_RX0_BUFFER_MASK)== UART0_RxTail)){
isArduinoRx0BufferEmpty = false;
}
#endif
if ( tmphead == UART0_RxTail ) {
/* error: receive buffer overflow */
lastRxError = UART_BUFFER_OVERFLOW >> 8;
#ifdef PLUS_BOARD
isArduinoRx0BufferOverFlowed = true;
#endif
} else {
/* store new index */
UART0_RxHead = tmphead;
/* store received data in buffer */
UART0_RxBuf[tmphead] = data;
}
UART0_LastRxError = lastRxError;
}
int16_t readFromUart1(){
uint16_t tmptail;
uint8_t data;
if ( UART1_RxHead == UART1_RxTail ) {
return UART_NO_DATA; /* no data available */
}
/* calculate /store buffer index */
tmptail = (UART1_RxTail + 1) & UART_RX1_BUFFER_MASK;
UART1_RxTail = tmptail;
/* get data from receive buffer */
data = UART1_RxBuf[tmptail];
return (UART1_LastRxError << 8) + data;
}
int16_t getAvailableDataCountOnUart1(){
return (UART_RX1_BUFFER_SIZE + UART1_RxHead - UART1_RxTail) & UART_RX1_BUFFER_MASK;
}
ISR (USART1_RXC_vect){
uint16_t tmphead;
uint8_t data;
uint8_t usr;
uint8_t lastRxError;
/* read UART status register and UART data register */
usr = UART1_STATUS;
data = UART1_DATA;
/* */
lastRxError = (usr & (_BV(FE1)|_BV(DOR1)) );
/* calculate buffer index */
tmphead = ( UART1_RxHead + 1) & UART_RX1_BUFFER_MASK;
if ( tmphead == UART1_RxTail ) {
/* error: receive buffer overflow */
lastRxError = UART_BUFFER_OVERFLOW >> 8;
} else {
/* store new index */
UART1_RxHead = tmphead;
/* store received data in buffer */
UART1_RxBuf[tmphead] = data;
}
UART1_LastRxError = lastRxError;
enableTimerOverflow();
enableRxLedBlinking();
}
ISR(USART1_TXC_vect)
{
enableTimerOverflow();
enableTxLedBlinking();
}
#ifdef PLUS_BOARD
uint8_t getIsArduinoRx0BufferEmptyFlag()
{
return isArduinoRx0BufferEmpty;
}
void setIsArduinoRx0BufferEmptyFlag(uint8_t state)
{
isArduinoRx0BufferEmpty = state;
}
uint8_t getIsArduinoRx0BufferOverFlowedFlag()
{
return isArduinoRx0BufferOverFlowed;
}
void setIsArduinoRx0BufferOverFlowedFlag(uint8_t state)
{
isArduinoRx0BufferOverFlowed = state;
}
#endif