
1.概述
本篇文章主要介绍如何使用e2studio对瑞萨进行Flash配置,并且分别对Code Flash & Data Flash进行读写操作。
Flash有Code Flash(储存程序代码)以及Data Flash(储存一般数据),其中Code Flash主要以NOR型为主,储存系统程序代码及小量数据;而Data Flash则是以NAND型为主,用于储存大量数据。
2.硬件准备
首先需要准备一个开发板,这里我准备的是芯片型号 R7FA2L1AB2DFL 的开发板。
3.新建工程

4.工程模板

5.保存工程路径

6.芯片配置
本文中使用R7FA2L1AB2DFL来进行演示。

7
7.工程模板选择

8.Flash配置
点击 Stacks -> New Stack -> Driver -> Storage -> Flash Driver on r_flash_lp。

9.Flash属性配置

10.设置E2STUDIO堆栈

11.e2studio的重定向printf设置

C++ 构建->设置->GNU ARM Cross C Linker->Miscellaneous去掉Other linker flags中的 “--specs=rdimon.specs”

12.printf输出重定向到串口
打印最常用的方法是printf,所以要解决的问题是将printf的输出重定向到串口,然后通过串口将数据发送出去。
注意一定要加上头文件#include
#ifdef __GNUC__ //串口重定向
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif
PUTCHAR_PROTOTYPE
{
err = R_SCI_UART_Write(&g_uart0_ctrl, (uint8_t *)&ch, 1);
if(FSP_SUCCESS != err) __BKPT();
while(uart_send_complete_flag == false){}
uart_send_complete_flag = false;
return ch;
}
int _write(int fd,char *pBuffer,int size)
{
for(int i=0;i;i++)>
13.R_FLASH_LP_Open()函数原型

故可以用R_FLASH_LP_Open ()函数进行初始化开启初始化Flash。
/* Open the flash lp instance. */
fsp_err_t err = R_FLASH_LP_Open(&g_flash0_ctrl, &g_flash0_cfg);
assert(FSP_SUCCESS == err);
14.R_FLASH_LP_Erase()函数原型

故可以用R_FLASH_LP_Erase()函数进行擦除指定的代码或数据闪存块。
/* Erase 1 block of code flash starting at block 62. */
err = R_FLASH_LP_Erase(&g_flash0_ctrl, FLASH_CF_BLOCK_62, 1);
assert(FSP_SUCCESS == err);
15.R_FLASH_LP_StatusGet()函数原型

故可以用R_FLASH_LP_StatusGet()函数对Code Flash或者Data Flash进行写数据。
/* Write 32 bytes to the first block of data flash. */
err = R_FLASH_LP_Write(&g_flash0_ctrl, (uint32_t) g_src_uint8, FLASH_CF_BLOCK_62, g_src_uint8_length);
assert(FSP_SUCCESS == err);
16.R_FLASH_L

故可以用R_FLASH_LP_Write()函数对Code Flash或者Data Flash进行写数据。
/* Write 32 bytes to the first block of data flash. */
err = R_FLASH_LP_Write(&g_flash0_ctrl, (uint32_t) g_src_uint8, FLASH_CF_BLOCK_62, g_src_uint8_length);
assert(FSP_SUCCESS == err);
17.R_FLASH_LP_StatusGet()函数原型

对Data Flash进行写操作时候,数据可以在后台运行,故可以用R_FLASH_LP_StatusGet()函数查询是否执行完毕。
/* Wait until the current flash operation completes. */
do
{
err = R_FLASH_LP_StatusGet(&g_flash0_ctrl, &status);
} while ((FSP_SUCCESS == err) && (FLASH_STATUS_BUSY == status));
18.Code Flash
对Code Flash进行读写操作时候,特别要注意写的地址,因为如果写的不对,会覆盖到代码区,造成运行错误,同时对于擦除,是一块的数据都会直接擦除掉。
在RA2L1中,Code flash有2种规格,分别是128KB和256KB,每块大小为2KB。

为了兼容其他的型号,向Block62种写入数据并且读取出来,地址范围是0x0001F000 - 0x0001F800。
使用R_FLASH_LP_Write()写入的时候,写入的是字节为单位,故num_bytes为g_src_uint8_length*1;
#define FLASH_CF_BLOCK_62 0x0001F000U /* 2 KB: 0x0001F000 - 0x0001F800 */
volatile uint8_t g_src_uint8[4]={0x1a,0x24,0x46,0x6a};
volatile uint8_t g_src_uint8_length=4;
/* Write 32 bytes to the first block of data flash. */
err = R_FLASH_LP_Write(&g_flash0_ctrl, (uint32_t) g_src_uint8, FLASH_CF_BLOCK_62, g_src_uint8_length);
assert(FSP_SUCCESS == err);
assert(0 == memcmp(g_src_uint8, (uint8_t *) FLASH_CF_BLOCK_62, g_src_uint8_length));
19.Data Flash
对Data Flash进行读写操作时候,特别要注意要等待Data Flash写完才能进行后续读写操作。
在RA2L1中, Data flash都是8KB的,每块大小为1KB 。

向Block0种写入数据并且读取出来,地址范围是0x40100000 - 0x401003FF。
使用R_FLASH_LP_Write()写入的时候,写入的是字节为单位,故num_bytes为g_src_uint8_length*1;
#define FLASH_DF_BLOCK_0 0x40100000U /* 1 KB: 0x40100000 - 0x401003FF */
volatile uint8_t g_src_uint8[4]={0x1a,0x24,0x46,0x6a};
volatile uint8_t g_src_uint8_length=4;
flash_status_t status;
/* Write 32 bytes to the first block of data flash. */
err = R_FLASH_LP_Write(&g_flash0_ctrl, (uint32_t) g_src_uint8, FLASH_DF_BLOCK_0, g_src_uint8_length);
assert(FSP_SUCCESS == err);
/* Wait until the current flash operation completes. */
do
{
err = R_FLASH_LP_StatusGet(&g_flash0_ctrl, &status);
} while ((FSP_SUCCESS == err) && (FLASH_STATUS_BUSY == status));
20.演示效果
向Data Flash地址0x40100000写入{0x1a,0x24,0x46,0x6a}和{0xaabbccdd,0x11111111,0x22222222,0x33333333,0x44444444}
向Code Flash地址0x0001F000写入{0x1a,0x24,0x46,0x6a}和{0xaabbccdd,0x11111111,0x22222222,0x33333333,0x44444444}
通过串口打印出的结果如下所示。

内存地址查询结果如下所示。


21.完整代码
#include "hal_data.h"
#include
FSP_CPP_HEADER
void R_BSP_WarmStart(bsp_warm_start_event_t event);
FSP_CPP_FOOTER
fsp_err_t err = FSP_SUCCESS;
volatile bool uart_send_complete_flag = false;
void user_uart_callback (uart_callback_args_t * p_args)
{
if(p_args->event == UART_EVENT_TX_COMPLETE)
{
uart_send_complete_flag = true;
}
}
#ifdef __GNUC__ //串口重定向
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif
PUTCHAR_PROTOTYPE
{
err = R_SCI_UART_Write(&g_uart0_ctrl, (uint8_t *)&ch, 1);
if(FSP_SUCCESS != err) __BKPT();
while(uart_send_complete_flag == false){}
uart_send_complete_flag = false;
return ch;
}
int _write(int fd,char *pBuffer,int size)
{
for(int i=0;ievent;
}
#define FLASH_DF_BLOCK_0 0x40100000U /* 1 KB: 0x40100000 - 0x401003FF */
#define FLASH_CF_BLOCK_62 0x0001F000U /* 2 KB: 0x0001F000 - 0x0001F800 */
/*******************************************************************************************************************//**
* main() is generated by the RA Configuration editor and is used to generate threads if an RTOS is used. This function
* is called by main() when no RTOS is used.
**********************************************************************************************************************/
void hal_entry(void)
{
/* TODO: add your own code here */
err = R_SCI_UART_Open(&g_uart0_ctrl, &g_uart0_cfg);
assert(FSP_SUCCESS == err);
volatile uint8_t g_src_uint8_length=4;
volatile uint8_t g_src_uint8[4]={0x1a,0x24,0x46,0x6a};
volatile uint8_t g_src_uint32_length=5;
volatile uint32_t g_src_uint32[5]={
0xaabbccdd,0x11111111,0x22222222,0x33333333,0x44444444
};
/********************code flash*******************************/
flash_result_t blank_check_result;
/* Open the flash lp instance. */
fsp_err_t err = R_FLASH_LP_Open(&g_flash0_ctrl, &g_flash0_cfg);
assert(FSP_SUCCESS == err);
/* Disable interrupts to prevent vector table access while code flash is in P/E mode. */
__disable_irq();
/* Erase 1 block of code flash starting at block 62. */
err = R_FLASH_LP_Erase(&g_flash0_ctrl, FLASH_CF_BLOCK_62, 1);
assert(FSP_SUCCESS == err);
/* Write 32 bytes to the first block of data flash. */
err = R_FLASH_LP_Write(&g_flash0_ctrl, (uint32_t) g_src_uint8, FLASH_CF_BLOCK_62, g_src_uint8_length);
assert(FSP_SUCCESS == err);
assert(0 == memcmp(g_src_uint8, (uint8_t *) FLASH_CF_BLOCK_62, g_src_uint8_length));
err = R_FLASH_LP_Write(&g_flash0_ctrl, (uint32_t) g_src_uint32, FLASH_CF_BLOCK_62+g_src_uint8_length*1, g_src_uint32_length*4);
assert(FSP_SUCCESS == err);
assert(0 == memcmp(g_src_uint32, (uint8_t *) FLASH_CF_BLOCK_62+g_src_uint8_length*1, g_src_uint32_length*4));
/* Enable interrupts after code flash operations are complete. */
__enable_irq();
printf("\n/********************code flash*******************************/\n");
PrintFlashTest(6,FLASH_CF_BLOCK_62);
/********************data flash*******************************/
interrupt_called = false;
/* Erase 1 block of data flash starting at block 0. */
err = R_FLASH_LP_Erase(&g_flash0_ctrl, FLASH_DF_BLOCK_0, 1);
assert(FSP_SUCCESS == err);
while (!interrupt_called)
{
;
}
assert(FLASH_EVENT_ERASE_COMPLETE == flash_event);
interrupt_called = false;
flash_status_t status;
/* Write 32 bytes to the first block of data flash. */
err = R_FLASH_LP_Write(&g_flash0_ctrl, (uint32_t) g_src_uint8, FLASH_DF_BLOCK_0, g_src_uint8_length);
assert(FSP_SUCCESS == err);
/* Wait until the current flash operation completes. */
do
{
err = R_FLASH_LP_StatusGet(&g_flash0_ctrl, &status);
} while ((FSP_SUCCESS == err) && (FLASH_STATUS_BUSY == status));
err = R_FLASH_LP_Write(&g_flash0_ctrl, (uint32_t) g_src_uint32, FLASH_DF_BLOCK_0+g_src_uint8_length*1, g_src_uint32_length*4);
assert(FSP_SUCCESS == err);
/* Wait until the current flash operation completes. */
do
{
err = R_FLASH_LP_StatusGet(&g_flash0_ctrl, &status);
} while ((FSP_SUCCESS == err) && (FLASH_STATUS_BUSY == status));
/* If the interrupt wasn't called process the error. */
assert(interrupt_called);
/* If the event wasn't a write complete process the error. */
assert(FLASH_EVENT_WRITE_COMPLETE == flash_event);
/* Verify the data was written correctly. */
assert(0 == memcmp(g_src_uint8, (uint8_t *) FLASH_DF_BLOCK_0, g_src_uint8_length));
assert(0 == memcmp(g_src_uint32, (uint8_t *) FLASH_DF_BLOCK_0+g_src_uint8_length*1, g_src_uint32_length*4));
printf("\n/********************data flash*******************************/\n");
PrintFlashTest(6,FLASH_DF_BLOCK_0);
while(1)
{
R_BSP_SoftwareDelay(1000, BSP_DELAY_UNITS_MILLISECONDS); // NOLINT100->160
}
#if BSP_TZ_SECURE_BUILD
/* Enter non-secure code */
R_BSP_NonSecureEnter();
#endif
}
/*FLASH读取打印程序*/
void PrintFlashTest(uint32_t L,uint32_t addr)
{
uint32_t i=0;
for(i=0;i;i++)>;i++)>
原创:By RA_Billy Xiao
原文标题:瑞萨e2studio----Code Flash&Data Flash读写
文章出处:【微信公众号:RA生态工作室】欢迎添加关注!文章转载请注明出处。
-
mcu
+关注
关注
146文章
17072浏览量
350698 -
ARM
+关注
关注
134文章
9069浏览量
367158 -
嵌入式
+关注
关注
5081文章
19069浏览量
304285 -
开发板
+关注
关注
25文章
5002浏览量
97317
发布评论请先 登录
相关推荐
【瑞萨 FPB-RA4E2 开发板测评】简介、环境搭建、程序测试
瑞萨e2studio(1)----瑞萨芯片之搭建FSP环境
STM32CUBEMX(10)--Flash读写
瑞萨e2studio----SPI速率解析

Teledyne e2v宣布扩展其Flash™ CMOS图像传感器系列
CX3开发平台是否能在SPI FLASH内放入User Data ?
瑞萨Flash示例程序01型SC版本(代码Flash)应用说明

瑞萨Flash示例程序01版V1.20(SC版)发布说明

RL78/G15组和RL78/G16组 瑞萨Flash示例程序01型SC版本应用说明

data flash存了两个标定参数后,不能正确读写了是什么情况?
GD32F系列MCU片上Flash中Code区和Data区使用解密

评论