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