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