摘要:本文主要向大家介绍了Flash基础入门的stm32 Flash读写[库函数],通过具体的代码展示,希望对大家在Flash基础入门的路上走得更远。
本文主要向大家介绍了Flash基础入门的stm32 Flash读写[库函数],通过具体的代码展示,希望对大家在Flash基础入门的路上走得更远。
一. stm32的FLASH分为
1.主存储块:用于保存具体的程序代码和用户数据,主存储块是以页为单位划分的,
一页大小为1KB。范围为从地址0x08000000开始的128KB内。
2.信息块 :用于负责由stm32出厂是放置2KB的启动程序(Bootloader)和512B的用户配置信息区
二.操作原则
对Flash 的写入操作要 “先擦除后写入”的原则;
stm32的内置flash 编程操作都是以页为单位写入的,而写入的操作必须要以16位半字宽度数据位单位,允许跨页写,写入非16位数据时将导致stm32内部总线错误。
进行内置flash读写时,必须要打开内部Rc振荡器。
头文件:
[cpp] view plain copy 1. #ifndef __BSP_FLASH_H 2. #define __BSP_FLASH_H 3. #include "stm32f10x.h" 4. 5. typedef enum { 6. FLASH_WRITE_OK = 1, 7. FLASH_WRIKE_NO = 0, 8. FLASH_READ_OK = 1, 9. FLASH_READ_NO = 0 10. }FLASH_FLAG ; 11. 12. extern unsigned char FLASH_WRITE(unsigned short int * memory_data); 13. extern unsigned char FLASH_READ(unsigned short int * memory_data,unsigned short int n); 14. 15. 16. #endif /* __LED_H */
读写函数:
[cpp] view plain copy 1. #include "bsp_flash.h" 2. 3. FLASH_FLAG FLASH_STATUS; 4. static u32 count = 0; 5. /**--------------------------------------------------------------------------------- 6. * @brief FLASH写入必不可少步骤 7. * @param 输入数据起始地址 8. * @retval 函数有无执行完毕 9. --------------------------------------------------------------------------------*/ 10. 11. unsigned char FLASH_WRITE(unsigned short int * memory_data) 12. { 13. static int t; 14. count = 0; 15. /*Enables or disables the Internal High Speed oscillator (HSI).*/ 16. RCC_HSICmd(ENABLE); 17. /*打开FLASH可擦除控制器*/ 18. FLASH_Unlock(); 19. /*将flash三个标志位全清*/ 20. FLASH_ClearFlag(FLASH_FLAG_EOP|FLASH_FLAG_PGERR|FLASH_FLAG_WRPRTERR); 21. FLASH_ErasePage(0x8002000); 22. t = sizeof((const char *)memory_data); 23. while(count <= t) 24. { 25. /*flash 为一个字节存储,16位数据必须地址加2*/ 26. FLASH_ProgramHalfWord((0x8002000 +count*2),*(memory_data+count)); 27. count++; 28. if(count > t) 29. { 30. FLASH_STATUS = FLASH_WRIKE_NO; 31. return FLASH_STATUS; 32. } 33. } 34. /* Locks the FLASH Program Erase Controller.*/ 35. FLASH_Lock(); 36. RCC_HSICmd(DISABLE); 37. FLASH_STATUS = FLASH_WRITE_OK; 38. return FLASH_STATUS; 39. } 40. 41. /**--------------------------------------------------------------------------------- 42. * @brief FLASH读出函数 43. * @param memory_data:输出存放地址,n 输出个数 44. * @retval 函数有无执行完毕 45. --------------------------------------------------------------------------------*/ 46. unsigned char FLASH_READ(unsigned short int * memory_data,unsigned short int n) 47. { 48. count = 0; 49. while(count < n) 50. { 51. *(memory_data+count) = *(u16 *)(0x8002000 + count*2); 52. printf("\r %d \r",*(memory_data+count)); //读取 53. count++; 54. if(count > n) 55. { 56. FLASH_STATUS = FLASH_READ_NO; 57. return FLASH_STATUS; 58. } 59. } 60. //FLASH_ErasePage(0x8002000); 61. FLASH_STATUS = FLASH_READ_OK; 62. return FLASH_STATUS; 63. } 64. 65. /*********************************************END OF FILE**********************/
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标常用软件Flash频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号