Subversion Repositories NaviCtrl

Rev

Rev 81 | 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 "91x_lib.h"
#include "uart0.h"
#include "uart1.h"
#include "ubx.h"
#include <stdio.h>
#include <stdarg.h>

//------------------------------------------------------------------------------------
// global variables


// UART0 MUXER

UART0_MuxerState_t UART0_Muxer = UART0_UNDEF;
u16 Uart0Baudrate = BAUD_RATE;
u16 Uart0MK3MagBaudrate = BAUD_RATE;

volatile u8 txd_buffer0[TXD_BUFFER_LEN];
volatile u8 txd_complete0 = TRUE;
static u16 ptr_txd_buffer0 = 0;

//------------------------------------------------------------------------------------
// functions

/********************************************************/
/*              Connect RXD & TXD to GPS                */
/********************************************************/
void UART0_Connect_to_MKGPS(void)
{
        GPIO_InitTypeDef GPIO_InitStructure;

        UART0_Muxer = UART0_UNDEF;

        SCU_APBPeriphClockConfig(__GPIO6, ENABLE); // Enable the GPIO6 Clock
        // unmap UART0 from Compass
        // set port pin 5.1 (serial data from compass) to input and disconnect from IP
        GPIO_StructInit(&GPIO_InitStructure);
        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);
        // set port pin 5.0 (serial data to compass) to input
        GPIO_StructInit(&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_IPConnected =   GPIO_IPConnected_Disable;
    GPIO_InitStructure.GPIO_Alternate =         GPIO_InputAlt1;
    GPIO_Init(GPIO5, &GPIO_InitStructure);
        // map UART0 to GPS
        // set port pin 6.6 (serial data from gps) to input and connect to IP
        GPIO_StructInit(&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; //UART0_RxD
    GPIO_Init(GPIO6, &GPIO_InitStructure);
        // set port pin 6.7 (serial data to gps) to output
        GPIO_StructInit(&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_IPConnected =   GPIO_IPConnected_Enable;
    GPIO_InitStructure.GPIO_Alternate =         GPIO_OutputAlt3; //UART0_TX
    GPIO_Init(GPIO6, &GPIO_InitStructure);
       
        UART0_Muxer = UART0_MKGPS;
}

/********************************************************/
/*              Connect RXD & TXD to MK3MAG             */
/********************************************************/
void UART0_Connect_to_MK3MAG(void)
{
        GPIO_InitTypeDef GPIO_InitStructure;

        UART0_Muxer = UART0_UNDEF;

        SCU_APBPeriphClockConfig(__GPIO5, ENABLE);
        // unmap UART0 from GPS
        // set port pin 6.6 (serial data from gps) to input and disconnect from IP
        GPIO_StructInit(&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_Disable;
    GPIO_InitStructure.GPIO_Alternate =         GPIO_InputAlt1;
    GPIO_Init(GPIO6, &GPIO_InitStructure);
        // set port pin 6.7 (serial data to gps) to input
        GPIO_StructInit(&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_IPConnected =   GPIO_IPConnected_Disable;
    GPIO_InitStructure.GPIO_Alternate =         GPIO_InputAlt1;
    GPIO_Init(GPIO6, &GPIO_InitStructure);

        // map UART0 to Compass
        // set port pin 5.1 (serial data from compass) to input and connect to IP
        GPIO_StructInit(&GPIO_InitStructure);
        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; //UART0_RxD
    GPIO_Init(GPIO5, &GPIO_InitStructure);
        // set port pin 5.0 (serial data to compass) to output
        GPIO_StructInit(&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_IPConnected =   GPIO_IPConnected_Enable;
    GPIO_InitStructure.GPIO_Alternate =         GPIO_OutputAlt3; //UART0_TX
    GPIO_Init(GPIO5, &GPIO_InitStructure);

        UART0_Muxer = UART0_MK3MAG;
}

/********************************************************/
/*                  Initialize UART0                    */
/********************************************************/
void UART0_Init(void)
{
    UART_InitTypeDef UART_InitStructure;
       
        SerialPutString("\r\n UART0 init...");

        SCU_APBPeriphClockConfig(__UART0, ENABLE);  // Enable the UART0 Clock

        Uart0Baudrate = BAUD_RATE + ((BAUD_RATE * 2)/100);   // MK3Mag baudrate is a litlle bit higher...

        /* UART0 configured as follow:
          - Word Length = 8 Bits
          - One Stop Bit
          - No parity
          - BaudRate = 57600 baud
          - Hardware flow control Disabled
          - Receive and transmit enabled
          - Receive and transmit FIFOs are Disabled
    */

        UART_StructInit(&UART_InitStructure);
    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 =                          Uart0Baudrate;
    UART_InitStructure.UART_HardwareFlowControl =       UART_HardwareFlowControl_None;
    UART_InitStructure.UART_Mode =                                      UART_Mode_Tx_Rx;
    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);     // reset uart 0 to default
    UART_Init(UART0, &UART_InitStructure);  // initialize uart 0

        // enable uart 0 interrupts selective
    UART_ITConfig(UART0, UART_IT_Receive | UART_IT_ReceiveTimeOut  /*| UART_IT_FrameError*/, ENABLE);
        UART_Cmd(UART0, ENABLE); // enable uart 0
        // configure the uart 0 interupt line as an IRQ with priority 10 (0 is highest)
        VIC_Config(UART0_ITLine, VIC_IRQ, 10);
        // enable the uart 0 IRQ
        VIC_ITCmd(UART0_ITLine, ENABLE);
        UART0_Connect_to_MKGPS();
        SerialPutString("ok");
}

/**************************************************************/
/*         Transmit tx buffer via debug uart                  */
/**************************************************************/
void UART0_Transmit(void)
{
        u8 tmp_tx;
        // if something has to be send and the txd fifo is not full
        if((!txd_complete0) && (UART_GetFlagStatus(UART0, UART_FLAG_TxFIFOFull) == RESET))
        {
                tmp_tx = txd_buffer0[ptr_txd_buffer0]; // read byte from txd buffer
                // if terminating character or end of txd buffer reached
                if((tmp_tx == '\r') || (ptr_txd_buffer0 == TXD_BUFFER_LEN))
                {
                        ptr_txd_buffer0 = 0;    // reset txd buffer pointer
                        txd_complete0 = TRUE;// set complete flag
                }
                UART_SendData(UART0, tmp_tx); // put character to txd fifo
                // set pointer to next byte
                ptr_txd_buffer0++;
        }
}

/**************************************************************/
/* Add CRC and initiate transmission via debug uart           */
/**************************************************************/
void AddCRC0(u16 datalen)
{
        u16 tmpCRC = 0, i;
        for(i = 0; i < datalen; i++)
        {
                tmpCRC += txd_buffer0[i];
        }
        tmpCRC %= 4096;
        txd_buffer0[i++] = '=' + tmpCRC / 64;
        txd_buffer0[i++] = '=' + tmpCRC % 64;
        txd_buffer0[i++] = '\r';

        ptr_txd_buffer0 = 0;
        txd_complete0 = FALSE;
        UART_SendData(UART0, txd_buffer0[ptr_txd_buffer0++]);   // send first byte, to be continued in the txd irq
}



/**************************************************************/
/* Code output data                                           */
/**************************************************************/
void SendOutData0(u8 cmd, u8 Address, u8 numofbuffers , ...) //u8 *data, u8 len, ....
{
        va_list ap;

        u16 pt = 0;
        u8 a,b,c;
        u8 ptr = 0;

        u8* pdata = NULL;
        int len = 0;

        txd_buffer0[pt++] = '#';                                // Start character
        txd_buffer0[pt++] = 'a' + Address;      // Address (a=0; b=1,...)
        txd_buffer0[pt++] = cmd;                                // Command

        va_start(ap, numofbuffers);
        if(numofbuffers)
        {
                pdata = va_arg(ap, u8*);
                len = va_arg(ap, int);
                ptr = 0;
                numofbuffers--;
        }
        while(len)
        {
                if(len)
                {
                        a = pdata[ptr++];
                        len--;
                        if((!len) && numofbuffers) // try to jump to next buffer
                        {
                                pdata = va_arg(ap, u8*);
                                len = va_arg(ap, int);
                                ptr = 0;
                                numofbuffers--;
                        }
                }
                else a = 0;
                if(len)
                {
                        b = pdata[ptr++];
                        len--;
                        if((!len) && numofbuffers) // try to jump to next buffer
                        {
                                pdata = va_arg(ap, u8*);
                                len = va_arg(ap, int);
                                ptr = 0;
                                numofbuffers--;
                        }
                }
                else b = 0;
                if(len)
                {
                        c = pdata[ptr++];
                        len--;
                        if((!len) && numofbuffers) // try to jump to next buffer
                        {
                                pdata = va_arg(ap, u8*);
                                len = va_arg(ap, int);
                                ptr = 0;
                                numofbuffers--;
                        }
                }
                else c = 0;
                txd_buffer0[pt++] = '=' + (a >> 2);
                txd_buffer0[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4));
                txd_buffer0[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6));
                txd_buffer0[pt++] = '=' + ( c & 0x3f);
        }
        va_end(ap);
        AddCRC0(pt);    // add checksum after data block and initates the transmission
}


/********************************************************/
/*            UART0 Interrupt Handler                   */
/********************************************************/
void UART0_IRQHandler(void)
{
        u8 c;
        /*
        static u8 frame_error_cnt = 0;
       
    // use frame error to adjust baudrate for MK3MAG
        if((UART_GetITStatus(UART0, UART_IT_FrameError) == SET))
        {
           u8 msg[20];
           frame_error_cnt++;
         
           if (frame_error_cnt == 200)
           {
            frame_error_cnt = 0;
            VIC_ITCmd(UART0_ITLine, DISABLE);
           
            Uart0MK3MagBaudrate +=100;
            if (Uart0MK3MagBaudrate > (BAUD_RATE+1000)) Uart0MK3MagBaudrate = BAUD_RATE;
           
            Uart0Baudrate = Uart0MK3MagBaudrate;
           
            SerialPutString(" adjusting UART0 baudrate to");
            sprintf(msg, " %d baud...", Uart0MK3MagBaudrate); SerialPutString(msg);
           
                while(UART_GetFlagStatus(UART0, UART_FLAG_RxFIFOEmpty) != SET)
                        {
                                c =  UART_ReceiveData(UART0);
                        }
       
            UART_ClearITPendingBit(UART0, UART_IT_FrameError);                  // clear frame error interrupt flag
           
            UART0_Init();
            return;
         }
 
        }*/

        // if receive irq or receive timeout irq has occured
        if((UART_GetITStatus(UART0, UART_IT_Receive) != RESET) || (UART_GetITStatus(UART0, UART_IT_ReceiveTimeOut) != RESET) )
        {
                UART_ClearITPendingBit(UART0, UART_IT_Receive);                 // clear receive interrupt flag
                UART_ClearITPendingBit(UART0, UART_IT_ReceiveTimeOut);  // clear receive timeout interrupt flag

                // if debug UART is UART0
                if (DebugUART == UART0)
                {       // forward received data to the UART1 tx buffer
                        while(UART_GetFlagStatus(UART0, UART_FLAG_RxFIFOEmpty) != SET)
                        {
                                // wait for space in the tx buffer of the UART1
                                while(UART_GetFlagStatus(UART1, UART_FLAG_TxFIFOFull) == SET) {};
                                // move the byte from the rx buffer of UART0 to the tx buffer of UART1
                                UART_SendData(UART1, UART_ReceiveData(UART0));
                        }
                }
                else // UART0 is not the DebugUART (normal operation)
                {
                        // repeat until no byte is in the RxFIFO
                        while (UART_GetFlagStatus(UART0, UART_FLAG_RxFIFOEmpty) != SET)
                        {  
                                c = UART_ReceiveData(UART0); // get byte from rx fifo
                                switch(UART0_Muxer)
                                {
                                        case UART0_MKGPS:
                                                UBX_Parser(c); // if connected to GPS forward byte to ubx parser
                                                break;
                                        case UART0_MK3MAG:
                                                // ignore any byte send from MK3MAG
                                                break;
                                        case UART0_UNDEF:
                                        default:
                                                // ignore the byte from unknown source
                                                break;
                                } // eof switch(UART0_Muxer)
                        } // eof while
                }  // eof UART0 is not the DebugUART
        } // eof receive irq or receive timeout irq
}