我是靠谱客的博主 幸福太阳,最近开发中收集的这篇文章主要介绍HAL读写FLASH笔记HAL读写FLASH笔记代码整理,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

HAL读写FLASH笔记

目录

  • HAL读写FLASH笔记
    • 1. 整理数据
    • 2. 解锁
    • 3.擦除扇区
    • 4.写入数据
    • 5.上锁
  • 代码整理
    • 写函数
    • 读函数
    • 注意
    • 效果截图

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写的,中间有四倍的差别。可以在调用函数的时候处理,也可以在写的时候处理

效果截图

在这里插入图片描述

最后

以上就是幸福太阳为你收集整理的HAL读写FLASH笔记HAL读写FLASH笔记代码整理的全部内容,希望文章能够帮你解决HAL读写FLASH笔记HAL读写FLASH笔记代码整理所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(57)

评论列表共有 0 条评论

立即
投稿
返回
顶部