/* The example of esp-free-rtos
*
* This sample code is in the public domain.
*
* You have to add this in FreeRTOSConfig.h.
* #define
configUSE_QUEUE_SETS 1
*/
#include <stdlib.h>
#include <string.h>
#include "espressif/esp_common.h"
#include "esp/uart.h"
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "semphr.h"
#include "esp8266.h"
QueueSetHandle_t xQueueSet;
QueueHandle_t xQueue1, xQueue2;
SemaphoreHandle_t xSemaphore;
QueueSetMemberHandle_t xActivatedMember;
uint32_t xReceivedFromQueue1;
char xReceivedFromQueue2[20];
/* Define the lengths of the queues that will be added to
the queue set. */
#define QUEUE_LENGTH_1 10
#define QUEUE_LENGTH_2 10
/* Define the size of the item to be held by queue 1 and
queue 2 respectively.
The values used here are just for demonstration purposes.
*/
#define ITEM_SIZE_QUEUE_1 sizeof( xReceivedFromQueue1 )
#define ITEM_SIZE_QUEUE_2 sizeof( xReceivedFromQueue2 )
/* Binary semaphores have an effective length of 1. */
#define BINARY_SEMAPHORE_LENGTH 1
/* The combined length of the two queues and binary
semaphore that will be
added to the queue set. */
#define COMBINED_LENGTH ( QUEUE_LENGTH_1 + \
QUEUE_LENGTH_2 + \
BINARY_SEMAPHORE_LENGTH )
void task1(void *pvParameters)
{
TickType_t nowTick;
nowTick = xTaskGetTickCount();
printf("[%s:%d]
Start\n",pcTaskGetName(0),nowTick);
while(1) {
/* Block to wait for
something to be available from the queues or
semaphore that have been
added to the set. Don't block longer than
5000ms. */
nowTick =
xTaskGetTickCount();
printf("[%s:%d]
Waitting.....\n",pcTaskGetName(0),nowTick);
xActivatedMember =
xQueueSelectFromSet( xQueueSet,
5000 / portTICK_PERIOD_MS );
/* Which set member was
selected? Receives/takes can use a block time
of zero as they are
guaranteed to pass because xQueueSelectFromSet()
would not have returned the
handle unless something was available. */
if( xActivatedMember ==
xQueue1 ) {
xQueueReceive( xActivatedMember, &xReceivedFromQueue1,
0 );
nowTick = xTaskGetTickCount();
printf("[%s:%d]
xReceivedFromQueue1=%ul\n",pcTaskGetName(0),nowTick,xReceivedFromQueue1);
} else if( xActivatedMember
== xQueue2 ) {
xQueueReceive( xActivatedMember, &xReceivedFromQueue2,
0 );
nowTick = xTaskGetTickCount();
printf("[%s:%d]
xReceivedFromQueue2=[%s]\n",pcTaskGetName(0),nowTick,xReceivedFromQueue2);
} else if( xActivatedMember
== xSemaphore ) {
/*
Take the semaphore to make sure it can be "given" again.
*/
xSemaphoreTake( xActivatedMember, 0 );
nowTick = xTaskGetTickCount();
printf("[%s:%d]
xSemaphoreTake\n",pcTaskGetName(0),nowTick);
//break;
} else {
/*
The 2000ms block time expired without an RTOS queue or
semaphore
being ready to process. */
nowTick = xTaskGetTickCount();
printf("[%s] Nothing.....%d\n",pcTaskGetName(0),nowTick);
}
}
}
// Send Queue
// QueueHandle_t xQueue1
void task2(void *pvParameters)
{
uint32_t count = 0;
TickType_t nowTick;
nowTick = xTaskGetTickCount();
printf("[%s:%d]
Start\n",pcTaskGetName(0),nowTick);
while(1) {
vTaskDelay(1000/portTICK_PERIOD_MS);
xQueueSend(xQueue1, &count, 0);
count++;
}
}
// Send Queue
// QueueHandle_t xQueue2
void task3(void *pvParameters)
{
char msg[ITEM_SIZE_QUEUE_2];
TickType_t nowTick;
nowTick = xTaskGetTickCount();
printf("[%s:%d]
Start\n",pcTaskGetName(0),nowTick);
while(1) {
vTaskDelay(2000/portTICK_PERIOD_MS);
nowTick =
xTaskGetTickCount();
snprintf(msg,
ITEM_SIZE_QUEUE_2, "nowTick=%d", nowTick);
xQueueSend(xQueue2, (void *)msg, 0);
}
}
// Give Semaphore
// SemaphoreHandle_t xSemaphore
void task4(void *pvParameters)
{
TickType_t nowTick;
nowTick = xTaskGetTickCount();
printf("[%s:%d]
Start\n",pcTaskGetName(0),nowTick);
while(1) {
vTaskDelay(5000/portTICK_PERIOD_MS);
xSemaphoreGive(
xSemaphore );
}
}
void user_init(void)
{
uart_set_baud(0, 115200);
printf("SDK version:%s\n",
sdk_system_get_sdk_version());
printf("pcTaskGetName=%s\n",pcTaskGetName(0));
/* Create the queue set large enough to
hold an event for every space in
every queue and semaphore that is to be
added to the set. */
xQueueSet = xQueueCreateSet(
COMBINED_LENGTH );
/* Create the queues and semaphores
that will be contained in the set. */
printf("ITEM_SIZE_QUEUE_1=%d
ITEM_SIZE_QUEUE_2=%d\n",ITEM_SIZE_QUEUE_1,ITEM_SIZE_QUEUE_2);
xQueue1 = xQueueCreate( QUEUE_LENGTH_1,
ITEM_SIZE_QUEUE_1 );
xQueue2 = xQueueCreate( QUEUE_LENGTH_2,
ITEM_SIZE_QUEUE_2 );
/* Create the semaphore that is being
added to the set. */
xSemaphore = xSemaphoreCreateBinary();
/* Check everything was created. */
configASSERT( xQueueSet );
configASSERT( xQueue1 );
configASSERT( xQueue2 );
configASSERT( xSemaphore );
/* Add the queues and semaphores to the
set. Reading from these queues and
semaphore can only be performed after a
call to xQueueSelectFromSet() has
returned the queue or semaphore handle
from this point on. */
xQueueAddToSet( xQueue1, xQueueSet );
xQueueAddToSet( xQueue2, xQueueSet );
xQueueAddToSet( xSemaphore, xQueueSet
);
xTaskCreate(task1, "task1", 256, NULL,
2, NULL);
xTaskCreate(task2, "task2", 256, NULL,
2, NULL);
xTaskCreate(task3, "task3", 256, NULL,
2, NULL);
xTaskCreate(task4, "task4", 256, NULL,
2, NULL);
} |