Subversion Repositories FlightCtrl

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1263 thjac 1
// ######################## SPI - FlightCtrl ###################
2
#include "main.h"
3
 
4
 
5
//struct str_ToNaviCtrl_Version   ToNaviCtrl_Version;
6
//struct str_FromNaviCtrl_Version   FromNaviCtrl_Version;
7
struct str_ToNaviCtrl   ToNaviCtrl;
8
struct str_FromNaviCtrl   FromNaviCtrl;
9
struct str_FromNaviCtrl_Value FromNaviCtrl_Value;
10
struct str_SPI_VersionInfo SPI_VersionInfo;
11
 
12
unsigned char              SPI_BufferIndex;
13
unsigned char              SPI_RxBufferIndex;
14
 
15
volatile unsigned char     SPI_Buffer[sizeof(FromNaviCtrl)];
16
unsigned char *SPI_TX_Buffer;
17
 
18
unsigned char SPITransferCompleted, SPI_ChkSum;
19
unsigned char SPI_RxDataValid,NaviDataOkay = 0;
20
 
21
unsigned char SPI_CommandSequence[] = { SPI_CMD_USER, SPI_CMD_STICK, SPI_CMD_PARAMETER1, SPI_CMD_STICK, SPI_CMD_MISC, SPI_CMD_VERSION };
22
unsigned char SPI_CommandCounter = 0;
23
 
24
#ifdef USE_SPI_COMMUNICATION
25
 
26
//------------------------------------------------------
27
void SPI_MasterInit(void)
28
{
29
  DDR_SPI |= (1<<DD_MOSI)|(1<<DD_SCK);    // Set MOSI and SCK output, all others input
30
  SLAVE_SELECT_DDR_PORT |= (1 << SPI_SLAVE_SELECT);
31
 
32
  SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1)|(0<<SPR0)|(0<<SPIE);   // Enable SPI, Master, set clock rate fck/64
33
  SPSR = 0;//(1<<SPI2X);
34
 
35
  SLAVE_SELECT_PORT |=  (1 << SPI_SLAVE_SELECT);
36
  SPITransferCompleted = 1;
37
 
38
  //SPDR = 0x00;  // dummy write
39
 
40
  ToNaviCtrl.Sync1 = 0xAA;
41
  ToNaviCtrl.Sync2 = 0x83;
42
 
43
  ToNaviCtrl.Command = SPI_CMD_USER;
44
  ToNaviCtrl.IntegralNick = 0;
45
  ToNaviCtrl.IntegralRoll = 0;
46
  FromNaviCtrl_Value.SerialDataOkay = 0;
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;
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
                        //DebugOut.Analog[17]++;
106
                        rxchksum = rxdata;
107
                        if (rxdata == 0x81 )  { SPI_RXState  = 1;  }   // 1. Syncbyte ok
108
 
109
           break;
110
 
111
   case 1:
112
                    if (rxdata == 0x55) { rxchksum += rxdata; SPI_RXState  = 2;  }   // 2. Syncbyte ok
113
                 else SPI_RXState  = 0;
114
                        //DebugOut.Analog[18]++;
115
           break;
116
 
117
   case 2:
118
                   SPI_Buffer[SPI_RxBufferIndex++]= rxdata;             // get data
119
           //DebugOut.Analog[19]++;
120
           if (SPI_RxBufferIndex >= sizeof(FromNaviCtrl))
121
                   {
122
 
123
                if (rxdata == rxchksum)
124
                        {
125
                  unsigned char *ptr = (unsigned char *)&FromNaviCtrl;
126
 
127
                          memcpy(ptr, (unsigned char *) SPI_Buffer,  sizeof(SPI_Buffer));
128
 
129
                          SPI_RxDataValid = 1;
130
                        }
131
                        else SPI_RxDataValid = 0;
132
 
133
                        SPI_RXState  = 0;
134
                   }
135
                  else rxchksum += rxdata;
136
        break;
137
 
138
  }
139
 
140
   if (SPI_BufferIndex < sizeof(ToNaviCtrl))
141
     {
142
           SLAVE_SELECT_PORT &=  ~(1 << SPI_SLAVE_SELECT);  // SelectSlave
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");
146
 
147
           SPDR = SPI_TX_Buffer[SPI_BufferIndex];
148
           ToNaviCtrl.Chksum += SPI_TX_Buffer[SPI_BufferIndex];
149
        //   SLAVE_SELECT_PORT |=  (1 << SPI_SLAVE_SELECT);   // DeselectSlave
150
 
151
         }
152
         else SPITransferCompleted = 1;
153
 
154
         SPI_BufferIndex++;
155
}
156
 
157
 
158
//------------------------------------------------------
159
void UpdateSPI_Buffer(void)
160
{
161
  signed int tmp;
162
  cli();
163
 
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;
169
  NaviCntAcc = 0; NaviAccNick = 0; NaviAccRoll = 0;
170
//  ToNaviCtrl.User8 = Parameter_UserParam8;
171
//  ToNaviCtrl.CalState = WinkelOut.CalcState;
172
 
173
   switch(ToNaviCtrl.Command)  //
174
   {
175
         case SPI_CMD_USER:
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;
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;
190
         case SPI_CMD_PARAMETER1:
191
                                ToNaviCtrl.Param.Byte[0] = EE_Parameter.NaviGpsModeControl;     // Parameters for the Naviboard
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;
199
                ToNaviCtrl.Param.Byte[8] = EE_Parameter.NaviOperatingRadius;
200
                ToNaviCtrl.Param.Byte[9] = EE_Parameter.NaviWindCorrection;
201
                ToNaviCtrl.Param.Byte[10] = EE_Parameter.NaviSpeedCompensation;
202
                                ToNaviCtrl.Param.Byte[11] = EE_Parameter.NaviAngleLimitation;
203
            break;
204
         case SPI_CMD_STICK:
205
                tmp = PPM_in[EE_Parameter.Kanalbelegung[K_GAS]];  if(tmp > 127) tmp = 127; else if(tmp < -127) tmp = -127;
206
                                ToNaviCtrl.Param.Byte[0] = (char) tmp;
207
                tmp = PPM_in[EE_Parameter.Kanalbelegung[K_GIER]]; if(tmp > 127) tmp = 127; else if(tmp < -127) tmp = -127;
208
                                ToNaviCtrl.Param.Byte[1] = (char) tmp;
209
                tmp = PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]]; if(tmp > 127) tmp = 127; else if(tmp < -127) tmp = -127;
210
                                ToNaviCtrl.Param.Byte[2] = (char) tmp;
211
                tmp = PPM_in[EE_Parameter.Kanalbelegung[K_NICK]]; if(tmp > 127) tmp = 127; else if(tmp < -127) tmp = -127;
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;
217
                    ToNaviCtrl.Param.Byte[8] = (unsigned char) SenderOkay;
218
            break;
219
         case SPI_CMD_MISC:
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;
226
                                ToNaviCtrl.Param.Int[1] = HoehenWert;
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;
231
            break;
232
 
233
         case SPI_CMD_VERSION:
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;
238
                                ToNaviCtrl.Param.Byte[4] = PlatinenVersion;
239
            break;
240
   }
241
 
242
  sei();
243
 
244
  if(SPI_RxDataValid)
245
  {
246
   if(abs(FromNaviCtrl.GPS_Nick) < 512 && abs(FromNaviCtrl.GPS_Roll) < 512 && (EE_Parameter.GlobalConfig & CFG_GPS_AKTIV))
247
   {
248
    GPS_Nick = FromNaviCtrl.GPS_Nick;
249
    GPS_Roll = FromNaviCtrl.GPS_Roll;
250
        NaviDataOkay = 250;
251
   }
252
    if(FromNaviCtrl.CompassValue <= 360)   KompassValue = FromNaviCtrl.CompassValue;
253
    KompassRichtung = ((540 + KompassValue - KompassStartwert) % 360) - 180;
254
 
255
    if(FromNaviCtrl.BeepTime > beeptime && !WinkelOut.CalcState) beeptime = FromNaviCtrl.BeepTime;
256
 
257
          switch (FromNaviCtrl.Command)
258
          {
259
            case  SPI_KALMAN:
260
               FromNaviCtrl_Value.Kalman_K = FromNaviCtrl.Param.Byte[0];
261
               FromNaviCtrl_Value.Kalman_MaxFusion = FromNaviCtrl.Param.Byte[1];
262
               FromNaviCtrl_Value.Kalman_MaxDrift = FromNaviCtrl.Param.Byte[2];
263
               FromNaviCtrl_Value.SerialDataOkay = FromNaviCtrl.Param.Byte[3];
264
               break;
265
 
266
                default:
267
                  break;
268
          }
269
  }
270
  else
271
  {
272
//    KompassValue = 0;
273
//    KompassRichtung = 0;
274
        GPS_Nick = 0;
275
    GPS_Roll = 0;
276
  }
277
}
278
 
279
#endif
280
 
281