目录
概述
1 Data Flash空间
2 FSP配置参数
3 源代码介绍
3.1 源代码
3.2 中断函数
3.3 源代码文件
4 测试
4.1 测试实现
4.2 测试
概述
本文主要介绍使用FSP提供的库函数操作Renesas R7FA8D1BH (Cortex®-M85) Data Flash的方法,笔者使用FSP配置参数,并生成工程文件,然后实现测试函数,以验证操作Flash的功能。
1 Data Flash空间
Renesas R7FA8D1BH (Cortex®-M85) Data Flash的数据快闪存储器的数据区被划分为64字节块,每个字节块作为一个擦除单元。数据闪存的映射关系如下图所示:
2 FSP配置参数
1)使用FSP创建Stack
2) 配置Stack的参数
3 源代码介绍
3.1 源代码
1)应用函数代码
代码118行:打开Flash操作端口
代码122行:擦除Flash
代码127行: 判断是否查出完成
代码134行:检查擦除Flash空间的是否完成
代码144:写数据
代码149行:读取数据
3.2 中断函数
代码14~22行: 定义中断函数中使用的变量
代码25~51行: 判断中断状态
3.3 源代码文件
创建 bsp_vm_flash.c文件,编写如下代码
/*FILE NAME : bsp_vm_flash.cDescription: vm flash interfaceAuthor : tangmingfei2013@126.comDate : 2024/09/22*/
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
#include "r_ospi_b.h"
#include "bsp_vm_flash.h"
#include "hal_data.h"bool interrupt_called;
flash_event_t flash_event;volatile bool flash_erase_complete_flag = false;
volatile bool flash_write_complete_flag = false;
volatile bool flash_event_not_blank = false;
volatile bool flash_event_blank = false;
volatile bool flash_event_error = false;
volatile flash_event_t flash_event_error_type;void g_flash_callBack (flash_callback_args_t * p_args)
{interrupt_called = true;flash_event = p_args->event;if (FLASH_EVENT_ERASE_COMPLETE == flash_event){flash_erase_complete_flag = true;}else if (FLASH_EVENT_WRITE_COMPLETE == flash_event){flash_write_complete_flag = true;}else if (FLASH_EVENT_BLANK == flash_event){flash_event_blank = true;}else if (FLASH_EVENT_NOT_BLANK == flash_event){flash_event_not_blank = true;}else{flash_event_error = true;flash_event_error_type = flash_event;}
}#if 1#define FLASH_DF_BLOCK 0x27000000 /* 64 B: 0x27000000 - 0x2700004F */
#define FLASH_DATA_BLOCK_SIZE 64
#define FLASH_HP_EXAMPLE_WRITE_SIZE 64#define TRANSFER_LENGTH 256uint8_t g_dest[TRANSFER_LENGTH];
uint8_t g_src[TRANSFER_LENGTH];
flash_result_t blank_check_result;void r_flash_hp_basic_test(void)
{/* Initialize p_src to known data */for (uint32_t i = 0; i < TRANSFER_LENGTH; i++){g_src[i] = (uint8_t) ('A' + (i % 26));}/* Open the flash hp instance. */fsp_err_t err = R_FLASH_HP_Open(&g_flash_ctrl, &g_flash_cfg);assert(FSP_SUCCESS == err);/* Erase 1 block of data flash starting at block 0. */err = R_FLASH_HP_Erase(&g_flash_ctrl, FLASH_DF_BLOCK, 1);assert(FSP_SUCCESS == err);if (true == g_flash_cfg.data_flash_bgo){while (!flash_erase_complete_flag);flash_erase_complete_flag = false;}memcpy( g_dest, FLASH_DF_BLOCK, FLASH_HP_EXAMPLE_WRITE_SIZE);/* Check if block 0 is erased. */err = R_FLASH_HP_BlankCheck(&g_flash_ctrl, FLASH_DF_BLOCK, FLASH_DATA_BLOCK_SIZE, &blank_check_result);assert(FSP_SUCCESS == err);/* Verify the previously erased area is blank *///assert(FLASH_RESULT_BLANK == blank_check_result);/* Write 32 bytes to the first block of data flash. */err = R_FLASH_HP_Write(&g_flash_ctrl, (uint32_t) g_src, FLASH_DF_BLOCK, FLASH_HP_EXAMPLE_WRITE_SIZE);assert(FSP_SUCCESS == err);memcpy( g_dest, FLASH_DF_BLOCK, FLASH_HP_EXAMPLE_WRITE_SIZE);assert(0 == memcmp(g_src, (uint8_t *) FLASH_DF_BLOCK, FLASH_HP_EXAMPLE_WRITE_SIZE));}#endif
4 测试
4.1 测试实现
功能介绍:
1)选取BLOCK_0: 0x27000000 /* 64 B: 0x27000000 - 0x2700004F */
-
2) 在该区间写入如下数据:
/* Initialize p_src to known data */
for (uint32_t i = 0; i < TRANSFER_LENGTH; i++)
{
g_src[i] = (uint8_t) ('A' + (i % 26));
}-
3) 比较数据
准备数据
读写数据和比较数据
4.2 测试
1)准备数据
2)初始化和擦除Flash
3)读写和比较数据
写入的数据:
读到的数据: