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