Subversion Repositories FlightCtrl

Rev

Rev 597 | 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
 
5
struct str_ToNaviCtrl      ToNaviCtrl;
6
struct str_FromNaviCtrl    FromNaviCtrl;
7
unsigned char              SPI_BufferIndex;
8
volatile unsigned char     SPI_Buffer[sizeof(FromNaviCtrl)];
9
unsigned char *Ptr_buffer = (unsigned char *) &ToNaviCtrl;
606 ingob 10
 
11
unsigned char SPITransferCompleted;
597 ingob 12
#ifdef USE_SPI_COMMUNICATION
304 ingob 13
//------------------------------------------------------
14
void SPI_MasterInit(void)
15
{
16
  DDR_SPI |= (1<<DD_MOSI)|(1<<DD_SCK);    // Set MOSI and SCK output, all others input 
17
  SLAVE_SELECT_DDR_PORT |= (1 << SPI_SLAVE_SELECT);
18
 
19
  SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1)|(0<<SPIE);   // Enable SPI, Master, set clock rate fck/64 
20
  SLAVE_SELECT_PORT |=  (1 << SPI_SLAVE_SELECT);
606 ingob 21
  SPITransferCompleted = 1;
22
 
23
  ToNaviCtrl.Sync1 = 0x81;
24
  ToNaviCtrl.Sync2 = 0x55;
25
 
26
 
304 ingob 27
}
28
 
29
//------------------------------------------------------
30
void SPI_StartTransmitPacket(void)
31
{
606 ingob 32
   //if ((SLAVE_SELECT_PORT & (1 << SPI_SLAVE_SELECT)) == 0) return;    // transfer of prev. packet not completed
33
   if (!SPITransferCompleted) return;
304 ingob 34
 
35
   SLAVE_SELECT_PORT &=  ~(1 << SPI_SLAVE_SELECT);  // SelectSlave
606 ingob 36
   SPITransferCompleted = 0;
304 ingob 37
   UpdateSPI_Buffer();                              // update buffer
38
   SPI_BufferIndex = 1;
39
   DebugOut.Analog[16]++;
40
   // -- Debug-Output ---
596 ingob 41
/*         DebugOut.Analog[20] = FromNaviCtrl.Comp;
304 ingob 42
       DebugOut.Analog[21] = FromNaviCtrl.GPS_Nick;
43
       DebugOut.Analog[22] = FromNaviCtrl.GPS_Roll;
44
       DebugOut.Analog[23] = FromNaviCtrl.CompassValue;
596 ingob 45
*/  
304 ingob 46
   //----
606 ingob 47
   SPDR = ToNaviCtrl.Sync1;                  // Start transmission 
304 ingob 48
}
49
 
50
//------------------------------------------------------
51
//SIGNAL(SIG_SPI)
52
void SPI_TransmitByte(void)
53
{
54
   if (!(SPSR & (1 << SPIF))) return;
606 ingob 55
  SLAVE_SELECT_PORT |=  (1 << SPI_SLAVE_SELECT);   // DeselectSlave
56
 
304 ingob 57
          if (SPI_BufferIndex < sizeof(FromNaviCtrl))
606 ingob 58
      {
59
            SPI_Buffer[SPI_BufferIndex]= SPDR;             // get data 
596 ingob 60
//              if (SPI_BufferIndex < 32 ) DebugOut.Analog[26+SPI_BufferIndex] = SPI_Buffer[SPI_BufferIndex];
61
 
304 ingob 62
        //if(SPDR!= 0x00) DebugOut.Analog[19]++; ;             
63
          }
64
 
65
   if (SPI_BufferIndex < sizeof(ToNaviCtrl))  
606 ingob 66
     {
67
           asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop");
68
           SLAVE_SELECT_PORT &=  ~(1 << SPI_SLAVE_SELECT);  // SelectSlave
69
           asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop");
70
           SPDR = Ptr_buffer[SPI_BufferIndex];
71
         }
304 ingob 72
   else
73
     {
74
           unsigned char *ptr = (unsigned char *)&FromNaviCtrl;
596 ingob 75
 
606 ingob 76
       SPITransferCompleted = 1;  
304 ingob 77
       memcpy(ptr, (unsigned char *) SPI_Buffer,  sizeof(SPI_Buffer));
78
         }
79
 
80
         SPI_BufferIndex++;
81
}
82
 
83
//------------------------------------------------------
84
void UpdateSPI_Buffer(void)
85
{
606 ingob 86
  static unsigned char i =0;
304 ingob 87
  cli();
88
  ToNaviCtrl.Comp = SPI_PROTOCOL_COMP;
89
  ToNaviCtrl.IntegralNick = (int) (IntegralNick >> 4);
90
  ToNaviCtrl.IntegralRoll = (int) (IntegralRoll >> 4);
606 ingob 91
  ToNaviCtrl.StickNick = 4;
92
  ToNaviCtrl.StickRoll = 5;//(char) StickRoll;
93
  ToNaviCtrl.StickGier = 6;//(char) StickGier;
304 ingob 94
  sei();
95
}
96
 
597 ingob 97
#endif
304 ingob 98
 
99