帝国cms做下载网站/杭州seo关键字优化
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;//SPI模块时钟使能SPIX_CLK_INIT(SPIX_CLK, ENABLE);//IO口时钟使能RCC_AHB1PeriphClockCmd(SPIX_SCK_GPIO_CLK | SPIX_MISO_GPIO_CLK | SPIX_MOSI_GPIO_CLK | sFLASH_CS_GPIO_CLK, ENABLE);//设置SPI引脚利用功能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;/*!< SPI SCK pin configuration */GPIO_InitStructure.GPIO_Pin = SPIX_SCK_PIN;GPIO_Init(SPIX_SCK_GPIO_PORT, &GPIO_InitStructure);/*!< SPI MOSI pin configuration */GPIO_InitStructure.GPIO_Pin = SPIX_MOSI_PIN;GPIO_Init(SPIX_MOSI_GPIO_PORT, &GPIO_InitStructure);/*!< SPI MISO pin configuration */GPIO_InitStructure.GPIO_Pin = SPIX_MISO_PIN;GPIO_Init(SPIX_MISO_GPIO_PORT, &GPIO_InitStructure);/*!< Configure sFLASH Card CS pin in output pushpull mode ********************/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(); //设置SPI接口MSD0_SPIHighSpeed(0); //使能SPI模块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;/* Speed select */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);//打开文件 如果data.txt存在,则打开;否则,创建一个新文件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
https://blog.csdn.net/raoqin/article/details/8887384