Subversion Repositories NaviCtrl

Rev

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

/*#######################################################################################*/
/* !!! THIS IS NOT FREE SOFTWARE !!!                                                     */
/*#######################################################################################*/
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + www.MikroKopter.com
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Software Nutzungsbedingungen (english version: see below)
// + der Fa. HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland - nachfolgend Lizenzgeber genannt -
// + Der Lizenzgeber räumt dem Kunden ein nicht-ausschließliches, zeitlich und räumlich* unbeschränktes Recht ein, die im den
// + Mikrocontroller verwendete Firmware für die Hardware Flight-Ctrl, Navi-Ctrl, BL-Ctrl, MK3Mag & PC-Programm MikroKopter-Tool
// + - nachfolgend Software genannt - nur für private Zwecke zu nutzen.
// + Der Einsatz dieser Software ist nur auf oder mit Produkten des Lizenzgebers zulässig.
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Die vom Lizenzgeber gelieferte Software ist urheberrechtlich geschützt. Alle Rechte an der Software sowie an sonstigen im
// + Rahmen der Vertragsanbahnung und Vertragsdurchführung überlassenen Unterlagen stehen im Verhältnis der Vertragspartner ausschließlich dem Lizenzgeber zu.
// + Die in der Software enthaltenen Copyright-Vermerke, Markenzeichen, andere Rechtsvorbehalte, Seriennummern sowie
// + sonstige der Programmidentifikation dienenden Merkmale dürfen vom Kunden nicht verändert oder unkenntlich gemacht werden.
// + Der Kunde trifft angemessene Vorkehrungen für den sicheren Einsatz der Software. Er wird die Software gründlich auf deren
// + Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
// + Die Haftung des Lizenzgebers wird - soweit gesetzlich zulässig - begrenzt in Höhe des typischen und vorhersehbaren
// + Schadens. Die gesetzliche Haftung bei Personenschäden und nach dem Produkthaftungsgesetz bleibt unberührt. Dem Lizenzgeber steht jedoch der Einwand
// + des Mitverschuldens offen.
// + Der Kunde trifft angemessene Vorkehrungen für den Fall, dass die Software ganz oder teilweise nicht ordnungsgemäß arbeitet.
// + Er wird die Software gründlich auf deren Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
// + Der Kunde wird er seine Daten vor Einsatz der Software nach dem Stand der Technik sichern.
// + Der Kunde ist darüber unterrichtet, dass der Lizenzgeber seine Daten im zur Vertragsdurchführung erforderlichen Umfang
// + und auf Grundlage der Datenschutzvorschriften erhebt, speichert, verarbeitet und, sofern notwendig, an Dritte übermittelt.
// + *) Die räumliche Nutzung bezieht sich nur auf den Einsatzort, nicht auf die Reichweite der programmierten Software.
// + #### ENDE DER NUTZUNGSBEDINGUNGEN ####'
// +  Hinweis: Informationen über erweiterte Nutzungsrechte (wie z.B. Nutzung für nicht-private Zwecke) sind auf Anfrage per Email an info(@)hisystems.de verfügbar.
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Software LICENSING TERMS
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + of HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland, Germany - the Licensor -
// + The Licensor grants the customer a non-exclusive license to use the microcontroller firmware of the Flight-Ctrl, Navi-Ctrl, BL-Ctrl, and MK3Mag hardware
// + (the Software) exclusively for private purposes. The License is unrestricted with respect to time and territory*.
// + The Software may only be used with the Licensor's products.
// + The Software provided by the Licensor is protected by copyright. With respect to the relationship between the parties to this
// + agreement, all rights pertaining to the Software and other documents provided during the preparation and execution of this
// + agreement shall be the property of the Licensor.
// + The information contained in the Software copyright notices, trademarks, other legal reservations, serial numbers and other
// + features that can be used to identify the program may not be altered or defaced by the customer.
// + The customer shall be responsible for taking reasonable precautions
// + for the safe use of the Software. The customer shall test the Software thoroughly regarding its suitability for the
// + intended purpose before implementing it for actual operation. The Licensor's liability shall be limited to the extent of typical and
// + foreseeable damage to the extent permitted by law, notwithstanding statutory liability for bodily injury and product
// + liability. However, the Licensor shall be entitled to the defense of contributory negligence.
// + The customer will take adequate precautions in the case, that the software is not working properly. The customer will test
// + the software for his purpose before any operational usage. The customer will backup his data before using the software.
// + The customer understands that the Licensor collects, stores and processes, and, where required, forwards, customer data
// + to third parties to the extent necessary for executing the agreement, subject to applicable data protection and privacy regulations.
// + *) The territory aspect only refers to the place where the Software is used, not its programmed range.
// + #### END OF LICENSING TERMS ####
// + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de.
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//#include <stdio.h>
#include "91x_lib.h"
#include "config.h"
#include "uart1.h"
#include "mkprotocol.h"

// the tx buffer
#define UART2_TX_BUFFER_LEN  150
u8 UART2_tbuffer[UART2_TX_BUFFER_LEN];
Buffer_t UART2_tx_buffer;

/********************************************************/
/*                  Initialize UART2                    */
/********************************************************/
void UART2_Init(void)
{
    UART_InitTypeDef UART_InitStructure;
        GPIO_InitTypeDef GPIO_InitStructure;

//      UART1_PutString("\r\n UART2 init...");

        // initialize txd buffer
        Buffer_Init(&UART2_tx_buffer, UART2_tbuffer, UART2_TX_BUFFER_LEN);

        SCU_APBPeriphClockConfig(__UART2, ENABLE);  // Enable the UART2 Clock

        SCU_APBPeriphClockConfig(__GPIO5, ENABLE);  // Enable the GPIO5 Clock
    /*Configure UART2_Rx pin GPIO5.2*/
        GPIO_StructInit(&GPIO_InitStructure);
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinInput;
    GPIO_InitStructure.GPIO_Pin =                       GPIO_Pin_2;
    GPIO_InitStructure.GPIO_Type =                      GPIO_Type_PushPull;
    GPIO_InitStructure.GPIO_IPInputConnected =  GPIO_IPInputConnected_Enable;
    GPIO_InitStructure.GPIO_Alternate =         GPIO_InputAlt1; // UART2_RxD
    GPIO_Init(GPIO5, &GPIO_InitStructure);

        SCU_APBPeriphClockConfig(__GPIO3, ENABLE);  // Enable the GPIO3 Clock
    /*Configure UART2_Tx pin GPIO3.0*/
        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_Alternate =         GPIO_OutputAlt2; // UART2_TX
    GPIO_Init(GPIO3, &GPIO_InitStructure);


        /* UART2 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 =                          UART2_BAUD_RATE;
    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(UART2);     // reset uart 2 to default
    UART_Init(UART2, &UART_InitStructure);  // initialize uart 2

        // enable uart 2 interrupts selective
    UART_ITConfig(UART2, UART_IT_Receive | UART_IT_ReceiveTimeOut, ENABLE);
        UART_Cmd(UART2, ENABLE); // enable uart 2
        // configure the uart 2 interupt line
        VIC_Config(UART2_ITLine, VIC_IRQ, PRIORITY_UART2);
        // enable the uart 2 IRQ
        VIC_ITCmd(UART2_ITLine, ENABLE);

//      UART1_PutString("ok");
}


void UART2_Deinit(void)
{
        GPIO_InitTypeDef GPIO_InitStructure;

        UART1_PutString("\r\n UART2 deinit...");
        VIC_ITCmd(UART2_ITLine, DISABLE); // disable the uart 2 IRQ
        UART_Cmd(UART2, DISABLE); // disable uart 2
        UART_DeInit(UART2);     // reset uart 0 to default

        SCU_APBPeriphClockConfig(__UART2, DISABLE);  // disable the UART2 Clock

        SCU_APBPeriphClockConfig(__GPIO5, ENABLE);
        // unmap UART2 from FC
        // set port pin 5.2 (serial data from FC) to input and disconnect from IP
        GPIO_StructInit(&GPIO_InitStructure);
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinInput;
    GPIO_InitStructure.GPIO_Pin =                       GPIO_Pin_2;
    GPIO_InitStructure.GPIO_Type =                      GPIO_Type_PushPull;
    GPIO_InitStructure.GPIO_IPInputConnected =  GPIO_IPInputConnected_Disable;
    GPIO_InitStructure.GPIO_Alternate =         GPIO_InputAlt1;
    GPIO_Init(GPIO5, &GPIO_InitStructure);

        SCU_APBPeriphClockConfig(__GPIO3, ENABLE);
        // set port pin 3.0 (serial data to FC) to input and disconnect from IP
        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_IPInputConnected =      GPIO_IPInputConnected_Disable;
    GPIO_InitStructure.GPIO_Alternate =         GPIO_InputAlt1;
    GPIO_Init(GPIO3, &GPIO_InitStructure);

        UART1_PutString("ok");
}

/********************************************************/
/*            UART2 Interrupt Handler                   */
/********************************************************/
void UART2_IRQHandler(void)
{


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

                // if debug UART is UART2
                if (DebugUART == UART2)
                {       // forward received data to the UART1 tx buffer
                        while(UART_GetFlagStatus(UART2, 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 UART2 to the tx buffer of UART1
                                UART_SendData(UART1, UART_ReceiveData(UART2));
                        }
                }
                else
                {
                        // ignore serial data from the FC
                        while(UART_GetFlagStatus(UART2, UART_FLAG_RxFIFOEmpty) != SET)
                        {
                                UART_ReceiveData(UART2);
                        }
                }
        } // eof receive irq or receive timeout irq
       

        VIC1->VAR = 0xFF; // write any value to VIC1 Vector address register
}

/**************************************************************/
/*         Transmit tx buffer via uart2                       */
/**************************************************************/
void UART2_Transmit(void)
{
        u8 tmp_tx;
        if(DebugUART == UART2) return; // no data output if debug uart is rederected to UART2
        // if something has to be send and the txd fifo is not full
        if((UART2_tx_buffer.Locked == TRUE) && (UART_GetFlagStatus(UART2, UART_FLAG_TxFIFOFull) == RESET))
        {
                tmp_tx = UART2_tx_buffer.pData[UART2_tx_buffer.Position++]; // read next byte from txd buffer
                UART_SendData(UART2, tmp_tx); // put character to txd fifo
                // if terminating character or end of txd buffer reached
                if((tmp_tx == '\r') || (UART2_tx_buffer.Position == UART2_tx_buffer.Size))
                {
                        Buffer_Clear(&UART2_tx_buffer);
                }
        }
}

/**************************************************************/
/* Send the answers to incomming commands at the uart2        */
/**************************************************************/
void UART2_TransmitTxData(void)
{
        if(DebugUART == UART2) return;
        UART2_Transmit(); // output pending bytes in tx buffer
}


void UART2_ProcessRxData(void)
{

}