Subversion Repositories NaviCtrl

Rev

Rev 23 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 ingob 1
/*#######################################################################################*/
2
/* !!! THIS IS NOT FREE SOFTWARE !!!                                                     */
3
/*#######################################################################################*/
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5
// + Copyright (c) 2008 Ingo Busker, Holger Buss
171 ingob 6
// + Nur für den privaten Gebrauch / NON-COMMERCIAL USE ONLY
1 ingob 7
// + FOR NON COMMERCIAL USE ONLY
8
// + www.MikroKopter.com
9
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
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. 
12
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt 
13
// + bzgl. der Nutzungsbedingungen aufzunehmen. 
14
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
15
// + Verkauf von Luftbildaufnahmen, usw.
16
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
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
19
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
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"
22
// + eindeutig als Ursprung verlinkt werden
23
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
24
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
25
// + Benutzung auf eigene Gefahr
26
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
27
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
171 ingob 28
// + Die Portierung oder Nutzung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur 
1 ingob 29
// + mit unserer Zustimmung zulässig
30
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
31
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
32
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
33
// + Redistributions of source code (with or without modifications) must retain the above copyright notice, 
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
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 
38
// +     for non-commercial use (directly or indirectly)
39
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted 
40
// +     with our written permission
41
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be 
42
// +     clearly linked as origin 
171 ingob 43
// +   * porting the sources to other systems or using the software on other systems (except hardware from www.mikrokopter.de) is not allowed
1 ingob 44
//
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
47
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
49
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
50
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
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
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
55
// +  POSSIBILITY OF SUCH DAMAGE. 
56
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
57
#include "main.h"
58
 
59
u8 NewGPSDataAvail = 0;
60
u8 NewPCTargetGPSPosition = 0;
61
u8 GPS_RxBuffer[GPS_RX_SIZE];
62
 
63
struct str_ubx_nav_sol    *GPS_DataSol;
64
struct str_ubx_nav_posllh *GPS_DataPosllh;
65
struct str_ubx_nav_velned *GPS_DataVelned;
66
struct str_ubx_nav_posutm *GPS_DataPosutm;
67
struct str_gps_nav_data   GPS_Data;
68
 
69
 
70
// ----------------------------- GPS - Receive ------------------------------------
71
// --- Connect RXD & TXD to GPS ---
72
void Connect_UART0_to_GPS(void)
73
{
74
        GPIO_InitTypeDef GPIO_InitStructure;
75
 
76
        SCU_APBPeriphClockConfig(__GPIO6, ENABLE);  
77
 
78
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinInput;
79
    GPIO_InitStructure.GPIO_Pin =                       GPIO_Pin_1;
80
    GPIO_InitStructure.GPIO_Type =                      GPIO_Type_PushPull ;
81
    GPIO_InitStructure.GPIO_IPConnected =       GPIO_IPConnected_Disable;
82
    GPIO_InitStructure.GPIO_Alternate =         GPIO_InputAlt1  ;
83
    GPIO_Init(GPIO5, &GPIO_InitStructure);
84
 
85
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinInput;
86
    GPIO_InitStructure.GPIO_Pin =                       GPIO_Pin_0;
87
    GPIO_InitStructure.GPIO_Type =                      GPIO_Type_PushPull ;
88
    GPIO_InitStructure.GPIO_Alternate =         GPIO_InputAlt1  ;
89
    GPIO_Init(GPIO5, &GPIO_InitStructure);
90
 
91
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinInput;
92
    GPIO_InitStructure.GPIO_Pin =                       GPIO_Pin_6;
93
    GPIO_InitStructure.GPIO_Type =                      GPIO_Type_PushPull ;
94
    GPIO_InitStructure.GPIO_IPConnected =       GPIO_IPConnected_Enable;
95
    GPIO_InitStructure.GPIO_Alternate =         GPIO_InputAlt1  ;
96
    GPIO_Init(GPIO6, &GPIO_InitStructure);
97
 
98
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinOutput;
99
    GPIO_InitStructure.GPIO_Pin =                       GPIO_Pin_7;
100
    GPIO_InitStructure.GPIO_Type =                      GPIO_Type_PushPull ;
101
    GPIO_InitStructure.GPIO_Alternate =         GPIO_OutputAlt3  ;
102
    GPIO_Init(GPIO6, &GPIO_InitStructure);
103
}
104
//-----------------------------------------------
105
void Connect_UART0_to_Compass(void)
106
{
107
        GPIO_InitTypeDef GPIO_InitStructure;
108
 
109
        SCU_APBPeriphClockConfig(__GPIO5, ENABLE);  
110
   // GPS off
111
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinInput;
112
    GPIO_InitStructure.GPIO_Pin =                       GPIO_Pin_6;
113
    GPIO_InitStructure.GPIO_Type =                      GPIO_Type_PushPull ;
114
    GPIO_InitStructure.GPIO_IPConnected =       GPIO_IPConnected_Disable;
115
    GPIO_InitStructure.GPIO_Alternate =         GPIO_InputAlt1  ;
116
    GPIO_Init(GPIO6, &GPIO_InitStructure);
117
 
118
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinInput;
119
    GPIO_InitStructure.GPIO_Pin =                       GPIO_Pin_7;
120
    GPIO_InitStructure.GPIO_Type =                      GPIO_Type_PushPull ;
121
    GPIO_InitStructure.GPIO_Alternate =         GPIO_InputAlt1  ;
122
    GPIO_Init(GPIO6, &GPIO_InitStructure);
123
 
124
        // map UART0 to Compass
125
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinInput;
126
    GPIO_InitStructure.GPIO_Pin =                       GPIO_Pin_1;
127
    GPIO_InitStructure.GPIO_Type =                      GPIO_Type_PushPull ;
128
    GPIO_InitStructure.GPIO_IPConnected =       GPIO_IPConnected_Enable;
129
    GPIO_InitStructure.GPIO_Alternate =         GPIO_InputAlt1  ;
130
    GPIO_Init(GPIO5, &GPIO_InitStructure);
131
 
132
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinOutput;
133
    GPIO_InitStructure.GPIO_Pin =                       GPIO_Pin_0;
134
    GPIO_InitStructure.GPIO_Type =                      GPIO_Type_PushPull ;
135
    GPIO_InitStructure.GPIO_Alternate =         GPIO_OutputAlt3  ;
136
    GPIO_Init(GPIO5, &GPIO_InitStructure);
137
}
138
 
139
 
140
// --------------------------- UART0 Init ------------------------------------
141
void GPS_UART0_Init (void)
142
{
143
    UART_InitTypeDef UART_InitStructure;
144
 
145
    SerialPutString("GPS init...");
146
 
147
        SCU_APBPeriphClockConfig(__UART0, ENABLE);  // Enable the UART1 Clock 
148
 
149
        Connect_UART0_to_GPS();
150
 
151
    UART_InitStructure.UART_WordLength =                        UART_WordLength_8D;
152
    UART_InitStructure.UART_StopBits =                          UART_StopBits_1;
153
    UART_InitStructure.UART_Parity =                            UART_Parity_No ;
154
    UART_InitStructure.UART_BaudRate =                          BAUD_RATE;
155
    UART_InitStructure. UART_HardwareFlowControl =      UART_HardwareFlowControl_None;
156
    UART_InitStructure.UART_Mode =                                      UART_Mode_Tx_Rx;
157
    //UART_InitStructure.UART_FIFO =                                    UART_FIFO_Disable;
158
    UART_InitStructure.UART_FIFO =                                      UART_FIFO_Enable;
159
    UART_InitStructure.UART_TxFIFOLevel = UART_FIFOLevel_1_2;
160
    UART_InitStructure.UART_RxFIFOLevel = UART_FIFOLevel_1_2;
161
 
162
        UART_DeInit(UART0);
163
    UART_Init(UART0, &UART_InitStructure);
164
 
165
        VIC_Config(UART0_ITLine, VIC_IRQ, 10);  
166
        VIC_ITCmd(UART0_ITLine, ENABLE);  
167
 
168
    UART_ITConfig(UART0, UART_IT_Receive | UART_IT_ReceiveTimeOut, ENABLE);
169
        UART_Cmd(UART0, ENABLE);
170
 
171
    GPS_Init();
172
 
173
        SerialPutString("ok\n\r");
174
}
175
 
176
// ----------------------------- GPS - Receive ------------------------------------
177
void UART0_IRQHandler(void)
178
{
179
  u8 sioTmp = 0;
180
  static u8 uartState = 0, gps_buf_ptr = 0;
181
 
182
 
183
 GPIO_ToggleBit(GPIO5, GPIO_Pin_6);
184
  if (DebugUART == UART0)
185
  {
186
    while (UART_GetFlagStatus(UART0, UART_FLAG_RxFIFOEmpty) != SET)
187
          UART_SendData(UART1, UART_ReceiveData(UART0));
188
 
189
   UART_ClearITPendingBit(UART0, UART_IT_Receive);
190
   UART_ClearITPendingBit(UART0, UART_IT_ReceiveTimeOut);
191
 
192
  }
193
 else
194
 if((UART_GetITStatus(UART0, UART_IT_Receive) != RESET)|| (UART_GetITStatus(UART0, UART_IT_ReceiveTimeOut) != RESET) )
195
 {
196
   UART_ClearITPendingBit(UART0, UART_IT_Receive);
197
   UART_ClearITPendingBit(UART0, UART_IT_ReceiveTimeOut);
198
 
199
  while ((UART_GetFlagStatus(UART0, UART_FLAG_RxFIFOEmpty) != SET) && NewGPSDataAvail == 0)
200
  {
201
 
202
   sioTmp = UART_ReceiveData(UART0);
203
 
204
 
205
   switch (uartState)
206
   {
207
     case 0: if(sioTmp == 0xB5) { GPS_RxBuffer[4]=0; uartState = 1; }  // GPS-UBX Startzeichen 
208
             gps_buf_ptr = 0;
209
             GPS_RxBuffer[gps_buf_ptr++] = sioTmp;
210
                break;
211
 
212
         case 1:
213
                 if(gps_buf_ptr == 1 && sioTmp != 0x62) { uartState = 0; break;};  // andere Meldung unterdrücken
214
             if(gps_buf_ptr == 2 && sioTmp != 0x01) { uartState = 0; break;};  // andere Meldung unterdrücken
215
             if(gps_buf_ptr == 3 && sioTmp != 0x02 && sioTmp != 0x06 && sioTmp != 0x12 && sioTmp != 0x08) { uartState = 0; break;};  // andere Meldung unterdrücken
216
 
217
                         GPS_RxBuffer[gps_buf_ptr] = sioTmp;
218
 
219
                         if(gps_buf_ptr < (GPS_RxBuffer[4]+6)) gps_buf_ptr++;
220
                     else
221
                     {
222
                      if(GPS_RxBuffer[3] == 0x06)   // NAV SOL
223
              {
224
                GPS_DataSol = (struct str_ubx_nav_sol *) GPS_RxBuffer;
225
                GPS_Data.Flags = GPS_DataSol->Flags;
226
                                GPS_Data.Used_Sat = GPS_DataSol->Number_SV;
227
                            GPS_Data.GPS_week  = GPS_DataSol->GPS_week;
228
              }
229
             else if(GPS_RxBuffer[3] == 0x12)   //NAV VELNED
230
             {
231
                           GPS_DataVelned = (struct str_ubx_nav_velned *) GPS_RxBuffer;
232
                           GPS_Data.N_Speed = GPS_DataVelned->N_Speed;
233
               GPS_Data.E_Speed = GPS_DataVelned->E_Speed;
234
               GPS_Data.DownSpeed = GPS_DataVelned->DownSpeed;
235
 
236
               GPS_Data.SpeedAccuracy = GPS_DataVelned->SpeedAccuracy;
237
                           NewGPSDataAvail = 1;
238
             }
239
             else if(GPS_RxBuffer[3] == 0x02)   // NAV POSLLH
240
             {
241
                           GPS_DataPosllh = (struct str_ubx_nav_posllh *) GPS_RxBuffer;
242
                           GPS_Data.Longitude = GPS_DataPosllh->Longitude;
243
                           GPS_Data.Latitude  = GPS_DataPosllh->Latitude;
244
                           GPS_Data.Height    = GPS_DataPosllh->Height;
245
                           GPS_Data.HeightSL  = GPS_DataPosllh->HeightSL;
246
                           GPS_Data.GPS_time  = GPS_DataPosllh->GPS_time;
247
                           GPS_Data.HorizontalAccuracy = GPS_DataPosllh->HorizontalAccuracy;
248
                           GPS_Data.VerticalAccuracy = GPS_DataPosllh->VerticalAccuracy;
249
             }
250
                         else if(GPS_RxBuffer[3] == 0x08)   // NAV POSUTM
251
             {
252
                GPS_DataPosutm = (struct str_ubx_nav_posutm *) GPS_RxBuffer;
253
                            GPS_Data.North = GPS_DataPosutm->North;
254
                GPS_Data.East = GPS_DataPosutm->East;
255
                GPS_Data.Altitude = GPS_DataPosutm->Altitude;
256
                         }
257
            uartState = 0;
258
                   }
259
            break;
260
   }
261
  }
262
 
263
 }
264
}