/*!
 * @file        main.c
 *
 * @brief       Main program body
 *
 * @version     V1.0.0
 *
 * @date        2023-03-30
 *
 * @attention
 *
 *  Copyright (C) 2023 Geehy Semiconductor
 *
 *  You may not use this file except in compliance with the
 *  GEEHY COPYRIGHT NOTICE (GEEHY SOFTWARE PACKAGE LICENSE).
 *
 *  The program is only for reference, which is distributed in the hope
 *  that it will be useful and instructional for customers to develop
 *  their software. Unless required by applicable law or agreed to in
 *  writing, the program is distributed on an "AS IS" BASIS, WITHOUT
 *  ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the GEEHY SOFTWARE PACKAGE LICENSE for the governing permissions
 *  and limitations under the License.
 */

/* Includes */
#include "main.h"
#include "Board.h"
#include "stdio.h"
#include "apm32a0xx_crc.h"

/** @addtogroup Examples
  @{
  */

/** @addtogroup CRC_8BitsCRCMessage
  @{
  */
  
/** @defgroup CRC_8BitsCRCMessage_Macros Macros
  @{
*/

/* printf function configs to USART1 */
#define DEBUG_USART  USART1

/* set data buffer size */
#define COUNT(a)        ((uint8_t)(sizeof(a) / sizeof(*(a))))
#define BUFFER_SIZE     (COUNT(DataBuffer) - 1)

/**@} end of group CRC_8BitsCRCMessage_Macros*/

/** @defgroup CRC_8BitsCRCMessage_Variables Variables
  @{
  */

uint8_t DataBuffer[] =
{
    0x4a, 0x6a, 0x0a, 0x2a, 0xfb, 0x9b,
    0xbb, 0x6c, 0x5c, 0x3c, 0x1c, 0xfd,
    0xad, 0x8d, 0x7e, 0x5e, 0x2e, 0x0e,
    0xef, 0xcf, 0x9f, 0x91, 0xb1, 0xd1,
    0xe1, 0x00, 0x20, 0x40, 0x83, 0xa3,
    0xc3, 0xe3, 0x12, 0x32, 0x52, 0x72,
};

/* CRC value*/
uint8_t CRCValue = 0;
/* CRC Experct value*/
uint8_t CRCExpectedValue = 0xff;

/**@} end of group CRC_8BitsCRCMessage_Variables*/




/** @defgroup CRC_8BitsCRCMessage_Functions Functions
  @{
  */

/* Delay */
void Delay(void);

/*CRC Config */
static void CRC_Config(uint8_t Poly);

/* Get CRC value */
static uint8_t CRC_Compute8Bits(uint8_t* Data,uint32_t size);

/*!
 * @brief       Main program
 *
 * @param       None
 *
 * @retval      None
 *
 * @note
 */
int main(void)
{
    /* Configures LED GPIO */
    APM_MINI_LEDInit(LED2);
    APM_MINI_LEDInit(LED3);
    APM_MINI_COMInit(COM1);
    
    /* Configure CRC peripheral  to use 8-bits polynomials */
    CRC_Config(0);
        
    /* Get CRC value of the 8-bit buffer:DataBuffer */
    CRCValue = CRC_Compute8Bits(DataBuffer,BUFFER_SIZE);

    /* Check if the CRCValue is equal to the CRCExpectedValue */
    if(CRCValue == CRCExpectedValue)
    {
        Delay();
        /* Turns selected LED On */
        APM_MINI_LEDOn(LED2);
    }
        
    else
    {
        Delay();
        /* Turns selected LED On */
        APM_MINI_LEDOn(LED3);
    }
    printf("CRC_Result : %x\r\n", CRCValue);

    while (1)
    {
    }
}


/*!
 * @brief       Configure CRC peripheral to use 8-bits polynomials
 *
 * @param       CRC polynomial
 *
 * @retval      None
 *
 */
static void CRC_Config(uint8_t Poly)
{
    /* Enable CRC clock */
    RCM_EnableAHBPeriphClock(RCM_AHB_PERIPH_CRC);

    /*  Resets CRC peripheral */
    CRC_Reset();
    
    /* Reset CRC data register */
    CRC_ResetDATA();
        
    /* Set the CRC polynomial size */
    CRC_SetPolynomialSize(CRC_POLYNOMIAL_SIZE_8);
        
    /* Set the CRC polynomail coefficients */
    CRC_SetPolynomialValue(Poly);
}

/*!
* @brief       Get CRC value for input message.
*
* @param       *Data:  pointer at uint8_t.
*
* @param       Size:   the input message size.
*
* @retval      Calculated CRC value.
*
*/
static uint8_t CRC_Compute8Bits(uint8_t* Data,uint32_t Size)
{
    uint8_t* DATAEnd = Data + Size;
    
    /* Reset CRC data register */
    CRC_ResetDATA();
    
    while(Data < DATAEnd)
    {
        /* Write input Data to CRC register */
        CRC_CalculateCRC8bits(*Data++);
    }
        
    /* Return CRC value */
    return (uint8_t)CRC_ReadCRC();
}

/*!
 * @brief       Delay
 *
 * @param       None
 *
 * @retval      None
 *
 * @note
 */
void Delay(void)
{
    volatile uint32_t delay = 0xff5;

    while (delay--);
}

#if defined (__CC_ARM) || defined (__ICCARM__) || (defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050))

/*!
* @brief       Redirect C Library function printf to serial port.
*              After Redirection, you can use printf function.
*
* @param       ch:  The characters that need to be send.
*
* @param       *f:  pointer to a FILE that can recording all information
*              needed to control a stream
*
* @retval      The characters that need to be send.
*
* @note
*/
int fputc(int ch, FILE* f)
{
    /* send a byte of data to the serial port */
    USART_TxData(DEBUG_USART, (uint8_t)ch);

    /* wait for the data to be send  */
    while (USART_ReadStatusFlag(DEBUG_USART, USART_FLAG_TXBE) == RESET);

    return (ch);
}

#elif defined (__GNUC__)

/*!
* @brief       Redirect C Library function printf to serial port.
*              After Redirection, you can use printf function.
*
* @param       ch:  The characters that need to be send.
*
* @retval      The characters that need to be send.
*
* @note
*/
int __io_putchar(int ch)
{
    /* send a byte of data to the serial port */
    USART_TxData(DEBUG_USART, ch);

    /* wait for the data to be send  */
    while (USART_ReadStatusFlag(DEBUG_USART, USART_FLAG_TXBE) == RESET);

    return ch;
}

/*!
* @brief       Redirect C Library function printf to serial port.
*              After Redirection, you can use printf function.
*
* @param       file:  Meaningless in this function.
*
* @param       *ptr:  Buffer pointer for data to be sent.
*
* @param       len:  Length of data to be sent.
*
* @retval      The characters that need to be send.
*
* @note
*/
int _write(int file, char* ptr, int len)
{
    int i;
    for (i = 0; i < len; i++)
    {
        __io_putchar(*ptr++);
    }

    return len;
}

#else
#warning Not supported compiler type
#endif

/**@} end of group CRC_8BitsCRCMessage_Functions */
/**@} end of group CRC_8BitsCRCMessage */
/**@} end of group Examples */
