Subversion Repositories NaviCtrl

Rev

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

Rev 277 Rev 278
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 / NON-COMMERCIAL USE ONLY
6
// + Nur für den privaten Gebrauch / NON-COMMERCIAL USE ONLY
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 oder Nutzung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
28
// + Die Portierung oder Nutzung 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 the sources to other systems or using the software on other systems (except hardware from www.mikrokopter.de) is not allowed
43
// +   * porting the sources to other systems or using the software on other systems (except 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 "compass.h"
64
#include "compass.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 "compass.h" 
69
#include "compass.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
//communication packets
77
//communication packets
78
FromFlightCtrl_t   FromFlightCtrl;
78
FromFlightCtrl_t   FromFlightCtrl;
79
ToFlightCtrl_t     ToFlightCtrl;
79
ToFlightCtrl_t     ToFlightCtrl;
80
#define SPI0_TIMEOUT 500 // 500ms
80
#define SPI0_TIMEOUT 500 // 500ms
81
volatile u32 SPI0_Timeout = 0;
81
volatile u32 SPI0_Timeout = 0;
82
 
82
 
83
// tx packet buffer
83
// tx packet buffer
84
#define SPI_TXBUFFER_LEN (2 + sizeof(ToFlightCtrl)) // 2 bytes at start are for synchronization
84
#define SPI_TXBUFFER_LEN (2 + sizeof(ToFlightCtrl)) // 2 bytes at start are for synchronization
85
volatile u8 SPI_TxBuffer[SPI_TXBUFFER_LEN];
85
volatile u8 SPI_TxBuffer[SPI_TXBUFFER_LEN];
86
volatile u8 SPI_TxBufferIndex = 0;
86
volatile u8 SPI_TxBufferIndex = 0;
87
u8 *Ptr_TxChksum = NULL ;  // pointer to checksum in TxBuffer
87
u8 *Ptr_TxChksum = NULL ;  // pointer to checksum in TxBuffer
88
 
88
 
89
// rx packet buffer
89
// rx packet buffer
90
#define SPI_RXBUFFER_LEN sizeof(FromFlightCtrl)
90
#define SPI_RXBUFFER_LEN sizeof(FromFlightCtrl)
91
volatile u8 SPI_RxBuffer[SPI_RXBUFFER_LEN];
91
volatile u8 SPI_RxBuffer[SPI_RXBUFFER_LEN];
92
volatile u8 SPI_RxBufferIndex = 0;
92
volatile u8 SPI_RxBufferIndex = 0;
93
volatile u8 SPI_RxBuffer_Request = 0;
93
volatile u8 SPI_RxBuffer_Request = 0;
94
#define SPI_COMMAND_INDEX 0
94
#define SPI_COMMAND_INDEX 0
95
 
95
 
96
s32 Kalman_K = 32;
96
s32 Kalman_K = 32;
97
s32 Kalman_MaxDrift = 5 * 16;
97
s32 Kalman_MaxDrift = 5 * 16;
98
s32 Kalman_MaxFusion = 64;
98
s32 Kalman_MaxFusion = 64;
99
s32 ToFcGpsZ = 0;
99
s32 ToFcGpsZ = 0;
100
 
100
 
101
u8 SPI_CommandSequence[] = { SPI_NCCMD_VERSION, SPI_NCCMD_KALMAN, SPI_NCCMD_GPSINFO ,SPI_NCCMD_KALMAN, SPI_NCCMD_KALMAN};
101
u8 SPI_CommandSequence[] = { SPI_NCCMD_VERSION, SPI_NCCMD_KALMAN, SPI_NCCMD_GPSINFO ,SPI_NCCMD_KALMAN, SPI_NCCMD_KALMAN};
102
u8 SPI_CommandCounter = 0;
102
u8 SPI_CommandCounter = 0;
103
s32 ToFC_Rotate_C = 64, ToFC_Rotate_S = 0;
103
s32 ToFC_Rotate_C = 64, ToFC_Rotate_S = 0;
104
s32 HeadFreeStartAngle = 0;
104
s32 HeadFreeStartAngle = 0;
105
s16 NewCompassDirectionSetpoint = -1;
-
 
106
s16 FC_WP_EventChannel = 0; // gibt einen Schaltkanal an die FC weiter, wenn der Wegpunkt erreicht wurde
105
s16 FC_WP_EventChannel = 0; // gibt einen Schaltkanal an die FC weiter, wenn der Wegpunkt erreicht wurde
107
 
106
 
108
SPI_Version_t FC_Version;
107
SPI_Version_t FC_Version;
109
 
108
 
110
//--------------------------------------------------------------
109
//--------------------------------------------------------------
111
void SSP0_IRQHandler(void)
110
void SSP0_IRQHandler(void)
112
{
111
{
113
        static u8 rxchksum = 0;
112
        static u8 rxchksum = 0;
114
        u8 rxdata;
113
        u8 rxdata;
115
 
114
 
116
        #define SPI_SYNC1       0
115
        #define SPI_SYNC1       0
117
        #define SPI_SYNC2       1
116
        #define SPI_SYNC2       1
118
        #define SPI_DATA        2
117
        #define SPI_DATA        2
119
        static u8 SPI_State = SPI_SYNC1;
118
        static u8 SPI_State = SPI_SYNC1;
120
 
119
 
121
        IENABLE;
120
        IENABLE;
122
 
121
 
123
        // clear pending bits
122
        // clear pending bits
124
        SSP_ClearITPendingBit(SSP0, SSP_IT_RxTimeOut);
123
        SSP_ClearITPendingBit(SSP0, SSP_IT_RxTimeOut);
125
        SSP_ClearITPendingBit(SSP0, SSP_IT_RxFifo);
124
        SSP_ClearITPendingBit(SSP0, SSP_IT_RxFifo);
126
 
125
 
127
        // while RxFIFO not empty
126
        // while RxFIFO not empty
128
        while (SSP_GetFlagStatus(SSP0, SSP_FLAG_RxFifoNotEmpty) == SET)
127
        while (SSP_GetFlagStatus(SSP0, SSP_FLAG_RxFifoNotEmpty) == SET)
129
        {
128
        {
130
                rxdata =  SSP0->DR; // catch the received byte
129
                rxdata =  SSP0->DR; // catch the received byte
131
                // Fill TxFIFO while its not full or end of packet is reached
130
                // Fill TxFIFO while its not full or end of packet is reached
132
                while (SSP_GetFlagStatus(SSP0, SSP_FLAG_TxFifoNotFull) == SET)
131
                while (SSP_GetFlagStatus(SSP0, SSP_FLAG_TxFifoNotFull) == SET)
133
                {
132
                {
134
                        if (SPI_TxBufferIndex  < SPI_TXBUFFER_LEN)   // still data to send ?
133
                        if (SPI_TxBufferIndex  < SPI_TXBUFFER_LEN)   // still data to send ?
135
                        {
134
                        {
136
                                SSP0->DR = SPI_TxBuffer[SPI_TxBufferIndex];       // send a byte
135
                                SSP0->DR = SPI_TxBuffer[SPI_TxBufferIndex];       // send a byte
137
                                *Ptr_TxChksum += SPI_TxBuffer[SPI_TxBufferIndex]; // update checksum
136
                                *Ptr_TxChksum += SPI_TxBuffer[SPI_TxBufferIndex]; // update checksum
138
                                SPI_TxBufferIndex++; // pointer to next byte
137
                                SPI_TxBufferIndex++; // pointer to next byte
139
                        }
138
                        }
140
                        else // end of packet is reached reset and copy data to tx buffer
139
                        else // end of packet is reached reset and copy data to tx buffer
141
                        {
140
                        {
142
                                SPI_TxBufferIndex = 0;  // reset buffer index
141
                                SPI_TxBufferIndex = 0;  // reset buffer index
143
                                ToFlightCtrl.Chksum = 0;  // initialize checksum
142
                                ToFlightCtrl.Chksum = 0;  // initialize checksum
144
                                ToFlightCtrl.BeepTime = BeepTime;  // set beeptime
143
                                ToFlightCtrl.BeepTime = BeepTime;  // set beeptime
145
                                BeepTime = 0; // reset local beeptime
144
                                BeepTime = 0; // reset local beeptime
146
                                // copy contents of ToFlightCtrl->SPI_TxBuffer
145
                                // copy contents of ToFlightCtrl->SPI_TxBuffer
147
                                memcpy((u8 *) &(SPI_TxBuffer[2]), (u8 *) &ToFlightCtrl, sizeof(ToFlightCtrl));
146
                                memcpy((u8 *) &(SPI_TxBuffer[2]), (u8 *) &ToFlightCtrl, sizeof(ToFlightCtrl));
148
                        }
147
                        }
149
                }
148
                }
150
                switch (SPI_State)
149
                switch (SPI_State)
151
                {
150
                {
152
                        case SPI_SYNC1:
151
                        case SPI_SYNC1:
153
                                SPI_RxBufferIndex = 0; // reset buffer index
152
                                SPI_RxBufferIndex = 0; // reset buffer index
154
                                rxchksum = rxdata;     // init checksum
153
                                rxchksum = rxdata;     // init checksum
155
                                if (rxdata == SPI_RXSYNCBYTE1)
154
                                if (rxdata == SPI_RXSYNCBYTE1)
156
                                {   // 1st syncbyte ok
155
                                {   // 1st syncbyte ok
157
                                        SPI_State = SPI_SYNC2;  // step to sync2
156
                                        SPI_State = SPI_SYNC2;  // step to sync2
158
                                }
157
                                }
159
                                break;
158
                                break;
160
                        case SPI_SYNC2:
159
                        case SPI_SYNC2:
161
                                if (rxdata == SPI_RXSYNCBYTE2)
160
                                if (rxdata == SPI_RXSYNCBYTE2)
162
                                {  // 2nd Syncbyte ok
161
                                {  // 2nd Syncbyte ok
163
                                        rxchksum += rxdata;
162
                                        rxchksum += rxdata;
164
                                        SPI_State = SPI_DATA;
163
                                        SPI_State = SPI_DATA;
165
                                }  // 2nd Syncbyte does not match
164
                                }  // 2nd Syncbyte does not match
166
                                else
165
                                else
167
                                {
166
                                {
168
                                        SPI_State  = SPI_SYNC1; //jump back to sync1
167
                                        SPI_State  = SPI_SYNC1; //jump back to sync1
169
                                }
168
                                }
170
                                break;
169
                                break;
171
                        case SPI_DATA:
170
                        case SPI_DATA:
172
                                SPI_RxBuffer[SPI_RxBufferIndex++]= rxdata; // copy databyte to rx buffer
171
                                SPI_RxBuffer[SPI_RxBufferIndex++]= rxdata; // copy databyte to rx buffer
173
                                if (SPI_RxBufferIndex >= SPI_RXBUFFER_LEN) // end of packet is reached
172
                                if (SPI_RxBufferIndex >= SPI_RXBUFFER_LEN) // end of packet is reached
174
                                {
173
                                {
175
                                        if (rxdata == rxchksum) // verify checksum byte
174
                                        if (rxdata == rxchksum) // verify checksum byte
176
                                        {
175
                                        {
177
                                                // copy SPI_RxBuffer -> FromFlightCtrl
176
                                                // copy SPI_RxBuffer -> FromFlightCtrl
178
                                                if(!SPI_RxBuffer_Request) // block writing to FromFlightCtrl on reading access
177
                                                if(!SPI_RxBuffer_Request) // block writing to FromFlightCtrl on reading access
179
                                                {
178
                                                {
180
                                                        memcpy((u8 *) &FromFlightCtrl, (u8 *) SPI_RxBuffer, sizeof(FromFlightCtrl));
179
                                                        memcpy((u8 *) &FromFlightCtrl, (u8 *) SPI_RxBuffer, sizeof(FromFlightCtrl));
181
                                                        SPI_RxBuffer_Request = 1;
180
                                                        SPI_RxBuffer_Request = 1;
182
                                                }
181
                                                }
183
                                                // reset timeout counter on good packet
182
                                                // reset timeout counter on good packet
184
                                                SPI0_Timeout = SetDelay(SPI0_TIMEOUT);
183
                                                SPI0_Timeout = SetDelay(SPI0_TIMEOUT);
185
                                                DebugOut.Analog[13]++;
184
                                                DebugOut.Analog[13]++;
186
                                        }
185
                                        }
187
                                        else // bad checksum byte
186
                                        else // bad checksum byte
188
                                        {
187
                                        {
189
                                                DebugOut.Analog[12]++; // increase SPI chksum error counter
188
                                                DebugOut.Analog[12]++; // increase SPI chksum error counter
190
                                        }
189
                                        }
191
                                        SPI_State  = SPI_SYNC1; // reset state
190
                                        SPI_State  = SPI_SYNC1; // reset state
192
                                }
191
                                }
193
                                else // end of packet not reached
192
                                else // end of packet not reached
194
                                {
193
                                {
195
                                        rxchksum += rxdata;      // update checksum
194
                                        rxchksum += rxdata;      // update checksum
196
                                }
195
                                }
197
                                break;
196
                                break;
198
                        default:
197
                        default:
199
                                SPI_State  = SPI_SYNC1;
198
                                SPI_State  = SPI_SYNC1;
200
                                break;
199
                                break;
201
                }
200
                }
202
        }
201
        }
203
 
202
 
204
        IDISABLE;
203
        IDISABLE;
205
}
204
}
206
 
205
 
207
//--------------------------------------------------------------
206
//--------------------------------------------------------------
208
void SPI0_Init(void)
207
void SPI0_Init(void)
209
{
208
{
210
        GPIO_InitTypeDef GPIO_InitStructure;
209
        GPIO_InitTypeDef GPIO_InitStructure;
211
        SSP_InitTypeDef   SSP_InitStructure;
210
        SSP_InitTypeDef   SSP_InitStructure;
212
 
211
 
213
        UART1_PutString("\r\n SPI init...");
212
        UART1_PutString("\r\n SPI init...");
214
 
213
 
215
        SCU_APBPeriphClockConfig(__GPIO2 ,ENABLE);
214
        SCU_APBPeriphClockConfig(__GPIO2 ,ENABLE);
216
        SCU_APBPeriphClockConfig(__SSP0 ,ENABLE);
215
        SCU_APBPeriphClockConfig(__SSP0 ,ENABLE);
217
 
216
 
218
        GPIO_DeInit(GPIO2);
217
        GPIO_DeInit(GPIO2);
219
        //SSP0_CLK, SSP0_MOSI, SSP0_NSS pins
218
        //SSP0_CLK, SSP0_MOSI, SSP0_NSS pins
220
        GPIO_StructInit(&GPIO_InitStructure);
219
        GPIO_StructInit(&GPIO_InitStructure);
221
        GPIO_InitStructure.GPIO_Direction = GPIO_PinInput;
220
        GPIO_InitStructure.GPIO_Direction = GPIO_PinInput;
222
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_7;
221
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_7;
223
        GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;
222
        GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;
224
        GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Enable;
223
        GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Enable;
225
        GPIO_InitStructure.GPIO_Alternate = GPIO_InputAlt1; //SSP0_SCLK, SSP0_MOSI, SSP0_NSS
224
        GPIO_InitStructure.GPIO_Alternate = GPIO_InputAlt1; //SSP0_SCLK, SSP0_MOSI, SSP0_NSS
226
        GPIO_Init (GPIO2, &GPIO_InitStructure);
225
        GPIO_Init (GPIO2, &GPIO_InitStructure);
227
 
226
 
228
        // SSP0_MISO pin GPIO2.6
227
        // SSP0_MISO pin GPIO2.6
229
        GPIO_StructInit(&GPIO_InitStructure);
228
        GPIO_StructInit(&GPIO_InitStructure);
230
        GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput;
229
        GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput;
231
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
230
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
232
        GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;
231
        GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;
233
        GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Enable;
232
        GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Enable;
234
        GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt2; //SSP0_MISO
233
        GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt2; //SSP0_MISO
235
        GPIO_Init (GPIO2, &GPIO_InitStructure);
234
        GPIO_Init (GPIO2, &GPIO_InitStructure);
236
 
235
 
237
        SSP_DeInit(SSP0);
236
        SSP_DeInit(SSP0);
238
        SSP_StructInit(&SSP_InitStructure);
237
        SSP_StructInit(&SSP_InitStructure);
239
        SSP_InitStructure.SSP_FrameFormat = SSP_FrameFormat_Motorola;
238
        SSP_InitStructure.SSP_FrameFormat = SSP_FrameFormat_Motorola;
240
        SSP_InitStructure.SSP_Mode = SSP_Mode_Slave;
239
        SSP_InitStructure.SSP_Mode = SSP_Mode_Slave;
241
        SSP_InitStructure.SSP_SlaveOutput = SSP_SlaveOutput_Enable;
240
        SSP_InitStructure.SSP_SlaveOutput = SSP_SlaveOutput_Enable;
242
        SSP_InitStructure.SSP_CPHA = SSP_CPHA_1Edge;
241
        SSP_InitStructure.SSP_CPHA = SSP_CPHA_1Edge;
243
        SSP_InitStructure.SSP_CPOL = SSP_CPOL_Low;
242
        SSP_InitStructure.SSP_CPOL = SSP_CPOL_Low;
244
        SSP_InitStructure.SSP_ClockRate = 0;
243
        SSP_InitStructure.SSP_ClockRate = 0;
245
 
244
 
246
        SSP_Init(SSP0, &SSP_InitStructure);
245
        SSP_Init(SSP0, &SSP_InitStructure);
247
        SSP_ITConfig(SSP0, SSP_IT_RxFifo | SSP_IT_RxTimeOut, ENABLE);
246
        SSP_ITConfig(SSP0, SSP_IT_RxFifo | SSP_IT_RxTimeOut, ENABLE);
248
 
247
 
249
        SSP_Cmd(SSP0, ENABLE);
248
        SSP_Cmd(SSP0, ENABLE);
250
        // initialize the syncbytes in the tx buffer
249
        // initialize the syncbytes in the tx buffer
251
        SPI_TxBuffer[0] = SPI_TXSYNCBYTE1;
250
        SPI_TxBuffer[0] = SPI_TXSYNCBYTE1;
252
        SPI_TxBuffer[1] = SPI_TXSYNCBYTE2;
251
        SPI_TxBuffer[1] = SPI_TXSYNCBYTE2;
253
        // set the pointer to the checksum byte in the tx buffer
252
        // set the pointer to the checksum byte in the tx buffer
254
        Ptr_TxChksum = (u8 *) &(((ToFlightCtrl_t *) &(SPI_TxBuffer[2]))->Chksum);
253
        Ptr_TxChksum = (u8 *) &(((ToFlightCtrl_t *) &(SPI_TxBuffer[2]))->Chksum);
255
 
254
 
256
        ToFlightCtrl.GPSStick.Nick = 0;
255
        ToFlightCtrl.GPSStick.Nick = 0;
257
        ToFlightCtrl.GPSStick.Roll = 0;
256
        ToFlightCtrl.GPSStick.Roll = 0;
258
        ToFlightCtrl.GPSStick.Yaw = 0;
257
        ToFlightCtrl.GPSStick.Yaw = 0;
259
 
258
 
260
        VIC_Config(SSP0_ITLine, VIC_IRQ, PRIORITY_SPI0);
259
        VIC_Config(SSP0_ITLine, VIC_IRQ, PRIORITY_SPI0);
261
        VIC_ITCmd(SSP0_ITLine, ENABLE);
260
        VIC_ITCmd(SSP0_ITLine, ENABLE);
262
 
261
 
263
        SPI0_Timeout = SetDelay(4*SPI0_TIMEOUT);
262
        SPI0_Timeout = SetDelay(4*SPI0_TIMEOUT);
264
 
263
 
265
        UART1_PutString("ok");
264
        UART1_PutString("ok");
266
}
265
}
267
 
266
 
268
 
267
 
269
//------------------------------------------------------
268
//------------------------------------------------------
270
void SPI0_UpdateBuffer(void)
269
void SPI0_UpdateBuffer(void)
271
{
270
{
272
        static u32 timeout = 0;
271
        static u32 timeout = 0;
273
        static u8 counter = 50;
272
        static u8 counter = 50;
274
        static u8 CompassCalState = 0;
273
        static u8 CompassCalState = 0;
275
        static u8 FCCalibActive = 0;
274
        static u8 FCCalibActive = 0;
276
 
275
 
277
        if (SPI_RxBuffer_Request)
276
        if (SPI_RxBuffer_Request)
278
        {
277
        {
279
                // avoid sending data via SPI during the update of the  ToFlightCtrl structure
278
                // avoid sending data via SPI during the update of the  ToFlightCtrl structure
280
                VIC_ITCmd(SSP0_ITLine, DISABLE); // disable SPI interrupt
279
                VIC_ITCmd(SSP0_ITLine, DISABLE); // disable SPI interrupt
281
                ToFlightCtrl.CompassHeading = Compass_Heading;
280
                ToFlightCtrl.CompassHeading = Compass_Heading;
282
                DebugOut.Analog[10] = ToFlightCtrl.CompassHeading;
281
                DebugOut.Analog[10] = ToFlightCtrl.CompassHeading;
283
                if(ToFlightCtrl.CompassHeading >= 0) ToFlightCtrl.CompassHeading = (360 + ToFlightCtrl.CompassHeading + FromFlightCtrl.GyroYaw / 12) % 360;
282
                if(ToFlightCtrl.CompassHeading >= 0) ToFlightCtrl.CompassHeading = (360 + ToFlightCtrl.CompassHeading + FromFlightCtrl.GyroYaw / 12) % 360;
284
                // cycle spi commands
283
                // cycle spi commands
285
                ToFlightCtrl.Command = SPI_CommandSequence[SPI_CommandCounter++];
284
                ToFlightCtrl.Command = SPI_CommandSequence[SPI_CommandCounter++];
286
                // restart command cycle at the end
285
                // restart command cycle at the end
287
                if (SPI_CommandCounter >= sizeof(SPI_CommandSequence)) SPI_CommandCounter = 0;
286
                if (SPI_CommandCounter >= sizeof(SPI_CommandSequence)) SPI_CommandCounter = 0;
288
 
287
 
289
                switch (ToFlightCtrl.Command)
288
                switch (ToFlightCtrl.Command)
290
                {
289
                {
291
                        case  SPI_NCCMD_KALMAN:
290
                        case  SPI_NCCMD_KALMAN:
292
                                CalcHeadFree();
291
                                CalcHeadFree();
293
                                ToFlightCtrl.Param.sByte[0] = (s8) Kalman_K;
292
                                ToFlightCtrl.Param.sByte[0] = (s8) Kalman_K;
294
                                ToFlightCtrl.Param.sByte[1] = (s8) Kalman_MaxFusion;
293
                                ToFlightCtrl.Param.sByte[1] = (s8) Kalman_MaxFusion;
295
                                ToFlightCtrl.Param.sByte[2] = (s8) Kalman_MaxDrift;
294
                                ToFlightCtrl.Param.sByte[2] = (s8) Kalman_MaxDrift;
296
                                ToFlightCtrl.Param.Byte[3]      = (u8) SerialLinkOkay;
295
                                ToFlightCtrl.Param.Byte[3]      = (u8) SerialLinkOkay;
297
                                ToFlightCtrl.Param.sByte[4] = (s8) ToFcGpsZ;
296
                                ToFlightCtrl.Param.sByte[4] = (s8) ToFcGpsZ;
298
                                ToFlightCtrl.Param.Byte[5] = (s8) ToFC_Rotate_C;
297
                                ToFlightCtrl.Param.Byte[5] = (s8) ToFC_Rotate_C;
299
                                ToFlightCtrl.Param.Byte[6] = (s8) ToFC_Rotate_S;
298
                                ToFlightCtrl.Param.Byte[6] = (s8) ToFC_Rotate_S;
-
 
299
                                if(CAM_Orientation.State == NEWDATA)
-
 
300
                                {
300
                                // ToFlightCtrl.Param.Byte[7] ist frei
301
                                        ToFlightCtrl.Param.sInt[4] = CAM_Orientation.Azimuth;
301
                                ToFlightCtrl.Param.sInt[4] = (s16) NewCompassDirectionSetpoint;
302
                                        ToFlightCtrl.Param.sInt[5] = CAM_Orientation.Elevation;
302
                                NewCompassDirectionSetpoint = -1; // delete the Value
303
                                        CAM_Orientation.State = PROCESSED;
-
 
304
                                }
-
 
305
                                else
-
 
306
                                {       // set invalid values
-
 
307
                                        ToFlightCtrl.Param.sInt[4] = -1;
-
 
308
                                        ToFlightCtrl.Param.sInt[5] = -1;
-
 
309
                                }
303
                                break;
310
                                break;
304
 
311
 
305
                        case SPI_NCCMD_VERSION:
312
                        case SPI_NCCMD_VERSION:
306
                                ToFlightCtrl.Param.Byte[0] = VERSION_MAJOR;
313
                                ToFlightCtrl.Param.Byte[0] = VERSION_MAJOR;
307
                                ToFlightCtrl.Param.Byte[1] = VERSION_MINOR;
314
                                ToFlightCtrl.Param.Byte[1] = VERSION_MINOR;
308
                                ToFlightCtrl.Param.Byte[2] = VERSION_PATCH;
315
                                ToFlightCtrl.Param.Byte[2] = VERSION_PATCH;
309
                                ToFlightCtrl.Param.Byte[3] = FC_SPI_COMPATIBLE;
316
                                ToFlightCtrl.Param.Byte[3] = FC_SPI_COMPATIBLE;
310
                                ToFlightCtrl.Param.Byte[4] = Version_HW;
317
                                ToFlightCtrl.Param.Byte[4] = Version_HW;
311
                                ToFlightCtrl.Param.Byte[5] = DebugOut.Status[0];
318
                                ToFlightCtrl.Param.Byte[5] = DebugOut.Status[0];
312
                                ToFlightCtrl.Param.Byte[6] = DebugOut.Status[1];
319
                                ToFlightCtrl.Param.Byte[6] = DebugOut.Status[1];
313
                                ToFlightCtrl.Param.Byte[7] = ErrorCode;
320
                                ToFlightCtrl.Param.Byte[7] = ErrorCode;
314
                                break;
321
                                break;
315
 
322
 
316
                        case SPI_NCCMD_GPSINFO:
323
                        case SPI_NCCMD_GPSINFO:
317
                                ToFlightCtrl.Param.Byte[0] = GPSData.Flags;
324
                                ToFlightCtrl.Param.Byte[0] = GPSData.Flags;
318
                                ToFlightCtrl.Param.Byte[1] = GPSData.NumOfSats;
325
                                ToFlightCtrl.Param.Byte[1] = GPSData.NumOfSats;
319
                                ToFlightCtrl.Param.Byte[2] = GPSData.SatFix;
326
                                ToFlightCtrl.Param.Byte[2] = GPSData.SatFix;
320
                                ToFlightCtrl.Param.Byte[3] = GPSData.Speed_Ground / 100; // m/s
327
                                ToFlightCtrl.Param.Byte[3] = GPSData.Speed_Ground / 100; // m/s
321
                                ToFlightCtrl.Param.Int[2]  = NaviData.HomePositionDeviation.Distance; // dm   //4&5
328
                                ToFlightCtrl.Param.Int[2]  = NaviData.HomePositionDeviation.Distance; // dm   //4&5
322
                                ToFlightCtrl.Param.sInt[3] = NaviData.HomePositionDeviation.Bearing;  // deg  //6&7
329
                                ToFlightCtrl.Param.sInt[3] = NaviData.HomePositionDeviation.Bearing;  // deg  //6&7
323
                                ToFlightCtrl.Param.Byte[8] = (s8)(FC_WP_EventChannel - 110);
330
                                ToFlightCtrl.Param.Byte[8] = (s8)(FC_WP_EventChannel - 110);
324
                                break;
331
                                break;
325
                        default:
332
                        default:
326
                                break;
333
                                break;
327
                }
334
                }
328
                VIC_ITCmd(SSP0_ITLine, ENABLE);         // enable SPI interrupt
335
                VIC_ITCmd(SSP0_ITLine, ENABLE);         // enable SPI interrupt
329
 
336
 
330
 
337
 
331
                switch(FromFlightCtrl.Command)
338
                switch(FromFlightCtrl.Command)
332
                {
339
                {
333
                        case SPI_FCCMD_USER:
340
                        case SPI_FCCMD_USER:
334
                                Parameter.User1 = FromFlightCtrl.Param.Byte[0];
341
                                Parameter.User1 = FromFlightCtrl.Param.Byte[0];
335
                                Parameter.User2 = FromFlightCtrl.Param.Byte[1];
342
                                Parameter.User2 = FromFlightCtrl.Param.Byte[1];
336
                                Parameter.User3 = FromFlightCtrl.Param.Byte[2];
343
                                Parameter.User3 = FromFlightCtrl.Param.Byte[2];
337
                                Parameter.User4 = FromFlightCtrl.Param.Byte[3];
344
                                Parameter.User4 = FromFlightCtrl.Param.Byte[3];
338
                                Parameter.User5 = FromFlightCtrl.Param.Byte[4];
345
                                Parameter.User5 = FromFlightCtrl.Param.Byte[4];
339
                                Parameter.User6 = FromFlightCtrl.Param.Byte[5];
346
                                Parameter.User6 = FromFlightCtrl.Param.Byte[5];
340
                                Parameter.User7 = FromFlightCtrl.Param.Byte[6];
347
                                Parameter.User7 = FromFlightCtrl.Param.Byte[6];
341
                                Parameter.User8 = FromFlightCtrl.Param.Byte[7];
348
                                Parameter.User8 = FromFlightCtrl.Param.Byte[7];
342
                                if(ClearFCStatusFlags)
349
                                if(ClearFCStatusFlags)
343
                                {
350
                                {
344
                                        FC.StatusFlags = 0;
351
                                        FC.StatusFlags = 0;
345
                                        ClearFCStatusFlags = 0;
352
                                        ClearFCStatusFlags = 0;
346
                                }
353
                                }
347
                                FC.StatusFlags |= FromFlightCtrl.Param.Byte[8];
354
                                FC.StatusFlags |= FromFlightCtrl.Param.Byte[8];
348
                                if(FC.StatusFlags&FC_STATUS_CALIBRATE && !FCCalibActive)
355
                                if(FC.StatusFlags&FC_STATUS_CALIBRATE && !FCCalibActive)
349
                                {
356
                                {
350
                                        Compass_Init();
357
                                        Compass_Init();
351
                                        FCCalibActive = 1;
358
                                        FCCalibActive = 1;
352
                                }
359
                                }
353
                                else
360
                                else
354
                                {
361
                                {
355
                                        FCCalibActive = 0;     
362
                                        FCCalibActive = 0;     
356
                                }
363
                                }
357
                                Parameter.ActiveSetting = FromFlightCtrl.Param.Byte[9];
364
                                Parameter.ActiveSetting = FromFlightCtrl.Param.Byte[9];
358
                                DebugOut.Analog[5] = FC.StatusFlags;
365
                                DebugOut.Analog[5] = FC.StatusFlags;
359
                                NaviData.FCStatusFlags = FC.StatusFlags;
366
                                NaviData.FCStatusFlags = FC.StatusFlags;
360
                                HeadFreeStartAngle = (s32) FromFlightCtrl.Param.Byte[10] * 20; // convert to 0.1°
367
                                HeadFreeStartAngle = (s32) FromFlightCtrl.Param.Byte[10] * 20; // convert to 0.1°
361
                                break;
368
                                break;
362
 
369
 
363
                        case SPI_FCCMD_ACCU:
370
                        case SPI_FCCMD_ACCU:
364
                                FC.BAT_Current = FromFlightCtrl.Param.Int[0];
371
                                FC.BAT_Current = FromFlightCtrl.Param.Int[0];
365
                                FC.BAT_UsedCapacity = FromFlightCtrl.Param.Int[1];
372
                                FC.BAT_UsedCapacity = FromFlightCtrl.Param.Int[1];
366
                                FC.BAT_Voltage = FromFlightCtrl.Param.Byte[4];
373
                                FC.BAT_Voltage = FromFlightCtrl.Param.Byte[4];
367
                                Parameter.LowVoltageWarning = FromFlightCtrl.Param.Byte[5];
374
                                Parameter.LowVoltageWarning = FromFlightCtrl.Param.Byte[5];
368
                                NaviData.UBat = FC.BAT_Voltage;
375
                                NaviData.UBat = FC.BAT_Voltage;
369
                                NaviData.Current = FC.BAT_Current;
376
                                NaviData.Current = FC.BAT_Current;
370
                                NaviData.UsedCapacity = FC.BAT_UsedCapacity;
377
                                NaviData.UsedCapacity = FC.BAT_UsedCapacity;
371
                                break;
378
                                break;
372
 
379
 
373
#define CHK_POTI(b,a) { if(a < 248) b = a; else b = FC.Poti[255 - a]; }
380
#define CHK_POTI(b,a) { if(a < 248) b = a; else b = FC.Poti[255 - a]; }
374
#define CHK_POTI_MM(b,a,min,max) {CHK_POTI(b,a); LIMIT_MIN_MAX(b, min, max); }
381
#define CHK_POTI_MM(b,a,min,max) {CHK_POTI(b,a); LIMIT_MIN_MAX(b, min, max); }
375
 
382
 
376
                        case SPI_FCCMD_PARAMETER1:
383
                        case SPI_FCCMD_PARAMETER1:
377
                                CHK_POTI_MM(Parameter.NaviGpsModeControl,FromFlightCtrl.Param.Byte[0],0,255);
384
                                CHK_POTI_MM(Parameter.NaviGpsModeControl,FromFlightCtrl.Param.Byte[0],0,255);
378
                                CHK_POTI_MM(Parameter.NaviGpsGain,FromFlightCtrl.Param.Byte[1],0,255);
385
                                CHK_POTI_MM(Parameter.NaviGpsGain,FromFlightCtrl.Param.Byte[1],0,255);
379
                                CHK_POTI_MM(Parameter.NaviGpsP,FromFlightCtrl.Param.Byte[2],0,255);
386
                                CHK_POTI_MM(Parameter.NaviGpsP,FromFlightCtrl.Param.Byte[2],0,255);
380
                                CHK_POTI_MM(Parameter.NaviGpsI,FromFlightCtrl.Param.Byte[3],0,255);
387
                                CHK_POTI_MM(Parameter.NaviGpsI,FromFlightCtrl.Param.Byte[3],0,255);
381
                                CHK_POTI_MM(Parameter.NaviGpsD,FromFlightCtrl.Param.Byte[4],0,255);
388
                                CHK_POTI_MM(Parameter.NaviGpsD,FromFlightCtrl.Param.Byte[4],0,255);
382
                                CHK_POTI_MM(Parameter.NaviGpsACC,FromFlightCtrl.Param.Byte[5],0,255);
389
                                CHK_POTI_MM(Parameter.NaviGpsACC,FromFlightCtrl.Param.Byte[5],0,255);
383
                                Parameter.NaviGpsMinSat = FromFlightCtrl.Param.Byte[6];
390
                                Parameter.NaviGpsMinSat = FromFlightCtrl.Param.Byte[6];
384
                                Parameter.NaviStickThreshold = FromFlightCtrl.Param.Byte[7];
391
                                Parameter.NaviStickThreshold = FromFlightCtrl.Param.Byte[7];
385
                                CHK_POTI_MM(Parameter.NaviOperatingRadius,FromFlightCtrl.Param.Byte[8],0,255);
392
                                CHK_POTI_MM(Parameter.NaviOperatingRadius,FromFlightCtrl.Param.Byte[8],0,255);
386
                                CHK_POTI_MM(Parameter.NaviWindCorrection,FromFlightCtrl.Param.Byte[9],0,255);
393
                                CHK_POTI_MM(Parameter.NaviWindCorrection,FromFlightCtrl.Param.Byte[9],0,255);
387
                                CHK_POTI_MM(Parameter.NaviSpeedCompensation,FromFlightCtrl.Param.Byte[10],0,255);
394
                                CHK_POTI_MM(Parameter.NaviSpeedCompensation,FromFlightCtrl.Param.Byte[10],0,255);
388
                                CHK_POTI_MM(Parameter.NaviAngleLimitation,FromFlightCtrl.Param.Byte[11],0,255);
395
                                CHK_POTI_MM(Parameter.NaviAngleLimitation,FromFlightCtrl.Param.Byte[11],0,255);
389
                                break;
396
                                break;
390
 
397
 
391
                        case SPI_FCCMD_STICK:
398
                        case SPI_FCCMD_STICK:
392
                                FC.StickGas     = FromFlightCtrl.Param.sByte[0];
399
                                FC.StickGas     = FromFlightCtrl.Param.sByte[0];
393
                                FC.StickYaw     = FromFlightCtrl.Param.sByte[1];
400
                                FC.StickYaw     = FromFlightCtrl.Param.sByte[1];
394
                                FC.StickRoll    = FromFlightCtrl.Param.sByte[2];
401
                                FC.StickRoll    = FromFlightCtrl.Param.sByte[2];
395
                                FC.StickNick    = FromFlightCtrl.Param.sByte[3];
402
                                FC.StickNick    = FromFlightCtrl.Param.sByte[3];
396
                                FC.Poti[0]              = FromFlightCtrl.Param.Byte[4];
403
                                FC.Poti[0]              = FromFlightCtrl.Param.Byte[4];
397
                                FC.Poti[1]              = FromFlightCtrl.Param.Byte[5];
404
                                FC.Poti[1]              = FromFlightCtrl.Param.Byte[5];
398
                                FC.Poti[2]              = FromFlightCtrl.Param.Byte[6];
405
                                FC.Poti[2]              = FromFlightCtrl.Param.Byte[6];
399
                                FC.Poti[3]              = FromFlightCtrl.Param.Byte[7];
406
                                FC.Poti[3]              = FromFlightCtrl.Param.Byte[7];
400
                                FC.Poti[4]              = FromFlightCtrl.Param.Byte[8];
407
                                FC.Poti[4]              = FromFlightCtrl.Param.Byte[8];
401
                                FC.Poti[5]              = FromFlightCtrl.Param.Byte[9];
408
                                FC.Poti[5]              = FromFlightCtrl.Param.Byte[9];
402
                                FC.Poti[6]              = FromFlightCtrl.Param.Byte[10];
409
                                FC.Poti[6]              = FromFlightCtrl.Param.Byte[10];
403
                                FC.Poti[7]              = FromFlightCtrl.Param.Byte[11];
410
                                FC.Poti[7]              = FromFlightCtrl.Param.Byte[11];
404
                                break;
411
                                break;
405
 
412
 
406
                        case SPI_FCCMD_MISC:
413
                        case SPI_FCCMD_MISC:
407
                                if(CompassCalState != FromFlightCtrl.Param.Byte[0])
414
                                if(CompassCalState != FromFlightCtrl.Param.Byte[0])
408
                                {       // put only new CompassCalState into queue to send via I2C
415
                                {       // put only new CompassCalState into queue to send via I2C
409
                                        CompassCalState = FromFlightCtrl.Param.Byte[0];
416
                                        CompassCalState = FromFlightCtrl.Param.Byte[0];
410
                                        Compass_SetCalState(CompassCalState);
417
                                        Compass_SetCalState(CompassCalState);
411
                                }
418
                                }
412
                                Parameter.NaviPH_LoginTime = FromFlightCtrl.Param.Byte[1];
419
                                Parameter.NaviPH_LoginTime = FromFlightCtrl.Param.Byte[1];
413
                                NaviData.Variometer = (NaviData.Variometer + 2 * (FromFlightCtrl.Param.sInt[1] - NaviData.Altimeter)) / 2; // provisorisch
420
                                NaviData.Variometer = (NaviData.Variometer + 2 * (FromFlightCtrl.Param.sInt[1] - NaviData.Altimeter)) / 2; // provisorisch
414
                                NaviData.Altimeter = FromFlightCtrl.Param.sInt[1]; // is located at byte 2 and 3
421
                                NaviData.Altimeter = FromFlightCtrl.Param.sInt[1]; // is located at byte 2 and 3
415
                                NaviData.SetpointAltitude = FromFlightCtrl.Param.sInt[2]; // is located at byte 4 and 5
422
                                NaviData.SetpointAltitude = FromFlightCtrl.Param.sInt[2]; // is located at byte 4 and 5
416
                                CHK_POTI_MM(Parameter.NaviGpsPLimit,FromFlightCtrl.Param.Byte[6],0,255);
423
                                CHK_POTI_MM(Parameter.NaviGpsPLimit,FromFlightCtrl.Param.Byte[6],0,255);
417
                                CHK_POTI_MM(Parameter.NaviGpsILimit,FromFlightCtrl.Param.Byte[7],0,255);
424
                                CHK_POTI_MM(Parameter.NaviGpsILimit,FromFlightCtrl.Param.Byte[7],0,255);
418
                                CHK_POTI_MM(Parameter.NaviGpsDLimit,FromFlightCtrl.Param.Byte[8],0,255);
425
                                CHK_POTI_MM(Parameter.NaviGpsDLimit,FromFlightCtrl.Param.Byte[8],0,255);
419
                                FC.RC_Quality   = FromFlightCtrl.Param.Byte[9];
426
                                FC.RC_Quality   = FromFlightCtrl.Param.Byte[9];
420
                                FC.RC_RSSI              = FromFlightCtrl.Param.Byte[10];
427
                                FC.RC_RSSI              = FromFlightCtrl.Param.Byte[10];
421
                                NaviData.RC_Quality = FC.RC_Quality;
428
                                NaviData.RC_Quality = FC.RC_Quality;
422
                                NaviData.RC_RSSI = FC.RC_RSSI;
429
                                NaviData.RC_RSSI = FC.RC_RSSI;
423
                                NaviData.Gas    = (FC.BAT_Voltage * (u32) FromFlightCtrl.Param.Byte[11]) / (u32) Parameter.LowVoltageWarning;
430
                                NaviData.Gas    = (FC.BAT_Voltage * (u32) FromFlightCtrl.Param.Byte[11]) / (u32) Parameter.LowVoltageWarning;
424
                                break;
431
                                break;
425
 
432
 
426
                        case SPI_FCCMD_SERVOS:
433
                        case SPI_FCCMD_SERVOS:
427
                                ServoParams.Refresh             = FromFlightCtrl.Param.Byte[0];
434
                                ServoParams.Refresh             = FromFlightCtrl.Param.Byte[0];
428
                                ServoParams.CompInvert  = FromFlightCtrl.Param.Byte[1];
435
                                ServoParams.CompInvert  = FromFlightCtrl.Param.Byte[1];
429
                                ServoParams.NickControl = FromFlightCtrl.Param.Byte[2];
436
                                ServoParams.NickControl = FromFlightCtrl.Param.Byte[2];
430
                                ServoParams.NickComp    = FromFlightCtrl.Param.Byte[3];
437
                                ServoParams.NickComp    = FromFlightCtrl.Param.Byte[3];
431
                                ServoParams.NickMin             = FromFlightCtrl.Param.Byte[4];
438
                                ServoParams.NickMin             = FromFlightCtrl.Param.Byte[4];
432
                                ServoParams.NickMax             = FromFlightCtrl.Param.Byte[5];
439
                                ServoParams.NickMax             = FromFlightCtrl.Param.Byte[5];
433
                                ServoParams.RollControl = FromFlightCtrl.Param.Byte[6];
440
                                ServoParams.RollControl = FromFlightCtrl.Param.Byte[6];
434
                                ServoParams.RollComp    = FromFlightCtrl.Param.Byte[7];
441
                                ServoParams.RollComp    = FromFlightCtrl.Param.Byte[7];
435
                                ServoParams.RollMin             = FromFlightCtrl.Param.Byte[8];
442
                                ServoParams.RollMin             = FromFlightCtrl.Param.Byte[8];
436
                                ServoParams.RollMax             = FromFlightCtrl.Param.Byte[9];
443
                                ServoParams.RollMax             = FromFlightCtrl.Param.Byte[9];
437
                                break;
444
                                break;
438
 
445
 
439
                        case SPI_FCCMD_VERSION:
446
                        case SPI_FCCMD_VERSION:
440
                                FC_Version.Major                = FromFlightCtrl.Param.Byte[0];
447
                                FC_Version.Major                = FromFlightCtrl.Param.Byte[0];
441
                                FC_Version.Minor                = FromFlightCtrl.Param.Byte[1];
448
                                FC_Version.Minor                = FromFlightCtrl.Param.Byte[1];
442
                                FC_Version.Patch                = FromFlightCtrl.Param.Byte[2];
449
                                FC_Version.Patch                = FromFlightCtrl.Param.Byte[2];
443
                                FC_Version.Compatible   = FromFlightCtrl.Param.Byte[3];
450
                                FC_Version.Compatible   = FromFlightCtrl.Param.Byte[3];
444
                                FC_Version.Hardware             = FromFlightCtrl.Param.Byte[4];
451
                                FC_Version.Hardware             = FromFlightCtrl.Param.Byte[4];
445
                                FC.Error[0]                     = FromFlightCtrl.Param.Byte[5];
452
                                FC.Error[0]                     = FromFlightCtrl.Param.Byte[5];
446
                                FC.Error[1]                     = FromFlightCtrl.Param.Byte[6];
453
                                FC.Error[1]                     = FromFlightCtrl.Param.Byte[6];
447
                                FC.Error[2]                     = FromFlightCtrl.Param.Byte[7];
454
                                FC.Error[2]                     = FromFlightCtrl.Param.Byte[7];
448
                                FC.Error[3]                     = FromFlightCtrl.Param.Byte[8];
455
                                FC.Error[3]                     = FromFlightCtrl.Param.Byte[8];
449
                                FC.Error[4]                     = FromFlightCtrl.Param.Byte[9];
456
                                FC.Error[4]                     = FromFlightCtrl.Param.Byte[9];
450
                                DebugOut.Status[0] |= 0x01; // status of FC Present
457
                                DebugOut.Status[0] |= 0x01; // status of FC Present
451
                                DebugOut.Status[0] |= 0x02; // status of BL Present
458
                                DebugOut.Status[0] |= 0x02; // status of BL Present
452
                                if(FC.Error[0] || FC.Error[1] || FC.Error[2] || FC.Error[3] || FC.Error[4]) DebugOut.Status[1] |= 0x01;
459
                                if(FC.Error[0] || FC.Error[1] || FC.Error[2] || FC.Error[3] || FC.Error[4]) DebugOut.Status[1] |= 0x01;
453
                                else DebugOut.Status[1] &= ~0x01;
460
                                else DebugOut.Status[1] &= ~0x01;
454
                                break;
461
                                break;
455
                        default:
462
                        default:
456
                                break;
463
                                break;
457
                }
464
                }
458
 
465
 
459
                // every time we got new data from the FC via SPI call the navigation routine
466
                // every time we got new data from the FC via SPI call the navigation routine
460
                // and update GPSStick that are returned to FC
467
                // and update GPSStick that are returned to FC
461
                GPS_Navigation(&GPSData, &(ToFlightCtrl.GPSStick));
468
                GPS_Navigation(&GPSData, &(ToFlightCtrl.GPSStick));
462
                ClearFCStatusFlags = 1;
469
                ClearFCStatusFlags = 1;
463
 
470
 
464
                if(counter)
471
                if(counter)
465
                {
472
                {
466
                        counter--;                                       // count down to enable servo
473
                        counter--;                                       // count down to enable servo
467
                        if(!counter) TIMER2_Init();  // enable Servo Output
474
                        if(!counter) TIMER2_Init();  // enable Servo Output
468
                }
475
                }
469
 
476
 
470
                SPI_RxBuffer_Request = 0;
477
                SPI_RxBuffer_Request = 0;
471
                timeout = SetDelay(80); // 80 ms, new data are send every 20 ms
478
                timeout = SetDelay(80); // 80 ms, new data are send every 20 ms
472
 
479
 
473
                DebugOut.Analog[0] = FromFlightCtrl.AngleNick;
480
                DebugOut.Analog[0] = FromFlightCtrl.AngleNick;
474
                DebugOut.Analog[1] = FromFlightCtrl.AngleRoll;
481
                DebugOut.Analog[1] = FromFlightCtrl.AngleRoll;
475
                DebugOut.Analog[2] = FromFlightCtrl.AccNick;
482
                DebugOut.Analog[2] = FromFlightCtrl.AccNick;
476
                DebugOut.Analog[3] = FromFlightCtrl.AccRoll;
483
                DebugOut.Analog[3] = FromFlightCtrl.AccRoll;
477
                DebugOut.Analog[11] = FromFlightCtrl.GyroHeading/10;// in deg
484
                DebugOut.Analog[11] = FromFlightCtrl.GyroHeading/10;// in deg
478
                Data3D.AngleNick = FromFlightCtrl.AngleNick;            // in 0.1 deg
485
                Data3D.AngleNick = FromFlightCtrl.AngleNick;            // in 0.1 deg
479
                Data3D.AngleRoll = FromFlightCtrl.AngleRoll;            // in 0.1 deg
486
                Data3D.AngleRoll = FromFlightCtrl.AngleRoll;            // in 0.1 deg
480
                Data3D.Heading   = FromFlightCtrl.GyroHeading;          // in 0.1 deg
487
                Data3D.Heading   = FromFlightCtrl.GyroHeading;          // in 0.1 deg
481
        }       // EOF if(SPI_RxBuffer_Request)
488
        }       // EOF if(SPI_RxBuffer_Request)
482
        else // no new SPI data
489
        else // no new SPI data
483
        {
490
        {
484
                if(CheckDelay(timeout) && (counter == 0))
491
                if(CheckDelay(timeout) && (counter == 0))
485
                {
492
                {
486
                        TIMER2_Deinit();  // disable Servo Output
493
                        TIMER2_Deinit();  // disable Servo Output
487
                        counter = 50;     // reset counter for enabling Servo Output
494
                        counter = 50;     // reset counter for enabling Servo Output
488
                }
495
                }
489
        }
496
        }
490
}
497
}
491
 
498
 
492
//------------------------------------------------------
499
//------------------------------------------------------
493
void SPI0_GetFlightCtrlVersion(void)
500
void SPI0_GetFlightCtrlVersion(void)
494
{
501
{
495
        u32 timeout;
502
        u32 timeout;
496
        u8 repeat;
503
        u8 repeat;
497
        u8 msg[64];
504
        u8 msg[64];
498
 
505
 
499
        UART1_PutString("\r\n Looking for FlightContol");
506
        UART1_PutString("\r\n Looking for FlightContol");
500
        FC_Version.Major = 0xFF;
507
        FC_Version.Major = 0xFF;
501
        FC_Version.Minor = 0xFF;
508
        FC_Version.Minor = 0xFF;
502
        FC_Version.Patch = 0xFF;
509
        FC_Version.Patch = 0xFF;
503
        FC_Version.Compatible = 0xFF;
510
        FC_Version.Compatible = 0xFF;
504
 
511
 
505
        // polling FC version info
512
        // polling FC version info
506
        repeat = 0;
513
        repeat = 0;
507
        do
514
        do
508
        {
515
        {
509
                timeout = SetDelay(250);
516
                timeout = SetDelay(250);
510
                do
517
                do
511
                {
518
                {
512
                        SPI0_UpdateBuffer();
519
                        SPI0_UpdateBuffer();
513
                        if (FC_Version.Major != 0xFF)  break;
520
                        if (FC_Version.Major != 0xFF)  break;
514
                }while (!CheckDelay(timeout));
521
                }while (!CheckDelay(timeout));
515
                UART1_PutString(".");
522
                UART1_PutString(".");
516
                repeat++;
523
                repeat++;
517
        }while((FC_Version.Major == 0xFF) && (repeat < 40)); // 40*250ms = 10s
524
        }while((FC_Version.Major == 0xFF) && (repeat < 40)); // 40*250ms = 10s
518
        // if we got it
525
        // if we got it
519
        if (FC_Version.Major != 0xFF)
526
        if (FC_Version.Major != 0xFF)
520
        {
527
        {
521
                sprintf(msg, " FC 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);
528
                sprintf(msg, " FC 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);
522
                UART1_PutString(msg);
529
                UART1_PutString(msg);
523
        }
530
        }
524
        else UART1_PutString("\n\r not found!");
531
        else UART1_PutString("\n\r not found!");
525
}
532
}
526
 
533
 
527
 
534
 
528
 
535