[DemoCode下载] SPI FLASH移植FATFS

[复制链接]
2326|8
 楼主| a_ziliu 发表于 2016-12-26 10:48 | 显示全部楼层 |阅读模式
SPI FLASH移植FATFS
EC_NUC230_240_SPI_FATFS.zip (1.39 MB, 下载次数: 85)
643757107 发表于 2016-12-26 15:20 | 显示全部楼层
FATFS作为轻量级的文件系统,真心好用,51单片机都可以搞。
598330983 发表于 2016-12-26 16:08 | 显示全部楼层
huangcunxiake 发表于 2016-12-31 10:46 | 显示全部楼层
  1. /******************************************************************************
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     main.c
  3. * [url=home.php?mod=space&uid=895143]@version[/url]  V2.00
  4. * $Revision: 3 $
  5. * $Date: 15/04/16 2:18p $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    NUC230_240 Series SPI Driver Sample Code
  7. *
  8. * @note
  9. * Copyright (C) 2014 Nuvoton Technology Corp. All rights reserved.
  10. *****************************************************************************/
  11. #include <stdio.h>
  12. #include "NUC230_240.h"
  13. #include "SPI_FLASH.h"
  14. #include "diskio.h"
  15. #include "ff.h"

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

  29.    uint8_t data[512];
  30.   uint32_t sd_size;
  31.        
  32. void put_rc (FRESULT rc)
  33. {
  34.     const TCHAR *p =
  35.         _T("OK\0DISK_ERR\0INT_ERR\0NOT_READY\0NO_FILE\0NO_PATH\0INVALID_NAME\0")
  36.         _T("DENIED\0EXIST\0INVALID_OBJECT\0WRITE_PROTECTED\0INVALID_DRIVE\0")
  37.         _T("NOT_ENABLED\0NO_FILE_SYSTEM\0MKFS_ABORTED\0TIMEOUT\0LOCKED\0")
  38.         _T("NOT_ENOUGH_CORE\0TOO_MANY_OPEN_FILES\0");

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

  45. /* ------------- */
  46. /* Main function */
  47. /* ------------- */
  48. int main(void)
  49. {
  50.           uint32_t p1, s1, s2;
  51.         FATFS *fs;                                /* Pointer to file system object */
  52.         DIR dir;                                /* Directory object */
  53.         FILINFO Finfo;

  54.         char *ptr="\";
  55.         FRESULT res;   

  56.     /* Unlock protected registers */
  57.     SYS_UnlockReg();

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

  60.     /* Lock protected registers */
  61. // SYS_LockReg();

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

  64.     SpiInit();

  65.     /* Read MID & DID */
  66.     MidDid = SpiReadMidDid();
  67.     printf("\nMID and DID = %x\n\r", MidDid);
  68.        
  69.                     res        = (FRESULT)disk_initialize(0);       
  70.         if(res)
  71.         {
  72.                 put_rc(res);
  73.                 printf("\n\nDon't initialize SPI flash\n");
  74.         }

  75.         res        =f_mount(0, &FatFs[0]);       
  76.         if(res)
  77.         {
  78.                 put_rc(res);
  79.                 printf("Don't mount file system\n");
  80.         }         
  81.         #if 0
  82.          res = f_mkfs(0, 0, _MAX_SS);
  83.                 if(res)
  84.         {
  85.                 put_rc(res);
  86.                 printf("Don't format\n");
  87.         }         
  88.         #endif
  89.         // List direct information
  90.         put_rc(f_opendir(&dir, ptr));
  91.         p1 = s1 = s2 = 0;
  92.         for(;;)
  93.         {
  94.                 res = f_readdir(&dir, &Finfo);
  95.                 if ((res != FR_OK) || !Finfo.fname[0]) break;
  96.                 if (Finfo.fattrib & AM_DIR){
  97.                         s2++;
  98.                 } else {
  99.                         s1++; p1 += Finfo.fsize;
  100.                 }
  101.                 printf("%c%c%c%c%c %u/%02u/%02u %02u:%02u %9lu  %s\n",
  102.                         (Finfo.fattrib & AM_DIR) ? 'D' : '-',
  103.                         (Finfo.fattrib & AM_RDO) ? 'R' : '-',
  104.                         (Finfo.fattrib & AM_HID) ? 'H' : '-',
  105.                         (Finfo.fattrib & AM_SYS) ? 'S' : '-',
  106.                         (Finfo.fattrib & AM_ARC) ? 'A' : '-',
  107.                         (Finfo.fdate >> 9) + 1980, (Finfo.fdate >> 5) & 15, Finfo.fdate & 31,
  108.                         (Finfo.ftime >> 11), (Finfo.ftime >> 5) & 63, Finfo.fsize, Finfo.fname);
  109.         }
  110.           printf("%4u File(s),%10lu bytes total\n%4u Dir(s)", s1, p1, s2);
  111.           if (f_getfree(ptr, (DWORD*)&p1, &fs) == FR_OK)
  112.                 printf(", %10lu bytes free\n", p1 * fs->csize * 512);

  113.        
  114.     while(1);
  115. }

  116. void SYS_Init(void)
  117. {
  118.    
  119.     /*---------------------------------------------------------------------------------------------------------*/
  120.     /* Init System Clock                                                                                       */
  121.     /*---------------------------------------------------------------------------------------------------------*/

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

  124.     /* Waiting for Internal RC clock ready */
  125.     CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk);
  126.    
  127.     /* Enable external 12 MHz XTAL */
  128.     CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk);

  129.     /* Waiting for clock ready */
  130.     CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk);

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

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


  135.     /* Enable UART peripheral clock */
  136.     CLK_EnableModuleClock(UART0_MODULE);

  137.     /*---------------------------------------------------------------------------------------------------------*/
  138.     /* Init I/O Multi-function                                                                                 */
  139.     /*---------------------------------------------------------------------------------------------------------*/

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

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


huangcunxiake 发表于 2016-12-31 10:46 | 显示全部楼层
看了看这主函数,发现移植也不是很难。
天灵灵地灵灵 发表于 2017-1-14 20:38 | 显示全部楼层
使用了这个文件系统后,文件就可以通过文件指针来操作了。
捉虫天师 发表于 2017-1-16 17:21 | 显示全部楼层
时钟如果低了,是不是也可以正常。
yiyigirl2014 发表于 2017-1-17 21:55 | 显示全部楼层
还好不难理解,回头找个SD卡自己玩玩试试。
dongnanxibei 发表于 2017-1-23 20:25 | 显示全部楼层
楼主最近有什么其他的好资料没啊,发点学习学习
您需要登录后才可以回帖 登录 | 注册

本版积分规则

100

主题

310

帖子

6

粉丝
快速回复 在线客服 返回列表 返回顶部