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
 
708 ingob 5
struct str_ToNaviCtrl_Version   ToNaviCtrl_Version;
6
struct str_FromNaviCtrl_Version   FromNaviCtrl_Version;
7
struct str_ToNaviCtrl_Value   ToNaviCtrl_Value;
8
struct str_FromNaviCtrl_Value   FromNaviCtrl_Value;
9
 
304 ingob 10
unsigned char              SPI_BufferIndex;
708 ingob 11
unsigned char              SPI_RxBufferIndex;
606 ingob 12
 
819 hbuss 13
volatile unsigned char     SPI_Buffer[sizeof(FromNaviCtrl_Value) + 8];
708 ingob 14
unsigned char *SPI_TX_Buffer, *ToNaviCtrl_Chksum;
15
 
617 ingob 16
unsigned char SPITransferCompleted, SPI_ChkSum;
720 ingob 17
unsigned char SPI_RxDataValid;
18
 
597 ingob 19
#ifdef USE_SPI_COMMUNICATION
691 ingob 20
 
304 ingob 21
//------------------------------------------------------
22
void SPI_MasterInit(void)
23
{
24
  DDR_SPI |= (1<<DD_MOSI)|(1<<DD_SCK);    // Set MOSI and SCK output, all others input 
25
  SLAVE_SELECT_DDR_PORT |= (1 << SPI_SLAVE_SELECT);
26
 
691 ingob 27
  SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1)|(0<<SPR0)|(0<<SPIE);   // Enable SPI, Master, set clock rate fck/64 
723 hbuss 28
  SPSR = 0;//(1<<SPI2X);
691 ingob 29
 
304 ingob 30
  SLAVE_SELECT_PORT |=  (1 << SPI_SLAVE_SELECT);
606 ingob 31
  SPITransferCompleted = 1;
32
 
708 ingob 33
  //SPDR = 0x00;  // dummy write
606 ingob 34
 
720 ingob 35
  ToNaviCtrl_Value.Sync1 = 0xAA;
36
  ToNaviCtrl_Value.Sync2 = 0x83;
606 ingob 37
 
708 ingob 38
  ToNaviCtrl_Value.Command = SPI_CMD_VALUE;
819 hbuss 39
  ToNaviCtrl_Value.IntegralNick = 0;
40
  ToNaviCtrl_Value.IntegralRoll = 0;
41
  ToNaviCtrl_Value.StickNick = 0;
42
  ToNaviCtrl_Value.StickRoll = 0;//(char) StickRoll;
43
  ToNaviCtrl_Value.StickGier = 0;//(char) StickGier;
44
  ToNaviCtrl_Value.CalState = 0;
720 ingob 45
  SPI_RxDataValid = 0;
304 ingob 46
}
47
 
48
//------------------------------------------------------
708 ingob 49
void SPI_StartTransmitPacket(unsigned char command)
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
617 ingob 56
 
708 ingob 57
   switch(command)
58
   {
59
     case SPI_CMD_VERSION: SPI_TX_Buffer  = (unsigned char *) &ToNaviCtrl_Version;
60
                                                   ToNaviCtrl_Chksum = (unsigned char *) &ToNaviCtrl_Version.Chksum;
61
            break;       
62
 
63
         case SPI_CMD_VALUE: SPI_TX_Buffer = (unsigned char *) &ToNaviCtrl_Value;
64
                             ToNaviCtrl_Chksum = (unsigned char *) &ToNaviCtrl_Value.Chksum;
65
            break;
66
 
67
   }
723 hbuss 68
 
708 ingob 69
 
606 ingob 70
   SPITransferCompleted = 0;
304 ingob 71
   UpdateSPI_Buffer();                              // update buffer
72
   SPI_BufferIndex = 1;
708 ingob 73
  //ebugOut.Analog[16]++; 
304 ingob 74
   // -- Debug-Output ---
75
   //----
691 ingob 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");
78
   asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop");         asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop");
708 ingob 79
   SPDR = ToNaviCtrl_Value.Sync1;                  // Start transmission 
80
   *ToNaviCtrl_Chksum = ToNaviCtrl_Value.Sync1;
691 ingob 81
//     SLAVE_SELECT_PORT |=  (1 << SPI_SLAVE_SELECT);   // DeselectSlave
82
 
304 ingob 83
}
84
 
85
//------------------------------------------------------
86
//SIGNAL(SIG_SPI)
87
void SPI_TransmitByte(void)
88
{
708 ingob 89
   static unsigned char SPI_RXState = 0;
90
   unsigned char rxdata;
91
   static unsigned char rxchksum;
691 ingob 92
 
93
   if (SPITransferCompleted) return;
304 ingob 94
   if (!(SPSR & (1 << SPIF))) return;
723 hbuss 95
  SendSPI = 4;
691 ingob 96
 
723 hbuss 97
//   _delay_us(30); 
606 ingob 98
  SLAVE_SELECT_PORT |=  (1 << SPI_SLAVE_SELECT);   // DeselectSlave
304 ingob 99
 
708 ingob 100
  rxdata = SPDR;
101
  switch ( SPI_RXState)
102
  {
103
  case 0:
104
 
105
                        SPI_RxBufferIndex = 0;
106
                        //DebugOut.Analog[17]++;
107
                        rxchksum = rxdata;  
108
                        if (rxdata == 0x81 )  { SPI_RXState  = 1;  }   // 1. Syncbyte ok 
109
 
110
           break;
111
 
112
   case 1:
113
                    if (rxdata == 0x55) { rxchksum += rxdata; SPI_RXState  = 2;  }   // 2. Syncbyte ok 
114
                 else SPI_RXState  = 0;    
115
                        //DebugOut.Analog[18]++;
116
           break;      
117
 
118
   case 2:
119
                   SPI_Buffer[SPI_RxBufferIndex++]= rxdata;             // get data
120
           //DebugOut.Analog[19]++;
121
           if (SPI_RxBufferIndex >= sizeof(FromNaviCtrl_Value))
122
                   {  
123
 
124
                if (rxdata == rxchksum)
125
                        {
126
                  unsigned char *ptr = (unsigned char *)&FromNaviCtrl_Value;
127
 
128
                          memcpy(ptr, (unsigned char *) SPI_Buffer,  sizeof(SPI_Buffer));
129
 
720 ingob 130
                          SPI_RxDataValid = 1;
708 ingob 131
                        }
720 ingob 132
                        else SPI_RxDataValid = 0;
708 ingob 133
 
134
                        SPI_RXState  = 0;  
135
                   }
136
                  else rxchksum += rxdata;
137
        break;   
138
 
139
  }            
140
 
141
   if (SPI_BufferIndex < sizeof(ToNaviCtrl_Value))  
606 ingob 142
     {
143
           SLAVE_SELECT_PORT &=  ~(1 << SPI_SLAVE_SELECT);  // SelectSlave
691 ingob 144
           asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop");         asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop");
145
           asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop");         asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop");
146
           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 147
 
708 ingob 148
           SPDR = SPI_TX_Buffer[SPI_BufferIndex];
149
           *ToNaviCtrl_Chksum += SPI_TX_Buffer[SPI_BufferIndex];
691 ingob 150
        //   SLAVE_SELECT_PORT |=  (1 << SPI_SLAVE_SELECT);   // DeselectSlave
151
 
606 ingob 152
         }
708 ingob 153
         else SPITransferCompleted = 1;  
154
 
304 ingob 155
         SPI_BufferIndex++;
156
}
157
 
691 ingob 158
 
304 ingob 159
//------------------------------------------------------
160
void UpdateSPI_Buffer(void)
161
{
691 ingob 162
  static unsigned char i =0;
304 ingob 163
  cli();
708 ingob 164
  ToNaviCtrl_Value.Command = SPI_CMD_VALUE;
805 hbuss 165
  ToNaviCtrl_Value.IntegralNick = (int) (IntegralNick / 1024);
709 ingob 166
  ToNaviCtrl_Value.IntegralRoll = (int) (IntegralRoll / 1024);
744 hbuss 167
  ToNaviCtrl_Value.StickNick = (char) PPM_in[EE_Parameter.Kanalbelegung[K_NICK]];
168
  ToNaviCtrl_Value.StickRoll = (char) PPM_in[EE_Parameter.Kanalbelegung[K_ROLL]];
169
  ToNaviCtrl_Value.StickGier = (char) PPM_in[EE_Parameter.Kanalbelegung[K_GIER]];
717 hbuss 170
  ToNaviCtrl_Value.GyroCompass = ErsatzKompass / GIER_GRAD_FAKTOR;
819 hbuss 171
  ToNaviCtrl_Value.AccNick = (int) ACC_AMPLIFY * (NaviAccNick / NaviCntAcc);
172
  ToNaviCtrl_Value.AccRoll = (int) ACC_AMPLIFY * (Mittelwert_AccRoll / NaviCntAcc);
173
  NaviCntAcc = 0; NaviAccNick = 0; NaviAccRoll = 0;
720 ingob 174
  ToNaviCtrl_Value.User1 = Parameter_UserParam1;
175
  ToNaviCtrl_Value.User2 = Parameter_UserParam2;
176
  ToNaviCtrl_Value.User3 = Parameter_UserParam3;
177
  ToNaviCtrl_Value.User4 = Parameter_UserParam4;
178
  ToNaviCtrl_Value.User5 = Parameter_UserParam5;
179
  ToNaviCtrl_Value.User6 = Parameter_UserParam6;
180
  ToNaviCtrl_Value.User7 = Parameter_UserParam7;
819 hbuss 181
//  ToNaviCtrl_Value.User8 = Parameter_UserParam8;
182
  ToNaviCtrl_Value.CalState = WinkelOut.CalcState;
304 ingob 183
  sei();
691 ingob 184
 
720 ingob 185
  if (SPI_RxDataValid)
186
  {
187
    GPS_Nick = FromNaviCtrl_Value.GPS_Nick;
188
    GPS_Roll = FromNaviCtrl_Value.GPS_Roll;
189
    KompassValue = FromNaviCtrl_Value.CompassValue;
190
    KompassRichtung = ((540 + KompassValue - KompassStartwert) % 360) - 180;
191
 
723 hbuss 192
    if (FromNaviCtrl_Value.BeepTime > beeptime) beeptime = FromNaviCtrl_Value.BeepTime;
720 ingob 193
  }
194
  else
195
  {
819 hbuss 196
//    KompassValue = 0;
197
//    KompassRichtung = 0;
720 ingob 198
 
199
        GPS_Nick = 0;
200
    GPS_Roll = 0;
201
  }
304 ingob 202
}
203
 
597 ingob 204
#endif
304 ingob 205
 
206