Subversion Repositories NaviCtrl

Rev

Rev 869 | 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 "91x_lib.h"
#include "config.h"
#include "main.h"

//________________________________________________________________________________________________________________________________________
// Module name:                 ssc.c
// Compiler used:               avr-gcc 3.4.5
// Last Modifikation:   24.07.2007
// Version:                             1.02
// Authors:                             Stephan Busker  
// Description:                 Source files for access to the synchrnous serial channel.
//                                              Copyright (C) 2007 Stephan Busker
//........................................................................................................................................
// ext. Functions:              extern void                     SSC_Init(void);        
//                                              extern u8                               SSC_GetChar (void);
//                                              extern void                     SSC_PutChar (u8 Byte);
//                                              extern void                     SSC_Disable(void);
//                                              extern void                     SSC_Enable(void);
//                                              extern void                             SSC_ClearRxFifo();
//........................................................................................................................................
// URL:                                 www.Mikro-Control.de
// mailto:                              stephan.busker@mikro-control.de
//________________________________________________________________________________________________________________________________________


u16 SPI_Speed = 2000;
//________________________________________________________________________________________________________________________________________
// Function:    SSC_Enable(void);
//
// Description: This function enables chipselect of the sdcard (active low)
//                             
//
// Returnvalue: none
//________________________________________________________________________________________________________________________________________

void SSC_Enable(void)
{
        // enable chipselect of the sd-card (P5.4 -> SD-CS, active low).
        GPIO_WriteBit(GPIO5, GPIO_Pin_4 , Bit_RESET);
}
 
//________________________________________________________________________________________________________________________________________
// Function:    SSC_Disable(void);
//
// Description: This function disables chipselect of the sdcard (active low)
//                             
//
// Returnvalue: none
//________________________________________________________________________________________________________________________________________

void SSC_Disable(void)
{
        // disable chipselect of the sd-card (P5.4 -> SD-CS, active low).
        GPIO_WriteBit(GPIO5, GPIO_Pin_4 , Bit_SET);
}


//________________________________________________________________________________________________________________________________________
// Function:    SSC_Init(void);
//
// Description: This function initialises the synchronus serial channel to the sdcard.
//                             
//
// Returnvalue: none
//________________________________________________________________________________________________________________________________________

void SSC_Speed(u32 speed)
{
        SSP_InitTypeDef         SSP_InitStructure;

        SPI_Speed = speed;

    SSP_Cmd(SSP1, DISABLE);
        SSP_DeInit(SSP1);
        SSP_StructInit(&SSP_InitStructure);
        SSP_InitStructure.SSP_FrameFormat = SSP_FrameFormat_Motorola;
        SSP_InitStructure.SSP_Mode = SSP_Mode_Master;
        SSP_InitStructure.SSP_CPHA = SSP_CPHA_1Edge;
        SSP_InitStructure.SSP_CPOL = SSP_CPOL_Low;
        // Set Baud Rate (Prescaler)
        // bit rate is BRCLK/SSP_ClockPrescaler/(1+SSP_ClockRate))
        // With MSCLK = 48MHz = BRCLK we get for the SPICLK = 48Mhz / 20 / (1+5) = 400 kHz
        SSP_InitStructure.SSP_ClockRate = 5; //5
        SSP_InitStructure.SSP_ClockPrescaler = (48000 / (5+1)) / speed;
        SSP_Init(SSP1, &SSP_InitStructure);
        SSP_Cmd(SSP1, ENABLE);
}


void SSC_Init(void)
{
        GPIO_InitTypeDef        GPIO_InitStructure;
//      WIU_InitTypeDef         WIU_InitStructure;
        // enable APB clock for SPI1
        SCU_APBPeriphClockConfig(__SSP1 ,ENABLE);
        // configure P5.4 -> SD-CS as an output pin
        GPIO_StructInit(&GPIO_InitStructure);
        GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput;
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
        GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;
        GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Disable;
        GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt1;
        GPIO_Init (GPIO5, &GPIO_InitStructure);
        // configure P3.4 -> SCK1 and P3.6 -> MOSI1 as an output pin
        GPIO_StructInit(&GPIO_InitStructure);
        GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput;
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_6;
        GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;
        GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Enable;
        GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt2;
        GPIO_Init (GPIO3, &GPIO_InitStructure);
        // configure P3.5 <- MISO1 as an input pin
        GPIO_StructInit(&GPIO_InitStructure);
        GPIO_InitStructure.GPIO_Direction = GPIO_PinInput;
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
        GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull;
        GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Enable;
        GPIO_InitStructure.GPIO_Alternate = GPIO_InputAlt1;
        GPIO_Init (GPIO3, &GPIO_InitStructure);
        // configure SPI1
    SSC_Speed(300);
        SSC_Disable();
        SSP_Cmd(SSP1, ENABLE);

        // Configure SD_SWITCH at pin GPIO5.3 as an external irq 11

        // configure the port
        SCU_APBPeriphClockConfig(__GPIO5, ENABLE); // Enable the GPIO5 Clock    
    GPIO_InitStructure.GPIO_Direction =         GPIO_PinInput;
    GPIO_InitStructure.GPIO_Pin =                       GPIO_Pin_3;
        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);
       
/*
        // configure the EXT11 interrupt line (P5.3) as an IRQ with the lowest priority
        SCU_APBPeriphClockConfig(__WIU, ENABLE);
        WIU_Cmd(ENABLE);
        WIU_ClearITPendingBit(WIU_Line11);
        //WIU_DeInit();
        WIU_InitStructure.WIU_TriggerEdge = WIU_FallingEdge;
        WIU_InitStructure.WIU_Line = WIU_Line11;
        WIU_Init(&WIU_InitStructure);
        // The  EXTIT1_IRQHandler() is called every time the SD-Switch is activated (falling edge)
        // by inserting an sd-card
        SCU_WakeUpLineConfig(11);
    VIC_Config(EXTIT1_ITLine, VIC_IRQ, PRIORITY_SDSWITCH);
    VIC_ITCmd(EXTIT1_ITLine, ENABLE);
*/

}

void SSC_Deinit(void)
{
        GPIO_InitTypeDef        GPIO_InitStructure;

        SSP_Cmd(SSP1, DISABLE);
        SSP_DeInit(SSP1);

        // configure P5.4 -> SD-CS as an input pin
        GPIO_InitStructure.GPIO_Direction = GPIO_PinInput;
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
        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);
        // configure P3.4 -> SCK1 and P3.6 -> MOSI1 as an input pin
        GPIO_InitStructure.GPIO_Direction = GPIO_PinInput;
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_6;
        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);
        // configure P3.5 <- MISO1 as an input pin
        GPIO_InitStructure.GPIO_Direction = GPIO_PinInput;
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
        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);

        // disable APB clock for SPI1
        SCU_APBPeriphClockConfig(__SSP1 ,DISABLE);

        // configure the port of the SDC-Switch
        SCU_APBPeriphClockConfig(__GPIO5, ENABLE); // Enable the GPIO5 Clock    
    GPIO_InitStructure.GPIO_Direction =         GPIO_PinInput;
    GPIO_InitStructure.GPIO_Pin =                       GPIO_Pin_3;
        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);
}


//________________________________________________________________________________________________________________________________________
// Function:    SSC_GetChar(void);
//
// Description: This function reads one byte from the SSC
//                             
//
// Returnvalue: the byte received.
//________________________________________________________________________________________________________________________________________

u8 SSC_GetChar (void)
{
        u8 Byte = 0;
        while(SSP_GetFlagStatus(SSP1, SSP_FLAG_TxFifoNotFull) != SET);  // wait for space in the tx fifo
        SSP_SendData(SSP1, 0xFF);// send dymmy byte (0xFF) as master to receive a byte from the slave
        while(SSP_GetFlagStatus(SSP1, SSP_FLAG_TxFifoEmpty) != SET); // wait for the byte to be sent
        Byte = SSP_ReceiveData(SSP1); // read the byte transmitted from the slave
        return (Byte);
}

//________________________________________________________________________________________________________________________________________
void SSC_ClearRxFifo (void)
{
        // wait that the tx fifo is empty
        while(SSP_GetFlagStatus(SSP1, SSP_FLAG_TxFifoEmpty) != SET);
        // then empty the rx fifo by reading all the bytes that are available
        while(SSP_GetFlagStatus(SSP1, SSP_FLAG_RxFifoNotEmpty) == SET) SSP_ReceiveData(SSP1);
}
//________________________________________________________________________________________________________________________________________
// Function:    SSC_PutChar(u8 Byte);
//
// Description: This function writes one byte to the SSC
//                             
//
// Returnvalue: none
//________________________________________________________________________________________________________________________________________

void SSC_PutChar (u8 Byte)
{
        // wait for some space in the tx fifo
        while(SSP_GetFlagStatus(SSP1, SSP_FLAG_TxFifoNotFull) != SET);
        // put the byte to send in the tx fifo
        SSP_SendData(SSP1, Byte);
}