#include "CDD_Crc.h"

#include "CDD_Usart.h"
#include "Mcu.h"
#include "Port.h"

/* CRC calculate Buffer */
const uint32 CRCBuffer[] =
{
    0x4ad47ab7, 0x6a961a71, 0x0a503a33, 0x2a12dbfd, 0xfbbfeb9e, 0x9b798b58,
    0xbb3bab1a, 0x6ca67c87, 0x5cc52c22, 0x3c030c60, 0x1c41edae, 0xfd8fcdec,
    0xad2abd0b, 0x8d689d49, 0x7e976eb6, 0x5ed54ef4, 0x2e321e51, 0x0e70ff9f,
    0xefbedfdd, 0xcffcbf1b, 0x9f598f78, 0x918881a9, 0xb1caa1eb, 0xd10cc12d,
    0xe16f1080, 0x00a130c2, 0x20e35004, 0x40257046, 0x83b99398, 0xa3fbb3da,
    0xc33dd31c, 0xe37ff35e, 0x129022f3, 0x32d24235, 0x52146277, 0x7256b5ea,
};

int main(void)
{
    VAR( Crc_CalculateResultType, AUTOMATIC ) testResult = {0};

    /* Initialize MCU module */
    Mcu_Init(&g_mcuConfig);
    /* Initialize clock */
    Mcu_InitClock(McuClockSettingConfig_0);
    /* Initialize PORT module */
    Port_Init(&g_portConfig);
    /* Initialize LPUART1 */
    Usart_Init(g_usartConfg);//TX PIN:PA9

    printf("\r\n---------------- Crc_ITA Start ----------------\r\n");

    Crc_Init();

    Crc_CalculateBlockCRC(CRCBuffer, sizeof(CRCBuffer) / sizeof(CRCBuffer[0]), &testResult);

    printf("\r\nCrc result is : 0x%08X (Expect: 0x34858D7E)\r\n", (unsigned int)(testResult.crcValue));

    Crc_DeInit();

    if (testResult.crcValue == 0x34858D7E) 
    {
        printf(" \r\nCrc test success!\r\n");
    }
    else
    {
        printf(" \r\nCrc test error, please check!\r\n");
    }

    printf("\r\n---------------- Crc_ITA End ----------------\r\n");

    while(1);
}

#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)
{
    int reVal = 0;
    reVal = Usart_PutRedefinition(USART1_ID,&ch);
    
    return reVal;
}

/* If configuring the full library using versions after IAR 9.30,
   it is necessary to implement the _write function for printf redirection.*/
size_t __write(int handle,const unsigned char *buf,size_t bufSize)
{
    int32 tempCh = 0;
    for(int i=0; i<bufSize; i++)
    {
        Usart_PutRedefinition(USART1_ID,&tempCh);
    }
    return bufSize;
}

#elif defined (__GNUC__)

/* For Arm Cross GCC: Eclipse */
int _write(int fd,const unsigned char *buf,int len)
{
    int32 tempCh = 0;
    for(int i=0; i<len; i++)
    {
        int32 tempCh = (int32)buf[i];
        Usart_PutRedefinition(USART1_ID,&tempCh);
    }
    return len;
}

#endif

