Subversion Repositories FlightCtrl

Rev

Rev 1221 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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