Subversion Repositories FlightCtrl

Rev

Rev 515 | Blame | Compare with Previous | Last modification | View Log | RSS feed

// ######################## SPI - FlightCtrl ###################
#include "main.h"


struct str_ToNaviCtrl      ToNaviCtrl;
struct str_FromNaviCtrl    FromNaviCtrl;
unsigned char              SPI_BufferIndex;
volatile unsigned char     SPI_Buffer[sizeof(FromNaviCtrl)];
unsigned char *Ptr_buffer = (unsigned char *) &ToNaviCtrl;

//------------------------------------------------------
void SPI_MasterInit(void)
{
  DDR_SPI |= (1<<DD_MOSI)|(1<<DD_SCK);    // Set MOSI and SCK output, all others input
  SLAVE_SELECT_DDR_PORT |= (1 << SPI_SLAVE_SELECT);
   
  SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1)|(0<<SPIE);   // Enable SPI, Master, set clock rate fck/64
  SLAVE_SELECT_PORT |=  (1 << SPI_SLAVE_SELECT);
}

//------------------------------------------------------
void SPI_StartTransmitPacket(void)
{
   if ((SLAVE_SELECT_PORT & (1 << SPI_SLAVE_SELECT)) == 0) return;    // transfer of prev. packet not completed
   
   SLAVE_SELECT_PORT &=  ~(1 << SPI_SLAVE_SELECT);  // SelectSlave
   UpdateSPI_Buffer();                              // update buffer
   SPI_BufferIndex = 1;
   DebugOut.Analog[16]++;
   // -- Debug-Output ---
     cli();
           DebugOut.Analog[20] = FromNaviCtrl.Comp;
       DebugOut.Analog[21] = FromNaviCtrl.GPS_Nick;
       DebugOut.Analog[22] = FromNaviCtrl.GPS_Roll;
       DebugOut.Analog[23] = FromNaviCtrl.CompassValue;
         sei();
 
   
   //----
   SPDR = ToNaviCtrl.Comp;                  // Start transmission
}

//------------------------------------------------------
//SIGNAL(SIG_SPI)
void SPI_TransmitByte(void)
{
   if (!(SPSR & (1 << SPIF))) return;
   
          if (SPI_BufferIndex < sizeof(FromNaviCtrl))
      { SPI_Buffer[SPI_BufferIndex]= SPDR;             // get data
               
        //if(SPDR!= 0x00) DebugOut.Analog[19]++; ;            
          }
 
   if (SPI_BufferIndex < sizeof(ToNaviCtrl))  
     { SPDR = Ptr_buffer[SPI_BufferIndex];
       
         }
   else
     {
           unsigned char *ptr = (unsigned char *)&FromNaviCtrl;
           unsigned char i;
         
       SLAVE_SELECT_PORT |=  (1 << SPI_SLAVE_SELECT);   // DeselectSlave
           
       memcpy(ptr, (unsigned char *) SPI_Buffer,  sizeof(SPI_Buffer));
         }
         
         SPI_BufferIndex++;
}

//------------------------------------------------------
void UpdateSPI_Buffer(void)
{
  cli();
  ToNaviCtrl.Comp = SPI_PROTOCOL_COMP;
  ToNaviCtrl.IntegralNick = (int) (IntegralNick >> 4);
  ToNaviCtrl.IntegralRoll = (int) (IntegralRoll >> 4);
  ToNaviCtrl.StickNick = (char) StickNick;
  ToNaviCtrl.StickRoll = (char) StickRoll;
  ToNaviCtrl.StickGier = (char) StickGier;
  sei();
}