Subversion Repositories NaviCtrl

Rev

Rev 144 | Rev 148 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 144 Rev 146
1
/*#######################################################################################*/
1
/*#######################################################################################*/
2
/* !!! THIS IS NOT FREE SOFTWARE !!!                                                     */
2
/* !!! THIS IS NOT FREE SOFTWARE !!!                                                     */
3
/*#######################################################################################*/
3
/*#######################################################################################*/
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5
// + Copyright (c) 2008 Ingo Busker, Holger Buss
5
// + Copyright (c) 2008 Ingo Busker, Holger Buss
6
// + Nur für den privaten Gebrauch
6
// + Nur für den privaten Gebrauch
7
// + FOR NON COMMERCIAL USE ONLY
7
// + FOR NON COMMERCIAL USE ONLY
8
// + www.MikroKopter.com
8
// + www.MikroKopter.com
9
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
9
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
10
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
10
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
11
// + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist.
11
// + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist.
12
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
12
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
13
// + bzgl. der Nutzungsbedingungen aufzunehmen.
13
// + bzgl. der Nutzungsbedingungen aufzunehmen.
14
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
14
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
15
// + Verkauf von Luftbildaufnahmen, usw.
15
// + Verkauf von Luftbildaufnahmen, usw.
16
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
16
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
17
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
17
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
18
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
18
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
19
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
19
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
20
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
20
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
21
// + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
21
// + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
22
// + eindeutig als Ursprung verlinkt werden
22
// + eindeutig als Ursprung verlinkt werden
23
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
23
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
24
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
24
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
25
// + Benutzung auf eigene Gefahr
25
// + Benutzung auf eigene Gefahr
26
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
26
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
27
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
27
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
28
// + Die PORTIERUNG der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
28
// + Die PORTIERUNG der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
29
// + mit unserer Zustimmung zulässig
29
// + mit unserer Zustimmung zulässig
30
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
30
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
31
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
31
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
32
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
32
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
33
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
33
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
34
// + this list of conditions and the following disclaimer.
34
// + this list of conditions and the following disclaimer.
35
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
35
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
36
// +     from this software without specific prior written permission.
36
// +     from this software without specific prior written permission.
37
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permitted
37
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permitted
38
// +     for non-commercial use (directly or indirectly)
38
// +     for non-commercial use (directly or indirectly)
39
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
39
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
40
// +     with our written permission
40
// +     with our written permission
41
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
41
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
42
// +     clearly linked as origin
42
// +     clearly linked as origin
43
// +   * PORTING this software (or part of it) to systems (other than hardware from www.mikrokopter.de) is NOT allowed
43
// +   * PORTING this software (or part of it) to systems (other than hardware from www.mikrokopter.de) is NOT allowed
44
//
44
//
45
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
45
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
46
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
48
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
49
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
49
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
50
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
50
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
51
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
51
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
52
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
52
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
53
// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
53
// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
54
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
54
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
55
// +  POSSIBILITY OF SUCH DAMAGE.
55
// +  POSSIBILITY OF SUCH DAMAGE.
56
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
56
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
57
 
57
 
58
#include <string.h>
58
#include <string.h>
59
#include "91x_lib.h"
59
#include "91x_lib.h"
60
#include "led.h"
60
#include "led.h"
61
#include "GPS.h"
61
#include "GPS.h"
62
#include "uart1.h"
62
#include "uart1.h"
63
#include "spi_slave.h"
63
#include "spi_slave.h"
64
#include "i2c.h"
64
#include "i2c.h"
65
#include "timer1.h"
65
#include "timer1.h"
66
#include "timer2.h"
66
#include "timer2.h"
67
#include "config.h"
67
#include "config.h"
68
#include "main.h"
68
#include "main.h"
69
#include "fifo.h"
69
#include "fifo.h"
70
 
70
 
71
 
71
 
72
#define SPI_RXSYNCBYTE1 0xAA
72
#define SPI_RXSYNCBYTE1 0xAA
73
#define SPI_RXSYNCBYTE2 0x83
73
#define SPI_RXSYNCBYTE2 0x83
74
#define SPI_TXSYNCBYTE1 0x81
74
#define SPI_TXSYNCBYTE1 0x81
75
#define SPI_TXSYNCBYTE2 0x55
75
#define SPI_TXSYNCBYTE2 0x55
76
 
76
 
77
typedef enum
77
typedef enum
78
{
78
{
79
        SPI_SYNC1,
79
        SPI_SYNC1,
80
        SPI_SYNC2,
80
        SPI_SYNC2,
81
        SPI_DATA
81
        SPI_DATA
82
} SPI_State_t;
82
} SPI_State_t;
83
 
83
 
84
//communication packets
84
//communication packets
85
volatile FromFlightCtrl_t   FromFlightCtrl;
85
volatile FromFlightCtrl_t   FromFlightCtrl;
86
volatile ToFlightCtrl_t     ToFlightCtrl;
86
volatile ToFlightCtrl_t     ToFlightCtrl;
-
 
87
#define SPI0_TIMEOUT 500 // 500 ms
-
 
88
volatile u32 SPI0_Timeout = 0;
87
 
89
 
88
// tx packet buffer
90
// tx packet buffer
89
#define SPI_TXBUFFER_LEN (2 + sizeof(ToFlightCtrl)) // 2 bytes at start are for synchronization
91
#define SPI_TXBUFFER_LEN (2 + sizeof(ToFlightCtrl)) // 2 bytes at start are for synchronization
90
volatile u8 SPI_TxBuffer[SPI_TXBUFFER_LEN];
92
volatile u8 SPI_TxBuffer[SPI_TXBUFFER_LEN];
91
volatile u8 SPI_TxBufferIndex = 0;
93
volatile u8 SPI_TxBufferIndex = 0;
92
u8 *Ptr_TxChksum = NULL ;  // pointer to checksum in TxBuffer
94
u8 *Ptr_TxChksum = NULL ;  // pointer to checksum in TxBuffer
93
 
95
 
94
// rx packet buffer
96
// rx packet buffer
95
#define SPI_RXBUFFER_LEN sizeof(FromFlightCtrl)
97
#define SPI_RXBUFFER_LEN sizeof(FromFlightCtrl)
96
volatile u8 SPI_RxBuffer[SPI_RXBUFFER_LEN];
98
volatile u8 SPI_RxBuffer[SPI_RXBUFFER_LEN];
97
volatile u8 SPI_RxBufferIndex = 0;
99
volatile u8 SPI_RxBufferIndex = 0;
98
volatile u8 SPI_RxBuffer_Request = 0;
100
volatile u8 SPI_RxBuffer_Request = 0;
99
#define SPI_COMMAND_INDEX 0
101
#define SPI_COMMAND_INDEX 0
100
 
102
 
101
s32 FC_Kalman_K = 32;
103
s32 FC_Kalman_K = 32;
102
s32 Kalman_MaxDrift = 5 * 16;
104
s32 Kalman_MaxDrift = 5 * 16;
103
s32 Kalman_MaxFusion = 64;
105
s32 Kalman_MaxFusion = 64;
104
u32 CheckSPIOkay = 0;
-
 
105
 
106
 
106
u8 SPI_CommandSequence[] = { SPI_KALMAN };
107
u8 SPI_CommandSequence[] = { SPI_KALMAN };
107
u8 SPI_CommandCounter = 0;
108
u8 SPI_CommandCounter = 0;
108
 
109
 
109
SPI_Version_t FC_Version;
110
SPI_Version_t FC_Version;
110
 
111
 
111
u8 CompassCalStateQueue[10];
112
u8 CompassCalStateQueue[10];
112
fifo_t CompassCalcStateFiFo;
113
fifo_t CompassCalcStateFiFo;
113
 
114
 
114
u8 CompassCalState = 0;
115
u8 CompassCalState = 0;
115
 
116
 
116
//--------------------------------------------------------------
117
//--------------------------------------------------------------
117
void SSP0_IRQHandler(void)
118
void SSP0_IRQHandler(void)
118
{
119
{
119
        static u8 rxchksum = 0;
120
        static u8 rxchksum = 0;
120
        u8 rxdata;
121
        u8 rxdata;
121
        static SPI_State_t SPI_State = SPI_SYNC1;
122
        static SPI_State_t SPI_State = SPI_SYNC1;
122
 
123
 
123
        // clear pending bits
124
        // clear pending bits
124
        SSP_ClearITPendingBit(SSP0, SSP_IT_RxTimeOut);
125
        SSP_ClearITPendingBit(SSP0, SSP_IT_RxTimeOut);
125
        SSP_ClearITPendingBit(SSP0, SSP_IT_RxFifo);
126
        SSP_ClearITPendingBit(SSP0, SSP_IT_RxFifo);
126
        /*
127
        /*
127
        // Fill TxFIFO while its not full or end of packet is reached
128
        // Fill TxFIFO while its not full or end of packet is reached
128
        while (SSP_GetFlagStatus(SSP0, SSP_FLAG_TxFifoNotFull) == SET)
129
        while (SSP_GetFlagStatus(SSP0, SSP_FLAG_TxFifoNotFull) == SET)
129
        {
130
        {
130
                if (SPI_TxBufferIndex < SPI_TXBUFFER_LEN)   // still data to send ?
131
                if (SPI_TxBufferIndex < SPI_TXBUFFER_LEN)   // still data to send ?
131
                {
132
                {
132
                        SSP0->DR = SPI_TxBuffer[SPI_TxBufferIndex];       // send a byte
133
                        SSP0->DR = SPI_TxBuffer[SPI_TxBufferIndex];       // send a byte
133
                        *Ptr_TxChksum += SPI_TxBuffer[SPI_TxBufferIndex]; // update checksum
134
                        *Ptr_TxChksum += SPI_TxBuffer[SPI_TxBufferIndex]; // update checksum
134
                        SPI_TxBufferIndex++;  // pointer to next byte
135
                        SPI_TxBufferIndex++;  // pointer to next byte
135
                }
136
                }
136
                else // TxBuffer end is reached then reset and copy data to tx buffer
137
                else // TxBuffer end is reached then reset and copy data to tx buffer
137
                {
138
                {
138
                        SPI_TxBufferIndex = 0;   // reset buffer index
139
                        SPI_TxBufferIndex = 0;   // reset buffer index
139
                        ToFlightCtrl.Chksum = 0;  // initialize checksum
140
                        ToFlightCtrl.Chksum = 0;  // initialize checksum
140
                        ToFlightCtrl.BeepTime = BeepTime; // set beeptime
141
                        ToFlightCtrl.BeepTime = BeepTime; // set beeptime
141
                        BeepTime = 0; // reset local beeptime
142
                        BeepTime = 0; // reset local beeptime
142
                        // copy contents of ToFlightCtrl->SPI_TxBuffer
143
                        // copy contents of ToFlightCtrl->SPI_TxBuffer
143
                        memcpy( (u8 *) &(SPI_TxBuffer[2]), (u8 *) &ToFlightCtrl, sizeof(ToFlightCtrl));
144
                        memcpy( (u8 *) &(SPI_TxBuffer[2]), (u8 *) &ToFlightCtrl, sizeof(ToFlightCtrl));
144
                }
145
                }
145
        }
146
        }
146
        */
147
        */
147
        // while RxFIFO not empty
148
        // while RxFIFO not empty
148
        while (SSP_GetFlagStatus(SSP0, SSP_FLAG_RxFifoNotEmpty) == SET)
149
        while (SSP_GetFlagStatus(SSP0, SSP_FLAG_RxFifoNotEmpty) == SET)
149
        {
150
        {
150
                rxdata =  SSP0->DR; // catch the received byte
151
                rxdata =  SSP0->DR; // catch the received byte
151
                // Fill TxFIFO while its not full or end of packet is reached
152
                // Fill TxFIFO while its not full or end of packet is reached
152
                while (SSP_GetFlagStatus(SSP0, SSP_FLAG_TxFifoNotFull) == SET)
153
                while (SSP_GetFlagStatus(SSP0, SSP_FLAG_TxFifoNotFull) == SET)
153
                {
154
                {
154
                        if (SPI_TxBufferIndex  < SPI_TXBUFFER_LEN)   // still data to send ?
155
                        if (SPI_TxBufferIndex  < SPI_TXBUFFER_LEN)   // still data to send ?
155
                        {
156
                        {
156
                                SSP0->DR = SPI_TxBuffer[SPI_TxBufferIndex];       // send a byte
157
                                SSP0->DR = SPI_TxBuffer[SPI_TxBufferIndex];       // send a byte
157
                                *Ptr_TxChksum += SPI_TxBuffer[SPI_TxBufferIndex]; // update checksum
158
                                *Ptr_TxChksum += SPI_TxBuffer[SPI_TxBufferIndex]; // update checksum
158
                                SPI_TxBufferIndex++; // pointer to next byte
159
                                SPI_TxBufferIndex++; // pointer to next byte
159
                        }
160
                        }
160
                        else // end of packet is reached reset and copy data to tx buffer
161
                        else // end of packet is reached reset and copy data to tx buffer
161
                        {
162
                        {
162
                                SPI_TxBufferIndex = 0;  // reset buffer index
163
                                SPI_TxBufferIndex = 0;  // reset buffer index
163
                                ToFlightCtrl.Chksum = 0;  // initialize checksum
164
                                ToFlightCtrl.Chksum = 0;  // initialize checksum
164
                                ToFlightCtrl.BeepTime = BeepTime;  // set beeptime
165
                                ToFlightCtrl.BeepTime = BeepTime;  // set beeptime
165
                                BeepTime = 0; // reset local beeptime
166
                                BeepTime = 0; // reset local beeptime
166
                                // copy contents of ToFlightCtrl->SPI_TxBuffer
167
                                // copy contents of ToFlightCtrl->SPI_TxBuffer
167
                                memcpy((u8 *) &(SPI_TxBuffer[2]), (u8 *) &ToFlightCtrl, sizeof(ToFlightCtrl));
168
                                memcpy((u8 *) &(SPI_TxBuffer[2]), (u8 *) &ToFlightCtrl, sizeof(ToFlightCtrl));
168
                        }
169
                        }
169
                }
170
                }
170
                switch (SPI_State)
171
                switch (SPI_State)
171
                {
172
                {
172
                        case SPI_SYNC1:
173
                        case SPI_SYNC1:
173
                                SPI_RxBufferIndex = 0; // reset buffer index
174
                                SPI_RxBufferIndex = 0; // reset buffer index
174
                                rxchksum = rxdata;     // init checksum
175
                                rxchksum = rxdata;     // init checksum
175
                                if (rxdata == SPI_RXSYNCBYTE1)
176
                                if (rxdata == SPI_RXSYNCBYTE1)
176
                                {   // 1st syncbyte ok
177
                                {   // 1st syncbyte ok
177
                                        SPI_State = SPI_SYNC2;  // step to sync2
178
                                        SPI_State = SPI_SYNC2;  // step to sync2
178
                                }
179
                                }
179
                                break;
180
                                break;
180
                        case SPI_SYNC2:
181
                        case SPI_SYNC2:
181
                                if (rxdata == SPI_RXSYNCBYTE2)
182
                                if (rxdata == SPI_RXSYNCBYTE2)
182
                                {  // 2nd Syncbyte ok
183
                                {  // 2nd Syncbyte ok
183
                                        rxchksum += rxdata;
184
                                        rxchksum += rxdata;
184
                                        SPI_State = SPI_DATA;
185
                                        SPI_State = SPI_DATA;
185
                                }  // 2nd Syncbyte does not match
186
                                }  // 2nd Syncbyte does not match
186
                                else
187
                                else
187
                                {
188
                                {
188
                                        SPI_State  = SPI_SYNC1; //jump back to sync1
189
                                        SPI_State  = SPI_SYNC1; //jump back to sync1
189
                                }
190
                                }
190
                                break;
191
                                break;
191
                        case SPI_DATA:
192
                        case SPI_DATA:
192
                                SPI_RxBuffer[SPI_RxBufferIndex++]= rxdata; // copy databyte to rx buffer
193
                                SPI_RxBuffer[SPI_RxBufferIndex++]= rxdata; // copy databyte to rx buffer
193
                                if (SPI_RxBufferIndex >= SPI_RXBUFFER_LEN) // end of packet is reached
194
                                if (SPI_RxBufferIndex >= SPI_RXBUFFER_LEN) // end of packet is reached
194
                                {
195
                                {
195
                                        if (rxdata == rxchksum) // verify checksum byte
196
                                        if (rxdata == rxchksum) // verify checksum byte
196
                                        {
197
                                        {
197
                                                // copy SPI_RxBuffer -> FromFlightCtrl
198
                                                // copy SPI_RxBuffer -> FromFlightCtrl
198
                                                if(!SPI_RxBuffer_Request) // block writing to FromFlightCtrl on reading access
199
                                                if(!SPI_RxBuffer_Request) // block writing to FromFlightCtrl on reading access
199
                                                {
200
                                                {
200
                                                        memcpy((u8 *) &FromFlightCtrl, (u8 *) SPI_RxBuffer, sizeof(FromFlightCtrl));
201
                                                        memcpy((u8 *) &FromFlightCtrl, (u8 *) SPI_RxBuffer, sizeof(FromFlightCtrl));
201
                                                        SPI_RxBuffer_Request = 1;
202
                                                        SPI_RxBuffer_Request = 1;
202
                                                }
203
                                                }
-
 
204
                                                // reset timeout counter on good packet
203
                                                CheckSPIOkay++;
205
                                                SPI0_Timeout = SetDelay(SPI0_TIMEOUT);
204
                                                DebugOut.Analog[13]++;
206
                                                DebugOut.Analog[13]++;
205
                                        }
207
                                        }
206
                                        else // bad checksum byte
208
                                        else // bad checksum byte
207
                                        {
209
                                        {
208
                                                DebugOut.Analog[12]++; // increase SPI chksum error counter
210
                                                DebugOut.Analog[12]++; // increase SPI chksum error counter
209
                                        }
211
                                        }
210
                                        SPI_State  = SPI_SYNC1; // reset state
212
                                        SPI_State  = SPI_SYNC1; // reset state
211
                                }
213
                                }
212
                                else // end of packet not reached
214
                                else // end of packet not reached
213
                                {
215
                                {
214
                                        rxchksum += rxdata;      // update checksum
216
                                        rxchksum += rxdata;      // update checksum
215
                                }
217
                                }
216
                                break;
218
                                break;
217
                        default:
219
                        default:
218
                                SPI_State  = SPI_SYNC1;
220
                                SPI_State  = SPI_SYNC1;
219
                                break;
221
                                break;
220
                }
222
                }
221
        }
223
        }
222
}
224
}
223
 
225
 
224
//--------------------------------------------------------------
226
//--------------------------------------------------------------
225
void SPI0_Init(void)
227
void SPI0_Init(void)
226
{
228
{
227
        GPIO_InitTypeDef GPIO_InitStructure;
229
        GPIO_InitTypeDef GPIO_InitStructure;
228
        SSP_InitTypeDef   SSP_InitStructure;
230
        SSP_InitTypeDef   SSP_InitStructure;
229
 
231
 
230
        UART1_PutString("\r\n SPI init...");
232
        UART1_PutString("\r\n SPI init...");
231
 
233
 
232
        SCU_APBPeriphClockConfig(__GPIO2 ,ENABLE);
234
        SCU_APBPeriphClockConfig(__GPIO2 ,ENABLE);
233
        SCU_APBPeriphClockConfig(__SSP0 ,ENABLE);
235
        SCU_APBPeriphClockConfig(__SSP0 ,ENABLE);
234
 
236
 
235
        GPIO_DeInit(GPIO2);
237
        GPIO_DeInit(GPIO2);
236
        //SSP0_CLK, SSP0_MOSI, SSP0_NSS pins
238
        //SSP0_CLK, SSP0_MOSI, SSP0_NSS pins
237
        GPIO_StructInit(&GPIO_InitStructure);
239
        GPIO_StructInit(&GPIO_InitStructure);
238
        GPIO_InitStructure.GPIO_Direction = GPIO_PinInput;
240
        GPIO_InitStructure.GPIO_Direction = GPIO_PinInput;
239
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_7;
241
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_7;
240
        GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;
242
        GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;
241
        GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Enable;
243
        GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Enable;
242
        GPIO_InitStructure.GPIO_Alternate = GPIO_InputAlt1; //SSP0_SCLK, SSP0_MOSI, SSP0_NSS
244
        GPIO_InitStructure.GPIO_Alternate = GPIO_InputAlt1; //SSP0_SCLK, SSP0_MOSI, SSP0_NSS
243
        GPIO_Init (GPIO2, &GPIO_InitStructure);
245
        GPIO_Init (GPIO2, &GPIO_InitStructure);
244
 
246
 
245
        // SSP0_MISO pin GPIO2.6
247
        // SSP0_MISO pin GPIO2.6
246
        GPIO_StructInit(&GPIO_InitStructure);
248
        GPIO_StructInit(&GPIO_InitStructure);
247
        GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput;
249
        GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput;
248
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
250
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
249
        GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;
251
        GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;
250
        GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Enable;
252
        GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Enable;
251
        GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt2; //SSP0_MISO
253
        GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt2; //SSP0_MISO
252
        GPIO_Init (GPIO2, &GPIO_InitStructure);
254
        GPIO_Init (GPIO2, &GPIO_InitStructure);
253
 
255
 
254
        SSP_DeInit(SSP0);
256
        SSP_DeInit(SSP0);
255
        SSP_StructInit(&SSP_InitStructure);
257
        SSP_StructInit(&SSP_InitStructure);
256
        SSP_InitStructure.SSP_FrameFormat = SSP_FrameFormat_Motorola;
258
        SSP_InitStructure.SSP_FrameFormat = SSP_FrameFormat_Motorola;
257
        SSP_InitStructure.SSP_Mode = SSP_Mode_Slave;
259
        SSP_InitStructure.SSP_Mode = SSP_Mode_Slave;
258
        SSP_InitStructure.SSP_SlaveOutput = SSP_SlaveOutput_Enable;
260
        SSP_InitStructure.SSP_SlaveOutput = SSP_SlaveOutput_Enable;
259
        SSP_InitStructure.SSP_CPHA = SSP_CPHA_1Edge;
261
        SSP_InitStructure.SSP_CPHA = SSP_CPHA_1Edge;
260
        SSP_InitStructure.SSP_CPOL = SSP_CPOL_Low;
262
        SSP_InitStructure.SSP_CPOL = SSP_CPOL_Low;
261
        SSP_InitStructure.SSP_ClockRate = 0;
263
        SSP_InitStructure.SSP_ClockRate = 0;
262
 
264
 
263
        SSP_Init(SSP0, &SSP_InitStructure);
265
        SSP_Init(SSP0, &SSP_InitStructure);
264
 
266
 
265
        //SSP_ITConfig(SSP0, SSP_IT_RxFifo | SSP_IT_TxFifo | SSP_IT_RxTimeOut, ENABLE);
267
        //SSP_ITConfig(SSP0, SSP_IT_RxFifo | SSP_IT_TxFifo | SSP_IT_RxTimeOut, ENABLE);
266
        SSP_ITConfig(SSP0, SSP_IT_RxFifo | SSP_IT_RxTimeOut, ENABLE);
268
        SSP_ITConfig(SSP0, SSP_IT_RxFifo | SSP_IT_RxTimeOut, ENABLE);
267
 
269
 
268
 
270
 
269
        fifo_init(&CompassCalcStateFiFo, CompassCalStateQueue, sizeof(CompassCalStateQueue));
271
        fifo_init(&CompassCalcStateFiFo, CompassCalStateQueue, sizeof(CompassCalStateQueue));
270
 
272
 
271
        SSP_Cmd(SSP0, ENABLE);
273
        SSP_Cmd(SSP0, ENABLE);
272
        // initialize the syncbytes in the tx buffer
274
        // initialize the syncbytes in the tx buffer
273
        SPI_TxBuffer[0] = SPI_TXSYNCBYTE1;
275
        SPI_TxBuffer[0] = SPI_TXSYNCBYTE1;
274
        SPI_TxBuffer[1] = SPI_TXSYNCBYTE2;
276
        SPI_TxBuffer[1] = SPI_TXSYNCBYTE2;
275
        // set the pointer to the checksum byte in the tx buffer
277
        // set the pointer to the checksum byte in the tx buffer
276
        Ptr_TxChksum = (u8 *) &(((ToFlightCtrl_t *) &(SPI_TxBuffer[2]))->Chksum);
278
        Ptr_TxChksum = (u8 *) &(((ToFlightCtrl_t *) &(SPI_TxBuffer[2]))->Chksum);
277
 
279
 
278
        VIC_Config(SSP0_ITLine, VIC_IRQ, PRIORITY_SPI0);
280
        VIC_Config(SSP0_ITLine, VIC_IRQ, PRIORITY_SPI0);
279
        VIC_ITCmd(SSP0_ITLine, ENABLE);
281
        VIC_ITCmd(SSP0_ITLine, ENABLE);
-
 
282
 
-
 
283
        SPI0_Timeout = SetDelay(4*SPI0_TIMEOUT);
280
 
284
 
281
        UART1_PutString("ok");
285
        UART1_PutString("ok");
282
}
286
}
283
 
287
 
284
//------------------------------------------------------
288
//------------------------------------------------------
285
void SPI0_UpdateBuffer(void)
289
void SPI0_UpdateBuffer(void)
286
{
290
{
287
        if (SPI_RxBuffer_Request)
291
        if (SPI_RxBuffer_Request)
288
        {
292
        {
289
                // avoid sending data via SPI during the update of the  ToFlightCtrl structure
293
                // avoid sending data via SPI during the update of the  ToFlightCtrl structure
290
                VIC_ITCmd(SSP0_ITLine, DISABLE); // disable SPI interrupt
294
                VIC_ITCmd(SSP0_ITLine, DISABLE); // disable SPI interrupt
291
 
295
 
292
                ToFlightCtrl.CompassHeading = I2C_Heading.Heading;
296
                ToFlightCtrl.CompassHeading = I2C_Heading.Heading;
293
                ToFlightCtrl.GPS_Nick   = GPS_Stick.Nick;
297
                ToFlightCtrl.GPS_Nick   = GPS_Stick.Nick;
294
                ToFlightCtrl.GPS_Roll   = GPS_Stick.Roll;
298
                ToFlightCtrl.GPS_Roll   = GPS_Stick.Roll;
295
                ToFlightCtrl.GPS_Yaw    = GPS_Stick.Yaw;
299
                ToFlightCtrl.GPS_Yaw    = GPS_Stick.Yaw;
296
                // cycle spi commands
300
                // cycle spi commands
297
                ToFlightCtrl.Command = SPI_CommandSequence[SPI_CommandCounter++];
301
                ToFlightCtrl.Command = SPI_CommandSequence[SPI_CommandCounter++];
298
                // restart command cycle at the end
302
                // restart command cycle at the end
299
                if (SPI_CommandCounter >= sizeof(SPI_CommandSequence)) SPI_CommandCounter = 0;
303
                if (SPI_CommandCounter >= sizeof(SPI_CommandSequence)) SPI_CommandCounter = 0;
300
 
304
 
301
                switch (ToFlightCtrl.Command)
305
                switch (ToFlightCtrl.Command)
302
                {
306
                {
303
                        case  SPI_KALMAN:
307
                        case  SPI_KALMAN:
304
                                ToFlightCtrl.Param.sByte[0] = (s8) FC_Kalman_K;
308
                                ToFlightCtrl.Param.sByte[0] = (s8) FC_Kalman_K;
305
                                ToFlightCtrl.Param.sByte[1] = (s8) Kalman_MaxFusion;
309
                                ToFlightCtrl.Param.sByte[1] = (s8) Kalman_MaxFusion;
306
                                ToFlightCtrl.Param.sByte[2] = (s8) Kalman_MaxDrift;
310
                                ToFlightCtrl.Param.sByte[2] = (s8) Kalman_MaxDrift;
307
                                ToFlightCtrl.Param.Byte[3]      = (u8) SerialLinkOkay;
311
                                ToFlightCtrl.Param.Byte[3]      = (u8) SerialLinkOkay;
308
                                break;         
312
                                break;         
309
 
313
 
310
                        default:
314
                        default:
311
                                break;
315
                                break;
312
                }
316
                }
313
                VIC_ITCmd(SSP0_ITLine, ENABLE);         // enable SPI interrupt
317
                VIC_ITCmd(SSP0_ITLine, ENABLE);         // enable SPI interrupt
314
 
318
 
315
 
319
 
316
                switch(FromFlightCtrl.Command)
320
                switch(FromFlightCtrl.Command)
317
                {
321
                {
318
                        case SPI_CMD_USER:
322
                        case SPI_CMD_USER:
319
                                Parameter.User1 = FromFlightCtrl.Param.Byte[0];
323
                                Parameter.User1 = FromFlightCtrl.Param.Byte[0];
320
                                Parameter.User2 = FromFlightCtrl.Param.Byte[1];
324
                                Parameter.User2 = FromFlightCtrl.Param.Byte[1];
321
                                Parameter.User3 = FromFlightCtrl.Param.Byte[2];
325
                                Parameter.User3 = FromFlightCtrl.Param.Byte[2];
322
                                Parameter.User4 = FromFlightCtrl.Param.Byte[3];
326
                                Parameter.User4 = FromFlightCtrl.Param.Byte[3];
323
                                Parameter.User5 = FromFlightCtrl.Param.Byte[4];
327
                                Parameter.User5 = FromFlightCtrl.Param.Byte[4];
324
                                Parameter.User6 = FromFlightCtrl.Param.Byte[5];
328
                                Parameter.User6 = FromFlightCtrl.Param.Byte[5];
325
                                Parameter.User7 = FromFlightCtrl.Param.Byte[6];
329
                                Parameter.User7 = FromFlightCtrl.Param.Byte[6];
326
                                Parameter.User8 = FromFlightCtrl.Param.Byte[7];
330
                                Parameter.User8 = FromFlightCtrl.Param.Byte[7];
327
                                if(ClearMKFlags)
331
                                if(ClearMKFlags)
328
                                {
332
                                {
329
                                        FC.MKFlags = 0;
333
                                        FC.MKFlags = 0;
330
                                        ClearMKFlags = 0;
334
                                        ClearMKFlags = 0;
331
                                }
335
                                }
332
                                FC.MKFlags         |= FromFlightCtrl.Param.Byte[8];
336
                                FC.MKFlags         |= FromFlightCtrl.Param.Byte[8];
333
                                FC.UBat                 = FromFlightCtrl.Param.Byte[9];
337
                                FC.UBat                 = FromFlightCtrl.Param.Byte[9];
334
                                Parameter.LowVoltageWarning = FromFlightCtrl.Param.Byte[10];
338
                                Parameter.LowVoltageWarning = FromFlightCtrl.Param.Byte[10];
335
                                Parameter.ActiveSetting         = FromFlightCtrl.Param.Byte[11];
339
                                Parameter.ActiveSetting         = FromFlightCtrl.Param.Byte[11];
336
                                DebugOut.Analog[5] = FC.MKFlags;
340
                                DebugOut.Analog[5] = FC.MKFlags;
337
                                break;
341
                                break;
338
 
342
 
339
 #define CHK_POTI_MM(b,a,min,max) { if(a > 250) { if(a == 251) b = FC.Poti1; else if(a == 252) b = FC.Poti2; else if(a == 253) b = FC.Poti3; else if(a == 254) b = FC.Poti4;} else b = a; if(b <= min) b = min; else if(b >= max) b = max;}
343
 #define CHK_POTI_MM(b,a,min,max) { if(a > 250) { if(a == 251) b = FC.Poti1; else if(a == 252) b = FC.Poti2; else if(a == 253) b = FC.Poti3; else if(a == 254) b = FC.Poti4;} else b = a; if(b <= min) b = min; else if(b >= max) b = max;}
340
                        case SPI_CMD_PARAMETER1:
344
                        case SPI_CMD_PARAMETER1:
341
                                CHK_POTI_MM(Parameter.NaviGpsModeControl,FromFlightCtrl.Param.Byte[0],0,255);
345
                                CHK_POTI_MM(Parameter.NaviGpsModeControl,FromFlightCtrl.Param.Byte[0],0,255);
342
                                CHK_POTI_MM(Parameter.NaviGpsGain,FromFlightCtrl.Param.Byte[1],0,255);
346
                                CHK_POTI_MM(Parameter.NaviGpsGain,FromFlightCtrl.Param.Byte[1],0,255);
343
                                CHK_POTI_MM(Parameter.NaviGpsP,FromFlightCtrl.Param.Byte[2],0,255);
347
                                CHK_POTI_MM(Parameter.NaviGpsP,FromFlightCtrl.Param.Byte[2],0,255);
344
                                CHK_POTI_MM(Parameter.NaviGpsI,FromFlightCtrl.Param.Byte[3],0,255);
348
                                CHK_POTI_MM(Parameter.NaviGpsI,FromFlightCtrl.Param.Byte[3],0,255);
345
                                CHK_POTI_MM(Parameter.NaviGpsD,FromFlightCtrl.Param.Byte[4],0,255);
349
                                CHK_POTI_MM(Parameter.NaviGpsD,FromFlightCtrl.Param.Byte[4],0,255);
346
                                CHK_POTI_MM(Parameter.NaviGpsACC,FromFlightCtrl.Param.Byte[5],0,255);
350
                                CHK_POTI_MM(Parameter.NaviGpsACC,FromFlightCtrl.Param.Byte[5],0,255);
347
                                Parameter.NaviGpsMinSat = FromFlightCtrl.Param.Byte[6];
351
                                Parameter.NaviGpsMinSat = FromFlightCtrl.Param.Byte[6];
348
                                Parameter.NaviStickThreshold = FromFlightCtrl.Param.Byte[7];
352
                                Parameter.NaviStickThreshold = FromFlightCtrl.Param.Byte[7];
349
                                CHK_POTI_MM(Parameter.NaviOperatingRadius,FromFlightCtrl.Param.Byte[8],0,255);
353
                                CHK_POTI_MM(Parameter.NaviOperatingRadius,FromFlightCtrl.Param.Byte[8],0,255);
350
                                CHK_POTI_MM(Parameter.NaviWindCorrection,FromFlightCtrl.Param.Byte[9],0,255);
354
                                CHK_POTI_MM(Parameter.NaviWindCorrection,FromFlightCtrl.Param.Byte[9],0,255);
351
                                CHK_POTI_MM(Parameter.NaviSpeedCompensation,FromFlightCtrl.Param.Byte[10],0,255);
355
                                CHK_POTI_MM(Parameter.NaviSpeedCompensation,FromFlightCtrl.Param.Byte[10],0,255);
352
                                CHK_POTI_MM(Parameter.NaviAngleLimitation,FromFlightCtrl.Param.Byte[11],0,255);
356
                                CHK_POTI_MM(Parameter.NaviAngleLimitation,FromFlightCtrl.Param.Byte[11],0,255);
353
                                break;
357
                                break;
354
 
358
 
355
                        case SPI_CMD_STICK:
359
                        case SPI_CMD_STICK:
356
                                FC.StickGas     = FromFlightCtrl.Param.sByte[0];
360
                                FC.StickGas     = FromFlightCtrl.Param.sByte[0];
357
                                FC.StickYaw     = FromFlightCtrl.Param.sByte[1];
361
                                FC.StickYaw     = FromFlightCtrl.Param.sByte[1];
358
                                FC.StickRoll    = FromFlightCtrl.Param.sByte[2];
362
                                FC.StickRoll    = FromFlightCtrl.Param.sByte[2];
359
                                FC.StickNick    = FromFlightCtrl.Param.sByte[3];
363
                                FC.StickNick    = FromFlightCtrl.Param.sByte[3];
360
                                FC.Poti1                = FromFlightCtrl.Param.Byte[4];
364
                                FC.Poti1                = FromFlightCtrl.Param.Byte[4];
361
                                FC.Poti2                = FromFlightCtrl.Param.Byte[5];
365
                                FC.Poti2                = FromFlightCtrl.Param.Byte[5];
362
                                FC.Poti3                = FromFlightCtrl.Param.Byte[6];
366
                                FC.Poti3                = FromFlightCtrl.Param.Byte[6];
363
                                FC.Poti4                = FromFlightCtrl.Param.Byte[7];
367
                                FC.Poti4                = FromFlightCtrl.Param.Byte[7];
364
                                FC.RC_Quality   = FromFlightCtrl.Param.Byte[8];
368
                                FC.RC_Quality   = FromFlightCtrl.Param.Byte[8];
365
                                FC.RC_RSSI              = FromFlightCtrl.Param.Byte[9];
369
                                FC.RC_RSSI              = FromFlightCtrl.Param.Byte[9];
366
                                break;
370
                                break;
367
 
371
 
368
                        case SPI_CMD_MISC:
372
                        case SPI_CMD_MISC:
369
                                if(CompassCalState != FromFlightCtrl.Param.Byte[0])
373
                                if(CompassCalState != FromFlightCtrl.Param.Byte[0])
370
                                {       // put only new CompassCalState into queue to send via I2C
374
                                {       // put only new CompassCalState into queue to send via I2C
371
                                        CompassCalState = FromFlightCtrl.Param.Byte[0];
375
                                        CompassCalState = FromFlightCtrl.Param.Byte[0];
372
                                        fifo_put(&CompassCalcStateFiFo, CompassCalState);
376
                                        fifo_put(&CompassCalcStateFiFo, CompassCalState);
373
                                }
377
                                }
374
                                Parameter.NaviPH_LoginTime = FromFlightCtrl.Param.Byte[1];
378
                                Parameter.NaviPH_LoginTime = FromFlightCtrl.Param.Byte[1];
375
                                NaviData.Variometer = (NaviData.Variometer + 2 * ((s16) FromFlightCtrl.Param.Int[1] - NaviData.Altimeter)) / 2; // provisorisch
379
                                NaviData.Variometer = (NaviData.Variometer + 2 * ((s16) FromFlightCtrl.Param.Int[1] - NaviData.Altimeter)) / 2; // provisorisch
376
                                NaviData.Altimeter = (s16) FromFlightCtrl.Param.Int[1]; // is located at byte 2 and 3
380
                                NaviData.Altimeter = (s16) FromFlightCtrl.Param.Int[1]; // is located at byte 2 and 3
377
                                CHK_POTI_MM(Parameter.NaviGpsPLimit,FromFlightCtrl.Param.Byte[4],0,255);
381
                                CHK_POTI_MM(Parameter.NaviGpsPLimit,FromFlightCtrl.Param.Byte[4],0,255);
378
                                CHK_POTI_MM(Parameter.NaviGpsILimit,FromFlightCtrl.Param.Byte[5],0,255);
382
                                CHK_POTI_MM(Parameter.NaviGpsILimit,FromFlightCtrl.Param.Byte[5],0,255);
379
                                CHK_POTI_MM(Parameter.NaviGpsDLimit,FromFlightCtrl.Param.Byte[6],0,255);
383
                                CHK_POTI_MM(Parameter.NaviGpsDLimit,FromFlightCtrl.Param.Byte[6],0,255);
380
                                break;
384
                                break;
381
                       
385
                       
382
                        case SPI_CMD_SERVOS:
386
                        case SPI_CMD_SERVOS:
383
                                ServoParams.Refresh             = FromFlightCtrl.Param.Byte[0];
387
                                ServoParams.Refresh             = FromFlightCtrl.Param.Byte[0];
384
                                ServoParams.CompInvert  = FromFlightCtrl.Param.Byte[1];
388
                                ServoParams.CompInvert  = FromFlightCtrl.Param.Byte[1];
385
                                ServoParams.NickControl = FromFlightCtrl.Param.Byte[2];
389
                                ServoParams.NickControl = FromFlightCtrl.Param.Byte[2];
386
                                ServoParams.NickComp    = FromFlightCtrl.Param.Byte[3];
390
                                ServoParams.NickComp    = FromFlightCtrl.Param.Byte[3];
387
                                ServoParams.NickMin             = FromFlightCtrl.Param.Byte[4];
391
                                ServoParams.NickMin             = FromFlightCtrl.Param.Byte[4];
388
                                ServoParams.NickMax             = FromFlightCtrl.Param.Byte[5];
392
                                ServoParams.NickMax             = FromFlightCtrl.Param.Byte[5];
389
                                ServoParams.RollControl = FromFlightCtrl.Param.Byte[6];
393
                                ServoParams.RollControl = FromFlightCtrl.Param.Byte[6];
390
                                ServoParams.RollComp    = FromFlightCtrl.Param.Byte[7];
394
                                ServoParams.RollComp    = FromFlightCtrl.Param.Byte[7];
391
                                ServoParams.RollMin             = FromFlightCtrl.Param.Byte[8];
395
                                ServoParams.RollMin             = FromFlightCtrl.Param.Byte[8];
392
                                ServoParams.RollMax             = FromFlightCtrl.Param.Byte[9];
396
                                ServoParams.RollMax             = FromFlightCtrl.Param.Byte[9];
393
                                break;
397
                                break;
394
 
398
 
395
                        case SPI_CMD_VERSION:
399
                        case SPI_CMD_VERSION:
396
                                FC_Version.Major                = FromFlightCtrl.Param.Byte[0];
400
                                FC_Version.Major                = FromFlightCtrl.Param.Byte[0];
397
                                FC_Version.Minor                = FromFlightCtrl.Param.Byte[1];
401
                                FC_Version.Minor                = FromFlightCtrl.Param.Byte[1];
398
                                FC_Version.Patch                = FromFlightCtrl.Param.Byte[2];
402
                                FC_Version.Patch                = FromFlightCtrl.Param.Byte[2];
399
                                FC_Version.Compatible   = FromFlightCtrl.Param.Byte[3];
403
                                FC_Version.Compatible   = FromFlightCtrl.Param.Byte[3];
400
                                FC_Version.Hardware             = FromFlightCtrl.Param.Byte[4];
404
                                FC_Version.Hardware             = FromFlightCtrl.Param.Byte[4];
401
                                break;
405
                                break;
402
 
406
 
403
                        default:
407
                        default:
404
                        break;
408
                        break;
405
                }
409
                }
406
 
410
 
407
                // every time we got new data from the FC via SPI call the navigation routine
411
                // every time we got new data from the FC via SPI call the navigation routine
408
                GPS_Navigation();
412
                GPS_Navigation();
409
                ClearMKFlags = 1;
413
                ClearMKFlags = 1;
410
 
414
 
411
                SPI_RxBuffer_Request = 0;
415
                SPI_RxBuffer_Request = 0;
412
 
416
 
413
                DebugOut.Analog[0] = FromFlightCtrl.AngleNick;
417
                DebugOut.Analog[0] = FromFlightCtrl.AngleNick;
414
                DebugOut.Analog[1] = FromFlightCtrl.AngleRoll;
418
                DebugOut.Analog[1] = FromFlightCtrl.AngleRoll;
415
                DebugOut.Analog[2] = FromFlightCtrl.AccNick;
419
                DebugOut.Analog[2] = FromFlightCtrl.AccNick;
416
                DebugOut.Analog[3] = FromFlightCtrl.AccRoll;
420
                DebugOut.Analog[3] = FromFlightCtrl.AccRoll;
417
                DebugOut.Analog[11] = FromFlightCtrl.GyroHeading/10;// in deg
421
                DebugOut.Analog[11] = FromFlightCtrl.GyroHeading/10;// in deg
418
                Data3D.AngleNick = FromFlightCtrl.AngleNick;            // in 0.1 deg
422
                Data3D.AngleNick = FromFlightCtrl.AngleNick;            // in 0.1 deg
419
                Data3D.AngleRoll = FromFlightCtrl.AngleRoll;            // in 0.1 deg
423
                Data3D.AngleRoll = FromFlightCtrl.AngleRoll;            // in 0.1 deg
420
                Data3D.Heading   = FromFlightCtrl.GyroHeading;          // in 0.1 deg
424
                Data3D.Heading   = FromFlightCtrl.GyroHeading;          // in 0.1 deg
421
        }       // EOF if(SPI_RxBuffer_Request)
425
        }       // EOF if(SPI_RxBuffer_Request)
422
}
426
}
423
 
427
 
424
//------------------------------------------------------
428
//------------------------------------------------------
425
void SPI0_GetFlightCtrlVersion(void)
429
void SPI0_GetFlightCtrlVersion(void)
426
{
430
{
427
        u16 timeout;
431
        u16 timeout;
428
        u8 msg[64];
432
        u8 msg[64];
429
 
433
 
430
        FC_Version.Major = 0xFF;
434
        FC_Version.Major = 0xFF;
431
        FC_Version.Minor = 0xFF;
435
        FC_Version.Minor = 0xFF;
432
        FC_Version.Patch = 0xFF;
436
        FC_Version.Patch = 0xFF;
433
        FC_Version.Compatible = 0xFF;
437
        FC_Version.Compatible = 0xFF;
434
 
438
 
435
        // polling FC version info for 2 second
439
        // polling FC version info for 2 second
436
        timeout = SetDelay(2000);
440
        timeout = SetDelay(2000);
437
        do
441
        do
438
        {
442
        {
439
                SPI0_UpdateBuffer();
443
                SPI0_UpdateBuffer();
440
                if (FC_Version.Major != 0xFF)  break;
444
                if (FC_Version.Major != 0xFF)  break;
441
        }while (!CheckDelay(timeout));
445
        }while (!CheckDelay(timeout));
442
        // if we got it
446
        // if we got it
443
        if (FC_Version.Major != 0xFF)
447
        if (FC_Version.Major != 0xFF)
444
        {
448
        {
445
                sprintf(msg, "\n\r FlightCtrl V%d.%d%c HW:%d.%d", FC_Version.Major, FC_Version.Minor, 'a'+FC_Version.Patch, FC_Version.Hardware/10,FC_Version.Hardware%10);
449
                sprintf(msg, "\n\r FlightCtrl V%d.%d%c HW:%d.%d", FC_Version.Major, FC_Version.Minor, 'a'+FC_Version.Patch, FC_Version.Hardware/10,FC_Version.Hardware%10);
446
                UART1_PutString(msg);
450
                UART1_PutString(msg);
447
                sprintf(msg, " Compatible: %d", FC_Version.Compatible);
451
                sprintf(msg, " Compatible: %d", FC_Version.Compatible);
448
                UART1_PutString(msg);
452
                UART1_PutString(msg);
449
        }
453
        }
450
        else UART1_PutString("\n\r No version information from FlightCtrl.");
454
        else UART1_PutString("\n\r No version information from FlightCtrl.");
451
}
455
}
452
 
456
 
453
 
457
 
454
 
458