/**
  ******************************************************************************
  * @file     CRC_user.c
  * @author   SINOMCU-AE
  * @brief    CRC user test function
  *    
  *          This file provides functions CRC test:
  *     uint32_t CRC_calculate_byte(uint8_t * buffer,uint32_t len);
  *     ErrorStatus CRC_calculate_test(void);
  *     
  ******************************************************************************
  * @attention 
  *                           DISCLAIMER
  * The present firmware which is for guidance only aims at providing customers
  * with coding information regarding their products in order for them to save
  * time. As a result, SinoMCU shall not be held liable for any direct,indirect
  * or consequential damages with respect to any claims arising from the content 
  * of such firmware and/or the use made by customers of the coding information 
  * contained herein in connection with their products.
  *
  * <h2><center>&copy; Copyright (c) 2021 sinomcu All rights reserved.</center></h2>
  *
  ******************************************************************************
  */
  
#include "stdio.h"
#include "CRC_CFG.h"
#include "CRC_user.h"

const uint8_t crc_test_buff[CRC_TEST_BUFFER_SIZE] ={
  0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xAA
};

uint32_t CRC_result_check = 0xF9989F60;          // http://www.ip33.com/crc.html ,for check

/**
  * @brief  CRC calculation by byte input
  * @param  byte buffer pointer,buff length
  * @retval crc result,32bit
  */
uint32_t CRC_calculate_byte(uint8_t * buffer,uint32_t len) {
  uint32_t count = 0;

  for (count = 0; count < len; count++) {
    MS32_CRC_FeedData8(CRC, buffer[count]);
  }

  return( ~ MS32_CRC_ReadData32(CRC) );          // ~ for( result XOR FFFFFFFF )
}

/**
  * @brief  CRC test
  * @param  none
  * @retval test result:ERROR,SUCCESS
  */
ErrorStatus CRC_calculate_test(void) {
  ErrorStatus test_result = ERROR;
  uint32_t CRC_cal_result = 0;

  CRC_cal_result =CRC_calculate_byte((uint8_t *)crc_test_buff,CRC_TEST_BUFFER_SIZE);
  
  printf("\r\n--Inf:CRC calculate check value:%X",CRC_result_check);    
  printf("\r\n--Inf:CRC calculate result is  :%X",CRC_cal_result);  
  
  if( CRC_cal_result == CRC_result_check) {      // "calculate result" equal to "check value" or not
    test_result = SUCCESS;
    printf("\r\n--Inf:CRC test successful.");    
  }
  else {
    test_result = ERROR;
    printf("\r\n**Error:CRC test fail!");  
  }

  return test_result;
}

/******************************** END OF FILE *********************************/
