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