Subversion Repositories FlightCtrl

Rev

Rev 717 | Rev 723 | 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
 
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
 
708 ingob 13
volatile unsigned char     SPI_Buffer[sizeof(FromNaviCtrl_Value)];
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 
28
  SPSR = 0;  // SPI2X
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;
39
  ToNaviCtrl_Value.IntegralNick = 12345;
40
  ToNaviCtrl_Value.IntegralRoll = 56789;
41
  ToNaviCtrl_Value.StickNick = 100;
42
  ToNaviCtrl_Value.StickRoll = 150;//(char) StickRoll;
43
  ToNaviCtrl_Value.StickGier = 200;//(char) StickGier;
720 ingob 44
 
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;
708 ingob 53
   //if (!(SPSR & (1 << SPIF))) return;
54
   _delay_us(30);
304 ingob 55
 
56
   SLAVE_SELECT_PORT &=  ~(1 << SPI_SLAVE_SELECT);  // SelectSlave
617 ingob 57
 
708 ingob 58
   switch(command)
59
   {
60
     case SPI_CMD_VERSION: SPI_TX_Buffer  = (unsigned char *) &ToNaviCtrl_Version;
61
                                                   ToNaviCtrl_Chksum = (unsigned char *) &ToNaviCtrl_Version.Chksum;
62
            break;       
63
 
64
         case SPI_CMD_VALUE: SPI_TX_Buffer = (unsigned char *) &ToNaviCtrl_Value;
65
                             ToNaviCtrl_Chksum = (unsigned char *) &ToNaviCtrl_Value.Chksum;
66
            break;
67
 
68
   }
69
 
70
 
606 ingob 71
   SPITransferCompleted = 0;
304 ingob 72
   UpdateSPI_Buffer();                              // update buffer
73
   SPI_BufferIndex = 1;
708 ingob 74
  //ebugOut.Analog[16]++; 
304 ingob 75
   // -- Debug-Output ---
76
   //----
691 ingob 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");
79
   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 80
   SPDR = ToNaviCtrl_Value.Sync1;                  // Start transmission 
81
   *ToNaviCtrl_Chksum = ToNaviCtrl_Value.Sync1;
691 ingob 82
//     SLAVE_SELECT_PORT |=  (1 << SPI_SLAVE_SELECT);   // DeselectSlave
83
 
304 ingob 84
}
85
 
86
//------------------------------------------------------
87
//SIGNAL(SIG_SPI)
88
void SPI_TransmitByte(void)
89
{
708 ingob 90
   static unsigned char SPI_RXState = 0;
91
   unsigned char rxdata;
92
   static unsigned char rxchksum;
691 ingob 93
 
94
   if (SPITransferCompleted) return;
304 ingob 95
   if (!(SPSR & (1 << SPIF))) return;
691 ingob 96
 
708 ingob 97
  _delay_us(30);
98
 
606 ingob 99
  SLAVE_SELECT_PORT |=  (1 << SPI_SLAVE_SELECT);   // DeselectSlave
304 ingob 100
 
708 ingob 101
  rxdata = SPDR;
102
  switch ( SPI_RXState)
103
  {
104
  case 0:
105
 
106
                        SPI_RxBufferIndex = 0;
107
                        //DebugOut.Analog[17]++;
108
                        rxchksum = rxdata;  
109
                        if (rxdata == 0x81 )  { SPI_RXState  = 1;  }   // 1. Syncbyte ok 
110
 
111
           break;
112
 
113
   case 1:
114
                    if (rxdata == 0x55) { rxchksum += rxdata; SPI_RXState  = 2;  }   // 2. Syncbyte ok 
115
                 else SPI_RXState  = 0;    
116
                        //DebugOut.Analog[18]++;
117
           break;      
118
 
119
   case 2:
120
                   SPI_Buffer[SPI_RxBufferIndex++]= rxdata;             // get data
121
           //DebugOut.Analog[19]++;
122
           if (SPI_RxBufferIndex >= sizeof(FromNaviCtrl_Value))
123
                   {  
124
 
125
                if (rxdata == rxchksum)
126
                        {
127
                  unsigned char *ptr = (unsigned char *)&FromNaviCtrl_Value;
128
 
129
                          memcpy(ptr, (unsigned char *) SPI_Buffer,  sizeof(SPI_Buffer));
130
 
720 ingob 131
                          SPI_RxDataValid = 1;
708 ingob 132
                        }
720 ingob 133
                        else SPI_RxDataValid = 0;
708 ingob 134
 
135
                        SPI_RXState  = 0;  
136
                   }
137
                  else rxchksum += rxdata;
138
        break;   
139
 
140
  }            
141
 
142
   if (SPI_BufferIndex < sizeof(ToNaviCtrl_Value))  
606 ingob 143
     {
144
           SLAVE_SELECT_PORT &=  ~(1 << SPI_SLAVE_SELECT);  // SelectSlave
691 ingob 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");
147
           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 148
 
708 ingob 149
           SPDR = SPI_TX_Buffer[SPI_BufferIndex];
150
           *ToNaviCtrl_Chksum += SPI_TX_Buffer[SPI_BufferIndex];
691 ingob 151
        //   SLAVE_SELECT_PORT |=  (1 << SPI_SLAVE_SELECT);   // DeselectSlave
152
 
606 ingob 153
         }
708 ingob 154
         else SPITransferCompleted = 1;  
155
 
304 ingob 156
         SPI_BufferIndex++;
157
}
158
 
691 ingob 159
 
304 ingob 160
//------------------------------------------------------
161
void UpdateSPI_Buffer(void)
162
{
691 ingob 163
  static unsigned char i =0;
304 ingob 164
  cli();
708 ingob 165
  ToNaviCtrl_Value.Command = SPI_CMD_VALUE;
709 ingob 166
  ToNaviCtrl_Value.IntegralNick = (int) ( IntegralNick / 1024);
167
  ToNaviCtrl_Value.IntegralRoll = (int) (IntegralRoll / 1024);
708 ingob 168
  ToNaviCtrl_Value.StickNick = 4;
169
  ToNaviCtrl_Value.StickRoll = 5;//(char) StickRoll;
170
  ToNaviCtrl_Value.StickGier = 6;//(char) StickGier;
717 hbuss 171
  ToNaviCtrl_Value.GyroCompass = ErsatzKompass / GIER_GRAD_FAKTOR;
720 ingob 172
  ToNaviCtrl_Value.User1 = Parameter_UserParam1;
173
  ToNaviCtrl_Value.User2 = Parameter_UserParam2;
174
  ToNaviCtrl_Value.User3 = Parameter_UserParam3;
175
  ToNaviCtrl_Value.User4 = Parameter_UserParam4;
176
  ToNaviCtrl_Value.User5 = Parameter_UserParam5;
177
  ToNaviCtrl_Value.User6 = Parameter_UserParam6;
178
  ToNaviCtrl_Value.User7 = Parameter_UserParam7;
179
  ToNaviCtrl_Value.User8 = Parameter_UserParam8;
304 ingob 180
  sei();
691 ingob 181
 
720 ingob 182
  if (SPI_RxDataValid)
183
  {
184
    GPS_Nick = FromNaviCtrl_Value.GPS_Nick;
185
    GPS_Roll = FromNaviCtrl_Value.GPS_Roll;
186
    KompassValue = FromNaviCtrl_Value.CompassValue;
187
    KompassRichtung = ((540 + KompassValue - KompassStartwert) % 360) - 180;
188
 
189
    if (FromNaviCtrl_Value.BeepTime != 0) beeptime = FromNaviCtrl_Value.BeepTime;
190
  }
191
  else
192
  {
193
    KompassValue = 0;
194
    KompassRichtung = 0;
195
 
196
        GPS_Nick = 0;
197
    GPS_Roll = 0;
198
  }
304 ingob 199
}
200
 
597 ingob 201
#endif
304 ingob 202
 
203