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