Rev 436 | Rev 759 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 436 | Rev 744 | ||
---|---|---|---|
1 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2 | // + Copyright (c) 04.2007 Holger Buss |
2 | // + Copyright (c) 04.2007 Holger Buss |
3 | // + only for non-profit use |
3 | // + only for non-profit use |
4 | // + www.MikroKopter.com |
4 | // + www.MikroKopter.com |
5 | // + see the File "License.txt" for further Informations |
5 | // + see the File "License.txt" for further Informations |
6 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
6 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
7 | 7 | ||
8 | #include <avr/io.h> |
8 | #include <avr/io.h> |
9 | #include <avr/interrupt.h> |
9 | #include <avr/interrupt.h> |
10 | #include <avr/pgmspace.h> |
10 | #include <avr/pgmspace.h> |
11 | #include <avr/wdt.h> |
11 | #include <avr/wdt.h> |
12 | #include <stdarg.h> |
12 | #include <stdarg.h> |
13 | #include <string.h> |
13 | #include <string.h> |
14 | 14 | ||
15 | #include "main.h" |
15 | #include "main.h" |
16 | #include "menu.h" |
16 | #include "menu.h" |
17 | #include "timer0.h" |
17 | #include "timer0.h" |
18 | #include "uart0.h" |
18 | #include "uart0.h" |
19 | #include "ubx.h" |
19 | #include "ubx.h" |
20 | #include "printf_P.h" |
20 | #include "printf_P.h" |
21 | 21 | ||
22 | 22 | ||
23 | #define FC_ADDRESS 1 |
23 | #define FC_ADDRESS 1 |
24 | #define NC_ADDRESS 2 |
24 | #define NC_ADDRESS 2 |
25 | #define MK3MAG_ADDRESS 3 |
25 | #define MK3MAG_ADDRESS 3 |
26 | 26 | ||
27 | #define FM_ADDRESS 10 // FOLLOW ME |
27 | #define FM_ADDRESS 10 // FOLLOW ME |
28 | 28 | ||
29 | #define FALSE 0 |
29 | #define FALSE 0 |
30 | #define TRUE 1 |
30 | #define TRUE 1 |
31 | 31 | ||
32 | 32 | ||
33 | //int8_t test __attribute__ ((section (".noinit"))); |
33 | //int8_t test __attribute__ ((section (".noinit"))); |
34 | uint8_t Request_VerInfo = FALSE; |
34 | uint8_t Request_VerInfo = FALSE; |
35 | uint8_t Request_Display = FALSE; |
35 | uint8_t Request_Display = FALSE; |
36 | uint8_t Request_Display1 = FALSE; |
36 | uint8_t Request_Display1 = FALSE; |
37 | uint8_t Request_ExternalControl = FALSE; |
37 | uint8_t Request_ExternalControl = FALSE; |
38 | uint8_t Request_DebugData = FALSE; |
38 | uint8_t Request_DebugData = FALSE; |
39 | uint8_t Request_DebugLabel = 255; |
39 | uint8_t Request_DebugLabel = 255; |
40 | uint8_t Request_SendFollowMe = FALSE; |
40 | uint8_t Request_SendFollowMe = FALSE; |
41 | uint8_t DisplayLine = 0; |
41 | uint8_t DisplayLine = 0; |
42 | 42 | ||
43 | volatile uint8_t txd_buffer[TXD_BUFFER_LEN]; |
43 | volatile uint8_t txd_buffer[TXD_BUFFER_LEN]; |
44 | volatile uint8_t rxd_buffer_locked = FALSE; |
44 | volatile uint8_t rxd_buffer_locked = FALSE; |
45 | volatile uint8_t rxd_buffer[RXD_BUFFER_LEN]; |
45 | volatile uint8_t rxd_buffer[RXD_BUFFER_LEN]; |
46 | volatile uint8_t txd_complete = TRUE; |
46 | volatile uint8_t txd_complete = TRUE; |
47 | volatile uint8_t ReceivedBytes = 0; |
47 | volatile uint8_t ReceivedBytes = 0; |
48 | volatile uint8_t *pRxData = 0; |
48 | volatile uint8_t *pRxData = 0; |
49 | volatile uint8_t RxDataLen = 0; |
49 | volatile uint8_t RxDataLen = 0; |
50 | 50 | ||
51 | uint8_t PcAccess = 100; |
51 | uint8_t PcAccess = 100; |
52 | 52 | ||
53 | ExternControl_t ExternControl; |
53 | ExternControl_t ExternControl; |
54 | DebugOut_t DebugOut; |
54 | DebugOut_t DebugOut; |
55 | UART_VersionInfo_t UART_VersionInfo; |
55 | UART_VersionInfo_t UART_VersionInfo; |
56 | 56 | ||
57 | uint16_t DebugData_Timer; |
57 | uint16_t DebugData_Timer; |
58 | uint16_t DebugData_Interval = 0; // in 1ms |
58 | uint16_t DebugData_Interval = 0; // in 1ms |
- | 59 | uint16_t Display_Interval = 0; // in 1 ms |
|
59 | 60 | ||
60 | Waypoint_t FollowMe; |
61 | Waypoint_t FollowMe; |
61 | 62 | ||
62 | // keep lables in flash to save 512 bytes of sram space |
63 | // keep lables in flash to save 512 bytes of sram space |
63 | const prog_uint8_t ANALOG_LABEL[32][16] = |
64 | const prog_uint8_t ANALOG_LABEL[32][16] = |
64 | { |
65 | { |
65 | //1234567890123456 |
66 | //1234567890123456 |
66 | "Analog_Ch0 ", //0 |
67 | "Analog_Ch0 ", //0 |
67 | "Analog_Ch1 ", |
68 | "Analog_Ch1 ", |
68 | "Analog_Ch2 ", |
69 | "Analog_Ch2 ", |
69 | "Analog_Ch3 ", |
70 | "Analog_Ch3 ", |
70 | "Analog_Ch4 ", |
71 | "Analog_Ch4 ", |
71 | "Analog_Ch5 ", //5 |
72 | "Analog_Ch5 ", //5 |
72 | "Analog_Ch6 ", |
73 | "Analog_Ch6 ", |
73 | "Analog_Ch7 ", |
74 | "Analog_Ch7 ", |
74 | "UBat ", |
75 | "UBat ", |
75 | "SysState ", |
76 | "SysState ", |
76 | "Debug10 ", //10 |
77 | "Debug10 ", //10 |
77 | "Debug11 ", |
78 | "Debug11 ", |
78 | "Debug12 ", |
79 | "Debug12 ", |
79 | "Debug13 ", |
80 | "Debug13 ", |
80 | "Debug14 ", |
81 | "Debug14 ", |
81 | "Debug15 ", //15 |
82 | "Debug15 ", //15 |
82 | "Zellenzahl ", |
83 | "Zellenzahl ", |
83 | "PowerOn ", |
84 | "PowerOn ", |
84 | "Debug18 ", |
85 | "Debug18 ", |
85 | "Debug19 ", |
86 | "Debug19 ", |
86 | "Debug20 ", //20 |
87 | "Debug20 ", //20 |
87 | "Debug21 ", |
88 | "Debug21 ", |
88 | "Debug22 ", |
89 | "Debug22 ", |
89 | "Debug23 ", |
90 | "Debug23 ", |
90 | "Debug24 ", |
91 | "Debug24 ", |
91 | "Debug25 ", //25 |
92 | "Debug25 ", //25 |
92 | "Debug26 ", |
93 | "Debug26 ", |
93 | "Debug27 ", |
94 | "Debug27 ", |
94 | "Debug28 ", |
95 | "Debug28 ", |
95 | "Debug29 ", |
96 | "Debug29 ", |
96 | "Debug30 ", //30 |
97 | "Debug30 ", //30 |
97 | "Debug31 " |
98 | "Debug31 " |
98 | }; |
99 | }; |
99 | 100 | ||
100 | 101 | ||
101 | 102 | ||
102 | /****************************************************************/ |
103 | /****************************************************************/ |
103 | /* Initialization of the USART0 */ |
104 | /* Initialization of the USART0 */ |
104 | /****************************************************************/ |
105 | /****************************************************************/ |
105 | void USART0_Init (void) |
106 | void USART0_Init (void) |
106 | { |
107 | { |
107 | uint8_t sreg = SREG; |
108 | uint8_t sreg = SREG; |
108 | uint16_t ubrr = (uint16_t) ((uint32_t) SYSCLK/(8 * USART0_BAUD) - 1); |
109 | uint16_t ubrr = (uint16_t) ((uint32_t) SYSCLK/(8 * USART0_BAUD) - 1); |
109 | 110 | ||
110 | // disable all interrupts before configuration |
111 | // disable all interrupts before configuration |
111 | cli(); |
112 | cli(); |
112 | 113 | ||
113 | // disable RX-Interrupt |
114 | // disable RX-Interrupt |
114 | UCSR0B &= ~(1 << RXCIE0); |
115 | UCSR0B &= ~(1 << RXCIE0); |
115 | // disable TX-Interrupt |
116 | // disable TX-Interrupt |
116 | UCSR0B &= ~(1 << TXCIE0); |
117 | UCSR0B &= ~(1 << TXCIE0); |
117 | 118 | ||
118 | // set direction of RXD0 and TXD0 pins |
119 | // set direction of RXD0 and TXD0 pins |
119 | // set RXD0 (PD0) as an input pin |
120 | // set RXD0 (PD0) as an input pin |
120 | PORTD |= (1 << PORTD0); |
121 | PORTD |= (1 << PORTD0); |
121 | DDRD &= ~(1 << DDD0); |
122 | DDRD &= ~(1 << DDD0); |
122 | // set TXD0 (PD1) as an output pin |
123 | // set TXD0 (PD1) as an output pin |
123 | PORTD |= (1 << PORTD1); |
124 | PORTD |= (1 << PORTD1); |
124 | DDRD |= (1 << DDD1); |
125 | DDRD |= (1 << DDD1); |
125 | 126 | ||
126 | // USART0 Baud Rate Register |
127 | // USART0 Baud Rate Register |
127 | // set clock divider |
128 | // set clock divider |
128 | UBRR0H = (uint8_t)(ubrr >> 8); |
129 | UBRR0H = (uint8_t)(ubrr >> 8); |
129 | UBRR0L = (uint8_t)ubrr; |
130 | UBRR0L = (uint8_t)ubrr; |
130 | 131 | ||
131 | // USART0 Control and Status Register A, B, C |
132 | // USART0 Control and Status Register A, B, C |
132 | 133 | ||
133 | // enable double speed operation in |
134 | // enable double speed operation in |
134 | UCSR0A |= (1 << U2X0); |
135 | UCSR0A |= (1 << U2X0); |
135 | // enable receiver and transmitter in |
136 | // enable receiver and transmitter in |
136 | UCSR0B = (1 << TXEN0) | (1 << RXEN0); |
137 | UCSR0B = (1 << TXEN0) | (1 << RXEN0); |
137 | // set asynchronous mode |
138 | // set asynchronous mode |
138 | UCSR0C &= ~(1 << UMSEL01); |
139 | UCSR0C &= ~(1 << UMSEL01); |
139 | UCSR0C &= ~(1 << UMSEL00); |
140 | UCSR0C &= ~(1 << UMSEL00); |
140 | // no parity |
141 | // no parity |
141 | UCSR0C &= ~(1 << UPM01); |
142 | UCSR0C &= ~(1 << UPM01); |
142 | UCSR0C &= ~(1 << UPM00); |
143 | UCSR0C &= ~(1 << UPM00); |
143 | // 1 stop bit |
144 | // 1 stop bit |
144 | UCSR0C &= ~(1 << USBS0); |
145 | UCSR0C &= ~(1 << USBS0); |
145 | // 8-bit |
146 | // 8-bit |
146 | UCSR0B &= ~(1 << UCSZ02); |
147 | UCSR0B &= ~(1 << UCSZ02); |
147 | UCSR0C |= (1 << UCSZ01); |
148 | UCSR0C |= (1 << UCSZ01); |
148 | UCSR0C |= (1 << UCSZ00); |
149 | UCSR0C |= (1 << UCSZ00); |
149 | 150 | ||
150 | // flush receive buffer |
151 | // flush receive buffer |
151 | while ( UCSR0A & (1<<RXC0) ) UDR0; |
152 | while ( UCSR0A & (1<<RXC0) ) UDR0; |
152 | 153 | ||
153 | // enable interrupts at the end |
154 | // enable interrupts at the end |
154 | // enable RX-Interrupt |
155 | // enable RX-Interrupt |
155 | UCSR0B |= (1 << RXCIE0); |
156 | UCSR0B |= (1 << RXCIE0); |
156 | // enable TX-Interrupt |
157 | // enable TX-Interrupt |
157 | UCSR0B |= (1 << TXCIE0); |
158 | UCSR0B |= (1 << TXCIE0); |
158 | 159 | ||
159 | // initialize the debug timer |
160 | // initialize the debug timer |
160 | DebugData_Timer = SetDelay(DebugData_Interval); |
161 | DebugData_Timer = SetDelay(DebugData_Interval); |
161 | 162 | ||
162 | // unlock rxd_buffer |
163 | // unlock rxd_buffer |
163 | rxd_buffer_locked = FALSE; |
164 | rxd_buffer_locked = FALSE; |
164 | pRxData = 0; |
165 | pRxData = 0; |
165 | RxDataLen = 0; |
166 | RxDataLen = 0; |
166 | 167 | ||
167 | // no bytes to send |
168 | // no bytes to send |
168 | txd_complete = TRUE; |
169 | txd_complete = TRUE; |
169 | 170 | ||
170 | UART_VersionInfo.SWMajor = VERSION_MAJOR; |
171 | UART_VersionInfo.SWMajor = VERSION_MAJOR; |
171 | UART_VersionInfo.SWMinor = VERSION_MINOR; |
172 | UART_VersionInfo.SWMinor = VERSION_MINOR; |
172 | UART_VersionInfo.SWPatch = VERSION_PATCH; |
173 | UART_VersionInfo.SWPatch = VERSION_PATCH; |
173 | UART_VersionInfo.ProtoMajor = VERSION_SERIAL_MAJOR; |
174 | UART_VersionInfo.ProtoMajor = VERSION_SERIAL_MAJOR; |
174 | UART_VersionInfo.ProtoMinor = VERSION_SERIAL_MINOR; |
175 | UART_VersionInfo.ProtoMinor = VERSION_SERIAL_MINOR; |
175 | 176 | ||
176 | // restore global interrupt flags |
177 | // restore global interrupt flags |
177 | SREG = sreg; |
178 | SREG = sreg; |
178 | sei(); |
179 | sei(); |
179 | printf("\r\n UART0 init...ok"); |
180 | printf("\r\n UART0 init...ok"); |
180 | } |
181 | } |
181 | 182 | ||
182 | /****************************************************************/ |
183 | /****************************************************************/ |
183 | /* USART0 transmitter ISR */ |
184 | /* USART0 transmitter ISR */ |
184 | /****************************************************************/ |
185 | /****************************************************************/ |
185 | ISR(USART0_TX_vect) |
186 | ISR(USART0_TX_vect) |
186 | { |
187 | { |
187 | static uint16_t ptr_txd_buffer = 0; |
188 | static uint16_t ptr_txd_buffer = 0; |
188 | uint8_t tmp_tx; |
189 | uint8_t tmp_tx; |
189 | if(!txd_complete) // transmission not completed |
190 | if(!txd_complete) // transmission not completed |
190 | { |
191 | { |
191 | ptr_txd_buffer++; // die [0] wurde schon gesendet |
192 | ptr_txd_buffer++; // die [0] wurde schon gesendet |
192 | tmp_tx = txd_buffer[ptr_txd_buffer]; |
193 | tmp_tx = txd_buffer[ptr_txd_buffer]; |
193 | // if terminating character or end of txd buffer was reached |
194 | // if terminating character or end of txd buffer was reached |
194 | if((tmp_tx == '\r') || (ptr_txd_buffer == TXD_BUFFER_LEN)) |
195 | if((tmp_tx == '\r') || (ptr_txd_buffer == TXD_BUFFER_LEN)) |
195 | { |
196 | { |
196 | ptr_txd_buffer = 0; // reset txd pointer |
197 | ptr_txd_buffer = 0; // reset txd pointer |
197 | txd_complete = 1; // stop transmission |
198 | txd_complete = 1; // stop transmission |
198 | } |
199 | } |
199 | UDR0 = tmp_tx; // send current byte will trigger this ISR again |
200 | UDR0 = tmp_tx; // send current byte will trigger this ISR again |
200 | } |
201 | } |
201 | // transmission completed |
202 | // transmission completed |
202 | else ptr_txd_buffer = 0; |
203 | else ptr_txd_buffer = 0; |
203 | } |
204 | } |
204 | 205 | ||
205 | /****************************************************************/ |
206 | /****************************************************************/ |
206 | /* USART0 receiver ISR */ |
207 | /* USART0 receiver ISR */ |
207 | /****************************************************************/ |
208 | /****************************************************************/ |
208 | ISR(USART0_RX_vect) |
209 | ISR(USART0_RX_vect) |
209 | { |
210 | { |
210 | static uint16_t crc; |
211 | static uint16_t crc; |
211 | static uint8_t ptr_rxd_buffer = 0; |
212 | static uint8_t ptr_rxd_buffer = 0; |
212 | uint8_t crc1, crc2; |
213 | uint8_t crc1, crc2; |
213 | uint8_t c; |
214 | uint8_t c; |
214 | 215 | ||
215 | c = UDR0; // catch the received byte |
216 | c = UDR0; // catch the received byte |
216 | 217 | ||
217 | if(rxd_buffer_locked) return; // if rxd buffer is locked immediately return |
218 | if(rxd_buffer_locked) return; // if rxd buffer is locked immediately return |
218 | 219 | ||
219 | // the rxd buffer is unlocked |
220 | // the rxd buffer is unlocked |
220 | if((ptr_rxd_buffer == 0) && (c == '#')) // if rxd buffer is empty and syncronisation character is received |
221 | if((ptr_rxd_buffer == 0) && (c == '#')) // if rxd buffer is empty and syncronisation character is received |
221 | { |
222 | { |
222 | rxd_buffer[ptr_rxd_buffer++] = c; // copy 1st byte to buffer |
223 | rxd_buffer[ptr_rxd_buffer++] = c; // copy 1st byte to buffer |
223 | crc = c; // init crc |
224 | crc = c; // init crc |
224 | } |
225 | } |
225 | #if 0 |
226 | #if 0 |
226 | else if (ptr_rxd_buffer == 1) // handle address |
227 | else if (ptr_rxd_buffer == 1) // handle address |
227 | { |
228 | { |
228 | rxd_buffer[ptr_rxd_buffer++] = c; // copy byte to rxd buffer |
229 | rxd_buffer[ptr_rxd_buffer++] = c; // copy byte to rxd buffer |
229 | crc += c; // update crc |
230 | crc += c; // update crc |
230 | } |
231 | } |
231 | #endif |
232 | #endif |
232 | else if (ptr_rxd_buffer < RXD_BUFFER_LEN) // collect incomming bytes |
233 | else if (ptr_rxd_buffer < RXD_BUFFER_LEN) // collect incomming bytes |
233 | { |
234 | { |
234 | if(c != '\r') // no termination character |
235 | if(c != '\r') // no termination character |
235 | { |
236 | { |
236 | rxd_buffer[ptr_rxd_buffer++] = c; // copy byte to rxd buffer |
237 | rxd_buffer[ptr_rxd_buffer++] = c; // copy byte to rxd buffer |
237 | crc += c; // update crc |
238 | crc += c; // update crc |
238 | } |
239 | } |
239 | else // termination character was received |
240 | else // termination character was received |
240 | { |
241 | { |
241 | // the last 2 bytes are no subject for checksum calculation |
242 | // the last 2 bytes are no subject for checksum calculation |
242 | // they are the checksum itself |
243 | // they are the checksum itself |
243 | crc -= rxd_buffer[ptr_rxd_buffer-2]; |
244 | crc -= rxd_buffer[ptr_rxd_buffer-2]; |
244 | crc -= rxd_buffer[ptr_rxd_buffer-1]; |
245 | crc -= rxd_buffer[ptr_rxd_buffer-1]; |
245 | // calculate checksum from transmitted data |
246 | // calculate checksum from transmitted data |
246 | crc %= 4096; |
247 | crc %= 4096; |
247 | crc1 = '=' + crc / 64; |
248 | crc1 = '=' + crc / 64; |
248 | crc2 = '=' + crc % 64; |
249 | crc2 = '=' + crc % 64; |
249 | // compare checksum to transmitted checksum bytes |
250 | // compare checksum to transmitted checksum bytes |
250 | if((crc1 == rxd_buffer[ptr_rxd_buffer-2]) && (crc2 == rxd_buffer[ptr_rxd_buffer-1])) |
251 | if((crc1 == rxd_buffer[ptr_rxd_buffer-2]) && (crc2 == rxd_buffer[ptr_rxd_buffer-1])) |
251 | { // checksum valid |
252 | { // checksum valid |
252 | rxd_buffer[ptr_rxd_buffer] = '\r'; // set termination character |
253 | rxd_buffer[ptr_rxd_buffer] = '\r'; // set termination character |
253 | ReceivedBytes = ptr_rxd_buffer + 1;// store number of received bytes |
254 | ReceivedBytes = ptr_rxd_buffer + 1;// store number of received bytes |
254 | rxd_buffer_locked = TRUE; // lock the rxd buffer |
255 | rxd_buffer_locked = TRUE; // lock the rxd buffer |
255 | // if 2nd byte is an 'R' enable watchdog that will result in an reset |
256 | // if 2nd byte is an 'R' enable watchdog that will result in an reset |
256 | if(rxd_buffer[2] == 'R') {wdt_enable(WDTO_250MS);} // Reset-Commando |
257 | if(rxd_buffer[2] == 'R') {wdt_enable(WDTO_250MS);} // Reset-Commando |
257 | } |
258 | } |
258 | else |
259 | else |
259 | { // checksum invalid |
260 | { // checksum invalid |
260 | rxd_buffer_locked = FALSE; // unlock rxd buffer |
261 | rxd_buffer_locked = FALSE; // unlock rxd buffer |
261 | } |
262 | } |
262 | ptr_rxd_buffer = 0; // reset rxd buffer pointer |
263 | ptr_rxd_buffer = 0; // reset rxd buffer pointer |
263 | } |
264 | } |
264 | } |
265 | } |
265 | else // rxd buffer overrun |
266 | else // rxd buffer overrun |
266 | { |
267 | { |
267 | ptr_rxd_buffer = 0; // reset rxd buffer |
268 | ptr_rxd_buffer = 0; // reset rxd buffer |
268 | rxd_buffer_locked = FALSE; // unlock rxd buffer |
269 | rxd_buffer_locked = FALSE; // unlock rxd buffer |
269 | } |
270 | } |
270 | 271 | ||
271 | } |
272 | } |
272 | 273 | ||
273 | 274 | ||
274 | // -------------------------------------------------------------------------- |
275 | // -------------------------------------------------------------------------- |
275 | void AddCRC(uint16_t datalen) |
276 | void AddCRC(uint16_t datalen) |
276 | { |
277 | { |
277 | uint16_t tmpCRC = 0, i; |
278 | uint16_t tmpCRC = 0, i; |
278 | for(i = 0; i < datalen; i++) |
279 | for(i = 0; i < datalen; i++) |
279 | { |
280 | { |
280 | tmpCRC += txd_buffer[i]; |
281 | tmpCRC += txd_buffer[i]; |
281 | } |
282 | } |
282 | tmpCRC %= 4096; |
283 | tmpCRC %= 4096; |
283 | txd_buffer[i++] = '=' + tmpCRC / 64; |
284 | txd_buffer[i++] = '=' + tmpCRC / 64; |
284 | txd_buffer[i++] = '=' + tmpCRC % 64; |
285 | txd_buffer[i++] = '=' + tmpCRC % 64; |
285 | txd_buffer[i++] = '\r'; |
286 | txd_buffer[i++] = '\r'; |
286 | txd_complete = FALSE; |
287 | txd_complete = FALSE; |
287 | UDR0 = txd_buffer[0]; // initiates the transmittion (continued in the TXD ISR) |
288 | UDR0 = txd_buffer[0]; // initiates the transmittion (continued in the TXD ISR) |
288 | } |
289 | } |
289 | 290 | ||
290 | 291 | ||
291 | 292 | ||
292 | // -------------------------------------------------------------------------- |
293 | // -------------------------------------------------------------------------- |
293 | void SendOutData(uint8_t cmd, uint8_t addr, uint8_t numofbuffers, ...) // uint8_t *pdata, uint8_t len, ... |
294 | void SendOutData(uint8_t cmd, uint8_t addr, uint8_t numofbuffers, ...) // uint8_t *pdata, uint8_t len, ... |
294 | { |
295 | { |
295 | va_list ap; |
296 | va_list ap; |
296 | uint16_t pt = 0; |
297 | uint16_t pt = 0; |
297 | uint8_t a,b,c; |
298 | uint8_t a,b,c; |
298 | uint8_t ptr = 0; |
299 | uint8_t ptr = 0; |
299 | 300 | ||
300 | uint8_t *pdata = 0; |
301 | uint8_t *pdata = 0; |
301 | int len = 0; |
302 | int len = 0; |
302 | 303 | ||
303 | txd_buffer[pt++] = '#'; // Start character |
304 | txd_buffer[pt++] = '#'; // Start character |
304 | txd_buffer[pt++] = 'a' + addr; // Address (a=0; b=1,...) |
305 | txd_buffer[pt++] = 'a' + addr; // Address (a=0; b=1,...) |
305 | txd_buffer[pt++] = cmd; // Command |
306 | txd_buffer[pt++] = cmd; // Command |
306 | 307 | ||
307 | va_start(ap, numofbuffers); |
308 | va_start(ap, numofbuffers); |
308 | if(numofbuffers) |
309 | if(numofbuffers) |
309 | { |
310 | { |
310 | pdata = va_arg(ap, uint8_t*); |
311 | pdata = va_arg(ap, uint8_t*); |
311 | len = va_arg(ap, int); |
312 | len = va_arg(ap, int); |
312 | ptr = 0; |
313 | ptr = 0; |
313 | numofbuffers--; |
314 | numofbuffers--; |
314 | } |
315 | } |
315 | 316 | ||
316 | while(len) |
317 | while(len) |
317 | { |
318 | { |
318 | if(len) |
319 | if(len) |
319 | { |
320 | { |
320 | a = pdata[ptr++]; |
321 | a = pdata[ptr++]; |
321 | len--; |
322 | len--; |
322 | if((!len) && numofbuffers) |
323 | if((!len) && numofbuffers) |
323 | { |
324 | { |
324 | pdata = va_arg(ap, uint8_t*); |
325 | pdata = va_arg(ap, uint8_t*); |
325 | len = va_arg(ap, int); |
326 | len = va_arg(ap, int); |
326 | ptr = 0; |
327 | ptr = 0; |
327 | numofbuffers--; |
328 | numofbuffers--; |
328 | } |
329 | } |
329 | } |
330 | } |
330 | else a = 0; |
331 | else a = 0; |
331 | if(len) |
332 | if(len) |
332 | { |
333 | { |
333 | b = pdata[ptr++]; |
334 | b = pdata[ptr++]; |
334 | len--; |
335 | len--; |
335 | if((!len) && numofbuffers) |
336 | if((!len) && numofbuffers) |
336 | { |
337 | { |
337 | pdata = va_arg(ap, uint8_t*); |
338 | pdata = va_arg(ap, uint8_t*); |
338 | len = va_arg(ap, int); |
339 | len = va_arg(ap, int); |
339 | ptr = 0; |
340 | ptr = 0; |
340 | numofbuffers--; |
341 | numofbuffers--; |
341 | } |
342 | } |
342 | } |
343 | } |
343 | else b = 0; |
344 | else b = 0; |
344 | if(len) |
345 | if(len) |
345 | { |
346 | { |
346 | c = pdata[ptr++]; |
347 | c = pdata[ptr++]; |
347 | len--; |
348 | len--; |
348 | if((!len) && numofbuffers) |
349 | if((!len) && numofbuffers) |
349 | { |
350 | { |
350 | pdata = va_arg(ap, uint8_t*); |
351 | pdata = va_arg(ap, uint8_t*); |
351 | len = va_arg(ap, int); |
352 | len = va_arg(ap, int); |
352 | ptr = 0; |
353 | ptr = 0; |
353 | numofbuffers--; |
354 | numofbuffers--; |
354 | } |
355 | } |
355 | } |
356 | } |
356 | else c = 0; |
357 | else c = 0; |
357 | txd_buffer[pt++] = '=' + (a >> 2); |
358 | txd_buffer[pt++] = '=' + (a >> 2); |
358 | txd_buffer[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4)); |
359 | txd_buffer[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4)); |
359 | txd_buffer[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6)); |
360 | txd_buffer[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6)); |
360 | txd_buffer[pt++] = '=' + ( c & 0x3f); |
361 | txd_buffer[pt++] = '=' + ( c & 0x3f); |
361 | } |
362 | } |
362 | va_end(ap); |
363 | va_end(ap); |
363 | AddCRC(pt); // add checksum after data block and initates the transmission |
364 | AddCRC(pt); // add checksum after data block and initates the transmission |
364 | } |
365 | } |
365 | 366 | ||
366 | 367 | ||
367 | // -------------------------------------------------------------------------- |
368 | // -------------------------------------------------------------------------- |
368 | void Decode64(void) |
369 | void Decode64(void) |
369 | { |
370 | { |
370 | uint8_t a,b,c,d; |
371 | uint8_t a,b,c,d; |
371 | uint8_t x,y,z; |
372 | uint8_t x,y,z; |
372 | uint8_t ptrIn = 3; |
373 | uint8_t ptrIn = 3; |
373 | uint8_t ptrOut = 3; |
374 | uint8_t ptrOut = 3; |
374 | uint8_t len = ReceivedBytes - 6; |
375 | uint8_t len = ReceivedBytes - 6; |
375 | 376 | ||
376 | while(len) |
377 | while(len) |
377 | { |
378 | { |
378 | a = rxd_buffer[ptrIn++] - '='; |
379 | a = rxd_buffer[ptrIn++] - '='; |
379 | b = rxd_buffer[ptrIn++] - '='; |
380 | b = rxd_buffer[ptrIn++] - '='; |
380 | c = rxd_buffer[ptrIn++] - '='; |
381 | c = rxd_buffer[ptrIn++] - '='; |
381 | d = rxd_buffer[ptrIn++] - '='; |
382 | d = rxd_buffer[ptrIn++] - '='; |
382 | //if(ptrIn > ReceivedBytes - 3) break; |
383 | //if(ptrIn > ReceivedBytes - 3) break; |
383 | 384 | ||
384 | x = (a << 2) | (b >> 4); |
385 | x = (a << 2) | (b >> 4); |
385 | y = ((b & 0x0f) << 4) | (c >> 2); |
386 | y = ((b & 0x0f) << 4) | (c >> 2); |
386 | z = ((c & 0x03) << 6) | d; |
387 | z = ((c & 0x03) << 6) | d; |
387 | 388 | ||
388 | if(len--) rxd_buffer[ptrOut++] = x; else break; |
389 | if(len--) rxd_buffer[ptrOut++] = x; else break; |
389 | if(len--) rxd_buffer[ptrOut++] = y; else break; |
390 | if(len--) rxd_buffer[ptrOut++] = y; else break; |
390 | if(len--) rxd_buffer[ptrOut++] = z; else break; |
391 | if(len--) rxd_buffer[ptrOut++] = z; else break; |
391 | } |
392 | } |
392 | pRxData = &rxd_buffer[3]; |
393 | pRxData = &rxd_buffer[3]; |
393 | RxDataLen = ptrOut - 3; |
394 | RxDataLen = ptrOut - 3; |
394 | } |
395 | } |
395 | 396 | ||
396 | 397 | ||
397 | // -------------------------------------------------------------------------- |
398 | // -------------------------------------------------------------------------- |
398 | void USART0_ProcessRxData(void) |
399 | void USART0_ProcessRxData(void) |
399 | { |
400 | { |
400 | // if data in the rxd buffer are not locked immediately return |
401 | // if data in the rxd buffer are not locked immediately return |
401 | if(!rxd_buffer_locked) return; |
402 | if(!rxd_buffer_locked) return; |
402 | 403 | ||
403 | Decode64(); // decode data block in rxd_buffer |
404 | Decode64(); // decode data block in rxd_buffer |
404 | 405 | ||
405 | 406 | ||
406 | switch(rxd_buffer[1] - 'a') |
407 | switch(rxd_buffer[1] - 'a') |
407 | { |
408 | { |
408 | case FM_ADDRESS: |
409 | case FM_ADDRESS: |
409 | 410 | ||
410 | switch(rxd_buffer[2]) |
411 | switch(rxd_buffer[2]) |
411 | { |
412 | { |
412 | default: |
413 | default: |
413 | //unsupported command received |
414 | //unsupported command received |
414 | break; |
415 | break; |
415 | } // case FC_ADDRESS: |
416 | } // case FC_ADDRESS: |
416 | 417 | ||
417 | default: // any Slave Address |
418 | default: // any Slave Address |
418 | 419 | ||
419 | switch(rxd_buffer[2]) |
420 | switch(rxd_buffer[2]) |
420 | { |
421 | { |
421 | case 'a':// request for labels of the analog debug outputs |
422 | case 'a':// request for labels of the analog debug outputs |
422 | Request_DebugLabel = pRxData[0]; |
423 | Request_DebugLabel = pRxData[0]; |
423 | if(Request_DebugLabel > 31) Request_DebugLabel = 31; |
424 | if(Request_DebugLabel > 31) Request_DebugLabel = 31; |
424 | PcAccess = 255; |
425 | PcAccess = 255; |
425 | break; |
426 | break; |
426 | 427 | ||
427 | case 'h':// request for display columns |
428 | case 'h':// request for display columns |
428 | PcAccess = 255; |
429 | PcAccess = 255; |
- | 430 | if((pRxData[0] & 0x80) == 0x00) // old format |
|
- | 431 | { |
|
- | 432 | DisplayLine = 2; |
|
- | 433 | Display_Interval = 0; |
|
- | 434 | } |
|
- | 435 | else // new format |
|
- | 436 | { |
|
429 | RemoteKeys |= pRxData[0]; |
437 | RemoteKeys |= ~pRxData[0]; |
- | 438 | Display_Interval = (unsigned int)pRxData[1] * 10; |
|
430 | if(RemoteKeys) DisplayLine = 0; |
439 | DisplayLine = 4; |
431 | Request_Display = TRUE; |
440 | Request_Display = TRUE; |
- | 441 | } |
|
432 | break; |
442 | break; |
433 | 443 | ||
434 | case 'l':// request for display columns |
444 | case 'l':// request for display columns |
435 | PcAccess = 255; |
445 | PcAccess = 255; |
436 | MenuItem = pRxData[0]; |
446 | MenuItem = pRxData[0]; |
437 | Request_Display1 = TRUE; |
447 | Request_Display1 = TRUE; |
438 | break; |
448 | break; |
439 | 449 | ||
440 | case 'v': // request for version and board release |
450 | case 'v': // request for version and board release |
441 | Request_VerInfo = TRUE; |
451 | Request_VerInfo = TRUE; |
442 | break; |
452 | break; |
443 | 453 | ||
444 | case 'd': // request for the debug data |
454 | case 'd': // request for the debug data |
445 | DebugData_Interval = (uint16_t) pRxData[0] * 10; |
455 | DebugData_Interval = (uint16_t) pRxData[0] * 10; |
446 | if(DebugData_Interval > 0) Request_DebugData = TRUE; |
456 | if(DebugData_Interval > 0) Request_DebugData = TRUE; |
447 | break; |
457 | break; |
448 | 458 | ||
449 | case 'g':// get external control data |
459 | case 'g':// get external control data |
450 | Request_ExternalControl = TRUE; |
460 | Request_ExternalControl = TRUE; |
451 | break; |
461 | break; |
452 | 462 | ||
453 | default: |
463 | default: |
454 | //unsupported command received |
464 | //unsupported command received |
455 | break; |
465 | break; |
456 | } |
466 | } |
457 | break; // default: |
467 | break; // default: |
458 | } |
468 | } |
459 | // unlock the rxd buffer after processing |
469 | // unlock the rxd buffer after processing |
460 | pRxData = 0; |
470 | pRxData = 0; |
461 | RxDataLen = 0; |
471 | RxDataLen = 0; |
462 | rxd_buffer_locked = FALSE; |
472 | rxd_buffer_locked = FALSE; |
463 | } |
473 | } |
464 | 474 | ||
465 | //############################################################################ |
475 | //############################################################################ |
466 | //Routine für die Serielle Ausgabe |
476 | //Routine für die Serielle Ausgabe |
467 | int16_t uart_putchar (int8_t c) |
477 | int16_t uart_putchar (int8_t c) |
468 | //############################################################################ |
478 | //############################################################################ |
469 | { |
479 | { |
470 | if (c == '\n') |
480 | if (c == '\n') |
471 | uart_putchar('\r'); |
481 | uart_putchar('\r'); |
472 | // wait until previous character was send |
482 | // wait until previous character was send |
473 | loop_until_bit_is_set(UCSR0A, UDRE0); |
483 | loop_until_bit_is_set(UCSR0A, UDRE0); |
474 | // send character |
484 | // send character |
475 | UDR0 = c; |
485 | UDR0 = c; |
476 | return (0); |
486 | return (0); |
477 | } |
487 | } |
478 | 488 | ||
479 | 489 | ||
480 | //--------------------------------------------------------------------------------------------- |
490 | //--------------------------------------------------------------------------------------------- |
481 | void USART0_TransmitTxData(void) |
491 | void USART0_TransmitTxData(void) |
482 | { |
492 | { |
483 | if(!txd_complete) return; |
493 | if(!txd_complete) return; |
484 | 494 | ||
485 | if(Request_VerInfo && txd_complete) |
495 | if(Request_VerInfo && txd_complete) |
486 | { |
496 | { |
487 | SendOutData('V', FM_ADDRESS, 1, (uint8_t *) &UART_VersionInfo, sizeof(UART_VersionInfo)); |
497 | SendOutData('V', FM_ADDRESS, 1, (uint8_t *) &UART_VersionInfo, sizeof(UART_VersionInfo)); |
488 | Request_VerInfo = FALSE; |
498 | Request_VerInfo = FALSE; |
489 | } |
499 | } |
490 | if(Request_Display && txd_complete) |
500 | if( ((DebugData_Interval && CheckDelay(DebugData_Timer)) || Request_Display) && txd_complete) |
491 | { |
501 | { |
- | 502 | if(DisplayLine > 3)// new format |
|
- | 503 | { |
|
492 | LCD_PrintMenu(); |
504 | LCD_PrintMenu(); |
493 | SendOutData('H', FM_ADDRESS, 2, &DisplayLine, sizeof(DisplayLine), &DisplayBuff[DisplayLine * 20], 20); |
505 | SendOutData('H', FM_ADDRESS, 1, (uint8_t *)DisplayBuff, 80); |
- | 506 | } |
|
494 | DisplayLine++; |
507 | else // old format |
- | 508 | { |
|
- | 509 | LCD_printfxy(0,0,"!!! INCOMPATIBLE !!!"); |
|
- | 510 | SendOutData('H', FC_ADDRESS, 2, &DisplayLine, sizeof(DisplayLine), (uint8_t *)DisplayBuff, 20); |
|
495 | if(DisplayLine >= 4) DisplayLine = 0; |
511 | if(DisplayLine++ > 3) DisplayLine = 0; |
- | 512 | } |
|
496 | Request_Display = FALSE; |
513 | Request_Display = FALSE; |
497 | } |
514 | } |
498 | if(Request_Display1 && txd_complete) |
515 | if(Request_Display1 && txd_complete) |
499 | { |
516 | { |
500 | LCD_PrintMenu(); |
517 | LCD_PrintMenu(); |
501 | SendOutData('L', FM_ADDRESS, 3, &MenuItem, sizeof(MenuItem), &MaxMenuItem, sizeof(MaxMenuItem), DisplayBuff, sizeof(DisplayBuff)); |
518 | SendOutData('L', FM_ADDRESS, 3, &MenuItem, sizeof(MenuItem), &MaxMenuItem, sizeof(MaxMenuItem), DisplayBuff, sizeof(DisplayBuff)); |
502 | Request_Display1 = FALSE; |
519 | Request_Display1 = FALSE; |
503 | } |
520 | } |
504 | if(Request_DebugLabel != 0xFF) // Texte für die Analogdaten |
521 | if(Request_DebugLabel != 0xFF) // Texte für die Analogdaten |
505 | { |
522 | { |
506 | uint8_t label[16]; // local sram buffer |
523 | uint8_t label[16]; // local sram buffer |
507 | memcpy_P(label, ANALOG_LABEL[Request_DebugLabel], 16); // read lable from flash to sram buffer |
524 | memcpy_P(label, ANALOG_LABEL[Request_DebugLabel], 16); // read lable from flash to sram buffer |
508 | SendOutData('A', FM_ADDRESS, 2, (uint8_t *) &Request_DebugLabel, sizeof(Request_DebugLabel), label, 16); |
525 | SendOutData('A', FM_ADDRESS, 2, (uint8_t *) &Request_DebugLabel, sizeof(Request_DebugLabel), label, 16); |
509 | Request_DebugLabel = 0xFF; |
526 | Request_DebugLabel = 0xFF; |
510 | } |
527 | } |
511 | if(Request_ExternalControl && txd_complete) |
528 | if(Request_ExternalControl && txd_complete) |
512 | { |
529 | { |
513 | SendOutData('G', FM_ADDRESS, 1,(uint8_t *) &ExternControl, sizeof(ExternControl)); |
530 | SendOutData('G', FM_ADDRESS, 1,(uint8_t *) &ExternControl, sizeof(ExternControl)); |
514 | Request_ExternalControl = FALSE; |
531 | Request_ExternalControl = FALSE; |
515 | } |
532 | } |
516 | if( ((DebugData_Interval && CheckDelay(DebugData_Timer)) || Request_DebugData) && txd_complete) |
533 | if( ((DebugData_Interval && CheckDelay(DebugData_Timer)) || Request_DebugData) && txd_complete) |
517 | { |
534 | { |
518 | SendOutData('D', FM_ADDRESS, 1,(uint8_t *) &DebugOut, sizeof(DebugOut)); |
535 | SendOutData('D', FM_ADDRESS, 1,(uint8_t *) &DebugOut, sizeof(DebugOut)); |
519 | DebugData_Timer = SetDelay(DebugData_Interval); |
536 | DebugData_Timer = SetDelay(DebugData_Interval); |
520 | Request_DebugData = FALSE; |
537 | Request_DebugData = FALSE; |
521 | } |
538 | } |
522 | if(Request_SendFollowMe && txd_complete) |
539 | if(Request_SendFollowMe && txd_complete) |
523 | { |
540 | { |
524 | SendOutData('s', NC_ADDRESS, 1, (uint8_t *)&FollowMe, sizeof(FollowMe)); |
541 | SendOutData('s', NC_ADDRESS, 1, (uint8_t *)&FollowMe, sizeof(FollowMe)); |
525 | FollowMe.Position.Status = PROCESSED; |
542 | FollowMe.Position.Status = PROCESSED; |
526 | Request_SendFollowMe = FALSE; |
543 | Request_SendFollowMe = FALSE; |
527 | } |
544 | } |
528 | } |
545 | } |
529 | 546 | ||
530 | 547 |