Subversion Repositories FlightCtrl

Rev

Rev 683 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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