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"
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;
708 ingob 10
 
304 ingob 11
unsigned char              SPI_BufferIndex;
708 ingob 12
unsigned char              SPI_RxBufferIndex;
606 ingob 13
 
882 hbuss 14
volatile unsigned char     SPI_Buffer[sizeof(FromNaviCtrl)];
823 ingob 15
unsigned char *SPI_TX_Buffer;
708 ingob 16
 
617 ingob 17
unsigned char SPITransferCompleted, SPI_ChkSum;
720 ingob 18
unsigned char SPI_RxDataValid;
19
 
991 ingob 20
unsigned char SPI_CommandSequence[] = { SPI_CMD_USER, SPI_CMD_STICK, SPI_CMD_PARAMETER1, SPI_CMD_STICK, SPI_CMD_CAL_COMPASS, SPI_CMD_VERSION };
823 ingob 21
unsigned char SPI_CommandCounter = 0;
22
 
597 ingob 23
#ifdef USE_SPI_COMMUNICATION
691 ingob 24
 
304 ingob 25
//------------------------------------------------------
26
void SPI_MasterInit(void)
27
{
28
  DDR_SPI |= (1<<DD_MOSI)|(1<<DD_SCK);    // Set MOSI and SCK output, all others input 
29
  SLAVE_SELECT_DDR_PORT |= (1 << SPI_SLAVE_SELECT);
30
 
691 ingob 31
  SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1)|(0<<SPR0)|(0<<SPIE);   // Enable SPI, Master, set clock rate fck/64 
723 hbuss 32
  SPSR = 0;//(1<<SPI2X);
691 ingob 33
 
304 ingob 34
  SLAVE_SELECT_PORT |=  (1 << SPI_SLAVE_SELECT);
606 ingob 35
  SPITransferCompleted = 1;
36
 
708 ingob 37
  //SPDR = 0x00;  // dummy write
606 ingob 38
 
823 ingob 39
  ToNaviCtrl.Sync1 = 0xAA;
40
  ToNaviCtrl.Sync2 = 0x83;
606 ingob 41
 
823 ingob 42
  ToNaviCtrl.Command = SPI_CMD_USER;
43
  ToNaviCtrl.IntegralNick = 0;
44
  ToNaviCtrl.IntegralRoll = 0;
720 ingob 45
  SPI_RxDataValid = 0;
304 ingob 46
}
47
 
48
//------------------------------------------------------
823 ingob 49
void SPI_StartTransmitPacket(void)
304 ingob 50
{
606 ingob 51
   //if ((SLAVE_SELECT_PORT & (1 << SPI_SLAVE_SELECT)) == 0) return;    // transfer of prev. packet not completed
52
   if (!SPITransferCompleted) return;
723 hbuss 53
//   _delay_us(30); 
304 ingob 54
 
55
   SLAVE_SELECT_PORT &=  ~(1 << SPI_SLAVE_SELECT);  // SelectSlave
823 ingob 56
   SPI_TX_Buffer = (unsigned char *) &ToNaviCtrl;
617 ingob 57
 
823 ingob 58
   ToNaviCtrl.Command = SPI_CommandSequence[SPI_CommandCounter++];
59
   if (SPI_CommandCounter >= sizeof(SPI_CommandSequence)) SPI_CommandCounter = 0;
708 ingob 60
 
606 ingob 61
   SPITransferCompleted = 0;
304 ingob 62
   UpdateSPI_Buffer();                              // update buffer
823 ingob 63
 
304 ingob 64
   SPI_BufferIndex = 1;
708 ingob 65
  //ebugOut.Analog[16]++; 
304 ingob 66
   // -- Debug-Output ---
67
   //----
691 ingob 68
   asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop");         asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop");
69
   asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop");         asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop");
70
   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 71
   ToNaviCtrl.Chksum = ToNaviCtrl.Sync1;
72
   SPDR = ToNaviCtrl.Sync1;                  // Start transmission 
691 ingob 73
//     SLAVE_SELECT_PORT |=  (1 << SPI_SLAVE_SELECT);   // DeselectSlave
74
 
304 ingob 75
}
76
 
77
//------------------------------------------------------
78
//SIGNAL(SIG_SPI)
79
void SPI_TransmitByte(void)
80
{
708 ingob 81
   static unsigned char SPI_RXState = 0;
82
   unsigned char rxdata;
83
   static unsigned char rxchksum;
691 ingob 84
 
85
   if (SPITransferCompleted) return;
304 ingob 86
   if (!(SPSR & (1 << SPIF))) return;
723 hbuss 87
  SendSPI = 4;
691 ingob 88
 
723 hbuss 89
//   _delay_us(30); 
606 ingob 90
  SLAVE_SELECT_PORT |=  (1 << SPI_SLAVE_SELECT);   // DeselectSlave
304 ingob 91
 
708 ingob 92
  rxdata = SPDR;
93
  switch ( SPI_RXState)
94
  {
95
  case 0:
96
 
97
                        SPI_RxBufferIndex = 0;
98
                        //DebugOut.Analog[17]++;
99
                        rxchksum = rxdata;  
100
                        if (rxdata == 0x81 )  { SPI_RXState  = 1;  }   // 1. Syncbyte ok 
101
 
102
           break;
103
 
104
   case 1:
105
                    if (rxdata == 0x55) { rxchksum += rxdata; SPI_RXState  = 2;  }   // 2. Syncbyte ok 
106
                 else SPI_RXState  = 0;    
107
                        //DebugOut.Analog[18]++;
108
           break;      
109
 
110
   case 2:
111
                   SPI_Buffer[SPI_RxBufferIndex++]= rxdata;             // get data
112
           //DebugOut.Analog[19]++;
823 ingob 113
           if (SPI_RxBufferIndex >= sizeof(FromNaviCtrl))
708 ingob 114
                   {  
115
 
116
                if (rxdata == rxchksum)
117
                        {
823 ingob 118
                  unsigned char *ptr = (unsigned char *)&FromNaviCtrl;
708 ingob 119
 
120
                          memcpy(ptr, (unsigned char *) SPI_Buffer,  sizeof(SPI_Buffer));
121
 
720 ingob 122
                          SPI_RxDataValid = 1;
708 ingob 123
                        }
720 ingob 124
                        else SPI_RxDataValid = 0;
708 ingob 125
 
126
                        SPI_RXState  = 0;  
127
                   }
128
                  else rxchksum += rxdata;
129
        break;   
130
 
131
  }            
132
 
823 ingob 133
   if (SPI_BufferIndex < sizeof(ToNaviCtrl))  
606 ingob 134
     {
135
           SLAVE_SELECT_PORT &=  ~(1 << SPI_SLAVE_SELECT);  // SelectSlave
691 ingob 136
           asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop");         asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop");
137
           asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop");         asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop");
138
           asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop");         asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop");
617 ingob 139
 
708 ingob 140
           SPDR = SPI_TX_Buffer[SPI_BufferIndex];
823 ingob 141
           ToNaviCtrl.Chksum += SPI_TX_Buffer[SPI_BufferIndex];
691 ingob 142
        //   SLAVE_SELECT_PORT |=  (1 << SPI_SLAVE_SELECT);   // DeselectSlave
143
 
606 ingob 144
         }
708 ingob 145
         else SPITransferCompleted = 1;  
146
 
304 ingob 147
         SPI_BufferIndex++;
148
}
149
 
691 ingob 150
 
304 ingob 151
//------------------------------------------------------
152
void UpdateSPI_Buffer(void)
153
{
691 ingob 154
  static unsigned char i =0;
846 hbuss 155
  signed int tmp;
304 ingob 156
  cli();
823 ingob 157
 
846 hbuss 158
  ToNaviCtrl.IntegralNick = (int) (IntegralNick / 108);
159
  ToNaviCtrl.IntegralRoll = (int) (IntegralRoll / 108);
823 ingob 160
  ToNaviCtrl.GyroCompass = ErsatzKompass / GIER_GRAD_FAKTOR;
161
  ToNaviCtrl.AccNick = (int) ACC_AMPLIFY * (NaviAccNick / NaviCntAcc);
846 hbuss 162
  ToNaviCtrl.AccRoll = (int) ACC_AMPLIFY * (NaviAccRoll / NaviCntAcc);
819 hbuss 163
  NaviCntAcc = 0; NaviAccNick = 0; NaviAccRoll = 0;
823 ingob 164
//  ToNaviCtrl.User8 = Parameter_UserParam8;
165
//  ToNaviCtrl.CalState = WinkelOut.CalcState;
166
 
167
   switch(ToNaviCtrl.Command)  // 
168
   {
169
         case SPI_CMD_USER:
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;
178
                                ToNaviCtrl.Param.Byte[8] = (unsigned char) MikroKopterFlags;
179
                MikroKopterFlags &= ~(FLAG_CALIBRATE | FLAG_START);
180
                    ToNaviCtrl.Param.Byte[9] = (unsigned char) UBat;
181
                    ToNaviCtrl.Param.Byte[10] =(unsigned char) EE_Parameter.UnterspannungsWarnung;
182
                    ToNaviCtrl.Param.Byte[11] =(unsigned char) eeprom_read_byte(&EEPromArray[EEPROM_ADR_ACTIVE_SET]);
183
        break;
184
         case SPI_CMD_PARAMETER1:
993 hbuss 185
                                ToNaviCtrl.Param.Byte[0] = EE_Parameter.NaviGpsModeControl;     // Parameters for the Naviboard
186
                                ToNaviCtrl.Param.Byte[1] = EE_Parameter.NaviGpsGain;    
187
                                ToNaviCtrl.Param.Byte[2] = EE_Parameter.NaviGpsP;        
188
                                ToNaviCtrl.Param.Byte[3] = EE_Parameter.NaviGpsI;        
189
                                ToNaviCtrl.Param.Byte[4] = EE_Parameter.NaviGpsD;        
190
                                ToNaviCtrl.Param.Byte[5] = EE_Parameter.NaviGpsACC;        
921 hbuss 191
                                ToNaviCtrl.Param.Byte[6] = EE_Parameter.NaviGpsMinSat;        
192
                ToNaviCtrl.Param.Byte[7] = EE_Parameter.NaviStickThreshold;        
993 hbuss 193
                ToNaviCtrl.Param.Byte[8] = EE_Parameter.NaviOperatingRadius;
194
                ToNaviCtrl.Param.Byte[9] = EE_Parameter.NaviWindCorrection;
195
                ToNaviCtrl.Param.Byte[10] = EE_Parameter.NaviSpeedCompensation;
823 ingob 196
            break;
197
         case SPI_CMD_STICK:
871 hbuss 198
                tmp = PPM_in[EE_Parameter.Kanalbelegung[K_GAS]];  if(tmp > 127) tmp = 127; else if(tmp < -127) tmp = -127;
846 hbuss 199
                                ToNaviCtrl.Param.Byte[0] = (char) tmp;
871 hbuss 200
                tmp = PPM_in[EE_Parameter.Kanalbelegung[K_GIER]]; if(tmp > 127) tmp = 127; else if(tmp < -127) tmp = -127;
846 hbuss 201
                                ToNaviCtrl.Param.Byte[1] = (char) tmp;
871 hbuss 202
                tmp = PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]]; if(tmp > 127) tmp = 127; else if(tmp < -127) tmp = -127;
846 hbuss 203
                                ToNaviCtrl.Param.Byte[2] = (char) tmp;
871 hbuss 204
                tmp = PPM_in[EE_Parameter.Kanalbelegung[K_NICK]]; if(tmp > 127) tmp = 127; else if(tmp < -127) tmp = -127;
846 hbuss 205
                                ToNaviCtrl.Param.Byte[3] = (char) tmp;
206
                                ToNaviCtrl.Param.Byte[4] = (unsigned char) Poti1;
207
                                ToNaviCtrl.Param.Byte[5] = (unsigned char) Poti2;
208
                                ToNaviCtrl.Param.Byte[6] = (unsigned char) Poti3;
209
                    ToNaviCtrl.Param.Byte[7] = (unsigned char) Poti4;
855 hbuss 210
                    ToNaviCtrl.Param.Byte[8] = (unsigned char) SenderOkay;
823 ingob 211
            break;
212
         case SPI_CMD_CAL_COMPASS:
855 hbuss 213
                if(WinkelOut.CalcState > 5)  
214
                  {
215
                    WinkelOut.CalcState = 0;    
216
                    ToNaviCtrl.Param.Byte[0] = 5;
217
                  }  
218
                                else ToNaviCtrl.Param.Byte[0] = WinkelOut.CalcState;
823 ingob 219
            break;
991 ingob 220
 
221
         case SPI_CMD_VERSION:
222
                                ToNaviCtrl.Param.Byte[0] = VersionInfo.Hauptversion;
223
                                ToNaviCtrl.Param.Byte[1] = VersionInfo.Nebenversion;
224
                                ToNaviCtrl.Param.Byte[2] = VersionInfo.PCKompatibel;
225
                                ToNaviCtrl.Param.Byte[3] = VersionInfo.Patch;
226
            break;
823 ingob 227
   }
228
 
304 ingob 229
  sei();
855 hbuss 230
 
720 ingob 231
  if (SPI_RxDataValid)
232
  {
855 hbuss 233
   if(abs(FromNaviCtrl.GPS_Nick) < 512 && abs(FromNaviCtrl.GPS_Roll) < 512 && (EE_Parameter.GlobalConfig & CFG_GPS_AKTIV))
854 hbuss 234
   {
823 ingob 235
    GPS_Nick = FromNaviCtrl.GPS_Nick;
236
    GPS_Roll = FromNaviCtrl.GPS_Roll;
854 hbuss 237
   }
238
    if(FromNaviCtrl.CompassValue <= 360)   KompassValue = FromNaviCtrl.CompassValue;
720 ingob 239
    KompassRichtung = ((540 + KompassValue - KompassStartwert) % 360) - 180;
240
 
855 hbuss 241
    if(FromNaviCtrl.BeepTime > beeptime && !WinkelOut.CalcState) beeptime = FromNaviCtrl.BeepTime;
823 ingob 242
 
243
          switch (FromNaviCtrl.Command)
244
          {
992 hbuss 245
            case  SPI_KALMAN:
246
               FromNaviCtrl_Value.Kalman_K = FromNaviCtrl.Param.Byte[0];
247
               FromNaviCtrl_Value.Kalman_MaxFusion = FromNaviCtrl.Param.Byte[1];
248
               FromNaviCtrl_Value.Kalman_MaxDrift = FromNaviCtrl.Param.Byte[2];
249
               break;
250
 
823 ingob 251
            case  SPI_CMD_OSD_DATA:
978 hbuss 252
               FromNaviCtrl_Value.OsdBar = FromNaviCtrl.Param.Byte[0];
253
               FromNaviCtrl_Value.Distance = FromNaviCtrl.Param.Int[1];
992 hbuss 254
               break;
823 ingob 255
 
256
            case  SPI_CMD_GPS_POS:
257
//                                ToFlightCtrl.Param.Long[0]  = GPS_Data.Longitude;
258
//                                ToFlightCtrl.Param.Long[1]  = GPS_Data.Latitude;
259
                  break;
260
 
261
            case  SPI_CMD_GPS_TARGET:
262
//                                ToFlightCtrl.Param.Long[0]  = GPS_Data.TargetLongitude;
263
//                                ToFlightCtrl.Param.Long[1]  = GPS_Data.TargetLatitude;
264
                  break;
265
 
266
                default:
267
                  break;
268
          }
720 ingob 269
  }
270
  else
271
  {
819 hbuss 272
//    KompassValue = 0;
273
//    KompassRichtung = 0;
720 ingob 274
 
275
        GPS_Nick = 0;
276
    GPS_Roll = 0;
277
  }
304 ingob 278
}
279
 
597 ingob 280
#endif
304 ingob 281
 
282