Subversion Repositories FlightCtrl

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1538 killagreg 1
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2
// + Copyright (c) Holger Buss, Ingo Busker
3
// + Nur für den privaten Gebrauch
4
// + www.MikroKopter.com
5
// + porting the sources to other systems or using the software on other systems (except hardware from www.mikrokopter.de) is not allowed
6
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
7
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
8
// + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist.
9
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
10
// + bzgl. der Nutzungsbedingungen aufzunehmen.
11
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
12
// + Verkauf von Luftbildaufnahmen, usw.
13
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
14
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
15
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
16
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
17
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
18
// + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
19
// + eindeutig als Ursprung verlinkt werden
20
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
21
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
22
// + Benutzung auf eigene Gefahr
23
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
24
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
25
// + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
26
// + mit unserer Zustimmung zulässig
27
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
28
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
29
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
30
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
31
// + this list of conditions and the following disclaimer.
32
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
33
// +     from this software without specific prior written permission.
34
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permittet
35
// +     for non-commercial use (directly or indirectly)
36
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
37
// +     with our written permission
38
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
39
// +     clearly linked as origin
40
// +   * porting to systems other than hardware from www.mikrokopter.de is not allowed
41
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
42
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
45
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
46
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
47
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
48
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
50
// +  POSSIBILITY OF SUCH DAMAGE.
51
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
52
#include <avr/io.h>
53
#include <avr/interrupt.h>
54
#include <string.h>
55
#include <stdlib.h>
56
#include "main.h"
57
#include "spi.h"
58
#include "fc.h"
59
#include "rc.h"
60
#include "eeprom.h"
61
#include "uart0.h"
62
#include "timer0.h"
63
#include "analog.h"
64
 
65
 
66
//-----------------------------------------
67
#define DDR_SPI DDRB
68
#define DD_SS   PB4
69
#define DD_SCK  PB7
70
#define DD_MOSI PB5
71
#define DD_MISO PB6
72
 
73
// for compatibility reasons gcc3.x <-> gcc4.x
74
#ifndef SPCR
75
#define SPCR   SPCR0
76
#endif
77
#ifndef SPIE
78
#define SPIE   SPIE0
79
#endif
80
#ifndef SPE
81
#define SPE    SPE0
82
#endif
83
#ifndef DORD
84
#define DORD   DORD0
85
#endif
86
#ifndef MSTR
87
#define MSTR   MSTR0
88
#endif
89
#ifndef CPOL
90
#define CPOL   CPOL0
91
#endif
92
#ifndef CPHA
93
#define CPHA   CPHA0
94
#endif
95
#ifndef SPR1
96
#define SPR1   SPR01
97
#endif
98
#ifndef SPR0
99
#define SPR0   SPR00
100
#endif
101
 
102
#ifndef SPDR
103
#define SPDR   SPDR0
104
#endif
105
 
106
#ifndef SPSR
107
#define SPSR   SPSR0
108
#endif
109
#ifndef SPIF
110
#define SPIF   SPIF0
111
#endif
112
#ifndef WCOL
113
#define WCOL   WCOL0
114
#endif
115
#ifndef SPI2X
116
#define SPI2X  SPI2X0
117
#endif
118
// -------------------------
119
 
120
#define SLAVE_SELECT_DDR_PORT   DDRC
121
#define SLAVE_SELECT_PORT       PORTC
122
#define SPI_SLAVE_SELECT        PC5
123
 
124
 
125
#define SPI_TXSYNCBYTE1 0xAA
126
#define SPI_TXSYNCBYTE2 0x83
127
#define SPI_RXSYNCBYTE1 0x81
128
#define SPI_RXSYNCBYTE2 0x55
129
 
130
typedef enum
131
{
132
        SPI_SYNC1,
133
        SPI_SYNC2,
134
        SPI_DATA
135
} SPI_RXState_t;
136
 
137
 
138
// data exchange packets to and From NaviCtrl
139
ToNaviCtrl_t                    ToNaviCtrl;
140
FromNaviCtrl_t                  FromNaviCtrl;
141
 
142
SPI_VersionInfo_t SPI_VersionInfo;
143
 
144
// rx packet buffer
145
#define SPI_RXBUFFER_LEN sizeof(FromNaviCtrl)
146
uint8_t SPI_RxBuffer[SPI_RXBUFFER_LEN];
147
uint8_t SPI_RxBufferIndex = 0;
148
uint8_t SPI_RxBuffer_Request = 0;
149
 
150
// tx packet buffer
151
#define SPI_TXBUFFER_LEN sizeof(ToNaviCtrl)
152
uint8_t *SPI_TxBuffer;
153
uint8_t SPI_TxBufferIndex = 0;
154
 
155
uint8_t SPITransferCompleted, SPI_ChkSum;
156
uint8_t SPI_RxDataValid = 0;
157
uint8_t NCDataOkay = 0;
158
uint8_t NCSerialDataOkay = 0;
159
int8_t  NCGpsZ = 0;
160
 
161
uint8_t SPI_CommandSequence[] = { SPI_CMD_USER, SPI_CMD_STICK, SPI_CMD_PARAMETER1, SPI_CMD_STICK, SPI_CMD_MISC, SPI_CMD_VERSION, SPI_CMD_SERVOS};
162
uint8_t SPI_CommandCounter = 0;
163
 
164
/*********************************************/
165
/*  Initialize SPI interface to NaviCtrl     */
166
/*********************************************/
167
void SPI_MasterInit(void)
168
{
169
        DDR_SPI |= (1<<DD_MOSI)|(1<<DD_SCK);    // Set MOSI and SCK output, all others input
170
        SLAVE_SELECT_DDR_PORT |= (1 << SPI_SLAVE_SELECT); // set Slave select port as output port
171
 
172
        SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1)|(0<<SPR0)|(0<<SPIE);   // Enable SPI, Master, set clock rate fck/64
173
        SPSR = 0;//(1<<SPI2X);
174
 
175
        SLAVE_SELECT_PORT |=  (1 << SPI_SLAVE_SELECT); // Deselect Slave
176
 
177
        SPI_TxBuffer = (uint8_t *) &ToNaviCtrl; // set pointer to tx-buffer
178
        SPITransferCompleted = 1;
179
        // initialize data packet to NaviControl
180
        ToNaviCtrl.Sync1 = SPI_TXSYNCBYTE1;
181
        ToNaviCtrl.Sync2 = SPI_TXSYNCBYTE2;
182
 
183
        ToNaviCtrl.Command = SPI_CMD_USER;
184
        ToNaviCtrl.IntegralNick = 0;
185
        ToNaviCtrl.IntegralRoll = 0;
186
        NCSerialDataOkay = 0;
187
        NCDataOkay = 0;
188
 
189
        SPI_RxDataValid = 0;
190
 
191
        SPI_VersionInfo.Major = VERSION_MAJOR;
192
        SPI_VersionInfo.Minor = VERSION_MINOR;
193
        SPI_VersionInfo.Patch = VERSION_PATCH;
194
        SPI_VersionInfo.Compatible = NC_SPI_COMPATIBLE;
195
}
196
 
197
 
198
/**********************************************************/
199
/*  Update Data transferd by the SPI from/to NaviCtrl     */
200
/**********************************************************/
201
void UpdateSPI_Buffer(void)
202
{
203
        int16_t tmp;
204
 
205
        // update content of packet to NaviCtrl
206
        ToNaviCtrl.IntegralNick = (int16_t)((10 * IntegralGyroNick) / GYRO_DEG_FACTOR); // convert to multiple of 0.1°
207
        ToNaviCtrl.IntegralRoll = (int16_t)((10 * IntegralGyroRoll) / GYRO_DEG_FACTOR); // convert to multiple of 0.1°
208
        ToNaviCtrl.GyroHeading  = (int16_t)((10 * YawGyroHeading)   / GYRO_DEG_FACTOR); // convert to multiple of 0.1°
209
        ToNaviCtrl.GyroNick = GyroNick;
210
        ToNaviCtrl.GyroRoll = GyroRoll;
211
        ToNaviCtrl.GyroYaw =  GyroYaw;
212
        ToNaviCtrl.AccNick =  ((int16_t) 10 * ACC_AMPLIFY * (NaviAccNick / NaviCntAcc)) / ACC_DEG_FACTOR; // convert to multiple of 0.1°
213
        ToNaviCtrl.AccRoll =  ((int16_t) 10 * ACC_AMPLIFY * (NaviAccRoll / NaviCntAcc)) / ACC_DEG_FACTOR; // convert to multiple of 0.1°
214
        NaviCntAcc = 0; NaviAccNick = 0; NaviAccRoll = 0;
215
 
216
        switch(ToNaviCtrl.Command)
217
        {
218
                case SPI_CMD_USER:
219
                        ToNaviCtrl.Param.Byte[0] = FCParam.UserParam1;
220
                        ToNaviCtrl.Param.Byte[1] = FCParam.UserParam2;
221
                        ToNaviCtrl.Param.Byte[2] = FCParam.UserParam3;
222
                        ToNaviCtrl.Param.Byte[3] = FCParam.UserParam4;
223
                        ToNaviCtrl.Param.Byte[4] = FCParam.UserParam5;
224
                        ToNaviCtrl.Param.Byte[5] = FCParam.UserParam6;
225
                        ToNaviCtrl.Param.Byte[6] = FCParam.UserParam7;
226
                        ToNaviCtrl.Param.Byte[7] = FCParam.UserParam8;
227
                        ToNaviCtrl.Param.Byte[8] = MKFlags;
228
                        MKFlags &= ~(MKFLAG_CALIBRATE | MKFLAG_START); // calibrate and start are temporal states that are cleared immediately after transmitting
229
                        ToNaviCtrl.Param.Byte[9] = (uint8_t)UBat;
230
                        ToNaviCtrl.Param.Byte[10] = LowVoltageWarning;
231
                        ToNaviCtrl.Param.Byte[11] = GetActiveParamSet();
232
                        break;
233
 
234
                case SPI_CMD_PARAMETER1:
235
                        ToNaviCtrl.Param.Byte[0] = ParamSet.NaviGpsModeControl;     // Parameters for the Naviboard
236
                        ToNaviCtrl.Param.Byte[1] = ParamSet.NaviGpsGain;
237
                        ToNaviCtrl.Param.Byte[2] = ParamSet.NaviGpsP;
238
                        ToNaviCtrl.Param.Byte[3] = ParamSet.NaviGpsI;
239
                        ToNaviCtrl.Param.Byte[4] = ParamSet.NaviGpsD;
240
                        ToNaviCtrl.Param.Byte[5] = ParamSet.NaviGpsACC;
241
                        ToNaviCtrl.Param.Byte[6] = ParamSet.NaviGpsMinSat;
242
                        ToNaviCtrl.Param.Byte[7] = ParamSet.NaviStickThreshold;
243
                        ToNaviCtrl.Param.Byte[8] = ParamSet.NaviOperatingRadius;
244
                        ToNaviCtrl.Param.Byte[9] = ParamSet.NaviWindCorrection;
245
            ToNaviCtrl.Param.Byte[10] = ParamSet.NaviSpeedCompensation;
246
            ToNaviCtrl.Param.Byte[11] = ParamSet.NaviAngleLimitation;
247
                        break;
248
 
249
 
250
                case SPI_CMD_STICK:
251
                        cli();
252
                        tmp = PPM_in[ParamSet.ChannelAssignment[CH_GAS]];  if(tmp > 127) tmp = 127; else if(tmp < -128) tmp = -128;
253
                        ToNaviCtrl.Param.Byte[0] = (int8_t) tmp;
254
                        tmp = PPM_in[ParamSet.ChannelAssignment[CH_YAW]]; if(tmp > 127) tmp = 127; else if(tmp < -128) tmp = -128;
255
                        ToNaviCtrl.Param.Byte[1] = (int8_t) tmp;
256
                        tmp = PPM_in[ParamSet.ChannelAssignment[CH_ROLL]]; if(tmp > 127) tmp = 127; else if(tmp < -128) tmp = -128;
257
                        ToNaviCtrl.Param.Byte[2] = (int8_t) tmp;
258
                        tmp = PPM_in[ParamSet.ChannelAssignment[CH_NICK]]; if(tmp > 127) tmp = 127; else if(tmp < -128) tmp = -128;
259
                        ToNaviCtrl.Param.Byte[3] = (int8_t) tmp;
260
                        sei();
261
                        ToNaviCtrl.Param.Byte[4] = (uint8_t) Poti1;
262
                        ToNaviCtrl.Param.Byte[5] = (uint8_t) Poti2;
263
                        ToNaviCtrl.Param.Byte[6] = (uint8_t) Poti3;
264
                        ToNaviCtrl.Param.Byte[7] = (uint8_t) Poti4;
265
                        ToNaviCtrl.Param.Byte[8] = (uint8_t) RC_Quality;
266
                        ToNaviCtrl.Param.Byte[9] = (uint8_t) RC_RSSI;
267
                        ToNaviCtrl.Param.Byte[10] = DebugOut.Analog[7] / 4; // gasmixfraction
268
                        break;
269
 
270
                case SPI_CMD_MISC:
271
                        ToNaviCtrl.Param.Byte[0] = CompassCalState;
272
                        if(CompassCalState > 4)
273
                        { // jump from 5 to 0
274
                                CompassCalState  = 0;
275
                        }
276
                        ToNaviCtrl.Param.Byte[1] = ParamSet.NaviPHLoginTime;
277
                        ToNaviCtrl.Param.Int[1]  = (int16_t)(ReadingHeight/5); // at address of Byte 2 and 3
278
                        ToNaviCtrl.Param.Int[2]  = (int16_t)(SetPointHeight/5);// at address of Byte 4 and 5
279
                        ToNaviCtrl.Param.Byte[6] = ParamSet.NaviGpsPLimit;
280
                        ToNaviCtrl.Param.Byte[7] = ParamSet.NaviGpsILimit;
281
                        ToNaviCtrl.Param.Byte[8] = ParamSet.NaviGpsDLimit;
282
                        break;
283
 
284
                case SPI_CMD_VERSION:
285
                        ToNaviCtrl.Param.Byte[0] = SPI_VersionInfo.Major;
286
                        ToNaviCtrl.Param.Byte[1] = SPI_VersionInfo.Minor;
287
                        ToNaviCtrl.Param.Byte[2] = SPI_VersionInfo.Patch;
288
                        ToNaviCtrl.Param.Byte[3] = SPI_VersionInfo.Compatible;
289
                        ToNaviCtrl.Param.Byte[4] = BoardRelease;
290
                        break;
291
 
292
                case SPI_CMD_SERVOS:
293
                        ToNaviCtrl.Param.Byte[0] = ParamSet.ServoRefresh;     // Parameters for the Servo Control
294
                        ToNaviCtrl.Param.Byte[1] = ParamSet.ServoCompInvert;
295
                        ToNaviCtrl.Param.Byte[2] = FCParam.ServoNickControl;
296
                        ToNaviCtrl.Param.Byte[3] = ParamSet.ServoNickComp;
297
                        ToNaviCtrl.Param.Byte[4] = ParamSet.ServoNickMin;
298
                        ToNaviCtrl.Param.Byte[5] = ParamSet.ServoNickMax;
299
                        ToNaviCtrl.Param.Byte[6] = FCParam.ServoRollControl;
300
                        ToNaviCtrl.Param.Byte[7] = ParamSet.ServoRollComp;
301
                        ToNaviCtrl.Param.Byte[8] = ParamSet.ServoRollMin;
302
                        ToNaviCtrl.Param.Byte[9] = ParamSet.ServoRollMax;
303
                        break;
304
 
305
                default:
306
                        break;
307
        }
308
 
309
        // analyze content of packet from NaviCtrl if valid
310
        if (SPI_RxDataValid)
311
        {
312
                // update gps controls
313
                if(abs(FromNaviCtrl.GPSStickNick) < 512 && abs(FromNaviCtrl.GPSStickRoll) < 512 && (ParamSet.Config0 & CFG0_GPS_ACTIVE))
314
                {
315
                        GPSStickNick    = FromNaviCtrl.GPSStickNick;
316
                        GPSStickRoll    = FromNaviCtrl.GPSStickRoll;
317
                        NCDataOkay = 250;
318
                }
319
                // update compass readings
320
                if(FromNaviCtrl.CompassHeading <= 360)
321
                {
322
                        CompassHeading = FromNaviCtrl.CompassHeading;
323
                }
324
                if(CompassHeading < 0) CompassOffCourse = 0;
325
                else CompassOffCourse = ((540 + CompassHeading - CompassCourse) % 360) - 180;
326
        // NaviCtrl wants to beep?
327
                if (FromNaviCtrl.BeepTime > BeepTime && !CompassCalState) BeepTime = FromNaviCtrl.BeepTime;
328
 
329
                switch (FromNaviCtrl.Command)
330
                {
331
 
332
                        case SPI_KALMAN:
333
                                FCParam.KalmanK = FromNaviCtrl.Param.sByte[0];
334
                                FCParam.KalmanMaxFusion = FromNaviCtrl.Param.sByte[1];
335
                                FCParam.KalmanMaxDrift = FromNaviCtrl.Param.sByte[2];
336
                                NCSerialDataOkay = FromNaviCtrl.Param.Byte[3];
337
                                NCGpsZ = FromNaviCtrl.Param.sByte[4];
338
                                break;
339
 
340
                        default:
341
                                break;
342
                }
343
        }
344
        else // no valid data from NaviCtrl
345
        {
346
                // disable GPS control
347
                GPSStickNick = 0;
348
                GPSStickRoll = 0;
349
                NCGpsZ = 0;
350
        }
351
}
352
 
353
 
354
 
355
/*********************************************/
356
/*  Start Transmission of packet to NaviCtrl */
357
/*********************************************/
358
void SPI_StartTransmitPacket(void)
359
{
360
 
361
        if (!SPITransferCompleted) return; // return immediately if transfer is in progress
362
        else // transmission was completed
363
        {
364
                SLAVE_SELECT_PORT &=  ~(1 << SPI_SLAVE_SELECT);  // Select slave
365
 
366
                // cyclic commands
367
                ToNaviCtrl.Command = SPI_CommandSequence[SPI_CommandCounter++];
368
                if (SPI_CommandCounter >= sizeof(SPI_CommandSequence)) SPI_CommandCounter = 0;
369
 
370
                SPITransferCompleted = 0; // transfer is in progress
371
                UpdateSPI_Buffer();    // update data in ToNaviCtrl
372
 
373
                SPI_TxBufferIndex = 1; //proceed with 2nd byte
374
 
375
                // -- Debug-Output ---
376
                //----
377
                asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop");            asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop");
378
                asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop");            asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop");
379
                asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop");            asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop");
380
                ToNaviCtrl.Chksum = ToNaviCtrl.Sync1; // init checksum
381
                SPDR = ToNaviCtrl.Sync1; // send first byte
382
        }
383
}
384
 
385
//------------------------------------------------------
386
// This is the spi data transfer between FlightCtrl and NaviCtrl
387
// Every time this routine is called within the mainloop one byte of the packet to
388
// the NaviCtrl and one byte of the packet from the NaviCtrl is possible transfered
389
 
390
void SPI_TransmitByte(void)
391
{
392
        static SPI_RXState_t SPI_RXState = SPI_SYNC1;
393
        uint8_t rxdata;
394
        static uint8_t rxchksum;
395
 
396
        if (SPITransferCompleted) return;  // return immediatly if transfer was completed
397
        if (!(SPSR & (1 << SPIF))) return; // return if no SPI-IRQ pending
398
        SendSPI = 4; // mait 4 * 0.102 ms for the next call of SPI_TransmitByte() in the main loop
399
 
400
        SLAVE_SELECT_PORT |=  (1 << SPI_SLAVE_SELECT);   // DeselectSlave
401
 
402
        rxdata = SPDR; // save spi data register
403
 
404
        switch (SPI_RXState)
405
        {
406
                case SPI_SYNC1: // first sync byte
407
                        SPI_RxBufferIndex = 0; // set pointer to start of rx buffer
408
                        rxchksum = rxdata; // initialize checksum
409
                        if (rxdata == SPI_RXSYNCBYTE1 )
410
                        {       // 1st Syncbyte found
411
                                SPI_RXState  = SPI_SYNC2; // trigger to state for second sync byte
412
                        }
413
                        break;
414
 
415
                case SPI_SYNC2: // second sync byte
416
                        if (rxdata == SPI_RXSYNCBYTE2)
417
                        {       // 2nd Syncbyte found
418
                                rxchksum += rxdata; // update checksum
419
                                SPI_RXState  = SPI_DATA;   // trigger to state for second sync byte
420
                        }
421
                        else // 2nd Syncbyte not found
422
                        {
423
                                SPI_RXState  = SPI_SYNC1; // jump back to 1st sync byte
424
                        }
425
                        break;
426
 
427
                case SPI_DATA: // data bytes
428
                        SPI_RxBuffer[SPI_RxBufferIndex++] = rxdata;  // copy data byte to spi buffer
429
                        // if all bytes are received of a packet from the NaviCtrl
430
                        if (SPI_RxBufferIndex >= SPI_RXBUFFER_LEN)
431
                        {   // last byte transfered is the checksum of the packet
432
                                if (rxdata == rxchksum) // checksum matching?
433
                                {
434
                                        // copy SPI_RxBuffer -> FromFlightCtrl
435
                                        uint8_t *ptr = (uint8_t *)&FromNaviCtrl;
436
                                        cli();
437
                                        memcpy(ptr, (uint8_t *) SPI_RxBuffer, sizeof(FromNaviCtrl));
438
                                        sei();
439
                                        SPI_RxDataValid = 1;
440
                                        //DebugOut.Analog[18]++;
441
                                }
442
                                else
443
                                {   // checksum does not match
444
                                        //DebugOut.Analog[17]++;
445
                                        SPI_RxDataValid = 0; // reset valid flag
446
                                }
447
                                SPI_RXState  = SPI_SYNC1; // reset state sync
448
                        }
449
                        else // not all bytes transfered
450
                        {
451
                                rxchksum += rxdata; // update checksum
452
                        }
453
                        break;
454
        }// eof switch(SPI_RXState)
455
 
456
    // if still some bytes left for transmission to NaviCtrl
457
        if (SPI_TxBufferIndex < SPI_TXBUFFER_LEN)
458
        {
459
                SLAVE_SELECT_PORT &=  ~(1 << SPI_SLAVE_SELECT);  // SelectSlave
460
                asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop");
461
                asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop");
462
                asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop");
463
 
464
                SPDR = SPI_TxBuffer[SPI_TxBufferIndex]; // transmit byte
465
                ToNaviCtrl.Chksum += SPI_TxBuffer[SPI_TxBufferIndex]; // update checksum for everey byte that was sent
466
                SPI_TxBufferIndex++;
467
        }
468
        else
469
        {
470
                //Transfer of all bytes of the packet to NaviCtrl completed
471
                SPITransferCompleted = 1;
472
        }
473
}
474
 
475
 
476