Subversion Repositories NaviCtrl

Rev

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

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