Hi Ronan,
I will post this function out directly, just for your reference.
1, SSP0LowSpeed();
/**************************************************************************************
* FunctionName : SSP0LowSpeed()
* Description : SSP0 low speed = 400K
* EntryParameter : None
* ReturnValue : None
**************************************************************************************/
void SSP0LowSpeed(void)
{
SPI0_BASE_PTR->BR = 7;
}
Actually, just configure the SPI clock speed to 400K, as you know the SD card configuration have the different speed.
In LPC, you also can configure the low clock speed to 400K.
2, MMCDelayUs();
void MMCDelayUs(uint16 tt)
{
uint8 i;
while (tt--)
{
for (i=0; i<250; i++)
{
;
}
}
}
Just a software delay.
3, Send_Byte();
uint8 Send_Byte (uint8 ucdata)
{
uint8 ucTemp;
while((SPI0_S & SPI_S_SPTEF_MASK) != SPI_S_SPTEF_MASK);
SPI0_DL = ucdata;
while((SPI0_S & SPI_S_SPRF_MASK) != SPI_S_SPRF_MASK);
ucTemp = SPI0_DL;
ucTemp = ucTemp;
return ucTemp;
}
Just send one byte SPI data.
4. MMCWriteCmd();
uint8 MMCWriteCmd( uint8 cmd, uint32 arg, uint8 crc )
{
uint16 cnt=512;
uint8 sta;
MMCCS(1); //SEL high
Send_Byte( 0xff ); //delay
MMCCS(0); //SEL low
if ( sd_Enable_Select() )
{
return 0xff;
}
Send_Byte( cmd | 0x40 );
Send_Byte( (uint8)(arg>>24) );
Send_Byte( (uint8)(arg>>16) );
Send_Byte( (uint8)(arg>>8) );
Send_Byte( (uint8)(arg) );
Send_Byte( crc ); // CRC
do
{
sta = Send_Byte( 0xff );
cnt--;
} while ( (cnt)&&(sta==0xff) );
return sta;
}
__inline static void sd_Disable_Select( void )
{
MMCCS(1); //
Send_Byte( 0xff ); //
}
__inline static uint8 sd_Enable_Select( void )
{
MMCCS(0); //
if ( sd_WaitRead() == 0 ) // ??SD/MMC??
{
return 0;
}
sd_Disable_Select();
return 1;
}
uint8 sd_WaitRead( void )
{
uint32 cnt = 0x00fffff;
uint8 sta;
do
{
sta = Send_Byte( 0xff );;
if ( sta == 0xff ) //
{
return 0;
}
cnt--;
} while ( cnt );
return 1;
}
5. MMCCS();
void MMCCS(uint8 cs)
{
if (cs == 1)
{
MMC_CS_SET(); // SS=1
}
else
{
MMC_CS_CLR(); // SS=0
}
}
Just control SPI_SEL pin high or low level.
6 Get_Byte();
uint8 Get_Byte (void)
{
uint8 ucTemp;
while((SPI0_S & SPI_S_SPTEF_MASK) != SPI_S_SPTEF_MASK);
SPI0_DL = 0xff;
while((SPI0_S & SPI_S_SPRF_MASK) != SPI_S_SPRF_MASK);
ucTemp = SPI0_DL;
return (ucTemp);
}
Read one SPI byte data.
7. SSP0HighSpeed();
void SSP0HighSpeed(void)
{
SPI0_BASE_PTR->BR = 2;
}
Set high clock speed, to 24Mhz.
8. System_init();
MCU chip system initial, you just use the LPC system initial code is OK.
9. spiInit();
You just need to use the LPC SPI initilization code.
Wish it helps you!
Have a great day,
Kerry
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------