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;
1668 holgerb 16
signed char FromNC_Rotate_C = 32, FromNC_Rotate_S = 0;
606 ingob 17
 
882 hbuss 18
volatile unsigned char     SPI_Buffer[sizeof(FromNaviCtrl)];
823 ingob 19
unsigned char *SPI_TX_Buffer;
708 ingob 20
 
617 ingob 21
unsigned char SPITransferCompleted, SPI_ChkSum;
1215 hbuss 22
unsigned char SPI_RxDataValid,NaviDataOkay = 0;
720 ingob 23
 
1509 killagreg 24
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 25
unsigned char SPI_CommandCounter = 0;
26
 
597 ingob 27
#ifdef USE_SPI_COMMUNICATION
691 ingob 28
 
304 ingob 29
//------------------------------------------------------
30
void SPI_MasterInit(void)
31
{
1051 killagreg 32
  DDR_SPI |= (1<<DD_MOSI)|(1<<DD_SCK);    // Set MOSI and SCK output, all others input
304 ingob 33
  SLAVE_SELECT_DDR_PORT |= (1 << SPI_SLAVE_SELECT);
1051 killagreg 34
 
35
  SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1)|(0<<SPR0)|(0<<SPIE);   // Enable SPI, Master, set clock rate fck/64
723 hbuss 36
  SPSR = 0;//(1<<SPI2X);
1051 killagreg 37
 
38
  SLAVE_SELECT_PORT |=  (1 << SPI_SLAVE_SELECT);
606 ingob 39
  SPITransferCompleted = 1;
1051 killagreg 40
 
708 ingob 41
  //SPDR = 0x00;  // dummy write
1051 killagreg 42
 
823 ingob 43
  ToNaviCtrl.Sync1 = 0xAA;
44
  ToNaviCtrl.Sync2 = 0x83;
1051 killagreg 45
 
1448 killagreg 46
  ToNaviCtrl.Command = SPI_FCCMD_USER;
823 ingob 47
  ToNaviCtrl.IntegralNick = 0;
48
  ToNaviCtrl.IntegralRoll = 0;
1215 hbuss 49
  FromNaviCtrl_Value.SerialDataOkay = 0;
1051 killagreg 50
  SPI_RxDataValid = 0;
51
 
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;
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;
112
           break;
113
 
708 ingob 114
   case 2:
115
                   SPI_Buffer[SPI_RxBufferIndex++]= rxdata;             // get data
116
           //DebugOut.Analog[19]++;
1051 killagreg 117
           if (SPI_RxBufferIndex >= sizeof(FromNaviCtrl))
118
                   {
119
 
708 ingob 120
                if (rxdata == rxchksum)
121
                        {
823 ingob 122
                  unsigned char *ptr = (unsigned char *)&FromNaviCtrl;
1051 killagreg 123
 
708 ingob 124
                          memcpy(ptr, (unsigned char *) SPI_Buffer,  sizeof(SPI_Buffer));
1051 killagreg 125
 
126
                          SPI_RxDataValid = 1;
127
                        }
128
                        else SPI_RxDataValid = 0;
129
 
130
                        SPI_RXState  = 0;
708 ingob 131
                   }
1051 killagreg 132
                  else rxchksum += rxdata;
133
        break;
134
 
135
  }
136
 
137
   if (SPI_BufferIndex < sizeof(ToNaviCtrl))
138
     {
606 ingob 139
           SLAVE_SELECT_PORT &=  ~(1 << SPI_SLAVE_SELECT);  // SelectSlave
691 ingob 140
           asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop");         asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop");
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");
1051 killagreg 143
 
708 ingob 144
           SPDR = SPI_TX_Buffer[SPI_BufferIndex];
823 ingob 145
           ToNaviCtrl.Chksum += SPI_TX_Buffer[SPI_BufferIndex];
691 ingob 146
        //   SLAVE_SELECT_PORT |=  (1 << SPI_SLAVE_SELECT);   // DeselectSlave
147
 
606 ingob 148
         }
1051 killagreg 149
         else SPITransferCompleted = 1;
150
 
304 ingob 151
         SPI_BufferIndex++;
152
}
153
 
691 ingob 154
 
304 ingob 155
//------------------------------------------------------
156
void UpdateSPI_Buffer(void)
157
{
846 hbuss 158
  signed int tmp;
1171 hbuss 159
  ToNaviCtrl.IntegralNick = (int) (IntegralNick / (long)(EE_Parameter.GyroAccFaktor * 4));
160
  ToNaviCtrl.IntegralRoll = (int) (IntegralRoll / (long)(EE_Parameter.GyroAccFaktor * 4));
161
  ToNaviCtrl.GyroCompass = (10 * ErsatzKompass) / GIER_GRAD_FAKTOR;
1664 holgerb 162
  ToNaviCtrl.GyroGier = (signed int) AdNeutralGier - AdWertGier;
1171 hbuss 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();
1668 holgerb 182
                                ToNaviCtrl.Param.Byte[10] = ControlHeading;
921 hbuss 183
        break;
1241 killagreg 184
 
1508 killagreg 185
     case SPI_FCCMD_ACCU:
186
                        ToNaviCtrl.Param.Int[0] = Capacity.ActualCurrent; // 0.1A
187
                        ToNaviCtrl.Param.Int[1] = Capacity.UsedCapacity; // mAh
188
                        ToNaviCtrl.Param.Byte[4] = (unsigned char) UBat; // 0.1V
189
                        ToNaviCtrl.Param.Byte[5] = (unsigned char) BattLowVoltageWarning; //0.1V
190
        break;
191
 
1448 killagreg 192
         case SPI_FCCMD_PARAMETER1:
993 hbuss 193
                                ToNaviCtrl.Param.Byte[0] = EE_Parameter.NaviGpsModeControl;     // Parameters for the Naviboard
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;
1241 killagreg 206
 
1448 killagreg 207
         case SPI_FCCMD_STICK:
1320 hbuss 208
              cli();
871 hbuss 209
                tmp = PPM_in[EE_Parameter.Kanalbelegung[K_GAS]];  if(tmp > 127) tmp = 127; else if(tmp < -127) tmp = -127;
846 hbuss 210
                                ToNaviCtrl.Param.Byte[0] = (char) tmp;
871 hbuss 211
                tmp = PPM_in[EE_Parameter.Kanalbelegung[K_GIER]]; if(tmp > 127) tmp = 127; else if(tmp < -127) tmp = -127;
846 hbuss 212
                                ToNaviCtrl.Param.Byte[1] = (char) tmp;
871 hbuss 213
                tmp = PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]]; if(tmp > 127) tmp = 127; else if(tmp < -127) tmp = -127;
846 hbuss 214
                                ToNaviCtrl.Param.Byte[2] = (char) tmp;
871 hbuss 215
                tmp = PPM_in[EE_Parameter.Kanalbelegung[K_NICK]]; if(tmp > 127) tmp = 127; else if(tmp < -127) tmp = -127;
1320 hbuss 216
              sei();
846 hbuss 217
                                ToNaviCtrl.Param.Byte[3] = (char) tmp;
1377 hbuss 218
                                ToNaviCtrl.Param.Byte[4] = (unsigned char) Poti[0];
219
                                ToNaviCtrl.Param.Byte[5] = (unsigned char) Poti[1];
220
                                ToNaviCtrl.Param.Byte[6] = (unsigned char) Poti[2];
221
                    ToNaviCtrl.Param.Byte[7] = (unsigned char) Poti[3];
222
                                ToNaviCtrl.Param.Byte[8] = (unsigned char) Poti[4];
223
                                ToNaviCtrl.Param.Byte[9] = (unsigned char) Poti[5];
224
                                ToNaviCtrl.Param.Byte[10] = (unsigned char) Poti[6];
225
                                ToNaviCtrl.Param.Byte[11] = (unsigned char) Poti[7];
1241 killagreg 226
                        break;
1448 killagreg 227
                case SPI_FCCMD_MISC:
1241 killagreg 228
                        if(WinkelOut.CalcState > 5)
229
                        {
230
                                WinkelOut.CalcState = 0;
231
                                ToNaviCtrl.Param.Byte[0] = 5;
232
                        }
233
                        else ToNaviCtrl.Param.Byte[0] = WinkelOut.CalcState;
1312 hbuss 234
                        ToNaviCtrl.Param.Byte[1] = EE_Parameter.NaviPH_LoginTime;
1662 killagreg 235
                        ToNaviCtrl.Param.Int[1] = (int)(HoehenWert/5);
1312 hbuss 236
                        ToNaviCtrl.Param.Int[2] = (int)(SollHoehe/5);
237
                        ToNaviCtrl.Param.Byte[6] = EE_Parameter.NaviGpsPLimit;
238
                        ToNaviCtrl.Param.Byte[7] = EE_Parameter.NaviGpsILimit;
239
                        ToNaviCtrl.Param.Byte[8] = EE_Parameter.NaviGpsDLimit;
1377 hbuss 240
            ToNaviCtrl.Param.Byte[9] = (unsigned char) SenderOkay;
1600 killagreg 241
            ToNaviCtrl.Param.Byte[10] = (unsigned char) PPM_in[0];
1377 hbuss 242
                        ToNaviCtrl.Param.Byte[11] = DebugOut.Analog[7] / 4; //GasMischanteil
1241 killagreg 243
                        break;
1448 killagreg 244
                case SPI_FCCMD_VERSION:
245
                        ToNaviCtrl.Param.Byte[0] = VERSION_MAJOR;
246
                        ToNaviCtrl.Param.Byte[1] = VERSION_MINOR;
247
                        ToNaviCtrl.Param.Byte[2] = VERSION_PATCH;
248
                        ToNaviCtrl.Param.Byte[3] = NC_SPI_COMPATIBLE;
1241 killagreg 249
                        ToNaviCtrl.Param.Byte[4] = PlatinenVersion;
250
                break;
251
 
1448 killagreg 252
            case SPI_FCCMD_SERVOS:
1241 killagreg 253
                ToNaviCtrl.Param.Byte[0] = EE_Parameter.ServoNickRefresh;     // Parameters for the Servo Control
254
                        ToNaviCtrl.Param.Byte[1] = EE_Parameter.ServoCompInvert;
255
                        ToNaviCtrl.Param.Byte[2] = Parameter_ServoNickControl;
256
                        ToNaviCtrl.Param.Byte[3] = EE_Parameter.ServoNickComp;
257
                        ToNaviCtrl.Param.Byte[4] = EE_Parameter.ServoNickMin;
258
                        ToNaviCtrl.Param.Byte[5] = EE_Parameter.ServoNickMax;
259
                        ToNaviCtrl.Param.Byte[6] = Parameter_ServoRollControl;
260
                        ToNaviCtrl.Param.Byte[7] = EE_Parameter.ServoRollComp;
261
                        ToNaviCtrl.Param.Byte[8] = EE_Parameter.ServoRollMin;
262
                        ToNaviCtrl.Param.Byte[9] = EE_Parameter.ServoRollMax;
263
                break;
264
        }
265
 
1215 hbuss 266
  if(SPI_RxDataValid)
1051 killagreg 267
  {
1536 killagreg 268
   NaviDataOkay = 250;
855 hbuss 269
   if(abs(FromNaviCtrl.GPS_Nick) < 512 && abs(FromNaviCtrl.GPS_Roll) < 512 && (EE_Parameter.GlobalConfig & CFG_GPS_AKTIV))
854 hbuss 270
   {
823 ingob 271
    GPS_Nick = FromNaviCtrl.GPS_Nick;
272
    GPS_Roll = FromNaviCtrl.GPS_Roll;
854 hbuss 273
   }
274
    if(FromNaviCtrl.CompassValue <= 360)   KompassValue = FromNaviCtrl.CompassValue;
720 ingob 275
    KompassRichtung = ((540 + KompassValue - KompassStartwert) % 360) - 180;
1051 killagreg 276
 
855 hbuss 277
    if(FromNaviCtrl.BeepTime > beeptime && !WinkelOut.CalcState) beeptime = FromNaviCtrl.BeepTime;
1051 killagreg 278
 
823 ingob 279
          switch (FromNaviCtrl.Command)
280
          {
1448 killagreg 281
            case SPI_NCCMD_KALMAN:
1231 killagreg 282
                        FromNaviCtrl_Value.Kalman_K = FromNaviCtrl.Param.sByte[0];
283
                        FromNaviCtrl_Value.Kalman_MaxFusion = FromNaviCtrl.Param.sByte[1];
284
                        FromNaviCtrl_Value.Kalman_MaxDrift = FromNaviCtrl.Param.sByte[2];
285
                        FromNaviCtrl_Value.SerialDataOkay = FromNaviCtrl.Param.Byte[3];
1283 hbuss 286
                        FromNaviCtrl_Value.GpsZ = FromNaviCtrl.Param.Byte[4];
1664 holgerb 287
                        FromNC_Rotate_C = FromNaviCtrl.Param.Byte[5];
288
                        FromNC_Rotate_S = FromNaviCtrl.Param.Byte[6];
1231 killagreg 289
                        break;
1448 killagreg 290
 
291
                case SPI_NCCMD_VERSION:
292
                        NC_Version.Major = FromNaviCtrl.Param.Byte[0];
293
                        NC_Version.Minor = FromNaviCtrl.Param.Byte[1];
294
                        NC_Version.Patch = FromNaviCtrl.Param.Byte[2];
295
                        NC_Version.Compatible = FromNaviCtrl.Param.Byte[3];
296
                        NC_Version.Hardware = FromNaviCtrl.Param.Byte[4];
297
                        break;
1451 killagreg 298
 
299
                case SPI_NCCMD_GPSINFO:
300
                        GPSInfo.Flags = FromNaviCtrl.Param.Byte[0];
301
                        GPSInfo.NumOfSats = FromNaviCtrl.Param.Byte[1];
302
                        GPSInfo.SatFix = FromNaviCtrl.Param.Byte[2];
1472 killagreg 303
                        GPSInfo.HomeDistance = FromNaviCtrl.Param.Int[2];
304
                        GPSInfo.HomeBearing = FromNaviCtrl.Param.sInt[3];
1451 killagreg 305
                        break;
306
 
823 ingob 307
                default:
1451 killagreg 308
                        break;
823 ingob 309
          }
720 ingob 310
  }
311
  else
312
  {
819 hbuss 313
//    KompassValue = 0;
314
//    KompassRichtung = 0;
720 ingob 315
        GPS_Nick = 0;
316
    GPS_Roll = 0;
317
  }
304 ingob 318
}
319
 
597 ingob 320
#endif
304 ingob 321
 
322