/*********************************************************
*Copyright (C), 2018, Shanghai Eastsoft Microelectronics Co., Ltd.
*文件名:  main.c
*作  者:  Jiayc
*版  本:  V1.00
*日  期:  2020/8/24
*描  述:  FreeRTOS_Mutexes演示程序
          MCU：ES8P5088
          FreeRTOS版本：v10.2.1
          创建队列发送接收任务，实现任务间消息传递
          硬件管脚：LED:PA14  UART_TX:PA15 UART_RX:PA16
*备  注:  本软件仅供学习和演示使用，对用户直接引用代码所带来的风险或后果不承担任何法律责任。
**********************************************************/
#include "ES8P508x.h"
#include "stdio.h"
#include "bsp.h"
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"

QueueHandle_t MessageQueue;

static void vLED_Task(void *pvParameters);
static void vMessageQueuesProduce_Task(void *pvParameters);
static void vMessageQueuesConsume_Task(void *pvParameters);


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

    MessageQueue = xQueueCreate(1, sizeof(char *));     //创建消息队列

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

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

    xTaskCreate(vMessageQueuesProduce_Task,
                "MessageQueuesProduce",
                128,
                NULL,
                3,
                NULL);
    xTaskCreate(vMessageQueuesConsume_Task,
                "MessageQueuesConsume",
                128,
                NULL,
                4,
                NULL);

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

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

    for (;;)
    {

    }
}

static void vLED_Task(void *pvParameters)
{

    for (;;)
    {
        GPIO->PADATABRR.Word = 1U << 14;
        vTaskDelay(500);
    }
}

static void vMessageQueuesProduce_Task(void *pvParameters)
{
    for (;;)
    {
        char *ptr = "Hello world!";
        printf("MessageQueuesProduce_Task sends message.\r\n");
        xQueueSend(MessageQueue, &ptr, 0);                          //发送消息队列
        vTaskDelay(1000);
    }
}

static void vMessageQueuesConsume_Task(void *pvParameters)
{
    char *ptr;

    for (;;)
    {
        if (xQueueReceive(MessageQueue, &ptr, portMAX_DELAY) == pdPASS) //接收消息队列
        {
            printf("MessageQueuesConsume_Task receives message, the message is: %s\r\n\r\n", ptr);
        }
    }
}

