Subversion Repositories FlightCtrl

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
715 Nick666 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;
10
 
11
unsigned char SPITransferCompleted, SPI_ChkSum;
12
#ifdef USE_SPI_COMMUNICATION
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);
21
  SPITransferCompleted = 1;
22
 
23
  ToNaviCtrl.Sync1 = 0x81;
24
  ToNaviCtrl.Sync2 = 0x55;
25
 
26
  ToNaviCtrl.Comp = 12;
27
  ToNaviCtrl.IntegralNick = 12345;
28
  ToNaviCtrl.IntegralRoll = 56789;
29
  ToNaviCtrl.StickNick = 100;
30
  ToNaviCtrl.StickRoll = 150;//(char) StickRoll;
31
  ToNaviCtrl.StickGier = 200;//(char) StickGier;
32
 
33
}
34
 
35
//------------------------------------------------------
36
void SPI_StartTransmitPacket(void)
37
{
38
   //if ((SLAVE_SELECT_PORT & (1 << SPI_SLAVE_SELECT)) == 0) return;    // transfer of prev. packet not completed
39
   if (!SPITransferCompleted) return;
40
 
41
   SLAVE_SELECT_PORT &=  ~(1 << SPI_SLAVE_SELECT);  // SelectSlave
42
 
43
   SPITransferCompleted = 0;
44
   UpdateSPI_Buffer();                              // update buffer
45
   SPI_BufferIndex = 1;
46
   DebugOut.Analog[16]++;
47
   // -- Debug-Output ---
48
/*         DebugOut.Analog[20] = FromNaviCtrl.Comp;
49
       DebugOut.Analog[21] = FromNaviCtrl.GPS_Nick;
50
       DebugOut.Analog[22] = FromNaviCtrl.GPS_Roll;
51
       DebugOut.Analog[23] = FromNaviCtrl.CompassValue;
52
*/  
53
   //----
54
   SPDR = ToNaviCtrl.Sync1;                  // Start transmission 
55
   ToNaviCtrl.ChkSum = ToNaviCtrl.Sync1;
56
}
57
 
58
//------------------------------------------------------
59
//SIGNAL(SIG_SPI)
60
void SPI_TransmitByte(void)
61
{
62
   if (!(SPSR & (1 << SPIF))) return;
63
  SLAVE_SELECT_PORT |=  (1 << SPI_SLAVE_SELECT);   // DeselectSlave
64
 
65
          if (SPI_BufferIndex < sizeof(FromNaviCtrl))
66
      {
67
            SPI_Buffer[SPI_BufferIndex]= SPDR;             // get data 
68
 
69
//              if (SPI_BufferIndex < 32 ) DebugOut.Analog[26+SPI_BufferIndex] = SPI_Buffer[SPI_BufferIndex];
70
 
71
        //if(SPDR!= 0x00) DebugOut.Analog[19]++; ;             
72
          }
73
 
74
   if (SPI_BufferIndex < sizeof(ToNaviCtrl))  
75
     {
76
           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");
78
           SLAVE_SELECT_PORT &=  ~(1 << SPI_SLAVE_SELECT);  // SelectSlave
79
           asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop");
80
           asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop");
81
 
82
           SPDR = Ptr_buffer[SPI_BufferIndex];
83
           ToNaviCtrl.ChkSum += Ptr_buffer[SPI_BufferIndex];
84
         }
85
   else
86
     {
87
           unsigned char *ptr = (unsigned char *)&FromNaviCtrl;
88
 
89
       SPITransferCompleted = 1;  
90
       memcpy(ptr, (unsigned char *) SPI_Buffer,  sizeof(SPI_Buffer));
91
         }
92
 
93
         SPI_BufferIndex++;
94
}
95
 
96
//------------------------------------------------------
97
void UpdateSPI_Buffer(void)
98
{
99
  /*static unsigned char i =0;
100
  cli();
101
  ToNaviCtrl.Comp = SPI_PROTOCOL_COMP;
102
  ToNaviCtrl.IntegralNick = (int) (IntegralNick >> 4);
103
  ToNaviCtrl.IntegralRoll = (int) (IntegralRoll >> 4);
104
  ToNaviCtrl.StickNick = 4;
105
  ToNaviCtrl.StickRoll = 5;//(char) StickRoll;
106
  ToNaviCtrl.StickGier = 6;//(char) StickGier;
107
  sei();
108
  */
109
}
110
 
111
#endif
112
 
113