×
单片机 > 单片机程序设计 > 详情

STM flash数据读取

发布时间:2020-09-03 发布时间:
|
  1. ////  

  2. /////////**************************.h文件*********************************///////////////////////////////////  

  3. #ifndef _FLASH_CTRL_H_  

  4. #define _FLASH_CTRL_H_  

  5. #ifdef STM32F10X_HD  

  6. #define FLASHADDRSTART 0x0807F800 //  

  7. #define FLASHADDREND 0x08080000 //  

  8. #elif STM32F10X_MD  

  9. #define FLASHADDRSTART 0x0801FC00 //  

  10. #define FLASHADDREND 0x0801FFFF //  

  11. #elif STM32F10X_LD  

  12. #define FLASHADDRSTART 0x08001000 //  

  13. #define FLASHADDREND 0x080013FF   //  

  14. #endif  

  15.  #ifdef  STM32F10X_HD //大容量产品,flash>=256K  

  16. #define FLASH_PAGE_SIZE    ((u16)0x800)//2K  

  17. #elif STM32F10X_MD //小容量产品, flash <256K  

  18. #define FLASH_PAGE_SIZE  ((u16)0x400)//1K  

  19. #else  

  20. #define FLASH_PAGE_SIZE  ((u16)0x400)//1K  

  21. #endif  

  22. #define UCHAR unsigned char  

  23. #define CHAR  char  

  24. #define ULONG unsigned long    

  25. #define UINT unsigned int  

  26. #define boolen UCHAR  

  27. #ifndef true  

  28. #define true 1  

  29. #endif  

  30. #ifndef false   

  31. #define false 0  

  32. #endif  

  33. #define BLOCK_SIZE 64  

  34. typedef struct _tagFLASHWORDBUFF  

  35. {  

  36.  ULONG ulItems[BLOCK_SIZE/4];  

  37. }    

  38. FLASHWORDBUFF;  

  39. typedef struct _tagFLASHHALFWORDBUFF  

  40. {  

  41.  UINT ulItems[BLOCK_SIZE/2];    

  42. }FLASHHALFWORDBUFF;  

  43. typedef struct _tagFLASHBYTEBUFF  

  44. {  

  45.  UCHAR ulitems[BLOCK_SIZE];  

  46. }  

  47. FLASHBYTEBUFF;  

  48. boolen writeFlash(UCHAR* str,UINT len);   

  49. UINT readByteFlash(UINT len);  

  50. UINT readHalfWordFlash(UINT len);  

  51. UINT readWordFlash(UINT len);  

  52. #endif  

  53. /////////////////////////********************.c文件***************************///////////////////////////////////////////////  

  54. /* Includes ------------------------------------------------------------------*/  

  55. #include "flashctrl.h"  

  56. #include "stm32f10x_flash.h"  

  57. /* Private variables ---------------------------------------------------------*/  

  58. vu32 NbrOfPage = 0x00;  

  59. u32 EraseCounter = 0x00, Address = 0x00;  

  60. volatile FLASH_Status FLASHStatus;  

  61. FLASHBYTEBUFF flashBytebuff;  

  62. FLASHHALFWORDBUFF flashHalfWrodbuff;  

  63. FLASHWORDBUFF flashWordbuff;  

  64. /* Public function------------------------------------------------------------*/  

  65. /****************************************************************************** 

  66. * Function Name: writeFlash  

  67. * Description  : Erease the range (FLASHADDREND - FLASHADDRSTART) of flash,and 

  68. *                Write the string to it. 

  69. * input        : the writed of string - str, the len of str 

  70. * output       : write or erease success return 1, otherwise return 0. 

  71. *******************************************************************************/  

  72. boolen writeFlash(UCHAR* str,UINT len)  

  73. {  

  74.  FLASH_Unlock();  

  75.  NbrOfPage = ( FLASHADDREND - FLASHADDRSTART ) / FLASH_PAGE_SIZE;  

  76.  FLASH_ClearFlag(FLASH_FLAG_BSY | FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR);   

  77.  FLASHStatus = FLASH_COMPLETE;  

  78.  for(EraseCounter = 0; (EraseCounter 

  79.  {  

  80.       FLASHStatus = FLASH_ErasePage(FLASHADDRSTART + (FLASH_PAGE_SIZE * EraseCounter));  

  81.   }  

  82.  if(FLASHStatus != FLASH_COMPLETE )//擦除不成功  

  83.  {  

  84.   return false;  

  85.  }  

  86.  Address = FLASHADDRSTART;  

  87.  while((Address 

  88.  {  

  89.   //instr = (*str)|((*(str+1))<<8);  

  90.   if(len == 0) break;  

  91.   FLASHStatus = FLASH_ProgramWord(Address, *(u32*)str);//一次性写入四个字节=1个字  

  92.   Address = Address + 4;  

  93.   len-=4;  

  94.   str+=4;  

  95.   //str+=2;  

  96.   if(len 

  97.   {  

  98.    //由于是按字的写入方式,即一次性写入4个字节的数据,  

  99.    //所有后面剩余的(4-len%4)%4个字节的补零处理  

  100.    FLASHStatus = FLASH_ProgramWord(Address, *(u32*)str);  

  101.    break;  

  102.   }  

  103.  }  

  104.  FLASH_Lock();  

  105.  if(FLASHStatus != FLASH_COMPLETE)  

  106.  {  

  107.   return false;  

  108.  }  

  109.  else return true;  

  110. }  

  111. /****************************************************************************** 

  112. * Function Name: readByteFlash  

  113. * Description  : read specified length string from flash,the begin flash is : FLASHADDRSTART 

  114. *                A byte by byte to read. 

  115. * input        : the readed of string - str, the len of str 

  116. * output       : return the length of string take away 1 

  117. *******************************************************************************/  

  118. UINT readByteFlash(UINT len)//  

  119. {  

  120.  UINT i=0;  

  121.  Address = FLASHADDRSTART;  

  122.  for(i=0;i

  123.  {   

  124.   flashBytebuff.ulitems[i] = (*(__IO int8_t*) Address);  

  125.   Address+=1;  

  126.  }  

  127.  return i;  

  128. }  

  129. /****************************************************************************** 

  130. * Function Name: readHalfWordFlash  

  131. * Description  : read specified length string from flash,the begin flash is : FLASHADDRSTART 

  132. *                A Halfword by halfword to read. 

  133. * input        : the readed of string - str, the len of str 

  134. * output       : return the length of string take away 1 

  135. *******************************************************************************/  

  136. UINT readHalfWordFlash(UINT len)//  

  137. {  

  138.  UINT i=0;  

  139.  Address = FLASHADDRSTART;  

  140.  for(i=0;i

  141.  {   

  142.   flashHalfWrodbuff.ulItems[i] = (*(__IO uint16_t*) Address);  

  143.   Address+=2;  

  144.  }  

  145.  if(len/2 )  

  146.  {  

  147.   flashHalfWrodbuff.ulItems[i] = (*(__IO uint16_t*) Address);  

  148.  }  

  149.  return i;  

  150. }  

  151. /****************************************************************************** 

  152. * Function Name: readWordFlash  

  153. * Description  : read specified length string from flash,the begin flash is : FLASHADDRSTART 

  154. *                A word by word to read. 

  155. * input        : the readed of string - str, the len of str 

  156. * output       : return the length of string take away 1 

  157. *******************************************************************************/  

  158. UINT readWordFlash(UINT len)  

  159. {   

  160.  UINT i=0;  

  161.  Address = FLASHADDRSTART;  

  162.  for(i=0;i

  163.  {   

  164.   flashWordbuff.ulItems[i] = (*(__IO uint32_t*) Address);  

  165.   Address+=4;  

  166.     

  167.  }  

  168.  if(len/4)  

  169.  {  

  170.   flashWordbuff.ulItems[i] = (*(__IO uint32_t*) Address);  

  171.  }  

  172.  return i;  

  173. }  

  174. /***********************************the end of file*****************************************/




『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
C51 特殊功能寄存器SFR的名称和地址