Subversion Repositories FlightCtrl

Rev

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

Rev 617 Rev 691
Line 8... Line 8...
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;
Line 10... Line 10...
10
 
10
 
11
unsigned char SPITransferCompleted, SPI_ChkSum;
11
unsigned char SPITransferCompleted, SPI_ChkSum;
-
 
12
#ifdef USE_SPI_COMMUNICATION
12
#ifdef USE_SPI_COMMUNICATION
13
 
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 
Line 17... Line 18...
17
  SLAVE_SELECT_DDR_PORT |= (1 << SPI_SLAVE_SELECT);
18
  SLAVE_SELECT_DDR_PORT |= (1 << SPI_SLAVE_SELECT);
-
 
19
   
-
 
20
  SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1)|(0<<SPR0)|(0<<SPIE);   // Enable SPI, Master, set clock rate fck/64 
18
   
21
  SPSR = 0;  // SPI2X
19
  SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1)|(0<<SPIE);   // Enable SPI, Master, set clock rate fck/64 
22
 
Line 20... Line 23...
20
  SLAVE_SELECT_PORT |=  (1 << SPI_SLAVE_SELECT);
23
  SLAVE_SELECT_PORT |=  (1 << SPI_SLAVE_SELECT);
21
  SPITransferCompleted = 1;
24
  SPITransferCompleted = 1;
Line 43... Line 46...
43
   SPITransferCompleted = 0;
46
   SPITransferCompleted = 0;
44
   UpdateSPI_Buffer();                              // update buffer
47
   UpdateSPI_Buffer();                              // update buffer
45
   SPI_BufferIndex = 1;
48
   SPI_BufferIndex = 1;
46
   DebugOut.Analog[16]++;
49
   DebugOut.Analog[16]++;
47
   // -- Debug-Output ---
50
   // -- Debug-Output ---
48
/*         DebugOut.Analog[20] = FromNaviCtrl.Comp;
-
 
49
       DebugOut.Analog[21] = FromNaviCtrl.GPS_Nick;
-
 
50
       DebugOut.Analog[22] = FromNaviCtrl.GPS_Roll;
-
 
51
       DebugOut.Analog[23] = FromNaviCtrl.CompassValue;
-
 
52
*/  
-
 
53
   //----
51
   //----
-
 
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");
54
   SPDR = ToNaviCtrl.Sync1;                  // Start transmission 
55
   SPDR = ToNaviCtrl.Sync1;                  // Start transmission 
55
   ToNaviCtrl.ChkSum = ToNaviCtrl.Sync1;
56
   ToNaviCtrl.ChkSum = ToNaviCtrl.Sync1;
-
 
57
//     SLAVE_SELECT_PORT |=  (1 << SPI_SLAVE_SELECT);   // DeselectSlave
-
 
58
 
56
}
59
}
Line 57... Line 60...
57
 
60
 
58
//------------------------------------------------------
61
//------------------------------------------------------
59
//SIGNAL(SIG_SPI)
62
//SIGNAL(SIG_SPI)
60
void SPI_TransmitByte(void)
63
void SPI_TransmitByte(void)
-
 
64
{
-
 
65
   
61
{
66
   if (SPITransferCompleted) return;
-
 
67
   if (!(SPSR & (1 << SPIF))) return;
62
   if (!(SPSR & (1 << SPIF))) return;
68
   
Line 63... Line 69...
63
  SLAVE_SELECT_PORT |=  (1 << SPI_SLAVE_SELECT);   // DeselectSlave
69
  SLAVE_SELECT_PORT |=  (1 << SPI_SLAVE_SELECT);   // DeselectSlave
64
   
70
   
65
          if (SPI_BufferIndex < sizeof(FromNaviCtrl))
71
          if (SPI_BufferIndex < sizeof(FromNaviCtrl))
Line 71... Line 77...
71
        //if(SPDR!= 0x00) DebugOut.Analog[19]++; ;             
77
        //if(SPDR!= 0x00) DebugOut.Analog[19]++; ;             
72
          }
78
          }
Line 73... Line 79...
73
 
79
 
74
   if (SPI_BufferIndex < sizeof(ToNaviCtrl))  
80
   if (SPI_BufferIndex < sizeof(ToNaviCtrl))  
75
     {
-
 
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");
81
     {
-
 
82
           SLAVE_SELECT_PORT &=  ~(1 << SPI_SLAVE_SELECT);  // SelectSlave
78
           SLAVE_SELECT_PORT &=  ~(1 << SPI_SLAVE_SELECT);  // SelectSlave
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");
79
           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");
Line 80... Line 85...
80
           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");
81
           
86
           
-
 
87
           SPDR = Ptr_buffer[SPI_BufferIndex];
-
 
88
           ToNaviCtrl.ChkSum += Ptr_buffer[SPI_BufferIndex];
82
           SPDR = Ptr_buffer[SPI_BufferIndex];
89
        //   SLAVE_SELECT_PORT |=  (1 << SPI_SLAVE_SELECT);   // DeselectSlave
83
           ToNaviCtrl.ChkSum += Ptr_buffer[SPI_BufferIndex];
90
 
84
         }
91
         }
85
   else
92
   else
Line 91... Line 98...
91
         }
98
         }
Line 92... Line 99...
92
         
99
         
93
         SPI_BufferIndex++;
100
         SPI_BufferIndex++;
Line -... Line 101...
-
 
101
}
-
 
102
 
-
 
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
 
-
 
119
//------------------------------------------------------
-
 
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++;
94
}
166
}
95
 
167
*/
96
//------------------------------------------------------
168
//------------------------------------------------------
97
void UpdateSPI_Buffer(void)
169
void UpdateSPI_Buffer(void)
98
{
170
{
99
  /*static unsigned char i =0;
171
  static unsigned char i =0;
100
  cli();
172
  cli();
101
  ToNaviCtrl.Comp = SPI_PROTOCOL_COMP;
173
  ToNaviCtrl.Comp = SPI_PROTOCOL_COMP;
102
  ToNaviCtrl.IntegralNick = (int) (IntegralNick >> 4);
174
  ToNaviCtrl.IntegralNick = (int) (IntegralNick >> 4);
103
  ToNaviCtrl.IntegralRoll = (int) (IntegralRoll >> 4);
175
  ToNaviCtrl.IntegralRoll = (int) (IntegralRoll >> 4);
104
  ToNaviCtrl.StickNick = 4;
176
  ToNaviCtrl.StickNick = 4;
105
  ToNaviCtrl.StickRoll = 5;//(char) StickRoll;
177
  ToNaviCtrl.StickRoll = 5;//(char) StickRoll;
106
  ToNaviCtrl.StickGier = 6;//(char) StickGier;
178
  ToNaviCtrl.StickGier = 6;//(char) StickGier;
107
  sei();
179
  sei();
Line 108... Line 180...
108
  */
180