/*******************************************************************************
*
*
* File:          demo_hal_crc.c
* Description：  crc demo源文件
* Version：      V0.1
* Date:          2023-07-27
* Author：       zzf
 *******************************************************************************/

/*******************************************************************************
* 1.Included files
*******************************************************************************/
#include "demo_hal_crc.h"

/*******************************************************************************
* 2.Global constant and macro definitions using #define
*******************************************************************************/
#ifdef LOG_TAG
#undef LOG_TAG
#endif
#define LOG_TAG "crc-log"

/*  CRC计算方式，0-CPU方式； 1-DMA方式              */
#define   CRC_DMA_CAL_EN                        (1)

/*  for max total of test elements                */
#define   CRC_ELEMENT_MAX_TATOL                 (32u)
/*  for crc initial POL regs value                */
#define   CRC_32_POL_VALUE0                     (0x04C11DB7u)
#define   CRC_16_POL_VALUE0                     (0x8005u)
/*  for crc calculate in crc software calculate   */
#define   CRC_SEED_VALUE_0                      (0u)
#define   CRC_SEED_VALUE_F                      (0xFFFFu)

/*******************************************************************************
* 3.Global function and Global variable declarations
*******************************************************************************/
#if CRC_DMA_CAL_EN
/* all the one element value crc calculate for software crc      */
static uint32_t sg_crc32_arry[CRC_ELEMENT_MAX_TATOL] =
{
    0x00000000, 0xFFFFFFFF, 0xAAAAAAAA, 0xEEEEEEEE, 0x55555555, 0x66666666, 0x11111111, 0x77777777,
    0x12345678, 0x456789AB, 0x89ABCDEF, 0x3456789A, 0x6789ABCD, 0x9ABCDEF0, 0xBCDEF012, 0xFEDCAB98,
    0x5a5aa5a5, 0x37377373, 0x98988989, 0x76767676, 0x41233214, 0x67433467, 0x91DF91DF, 0x76347634,
    0x46378912, 0x57351059, 0xABFC9483, 0x837204AF, 0x41057DBA, 0x893CD024, 0x56378105, 0xFA34610B,
};
#else
/* all the one element value crc calculate for software crc      */
static uint16_t sg_crc16_element_arry[CRC_ELEMENT_MAX_TATOL] =
{
    0x0000, 0xFFFF, 0xAAAA, 0x2222, 0x3333, 0xDDDD, 0x9999, 0x8888,
    0x1234, 0x5678, 0x9ABC, 0x2345, 0x4567, 0xABCD, 0xCDEF, 0x789A,
    0x5a5a, 0x2323, 0x6868, 0x5aa5, 0x7887, 0x8558, 0x6336, 0xAFAF,
    0x3194, 0x7853, 0x7733, 0x76DE, 0x89CA, 0x7401, 0x7392, 0xD2A8,
};
#endif
/*******************************************************************************
* 4.Global function prototypes
*******************************************************************************/
#if CRC_DMA_CAL_EN
/**
* @brief  DMA计算CRC值回调函数
* @param  calculate_result: CRC计算结果
* @retval None
*/
void demo_crc_dma_callback(uint32_t calculate_result)
{
    TAU_LOGD("dma demo[0x%x]\n", calculate_result);
}

/**
* @brief  DMA计算CRC值
* @param  None
* @retval true or false
*/
bool demo_crc_array_dma_cal(void)
{
    crc_ctrl_handle_t crc_cfg_para =
    {
        CRC_32_POL_VALUE0,
        CRC_SEED_VALUE_0,
        CRC_32_BIT_PROTOCOL,
        CRC_FXOR_ENABLE,
        CRC_REV_ONLY_BITS_TRANSPOSE,
        CRC_REV_ONLY_BITS_TRANSPOSE
    };

    hal_crc_dma_init(&crc_cfg_para, demo_crc_dma_callback, sg_crc32_arry, CRC_ELEMENT_MAX_TATOL);

    hal_crc_dma_start();

    return true;
}
#else
/**
* @brief  CPU阻塞式计算CRC值
* @param  None
* @retval true or false
*/
bool demo_crc_array_cal(void)
{
    crc_ctrl_handle_t crc_cfg_para =
    {
        CRC_16_POL_VALUE0,
        CRC_SEED_VALUE_F,
        CRC_16_BIT_PROTOCOL,
        CRC_FXOR_ENABLE,
        CRC_REV_BOTH_TRANSPOSE,
        CRC_REV_BOTH_TRANSPOSE
    };

    hal_crc_init(&crc_cfg_para);
    uint32_t output_crc = hal_crc_cal(sg_crc16_element_arry, CRC_ELEMENT_MAX_TATOL);
    TAU_LOGD("cpu demo[0x%x]\n", output_crc);

    return true;
}
#endif

/**
* @brief  demo测试接口函数
* @param  None
* @retval None
*/
void demo_hal_crc_case(void)
{
    TAU_LOGD("HAL CRC DEMO.\n");

#if CRC_DMA_CAL_EN
    demo_crc_array_dma_cal();   // DMA方式计算
#else
    demo_crc_array_cal();       // CPU方式计算
#endif

}

