/*!
 * @file        main.c
 *
 * @brief       Main program body
 *
 * @version     V1.0.0
 *
 * @date        2022-11-10
 *
 * @attention
 *
 *  Copyright (C) 2022 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 "FreeRTOS.h"
#include "task.h"

/** @addtogroup Examples
  * @brief FreeRTOS Demo examples
  @{
  */

/** @addtogroup FreeRTOS_Demo
  @{
  */

/** @defgroup FreeRTOS_Demo_Macros Macros
  @{
*/

/* printf using UART4 */
#define DEBUG_USART           UART4

/**@} end of group FreeRTOS_Demo_Macros */

/** @defgroup FreeRTOS_Demo_Variables Variables
  @{
  */

/** Task handle */
TaskHandle_t xHandleTaskLedToggle = NULL;
TaskHandle_t xHandleTaskPrint = NULL;

/* Menu title string */
const char* menuTitleStr[] =
{
    "      FreeRTOS Example     ",
};

/* Menu bottom column string*/
const char* menuBottomColStr[] =
{
    "   GEEHY SEMICONDUCTOR   ",
};

/**@} end of group FreeRTOS_Demo_Variables*/

/** @defgroup FreeRTOS_Demo_Functions Functions
  @{
  */

/* Private functions declaration */
void APM_EVAL_Init(void);
void Menu_DisplayInit(void);
void UserTaskCreate(void);
void vTaskLedToggle(void *pvParameters);
void vTaskPrint(void *pvParameters);

/*!
 * @brief       Main program
 *
 * @param       None
 *
 * @retval      None
 *
 */
int main(void)
{
    APM_EVAL_Init();
    
    printf("\r\nThis is an example of FreeRTOS V10.4.3.\r\n");
    
    UserTaskCreate();
    
    /* Startup FreeRTOS */
    vTaskStartScheduler();
    
    while (1)
    {
    }
}

/*!
 * @brief       Init EVAL board
 *
 * @param       None
 *
 * @retval      None
 *
 * @note
 */
void APM_EVAL_Init(void)
{
    USART_Config_T usartConfigStruct;
    
    /* Init delay function */
    APM_EVAL_DelayInit();

    APM_EVAL_LEDInit(LED1);
    
    /* UART4 Config */
    usartConfigStruct.baudRate = 115200;
    usartConfigStruct.hardwareFlow = USART_HARDWARE_FLOW_NONE;
    usartConfigStruct.mode = USART_MODE_TX_RX;
    usartConfigStruct.parity = USART_PARITY_NONE;
    usartConfigStruct.stopBits = USART_STOP_BIT_1;
    usartConfigStruct.wordLength = USART_WORD_LEN_8B;
    APM_EVAL_COMInit(COM4, &usartConfigStruct);

    APM_LCDInit();
    LCD_Clear(0, 0, LCD_WIDTH, LCD_HEIGHT, LCD_BCOLOR);

    APM_EVAL_DelayMs(100);

    Menu_DisplayInit();
}

/*!
 * @brief       Initialize the Menu UI
 *
 * @param       None
 *
 * @retval      None
 *
 * @note
 */
void Menu_DisplayInit(void)
{
    /* Menu title */
    LCD_Clear(0, 0, LCD_WIDTH, 40, LCD_FCOLOR);
    LCD_DisplayString(10, LCD_LINE_TITLE, menuTitleStr[0], LCD_TEXT_SELECT_FC, LCD_TEXT_SELECT_BC, 16, 0);

    /* Menu bottom */
    LCD_Clear(0, LCD_LINE_BOTTOM, LCD_WIDTH, LCD_HEIGHT, LCD_FCOLOR);
    LCD_DisplayString(20, LCD_LINE_BOTTOM, menuBottomColStr[0], LCD_TEXT_SELECT_FC, LCD_TEXT_SELECT_BC, 16, 0);
}

/*!
 * @brief       User Create Task
 *
 * @param       None
 *
 * @retval      None
 */
void UserTaskCreate(void)
{
    xTaskCreate((TaskFunction_t )vTaskLedToggle,
                (const char*    )"Led Toggle Task",
                (uint16_t       )256,
                (void*          )NULL,
                (UBaseType_t    )6,
                (TaskHandle_t*  )&xHandleTaskLedToggle);
                
    xTaskCreate((TaskFunction_t )vTaskPrint,
                (const char*    )"Print Task",
                (uint16_t       )256,
                (void*          )NULL,
                (UBaseType_t    )7,
                (TaskHandle_t*  )&xHandleTaskPrint);
}

/*!
 * @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_EVAL_LEDToggle(LED1);
        /* Task blocking time Delay */
        vTaskDelay(1000);
    }
}

/*!
 * @brief       print task
 *
 * @param       pvParameters: passed into the task function as the function parameters
 *
 * @retval      None
 */
void vTaskPrint(void *pvParameters)
{
    while(1)
    {
        /* print test */
        printf("APM32F107 EVAL FreeRTOS Example Test.\r\n");
        /* Task blocking time Delay */
        vTaskDelay(1000);
    }
}

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

/**@} end of group FreeRTOS_Demo_Functions */
/**@} end of group FreeRTOS_Demo */
/**@} end of group Examples */
