/*********************************************************
*Copyright (C), 2018, Shanghai Eastsoft Microelectronics Co., Ltd.
*文件名:  main.c
*作  者:  Jiayc
*版  本:  V1.00
*日  期:  2020/8/24
*描  述:  FreeRTOS_NotifyFromISR演示程序
          MCU：ES8P5088
          创建接收通知任务，在串口中断中向接收通知任务发送通知信号。
          硬件管脚：LED:PA14  UART_TX:PA15 UART_RX:PA16
*备  注:  本软件仅供学习和演示使用，对用户直接引用代码所带来的风险或后果不承担任何法律责任。
**********************************************************/
#include "ES8P508x.h"
#include "bsp.h"
#include "lib_uart.h"
#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"


static void vLED_Task(void *pvParameters);
static void vNotifyTake_Task(void *pvParameters);

TaskHandle_t NotifyTake_Handler;

int main()
{
    LED_init();
    UARTInit();

    taskENTER_CRITICAL();                   //进入临界保护

    xTaskCreate(vLED_Task,                  //创建任务
                "LED",
                128,
                NULL,
                5,
                NULL);

    xTaskCreate(vNotifyTake_Task,
                "NotifyTake",
                128,
                NULL,
                3,
                &NotifyTake_Handler);

    taskEXIT_CRITICAL();                    //退出临界保护

    vTaskStartScheduler();                  //开始任务调度

    for (;;)
    {

    }
}

static void vLED_Task(void *pvParameters)
{
    for (;;)
    {
        GPIO->PADATABRR.Word = 1U << 14;
        vTaskDelay(500);
    }
}

static void vNotifyTake_Task(void *pvParameters)
{
    for (;;)
    {
        if (ulTaskNotifyTake(pdTRUE, 0) == pdTRUE)              //接收通知信号
        {
            UART_SendString("NotifyTake_Task takes notify.\r\n");
            UART_SendString("\r\n");
        }
    }
}

void UART2_IRQHandler()
{
    volatile uint8_t c;
    static BaseType_t xHigherPriorityTaskWoken;
    xHigherPriorityTaskWoken = pdFALSE;

    if (UART_GetITStatus(UART2, UART_IT_RB))
    {
        c = UART_RecByte(UART2);

        if (c == 0x55)                          //当串口收到0x55时vNotifyTake_Task发送通知信号
        {
            vTaskNotifyGiveFromISR(NotifyTake_Handler, &xHigherPriorityTaskWoken);
        }
    }
}

