Rev 686 | Rev 738 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 686 | Rev 728 | ||
---|---|---|---|
1 | /**************************************************************************** |
1 | /**************************************************************************** |
2 | * Copyright (C) 2009 by Claas Anders "CaScAdE" Rathje * |
2 | * Copyright (C) 2009-2010 by Claas Anders "CaScAdE" Rathje * |
3 | * admiralcascade@gmail.com * |
3 | * admiralcascade@gmail.com * |
4 | * Project-URL: http://www.mylifesucks.de/oss/c-osd/ * |
4 | * Project-URL: http://www.mylifesucks.de/oss/c-osd/ * |
5 | * * |
5 | * * |
6 | * This program is free software; you can redistribute it and/or modify * |
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 * |
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. * |
8 | * the Free Software Foundation; either version 2 of the License. * |
9 | * * |
9 | * * |
10 | * This program is distributed in the hope that it will be useful, * |
10 | * This program is distributed in the hope that it will be useful, * |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
13 | * GNU General Public License for more details. * |
13 | * GNU General Public License for more details. * |
14 | * * |
14 | * * |
15 | * You should have received a copy of the GNU General Public License * |
15 | * You should have received a copy of the GNU General Public License * |
16 | * along with this program; if not, write to the * |
16 | * along with this program; if not, write to the * |
17 | * Free Software Foundation, Inc., * |
17 | * Free Software Foundation, Inc., * |
18 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
18 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
19 | ****************************************************************************/ |
19 | ****************************************************************************/ |
20 | 20 | ||
21 | #include <avr/io.h> |
21 | #include <avr/io.h> |
22 | #include <avr/interrupt.h> |
22 | #include <avr/interrupt.h> |
23 | #include <util/delay.h> |
23 | #include <util/delay.h> |
24 | #include "main.h" |
24 | #include "main.h" |
25 | #include "usart1.h" |
25 | #include "usart1.h" |
26 | 26 | ||
27 | #if !(ALLCHARSDEBUG|(WRITECHARS != -1)) |
27 | #if !(ALLCHARSDEBUG|(WRITECHARS != -1)) |
28 | 28 | ||
29 | volatile uint8_t rxd_buffer_locked = 0; |
29 | volatile uint8_t rxd_buffer_locked = 0; |
30 | volatile uint8_t rxd_buffer[RXD_BUFFER_LEN]; |
30 | volatile uint8_t rxd_buffer[RXD_BUFFER_LEN]; |
31 | volatile uint8_t txd_buffer[TXD_BUFFER_LEN]; |
31 | volatile uint8_t txd_buffer[TXD_BUFFER_LEN]; |
32 | volatile uint8_t ReceivedBytes = 0; |
32 | volatile uint8_t ReceivedBytes = 0; |
33 | volatile uint8_t *pRxData = 0; |
33 | volatile uint8_t *pRxData = 0; |
34 | volatile uint8_t RxDataLen = 0; |
34 | volatile uint8_t RxDataLen = 0; |
35 | 35 | ||
36 | /* ########################################################################## |
36 | /* ########################################################################## |
37 | * USART stuff |
37 | * USART stuff |
38 | * ##########################################################################*/ |
38 | * ##########################################################################*/ |
39 | 39 | ||
40 | /** |
40 | /** |
41 | * init usart1 |
41 | * init usart1 |
42 | */ |
42 | */ |
43 | void usart1_init() { |
43 | void usart1_init() { |
44 | UBRR1H = ((F_CPU / (16UL * baud)) - 1) >> 8; |
44 | UBRR1H = ((F_CPU / (16UL * baud)) - 1) >> 8; |
45 | UBRR1L = (F_CPU / (16UL * baud)) - 1; |
45 | UBRR1L = (F_CPU / (16UL * baud)) - 1; |
46 | 46 | ||
47 | // Enable receiver and transmitter; enable RX interrupt |
47 | // Enable receiver and transmitter; enable RX interrupt |
48 | UCSR1B = (1 << RXEN1) | (1 << TXEN1) | (1 << RXCIE1); |
48 | UCSR1B = (1 << RXEN1) | (1 << TXEN1) | (1 << RXCIE1); |
49 | 49 | ||
50 | //asynchronous 8N1 |
50 | //asynchronous 8N1 |
51 | UCSR1C = (1 << URSEL1) | (3 << UCSZ10); |
51 | UCSR1C = (1 << URSEL1) | (3 << UCSZ10); |
52 | } |
52 | } |
53 | 53 | ||
54 | /** |
54 | /** |
55 | * disable the txd pin of usart1 |
55 | * disable the txd pin of usart1 |
56 | */ |
56 | */ |
57 | void usart1_DisableTXD(void) { |
57 | void usart1_DisableTXD(void) { |
58 | UCSR1B &= ~(1 << TXCIE1); // disable TX-Interrupt |
58 | UCSR1B &= ~(1 << TXCIE1); // disable TX-Interrupt |
59 | UCSR1B &= ~(1 << TXEN1); // disable TX in USART |
59 | UCSR1B &= ~(1 << TXEN1); // disable TX in USART |
60 | DDRB &= ~(1 << DDB3); // set TXD pin as input |
60 | DDRB &= ~(1 << DDB3); // set TXD pin as input |
61 | PORTB &= ~(1 << PORTB3); // disable pullup on TXD pin |
61 | PORTB &= ~(1 << PORTB3); // disable pullup on TXD pin |
62 | } |
62 | } |
63 | 63 | ||
64 | /** |
64 | /** |
65 | * enable the txd pin of usart1 |
65 | * enable the txd pin of usart1 |
66 | */ |
66 | */ |
67 | void usart1_EnableTXD(void) { |
67 | void usart1_EnableTXD(void) { |
68 | DDRB |= (1 << DDB3); // set TXD pin as output |
68 | DDRB |= (1 << DDB3); // set TXD pin as output |
69 | PORTB &= ~(1 << PORTB3); // disable pullup on TXD pin |
69 | PORTB &= ~(1 << PORTB3); // disable pullup on TXD pin |
70 | UCSR1B |= (1 << TXEN1); // enable TX in USART |
70 | UCSR1B |= (1 << TXEN1); // enable TX in USART |
71 | UCSR1B |= (1 << TXCIE1); // enable TX-Interrupt |
71 | UCSR1B |= (1 << TXCIE1); // enable TX-Interrupt |
72 | } |
72 | } |
73 | 73 | ||
74 | /** |
74 | /** |
75 | * send a single <character> through usart1 |
75 | * send a single <character> through usart1 |
76 | */ |
76 | */ |
77 | void usart1_putc(unsigned char character) { |
77 | void usart1_putc(unsigned char character) { |
78 | // wait until UDR ready |
78 | // wait until UDR ready |
79 | while (!(UCSR1A & (1 << UDRE1))); |
79 | while (!(UCSR1A & (1 << UDRE1))); |
80 | UDR1 = character; |
80 | UDR1 = character; |
81 | } |
81 | } |
82 | 82 | ||
83 | /** |
83 | /** |
84 | * send a <string> throught usart1 |
84 | * send a <string> throught usart1 |
85 | */ |
85 | */ |
86 | void usart1_puts(char *s) { |
86 | void usart1_puts(char *s) { |
87 | while (*s) { |
87 | while (*s) { |
88 | usart1_putc(*s); |
88 | usart1_putc(*s); |
89 | s++; |
89 | s++; |
90 | } |
90 | } |
91 | } |
91 | } |
92 | 92 | ||
93 | /** |
93 | /** |
94 | * transmit interrupt handler |
94 | * transmit interrupt handler |
95 | * unused |
95 | * unused |
96 | */ |
96 | */ |
97 | ISR(SIG_USART1_DATA) { |
97 | ISR(SIG_USART1_DATA) { |
98 | } |
98 | } |
99 | 99 | ||
100 | /* |
100 | /* |
101 | * receive data through usart1 |
101 | * receive data through usart1 |
102 | * portions taken and adapted from |
102 | * portions taken and adapted from |
103 | * http://svn.mikrokopter.de/mikrowebsvn/filedetails.php?repname=FlightCtrl&path=%2Fbranches%2FV0.72p+Code+Redesign+killagreg%2Fuart0.c |
103 | * http://svn.mikrokopter.de/mikrowebsvn/filedetails.php?repname=FlightCtrl&path=%2Fbranches%2FV0.72p+Code+Redesign+killagreg%2Fuart0.c |
104 | */ |
104 | */ |
105 | ISR(SIG_USART1_RECV) { |
105 | ISR(SIG_USART1_RECV) { |
106 | uint8_t c; |
106 | uint8_t c; |
107 | // catch the received byte |
107 | // catch the received byte |
108 | c = UDR1; |
108 | c = UDR1; |
109 | if (rxd_buffer_locked) return; // if rxd buffer is locked immediately return |
109 | if (rxd_buffer_locked) return; // if rxd buffer is locked immediately return |
110 | static uint16_t crc; |
110 | static uint16_t crc; |
111 | static uint8_t ptr_rxd_buffer = 0; |
111 | static uint8_t ptr_rxd_buffer = 0; |
112 | static uint8_t c1 = 0; |
112 | static uint8_t c1 = 0; |
113 | static uint8_t c2 = 0; |
113 | static uint8_t c2 = 0; |
114 | static uint8_t usart_rx_ok = 0; |
114 | static uint8_t usart_rx_ok = 0; |
115 | uint8_t crc1, crc2; |
115 | uint8_t crc1, crc2; |
116 | // the rxd buffer is unlocked |
116 | // the rxd buffer is unlocked |
117 | if (usart_rx_ok == 0) { |
117 | if (usart_rx_ok == 0) { |
118 | if ((c2 == '#') && (c1 == 'b' || c1 == 'c') && (c == 'D' || c == 'V' || c == 'O')) { |
118 | if ((c2 == '#') && (c1 == 'b' || c1 == 'c') && (c == 'D' || c == 'V' || c == 'O')) { |
119 | usart_rx_ok = 1; |
119 | usart_rx_ok = 1; |
120 | rxd_buffer[ptr_rxd_buffer++] = c2; |
120 | rxd_buffer[ptr_rxd_buffer++] = c2; |
121 | crc = c2; |
121 | crc = c2; |
122 | rxd_buffer[ptr_rxd_buffer++] = c1; |
122 | rxd_buffer[ptr_rxd_buffer++] = c1; |
123 | crc += c1; |
123 | crc += c1; |
124 | rxd_buffer[ptr_rxd_buffer++] = c; |
124 | rxd_buffer[ptr_rxd_buffer++] = c; |
125 | crc += c; |
125 | crc += c; |
126 | c2 = 0; |
126 | c2 = 0; |
127 | c1 = 0; |
127 | c1 = 0; |
128 | LED1_ON |
128 | LED1_ON |
129 | LED2_OFF |
129 | LED2_OFF |
130 | } else { |
130 | } else { |
131 | c2 = c1; |
131 | c2 = c1; |
132 | c1 = c; |
132 | c1 = c; |
133 | } |
133 | } |
134 | } else if (ptr_rxd_buffer < RXD_BUFFER_LEN) { // collect incomming bytes |
134 | } else if (ptr_rxd_buffer < RXD_BUFFER_LEN) { // collect incomming bytes |
135 | if (c != '\r') { // no termination character |
135 | if (c != '\r') { // no termination character |
136 | rxd_buffer[ptr_rxd_buffer++] = c; // copy byte to rxd buffer |
136 | rxd_buffer[ptr_rxd_buffer++] = c; // copy byte to rxd buffer |
137 | crc += c; // update crc |
137 | crc += c; // update crc |
138 | } else { // termination character was received |
138 | } else { // termination character was received |
139 | // the last 2 bytes are no subject for checksum calculation |
139 | // the last 2 bytes are no subject for checksum calculation |
140 | // they are the checksum itself |
140 | // they are the checksum itself |
141 | crc -= rxd_buffer[ptr_rxd_buffer - 2]; |
141 | crc -= rxd_buffer[ptr_rxd_buffer - 2]; |
142 | crc -= rxd_buffer[ptr_rxd_buffer - 1]; |
142 | crc -= rxd_buffer[ptr_rxd_buffer - 1]; |
143 | // calculate checksum from transmitted data |
143 | // calculate checksum from transmitted data |
144 | crc %= 4096; |
144 | crc %= 4096; |
145 | crc1 = '=' + crc / 64; |
145 | crc1 = '=' + crc / 64; |
146 | crc2 = '=' + crc % 64; |
146 | crc2 = '=' + crc % 64; |
147 | // compare checksum to transmitted checksum bytes |
147 | // compare checksum to transmitted checksum bytes |
148 | if ((crc1 == rxd_buffer[ptr_rxd_buffer - 2]) && (crc2 == rxd_buffer[ptr_rxd_buffer - 1])) { // checksum valid |
148 | if ((crc1 == rxd_buffer[ptr_rxd_buffer - 2]) && (crc2 == rxd_buffer[ptr_rxd_buffer - 1])) { // checksum valid |
149 | rxd_buffer[ptr_rxd_buffer] = '\r'; // set termination character |
149 | rxd_buffer[ptr_rxd_buffer] = '\r'; // set termination character |
150 | ReceivedBytes = ptr_rxd_buffer + 1; // store number of received bytes |
150 | ReceivedBytes = ptr_rxd_buffer + 1; // store number of received bytes |
151 | rxd_buffer_locked = 1; // lock the rxd buffer |
151 | rxd_buffer_locked = 1; // lock the rxd buffer |
152 | LED1_OFF |
152 | LED1_OFF |
153 | } else { // checksum invalid |
153 | } else { // checksum invalid |
154 | rxd_buffer_locked = 0; // unlock rxd buffer |
154 | rxd_buffer_locked = 0; // unlock rxd buffer |
155 | LED2_ON |
155 | LED2_ON |
156 | } |
156 | } |
157 | ptr_rxd_buffer = 0; // reset rxd buffer pointer |
157 | ptr_rxd_buffer = 0; // reset rxd buffer pointer |
158 | usart_rx_ok = 0; |
158 | usart_rx_ok = 0; |
159 | } |
159 | } |
160 | } else { // rxd buffer overrun |
160 | } else { // rxd buffer overrun |
161 | ptr_rxd_buffer = 0; // reset rxd buffer |
161 | ptr_rxd_buffer = 0; // reset rxd buffer |
162 | rxd_buffer_locked = 0; // unlock rxd buffer |
162 | rxd_buffer_locked = 0; // unlock rxd buffer |
163 | usart_rx_ok = 0; |
163 | usart_rx_ok = 0; |
164 | LED2_ON |
164 | LED2_ON |
165 | } |
165 | } |
166 | } |
166 | } |
167 | 167 | ||
168 | /** |
168 | /** |
169 | * Decode the recevied Buffer |
169 | * Decode the recevied Buffer |
170 | * portions taken and adapted from |
170 | * portions taken and adapted from |
171 | * http://svn.mikrokopter.de/mikrowebsvn/filedetails.php?repname=FlightCtrl&path=%2Ftags%2FV0.72p%2Fuart.c |
171 | * http://svn.mikrokopter.de/mikrowebsvn/filedetails.php?repname=FlightCtrl&path=%2Ftags%2FV0.72p%2Fuart.c |
172 | */ |
172 | */ |
173 | void Decode64(void) { |
173 | void Decode64(void) { |
174 | uint8_t a, b, c, d; |
174 | uint8_t a, b, c, d; |
175 | uint8_t x, y, z; |
175 | uint8_t x, y, z; |
176 | uint8_t ptrIn = 3; |
176 | uint8_t ptrIn = 3; |
177 | uint8_t ptrOut = 3; |
177 | uint8_t ptrOut = 3; |
178 | uint8_t len = ReceivedBytes - 6; |
178 | uint8_t len = ReceivedBytes - 6; |
179 | 179 | ||
180 | while (len) { |
180 | while (len) { |
181 | a = rxd_buffer[ptrIn++] - '='; |
181 | a = rxd_buffer[ptrIn++] - '='; |
182 | b = rxd_buffer[ptrIn++] - '='; |
182 | b = rxd_buffer[ptrIn++] - '='; |
183 | c = rxd_buffer[ptrIn++] - '='; |
183 | c = rxd_buffer[ptrIn++] - '='; |
184 | d = rxd_buffer[ptrIn++] - '='; |
184 | d = rxd_buffer[ptrIn++] - '='; |
185 | 185 | ||
186 | x = (a << 2) | (b >> 4); |
186 | x = (a << 2) | (b >> 4); |
187 | y = ((b & 0x0f) << 4) | (c >> 2); |
187 | y = ((b & 0x0f) << 4) | (c >> 2); |
188 | z = ((c & 0x03) << 6) | d; |
188 | z = ((c & 0x03) << 6) | d; |
189 | 189 | ||
190 | if (len--) rxd_buffer[ptrOut++] = x; |
190 | if (len--) rxd_buffer[ptrOut++] = x; |
191 | else break; |
191 | else break; |
192 | if (len--) rxd_buffer[ptrOut++] = y; |
192 | if (len--) rxd_buffer[ptrOut++] = y; |
193 | else break; |
193 | else break; |
194 | if (len--) rxd_buffer[ptrOut++] = z; |
194 | if (len--) rxd_buffer[ptrOut++] = z; |
195 | else break; |
195 | else break; |
196 | } |
196 | } |
197 | pRxData = &rxd_buffer[3]; |
197 | pRxData = &rxd_buffer[3]; |
198 | RxDataLen = ptrOut - 3; |
198 | RxDataLen = ptrOut - 3; |
199 | } |
199 | } |
200 | 200 | ||
201 | /** |
201 | /** |
202 | * request Data through USART in special MK format by adding checksum and |
202 | * request Data through USART in special MK format by adding checksum and |
203 | * encode data in modified Base64 |
203 | * encode data in modified Base64 |
204 | * portions taken and adapted from |
204 | * portions taken and adapted from |
205 | * http://svn.mikrokopter.de/mikrowebsvn/filedetails.php?repname=FlightCtrl&path=%2Ftags%2FV0.72p%2Fuart.c |
205 | * http://svn.mikrokopter.de/mikrowebsvn/filedetails.php?repname=FlightCtrl&path=%2Ftags%2FV0.72p%2Fuart.c |
206 | */ |
206 | */ |
207 | void sendMKData(unsigned char cmd, unsigned char addr, unsigned char *snd, unsigned char len) { |
207 | void sendMKData(unsigned char cmd, unsigned char addr, unsigned char *snd, unsigned char len) { |
208 | unsigned int pt = 0; |
208 | unsigned int pt = 0; |
209 | unsigned char a, b, c; |
209 | unsigned char a, b, c; |
210 | unsigned char ptr = 0; |
210 | unsigned char ptr = 0; |
211 | 211 | ||
212 | txd_buffer[pt++] = '#'; // Start-Byte |
212 | txd_buffer[pt++] = '#'; // Start-Byte |
213 | txd_buffer[pt++] = 'a' + addr; // Adress |
213 | txd_buffer[pt++] = 'a' + addr; // Adress |
214 | txd_buffer[pt++] = cmd; // Command |
214 | txd_buffer[pt++] = cmd; // Command |
215 | while (len) { |
215 | while (len) { |
216 | if (len) { |
216 | if (len) { |
217 | a = snd[ptr++]; |
217 | a = snd[ptr++]; |
218 | len--; |
218 | len--; |
219 | } else a = 0; |
219 | } else a = 0; |
220 | if (len) { |
220 | if (len) { |
221 | b = snd[ptr++]; |
221 | b = snd[ptr++]; |
222 | len--; |
222 | len--; |
223 | } else b = 0; |
223 | } else b = 0; |
224 | if (len) { |
224 | if (len) { |
225 | c = snd[ptr++]; |
225 | c = snd[ptr++]; |
226 | len--; |
226 | len--; |
227 | } else c = 0; |
227 | } else c = 0; |
228 | txd_buffer[pt++] = '=' + (a >> 2); |
228 | txd_buffer[pt++] = '=' + (a >> 2); |
229 | txd_buffer[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4)); |
229 | txd_buffer[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4)); |
230 | txd_buffer[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6)); |
230 | txd_buffer[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6)); |
231 | txd_buffer[pt++] = '=' + (c & 0x3f); |
231 | txd_buffer[pt++] = '=' + (c & 0x3f); |
232 | } |
232 | } |
233 | 233 | ||
234 | // add crc |
234 | // add crc |
235 | unsigned int tmpCRC = 0, i; |
235 | unsigned int tmpCRC = 0, i; |
236 | for (i = 0; i < pt; i++) { |
236 | for (i = 0; i < pt; i++) { |
237 | tmpCRC += txd_buffer[i]; |
237 | tmpCRC += txd_buffer[i]; |
238 | } |
238 | } |
239 | tmpCRC %= 4096; |
239 | tmpCRC %= 4096; |
240 | txd_buffer[i++] = '=' + tmpCRC / 64; |
240 | txd_buffer[i++] = '=' + tmpCRC / 64; |
241 | txd_buffer[i++] = '=' + tmpCRC % 64; |
241 | txd_buffer[i++] = '=' + tmpCRC % 64; |
242 | txd_buffer[i++] = '\r'; |
242 | txd_buffer[i++] = '\r'; |
243 | 243 | ||
244 | usart1_puts((char*) txd_buffer); |
244 | usart1_puts((char*) txd_buffer); |
245 | } |
245 | } |
246 | 246 | ||
247 | /** |
247 | /** |
248 | * short script to directly send a request thorugh usart including en- and disabling it |
248 | * short script to directly send a request thorugh usart including en- and disabling it |
249 | * where <address> is the address of the receipient, <label> is which data set to request |
249 | * where <address> is the address of the receipient, <label> is which data set to request |
250 | * and <ms> represents the milliseconds delay between data |
250 | * and <ms> represents the milliseconds delay between data |
251 | */ |
251 | */ |
252 | void usart1_request_mk_data(uint8_t address, char label, uint8_t ms) { |
252 | void usart1_request_mk_data(uint8_t address, char label, uint8_t ms) { |
253 | // re-enable TXD pin |
253 | // re-enable TXD pin |
254 | usart1_EnableTXD(); |
254 | usart1_EnableTXD(); |
255 | 255 | ||
256 | unsigned char mstenth = ms / 10; |
256 | unsigned char mstenth = ms / 10; |
257 | sendMKData(label, address, &mstenth, 1); |
257 | sendMKData(label, address, &mstenth, 1); |
258 | // wait until UDR ready |
258 | // wait until UDR ready |
259 | while (!(UCSR1A & (1 << UDRE1))); |
259 | while (!(UCSR1A & (1 << UDRE1))); |
260 | // disable TXD pin again |
260 | // disable TXD pin again |
261 | usart1_DisableTXD(); |
261 | usart1_DisableTXD(); |
262 | } |
262 | } |
263 | 263 | ||
264 | #endif |
264 | #endif |
265 | 265 |