- <font face="Arial">#include "ff.h" /* Obtains integer types */
- #include "diskio.h" /* Declarations of disk functions */
- #include "sdspi.h"
- /* Definitions of physical drive number for each drive */
- #define DEV_RAM 0 /* Example: Map Ramdisk to physical drive 0 */
- #define DEV_MMC 1 /* Example: Map MMC/SD card to physical drive 1 */
- #define DEV_USB 2 /* Example: Map USB MSD to physical drive 2 */
- SDSPI_ApiRetStatus_Type app_sdspi_ret;
- SDSPI_CardHandler_Type app_sdspi_card;
- extern const SDSPI_Interface_Type sdspi_if;
- /*-----------------------------------------------------------------------*/
- /* Get Drive Status */
- /*-----------------------------------------------------------------------*/
- DSTATUS disk_status (
- BYTE pdrv /* Physical drive nmuber to identify the drive */
- ){
- DSTATUS stat = 0u;
- //int result;
- switch (pdrv) {
- case DEV_RAM :
- //result = RAM_disk_status();
- //translate the reslut code here
- return stat;
- case DEV_MMC :
- //result = MMC_disk_status();
- stat = RES_OK;
- // translate the reslut code here
- return stat;
- case DEV_USB :
- //result = USB_disk_status();
- // translate the reslut code here
- return stat;}
- return STA_NOINIT;}
- </font>
Inidialize a Drive,Read Sector(s),Write Sector(s)等函数,也做一样的修改。
4.样例
4.1fatfs sdspi basic样例
用户通过串口输入字符,根据字符决定装载,卸载 SD 卡,读写等内容。
主函数如下
- <font face="Arial">#include "board_init.h"
- #include "ffconf.h"
- #include "ff.h"
- FATFS fs;
- FIL fil;
- const TCHAR fs_drv[] = "1:/";
- const TCHAR filname[] = "1:/hello.txt";
- BYTE work[FF_MAX_SS]; /* Work area (larger is better for processing time) */
- UINT bw, br;
- #define FIL_BUFF_LEN 16u
- uint8_t fil_write_buff[FIL_BUFF_LEN];
- uint8_t fil_read_buff[FIL_BUFF_LEN];
- FRESULT fs_ret;
- uint8_t fil_write_seed = 0u;
- /*
- * Functions.
- */
- int main(void){
- uint8_t c;
- BOARD_Init();
- printf("fatfs_basic\r\n");
- printf("Press any key for help...\r\n");
- while (1){
- c = getchar();
- switch (c){
- case 'a':
- printf("a: f_mkfs().\r\n");
-
- if ( !f_mkfs(fs_drv, 0u, work, sizeof(work)) ){
- printf("succ.\r\n");}
- else{
- printf("fail.\r\n");}
- break;
- case 'b':
- printf("b: f_mount().\r\n");
- if( !f_mount(&fs, fs_drv ,1) ){
- printf("succ.\r\n");}
- else{
- printf("fail.\r\n");}
- break;
- case 'c':
- printf("c: f_open() & f_write() & f_close().\r\n");
-
- printf("f_open().\r\n");
- fs_ret = f_open(&fil, filname, FA_CREATE_NEW | FA_WRITE );
- if ( fs_ret != FR_OK ){
- printf("fail.\r\n");
- break;}
-
- printf("f_write().\r\n");
-
- sprintf((char *)fil_write_buff, "hi, %d\r\n", fil_write_seed++),
- fs_ret = f_write(&fil, fil_write_buff, FIL_BUFF_LEN, &bw);
- if (bw != FIL_BUFF_LEN){
- printf("fail.\r\n");
-
- f_close(&fil);
- break;}
-
- printf("f_close().\r\n");
- f_close(&fil);
-
- printf("succ.\r\n");
- break;
-
- case 'd':
- printf("d: f_open() & f_read() & f_close().\r\n");
-
- printf("f_open().\r\n");
- fs_ret = f_open(&fil, filname, FA_READ );
- if ( fs_ret != FR_OK ){
- printf("fail.\r\n");
- break;}
-
- printf("f_read()\r\n");
- fs_ret = f_read(&fil, fil_read_buff, FIL_BUFF_LEN, &br);
- if (br != FIL_BUFF_LEN){
- printf("fail.\r\n");
-
- f_close(&fil);
- break;}
-
- for (uint32_t i = 0u; i < FIL_BUFF_LEN; i++){
- printf("%c", fil_read_buff[i]);}
- printf("\r\n");
-
- printf("f_close().\r\n");
- f_close(&fil);
- printf("succ.\r\n");
- break;
-
- case 'f':
- printf("f: f_unlink().\r\n");
- f_unlink(filname);
- printf("file removed.\r\n");
- break;
-
- default: /* help. */
- printf("\r\n");
- printf("a: f_mkfs().\r\n");
- printf("b: f_mount().\r\n");
- printf("c: f_open() & f_write() & f_close().\r\n");
- printf("d: f_open() & f_read() & f_close().\r\n");
- printf("f: f_unlink().\r\n");
- printf("\r\n");
- break;}}}</font>
串口可见输出结果:
4.2fatfs sdspi listfiles样例
主函数如下:
- <font face="Arial">/*
- * Variables.
- */
- FATFS fs;
- const TCHAR fs_drv[] = "1:/";
- TCHAR fs_path[256] = "\0";
- /*
- * Declerations.
- */
- FRESULT app_fatfs_listfiles(const char * dir_path);
- /*
- * Functions.
- */
- int main(void){
- BOARD_Init();
- printf("\r\app_fatfs_listfiles() example.\r\n");
-
- /* f_mount().\r\n */
- printf("f_mount(). ");
- if( !f_mount(&fs, fs_drv ,1) ){
- printf("succ.\r\n");}
- else{
- printf("fail.\r\n");
- while (1)
- {}}
-
- /* root dir. */
- app_fatfs_listfiles(fs_drv);
- /* dir0. */
- fs_path[0] = '\0';
- strcat(fs_path, fs_drv);
- strcat(fs_path, "dir0/");
- app_fatfs_listfiles(fs_path);
- /* dir1. */
- fs_path[0] = '\0';
- strcat(fs_path, fs_drv);
- strcat(fs_path, "dir1/");
- app_fatfs_listfiles(fs_path);
-
- printf("app_fatfs_listfiles() done.\r\n");
- while (1)
- {}}
- /* list the file items under the indecated path. */
- FRESULT app_fatfs_listfiles(const char * dir_path){
- FRESULT res;
- FILINFO fno;
- DIR dir;
- char *fn;
- printf("* %s ->\r\n", dir_path);
- res = f_opendir(&dir, dir_path);
- if (res != FR_OK){
- return res;}
- for (;;){
- /* read iterator. */
- res = f_readdir(&dir, &fno);
- if ( (res != FR_OK) || (fno.fname[0] == 0) ){
- break;}
- /* skip the "self" and "father" dir item. */
- if (fno.fname[0] == '.') {
- continue;}
- /* collect the dir or file name. */
- fn = fno.fname;
- if (fno.fattrib & AM_DIR) /* dir name. */{
- printf("\t%s/\r\n", fn);}
- else /* file name */{
- printf("\t%s: %u B\r\n", fn, (unsigned)fno.fsize);}}
- /* close the opened dir to reest the iterator. */
- res = f_closedir(&dir);
- return res;}
- </font>
串口可见输出结果:
5.配置SPI部分
源文件为软件SPI,如有需求可优化为硬件SPI。
5.1开启时钟
在clock_init.c中开启SPI3时钟
5.2配置引脚
在pin_init.c中配置SPI3引脚
5.3配置sdspi_port.c文件
- #include "board_init.h"
- #include "sdspi.h"
- #include "hal_spi.h"
- SDSPI_ApiRetStatus_Type sdspi_spi_init(void);
- SDSPI_ApiRetStatus_Type sdspi_spi_freq(uint32_t hz);
- SDSPI_ApiRetStatus_Type sdspi_spi_xfer(uint8_t *in, uint8_t *out, uint32_t len);
- const SDSPI_Interface_Type board_sdspi_if ={
- .baudrate = 1000000u, /* 1mhz. */
- .spi_init = sdspi_spi_init,
- .spi_freq = sdspi_spi_freq,
- .spi_xfer = sdspi_spi_xfer};
- uint32_t board_sdspi_delay_count;
- static void board_sdspi_delay(uint32_t count){
- for (uint32_t i = count; i > 0u; i--){
- __NOP();}}
- SDSPI_ApiRetStatus_Type sdspi_spi_init(void){
- GPIO_WriteBit(BOARD_SDSPI_CS_GPIO_PORT , BOARD_SDSPI_CS_GPIO_PIN , 1u);
-
- /* Setup SPI module. */
- SPI_Master_Init_Type spi_init;
- spi_init.ClockFreqHz = CLOCK_APB1_FREQ;
- spi_init.BaudRate = SDMMC_CLOCK_400KHZ;
- spi_init.XferMode = SPI_XferMode_TxRx;
- spi_init.PolPha = SPI_PolPha_Alt2;
- spi_init.DataWidth = SPI_DataWidth_8b;
- spi_init.LSB = false;
- spi_init.AutoCS = true;
- SPI_InitMaster(SPI3, &spi_init);
-
- /* Enable SPI. */
- SPI_Enable(SPI3, true);
- // board_sdspi_delay_count = 100u;
- return SDSPI_ApiRetStatus_Success;}
- void SPI_SetBaudRate(SPI_Type * SPIx, uint32_t src_clk, uint32_t baudrate){
- uint32_t div = src_clk / baudrate;
- if (div < 2u){
- /* div = 0, 1 is not allowed. */
- div = 2u;}
6.附件
fatfs sdspi listfiles样例:
plus-f5270_fatfs_sdspi_basic_mdk.zip
(3.21 MB, 下载次数: 9)
fatfs sdspi listfiles样例:
plus-f5270_fatfs_sdspi_listfiles_mdk.zip
(3.21 MB, 下载次数: 10)
[/pay]