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