High level interface to the onboard Non-Volatile memory (Internal Flash, RRAM, OTP region).
Onboard NVM provides non-volatile storage for factory settings, secure data, user firmware, configuration, and bulk data.
This driver allows data to be read from and written to NVM. It also provides the ability to obtain information about the address and characteristics of the NVM block(s) contained on the device. During NVM write time, the device should not be reset (including XRES pin, software reset, and watchdog) or unexpected changes may be made to portions of the NVM. Also, the low-voltage detect circuits should be configured to generate an interrupt instead of a reset.
- Note
- A Read while Write violation may occur for some devices when a NVM Read operation is initiated in the same or neighboring NVM sector/region where the NVM Write, Erase(if applicable), or Program operation is working. Refer the device datasheet for more information. This violation may cause a HardFault exception. To avoid the Read while Write violation, the user must carefully split the Read and Write operations on NVM sectors/regions which are not neighboring, considering all cores in a multi-processor device. User may edit the linker script to place the code into neighboring sectors/regions. For example, use sectors number 0 and 1 for code and sectors 2 and 3 for data storage.
Features
- NVM operations are performed on a per-block (program) or per-sector (erase if applicable) basis (Refer the device datasheet for more information on the block size)
- Supports Blocking or Non-Blocking erase(if applicable), program, and write
Code Snippets
Snippet 1: Get NVM Characteristics
Following code snippet demonstrates how to fetch NVM characteristics. Refer mtb_hal_nvm_info_t for more information.
uint32_t nvm_start_address = 0;
uint32_t nvm_size = 0;
uint32_t nvm_sector_size = 0;
uint32_t nvm_block_size = 0;
uint8_t nvm_erase_value = 0;
bool is_erase_required = false;
mtb_hal_nvm_setup(&nvm_obj, NULL);
mtb_hal_nvm_get_info(&nvm_obj, &nvm_info);
{
region_info += index;
nvm_size = region_info->
size;
}
mtb_hal_nvm_type_t
Enum of Non-volatile memory (NVM) types.
Definition: mtb_hal_nvm.h:131
@ MTB_HAL_NVM_TYPE_FLASH
Internal Flash.
Definition: mtb_hal_nvm.h:133
Information about all of the regions of NVM memory.
Definition: mtb_hal_nvm.h:156
const mtb_hal_nvm_region_info_t * regions
Array of the distinct NVM regions.
Definition: mtb_hal_nvm.h:158
uint8_t region_count
The number of distinct NVM regions.
Definition: mtb_hal_nvm.h:157
Information about a single region of NVM memory.
Definition: mtb_hal_nvm.h:140
uint32_t sector_size
Sector size of the distinct NVM region.
Definition: mtb_hal_nvm.h:146
uint32_t size
Size of the distinct NVM region.
Definition: mtb_hal_nvm.h:145
mtb_hal_nvm_type_t nvm_type
NVM type.
Definition: mtb_hal_nvm.h:141
bool is_erase_required
true = erase required before program, false = erase not required before program.
Definition: mtb_hal_nvm.h:149
uint32_t block_size
Block size (programming granularity) of the distinct NVM region.
Definition: mtb_hal_nvm.h:147
uint8_t erase_value
NVM erase value (if applicable).
Definition: mtb_hal_nvm.h:151
uint32_t start_address
Base address of the distinct NVM region.
Definition: mtb_hal_nvm.h:142
NVM object.
Definition: mtb_hal_hw_types_nvm.h:67
Snippet 2: Blocking NVM Erase-Write and Read
Following code snippet demonstrates blocking NVM write. It uses a constant array with a size equaling the size of one NVM row/block. It uses blocking NVM write operation which blocks the caller until the write is completed. It then verifies the NVM data by comparing the NVM data with the written data.
uint8_t ram_data[512];
uint8_t nvm_read_data[512];
bool compare_status = false;
uint32_t nvm_addr;
mtb_hal_nvm_setup(&nvm_obj, NULL);
mtb_hal_nvm_get_info(&nvm_obj, &nvm_info);
for (size_t index = 0; index < sizeof(ram_data); index++)
{
ram_data[index] = (uint8_t)index;
}
{
}
{
{
result =
sizeof(nvm_read_data));
{
if (0 == memcmp(ram_data, nvm_read_data, sizeof(nvm_read_data)))
{
compare_status = true;
}
}
}
}
else
{
}
cy_rslt_t mtb_hal_nvm_erase(mtb_hal_nvm_t *obj, uint32_t address)
Erase one block of NVM starting at the given address.
Definition: mtb_hal_nvm.c:160
cy_rslt_t mtb_hal_nvm_program(mtb_hal_nvm_t *obj, uint32_t address, const uint32_t *data)
Program one block with the provided data starting at the given address.
Definition: mtb_hal_nvm.c:178
cy_rslt_t mtb_hal_nvm_read(mtb_hal_nvm_t *obj, uint32_t address, uint8_t *data, size_t size)
Read size amount of data starting from the given address of NVM.
Definition: mtb_hal_nvm.c:102
uint32_t cy_rslt_t
Provides the result of an operation as a structured bitfield.
Definition: cy_result.h:481
#define CY_RSLT_SUCCESS
cy_rslt_t return value indicating success
Definition: cy_result.h:508
Snippet 3: Non-blocking NVM Erase-Write and Read
Following code snippet demonstrates non-blocking NVM write. It uses a constant array with a size equaling the size of one NVM row/block. It uses non-blocking NVM write operation which to not block the caller. It then verifies the NVM data by comparing the NVM data with the written data.
- Note
- When using non-blocking NVM operations:
- Interrupts must be enabled.
- NVM memory have to be initialized and configured properly before starting the non-blocking operation.
- The user must ensure that the CPU is not in critical section for the duration of the non-blocking NVM operation, otherwise NVM operation will not happen and the operation complete mtb_hal_nvm_is_operation_complete() will always return false.
uint8_t ram_data[512];
uint8_t nvm_read_data[512];
bool compare_status = false;
uint32_t nvm_addr;
#if defined(CY_IP_MXS40FLASHC)
Cy_Flash_Init(false);
#endif
mtb_hal_nvm_setup(&nvm_obj, NULL);
mtb_hal_nvm_get_info(&nvm_obj, &nvm_info);
for (size_t index = 0; index < sizeof(ram_data); index++)
{
ram_data[index] = (uint8_t)index;
}
{
}
{
{
{
}
result =
sizeof(nvm_read_data));
{
if (0 == memcmp(ram_data, nvm_read_data, sizeof(nvm_read_data)))
{
compare_status = true;
}
}
}
#if defined(CY_IP_MXS40FLASHC)
Cy_Flash_Init(false);
cy_stc_flash_callback_t config = {0};
config.callback_operation_complete = flash_irq_complete_callback;
Cy_Flash_RegisterCallback(&config);
mtb_hal_nvm_setup(&nvm_obj, NULL);
mtb_hal_nvm_get_info(&nvm_obj, &nvm_info);
flash_operation_complete = false;
{
while (!flash_operation_complete)
{
}
flash_operation_complete = false;
result =
sizeof(nvm_read_data));
{
if (0 == memcmp(ram_data, nvm_read_data, sizeof(nvm_read_data)))
{
compare_status = true;
}
}
}
#endif
}
else
{
}
cy_rslt_t mtb_hal_nvm_program_nb(mtb_hal_nvm_t *obj, uint32_t address, const uint32_t *data)
Program one block with the provided data starting at the given address (non-blocking).
Definition: mtb_hal_nvm.c:205
cy_rslt_t mtb_hal_nvm_erase_nb(mtb_hal_nvm_t *obj, uint32_t address)
Erase one block of NVM starting at the given address (non-blocking).
Definition: mtb_hal_nvm.c:187
cy_rslt_t mtb_hal_nvm_write_nb(mtb_hal_nvm_t *obj, uint32_t address, const uint32_t *data)
Write new data into the block starting at the given address (non-blocking).
Definition: mtb_hal_nvm.c:196
bool mtb_hal_nvm_is_operation_complete(mtb_hal_nvm_t *obj)
Check if a non-blocking NVM operation is complete.
Definition: mtb_hal_nvm.c:214
|
| cy_rslt_t | mtb_hal_nvm_read (mtb_hal_nvm_t *obj, uint32_t address, uint8_t *data, size_t size) |
| | Read size amount of data starting from the given address of NVM. More...
|
| |
| cy_rslt_t | mtb_hal_nvm_erase (mtb_hal_nvm_t *obj, uint32_t address) |
| | Erase one block of NVM starting at the given address. More...
|
| |
| cy_rslt_t | mtb_hal_nvm_write (mtb_hal_nvm_t *obj, uint32_t address, const uint32_t *data) |
| | This function erases the block, if required, and writes the new data into the block starting at the given address. More...
|
| |
| cy_rslt_t | mtb_hal_nvm_program (mtb_hal_nvm_t *obj, uint32_t address, const uint32_t *data) |
| | Program one block with the provided data starting at the given address. More...
|
| |
| cy_rslt_t | mtb_hal_nvm_erase_nb (mtb_hal_nvm_t *obj, uint32_t address) |
| | Erase one block of NVM starting at the given address (non-blocking). More...
|
| |
| cy_rslt_t | mtb_hal_nvm_write_nb (mtb_hal_nvm_t *obj, uint32_t address, const uint32_t *data) |
| | Write new data into the block starting at the given address (non-blocking). More...
|
| |
| cy_rslt_t | mtb_hal_nvm_program_nb (mtb_hal_nvm_t *obj, uint32_t address, const uint32_t *data) |
| | Program one block with the provided data starting at the given address (non-blocking). More...
|
| |
| bool | mtb_hal_nvm_is_operation_complete (mtb_hal_nvm_t *obj) |
| | Check if a non-blocking NVM operation is complete. More...
|
| |
| const mtb_hal_nvm_region_info_t * | mtb_hal_nvm_get_region_for_address (mtb_hal_nvm_t *obj, uint32_t addr, uint32_t length) |
| | Find the nvm region based on given address and length. More...
|
| |
◆ mtb_hal_nvm_type_t
Enum of Non-volatile memory (NVM) types.
| Enumerator |
|---|
| MTB_HAL_NVM_TYPE_INVALID | Invalid type of NVM.
|
| MTB_HAL_NVM_TYPE_FLASH | Internal Flash.
|
| MTB_HAL_NVM_TYPE_RRAM | RRAM.
|
| MTB_HAL_NVM_TYPE_OTP | OTP.
|
◆ mtb_hal_nvm_read()
Read size amount of data starting from the given address of NVM.
- Parameters
-
| [in] | obj | The NVM object. |
| [in] | address | Address to begin reading from. |
| [out] | data | The buffer to read data into. |
| [in] | size | The number of bytes to read. |
- Returns
- The status of the read request. Returns CY_RSLT_SUCCESS on successful operation.
Refer Snippet 2: Blocking NVM Erase-Write and Read for more information.
◆ mtb_hal_nvm_erase()
Erase one block of NVM starting at the given address.
The address must be at block boundary. This will block until the erase operation is complete.
- See also
- mtb_hal_nvm_get_info() to get the NVM characteristics for legal address values, is erase required/applicable, and the erase block size(if applicable).
- Parameters
-
| [in] | obj | The NVM object |
| [in] | address | The block address to be erased |
- Returns
- The status of the erase request. Returns CY_RSLT_SUCCESS on successful operation.
Refer Snippet 2: Blocking NVM Erase-Write and Read for more information.
◆ mtb_hal_nvm_write()
This function erases the block, if required, and writes the new data into the block starting at the given address.
The address must be at block boundary. This will block until the write operation is complete.
- See also
- mtb_hal_nvm_get_info() to get the NVM characteristics for legal address values and the write block size. The provided data buffer must be at least as large as the NVM block_size.
- Note
- Generally the
data to be written must be located in the SRAM memory region.
- Parameters
-
| [in] | obj | The NVM object |
| [in] | address | The address of the block to be written |
| [in] | data | The data buffer to be written to the NVM block |
- Returns
- The status of the write request. Returns CY_RSLT_SUCCESS on successful operation.
Refer Snippet 2: Blocking NVM Erase-Write and Read for more information.
◆ mtb_hal_nvm_program()
Program one block with the provided data starting at the given address.
The address must be at block boundary. This will block until the write operation is complete.
- Note
- This function does not erase the block prior to writing. The block must be erased first via a separate call to erase.
- See also
- mtb_hal_nvm_get_info() to get the NVM characteristics for legal address values and the total block size. The provided data buffer must be at least as large as the NVM block_size.
- Note
- Generally the
data to be programmed must be located in the SRAM memory region.
- Parameters
-
| [in] | obj | The NVM object |
| [in] | address | The address of the block to be programmed |
| [in] | data | The data buffer to be programmed to the NVM block |
- Returns
- The status of the program request. Returns CY_RSLT_SUCCESS on successful operation.
◆ mtb_hal_nvm_erase_nb()
Erase one block of NVM starting at the given address (non-blocking).
The address must be at block boundary. This function starts the erase operation and returns immediately. Use mtb_hal_nvm_is_operation_complete() to check completion.
- See also
- mtb_hal_nvm_get_info() to get the NVM characteristics for legal address values, is erase required/applicable, and the erase block size(if applicable).
- Parameters
-
| [in] | obj | The NVM object |
| [in] | address | The block address to be erased |
- Returns
- The status of the erase request. Returns CY_RSLT_SUCCESS on successful operation start.
◆ mtb_hal_nvm_write_nb()
Write new data into the block starting at the given address (non-blocking).
The address must be at block boundary. This function starts the write operation and returns immediately. Use mtb_hal_nvm_is_operation_complete() to check completion.
- See also
- mtb_hal_nvm_get_info() to get the NVM characteristics for legal address values and the write block size. The provided data buffer must be at least as large as the NVM block_size.
- Note
- Generally the
data to be written must be located in the SRAM memory region.
- Parameters
-
| [in] | obj | The NVM object |
| [in] | address | The address of the block to be written |
| [in] | data | The data buffer to be written to the NVM block |
- Returns
- The status of the write request. Returns CY_RSLT_SUCCESS on successful operation start.
◆ mtb_hal_nvm_program_nb()
Program one block with the provided data starting at the given address (non-blocking).
The address must be at block boundary. This function starts the program operation and returns immediately. Use mtb_hal_nvm_is_operation_complete() to check completion.
- Note
- This function does not erase the block prior to writing. The block must be erased first via a separate call to erase.
- See also
- mtb_hal_nvm_get_info() to get the NVM characteristics for legal address values and the total block size. The provided data buffer must be at least as large as the NVM block_size.
- Note
- Generally the
data to be programmed must be located in the SRAM memory region.
- Parameters
-
| [in] | obj | The NVM object |
| [in] | address | The address of the block to be programmed |
| [in] | data | The data buffer to be programmed to the NVM block |
- Returns
- The status of the program request. Returns CY_RSLT_SUCCESS on successful operation start.
◆ mtb_hal_nvm_is_operation_complete()
Check if a non-blocking NVM operation is complete.
- Parameters
-
- Returns
- True if operation is complete and false if still in progress.
◆ mtb_hal_nvm_get_region_for_address()
Find the nvm region based on given address and length.
If "length is zero and address is not in any nvm region" or if "length is not zero and address is not in any nvm region" or if "length is not zero and address is one nvm region but address + length goes into another nvm
region", the function will return Null.
- Parameters
-
| [in] | obj | The NVM object |
| [in] | addr | The start address of the block |
| [in] | length | The legnth to block |
- Returns
- pointer, mtb_hal_nvm_region_info_t, to nvm region