Subversion Repositories NaviCtrl

Rev

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

/*#######################################################################################*/
/* !!! THIS IS NOT FREE SOFTWARE !!!                                                     */
/*#######################################################################################*/
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Copyright (c) 2008 Ingo Busker, Holger Buss
// + Nur für den privaten Gebrauch / NON-COMMERCIAL USE ONLY
// + FOR NON COMMERCIAL USE ONLY
// + www.MikroKopter.com
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
// + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist.
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
// + bzgl. der Nutzungsbedingungen aufzunehmen.
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
// + Verkauf von Luftbildaufnahmen, usw.
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
// + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
// + eindeutig als Ursprung verlinkt werden
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
// + Benutzung auf eigene Gefahr
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Die Portierung oder Nutzung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
// + mit unserer Zustimmung zulässig
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
// + this list of conditions and the following disclaimer.
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
// +     from this software without specific prior written permission.
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permitted
// +     for non-commercial use (directly or indirectly)
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
// +     with our written permission
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
// +     clearly linked as origin
// +   * porting the sources to other systems or using the software on other systems (except hardware from www.mikrokopter.de) is not allowed
//
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// +  POSSIBILITY OF SUCH DAMAGE.
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#include "main.h"

u8 NewGPSDataAvail = 0;
u8 NewPCTargetGPSPosition = 0;
u8 GPS_RxBuffer[GPS_RX_SIZE];

struct str_ubx_nav_sol    *GPS_DataSol;
struct str_ubx_nav_posllh *GPS_DataPosllh;
struct str_ubx_nav_velned *GPS_DataVelned;
struct str_ubx_nav_posutm *GPS_DataPosutm;
struct str_gps_nav_data   GPS_Data;


// ----------------------------- GPS - Receive ------------------------------------
// --- Connect RXD & TXD to GPS ---
void Connect_UART0_to_GPS(void)
{
        GPIO_InitTypeDef GPIO_InitStructure;

        SCU_APBPeriphClockConfig(__GPIO6, ENABLE);  
       
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinInput;
    GPIO_InitStructure.GPIO_Pin =                       GPIO_Pin_1;
    GPIO_InitStructure.GPIO_Type =                      GPIO_Type_PushPull ;
    GPIO_InitStructure.GPIO_IPConnected =       GPIO_IPConnected_Disable;
    GPIO_InitStructure.GPIO_Alternate =         GPIO_InputAlt1  ;
    GPIO_Init(GPIO5, &GPIO_InitStructure);

        GPIO_InitStructure.GPIO_Direction =     GPIO_PinInput;
    GPIO_InitStructure.GPIO_Pin =                       GPIO_Pin_0;
    GPIO_InitStructure.GPIO_Type =                      GPIO_Type_PushPull ;
    GPIO_InitStructure.GPIO_Alternate =         GPIO_InputAlt1  ;
    GPIO_Init(GPIO5, &GPIO_InitStructure);

        GPIO_InitStructure.GPIO_Direction =     GPIO_PinInput;
    GPIO_InitStructure.GPIO_Pin =                       GPIO_Pin_6;
    GPIO_InitStructure.GPIO_Type =                      GPIO_Type_PushPull ;
    GPIO_InitStructure.GPIO_IPConnected =       GPIO_IPConnected_Enable;
    GPIO_InitStructure.GPIO_Alternate =         GPIO_InputAlt1  ;
    GPIO_Init(GPIO6, &GPIO_InitStructure);

        GPIO_InitStructure.GPIO_Direction =     GPIO_PinOutput;
    GPIO_InitStructure.GPIO_Pin =                       GPIO_Pin_7;
    GPIO_InitStructure.GPIO_Type =                      GPIO_Type_PushPull ;
    GPIO_InitStructure.GPIO_Alternate =         GPIO_OutputAlt3  ;
    GPIO_Init(GPIO6, &GPIO_InitStructure);
}
//-----------------------------------------------
void Connect_UART0_to_Compass(void)
{
        GPIO_InitTypeDef GPIO_InitStructure;

        SCU_APBPeriphClockConfig(__GPIO5, ENABLE);  
   // GPS off
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinInput;
    GPIO_InitStructure.GPIO_Pin =                       GPIO_Pin_6;
    GPIO_InitStructure.GPIO_Type =                      GPIO_Type_PushPull ;
    GPIO_InitStructure.GPIO_IPConnected =       GPIO_IPConnected_Disable;
    GPIO_InitStructure.GPIO_Alternate =         GPIO_InputAlt1  ;
    GPIO_Init(GPIO6, &GPIO_InitStructure);

        GPIO_InitStructure.GPIO_Direction =     GPIO_PinInput;
    GPIO_InitStructure.GPIO_Pin =                       GPIO_Pin_7;
    GPIO_InitStructure.GPIO_Type =                      GPIO_Type_PushPull ;
    GPIO_InitStructure.GPIO_Alternate =         GPIO_InputAlt1  ;
    GPIO_Init(GPIO6, &GPIO_InitStructure);

        // map UART0 to Compass
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinInput;
    GPIO_InitStructure.GPIO_Pin =                       GPIO_Pin_1;
    GPIO_InitStructure.GPIO_Type =                      GPIO_Type_PushPull ;
    GPIO_InitStructure.GPIO_IPConnected =       GPIO_IPConnected_Enable;
    GPIO_InitStructure.GPIO_Alternate =         GPIO_InputAlt1  ;
    GPIO_Init(GPIO5, &GPIO_InitStructure);

        GPIO_InitStructure.GPIO_Direction =     GPIO_PinOutput;
    GPIO_InitStructure.GPIO_Pin =                       GPIO_Pin_0;
    GPIO_InitStructure.GPIO_Type =                      GPIO_Type_PushPull ;
    GPIO_InitStructure.GPIO_Alternate =         GPIO_OutputAlt3  ;
    GPIO_Init(GPIO5, &GPIO_InitStructure);
}


// --------------------------- UART0 Init ------------------------------------
void GPS_UART0_Init (void)
{
    UART_InitTypeDef UART_InitStructure;

    SerialPutString("GPS init...");
   
        SCU_APBPeriphClockConfig(__UART0, ENABLE);  // Enable the UART1 Clock
   
        Connect_UART0_to_GPS();

    UART_InitStructure.UART_WordLength =                        UART_WordLength_8D;
    UART_InitStructure.UART_StopBits =                          UART_StopBits_1;
    UART_InitStructure.UART_Parity =                            UART_Parity_No ;
    UART_InitStructure.UART_BaudRate =                          BAUD_RATE;
    UART_InitStructure. UART_HardwareFlowControl =      UART_HardwareFlowControl_None;
    UART_InitStructure.UART_Mode =                                      UART_Mode_Tx_Rx;
    //UART_InitStructure.UART_FIFO =                                    UART_FIFO_Disable;
    UART_InitStructure.UART_FIFO =                                      UART_FIFO_Enable;
    UART_InitStructure.UART_TxFIFOLevel = UART_FIFOLevel_1_2;
    UART_InitStructure.UART_RxFIFOLevel = UART_FIFOLevel_1_2;

        UART_DeInit(UART0);
    UART_Init(UART0, &UART_InitStructure);

        VIC_Config(UART0_ITLine, VIC_IRQ, 10);  
        VIC_ITCmd(UART0_ITLine, ENABLE);  

    UART_ITConfig(UART0, UART_IT_Receive | UART_IT_ReceiveTimeOut, ENABLE);
        UART_Cmd(UART0, ENABLE);

    GPS_Init();

        SerialPutString("ok\n\r");
}

// ----------------------------- GPS - Receive ------------------------------------
void UART0_IRQHandler(void)
{
  u8 sioTmp = 0;
  static u8 uartState = 0, gps_buf_ptr = 0;
 

 GPIO_ToggleBit(GPIO5, GPIO_Pin_6);
  if (DebugUART == UART0)
  {
    while (UART_GetFlagStatus(UART0, UART_FLAG_RxFIFOEmpty) != SET)
          UART_SendData(UART1, UART_ReceiveData(UART0));

   UART_ClearITPendingBit(UART0, UART_IT_Receive);
   UART_ClearITPendingBit(UART0, UART_IT_ReceiveTimeOut);

  }
 else
 if((UART_GetITStatus(UART0, UART_IT_Receive) != RESET)|| (UART_GetITStatus(UART0, UART_IT_ReceiveTimeOut) != RESET) )
 {
   UART_ClearITPendingBit(UART0, UART_IT_Receive);
   UART_ClearITPendingBit(UART0, UART_IT_ReceiveTimeOut);

  while ((UART_GetFlagStatus(UART0, UART_FLAG_RxFIFOEmpty) != SET) && NewGPSDataAvail == 0)
  {

   sioTmp = UART_ReceiveData(UART0);


   switch (uartState)
   {
     case 0: if(sioTmp == 0xB5) { GPS_RxBuffer[4]=0; uartState = 1; }  // GPS-UBX Startzeichen
             gps_buf_ptr = 0;
             GPS_RxBuffer[gps_buf_ptr++] = sioTmp;
                break;
         
         case 1:
                 if(gps_buf_ptr == 1 && sioTmp != 0x62) { uartState = 0; break;};  // andere Meldung unterdrücken
             if(gps_buf_ptr == 2 && sioTmp != 0x01) { uartState = 0; break;};  // andere Meldung unterdrücken
             if(gps_buf_ptr == 3 && sioTmp != 0x02 && sioTmp != 0x06 && sioTmp != 0x12 && sioTmp != 0x08) { uartState = 0; break;};  // andere Meldung unterdrücken
                     
                         GPS_RxBuffer[gps_buf_ptr] = sioTmp;
                     
                         if(gps_buf_ptr < (GPS_RxBuffer[4]+6)) gps_buf_ptr++;
                     else
                     {
                      if(GPS_RxBuffer[3] == 0x06)   // NAV SOL
              {
                GPS_DataSol = (struct str_ubx_nav_sol *) GPS_RxBuffer;
                GPS_Data.Flags = GPS_DataSol->Flags;
                                GPS_Data.Used_Sat = GPS_DataSol->Number_SV;
                            GPS_Data.GPS_week  = GPS_DataSol->GPS_week;
              }
             else if(GPS_RxBuffer[3] == 0x12)   //NAV VELNED
             {
                           GPS_DataVelned = (struct str_ubx_nav_velned *) GPS_RxBuffer;
                           GPS_Data.N_Speed = GPS_DataVelned->N_Speed;
               GPS_Data.E_Speed = GPS_DataVelned->E_Speed;
               GPS_Data.DownSpeed = GPS_DataVelned->DownSpeed;

               GPS_Data.SpeedAccuracy = GPS_DataVelned->SpeedAccuracy;
                           NewGPSDataAvail = 1;
             }
             else if(GPS_RxBuffer[3] == 0x02)   // NAV POSLLH
             {
                           GPS_DataPosllh = (struct str_ubx_nav_posllh *) GPS_RxBuffer;
                           GPS_Data.Longitude = GPS_DataPosllh->Longitude;
                           GPS_Data.Latitude  = GPS_DataPosllh->Latitude;
                           GPS_Data.Height    = GPS_DataPosllh->Height;
                           GPS_Data.HeightSL  = GPS_DataPosllh->HeightSL;
                           GPS_Data.GPS_time  = GPS_DataPosllh->GPS_time;
                           GPS_Data.HorizontalAccuracy = GPS_DataPosllh->HorizontalAccuracy;
                           GPS_Data.VerticalAccuracy = GPS_DataPosllh->VerticalAccuracy;
             }
                         else if(GPS_RxBuffer[3] == 0x08)   // NAV POSUTM
             {
                GPS_DataPosutm = (struct str_ubx_nav_posutm *) GPS_RxBuffer;
                            GPS_Data.North = GPS_DataPosutm->North;
                GPS_Data.East = GPS_DataPosutm->East;
                GPS_Data.Altitude = GPS_DataPosutm->Altitude;
                         }
            uartState = 0;
                   }
            break;
   }
  }
 
 }
}