Go to most recent revision | Details | 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; |
||
708 | ingob | 9 | |
304 | ingob | 10 | unsigned char SPI_BufferIndex; |
708 | ingob | 11 | unsigned char SPI_RxBufferIndex; |
606 | ingob | 12 | |
823 | ingob | 13 | volatile unsigned char SPI_Buffer[sizeof(FromNaviCtrl) + 8]; |
14 | unsigned char *SPI_TX_Buffer; |
||
708 | ingob | 15 | |
617 | ingob | 16 | unsigned char SPITransferCompleted, SPI_ChkSum; |
720 | ingob | 17 | unsigned char SPI_RxDataValid; |
18 | |||
823 | ingob | 19 | unsigned char SPI_CommandSequence[] = { SPI_CMD_USER, SPI_CMD_STICK, SPI_CMD_USER, SPI_CMD_STICK, SPI_CMD_CAL_COMPASS }; |
20 | unsigned char SPI_CommandCounter = 0; |
||
21 | |||
597 | ingob | 22 | #ifdef USE_SPI_COMMUNICATION |
691 | ingob | 23 | |
304 | ingob | 24 | //------------------------------------------------------ |
25 | void SPI_MasterInit(void) |
||
26 | { |
||
27 | DDR_SPI |= (1<<DD_MOSI)|(1<<DD_SCK); // Set MOSI and SCK output, all others input |
||
28 | SLAVE_SELECT_DDR_PORT |= (1 << SPI_SLAVE_SELECT); |
||
29 | |||
691 | ingob | 30 | SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1)|(0<<SPR0)|(0<<SPIE); // Enable SPI, Master, set clock rate fck/64 |
723 | hbuss | 31 | SPSR = 0;//(1<<SPI2X); |
691 | ingob | 32 | |
304 | ingob | 33 | SLAVE_SELECT_PORT |= (1 << SPI_SLAVE_SELECT); |
606 | ingob | 34 | SPITransferCompleted = 1; |
35 | |||
708 | ingob | 36 | //SPDR = 0x00; // dummy write |
606 | ingob | 37 | |
823 | ingob | 38 | ToNaviCtrl.Sync1 = 0xAA; |
39 | ToNaviCtrl.Sync2 = 0x83; |
||
606 | ingob | 40 | |
823 | ingob | 41 | ToNaviCtrl.Command = SPI_CMD_USER; |
42 | ToNaviCtrl.IntegralNick = 0; |
||
43 | ToNaviCtrl.IntegralRoll = 0; |
||
720 | ingob | 44 | SPI_RxDataValid = 0; |
304 | ingob | 45 | } |
46 | |||
47 | //------------------------------------------------------ |
||
823 | ingob | 48 | void SPI_StartTransmitPacket(void) |
304 | ingob | 49 | { |
606 | ingob | 50 | //if ((SLAVE_SELECT_PORT & (1 << SPI_SLAVE_SELECT)) == 0) return; // transfer of prev. packet not completed |
51 | if (!SPITransferCompleted) return; |
||
723 | hbuss | 52 | // _delay_us(30); |
304 | ingob | 53 | |
54 | SLAVE_SELECT_PORT &= ~(1 << SPI_SLAVE_SELECT); // SelectSlave |
||
823 | ingob | 55 | SPI_TX_Buffer = (unsigned char *) &ToNaviCtrl; |
617 | ingob | 56 | |
823 | ingob | 57 | ToNaviCtrl.Command = SPI_CommandSequence[SPI_CommandCounter++]; |
58 | if (SPI_CommandCounter >= sizeof(SPI_CommandSequence)) SPI_CommandCounter = 0; |
||
708 | ingob | 59 | |
606 | ingob | 60 | SPITransferCompleted = 0; |
304 | ingob | 61 | UpdateSPI_Buffer(); // update buffer |
823 | ingob | 62 | |
304 | ingob | 63 | SPI_BufferIndex = 1; |
708 | ingob | 64 | //ebugOut.Analog[16]++; |
304 | ingob | 65 | // -- Debug-Output --- |
66 | //---- |
||
691 | ingob | 67 | asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); |
68 | asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); |
||
69 | 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 | 70 | ToNaviCtrl.Chksum = ToNaviCtrl.Sync1; |
71 | SPDR = ToNaviCtrl.Sync1; // Start transmission |
||
691 | ingob | 72 | // SLAVE_SELECT_PORT |= (1 << SPI_SLAVE_SELECT); // DeselectSlave |
73 | |||
304 | ingob | 74 | } |
75 | |||
76 | //------------------------------------------------------ |
||
77 | //SIGNAL(SIG_SPI) |
||
78 | void SPI_TransmitByte(void) |
||
79 | { |
||
708 | ingob | 80 | static unsigned char SPI_RXState = 0; |
81 | unsigned char rxdata; |
||
82 | static unsigned char rxchksum; |
||
691 | ingob | 83 | |
84 | if (SPITransferCompleted) return; |
||
304 | ingob | 85 | if (!(SPSR & (1 << SPIF))) return; |
723 | hbuss | 86 | SendSPI = 4; |
691 | ingob | 87 | |
723 | hbuss | 88 | // _delay_us(30); |
606 | ingob | 89 | SLAVE_SELECT_PORT |= (1 << SPI_SLAVE_SELECT); // DeselectSlave |
304 | ingob | 90 | |
708 | ingob | 91 | rxdata = SPDR; |
92 | switch ( SPI_RXState) |
||
93 | { |
||
94 | case 0: |
||
95 | |||
96 | SPI_RxBufferIndex = 0; |
||
97 | //DebugOut.Analog[17]++; |
||
98 | rxchksum = rxdata; |
||
99 | if (rxdata == 0x81 ) { SPI_RXState = 1; } // 1. Syncbyte ok |
||
100 | |||
101 | break; |
||
102 | |||
103 | case 1: |
||
104 | if (rxdata == 0x55) { rxchksum += rxdata; SPI_RXState = 2; } // 2. Syncbyte ok |
||
105 | else SPI_RXState = 0; |
||
106 | //DebugOut.Analog[18]++; |
||
107 | break; |
||
108 | |||
109 | case 2: |
||
110 | SPI_Buffer[SPI_RxBufferIndex++]= rxdata; // get data |
||
111 | //DebugOut.Analog[19]++; |
||
823 | ingob | 112 | if (SPI_RxBufferIndex >= sizeof(FromNaviCtrl)) |
708 | ingob | 113 | { |
114 | |||
115 | if (rxdata == rxchksum) |
||
116 | { |
||
823 | ingob | 117 | unsigned char *ptr = (unsigned char *)&FromNaviCtrl; |
708 | ingob | 118 | |
119 | memcpy(ptr, (unsigned char *) SPI_Buffer, sizeof(SPI_Buffer)); |
||
120 | |||
720 | ingob | 121 | SPI_RxDataValid = 1; |
708 | ingob | 122 | } |
720 | ingob | 123 | else SPI_RxDataValid = 0; |
708 | ingob | 124 | |
125 | SPI_RXState = 0; |
||
126 | } |
||
127 | else rxchksum += rxdata; |
||
128 | break; |
||
129 | |||
130 | } |
||
131 | |||
823 | ingob | 132 | if (SPI_BufferIndex < sizeof(ToNaviCtrl)) |
606 | ingob | 133 | { |
134 | SLAVE_SELECT_PORT &= ~(1 << SPI_SLAVE_SELECT); // SelectSlave |
||
691 | ingob | 135 | asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); |
136 | asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); |
||
137 | asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); |
||
617 | ingob | 138 | |
708 | ingob | 139 | SPDR = SPI_TX_Buffer[SPI_BufferIndex]; |
823 | ingob | 140 | ToNaviCtrl.Chksum += SPI_TX_Buffer[SPI_BufferIndex]; |
691 | ingob | 141 | // SLAVE_SELECT_PORT |= (1 << SPI_SLAVE_SELECT); // DeselectSlave |
142 | |||
606 | ingob | 143 | } |
708 | ingob | 144 | else SPITransferCompleted = 1; |
145 | |||
304 | ingob | 146 | SPI_BufferIndex++; |
147 | } |
||
148 | |||
691 | ingob | 149 | |
304 | ingob | 150 | //------------------------------------------------------ |
151 | void UpdateSPI_Buffer(void) |
||
152 | { |
||
691 | ingob | 153 | static unsigned char i =0; |
846 | hbuss | 154 | signed int tmp; |
304 | ingob | 155 | cli(); |
823 | ingob | 156 | |
846 | hbuss | 157 | ToNaviCtrl.IntegralNick = (int) (IntegralNick / 108); |
158 | ToNaviCtrl.IntegralRoll = (int) (IntegralRoll / 108); |
||
823 | ingob | 159 | ToNaviCtrl.GyroCompass = ErsatzKompass / GIER_GRAD_FAKTOR; |
160 | ToNaviCtrl.AccNick = (int) ACC_AMPLIFY * (NaviAccNick / NaviCntAcc); |
||
846 | hbuss | 161 | ToNaviCtrl.AccRoll = (int) ACC_AMPLIFY * (NaviAccRoll / NaviCntAcc); |
819 | hbuss | 162 | NaviCntAcc = 0; NaviAccNick = 0; NaviAccRoll = 0; |
823 | ingob | 163 | // ToNaviCtrl.User8 = Parameter_UserParam8; |
164 | // ToNaviCtrl.CalState = WinkelOut.CalcState; |
||
165 | |||
166 | switch(ToNaviCtrl.Command) // |
||
167 | { |
||
168 | case SPI_CMD_USER: |
||
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; |
||
176 | break; |
||
177 | |||
178 | case SPI_CMD_STICK: |
||
871 | hbuss | 179 | tmp = PPM_in[EE_Parameter.Kanalbelegung[K_GAS]]; if(tmp > 127) tmp = 127; else if(tmp < -127) tmp = -127; |
846 | hbuss | 180 | ToNaviCtrl.Param.Byte[0] = (char) tmp; |
871 | hbuss | 181 | tmp = PPM_in[EE_Parameter.Kanalbelegung[K_GIER]]; if(tmp > 127) tmp = 127; else if(tmp < -127) tmp = -127; |
846 | hbuss | 182 | ToNaviCtrl.Param.Byte[1] = (char) tmp; |
871 | hbuss | 183 | tmp = PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]]; if(tmp > 127) tmp = 127; else if(tmp < -127) tmp = -127; |
846 | hbuss | 184 | ToNaviCtrl.Param.Byte[2] = (char) tmp; |
871 | hbuss | 185 | tmp = PPM_in[EE_Parameter.Kanalbelegung[K_NICK]]; if(tmp > 127) tmp = 127; else if(tmp < -127) tmp = -127; |
846 | hbuss | 186 | ToNaviCtrl.Param.Byte[3] = (char) tmp; |
187 | ToNaviCtrl.Param.Byte[4] = (unsigned char) Poti1; |
||
188 | ToNaviCtrl.Param.Byte[5] = (unsigned char) Poti2; |
||
189 | ToNaviCtrl.Param.Byte[6] = (unsigned char) Poti3; |
||
190 | ToNaviCtrl.Param.Byte[7] = (unsigned char) Poti4; |
||
855 | hbuss | 191 | ToNaviCtrl.Param.Byte[8] = (unsigned char) SenderOkay; |
823 | ingob | 192 | |
193 | break; |
||
194 | case SPI_CMD_CAL_COMPASS: |
||
855 | hbuss | 195 | if(WinkelOut.CalcState > 5) |
196 | { |
||
197 | WinkelOut.CalcState = 0; |
||
198 | ToNaviCtrl.Param.Byte[0] = 5; |
||
199 | } |
||
200 | else ToNaviCtrl.Param.Byte[0] = WinkelOut.CalcState; |
||
823 | ingob | 201 | break; |
202 | } |
||
203 | |||
304 | ingob | 204 | sei(); |
855 | hbuss | 205 | |
720 | ingob | 206 | if (SPI_RxDataValid) |
207 | { |
||
855 | hbuss | 208 | if(abs(FromNaviCtrl.GPS_Nick) < 512 && abs(FromNaviCtrl.GPS_Roll) < 512 && (EE_Parameter.GlobalConfig & CFG_GPS_AKTIV)) |
854 | hbuss | 209 | { |
823 | ingob | 210 | GPS_Nick = FromNaviCtrl.GPS_Nick; |
211 | GPS_Roll = FromNaviCtrl.GPS_Roll; |
||
854 | hbuss | 212 | } |
213 | if(FromNaviCtrl.CompassValue <= 360) KompassValue = FromNaviCtrl.CompassValue; |
||
720 | ingob | 214 | KompassRichtung = ((540 + KompassValue - KompassStartwert) % 360) - 180; |
215 | |||
855 | hbuss | 216 | if(FromNaviCtrl.BeepTime > beeptime && !WinkelOut.CalcState) beeptime = FromNaviCtrl.BeepTime; |
823 | ingob | 217 | |
218 | switch (FromNaviCtrl.Command) |
||
219 | { |
||
220 | case SPI_CMD_OSD_DATA: |
||
221 | // ToFlightCtrl.Param.Byte[0] = OsdBar; |
||
222 | // ToFlightCtrl.Param.Int[1] = Distance; |
||
223 | break; |
||
224 | |||
225 | case SPI_CMD_GPS_POS: |
||
226 | // ToFlightCtrl.Param.Long[0] = GPS_Data.Longitude; |
||
227 | // ToFlightCtrl.Param.Long[1] = GPS_Data.Latitude; |
||
228 | break; |
||
229 | |||
230 | case SPI_CMD_GPS_TARGET: |
||
231 | // ToFlightCtrl.Param.Long[0] = GPS_Data.TargetLongitude; |
||
232 | // ToFlightCtrl.Param.Long[1] = GPS_Data.TargetLatitude; |
||
233 | break; |
||
234 | |||
235 | default: |
||
236 | break; |
||
237 | } |
||
720 | ingob | 238 | } |
239 | else |
||
240 | { |
||
819 | hbuss | 241 | // KompassValue = 0; |
242 | // KompassRichtung = 0; |
||
720 | ingob | 243 | |
244 | GPS_Nick = 0; |
||
245 | GPS_Roll = 0; |
||
246 | } |
||
304 | ingob | 247 | } |
248 | |||
597 | ingob | 249 | #endif |
304 | ingob | 250 | |
251 |