Subversion Repositories FlightCtrl

Rev

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