Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

HELP!How to initialiazation the SD via SPI with CC2430?

Status
Not open for further replies.

telly270

New Member
I am attempting to read/write files to a SD card via SPI mode with CC2430.
But I can't initialiazation it successfully now.
When i send the command 0 to SD card,no response receiveed but 0xFF.
The expect response is 0x01.
And I find that the SPI_Init() has something wrong.But I don't know how to modity it.

I hope every advice.

CC2430: UASRT1,SPI,master,Alt.1

void SPI_Init(void) // Initialize and enable the SPI mode
{
P0SEL =0xFB; // Setup P0 for SPI mode
P0DIR =0xDF; // Setup P0.2~P0.5 as spi interface

PERCFG=0x00; // UASRT1,SPI,Alt.1,master
P2DIR =0X40;
P2SEL =0X40;

U1CSR =0x40; // Enable SPI

U1GCR =0x31; //Set baudrate F/8=4MHz(F=32MHz),
//CPOL=0,CPHA=0,MSB first
U1BAUD=0x00;
}


unsigned char SD_ResetSD(void)
{
unsigned char param[4] = {0x00,0x00,0x00,0x00},resp;
return (SD_SendCmd(CMD0, param, CMD0_R, &resp));
}

unsigned char SD_SendCmd(unsigned char cmd,unsigned char *param,
unsigned char resptype,unsigned char *resp)
{
unsigned char i,rlen;
unsigned char tmp;

SPI_CS_Assert();
SPI_SendByte((cmd & 0x3F) | 0x40);

for(i=0;i<4;i++)
{
SPI_SendByte(param[3-i]); //send parameters
}
SPI_SendByte(0x95); // send CRC
SPI_SendByte(0xFF); // 8 clocks
rlen = 0;
switch (resptype)
{
case R1:
case R1B: rlen = 1; break;
case R2: rlen = 2; break;
case R3: rlen = 5; break;
default: break;
}
i = 0;
do
{
tmp = SPI_RecByte();

i++;
}
while (((tmp & 0x80) != 0) && (i < SD_CMD_TIMEOUT));
//SD_CMD_TIMEOUT=100.

if (i >= SD_CMD_TIMEOUT)
{
SPI_CS_Deassert();
SPI_SendByte(0xFF);
return SD_ERR_CMD_TIMEOUT; //SD_ERR_CMD_TIMEOUT=0x11.
}

for (i = rlen - 1; i >= 0; i--)
{
resp = tmp;
tmp = SPI_RecByte();
}
P1_0=0;
SPI_CS_Deassert();
SPI_SendByte(0xFF);
return SD_NO_ERR;
}

unsigned char SD_Initialize(void)
{
unsigned char recbuf[4],ret;

SPI_Init();

SPI_Clk400k();

SPI_CS_Deassert();
SD_SPIDelay(10);

ret = SD_ResetSD();
if (ret != SD_NO_ERR)
return ret;
ret = SD_ActiveInit();

if (ret != SD_NO_ERR)
return ret;

ret = SD_ReadOCR(4, recbuf);
if (ret != SD_NO_ERR)
return ret;

if ((recbuf[1] & MSK_OCR_33) != MSK_OCR_33) //MSK_OCR_33=0xC0
return SD_ERR_VOL_NOTSUSP;//SD_ERR_VOL_NOTSUSP=0x04

SPI_ClkToMax();

ret = SD_SetBlockLen(SD_BLOCKSIZE);
if (ret != SD_NO_ERR)
return ret;

return SD_NO_ERR;
}
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top