/* The example of ESP-IDF
*
* This sample code is in the public domain.
*/
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/ringbuf.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "esp_log.h"
#define No_Split
//#define Allow_Split
TaskHandle_t xTask1 = NULL, xTask2 = NULL;
SemaphoreHandle_t xSemaphore;
RingbufHandle_t xRingbuffer;
void task1(void *pvParameter)
{
TickType_t nowTick;
UBaseType_t res;
char tx_item[64];
size_t freeSize;
UBaseType_t prio =
uxTaskPriorityGet(NULL);
nowTick = xTaskGetTickCount();
printf("[%s:%d] Start
Priority=%d\n",pcTaskGetTaskName(NULL),nowTick,prio);
//Send an item
xSemaphoreTake(xSemaphore,
portMAX_DELAY);
xRingbufferPrintInfo(xRingbuffer);
memset(tx_item,'A',sizeof(tx_item));
res =
xRingbufferSend(xRingbuffer, tx_item, 32,
pdMS_TO_TICKS(1000));
if (res != pdTRUE) {
printf("Failed
to send item\n");
}
nowTick = xTaskGetTickCount();
freeSize =
xRingbufferGetCurFreeSize(xRingbuffer);
printf("[%s:%d]
freeSize=%d\n",pcTaskGetTaskName(NULL),nowTick,freeSize);
xSemaphoreGive(xSemaphore);
vTaskDelay(1);
xSemaphoreTake(xSemaphore,
portMAX_DELAY);
xRingbufferPrintInfo(xRingbuffer);
memset(tx_item,'B',sizeof(tx_item));
res =
xRingbufferSend(xRingbuffer, tx_item, 16,
pdMS_TO_TICKS(1000));
if (res != pdTRUE) {
printf("Failed
to send item\n");
}
nowTick = xTaskGetTickCount();
freeSize =
xRingbufferGetCurFreeSize(xRingbuffer);
printf("[%s:%d]
freeSize=%d\n",pcTaskGetTaskName(NULL),nowTick,freeSize);
xSemaphoreGive(xSemaphore);
vTaskDelay(1);
xSemaphoreTake(xSemaphore,
portMAX_DELAY);
xRingbufferPrintInfo(xRingbuffer);
memset(tx_item,'C',sizeof(tx_item));
res =
xRingbufferSend(xRingbuffer, tx_item, 40,
pdMS_TO_TICKS(1000));
if (res != pdTRUE) {
printf("Failed
to send item\n");
}
nowTick = xTaskGetTickCount();
freeSize =
xRingbufferGetCurFreeSize(xRingbuffer);
printf("[%s:%d]
freeSize=%d\n",pcTaskGetTaskName(NULL),nowTick,freeSize);
xSemaphoreGive(xSemaphore);
vTaskDelay(1);
xSemaphoreTake(xSemaphore,
portMAX_DELAY);
xRingbufferPrintInfo(xRingbuffer);
memset(tx_item,'D',sizeof(tx_item));
res =
xRingbufferSend(xRingbuffer, tx_item, 28,
pdMS_TO_TICKS(1000));
if (res != pdTRUE) {
printf("Failed
to send item\n");
}
nowTick = xTaskGetTickCount();
freeSize =
xRingbufferGetCurFreeSize(xRingbuffer);
printf("[%s:%d]
freeSize=%d\n",pcTaskGetTaskName(NULL),nowTick,freeSize);
xSemaphoreGive(xSemaphore);
vTaskDelay(1);
while(1) {
vTaskDelay(100);
}
}
void task2(void *pvParameter)
{
TickType_t nowTick;
UBaseType_t prio =
uxTaskPriorityGet(NULL);
nowTick = xTaskGetTickCount();
printf("[%s:%d] Start
Priority=%d\n",pcTaskGetTaskName(NULL),nowTick,prio);
#ifdef No_Split
size_t item_size;
while (1) {
xSemaphoreTake(xSemaphore,
portMAX_DELAY);
//Receive an item from
no-split ring buffer
char *item = (char
*)xRingbufferReceive(xRingbuffer, &item_size,
pdMS_TO_TICKS(1000));
//Check received item
if (item != NULL) {
//Print item
nowTick =
xTaskGetTickCount();
printf("[%s:%d]
item_size=%d:",pcTaskGetTaskName(0),nowTick,item_size);
for (int i = 0;
i < item_size; i++) {
printf("%c", item[i]);
}
printf("\n");
//Return Item
vRingbufferReturnItem(xRingbuffer, (void *)item);
#if 0
} else {
//Failed to
receive item
printf("Failed
to receive item\n");
#endif
}
xSemaphoreGive(xSemaphore);
// nowTick =
xTaskGetTickCount();
// printf("[%s:%d]
Give\n",pcTaskGetTaskName(NULL),nowTick);
}
#endif
#ifdef Allow_Split
//Receive an item from allow-split ring
buffer
size_t item_size1, item_size2;
char *item1, *item2;
while (1) {
xSemaphoreTake(xSemaphore,
portMAX_DELAY);
//Receive an item from
allow-split ring buffer
BaseType_t ret =
xRingbufferReceiveSplit(xRingbuffer, (void **)&item1,
(void **)&item2, &item_size1, &item_size2,
pdMS_TO_TICKS(1000));
//Check received item
if (ret == pdTRUE
&& item1 != NULL) {
nowTick =
xTaskGetTickCount();
printf("[%s:%d]
item_size1=%d:",pcTaskGetTaskName(0),nowTick,item_size1);
for (int i = 0;
i < item_size1; i++) {
printf("%c", item1[i]);
}
printf("\n");
vRingbufferReturnItem(xRingbuffer, (void *)item1);
//Check if item
was split
if (item2 !=
NULL) {
printf("[%s:%d]
item_size2=%d:",pcTaskGetTaskName(0),nowTick,item_size2);
for (int i = 0; i < item_size2; i++) {
printf("%c", item2[i]);
}
printf("\n");
vRingbufferReturnItem(xRingbuffer, (void *)item2);
}
#if 0
} else {
//Failed to
receive item
printf("Failed
to receive item\n");
#endif
}
xSemaphoreGive(xSemaphore);
}
#endif
}
void app_main()
{
TickType_t nowTick;
vTaskDelay(2000/portTICK_PERIOD_MS);
UBaseType_t prio =
uxTaskPriorityGet(NULL);
nowTick = xTaskGetTickCount();
printf("[%s:%d] Start
Priority=%d\n",pcTaskGetTaskName(NULL),nowTick,prio);
printf("[%s:%d]
portTICK_PERIOD_MS=%d\n",pcTaskGetTaskName(NULL),nowTick,portTICK_PERIOD_MS);
/* Create Mutex */
xSemaphore = xSemaphoreCreateMutex();
/* Create Ring Buffer */
#ifdef No_Split
xRingbuffer = xRingbufferCreate(128,
RINGBUF_TYPE_NOSPLIT);
#endif
#ifdef Allow_Split
xRingbuffer = xRingbufferCreate(128,
RINGBUF_TYPE_ALLOWSPLIT);
#endif
/* Check everything was created. */
configASSERT( xSemaphore );
configASSERT( xRingbuffer );
/* Create task */
xTaskCreatePinnedToCore(task1, "Task1",
4096, NULL, 1, &xTask1, tskNO_AFFINITY);
xTaskCreatePinnedToCore(task2, "Task2",
4096, NULL, 1, &xTask2, tskNO_AFFINITY);
}
|