Renesas Flash HAL User Manual

Application Note Renesas Synergy™ Platform
Flash HAL Module Guide
Introduction
This module guide will enable you to effectively use a module in your own design. Upon completion of this guide, you will be able to add this module to your own design, configure it correctly for the target application and write code, using the included application project code as a reference and efficient starting point. References to more detailed API descriptions and suggestions of other application projects that illustrate more advanced uses of the module are available in the Renesas Synergy™ Knowledge Base (as described in the References section in this document) and should be valuable resources for creating more complex designs.
There are two separate Flash modules: r_flash_lp and r_flash_hp. The High-Performance Flash module (Flash_HP) is used for programming the S7 and S5 MCU Series. The Low-Power Flash module (Flash_LP) is used for programming the S3 and S1 MCU Series. The two are not interchangeable, alt hou gh the APIs and other features of the modules are very similar. This guide covers the operation of both HAL modules.
Contents
1. Flash HAL Module Features .................................................................................................... 2
2. Flash HAL Module APIs Overview ........................................................................................... 2
3. Flash HAL Module Operational Overview ................................................................................ 4
3.1 Flash HAL Module Important Operational Notes and Limitations ........................................................... 4
3.1.1 Flash HAL Module Operational Notes ................................................................................................... 4
3.1.2 Flash HAL Module Limitations ............................................................................................................... 6
4. Including the Flash HAL Module in an Application ................................................................... 6
5. Configuring the Flash HAL Module .......................................................................................... 6
5.1 Flash HAL Module Clock Configuration .................................................................................................. 8
5.2 Flash HAL Module Clock Configuration .................................................................................................. 8
5.3 Flash HAL Module Pin Configuration ...................................................................................................... 9
6. Using the Flash HAL Module in an Application ........................................................................ 9
7. The Flash HAL Module Application Project .............................................................................. 9
8. Customizing the Flash HAL Module for a Target Application ................................................. 13
9. Running the Flash HAL Module Application Project ............................................................... 13
10. Flash HAL Module Conclusion ............................................................................................... 15
11. Flash HAL Module Next Steps ............................................................................................... 15
12. Flash HAL Module Reference Information ............................................................................. 15
13. Revision History ..................................................................................................................... 17
R11AN0087EU0120 Rev.1.20 Page 1 of 17 Apr.29.19
Renesas Synergy™ Platform Flash HAL Module Guide
Function Name
Example API Call and Description
.open
g_flash0.p_api->open(g_flash0.p_ctrl,
Open FLASH device.
.write
g_flash0.p_api->write(g_flash0.p_ctrl,(uint32_t)
Write FLASH device.
.read
g_flash0.p_api->read(g_flash0.p_ctrl, read_buffer,
Read FLASH device.
.erase
g_flash0.p_api->erase(g_flash0.p_ctrl,
Erase FLASH device.

1. Flash HAL Module Features

The Flash HAL modules APIs allow an application to read, write, and erase both the data and ROM flash areas that reside within the MCU. The amount of flash memory available varies across MCU parts, but the API functions apply to all devices. Key features of the Flash HAL modules include:
Support for block erasing, reading, writing, and blank checking of code flash (ROM).
Support for both blocking and non-blocking, erasing, reading, writing, and blank checking of data flash.
Support for blocking erasing, reading, writing, and blank checking of code flash.
Support for callback functions for completion of non-blocking data-flash operation s.
Support for access window (write protection) for ROM Flash, allowing only specified areas of code flash
to be erased or written.
Support for boot block-swapping, which allows safe rewriting of the startup program without first erasing it.
Figure 1. Flash HAL Module Block Diagram

2. Flash HAL Module APIs Overview

The Flash HAL module defines APIs for several operations including opening, reading, erasing, and closing the flash memory. A complete list of the available APIs, an example API call, and a short description of each can be found in the following table. A table of status return values follows the API summary table.
Table 1. Flash HAL Module API Summary
g_flash0.p_cfg);
write_buffer, FLASH_CF_32KB_BLOCK55, CODE_BLOCK_SIZE_32KB);
DATA_FLASH_ADDR, num_bytes);
FLASH_CF_32KB_BLOCK55,num_sectors);
R11AN0087EU0120 Rev.1.20 Page 2 of 17 Apr.29.19
Renesas Synergy™ Platform Flash HAL Module Guide
Function Name
Example API Call and Description
.blankCheck
g_flash0.p_api->blankCheck(g_flash0.p_ctrl,
Blank check FLASH device.
.close
g_flash0.p_api->close(g_flash0.p_ctrl);
.statusGet
g_flash0.p_api->statusGet(g_flash0.p_ctrl);
Get Status for FLASH device.
.accessWindowSet
g_flash0.p_api->accessWindowSet(g_flash0.p_ctrl,
Set Access Window for FLASH device.
.accessWindowClear
g_flash0.p_api->accessWindowClear(g_flash0.p_ctrl);
Clear any existing code-flash access window for FLASH device.
.idCodeSet
g_flash0.p_api-> idCodeSet(g_flash0.p_ctrl,id_bytes, .reset
g_flash0.p_api->reset(g_flash0.p_ctrl);
Reset function for FLASH device.
.updateFlashClockFreq
g_flash0.p_api-> updateFlashClockFreq
.startupAreaSelect
g_flash0.p_api->startupAreaSelect(g_flash0.p_ctrl,
Refer to the table below for all the possible values for parameter2.
.versionGet
g_flash0.p_api->versionGet(&version);
Swap Type
Is_temporary
Operation
FLASH_STARTUP_AREA_BLOCK0
False
On next reset, Startup ar ea is Block 0.
FLASH_STARTUP_AREA_BLOCK0
False
On next reset, Startup ar ea is Block 0.
FLASH_STARTUP_AREA_BLOCK1
False
On next reset, Startup ar ea is Block 1.
FLASH_STARTUP_AREA_BLOCK1
True
Startup area is immediately, but temporarily
FLASH_STARTUP_AREA_BTFLG
True
Startup area is immediately, but temporarily Configuration BTFLG.
Name
Description
SSP_SUCCESS
Function successful.
SSP_ERR_IN_USE
Device in use error.
SSP_FLASH_ERR_FAILURE
Flash failure error.
SSP_ERR_FCLK
FCLK must be a minimum of 4 MHz for Flash operations.
SSP_ERR_TIMEOUT
Timeout error.
SSP_ERR_INVALID_SIZE
Invalid size error.
SSP_ERR_INVALID_ADDRESS
Invalid address error.
FLASH_CF_32KB_BLOCK55, FLASH_DATA_BLOCK_SIZE, &blankCheck);
Close FLASH device.
FLASH_CF_32KB_BLOCK1, FLASH_CF_32KB_BLOCK3);
mode);
Write the ID code provided to the id coderegisters.
(g_flash0.p_ctrl);
Update Flash clock frequency (FCLK) and recalculate timeout values.
FLASH_STARTUP_AREA_BLOCK1);
Select which block - Default (Block 0) or Alternate (Block 1) is used as the start-up area block.
Retrieve the API version using the versi on poi nter.
Note: For details on operation and definitions for the function data structures, typedefs, defines, API data,
API structures and function variables, review the SSP User’s Manual available as described in the References section in this document.
Table 2. .setupAreaSelect Parameter2 Options
switched to Block 1.
switched to the Block determined by the
Table 3. Status Return Values
R11AN0087EU0120 Rev.1.20 Page 3 of 17 Apr.29.19
Renesas Synergy™ Platform Flash HAL Module Guide
Name
Description
SSP_ERR_ASSERTION
Assertion error.
SSP_ERR_INVALID_BLOCKS
Invalid number of blocks specified.
SSP_ERR_INVALID_ARGUMENT
Invalid argument error.
SSP_ERR_HW_LOCKED
Peripheral already in use.
SSP_ERR_CMD_LOCKED
FCU is in locked state, typically as a result of attempting to Erase an area that is protected by an Access Window.
SSP_ERR_NOT_OPEN
Flash has not yet been opened.
SSP_ERR_IRQ_BSP_DISABLED
Caller is requesting BGO (background mode operation) but the Flash interrupt is not enabled.
SSP_ERR_WRITE_FAILED
Write operation failed. This may be returned if the requested SSP_ERR_PE_FAILURE
Failed to enter P/E mode
SSP_ERR_INVALID_HW_CONDITION
Detected hardware is in invalid condition
SSP_ERR_INVALID_LINKED_ADDRESS
Function or data is linked at an invalid region of memory
False
Supplied address is valid flash address on this MCU.
True
Supplied address is valid and p_block info contains the details on this address block.
Flash area is not blank.
Note: Lower-level drivers may return common error codes. Refer to the SSP User’s Manual API Reference
section for the associated module for a definition of all relevant status return values.

3. Flash HAL Module Operational Overview

The Flash API makes the process of programming and erasing on-chip flash areas easy. Both code (User ROM) and data-flash areas are supported. The API, in its simplest form, can be used to perform blocking erase and program operations. The term blocking means that when a program or erase function is called, the function does not return until the operation has finished. This API supports blocking for both code and data-flash, with BGO (background-mode operation) available for data-flash operations only. When a code­flash operation is on-going, you cannot access that code-flash area. If you attempt to access the code-flash area while a code-flash operation is in progress, the flash-control unit transitions into an error state.
It is important to keep in mind that even though a code-flash operation is blocking, there are several situations where the code-flash could still end up being accessed while the operation is blocking. These must be prevented. These include:
Vector table access if the Vector table is located in the ROM.
ROM access by an interrupt vectoring to a ROM address, even if the vector table itself is not in ROM.
A multithreaded application where multiple threads are allowed to continue to run while a code-flash operation is blocking.

3.1 Flash HAL Module Important Operational Notes and Limitations

startupAreaSelect() swaps data in to block 0. Be sure that the swapped-in data is valid if you use startupAreaSelect().

3.1.1 Flash HAL Module Operational Notes

Data-Flash BGO Precautions
When using the data-flash BGO, the User ROM, RAM, and external memory can still be accessed. You must ensure that the data-flash is not accessed during a data-flash operation. This includes interrupts that may access the data-flash.
Code-Flash Precautions
BGO mode is not supported for code-flash, so a code-flash operation is not returned before the operation has completed. By default, the vector table resides in the user ROM (code-flash). If an interrupt occurs during the ROM operation, then ROM is accessed to retrieve the interrupt’s starting address and an error occurs.
R11AN0087EU0120 Rev.1.20 Page 4 of 17 Apr.29.19
Renesas Synergy™ Platform Flash HAL Module Guide
The simplest work-around is to disable interrupts during code-flash operations. Another option is to copy the vector table to RAM, update the VTOR (Vector Table Offset Register) accordingly and ensure that any interrupt service routines execute out of RAM. Similarly, you must ensure that if in a multithreaded environment, threads running from ROM cannot become active while a code-flash operation is in progress.
Blank Checking
The blankCheck API function checks whether code or data-flash contents are blank. It is not possible to write to flash (code or data) without first erasing it. The blankCheck function determines whether a specified area is blank and therefore writable. In almost all cases, it is not sufficient to compare flash contents to 0xFF to determine whether the area is blank. The one exception is Flash HP code-flash. A 0xFF in Flash_HP code-flash does indicate blank. Renesas strongly recommends using the blankCheck API function in all cases.
Flash Status
The statusGet API function allows the application to query the ‘Ready’ status of the flash. This is useful in data-flash BGO operations when you choose not to use a callback function, so there is no asynchronous notification of a completed data-flash operation. In this case, the data-flash is configured to operate in BGO mode, so once the operation is started (an erase, for example), the call returns immediately with the operation executing in the background. By calling the statusGet API function, you can determine when the operation has safely completed or generated an error, and it is now safe to proceed with another flash operation.
Swap Blocks
The startupAreaSelect API function allows the user to select which block - default (Block 0) or alternate (Block 1) - is used as the startup-area block. The provided parameters determine which block becomes the active startup block and whether that action is immediate (but temporary) or permanent, subsequent to the next reset.
Doing a temporary switch might appear to have limited usefulness; however, if there is an access window in place such that Block 0 is write-protected, then you could do a temporary switch, update the block, and switch them back without having to touch the access window.
Flash Clock (FCLK)
The FCLK is the clock used by the Flash peripheral in performing all Flash operations. It must be >= 4 MHz for successful flash operations. As part of the open function, the Flash clock is checked, and if < 4 MHz open API returns SSP_ERR_FCLK. Once the Flash API has been opened, if the FCLK frequency is changed, the updateFlashClockFreq API function must be called to inform the API of the change. Failure to do so could result in flash operation failures and possibly damage the part.
Interrupts
Enable the flash ready interrupt only if you plan to use the data-flash BGO. In this mode, the application can initiate a data-flash operation and then be asynchronously notified of its completion, or an error, using a user­supplied callback function. The callback function is passed a structure containing event information that indicates the source of the callback event (that is FLASH_EVENT_ERASE_COMPLETE).
When the FLASH FRDYI interrupt is enabled, the corresponding ISR is defined in the flash driver. The ISR calls a user-callback function if one was registered with the open API.
Note: The Flash HP supports an additional flash-error interrupt and if the BGO mode is enabled for the
FLASH HP then both FRDYI and FIFERR interrupts must be given a priority.
AccessWindow
An access window defines a contiguous area in code flash for which programming/erase is enabled. This area is on block boundaries with a starting and ending address being provided to accessWindowSet. The block containing the start address is the first block. The block containing the end address is the last block. The access window then becomes the first block – last block inclusive. Anything outside this range is write protected. Invalid address information provided to accessWindowSet returns SSP_ERR_INVALID_ADDRESS. An access window may be removed by calling the accessWindowClear API function.
R11AN0087EU0120 Rev.1.20 Page 5 of 17 Apr.29.19
Renesas Synergy™ Platform Flash HAL Module Guide
Resource
ISDE Tab
Stacks Selection Sequence
g_flash0 Flash Driver on r_rflash_hp
Threads
New Stack > Driver > Storage > Flash Driver on r_flash_hp
g_flash0 Flash Driver on r_rflash_lp
Threads
New Stack > Driver > Storage > Flash Driver on r_flash_lp

3.1.2 Flash HAL Module Limitations

The High-Performance Flash module (Flash_HP) is the API used for programming the S7 and S5 family of MCUs.
The Low-Power Flash module (Flash_LP) is the API used for programming the S3 and S1 family of MCUs.
Refer to the latest SSP Release Note for any additional operational limitations for this module.

4. Including the Flash HAL Module in an Application

This section describes how to include the Flash HAL module in an application using the SSP Configurator. Note: It is assumed you are familiar with creating a project, adding threads, adding a stack to a thread and
configuring a block within the stack. If you are unfamiliar with any of these items, refer to the first few chapters of the SSP Us er ’s Manual to learn how to manage each of these important steps in creating SSP-based applications.
To add the Flash Driver to an application, simply add it to a thread using the stacks selection sequence given in the following table. (The default name for the Flash Driver is g_flash0. This name can be changed in the associated Properties window.)
Table 4. Flash Driver Selection Sequence
When the Flash HAL modules on r_flash_hp or r_flash_lp are added to the thread stack as shown in the figure below, the configurator automatically adds any needed lower-level modules. Any drivers that need additional configuration information is box text highlighted in Red. Modules with a Gray band are individual modules that stand alone.
Note: The following figure shows both Flash HAL modules. Only one module should be used, depending on
the selected MCU; they are only shown together for completeness.
Figure 2. Flash HAL Module Stack

5. Configuring the Flash HAL Module

The Flash HAL module must be configured by the user for the desired operation. The SSP configuration window automatically identifies (by highlighting the block in red) any required configuration selections, such as interrupts or operating modes, which must be configured for lower-level modules for successful operation. Only those properties that can be changed without causing conflicts are available for modification. Other properties are ‘locked’ and are not available for changes and are identified with a lock icon for the ‘locked’ property in the Properties window in the ISDE. This approach simplifies the configuration process and makes it much less error prone than previous ‘manual’ approaches to configuration. The available configuration settings and defaults for all the user-accessible properties are given in the Properties tab within the SSP configurator and are shown in the following tables for easy reference.
R11AN0087EU0120 Rev.1.20 Page 6 of 17 Apr.29.19
Loading...
+ 12 hidden pages