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