mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2026-01-01 01:41:18 +01:00
cpu/esp_common/freertos: add required ringbuffer handling
This commit is contained in:
parent
780fd9a815
commit
c8d8e5d3f5
104
cpu/esp_common/freertos/ringbuf.c
Normal file
104
cpu/esp_common/freertos/ringbuf.c
Normal file
@ -0,0 +1,104 @@
|
||||
/*
|
||||
* Copyright (C) 2022 Gunar Schorcht
|
||||
*
|
||||
* This file is subject to the terms and conditions of the GNU Lesser
|
||||
* General Public License v2.1. See the file LICENSE in the top level
|
||||
* directory for more details.
|
||||
*
|
||||
* FreeRTOS to RIOT-OS adaption module for source code compatibility
|
||||
*/
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "irq_arch.h"
|
||||
#include "ringbuffer.h"
|
||||
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/ringbuf.h"
|
||||
|
||||
#define ENABLE_DEBUG 0
|
||||
#include "debug.h"
|
||||
|
||||
typedef struct {
|
||||
ringbuffer_t rbuf;
|
||||
uint16_t item_size;
|
||||
char *buf;
|
||||
} rbuf_handle_t;
|
||||
|
||||
RingbufHandle_t xRingbufferCreate(size_t xBufferSize, RingbufferType_t xBufferType)
|
||||
{
|
||||
/* only byte buffer supported for now */
|
||||
assert(xBufferType == RINGBUF_TYPE_BYTEBUF);
|
||||
|
||||
/* allocate the space for rbuf_handle_t including the buffer */
|
||||
rbuf_handle_t *handle = malloc(xBufferSize + sizeof(uint16_t) + sizeof(ringbuffer_t));
|
||||
if (handle == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
handle->item_size = 0;
|
||||
ringbuffer_init((ringbuffer_t *)handle, handle->buf, xBufferSize);
|
||||
|
||||
return handle;
|
||||
}
|
||||
|
||||
void vRingbufferDelete(RingbufHandle_t xRingbuffer)
|
||||
{
|
||||
assert(xRingbuffer != NULL);
|
||||
free(xRingbuffer);
|
||||
}
|
||||
|
||||
void *xRingbufferReceiveUpToFromISR(RingbufHandle_t xRingbuffer,
|
||||
size_t *pxItemSize, size_t xMaxSize)
|
||||
{
|
||||
rbuf_handle_t *handle = xRingbuffer;
|
||||
size_t data_len = 0;
|
||||
|
||||
assert(handle != NULL);
|
||||
|
||||
/* determine the number of bytes to be read */
|
||||
if (handle->rbuf.avail) {
|
||||
data_len = ((xMaxSize == 0) || (handle->rbuf.avail < xMaxSize)) ? handle->rbuf.avail
|
||||
: xMaxSize;
|
||||
}
|
||||
/* ESP-IDF ring buffers require two read operation if the data wrap around */
|
||||
if (data_len > (handle->rbuf.size - handle->rbuf.start)) {
|
||||
data_len = handle->rbuf.size - handle->rbuf.start;
|
||||
}
|
||||
handle->item_size = data_len;
|
||||
*pxItemSize = data_len;
|
||||
|
||||
return handle->rbuf.buf + handle->rbuf.start;
|
||||
}
|
||||
|
||||
void *xRingbufferReceiveFromISR(RingbufHandle_t xRingbuffer, size_t *pxItemSize)
|
||||
{
|
||||
return xRingbufferReceiveUpToFromISR(xRingbuffer, pxItemSize, 0);
|
||||
}
|
||||
|
||||
void vRingbufferReturnItemFromISR(RingbufHandle_t xRingbuffer, void *pvItem,
|
||||
BaseType_t *pxHigherPriorityTaskWoken)
|
||||
{
|
||||
rbuf_handle_t *handle = xRingbuffer;
|
||||
if (handle->item_size) {
|
||||
ringbuffer_remove(&handle->rbuf, handle->item_size);
|
||||
}
|
||||
}
|
||||
|
||||
BaseType_t xRingbufferSendFromISR(RingbufHandle_t xRingbuffer,
|
||||
const void *pvItem,
|
||||
size_t xItemSize,
|
||||
BaseType_t *pxHigherPriorityTaskWoken)
|
||||
{
|
||||
rbuf_handle_t *handle = xRingbuffer;
|
||||
|
||||
assert(handle != NULL);
|
||||
|
||||
/* return immediately if there is not enough space in the ring buffer */
|
||||
if (ringbuffer_get_free(&handle->rbuf) < xItemSize) {
|
||||
return pdFALSE;
|
||||
}
|
||||
|
||||
/* add data to the ringbuffer */
|
||||
return ringbuffer_add(&handle->rbuf, pvItem, xItemSize) == xItemSize;
|
||||
}
|
||||
@ -21,6 +21,33 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define RINGBUF_TYPE_BYTEBUF 2
|
||||
|
||||
typedef unsigned RingbufferType_t;
|
||||
typedef void * RingbufHandle_t;
|
||||
|
||||
RingbufHandle_t xRingbufferCreate(size_t xBufferSize, RingbufferType_t xBufferType);
|
||||
|
||||
void vRingbufferDelete(RingbufHandle_t xRingbuffer);
|
||||
|
||||
void *xRingbufferReceiveUpTo(RingbufHandle_t xRingbuffer,
|
||||
size_t *pxItemSize,
|
||||
TickType_t xTicksToWait,
|
||||
size_t xMaxSize);
|
||||
|
||||
BaseType_t xRingbufferSendFromISR(RingbufHandle_t xRingbuffer,
|
||||
const void *pvItem,
|
||||
size_t xItemSize,
|
||||
BaseType_t *pxHigherPriorityTaskWoken);
|
||||
|
||||
void *xRingbufferReceiveUpToFromISR(RingbufHandle_t xRingbuffer,
|
||||
size_t *pxItemSize, size_t xMaxSize);
|
||||
|
||||
void *xRingbufferReceiveFromISR(RingbufHandle_t xRingbuffer, size_t *pxItemSize);
|
||||
|
||||
void vRingbufferReturnItemFromISR(RingbufHandle_t xRingbuffer, void *pvItem,
|
||||
BaseType_t *pxHigherPriorityTaskWoken);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user