/* Copyright Statement:
 *
 * This software/firmware and related documentation ("AutoChips Software") are
 * protected under relevant copyright laws. The information contained herein is
 * confidential and proprietary to AutoChips Inc. and/or its licensors. Without
 * the prior written permission of AutoChips inc. and/or its licensors, any
 * reproduction, modification, use or disclosure of AutoChips Software, and
 * information contained herein, in whole or in part, shall be strictly
 * prohibited.
 *
 * AutoChips Inc. (C) 2024. All rights reserved.
 *
 * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
 * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("AUTOCHIPS SOFTWARE")
 * RECEIVED FROM AUTOCHIPS AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
 * ON AN "AS-IS" BASIS ONLY. AUTOCHIPS EXPRESSLY DISCLAIMS ANY AND ALL
 * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
 * NONINFRINGEMENT. NEITHER DOES AUTOCHIPS PROVIDE ANY WARRANTY WHATSOEVER WITH
 * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
 * INCORPORATED IN, OR SUPPLIED WITH THE AUTOCHIPS SOFTWARE, AND RECEIVER AGREES
 * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
 * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
 * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN AUTOCHIPS
 * SOFTWARE. AUTOCHIPS SHALL ALSO NOT BE RESPONSIBLE FOR ANY AUTOCHIPS SOFTWARE
 * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
 * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND AUTOCHIPS'S
 * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE AUTOCHIPS SOFTWARE
 * RELEASED HEREUNDER WILL BE, AT AUTOCHIPS'S OPTION, TO REVISE OR REPLACE THE
 * AUTOCHIPS SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
 * CHARGE PAID BY RECEIVER TO AUTOCHIPS FOR SUCH AUTOCHIPS SOFTWARE AT ISSUE.
 */
/******************************************************************************
 * @version: V3.1.0  AC7840x  Demo
 ******************************************************************************/

/*!
* @file crc.c
*
* @brief This file contains the user crc function..
*
*/

/* ===========================================  Includes  =========================================== */
#include "crc.h"
#include "string.h"

/* ============================================  Define  ============================================ */
#define CRC_DATA_SIZE   40

/* ===========================================  Typedef  ============================================ */

/* ==========================================  Variables  =========================================== */
uint32_t tmpResult = 0;                    /* CRC计算结果 */
uint32_t tmpCRCType = CRC_PROTOCOL_16BIT;  /* CRC 支持16位和32位长度多项式 */

const uint32_t CRC_data[] =
{
    0x9421FFE0, 0x7C0802A6, 0xBFC10018, 0x90010024, 0x3BE00000,
    0x93E1000C, 0x88030034, 0x2C000000, 0x4182004C, 0x80030000,
};


/*!
 * @brief CRC_InitTest
 *
 * @param[in] instance: none
 * @return none
 */
void CRC_InitTest(void)
{
    crc_user_config_t CRCConfig;               /* CRC配置结构体 */
    memset(&CRCConfig, 0, sizeof(CRCConfig));

    CRCConfig.crcProtocolType = (crc_protocol_type_t)tmpCRCType;

    if (tmpCRCType == CRC_PROTOCOL_32BIT)
    {
        CRCConfig.finalXOR = 0;                /* 对读到的数据不进行异或运算 */
        CRCConfig.poly = 0x4C11DB7;            /* CRC-32 Poly: 0x4C11DB7 */
        CRCConfig.readTransposeType = CRC_TRANSPOSE_BITS_BYTES;     /* 读转置配置 */
        CRCConfig.seed = 0xFFFFFFFF;           /* 种子32位为: 0xFFFFFFFF*/
        CRCConfig.writeBytesNumOnce = CRC_WRITE_4_BYTE_ONCE;  /* CRC一次写一个字节 */
        CRCConfig.writeTransposeType = CRC_TRANSPOSE_BITS_BYTES;    /* 写字节转置配置 */
    }
    else                                         /* CRC16 配置 */
    {
        CRCConfig.finalXOR = 0;                  /* 对读到的数据不进行异或运算 */
        CRCConfig.poly = 0x1021;
        CRCConfig.readTransposeType = CRC_TRANSPOSE_NONE;  /* 读转置配置 */
        CRCConfig.seed = 0xFFFF;                      /* 种子16位为: 0xFFFF*/
        CRCConfig.writeBytesNumOnce = CRC_WRITE_1_BYTE_ONCE;
        CRCConfig.writeTransposeType = CRC_TRANSPOSE_NONE;
    }

    CRC_DRV_Init(0, &CRCConfig);    /* 初始化CRC */

    CRC_DRV_WriteData(0, (const uint8_t *)CRC_data, CRC_DATA_SIZE);

    tmpResult = CRC_DRV_GetCrcResult(0);

    printf("CRC result is: 0x%X\r\n", tmpResult);
}

/* =============================================  EOF  ============================================== */

