/*******************************************************************************
* Copyright (C) 2021-2022, All Rights Reserved.
*
* File:          test_hal_flash.c
* Description：  hal_flash 测试用例源文件
* Version：      V0.1
* Date：         2022-04-21
* Author：       RANDY
 *******************************************************************************/

/*******************************************************************************
* 1.Included files
*******************************************************************************/
#include "demo_hal_flash.h"
#include "hal_flash.h"
#include "tau_delay.h"
#include "tau_log.h"
#include "tau_common.h"
#include "test_cfg_global.h"

#if _MODULE_DEMO_FLASH_EN
/*******************************************************************************
* 2.Global constant and macro definitions using #define
*******************************************************************************/
#ifdef LOG_TAG
#undef LOG_TAG
#endif
#define LOG_TAG "demo_flash"

#define TEST_DATA_SIZE     32

#define DATA_BLOCK_SIZE    (64*1024)
#define DATA_PAGE_SIZE     1024

/*******************************************************************************
* 3.Global structures, unions and enumerations using typedef
*******************************************************************************/
static fls_ops_cfg_t *fls_ops_cfg;

/*******************************************************************************
* 4.Global variable extern declarations
*******************************************************************************/

/*******************************************************************************
* 5.Global function prototypes
*******************************************************************************/

/**************************************************************************
* @name :
* @brief
* @param
* @retval
**************************************************************************/

void test_hal_flash_get_public_region_test(void)
{
    uint8_t test_data[TEST_DATA_SIZE] = 0;
    TAU_LOGD("test get data public\n");

    fls_ops_cfg->flash_block         = 7;//固定值
    fls_ops_cfg->flash_page          = 0;//0-32
    fls_ops_cfg->page_offset_addr    = 0;
    fls_ops_cfg->data_size           = TEST_DATA_SIZE;
    fls_ops_cfg->user_data           = test_data;

    hal_flash_init();

    //1 读数据
    hal_flash_public_region_ops(FLASH_PUBLIC_READ, fls_ops_cfg);

    for (uint8_t i = 0; i < TEST_DATA_SIZE; i++)
    {
        TAU_LOGD("read data[%d]=0x%x ", i, test_data[i]);
    }

    //2 写数据
    for (uint32_t i = 0; i < TEST_DATA_SIZE; i++)
    {
        test_data[i] = i;
    }

    hal_flash_public_region_ops(FLASH_PUBLIC_WRITE, fls_ops_cfg);

    //3 回读
    for (uint32_t i = 0; i < TEST_DATA_SIZE; i++)
    {
        test_data[i] = 0;
    }

    hal_flash_public_region_ops(FLASH_PUBLIC_READ, fls_ops_cfg);

    //4 对比
    for (uint8_t i = 0; i < TEST_DATA_SIZE; i++)
    {
        TAU_LOGD("read data[%d]=0x%x ", i, test_data[i]);
    }

}



// 验证flash UID的读取，16byte的UID
void test_flash_uid_read(void)
{
    uint8_t get_id[16] = {0};

    TAU_LOGD("test_flash_uid_read begin \n");

    hal_flash_init();

    hal_flash_read_uid(get_id, 16);
    //不同的IC，ID不一样，存在0x00或者0xFF的情况
    for (uint8_t i = 0; i < 16; i++)
    {
        TAU_LOGD("read ID [%d]=0x%X \n", i, get_id[i]);
    }
}


/**
* @brief  获取user_data的数据个数
* @param  无
* @retval bool  无
*/
void  get_user_data_size(void)
{
    uint32_t user_data_size = 0;

    TAU_LOGD("test get data addr\n");

    user_data_size = hal_flash_get_user_data_size();

    TAU_LOGD("user_data size 0x%x \n", user_data_size);
}


void  test_hal_flash_get_user_region_test(void)
{
    uint8_t user_data[32];
    uint32_t user_address = 0;  //需要读取数据的位置
    uint32_t user_data_size = 0;

    TAU_LOGD("test get data addr\n");
    //读取user data 0地址
    user_address = 0;

    fls_ops_cfg->flash_block         = user_address / DATA_BLOCK_SIZE;
    fls_ops_cfg->flash_page          = (user_address % DATA_BLOCK_SIZE) / DATA_PAGE_SIZE;
    fls_ops_cfg->page_offset_addr    = user_address % DATA_PAGE_SIZE;
    fls_ops_cfg->data_size           = 16;
    fls_ops_cfg->user_data           = user_data;
    hal_flash_user_region_ops(FLASH_USERDATA_READ, fls_ops_cfg);
    for (uint8_t i = 0; i < 2; i++)
    {
        TAU_LOGD("user_data[%d]=0x%x ", i, user_data[i]);
    }

    //读取user data 0x6000地址
    user_address = 0x6000;

    fls_ops_cfg->flash_block         = user_address / DATA_BLOCK_SIZE;
    fls_ops_cfg->flash_page          = (user_address % DATA_BLOCK_SIZE) / DATA_PAGE_SIZE;
    fls_ops_cfg->page_offset_addr    = user_address % DATA_PAGE_SIZE;
    fls_ops_cfg->data_size           = 16;
    fls_ops_cfg->user_data           = user_data;
    hal_flash_user_region_ops(FLASH_USERDATA_READ, fls_ops_cfg);
    for (uint8_t i = 0; i < 2; i++)
    {
        TAU_LOGD("user_data[%d]=0x%x ", i, user_data[i]);
    }
}


/**
* @brief  获取user_data的绝对起始地址 的接口测试
* @param  无
* @retval bool  无
*/

void demo_hal_flash(void)
{
    uint8_t case_sel = 2;

    switch (case_sel)
    {
    case 1:
        test_hal_flash_get_public_region_test();
        break;

    case 2:
        test_hal_flash_get_user_region_test();
        break;

    case 3:
        test_flash_uid_read();
        break;

    case 4:
        get_user_data_size();
        break;

    case 5:
        //测试共享flash接口,需要外接SPI主机测试
        TAU_LOGI("test TMON \n");
        hal_flash_share_mode(true);
        break;

    default:
        break;
    }


}



#endif

