日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

STM32:Flash擦除与读写操作(HAL库)

發布時間:2025/3/21 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 STM32:Flash擦除与读写操作(HAL库) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
  • 應用平臺:STM32F030F4P6
  • ST官方庫:STM32Cube_FW_F0_V1.9.0

背景知識

?


  • 絕大多數的單片機和微控制器(ARM,x86),地址空間都是以字節為單位的,也就是說一個地址是一個字節。
  • Flash存儲器有個特點,就是只能寫0,不能寫1。所以如果原來的地址有數據了,意味著有一些位為0,這些位就相當于無效了。所以必須寫之前確保他們都為1,只有擦除才可以。另外每次擦除都必須擦除一個4K大小的扇區,這是flash的特性所決定的。
  • 對Flash操作前必需打開內部振蕩器。?

?

參考:stm32的學習—FLASH的操作和使用

STM32F030F4P6的Flash存儲簡介

STM32F030F4P6硬件配置:??FLASH (16KB)??RAM (4KB)?
(包含4個扇區,1個扇區包含4個頁,每頁有1Kbyte空間)?
用戶可以對Flash進行program?和?erase?操作。

Main Flash memory programming?
The main Flash memory can be programmed?16 bits?at a time.?
Flash memory erase?
The Flash memory can be erased page?by page?or completely (Mass Erase).?

Flash memory addressesSize(byte)NameDescription
0x0800 0000 - 0x0800 03FF1 KbytePage 0Sector 0
0x0800 0400 - 0x0800 07FF1 KbytePage 1Sector 0
0x0800 0800 - 0x0800 0BFF1 KbytePage 2Sector 0
0x0800 0C00 - 0x0800 0FFF1 KbytePage 3Sector 0
0x0800 1000 - 0x0800 13FF1 KbytePage 4Sector 1
0x0800 1400 - 0x0800 17FF1 KbytePage 5Sector 1
0x0800 1800 - 0x0800 1BFF1 KbytePage 6Sector 1
0x0800 1C00 - 0x0800 1FFF1 KbytePage 7Sector 1
0x0800 2000 - 0x0800 23FF1 KbytePage 8Sector 2
0x0800 2400 - 0x0800 27FF1 KbytePage 9Sector 2
0x0800 2800 - 0x0800 2BFF1 KbytePage 10Sector 2
0x0800 2C00 - 0x0800 2FFF1 KbytePage 11Sector 2
0x0800 3000 - 0x0800 33FF1 KbytePage 12Sector 3
0x0800 3400 - 0x0800 37FF1 KbytePage 13Sector 3
0x0800 3800 - 0x0800 3BFF1 KbytePage 14Sector 3
0x0800 3C00 - 0x0800 3FFF1 KbytePage 15Sector 3

STM32F030F4P6的Flash讀寫參考代碼(HAL庫)

/* Base address of the Flash sectors */ #define ADDR_FLASH_PAGE_0 ((uint32_t)0x08000000) /* Base @ of Page 0, 1 Kbyte */ #define ADDR_FLASH_PAGE_1 ((uint32_t)0x08000400) /* Base @ of Page 1, 1 Kbyte */ #define ADDR_FLASH_PAGE_2 ((uint32_t)0x08000800) /* Base @ of Page 2, 1 Kbyte */ #define ADDR_FLASH_PAGE_3 ((uint32_t)0x08000C00) /* Base @ of Page 3, 1 Kbyte */ #define ADDR_FLASH_PAGE_4 ((uint32_t)0x08001000) /* Base @ of Page 4, 1 Kbyte */ #define ADDR_FLASH_PAGE_5 ((uint32_t)0x08001400) /* Base @ of Page 5, 1 Kbyte */ #define ADDR_FLASH_PAGE_6 ((uint32_t)0x08001800) /* Base @ of Page 6, 1 Kbyte */ #define ADDR_FLASH_PAGE_7 ((uint32_t)0x08001C00) /* Base @ of Page 7, 1 Kbyte */ #define ADDR_FLASH_PAGE_8 ((uint32_t)0x08002000) /* Base @ of Page 8, 1 Kbyte */ #define ADDR_FLASH_PAGE_9 ((uint32_t)0x08002400) /* Base @ of Page 9, 1 Kbyte */ #define ADDR_FLASH_PAGE_10 ((uint32_t)0x08002800) /* Base @ of Page 10, 1 Kbyte */ #define ADDR_FLASH_PAGE_11 ((uint32_t)0x08002C00) /* Base @ of Page 11, 1 Kbyte */ #define ADDR_FLASH_PAGE_12 ((uint32_t)0x08003000) /* Base @ of Page 12, 1 Kbyte */ #define ADDR_FLASH_PAGE_13 ((uint32_t)0x08003400) /* Base @ of Page 13, 1 Kbyte */ #define ADDR_FLASH_PAGE_14 ((uint32_t)0x08003800) /* Base @ of Page 14, 1 Kbyte */ #define ADDR_FLASH_PAGE_15 ((uint32_t)0x08003C00) /* Base @ of Page 15, 1 Kbyte *//* Private define ------------------------------------------------------------*/ #define FLASH_USER_START_ADDR ADDR_FLASH_PAGE_15 /* Start @ of user Flash area */ #define FLASH_USER_END_ADDR ADDR_FLASH_PAGE_15 + FLASH_PAGE_SIZE /* End @ of user Flash area */#define DATA_32 ((uint32_t)0x12345678)/*Variable used for Erase procedure*/ static FLASH_EraseInitTypeDef EraseInitStruct; uint32_t Address = 0;/*** @brief Main program* @param None* @retval None*/ int main(void) {/* STM32F0xx HAL library initialization:- Configure the Flash prefetch- Systick timer is configured by default as source of time base, but usercan eventually implement his proper time base source (a general purposetimer for example or other time source), keeping in mind that Time baseduration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined andhandled in milliseconds basis.- Low Level Initialization*/HAL_Init();/* Configure the system clock to 48 MHz */SystemClock_Config();/* Unlock the Flash to enable the flash control register access *************/HAL_FLASH_Unlock();/* Erase the user Flash area(area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********//* Fill EraseInit structure*/EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;EraseInitStruct.PageAddress = FLASH_USER_START_ADDR;EraseInitStruct.NbPages = (FLASH_USER_END_ADDR - FLASH_USER_START_ADDR) / FLASH_PAGE_SIZE;if (HAL_FLASHEx_Erase(&EraseInitStruct, &PageError) != HAL_OK){/*Error occurred while page erase.User can add here some code to deal with this error.PageError will contain the faulty page and then to know the code error on this page,user can call function 'HAL_FLASH_GetError()'*//* Infinite loop */while (1){/* User doing something here */}}/* Program the user Flash area word by word(area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/Address = FLASH_USER_START_ADDR;while (Address < FLASH_USER_END_ADDR){if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, Address, DATA_32) == HAL_OK){Address = Address + 4;}else{/* Error occurred while writing data in Flash memory.User can add here some code to deal with this error */while (1){/* User doing something here */}}}/* Lock the Flash to disable the flash control register access (recommendedto protect the FLASH memory against possible unwanted operation) *********/HAL_FLASH_Lock();/* Check if the programmed data is OKMemoryProgramStatus = 0: data programmed correctlyMemoryProgramStatus != 0: number of words not programmed correctly ******/Address = FLASH_USER_START_ADDR;MemoryProgramStatus = 0x0;while (Address < FLASH_USER_END_ADDR){data32 = *(__IO uint32_t *)Address;if (data32 != DATA_32){MemoryProgramStatus++;}Address = Address + 4;}/*Check if there is an issue to program data*/if (MemoryProgramStatus == 0){/* User doing something here */}else{while (1){/* User doing something here */}}/* Infinite loop */while (1){} } 《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的STM32:Flash擦除与读写操作(HAL库)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。