HAL读写FLASH笔记

HAL读写FLASH笔记

1. 整理数据

FLASH写入函数HAL_FLASH_Program可以写入16bit,32bit,64bit,实际最终调用FLASH_Program_HalfWord写入的是16bit,所以在写入之前要先把数据存到16bit数组里,然后按照16位写。
其实不用麻烦,直接存8位数组里,然后按照16位和32位写就可以了。

/**
  * @brief  Program a half-word (16-bit) at a specified address.
  * @param  Address specify the address to be programmed.
  * @param  Data    specify the data to be programmed.
  * @retval None
  */
static void FLASH_Program_HalfWord(uint32_t Address, uint16_t Data)

/**
  * @brief  Program halfword, word or double word at a specified address
  * @note   The function HAL_FLASH_Unlock() should be called before to unlock the FLASH interface
  *         The function HAL_FLASH_Lock() should be called after to lock the FLASH interface
  *
  * @note   If an erase and a program operations are requested simultaneously,    
  *         the erase operation is performed before the program one.
  *  
  * @note   FLASH should be previously erased before new programmation (only exception to this 
  *         is when 0x0000 is programmed)
  *
  * @param  TypeProgram:  Indicate the way to program at a specified address.
  *                       This parameter can be a value of @ref FLASH_Type_Program
  * @param  Address:      Specifies the address to be programmed.
  * @param  Data:         Specifies the data to be programmed
  * 
  * @retval HAL_StatusTypeDef HAL Status
  */
HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t Data)

2. 解锁

直接调用函数就可以了,实际是在FLASH->KEYR寄存器分别写入FLASH_KEY1,FLASH_KEY2.

/**
  * @brief  Unlock the FLASH control register access
  * @retval HAL Status
  */
HAL_StatusTypeDef HAL_FLASH_Unlock(void)

3.擦除扇区

FLASH擦除结构体,定义擦除类型TypeErase为页擦除,定义擦除地址PageAddress,定义擦除页数NbPages

/**
  * @brief  FLASH Erase structure definition
  */
typedef struct
{
  uint32_t TypeErase; /*Mass erase or page erase*/
  uint32_t Banks;       /*!< Select banks to erase when Mass erase is enabled */    
  uint32_t PageAddress; /*!< PageAdress: Initial FLASH page address to erase when mass erase is disabled
                             This parameter must be a number between Min_Data = 0x08000000 and Max_Data = FLASH_BANKx_END 
                             (x = 1 or 2 depending on devices)*/
  
  uint32_t NbPages;     /*!< NbPages: Number of pagess to be erased.
                             This parameter must be a value between Min_Data = 1 and Max_Data = (max number of pages - value of initial page)*/
                                                         
} FLASH_EraseInitTypeDef;

4.写入数据

调用HAL_FLASH_Program函数写入

/**
  * @brief  Program halfword, word or double word at a specified address
  * @note   The function HAL_FLASH_Unlock() should be called before to unlock the FLASH interface
  *         The function HAL_FLASH_Lock() should be called after to lock the FLASH interface
  *
  * @note   If an erase and a program operations are requested simultaneously,    
  *         the erase operation is performed before the program one.
  *  
  * @note   FLASH should be previously erased before new programmation (only exception to this 
  *         is when 0x0000 is programmed)
  *
  * @param  TypeProgram:  Indicate the way to program at a specified address.
  *                       This parameter can be a value of @ref FLASH_Type_Program
  * @param  Address:      Specifies the address to be programmed.
  * @param  Data:         Specifies the data to be programmed
  * 
  * @retval HAL_StatusTypeDef HAL Status
  */
HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t Data)

5.上锁

HAL_FLASH_Lock();

代码整理

写函数

/**
  * @brief  写若干页
  * @param  地址
  * @param  数组
  * @param  长度,8bit
  * @retval None
  */
int Flash_write(uint32_t appxaddr,uint32_t *appbuf,uint32_t appsize)
{
	static FLASH_EraseInitTypeDef EraseInitStruct;
	uint32_t SECTORError = 0;
	uint32_t Address = 0x00;				//记录写入的地址
//	printf("\nFLASH:%d ",appbuf[0]%0x100);
//	printf("\nFLASH:%d ",appbuf[0] );
  /* 解锁 */
	HAL_FLASH_Unlock();
	
  /* 擦除 */
	EraseInitStruct.TypeErase     = FLASH_TYPEERASE_PAGES;
	EraseInitStruct.NbPages       = appsize/2048+1;
	EraseInitStruct.PageAddress   = appxaddr;
  if (HAL_FLASHEx_Erase(&EraseInitStruct, &SECTORError) != HAL_OK)
	{
		return -1;
	}
	
  /* 向内部FLASH写入数据 */
	Address = appxaddr;
	uint32_t i=0;
	while(Address < appxaddr + appsize)
	{
		if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, Address,appbuf[i]) == HAL_OK)
		{
		  Address = Address + 4;
			i+=1;
		}
		else
		{
		  /*写入出错,返回,实际应用中可加入处理 */
				return -1;
		}
	}
	
	/* 上锁 */
  HAL_FLASH_Lock();
	
	return 0;
}

写函数调用

Flash_write(FLASH_APP1_ADDR,(u32 *)USART_RX_BUF,Uart1.Length);

读函数

读的时候直接用memcpy就可以了

memcpy((uint8_t *)USART_TX_BUF+2,(uint8_t *)FLASH_APP1_ADDR,Uart1.Length-4);

注意

写入的数据是串口接收到的8bit数据,写是按照32bit写的,中间有四倍的差别。可以在调用函数的时候处理,也可以在写的时候处理

效果截图

在这里插入图片描述

  • 2
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值