stm32f4基于spi用fatfs读写SD卡的实现

作者:raoqin

转自:http://blog.csdn.net/raoqin/article/details/8887384


stm32f4之中其实有SDIO这个接口,但是我用封装是100引脚的,有些功能分不开,没办法,只能用SPI来读写SD卡。

这里用加了FATFS文件系统,用的是官方的09版本,这种文件中包括6个文件,分别如下

ff.c 

ff.h 

diskio.c 

diskio.h

integer.h 

ffconf.h

其中需要写的是diskio.c中的函数,这个文件中要写的函数有6个,如下

disk_initialize( )

disk_status( )

disk_read( )

disk_write( )

disk_ioctl( )

disk_fattime( )

这些函数却又是要调用stm32库中的spi读写函数。而其他的integer.h 和 ffconf.h是配置用的,一般也就改一两个宏定义就好。

下面主要是说一下SPI的配置了

  1. #define SPIX                           SPI2  
  2. #define SPIX_CLK                       RCC_APB1Periph_SPI2  
  3. #define SPIX_CLK_INIT                  RCC_APB1PeriphClockCmd  
  4.   
  5. #define SPIX_SCK_PIN                   GPIO_Pin_13  
  6. #define SPIX_SCK_GPIO_PORT             GPIOB  
  7. #define SPIX_SCK_GPIO_CLK              RCC_AHB1Periph_GPIOB  
  8. #define SPIX_SCK_SOURCE                GPIO_PinSource13  
  9. #define SPIX_SCK_AF                    GPIO_AF_SPI2  
  10.   
  11. #define SPIX_MISO_PIN                  GPIO_Pin_14  
  12. #define SPIX_MISO_GPIO_PORT            GPIOB  
  13. #define SPIX_MISO_GPIO_CLK             RCC_AHB1Periph_GPIOB  
  14. #define SPIX_MISO_SOURCE               GPIO_PinSource14  
  15. #define SPIX_MISO_AF                   GPIO_AF_SPI2  
  16.   
  17. #define SPIX_MOSI_PIN                  GPIO_Pin_15  
  18. #define SPIX_MOSI_GPIO_PORT            GPIOB  
  19. #define SPIX_MOSI_GPIO_CLK             RCC_AHB1Periph_GPIOB  
  20. #define SPIX_MOSI_SOURCE               GPIO_PinSource15  
  21. #define SPIX_MOSI_AF                   GPIO_AF_SPI2  
  22.   
  23. #define sFLASH_CS_PIN                        GPIO_Pin_11  
  24. #define sFLASH_CS_GPIO_PORT                  GPIOB  
  25. #define sFLASH_CS_GPIO_CLK                   RCC_AHB1Periph_GPIOB  

初始化函数如下
  1. void MSD0_SPI_Configuration(void)  
  2. {         
  3.   GPIO_InitTypeDef GPIO_InitStructure;  
  4.   
  5.   //SPI模块时钟使能  
  6.   SPIX_CLK_INIT(SPIX_CLK, ENABLE);  
  7.   
  8.   //IO口时钟使能  
  9.   RCC_AHB1PeriphClockCmd(SPIX_SCK_GPIO_CLK | SPIX_MISO_GPIO_CLK |   
  10.                          SPIX_MOSI_GPIO_CLK | sFLASH_CS_GPIO_CLK, ENABLE);  
  11.     
  12.   //设置SPI引脚利用功能  
  13.   GPIO_PinAFConfig(SPIX_SCK_GPIO_PORT, SPIX_SCK_SOURCE, SPIX_SCK_AF);  
  14.   GPIO_PinAFConfig(SPIX_MISO_GPIO_PORT, SPIX_MISO_SOURCE, SPIX_MISO_AF);  
  15.   GPIO_PinAFConfig(SPIX_MOSI_GPIO_PORT, SPIX_MOSI_SOURCE, SPIX_MOSI_AF);  
  16.     
  17.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;  
  18.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  
  19.   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;  
  20.   GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_DOWN;  
  21.           
  22.   /*!< SPI SCK pin configuration */  
  23.   GPIO_InitStructure.GPIO_Pin = SPIX_SCK_PIN;  
  24.   GPIO_Init(SPIX_SCK_GPIO_PORT, &GPIO_InitStructure);  
  25.    
  26.   /*!< SPI MOSI pin configuration */  
  27.   GPIO_InitStructure.GPIO_Pin =  SPIX_MOSI_PIN;  
  28.   GPIO_Init(SPIX_MOSI_GPIO_PORT, &GPIO_InitStructure);  
  29.   
  30.   /*!< SPI MISO pin configuration */  
  31.   GPIO_InitStructure.GPIO_Pin =  SPIX_MISO_PIN;  
  32.   GPIO_Init(SPIX_MISO_GPIO_PORT, &GPIO_InitStructure);  
  33.   
  34.   /*!< Configure sFLASH Card CS pin in output pushpull mode ********************/  
  35.   GPIO_InitStructure.GPIO_Pin = sFLASH_CS_PIN;  
  36.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;  
  37.   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;  
  38.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  
  39.   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;  
  40.   GPIO_Init(sFLASH_CS_GPIO_PORT, &GPIO_InitStructure);  
  41.     
  42.   //关闭片选  
  43.   MSD0_card_disable();   
  44.   
  45.   //设置SPI接口  
  46.   MSD0_SPIHighSpeed(0);       
  47.   
  48.   //使能SPI模块  
  49.   SPI_Cmd(SPIX, ENABLE);  
  50. }  
  1. void MSD0_SPIHighSpeed(uint8_t b_high)  
  2. {  
  3.     SPI_InitTypeDef SPI_InitStructure;  
  4.   
  5.     SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;  
  6.     SPI_InitStructure.SPI_Mode = SPI_Mode_Master;  
  7.     SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;  
  8.     SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;  
  9.     SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;  
  10.     SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;  
  11.     SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;  
  12.     SPI_InitStructure.SPI_CRCPolynomial = 7;  
  13.   
  14.     /* Speed select */  
  15.     if(b_high == 0)  
  16.     {  
  17.         SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;  
  18.     }  
  19.     else  
  20.     {  
  21.         SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8;  
  22.     }  
  23.   
  24.     SPI_Init(SPIX, &SPI_InitStructure);  
  25. }  
个人就感觉这两个函数让人头疼。其他的就还好了。

大家只需要在主函数中添加头文件即可调用。所用接口如下:

stm32f4通过spi用fatfs读写sd卡程序,已经调通。用的是单片机中B口的

B11--CS、

B13--SCLK、

B14--MISO、

B15--MOSI

例如

  1. #include "stm32f4_discovery.h"  
  2. #include "stm32f4xx_conf.h"  
  3. #include "stm32f4xx_rcc.h"  
  4. #include <stdio.h>  
  5.   
  6. #include "stm32f4xx_spi.h"  
  7. #include "ff.h"  
  8. #include "diskio.h"  
  9. #include "SPI_MSD0_Driver.h"  
  10.   
  11. //----  
  12.   
  13. FATFS fs;             
  14. FRESULT res;                   
  15. DIR dirs;  
  16. FIL file;  
  17. FILINFO finfo;  
  18. UINT br;  
  19. unsigned char buffer[200];  
  20. char spfline[30];  
  21. void main()  
  22. {  
  23. /*一些初始化*/  
  24. res=f_mount(0, &fs);  
  25.     //打开文件  如果data.txt存在,则打开;否则,创建一个新文件  
  26.     res = f_open(&file, "0:/raoqin11.txt",FA_OPEN_ALWAYS|FA_READ|FA_WRITE );  
  27.     if(res!=FR_OK) { printf("f_open() fail \r\n"); }  
  28.     else { printf("f_open() success \r\n"); }  
  29.        
  30.     //写文件  
  31.     //将指针指向文件末  
  32.     res = f_lseek(&file, file.fsize);     
  33.     br = f_puts("1234567890", &file) ;  //向文件末写入字符串  
  34.     if(br<1) { printf("f_puts() fail \r\n"); }  
  35.     else { printf("f_puts() success \r\n"); }  
  36.       
  37.     res = f_lseek(&file, 2);     
  38.     br = f_puts("--", &file) ;  //向文件末写入字符串  
  39.     if(br<1) { printf("f_puts() fail \r\n"); }  
  40.     else { printf("f_puts() success \r\n"); }  
  41.       
  42.     //读文件  
  43.     br = file.fsize;  
  44.     printf("file size:%d\r\n",br);  
  45.     res = f_read(&file, buffer, file.fsize, &br);     //一次读一个字节知道读完全部文件信息  
  46.     if(res == FR_OK ) { printf("text:%s\r\n", buffer); }  
  47.     else { printf(" f_read() fail \r\n"); }  
  48.   
  49.     //关闭文件  
  50.     f_close(&file);  
  51. /*其他的一些操作*/  
  52. }  

发现这里不好上传文件,我给个链接吧,想用的人可以在这里下。不要下载积分

http://download.csdn.net/detail/raoqin/5333487
  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值