/*!
 * @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.
 */

/*
* MISRA-C:2012 compliance checks
*
*       main_c_MISRA_REF1
*          Breaks the required Rule-8.13 of MISRA 2012 guidelines,
*          A pointer should point to a const-qualified type whenever possible
*
*       main_c_MISRA_REF2
*          Breaks the required Rule-17.11-C11 of MISRA 2012 guidelines,
*          A function that never returns should be declared with a _Noreturn function specifier
*
*
*/


/* Includes */
#include "Board.h"

/* FreeRTOS includes */
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "list.h"

/** @addtogroup Examples
  @{
  */

/** @addtogroup RTOS_Examples
  @{
  */

/** @addtogroup FreeRTOS
  * @{
*/

/** @defgroup FreeRTOS_Macros Macros
  @{
  */

#define DATA_BUF_SIZE       (32)

/**@} end of group FreeRTOS_Macros */

/** @defgroup FreeRTOS_Functions Functions
  @{
  */

/* USART1 receive buffer */
static uint8_t rxDataBufUSART1[DATA_BUF_SIZE] = {0};
/* USART1 transmit buffer */
static uint8_t txDataBufUSART1[DATA_BUF_SIZE] = {0};

/* USART2 receive buffer */
static uint8_t rxDataBufUSART2[DATA_BUF_SIZE] = {0};
/* USART2 transmit buffer */
static uint8_t txDataBufUSART2[DATA_BUF_SIZE] = {0};

/* USART Init */
void Usart_Init(void);
/* Rtos_Delay */
void Rtos_Delay(uint32_t Count);
/* Buffer compare*/
BOOL Rtos_BufferCompare(const uint8_t* Buf1Ptr, const uint8_t* Buf2Ptr, uint8_t Size);

/* Task handle */
static TaskHandle_t s_xHandleTaskLedToggle = NULL;
static TaskHandle_t s_xHandleTaskUsartTest = NULL;

/* User create task */
static void User_TaskCreate(void);

/* Led toggle task */
void Task_LedToggle(void* pvParametersPtr);
/* Usart test task */
void Task_UsartTest(void* pvParametersPtr);

/*!
 * @brief       Main program
 *
 * @param       None
 *
 * @retval      None
 */
int main(void)
{
    Board_LedInit(LED2);
    Board_LedInit(LED3);

    /* USART Initialization */
    Usart_Init();

    /* User create task */
    User_TaskCreate();

    /* Startup FreeRTOS */
    vTaskStartScheduler();

    while (1)
    {
        /* nothing */
    }
}

/*!
 * @brief       User Create Task
 *
 * @param       None
 *
 * @retval      None
 */
static void User_TaskCreate(void)
{
    xTaskCreate(&Task_LedToggle,
                "TaskLedToggle",
                128,
                NULL,
                0,
                &s_xHandleTaskLedToggle);

    xTaskCreate(&Task_UsartTest,
                "TaskUsartTest",
                128,
                NULL,
                2,
                &s_xHandleTaskUsartTest);
}

/*!
 * @brief       Led toggle task
 *
 * @param       pvParametersPtr - passed into the task function as the function parameters
 *
 * @retval      None
 */
/*** @warning main_c_MISRA_REF1 The object addressed by the pointer parameter '%1s' is not modified and so the pointer could be of type 'pointer to const'. */
/*** @warning main_c_MISRA_REF2 This function doesn't return to the caller despite not being declared as non-returning. */
void Task_LedToggle(void* pvParametersPtr)
{
   (void)pvParametersPtr; 
    while (1)
    {
        /* Toggle LED2 */
        Board_LedToggle(LED2);
        /* Task blocking time Rtos_Delay */
        vTaskDelay(500);
    }
}

/*!
 * @brief       Led toggle task
 *
 * @param       pvParametersPtr - passed into the task function as the function parameters
 *
 * @retval      None
 */
/*** @warning main_c_MISRA_REF1 The object addressed by the pointer parameter '%1s' is not modified and so the pointer could be of type 'pointer to const'. */
/*** @warning main_c_MISRA_REF2 This function doesn't return to the caller despite not being declared as non-returning. */
void Task_UsartTest(void* pvParametersPtr)
{
    int i = 0;
    BOOL state = TRUE;
    int timeout = 0;
    /* Init txDataBufUSART2 */
    for (i = 0; i < DATA_BUF_SIZE; i++)
    {
        txDataBufUSART1[i] = (uint8_t)i;
        txDataBufUSART2[i] = (uint8_t)i;
    }

    (void)pvParametersPtr; 
    while (1)
    {
        /* Send Data from USART1 to USART2 */
        timeout = 0;
        for (i = 0; i < DATA_BUF_SIZE; i++)
        {
            /* Wait until end of transmission */
            while (Usart_ReadStatusFlag(EVAL_COM1, USART_FLAG_TXBE) == (uint8_t)RESET)
            {
                vTaskDelay(10);
                timeout++;
                if (timeout > 10)
                {
                    break;
                }
                else
                {
                    /* nothing */
                }
            }

            Usart_TxData(EVAL_COM1, txDataBufUSART1[i]);

            /* Wait until end of reception */
            while (Usart_ReadStatusFlag(EVAL_COM2, USART_FLAG_RXBNE) == (uint8_t)RESET)
            {
                vTaskDelay(10);
                timeout++;
                if (timeout > 10)
                {
                    break;
                }
                else
                {
                    /* nothing */
                }
            }

            rxDataBufUSART2[i] = (uint8_t)Usart_RxData(EVAL_COM2);
        }

        /* Verify data */
        state = Rtos_BufferCompare(rxDataBufUSART2, txDataBufUSART1, DATA_BUF_SIZE);

        /* Data is ok then turn off LED3 */
        if (state == TRUE)
        {
            Board_LedOn(LED3);
        }
        else
        {
            Board_LedOff(LED3);
        }

        /* Task blocking time Rtos_Delay */
        vTaskDelay(1000);

        timeout = 0;
        /* Send Data from USART2 to USART1 */
        for (i = 0; i < DATA_BUF_SIZE; i++)
        {
            /* Wait until end of transmission */
            while (Usart_ReadStatusFlag(EVAL_COM2, USART_FLAG_TXBE) == (uint8_t)RESET)
            {
                vTaskDelay(10);
                timeout++;
                if (timeout > 10)
                {
                    break;
                }
                else
                {
                    /* nothing */
                }
            }

            Usart_TxData(EVAL_COM2, txDataBufUSART2[i]);

            /* Wait until end of reception */
            while (Usart_ReadStatusFlag(EVAL_COM1, USART_FLAG_RXBNE) == (uint8_t)RESET)
            {
                vTaskDelay(10);
                timeout++;
                if (timeout > 10)
                {
                    break;
                }
                else
                {
                    /* nothing */
                }
            }

            rxDataBufUSART1[i] = (uint8_t)Usart_RxData(EVAL_COM1);
        }

        /* Verify data */
        state = Rtos_BufferCompare(rxDataBufUSART1, txDataBufUSART2, DATA_BUF_SIZE);

        /* Data is ok then turn off LED3 */
        if (state == TRUE)
        {
            Board_LedOn(LED3);
        }
        else
        {
            Board_LedOff(LED3);
        }

        /* Task blocking time Rtos_Delay */
        vTaskDelay(1000);
    }
}

/*!
 * @brief       Compares two buffers
 *
 * @param       Buf1Ptr:    First buffer to be compared
 *
 * @param       Buf2Ptr:    Second buffer to be compared
 *
 * @param       Size:    Buffer size
 *
 * @retval      Return SET if buf1 = buf2. If not then return RESET
 */
BOOL Rtos_BufferCompare(const uint8_t* Buf1Ptr, const uint8_t* Buf2Ptr, uint8_t Size)
{
    BOOL temoRte = TRUE;
    uint8_t i;

    for (i = 0; i < Size; i++)
    {
        if (Buf1Ptr[i] != Buf2Ptr[i])
        {
            temoRte = FALSE;
            break;
        }
        else
        {
            /* nothing */
        }
    }

    return temoRte;
}

/*!
 * @brief       USARTS Initialization
 *
 * @param       None
 *
 * @retval      None
 */
void Usart_Init(void)
{
    Board_ComInit(COM1);
    Board_ComInit(COM2);

    Rtos_Delay(0x7FFF);

    /* Disable USART1 RXBNE interrput */
    Usart_DisableInterrupt(EVAL_COM1, USART_INT_RXBNEIE);
    Usart_DisableInterrupt(EVAL_COM2, USART_INT_RXBNEIE);

    /* Disable USART2 RXBNE interrput */
    Usart_ClearStatusFlag(EVAL_COM1, USART_FLAG_RXBNE);
    Usart_ClearStatusFlag(EVAL_COM2, USART_FLAG_RXBNE);
}

/*!
 * @brief       Rtos_Delay
 *
 * @param       count:  delay count
 *
 * @retval      None
 */
void Rtos_Delay(uint32_t Count)
{
    volatile uint32_t delay = Count;
    while (0u != delay)
    {
        delay = delay - 1u;
    }
}

/**@} end of group FreeRTOS_Functions */
/**@} end of group FreeRTOS */
/**@} end of group RTOS_Examples */
/**@} end of group Examples */
