Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4524198
  • 博文数量: 1148
  • 博客积分: 25453
  • 博客等级: 上将
  • 技术积分: 11949
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-06 21:14
文章分类

全部博文(1148)

文章存档

2012年(15)

2011年(1078)

2010年(58)

分类: 嵌入式

2011-07-03 20:49:02

工程代码:  3_fatfs.rar  

参考资料: http://blog.ednchina.com/nthq2004/307859/message.aspx智林STM32开发板上移植FatFs移植
           http://blog.ednchina.com/jjldc/190753/message.aspx 九九 FatFs文件系统移植


     经过半天的学习,终于将 fatfs 移植到了stm32上,我这次移植的fatfs是最新版本 ff8b在这个网站下载      。

     移植过程介绍下:
      
其中的 diskio.c 是我自己新建的。从 前几个版本 如 ff7c 中复制过来的。 diskio.c  实现的 低级io操作的, 我们移植,也就是针对这个 文件进行修改, 将 sd_spi.h 中实现的sd卡 初始化、 read write 函数功能  在 diskio.c  中实现

  1. /*-----------------------------------------------------------------------*/
  2. /* Low level disk I/O module skeleton for FatFs (C)ChaN, 2007 */
  3. /*-----------------------------------------------------------------------*/
  4. /* This is a stub disk I/O module that acts as front end of the existing */
  5. /* disk I/O modules and attach it to FatFs module with common interface. */
  6. /*-----------------------------------------------------------------------*/

  7. #include "diskio.h"
  8. #include "..\User\sd_spi.h"



  9. /*-----------------------------------------------------------------------*/
  10. /* Inidialize a Drive */

  11. DSTATUS disk_initialize (
  12.     BYTE drv                /* Physical drive nmuber (0..) */
  13. )
  14. {
  15.     u8 state;

  16.     if(drv)
  17.     {
  18.         return STA_NOINIT; //仅支持磁盘0的操作
  19.     }

  20.     state = SD_Init();
  21.     if(state == STA_NODISK)
  22.     {
  23.         return STA_NODISK;
  24.     }
  25.     else if(state != 0)
  26.     {
  27.         return STA_NOINIT; //其他错误:初始化失败
  28.     }
  29.     else
  30.     {
  31.         return 0; //初始化成功
  32.     }
  33. }



  34. /*-----------------------------------------------------------------------*/
  35. /* Return Disk Status */

  36. DSTATUS disk_status (
  37.     BYTE drv        /* Physical drive nmuber (0..) */
  38. )
  39. {
  40.     if(drv)
  41.     {
  42.         return STA_NOINIT; //仅支持磁盘0操作
  43.     }

  44.     //检查SD卡是否插入
  45.   // if(!SD_DET())
  46.    // {
  47.   // return STA_NODISK;
  48.   // }
  49.     return 0;
  50. }



  51. /*-----------------------------------------------------------------------*/
  52. /* Read Sector(s) */

  53. DRESULT disk_read (
  54.     BYTE drv,        /* Physical drive nmuber (0..) */
  55.     BYTE *buff,        /* Data buffer to store read data */
  56.     DWORD sector,    /* Sector address (LBA) */
  57.     BYTE count        /* Number of sectors to read (1..255) */
  58. )
  59. {
  60.     u8 res=0;
  61.     if (drv || !count)
  62.     {
  63.         return RES_PARERR; //仅支持单磁盘操作,count不能等于0,否则返回参数错误
  64.     }
  65.   // if(!SD_DET())
  66.   // {
  67.   // return RES_NOTRDY; //没有检测到SD卡,报NOT READY错误
  68.  // }

  69.     
  70.     
  71.     if(count==1) //1个sector的读操作
  72.     {
  73.         res = SD_ReadSingleBlock(sector, buff);
  74.     }
  75.     else //多个sector的读操作
  76.     {
  77.         res = SD_ReadMultiBlock(sector, buff, count);
  78.     }
  79.     /*
  80.     do
  81.     {
  82.         if(SD_ReadSingleBlock(sector, buff)!=0)
  83.         {
  84.             res = 1;
  85.             break;
  86.         }
  87.         buff+=512;
  88.     }while(--count);
  89.     */
  90.     //处理返回值,将SPI_SD_driver.c的返回值转成ff.c的返回值
  91.     if(res == 0x00)
  92.     {
  93.         return RES_OK;
  94.     }
  95.     else
  96.     {
  97.         return RES_ERROR;
  98.     }
  99. }



  100. /*-----------------------------------------------------------------------*/
  101. /* Write Sector(s) */

  102. #if _READONLY == 0
  103. DRESULT disk_write (
  104.     BYTE drv,            /* Physical drive nmuber (0..) */
  105.     const BYTE *buff,    /* Data to be written */
  106.     DWORD sector,        /* Sector address (LBA) */
  107.     BYTE count            /* Number of sectors to write (1..255) */
  108. )
  109. {
  110.     u8 res;

  111.     if (drv || !count)
  112.     {
  113.         return RES_PARERR; //仅支持单磁盘操作,count不能等于0,否则返回参数错误
  114.     }
  115.    // if(!SD_DET())
  116.   // {
  117.   // return RES_NOTRDY; //没有检测到SD卡,报NOT READY错误
  118.  // }

  119.     // 读写操作
  120.     if(count == 1)
  121.     {
  122.         res = SD_WriteSingleBlock(sector, buff);
  123.     }
  124.     else
  125.     {
  126.         res = SD_WriteMultiBlock(sector, buff, count);
  127.     }
  128.     // 返回值转换
  129.     if(res == 0)
  130.     {
  131.         return RES_OK;
  132.     }
  133.     else
  134.     {
  135.         return RES_ERROR;
  136.     }
  137. }
  138. #endif /* _READONLY */



  139. /*-----------------------------------------------------------------------*/
  140. /* Miscellaneous Functions */

  141. DRESULT disk_ioctl (
  142.     BYTE drv,        /* Physical drive nmuber (0..) */
  143.     BYTE ctrl,        /* Control code */
  144.     void *buff        /* Buffer to send/receive control data */
  145. )
  146. {
  147.     DRESULT res;


  148.     if (drv)
  149.     {
  150.         return RES_PARERR; //仅支持单磁盘操作,否则返回参数错误
  151.     }
  152.     
  153.     //FATFS目前版本仅需处理CTRL_SYNC,GET_SECTOR_COUNT,GET_BLOCK_SIZ三个命令
  154.     switch(ctrl)
  155.     {
  156.     case CTRL_SYNC:
  157.         SD_CS_ENABLE();
  158.         if(SD_WaitReady()==0)
  159.         {
  160.             res = RES_OK;
  161.         }
  162.         else
  163.         {
  164.             res = RES_ERROR;
  165.         }
  166.         SD_CS_DISABLE();
  167.         break;
  168.         
  169.     case GET_BLOCK_SIZE:
  170.         *(WORD*)buff = 512;
  171.         res = RES_OK;
  172.         break;

  173.     case GET_SECTOR_COUNT:
  174.         *(DWORD*)buff = SD_GetCapacity();
  175.         res = RES_OK;
  176.         break;
  177.     default:
  178.         res = RES_PARERR;
  179.         break;
  180.     }

  181.     return res;
  182. }


  183. /*-----------------------------------------------------------------------*/
  184. /* User defined function to give a current time to fatfs module */
  185. /* 31-25: Year(0-127 org.1980), 24-21: Month(1-12), 20-16: Day(1-31) */
  186. /* 15-11: Hour(0-23), 10-5: Minute(0-59), 4-0: Second(0-29 *2) */
  187. DWORD get_fattime (void)
  188. {
  189.   // struct tm t;
  190.   // DWORD date;
  191.   // t = Time_GetCalendarTime();
  192.   // t.tm_year -= 1980;        //年份改为1980年起
  193.   // t.tm_mon++;     //0-11月改为1-12月
  194.   // t.tm_sec /= 2;     //将秒数改为0-29
  195.     
  196.   // date = 0;
  197.   // date = (t.tm_year << 25)|(t.tm_mon<<21)|(t.tm_mday<<16)|\
  198.   // (t.tm_hour<<11)|(t.tm_min<<5)|(t.tm_sec);

  199.    // return date;
  200.    return 0;
  201. }
程序的测试代码:
   
  1. /*
  2. *    Author :     余威先
  3. *    Date:         2011.6.19    
  4. * 开发板上:PD2 ~ LED2
  5. * PA8 ~ LED0
  6. * PA15 ~ KEY1
  7. * PA13 ~ KEY2
  8. * 修改Date: 2011.6.30 19:20
  9. * rcc时钟配置: SYSCLK = 16MHZ
  10. * AHB = 16MHZ
  11. *              APB1 = 8 MHZ // TIM2 TIM3 TIM4
  12. *                 APB2 = 16 MHZ //GPIO A B C D
  13. * 修改Date:2011.7.3 21:00
  14.     简单描述:
  15.      移植 fatfs 成功 ff8b
  16.     
  17. */
  18. #include "stm32f10x.h"
  19. #include "rcc.h"
  20. #include "systick.h"
  21. #include "led.h"
  22. #include "delay.h"
  23. //#include "key.h"
  24. #include "tim3.h"
  25. #include "usart1.h"
  26. #include "lcd.h"
  27. #include "rtc.h"
  28. #include "flash.h"
  29. #include "sd_spi.h"
  30. #include "..\FATS\ff.h"
  31. #include "..\FATS\integer.h"
  32. #include "..\FATS\ffconf.h"
  33. #include "..\FATS\diskio.h"

  34. volatile u8 sec = 0; // 全局变量 秒 时 小时
  35. volatile u8 min = 0;
  36. volatile u8 hour = 0;

  37. FATFS fs;
  38. FRESULT res;
  39. FIL file;

  40. u8 send_buffer[512] = {97,6};
  41. u8 receiv_buffer[512] = {0,0};
  42. u32 capacity = 0;
  43. void write_file(void);

  44. int main(void)
  45. {    
  46.     u16 i = 0;//

  47.     RCC_Configuration(); //系统时钟配置
  48.     delay_init();     // 延时 初始化

  49. //    RTC_Configuration(); //RTC系统 配置
  50. //    RTC_NVIC_Configuration(); //RTC中断配置
  51. //    RTC_Init();// RTC 时钟初始化

  52.     SPI1_Configuration(); //SPI1 初始化
  53. //    SD_Init();             //SD卡 初始化

  54.     LCD_Init();         //LCD 彩屏初始化

  55.     write_cmd(0x2C); //LCD 写数据命令
  56.     DrawFull_single_colour(0xff, 0xff);    //显示 纯白色

  57.     capacity = SD_GetCapacity();    //获取 容量
  58.     LCD_show_number(48,128,capacity); //打印低16位
  59.     LCD_show_number(0,128,capacity>>16); //打印高16位

  60.     LCD_PutString(0,0, "start to write file..");
  61.     write_file();
  62. #if 0
  63.     for(i = 0; i < 256; i++) //发送数据填充
  64.         send_buffer[i] = i;    
  65.     
  66.     for(i = 0; i < 256; i++) //发送数据填充
  67.         send_buffer[i + 256] = i;    

  68.     SD_WriteSingleBlock(0, send_buffer); //写数据到 块 中
  69.     SD_ReadSingleBlock(0, receiv_buffer); //从 块 中 读数据

  70.     for(i = 0; i < 512; i++) // 显示从块中读取到的数据, 一个字节最大255
  71.     {
  72.         LCD_show_number(8,32,receiv_buffer[i]);
  73.         delay_s(1);
  74.     }
  75. #endif

  76.     while(1) //无限循环, 中断中 显示 秒时钟
  77.     {
  78.         #if 0
  79.         LCD_show_number_2(40,16,hour);
  80.         LCD_show_number_2(64,16,min);
  81.         LCD_show_number_2(88,16,sec);    
  82.         #endif
  83.     }
  84.     return 0;
  85. }


  86. void write_file(void)
  87. {
  88.        UINT br;
  89.     u16 i;
  90.     s8 data[512];
  91.     for(i=0;i<10;i++)
  92.     {
  93.         data[i] = 'o';
  94.     }
  95.     for(i=10;i<20;i++)
  96.     {
  97.         data[i] = 'p';
  98.     }
  99.     for(i=20;i<30;i++)
  100.     {
  101.         data[i] = 'q';
  102.     }
  103.     data[30]='\n';

  104.     res = f_mount(0, &fs);
  105.     if(res != 0)
  106.         LCD_PutString(0,16, "f_mount failed.");
  107.     else
  108.         LCD_PutString(0,16, "f_mount successed.");

  109.     res = f_open(&file, "test.txt", FA_CREATE_ALWAYS | FA_WRITE);
  110.     if(res != 0)
  111.         LCD_PutString(0,32, "f_open failed.");
  112.     else
  113.         LCD_PutString(0,32, "f_open successed.");
  114.         
  115.     res = f_write(&file, data, 512, &br);
  116.     if(res != 0)
  117.         LCD_PutString(0,48, "f_write failed.");
  118.     else
  119.         LCD_PutString(0,48, "f_write successed.");
  120.              
  121.     f_close(&file);
  122.         LCD_PutString(0,64, "f_close successed.");
  123. }

  124. void RTC_IRQHandler(void) //中断函数
  125. {
  126.     if (RTC_GetITStatus(RTC_IT_SEC) != RESET)
  127.     {
  128.         RTC_ClearITPendingBit(RTC_IT_SEC);//清除 RTC秒中断标志
  129.         sec ++;// 定义的全局变量
  130.         if(sec ==60)
  131.         {
  132.             sec = 0;
  133.             min ++;
  134.             if(min == 60)
  135.             {
  136.                 min = 0;
  137.                 hour++;
  138.                 if(hour==12)
  139.                     hour = 0;
  140.             }
  141.         }    
  142.         RTC_WaitForLastTask();//等待就绪
  143.     }    
  144. }



SD卡写入后 的文件



在 winhex 下 显示









阅读(5342) | 评论(0) | 转发(1) |
0

上一篇:V3.5 SPI 代码

下一篇:FATFS 的几个函数用法

给主人留下些什么吧!~~