在STM32的SPI驱动上移植过来的,由于代码较长,这里只提供关键代码:
/* SSPxSR - bit definitions. */
#define TFE 0x01
#define TNF 0x02
#define RNE 0x04
#define RFF 0x08
#define BSY 0x10
/*******************************************************************************
* Function Name : _spi_read_write
* Description : None
* Input : - data:
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
__inline uint8_t _spi_read_write(uint8_t dat)
{
/* Write and Read a byte on SPI interface. */
LPC_SSP0->DR = dat;
while (LPC_SSP0->SR & BSY); /* Wait for transfer to finish */
return (LPC_SSP0->DR); /* Return received value */
}
/*******************************************************************************
* Function Name : MSD_SPI_Configuration
* Description : SD Card SPI Configuration
* Input : None
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
void MSD_SPI_Configuration(void)
{
/* Initialize and enable the SSP Interface module. */
// LPC_SC->PCONP |= (1 << 21); /* Enable power to SSPI0 block */
/* P3.26 is SD Card Power Supply Enable Pin */
LPC_GPIO3->FIODIR |= (1<<26); /* P3.26 is output */
LPC_GPIO3->FIOPIN &= ~(1<<26); /* set P3.26 low(enable power) */
/* SCK, MISO, MOSI are SSP pins. */
// LPC_PINCON->PINSEL3 &= ~(3<<8); /* P1.20 cleared */
LPC_PINCON->PINSEL3 |= (3<<8); /* P1.20 SCK0 */
// LPC_PINCON->PINSEL3 &= ~((3<<14) | (3<<16)); /* P1.23, P1.24 cleared */
LPC_PINCON->PINSEL3 |= ((3<<14) | (3<<16)); /* P1.23 MISO0, P1.24 MOSI0 */
/* SSEL is GPIO, output set to high. */
LPC_GPIO1->FIODIR |= (1<<21); /* P1.21 is output */
LPC_GPIO1->FIOPIN |= (1<<21); /* set P1.21 high (SSEL inact.)*/
LPC_PINCON->PINSEL3 &= ~(3<<10); /* P1.21 SSEL (used as GPIO) */
// LPC_SC->PCLKSEL1 &= ~(3<<10); /* PCLKSP0 = CCLK/4 ( 25MHz) */
// LPC_SC->PCLKSEL1 |= (1<<10); /* PCLKSP0 = CCLK (100MHz) */
LPC_SSP0->CPSR = 50; //25M/50
//LPC_SSP0->CPSR = 250; /* 100MHz / 250 = 400kBit */
/* maximum of 18MHz is possible*/
LPC_SSP0->CR0 = (7<<0)|(3<<6); /* 8Bit, CPOL=1, CPHA=1 */
LPC_SSP0->CR1 = 0x0002; /* SSP0 enable, master */
}
/*******************************************************************************
2019年2月27日18:39:50:经过调试发现最小是58,低于这个值就会返回FR_DISK_ERR
*******************************************************************************/
void MSD_SPIHighSpeed(uint8_t on)
{
/* Set a SPI clock speed to desired value. */
if (on==1) {
/* Max. 12 MBit used for Data Transfer. */
LPC_SSP0->CPSR = 14; /* 100MHz/10 = 10MBit 经过测试我手上的卡最大是1.724MBit */
printf("MSD_SPIHighSpeed(1)\n");
}
else {
/* Max. 400 kBit used in Card Initialization. */
LPC_SSP0->CPSR = 50; /* 100MHz / 250 = 400kBit */
printf("MSD_SPIHighSpeed(0)\n");
}
}
//测试代码
FRESULT res;
res=f_mount(0,&fs);
if(res==FR_OK)
{
printf("f_mount() FR_OK\n");
}
else
{
printf("f_mount()=%d\n",res);
}
res = f_open(&fsrc , "0:/system.txt" , FA_CREATE_ALWAYS|FA_WRITE);
if(res==FR_OK)
{
printf("f_open() FR_OK\n");
res = f_write(&fsrc,"这个是sd卡驱动测试,hello!",sizeof("这个是sd卡驱动测试,hello!")-1, &WriteSize);
f_close(&fsrc);
printf("f_write()=%d\n",res);
}
else
{
printf("f_open()=%d\n",res);
}