Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
10/24/08 19:31
Read: times


 
#159318 - bit banged spi on ds89C420 for sd card
Hello, i'm trying to connect a sd card to my ds89C420 via bit banged spi. Without success... Someone has an idea seeing the code hereunder ?

/*------------------------------------------------------------------------------
spi.C
------------------------------------------------------------------------------*/

#include <REG420.H>               
#include <stdio.h>        
#include <intrins.h>
#define N_OF_SPI_BYTES 6
/* pin definition	*/
 sbit CS=P1^1;
 sbit SCK=P1^2;
 sbit MOSI=P1^3;
 sbit MISO=P1^4;
 /* global variable*/
 unsigned char data spidata[N_OF_SPI_BYTES]	;
 unsigned char bdata spitmp;
 sbit spitmp7=spitmp^7	;
 sbit spitmp6=spitmp^6	;
 sbit spitmp5=spitmp^5 ;
 sbit spitmp4=spitmp^4 ;
 sbit spitmp3=spitmp^3 ;
 sbit spitmp2=spitmp^2 ;
 sbit spitmp1=spitmp^1 ;
 sbit spitmp0=spitmp^0 ;

 /* definition fonction */
 void spireadwrite(char nbre_byte, char delayclock);
 void initsd(void);
 void delay(char pauseclock);
 void sendcmd(char cmd,char arg1,char arg2,char arg3,char arg4,char crc);

 /* generation de pause */
void delay(char pauseclock)
{
  unsigned char i;
  for(i=0;i==pauseclock;i++)
   {
    _nop_();
   }
}


/* fonction envoi reception spi */

void spireadwrite(char nbre_byte, char delayclock)
{
unsigned char data i = nbre_byte-1;
while(1)
  {		spitmp=spidata[i];
  		MOSI=spitmp7;SCK=1;delay(delayclock);spitmp7=MISO;SCK=0;delay(delayclock);
		MOSI=spitmp6;SCK=1;delay(delayclock);spitmp6=MISO;SCK=0;delay(delayclock);
		MOSI=spitmp5;SCK=1;delay(delayclock);spitmp5=MISO;SCK=0;delay(delayclock);
		MOSI=spitmp4;SCK=1;delay(delayclock);spitmp4=MISO;SCK=0;delay(delayclock);
		MOSI=spitmp3;SCK=1;delay(delayclock);spitmp3=MISO;SCK=0;delay(delayclock);
		MOSI=spitmp2;SCK=1;delay(delayclock);spitmp2=MISO;SCK=0;delay(delayclock);
		MOSI=spitmp1;SCK=1;delay(delayclock);spitmp1=MISO;SCK=0;delay(delayclock);
		MOSI=spitmp0;SCK=1;delay(delayclock);spitmp0=MISO;SCK=0;delay(delayclock);
		spidata[i]=spitmp;
		if (i==0) break;
		i--;
  }
}


/* envoi des commandes SD*/
void sendcmd(char cmd,char arg1,char arg2,char arg3,char arg4,char crc)
{
 spidata[5]=cmd; 
 spidata[4]=arg1;   
 spidata[3]=arg2; 
 spidata[2]=arg3;
 spidata[1]=arg4;   
 spidata[0]=crc; 
 spireadwrite(6,100); 
}

/*initialisation de la sd*/

void initsd(void)
{
unsigned char j;
unsigned char success;	 
CS=1;
SCK=0;
success=0;
printf ("Envoi 80 clk - debut\n");
sendcmd(0xff,0xff,0xff,0xff,0xff,0xff); 
sendcmd(0xff,0xff,0xff,0xff,0xff,0xff); 
printf ("Envoi 80 clk - fin\n");
CS=0;
delay(250);
printf ("Envoi cmd0\n");
spidata[0]=0x0ff;
spireadwrite(1,100);
sendcmd(0x40,0x00,0x00,0x00,0x00,0x95); 
printf ("Cmd0 envoyée attente rep\n");

for(j=0;j<10;j++)
   {
   spidata[0] =0x0ff ;
   spireadwrite(1,100) ;
   printf("rep sd : %c\n",spidata[0]);
   if (spidata[0] ==1)
     {
	  	  printf(" rep sd spi mode ok\n");
		  success=1;
		  break;
	 }

   }
if(success==1)
	{ printf("fin cmd0 : succes !\n");}
	else
	{ printf("fin cmd0 : erreur !\n");}
success=0;
printf ("Envoi cmd55\n");
sendcmd(0x77,0x00,0x00,0x00,0x00,0xFF);
printf ("cmd55 envoyée attente rep\n");
for(j=0;j<10;j++)
   {
   spidata[0] =0x0ff ;
   spireadwrite(1,10) ;
   printf("sd rep : %c\n",spidata[0]);
   if (spidata[0] ==0)
     {
	  	  printf(" rep sd cmd55 ok\n");
		  success=1;
		  break;
	 }

   }
if(success==1)
	{ printf("fin cmd55 : succes !\n");}
	else
	{ printf("fin cmd55 : erreur !\n");}
success=0;
printf ("Envoi de acmd41\n");
sendcmd(0x69,0x00,0x00,0x00,0x00,0xFF);
printf ("Commande 41 envoyée attente reponse\n");
for(j=0;j<10;j++)
   {
   spidata[0] =0x0ff ;
   spireadwrite(1,10) ;
   printf("carte sd rep : %c\n",spidata[0]);
   if (spidata[0] ==0)
     {
	  	  printf(" rep sd acmd41 ok : sd idle\n");
		  success=1;
		  break;
	 }

   }
if(success==1)
	{ printf("fin cmd41 : succes !\n");}
	else
	{ printf("fin cmd41 : erreur !\n");}
}
   
void main (void) {

    SCON0  = 0x50;		        /* SCON: mode 1, 8-bit UART, enable rcvr      */
    TMOD |= 0x20;               /* TMOD: timer 1, mode 2, 8-bit reload        */
    TH1   = 253;                /* TH1:  reload value for 9600 baud @ 11.0592MHz   */
    TR1   = 1;                  /* TR1:  timer 1 run                          */
    TI    = 1;                  /* TI:   set TI to send first char of UART    */

  printf ("Hello World\n");
  CS=1;
  SCK=0;
  initsd();
  
  while (1) {
  
  }
}


List of 12 messages in thread
TopicAuthorDate
bit banged spi on ds89C420 for sd card            01/01/70 00:00      
   Some notes about the code            01/01/70 00:00      
      some more details            01/01/70 00:00      
         Have you found the "real" specification?            01/01/70 00:00      
            Keil logic analyzer            01/01/70 00:00      
               GIGO - verify with the so-called standard            01/01/70 00:00      
                  I agree !            01/01/70 00:00      
         More comments            01/01/70 00:00      
            some answers            01/01/70 00:00      
               Have you considered....            01/01/70 00:00      
                  3V3 to 5V is easily accomplished ..            01/01/70 00:00      
               To Show My Point            01/01/70 00:00      

Back to Subject List