Subversion Repositories NaviCtrl

Compare Revisions

Ignore whitespace Rev 22 → Rev 23

/tags/V0.1/sdc.c
0,0 → 1,295
/*#######################################################################################*/
/* !!! THIS IS NOT FREE SOFTWARE !!! */
/*#######################################################################################*/
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Copyright (c) 2008 Ingo Busker, Holger Buss
// + Nur für den privaten Gebrauch
// + 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 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 this software (or part of it) to systems (other than 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: mmc.c
// Compiler used: avr-gcc 3.4.5
// Last Modifikation: 24.07.2007
// Version: 1.05
// Authors: Stephan Busker
// Description: Source files for connecting to an sdcard using the SSC
//
//........................................................................................................................................
// Functions: u8 SDC_init(void);
// u8 SDC_PutCommand (u8 *CMD);
// u8 SDC_PutSector(u32 addr,u8 *Buffer);
// u8 SDC_GetSector(u32 addr,u8 *Buffer);
// void SDC_GetBlock(u8 *CMD,u8 *Buffer,u16 Bytes);
//
////........................................................................................................................................
// ext. functions: extern void SSC_Init(void);
// extern u8 SSC_GetChar (void);
// extern void SSC_PutChar (u8);
// extern void SSC_Enable(void);
// extern void SSC_Disable(void);
//........................................................................................................................................
//
// URL: www.Mikro-Control.de
// mailto: stephan.busker@mikro-control.de
//________________________________________________________________________________________________________________________________________
 
 
 
 
//________________________________________________________________________________________________________________________________________
// Funtion: SDC_Init(void);
//
// Description: This function initialises the SDCard to spi-mode.
//
//
// Returnvalue: the function returns 0 if the initialisation was successfull otherwise the function returns an errorcode.
//________________________________________________________________________________________________________________________________________
 
u8 SDC_Init(void)
{
u16 Timeout = 0;
u8 CMD[] = {0x40,0x00,0x00,0x00,0x00,0x95};
 
u8 b;
 
SSC_Init(); // Initialise SSC to transmit data to the sdcard.
SerialPutString("Init SD...");
// Delay_ms(10);
 
for (b = 0;b<0x0f;b++) // sending 74Clocks brings the sdcard into spimode.
{
SSC_PutChar(0xff);
}
SerialPutString("reset...");
while(SDC_PutCommand (CMD) !=1) // Sending CMD0 (Reset) to the sdcard.
{
if (Timeout++ > 200)
{
return(1);
}
}
SerialPutString("ok.");
Timeout = 0;
CMD[0] = 0x41;
CMD[5] = 0xFF;
while( SDC_PutCommand (CMD) !=0) // Sending CMD1 to the sdcard.
{
if (Timeout++ > 100)
{
return(2);
}
}
 
SSC_Disable(); // disable sdcard.
return(0);
}
 
 
 
 
//________________________________________________________________________________________________________________________________________
// Funtion: SDC_PutCommand(* CMD);
//
// Description: This function initialises the SDCard to spi-mode.
//
//
// Returnvalue: the function returns 0 if the initialisation was successfull otherwise the function returns an errorcode.
//________________________________________________________________________________________________________________________________________
 
u8 SDC_PutCommand (u8 *CMD)
{
u8 tmp = 0xff;
u16 Timeout = 0;
u16 a;
SSC_ClearRxFifo();
SSC_Disable(); // disable chipselect
SSC_PutChar(0xFF); // Send 8 Clocks to the sdcard while card is not selected.
SSC_Enable(); // enable chipselect.
 
// if (*CMD == 0x41) Delay_ms(10); // if command is CMD0 generate a short delay.
for (a = 0;a<0x06;a++) // send the command sequence to the sdcard (6 bytes)
{
SSC_PutChar(*CMD++);
}
SSC_ClearRxFifo();
while (tmp == 0xff) // Wait for response from sdcard.
{
tmp = SSC_GetChar();
if (Timeout++ > 200)
{
break; // or timeout.
}
}
 
return(tmp);
}
 
 
 
 
//________________________________________________________________________________________________________________________________________
// Funtion: SDC_PutSector(void);
//
// Description: This function writes one sector of data to the SSC
//
//
// Returnvalue: none
//________________________________________________________________________________________________________________________________________
 
u8 SDC_PutSector(u32 addr,u8 *Buffer)
{
u8 tmp;
u8 CMD[] = {0x58,0x00,0x00,0x00,0x00,0xFF};
u16 a;
addr = addr << 9; // convert sectoradress to byteadress
CMD[1] = ((addr & 0xFF000000) >>24 );
CMD[2] = ((addr & 0x00FF0000) >>16 );
CMD[3] = ((addr & 0x0000FF00) >>8 );
 
tmp = SDC_PutCommand (CMD); // send command to sdcard.
if (tmp != 0)
{
return(tmp);
}
SSC_ClearRxFifo();
for (a=0;a<100;a++) // wait until sdcard is ready
{
SSC_GetChar();
}
SSC_PutChar(0xFE); // send start of header to the SSC
for (a=0;a<512;a++) // transmitt one sector (normaly 512bytes) of data to the sdcard.
{
SSC_PutChar(*Buffer++);
}
SSC_PutChar(0xFF); // write two bytes of crc to the sdcard (not used in spi-mode)
SSC_PutChar(0xFF);
SSC_ClearRxFifo();
while (SSC_GetChar() != 0xff){}; // wait untile the sdcard is ready.
SSC_Disable(); // disable sdcard.
 
return(0);
}
 
 
 
//________________________________________________________________________________________________________________________________________
// Funtion: SDC_GetSector(u32 addr,u8 *Buffer);
//
// Description: This function reads one sector of data from the SSC
//
//
// Returnvalue: none
//________________________________________________________________________________________________________________________________________
 
u8 SDC_GetSector(u32 addr,u8 *Buffer)
{
u8 CMD[] = {0x51,0x00,0x00,0x00,0x00,0xFF};
addr = addr << 9; // convert sectoradress to byteadress.
 
CMD[1] = ((addr & 0xFF000000) >>24 );
CMD[2] = ((addr & 0x00FF0000) >>16 );
CMD[3] = ((addr & 0x0000FF00) >>8 );
 
// sprintf(text,"\n\rGetSector: (%lu)", (u32)addr ); SerialPutString(text);
 
SDC_GetBlock(CMD,Buffer,512); // read specified sector from sdcard.
 
return(0);
}
 
 
 
//________________________________________________________________________________________________________________________________________
// Funtion: SDC_GetBlock(void);
//
// Description: This function reads one block of data of int bytes from the SSC.
//
//
// Returnvalue: the function returns 0 if the initialisation was successfull otherwise the function returns an errorcode.
//________________________________________________________________________________________________________________________________________
 
void SDC_GetBlock(u8 *CMD,u8 *Buffer,u16 Bytes)
{
if (SDC_PutCommand (CMD) != 0) // Send command to the sdcard.
{
return;
}
SSC_ClearRxFifo();
// SerialPutString("\n\rWait.");
while (SSC_GetChar() != 0xfe){}; // wait until the sdcard is ready to transmitt data.
// SerialPutString("Ok.");
u16 a;
for (a=0;a<Bytes;a++) // read the block from the SSC (normaly 512Bytes)
{
*Buffer++ = SSC_GetChar();
}
SSC_GetChar(); // Read two bytes CRC- checksum (not used in spi-mode)
SSC_GetChar();
SSC_Disable(); // disable sdcard.
}