|
作者: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的配置了
- #define SPIX SPI2
- #define SPIX_CLK RCC_APB1Periph_SPI2
- #define SPIX_CLK_INIT RCC_APB1PeriphClockCmd
-
- #define SPIX_SCK_PIN GPIO_Pin_13
- #define SPIX_SCK_GPIO_PORT GPIOB
- #define SPIX_SCK_GPIO_CLK RCC_AHB1Periph_GPIOB
- #define SPIX_SCK_SOURCE GPIO_PinSource13
- #define SPIX_SCK_AF GPIO_AF_SPI2
-
- #define SPIX_MISO_PIN GPIO_Pin_14
- #define SPIX_MISO_GPIO_PORT GPIOB
- #define SPIX_MISO_GPIO_CLK RCC_AHB1Periph_GPIOB
- #define SPIX_MISO_SOURCE GPIO_PinSource14
- #define SPIX_MISO_AF GPIO_AF_SPI2
-
- #define SPIX_MOSI_PIN GPIO_Pin_15
- #define SPIX_MOSI_GPIO_PORT GPIOB
- #define SPIX_MOSI_GPIO_CLK RCC_AHB1Periph_GPIOB
- #define SPIX_MOSI_SOURCE GPIO_PinSource15
- #define SPIX_MOSI_AF GPIO_AF_SPI2
-
- #define sFLASH_CS_PIN GPIO_Pin_11
- #define sFLASH_CS_GPIO_PORT GPIOB
- #define sFLASH_CS_GPIO_CLK RCC_AHB1Periph_GPIOB
初始化函数如下
- void MSD0_SPI_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
-
-
- SPIX_CLK_INIT(SPIX_CLK, ENABLE);
-
-
- RCC_AHB1PeriphClockCmd(SPIX_SCK_GPIO_CLK | SPIX_MISO_GPIO_CLK |
- SPIX_MOSI_GPIO_CLK | sFLASH_CS_GPIO_CLK, ENABLE);
-
-
- GPIO_PinAFConfig(SPIX_SCK_GPIO_PORT, SPIX_SCK_SOURCE, SPIX_SCK_AF);
- GPIO_PinAFConfig(SPIX_MISO_GPIO_PORT, SPIX_MISO_SOURCE, SPIX_MISO_AF);
- GPIO_PinAFConfig(SPIX_MOSI_GPIO_PORT, SPIX_MOSI_SOURCE, SPIX_MOSI_AF);
-
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
-
-
- GPIO_InitStructure.GPIO_Pin = SPIX_SCK_PIN;
- GPIO_Init(SPIX_SCK_GPIO_PORT, &GPIO_InitStructure);
-
-
- GPIO_InitStructure.GPIO_Pin = SPIX_MOSI_PIN;
- GPIO_Init(SPIX_MOSI_GPIO_PORT, &GPIO_InitStructure);
-
-
- GPIO_InitStructure.GPIO_Pin = SPIX_MISO_PIN;
- GPIO_Init(SPIX_MISO_GPIO_PORT, &GPIO_InitStructure);
-
-
- GPIO_InitStructure.GPIO_Pin = sFLASH_CS_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
- GPIO_Init(sFLASH_CS_GPIO_PORT, &GPIO_InitStructure);
-
-
- MSD0_card_disable();
-
-
- MSD0_SPIHighSpeed(0);
-
-
- SPI_Cmd(SPIX, ENABLE);
- }
- void MSD0_SPIHighSpeed(uint8_t b_high)
- {
- SPI_InitTypeDef SPI_InitStructure;
-
- SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
- SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
- SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
- SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
- SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
- SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
- SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
- SPI_InitStructure.SPI_CRCPolynomial = 7;
-
-
- if(b_high == 0)
- {
- SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
- }
- else
- {
- SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8;
- }
-
- SPI_Init(SPIX, &SPI_InitStructure);
- }
个人就感觉这两个函数让人头疼。其他的就还好了。
大家只需要在主函数中添加头文件即可调用。所用接口如下:
stm32f4通过spi用fatfs读写sd卡程序,已经调通。用的是单片机中B口的
B11--CS、
B13--SCLK、
B14--MISO、
B15--MOSI
例如
- #include "stm32f4_discovery.h"
- #include "stm32f4xx_conf.h"
- #include "stm32f4xx_rcc.h"
- #include <stdio.h>
-
- #include "stm32f4xx_spi.h"
- #include "ff.h"
- #include "diskio.h"
- #include "SPI_MSD0_Driver.h"
-
-
-
- FATFS fs;
- FRESULT res;
- DIR dirs;
- FIL file;
- FILINFO finfo;
- UINT br;
- unsigned char buffer[200];
- char spfline[30];
- void main()
- {
-
- res=f_mount(0, &fs);
-
- res = f_open(&file, "0:/raoqin11.txt",FA_OPEN_ALWAYS|FA_READ|FA_WRITE );
- if(res!=FR_OK) { printf("f_open() fail \r\n"); }
- else { printf("f_open() success \r\n"); }
-
-
-
- res = f_lseek(&file, file.fsize);
- br = f_puts("1234567890", &file) ;
- if(br<1) { printf("f_puts() fail \r\n"); }
- else { printf("f_puts() success \r\n"); }
-
- res = f_lseek(&file, 2);
- br = f_puts("--", &file) ;
- if(br<1) { printf("f_puts() fail \r\n"); }
- else { printf("f_puts() success \r\n"); }
-
-
- br = file.fsize;
- printf("file size:%d\r\n",br);
- res = f_read(&file, buffer, file.fsize, &br);
- if(res == FR_OK ) { printf("text:%s\r\n", buffer); }
- else { printf(" f_read() fail \r\n"); }
-
-
- f_close(&file);
-
- }
发现这里不好上传文件,我给个链接吧,想用的人可以在这里下。不要下载积分
http://download.csdn.net/detail/raoqin/5333487
|