瑞萨e2studio----Code Flash/Data Flash读写

描述

开发板

 

 

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生态工作室】欢迎添加关注!文章转载请注明出处。

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分