nuc100的SPI-sd的初始化的一点疑问。
#include <stdio.h>#include "NUC1xx.h"
#include "DrvSPI.h"
#include "DrvGPIO.h"
#include "DrvSYS.h"
#include "DrvUART.h"
void UARTSPI_INIT()
{
STR_UART_T param;
/* Unlock the protected registers */
UNLOCKREG();
/* Enable the 12MHz oscillator oscillation */
DrvSYS_SetOscCtrl(E_SYS_XTL12M, 1);
/* Waiting for 12M Xtal stable */
DrvSYS_Delay(5000);
/* HCLK clock source. 0: external 12MHz; 7:internal 22MHz RC oscillator */
DrvSYS_SelectHCLKSource(0);
LOCKREG();
DrvSYS_SetClockDivider(E_SYS_HCLK_DIV, 0); /* HCLK clock frequency = HCLK clock source / (HCLK_N + 1) */
DrvGPIO_InitFunction(E_FUNC_UART0);
/* Select UART Clock Source From 12MHz */
DrvSYS_SelectIPClockSource(E_SYS_UART_CLKSRC,0);
param.u32BaudRate = 115200;
param.u8cDataBits = DRVUART_DATABITS_8;
param.u8cStopBits = DRVUART_STOPBITS_1;
param.u8cParity = DRVUART_PARITY_NONE;
param.u8cRxTriggerLevel= DRVUART_FIFO_1BYTES;
param.u8TimeOut = 0;
DrvUART_Open(UART_PORT0, ¶m);
DrvSYS_SetIPClock(E_SYS_SPI0_CLK,1); //打开时钟
DrvGPIO_InitFunction(E_FUNC_SPI0); // 使能 IO口的 SPI 功能
DrvSPI_Open(eDRVSPI_PORT0,eDRVSPI_MASTER,eDRVSPI_TYPE1,8, FALSE);//设置SPI0为主机模式,8bit传输
//32bit其他模式,传输模式为1
DrvSPI_DisableAutoSS(eDRVSPI_PORT0); //关闭SPI0的自动CS功能
DrvSPI_SetSlaveSelectActiveLevel(eDRVSPI_PORT0, eDRVSPI_ACTIVE_LOW_FALLING);//设置数据传送接收时序
DrvSPI_SetEndian(eDRVSPI_PORT0, eDRVSPI_MSB_FIRST);//设置MSB传输
DrvSPI_SetClockFreq(eDRVSPI_PORT0,10000,10000); //设置频率
DrvSYS_Delay(0x20000); //延时
printf("\nOpen UART success\n");
}
uint32_t SPI_WriteByte(uint32_t val)
{
uint32_t auSendSPIData;
uint32_t auReadSPIData;
auSendSPIData=val;
auSendSPIData=val;
if(DrvSPI_SingleWrite(eDRVSPI_PORT0,&auSendSPIData)==FALSE)
printf("\n\nWrite Data is fail\n\n");
while(DrvSPI_IsBusy(eDRVSPI_PORT0));
DrvSPI_DumpRxRegister(eDRVSPI_PORT0,&auReadSPIData,1);
// printf("\n--- %x --- \n",auReadSPIData);
return auReadSPIData;
}
uint32_t SD_SendCommand(uint8_t cmd,uint32_t arg,uint8_t crc)
{
uint32_t r1;
uint32_t Retry=0; //重复操作次数
DrvSPI_SetSS(eDRVSPI_PORT0, eDRVSPI_SS0);
SPI_WriteByte(0xff);
SPI_WriteByte(0xff);
SPI_WriteByte(0xff);
DrvSPI_ClrSS(eDRVSPI_PORT0, eDRVSPI_SS0); //使能片选信号
SPI_WriteByte(cmd | 0x40); //分别写入命令
SPI_WriteByte(arg>>24); //数据段第4字节
SPI_WriteByte(arg>>16); //数据段第3字节
SPI_WriteByte(arg>>8); //数据段第2字节
SPI_WriteByte(arg); //数据段第1字节
SPI_WriteByte(crc); //CRC效验和
while((r1 = SPI_WriteByte(0xff)) == 0xff)//等待响应
if(Retry++ > 200)
break;//超时退出
DrvSPI_SetSS(eDRVSPI_PORT0, eDRVSPI_SS0); //清除片选信号
SPI_WriteByte(0xff);
return r1; //返回状态值
}
void MMC_FLASH_Init()
{
uint32_t Counter = 0;
uint32_t r1=0;
for(Counter = 0 ; Counter < 0xf00; Counter++);//延时等待SD卡上电
for(Counter = 0 ; Counter < 10; Counter++)SPI_WriteByte(0xff);//输入74个脉冲等待SD卡初始化
do
{
r1 = SD_SendCommand(0,0,0x95); //发idle命令
printf("\nR1: %x ",r1);
}while(r1!=0x01);
}
int32_t main()
{
// uint32_t R1=1;
UARTSPI_INIT();
MMC_FLASH_Init();
while(1){}
}
大家好,这个是小弟写的一个初始化代码,单是返回来的全部是0xff。请教大家,是什么原因。有什么检测方法 。。。。。。。。。。。。。。。。。。。。
页:
[1]