##// END OF EJS Templates
just backup
just backup

File last commit:

r11:798f00e676fa default
r12:cc0fb1c881c0 default
Show More
main.c
312 lines | 5.5 KiB | text/x-c | CLexer
/*------------------------------------------------------------------------------
-- This file is a part of the libuc, microcontroler library
-- Copyright (C) 2012, Alexis Jeandet
--
-- This program is free software; you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation; either version 3 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program; if not, write to the Free Software
-- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-------------------------------------------------------------------------------
-- Author : Alexis Jeandet
-- Mail : alexis.jeandet@gmail.com
-------------------------------------------------------------------------------*/
#include "LPC17xx.h"
#include "bsp.h"
#include "core.h"
#include "uart.h"
#include "libucstrings.h"
#define __delay(t) for(volatile int i=0;i<t;i++);
#define ledspos1 LED1_OFF;LED2_ON;LED3_OFF;LED4_ON;LED5_OFF;LED6_ON;LED7_OFF;LED8_ON
#define ledspos2 LED1_ON;LED2_OFF;LED3_ON;LED4_OFF;LED5_ON;LED6_OFF;LED7_ON;LED8_OFF
void checkforsdcard();
void senddummyclocks(int n);
int sendCMD0();
int sendCMD8();
int sendACMD41(int arg);
int sendCMD55();
int sendCMD1();
int sendCMD58();
int main (void)
{
int i;
bsp_init();
uartsetbaudrate(UART0,38400);
checkforsdcard();
while(!bspsdcardpresent());
checkforsdcard();
__delay(1024*10);
senddummyclocks(74);
__delay(1024*100);
if(sendCMD0())
{
//__delay(1024*10);
if(sendCMD8())
{
// __delay(1024*10);
libucprintf("Maybe SD card V2 \n\r");
if(sendACMD41(1))
{
// __delay(1024*10);
i = sendCMD58();
if(0x40== (i & 0x40))
{
libucprintf("SD HC card \n\r");
}
else
{
libucprintf("SD card V2 \n\r");
}
}
else
{
libucprintf("Unknow card\n\r");
}
}
else
{
//libucprintf("Maybe SD card V1 \n\r");
if(sendACMD41(0))
{
libucprintf("SD card V1 \n\r");
}
else
{
if(sendCMD1())
{
libucprintf("MMC V3 card \n\r");
}
else
{
libucprintf("Unknow card\n\r");
}
}
}
}
__delay(1024);
while(1)
{
ledspos1;
__delay(1024*1024);
ledspos2;
__delay(1024*1024);
}
return 0;
}
int sendCMD0()
{
//libucprintf("Send CMD0\n\r");
int n=10;
char resp[8];
bspsdcardselect(1);
sspputc(SSP0,0x40);
sspputc(SSP0,0);
sspputc(SSP0,0);
sspputc(SSP0,0);
sspputc(SSP0,0);
sspputc(SSP0,0x95);
do
{sspgetnc(SSP0,resp,1);}
while(resp[0]!=1 && n--);
if(resp[0]==1)
{
//sspgetnc(SSP0,resp,8);
bspsdcardselect(0);
return 1;
}
else
{
//sspgetnc(SSP0,resp,8);
bspsdcardselect(0);
return 0;
}
}
int sendCMD1()
{
//libucprintf("Send CMD1\n\r");
int n=10;
char resp[8];
do{
bspsdcardselect(1);
sspputc(SSP0,0x41);
sspputc(SSP0,0);
sspputc(SSP0,0);
sspputc(SSP0,0);
sspputc(SSP0,0);
sspputc(SSP0,0x1);
do
{
sspgetnc(SSP0,resp,1);
}while((resp[0] &0x80) && n--);
n=10;
resp[0]=0xff;
}while(resp[0]>1);
if(resp[0]==0)
{
//sspgetnc(SSP0,resp,8);
bspsdcardselect(0);
return 1;
}
else
{
//sspgetnc(SSP0,resp,8);
bspsdcardselect(0);
return 0;
}
}
int sendCMD8()
{
//libucprintf("Send CMD8\n\r");
int n=10;
char resp[8];
bspsdcardselect(1);
sspputc(SSP0,0x48);
sspputc(SSP0,0);
sspputc(SSP0,0);
sspputc(SSP0,0x1);
sspputc(SSP0,0xAA);
sspputc(SSP0,0x87);
do
{sspgetnc(SSP0,resp,1);}
while(resp[0]!=1 && n--);
if(resp[0]==1)
{
//sspgetnc(SSP0,resp,8);
bspsdcardselect(0);
return 1;
}
else
{
//sspgetnc(SSP0,resp,8);
bspsdcardselect(0);
return 0;
}
}
int sendCMD55()
{
//libucprintf("Send CMD55\n\r");
int n=10;
char resp[8];
bspsdcardselect(1);
sspputc(SSP0,0x40|55);
sspputc(SSP0,0);
sspputc(SSP0,0);
sspputc(SSP0,0);
sspputc(SSP0,0);
sspputc(SSP0,0x63);
do
{sspgetnc(SSP0,resp,1);}
while(resp[0]!=1 && n--);
if(resp[0]==1)
{
//sspgetnc(SSP0,resp,8);
bspsdcardselect(0);
return 1;
}
else
{
//sspgetnc(SSP0,resp,8);
bspsdcardselect(0);
return 0;
}
}
int sendCMD58()
{
int n=10;
char resp[4];
//libucprintf("Send CMD58\n\r");
bspsdcardselect(1);
sspputc(SSP0,0x58);
sspputc(SSP0,0);
sspputc(SSP0,0);
sspputc(SSP0,0x0);
sspputc(SSP0,0x0);
sspputc(SSP0,0x1);
sspgetnc(SSP0,resp,4);
bspsdcardselect(0);
int a = resp[0]+(resp[1]<<8)+(resp[1]<<16)+(resp[1]<<24);
return a;
}
int sendACMD41(int arg)
{
//libucprintf("Send ACMD41\n\r");
int n=10;
char resp[10];
do{
sendCMD55();
__delay(1024);
n=10;
resp[0]=0xff;
bspsdcardselect(1);
sspputc(SSP0,0x80+0x41);
sspputc(SSP0,(char)(arg>>24));
sspputc(SSP0,(char)(arg>>16));
sspputc(SSP0,(char)(arg>>8));
sspputc(SSP0,(char)(arg));
sspputc(SSP0,0x1);
do
{
sspgetnc(SSP0,resp,1);
}while((resp[0] & 0x80) && n--);
}while(resp[0]>1);
if(resp[0]==0)
{
//sspgetnc(SSP0,resp,8);
bspsdcardselect(0);
return 1;
}
else
{
//sspgetnc(SSP0,resp,8);
bspsdcardselect(0);
return 0;
}
}
void senddummyclocks(int n)
{
libucprintf("Send dummy clocks\n\r");
while(n--)
{sspputc(SSP0,0xFF);}
}
void checkforsdcard()
{
if(bspsdcardpresent())libucprintf("SD card detected\n\r");
else libucprintf("NO SD card detected\n\r");
}