Subversion Repositories FlightCtrl

Rev

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