Subversion Repositories FlightCtrl

Rev

Rev 617 | Rev 708 | 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
691 ingob 13
 
304 ingob 14
//------------------------------------------------------
15
void SPI_MasterInit(void)
16
{
17
  DDR_SPI |= (1<<DD_MOSI)|(1<<DD_SCK);    // Set MOSI and SCK output, all others input 
18
  SLAVE_SELECT_DDR_PORT |= (1 << SPI_SLAVE_SELECT);
19
 
691 ingob 20
  SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1)|(0<<SPR0)|(0<<SPIE);   // Enable SPI, Master, set clock rate fck/64 
21
  SPSR = 0;  // SPI2X
22
 
304 ingob 23
  SLAVE_SELECT_PORT |=  (1 << SPI_SLAVE_SELECT);
606 ingob 24
  SPITransferCompleted = 1;
25
 
26
  ToNaviCtrl.Sync1 = 0x81;
27
  ToNaviCtrl.Sync2 = 0x55;
28
 
617 ingob 29
  ToNaviCtrl.Comp = 12;
30
  ToNaviCtrl.IntegralNick = 12345;
31
  ToNaviCtrl.IntegralRoll = 56789;
32
  ToNaviCtrl.StickNick = 100;
33
  ToNaviCtrl.StickRoll = 150;//(char) StickRoll;
34
  ToNaviCtrl.StickGier = 200;//(char) StickGier;
606 ingob 35
 
304 ingob 36
}
37
 
38
//------------------------------------------------------
39
void SPI_StartTransmitPacket(void)
40
{
606 ingob 41
   //if ((SLAVE_SELECT_PORT & (1 << SPI_SLAVE_SELECT)) == 0) return;    // transfer of prev. packet not completed
42
   if (!SPITransferCompleted) return;
304 ingob 43
 
44
   SLAVE_SELECT_PORT &=  ~(1 << SPI_SLAVE_SELECT);  // SelectSlave
617 ingob 45
 
606 ingob 46
   SPITransferCompleted = 0;
304 ingob 47
   UpdateSPI_Buffer();                              // update buffer
48
   SPI_BufferIndex = 1;
49
   DebugOut.Analog[16]++;
50
   // -- Debug-Output ---
51
   //----
691 ingob 52
   asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop");         asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop");
53
   asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop");         asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop");
54
   asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop");         asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop");
606 ingob 55
   SPDR = ToNaviCtrl.Sync1;                  // Start transmission 
617 ingob 56
   ToNaviCtrl.ChkSum = ToNaviCtrl.Sync1;
691 ingob 57
//     SLAVE_SELECT_PORT |=  (1 << SPI_SLAVE_SELECT);   // DeselectSlave
58
 
304 ingob 59
}
60
 
61
//------------------------------------------------------
62
//SIGNAL(SIG_SPI)
63
void SPI_TransmitByte(void)
64
{
691 ingob 65
 
66
   if (SPITransferCompleted) return;
304 ingob 67
   if (!(SPSR & (1 << SPIF))) return;
691 ingob 68
 
606 ingob 69
  SLAVE_SELECT_PORT |=  (1 << SPI_SLAVE_SELECT);   // DeselectSlave
70
 
304 ingob 71
          if (SPI_BufferIndex < sizeof(FromNaviCtrl))
606 ingob 72
      {
73
            SPI_Buffer[SPI_BufferIndex]= SPDR;             // get data 
617 ingob 74
 
596 ingob 75
//              if (SPI_BufferIndex < 32 ) DebugOut.Analog[26+SPI_BufferIndex] = SPI_Buffer[SPI_BufferIndex];
76
 
304 ingob 77
        //if(SPDR!= 0x00) DebugOut.Analog[19]++; ;             
78
          }
79
 
80
   if (SPI_BufferIndex < sizeof(ToNaviCtrl))  
606 ingob 81
     {
82
           SLAVE_SELECT_PORT &=  ~(1 << SPI_SLAVE_SELECT);  // SelectSlave
691 ingob 83
           asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop");         asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop");
84
           asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop");         asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop");
85
           asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop");         asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop");
617 ingob 86
 
606 ingob 87
           SPDR = Ptr_buffer[SPI_BufferIndex];
617 ingob 88
           ToNaviCtrl.ChkSum += Ptr_buffer[SPI_BufferIndex];
691 ingob 89
        //   SLAVE_SELECT_PORT |=  (1 << SPI_SLAVE_SELECT);   // DeselectSlave
90
 
606 ingob 91
         }
304 ingob 92
   else
93
     {
94
           unsigned char *ptr = (unsigned char *)&FromNaviCtrl;
596 ingob 95
 
606 ingob 96
       SPITransferCompleted = 1;  
304 ingob 97
       memcpy(ptr, (unsigned char *) SPI_Buffer,  sizeof(SPI_Buffer));
98
         }
99
 
100
         SPI_BufferIndex++;
101
}
102
 
691 ingob 103
/*
104
void SPI_MasterInit(void)
105
{
106
  DDR_SPI |= (1<<DD_MOSI)|(1<<DD_SCK);    // Set MOSI and SCK output, all others input
107
  SLAVE_SELECT_DDR_PORT |= (1 << SPI_SLAVE_SELECT);
108
 
109
  SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1)|(0<<SPIE);   // Enable SPI, Master, set clock rate fck/64
110
  SLAVE_SELECT_PORT |=  (1 << SPI_SLAVE_SELECT);
111
  SPITransferCompleted = 1;
112
 
113
  ToNaviCtrl.Sync1 = 0x81;
114
  ToNaviCtrl.Sync2 = 0x55;
115
 
116
 
117
}
118
 
304 ingob 119
//------------------------------------------------------
691 ingob 120
void SPI_StartTransmitPacket(void)
121
{
122
   //if ((SLAVE_SELECT_PORT & (1 << SPI_SLAVE_SELECT)) == 0) return;    // transfer of prev. packet not completed
123
   if (!SPITransferCompleted) return;
124
 
125
   SLAVE_SELECT_PORT &=  ~(1 << SPI_SLAVE_SELECT);  // SelectSlave
126
   SPITransferCompleted = 0;
127
   UpdateSPI_Buffer();                              // update buffer
128
   SPI_BufferIndex = 1;
129
   DebugOut.Analog[16]++;
130
   // -- Debug-Output ---
131
   //----
132
   SPDR = ToNaviCtrl.Sync1;                  // Start transmission
133
}
134
 
135
//------------------------------------------------------
136
//SIGNAL(SIG_SPI)
137
void SPI_TransmitByte(void)
138
{
139
   if (!(SPSR & (1 << SPIF))) return;
140
  SLAVE_SELECT_PORT |=  (1 << SPI_SLAVE_SELECT);   // DeselectSlave
141
 
142
          if (SPI_BufferIndex < sizeof(FromNaviCtrl))
143
      {
144
            SPI_Buffer[SPI_BufferIndex]= SPDR;             // get data
145
//              if (SPI_BufferIndex < 32 ) DebugOut.Analog[26+SPI_BufferIndex] = SPI_Buffer[SPI_BufferIndex];
146
 
147
        //if(SPDR!= 0x00) DebugOut.Analog[19]++; ;            
148
          }
149
 
150
   if (SPI_BufferIndex < sizeof(ToNaviCtrl))  
151
     {
152
           asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop");
153
           SLAVE_SELECT_PORT &=  ~(1 << SPI_SLAVE_SELECT);  // SelectSlave
154
           asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop");
155
           SPDR = Ptr_buffer[SPI_BufferIndex];
156
         }
157
   else
158
     {
159
           unsigned char *ptr = (unsigned char *)&FromNaviCtrl;
160
 
161
       SPITransferCompleted = 1;  
162
       memcpy(ptr, (unsigned char *) SPI_Buffer,  sizeof(SPI_Buffer));
163
         }
164
 
165
         SPI_BufferIndex++;
166
}
167
*/
168
//------------------------------------------------------
304 ingob 169
void UpdateSPI_Buffer(void)
170
{
691 ingob 171
  static unsigned char i =0;
304 ingob 172
  cli();
173
  ToNaviCtrl.Comp = SPI_PROTOCOL_COMP;
174
  ToNaviCtrl.IntegralNick = (int) (IntegralNick >> 4);
175
  ToNaviCtrl.IntegralRoll = (int) (IntegralRoll >> 4);
606 ingob 176
  ToNaviCtrl.StickNick = 4;
177
  ToNaviCtrl.StickRoll = 5;//(char) StickRoll;
178
  ToNaviCtrl.StickGier = 6;//(char) StickGier;
304 ingob 179
  sei();
691 ingob 180
 
304 ingob 181
}
182
 
597 ingob 183
#endif
304 ingob 184
 
185