Files
kunlun/dtest/kl2_ndfc_test/kl2_ndfc_test.c
2024-09-28 14:24:04 +08:00

424 lines
9.7 KiB
C
Executable File

/****************************************************************************
*
* Copyright(c) 2019 by Aerospace C.Power (Chongqing) Microelectronics. ALL RIGHTS RESERVED.
*
* This Information is proprietary to Aerospace C.Power (Chongqing) Microelectronics Ltd and MAY NOT
* be copied by any method or incorporated into another program without
* the express written consent of Aerospace C.Power. This Information or any portion
* thereof remains the property of Aerospace C.Power. The Information contained herein
* is believed to be accurate and Aerospace C.Power assumes no responsibility or
* liability for its use in any way and conveys no license or title under
* any patent or copyright and makes no representation or warranty that this
* Information is free from patent or copyright infringement.
*
* ****************************************************************************/
/* os shim includes */
#include "os_types.h"
#include "os_task.h"
#include "os_utils.h"
#include "iot_errno_api.h"
/* common includes */
#include "iot_io.h"
#include "iot_bitops.h"
#include "iot_config.h"
/* driver includes */
#include "iot_clock.h"
#include "iot_uart.h"
#include "iot_i2c_api.h"
#include "iot_gpio_api.h"
#include "i2c_slv_hw.h"
/* cli includes */
#include "iot_cli.h"
#include "iot_uart_h.h"
/* debug includes*/
#include "dbg_io.h"
#include "hw_reg_api.h"
#include "ndfc.h"
#include "apb.h"
#include "ahb_hw.h"
#include "ahb.h"
#include "gpio_mtx_reg.h"
#include "iot_gpio_api.h"
#include "iot_nand_flash_api.h"
os_task_h test_init_handle;
extern int platform_init();
uint8_t *p_buf_raw;
uint8_t *p_buf_rd;
uint8_t *p_buf_wt;
ndfc_cfg_t g_test_ndfc_cfg = {
.data_mode = DATA_MODE_X4,
.gpio = {
.cs = 51,
.clk = 53,
.d0 = 50,
.d1 = 48,
.d2 = 49,
.d3 = 52,
},
};
#define TEST_RW_DATA_LEN (3 * 1024)
#define TEST_RW_DATA_OFFSET (0x800 + 0x700)
#define TEST_RW_DUMP_SIZE 128
char print_buf[128];
int IRAM_ATTR uart_e_try_putc(int port, char c);
void print_in_int(const char *err_msg)
{
while(*err_msg != '\0'){
uart_e_try_putc(0, *err_msg);
++err_msg;
}
return;
}
#if 0
void test_ndfc_isr(void)
{
uint32_t status = iot_ndfc_get_int_status();
iot_sprintf(print_buf, "\nNDFC isr status:0x%08x.", status);
print_in_int(print_buf);
return;
}
#endif
void ahb_reset(uint32_t module);
void test_data_dump(uint8_t *data, uint32_t dlen)
{
uint32_t i = 0;
uint8_t *p_data;
p_data = data;
for(i = 0; i < dlen; i++)
{
if(0 == (i % 32))
{
iot_printf("\n");
}
iot_printf("%02X ", ((int)*p_data) & 0xFF);
p_data++;
}
return;
}
void test_spi(void)
{
uint8_t dev_ID = 0;
iot_nandfc_cfg_t cfg = {0};
cfg.spi.gpio.clk = 49;
cfg.spi.gpio.cs = 53;
cfg.spi.gpio.miso = 48;
cfg.spi.gpio.mosi = 50;
cfg.spi.port = 0;
iot_nandfc_init(&cfg);
iot_nandfc_reset_device();
dev_ID = iot_nandfc_read_chip_ID();
iot_printf("\nTest SPI get id = %d!", dev_ID);
}
/* Multi-use GPIOs with sfc,sig4~7. */
void test_ndfc_unlink_sfc_sig(void)
{
uint32_t cnt, lev;
GPIO_MTX_WRITE_REG(CFG_SIG4_IN_CFG_ADDR, 0xFF);
GPIO_MTX_WRITE_REG(CFG_SIG5_IN_CFG_ADDR, 0xFF);
GPIO_MTX_WRITE_REG(CFG_SIG6_IN_CFG_ADDR, 0xFF);
GPIO_MTX_WRITE_REG(CFG_SIG7_IN_CFG_ADDR, 0xFF);
iot_gpio_open_as_output(g_test_ndfc_cfg.gpio.clk);
iot_gpio_open_as_output(g_test_ndfc_cfg.gpio.cs);
iot_gpio_open_as_output(g_test_ndfc_cfg.gpio.d0);
iot_gpio_open_as_output(g_test_ndfc_cfg.gpio.d1);
iot_gpio_open_as_output(g_test_ndfc_cfg.gpio.d2);
iot_gpio_open_as_output(g_test_ndfc_cfg.gpio.d3);
iot_gpio_value_set(g_test_ndfc_cfg.gpio.cs, 1);
lev = 0;
for(cnt = 0; cnt < 20; cnt++)
{
iot_gpio_value_set(g_test_ndfc_cfg.gpio.clk, lev);
lev = !lev;
}
}
int sfc_init(void);
int sfc_get_dev_id(uint8_t *data);
void test_ndfc_func(void)
{
uint32_t data, *p_data, *p_data_2;
char *p_mode[]={"Invalid", "X1", "X2", "X4"};
//sfc_init();
//sfc_get_dev_id((uint8_t *)&data);
//iot_printf("\nsfc id = %d!", data);
iot_printf("\n------NDFC check start------!");
if(NULL == (p_buf_raw = (uint8_t *)os_mem_malloc(0, TEST_RW_DATA_LEN * 2)))
{
iot_printf("\nNo enough memory!");
return;
}
p_buf_rd = p_buf_raw;
p_buf_wt = p_buf_raw + TEST_RW_DATA_LEN;
/* Random data. */
data = 0;
p_data = (uint32_t *)p_buf_wt;
while(data < TEST_RW_DATA_LEN)
{
*p_data = data;
p_data++;
data += 4;
}
apb_enable(APB_GPIO);
apb_enable(APB_GMTX);
apb_enable(APB_PIN);
apb_enable(APB_CLK);
ahb_enable(AHB_NDFC_EB);
ahb_reset(AHB_NDFC_RST);
test_ndfc_unlink_sfc_sig();
//test_spi();
iot_ndfc_init(&g_test_ndfc_cfg);
iot_printf("\nFinish initializing controller,mode %s!",
p_mode[g_test_ndfc_cfg.data_mode]);
data = iot_ndfc_flash_get_id();
iot_printf("\nCheck flash id %s, id = 0x%04x!",
(data == 0xE521) ? "okay" : "failed", data);
iot_printf("\nPage size 0x%x, pages per block 0x%x, blocks per chip 0x%x!",
iot_ndfc_flash_get_page_size(),
iot_ndfc_flash_get_page_num(),
iot_ndfc_flash_get_block_num());
iot_ndfc_flash_get_param_page(p_buf_rd, TEST_RW_DUMP_SIZE);
iot_printf("\nDevice string in parameter Page : %2.32s", p_buf_rd + 32);
iot_printf("\nPage size in parameter Page : 0x%X",
(p_buf_rd[80] << 24) | (p_buf_rd[81] << 16) | (p_buf_rd[82] << 8) | p_buf_rd[83]);
iot_printf("\n>>>>01. start read / write check<<<<!");
iot_ndfc_flash_erase_block(TEST_RW_DATA_OFFSET);
iot_printf("\nFinish erasing block offset = 0x%x!", TEST_RW_DATA_OFFSET);
iot_printf("\nData array offset 0x%x,first %d bytes:", TEST_RW_DATA_OFFSET, TEST_RW_DUMP_SIZE);
iot_ndfc_flash_read(p_buf_rd, TEST_RW_DATA_OFFSET, TEST_RW_DATA_LEN);
test_data_dump(p_buf_rd, TEST_RW_DUMP_SIZE);
p_data = (uint32_t *)(((int)p_buf_rd) & 0xFFFFFFFC);
data = 0;
while(p_data < (uint32_t *)p_buf_wt)
{
if(*p_data != 0xFFFFFFFF)
{
data = 1;
break;
}
p_data++;
}
if(data)
{
iot_printf("\nErase blank check failed *0x%08x = 0x%08x!",
p_data, *p_data);
}
else
{
iot_printf("\nErase blank check passed!");
}
/* Random data write, page size 0x800, cross 3 pages. */
iot_ndfc_flash_write(p_buf_wt, TEST_RW_DATA_OFFSET, TEST_RW_DATA_LEN);
iot_printf("\nFinish writing buf = 0x%08x, offset = 0x%x, length = 0x%x!",
p_buf_wt, TEST_RW_DATA_OFFSET, TEST_RW_DATA_LEN);
iot_printf("\nWrite-buffer,first %d bytes:", TEST_RW_DUMP_SIZE);
test_data_dump(p_buf_wt, TEST_RW_DUMP_SIZE);
iot_ndfc_flash_read(p_buf_rd, TEST_RW_DATA_OFFSET, TEST_RW_DATA_LEN);
iot_printf("\nFinish reading buf = 0x%08x, offset = 0x%x, length = 0x%x!",
p_buf_rd, TEST_RW_DATA_OFFSET, TEST_RW_DATA_LEN);
iot_printf("\nData array offset 0x%x,first %d bytes:", TEST_RW_DATA_OFFSET, TEST_RW_DUMP_SIZE);
test_data_dump(p_buf_rd, TEST_RW_DUMP_SIZE);
data = 0;
p_data = (uint32_t*)(((int)p_buf_rd) & 0xFFFFFFFC);
p_data_2 = (uint32_t*)(((int)p_buf_wt) & 0xFFFFFFFC);
data = TEST_RW_DATA_LEN / 4;
while(data > 0)
{
if(*p_data != *p_data_2)
{
data = 1;
break;
}
p_data++;
p_data_2++;
data--;
}
if(data)
{
iot_printf("\nRead/write check failed, org *0x%08x = 0x%08x, rd *0x%08x = 0x%08x!",
p_data_2, *p_data_2, p_data, *p_data);
}
else
{
iot_printf("\nRead/write check passed!");
}
iot_printf("\n>>>>02. start block erase check<<<<!");
iot_ndfc_flash_erase_block(TEST_RW_DATA_OFFSET);
iot_printf("\nFinish erasing block offset = 0x%x!", TEST_RW_DATA_OFFSET);
iot_ndfc_flash_read(p_buf_rd, TEST_RW_DATA_OFFSET, TEST_RW_DATA_LEN);
iot_printf("\nFinish reading buf = 0x%08x, offset = 0x%x, length = 0x%x!",
p_buf_rd, TEST_RW_DATA_OFFSET, TEST_RW_DATA_LEN);
iot_printf("\nData array offset 0x%x,first %d bytes:", TEST_RW_DATA_OFFSET, TEST_RW_DUMP_SIZE);
test_data_dump(p_buf_rd, TEST_RW_DUMP_SIZE);
p_data = (uint32_t *)(((int)p_buf_rd) & 0xFFFFFFFC);
data = 0;
while(p_data < (uint32_t *)p_buf_wt)
{
if(*p_data != 0xFFFFFFFF)
{
data = 1;
break;
}
p_data++;
}
if(data)
{
iot_printf("\nErase blank check failed *0x%08x = 0x%08x!",
p_data, *p_data);
}
else
{
iot_printf("\nErase blank check passed!");
}
iot_printf("\n------NDFC check end------!");
return;
}
void test_task(void *arg)
{
iot_printf("test_task entry....\n");
/* init common modules */
iot_bitops_init();
/* init os related modules and utilities */
os_utils_init();
test_ndfc_func();
while(1) __asm volatile ("nop\n");
}
int32_t test_task_init()
{
test_init_handle = os_create_task(test_task, NULL, 9);
//create the tasks;
if(test_init_handle != NULL) {
iot_printf("test_task_init init successfully...\n");
}
return 0;
}
int32_t test_task_start()
{
os_start_kernel();
return 0;
}
int32_t iot_platform_init()
{
platform_init();
system_clock_init();
system_uart_init();
dbg_uart_init();
return 0;
}
int32_t iot_module_init(void)
{
//platform intialization;
iot_platform_init();
//create all the tasks;
test_task_init();
iot_printf("starting...\n");
return 0;
}
int main(void)
{
//module init;
iot_module_init();
//module start;
test_task_start();
return 0;
}