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