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