Production of keil electronic clock based on RASC (Renesas RA) (9)----save data to flash

overview

This article mainly introduces how to use e2studio to configure Flash for Renesas, and to read and write Code Flash & Data Flash respectively.
Flash has Code Flash (storage program code) and Data Flash (storage general data), among which Code Flash is mainly NOR type, storing system program code and a small amount of data; and Data Flash is mainly NAND type, used for Store large amounts of data.

hardware preparation

First, you need to prepare a development board. Here I am preparing a development board with chip model R7FA2E1A72DFL:

insert image description here

insert image description here

video tutorial

https://www.bilibili.com/video/BV1PM4y1p7Ue/

Keil electronic clock production based on RASC (Renesas RA)----(9) Save data to flash

Flash

When reading and writing Code Flash, pay special attention to the written address, because if the writing is wrong, it will overwrite the code area, causing operation errors. At the same time, for erasing, all the data in a block will be erased directly.
In RA2E1, Code flash is up to 128KB, and Data flash is 4KB.
insert image description here

FLASH configuration

点击Stacks->New Stack->Storage -> Flash (r_flash_lp)。

insert image description here

FLASH attribute configuration

insert image description here

Data Flash

When performing read and write operations on the Data Flash, special attention must be paid to waiting for the Data Flash to finish writing before performing subsequent read and write operations.
In RA2E1, the Data Flash distribution is as follows.
insert image description here

If the callback function is used, the following events will be triggered.

insert image description here
Build flash_smg.c and flash_smg.h.
Add this header file to the main program
insert image description here

The callback function is shown below, in flash_smg.c.

volatile bool               interrupt_called;
volatile flash_event_t      flash_event;


void flash_callback (flash_callback_args_t * p_args)
{
    
    
    interrupt_called = true;
    flash_event      = p_args->event;
}

Write time minute data and hour data to Block0, the address range is 0x40100000 - 0x40100FFF, defined in flash_smg.c

extern fsp_err_t err ;
/*FLASH写入程序*/
void WriteFlashTest(uint32_t L,uint8_t Data[],uint32_t addr)
{
    
    


    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) Data, addr, L);
    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(Data, (uint8_t *) FLASH_DF_BLOCK_0, L));


}

Define the flag bit in the main program to judge the data storage.

volatile uint8_t g_src_uint8[4]={
    
    0x00,0x00,0x00,0x00};//时间保存在该数组里面
volatile uint8_t  g_src_uint8_length=4;
uint8_t flash_flag=0;//保存时间数据,一半在每过一分钟或者按键修改时间

insert image description here

In the main program, it is defined to save the data after the key is modified.

           if(flash_flag)//按键修改完毕数据后进行保存
           {
    
    
               g_src_uint8[0]=hour;
               g_src_uint8[1]=min;
               WriteFlashTest(4,g_src_uint8 ,FLASH_DF_BLOCK_0);
               flash_flag=0;
           } 

insert image description here
At the same time, the data needs to be saved after the key setting is completed, and the flag bit needs to be defined as 1 in mode 3.

            flash_flag=1;//保存数据

insert image description here

At the same time, it is necessary to pay attention to the introduction of variables into timer_smg.c.

extern uint8_t flash_flag;//保存时间数据,一半在每过一分钟或者按键修改时间

insert image description here

At the same time, the data is saved once when the RTC clock reaches 0 seconds.

                   g_src_uint8[0]=hour;
                   g_src_uint8[1]=min;
                   WriteFlashTest(4,g_src_uint8 ,FLASH_DF_BLOCK_0);

insert image description here

The read function is shown below, in flash_smg.h.

extern int sec,min,hour;//保存时间数据
/*FLASH读取打印程序*/
void PrintFlashTest(uint32_t addr)
{
    
    
    hour=*(__IO uint8_t*)(addr);
    min=*(__IO uint8_t*)(addr+1);

    if(hour>=24)
        hour=0;
    if(min>=60)
        min=0;
}

At the same time, open the flash in the main program and read the saved data.
Since the data needs to be put in when the RTC is turned on, it needs to be placed before the RTC is turned on.

/**********************data flash***************************************/
       flash_result_t blank_check_result;
       /* Open the flash lp instance. */
    	err = R_FLASH_LP_Open(&g_flash0_ctrl, &g_flash0_cfg);
       assert(FSP_SUCCESS == err);

//       WriteFlashTest(4,g_src_uint8 ,FLASH_DF_BLOCK_0);

       PrintFlashTest(FLASH_DF_BLOCK_0);


       set_time.tm_sec=0;//时间数据 秒
       set_time.tm_min=min;//时间数据 分钟
       hour=set_time.tm_hour=hour;//时间数据 小时 

insert image description here

flash_smg.c

/*
 * flash_smg.c
 *
 *  Created on: 2023年7月5日
 *      Author: a8456
 */
#include "flash_smg.h"

volatile bool               interrupt_called;
volatile flash_event_t      flash_event;


void flash_callback (flash_callback_args_t * p_args)
{
    
    
    interrupt_called = true;
    flash_event      = p_args->event;
}


extern fsp_err_t err ;
/*FLASH写入程序*/
void WriteFlashTest(uint32_t L,uint8_t Data[],uint32_t addr)
{
    
    


    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) Data, addr, L);
    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(Data, (uint8_t *) FLASH_DF_BLOCK_0, L));


}

extern int sec,min,hour;//保存时间数据
/*FLASH读取打印程序*/
void PrintFlashTest(uint32_t addr)
{
    
    
    hour=*(__IO uint8_t*)(addr);
    min=*(__IO uint8_t*)(addr+1);

    if(hour>=24)
        hour=0;
    if(min>=60)
        min=0;
}




flash_smg.h

/*
 * flash_smg.h
 *
 *  Created on: 2023年6月29日
 *      Author: a8456
 */

#ifndef FLASH_SMG_H_
#define FLASH_SMG_H_

#include "hal_data.h"

#define FLASH_DF_BLOCK_0                0x40100000U/*   1 KB: 0x40100000 - 0x401003FF */

/*FLASH写入程序*/
void WriteFlashTest(uint32_t L,uint8_t Data[],uint32_t addr);
/*FLASH读取打印程序*/
void PrintFlashTest(uint32_t addr);

#endif /* FLASH_SMG_H_ */

main program

#include "hal_data.h"
#include <stdio.h>
#include "smg.h"
#include "timer_smg.h"
#include "flash_smg.h"
FSP_CPP_HEADER
void R_BSP_WarmStart(bsp_warm_start_event_t event);
FSP_CPP_FOOTER

//数码管变量
uint8_t num1=1,num2=4,num3=6,num4=8;//4个数码管显示的数值
uint8_t num_flag=0;//4个数码管和冒号轮流显示,一轮刷新五次

//RTC变量
/* rtc_time_t is an alias for the C Standard time.h struct 'tm' */
rtc_time_t set_time =
{
    
    
    .tm_sec  = 50,      /* 秒,范围从 0 到 59 */
    .tm_min  = 59,      /* 分,范围从 0 到 59 */
    .tm_hour = 23,      /* 小时,范围从 0 到 23*/
    .tm_mday = 29,       /* 一月中的第几天,范围从 0 到 30*/
    .tm_mon  = 11,      /* 月份,范围从 0 到 11*/
    .tm_year = 123,     /* 自 1900 起的年数,2023为123*/
    .tm_wday = 6,       /* 一周中的第几天,范围从 0 到 6*/
//    .tm_yday=0,         /* 一年中的第几天,范围从 0 到 365*/
//    .tm_isdst=0;        /* 夏令时*/
};


//RTC闹钟变量
rtc_alarm_time_t set_alarm_time=
{
    
    
     .time.tm_sec  = 58,      /* 秒,范围从 0 到 59 */
     .time.tm_min  = 59,      /* 分,范围从 0 到 59 */
     .time.tm_hour = 23,      /* 小时,范围从 0 到 23*/
     .time.tm_mday = 29,       /* 一月中的第几天,范围从 1 到 31*/
     .time.tm_mon  = 11,      /* 月份,范围从 0 到 11*/
     .time.tm_year = 123,     /* 自 1900 起的年数,2023为123*/
     .time.tm_wday = 6,       /* 一周中的第几天,范围从 0 到 6*/

     .sec_match        =  1,//每次秒到达设置的进行报警
     .min_match        =  0,
     .hour_match       =  0,
     .mday_match       =  0,
     .mon_match        =  0,
     .year_match       =  0,
     .dayofweek_match  =  0,
    };

bsp_io_level_t sw1;//按键SW1状态
bsp_io_level_t sw2;//按键SW2状态
bsp_io_level_t sw3;//按键SW3状态
bsp_io_level_t sw4;//按键SW4状态
bsp_io_level_t qe_sw;//触摸电容状态

int sw1_num1=0;//按键SW1计数值,去抖和长按短按判断
int sw2_num1=0;//按键SW2计数值,去抖和长按短按判断
int sw3_num1=0;//按键SW3计数值,去抖和长按短按判断
int sw4_num1=0;//按键SW4计数值,去抖和长按短按判断
int qe_sw_num1=0;//触摸按键计数值,去抖和长按短按判断
void qe_touch_sw(void);

//数码管显示状态,0正常显示,1修改小时,2修改分钟,3保存修改数据,4温度,5湿度
int smg_mode=0;
int sec=0,min=0,hour=0;//保存时间数据
uint16_t time_mode_num=0;//定时器刷新时间,实现闪烁效果

volatile uint8_t g_src_uint8[4]={
    
    0x00,0x00,0x00,0x00};//时间保存在该数组里面
volatile uint8_t  g_src_uint8_length=4;
uint8_t flash_flag=0;//保存时间数据,一半在每过一分钟或者按键修改时间


//RTC回调函数
volatile bool rtc_flag = 0;//RTC延时1s标志位
volatile bool rtc_alarm_flag = 0;//RTC闹钟
/* Callback function */
void rtc_callback(rtc_callback_args_t *p_args)
{
    
    
    /* TODO: add your own code here */
    if(p_args->event == RTC_EVENT_PERIODIC_IRQ)
        rtc_flag=1;
    else if(p_args->event == RTC_EVENT_ALARM_IRQ)
        rtc_alarm_flag=1;
}


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_uart9_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<size;i++)
    {
    
    
        __io_putchar(*pBuffer++);
    }
    return size;
}


/*******************************************************************************************************************//**
 * 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 */

    /* Open the transfer instance with initial configuration. */
       err = R_SCI_UART_Open(&g_uart9_ctrl, &g_uart9_cfg);
       assert(FSP_SUCCESS == err);
/**********************数码管测试***************************************/
//              ceshi_smg();
/**********************定时器开启***************************************/
    /* Initializes the module. */
    err = R_GPT_Open(&g_timer0_ctrl, &g_timer0_cfg);
    /* Handle any errors. This function should be defined by the user. */
    assert(FSP_SUCCESS == err);
    /* Start the timer. */
    (void) R_GPT_Start(&g_timer0_ctrl);

/**********************data flash***************************************/
    flash_result_t blank_check_result;
    /* Open the flash lp instance. */
    err = R_FLASH_LP_Open(&g_flash0_ctrl, &g_flash0_cfg);
    assert(FSP_SUCCESS == err);

    //       WriteFlashTest(4,g_src_uint8 ,FLASH_DF_BLOCK_0);

    PrintFlashTest(FLASH_DF_BLOCK_0);


    set_time.tm_sec=0;//时间数据 秒
    set_time.tm_min=min;//时间数据 分钟
    hour=set_time.tm_hour=hour;//时间数据 小时



/**********************RTC开启***************************************/
    /* Initialize the RTC module*/
    err = R_RTC_Open(&g_rtc0_ctrl, &g_rtc0_cfg);
    /* Handle any errors. This function should be defined by the user. */
    assert(FSP_SUCCESS == err);

    /* Set the RTC clock source. Can be skipped if "Set Source Clock in Open" property is enabled. */
    R_RTC_ClockSourceSet(&g_rtc0_ctrl);

/* R_RTC_CalendarTimeSet must be called at least once to start the RTC */
    R_RTC_CalendarTimeSet(&g_rtc0_ctrl, &set_time);
    /* Set the periodic interrupt rate to 1 second */
    R_RTC_PeriodicIrqRateSet(&g_rtc0_ctrl, RTC_PERIODIC_IRQ_SELECT_1_SECOND);

           R_RTC_CalendarAlarmSet(&g_rtc0_ctrl, &set_alarm_time);
           uint8_t rtc_second= 0;      //秒
           uint8_t rtc_minute =0;      //分
           uint8_t rtc_hour =0;         //时
           uint8_t rtc_day =0;          //日
           uint8_t rtc_month =0;      //月
           uint16_t rtc_year =0;        //年
           uint8_t rtc_week =0;        //周
           rtc_time_t get_time;


           sec=set_time.tm_sec;//时间数据 秒
            min=set_time.tm_min;//时间数据 分钟
            hour=set_time.tm_hour;//时间数据 小时

       while(1)
       {
    
    
           if(flash_flag)//按键修改完毕数据后进行保存
           {
    
    
               g_src_uint8[0]=hour;
               g_src_uint8[1]=min;
               WriteFlashTest(4,g_src_uint8 ,FLASH_DF_BLOCK_0);
               flash_flag=0;
           }


           if(rtc_flag)
           {
    
    
               R_RTC_CalendarTimeGet(&g_rtc0_ctrl, &get_time);//获取RTC计数时间
               rtc_flag=0;
               rtc_second=get_time.tm_sec;//秒
               rtc_minute=get_time.tm_min;//分
               rtc_hour=get_time.tm_hour;//时
               rtc_day=get_time.tm_mday;//日
               rtc_month=get_time.tm_mon;//月
               rtc_year=get_time.tm_year; //年
               rtc_week=get_time.tm_wday;//周
               printf(" %d y %d m %d d %d h %d m %d s %d w\n",rtc_year+1900,rtc_month,rtc_day,rtc_hour,rtc_minute,rtc_second,rtc_week);

                //时间显示
               num1=rtc_hour/10;
               num2=rtc_hour%10;

               num3=rtc_minute/10;
               num4=rtc_minute%10;
               if(rtc_second==0&&smg_mode==0)//这个时候刷新变量
               {
    
    
                   sec=rtc_second;//时间数据 秒
                   min=rtc_minute;//时间数据 分钟
                   hour=rtc_hour;//时间数据 小时

                   g_src_uint8[0]=hour;
                   g_src_uint8[1]=min;
                   WriteFlashTest(4,g_src_uint8 ,FLASH_DF_BLOCK_0);


               }
           }
           if(rtc_alarm_flag)
           {
    
    
               rtc_alarm_flag=0;
               printf("/************************Alarm Clock********************************/\n");
           }
           set_smg_button();
           R_BSP_SoftwareDelay(10U, BSP_DELAY_UNITS_MILLISECONDS);
       }

#if BSP_TZ_SECURE_BUILD
    /* Enter non-secure code */
    R_BSP_NonSecureEnter();
#endif
}




Guess you like

Origin blog.csdn.net/qq_24312945/article/details/132131607