/*!
 * @file       main.c
 *
 * @brief      Main program body
 *
 * @version    V1.0.2
 *
 * @date       2023-03-31
 *
 * @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"

/** @addtogroup Examples
  @{
*/

/** @addtogroup FreeRTOS
  @{
*/

/** @defgroup FreeRTOS_Marcos Marcos
  @{
*/

#define USART_BUFFER_SIZE 16

/**@} end of group FreeRTOS_Marcos */

/** @defgroup FreeRTOS_Variables Variables
  @{
*/

__IO uint8_t USART_RxBuf[USART_BUFFER_SIZE] = {0};
__IO uint8_t USART_offset = 0;

/**@} end of group FreeRTOS_Variables */

/** @defgroup FreeRTOS_Functions Functions
  @{
*/

/* USART Init */
void USART_Init(void);

/* Task handle */
static TaskHandle_t xHandleTaskLedToggle = NULL;
static TaskHandle_t xHandleTaskUsartTest = NULL;

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

/* Led toggle task */
void vTaskLedToggle(void* pvParameters);
/* Usart test task */
void vTaskUsartTest(void* pvParameters);

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

    /* USART Initialization */
    USART_Init();

    /* User create task */
    UserTaskCreate();

    /* Startup FreeRTOS */
    vTaskStartScheduler();

    while (1);
}

/*!
 * @brief       User Create Task
 *
 * @param       None
 *
 * @retval      None
 */
static void UserTaskCreate(void)
{
    xTaskCreate(vTaskLedToggle,
                "TaskLedToggle",
                32,
                NULL,
                0,
                &xHandleTaskLedToggle);

    xTaskCreate(vTaskUsartTest,
                "TaskUsartTest",
                64,
                NULL,
                2,
                &xHandleTaskUsartTest);
}

/*!
 * @brief       Led toggle task
 *
 * @param       pvParameters - passed into the task function as the function parameters
 *
 * @retval      None
 */
void vTaskLedToggle(void* pvParameters)
{
    while (1)
    {
        /* Toggle LED2 */
        APM_MINI_LEDToggle(LED2);
        /* Toggle LED3 */
        APM_MINI_LEDToggle(LED3);
        /* Task blocking time Delay */
        vTaskDelay(500);
    }
}

/*!
 * @brief       Usart test task
 *
 * @param       pvParameters - passed into the task function as the function parameters
 *
 * @retval      None
 */
void vTaskUsartTest(void* pvParameters)
{
    uint8_t i = 0, offset = 0;
    __IO uint8_t timeout = 0;

    while (1)
    {
        USART_DisableInterrupt(USART1, USART_INT_RX);
        timeout = 0;
        if(USART_offset != 0)
        {
            offset = USART_offset;
        }

        for(i = 0; i < offset; i++)
        {
            /* Wait until end of transmission */
            while (USART_ReadStatusFlag(APM_MINI_COM1, USART_FLAG_TXBE) == RESET && timeout <= 10)
            {
                vTaskDelay(10);
                timeout++;
            }
            if(timeout <= 10)
            {
                USART_TxData8(APM_MINI_COM1, USART_RxBuf[i]);
                timeout = 0;
            }
        }
        USART_offset = 0;
        USART_EnableInterrupt(USART1, USART_INT_RX);

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

/*!
 * @brief       USARTS Initialization
 *
 * @param       None
 *
 * @retval      None
 */
void USART_Init(void)
{
    USART_Config_T usartConfig;

    usartConfig.baudRate = 115200;
    usartConfig.mode = USART_MODE_TX_RX;
    usartConfig.parity = USART_PARITY_NONE;
    usartConfig.interrupt = USART_INT_RX;
    usartConfig.stopBits = USART_STOP_BIT_1;
    usartConfig.wordLength = USART_WORD_LEN_8B;

    APM_MINI_COMInit(COM1, &usartConfig);

    /* Enable USART1 RXBNE interrput */
    USART_EnableInterrupt(APM_MINI_COM1, USART_INT_RX);
    NVIC_EnableIRQRequest(USART1_RX_IRQn, 5);
    USART_ClearStatusFlag(APM_MINI_COM1, USART_FLAG_RXBNE);
}

/*!
 * @brief       USART RX interrupt server routine
 *
 * @param       None
 *
 * @retval      None
 */
void USART_RX_Isr(void)
{
    if(USART_ReadStatusFlag(USART1, USART_FLAG_RXBNE))
    {
        USART_RxBuf[USART_offset] = USART_RxData8(APM_MINI_COM1);
        USART_offset++;
    }
}
/**@} end of group FreeRTOS_Functions */
/**@} end of group FreeRTOS */
/**@} end of group Examples */
