Subversion Repositories NaviCtrl

Rev

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

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