Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1755 | - | 1 | // ######################## SPI - FlightCtrl ################### |
2 | #include "main.h" |
||
3 | #include "eeprom.h" |
||
4 | |||
5 | |||
6 | //struct str_ToNaviCtrl_Version ToNaviCtrl_Version; |
||
7 | //struct str_FromNaviCtrl_Version FromNaviCtrl_Version; |
||
8 | struct str_ToNaviCtrl ToNaviCtrl; |
||
9 | struct str_FromNaviCtrl FromNaviCtrl; |
||
10 | struct str_FromNaviCtrl_Value FromNaviCtrl_Value; |
||
11 | struct str_SPI_VersionInfo NC_Version; |
||
12 | struct str_GPSInfo GPSInfo; |
||
13 | |||
14 | unsigned char SPI_BufferIndex; |
||
15 | unsigned char SPI_RxBufferIndex; |
||
16 | signed char FromNC_Rotate_C = 32, FromNC_Rotate_S = 0; |
||
17 | |||
18 | volatile unsigned char SPI_Buffer[sizeof(FromNaviCtrl)]; |
||
19 | unsigned char *SPI_TX_Buffer; |
||
20 | |||
21 | unsigned char SPITransferCompleted, SPI_ChkSum; |
||
22 | unsigned char SPI_RxDataValid,NaviDataOkay = 250; |
||
23 | |||
24 | unsigned char SPI_CommandSequence[] = {SPI_FCCMD_STICK, SPI_FCCMD_USER, SPI_FCCMD_PARAMETER1, SPI_FCCMD_STICK, SPI_FCCMD_MISC, SPI_FCCMD_VERSION, SPI_FCCMD_STICK, SPI_FCCMD_SERVOS, SPI_FCCMD_ACCU}; |
||
25 | unsigned char SPI_CommandCounter = 0; |
||
26 | unsigned char NC_ErrorCode = 0; |
||
27 | |||
28 | #ifdef USE_SPI_COMMUNICATION |
||
29 | |||
30 | //------------------------------------------------------ |
||
31 | void SPI_MasterInit(void) |
||
32 | { |
||
33 | DDR_SPI |= (1<<DD_MOSI)|(1<<DD_SCK); // Set MOSI and SCK output, all others input |
||
34 | SLAVE_SELECT_DDR_PORT |= (1 << SPI_SLAVE_SELECT); |
||
35 | |||
36 | SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1)|(0<<SPR0)|(0<<SPIE); // Enable SPI, Master, set clock rate fck/64 |
||
37 | SPSR = 0;//(1<<SPI2X); |
||
38 | |||
39 | SLAVE_SELECT_PORT |= (1 << SPI_SLAVE_SELECT); |
||
40 | SPITransferCompleted = 1; |
||
41 | |||
42 | //SPDR = 0x00; // dummy write |
||
43 | |||
44 | ToNaviCtrl.Sync1 = 0xAA; |
||
45 | ToNaviCtrl.Sync2 = 0x83; |
||
46 | |||
47 | ToNaviCtrl.Command = SPI_FCCMD_USER; |
||
48 | ToNaviCtrl.IntegralNick = 0; |
||
49 | ToNaviCtrl.IntegralRoll = 0; |
||
50 | FromNaviCtrl_Value.SerialDataOkay = 0; |
||
51 | SPI_RxDataValid = 0; |
||
52 | |||
53 | } |
||
54 | |||
55 | //------------------------------------------------------ |
||
56 | void SPI_StartTransmitPacket(void) |
||
57 | { |
||
58 | //if ((SLAVE_SELECT_PORT & (1 << SPI_SLAVE_SELECT)) == 0) return; // transfer of prev. packet not completed |
||
59 | if (!SPITransferCompleted) return; |
||
60 | // _delay_us(30); |
||
61 | |||
62 | SLAVE_SELECT_PORT &= ~(1 << SPI_SLAVE_SELECT); // SelectSlave |
||
63 | SPI_TX_Buffer = (unsigned char *) &ToNaviCtrl; |
||
64 | |||
65 | ToNaviCtrl.Command = SPI_CommandSequence[SPI_CommandCounter++]; |
||
66 | if (SPI_CommandCounter >= sizeof(SPI_CommandSequence)) SPI_CommandCounter = 0; |
||
67 | |||
68 | SPITransferCompleted = 0; |
||
69 | UpdateSPI_Buffer(); // update buffer |
||
70 | |||
71 | SPI_BufferIndex = 1; |
||
72 | //ebugOut.Analog[16]++; |
||
73 | // -- Debug-Output --- |
||
74 | //---- |
||
75 | asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); |
||
76 | asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); |
||
77 | asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); |
||
78 | ToNaviCtrl.Chksum = ToNaviCtrl.Sync1; |
||
79 | SPDR = ToNaviCtrl.Sync1; // Start transmission |
||
80 | // SLAVE_SELECT_PORT |= (1 << SPI_SLAVE_SELECT); // DeselectSlave |
||
81 | |||
82 | } |
||
83 | |||
84 | //------------------------------------------------------ |
||
85 | //SIGNAL(SIG_SPI) |
||
86 | void SPI_TransmitByte(void) |
||
87 | { |
||
88 | static unsigned char SPI_RXState = 0; |
||
89 | unsigned char rxdata; |
||
90 | static unsigned char rxchksum; |
||
91 | |||
92 | if (SPITransferCompleted) return; |
||
93 | if (!(SPSR & (1 << SPIF))) return; |
||
94 | SendSPI = 4; |
||
95 | |||
96 | // _delay_us(30); |
||
97 | SLAVE_SELECT_PORT |= (1 << SPI_SLAVE_SELECT); // DeselectSlave |
||
98 | |||
99 | rxdata = SPDR; |
||
100 | switch ( SPI_RXState) |
||
101 | { |
||
102 | case 0: |
||
103 | |||
104 | SPI_RxBufferIndex = 0; |
||
105 | rxchksum = rxdata; |
||
106 | if (rxdata == 0x81 ) { SPI_RXState = 1; } // 1. Syncbyte ok |
||
107 | |||
108 | break; |
||
109 | |||
110 | case 1: |
||
111 | if (rxdata == 0x55) { rxchksum += rxdata; SPI_RXState = 2; } // 2. Syncbyte ok |
||
112 | else SPI_RXState = 0; |
||
113 | break; |
||
114 | |||
115 | case 2: |
||
116 | SPI_Buffer[SPI_RxBufferIndex++]= rxdata; // get data |
||
117 | //DebugOut.Analog[19]++; |
||
118 | if (SPI_RxBufferIndex >= sizeof(FromNaviCtrl)) |
||
119 | { |
||
120 | |||
121 | if (rxdata == rxchksum) |
||
122 | { |
||
123 | unsigned char *ptr = (unsigned char *)&FromNaviCtrl; |
||
124 | |||
125 | memcpy(ptr, (unsigned char *) SPI_Buffer, sizeof(SPI_Buffer)); |
||
126 | |||
127 | SPI_RxDataValid = 1; |
||
128 | } |
||
129 | else SPI_RxDataValid = 0; |
||
130 | |||
131 | SPI_RXState = 0; |
||
132 | } |
||
133 | else rxchksum += rxdata; |
||
134 | break; |
||
135 | |||
136 | } |
||
137 | |||
138 | if (SPI_BufferIndex < sizeof(ToNaviCtrl)) |
||
139 | { |
||
140 | SLAVE_SELECT_PORT &= ~(1 << SPI_SLAVE_SELECT); // SelectSlave |
||
141 | asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); |
||
142 | asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); |
||
143 | asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); |
||
144 | |||
145 | SPDR = SPI_TX_Buffer[SPI_BufferIndex]; |
||
146 | ToNaviCtrl.Chksum += SPI_TX_Buffer[SPI_BufferIndex]; |
||
147 | // SLAVE_SELECT_PORT |= (1 << SPI_SLAVE_SELECT); // DeselectSlave |
||
148 | |||
149 | } |
||
150 | else SPITransferCompleted = 1; |
||
151 | |||
152 | SPI_BufferIndex++; |
||
153 | } |
||
154 | |||
155 | |||
156 | //------------------------------------------------------ |
||
157 | void UpdateSPI_Buffer(void) |
||
158 | { |
||
159 | signed int tmp; |
||
160 | ToNaviCtrl.IntegralNick = (int) (IntegralNick / (long)(EE_Parameter.GyroAccFaktor * 4)); |
||
161 | ToNaviCtrl.IntegralRoll = (int) (IntegralRoll / (long)(EE_Parameter.GyroAccFaktor * 4)); |
||
162 | ToNaviCtrl.GyroCompass = (10 * ErsatzKompass) / GIER_GRAD_FAKTOR; |
||
163 | ToNaviCtrl.GyroGier = (signed int) AdNeutralGier - AdWertGier; |
||
164 | ToNaviCtrl.AccNick = ((int) ACC_AMPLIFY * (NaviAccNick / NaviCntAcc))/4; |
||
165 | ToNaviCtrl.AccRoll = ((int) ACC_AMPLIFY * (NaviAccRoll / NaviCntAcc))/4; |
||
166 | NaviCntAcc = 0; NaviAccNick = 0; NaviAccRoll = 0; |
||
167 | // ToNaviCtrl.User8 = Parameter_UserParam8; |
||
168 | // ToNaviCtrl.CalState = WinkelOut.CalcState; |
||
169 | switch(ToNaviCtrl.Command) // |
||
170 | { |
||
171 | case SPI_FCCMD_USER: |
||
172 | ToNaviCtrl.Param.Byte[0] = Parameter_UserParam1; |
||
173 | ToNaviCtrl.Param.Byte[1] = Parameter_UserParam2; |
||
174 | ToNaviCtrl.Param.Byte[2] = Parameter_UserParam3; |
||
175 | ToNaviCtrl.Param.Byte[3] = Parameter_UserParam4; |
||
176 | ToNaviCtrl.Param.Byte[4] = Parameter_UserParam5; |
||
177 | ToNaviCtrl.Param.Byte[5] = Parameter_UserParam6; |
||
178 | ToNaviCtrl.Param.Byte[6] = Parameter_UserParam7; |
||
179 | ToNaviCtrl.Param.Byte[7] = Parameter_UserParam8; |
||
180 | ToNaviCtrl.Param.Byte[8] = (unsigned char) FCFlags; |
||
181 | FCFlags &= ~(FCFLAG_CALIBRATE | FCFLAG_START); |
||
182 | ToNaviCtrl.Param.Byte[9] = GetActiveParamSet(); |
||
183 | ToNaviCtrl.Param.Byte[10] = ControlHeading; |
||
184 | break; |
||
185 | |||
186 | case SPI_FCCMD_ACCU: |
||
187 | ToNaviCtrl.Param.Int[0] = Capacity.ActualCurrent; // 0.1A |
||
188 | ToNaviCtrl.Param.Int[1] = Capacity.UsedCapacity; // mAh |
||
189 | ToNaviCtrl.Param.Byte[4] = (unsigned char) UBat; // 0.1V |
||
190 | ToNaviCtrl.Param.Byte[5] = (unsigned char) BattLowVoltageWarning; //0.1V |
||
191 | break; |
||
192 | |||
193 | case SPI_FCCMD_PARAMETER1: |
||
194 | ToNaviCtrl.Param.Byte[0] = EE_Parameter.NaviGpsModeControl; // Parameters for the Naviboard |
||
195 | ToNaviCtrl.Param.Byte[1] = EE_Parameter.NaviGpsGain; |
||
196 | ToNaviCtrl.Param.Byte[2] = EE_Parameter.NaviGpsP; |
||
197 | ToNaviCtrl.Param.Byte[3] = EE_Parameter.NaviGpsI; |
||
198 | ToNaviCtrl.Param.Byte[4] = EE_Parameter.NaviGpsD; |
||
199 | ToNaviCtrl.Param.Byte[5] = EE_Parameter.NaviGpsACC; |
||
200 | ToNaviCtrl.Param.Byte[6] = EE_Parameter.NaviGpsMinSat; |
||
201 | ToNaviCtrl.Param.Byte[7] = EE_Parameter.NaviStickThreshold; |
||
202 | ToNaviCtrl.Param.Byte[8] = EE_Parameter.NaviOperatingRadius; |
||
203 | ToNaviCtrl.Param.Byte[9] = EE_Parameter.NaviWindCorrection; |
||
204 | ToNaviCtrl.Param.Byte[10] = EE_Parameter.NaviSpeedCompensation; |
||
205 | ToNaviCtrl.Param.Byte[11] = EE_Parameter.NaviAngleLimitation; |
||
206 | break; |
||
207 | |||
208 | case SPI_FCCMD_STICK: |
||
209 | cli(); |
||
210 | tmp = PPM_in[EE_Parameter.Kanalbelegung[K_GAS]]; if(tmp > 127) tmp = 127; else if(tmp < -127) tmp = -127; |
||
211 | ToNaviCtrl.Param.Byte[0] = (char) tmp; |
||
212 | tmp = PPM_in[EE_Parameter.Kanalbelegung[K_GIER]]; if(tmp > 127) tmp = 127; else if(tmp < -127) tmp = -127; |
||
213 | ToNaviCtrl.Param.Byte[1] = (char) tmp; |
||
214 | tmp = PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]]; if(tmp > 127) tmp = 127; else if(tmp < -127) tmp = -127; |
||
215 | ToNaviCtrl.Param.Byte[2] = (char) tmp; |
||
216 | tmp = PPM_in[EE_Parameter.Kanalbelegung[K_NICK]]; if(tmp > 127) tmp = 127; else if(tmp < -127) tmp = -127; |
||
217 | sei(); |
||
218 | ToNaviCtrl.Param.Byte[3] = (char) tmp; |
||
219 | ToNaviCtrl.Param.Byte[4] = (unsigned char) Poti[0]; |
||
220 | ToNaviCtrl.Param.Byte[5] = (unsigned char) Poti[1]; |
||
221 | ToNaviCtrl.Param.Byte[6] = (unsigned char) Poti[2]; |
||
222 | ToNaviCtrl.Param.Byte[7] = (unsigned char) Poti[3]; |
||
223 | ToNaviCtrl.Param.Byte[8] = (unsigned char) Poti[4]; |
||
224 | ToNaviCtrl.Param.Byte[9] = (unsigned char) Poti[5]; |
||
225 | ToNaviCtrl.Param.Byte[10] = (unsigned char) Poti[6]; |
||
226 | ToNaviCtrl.Param.Byte[11] = (unsigned char) Poti[7]; |
||
227 | break; |
||
228 | case SPI_FCCMD_MISC: |
||
229 | if(WinkelOut.CalcState > 5) |
||
230 | { |
||
231 | WinkelOut.CalcState = 0; |
||
232 | ToNaviCtrl.Param.Byte[0] = 5; |
||
233 | } |
||
234 | else ToNaviCtrl.Param.Byte[0] = WinkelOut.CalcState; |
||
235 | ToNaviCtrl.Param.Byte[1] = EE_Parameter.NaviPH_LoginTime; |
||
236 | ToNaviCtrl.Param.Int[1] = (int)(HoehenWert/5); |
||
237 | ToNaviCtrl.Param.Int[2] = (int)(SollHoehe/5); |
||
238 | ToNaviCtrl.Param.Byte[6] = EE_Parameter.NaviGpsPLimit; |
||
239 | ToNaviCtrl.Param.Byte[7] = EE_Parameter.NaviGpsILimit; |
||
240 | ToNaviCtrl.Param.Byte[8] = EE_Parameter.NaviGpsDLimit; |
||
241 | ToNaviCtrl.Param.Byte[9] = (unsigned char) SenderOkay; |
||
242 | ToNaviCtrl.Param.Byte[10] = (unsigned char) PPM_in[0]; |
||
243 | ToNaviCtrl.Param.Byte[11] = DebugOut.Analog[7] / 4; //GasMischanteil |
||
244 | break; |
||
245 | case SPI_FCCMD_VERSION: |
||
246 | ToNaviCtrl.Param.Byte[0] = VERSION_MAJOR; |
||
247 | ToNaviCtrl.Param.Byte[1] = VERSION_MINOR; |
||
248 | ToNaviCtrl.Param.Byte[2] = VERSION_PATCH; |
||
249 | ToNaviCtrl.Param.Byte[3] = NC_SPI_COMPATIBLE; |
||
250 | ToNaviCtrl.Param.Byte[4] = PlatinenVersion; |
||
251 | ToNaviCtrl.Param.Byte[5] = VersionInfo.HardwareError[0]; |
||
252 | ToNaviCtrl.Param.Byte[6] = VersionInfo.HardwareError[1]; |
||
253 | ToNaviCtrl.Param.Byte[7] = VersionInfo.HardwareError[2]; |
||
254 | break; |
||
255 | case SPI_FCCMD_SERVOS: |
||
256 | ToNaviCtrl.Param.Byte[0] = EE_Parameter.ServoNickRefresh; // Parameters for the Servo Control |
||
257 | ToNaviCtrl.Param.Byte[1] = EE_Parameter.ServoCompInvert; |
||
258 | ToNaviCtrl.Param.Byte[2] = Parameter_ServoNickControl; |
||
259 | ToNaviCtrl.Param.Byte[3] = EE_Parameter.ServoNickComp; |
||
260 | ToNaviCtrl.Param.Byte[4] = EE_Parameter.ServoNickMin; |
||
261 | ToNaviCtrl.Param.Byte[5] = EE_Parameter.ServoNickMax; |
||
262 | ToNaviCtrl.Param.Byte[6] = Parameter_ServoRollControl; |
||
263 | ToNaviCtrl.Param.Byte[7] = EE_Parameter.ServoRollComp; |
||
264 | ToNaviCtrl.Param.Byte[8] = EE_Parameter.ServoRollMin; |
||
265 | ToNaviCtrl.Param.Byte[9] = EE_Parameter.ServoRollMax; |
||
266 | break; |
||
267 | } |
||
268 | |||
269 | if(SPI_RxDataValid) |
||
270 | { |
||
271 | NaviDataOkay = 250; |
||
272 | if(abs(FromNaviCtrl.GPS_Nick) < 512 && abs(FromNaviCtrl.GPS_Roll) < 512 && (EE_Parameter.GlobalConfig & CFG_GPS_AKTIV)) |
||
273 | { |
||
274 | GPS_Nick = FromNaviCtrl.GPS_Nick; |
||
275 | GPS_Roll = FromNaviCtrl.GPS_Roll; |
||
276 | } |
||
277 | |||
278 | if(FromNaviCtrl.CompassValue <= 360) KompassValue = FromNaviCtrl.CompassValue; |
||
279 | KompassRichtung = ((540 + KompassValue - KompassStartwert) % 360) - 180; |
||
280 | |||
281 | if(FromNaviCtrl.BeepTime > beeptime && !WinkelOut.CalcState) beeptime = FromNaviCtrl.BeepTime; |
||
282 | |||
283 | switch (FromNaviCtrl.Command) |
||
284 | { |
||
285 | case SPI_NCCMD_KALMAN: |
||
286 | FromNaviCtrl_Value.Kalman_K = FromNaviCtrl.Param.sByte[0]; |
||
287 | FromNaviCtrl_Value.Kalman_MaxFusion = FromNaviCtrl.Param.sByte[1]; |
||
288 | FromNaviCtrl_Value.Kalman_MaxDrift = FromNaviCtrl.Param.sByte[2]; |
||
289 | FromNaviCtrl_Value.SerialDataOkay = FromNaviCtrl.Param.Byte[3]; |
||
290 | FromNaviCtrl_Value.GpsZ = FromNaviCtrl.Param.Byte[4]; |
||
291 | FromNC_Rotate_C = FromNaviCtrl.Param.Byte[5]; |
||
292 | FromNC_Rotate_S = FromNaviCtrl.Param.Byte[6]; |
||
293 | break; |
||
294 | |||
295 | case SPI_NCCMD_VERSION: |
||
296 | NC_Version.Major = FromNaviCtrl.Param.Byte[0]; |
||
297 | NC_Version.Minor = FromNaviCtrl.Param.Byte[1]; |
||
298 | NC_Version.Patch = FromNaviCtrl.Param.Byte[2]; |
||
299 | NC_Version.Compatible = FromNaviCtrl.Param.Byte[3]; |
||
300 | NC_Version.Hardware = FromNaviCtrl.Param.Byte[4]; |
||
301 | DebugOut.Status[0] |= FromNaviCtrl.Param.Byte[5]; |
||
302 | NC_ErrorCode = FromNaviCtrl.Param.Byte[6]; |
||
303 | DebugOut.Status[1] = (DebugOut.Status[1] & (0x01|0x02)) | (FromNaviCtrl.Param.Byte[6] & (0x04 | 0x08)); |
||
304 | break; |
||
305 | |||
306 | case SPI_NCCMD_GPSINFO: |
||
307 | GPSInfo.Flags = FromNaviCtrl.Param.Byte[0]; |
||
308 | GPSInfo.NumOfSats = FromNaviCtrl.Param.Byte[1]; |
||
309 | GPSInfo.SatFix = FromNaviCtrl.Param.Byte[2]; |
||
310 | GPSInfo.Speed = FromNaviCtrl.Param.Byte[3]; |
||
311 | GPSInfo.HomeDistance = FromNaviCtrl.Param.Int[2]; |
||
312 | GPSInfo.HomeBearing = FromNaviCtrl.Param.sInt[3]; |
||
313 | break; |
||
314 | |||
315 | default: |
||
316 | break; |
||
317 | } |
||
318 | } |
||
319 | else |
||
320 | { |
||
321 | // KompassValue = 0; |
||
322 | // KompassRichtung = 0; |
||
323 | GPS_Nick = 0; |
||
324 | GPS_Roll = 0; |
||
325 | } |
||
326 | } |
||
327 | |||
328 | #endif |
||
329 | |||
330 |