Rev 1226 | 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; |
1051 | killagreg | 10 | struct str_SPI_VersionInfo SPI_VersionInfo; |
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 | |
1036 | hbuss | 21 | unsigned char SPI_CommandSequence[] = { SPI_CMD_USER, SPI_CMD_STICK, SPI_CMD_PARAMETER1, SPI_CMD_STICK, SPI_CMD_MISC, SPI_CMD_VERSION }; |
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 | |
823 | ingob | 43 | ToNaviCtrl.Command = SPI_CMD_USER; |
44 | ToNaviCtrl.IntegralNick = 0; |
||
45 | ToNaviCtrl.IntegralRoll = 0; |
||
1215 | hbuss | 46 | FromNaviCtrl_Value.SerialDataOkay = 0; |
1051 | killagreg | 47 | SPI_RxDataValid = 0; |
48 | |||
49 | SPI_VersionInfo.Major = VERSION_MAJOR; |
||
50 | SPI_VersionInfo.Minor = VERSION_MINOR; |
||
51 | SPI_VersionInfo.Patch = VERSION_PATCH; |
||
52 | SPI_VersionInfo.Compatible = NC_SPI_COMPATIBLE; |
||
304 | ingob | 53 | } |
54 | |||
55 | //------------------------------------------------------ |
||
823 | ingob | 56 | void SPI_StartTransmitPacket(void) |
304 | ingob | 57 | { |
606 | ingob | 58 | //if ((SLAVE_SELECT_PORT & (1 << SPI_SLAVE_SELECT)) == 0) return; // transfer of prev. packet not completed |
59 | if (!SPITransferCompleted) return; |
||
1051 | killagreg | 60 | // _delay_us(30); |
61 | |||
304 | ingob | 62 | SLAVE_SELECT_PORT &= ~(1 << SPI_SLAVE_SELECT); // SelectSlave |
823 | ingob | 63 | SPI_TX_Buffer = (unsigned char *) &ToNaviCtrl; |
1051 | killagreg | 64 | |
823 | ingob | 65 | ToNaviCtrl.Command = SPI_CommandSequence[SPI_CommandCounter++]; |
66 | if (SPI_CommandCounter >= sizeof(SPI_CommandSequence)) SPI_CommandCounter = 0; |
||
1051 | killagreg | 67 | |
606 | ingob | 68 | SPITransferCompleted = 0; |
304 | ingob | 69 | UpdateSPI_Buffer(); // update buffer |
823 | ingob | 70 | |
304 | ingob | 71 | SPI_BufferIndex = 1; |
1051 | killagreg | 72 | //ebugOut.Analog[16]++; |
304 | ingob | 73 | // -- Debug-Output --- |
74 | //---- |
||
691 | ingob | 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"); |
||
823 | ingob | 78 | ToNaviCtrl.Chksum = ToNaviCtrl.Sync1; |
1051 | killagreg | 79 | SPDR = ToNaviCtrl.Sync1; // Start transmission |
691 | ingob | 80 | // SLAVE_SELECT_PORT |= (1 << SPI_SLAVE_SELECT); // DeselectSlave |
81 | |||
304 | ingob | 82 | } |
83 | |||
84 | //------------------------------------------------------ |
||
85 | //SIGNAL(SIG_SPI) |
||
86 | void SPI_TransmitByte(void) |
||
87 | { |
||
708 | ingob | 88 | static unsigned char SPI_RXState = 0; |
1051 | killagreg | 89 | unsigned char rxdata; |
708 | ingob | 90 | static unsigned char rxchksum; |
1051 | killagreg | 91 | |
691 | ingob | 92 | if (SPITransferCompleted) return; |
304 | ingob | 93 | if (!(SPSR & (1 << SPIF))) return; |
1051 | killagreg | 94 | SendSPI = 4; |
95 | |||
96 | // _delay_us(30); |
||
606 | ingob | 97 | SLAVE_SELECT_PORT |= (1 << SPI_SLAVE_SELECT); // DeselectSlave |
1051 | killagreg | 98 | |
708 | ingob | 99 | rxdata = SPDR; |
100 | switch ( SPI_RXState) |
||
101 | { |
||
1051 | killagreg | 102 | case 0: |
103 | |||
104 | SPI_RxBufferIndex = 0; |
||
708 | ingob | 105 | //DebugOut.Analog[17]++; |
1051 | killagreg | 106 | rxchksum = rxdata; |
107 | if (rxdata == 0x81 ) { SPI_RXState = 1; } // 1. Syncbyte ok |
||
108 | |||
708 | ingob | 109 | break; |
110 | |||
1051 | killagreg | 111 | case 1: |
112 | if (rxdata == 0x55) { rxchksum += rxdata; SPI_RXState = 2; } // 2. Syncbyte ok |
||
113 | else SPI_RXState = 0; |
||
708 | ingob | 114 | //DebugOut.Analog[18]++; |
1051 | killagreg | 115 | break; |
116 | |||
708 | ingob | 117 | case 2: |
118 | SPI_Buffer[SPI_RxBufferIndex++]= rxdata; // get data |
||
119 | //DebugOut.Analog[19]++; |
||
1051 | killagreg | 120 | if (SPI_RxBufferIndex >= sizeof(FromNaviCtrl)) |
121 | { |
||
122 | |||
708 | ingob | 123 | if (rxdata == rxchksum) |
124 | { |
||
823 | ingob | 125 | unsigned char *ptr = (unsigned char *)&FromNaviCtrl; |
1051 | killagreg | 126 | |
708 | ingob | 127 | memcpy(ptr, (unsigned char *) SPI_Buffer, sizeof(SPI_Buffer)); |
1051 | killagreg | 128 | |
129 | SPI_RxDataValid = 1; |
||
130 | } |
||
131 | else SPI_RxDataValid = 0; |
||
132 | |||
133 | SPI_RXState = 0; |
||
708 | ingob | 134 | } |
1051 | killagreg | 135 | else rxchksum += rxdata; |
136 | break; |
||
137 | |||
138 | } |
||
139 | |||
140 | if (SPI_BufferIndex < sizeof(ToNaviCtrl)) |
||
141 | { |
||
606 | ingob | 142 | SLAVE_SELECT_PORT &= ~(1 << SPI_SLAVE_SELECT); // SelectSlave |
691 | ingob | 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 | asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); |
||
145 | 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 | 146 | |
708 | ingob | 147 | SPDR = SPI_TX_Buffer[SPI_BufferIndex]; |
823 | ingob | 148 | ToNaviCtrl.Chksum += SPI_TX_Buffer[SPI_BufferIndex]; |
691 | ingob | 149 | // SLAVE_SELECT_PORT |= (1 << SPI_SLAVE_SELECT); // DeselectSlave |
150 | |||
606 | ingob | 151 | } |
1051 | killagreg | 152 | else SPITransferCompleted = 1; |
153 | |||
304 | ingob | 154 | SPI_BufferIndex++; |
155 | } |
||
156 | |||
691 | ingob | 157 | |
304 | ingob | 158 | //------------------------------------------------------ |
159 | void UpdateSPI_Buffer(void) |
||
160 | { |
||
846 | hbuss | 161 | signed int tmp; |
1286 | MartinR | 162 | // cli(); // MartinR: Interrupt wird zu lange abgeschaltet (130us). Daher deaktiviert |
1051 | killagreg | 163 | |
1171 | hbuss | 164 | ToNaviCtrl.IntegralNick = (int) (IntegralNick / (long)(EE_Parameter.GyroAccFaktor * 4)); |
165 | ToNaviCtrl.IntegralRoll = (int) (IntegralRoll / (long)(EE_Parameter.GyroAccFaktor * 4)); |
||
166 | ToNaviCtrl.GyroCompass = (10 * ErsatzKompass) / GIER_GRAD_FAKTOR; |
||
167 | ToNaviCtrl.AccNick = ((int) ACC_AMPLIFY * (NaviAccNick / NaviCntAcc))/4; |
||
168 | ToNaviCtrl.AccRoll = ((int) ACC_AMPLIFY * (NaviAccRoll / NaviCntAcc))/4; |
||
819 | hbuss | 169 | NaviCntAcc = 0; NaviAccNick = 0; NaviAccRoll = 0; |
823 | ingob | 170 | // ToNaviCtrl.User8 = Parameter_UserParam8; |
171 | // ToNaviCtrl.CalState = WinkelOut.CalcState; |
||
172 | |||
1051 | killagreg | 173 | switch(ToNaviCtrl.Command) // |
823 | ingob | 174 | { |
1051 | killagreg | 175 | case SPI_CMD_USER: |
823 | ingob | 176 | ToNaviCtrl.Param.Byte[0] = Parameter_UserParam1; |
177 | ToNaviCtrl.Param.Byte[1] = Parameter_UserParam2; |
||
178 | ToNaviCtrl.Param.Byte[2] = Parameter_UserParam3; |
||
179 | ToNaviCtrl.Param.Byte[3] = Parameter_UserParam4; |
||
180 | ToNaviCtrl.Param.Byte[4] = Parameter_UserParam5; |
||
181 | ToNaviCtrl.Param.Byte[5] = Parameter_UserParam6; |
||
182 | ToNaviCtrl.Param.Byte[6] = Parameter_UserParam7; |
||
921 | hbuss | 183 | ToNaviCtrl.Param.Byte[7] = Parameter_UserParam8; |
184 | ToNaviCtrl.Param.Byte[8] = (unsigned char) MikroKopterFlags; |
||
185 | MikroKopterFlags &= ~(FLAG_CALIBRATE | FLAG_START); |
||
186 | ToNaviCtrl.Param.Byte[9] = (unsigned char) UBat; |
||
187 | ToNaviCtrl.Param.Byte[10] =(unsigned char) EE_Parameter.UnterspannungsWarnung; |
||
188 | ToNaviCtrl.Param.Byte[11] =(unsigned char) eeprom_read_byte(&EEPromArray[EEPROM_ADR_ACTIVE_SET]); |
||
189 | break; |
||
1051 | killagreg | 190 | case SPI_CMD_PARAMETER1: |
1286 | MartinR | 191 | //ToNaviCtrl.Param.Byte[0] = EE_Parameter.NaviGpsModeControl; // Parameters for the Naviboard // MartinR: so war es |
192 | ToNaviCtrl.Param.Byte[0] = Parameter_NaviGpsModeControl; // MartinR: wird wiederverwendet für GPS free bei HH |
||
193 | |||
1051 | killagreg | 194 | ToNaviCtrl.Param.Byte[1] = EE_Parameter.NaviGpsGain; |
195 | ToNaviCtrl.Param.Byte[2] = EE_Parameter.NaviGpsP; |
||
196 | ToNaviCtrl.Param.Byte[3] = EE_Parameter.NaviGpsI; |
||
197 | ToNaviCtrl.Param.Byte[4] = EE_Parameter.NaviGpsD; |
||
198 | ToNaviCtrl.Param.Byte[5] = EE_Parameter.NaviGpsACC; |
||
199 | ToNaviCtrl.Param.Byte[6] = EE_Parameter.NaviGpsMinSat; |
||
200 | ToNaviCtrl.Param.Byte[7] = EE_Parameter.NaviStickThreshold; |
||
993 | hbuss | 201 | ToNaviCtrl.Param.Byte[8] = EE_Parameter.NaviOperatingRadius; |
202 | ToNaviCtrl.Param.Byte[9] = EE_Parameter.NaviWindCorrection; |
||
203 | ToNaviCtrl.Param.Byte[10] = EE_Parameter.NaviSpeedCompensation; |
||
1064 | hbuss | 204 | ToNaviCtrl.Param.Byte[11] = EE_Parameter.NaviAngleLimitation; |
823 | ingob | 205 | break; |
1051 | killagreg | 206 | case SPI_CMD_STICK: |
871 | hbuss | 207 | tmp = PPM_in[EE_Parameter.Kanalbelegung[K_GAS]]; if(tmp > 127) tmp = 127; else if(tmp < -127) tmp = -127; |
846 | hbuss | 208 | ToNaviCtrl.Param.Byte[0] = (char) tmp; |
871 | hbuss | 209 | tmp = PPM_in[EE_Parameter.Kanalbelegung[K_GIER]]; if(tmp > 127) tmp = 127; else if(tmp < -127) tmp = -127; |
846 | hbuss | 210 | ToNaviCtrl.Param.Byte[1] = (char) tmp; |
871 | hbuss | 211 | tmp = PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]]; if(tmp > 127) tmp = 127; else if(tmp < -127) tmp = -127; |
846 | hbuss | 212 | ToNaviCtrl.Param.Byte[2] = (char) tmp; |
871 | hbuss | 213 | tmp = PPM_in[EE_Parameter.Kanalbelegung[K_NICK]]; if(tmp > 127) tmp = 127; else if(tmp < -127) tmp = -127; |
846 | hbuss | 214 | ToNaviCtrl.Param.Byte[3] = (char) tmp; |
215 | ToNaviCtrl.Param.Byte[4] = (unsigned char) Poti1; |
||
216 | ToNaviCtrl.Param.Byte[5] = (unsigned char) Poti2; |
||
217 | ToNaviCtrl.Param.Byte[6] = (unsigned char) Poti3; |
||
218 | ToNaviCtrl.Param.Byte[7] = (unsigned char) Poti4; |
||
855 | hbuss | 219 | ToNaviCtrl.Param.Byte[8] = (unsigned char) SenderOkay; |
823 | ingob | 220 | break; |
1036 | hbuss | 221 | case SPI_CMD_MISC: |
1051 | killagreg | 222 | if(WinkelOut.CalcState > 5) |
855 | hbuss | 223 | { |
1051 | killagreg | 224 | WinkelOut.CalcState = 0; |
225 | ToNaviCtrl.Param.Byte[0] = 5; |
||
226 | } |
||
855 | hbuss | 227 | else ToNaviCtrl.Param.Byte[0] = WinkelOut.CalcState; |
1036 | hbuss | 228 | ToNaviCtrl.Param.Int[1] = HoehenWert; |
1120 | hbuss | 229 | ToNaviCtrl.Param.Byte[1] = EE_Parameter.NaviPH_LoginTime; |
230 | ToNaviCtrl.Param.Byte[4] = EE_Parameter.NaviGpsPLimit; |
||
231 | ToNaviCtrl.Param.Byte[5] = EE_Parameter.NaviGpsILimit; |
||
232 | ToNaviCtrl.Param.Byte[6] = EE_Parameter.NaviGpsDLimit; |
||
823 | ingob | 233 | break; |
991 | ingob | 234 | |
235 | case SPI_CMD_VERSION: |
||
1051 | killagreg | 236 | ToNaviCtrl.Param.Byte[0] = SPI_VersionInfo.Major; |
237 | ToNaviCtrl.Param.Byte[1] = SPI_VersionInfo.Minor; |
||
238 | ToNaviCtrl.Param.Byte[2] = SPI_VersionInfo.Patch; |
||
239 | ToNaviCtrl.Param.Byte[3] = SPI_VersionInfo.Compatible; |
||
1171 | hbuss | 240 | ToNaviCtrl.Param.Byte[4] = PlatinenVersion; |
991 | ingob | 241 | break; |
823 | ingob | 242 | } |
1051 | killagreg | 243 | |
1286 | MartinR | 244 | //sei(); // MartinR: Interrupt wird zu lange abgeschaltet (130us). Daher deaktiviert |
1051 | killagreg | 245 | |
1215 | hbuss | 246 | if(SPI_RxDataValid) |
1051 | killagreg | 247 | { |
855 | hbuss | 248 | if(abs(FromNaviCtrl.GPS_Nick) < 512 && abs(FromNaviCtrl.GPS_Roll) < 512 && (EE_Parameter.GlobalConfig & CFG_GPS_AKTIV)) |
854 | hbuss | 249 | { |
823 | ingob | 250 | GPS_Nick = FromNaviCtrl.GPS_Nick; |
251 | GPS_Roll = FromNaviCtrl.GPS_Roll; |
||
1215 | hbuss | 252 | NaviDataOkay = 250; |
854 | hbuss | 253 | } |
254 | if(FromNaviCtrl.CompassValue <= 360) KompassValue = FromNaviCtrl.CompassValue; |
||
720 | ingob | 255 | KompassRichtung = ((540 + KompassValue - KompassStartwert) % 360) - 180; |
1051 | killagreg | 256 | |
855 | hbuss | 257 | if(FromNaviCtrl.BeepTime > beeptime && !WinkelOut.CalcState) beeptime = FromNaviCtrl.BeepTime; |
1051 | killagreg | 258 | |
823 | ingob | 259 | switch (FromNaviCtrl.Command) |
260 | { |
||
992 | hbuss | 261 | case SPI_KALMAN: |
262 | FromNaviCtrl_Value.Kalman_K = FromNaviCtrl.Param.Byte[0]; |
||
263 | FromNaviCtrl_Value.Kalman_MaxFusion = FromNaviCtrl.Param.Byte[1]; |
||
264 | FromNaviCtrl_Value.Kalman_MaxDrift = FromNaviCtrl.Param.Byte[2]; |
||
1215 | hbuss | 265 | FromNaviCtrl_Value.SerialDataOkay = FromNaviCtrl.Param.Byte[3]; |
992 | hbuss | 266 | break; |
1051 | killagreg | 267 | |
823 | ingob | 268 | default: |
269 | break; |
||
270 | } |
||
720 | ingob | 271 | } |
272 | else |
||
273 | { |
||
819 | hbuss | 274 | // KompassValue = 0; |
275 | // KompassRichtung = 0; |
||
720 | ingob | 276 | GPS_Nick = 0; |
277 | GPS_Roll = 0; |
||
278 | } |
||
304 | ingob | 279 | } |
280 | |||
597 | ingob | 281 | #endif |
304 | ingob | 282 | |
283 |