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 disabledThis 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笔记相关推荐

  1. STM32F103学习笔记——SPI读写Flash(二)

      此系列文章是小白学习STM32的一些学习笔记.小白第一次写笔记文章,有不足或是错误之处,请多体谅和交流! 目录 1.软件设计流程 2.SPI初始化 3.SPI发送接收一字节函数编写 4.FLASH ...

  2. Xilinx-Spartan6-学习笔记(24):通过SPI总线读写FLASH

    Xilinx-Spartan6-学习笔记(24):通过SPI总线读写FLASH 利用SPI总线实现对FLASH进行读写,写入255个数据再读出255个数据.(这里为了模拟SDO信号,随机生成了0~1信 ...

  3. STM32笔记(十二)---SPI读写FLASH

    SPI读写FLASH 文章目录 SPI读写FLASH 一.SPI协议简介 1.1 SPI 物理层 1.2 协议层 1.2.1 SPI 基本通讯过程 1.2.2 通讯的起始和停止信号 1.2.3 数据有 ...

  4. STM32 HAL库学习笔记1-HAL库简介

    STM32 HAL库学习笔记1-HAL库简介 HAL库 SPL 库 和 HAL 库两者相互独立,互不兼容.几种库的比较如下 目前几种库对不同芯片的支持情况如下 ST 中文官网上有一篇<关于ST库 ...

  5. 【STM32】使用STM32cubeMX的库读写FLASH数据

    前言 做项目时有时候需要对一些数据进行掉电可存储,一般来说可以把这些数据存储到到EEPROM或FLASH,这次项目中我使用到的芯片是STM32F051C8T6,只有64KB的FLASH,没有EEPRO ...

  6. RT-Thread 使用FAL多字节读写FLASH

    RT-Thread 使用FAL多字节读写FLASH FAL 简介 在RT-Thread上使用FAL 添加SPI 总线驱动 添加SFUD驱动 添加SPI DEVICE 使用SFUD添加SPI 块设备 添 ...

  7. stm32 HAL库 Flash操作简介

    stm32 HAL库 Flash操作简介 目录 第一stm32,flash介绍 查看代码段,以判断代码长度 flash的基本操作规则 stm32 HAL库 Flash操作指南 stm32f1xx_ha ...

  8. STM32CUBEIDE之SPI读写FLASH进阶串行FLASH文件系统FatFs

    预备知识 >>W25Q128是16M spi flash,一共有256个block ,每个Block 64KB. >>一个Block可以分割为16个扇区(small secto ...

  9. Mycat读写分离笔记Windows

    Mycat读写分离笔记Windows 自己搭了一个运用Mycat中间件搭建了一个读写分离的demo,昨晚还在奋战当中,因为连接mycat的时候老是报错:No Mycat DataBases selec ...

最新文章

  1. jQuery学习笔记6:表单选择器
  2. linux系统管理常用命令
  3. Spring Cloud【Finchley】- 21 Spring Cloud Stream 构建消息驱动微服务
  4. Hive 0.13 数据类型
  5. 量子纠缠背后的故事(三):维格纳的朋友 精选
  6. 如何格式化电脑_电脑硬盘不小心格式化如何恢复【恢复方法】
  7. 概率矩阵分解模型 PMF
  8. html读取本地json数据库,用Javascript读取本地JSON到HTML
  9. web安全之信息收集
  10. 小甲鱼python课后题答案_小甲鱼python课后题
  11. csdn编辑器公式中插入空格
  12. python处理xps文件_xps/pdf/png/json转换
  13. 44、网络配线架如何打?超5类线水晶头与6类线水晶头制作方法
  14. Java实现IP代理
  15. JFlow:真心服务中国IT产业
  16. EPSON TM U220串口打印机乱码
  17. html 让页脚始终底部,CSS + DIV 让页脚始终保持在页面底部
  18. 如何租用虚拟服务器,怎么租用虚拟主机
  19. Python3.4如何读写Excel
  20. PostgreSQL 修改密码

热门文章

  1. Linguistically Regularized LSTM for Sentiment Classification 论文原文、代码实现以及翻译
  2. 移动安全逆向笔记 03:Androidkiller的使用+常见的破解工具
  3. 新一代大智慧文件夹结构
  4. php嘻哈算法解刨图,php排序算法
  5. 成长经历html代码,我的成长经历日记模板200字
  6. 【实战】android网页源代码查看器
  7. [转载]2014年Windows平台软件推荐:神器小工具(骨灰级)
  8. 电容通高频阻低频,为什么大电容通低频,小电容通高频
  9. linux qt目录查看器,6个轻量级的Qt图像查看器为Linux | MOS86
  10. 以色列欲当下一个世界网络安全中心