Subversion Repositories FlightCtrl

Rev

Rev 1793 | Rev 1832 | 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"
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;
1690 holgerb 22
unsigned char SPI_RxDataValid,NaviDataOkay = 250;
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;
1703 holgerb 26
unsigned char NC_ErrorCode = 0;
823 ingob 27
 
597 ingob 28
#ifdef USE_SPI_COMMUNICATION
691 ingob 29
 
304 ingob 30
//------------------------------------------------------
31
void SPI_MasterInit(void)
32
{
1051 killagreg 33
  DDR_SPI |= (1<<DD_MOSI)|(1<<DD_SCK);    // Set MOSI and SCK output, all others input
304 ingob 34
  SLAVE_SELECT_DDR_PORT |= (1 << SPI_SLAVE_SELECT);
1051 killagreg 35
 
36
  SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1)|(0<<SPR0)|(0<<SPIE);   // Enable SPI, Master, set clock rate fck/64
723 hbuss 37
  SPSR = 0;//(1<<SPI2X);
1051 killagreg 38
 
39
  SLAVE_SELECT_PORT |=  (1 << SPI_SLAVE_SELECT);
606 ingob 40
  SPITransferCompleted = 1;
1051 killagreg 41
 
708 ingob 42
  //SPDR = 0x00;  // dummy write
1051 killagreg 43
 
823 ingob 44
  ToNaviCtrl.Sync1 = 0xAA;
45
  ToNaviCtrl.Sync2 = 0x83;
1051 killagreg 46
 
1448 killagreg 47
  ToNaviCtrl.Command = SPI_FCCMD_USER;
823 ingob 48
  ToNaviCtrl.IntegralNick = 0;
49
  ToNaviCtrl.IntegralRoll = 0;
1215 hbuss 50
  FromNaviCtrl_Value.SerialDataOkay = 0;
1051 killagreg 51
  SPI_RxDataValid = 0;
52
 
304 ingob 53
}
54
 
55
//------------------------------------------------------
823 ingob 56
void SPI_StartTransmitPacket(void)
304 ingob 57
{
606 ingob 58
   //if ((SLAVE_SELECT_PORT & (1 << SPI_SLAVE_SELECT)) == 0) return;    // transfer of prev. packet not completed
59
   if (!SPITransferCompleted) return;
1051 killagreg 60
//   _delay_us(30);
61
 
304 ingob 62
   SLAVE_SELECT_PORT &=  ~(1 << SPI_SLAVE_SELECT);  // SelectSlave
823 ingob 63
   SPI_TX_Buffer = (unsigned char *) &ToNaviCtrl;
1051 killagreg 64
 
823 ingob 65
   ToNaviCtrl.Command = SPI_CommandSequence[SPI_CommandCounter++];
66
   if (SPI_CommandCounter >= sizeof(SPI_CommandSequence)) SPI_CommandCounter = 0;
1051 killagreg 67
 
606 ingob 68
   SPITransferCompleted = 0;
304 ingob 69
   UpdateSPI_Buffer();                              // update buffer
823 ingob 70
 
304 ingob 71
   SPI_BufferIndex = 1;
1051 killagreg 72
  //ebugOut.Analog[16]++;
304 ingob 73
   // -- Debug-Output ---
74
   //----
691 ingob 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");
823 ingob 78
   ToNaviCtrl.Chksum = ToNaviCtrl.Sync1;
1051 killagreg 79
   SPDR = ToNaviCtrl.Sync1;                  // Start transmission
691 ingob 80
//     SLAVE_SELECT_PORT |=  (1 << SPI_SLAVE_SELECT);   // DeselectSlave
81
 
304 ingob 82
}
83
 
84
//------------------------------------------------------
85
//SIGNAL(SIG_SPI)
86
void SPI_TransmitByte(void)
87
{
708 ingob 88
   static unsigned char SPI_RXState = 0;
1051 killagreg 89
   unsigned char rxdata;
708 ingob 90
   static unsigned char rxchksum;
1051 killagreg 91
 
691 ingob 92
   if (SPITransferCompleted) return;
304 ingob 93
   if (!(SPSR & (1 << SPIF))) return;
1051 killagreg 94
  SendSPI = 4;
95
 
96
//   _delay_us(30);
606 ingob 97
  SLAVE_SELECT_PORT |=  (1 << SPI_SLAVE_SELECT);   // DeselectSlave
1051 killagreg 98
 
708 ingob 99
  rxdata = SPDR;
100
  switch ( SPI_RXState)
101
  {
1051 killagreg 102
  case 0:
103
 
104
                        SPI_RxBufferIndex = 0;
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;
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;
1664 holgerb 163
  ToNaviCtrl.GyroGier = (signed int) AdNeutralGier - AdWertGier;
1171 hbuss 164
  ToNaviCtrl.AccNick = ((int) ACC_AMPLIFY * (NaviAccNick / NaviCntAcc))/4;
165
  ToNaviCtrl.AccRoll = ((int) ACC_AMPLIFY * (NaviAccRoll / NaviCntAcc))/4;
819 hbuss 166
  NaviCntAcc = 0; NaviAccNick = 0; NaviAccRoll = 0;
823 ingob 167
//  ToNaviCtrl.User8 = Parameter_UserParam8;
168
//  ToNaviCtrl.CalState = WinkelOut.CalcState;
1051 killagreg 169
   switch(ToNaviCtrl.Command)  //
823 ingob 170
   {
1448 killagreg 171
         case SPI_FCCMD_USER:
823 ingob 172
                                ToNaviCtrl.Param.Byte[0] = Parameter_UserParam1;
173
                                ToNaviCtrl.Param.Byte[1] = Parameter_UserParam2;
174
                                ToNaviCtrl.Param.Byte[2] = Parameter_UserParam3;
175
                                ToNaviCtrl.Param.Byte[3] = Parameter_UserParam4;
176
                                ToNaviCtrl.Param.Byte[4] = Parameter_UserParam5;
177
                                ToNaviCtrl.Param.Byte[5] = Parameter_UserParam6;
178
                                ToNaviCtrl.Param.Byte[6] = Parameter_UserParam7;
921 hbuss 179
                                ToNaviCtrl.Param.Byte[7] = Parameter_UserParam8;
1765 killagreg 180
                                ToNaviCtrl.Param.Byte[8] = FC_StatusFlags;
181
                FC_StatusFlags &= ~(FC_STATUS_CALIBRATE | FC_STATUS_START);
1622 killagreg 182
                    ToNaviCtrl.Param.Byte[9] = GetActiveParamSet();
1668 holgerb 183
                                ToNaviCtrl.Param.Byte[10] = ControlHeading;
921 hbuss 184
        break;
1241 killagreg 185
 
1508 killagreg 186
     case SPI_FCCMD_ACCU:
187
                        ToNaviCtrl.Param.Int[0] = Capacity.ActualCurrent; // 0.1A
188
                        ToNaviCtrl.Param.Int[1] = Capacity.UsedCapacity; // mAh
189
                        ToNaviCtrl.Param.Byte[4] = (unsigned char) UBat; // 0.1V
190
                        ToNaviCtrl.Param.Byte[5] = (unsigned char) BattLowVoltageWarning; //0.1V
191
        break;
192
 
1448 killagreg 193
         case SPI_FCCMD_PARAMETER1:
993 hbuss 194
                                ToNaviCtrl.Param.Byte[0] = EE_Parameter.NaviGpsModeControl;     // Parameters for the Naviboard
1051 killagreg 195
                                ToNaviCtrl.Param.Byte[1] = EE_Parameter.NaviGpsGain;
196
                                ToNaviCtrl.Param.Byte[2] = EE_Parameter.NaviGpsP;
197
                                ToNaviCtrl.Param.Byte[3] = EE_Parameter.NaviGpsI;
198
                                ToNaviCtrl.Param.Byte[4] = EE_Parameter.NaviGpsD;
199
                                ToNaviCtrl.Param.Byte[5] = EE_Parameter.NaviGpsACC;
200
                                ToNaviCtrl.Param.Byte[6] = EE_Parameter.NaviGpsMinSat;
201
                ToNaviCtrl.Param.Byte[7] = EE_Parameter.NaviStickThreshold;
993 hbuss 202
                ToNaviCtrl.Param.Byte[8] = EE_Parameter.NaviOperatingRadius;
203
                ToNaviCtrl.Param.Byte[9] = EE_Parameter.NaviWindCorrection;
204
                ToNaviCtrl.Param.Byte[10] = EE_Parameter.NaviSpeedCompensation;
1064 hbuss 205
                                ToNaviCtrl.Param.Byte[11] = EE_Parameter.NaviAngleLimitation;
823 ingob 206
            break;
1241 killagreg 207
 
1448 killagreg 208
         case SPI_FCCMD_STICK:
1320 hbuss 209
              cli();
871 hbuss 210
                tmp = PPM_in[EE_Parameter.Kanalbelegung[K_GAS]];  if(tmp > 127) tmp = 127; else if(tmp < -127) tmp = -127;
846 hbuss 211
                                ToNaviCtrl.Param.Byte[0] = (char) tmp;
871 hbuss 212
                tmp = PPM_in[EE_Parameter.Kanalbelegung[K_GIER]]; if(tmp > 127) tmp = 127; else if(tmp < -127) tmp = -127;
846 hbuss 213
                                ToNaviCtrl.Param.Byte[1] = (char) tmp;
871 hbuss 214
                tmp = PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]]; if(tmp > 127) tmp = 127; else if(tmp < -127) tmp = -127;
846 hbuss 215
                                ToNaviCtrl.Param.Byte[2] = (char) tmp;
871 hbuss 216
                tmp = PPM_in[EE_Parameter.Kanalbelegung[K_NICK]]; if(tmp > 127) tmp = 127; else if(tmp < -127) tmp = -127;
1320 hbuss 217
              sei();
846 hbuss 218
                                ToNaviCtrl.Param.Byte[3] = (char) tmp;
1377 hbuss 219
                                ToNaviCtrl.Param.Byte[4] = (unsigned char) Poti[0];
220
                                ToNaviCtrl.Param.Byte[5] = (unsigned char) Poti[1];
221
                                ToNaviCtrl.Param.Byte[6] = (unsigned char) Poti[2];
222
                    ToNaviCtrl.Param.Byte[7] = (unsigned char) Poti[3];
223
                                ToNaviCtrl.Param.Byte[8] = (unsigned char) Poti[4];
224
                                ToNaviCtrl.Param.Byte[9] = (unsigned char) Poti[5];
225
                                ToNaviCtrl.Param.Byte[10] = (unsigned char) Poti[6];
226
                                ToNaviCtrl.Param.Byte[11] = (unsigned char) Poti[7];
1241 killagreg 227
                        break;
1448 killagreg 228
                case SPI_FCCMD_MISC:
1241 killagreg 229
                        if(WinkelOut.CalcState > 5)
230
                        {
231
                                WinkelOut.CalcState = 0;
232
                                ToNaviCtrl.Param.Byte[0] = 5;
233
                        }
234
                        else ToNaviCtrl.Param.Byte[0] = WinkelOut.CalcState;
1312 hbuss 235
                        ToNaviCtrl.Param.Byte[1] = EE_Parameter.NaviPH_LoginTime;
1662 killagreg 236
                        ToNaviCtrl.Param.Int[1] = (int)(HoehenWert/5);
1312 hbuss 237
                        ToNaviCtrl.Param.Int[2] = (int)(SollHoehe/5);
238
                        ToNaviCtrl.Param.Byte[6] = EE_Parameter.NaviGpsPLimit;
239
                        ToNaviCtrl.Param.Byte[7] = EE_Parameter.NaviGpsILimit;
240
                        ToNaviCtrl.Param.Byte[8] = EE_Parameter.NaviGpsDLimit;
1377 hbuss 241
            ToNaviCtrl.Param.Byte[9] = (unsigned char) SenderOkay;
1600 killagreg 242
            ToNaviCtrl.Param.Byte[10] = (unsigned char) PPM_in[0];
1377 hbuss 243
                        ToNaviCtrl.Param.Byte[11] = DebugOut.Analog[7] / 4; //GasMischanteil
1241 killagreg 244
                        break;
1448 killagreg 245
                case SPI_FCCMD_VERSION:
246
                        ToNaviCtrl.Param.Byte[0] = VERSION_MAJOR;
247
                        ToNaviCtrl.Param.Byte[1] = VERSION_MINOR;
248
                        ToNaviCtrl.Param.Byte[2] = VERSION_PATCH;
249
                        ToNaviCtrl.Param.Byte[3] = NC_SPI_COMPATIBLE;
1241 killagreg 250
                        ToNaviCtrl.Param.Byte[4] = PlatinenVersion;
1702 holgerb 251
                        ToNaviCtrl.Param.Byte[5] = VersionInfo.HardwareError[0];
252
                        ToNaviCtrl.Param.Byte[6] = VersionInfo.HardwareError[1];
253
                        ToNaviCtrl.Param.Byte[7] = VersionInfo.HardwareError[2];
1765 killagreg 254
                        ToNaviCtrl.Param.Byte[8] = VersionInfo.HardwareError[3];
255
                        ToNaviCtrl.Param.Byte[9] = VersionInfo.HardwareError[4];
1241 killagreg 256
                break;
1448 killagreg 257
            case SPI_FCCMD_SERVOS:
1241 killagreg 258
                ToNaviCtrl.Param.Byte[0] = EE_Parameter.ServoNickRefresh;     // Parameters for the Servo Control
259
                        ToNaviCtrl.Param.Byte[1] = EE_Parameter.ServoCompInvert;
260
                        ToNaviCtrl.Param.Byte[2] = Parameter_ServoNickControl;
261
                        ToNaviCtrl.Param.Byte[3] = EE_Parameter.ServoNickComp;
262
                        ToNaviCtrl.Param.Byte[4] = EE_Parameter.ServoNickMin;
263
                        ToNaviCtrl.Param.Byte[5] = EE_Parameter.ServoNickMax;
264
                        ToNaviCtrl.Param.Byte[6] = Parameter_ServoRollControl;
265
                        ToNaviCtrl.Param.Byte[7] = EE_Parameter.ServoRollComp;
266
                        ToNaviCtrl.Param.Byte[8] = EE_Parameter.ServoRollMin;
267
                        ToNaviCtrl.Param.Byte[9] = EE_Parameter.ServoRollMax;
268
                break;
269
        }
270
 
1215 hbuss 271
  if(SPI_RxDataValid)
1051 killagreg 272
  {
1536 killagreg 273
   NaviDataOkay = 250;
855 hbuss 274
   if(abs(FromNaviCtrl.GPS_Nick) < 512 && abs(FromNaviCtrl.GPS_Roll) < 512 && (EE_Parameter.GlobalConfig & CFG_GPS_AKTIV))
854 hbuss 275
   {
823 ingob 276
    GPS_Nick = FromNaviCtrl.GPS_Nick;
277
    GPS_Roll = FromNaviCtrl.GPS_Roll;
854 hbuss 278
   }
1690 holgerb 279
 
280
        if(FromNaviCtrl.CompassValue <= 360)   KompassValue = FromNaviCtrl.CompassValue;
720 ingob 281
    KompassRichtung = ((540 + KompassValue - KompassStartwert) % 360) - 180;
1051 killagreg 282
 
855 hbuss 283
    if(FromNaviCtrl.BeepTime > beeptime && !WinkelOut.CalcState) beeptime = FromNaviCtrl.BeepTime;
1051 killagreg 284
 
823 ingob 285
          switch (FromNaviCtrl.Command)
286
          {
1448 killagreg 287
            case SPI_NCCMD_KALMAN:
1231 killagreg 288
                        FromNaviCtrl_Value.Kalman_K = FromNaviCtrl.Param.sByte[0];
289
                        FromNaviCtrl_Value.Kalman_MaxFusion = FromNaviCtrl.Param.sByte[1];
290
                        FromNaviCtrl_Value.Kalman_MaxDrift = FromNaviCtrl.Param.sByte[2];
291
                        FromNaviCtrl_Value.SerialDataOkay = FromNaviCtrl.Param.Byte[3];
1283 hbuss 292
                        FromNaviCtrl_Value.GpsZ = FromNaviCtrl.Param.Byte[4];
1664 holgerb 293
                        FromNC_Rotate_C = FromNaviCtrl.Param.Byte[5];
294
                        FromNC_Rotate_S = FromNaviCtrl.Param.Byte[6];
1231 killagreg 295
                        break;
1448 killagreg 296
 
297
                case SPI_NCCMD_VERSION:
298
                        NC_Version.Major = FromNaviCtrl.Param.Byte[0];
299
                        NC_Version.Minor = FromNaviCtrl.Param.Byte[1];
300
                        NC_Version.Patch = FromNaviCtrl.Param.Byte[2];
301
                        NC_Version.Compatible = FromNaviCtrl.Param.Byte[3];
302
                        NC_Version.Hardware = FromNaviCtrl.Param.Byte[4];
1702 holgerb 303
                        DebugOut.Status[0] |= FromNaviCtrl.Param.Byte[5];
1703 holgerb 304
                        NC_ErrorCode = FromNaviCtrl.Param.Byte[6];
1702 holgerb 305
                        DebugOut.Status[1] = (DebugOut.Status[1] & (0x01|0x02)) | (FromNaviCtrl.Param.Byte[6] & (0x04 | 0x08));
1448 killagreg 306
                        break;
1451 killagreg 307
 
308
                case SPI_NCCMD_GPSINFO:
309
                        GPSInfo.Flags = FromNaviCtrl.Param.Byte[0];
310
                        GPSInfo.NumOfSats = FromNaviCtrl.Param.Byte[1];
311
                        GPSInfo.SatFix = FromNaviCtrl.Param.Byte[2];
1690 holgerb 312
                        GPSInfo.Speed = FromNaviCtrl.Param.Byte[3];
1787 holgerb 313
                        GPSInfo.HomeDistance = FromNaviCtrl.Param.Int[2];
1472 killagreg 314
                        GPSInfo.HomeBearing = FromNaviCtrl.Param.sInt[3];
1790 holgerb 315
                        PPM_in[25] = (signed char) FromNaviCtrl.Param.Byte[8]; // WP_EVENT-Channel-Value
1451 killagreg 316
                        break;
317
 
823 ingob 318
                default:
1451 killagreg 319
                        break;
823 ingob 320
          }
720 ingob 321
  }
322
  else
323
  {
819 hbuss 324
//    KompassValue = 0;
325
//    KompassRichtung = 0;
720 ingob 326
        GPS_Nick = 0;
327
    GPS_Roll = 0;
328
  }
304 ingob 329
}
330
 
597 ingob 331
#endif
304 ingob 332
 
333