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