打印
[DemoCode下载]

SPI FLASH移植FATFS

[复制链接]
2001|8
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
a_ziliu|  楼主 | 2016-12-26 10:48 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
SPI FLASH移植FATFS
EC_NUC230_240_SPI_FATFS.zip (1.39 MB)
沙发
643757107| | 2016-12-26 15:20 | 只看该作者
FATFS作为轻量级的文件系统,真心好用,51单片机都可以搞。

使用特权

评论回复
板凳
598330983| | 2016-12-26 16:08 | 只看该作者
http://elm-chan.org/fsw/ff/ff12b.zip
最新版的下载地址

使用特权

评论回复
地板
huangcunxiake| | 2016-12-31 10:46 | 只看该作者
/******************************************************************************
* [url=home.php?mod=space&uid=288409]@file[/url]     main.c
* [url=home.php?mod=space&uid=895143]@version[/url]  V2.00
* $Revision: 3 $
* $Date: 15/04/16 2:18p $
* [url=home.php?mod=space&uid=247401]@brief[/url]    NUC230_240 Series SPI Driver Sample Code
*
* @note
* Copyright (C) 2014 Nuvoton Technology Corp. All rights reserved.
*****************************************************************************/
#include <stdio.h>
#include "NUC230_240.h"
#include "SPI_FLASH.h"
#include "diskio.h"
#include "ff.h"

/* Function prototype declaration */
void SYS_Init(void);
unsigned int MidDid;
volatile UINT Timer = 0;                /* Performance timer (1kHz increment) */
DWORD acc_size;                                /* Work register for fs command */
WORD acc_files, acc_dirs;
FILINFO Finfo;
FATFS FatFs[_DRIVES];                /* File system object for logical drive */
char Line[256];                                /* Console input buffer */
#if _USE_LFN
char Lfname[512];
#endif
BYTE Buff[1024] ;                /* Working buffer */

   uint8_t data[512];
  uint32_t sd_size;
       
void put_rc (FRESULT rc)
{
    const TCHAR *p =
        _T("OK\0DISK_ERR\0INT_ERR\0NOT_READY\0NO_FILE\0NO_PATH\0INVALID_NAME\0")
        _T("DENIED\0EXIST\0INVALID_OBJECT\0WRITE_PROTECTED\0INVALID_DRIVE\0")
        _T("NOT_ENABLED\0NO_FILE_SYSTEM\0MKFS_ABORTED\0TIMEOUT\0LOCKED\0")
        _T("NOT_ENOUGH_CORE\0TOO_MANY_OPEN_FILES\0");

    uint32_t i;
    for (i = 0; (i != (UINT)rc) && *p; i++) {
        while(*p++) ;
    }
    printf(_T("rc=%u FR_%s\n"), (UINT)rc, p);
}

/* ------------- */
/* Main function */
/* ------------- */
int main(void)
{
          uint32_t p1, s1, s2;
        FATFS *fs;                                /* Pointer to file system object */
        DIR dir;                                /* Directory object */
        FILINFO Finfo;

        char *ptr="\\";
        FRESULT res;   

    /* Unlock protected registers */
    SYS_UnlockReg();

    /* Init system, peripheral clock and multi-function I/O */
    SYS_Init();

    /* Lock protected registers */
// SYS_LockReg();

    /* Configure UART0: 115200, 8-bit word, no parity bit, 1 stop bit. */
    UART_Open(UART0, 115200);

    SpiInit();

    /* Read MID & DID */
    MidDid = SpiReadMidDid();
    printf("\nMID and DID = %x\n\r", MidDid);
       
                    res        = (FRESULT)disk_initialize(0);       
        if(res)
        {
                put_rc(res);
                printf("\n\nDon't initialize SPI flash\n");
        }

        res        =f_mount(0, &FatFs[0]);       
        if(res)
        {
                put_rc(res);
                printf("Don't mount file system\n");
        }         
        #if 0
         res = f_mkfs(0, 0, _MAX_SS);
                if(res)
        {
                put_rc(res);
                printf("Don't format\n");
        }         
        #endif
        // List direct information
        put_rc(f_opendir(&dir, ptr));
        p1 = s1 = s2 = 0;
        for(;;)
        {
                res = f_readdir(&dir, &Finfo);
                if ((res != FR_OK) || !Finfo.fname[0]) break;
                if (Finfo.fattrib & AM_DIR){
                        s2++;
                } else {
                        s1++; p1 += Finfo.fsize;
                }
                printf("%c%c%c%c%c %u/%02u/%02u %02u:%02u %9lu  %s\n",
                        (Finfo.fattrib & AM_DIR) ? 'D' : '-',
                        (Finfo.fattrib & AM_RDO) ? 'R' : '-',
                        (Finfo.fattrib & AM_HID) ? 'H' : '-',
                        (Finfo.fattrib & AM_SYS) ? 'S' : '-',
                        (Finfo.fattrib & AM_ARC) ? 'A' : '-',
                        (Finfo.fdate >> 9) + 1980, (Finfo.fdate >> 5) & 15, Finfo.fdate & 31,
                        (Finfo.ftime >> 11), (Finfo.ftime >> 5) & 63, Finfo.fsize, Finfo.fname);
        }
          printf("%4u File(s),%10lu bytes total\n%4u Dir(s)", s1, p1, s2);
          if (f_getfree(ptr, (DWORD*)&p1, &fs) == FR_OK)
                printf(", %10lu bytes free\n", p1 * fs->csize * 512);

       
    while(1);
}

void SYS_Init(void)
{
   
    /*---------------------------------------------------------------------------------------------------------*/
    /* Init System Clock                                                                                       */
    /*---------------------------------------------------------------------------------------------------------*/

    /* Enable Internal RC 22.1184 MHz clock */
    CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk);

    /* Waiting for Internal RC clock ready */
    CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk);
   
    /* Enable external 12 MHz XTAL */
    CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk);

    /* Waiting for clock ready */
    CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk);

    /* Set core clock rate as 72 MHz from PLL */
    CLK_SetCoreClock(72000000);

    /* Select HXT as the clock source of UART0 */
    CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_HXT, CLK_CLKDIV_UART(1));


    /* Enable UART peripheral clock */
    CLK_EnableModuleClock(UART0_MODULE);

    /*---------------------------------------------------------------------------------------------------------*/
    /* Init I/O Multi-function                                                                                 */
    /*---------------------------------------------------------------------------------------------------------*/

    /* Set PB multi-function pins for UART0 RXD and TXD */
    SYS->GPB_MFP = SYS_GPB_MFP_PB0_UART0_RXD | SYS_GPB_MFP_PB1_UART0_TXD;

    /* Update System Core Clock */
    /* User can use SystemCoreClockUpdate() to calculate SystemCoreClock and CyclesPerUs automatically. */
    SystemCoreClockUpdate();
}


使用特权

评论回复
5
huangcunxiake| | 2016-12-31 10:46 | 只看该作者
看了看这主函数,发现移植也不是很难。

使用特权

评论回复
6
天灵灵地灵灵| | 2017-1-14 20:38 | 只看该作者
使用了这个文件系统后,文件就可以通过文件指针来操作了。

使用特权

评论回复
7
捉虫天师| | 2017-1-16 17:21 | 只看该作者
时钟如果低了,是不是也可以正常。

使用特权

评论回复
8
yiyigirl2014| | 2017-1-17 21:55 | 只看该作者
还好不难理解,回头找个SD卡自己玩玩试试。

使用特权

评论回复
9
dongnanxibei| | 2017-1-23 20:25 | 只看该作者
楼主最近有什么其他的好资料没啊,发点学习学习

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

100

主题

295

帖子

6

粉丝