Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1742999
  • 博文数量: 1493
  • 博客积分: 38
  • 博客等级: 民兵
  • 技术积分: 5834
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-19 17:28
文章分类

全部博文(1493)

文章存档

2016年(11)

2015年(38)

2014年(137)

2013年(253)

2012年(1054)

2011年(1)

分类:

2012-11-27 09:01:09

 
头文件
文件名:FLASH.h

点击(此处)折叠或打开

  1. #ifndef __FLASH_H__
  2. #define __FLASH_H__


  3. #include "stm32f10x.h"
  4. #include "stm32f10x_flash.h"




  5. #if defined (STM32F10X_HD) || defined (STM32F10X_HD_VL) || defined (STM32F10X_CL) || defined (STM32F10X_XL)
  6. #define FLASH_PAGE_SIZE ((uint16_t)0x800)

  7. #else
  8. #define FLASH_PAGE_SIZE ((uint16_t)0x400)
  9. #endif




  10. int Flash_Read(uint32_t iAddress, uint8_t *buf, int32_t iNbrToRead) ;
  11. int Flash_Write(uint32_t iAddress, uint8_t *buf, uint32_t iNbrToWrite);


  12. #endif


源文件:

文件名:FLASH.c

点击(此处)折叠或打开

  1. #include "FLASH.h"

  2. uint16_t Flash_Write_Without_check(uint32_t iAddress, uint8_t *buf, uint16_t iNumByteToWrite)
  3. {
  4.     uint16_t i;
  5.     volatile FLASH_Status FLASHStatus = FLASH_COMPLETE;
  6.     i = 0;

  7.     // FLASH_UnlockBank1();
  8.     while((i < iNumByteToWrite) && (FLASHStatus == FLASH_COMPLETE))
  9.     {
  10.         FLASHStatus = FLASH_ProgramHalfWord(iAddress, *(uint16_t*)buf);
  11.         i = i+2;
  12.         iAddress = iAddress + 2;
  13.         buf = buf + 2;
  14.     }

  15.     return iNumByteToWrite;
  16. }
  17. /**
  18. * @brief Programs a half word at a specified Option Byte Data address.
  19. * @note This function can be used for all STM32F10x devices.
  20. * @param Address: specifies the address to be programmed.
  21. * @param buf: specifies the data to be programmed.
  22. * @param iNbrToWrite: the number to write into flash
  23. * @retval if success return the number to write, -1 if error
  24. *
  25. */
  26. int Flash_Write(uint32_t iAddress, uint8_t *buf, uint32_t iNbrToWrite)
  27. {
  28.     /* Unlock the Flash Bank1 Program Erase controller */
  29.     uint32_t secpos;
  30.     uint32_t iNumByteToWrite = iNbrToWrite;
  31.     uint16_t secoff;
  32.     uint16_t secremain;
  33.     uint16_t i = 0;
  34.     uint8_t tmp[FLASH_PAGE_SIZE];

  35.     FLASH_UnlockBank1();
  36.     secpos=iAddress & (~(FLASH_PAGE_SIZE -1 )) ;//扇区地址
  37.     secoff=iAddress & (FLASH_PAGE_SIZE -1); //在扇区内的偏移
  38.     secremain=FLASH_PAGE_SIZE-secoff; //扇区剩余空间大小
  39.     volatile FLASH_Status FLASHStatus = FLASH_COMPLETE;

  40.     if(iNumByteToWrite<=secremain) secremain = iNumByteToWrite;//不大于4096个字节

  41.     while( 1 )
  42.     {
  43.         Flash_Read(secpos, tmp, FLASH_PAGE_SIZE); //读出整个扇区
  44.         for(i=0;i<secremain;i++)
  45.         { //校验数据
  46.             if(tmp[secoff+i]!=0XFF)break; //需要擦除
  47.         }
  48.         if(i<secremain)
  49.         { //需要擦除
  50.             FLASHStatus = FLASH_ErasePage(secpos); //擦除这个扇区
  51.             if(FLASHStatus != FLASH_COMPLETE)
  52.             return -1;
  53.             for(i=0;i<secremain;i++) //复制
  54.             {
  55.                 tmp[i+secoff]=buf[i];
  56.             }
  57.             Flash_Write_Without_check(secpos ,tmp ,FLASH_PAGE_SIZE);//写入整个扇区
  58.         }
  59.         else
  60.         {
  61.             Flash_Write_Without_check(iAddress,buf,secremain);//写已经擦除了的,直接写入扇区剩余区间.
  62.         }

  63.         if(iNumByteToWrite==secremain) //写入结束了
  64.         {
  65.             break;
  66.         }
  67.         else
  68.         {
  69.             secpos += FLASH_PAGE_SIZE;
  70.             secoff = 0;//偏移位置为0
  71.             buf += secremain; //指针偏移
  72.             iAddress += secremain;//写地址偏移
  73.             iNumByteToWrite -= secremain; //字节数递减
  74.             if(iNumByteToWrite>FLASH_PAGE_SIZE) secremain=FLASH_PAGE_SIZE;//下一个扇区还是写不完
  75.             else secremain = iNumByteToWrite; //下一个扇区可以写完了
  76.         }
  77.     }
  78.     FLASH_LockBank1();
  79.     return iNbrToWrite;
  80. }






  81. /**
  82. * @brief Programs a half word at a specified Option Byte Data address.
  83. * @note This function can be used for all STM32F10x devices.
  84. * @param Address: specifies the address to be programmed.
  85. * @param buf: specifies the data to be programmed.
  86. * @param iNbrToWrite: the number to read from flash
  87. * @retval if success return the number to write, without error
  88. *
  89. */
  90. int Flash_Read(uint32_t iAddress, uint8_t *buf, int32_t iNbrToRead)
  91. {
  92.     int i = 0;
  93.     while(i < iNbrToRead)
  94.     {
  95.         *(buf + i) = *(__IO uint8_t*) iAddress++;
  96.         i++;
  97.     }
  98.     return i;
  99. }


 


阅读(387) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~