Rev 1199 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1199 | - | 1 | /**************************************************************************** |
1437 | - | 2 | * Copyright (C) 2011-2012 by Claas Anders "CaScAdE" Rathje * |
1199 | - | 3 | * admiralcascade@gmail.com * |
4 | * Project-URL: http://www.mylifesucks.de/oss/c-epilepsy/ * |
||
5 | * * |
||
6 | * This program is free software; you can redistribute it and/or modify * |
||
7 | * it under the terms of the GNU General Public License as published by * |
||
8 | * the Free Software Foundation; either version 2 of the License. * |
||
9 | * * |
||
10 | * This program is distributed in the hope that it will be useful, * |
||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
||
13 | * GNU General Public License for more details. * |
||
14 | * * |
||
15 | * You should have received a copy of the GNU General Public License * |
||
16 | * along with this program; if not, write to the * |
||
17 | * Free Software Foundation, Inc., * |
||
18 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
||
19 | ****************************************************************************/ |
||
20 | |||
21 | |||
22 | #include "usart0.h" |
||
23 | |||
24 | volatile uint8_t rxd_buffer_locked = 0; |
||
25 | volatile uint8_t rxd_buffer[RXD_BUFFER_LEN]; |
||
26 | volatile uint8_t txd_buffer[TXD_BUFFER_LEN]; |
||
27 | volatile uint8_t ReceivedBytes = 0; |
||
28 | volatile uint8_t *pRxData = 0; |
||
29 | volatile uint8_t RxDataLen = 0; |
||
30 | |||
31 | /** |
||
32 | * init usart0 |
||
33 | */ |
||
34 | void usart0_init() { |
||
35 | |||
36 | UBRR0H = ((F_CPU / (16UL * USART0_BAUD)) - 1) >> 8; |
||
37 | UBRR0L = (F_CPU / (16UL * USART0_BAUD)) - 1; |
||
38 | |||
39 | // Enable receiver and transmitter; enable RX interrupt |
||
40 | UCSR0B = (1 << RXEN0) | (1 << TXEN0) | (1 << RXCIE0); |
||
41 | |||
42 | // set asynchronous mode |
||
43 | UCSR0C &= ~(1 << UMSEL01); |
||
44 | UCSR0C &= ~(1 << UMSEL00); |
||
45 | // no parity |
||
46 | UCSR0C &= ~(1 << UPM01); |
||
47 | UCSR0C &= ~(1 << UPM00); |
||
48 | // 1 stop bit |
||
49 | UCSR0C &= ~(1 << USBS0); |
||
50 | // 8-bit |
||
51 | UCSR0B &= ~(1 << UCSZ02); |
||
52 | UCSR0C |= (1 << UCSZ01); |
||
53 | UCSR0C |= (1 << UCSZ00); |
||
54 | |||
55 | // set direction of RXD0 and TXD0 pins |
||
56 | // set RXD0 (PD0) as an input pin |
||
57 | PORTD &= ~(1 << PORTD0); |
||
58 | DDRD &= ~(1 << DDD0); |
||
59 | // set TXD0 (PD1) as an output pin |
||
60 | //PORTD |= (1 << PORTD1); |
||
61 | //DDRD &= ~(1 << DDD1); |
||
62 | |||
63 | } |
||
64 | |||
65 | /** |
||
66 | * disable the txd pin of usart0 |
||
67 | */ |
||
68 | void usart0_DisableTXD(void) { |
||
69 | UCSR0B &= ~(1 << TXCIE0); // disable TX-Interrupt |
||
70 | UCSR0B &= ~(1 << TXEN0); // disable TX in USART |
||
71 | DDRD &= ~(1 << DDD1); // set TXD pin as input |
||
72 | PORTD &= ~(1 << PORTD1); // disable pullup on TXD pin |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * enable the txd pin of usart0 |
||
77 | */ |
||
78 | void usart0_EnableTXD(void) { |
||
79 | DDRD |= (1 << DDD1); // set TXD pin as output |
||
80 | PORTD &= ~(1 << PORTD1); // disable pullup on TXD pin |
||
81 | UCSR0B |= (1 << TXEN0); // enable TX in USART |
||
82 | UCSR0B |= (1 << TXCIE0); // enable TX-Interrupt |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * send a single <character> through usart0 |
||
87 | */ |
||
88 | void usart0_putc(unsigned char character) { |
||
89 | // wait until UDR ready |
||
90 | while (!(UCSR0A & (1 << UDRE0))); |
||
91 | UDR0 = character; |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * send a <string> throught usart0 |
||
96 | */ |
||
97 | void usart0_puts(char *s) { |
||
98 | while (*s) { |
||
99 | usart0_putc(*s); |
||
100 | s++; |
||
101 | } |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * send a PGM<string> throught usart0 |
||
106 | */ |
||
107 | void usart0_puts_pgm(const char* string) { |
||
108 | while (pgm_read_byte(string) != 0x00) |
||
109 | usart0_putc(pgm_read_byte(string++)); |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * transmit interrupt handler |
||
114 | * unused |
||
115 | */ |
||
116 | ISR(USART0_TX_vect, ISR_BLOCK) { |
||
117 | } |
||
118 | |||
119 | /* |
||
120 | * receive data through usart0 |
||
121 | * portions taken and adapted from |
||
122 | * http://svn.mikrokopter.de/mikrowebsvn/filedetails.php?repname=FlightCtrl&path=%2Fbranches%2FV0.72p+Code+Redesign+killagreg%2Fuart0.c |
||
123 | */ |
||
124 | ISR(USART0_RX_vect, ISR_BLOCK) { |
||
125 | uint8_t c; |
||
126 | // catch the received byte |
||
127 | c = UDR0; |
||
128 | if (rxd_buffer_locked) return; // if rxd buffer is locked immediately return |
||
129 | static uint16_t crc; |
||
130 | static uint8_t ptr_rxd_buffer = 0; |
||
131 | static uint8_t c1 = 0; |
||
132 | static uint8_t c2 = 0; |
||
133 | static uint8_t usart_rx_ok = 0; |
||
134 | uint8_t crc1, crc2; |
||
135 | // the rxd buffer is unlocked |
||
136 | if (usart_rx_ok == 0) { |
||
137 | // if ((c2 == '#') && (c1 == 'b' || c1 == 'c') && (c == 'D' || c == 'V' || c == 'O')) { |
||
138 | |||
139 | if ((c2 == '#') && (c1 == 'b' || c1 == 'c') && |
||
140 | #if FCONLY |
||
141 | (c == 'V' || c == 'D' || c == 'Q' || c == 'L')) { // version, debug, settings, LCD |
||
142 | #else |
||
143 | (c == 'V' || c == 'O' || c == 'Q')) { // version, OSD, settings |
||
144 | #endif |
||
145 | usart_rx_ok = 1; |
||
146 | rxd_buffer[ptr_rxd_buffer++] = c2; |
||
147 | crc = c2; |
||
148 | rxd_buffer[ptr_rxd_buffer++] = c1; |
||
149 | crc += c1; |
||
150 | rxd_buffer[ptr_rxd_buffer++] = c; |
||
151 | crc += c; |
||
152 | c2 = 0; |
||
153 | c1 = 0; |
||
154 | } else { |
||
155 | c2 = c1; |
||
156 | c1 = c; |
||
157 | } |
||
158 | } else if (ptr_rxd_buffer < RXD_BUFFER_LEN) { // collect incomming bytes |
||
159 | if (c != '\r') { // no termination character |
||
160 | rxd_buffer[ptr_rxd_buffer++] = c; // copy byte to rxd buffer |
||
161 | crc += c; // update crc |
||
162 | } else { // termination character was received |
||
163 | // the last 2 bytes are no subject for checksum calculation |
||
164 | // they are the checksum itself |
||
165 | crc -= rxd_buffer[ptr_rxd_buffer - 2]; |
||
166 | crc -= rxd_buffer[ptr_rxd_buffer - 1]; |
||
167 | // calculate checksum from transmitted data |
||
168 | crc %= 4096; |
||
169 | crc1 = '=' + crc / 64; |
||
170 | crc2 = '=' + crc % 64; |
||
171 | // compare checksum to transmitted checksum bytes |
||
172 | if ((crc1 == rxd_buffer[ptr_rxd_buffer - 2]) && (crc2 == rxd_buffer[ptr_rxd_buffer - 1])) { // checksum valid |
||
173 | rxd_buffer[ptr_rxd_buffer] = '\r'; // set termination character |
||
174 | ReceivedBytes = ptr_rxd_buffer + 1; // store number of received bytes |
||
175 | rxd_buffer_locked = 1; // lock the rxd buffer |
||
176 | |||
177 | |||
178 | if (rxd_buffer[2] == 'R') { |
||
179 | wdt_enable(WDTO_250MS); // Reset-Commando |
||
180 | } |
||
181 | } else { // checksum invalid |
||
182 | rxd_buffer_locked = 0; // unlock rxd buffer |
||
183 | |||
184 | } |
||
185 | ptr_rxd_buffer = 0; // reset rxd buffer pointer |
||
186 | usart_rx_ok = 0; |
||
187 | } |
||
188 | } else { // rxd buffer overrun |
||
189 | ptr_rxd_buffer = 0; // reset rxd buffer |
||
190 | rxd_buffer_locked = 0; // unlock rxd buffer |
||
191 | usart_rx_ok = 0; |
||
192 | |||
193 | } |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * Decode the recevied Buffer |
||
198 | * portions taken and adapted from |
||
199 | * http://svn.mikrokopter.de/mikrowebsvn/filedetails.php?repname=FlightCtrl&path=%2Ftags%2FV0.72p%2Fuart.c |
||
200 | */ |
||
201 | void Decode64(void) { |
||
202 | uint8_t a, b, c, d; |
||
203 | uint8_t x, y, z; |
||
204 | uint8_t ptrIn = 3; |
||
205 | uint8_t ptrOut = 3; |
||
206 | uint8_t len = ReceivedBytes - 6; |
||
207 | |||
208 | while (len) { |
||
209 | a = rxd_buffer[ptrIn++] - '='; |
||
210 | b = rxd_buffer[ptrIn++] - '='; |
||
211 | c = rxd_buffer[ptrIn++] - '='; |
||
212 | d = rxd_buffer[ptrIn++] - '='; |
||
213 | |||
214 | x = (a << 2) | (b >> 4); |
||
215 | y = ((b & 0x0f) << 4) | (c >> 2); |
||
216 | z = ((c & 0x03) << 6) | d; |
||
217 | |||
218 | if (len--) rxd_buffer[ptrOut++] = x; |
||
219 | else break; |
||
220 | if (len--) rxd_buffer[ptrOut++] = y; |
||
221 | else break; |
||
222 | if (len--) rxd_buffer[ptrOut++] = z; |
||
223 | else break; |
||
224 | } |
||
225 | pRxData = &rxd_buffer[3]; |
||
226 | RxDataLen = ptrOut - 3; |
||
227 | } |
||
228 | |||
229 | /** |
||
230 | * request Data through USART in special MK format by adding checksum and |
||
231 | * encode data in modified Base64 |
||
232 | * portions taken and adapted from |
||
233 | * http://svn.mikrokopter.de/mikrowebsvn/filedetails.php?repname=FlightCtrl&path=%2Ftags%2FV0.72p%2Fuart.c |
||
234 | */ |
||
235 | void sendMKData(unsigned char cmd, unsigned char addr, unsigned char *snd, unsigned char len) { |
||
236 | unsigned int pt = 0; |
||
237 | unsigned char a, b, c; |
||
238 | unsigned char ptr = 0; |
||
239 | |||
240 | txd_buffer[pt++] = '#'; // Start-Byte |
||
241 | txd_buffer[pt++] = 'a' + addr; // Adress |
||
242 | txd_buffer[pt++] = cmd; // Command |
||
243 | while (len) { |
||
244 | if (len) { |
||
245 | a = snd[ptr++]; |
||
246 | len--; |
||
247 | } else a = 0; |
||
248 | if (len) { |
||
249 | b = snd[ptr++]; |
||
250 | len--; |
||
251 | } else b = 0; |
||
252 | if (len) { |
||
253 | c = snd[ptr++]; |
||
254 | len--; |
||
255 | } else c = 0; |
||
256 | txd_buffer[pt++] = '=' + (a >> 2); |
||
257 | txd_buffer[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4)); |
||
258 | txd_buffer[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6)); |
||
259 | txd_buffer[pt++] = '=' + (c & 0x3f); |
||
260 | } |
||
261 | |||
262 | // add crc |
||
263 | unsigned int tmpCRC = 0, i; |
||
264 | for (i = 0; i < pt; i++) { |
||
265 | tmpCRC += txd_buffer[i]; |
||
266 | } |
||
267 | tmpCRC %= 4096; |
||
268 | txd_buffer[i++] = '=' + tmpCRC / 64; |
||
269 | txd_buffer[i++] = '=' + tmpCRC % 64; |
||
270 | txd_buffer[i++] = '\r'; |
||
271 | |||
272 | usart0_puts((char*)txd_buffer); |
||
273 | } |
||
274 | |||
275 | /** |
||
276 | * short script to directly send a request thorugh usart including en- and disabling it |
||
277 | * where <address> is the address of the receipient, <label> is which data set to request |
||
278 | * and <ms> represents the milliseconds delay between data |
||
279 | */ |
||
280 | void usart0_request_mk_data(uint8_t address, char label, uint8_t ms) { |
||
281 | // re-enable TXD pin |
||
282 | usart0_EnableTXD(); |
||
283 | |||
284 | unsigned char mstenth = ms / 10; |
||
285 | sendMKData(label, address, &mstenth, 1); |
||
286 | // wait until UDR ready |
||
287 | while (!(UCSR0A & (1 << UDRE0))); |
||
288 | // disable TXD pin again |
||
289 | usart0_DisableTXD(); |
||
290 | } |
||
291 | |||
292 | |||
293 |