HAL库 FLASH程序编写注意事项

根据官方库例程和实验得知

1.FLASH擦写有两种方式: FLASH_TYPEERASE_PAGES按页擦除和FLASH_TYPEERASE_MASSERASE MASSERASE
2.FLASH烧写程序有三种方式
FLASH_TYPEPROGRAM_DOUBLEWORD 单次8个字节的方式
FLASH_TYPEPROGRAM_FAST 单次32行8个字节的方式
FLASH_TYPEPROGRAM_FAST_AND_LAST 单次32行8个字节的方式,但是是烧写地址的最后一页。
3.擦写方式和烧写方式有关联性

FLASH_TYPEPROGRAM_DOUBLEWORD的烧写方式可以使用按页擦除和MASSERASE,但是
FLASH_TYPEPROGRAM_FAST和FLASH_TYPEPROGRAM_FAST_AND_LAST只能使用MASSERASE的方式擦除。

4.可通过查看#define FLASH_SIZE_DATA_REGISTER ((uint32_t)0x1FFF75E0)该地址的值判断FLASH的size。

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个使用STM32Cube HAL库编写Flash编程示例。该示例演示了如何在STM32F4系列MCU上使用HAL库编程来擦除和编程Flash存储器。 ```c #include "stm32f4xx_hal.h" /* Private variables ---------------------------------------------------------*/ FLASH_EraseInitTypeDef EraseInitStruct; uint32_t SectorError = 0; /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); void Error_Handler(void); int main(void) { /* MCU Configuration----------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* Configure the system clock */ SystemClock_Config(); /* Enable Flash 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_SECTORS; EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3; EraseInitStruct.Sector = FLASH_SECTOR_5; EraseInitStruct.NbSectors = 4; if (HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError) != HAL_OK) { /* Error occurred while sector erase. User can add here some code to deal with this error. SectorError will contain the faulty sector and then to know the code error on this sector, user can call function 'HAL_FLASH_GetError()' */ Error_Handler(); } /* Program the user Flash area word by word (area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/ uint32_t flash_address = FLASH_USER_START_ADDR; uint32_t data[3] = {0x12345678, 0x87654321, 0xDEADBEEF}; for (int i=0; i<3; i++) { if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, flash_address, data[i]) != HAL_OK) { /* Error occurred while writing. User can add here some code to deal with this error. */ Error_Handler(); } flash_address += 4; } /* Lock the Flash to disable the flash control register access (recommended to protect the FLASH memory against possible unwanted operation) *********/ __HAL_FLASH_LOCK(); /* Infinite loop */ while (1) { } } /** * @brief System Clock Configuration * @retval None */ void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; /** Configure the main internal regulator output voltage */ __HAL_RCC_PWR_CLK_ENABLE(); __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); /** Initializes the RCC Oscillators according to the specified parameters * in the RCC_OscInitTypeDef structure. */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; RCC_OscInitStruct.HSIState = RCC_HSI_ON; RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); } /** Initializes the CPU, AHB and APB buses clocks */ RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK) { Error_Handler(); } } /** * @brief This function is executed in case of error occurrence. * @retval None */ void Error_Handler(void) { /* User can add his own implementation to report the HAL error return state */ while(1) { } } ``` 此示例程序假定您已经定义了以下宏: ```c #define FLASH_USER_START_ADDR ADDR_FLASH_SECTOR_5 /* Start @ of user Flash area */ #define FLASH_USER_END_ADDR ADDR_FLASH_SECTOR_9 /* End @ of user Flash area */ ``` 此外,您还需要在链接器脚本(例如,在使用Keil MDK时的“scatter file”)中定义以下符号: ```c FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 512K ``` 这样可以确保程序不会覆盖Flash中的任何其他部分。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值