Rev 70 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 70 | Rev 171 | ||
---|---|---|---|
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 / 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 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 this software (or part of it) to systems (other than 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 "i2c.h" |
64 | #include "i2c.h" |
65 | #include "timer.h" |
65 | #include "timer.h" |
66 | #include "main.h" |
66 | #include "main.h" |
67 | #include "fifo.h" |
67 | #include "fifo.h" |
68 | 68 | ||
69 | 69 | ||
70 | #define SPI_RXSYNCBYTE1 0xAA |
70 | #define SPI_RXSYNCBYTE1 0xAA |
71 | #define SPI_RXSYNCBYTE2 0x83 |
71 | #define SPI_RXSYNCBYTE2 0x83 |
72 | #define SPI_TXSYNCBYTE1 0x81 |
72 | #define SPI_TXSYNCBYTE1 0x81 |
73 | #define SPI_TXSYNCBYTE2 0x55 |
73 | #define SPI_TXSYNCBYTE2 0x55 |
74 | 74 | ||
75 | typedef enum |
75 | typedef enum |
76 | { |
76 | { |
77 | SPI_SYNC1, |
77 | SPI_SYNC1, |
78 | SPI_SYNC2, |
78 | SPI_SYNC2, |
79 | SPI_DATA |
79 | SPI_DATA |
80 | } SPI_State_t; |
80 | } SPI_State_t; |
81 | 81 | ||
82 | //communication packets |
82 | //communication packets |
83 | volatile FromFlightCtrl_t FromFlightCtrl; |
83 | volatile FromFlightCtrl_t FromFlightCtrl; |
84 | volatile ToFlightCtrl_t ToFlightCtrl; |
84 | volatile ToFlightCtrl_t ToFlightCtrl; |
85 | 85 | ||
86 | // tx packet buffer |
86 | // tx packet buffer |
87 | #define SPI_TXBUFFER_LEN (2 + sizeof(ToFlightCtrl)) // 2 bytes at start are for synchronization |
87 | #define SPI_TXBUFFER_LEN (2 + sizeof(ToFlightCtrl)) // 2 bytes at start are for synchronization |
88 | volatile u8 SPI_TxBuffer[SPI_TXBUFFER_LEN]; |
88 | volatile u8 SPI_TxBuffer[SPI_TXBUFFER_LEN]; |
89 | volatile u8 SPI_TxBufferIndex = 0; |
89 | volatile u8 SPI_TxBufferIndex = 0; |
90 | u8 *Ptr_TxChksum = NULL ; // pointer to checksum in TxBuffer |
90 | u8 *Ptr_TxChksum = NULL ; // pointer to checksum in TxBuffer |
91 | 91 | ||
92 | // rx packet buffer |
92 | // rx packet buffer |
93 | #define SPI_RXBUFFER_LEN sizeof(FromFlightCtrl) |
93 | #define SPI_RXBUFFER_LEN sizeof(FromFlightCtrl) |
94 | volatile u8 SPI_RxBuffer[SPI_RXBUFFER_LEN]; |
94 | volatile u8 SPI_RxBuffer[SPI_RXBUFFER_LEN]; |
95 | volatile u8 SPI_RxBufferIndex = 0; |
95 | volatile u8 SPI_RxBufferIndex = 0; |
96 | volatile u8 SPI_RxBuffer_Request = 0; |
96 | volatile u8 SPI_RxBuffer_Request = 0; |
97 | #define SPI_COMMAND_INDEX 0 |
97 | #define SPI_COMMAND_INDEX 0 |
98 | 98 | ||
99 | s32 FC_Kalman_K = 32; |
99 | s32 FC_Kalman_K = 32; |
100 | s32 Kalman_MaxDrift = 5 * 16; |
100 | s32 Kalman_MaxDrift = 5 * 16; |
101 | s32 Kalman_MaxFusion = 64; |
101 | s32 Kalman_MaxFusion = 64; |
102 | u32 CheckSPIOkay = 0; |
102 | u32 CheckSPIOkay = 0; |
103 | 103 | ||
104 | u8 SPI_CommandSequence[] = { SPI_KALMAN };//, SPI_CMD_OSD_DATA, SPI_CMD_GPS_POS, SPI_KALMAN, SPI_CMD_GPS_TARGET}; |
104 | u8 SPI_CommandSequence[] = { SPI_KALMAN };//, SPI_CMD_OSD_DATA, SPI_CMD_GPS_POS, SPI_KALMAN, SPI_CMD_GPS_TARGET}; |
105 | u8 SPI_CommandCounter = 0; |
105 | u8 SPI_CommandCounter = 0; |
106 | 106 | ||
107 | SPI_Version_t FC_Version; |
107 | SPI_Version_t FC_Version; |
108 | 108 | ||
109 | u8 CompassCalStateQueue[10]; |
109 | u8 CompassCalStateQueue[10]; |
110 | fifo_t CompassCalcStateFiFo; |
110 | fifo_t CompassCalcStateFiFo; |
111 | 111 | ||
112 | u8 CompassCalState = 0; |
112 | u8 CompassCalState = 0; |
113 | 113 | ||
114 | //-------------------------------------------------------------- |
114 | //-------------------------------------------------------------- |
115 | void SSP0_IRQHandler(void) |
115 | void SSP0_IRQHandler(void) |
116 | { |
116 | { |
117 | static u8 rxchksum = 0; |
117 | static u8 rxchksum = 0; |
118 | u8 rxdata; |
118 | u8 rxdata; |
119 | static SPI_State_t SPI_State = SPI_SYNC1; |
119 | static SPI_State_t SPI_State = SPI_SYNC1; |
120 | 120 | ||
121 | // clear pending bit |
121 | // clear pending bit |
122 | SSP_ClearITPendingBit(SSP0, SSP_IT_RxTimeOut); |
122 | SSP_ClearITPendingBit(SSP0, SSP_IT_RxTimeOut); |
123 | // Fill TxFIFO while its not full or end of packet is reached |
123 | // Fill TxFIFO while its not full or end of packet is reached |
124 | while (SSP_GetFlagStatus(SSP0, SSP_FLAG_TxFifoNotFull) == SET) |
124 | while (SSP_GetFlagStatus(SSP0, SSP_FLAG_TxFifoNotFull) == SET) |
125 | { |
125 | { |
126 | if (SPI_TxBufferIndex < SPI_TXBUFFER_LEN) // still data to send ? |
126 | if (SPI_TxBufferIndex < SPI_TXBUFFER_LEN) // still data to send ? |
127 | { |
127 | { |
128 | SSP0->DR = SPI_TxBuffer[SPI_TxBufferIndex]; // send a byte |
128 | SSP0->DR = SPI_TxBuffer[SPI_TxBufferIndex]; // send a byte |
129 | *Ptr_TxChksum += SPI_TxBuffer[SPI_TxBufferIndex]; // update checksum |
129 | *Ptr_TxChksum += SPI_TxBuffer[SPI_TxBufferIndex]; // update checksum |
130 | SPI_TxBufferIndex++; // pointer to next byte |
130 | SPI_TxBufferIndex++; // pointer to next byte |
131 | } |
131 | } |
132 | else // TxBuffer end is reached then reset and copy data to tx buffer |
132 | else // TxBuffer end is reached then reset and copy data to tx buffer |
133 | { |
133 | { |
134 | SPI_TxBufferIndex = 0; // reset buffer index |
134 | SPI_TxBufferIndex = 0; // reset buffer index |
135 | ToFlightCtrl.Chksum = 0; // initialize checksum |
135 | ToFlightCtrl.Chksum = 0; // initialize checksum |
136 | ToFlightCtrl.BeepTime = BeepTime; // set beeptime |
136 | ToFlightCtrl.BeepTime = BeepTime; // set beeptime |
137 | BeepTime = 0; // reset local beeptime |
137 | BeepTime = 0; // reset local beeptime |
138 | // copy contents of ToFlightCtrl->SPI_TxBuffer |
138 | // copy contents of ToFlightCtrl->SPI_TxBuffer |
139 | memcpy( (u8 *) &(SPI_TxBuffer[2]), (u8 *) &ToFlightCtrl, sizeof(ToFlightCtrl)); |
139 | memcpy( (u8 *) &(SPI_TxBuffer[2]), (u8 *) &ToFlightCtrl, sizeof(ToFlightCtrl)); |
140 | } |
140 | } |
141 | } |
141 | } |
142 | // while RxFIFO not empty |
142 | // while RxFIFO not empty |
143 | while (SSP_GetFlagStatus(SSP0, SSP_FLAG_RxFifoNotEmpty) == SET) |
143 | while (SSP_GetFlagStatus(SSP0, SSP_FLAG_RxFifoNotEmpty) == SET) |
144 | { |
144 | { |
145 | rxdata = SSP0->DR; // catch the received byte |
145 | rxdata = SSP0->DR; // catch the received byte |
146 | // Fill TxFIFO while its not full or end of packet is reached |
146 | // Fill TxFIFO while its not full or end of packet is reached |
147 | while (SSP_GetFlagStatus(SSP0, SSP_FLAG_TxFifoNotFull) == SET) |
147 | while (SSP_GetFlagStatus(SSP0, SSP_FLAG_TxFifoNotFull) == SET) |
148 | { |
148 | { |
149 | if (SPI_TxBufferIndex < SPI_TXBUFFER_LEN) // still data to send ? |
149 | if (SPI_TxBufferIndex < SPI_TXBUFFER_LEN) // still data to send ? |
150 | { |
150 | { |
151 | SSP0->DR = SPI_TxBuffer[SPI_TxBufferIndex]; // send a byte |
151 | SSP0->DR = SPI_TxBuffer[SPI_TxBufferIndex]; // send a byte |
152 | *Ptr_TxChksum += SPI_TxBuffer[SPI_TxBufferIndex]; // update checksum |
152 | *Ptr_TxChksum += SPI_TxBuffer[SPI_TxBufferIndex]; // update checksum |
153 | SPI_TxBufferIndex++; // pointer to next byte |
153 | SPI_TxBufferIndex++; // pointer to next byte |
154 | } |
154 | } |
155 | else // end of packet is reached reset and copy data to tx buffer |
155 | else // end of packet is reached reset and copy data to tx buffer |
156 | { |
156 | { |
157 | SPI_TxBufferIndex = 0; // reset buffer index |
157 | SPI_TxBufferIndex = 0; // reset buffer index |
158 | ToFlightCtrl.Chksum = 0; // initialize checksum |
158 | ToFlightCtrl.Chksum = 0; // initialize checksum |
159 | ToFlightCtrl.BeepTime = BeepTime; // set beeptime |
159 | ToFlightCtrl.BeepTime = BeepTime; // set beeptime |
160 | BeepTime = 0; // reset local beeptime |
160 | BeepTime = 0; // reset local beeptime |
161 | // copy contents of ToFlightCtrl->SPI_TxBuffer |
161 | // copy contents of ToFlightCtrl->SPI_TxBuffer |
162 | memcpy((u8 *) &(SPI_TxBuffer[2]), (u8 *) &ToFlightCtrl, sizeof(ToFlightCtrl)); |
162 | memcpy((u8 *) &(SPI_TxBuffer[2]), (u8 *) &ToFlightCtrl, sizeof(ToFlightCtrl)); |
163 | } |
163 | } |
164 | } |
164 | } |
165 | switch (SPI_State) |
165 | switch (SPI_State) |
166 | { |
166 | { |
167 | case SPI_SYNC1: |
167 | case SPI_SYNC1: |
168 | SPI_RxBufferIndex = 0; // reset buffer index |
168 | SPI_RxBufferIndex = 0; // reset buffer index |
169 | rxchksum = rxdata; // init checksum |
169 | rxchksum = rxdata; // init checksum |
170 | if (rxdata == SPI_RXSYNCBYTE1) |
170 | if (rxdata == SPI_RXSYNCBYTE1) |
171 | { // 1st syncbyte ok |
171 | { // 1st syncbyte ok |
172 | SPI_State = SPI_SYNC2; // step to sync2 |
172 | SPI_State = SPI_SYNC2; // step to sync2 |
173 | } |
173 | } |
174 | break; |
174 | break; |
175 | case SPI_SYNC2: |
175 | case SPI_SYNC2: |
176 | if (rxdata == SPI_RXSYNCBYTE2) |
176 | if (rxdata == SPI_RXSYNCBYTE2) |
177 | { // 2nd Syncbyte ok |
177 | { // 2nd Syncbyte ok |
178 | rxchksum += rxdata; |
178 | rxchksum += rxdata; |
179 | SPI_State = SPI_DATA; |
179 | SPI_State = SPI_DATA; |
180 | } // 2nd Syncbyte does not match |
180 | } // 2nd Syncbyte does not match |
181 | else |
181 | else |
182 | { |
182 | { |
183 | SPI_State = SPI_SYNC1; //jump back to sync1 |
183 | SPI_State = SPI_SYNC1; //jump back to sync1 |
184 | } |
184 | } |
185 | break; |
185 | break; |
186 | case SPI_DATA: |
186 | case SPI_DATA: |
187 | SPI_RxBuffer[SPI_RxBufferIndex++]= rxdata; // copy databyte to rx buffer |
187 | SPI_RxBuffer[SPI_RxBufferIndex++]= rxdata; // copy databyte to rx buffer |
188 | if (SPI_RxBufferIndex >= SPI_RXBUFFER_LEN) // end of packet is reached |
188 | if (SPI_RxBufferIndex >= SPI_RXBUFFER_LEN) // end of packet is reached |
189 | { |
189 | { |
190 | if (rxdata == rxchksum) // verify checksum byte |
190 | if (rxdata == rxchksum) // verify checksum byte |
191 | { |
191 | { |
192 | // copy SPI_RxBuffer -> FromFlightCtrl |
192 | // copy SPI_RxBuffer -> FromFlightCtrl |
193 | if(!SPI_RxBuffer_Request) // block writing to FromFlightCtrl on reading access |
193 | if(!SPI_RxBuffer_Request) // block writing to FromFlightCtrl on reading access |
194 | { |
194 | { |
195 | memcpy((u8 *) &FromFlightCtrl, (u8 *) SPI_RxBuffer, sizeof(FromFlightCtrl)); |
195 | memcpy((u8 *) &FromFlightCtrl, (u8 *) SPI_RxBuffer, sizeof(FromFlightCtrl)); |
196 | SPI_RxBuffer_Request = 1; |
196 | SPI_RxBuffer_Request = 1; |
197 | } |
197 | } |
198 | CheckSPIOkay++; |
198 | CheckSPIOkay++; |
199 | DebugOut.Analog[13]++; |
199 | DebugOut.Analog[13]++; |
200 | } |
200 | } |
201 | else // bad checksum byte |
201 | else // bad checksum byte |
202 | { |
202 | { |
203 | DebugOut.Analog[12]++; // increase SPI chksum error counter |
203 | DebugOut.Analog[12]++; // increase SPI chksum error counter |
204 | } |
204 | } |
205 | SPI_State = SPI_SYNC1; // reset state |
205 | SPI_State = SPI_SYNC1; // reset state |
206 | } |
206 | } |
207 | else // end of packet not reached |
207 | else // end of packet not reached |
208 | { |
208 | { |
209 | rxchksum += rxdata; // update checksum |
209 | rxchksum += rxdata; // update checksum |
210 | } |
210 | } |
211 | break; |
211 | break; |
212 | default: |
212 | default: |
213 | SPI_State = SPI_SYNC1; |
213 | SPI_State = SPI_SYNC1; |
214 | break; |
214 | break; |
215 | } |
215 | } |
216 | } |
216 | } |
217 | } |
217 | } |
218 | 218 | ||
219 | //-------------------------------------------------------------- |
219 | //-------------------------------------------------------------- |
220 | void SPI0_Init(void) |
220 | void SPI0_Init(void) |
221 | { |
221 | { |
222 | GPIO_InitTypeDef GPIO_InitStructure; |
222 | GPIO_InitTypeDef GPIO_InitStructure; |
223 | SSP_InitTypeDef SSP_InitStructure; |
223 | SSP_InitTypeDef SSP_InitStructure; |
224 | 224 | ||
225 | SerialPutString("\r\n SPI init..."); |
225 | SerialPutString("\r\n SPI init..."); |
226 | 226 | ||
227 | SCU_APBPeriphClockConfig(__GPIO2 ,ENABLE); |
227 | SCU_APBPeriphClockConfig(__GPIO2 ,ENABLE); |
228 | SCU_APBPeriphClockConfig(__SSP0 ,ENABLE); |
228 | SCU_APBPeriphClockConfig(__SSP0 ,ENABLE); |
229 | 229 | ||
230 | GPIO_DeInit(GPIO2); |
230 | GPIO_DeInit(GPIO2); |
231 | //SSP0_CLK, SSP0_MOSI, SSP0_NSS pins |
231 | //SSP0_CLK, SSP0_MOSI, SSP0_NSS pins |
232 | GPIO_StructInit(&GPIO_InitStructure); |
232 | GPIO_StructInit(&GPIO_InitStructure); |
233 | GPIO_InitStructure.GPIO_Direction = GPIO_PinInput; |
233 | GPIO_InitStructure.GPIO_Direction = GPIO_PinInput; |
234 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_7; |
234 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_7; |
235 | GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ; |
235 | GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ; |
236 | GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Enable; |
236 | GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Enable; |
237 | GPIO_InitStructure.GPIO_Alternate = GPIO_InputAlt1; //SSP0_SCLK, SSP0_MOSI, SSP0_NSS |
237 | GPIO_InitStructure.GPIO_Alternate = GPIO_InputAlt1; //SSP0_SCLK, SSP0_MOSI, SSP0_NSS |
238 | GPIO_Init (GPIO2, &GPIO_InitStructure); |
238 | GPIO_Init (GPIO2, &GPIO_InitStructure); |
239 | 239 | ||
240 | // SSP0_MISO pin GPIO2.6 |
240 | // SSP0_MISO pin GPIO2.6 |
241 | GPIO_StructInit(&GPIO_InitStructure); |
241 | GPIO_StructInit(&GPIO_InitStructure); |
242 | GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput; |
242 | GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput; |
243 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; |
243 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; |
244 | GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ; |
244 | GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ; |
245 | GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Enable; |
245 | GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Enable; |
246 | GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt2; //SSP0_MISO |
246 | GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt2; //SSP0_MISO |
247 | GPIO_Init (GPIO2, &GPIO_InitStructure); |
247 | GPIO_Init (GPIO2, &GPIO_InitStructure); |
248 | 248 | ||
249 | SSP_DeInit(SSP0); |
249 | SSP_DeInit(SSP0); |
250 | SSP_StructInit(&SSP_InitStructure); |
250 | SSP_StructInit(&SSP_InitStructure); |
251 | SSP_InitStructure.SSP_FrameFormat = SSP_FrameFormat_Motorola; |
251 | SSP_InitStructure.SSP_FrameFormat = SSP_FrameFormat_Motorola; |
252 | SSP_InitStructure.SSP_Mode = SSP_Mode_Slave; |
252 | SSP_InitStructure.SSP_Mode = SSP_Mode_Slave; |
253 | SSP_InitStructure.SSP_SlaveOutput = SSP_SlaveOutput_Enable; |
253 | SSP_InitStructure.SSP_SlaveOutput = SSP_SlaveOutput_Enable; |
254 | SSP_InitStructure.SSP_CPHA = SSP_CPHA_1Edge; |
254 | SSP_InitStructure.SSP_CPHA = SSP_CPHA_1Edge; |
255 | SSP_InitStructure.SSP_CPOL = SSP_CPOL_Low; |
255 | SSP_InitStructure.SSP_CPOL = SSP_CPOL_Low; |
256 | SSP_InitStructure.SSP_ClockRate = 0; |
256 | SSP_InitStructure.SSP_ClockRate = 0; |
257 | 257 | ||
258 | SSP_Init(SSP0, &SSP_InitStructure); |
258 | SSP_Init(SSP0, &SSP_InitStructure); |
259 | 259 | ||
260 | SSP_ITConfig(SSP0, SSP_IT_RxFifo | SSP_IT_TxFifo | SSP_IT_RxTimeOut, ENABLE); |
260 | SSP_ITConfig(SSP0, SSP_IT_RxFifo | SSP_IT_TxFifo | SSP_IT_RxTimeOut, ENABLE); |
261 | 261 | ||
262 | fifo_init(&CompassCalcStateFiFo, CompassCalStateQueue, sizeof(CompassCalStateQueue)); |
262 | fifo_init(&CompassCalcStateFiFo, CompassCalStateQueue, sizeof(CompassCalStateQueue)); |
263 | 263 | ||
264 | SSP_Cmd(SSP0, ENABLE); |
264 | SSP_Cmd(SSP0, ENABLE); |
265 | // initialize the syncbytes in the tx buffer |
265 | // initialize the syncbytes in the tx buffer |
266 | SPI_TxBuffer[0] = SPI_TXSYNCBYTE1; |
266 | SPI_TxBuffer[0] = SPI_TXSYNCBYTE1; |
267 | SPI_TxBuffer[1] = SPI_TXSYNCBYTE2; |
267 | SPI_TxBuffer[1] = SPI_TXSYNCBYTE2; |
268 | // set the pointer to the checksum byte in the tx buffer |
268 | // set the pointer to the checksum byte in the tx buffer |
269 | Ptr_TxChksum = (u8 *) &(((ToFlightCtrl_t *) &(SPI_TxBuffer[2]))->Chksum); |
269 | Ptr_TxChksum = (u8 *) &(((ToFlightCtrl_t *) &(SPI_TxBuffer[2]))->Chksum); |
270 | 270 | ||
271 | VIC_Config(SSP0_ITLine, VIC_IRQ, 1); |
271 | VIC_Config(SSP0_ITLine, VIC_IRQ, 1); |
272 | VIC_ITCmd(SSP0_ITLine, ENABLE); |
272 | VIC_ITCmd(SSP0_ITLine, ENABLE); |
273 | 273 | ||
274 | SerialPutString("ok"); |
274 | SerialPutString("ok"); |
275 | } |
275 | } |
276 | 276 | ||
277 | //------------------------------------------------------ |
277 | //------------------------------------------------------ |
278 | void SPI0_UpdateBuffer(void) |
278 | void SPI0_UpdateBuffer(void) |
279 | { |
279 | { |
280 | if (SPI_RxBuffer_Request) |
280 | if (SPI_RxBuffer_Request) |
281 | { |
281 | { |
282 | // avoid sending data via SPI during the update of the ToFlightCtrl structure |
282 | // avoid sending data via SPI during the update of the ToFlightCtrl structure |
283 | VIC_ITCmd(SSP0_ITLine, DISABLE); // disable SPI interrupt |
283 | VIC_ITCmd(SSP0_ITLine, DISABLE); // disable SPI interrupt |
284 | 284 | ||
285 | ToFlightCtrl.CompassHeading = I2C_Heading.Heading; |
285 | ToFlightCtrl.CompassHeading = I2C_Heading.Heading; |
286 | ToFlightCtrl.GPS_Nick = GPS_Stick.Nick; |
286 | ToFlightCtrl.GPS_Nick = GPS_Stick.Nick; |
287 | ToFlightCtrl.GPS_Roll = GPS_Stick.Roll; |
287 | ToFlightCtrl.GPS_Roll = GPS_Stick.Roll; |
288 | ToFlightCtrl.GPS_Yaw = GPS_Stick.Yaw; |
288 | ToFlightCtrl.GPS_Yaw = GPS_Stick.Yaw; |
289 | // cycle spi commands |
289 | // cycle spi commands |
290 | ToFlightCtrl.Command = SPI_CommandSequence[SPI_CommandCounter++]; |
290 | ToFlightCtrl.Command = SPI_CommandSequence[SPI_CommandCounter++]; |
291 | if (SPI_CommandCounter >= sizeof(SPI_CommandSequence)) SPI_CommandCounter = 0; |
291 | if (SPI_CommandCounter >= sizeof(SPI_CommandSequence)) SPI_CommandCounter = 0; |
292 | 292 | ||
293 | switch (ToFlightCtrl.Command) |
293 | switch (ToFlightCtrl.Command) |
294 | { |
294 | { |
295 | case SPI_KALMAN: |
295 | case SPI_KALMAN: |
296 | ToFlightCtrl.Param.Byte[0] = (char) FC_Kalman_K; |
296 | ToFlightCtrl.Param.Byte[0] = (char) FC_Kalman_K; |
297 | ToFlightCtrl.Param.Byte[1] = (char) Kalman_MaxFusion; |
297 | ToFlightCtrl.Param.Byte[1] = (char) Kalman_MaxFusion; |
298 | ToFlightCtrl.Param.Byte[2] = (char) Kalman_MaxDrift; |
298 | ToFlightCtrl.Param.Byte[2] = (char) Kalman_MaxDrift; |
299 | break; |
299 | break; |
300 | 300 | ||
301 | default: |
301 | default: |
302 | break; |
302 | break; |
303 | } |
303 | } |
304 | VIC_ITCmd(SSP0_ITLine, ENABLE); // enable SPI interrupt |
304 | VIC_ITCmd(SSP0_ITLine, ENABLE); // enable SPI interrupt |
305 | 305 | ||
306 | 306 | ||
307 | switch(FromFlightCtrl.Command) |
307 | switch(FromFlightCtrl.Command) |
308 | { |
308 | { |
309 | case SPI_CMD_USER: |
309 | case SPI_CMD_USER: |
310 | Parameter.User1 = FromFlightCtrl.Param.Byte[0]; |
310 | Parameter.User1 = FromFlightCtrl.Param.Byte[0]; |
311 | Parameter.User2 = FromFlightCtrl.Param.Byte[1]; |
311 | Parameter.User2 = FromFlightCtrl.Param.Byte[1]; |
312 | Parameter.User3 = FromFlightCtrl.Param.Byte[2]; |
312 | Parameter.User3 = FromFlightCtrl.Param.Byte[2]; |
313 | Parameter.User4 = FromFlightCtrl.Param.Byte[3]; |
313 | Parameter.User4 = FromFlightCtrl.Param.Byte[3]; |
314 | Parameter.User5 = FromFlightCtrl.Param.Byte[4]; |
314 | Parameter.User5 = FromFlightCtrl.Param.Byte[4]; |
315 | Parameter.User6 = FromFlightCtrl.Param.Byte[5]; |
315 | Parameter.User6 = FromFlightCtrl.Param.Byte[5]; |
316 | Parameter.User7 = FromFlightCtrl.Param.Byte[6]; |
316 | Parameter.User7 = FromFlightCtrl.Param.Byte[6]; |
317 | Parameter.User8 = FromFlightCtrl.Param.Byte[7]; |
317 | Parameter.User8 = FromFlightCtrl.Param.Byte[7]; |
318 | if(ClearMKFlags) |
318 | if(ClearMKFlags) |
319 | { |
319 | { |
320 | FC.MKFlags = 0; |
320 | FC.MKFlags = 0; |
321 | ClearMKFlags = 0; |
321 | ClearMKFlags = 0; |
322 | } |
322 | } |
323 | FC.MKFlags |= FromFlightCtrl.Param.Byte[8]; |
323 | FC.MKFlags |= FromFlightCtrl.Param.Byte[8]; |
324 | FC.UBat = FromFlightCtrl.Param.Byte[9]; |
324 | FC.UBat = FromFlightCtrl.Param.Byte[9]; |
325 | Parameter.LowVoltageWarning = FromFlightCtrl.Param.Byte[10]; |
325 | Parameter.LowVoltageWarning = FromFlightCtrl.Param.Byte[10]; |
326 | Parameter.ActiveSetting = FromFlightCtrl.Param.Byte[11]; |
326 | Parameter.ActiveSetting = FromFlightCtrl.Param.Byte[11]; |
327 | break; |
327 | break; |
328 | 328 | ||
329 | #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;} |
329 | #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;} |
330 | case SPI_CMD_PARAMETER1: |
330 | case SPI_CMD_PARAMETER1: |
331 | CHK_POTI_MM(Parameter.NaviGpsModeControl,FromFlightCtrl.Param.Byte[0],0,255); |
331 | CHK_POTI_MM(Parameter.NaviGpsModeControl,FromFlightCtrl.Param.Byte[0],0,255); |
332 | CHK_POTI_MM(Parameter.NaviGpsGain,FromFlightCtrl.Param.Byte[1],0,255); |
332 | CHK_POTI_MM(Parameter.NaviGpsGain,FromFlightCtrl.Param.Byte[1],0,255); |
333 | CHK_POTI_MM(Parameter.NaviGpsP,FromFlightCtrl.Param.Byte[2],0,255); |
333 | CHK_POTI_MM(Parameter.NaviGpsP,FromFlightCtrl.Param.Byte[2],0,255); |
334 | CHK_POTI_MM(Parameter.NaviGpsI,FromFlightCtrl.Param.Byte[3],0,255); |
334 | CHK_POTI_MM(Parameter.NaviGpsI,FromFlightCtrl.Param.Byte[3],0,255); |
335 | CHK_POTI_MM(Parameter.NaviGpsD,FromFlightCtrl.Param.Byte[4],0,255); |
335 | CHK_POTI_MM(Parameter.NaviGpsD,FromFlightCtrl.Param.Byte[4],0,255); |
336 | CHK_POTI_MM(Parameter.NaviGpsACC,FromFlightCtrl.Param.Byte[5],0,255); |
336 | CHK_POTI_MM(Parameter.NaviGpsACC,FromFlightCtrl.Param.Byte[5],0,255); |
337 | Parameter.NaviGpsMinSat = FromFlightCtrl.Param.Byte[6]; |
337 | Parameter.NaviGpsMinSat = FromFlightCtrl.Param.Byte[6]; |
338 | Parameter.NaviStickThreshold = FromFlightCtrl.Param.Byte[7]; |
338 | Parameter.NaviStickThreshold = FromFlightCtrl.Param.Byte[7]; |
339 | CHK_POTI_MM(Parameter.NaviOperatingRadius,FromFlightCtrl.Param.Byte[8],10,255); |
339 | CHK_POTI_MM(Parameter.NaviOperatingRadius,FromFlightCtrl.Param.Byte[8],10,255); |
340 | CHK_POTI_MM(Parameter.NaviWindCorrection,FromFlightCtrl.Param.Byte[9],0,255); |
340 | CHK_POTI_MM(Parameter.NaviWindCorrection,FromFlightCtrl.Param.Byte[9],0,255); |
341 | CHK_POTI_MM(Parameter.NaviSpeedCompensation,FromFlightCtrl.Param.Byte[10],0,255); |
341 | CHK_POTI_MM(Parameter.NaviSpeedCompensation,FromFlightCtrl.Param.Byte[10],0,255); |
342 | CHK_POTI_MM(Parameter.NaviAngleLimitation,FromFlightCtrl.Param.Byte[11],0,255); |
342 | CHK_POTI_MM(Parameter.NaviAngleLimitation,FromFlightCtrl.Param.Byte[11],0,255); |
343 | // DebugOut.Analog[4] = Parameter.NaviRadiusAlert; |
343 | // DebugOut.Analog[4] = Parameter.NaviRadiusAlert; |
344 | // DebugOut.Analog[5] = Parameter.NaviWindCorrection; |
344 | // DebugOut.Analog[5] = Parameter.NaviWindCorrection; |
345 | // DebugOut.Analog[6] = Parameter.NaviSpeedCompensation; |
345 | // DebugOut.Analog[6] = Parameter.NaviSpeedCompensation; |
346 | break; |
346 | break; |
347 | 347 | ||
348 | case SPI_CMD_STICK: |
348 | case SPI_CMD_STICK: |
349 | FC.StickGas = FromFlightCtrl.Param.sByte[0]; |
349 | FC.StickGas = FromFlightCtrl.Param.sByte[0]; |
350 | FC.StickYaw = FromFlightCtrl.Param.sByte[1]; |
350 | FC.StickYaw = FromFlightCtrl.Param.sByte[1]; |
351 | FC.StickRoll = FromFlightCtrl.Param.sByte[2]; |
351 | FC.StickRoll = FromFlightCtrl.Param.sByte[2]; |
352 | FC.StickNick = FromFlightCtrl.Param.sByte[3]; |
352 | FC.StickNick = FromFlightCtrl.Param.sByte[3]; |
353 | FC.Poti1 = FromFlightCtrl.Param.Byte[4]; |
353 | FC.Poti1 = FromFlightCtrl.Param.Byte[4]; |
354 | FC.Poti2 = FromFlightCtrl.Param.Byte[5]; |
354 | FC.Poti2 = FromFlightCtrl.Param.Byte[5]; |
355 | FC.Poti3 = FromFlightCtrl.Param.Byte[6]; |
355 | FC.Poti3 = FromFlightCtrl.Param.Byte[6]; |
356 | FC.Poti4 = FromFlightCtrl.Param.Byte[7]; |
356 | FC.Poti4 = FromFlightCtrl.Param.Byte[7]; |
357 | FC.RC_Quality = FromFlightCtrl.Param.Byte[8]; |
357 | FC.RC_Quality = FromFlightCtrl.Param.Byte[8]; |
358 | break; |
358 | break; |
359 | 359 | ||
360 | case SPI_CMD_MISC: |
360 | case SPI_CMD_MISC: |
361 | if(CompassCalState != FromFlightCtrl.Param.Byte[0]) |
361 | if(CompassCalState != FromFlightCtrl.Param.Byte[0]) |
362 | { // put only new CompassCalState into queue to send via I2C |
362 | { // put only new CompassCalState into queue to send via I2C |
363 | CompassCalState = FromFlightCtrl.Param.Byte[0]; |
363 | CompassCalState = FromFlightCtrl.Param.Byte[0]; |
364 | fifo_put(&CompassCalcStateFiFo, CompassCalState); |
364 | fifo_put(&CompassCalcStateFiFo, CompassCalState); |
365 | } |
365 | } |
366 | Parameter.NaviPH_LoginTime = FromFlightCtrl.Param.Byte[1]; |
366 | Parameter.NaviPH_LoginTime = FromFlightCtrl.Param.Byte[1]; |
367 | NaviData.Variometer = (NaviData.Variometer + 2 * ((s16) FromFlightCtrl.Param.Int[1] - NaviData.Altimeter)) / 2; // provisorisch |
367 | NaviData.Variometer = (NaviData.Variometer + 2 * ((s16) FromFlightCtrl.Param.Int[1] - NaviData.Altimeter)) / 2; // provisorisch |
368 | NaviData.Altimeter = (s16) FromFlightCtrl.Param.Int[1]; // is located at byte 2 and 3 |
368 | NaviData.Altimeter = (s16) FromFlightCtrl.Param.Int[1]; // is located at byte 2 and 3 |
369 | CHK_POTI_MM(Parameter.NaviGpsPLimit,FromFlightCtrl.Param.Byte[4],0,255); |
369 | CHK_POTI_MM(Parameter.NaviGpsPLimit,FromFlightCtrl.Param.Byte[4],0,255); |
370 | CHK_POTI_MM(Parameter.NaviGpsILimit,FromFlightCtrl.Param.Byte[5],0,255); |
370 | CHK_POTI_MM(Parameter.NaviGpsILimit,FromFlightCtrl.Param.Byte[5],0,255); |
371 | CHK_POTI_MM(Parameter.NaviGpsDLimit,FromFlightCtrl.Param.Byte[6],0,255); |
371 | CHK_POTI_MM(Parameter.NaviGpsDLimit,FromFlightCtrl.Param.Byte[6],0,255); |
372 | break; |
372 | break; |
373 | 373 | ||
374 | case SPI_CMD_VERSION: |
374 | case SPI_CMD_VERSION: |
375 | FC_Version.Major = FromFlightCtrl.Param.Byte[0]; |
375 | FC_Version.Major = FromFlightCtrl.Param.Byte[0]; |
376 | FC_Version.Minor = FromFlightCtrl.Param.Byte[1]; |
376 | FC_Version.Minor = FromFlightCtrl.Param.Byte[1]; |
377 | FC_Version.Patch = FromFlightCtrl.Param.Byte[2]; |
377 | FC_Version.Patch = FromFlightCtrl.Param.Byte[2]; |
378 | FC_Version.Compatible = FromFlightCtrl.Param.Byte[3]; |
378 | FC_Version.Compatible = FromFlightCtrl.Param.Byte[3]; |
379 | FC_Version.Hardware = FromFlightCtrl.Param.Byte[4]; |
379 | FC_Version.Hardware = FromFlightCtrl.Param.Byte[4]; |
380 | break; |
380 | break; |
381 | 381 | ||
382 | default: |
382 | default: |
383 | break; |
383 | break; |
384 | } |
384 | } |
385 | 385 | ||
386 | // every time we got new data from the FC via SPI call the navigation routine |
386 | // every time we got new data from the FC via SPI call the navigation routine |
387 | GPS_Navigation(); |
387 | GPS_Navigation(); |
388 | ClearMKFlags = 1; |
388 | ClearMKFlags = 1; |
389 | 389 | ||
390 | SPI_RxBuffer_Request = 0; |
390 | SPI_RxBuffer_Request = 0; |
391 | 391 | ||
392 | DebugOut.Analog[0] = FromFlightCtrl.AngleNick; |
392 | DebugOut.Analog[0] = FromFlightCtrl.AngleNick; |
393 | DebugOut.Analog[1] = FromFlightCtrl.AngleRoll; |
393 | DebugOut.Analog[1] = FromFlightCtrl.AngleRoll; |
394 | DebugOut.Analog[2] = FromFlightCtrl.AccNick; |
394 | DebugOut.Analog[2] = FromFlightCtrl.AccNick; |
395 | DebugOut.Analog[3] = FromFlightCtrl.AccRoll; |
395 | DebugOut.Analog[3] = FromFlightCtrl.AccRoll; |
396 | DebugOut.Analog[11] = FromFlightCtrl.GyroHeading/10;// in deg |
396 | DebugOut.Analog[11] = FromFlightCtrl.GyroHeading/10;// in deg |
397 | Data3D.AngleNick = FromFlightCtrl.AngleNick; // in 0.1 deg |
397 | Data3D.AngleNick = FromFlightCtrl.AngleNick; // in 0.1 deg |
398 | Data3D.AngleRoll = FromFlightCtrl.AngleRoll; // in 0.1 deg |
398 | Data3D.AngleRoll = FromFlightCtrl.AngleRoll; // in 0.1 deg |
399 | Data3D.Heading = FromFlightCtrl.GyroHeading; // in 0.1 deg |
399 | Data3D.Heading = FromFlightCtrl.GyroHeading; // in 0.1 deg |
400 | } // EOF if(SPI_RxBuffer_Request) |
400 | } // EOF if(SPI_RxBuffer_Request) |
401 | } |
401 | } |
402 | 402 | ||
403 | //------------------------------------------------------ |
403 | //------------------------------------------------------ |
404 | void SPI0_GetFlightCtrlVersion(void) |
404 | void SPI0_GetFlightCtrlVersion(void) |
405 | { |
405 | { |
406 | u16 timeout; |
406 | u16 timeout; |
407 | u8 msg[20]; |
407 | u8 msg[20]; |
408 | 408 | ||
409 | FC_Version.Major = 0xFF; |
409 | FC_Version.Major = 0xFF; |
410 | FC_Version.Minor = 0xFF; |
410 | FC_Version.Minor = 0xFF; |
411 | FC_Version.Patch = 0xFF; |
411 | FC_Version.Patch = 0xFF; |
412 | FC_Version.Compatible = 0xFF; |
412 | FC_Version.Compatible = 0xFF; |
413 | 413 | ||
414 | // polling FC version info for 2 second |
414 | // polling FC version info for 2 second |
415 | timeout = SetDelay(2000); |
415 | timeout = SetDelay(2000); |
416 | do |
416 | do |
417 | { |
417 | { |
418 | SPI0_UpdateBuffer(); |
418 | SPI0_UpdateBuffer(); |
419 | if (FC_Version.Major != 0xFF) break; |
419 | if (FC_Version.Major != 0xFF) break; |
420 | }while (!CheckDelay(timeout)); |
420 | }while (!CheckDelay(timeout)); |
421 | // if we got it |
421 | // if we got it |
422 | if (FC_Version.Major != 0xFF) |
422 | if (FC_Version.Major != 0xFF) |
423 | { |
423 | { |
424 | 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); |
424 | 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); |
425 | SerialPutString(msg); |
425 | SerialPutString(msg); |
426 | sprintf(msg, " Compatible: %d", FC_Version.Compatible); |
426 | sprintf(msg, " Compatible: %d", FC_Version.Compatible); |
427 | SerialPutString(msg); |
427 | SerialPutString(msg); |
428 | } |
428 | } |
429 | else SerialPutString("\n\r No version information from FlightCtrl."); |
429 | else SerialPutString("\n\r No version information from FlightCtrl."); |
430 | } |
430 | } |
431 | 431 | ||
432 | 432 | ||
433 | 433 |