Nonvolatile Memory

Last Updated on : 2024-05-17 03:16:20download

The SDK provides nonvolatile memory for storing application parameters.

  • If you use a one-time programmable (OTP) chip, the storage is 256 bytes of EEPROM.
  • If you use a flash memory chip, the SDK requires 4 KB of flash memory to enable 256-byte incremental storage, producing a similar outcome as EEPROM.

Overview

Concepts

Concepts Description
Nonvolatile memory A type of memory that can retain stored information even after power is removed.
Incremental storage When data is written to flash memory, the entire sector is erased and then reprogrammed with the new data. To ensure efficient and stable storage, write data to new sectors if there is sufficient space. Assume that 256 bytes of variables at the application layer need to be stored.
  • Non-incremental storage uses a 256-byte sector, which is erased before each write operation.
  • Incremental storage uses 16 sectors, each containing 256 bytes. The system sequentially writes data to these sectors one by one. When all 16 sectors are filled, they will be erased.

Features

Call API operations on 256-byte EEPROM. When the chip does not have EEPROM, the bottom layer will simulate the EEPROM implementation with 16 sectors of 256 bytes each.

It is recommended for the application layer to store short data, such as light on/off status, rather than large amounts of data.

Data structure

No external data structure.

API description

Function OPERATE_RET hal_storage_256_read_bytes
(UINT16_T offset, UINT8_T* pdata, UINT8_T len)
-
Purpose Read from 256-byte nonvolatile memory. -
Request parameter offset The offset address to access the 256-byte EEPROM.
-> pdata The address where the desired data resides.
-> len The length of the desired data.
Function OPERATE_RET hal_storage_256_write_bytes
(unsigned short offset, unsigned char* pdata,
unsigned char len)
-
Purpose Write to 256-byte nonvolatile memory. -
Request parameter offset The offset address to access the 256-byte EEPROM.
-> pdata The data to write.
-> len The length of the data to write.

The requirements for offset:

  • HS256_USERoffset < 256
  • offset must be a multiple of eight.

Example

Check the demo tuyaos_demo_beaconmesh_peripheral.

void app_led_reset_run(beacon_dev_s *beacon_dev){
    static u8 reset_judge_time = 1;
    static u8 state = STATE_NOT_PAIRED;
    static u32 count_1s_time = 0;
    static u32 count_1s = 0;

    u8 cnt;

    if(beacon_dev->state == STATE_PAIRED){
        if(reset_judge_time != 0){//300ms~6S
            if(hal_clock_time_exceed(0,reset_judge_time*300000)){//300MS
                if(reset_judge_time == 1){
                    reset_judge_time = 20;
                    if(0 == hal_storage_256_read_bytes(HS256_USER, &cnt,1)){//success
                        if(cnt > RESET_MAX_CNT)cnt = 0;
                    }else{
                        cnt = 0;
                    }

                    cnt++;
                    if(cnt >= RESET_MAX_CNT){
                        cnt = 0;
                        ty_beacon2_node_reset(0,180000000);
                        //app_led_blink(0xFF);
                        reset_judge_time = 0;
                    }
                    hal_storage_256_write_bytes(HS256_USER, &cnt,1);
                }else if(reset_judge_time == 20){
                    reset_judge_time = 0;
                    cnt = 0;
                    hal_storage_256_write_bytes(HS256_USER, &cnt,1);
                }
            }
        }
    }
}

Use one byte at offset 1 in the user area to store the count of power cycles within a short period. This is used to implement the logic that triggers a device reset after three power cycles.

Functional testing

Prerequisites

Enable logging.

Procedure

  1. Write the code to read and write data and verify that the data can be correctly read after writing.
  2. Write the code to read and write data and verify that the data can be correctly read after writing and a power cycle.
  3. Write the code to read and write data and verify that the device works correctly after 100,000 read-and-write operations.

Support and help

If you have any problems with TuyaOS development, you can post your questions in the Tuya Developer Forum.