-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusart.c
170 lines (131 loc) · 5.38 KB
/
usart.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
// Usart.c - Copyright 2016, HZB, ILL/SANE & ISIS
#define RELEASE_USART 1.00
// HISTORY -------------------------------------------------------------
// 1.00 - First release on March 2016
#include <avr/io.h>
#include <avr/interrupt.h>
#include <string.h>
#include "../config.h"
#include "xbee.h"
#include "xbee_utilities.h"
#include "usart.h"
UsartType USART0 = {.cmd_line = 0,.sending_cmd = 0};
UsartType USART1 = {.cmd_line = 0,.sending_cmd = 0};
uint8_t lastUSART1_Message[50];
// Initialize Interrupt Service Routine driven USART communication
void usart_init(uint16_t UBRR_register)
{
UCSR0B = 0; // Added by JG to be sure...
UCSR0B |= (1 << RXEN0) | (1 << TXEN0) | (1 << RXCIE0) | (1 << TXCIE0); //Turn on the transmission and reception circuitry
// UCSR0B => USART Control and Status Register 0 B
// TXEN0 => Transmitter Enable: Writing this bit to one enables the USART Transmitter. Details on datasheet - page 187
// RXEN0 => Receiver Enable: Writing this bit to one enables the USART Receiver. Details on datasheet - page 187
// RXCIE0 => RX Complete Interrupt Enable: Writing this bit to one enables interrupt on the RXCn Flag. A USART Receive Complete interrupt will be generated only if the RXCIEn bit is written to one, the Global Interrupt Flag in SREG is written to one and the RXCn bit in UCSRnA is set.
// TXCIE0 => TX Complete Interrupt Enable: Writing this bit to one enables interrupt on the TXCn Flag. A USART Transmit Complete interrupt will be generated only if the TXCIEn bit is written to one, the Global Interrupt Flag in SREG is written to one and the TXCn bit in UCSRnA is set.
UCSR0C = 0; // Added by JG to be sure...
UCSR0C |= (1 << UCSZ01) | (1 << UCSZ00); // Use 8-bit character sizes
// Load lower 8-bits of the baud rate value into the low byte of the UBRR register
UBRR0L = (unsigned char)UBRR_register;
// Load upper 8-bits of the baud rate value into the high byte of the UBRR register
UBRR0H = (unsigned char)(UBRR_register>>8);
buffer_init(); //Init RingBuffer
}
// Initialize Interrupt Service Routine driven USART communication
void usart1_init(uint16_t UBRR_register)
{
UCSR1B = 0; // Added by JG to be sure...
UCSR1B |= (1 << RXEN1) | (1 << TXEN1) | (1 << RXCIE1) | (1 << TXCIE1); //Turn on the transmission and reception circuitry
// UCSR0B => USART Control and Status Register 0 B
// TXEN0 => Transmitter Enable: Writing this bit to one enables the USART Transmitter. Details on datasheet - page 187
// RXEN0 => Receiver Enable: Writing this bit to one enables the USART Receiver. Details on datasheet - page 187
// RXCIE0 => RX Complete Interrupt Enable: Writing this bit to one enables interrupt on the RXCn Flag. A USART Receive Complete interrupt will be generated only if the RXCIEn bit is written to one, the Global Interrupt Flag in SREG is written to one and the RXCn bit in UCSRnA is set.
// TXCIE0 => TX Complete Interrupt Enable: Writing this bit to one enables interrupt on the TXCn Flag. A USART Transmit Complete interrupt will be generated only if the TXCIEn bit is written to one, the Global Interrupt Flag in SREG is written to one and the TXCn bit in UCSRnA is set.
UCSR1C = 0; // Added by JG to be sure...
UCSR1C |= (1 << UCSZ11) | (1 << UCSZ10); // Use 8-bit character sizes
// Load lower 8-bits of the baud rate value into the low byte of the UBRR register
UBRR1L = (unsigned char)UBRR_register;
// Load upper 8-bits of the baud rate value into the high byte of the UBRR register
UBRR1H = (unsigned char)(UBRR_register>>8);
//buffer_init(); //Init RingBuffer
}
// USART0 data received interrupt service routine (from XBee module)
ISR(USART0_RX_vect)
{
static uint16_t length_counter = 0;
static uint16_t summe = 0;
static uint16_t frame_length = 0;
static uint8_t buffer[SINGLE_FRAME_LENGTH];
static uint8_t buffer_size = 0;
uint8_t data = USART_IODR; // Read USART Input Data Register
if(buffer_size >= SINGLE_FRAME_LENGTH-1)
{
xbee_build_frame(buffer, buffer_size);
buffer_size=0;
frame_length=0;
}
if(USART0.cmd_line) return;
if (length_counter > 1)
{
length_counter--;
summe=+data;
buffer[buffer_size++] = data;
}
if (frame_length > 1)
{
frame_length--;
buffer[buffer_size++] = data;
}
if (frame_length == 1)
{
frame_length--;
xbee_build_frame(buffer, buffer_size);
buffer_size = 0;
return;
}
if (length_counter == 1)
{
frame_length = summe+2;
length_counter--;
}
if (data == 0x7E)
{
if(!frame_length)
{
length_counter = 3;
buffer[buffer_size++] = data;
}
}
}
// USART1 data received interrupt service routine (from XBee module)
ISR(USART1_RX_vect)
{
static uint8_t buffer[50];
static uint8_t buffer_size = 0;
uint8_t data = USART_IODR1 ; // Read USART Input Data Register
if((buffer_size >= 50-2)||(data == '\r'))
{
buffer[buffer_size++] = '\0';
memcpy(lastUSART1_Message, buffer,buffer_size);
buffer_size=0;
return;
}
buffer[buffer_size++] = data;
}
// USART0 data transmitted interrupt service routine (to XBee module)
ISR(USART0_TX_vect)
{
if(USART0.sending_cmd)
{
USART_IODR = *USART0.send_str_reader++; // Write USART0 Input Data Register
USART0.sending_cmd--;
}
}
// USART0 data transmitted interrupt service routine (to XBee module)
ISR(USART1_TX_vect)
{
if(USART1.sending_cmd)
{
USART_IODR1 = *USART1.send_str_reader++; // Write USART0 Input Data Register
USART1.sending_cmd--;
}
}