Subversion Repositories FlightCtrl

Rev

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