Subversion Repositories FlightCtrl

Rev

Details | Last modification | View Log | RSS feed

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