/*!
 * @file        main.c
 *
 * @brief       Main program body
 *
 * @version     V1.0.0
 *
 * @date        2026-02-25
 *
 * @attention
 *
 *  Copyright (C) 2026 Geehy Semiconductor
 *
 *  You may not use this file except in compliance with the
 *  GEEHY COPYRIGHT NOTICE (Geehy Semiconductor Software License Agreement).
 *
 *  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 Semiconductor Software License Agreement for the governing permissions
 *  and limitations under the License.
 */

/* Includes */
#include "Board.h"
#include "stdio.h"
#include "g32a10xx_eint.h"
#include "g32a10xx_syscfg.h"
#include "g32a10xx_sha256.h"
#include "g32a10xx_misc.h"
#include "user_sha256_config.h"
#include <string.h>

/** @addtogroup Examples
  @{
  */

/** @addtogroup SHA256_Examples
  @{
*/

/** @addtogroup HASH_SHA2
  @{
  */

/** @defgroup HASH_SHA2_Macros Macros
  @{
*/

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

/**@} end of group HASH_SHA2_Macros */

/** @defgroup HASH_SHA2_Functions Functions
  @{
  */

/* Sha256_Delay */
void Sha256_Delay(uint32_t count);

/* judge array if exist repetitional number */
uint32_t Sha256_Hashduplicates(const uint32_t Arr[], uint32_t Size);

/*!
 * @brief    main program
 *
 * @param    None
 *
 * @retval   None
 *
 * @note
 */
int main(void)
{
    BOOL flgTrue = TRUE;
    uint8_t cntTrue = 0;
    uint8_t i = 0;

    Board_LedInit(LED2);
    Board_LedInit(LED3);
    Board_ComInit(COM1);

    Rcm_EnableAhbPeriphClock(RCM_AHB_PERIPH_SHA);
    printf("\r\nSystem is Init\r\n");
    
    /* Pad and Initialization Mode */
    Rcm_EnableAhbPeriphReset(RCM_AHB_PERIPH_SHA);
    Rcm_DisableAhbPeriphReset(RCM_AHB_PERIPH_SHA);
    Sha256_Start(g_sha256PadInitModeConfig, SHA256_PAD_INIT_MODE_INPUT, strlen((const char*)SHA256_PAD_INIT_MODE_INPUT), SHA256_OUTPUT, 100);

    for (i = 0; i < 32U; i++)
    {
        if(SHA256_OUTPUT[i] != SHA256_PAD_INIT_MODE_OUTPUT[i])
        {
            flgTrue = FALSE;
            break;
        }
        else
        {
            flgTrue = TRUE;
        }
    }
    
    if (flgTrue == FALSE)
    {
        printf("\r\nSHA256 Pad and Initialization Mode calculate is false.\r\n");
    }
    else
    {
        cntTrue++;
        printf("\r\nSHA256 Pad and Initialization Mode calculate is true.\r\n");
    }
    
    /* Pad and none-Initialization Mode */
    Rcm_EnableAhbPeriphReset(RCM_AHB_PERIPH_SHA);
    Rcm_DisableAhbPeriphReset(RCM_AHB_PERIPH_SHA);
    Sha256_Start(g_sha256PadNoneInitModeConfig, SHA256_PAD_NONEINIT_MODE_INPUT, strlen((const char*)SHA256_PAD_NONEINIT_MODE_INPUT), SHA256_OUTPUT, 100);

    for (i = 0; i < 32U; i++)
    {
        if(SHA256_OUTPUT[i] != SHA256_PAD_NONEINIT_MODE_OUTPUT[i])
        {
            flgTrue = FALSE;
            break;
        }
        else
        {
            flgTrue = TRUE;
        }
    }
    
    if (flgTrue == FALSE)
    {
        printf("\r\nSHA256 Pad and none-Initialization Mode calculate is false.\r\n");
    }
    else
    {
        cntTrue++;
        printf("\r\nSHA256 Pad and none-Initialization Mode calculate is true.\r\n");
    }
    
    /* none-Pad and Initialization Mode */
    Rcm_EnableAhbPeriphReset(RCM_AHB_PERIPH_SHA);
    Rcm_DisableAhbPeriphReset(RCM_AHB_PERIPH_SHA);
    Sha256_Start(g_sha256NonePadInitModeConfig, SHA256_NONEPAD_INIT_MODE_INPUT, strlen((const char*)SHA256_NONEPAD_INIT_MODE_INPUT), SHA256_OUTPUT, 100);

    for (i = 0; i < 32U; i++)
    {
        if(SHA256_OUTPUT[i] != SHA256_NONEPAD_INIT_MODE_OUTPUT[i])
        {
            flgTrue = FALSE;
            break;
        }
        else
        {
            flgTrue = TRUE;
        }
    }
    
    if (flgTrue == FALSE)
    {
        printf("\r\nSHA256 none-Pad and Initialization Mode calculate is false.\r\n");
    }
    else
    {
        cntTrue++;
        printf("\r\nSHA256 none-Pad and Initialization Mode calculate is true.\r\n");
    }

    /* none-Pad and none-Initialization Mode */
    Rcm_EnableAhbPeriphReset(RCM_AHB_PERIPH_SHA);
    Rcm_DisableAhbPeriphReset(RCM_AHB_PERIPH_SHA);
    Sha256_Start(g_sha256NonePadNoneInitModeConfig, SHA256_NONEPAD_NONEINIT_MODE_INPUT, strlen((const char*)SHA256_NONEPAD_NONEINIT_MODE_INPUT), SHA256_OUTPUT, 100);

    for (i = 0; i < 32U; i++)
    {
        if(SHA256_OUTPUT[i] != SHA256_NONEPAD_NONEINIT_MODE_OUTPUT[i])
        {
            flgTrue = FALSE;
            break;
        }
        else
        {
            flgTrue = TRUE;
        }
    }
    
    if (flgTrue == FALSE)
    {
        printf("\r\nSHA256 none-Pad and none-Initialization Mode calculate is false.\r\n");
    }
    else
    {
        cntTrue++;
        printf("\r\nSHA256 none-Pad and none-Initialization Mode calculate is true.\r\n");
    }

    if (cntTrue == 4U)
    {
        Board_LedOn(LED2);
        printf("\r\nSHA256 all test is true.\r\n");
    }
    else
    {
        Board_LedOff(LED2);
        printf("\r\nSHA256 test is false.\r\n");
    }
    
    for (;;)
    {
        Sha256_Delay(0xfffff);
    }
}

/*!
 * @brief       Sha256_Delay
 *
 * @param       None
 *
 * @retval      None
 *
 * @note
 */
void Sha256_Delay(uint32_t count)
{
    volatile uint32_t delay = count;

    while (delay != 0U)
    {
        delay = delay - 1U;
    }
}


/*!
 * @brief       judge array if exist repetitional number
 *
 * @param       Arr[]: input array
 *
 * @param       Size: array size
 *
 * @retval      repetitions is true or not
 *
 * @note
 */
uint32_t Sha256_Hashduplicates(const uint32_t Arr[], uint32_t Size)
{
    uint8_t repetFlg = 0;
    uint32_t i = 0, j = 0;

    for (i = 0; i < Size; i++)
    {
        for (j = i + 1U; j < Size; j++)
        {
            if (Arr[i] == Arr[j])
            {
                repetFlg = 1; // there are repetitions
                break;
            }
            else
            {
                /* nothing */
            }
        }
        if (repetFlg == 1U)
        {
            break;
        }
        else
        {
            /* nothing */
        }
    }
    return repetFlg; // there are no repetitions
}

#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) == (uint8_t)RESET)
    {
        /* nothing */
    }

    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
#pragma message("Not supported compiler type")
#endif

/**@} end of group HASH_SHA2_Functions */
/**@} end of group HASH_SHA2 */
/**@} end of group SHA256_Examples */
/**@} end of group Examples */
