/*!
 * @file      apm32f10x_crc.c
 *
 * @brief     This file provides all the CRC firmware functions
 *
 * @version   V1.0.0
 *
 * @date      2019-8-6
 *
 */

#include "apm32f10x_crc.h"

/*!
 * @brief     Reset CRC data register (DATA).
 *
 * @param     None
 *
 * @retval    None
 *
 * @note
 */
void CRC_ResetDATA(void)
{
    CRC->CTRL_B.RST = BIT_SET;
}

/*!
 * @brief     Calculate a 32-bit CRC for a given data word (32 bits).
 *
 * @param     data: data word(32-bit) to compute its CRC
 *
 * @retval    32-bit CRC
 *
 * @note
 */
uint32_t CRC_CalcCRC(uint32_t data)
{
    CRC->DATA = data;

    return (CRC->DATA);
}

/*!
 * @brief     Computes the 32-bit CRC of a given buffer of data word(32-bit).
 *
 * @param     pBuffer: Pointer to the buffer containing the data to be computed
 * 
 * @param     bufferLength: buffer length
 *
 * @retval    32-bit CRC
 *
 * @note
 */
uint32_t CRC_CalcBlockCRC(uint32_t pBuffer[], uint32_t bufferLength)
{
    uint32_t index = 0;

    for(index = 0; index < bufferLength; index++)
    {
        CRC->DATA = pBuffer[index];
    }
    return (CRC->DATA);
}

/*!
 * @brief     Returns the current CRC value.
 *
 * @param     None
 *
 * @retval    32-bit CRC
 *
 * @note
 */
uint32_t CRC_ReadCRC(void)
{
    return (CRC->DATA);
}

/*!
 * @brief     Stores a 8-bit data in the Independent Data(ID) register.
 *
 * @param     IDValue: 8-bit value to be stored in the ID register
 *
 * @retval    None
 *
 * @note
 */
void CRC_WriteIDRegister(uint8_t IDValue)
{
    CRC->DB = IDValue;
}

/*!
 * @brief      Returns a 8-bit data stored in the Independent Data(ID) register
 *
 * @param      None
 *
 * @retval     8-bit value of the ID register
 *
 * @note
 */
uint8_t CRC_ReadIDRegister(void)
{
    return (CRC->DB);
}
