Subversion Repositories NaviCtrl

Rev

Rev 23 | Go to most recent revision | 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"

//________________________________________________________________________________________________________________________________________
// Module name:                 fat16.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
//........................................................................................................................................
// 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);
//........................................................................................................................................
// ext. functions:              extern u8 SDC_GetSector  (unsigned long,u8 *);         
//                                              extern u8 SDC_PutSector (unsigned long,u8 *);
//........................................................................................................................................
//
// URL:                                 www.Mikro-Control.de
// mailto:                              stephan.busker@mikro-control.de
//________________________________________________________________________________________________________________________________________




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

void SSC_Init(void)
{

  SerialPutString("SPI1 init...");
 
  GPIO_InitTypeDef GPIO_InitStructure;
  SSP_InitTypeDef   SSP_InitStructure;

  SCU_APBPeriphClockConfig(__SSP1 ,ENABLE);
   
 // GPIO_DeInit(GPIO2);
  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_IPConnected = GPIO_IPConnected_Enable;
  GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt2;
  GPIO_Init (GPIO3, &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_IPConnected = GPIO_IPConnected_Disable;
  GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt1;
  GPIO_Init (GPIO5, &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_IPConnected = GPIO_IPConnected_Enable;
  GPIO_InitStructure.GPIO_Alternate = GPIO_InputAlt1;
  GPIO_Init (GPIO3, &GPIO_InitStructure);
 
  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;
  // Hier muss die Baudrate noch richtig eingestellt werden (mit Prescaler)
  SSP_InitStructure.SSP_ClockRate = 5;
  SSP_InitStructure.SSP_ClockPrescaler = 8;
  SSP_Init(SSP1, &SSP_InitStructure);
  SSC_Disable();
  SSP_Cmd(SSP1, ENABLE);
/*      MMC_Direction_REG &=~(1<<SPI_DI);                                                               // Set the direction of the ssc-port
        MMC_Direction_REG |= (1<<SPI_Clock);                                                    //      _______                          _______
        MMC_Direction_REG |= (1<<SPI_DO);                                                               // CS              \________________________/
        MMC_Direction_REG |= (1<<MMC_Chip_Select);                                              //
        MMC_Direction_REG |= (1<<SPI_SS);                                                               //                 ___     ___     ___
                                                                                                                                        // clk  __________/   \___/   \___/   \_________
                                                                                                                                        //
                                                                                                        //               ___             ___
                                                                                                                                        // data_________/   \___________/   \___________

                                                                                                                                        // initialise ssc, clock = Idel low
                                                                                                                                        // devide clock by 32
        SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1)|(1<<CPOL);                          // Enable SSC in mastermode, invert clockpolarity (idle high)  
        SPSR = SPSR|(1<<SPI2X);*/

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


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

u8 SSC_GetChar (void)
{
        u8 Byte = 0;
//      SerialPutString("\n\rGetChar.");
        SSP_SendData(SSP1, 0xFF);
        while(SSP_GetFlagStatus(SSP1, SSP_FLAG_TxFifoEmpty) != SET);
        Byte = SSP_ReceiveData(SSP1);
/*      SPDR = 0x00;                                                                            // read one byte of data from the SSC
        while(!(SPSR & (1<<SPIF))){};                                           // wait until the data has been read.
        Byte = SPDR;
*/

  //  sprintf(text," %02X ", Byte);  SerialPutString(text);SerialPutString("done.");
        return (Byte);

}

//________________________________________________________________________________________________________________________________________
void SSC_ClearRxFifo (void)
{
  //    SerialPutString("\n\rClr RxFifo ");
        while(SSP_GetFlagStatus(SSP1, SSP_FLAG_TxFifoEmpty) != SET);
        while(SSP_GetFlagStatus(SSP1, SSP_FLAG_RxFifoNotEmpty) == SET) SSP_ReceiveData(SSP1);
//      SerialPutString("done.");
}
//________________________________________________________________________________________________________________________________________
// Funtion:     SSC_PutChar(u8 Byte);
//
// Description: This function writes one byte to the SSC
//                             
//
// Returnvalue: none
//________________________________________________________________________________________________________________________________________

void SSC_PutChar (u8 Byte)
{
        //SerialPutString("\n\rPutChar.");      sprintf(text," %02X ", Byte);  SerialPutString(text);
        while(SSP_GetFlagStatus(SSP1, SSP_FLAG_TxFifoNotFull) != SET);
        SSP_SendData(SSP1, Byte);
        //SerialPutString("done.");
       
}


//________________________________________________________________________________________________________________________________________
// Funtion:     SSC_Disable(void);
//
// Description: This function enables chipselect of the sdcard (active low)
//                             
//
// Returnvalue: none
//________________________________________________________________________________________________________________________________________

void SSC_Disable(void)
{
        //MMC_Write &= ~(1<<MMC_Chip_Select);                           // disable chipselect of the sdcard (active low).
        GPIO_WriteBit(GPIO5, GPIO_Pin_4 , Bit_SET);
}




//________________________________________________________________________________________________________________________________________
// Funtion:     SSC_Enable(void);
//
// Description: This function disables chipselect of the sdcard (active low)
//                             
//
// Returnvalue: none
//________________________________________________________________________________________________________________________________________

void SSC_Enable(void)
{
        //MMC_Write |= (1<<MMC_Chip_Select);                            // enable chipselect of the sdcard (active low).
        GPIO_WriteBit(GPIO5, GPIO_Pin_4 , Bit_RESET);
}