Rev 1662 | Rev 1668 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1662 | Rev 1665 | ||
---|---|---|---|
1 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2 | // + Copyright (c) Holger Buss, Ingo Busker |
2 | // + Copyright (c) Holger Buss, Ingo Busker |
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 | #include <stdarg.h> |
7 | #include <stdarg.h> |
8 | #include <string.h> |
8 | #include <string.h> |
9 | #include <avr/pgmspace.h> |
9 | #include <avr/pgmspace.h> |
10 | #include "main.h" |
10 | #include "main.h" |
11 | #include "uart.h" |
11 | #include "uart.h" |
12 | #include "libfc.h" |
12 | #include "libfc.h" |
13 | #include "eeprom.h" |
13 | #include "eeprom.h" |
14 | 14 | ||
15 | 15 | ||
16 | 16 | ||
17 | #define FC_ADDRESS 1 |
17 | #define FC_ADDRESS 1 |
18 | #define NC_ADDRESS 2 |
18 | #define NC_ADDRESS 2 |
19 | #define MK3MAG_ADDRESS 3 |
19 | #define MK3MAG_ADDRESS 3 |
20 | #define BL_CTRL_ADDRESS 5 |
20 | #define BL_CTRL_ADDRESS 5 |
21 | 21 | ||
22 | #define ABO_TIMEOUT 4000 // disable abo after 4 seconds |
22 | #define ABO_TIMEOUT 4000 // disable abo after 4 seconds |
23 | #define MAX_SENDE_BUFF 160 |
23 | #define MAX_SENDE_BUFF 160 |
24 | #define MAX_EMPFANGS_BUFF 160 |
24 | #define MAX_EMPFANGS_BUFF 160 |
25 | 25 | ||
26 | 26 | ||
27 | #define BLPARAM_REVISION 1 |
27 | #define BLPARAM_REVISION 1 |
28 | #define MASK_SET_PWM_SCALING 0x01 |
28 | #define MASK_SET_PWM_SCALING 0x01 |
29 | #define MASK_SET_CURRENT_LIMIT 0x02 |
29 | #define MASK_SET_CURRENT_LIMIT 0x02 |
30 | #define MASK_SET_TEMP_LIMIT 0x04 |
30 | #define MASK_SET_TEMP_LIMIT 0x04 |
31 | #define MASK_SET_CURRENT_SCALING 0x08 |
31 | #define MASK_SET_CURRENT_SCALING 0x08 |
32 | #define MASK_SET_BITCONFIG 0x10 |
32 | #define MASK_SET_BITCONFIG 0x10 |
33 | #define MASK_RESET_CAPCOUNTER 0x20 |
33 | #define MASK_RESET_CAPCOUNTER 0x20 |
34 | #define MASK_SET_DEFAULT_PARAMS 0x40 |
34 | #define MASK_SET_DEFAULT_PARAMS 0x40 |
35 | #define MASK_SET_SAVE_EEPROM 0x80 |
35 | #define MASK_SET_SAVE_EEPROM 0x80 |
36 | 36 | ||
37 | typedef struct |
37 | typedef struct |
38 | { |
38 | { |
39 | unsigned char Revision; // revision of parameter structure |
39 | unsigned char Revision; // revision of parameter structure |
40 | unsigned char Address; // target address |
40 | unsigned char Address; // target address |
41 | unsigned char PwmScaling; // maximum value of pwm setpoint |
41 | unsigned char PwmScaling; // maximum value of pwm setpoint |
42 | unsigned char CurrentLimit; // current limit in 1A steps |
42 | unsigned char CurrentLimit; // current limit in 1A steps |
43 | unsigned char TemperatureLimit; // in °C |
43 | unsigned char TemperatureLimit; // in °C |
44 | unsigned char CurrentScaling; // scaling factor for current measurement |
44 | unsigned char CurrentScaling; // scaling factor for current measurement |
45 | unsigned char BitConfig; // see defines above |
45 | unsigned char BitConfig; // see defines above |
46 | unsigned char SetMask; // filter for active paramters |
46 | unsigned char SetMask; // filter for active paramters |
47 | unsigned char Checksum; // checksum for parameter sturcture |
47 | unsigned char Checksum; // checksum for parameter sturcture |
48 | } __attribute__((packed)) BLParameter_t; |
48 | } __attribute__((packed)) BLParameter_t; |
49 | 49 | ||
50 | 50 | ||
51 | unsigned char GetExternalControl = 0,DebugDisplayAnforderung1 = 0, DebugDisplayAnforderung = 0,DebugDataAnforderung = 0,GetVersionAnforderung = 0, GetPPMChannelAnforderung = 0; |
51 | unsigned char GetExternalControl = 0,DebugDisplayAnforderung1 = 0, DebugDisplayAnforderung = 0,DebugDataAnforderung = 0,GetVersionAnforderung = 0, GetPPMChannelAnforderung = 0; |
52 | unsigned char DisplayLine = 0; |
52 | unsigned char DisplayLine = 0; |
53 | unsigned volatile char SioTmp = 0; |
53 | unsigned volatile char SioTmp = 0; |
54 | unsigned volatile char NeuerDatensatzEmpfangen = 0; |
54 | unsigned volatile char NeuerDatensatzEmpfangen = 0; |
55 | unsigned volatile char NeueKoordinateEmpfangen = 0; |
55 | unsigned volatile char NeueKoordinateEmpfangen = 0; |
56 | unsigned volatile char UebertragungAbgeschlossen = 1; |
56 | unsigned volatile char UebertragungAbgeschlossen = 1; |
57 | unsigned volatile char CntCrcError = 0; |
57 | unsigned volatile char CntCrcError = 0; |
58 | unsigned volatile char AnzahlEmpfangsBytes = 0; |
58 | unsigned volatile char AnzahlEmpfangsBytes = 0; |
59 | unsigned volatile char TxdBuffer[MAX_SENDE_BUFF]; |
59 | unsigned volatile char TxdBuffer[MAX_SENDE_BUFF]; |
60 | unsigned volatile char RxdBuffer[MAX_EMPFANGS_BUFF]; |
60 | unsigned volatile char RxdBuffer[MAX_EMPFANGS_BUFF]; |
61 | 61 | ||
62 | unsigned char *pRxData = 0; |
62 | unsigned char *pRxData = 0; |
63 | unsigned char RxDataLen = 0; |
63 | unsigned char RxDataLen = 0; |
64 | unsigned volatile char PC_DebugTimeout = 0; |
64 | unsigned volatile char PC_DebugTimeout = 0; |
65 | unsigned volatile char PC_MotortestActive = 0; |
65 | unsigned volatile char PC_MotortestActive = 0; |
66 | unsigned char DebugTextAnforderung = 255; |
66 | unsigned char DebugTextAnforderung = 255; |
67 | 67 | ||
68 | 68 | ||
69 | 69 | ||
70 | unsigned char PcZugriff = 100; |
70 | unsigned char PcZugriff = 100; |
71 | unsigned char MotorTest[16]; |
71 | unsigned char MotorTest[16]; |
72 | unsigned char MeineSlaveAdresse = 1; // Flight-Ctrl |
72 | unsigned char MeineSlaveAdresse = 1; // Flight-Ctrl |
73 | unsigned char ConfirmFrame; |
73 | unsigned char ConfirmFrame; |
74 | struct str_DebugOut DebugOut; |
74 | struct str_DebugOut DebugOut; |
75 | struct str_ExternControl ExternControl; |
75 | struct str_ExternControl ExternControl; |
76 | struct str_VersionInfo VersionInfo; |
76 | struct str_VersionInfo VersionInfo; |
77 | struct str_WinkelOut WinkelOut; |
77 | struct str_WinkelOut WinkelOut; |
78 | struct str_Data3D Data3D; |
78 | struct str_Data3D Data3D; |
79 | 79 | ||
80 | int Display_Timer, Debug_Timer,Kompass_Timer,Timer3D; |
80 | int Display_Timer, Debug_Timer,Kompass_Timer,Timer3D; |
81 | unsigned int DebugDataIntervall = 0, Intervall3D = 0, Display_Interval = 0; |
81 | unsigned int DebugDataIntervall = 0, Intervall3D = 0, Display_Interval = 0; |
82 | unsigned int AboTimeOut = 0; |
82 | unsigned int AboTimeOut = 0; |
83 | 83 | ||
84 | const unsigned char ANALOG_TEXT[32][16] PROGMEM = |
84 | const unsigned char ANALOG_TEXT[32][16] PROGMEM = |
85 | { |
85 | { |
86 | //1234567890123456 |
86 | //1234567890123456 |
87 | "AngleNick ", //0 |
87 | "AngleNick ", //0 |
88 | "AngleRoll ", |
88 | "AngleRoll ", |
89 | "AccNick ", |
89 | "AccNick ", |
90 | "AccRoll ", |
90 | "AccRoll ", |
91 | "YawGyro ", |
91 | "YawGyro ", |
92 | "Height Value ", //5 |
92 | "Height Value ", //5 |
93 | "AccZ ", |
93 | "AccZ ", |
94 | "Gas ", |
94 | "Gas ", |
95 | "Compass Value ", |
95 | "Compass Value ", |
96 | "Voltage [0.1V] ", |
96 | "Voltage [0.1V] ", |
97 | "Receiver Level ", //10 |
97 | "Receiver Level ", //10 |
98 | "Gyro Compass ", |
98 | "Gyro Compass ", |
99 | "Motor 1 ", |
99 | "Motor 1 ", |
100 | "Motor 2 ", |
100 | "Motor 2 ", |
101 | "Motor 3 ", |
101 | "Motor 3 ", |
102 | "Motor 4 ", //15 |
102 | "Motor 4 ", //15 |
103 | " ", |
103 | " ", |
104 | " ", |
104 | " ", |
105 | " ", |
105 | " ", |
106 | " ", |
106 | " ", |
107 | "Servo ", //20 |
107 | "Servo ", //20 |
108 | "Hovergas ", |
108 | "Hovergas ", |
109 | "Current [0.1A] ", |
109 | "Current [0.1A] ", |
110 | "Capacity [mAh] ", |
110 | "Capacity [mAh] ", |
111 | " ", |
111 | " ", |
112 | " ", //25 |
112 | " ", //25 |
113 | " ", |
113 | " ", |
114 | " ", |
114 | " ", |
115 | "I2C-Error ", |
115 | "I2C-Error ", |
116 | " ", // "Navi Serial Data", |
116 | " ", // "Navi Serial Data", |
117 | "GPS_Nick ", //30 |
117 | "GPS_Nick ", //30 |
118 | "GPS_Roll " |
118 | "GPS_Roll " |
119 | }; |
119 | }; |
120 | 120 | ||
121 | 121 | ||
122 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
122 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
123 | //++ Sende-Part der Datenübertragung |
123 | //++ Sende-Part der Datenübertragung |
124 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
124 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
125 | ISR(USART0_TX_vect) |
125 | ISR(USART0_TX_vect) |
126 | { |
126 | { |
127 | static unsigned int ptr = 0; |
127 | static unsigned int ptr = 0; |
128 | unsigned char tmp_tx; |
128 | unsigned char tmp_tx; |
129 | if(!UebertragungAbgeschlossen) |
129 | if(!UebertragungAbgeschlossen) |
130 | { |
130 | { |
131 | ptr++; // die [0] wurde schon gesendet |
131 | ptr++; // die [0] wurde schon gesendet |
132 | tmp_tx = TxdBuffer[ptr]; |
132 | tmp_tx = TxdBuffer[ptr]; |
133 | if((tmp_tx == '\r') || (ptr == MAX_SENDE_BUFF)) |
133 | if((tmp_tx == '\r') || (ptr == MAX_SENDE_BUFF)) |
134 | { |
134 | { |
135 | ptr = 0; |
135 | ptr = 0; |
136 | UebertragungAbgeschlossen = 1; |
136 | UebertragungAbgeschlossen = 1; |
137 | } |
137 | } |
138 | UDR0 = tmp_tx; |
138 | UDR0 = tmp_tx; |
139 | } |
139 | } |
140 | else ptr = 0; |
140 | else ptr = 0; |
141 | } |
141 | } |
142 | 142 | ||
143 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
143 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
144 | //++ Empfangs-Part der Datenübertragung, incl. CRC-Auswertung |
144 | //++ Empfangs-Part der Datenübertragung, incl. CRC-Auswertung |
145 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
145 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
146 | ISR(USART0_RX_vect) |
146 | ISR(USART0_RX_vect) |
147 | { |
147 | { |
148 | static unsigned int crc; |
148 | static unsigned int crc; |
149 | static unsigned char crc1,crc2,buf_ptr; |
149 | static unsigned char crc1,crc2,buf_ptr; |
150 | static unsigned char UartState = 0; |
150 | static unsigned char UartState = 0; |
151 | unsigned char CrcOkay = 0; |
151 | unsigned char CrcOkay = 0; |
152 | 152 | ||
153 | SioTmp = UDR0; |
153 | SioTmp = UDR0; |
154 | if(buf_ptr >= MAX_SENDE_BUFF) UartState = 0; |
154 | if(buf_ptr >= MAX_SENDE_BUFF) UartState = 0; |
155 | if(SioTmp == '\r' && UartState == 2) |
155 | if(SioTmp == '\r' && UartState == 2) |
156 | { |
156 | { |
157 | UartState = 0; |
157 | UartState = 0; |
158 | crc -= RxdBuffer[buf_ptr-2]; |
158 | crc -= RxdBuffer[buf_ptr-2]; |
159 | crc -= RxdBuffer[buf_ptr-1]; |
159 | crc -= RxdBuffer[buf_ptr-1]; |
160 | crc %= 4096; |
160 | crc %= 4096; |
161 | crc1 = '=' + crc / 64; |
161 | crc1 = '=' + crc / 64; |
162 | crc2 = '=' + crc % 64; |
162 | crc2 = '=' + crc % 64; |
163 | CrcOkay = 0; |
163 | CrcOkay = 0; |
164 | if((crc1 == RxdBuffer[buf_ptr-2]) && (crc2 == RxdBuffer[buf_ptr-1])) CrcOkay = 1; else { CrcOkay = 0; CntCrcError++;}; |
164 | if((crc1 == RxdBuffer[buf_ptr-2]) && (crc2 == RxdBuffer[buf_ptr-1])) CrcOkay = 1; else { CrcOkay = 0; CntCrcError++;}; |
165 | if(!NeuerDatensatzEmpfangen && CrcOkay) // Datensatz schon verarbeitet |
165 | if(!NeuerDatensatzEmpfangen && CrcOkay) // Datensatz schon verarbeitet |
166 | { |
166 | { |
167 | NeuerDatensatzEmpfangen = 1; |
167 | NeuerDatensatzEmpfangen = 1; |
168 | AnzahlEmpfangsBytes = buf_ptr + 1; |
168 | AnzahlEmpfangsBytes = buf_ptr + 1; |
169 | RxdBuffer[buf_ptr] = '\r'; |
169 | RxdBuffer[buf_ptr] = '\r'; |
170 | if(RxdBuffer[2] == 'R') |
170 | if(RxdBuffer[2] == 'R') |
171 | { |
171 | { |
172 | LcdClear(); |
172 | LcdClear(); |
173 | wdt_enable(WDTO_250MS); // Reset-Commando |
173 | wdt_enable(WDTO_250MS); // Reset-Commando |
174 | ServoActive = 0; |
174 | ServoActive = 0; |
175 | 175 | ||
176 | } |
176 | } |
177 | } |
177 | } |
178 | } |
178 | } |
179 | else |
179 | else |
180 | switch(UartState) |
180 | switch(UartState) |
181 | { |
181 | { |
182 | case 0: |
182 | case 0: |
183 | if(SioTmp == '#' && !NeuerDatensatzEmpfangen) UartState = 1; // Startzeichen und Daten schon verarbeitet |
183 | if(SioTmp == '#' && !NeuerDatensatzEmpfangen) UartState = 1; // Startzeichen und Daten schon verarbeitet |
184 | buf_ptr = 0; |
184 | buf_ptr = 0; |
185 | RxdBuffer[buf_ptr++] = SioTmp; |
185 | RxdBuffer[buf_ptr++] = SioTmp; |
186 | crc = SioTmp; |
186 | crc = SioTmp; |
187 | break; |
187 | break; |
188 | case 1: // Adresse auswerten |
188 | case 1: // Adresse auswerten |
189 | UartState++; |
189 | UartState++; |
190 | RxdBuffer[buf_ptr++] = SioTmp; |
190 | RxdBuffer[buf_ptr++] = SioTmp; |
191 | crc += SioTmp; |
191 | crc += SioTmp; |
192 | break; |
192 | break; |
193 | case 2: // Eingangsdaten sammeln |
193 | case 2: // Eingangsdaten sammeln |
194 | RxdBuffer[buf_ptr] = SioTmp; |
194 | RxdBuffer[buf_ptr] = SioTmp; |
195 | if(buf_ptr < MAX_EMPFANGS_BUFF) buf_ptr++; |
195 | if(buf_ptr < MAX_EMPFANGS_BUFF) buf_ptr++; |
196 | else UartState = 0; |
196 | else UartState = 0; |
197 | crc += SioTmp; |
197 | crc += SioTmp; |
198 | break; |
198 | break; |
199 | default: |
199 | default: |
200 | UartState = 0; |
200 | UartState = 0; |
201 | break; |
201 | break; |
202 | } |
202 | } |
203 | } |
203 | } |
204 | 204 | ||
205 | 205 | ||
206 | // -------------------------------------------------------------------------- |
206 | // -------------------------------------------------------------------------- |
207 | void AddCRC(unsigned int wieviele) |
207 | void AddCRC(unsigned int wieviele) |
208 | { |
208 | { |
209 | unsigned int tmpCRC = 0,i; |
209 | unsigned int tmpCRC = 0,i; |
210 | for(i = 0; i < wieviele;i++) |
210 | for(i = 0; i < wieviele;i++) |
211 | { |
211 | { |
212 | tmpCRC += TxdBuffer[i]; |
212 | tmpCRC += TxdBuffer[i]; |
213 | } |
213 | } |
214 | tmpCRC %= 4096; |
214 | tmpCRC %= 4096; |
215 | TxdBuffer[i++] = '=' + tmpCRC / 64; |
215 | TxdBuffer[i++] = '=' + tmpCRC / 64; |
216 | TxdBuffer[i++] = '=' + tmpCRC % 64; |
216 | TxdBuffer[i++] = '=' + tmpCRC % 64; |
217 | TxdBuffer[i++] = '\r'; |
217 | TxdBuffer[i++] = '\r'; |
218 | UebertragungAbgeschlossen = 0; |
218 | UebertragungAbgeschlossen = 0; |
219 | UDR0 = TxdBuffer[0]; |
219 | UDR0 = TxdBuffer[0]; |
220 | } |
220 | } |
221 | 221 | ||
222 | 222 | ||
223 | 223 | ||
224 | // -------------------------------------------------------------------------- |
224 | // -------------------------------------------------------------------------- |
225 | void SendOutData(unsigned char cmd,unsigned char address, unsigned char BufferAnzahl, ...) //unsigned char *snd, unsigned char len) |
225 | void SendOutData(unsigned char cmd,unsigned char address, unsigned char BufferAnzahl, ...) //unsigned char *snd, unsigned char len) |
226 | { |
226 | { |
227 | va_list ap; |
227 | va_list ap; |
228 | unsigned int pt = 0; |
228 | unsigned int pt = 0; |
229 | unsigned char a,b,c; |
229 | unsigned char a,b,c; |
230 | unsigned char ptr = 0; |
230 | unsigned char ptr = 0; |
231 | 231 | ||
232 | unsigned char *snd = 0; |
232 | unsigned char *snd = 0; |
233 | int len = 0; |
233 | int len = 0; |
234 | 234 | ||
235 | TxdBuffer[pt++] = '#'; // Startzeichen |
235 | TxdBuffer[pt++] = '#'; // Startzeichen |
236 | TxdBuffer[pt++] = 'a' + address; // Adresse (a=0; b=1,...) |
236 | TxdBuffer[pt++] = 'a' + address; // Adresse (a=0; b=1,...) |
237 | TxdBuffer[pt++] = cmd; // Commando |
237 | TxdBuffer[pt++] = cmd; // Commando |
238 | 238 | ||
239 | va_start(ap, BufferAnzahl); |
239 | va_start(ap, BufferAnzahl); |
240 | if(BufferAnzahl) |
240 | if(BufferAnzahl) |
241 | { |
241 | { |
242 | snd = va_arg(ap, unsigned char*); |
242 | snd = va_arg(ap, unsigned char*); |
243 | len = va_arg(ap, int); |
243 | len = va_arg(ap, int); |
244 | ptr = 0; |
244 | ptr = 0; |
245 | BufferAnzahl--; |
245 | BufferAnzahl--; |
246 | } |
246 | } |
247 | while(len) |
247 | while(len) |
248 | { |
248 | { |
249 | if(len) |
249 | if(len) |
250 | { |
250 | { |
251 | a = snd[ptr++]; |
251 | a = snd[ptr++]; |
252 | len--; |
252 | len--; |
253 | if((!len) && BufferAnzahl) |
253 | if((!len) && BufferAnzahl) |
254 | { |
254 | { |
255 | snd = va_arg(ap, unsigned char*); |
255 | snd = va_arg(ap, unsigned char*); |
256 | len = va_arg(ap, int); |
256 | len = va_arg(ap, int); |
257 | ptr = 0; |
257 | ptr = 0; |
258 | BufferAnzahl--; |
258 | BufferAnzahl--; |
259 | } |
259 | } |
260 | } |
260 | } |
261 | else a = 0; |
261 | else a = 0; |
262 | if(len) |
262 | if(len) |
263 | { |
263 | { |
264 | b = snd[ptr++]; |
264 | b = snd[ptr++]; |
265 | len--; |
265 | len--; |
266 | if((!len) && BufferAnzahl) |
266 | if((!len) && BufferAnzahl) |
267 | { |
267 | { |
268 | snd = va_arg(ap, unsigned char*); |
268 | snd = va_arg(ap, unsigned char*); |
269 | len = va_arg(ap, int); |
269 | len = va_arg(ap, int); |
270 | ptr = 0; |
270 | ptr = 0; |
271 | BufferAnzahl--; |
271 | BufferAnzahl--; |
272 | } |
272 | } |
273 | } |
273 | } |
274 | else b = 0; |
274 | else b = 0; |
275 | if(len) |
275 | if(len) |
276 | { |
276 | { |
277 | c = snd[ptr++]; |
277 | c = snd[ptr++]; |
278 | len--; |
278 | len--; |
279 | if((!len) && BufferAnzahl) |
279 | if((!len) && BufferAnzahl) |
280 | { |
280 | { |
281 | snd = va_arg(ap, unsigned char*); |
281 | snd = va_arg(ap, unsigned char*); |
282 | len = va_arg(ap, int); |
282 | len = va_arg(ap, int); |
283 | ptr = 0; |
283 | ptr = 0; |
284 | BufferAnzahl--; |
284 | BufferAnzahl--; |
285 | } |
285 | } |
286 | } |
286 | } |
287 | else c = 0; |
287 | else c = 0; |
288 | TxdBuffer[pt++] = '=' + (a >> 2); |
288 | TxdBuffer[pt++] = '=' + (a >> 2); |
289 | TxdBuffer[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4)); |
289 | TxdBuffer[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4)); |
290 | TxdBuffer[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6)); |
290 | TxdBuffer[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6)); |
291 | TxdBuffer[pt++] = '=' + ( c & 0x3f); |
291 | TxdBuffer[pt++] = '=' + ( c & 0x3f); |
292 | } |
292 | } |
293 | va_end(ap); |
293 | va_end(ap); |
294 | AddCRC(pt); |
294 | AddCRC(pt); |
295 | } |
295 | } |
296 | 296 | ||
297 | // -------------------------------------------------------------------------- |
297 | // -------------------------------------------------------------------------- |
298 | void Decode64(void) // die daten werden im rx buffer dekodiert, das geht nur, weil aus 4 byte immer 3 gemacht werden. |
298 | void Decode64(void) // die daten werden im rx buffer dekodiert, das geht nur, weil aus 4 byte immer 3 gemacht werden. |
299 | { |
299 | { |
300 | unsigned char a,b,c,d; |
300 | unsigned char a,b,c,d; |
301 | unsigned char x,y,z; |
301 | unsigned char x,y,z; |
302 | unsigned char ptrIn = 3; // start at begin of data block |
302 | unsigned char ptrIn = 3; // start at begin of data block |
303 | unsigned char ptrOut = 3; |
303 | unsigned char ptrOut = 3; |
304 | unsigned char len = AnzahlEmpfangsBytes - 6; // von der Gesamtbytezahl eines Frames gehen 3 Bytes des Headers ('#',Addr, Cmd) und 3 Bytes des Footers (CRC1, CRC2, '\r') ab. |
304 | unsigned char len = AnzahlEmpfangsBytes - 6; // von der Gesamtbytezahl eines Frames gehen 3 Bytes des Headers ('#',Addr, Cmd) und 3 Bytes des Footers (CRC1, CRC2, '\r') ab. |
305 | 305 | ||
306 | while(len) |
306 | while(len) |
307 | { |
307 | { |
308 | a = RxdBuffer[ptrIn++] - '='; |
308 | a = RxdBuffer[ptrIn++] - '='; |
309 | b = RxdBuffer[ptrIn++] - '='; |
309 | b = RxdBuffer[ptrIn++] - '='; |
310 | c = RxdBuffer[ptrIn++] - '='; |
310 | c = RxdBuffer[ptrIn++] - '='; |
311 | d = RxdBuffer[ptrIn++] - '='; |
311 | d = RxdBuffer[ptrIn++] - '='; |
312 | 312 | ||
313 | x = (a << 2) | (b >> 4); |
313 | x = (a << 2) | (b >> 4); |
314 | y = ((b & 0x0f) << 4) | (c >> 2); |
314 | y = ((b & 0x0f) << 4) | (c >> 2); |
315 | z = ((c & 0x03) << 6) | d; |
315 | z = ((c & 0x03) << 6) | d; |
316 | 316 | ||
317 | if(len--) RxdBuffer[ptrOut++] = x; else break; |
317 | if(len--) RxdBuffer[ptrOut++] = x; else break; |
318 | if(len--) RxdBuffer[ptrOut++] = y; else break; |
318 | if(len--) RxdBuffer[ptrOut++] = y; else break; |
319 | if(len--) RxdBuffer[ptrOut++] = z; else break; |
319 | if(len--) RxdBuffer[ptrOut++] = z; else break; |
320 | } |
320 | } |
321 | pRxData = (unsigned char*)&RxdBuffer[3]; // decodierte Daten beginnen beim 4. Byte |
321 | pRxData = (unsigned char*)&RxdBuffer[3]; // decodierte Daten beginnen beim 4. Byte |
322 | RxDataLen = ptrOut - 3; // wie viele Bytes wurden dekodiert? |
322 | RxDataLen = ptrOut - 3; // wie viele Bytes wurden dekodiert? |
323 | 323 | ||
324 | } |
324 | } |
325 | 325 | ||
326 | // -------------------------------------------------------------------------- |
326 | // -------------------------------------------------------------------------- |
327 | void BearbeiteRxDaten(void) |
327 | void BearbeiteRxDaten(void) |
328 | { |
328 | { |
329 | if(!NeuerDatensatzEmpfangen) return; |
329 | if(!NeuerDatensatzEmpfangen) return; |
330 | 330 | ||
331 | unsigned char tempchar1; |
331 | unsigned char tempchar1, tempchar2; |
332 | Decode64(); // dekodiere datenblock im Empfangsbuffer |
332 | Decode64(); // dekodiere datenblock im Empfangsbuffer |
333 | switch(RxdBuffer[1]-'a') // check for Slave Address |
333 | switch(RxdBuffer[1]-'a') // check for Slave Address |
334 | { |
334 | { |
335 | case FC_ADDRESS: // FC special commands |
335 | case FC_ADDRESS: // FC special commands |
336 | switch(RxdBuffer[2]) |
336 | switch(RxdBuffer[2]) |
337 | { |
337 | { |
338 | case 'K':// Kompasswert |
338 | case 'K':// Kompasswert |
339 | memcpy((unsigned char *)&KompassValue , (unsigned char *)pRxData, sizeof(KompassValue)); |
339 | memcpy((unsigned char *)&KompassValue , (unsigned char *)pRxData, sizeof(KompassValue)); |
340 | KompassRichtung = ((540 + KompassValue - KompassStartwert) % 360) - 180; |
340 | KompassRichtung = ((540 + KompassValue - KompassStartwert) % 360) - 180; |
341 | break; |
341 | break; |
342 | case 't':// Motortest |
342 | case 't':// Motortest |
343 | if(AnzahlEmpfangsBytes > 20) memcpy(&MotorTest[0], (unsigned char *)pRxData, sizeof(MotorTest)); |
343 | if(AnzahlEmpfangsBytes > 20) memcpy(&MotorTest[0], (unsigned char *)pRxData, sizeof(MotorTest)); |
344 | else memcpy(&MotorTest[0], (unsigned char *)pRxData, 4); |
344 | else memcpy(&MotorTest[0], (unsigned char *)pRxData, 4); |
345 | PC_MotortestActive = 240; |
345 | PC_MotortestActive = 240; |
346 | //while(!UebertragungAbgeschlossen); |
346 | //while(!UebertragungAbgeschlossen); |
347 | //SendOutData('T', MeineSlaveAdresse, 0); |
347 | //SendOutData('T', MeineSlaveAdresse, 0); |
348 | PcZugriff = 255; |
348 | PcZugriff = 255; |
349 | break; |
349 | break; |
350 | 350 | ||
351 | case 'n':// "Get Mixer |
351 | case 'n':// "Get Mixer |
352 | while(!UebertragungAbgeschlossen); |
352 | while(!UebertragungAbgeschlossen); |
353 | SendOutData('N', FC_ADDRESS, 1, (unsigned char *) &Mixer, sizeof(Mixer) - 1); |
353 | SendOutData('N', FC_ADDRESS, 1, (unsigned char *) &Mixer, sizeof(Mixer) - 1); |
354 | Debug("Mixer lesen"); |
354 | Debug("Mixer lesen"); |
355 | break; |
355 | break; |
356 | 356 | ||
357 | case 'm':// "Write Mixer |
357 | case 'm':// "Write Mixer |
358 | if(pRxData[0] == EEMIXER_REVISION) |
358 | if(pRxData[0] == EEMIXER_REVISION) |
359 | { |
359 | { |
360 | memcpy(&Mixer, (unsigned char *)pRxData, sizeof(Mixer) - 1); |
360 | memcpy(&Mixer, (unsigned char *)pRxData, sizeof(Mixer) - 1); |
361 | MixerTable_WriteToEEProm(); |
361 | MixerTable_WriteToEEProm(); |
362 | tempchar1 = 1; |
362 | tempchar1 = 1; |
363 | } |
363 | } |
364 | else |
364 | else |
365 | { |
365 | { |
366 | tempchar1 = 0; |
366 | tempchar1 = 0; |
367 | } |
367 | } |
368 | while(!UebertragungAbgeschlossen); |
368 | while(!UebertragungAbgeschlossen); |
369 | SendOutData('M', FC_ADDRESS, 1, &tempchar1, sizeof(tempchar1)); |
369 | SendOutData('M', FC_ADDRESS, 1, &tempchar1, sizeof(tempchar1)); |
370 | 370 | ||
371 | break; |
371 | break; |
372 | 372 | ||
373 | case 'p': // get PPM Channels |
373 | case 'p': // get PPM Channels |
374 | GetPPMChannelAnforderung = 1; |
374 | GetPPMChannelAnforderung = 1; |
375 | PcZugriff = 255; |
375 | PcZugriff = 255; |
376 | break; |
376 | break; |
377 | 377 | ||
378 | case 'q':// "Get"-Anforderung für Settings |
378 | case 'q':// "Get"-Anforderung für Settings |
379 | // Bei Get werden die vom PC einstellbaren Werte vom PC zurückgelesen |
379 | // Bei Get werden die vom PC einstellbaren Werte vom PC zurückgelesen |
380 | if(pRxData[0] == 0xFF) |
380 | if(pRxData[0] == 0xFF) |
381 | { |
381 | { |
382 | pRxData[0] = GetActiveParamSet(); |
382 | pRxData[0] = GetActiveParamSet(); |
383 | } |
383 | } |
384 | // limit settings range |
384 | // limit settings range |
385 | if(pRxData[0] < 1) pRxData[0] = 1; // limit to 5 |
385 | if(pRxData[0] < 1) pRxData[0] = 1; // limit to 5 |
386 | else if(pRxData[0] > 5) pRxData[0] = 5; // limit to 5 |
386 | else if(pRxData[0] > 5) pRxData[0] = 5; // limit to 5 |
387 | // load requested parameter set |
387 | // load requested parameter set |
388 | ParamSet_ReadFromEEProm(pRxData[0]); |
388 | ParamSet_ReadFromEEProm(pRxData[0]); |
389 | tempchar1 = pRxData[0]; |
389 | tempchar1 = pRxData[0]; |
390 | while(!UebertragungAbgeschlossen); |
390 | while(!UebertragungAbgeschlossen); |
391 | SendOutData('Q', FC_ADDRESS, 2, &tempchar1, sizeof(tempchar1), (unsigned char *) &EE_Parameter, sizeof(EE_Parameter) - 1); |
391 | SendOutData('Q', FC_ADDRESS, 2, &tempchar1, sizeof(tempchar1), (unsigned char *) &EE_Parameter, sizeof(EE_Parameter) - 1); |
392 | Debug("Lese Setting %d", tempchar1); |
392 | Debug("Lese Setting %d", tempchar1); |
393 | 393 | ||
394 | break; |
394 | break; |
395 | 395 | ||
396 | case 's': // Parametersatz speichern |
396 | case 's': // Parametersatz speichern |
397 | if((1 <= pRxData[0]) && (pRxData[0] <= 5) && (pRxData[1] == EEPARAM_REVISION)) // check for setting to be in range |
397 | if((1 <= pRxData[0]) && (pRxData[0] <= 5) && (pRxData[1] == EEPARAM_REVISION)) // check for setting to be in range |
398 | { |
398 | { |
399 | memcpy(&EE_Parameter, (uint8_t*)&pRxData[1], sizeof(EE_Parameter) - 1); |
399 | memcpy(&EE_Parameter, (uint8_t*)&pRxData[1], sizeof(EE_Parameter) - 1); |
400 | ParamSet_WriteToEEProm(pRxData[0]); |
400 | ParamSet_WriteToEEProm(pRxData[0]); |
401 | Umschlag180Nick = (long) EE_Parameter.WinkelUmschlagNick * 2500L; |
401 | Umschlag180Nick = (long) EE_Parameter.WinkelUmschlagNick * 2500L; |
402 | Umschlag180Roll = (long) EE_Parameter.WinkelUmschlagRoll * 2500L; |
402 | Umschlag180Roll = (long) EE_Parameter.WinkelUmschlagRoll * 2500L; |
403 | tempchar1 = GetActiveParamSet(); |
403 | tempchar1 = GetActiveParamSet(); |
404 | } |
404 | } |
405 | else |
405 | else |
406 | { |
406 | { |
407 | tempchar1 = 0; // mark in response an invlid setting |
407 | tempchar1 = 0; // mark in response an invlid setting |
408 | } |
408 | } |
409 | while(!UebertragungAbgeschlossen); |
409 | while(!UebertragungAbgeschlossen); |
410 | SendOutData('S', FC_ADDRESS, 1, &tempchar1, sizeof(tempchar1)); |
410 | SendOutData('S', FC_ADDRESS, 1, &tempchar1, sizeof(tempchar1)); |
411 | if(!MotorenEin) Piep(tempchar1,110); |
411 | if(!MotorenEin) Piep(tempchar1,110); |
412 | LipoDetection(0); |
412 | LipoDetection(0); |
413 | LIBFC_ReceiverInit(EE_Parameter.Receiver); |
413 | LIBFC_ReceiverInit(EE_Parameter.Receiver); |
414 | break; |
414 | break; |
415 | case 'f': // auf anderen Parametersatz umschalten |
415 | case 'f': // auf anderen Parametersatz umschalten |
416 | if((1 <= pRxData[0]) && (pRxData[0] <= 5)) ParamSet_ReadFromEEProm(pRxData[0]); |
416 | if((1 <= pRxData[0]) && (pRxData[0] <= 5)) ParamSet_ReadFromEEProm(pRxData[0]); |
417 | tempchar1 = GetActiveParamSet(); |
417 | tempchar1 = GetActiveParamSet(); |
418 | while(!UebertragungAbgeschlossen); |
418 | while(!UebertragungAbgeschlossen); |
419 | SendOutData('F', FC_ADDRESS, 1, &tempchar1, sizeof(tempchar1)); |
419 | SendOutData('F', FC_ADDRESS, 1, &tempchar1, sizeof(tempchar1)); |
420 | if(!MotorenEin) Piep(tempchar1,110); |
420 | if(!MotorenEin) Piep(tempchar1,110); |
421 | LipoDetection(0); |
421 | LipoDetection(0); |
422 | LIBFC_ReceiverInit(EE_Parameter.Receiver); |
422 | LIBFC_ReceiverInit(EE_Parameter.Receiver); |
423 | break; |
423 | break; |
424 | case 'y':// serial Potis |
424 | case 'y':// serial Potis |
425 | PPM_in[13] = (signed char) pRxData[0]; PPM_in[14] = (signed char) pRxData[1]; PPM_in[15] = (signed char) pRxData[2]; PPM_in[16] = (signed char) pRxData[3]; |
425 | PPM_in[13] = (signed char) pRxData[0]; PPM_in[14] = (signed char) pRxData[1]; PPM_in[15] = (signed char) pRxData[2]; PPM_in[16] = (signed char) pRxData[3]; |
426 | PPM_in[17] = (signed char) pRxData[4]; PPM_in[18] = (signed char) pRxData[5]; PPM_in[19] = (signed char) pRxData[6]; PPM_in[20] = (signed char) pRxData[7]; |
426 | PPM_in[17] = (signed char) pRxData[4]; PPM_in[18] = (signed char) pRxData[5]; PPM_in[19] = (signed char) pRxData[6]; PPM_in[20] = (signed char) pRxData[7]; |
427 | PPM_in[21] = (signed char) pRxData[8]; PPM_in[22] = (signed char) pRxData[9]; PPM_in[23] = (signed char) pRxData[10]; PPM_in[24] = (signed char) pRxData[11]; |
427 | PPM_in[21] = (signed char) pRxData[8]; PPM_in[22] = (signed char) pRxData[9]; PPM_in[23] = (signed char) pRxData[10]; PPM_in[24] = (signed char) pRxData[11]; |
428 | break; |
428 | break; |
429 | 429 | ||
430 | case 'u': // request BL parameter |
430 | case 'u': // request BL parameter |
431 | Debug("Reading BL %d", pRxData[0]); |
431 | Debug("Reading BL %d", pRxData[0]); |
432 | // try to read BL configuration |
432 | // try to read BL configuration |
433 | tempchar1 = I2C_ReadBLConfig(pRxData[0]); |
433 | tempchar2 = I2C_ReadBLConfig(pRxData[0]); |
434 | if(tempchar1) |
434 | if(tempchar2 == BLCONFIG_SUCCESS) |
435 | { |
435 | { |
436 | while(!UebertragungAbgeschlossen); // wait for previous frame to be sent |
436 | while(!UebertragungAbgeschlossen); // wait for previous frame to be sent |
437 | SendOutData('U', FC_ADDRESS, 2, tempchar1, 1, &BLConfig, sizeof(BLConfig_t)); |
437 | SendOutData('U', FC_ADDRESS, 2, pRxData[0], 1, &BLConfig, sizeof(BLConfig_t)); |
438 | } |
438 | } |
439 | break; |
439 | break; |
440 | 440 | ||
441 | case 'w': // write BL parameter |
441 | case 'w': // write BL parameter |
442 | Debug("Writing BL %d", pRxData[0]); |
442 | Debug("Writing BL %d", pRxData[0]); |
443 | if(RxDataLen >= 1+sizeof(BLConfig_t)) |
443 | if(RxDataLen >= 1+sizeof(BLConfig_t)) |
444 | { |
444 | { |
445 | memcpy(&BLConfig, (uint8_t*)(&pRxData[1]), sizeof(BLConfig_t)); |
445 | memcpy(&BLConfig, (uint8_t*)(&pRxData[1]), sizeof(BLConfig_t)); |
446 | // tbd. in MK-Tool |
446 | tempchar2 = I2C_WriteBLConfig(pRxData[0]); |
447 | BLConfig.crc = RAM_Checksum((uint8_t*)&BLConfig, sizeof(BLConfig_t) - 1); // update crc |
447 | if(tempchar2 == BLCONFIG_SUCCESS) tempchar1 = 1; |
448 | tempchar1 = I2C_WriteBLConfig(pRxData[0]); |
448 | else tempchar1 = 0; // indicate error |
449 | while(!UebertragungAbgeschlossen); // wait for previous frame to be sent |
449 | while(!UebertragungAbgeschlossen); // wait for previous frame to be sent |
450 | SendOutData('W', FC_ADDRESS,1, &tempchar1, sizeof(tempchar1)); |
450 | SendOutData('W', FC_ADDRESS,2, &tempchar1, sizeof(tempchar1), &tempchar2, sizeof(tempchar2)); |
451 | } |
451 | } |
452 | break; |
452 | break; |
453 | 453 | ||
454 | } // case FC_ADDRESS: |
454 | } // case FC_ADDRESS: |
455 | 455 | ||
456 | default: // any Slave Address |
456 | default: // any Slave Address |
457 | 457 | ||
458 | switch(RxdBuffer[2]) |
458 | switch(RxdBuffer[2]) |
459 | { |
459 | { |
460 | // 't' comand placed here only for compatibility to BL |
460 | // 't' comand placed here only for compatibility to BL |
461 | case 't':// Motortest |
461 | case 't':// Motortest |
462 | if(AnzahlEmpfangsBytes >= sizeof(MotorTest)) memcpy(&MotorTest[0], (unsigned char *)pRxData, sizeof(MotorTest)); |
462 | if(AnzahlEmpfangsBytes >= sizeof(MotorTest)) memcpy(&MotorTest[0], (unsigned char *)pRxData, sizeof(MotorTest)); |
463 | else memcpy(&MotorTest[0], (unsigned char *)pRxData, 4); |
463 | else memcpy(&MotorTest[0], (unsigned char *)pRxData, 4); |
464 | while(!UebertragungAbgeschlossen); |
464 | while(!UebertragungAbgeschlossen); |
465 | SendOutData('T', MeineSlaveAdresse, 0); |
465 | SendOutData('T', MeineSlaveAdresse, 0); |
466 | PC_MotortestActive = 250; |
466 | PC_MotortestActive = 250; |
467 | PcZugriff = 255; |
467 | PcZugriff = 255; |
468 | AboTimeOut = SetDelay(ABO_TIMEOUT); |
468 | AboTimeOut = SetDelay(ABO_TIMEOUT); |
469 | break; |
469 | break; |
470 | // 'K' comand placed here only for compatibility to old MK3MAG software, that does not send the right Slave Address |
470 | // 'K' comand placed here only for compatibility to old MK3MAG software, that does not send the right Slave Address |
471 | case 'K':// Kompasswert |
471 | case 'K':// Kompasswert |
472 | memcpy((unsigned char *)&KompassValue , (unsigned char *)pRxData, sizeof(KompassValue)); |
472 | memcpy((unsigned char *)&KompassValue , (unsigned char *)pRxData, sizeof(KompassValue)); |
473 | KompassRichtung = ((540 + KompassValue - KompassStartwert) % 360) - 180; |
473 | KompassRichtung = ((540 + KompassValue - KompassStartwert) % 360) - 180; |
474 | break; |
474 | break; |
475 | case 'a':// Texte der Analogwerte |
475 | case 'a':// Texte der Analogwerte |
476 | DebugTextAnforderung = pRxData[0]; |
476 | DebugTextAnforderung = pRxData[0]; |
477 | if (DebugTextAnforderung > 31) DebugTextAnforderung = 31; |
477 | if (DebugTextAnforderung > 31) DebugTextAnforderung = 31; |
478 | PcZugriff = 255; |
478 | PcZugriff = 255; |
479 | break; |
479 | break; |
480 | case 'b': |
480 | case 'b': |
481 | memcpy((unsigned char *)&ExternControl, (unsigned char *)pRxData, sizeof(ExternControl)); |
481 | memcpy((unsigned char *)&ExternControl, (unsigned char *)pRxData, sizeof(ExternControl)); |
482 | ConfirmFrame = ExternControl.Frame; |
482 | ConfirmFrame = ExternControl.Frame; |
483 | PcZugriff = 255; |
483 | PcZugriff = 255; |
484 | break; |
484 | break; |
485 | case 'c': // Poll the 3D-Data |
485 | case 'c': // Poll the 3D-Data |
486 | if(!Intervall3D) { if(pRxData[0]) Timer3D = SetDelay(pRxData[0] * 10);} |
486 | if(!Intervall3D) { if(pRxData[0]) Timer3D = SetDelay(pRxData[0] * 10);} |
487 | Intervall3D = pRxData[0] * 10; |
487 | Intervall3D = pRxData[0] * 10; |
488 | AboTimeOut = SetDelay(ABO_TIMEOUT); |
488 | AboTimeOut = SetDelay(ABO_TIMEOUT); |
489 | break; |
489 | break; |
490 | case 'd': // Poll the debug data |
490 | case 'd': // Poll the debug data |
491 | PcZugriff = 255; |
491 | PcZugriff = 255; |
492 | DebugDataIntervall = (unsigned int)pRxData[0] * 10; |
492 | DebugDataIntervall = (unsigned int)pRxData[0] * 10; |
493 | if(DebugDataIntervall > 0) DebugDataAnforderung = 1; |
493 | if(DebugDataIntervall > 0) DebugDataAnforderung = 1; |
494 | AboTimeOut = SetDelay(ABO_TIMEOUT); |
494 | AboTimeOut = SetDelay(ABO_TIMEOUT); |
495 | break; |
495 | break; |
496 | 496 | ||
497 | case 'h':// x-1 Displayzeilen |
497 | case 'h':// x-1 Displayzeilen |
498 | PcZugriff = 255; |
498 | PcZugriff = 255; |
499 | if((pRxData[0] & 0x80) == 0x00) // old format |
499 | if((pRxData[0] & 0x80) == 0x00) // old format |
500 | { |
500 | { |
501 | DisplayLine = 2; |
501 | DisplayLine = 2; |
502 | Display_Interval = 0; |
502 | Display_Interval = 0; |
503 | } |
503 | } |
504 | else // new format |
504 | else // new format |
505 | { |
505 | { |
506 | RemoteKeys |= ~pRxData[0]; |
506 | RemoteKeys |= ~pRxData[0]; |
507 | Display_Interval = (unsigned int)pRxData[1] * 10; |
507 | Display_Interval = (unsigned int)pRxData[1] * 10; |
508 | DisplayLine = 4; |
508 | DisplayLine = 4; |
509 | AboTimeOut = SetDelay(ABO_TIMEOUT); |
509 | AboTimeOut = SetDelay(ABO_TIMEOUT); |
510 | } |
510 | } |
511 | DebugDisplayAnforderung = 1; |
511 | DebugDisplayAnforderung = 1; |
512 | break; |
512 | break; |
513 | 513 | ||
514 | case 'l':// x-1 Displayzeilen |
514 | case 'l':// x-1 Displayzeilen |
515 | PcZugriff = 255; |
515 | PcZugriff = 255; |
516 | MenuePunkt = pRxData[0]; |
516 | MenuePunkt = pRxData[0]; |
517 | DebugDisplayAnforderung1 = 1; |
517 | DebugDisplayAnforderung1 = 1; |
518 | break; |
518 | break; |
519 | case 'v': // Version-Anforderung und Ausbaustufe |
519 | case 'v': // Version-Anforderung und Ausbaustufe |
520 | GetVersionAnforderung = 1; |
520 | GetVersionAnforderung = 1; |
521 | break; |
521 | break; |
522 | 522 | ||
523 | case 'g':// |
523 | case 'g':// |
524 | GetExternalControl = 1; |
524 | GetExternalControl = 1; |
525 | break; |
525 | break; |
526 | 526 | ||
527 | default: |
527 | default: |
528 | //unsupported command received |
528 | //unsupported command received |
529 | break; |
529 | break; |
530 | } |
530 | } |
531 | break; // default: |
531 | break; // default: |
532 | } |
532 | } |
533 | NeuerDatensatzEmpfangen = 0; |
533 | NeuerDatensatzEmpfangen = 0; |
534 | pRxData = 0; |
534 | pRxData = 0; |
535 | RxDataLen = 0; |
535 | RxDataLen = 0; |
536 | } |
536 | } |
537 | 537 | ||
538 | //############################################################################ |
538 | //############################################################################ |
539 | //Routine für die Serielle Ausgabe |
539 | //Routine für die Serielle Ausgabe |
540 | void uart_putchar (char c) |
540 | void uart_putchar (char c) |
541 | //############################################################################ |
541 | //############################################################################ |
542 | { |
542 | { |
543 | //Warten solange bis Zeichen gesendet wurde |
543 | //Warten solange bis Zeichen gesendet wurde |
544 | loop_until_bit_is_set(UCSR0A, UDRE0); |
544 | loop_until_bit_is_set(UCSR0A, UDRE0); |
545 | //Ausgabe des Zeichens |
545 | //Ausgabe des Zeichens |
546 | UDR0 = c; |
546 | UDR0 = c; |
547 | } |
547 | } |
548 | 548 | ||
549 | 549 | ||
550 | //############################################################################ |
550 | //############################################################################ |
551 | //INstallation der Seriellen Schnittstelle |
551 | //INstallation der Seriellen Schnittstelle |
552 | void UART_Init (void) |
552 | void UART_Init (void) |
553 | //############################################################################ |
553 | //############################################################################ |
554 | { |
554 | { |
555 | unsigned int ubrr = (unsigned int) ((unsigned long) F_CPU/(8 * USART0_BAUD) - 1); |
555 | unsigned int ubrr = (unsigned int) ((unsigned long) F_CPU/(8 * USART0_BAUD) - 1); |
556 | 556 | ||
557 | //Enable TXEN im Register UCR TX-Data Enable & RX Enable |
557 | //Enable TXEN im Register UCR TX-Data Enable & RX Enable |
558 | UCSR0B = (1 << TXEN0) | (1 << RXEN0); |
558 | UCSR0B = (1 << TXEN0) | (1 << RXEN0); |
559 | // UART Double Speed (U2X) |
559 | // UART Double Speed (U2X) |
560 | UCSR0A |= (1 << U2X0); |
560 | UCSR0A |= (1 << U2X0); |
561 | // RX-Interrupt Freigabe |
561 | // RX-Interrupt Freigabe |
562 | UCSR0B |= (1 << RXCIE0); |
562 | UCSR0B |= (1 << RXCIE0); |
563 | // TX-Interrupt Freigabe |
563 | // TX-Interrupt Freigabe |
564 | UCSR0B |= (1 << TXCIE0); |
564 | UCSR0B |= (1 << TXCIE0); |
565 | // USART0 Baud Rate Register |
565 | // USART0 Baud Rate Register |
566 | // set clock divider |
566 | // set clock divider |
567 | UBRR0H = (uint8_t)(ubrr >> 8); |
567 | UBRR0H = (uint8_t)(ubrr >> 8); |
568 | UBRR0L = (uint8_t)ubrr; |
568 | UBRR0L = (uint8_t)ubrr; |
569 | 569 | ||
570 | Debug_Timer = SetDelay(DebugDataIntervall); |
570 | Debug_Timer = SetDelay(DebugDataIntervall); |
571 | Kompass_Timer = SetDelay(220); |
571 | Kompass_Timer = SetDelay(220); |
572 | 572 | ||
573 | VersionInfo.SWMajor = VERSION_MAJOR; |
573 | VersionInfo.SWMajor = VERSION_MAJOR; |
574 | VersionInfo.SWMinor = VERSION_MINOR; |
574 | VersionInfo.SWMinor = VERSION_MINOR; |
575 | VersionInfo.SWPatch = VERSION_PATCH; |
575 | VersionInfo.SWPatch = VERSION_PATCH; |
576 | VersionInfo.ProtoMajor = VERSION_SERIAL_MAJOR; |
576 | VersionInfo.ProtoMajor = VERSION_SERIAL_MAJOR; |
577 | VersionInfo.ProtoMinor = VERSION_SERIAL_MINOR; |
577 | VersionInfo.ProtoMinor = VERSION_SERIAL_MINOR; |
578 | 578 | ||
579 | pRxData = 0; |
579 | pRxData = 0; |
580 | RxDataLen = 0; |
580 | RxDataLen = 0; |
581 | } |
581 | } |
582 | 582 | ||
583 | //--------------------------------------------------------------------------------------------- |
583 | //--------------------------------------------------------------------------------------------- |
584 | void DatenUebertragung(void) |
584 | void DatenUebertragung(void) |
585 | { |
585 | { |
586 | if(!UebertragungAbgeschlossen) return; |
586 | if(!UebertragungAbgeschlossen) return; |
587 | 587 | ||
588 | if(CheckDelay(AboTimeOut)) |
588 | if(CheckDelay(AboTimeOut)) |
589 | { |
589 | { |
590 | Display_Interval = 0; |
590 | Display_Interval = 0; |
591 | DebugDataIntervall = 0; |
591 | DebugDataIntervall = 0; |
592 | Intervall3D = 0; |
592 | Intervall3D = 0; |
593 | } |
593 | } |
594 | 594 | ||
595 | if(((Display_Interval>0 && CheckDelay(Display_Timer)) || DebugDisplayAnforderung) && UebertragungAbgeschlossen) |
595 | if(((Display_Interval>0 && CheckDelay(Display_Timer)) || DebugDisplayAnforderung) && UebertragungAbgeschlossen) |
596 | { |
596 | { |
597 | if(DisplayLine > 3)// new format |
597 | if(DisplayLine > 3)// new format |
598 | { |
598 | { |
599 | Menu(); |
599 | Menu(); |
600 | SendOutData('H', FC_ADDRESS, 1, (uint8_t *)DisplayBuff, 80); |
600 | SendOutData('H', FC_ADDRESS, 1, (uint8_t *)DisplayBuff, 80); |
601 | } |
601 | } |
602 | else // old format |
602 | else // old format |
603 | { |
603 | { |
604 | LCD_printfxy(0,0,"!!! INCOMPATIBLE !!!"); |
604 | LCD_printfxy(0,0,"!!! INCOMPATIBLE !!!"); |
605 | SendOutData('H', FC_ADDRESS, 2, &DisplayLine, sizeof(DisplayLine), (uint8_t *)DisplayBuff, 20); |
605 | SendOutData('H', FC_ADDRESS, 2, &DisplayLine, sizeof(DisplayLine), (uint8_t *)DisplayBuff, 20); |
606 | if(DisplayLine++ > 3) DisplayLine = 0; |
606 | if(DisplayLine++ > 3) DisplayLine = 0; |
607 | } |
607 | } |
608 | Display_Timer = SetDelay(Display_Interval); |
608 | Display_Timer = SetDelay(Display_Interval); |
609 | DebugDisplayAnforderung = 0; |
609 | DebugDisplayAnforderung = 0; |
610 | } |
610 | } |
611 | if(DebugDisplayAnforderung1 && UebertragungAbgeschlossen) |
611 | if(DebugDisplayAnforderung1 && UebertragungAbgeschlossen) |
612 | { |
612 | { |
613 | Menu(); |
613 | Menu(); |
614 | SendOutData('L', FC_ADDRESS, 3, &MenuePunkt, sizeof(MenuePunkt), &MaxMenue, sizeof(MaxMenue), DisplayBuff, sizeof(DisplayBuff)); |
614 | SendOutData('L', FC_ADDRESS, 3, &MenuePunkt, sizeof(MenuePunkt), &MaxMenue, sizeof(MaxMenue), DisplayBuff, sizeof(DisplayBuff)); |
615 | DebugDisplayAnforderung1 = 0; |
615 | DebugDisplayAnforderung1 = 0; |
616 | } |
616 | } |
617 | if(GetVersionAnforderung && UebertragungAbgeschlossen) |
617 | if(GetVersionAnforderung && UebertragungAbgeschlossen) |
618 | { |
618 | { |
619 | SendOutData('V', FC_ADDRESS, 1, (unsigned char *) &VersionInfo, sizeof(VersionInfo)); |
619 | SendOutData('V', FC_ADDRESS, 1, (unsigned char *) &VersionInfo, sizeof(VersionInfo)); |
620 | GetVersionAnforderung = 0; |
620 | GetVersionAnforderung = 0; |
621 | Debug_OK("Version gesendet"); |
621 | Debug_OK("Version gesendet"); |
622 | } |
622 | } |
623 | 623 | ||
624 | if(GetExternalControl && UebertragungAbgeschlossen) // Bei Get werden die vom PC einstellbaren Werte vom PC zurückgelesen |
624 | if(GetExternalControl && UebertragungAbgeschlossen) // Bei Get werden die vom PC einstellbaren Werte vom PC zurückgelesen |
625 | { |
625 | { |
626 | SendOutData('G',MeineSlaveAdresse, 1, (unsigned char *) &ExternControl, sizeof(ExternControl)); |
626 | SendOutData('G',MeineSlaveAdresse, 1, (unsigned char *) &ExternControl, sizeof(ExternControl)); |
627 | GetExternalControl = 0; |
627 | GetExternalControl = 0; |
628 | } |
628 | } |
629 | if((CheckDelay(Kompass_Timer)) && UebertragungAbgeschlossen) |
629 | if((CheckDelay(Kompass_Timer)) && UebertragungAbgeschlossen) |
630 | { |
630 | { |
631 | WinkelOut.Winkel[0] = (int) (IntegralNick / (EE_Parameter.GyroAccFaktor * 4)); // etwa in 0.1 Grad |
631 | WinkelOut.Winkel[0] = (int) (IntegralNick / (EE_Parameter.GyroAccFaktor * 4)); // etwa in 0.1 Grad |
632 | WinkelOut.Winkel[1] = (int) (IntegralRoll / (EE_Parameter.GyroAccFaktor * 4)); // etwa in 0.1 Grad |
632 | WinkelOut.Winkel[1] = (int) (IntegralRoll / (EE_Parameter.GyroAccFaktor * 4)); // etwa in 0.1 Grad |
633 | WinkelOut.UserParameter[0] = Parameter_UserParam1; |
633 | WinkelOut.UserParameter[0] = Parameter_UserParam1; |
634 | WinkelOut.UserParameter[1] = Parameter_UserParam2; |
634 | WinkelOut.UserParameter[1] = Parameter_UserParam2; |
635 | SendOutData('k', MK3MAG_ADDRESS, 1, (unsigned char *) &WinkelOut,sizeof(WinkelOut)); |
635 | SendOutData('k', MK3MAG_ADDRESS, 1, (unsigned char *) &WinkelOut,sizeof(WinkelOut)); |
636 | if(WinkelOut.CalcState > 4) WinkelOut.CalcState = 6; // wird dann in SPI auf Null gesetzt |
636 | if(WinkelOut.CalcState > 4) WinkelOut.CalcState = 6; // wird dann in SPI auf Null gesetzt |
637 | Kompass_Timer = SetDelay(99); |
637 | Kompass_Timer = SetDelay(99); |
638 | } |
638 | } |
639 | if(((DebugDataIntervall>0 && CheckDelay(Debug_Timer)) || DebugDataAnforderung) && UebertragungAbgeschlossen) |
639 | if(((DebugDataIntervall>0 && CheckDelay(Debug_Timer)) || DebugDataAnforderung) && UebertragungAbgeschlossen) |
640 | { |
640 | { |
641 | CopyDebugValues(); |
641 | CopyDebugValues(); |
642 | SendOutData('D', FC_ADDRESS, 1, (unsigned char *) &DebugOut,sizeof(DebugOut)); |
642 | SendOutData('D', FC_ADDRESS, 1, (unsigned char *) &DebugOut,sizeof(DebugOut)); |
643 | DebugDataAnforderung = 0; |
643 | DebugDataAnforderung = 0; |
644 | if(DebugDataIntervall>0) Debug_Timer = SetDelay(DebugDataIntervall); |
644 | if(DebugDataIntervall>0) Debug_Timer = SetDelay(DebugDataIntervall); |
645 | } |
645 | } |
646 | if(Intervall3D > 0 && CheckDelay(Timer3D) && UebertragungAbgeschlossen) |
646 | if(Intervall3D > 0 && CheckDelay(Timer3D) && UebertragungAbgeschlossen) |
647 | { |
647 | { |
648 | Data3D.Winkel[0] = (int) (IntegralNick / (EE_Parameter.GyroAccFaktor * 4)); // etwa in 0.1 Grad |
648 | Data3D.Winkel[0] = (int) (IntegralNick / (EE_Parameter.GyroAccFaktor * 4)); // etwa in 0.1 Grad |
649 | Data3D.Winkel[1] = (int) (IntegralRoll / (EE_Parameter.GyroAccFaktor * 4)); // etwa in 0.1 Grad |
649 | Data3D.Winkel[1] = (int) (IntegralRoll / (EE_Parameter.GyroAccFaktor * 4)); // etwa in 0.1 Grad |
650 | Data3D.Winkel[2] = (int) ((10 * ErsatzKompass) / GIER_GRAD_FAKTOR); |
650 | Data3D.Winkel[2] = (int) ((10 * ErsatzKompass) / GIER_GRAD_FAKTOR); |
651 | SendOutData('C', FC_ADDRESS, 1, (unsigned char *) &Data3D,sizeof(Data3D)); |
651 | SendOutData('C', FC_ADDRESS, 1, (unsigned char *) &Data3D,sizeof(Data3D)); |
652 | Timer3D = SetDelay(Intervall3D); |
652 | Timer3D = SetDelay(Intervall3D); |
653 | } |
653 | } |
654 | if(DebugTextAnforderung != 255) // Texte für die Analogdaten |
654 | if(DebugTextAnforderung != 255) // Texte für die Analogdaten |
655 | { |
655 | { |
656 | unsigned char label[16]; // local sram buffer |
656 | unsigned char label[16]; // local sram buffer |
657 | memcpy_P(label, ANALOG_TEXT[DebugTextAnforderung], 16); // read lable from flash to sra |
657 | memcpy_P(label, ANALOG_TEXT[DebugTextAnforderung], 16); // read lable from flash to sra |
658 | SendOutData('A', FC_ADDRESS, 2, (unsigned char *)&DebugTextAnforderung, sizeof(DebugTextAnforderung),label, 16); |
658 | SendOutData('A', FC_ADDRESS, 2, (unsigned char *)&DebugTextAnforderung, sizeof(DebugTextAnforderung),label, 16); |
659 | DebugTextAnforderung = 255; |
659 | DebugTextAnforderung = 255; |
660 | } |
660 | } |
661 | if(ConfirmFrame && UebertragungAbgeschlossen) // Datensatz bestätigen |
661 | if(ConfirmFrame && UebertragungAbgeschlossen) // Datensatz bestätigen |
662 | { |
662 | { |
663 | SendOutData('B', FC_ADDRESS, 1, (uint8_t*)&ConfirmFrame, sizeof(ConfirmFrame)); |
663 | SendOutData('B', FC_ADDRESS, 1, (uint8_t*)&ConfirmFrame, sizeof(ConfirmFrame)); |
664 | ConfirmFrame = 0; |
664 | ConfirmFrame = 0; |
665 | } |
665 | } |
666 | 666 | ||
667 | if(GetPPMChannelAnforderung && UebertragungAbgeschlossen) |
667 | if(GetPPMChannelAnforderung && UebertragungAbgeschlossen) |
668 | { |
668 | { |
669 | SendOutData('P', FC_ADDRESS, 1, (unsigned char *) &PPM_in, sizeof(PPM_in)); |
669 | SendOutData('P', FC_ADDRESS, 1, (unsigned char *) &PPM_in, sizeof(PPM_in)); |
670 | GetPPMChannelAnforderung = 0; |
670 | GetPPMChannelAnforderung = 0; |
671 | } |
671 | } |
672 | 672 | ||
673 | #ifdef DEBUG // only include functions if DEBUG is defined |
673 | #ifdef DEBUG // only include functions if DEBUG is defined |
674 | if(SendDebugOutput && UebertragungAbgeschlossen) |
674 | if(SendDebugOutput && UebertragungAbgeschlossen) |
675 | { |
675 | { |
676 | SendOutData('0', FC_ADDRESS, 1, (unsigned char *) &tDebug, sizeof(tDebug)); |
676 | SendOutData('0', FC_ADDRESS, 1, (unsigned char *) &tDebug, sizeof(tDebug)); |
677 | SendDebugOutput = 0; |
677 | SendDebugOutput = 0; |
678 | } |
678 | } |
679 | #endif |
679 | #endif |
680 | 680 | ||
681 | } |
681 | } |
682 | 682 | ||
683 | 683 | ||
684 | 684 |