/*******************************************************************************
* Copyright (C) 2020-2023, tau Systems (R),All Rights Reserved.
*
* File:         demo_hal_spi.c
* Description： spi demo code
* Version：      V0.1
* Date：         2023-07-27
* Author：       swx
*******************************************************************************/

/*******************************************************************************
* 1.Included files
*******************************************************************************/
#include "hal_spi_master.h"
#include "hal_spi_slave.h"
#include "demo_hal_spi.h"
#include "hal_gpio.h"
#include "tau_log.h"
#include "tau_delay.h"

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

#define BUFFER_SIZE             255

#define CHECK_TRANS_RESULT(src, dst, size)                 \
    for(uint32_t check_num = 0; check_num < size; check_num ++) \
    {\
        if(src[check_num] != dst[check_num])\
        {\
            TAU_LOGD("src[%d] = %x, dst[%d] = %x , error!!\n",check_num,src[check_num],check_num,dst[check_num]);\
            return false;\
        }\
    }\

/*******************************************************************************
* 3.Global function and Global variable declarations
*******************************************************************************/
bool spis_hw_miss_flag = false;

/*SPIS硬件回复功能32组回复数据*/
uint8_t spis_hw_header_data0[] = {0xf0, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d};
uint8_t spis_hw_header_data1[] = {0xf1, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19};
uint8_t spis_hw_header_data2[] = {0xf2, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29};
uint8_t spis_hw_header_data3[] = {0xf3, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39};
uint8_t spis_hw_header_data4[] = {0xf4, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49};
uint8_t spis_hw_header_data5[] = {0xf5, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59};
uint8_t spis_hw_header_data6[] = {0xf6, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69};
uint8_t spis_hw_header_data7[] = {0xf7, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79};
uint8_t spis_hw_header_data8[] = {0xf8, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89};
uint8_t spis_hw_header_data9[] = {0xf9, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99};
uint8_t spis_hw_header_data10[] = {0xfa, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8, 0xA9};
uint8_t spis_hw_header_data11[] = {0xfb, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9};
uint8_t spis_hw_header_data12[] = {0xfc, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9};
uint8_t spis_hw_header_data13[] = {0xfd, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9};
uint8_t spis_hw_header_data14[] = {0xfe, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9};
uint8_t spis_hw_header_data15[] = {0xff, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9};
uint8_t spis_hw_header_data16[] = {0xf0, 0x21, 0x31, 0x41, 0x51, 0x61, 0x71, 0x81, 0x91};
uint8_t spis_hw_header_data17[] = {0xf1, 0x22, 0x31, 0x42, 0x52, 0x62, 0x72, 0x82, 0x92};
uint8_t spis_hw_header_data18[] = {0xf2, 0x23, 0x33, 0x43, 0x53, 0x63, 0x73, 0x83, 0x93};
uint8_t spis_hw_header_data19[] = {0xf3, 0x24, 0x34, 0x44, 0x54, 0x64, 0x74, 0x84, 0x94};
uint8_t spis_hw_header_data20[] = {0xf4, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09};
uint8_t spis_hw_header_data21[] = {0xf5, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19};
uint8_t spis_hw_header_data22[] = {0xf6, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29};
uint8_t spis_hw_header_data23[] = {0xf7, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39};
uint8_t spis_hw_header_data24[] = {0xf8, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49};
uint8_t spis_hw_header_data25[] = {0xf9, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59};
uint8_t spis_hw_header_data26[] = {0xfa, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69};
uint8_t spis_hw_header_data27[] = {0xfb, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79};
uint8_t spis_hw_header_data28[] = {0xfc, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89};
uint8_t spis_hw_header_data29[] = {0xfd, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99};
uint8_t spis_hw_header_data30[] = {0xfe, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89};
uint8_t spis_hw_header_data31[] = {0xff, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xf0, 0x13, 0xff};

static hal_spis_hw_ack_info_t spis_hw_test_header[32] =
{
    /*序号_匹配值_匹配长度_使能_回复延时_回复数据的地址_回复数据长度            */
    {0, {0x01, 0x02, 0x03, 0x04}, 0, 1, 0, spis_hw_header_data0, sizeof(spis_hw_header_data0) / sizeof(uint8_t) - 1},
    {1, {0x05, 0x06, 0x07, 0x08}, 1, 1, 1, spis_hw_header_data1, sizeof(spis_hw_header_data1) / sizeof(uint8_t) - 1},
    {2, {0x09, 0x0a, 0x0b, 0x0c}, 2, 1, 2, spis_hw_header_data2, sizeof(spis_hw_header_data2) / sizeof(uint8_t) - 1},
    {3, {0x0d, 0x0e, 0x0f, 0x10}, 3, 1, 3, spis_hw_header_data3, sizeof(spis_hw_header_data3) / sizeof(uint8_t) - 1},
    {4, {0x11, 0x12, 0x13, 0x14}, 0, 1, 4, spis_hw_header_data4, sizeof(spis_hw_header_data4) / sizeof(uint8_t) - 1},
    {5, {0x15, 0x16, 0x17, 0x18}, 1, 1, 5, spis_hw_header_data5, sizeof(spis_hw_header_data5) / sizeof(uint8_t) - 1},
    {6, {0x19, 0x1a, 0x1b, 0x1c}, 2, 1, 6, spis_hw_header_data6, sizeof(spis_hw_header_data6) / sizeof(uint8_t) - 1},
    {7, {0x1d, 0x1e, 0x1f, 0x20}, 3, 1, 7, spis_hw_header_data7, sizeof(spis_hw_header_data7) / sizeof(uint8_t) - 1},
    {8, {0x21, 0x22, 0x23, 0x24}, 0, 1, 8, spis_hw_header_data8, sizeof(spis_hw_header_data8) / sizeof(uint8_t) - 1},
    {9, {0x25, 0x26, 0x27, 0x28}, 1, 1, 9, spis_hw_header_data9, sizeof(spis_hw_header_data9) / sizeof(uint8_t) - 1},
    {10, {0x29, 0x2a, 0x2b, 0x2c}, 2, 1, 10, spis_hw_header_data10, sizeof(spis_hw_header_data10) / sizeof(uint8_t) - 1},
    {11, {0x2d, 0x2e, 0x2f, 0x30}, 3, 1, 0, spis_hw_header_data11, sizeof(spis_hw_header_data11) / sizeof(uint8_t) - 1},
    {12, {0x31, 0x32, 0x33, 0x34}, 0, 1, 1, spis_hw_header_data12, sizeof(spis_hw_header_data12) / sizeof(uint8_t) - 1},
    {13, {0x35, 0x36, 0x37, 0x38}, 1, 1, 2, spis_hw_header_data13, sizeof(spis_hw_header_data13) / sizeof(uint8_t) - 1},
    {14, {0x39, 0x3a, 0x3b, 0x3c}, 2, 1, 3, spis_hw_header_data14, sizeof(spis_hw_header_data14) / sizeof(uint8_t) - 1},
    {15, {0x3d, 0x3e, 0x3f, 0x40}, 3, 1, 4, spis_hw_header_data15, sizeof(spis_hw_header_data15) / sizeof(uint8_t) - 1},
    {16, {0x41, 0x42, 0x43, 0x44}, 0, 1, 5, spis_hw_header_data16, sizeof(spis_hw_header_data16) / sizeof(uint8_t) - 1},
    {17, {0x45, 0x46, 0x47, 0x48}, 1, 1, 6, spis_hw_header_data17, sizeof(spis_hw_header_data17) / sizeof(uint8_t) - 1},
    {18, {0x49, 0x4a, 0x4b, 0x4c}, 2, 1, 7, spis_hw_header_data18, sizeof(spis_hw_header_data18) / sizeof(uint8_t) - 1},
    {19, {0x4d, 0x4e, 0x4f, 0x50}, 3, 1, 8, spis_hw_header_data19, sizeof(spis_hw_header_data19) / sizeof(uint8_t) - 1},
    {20, {0x51, 0x52, 0x53, 0x54}, 0, 1, 9, spis_hw_header_data20, sizeof(spis_hw_header_data20) / sizeof(uint8_t) - 1},
    {21, {0x55, 0x56, 0x57, 0x58}, 1, 1, 10, spis_hw_header_data21, sizeof(spis_hw_header_data21) / sizeof(uint8_t) - 1},
    {22, {0x59, 0x5a, 0x5b, 0x5c}, 2, 1, 11, spis_hw_header_data22, sizeof(spis_hw_header_data22) / sizeof(uint8_t) - 1},
    {23, {0x5d, 0x5e, 0x5f, 0x60}, 3, 1, 12, spis_hw_header_data23, sizeof(spis_hw_header_data23) / sizeof(uint8_t) - 1},
    {24, {0x61, 0x62, 0x63, 0x64}, 0, 1, 13, spis_hw_header_data24, sizeof(spis_hw_header_data24) / sizeof(uint8_t) - 1},
    {25, {0x65, 0x66, 0x67, 0x68}, 1, 1, 14, spis_hw_header_data25, sizeof(spis_hw_header_data25) / sizeof(uint8_t) - 1},
    {26, {0x69, 0x6a, 0x6b, 0x6c}, 2, 1, 15, spis_hw_header_data26, sizeof(spis_hw_header_data26) / sizeof(uint8_t) - 1},
    {27, {0x6d, 0x6e, 0x6f, 0x70}, 3, 1, 11, spis_hw_header_data27, sizeof(spis_hw_header_data27) / sizeof(uint8_t) - 1},
    {28, {0x71, 0x72, 0x73, 0x74}, 0, 1, 12, spis_hw_header_data28, sizeof(spis_hw_header_data28) / sizeof(uint8_t) - 1},
    {29, {0x75, 0x76, 0x77, 0x78}, 1, 1, 13, spis_hw_header_data29, sizeof(spis_hw_header_data29) / sizeof(uint8_t) - 1},
    {30, {0x79, 0x7a, 0x7b, 0x7c}, 2, 1, 12, spis_hw_header_data30, sizeof(spis_hw_header_data30) / sizeof(uint8_t) - 1},
    {31, {0x7d, 0x7e, 0x7f, 0x80}, 3, 1, 14, spis_hw_header_data31, sizeof(spis_hw_header_data31) / sizeof(uint8_t) - 1},
};

static uint8_t spim_write_buffer[BUFFER_SIZE] = {0};
static uint8_t spim_read_buffer[BUFFER_SIZE] = {0};
static uint8_t spis_read_buffer[BUFFER_SIZE] = {0};
static uint8_t spis_write_buffer[BUFFER_SIZE] = {0};

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

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

/**
* @brief  spi master IO初始化
* @param  none
* @retval
*/
static void spim_io_init(void)
{
    io_pad_attr_t attrs[] =
    {
        {IO_PAD_TD_SPIM_CLK, PIN5_MODE_SPIM_SCLK, IO_IOE_NONE, IO_LVL_HIGH},
        {IO_PAD_TD_SPIM_CSN, PIN6_MODE_SPIM_CSN, IO_IOE_NONE, IO_LVL_HIGH},
        {IO_PAD_TD_SPIM_MISO, PIN7_MODE_SPIM_MISO, IO_IOE_NONE, IO_LVL_HIGH},
        {IO_PAD_TD_SPIM_MOSI, PIN4_MODE_SPIM_MOSI, IO_IOE_NONE, IO_LVL_HIGH}
    };
    uint8_t size = sizeof(attrs) / sizeof(io_pad_attr_t);
    hal_gpio_config_pad(attrs, size);
}

/**
* @brief  spi slave IO初始化
* @param  none
* @retval
*/
static void spis_io_init(void)
{
    io_pad_attr_t attrs[] =
    {
        {IO_PAD_AP_SPIS_CLK, PIN32_MODE_SPIS_SCLK, IO_IOE_NONE, IO_LVL_HIGH},
        {IO_PAD_AP_SPIS_CSN, PIN31_MODE_SPIS_CSN, IO_IOE_NONE, IO_LVL_HIGH},
        {IO_PAD_AP_SPIS_MISO, PIN30_MODE_SPIS_MISO, IO_IOE_NONE, IO_LVL_HIGH},
        {IO_PAD_AP_SPIS_MOSI, PIN33_MODE_SPIS_MOSI, IO_IOE_NONE, IO_LVL_HIGH}
    };
    uint8_t size = sizeof(attrs) / sizeof(io_pad_attr_t);
    hal_gpio_config_pad(attrs, size);
}

/**
* @brief  spi slave中断回调函数
* @param  event：SPIS收到的中断事件
* @param  packet_info： SPIS收到的数据packet
* @retval
*/
void spis_callback(hal_spis_event_e event, hal_spis_packet_info_t *packet_info)
{
    /* 收到CS_RISE中断 */
    if (event == SPIS_EVENT_RCV_CS_RISE)
    {
        /* 更新SPIS的txbuffer和rxbuffer */
        hal_spis_update_rx_buffer(spis_read_buffer, BUFFER_SIZE);
        hal_spis_update_tx_buffer(spis_write_buffer, BUFFER_SIZE, true);
    }
    /* 收到CS_FALL中断 */
    else if (event == SPIS_EVENT_RCV_CS_FALL)
    {
        //TAU_LOGD("spis_test SPIS_EVENT_RCV_CS_FALL\n");
    }
    /* 收到数据接收指定阈值中断 */
    else if (event == SPIS_EVENT_RCV_CNT)
    {
        //TAU_LOGD("spis_test SPIS_EVENT_RCV_CNT\n");
    }
    /* 收到数据接收完全中断 */
    else if (event == SPIS_EVENT_RCV_FULL)
    {
        //TAU_LOGD("spis_test SPIS_EVENT_RCV_CNT\n");
    }
}

/**
* @brief  spi slave硬件快速回复功能中断回调函数
* @param  event：SPIS收到的中断事件
* @param  packet_info： SPIS收到的数据packet
* @retval
*/
void spis_hw_callback(hal_spis_event_e event, hal_spis_packet_info_t *packet_info)
{
    /* 收到CS_RISE中断 */
    if (event == SPIS_EVENT_RCV_CS_RISE)
    {
        /* 产生了ALL_MISS中断的标记 */
        if (spis_hw_miss_flag)
        {
            /* 使能SPIS硬件快速回复功能，默认回复值为0xE0 */
            hal_spis_set_hw_ack_enable(ENABLE, 0xE0);
            spis_hw_miss_flag = false;
        }
        /* SPIS更新读写buffer */
        hal_spis_update_tx_buffer(spis_hw_header_data31, sizeof(spis_hw_header_data31) / sizeof(uint8_t), true);
        hal_spis_update_rx_buffer(spis_read_buffer, BUFFER_SIZE);
    }

    /* 收到ALL_MISS中断 */
    if (event == SPIS_EVENT_ALL_MISS)
    {
        /* 关闭SPIS硬件快速回复功能，默认回复值为0xF0 */
        hal_spis_set_hw_ack_enable(DISABLE, 0xf0);
        spis_hw_miss_flag = true;
        TAU_LOGE("All miss intr test\n");
    }
}

/**
* @brief  获取随机值函数
* @param  range_begin: 随机值范围起始
* @param  renge_end:   随机值范围结束
* @retval uint32_t :   范围内随机值
*/
uint32_t unit_case_get_rand(uint32_t range_begin, uint32_t renge_end)
{
    uint32_t range = 1;
    if (renge_end > range_begin)
    {
        range = renge_end - range_begin + 1;
    }
    else
    {
        TAU_LOGE("rand range[%d ~ %d] error! default set to 1\n");
    }
    return (uint32_t)((rand() % range) + range_begin);
}

/**
* @brief  spi master和slave的发送/接收buffer初始化
* @param  none
* @retval
*/
static bool spi_case_rand_buffer(bool spim_en, bool spis_en)
{
    TAU_LOGD("spi_case_rand_buffer\n");
    /*  read buffer默认初始化为0, tx buffer 初始为随机数 */
    uint8_t i = 0 ;
    uint32_t count = BUFFER_SIZE;
    for (i = 0; i < count; i ++)
    {
        spis_read_buffer[i] = 0;
        spim_read_buffer[i] = 0;
        if (spim_en)
        {
            spim_write_buffer[i] = unit_case_get_rand(0, 255);
        }
        if (spis_en)
        {
            spis_write_buffer[i] = unit_case_get_rand(0, 255);
        }
    }
    return true;
}


/**
* @brief  芯片SPIM与SPIS对接,验证基础传输数据的正确性
* @param
* @retval bool true/false
*/
static bool hal_spi_base_case(void)
{
    uint32_t speed = 400000;
    uint8_t cpha = 0;
    uint8_t cpol = 0;

    /* 随机buffer值 */
    spi_case_rand_buffer(true, true);

    /* SPIS IO初始化 */
    spis_io_init();
    /* SPIS初始化，相位0+极性0 */
    hal_spis_init(cpha, cpol);
    /* SPIS设置初始读写buffer */
    hal_spis_update_rx_buffer(spis_read_buffer, BUFFER_SIZE);
    hal_spis_update_tx_buffer(spis_write_buffer, BUFFER_SIZE, true);
    /* SPIS注册回调函函数，这里开启了CS_RISE、CS_FALL、RX_CNT、RX_FULL中断，RX_CNT阈值设置为100 */
    hal_spis_register_callback(spis_callback, SPIS_EVENT_RCV_CS_RISE | SPIS_EVENT_RCV_CS_FALL | SPIS_EVENT_RCV_CNT | SPIS_EVENT_RCV_FULL, 100);
    /* SPIS启动 */
    hal_spis_start();

    /* SPIM IOc初始化 */
    spim_io_init();
    /* SPIM初始化，400k传输速率+相位0+极性0 */
    hal_spim_init(speed, cpha, cpol);

    TAU_LOGD("base trans para cpha[%d] cpol[%d] speed[%d] \n", cpha, cpol, speed);

    /* 测试 SPIM CPU读写 */
    hal_spim_read(spim_write_buffer, BUFFER_SIZE, spim_read_buffer, BUFFER_SIZE);
    /* SPIM 写完，检查 SPIS RX buffer 是否等于 SPIM TX buffer*/
    CHECK_TRANS_RESULT(spim_write_buffer, spis_read_buffer, BUFFER_SIZE);
    /* SPIM 读完，检查 SPIS TX buffer 是否等于 SPIM RX buffer*/
    CHECK_TRANS_RESULT(spis_write_buffer, spim_read_buffer, BUFFER_SIZE);

    /* 随机buffer值 */
    spi_case_rand_buffer(true, false);

    /* 测试 SPIM CPU写 */
    hal_spim_write(spim_write_buffer, BUFFER_SIZE);
    /* SPIM 写完，检查 SPIS RX buffer 是否等于 SPIM TX buffer*/
    CHECK_TRANS_RESULT(spim_write_buffer, spis_read_buffer, BUFFER_SIZE);

    /* 传输完成,关闭SPI */
    hal_spis_stop();
    hal_spis_deinit();
    hal_spim_deinit();

    TAU_LOGD("hal_spi_base_case done!\n");
    return true;
}


/**
* @brief  芯片SPIM与SPIS对接,       验证SPIS 硬件快速回复传输功能
* @param
* @retval bool true/false
*/
static bool hal_spis_hw_case(void)
{
    uint32_t speed = 400000;
    uint8_t cpha = 0;
    uint8_t cpol = 0;
    uint8_t write_buffer[30] = {0x9e, 0xa0, 0x00, 0x01, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19,
                                0x8a, 0x1b, 0x1c, 0x5d, 0x7e, 0x4f, 0x20, 0x21, 0x22, 0x23,
                                0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30, 0x31, 0x32, 0x33
                               };
    /* SPIS IO初始化 */
    spis_io_init();
    /* SPIS初始化，相位0+极性0 */
    hal_spis_init(cpha, cpol);

    /* SPIS设置初始读buffer */
    hal_spis_update_rx_buffer(spis_read_buffer, BUFFER_SIZE);
    /* SPIS设置初始写buffer */
    hal_spis_update_tx_buffer(spis_hw_header_data31, sizeof(spis_hw_header_data31) / sizeof(uint8_t), true);
    /* SPIS注册回调函函数，这里开启了CS_RISE和ALL_MISS中断 */
    hal_spis_register_callback(spis_hw_callback, SPIS_EVENT_RCV_CS_RISE | SPIS_EVENT_ALL_MISS, 0);

    /* 设置SPIS硬件快速回复参数 */
    hal_spis_set_hw_ack_info(spis_hw_test_header, (sizeof(spis_hw_test_header) / sizeof(hal_spis_hw_ack_info_t)));
    /* 使能SPIS硬件快速回复功能，默认回复值为0xE0 */
    hal_spis_set_hw_ack_enable(ENABLE, 0xE0);
    /* SPIS启动 */
    hal_spis_start();

    /* SPIM IO初始化 */
    spim_io_init();
    /* SPIM初始化，400k传输速率+相位0+极性0 */
    hal_spim_init(speed, cpha, cpol);

    /* 32组硬件快速回复功能参数demo */
    for (int k = 0; k < 32; k++)
    {
        /* write_buffer前4个字节设置为每组参数的匹配值 */
        for (int j = 0; j < 4; j++)
        {
            write_buffer[j] = spis_hw_test_header[k].cmp_data[j];
        }

        /* 将第11组和第14组的write_buffer[0]自加，这样匹配不到后就会产生ALL_MISS中断 */
        if ((k == 10) || (k == 13))
        {
            write_buffer[0]++;
        }

        /* FLUSH SPIM FIFO */
        hal_spim_flush();
        /* SPIM发送30个write_buffer数据给SPIS，并读取SPIS的30个数据到spim_read_buffer */
        hal_spim_read(write_buffer, 30, spim_read_buffer, 30);

        /* 等待SPIM传输完成 */
        while (!hal_spim_get_transfer_complete());

        printf("SPIM_TX:");
        for (int i = 0; i < 30; i++)
        {
            printf("%02x ", spis_read_buffer[i]);
        }
        printf("\n");
        printf("SPIM_RX:");
        for (int i = 0; i < 30; i++)
        {
            printf("%02x ", spim_read_buffer[i]);
        }
        printf("\n");

        /* 以下是校验SPIS的硬件快速回复数据是否正确 */
        int index = spis_hw_test_header[k].delay_clk + 1 + spis_hw_test_header[k].cmp_len + 1;
        printf("CMP_LEN:%d, DELAY_CLK:%d\n", spis_hw_test_header[k].cmp_len, spis_hw_test_header[k].delay_clk);
        bool flag = true;
        for (int i = 0; i < spis_hw_test_header[k].ack_length; i++)
        {
            if (spis_hw_test_header[k].ack_address[i] == spim_read_buffer[index + i])
            {
                if (flag)
                {
                    printf("Group[%d] OK\n", k);
                    flag = false;
                }

                continue;
            }
            else
            {
                if ((k == 10) || (k == 13))
                {
                    if (flag)
                    {
                        printf("Group[%d] All_miss init test\n", k);
                        flag = false;
                    }
                    continue;
                }
                else
                {
                    printf("Header_addr[%d]:%02x Header_addr[%d]:%02x ", i, spis_hw_test_header[k].ack_address[i], index + i, spim_read_buffer[index + i]);
                    printf("Group[%d] error\n", k);
                    return false;
                }
            }
        }
        printf("\n");
    }

    /* 传输完成,关闭SPI */
    hal_spis_stop();
    hal_spis_deinit();
    hal_spim_deinit();
    TAU_LOGD("hal_spis_hw_case done!\n");
    return true;
}

/**
* @brief  spi demo case
* @param  none
* @retval none
*/
void demo_hal_spi(void)
{
    /* 基础传输case */
    if (!hal_spi_base_case())
    {
        hal_spis_stop();
        hal_spis_deinit();
        hal_spim_deinit();
        TAU_LOGD("hal_spi_base_case fail!\n");
    }

    /* SPIS硬件快速回复功能case */
    if (!hal_spis_hw_case())
    {
        hal_spis_stop();
        hal_spis_deinit();
        hal_spim_deinit();
        TAU_LOGD("hal_spis_hw_case fail!\n");
    }

    TAU_LOGD("spi case done\n");
}
#endif
