Subversion Repositories FlightCtrl

Rev

Details | Last modification | View Log | RSS feed

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