Rev 39 | Rev 49 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 39 | Rev 41 | ||
---|---|---|---|
Line 53... | Line 53... | ||
53 | // + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
53 | // + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
54 | // + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
54 | // + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
55 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
55 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
56 | // + POSSIBILITY OF SUCH DAMAGE. |
56 | // + POSSIBILITY OF SUCH DAMAGE. |
57 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
57 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
- | 58 | //#define MCLK96MHZ |
|
58 | const unsigned long _Main_Crystal = 25000; |
59 | const unsigned long _Main_Crystal = 25000; |
59 | - | ||
- | 60 | #include <stdio.h> |
|
- | 61 | #include "91x_lib.h" |
|
- | 62 | #include "led.h" |
|
60 | #include "main.h" |
63 | #include "uart0.h" |
- | 64 | #include "uart1.h" |
|
- | 65 | #include "uart2.h" |
|
- | 66 | #include "GPS.h" |
|
- | 67 | #include "i2c.h" |
|
- | 68 | #include "timer.h" |
|
- | 69 | #include "spi_slave.h" |
|
- | 70 | #include "fat16.h" |
|
- | 71 | #include "usb.h" |
|
- | 72 | #include "sdc.h" |
|
61 | #include "kml.h" |
73 | #include "kml.h" |
- | 74 | #include "main.h" |
|
Line 62... | Line -... | ||
62 | - | ||
63 | u32 TimerCompassUpdate; |
- | |
64 | u32 TimerI2CReadDelay; |
- | |
65 | u32 TimerDebugDataDelay; |
- | |
66 | u32 KmlAddPointDelay; |
- | |
67 | - | ||
68 | u8 BeepTime; |
- | |
69 | void Init_Undef(void); |
- | |
70 | u8 Parameter_UserParam1; |
- | |
71 | u8 Parameter_UserParam2; |
- | |
72 | u8 Parameter_UserParam3; |
- | |
73 | u8 Parameter_UserParam4; |
- | |
74 | u8 Parameter_UserParam5; |
- | |
75 | u8 Parameter_UserParam6; |
- | |
76 | u8 Parameter_UserParam7; |
- | |
77 | u8 Parameter_UserParam8; |
- | |
78 | //++++++++++++++++++++++++++++++++++++++++++++++++++ |
- | |
79 | s32 FC_StickNick; |
- | |
80 | s32 FC_StickRoll; |
- | |
81 | s32 FC_StickGier; |
- | |
82 | s32 FC_StickGas; |
- | |
83 | s32 FC_Poti1; |
- | |
84 | s32 FC_Poti2; |
- | |
85 | s32 FC_Poti3; |
- | |
86 | s32 FC_Poti4; |
- | |
87 | //++++++++++++++++++++++++++++++++++++++++++++++++++ |
- | |
88 | s32 SenderOkay = 0; |
- | |
89 | s32 UBat = 0; |
- | |
90 | s32 UndervoltageLevel = 0; |
- | |
91 | s32 ActiveSetting = 1; |
- | |
92 | //++++++++++++++++++++++++++++++++++++++++++++++++++ |
- | |
93 | s32 Parameter_NaviGpsModeControl = 128; |
- | |
94 | s32 Parameter_NaviGpsGain = 0; |
- | |
95 | s32 Parameter_NaviGpsP = 90; |
- | |
96 | s32 Parameter_NaviGpsI = 0; |
- | |
97 | s32 Parameter_NaviGpsD = 90; |
- | |
98 | s32 Parameter_NaviGpsACC = 0; |
- | |
99 | s32 Parameter_NaviGpsMinSat = 3; |
- | |
100 | s32 Parameter_NaviStickThreshold = 8; |
- | |
101 | s32 Parameter_RadiusAlert = 15; |
- | |
102 | //++++++++++++++++++++++++++++++++++++++++++++++++++ |
- | |
103 | #define FLAG_MOTOR_RUN 1 |
- | |
104 | #define FLAG_FLY 2 |
- | |
105 | #define FLAG_CALIBRATE 4 |
- | |
106 | #define FLAG_START 8 |
- | |
107 | u32 MikroKopterFlags; |
- | |
108 | u32 ClearFlags = 1; |
- | |
109 | //++++++++++++++++++++++++++++++++++++++++++++++++++ |
- | |
Line 110... | Line -... | ||
110 | u8 text[20]; |
- | |
111 | 75 | ||
112 | //---------------------------------------------------------------------------------------------------- |
76 | |
- | 77 | typedef enum |
|
- | 78 | { |
|
- | 79 | LOGFILE_IDLE, |
|
- | 80 | LOGFILE_CLOSED, |
|
113 | void Leds_Init(void) |
81 | LOGFILE_OPENED, |
Line -... | Line 82... | ||
- | 82 | LOGFILE_ERROR, |
|
114 | { |
83 | } logfilestate_t; |
- | 84 | ||
- | 85 | u32 TimerCompassUpdate; |
|
- | 86 | u32 TimerKmlAddPointDelay; |
|
- | 87 | u32 TimerCheckError; |
|
- | 88 | u32 ErrorCode = 0; |
|
115 | GPIO_InitTypeDef GPIO_InitStructure; |
89 | u16 BeepTime; |
- | 90 | ||
- | 91 | u8 ClearMKFlags = 0; |
|
Line 116... | Line -... | ||
116 | - | ||
117 | SCU_APBPeriphClockConfig(__GPIO6, ENABLE); |
- | |
118 | SCU_APBPeriphClockConfig(__GPIO5, ENABLE); |
- | |
119 | - | ||
120 | //GPIO_DeInit(GPIO5); |
- | |
121 | GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput; |
- | |
122 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; |
- | |
123 | GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ; |
92 | u8 StopNavigation = 0; |
124 | GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt1 ; |
- | |
125 | GPIO_Init(GPIO5, &GPIO_InitStructure); |
- | |
126 | - | ||
127 | //SD_SWITCH |
- | |
128 | GPIO_InitStructure.GPIO_Direction = GPIO_PinInput; |
- | |
Line 129... | Line -... | ||
129 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; |
- | |
130 | GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull; |
- | |
- | 93 | Param_t Parameter; |
|
131 | GPIO_InitStructure.GPIO_Alternate = GPIO_InputAlt1; |
94 | FC_t FC; |
132 | GPIO_Init(GPIO5, &GPIO_InitStructure); |
95 | |
133 | - | ||
- | 96 | s8 ErrorMSG[25]; |
|
- | 97 | ||
- | 98 | //---------------------------------------------------------------------------------------------------- |
|
134 | } |
99 | void SCU_Config(void) |
135 | // |
100 | { |
136 | void OutputStartupData(void) |
- | |
- | 101 | /* configure PLL and set it as master clock source */ |
|
- | 102 | SCU_MCLKSourceConfig(SCU_MCLK_OSC); // set master clock source to external oscillator clock (25MHz) before diabling the PLL |
|
- | 103 | SCU_PLLCmd(DISABLE); // now disable the PLL |
|
137 | { |
104 | #ifdef MCLK96MHZ |
138 | 105 | SCU_BRCLKDivisorConfig(SCU_BRCLK_Div2); // set BRCLK to MCLK/2 = 48MHz |
|
- | 106 | SCU_PCLKDivisorConfig(SCU_PCLK_Div4); // set PCLK (APB bus clock) divisor to 4 (half Reference Clock) |
|
- | 107 | SCU_RCLKDivisorConfig(SCU_RCLK_Div2); // set RCLK (Reference Clock) divisor to 1 (full PPL clock) |
|
- | 108 | SCU_HCLKDivisorConfig(SCU_HCLK_Div2); // set HCLK (AHB bus clock) divisor to 1 (full Reference Clock) |
|
- | 109 | SCU_PLLFactorsConfig(192,25,2); // PLL = 96 MHz, Feedback Divider N=192, Pre-Divider M=25, Post-Divider P=2 |
|
- | 110 | #else |
|
139 | char tex[20]; |
111 | SCU_BRCLKDivisorConfig(SCU_BRCLK_Div1); // set BRCLK to MCLK = 48MHz |
- | 112 | SCU_PCLKDivisorConfig(SCU_PCLK_Div2); // set PCLK (APB bus clock) divisor to 2 (half Reference Clock) |
|
- | 113 | SCU_RCLKDivisorConfig(SCU_RCLK_Div1); // set RCLK (Reference Clock) divisor to 1 (full PPL clock) |
|
140 | SerialPutString("\n\r--------------\n\r"); |
114 | SCU_HCLKDivisorConfig(SCU_HCLK_Div1); // set HCLK (AHB bus clock) divisor to 1 (full Reference Clock) |
Line 141... | Line 115... | ||
141 | 115 | SCU_PLLFactorsConfig(192,25,3); // PLL = 48 MHz, Feedback Divider N=192, Pre-Divider M=25, Post-Divider P=3 |
|
142 | sprintf(text,"NaviCtrl V%d.%d\n\r", VersionInfo.Hauptversion, VersionInfo.Nebenversion); SerialPutString(text); |
116 | #endif |
143 | 117 | SCU_PLLCmd(ENABLE); // Enable PLL (is disabled by SCU_PLLFactorsConfig) |
|
144 | - | ||
145 | } |
- | |
146 | - | ||
147 | //---------------------------------------------------------------------------------------------------- |
- | |
148 | int main(void) |
- | |
149 | { |
- | |
150 | static u8 oldCompassCalState = 0; |
- | |
151 | - | ||
152 | - | ||
153 | static u8 kml_state = 0; |
118 | SCU_MCLKSourceConfig(SCU_MCLK_PLL); // set master clock source to PLL |
154 | u8 kml_cnt = 0; |
- | |
155 | KML_Document_t mydocument; |
- | |
156 | File *file1; |
- | |
157 | - | ||
158 | - | ||
159 | u8 str[80]; |
- | |
160 | - | ||
161 | - | ||
Line -... | Line 119... | ||
- | 119 | } |
|
- | 120 | ||
- | 121 | //---------------------------------------------------------------------------------------------------- |
|
Line 162... | Line -... | ||
162 | KML_DocumentInit(&mydocument); // Initialise the new kml-document for further use. |
- | |
163 | - | ||
164 | - | ||
165 | // SCU_MCLKSourceConfig(SCU_MCLK_OSC); |
- | |
166 | // SCU_PCLKDivisorConfig(SCU_PCLK_Div2); |
- | |
167 | // SCU_MCLKSourceConfig(SCU_MCLK_PLL); |
- | |
168 | - | ||
169 | - | ||
Line -... | Line 122... | ||
- | 122 | void GetNaviCtrlVersion(void) |
|
- | 123 | { |
|
- | 124 | u8 msg[25]; |
|
170 | SCU_MCLKSourceConfig(SCU_MCLK_OSC); |
125 | |
- | 126 | sprintf(msg,"\n\r NaviCtrl V%d.%d%c", VERSION_MAJOR, VERSION_MINOR, 'a'+ VERSION_PATCH); |
|
- | 127 | SerialPutString(msg); |
|
Line -... | Line 128... | ||
- | 128 | } |
|
- | 129 | ||
- | 130 | ||
- | 131 | //---------------------------------------------------------------------------------------------------- |
|
- | 132 | s8* GenerateGPSLogFileName(void) |
|
- | 133 | { |
|
171 | SCU_PLLCmd(DISABLE); // PLL aus |
134 | static u16 filenum = 0; // file name counter |
- | 135 | static s8 filename[30]; |
|
- | 136 | static DateTime_t LastTime = {0,0,0,0,0,0,0,0}; |
|
- | 137 | ||
- | 138 | if(SystemTime.Valid) |
|
- | 139 | { |
|
- | 140 | // if the day has been changed |
|
- | 141 | if((LastTime.Year != SystemTime.Year) || (LastTime.Month != SystemTime.Month) || (LastTime.Day != SystemTime.Day)) |
|
- | 142 | { |
|
- | 143 | LastTime.Year = SystemTime.Year; |
|
- | 144 | LastTime.Month = SystemTime.Month; |
|
Line -... | Line 145... | ||
- | 145 | LastTime.Day = SystemTime.Day; |
|
- | 146 | LastTime.Valid = 1; |
|
- | 147 | filenum = 0; // reset file counter |
|
- | 148 | } |
|
- | 149 | sprintf(filename, "LOG/%04i%02i%02i/GPS%05i.KML", SystemTime.Year, SystemTime.Month, SystemTime.Day, filenum); |
|
- | 150 | filenum++; |
|
172 | SCU_RCLKDivisorConfig(SCU_RCLK_Div1); |
151 | return filename; |
- | 152 | } |
|
- | 153 | else return NULL; |
|
- | 154 | } |
|
- | 155 | ||
- | 156 | ||
- | 157 | //---------------------------------------------------------------------------------------------------- |
|
173 | SCU_HCLKDivisorConfig(SCU_HCLK_Div1); |
158 | void CheckErrors(void) |
- | 159 | { |
|
- | 160 | if(!CheckSPIOkay) |
|
- | 161 | { |
|
174 | SCU_PCLKDivisorConfig(SCU_PCLK_Div2); |
162 | LED_RED_ON; |
- | 163 | sprintf(ErrorMSG,"no FC communication "); |
|
- | 164 | ErrorCode = 3; |
|
- | 165 | StopNavigation = 1; |
|
- | 166 | } |
|
- | 167 | else if(!CheckI2COkay) |
|
- | 168 | { |
|
- | 169 | LED_RED_ON; |
|
- | 170 | sprintf(ErrorMSG,"no MK3Mag communication "); |
|
- | 171 | ErrorCode = 4; |
|
- | 172 | StopNavigation = 1; |
|
- | 173 | } |
|
- | 174 | else if(FC_Version.Compatible != FC_SPI_COMPATIBLE) |
|
- | 175 | { |
|
- | 176 | LED_RED_ON; |
|
- | 177 | sprintf(ErrorMSG,"FC not compatible "); |
|
- | 178 | ErrorCode = 1; |
|
- | 179 | StopNavigation = 1; |
|
- | 180 | } |
|
- | 181 | else if(MK3MAG_Version.Compatible != MK3MAG_I2C_COMPATIBLE) |
|
- | 182 | { |
|
- | 183 | sprintf(ErrorMSG,"MK3Mag not compatible "); |
|
- | 184 | LED_RED_ON; |
|
- | 185 | ErrorCode = 2; |
|
- | 186 | StopNavigation = 1; |
|
- | 187 | } |
|
- | 188 | else if(!CheckGPSOkay) |
|
- | 189 | { |
|
- | 190 | LED_RED_ON; |
|
- | 191 | sprintf(ErrorMSG,"no GPS communication "); |
|
- | 192 | ErrorCode = 5; |
|
- | 193 | StopNavigation = 1; |
|
- | 194 | } |
|
- | 195 | else if(I2C_Heading.Heading < 0) |
|
- | 196 | { |
|
- | 197 | LED_RED_ON; |
|
- | 198 | sprintf(ErrorMSG,"bad compass value "); |
|
- | 199 | ErrorCode = 6; |
|
- | 200 | StopNavigation = 1; |
|
- | 201 | } |
|
- | 202 | else if(FC.RC_Quality < 100) |
|
- | 203 | { |
|
- | 204 | LED_RED_ON; |
|
- | 205 | sprintf(ErrorMSG,"RC Signal lost "); |
|
- | 206 | ErrorCode = 7; |
|
Line 175... | Line -... | ||
175 | // SCU_FMICLKDivisorConfig(SCU_FMICLK_Div1); |
- | |
176 | // FMI_Config(FMI_READ_WAIT_STATE_3,FMI_WRITE_WAIT_STATE_0, FMI_PWD_ENABLE, FMI_LVD_ENABLE,FMI_FREQ_HIGH); |
- | |
177 | SCU_PLLFactorsConfig(192,25,3); // PLL = 48 MHz |
- | |
Line 178... | Line -... | ||
178 | - | ||
Line -... | Line 207... | ||
- | 207 | } |
|
- | 208 | else // no error occured |
|
- | 209 | { |
|
- | 210 | sprintf(ErrorMSG,"No Error "); |
|
- | 211 | ErrorCode = 0; |
|
- | 212 | StopNavigation = 0; |
|
- | 213 | LED_RED_OFF; |
|
- | 214 | } |
|
- | 215 | CheckSPIOkay = 0; |
|
- | 216 | CheckGPSOkay = 0; |
|
- | 217 | CheckI2COkay = 0; |
|
- | 218 | } |
|
- | 219 | ||
- | 220 | ||
- | 221 | ||
- | 222 | //---------------------------------------------------------------------------------------------------- |
|
- | 223 | int main(void) |
|
179 | SCU_PLLCmd(ENABLE); // PLL Enabled |
224 | { |
- | 225 | logfilestate_t logfilestate = LOGFILE_IDLE; |
|
180 | 226 | KML_Document_t gpslogfile; |
|
- | 227 | s8* gpslogfilename = NULL; |
|
- | 228 | ||
- | 229 | /* Configure the system clocks */ |
|
181 | SCU_MCLKSourceConfig(SCU_MCLK_PLL); // MCLK = PLL |
230 | SCU_Config(); |
- | 231 | ||
Line -... | Line 232... | ||
- | 232 | KML_DocumentInit(&gpslogfile); // Initialize the new kml-document for further use. |
|
- | 233 | ||
- | 234 | /* init VIC (Vectored Interrupt Controller) */ |
|
182 | 235 | SCU_AHBPeriphClockConfig(__VIC,ENABLE); // enable AHB bus clock for VIC |
|
- | 236 | SCU_AHBPeriphReset(__VIC, DISABLE); // disable reset state for VIC |
|
183 | 237 | VIC_DeInit(); // deinitializes the VIC module registers to their default reset values. |
|
184 | VersionInfo.Hauptversion = VERSION_HAUPTVERSION; |
- | |
185 | VersionInfo.Nebenversion = VERSION_NEBENVERSION; |
- | |
- | 238 | // initialize the interrupt handler |
|
186 | VersionInfo.PCKompatibel = VERSION_KOMPATIBEL; |
239 | Interrupt_Init(); |
187 | - | ||
188 | SCU_AHBPeriphClockConfig(__VIC,ENABLE); |
- | |
189 | SCU_AHBPeriphReset(__VIC,DISABLE); |
- | |
190 | VIC_DeInit(); |
240 | // initialize timer 1 |
191 | 241 | TIMER1_Init(); |
|
- | 242 | // initialize the LEDs (needs Timer 1) |
|
- | 243 | Led_Init(); |
|
- | 244 | // initialize the debug UART1 |
|
- | 245 | UART1_Init(); |
|
Line 192... | Line -... | ||
192 | InitInterrupt(); |
- | |
193 | - | ||
194 | Leds_Init(); |
- | |
195 | Debug_UART1_Init(); |
- | |
196 | OutputStartupData(); |
- | |
197 | - | ||
Line 198... | Line 246... | ||
198 | GPS_UART0_Init(); |
246 | SerialPutString("\r\n---------------------------------------------"); |
199 | USB_ConfigInit(); |
247 | |
200 | - | ||
201 | 248 | // initialize UART2 to FLIGHTCTRL |
|
202 | SPI0_Init(); |
- | |
Line 203... | Line 249... | ||
203 | 249 | UART2_Init(); |
|
- | 250 | // initialize UART0 (to MKGPS or MK3MAG) |
|
- | 251 | UART0_Init(); |
|
- | 252 | // initialize usb |
|
- | 253 | USB_ConfigInit(); |
|
204 | 254 | // initialize SPI0 to FC |
|
- | 255 | SPI0_Init(); |
|
- | 256 | // initialize i2c bus to MK3MAG (needs Timer 1) |
|
- | 257 | I2C1_Init(); |
|
- | 258 | // initialize the gps position controller (needs Timer 1) |
|
- | 259 | GPS_Init(); |
|
- | 260 | // initialize fat16 partition on sd card (needs Timer 1) |
|
- | 261 | Fat16_Init(); |
|
- | 262 | ||
- | 263 | ||
- | 264 | TimerCompassUpdate = SetDelay(5); |
|
- | 265 | TimerKmlAddPointDelay = SetDelay(250); |
|
205 | 266 | TimerCheckError = SetDelay(3000); |
|
- | 267 | ||
206 | TIMER1_Init(); |
268 | SerialPutString("\r\n---------------------------------------------"); |
- | 269 | SerialPutString("\n\r Version information:"); |
|
207 | I2C1_Init(); |
270 | GetNaviCtrlVersion(); |
Line 208... | Line -... | ||
208 | - | ||
209 | I2C_Version.Hauptversion = 0xff; |
- | |
210 | - | ||
211 | SendI2C_Command(I2C_CMD_VERSION); |
271 | SPI0_GetFlightCtrlVersion(); |
212 | 272 | if(FC_Version.Compatible != FC_SPI_COMPATIBLE) |
|
213 | TimerCompassUpdate = SetDelay(50); |
273 | { |
214 | while (!CheckDelay(TimerCompassUpdate)); |
274 | //sprintf(ErrorMSG,"\r\n %d <-> %d", FlightCtrlVersion.NaviCompatible, FC_SPI_COMPATIBLE); |
215 | 275 | //SerialPutString(ErrorMSG); |
|
216 | TimerCompassUpdate = SetDelay(5); |
- | |
217 | TimerI2CReadDelay = SetDelay(5); |
- | |
218 | - | ||
219 | KmlAddPointDelay = SetDelay(250); |
276 | SerialPutString("\n\r Flight-Ctrl not compatible"); |
- | 277 | LED_RED_ON; |
|
Line -... | Line 278... | ||
- | 278 | } |
|
- | 279 | I2C1_GetMK3MagVersion(); |
|
Line 220... | Line -... | ||
220 | - | ||
221 | - | ||
222 | SerialPutString("\r\n---------------------------------------------"); |
- | |
223 | - | ||
224 | 280 | if(MK3MAG_Version.Compatible != MK3MAG_I2C_COMPATIBLE) |
|
225 | // ReadSetting(1); |
281 | { |
226 | SerialPutString("\r\n---------------------------------------------\r\n\r\n"); |
282 | SerialPutString("\n\r MK3Mag not compatible"); |
227 | 283 | LED_RED_ON; |
|
228 | InitFat16(); |
284 | } |
229 | 285 | ||
230 | 286 | /* |
|
- | 287 | ReadSetting(1); |
|
231 | for (;;) |
288 | */ |
- | 289 | SerialPutString("\r\n---------------------------------------------\r\n\r\n"); |
|
- | 290 | ||
232 | { |
291 | for (;;) |
233 | if(NeuerDatensatzEmpfangen) |
292 | { |
- | 293 | if(rxd_buffer_locked) // new incomming command |
|
- | 294 | { |
|
- | 295 | UART1_ProcessRxData(); // process request |
|
- | 296 | } |
|
- | 297 | UART1_TransmitTxData(); // send answer |
|
234 | { |
298 | |
235 | BearbeiteRxDaten(); |
- | |
236 | DatenUebertragung(); |
- | |
237 | // if (GPIO_ReadBit(GPIO5, GPIO_Pin_3)) SerialPutString("keine SD\n\r"); else SerialPutString("SD eingesteckt!\n\r"); |
- | |
238 | 299 | SPI0_UpdateBuffer(); |
|
239 | } |
300 | UART1_Transmit(); // empty txd buffer |
240 | - | ||
241 | - | ||
242 | UpdateSPI_Buffer(); |
- | |
243 | SPI_CheckSlaveSelect(); |
301 | |
244 | UART1_Transmit(); |
302 | // ------------------------- I2C Timing -------------------------------- |
245 | - | ||
246 | // ----------- I2C Timing ------------------------- |
- | |
247 | if (CheckDelay(TimerCompassUpdate)) |
- | |
248 | { |
- | |
249 | if(oldCompassCalState != CompassCalState) |
303 | if(I2C_State != I2C_OFF) |
250 | { |
- | |
251 | oldCompassCalState = CompassCalState; |
304 | { |
252 | TimerCompassUpdate = SetDelay(25); |
305 | if (CheckDelay(TimerCompassUpdate)) |
253 | SendI2C_Command(I2C_CMD_WRITE_CAL); |
306 | { |
Line 254... | Line -... | ||
254 | } |
- | |
255 | else |
- | |
256 | { |
- | |
257 | GPIO_ToggleBit(GPIO5, GPIO_Pin_6); |
307 | // check for hanging I2C bus |
258 | GPIO_ToggleBit(GPIO5, GPIO_Pin_7); |
- | |
259 | TimerCompassUpdate = SetDelay(25); |
308 | if(CheckDelay(I2C1_Timeout)) |
Line 260... | Line -... | ||
260 | SendI2C_Command(I2C_CMD_READ_HEADING); |
- | |
261 | } |
309 | { // reset I2C |
262 | 310 | SerialPutString("\n\rCommunication-Error: MK3Mag\n\r"); |
|
263 | } |
311 | I2C1_Deinit(); |
- | 312 | I2C1_Init(); |
|
- | 313 | } |
|
264 | 314 | else |
|
- | 315 | { // check for incomming compass calibration request |
|
- | 316 | // update CalByte from spi input queue |
|
- | 317 | fifo_get(&CompassCalcStateFiFo, (u8 *)&(I2C_WriteCal.CalByte)); |
|
265 | if (CheckDelay(TimerI2CReadDelay)) |
318 | // send new calstate |
266 | { |
319 | if(I2C_ReadCal.CalByte != I2C_WriteCal.CalByte) |
267 | if (I2C_ReadRequest) |
320 | { |
- | 321 | I2C1_SendCommand(I2C_CMD_WRITE_CAL); |
|
268 | { |
322 | } |
269 | I2C_ReadRequest= 0; |
323 | else // request current heading |
270 | 324 | { |
|
271 | I2C_Direction = I2C_MODE_RECEIVER; |
325 | I2C1_SendCommand(I2C_CMD_READ_HEADING); |
- | 326 | } |
|
- | 327 | TimerCompassUpdate = SetDelay(25); // every 25 ms |
|
- | 328 | } |
|
- | 329 | ||
- | 330 | } |
|
- | 331 | } |
|
272 | I2C_GenerateStart(I2C1, ENABLE); |
332 | |
- | 333 | // ---------------- Error Check Timing ---------------------------- |
|
- | 334 | if(CheckDelay(TimerCheckError)) |
|
273 | } |
335 | { |
274 | TimerI2CReadDelay = SetDelay(1000); |
336 | if(!CheckSPIOkay) GPS_Navigation(); // process the GPS data even if the FC is not connected |
275 | } |
337 | CheckErrors(); |
276 | 338 | TimerCheckError = SetDelay(1000); |
|
- | 339 | Request_SendFollowMe = TRUE; |
|
- | 340 | } |
|
- | 341 | // ---------------- KML Timing ------------------------------------ |
|
- | 342 | if(CheckDelay(TimerKmlAddPointDelay)) |
|
277 | if (CheckDelay(TimerDebugDataDelay)) |
343 | { |
278 | { SendGPSPosAnforderung = 1; |
344 | TimerKmlAddPointDelay = SetDelay(500); // every 500 ms |
279 | DatenUebertragung(); |
345 | |
- | 346 | if(FC.MKFlags & MKFLAG_FLY) // model is flying |
|
- | 347 | { |
|
- | 348 | switch(logfilestate) |
|
280 | 349 | { |
|
281 | TimerDebugDataDelay = SetDelay(500); |
350 | case LOGFILE_IDLE: |
282 | } |
351 | case LOGFILE_CLOSED: |
283 | 352 | // find unused logfile name |
|
284 | 353 | do |
|
- | 354 | { // try to generate a new logfile name |
|
Line -... | Line 355... | ||
- | 355 | gpslogfilename = GenerateGPSLogFileName(); |
|
- | 356 | }while((gpslogfilename != NULL) && fexist_(gpslogfilename)); |
|
- | 357 | // if logfilename exist |
|
285 | // ----------- KML Timing ------------------------- |
358 | if(gpslogfilename != NULL) |
- | 359 | { |
|
286 | if(CheckDelay(KmlAddPointDelay) && 0) |
360 | // try to create the log file |
- | 361 | if(KML_DocumentOpen(gpslogfilename, &gpslogfile)) |
|
- | 362 | { |
|
- | 363 | logfilestate = LOGFILE_OPENED; // goto next step |
|
- | 364 | SerialPutString("\r\nOpening kml-file: "); |
|
- | 365 | SerialPutString(gpslogfilename); |
|
- | 366 | SerialPutString("\r\n"); |
|
- | 367 | } |
|
- | 368 | else |
|
- | 369 | { |
|
- | 370 | logfilestate = LOGFILE_ERROR; |
|
- | 371 | SerialPutString("\r\nError opening kml-file: "); |
|
287 | { |
372 | SerialPutString(gpslogfilename); |
288 | KmlAddPointDelay = SetDelay(500); |
373 | SerialPutString("\r\n"); |
- | 374 | TimerKmlAddPointDelay = SetDelay(10); // try again in open logfile in 10 mili sec |
|
289 | switch(kml_state) |
375 | } |
290 | { |
376 | } |
291 | case 0: |
377 | // else retry in next loop |
292 | if((GPS_Data.Flags & GPS_FIX) && (GPS_Data.Used_Sat >= GPS_SAT_MIN)) |
378 | break; |
293 | { |
379 | |
294 | if(str_Volume.state == INITIALIZED) // has the volume and filesystem been initialized? |
- | |
295 | { |
380 | |
296 | if(KML_DocumentOpen("flight01.kml",&mydocument)) // create a new kml-document on the memorycard. |
381 | case LOGFILE_OPENED: |
Line -... | Line 382... | ||
- | 382 | // append new gps log data |
|
- | 383 | if((GPSData.Status != INVALID) && (GPSData.Flags & FLAG_GPSFIXOK) && (GPSData.SatFix == SATFIX_3D)) |
|
- | 384 | { |
|
- | 385 | if(!KML_LoggGPSCoordinates(&(GPSData.Position), &gpslogfile)) |
|
- | 386 | { // error logging data |
|
- | 387 | SerialPutString("\r\nError Logging GPS data\r\n"); |
|
- | 388 | KML_DocumentClose(&gpslogfile); |
|
- | 389 | logfilestate = LOGFILE_ERROR; |
|
- | 390 | } |
|
- | 391 | } |
|
297 | { |
392 | break; |
- | 393 | ||
- | 394 | case LOGFILE_ERROR: |
|
- | 395 | // try to reinitialize the fat16 filesystem |
|
- | 396 | if(0 == Fat16_Init()) |
|
- | 397 | { |
|
298 | SerialPutString("\r\nopening kml-file\r\nadding data"); |
398 | logfilestate = LOGFILE_IDLE; |
299 | kml_state = 1; |
399 | TimerKmlAddPointDelay = SetDelay(10); // try again in open logfile in 10 mili sec |
- | 400 | } |
|
- | 401 | else |
|
300 | } |
402 | { |
301 | } |
403 | logfilestate = LOGFILE_ERROR; |
302 | else |
- |