stm32开发3D打印机(五)——TF卡spi协议与FATFS文件系统(已完成)

借鉴于正点原子stm32 Mini板
下方的代码仅为对TF卡的初始化

FATFS移植方法请看正点原子Mini板教程,以及建议完成USMART的移植
因为TFATFS移植与USMATRT没有什么好写的,而且在下方的链接:正点原子教程已经有详细的教程了,所以跳过了TATFS与USMART。
链接:https://pan.baidu.com/s/1_S0394ZSRZaNxgz3QIbstA
提取码:6718

电路图如下所示

stm32开发3D打印机(五)——TF卡spi协议与FATFS文件系统(已完成)_第1张图片
stm32开发3D打印机(五)——TF卡spi协议与FATFS文件系统(已完成)_第2张图片

各引脚如下所示

TF_SS PA4
TF_SCLK PA5
TF_MISO PA6
TF_MOSi PB5
TF_SW PB11

初始化SPI



u8 SPI1_ReadWriteByte(u8 byte)
{
	u8 retry=0;				 	
	while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET) 
		{
		retry++;
		if(retry>200)return 0;
		}			  
	SPI_I2S_SendData(SPI1, byte); //ͨ¹ýÍâÉèSPIx·¢ËÍÒ»¸öÊý¾Ý
	retry=0;

	while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET)//¼ì²éÖ¸¶¨µÄSPI±ê־λÉèÖÃÓë·ñ:½ÓÊÜ»º´æ·Ç¿Õ±ê־λ
		{
		retry++;
		if(retry>200)return 0;
		}	  						    
	return SPI_I2S_ReceiveData(SPI1); //·µ»Øͨ¹ýSPIx×î½ü½ÓÊÕµÄÊý¾Ý					    

}
void SPI1_SetSpeed(u8 SpeedSet)
{ 
	SPI_InitTypeDef  SPI_InitStructure;
 	SPI_InitStructure.SPI_BaudRatePrescaler = SpeedSet ;
  	SPI_Init(SPI1, &SPI_InitStructure);
 	SPI_Cmd(SPI1,ENABLE);
}

u8 SD_SPI_ReadWriteByte(u8 data)
{
	return SPI1_ReadWriteByte(data);
}

void SPI1_Init()
{
	GPIO_InitTypeDef  GPIO_InitStructure;
  	SPI_InitTypeDef  SPI_InitStructure;
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1|RCC_AHB1Periph_GPIOA|RCC_AHB1Periph_GPIOB, ENABLE);//?? SPI1 ??

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_5;//PB3~5¸´Óù¦ÄÜÊä³ö	
  	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//¸´Óù¦ÄÜ
  	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//ÍÆÍìÊä³ö
  	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//100MHz
  	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//ÉÏÀ­
  	GPIO_Init(GPIOA, &GPIO_InitStructure);//³õʼ»¯
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
	GPIO_Init(GPIOB, &GPIO_InitStructure);//³õʼ»¯
	
	

	GPIO_SetBits(GPIOB,GPIO_Pin_5);//PA2.3.4ÉÏÀ­ 
	GPIO_SetBits(GPIOA,GPIO_Pin_5|GPIO_Pin_6);//PA2.3.4ÉÏÀ­ 


	SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;  //ÉèÖÃSPIµ¥Ïò»òÕßË«ÏòµÄÊý¾Ýģʽ:SPIÉèÖÃΪ˫ÏßË«ÏòÈ«Ë«¹¤
	SPI_InitStructure.SPI_Mode = SPI_Mode_Master;		//ÉèÖÃSPI¹¤×÷ģʽ:ÉèÖÃΪÖ÷SPI
	SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;		//ÉèÖÃSPIµÄÊý¾Ý´óС:SPI·¢ËͽÓÊÕ8λ֡½á¹¹
	SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;		//´®ÐÐͬ²½Ê±ÖӵĿÕÏÐ״̬Ϊ¸ßµçƽ
	SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;	//´®ÐÐͬ²½Ê±Öӵĵڶþ¸öÌø±äÑØ£¨ÉÏÉý»òϽµ£©Êý¾Ý±»²ÉÑù
	SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;		//NSSÐźÅÓÉÓ²¼þ£¨NSS¹Ü½Å£©»¹ÊÇÈí¼þ£¨Ê¹ÓÃSSI룩¹ÜÀí:ÄÚ²¿NSSÐźÅÓÐSSIλ¿ØÖÆ
	SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;		//¶¨Ò岨ÌØÂÊÔ¤·ÖƵµÄÖµ:²¨ÌØÂÊÔ¤·ÖƵֵΪ256
	SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;	//Ö¸¶¨Êý¾Ý´«Êä´ÓMSBλ»¹ÊÇLSBλ¿ªÊ¼:Êý¾Ý´«Êä´ÓMSBλ¿ªÊ¼
	SPI_InitStructure.SPI_CRCPolynomial = 7;	//CRCÖµ¼ÆËãµÄ¶àÏîʽ
	SPI_Init(SPI1, &SPI_InitStructure);  //¸ù¾ÝSPI_InitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèSPIx¼Ä´æÆ÷
 	GPIO_PinAFConfig(GPIOA,GPIO_PinSource5,GPIO_AF_SPI1); //PB3 ??? SPI1 
	GPIO_PinAFConfig(GPIOA,GPIO_PinSource6,GPIO_AF_SPI1); //PB4 ??? SPI1 
	GPIO_PinAFConfig(GPIOB,GPIO_PinSource5,GPIO_AF_SPI1); //PB5 ??? SPI1 

	SPI_Cmd(SPI1, ENABLE); //ʹÄÜSPIÍâÉè

	SPI1_ReadWriteByte(0xff);//Æô¶¯´«Êä		
	printf("SPI_Init Finsh\r\n\r\n");
} 
 
u8 SD_Initialize(void)
{
	u8 r1;      // 
    u16 retry;  // 
    u8 buf[4];  
    u16 i;
    
    SD_SPI_Init();		//
	SD_SPI_SpeedLow();	//
 	
    for(i=0;i<10;i++)SPI1_ReadWriteByte(0XFF);
    retry=20;
    do
    {
        r1=SD_SendCmd(CMD0,0,0x95);
    }while((r1!=0X01) && retry--);
    SD_Type=0;
    if(r1==0X01)
    {
        if(SD_SendCmd(CMD8,0x1AA,0x87)==1)//SD V2.0
        {
            for(i=0;i<4;i++)buf[i]=SPI1_ReadWriteByte(0XFF);	//Get trailing return value of R7 resp
            if(buf[2]==0X01&&buf[3]==0XAA)//?????2.7~3.6V
            {
                retry=0XFFFE;
                do
                {
                    SD_SendCmd(CMD55,0,0X01);	//??CMD55
                    r1=SD_SendCmd(CMD41,0x40000000,0X01);//??CMD41
                }while(r1&&retry--);
                if(retry&&SD_SendCmd(CMD58,0,0X01)==0)//??SD2.0?????
                {
                    for(i=0;i<4;i++)buf[i]=SPI1_ReadWriteByte(0XFF);//??OCR?
                    if(buf[0]&0x40)SD_Type=SD_TYPE_V2HC;//printf("SD_TYPE_V2HC");    //??CCS
                    else SD_Type=SD_TYPE_V2;//printf("SD_TYPE_V2");   
                }
            }
        }
		else//SD V1.x/ MMC	V3
        {
            SD_SendCmd(CMD55,0,0X01);		//??CMD55
            r1=SD_SendCmd(CMD41,0,0X01);	//??CMD41
            if(r1<=1)
            {		
                SD_Type=SD_TYPE_V1;				
				//	printf("SD_TYPE_V1");		
                retry=0XFFFE;
                do //????IDLE??
                {
                    SD_SendCmd(CMD55,0,0X01);	//??CMD55
                    r1=SD_SendCmd(CMD41,0,0X01);//??CMD41
                }while(r1&&retry--);
            }else
            {
                SD_Type=SD_TYPE_MMC;//MMC V3
							
				//			printf("SD_TYPE_MMC");
							
                retry=0XFFFE;
                do //????IDLE??
                {											    
                    r1=SD_SendCmd(CMD1,0,0X01);//??CMD1
                }while(r1&&retry--);  
            }
            if(retry==0||SD_SendCmd(CMD16,512,0X01)!=0)SD_Type=SD_TYPE_ERR;//????
        }
    }
    SD_DisSelect();//????		
	SD_SPI_SpeedHigh();	//¸ßËÙģʽ
	printf("finsh");
	if(SD_Type)			
		return 0;		//SD¿¨³õʼ»¯³É¹¦
	else if(r1)
		return r1; 		//´íÎó	   
	else
		return 0xAA;	//ÆäËû´íÎó	
}

若初始化函数SD_Initialize()返回0,则初始化成功

int main()
{
	allinit();
	while(SD_Initialze())		//对SD卡进行初始化
		printf("SD Init error");
	printf("SD Init finsh\r\n");
	while(exfuns_init())		//为FATFS申请内存
		printf("error\r\n");
	f_mount(fs[0],"0:",1);		//挂载TF卡
	
	/即可使用FATFS库内的函数
}


你可能感兴趣的:(3D打印机,stm32开源,stm32,c语言)