Skip to content
Permalink
Browse files
[QBSS] Add bss queue base on liblfds to remove dependancy on RTOS
  • Loading branch information
darthcloud committed Feb 21, 2021
1 parent 22b27da commit 35eec092d0bffffe587e3da8b2df01bf7ad0e235
Showing with 207 additions and 0 deletions.
  1. +3 −0 .gitmodules
  2. +21 −0 components/queue_bss/CMakeLists.txt
  3. +1 −0 components/queue_bss/liblfds
  4. +5 −0 components/queue_bss/linker.lf
  5. +159 −0 components/queue_bss/queue_bss.c
  6. +18 −0 components/queue_bss/queue_bss.h
@@ -0,0 +1,3 @@
[submodule "components/queue_bss/liblfds"]
path = components/queue_bss/liblfds
url = https://github.com/liblfds/liblfds7.1.1.git
@@ -0,0 +1,21 @@
idf_component_register(SRCS "liblfds/liblfds7.1.1/liblfds711/src/lfds711_queue_bounded_singleproducer_singleconsumer/lfds711_queue_bounded_singleproducer_singleconsumer_cleanup.c"
"liblfds/liblfds7.1.1/liblfds711/src/lfds711_queue_bounded_singleproducer_singleconsumer/lfds711_queue_bounded_singleproducer_singleconsumer_dequeue.c"
"liblfds/liblfds7.1.1/liblfds711/src/lfds711_queue_bounded_singleproducer_singleconsumer/lfds711_queue_bounded_singleproducer_singleconsumer_enqueue.c"
"liblfds/liblfds7.1.1/liblfds711/src/lfds711_queue_bounded_singleproducer_singleconsumer/lfds711_queue_bounded_singleproducer_singleconsumer_init.c"
"liblfds/liblfds7.1.1/liblfds711/src/lfds711_queue_bounded_singleproducer_singleconsumer/lfds711_queue_bounded_singleproducer_singleconsumer_internal.h"
"liblfds/liblfds7.1.1/liblfds711/src/lfds711_queue_bounded_singleproducer_singleconsumer/lfds711_queue_bounded_singleproducer_singleconsumer_query.c"
"queue_bss.c"
INCLUDE_DIRS "."
PRIV_INCLUDE_DIRS "liblfds/liblfds7.1.1/liblfds711/inc"
"liblfds/liblfds7.1.1/liblfds711/src/lfds711_queue_bounded_singleproducer_singleconsumer"
"liblfds/liblfds7.1.1/liblfds711/src/lfds711_freelist"
"liblfds/liblfds7.1.1/liblfds711/src/lfds711_misc"
LDFRAGMENTS linker.lf)

target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-unknown-pragmas"
"-Wno-strict-aliasing"
"-Wno-unused-but-set-variable")

# Should get us what we need without having to fork the lib!
target_compile_options(${COMPONENT_TARGET} PRIVATE "-D__linux__"
"-D__mips__")
Submodule liblfds added at da3494
@@ -0,0 +1,5 @@
[mapping:queue_bss]
archive: libqueue_bss.a
entries:
* (noflash)

@@ -0,0 +1,159 @@
/*
* Copyright (c) 2021, Jacques Gagnon
* SPDX-License-Identifier: Apache-2.0
*/

#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "esp32/rom/ets_sys.h"
#include "liblfds711.h"
#include "queue_bss.h"

struct queue_bss {
struct lfds711_queue_bss_state item_queue_state;
struct lfds711_queue_bss_state item_free_state;
struct lfds711_queue_bss_element *item_queue_element;
struct lfds711_queue_bss_element *item_free_element;
uint8_t *item;
uint32_t *item_len;
uint32_t item_max_len;
uint32_t item_num;
};

queue_bss_handle_t queue_bss_init(uint32_t item_num, uint32_t item_len) {
struct queue_bss *q = calloc(item_num, item_len);
if (NULL == q) {
goto free_q;
}
q->item_queue_element = malloc(sizeof(struct lfds711_queue_bss_element) * item_num);
if (NULL == q->item_queue_element) {
goto free_item_queue_element;
}
q->item_free_element = malloc(sizeof(struct lfds711_queue_bss_element) * item_num);
if (NULL == q->item_free_element) {
goto free_item_free_element;
}
q->item = malloc(sizeof(uint8_t) * item_len * item_num);
if (NULL == q->item) {
goto free_item;
}
q->item_len = malloc(sizeof(uint32_t) * item_num);
if (NULL == q->item_len) {
goto free_item_len;
}
uint8_t *cur_ptr = q->item;
q->item_max_len = item_len;
q->item_num = item_num;

lfds711_queue_bss_init_valid_on_current_logical_core(&q->item_free_state, q->item_free_element, item_num, NULL);

for (uint32_t i = 0; i < (item_num - 1); i++, cur_ptr += item_len) {
q->item_len[i] = 0;
if (!lfds711_queue_bss_enqueue(&q->item_free_state, &q->item_len[i], cur_ptr)) {
ets_printf("queue_bss: Enqueue fail %d\n", i);
goto free_item_free_queue;
}
}

lfds711_queue_bss_init_valid_on_current_logical_core(&q->item_queue_state, q->item_queue_element, item_num, NULL);

return (queue_bss_handle_t)q;

free_item_free_queue:
lfds711_queue_bss_cleanup(&q->item_free_state, NULL);
free_item_len:
free(q->item_len);
free_item:
free(q->item);
free_item_free_element:
free(q->item_free_element);
free_item_queue_element:
free(q->item_queue_element);
free_q:
free(q);
ets_printf("queue_bss: Init fail\n");
return NULL;
}

void queue_bss_init_othercores(void) {
LFDS711_MISC_MAKE_VALID_ON_CURRENT_LOGICAL_CORE_INITS_COMPLETED_BEFORE_NOW_ON_ANY_OTHER_LOGICAL_CORE;
}

int32_t queue_bss_enqueue(queue_bss_handle_t qhdl, uint8_t *item, uint32_t item_len) {
struct queue_bss *q = (struct queue_bss *)qhdl;
uint8_t *qitem;
uint32_t *qitem_len;

if (NULL == q) {
ets_printf("queue_bss: NULL handle\n");
return -1;
}

if (item_len > q->item_max_len) {
ets_printf("queue_bss: Item too large %d, max: %d\n", item_len, q->item_max_len);
return -1;
}

if (!lfds711_queue_bss_dequeue(&q->item_free_state, (void **)&qitem_len, (void **)&qitem)) {
ets_printf("queue_bss: No free slot\n");
return -1;
}
memcpy(qitem, item, item_len);
*qitem_len = item_len;

if (!lfds711_queue_bss_enqueue(&q->item_queue_state, qitem_len, qitem)) {
ets_printf("queue_bss: Queue full!\n");
queue_bss_return(qhdl, qitem, qitem_len);
return -1;
}
return 0;
}

uint8_t *queue_bss_dequeue(queue_bss_handle_t qhdl, uint32_t **item_len) {
struct queue_bss *q = (struct queue_bss *)qhdl;
uint8_t *qitem;
uint32_t *qitem_len;

if (NULL == q || NULL == item_len) {
ets_printf("queue_bss: NULL paramters\n");
return NULL;
}

if (lfds711_queue_bss_dequeue(&q->item_queue_state, (void **)&qitem_len, (void **)&qitem))
{
*item_len = qitem_len;
return qitem;
}
return NULL;
}

int32_t queue_bss_return(queue_bss_handle_t qhdl, uint8_t *item, uint32_t *item_len) {
struct queue_bss *q = (struct queue_bss *)qhdl;

if (NULL == q || NULL == item || NULL == item_len) {
ets_printf("queue_bss: NULL paramters\n");
return -1;
}

if (lfds711_queue_bss_enqueue(&q->item_free_state, item_len, item)) {
return -1;
}
return 0;
}

void queue_bss_deinit(queue_bss_handle_t qhdl) {
struct queue_bss *q = (struct queue_bss *)qhdl;

if (NULL == q) {
ets_printf("queue_bss: NULL handle\n");
return;
}

lfds711_queue_bss_cleanup(&q->item_free_state, NULL);
free(q->item_len);
free(q->item);
free(q->item_free_element);
free(q->item_queue_element);
free(q);
}
@@ -0,0 +1,18 @@
/*
* Copyright (c) 2021, Jacques Gagnon
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef _QUEUE_BSS_H_
#define _QUEUE_BSS_H_

typedef void * queue_bss_handle_t;

queue_bss_handle_t queue_bss_init(uint32_t item_num, uint32_t item_len);
void queue_bss_init_othercores(void);
int32_t queue_bss_enqueue(queue_bss_handle_t qhdl, uint8_t *item, uint32_t item_len);
uint8_t *queue_bss_dequeue(queue_bss_handle_t qhdl, uint32_t **item_len);
int32_t queue_bss_return(queue_bss_handle_t qhdl, uint8_t *item, uint32_t *item_len);
void queue_bss_deinit(queue_bss_handle_t qhdl);

#endif /* _QUEUE_BSS_H_ */

0 comments on commit 35eec09

Please sign in to comment.