Subversion Repositories FlightCtrl

Rev

Rev 597 | Rev 617 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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