add cherryrb and cherry pool

This commit is contained in:
sakumisu 2024-04-07 23:30:51 +08:00
parent 1eef5f3e35
commit 725d635c76
12 changed files with 1323 additions and 3 deletions

View File

@ -127,6 +127,11 @@ if(CONFIG_CHERRYUSB_HOST)
endif()
if(CONFIG_CHERRYUSB_HOST_VIDEO)
list(APPEND cherryusb_srcs ${CMAKE_CURRENT_LIST_DIR}/class/video/usbh_video.c)
list(APPEND cherryusb_srcs ${CMAKE_CURRENT_LIST_DIR}/class/video/usbh_uvc_queue.c)
list(APPEND cherryusb_srcs ${CMAKE_CURRENT_LIST_DIR}/third_party/cherryrb/chry_ringbuffer.c)
list(APPEND cherryusb_srcs ${CMAKE_CURRENT_LIST_DIR}/third_party/cherrypool/chry_pool.c)
list(APPEND cherryusb_incs ${CMAKE_CURRENT_LIST_DIR}/third_party/cherryrb)
list(APPEND cherryusb_incs ${CMAKE_CURRENT_LIST_DIR}/third_party/cherrypool)
endif()
if(CONFIG_CHERRYUSB_HOST_AUDIO)
list(APPEND cherryusb_srcs ${CMAKE_CURRENT_LIST_DIR}/class/audio/usbh_audio.c)

View File

@ -0,0 +1,76 @@
#include "usbh_uvc_queue.h"
#include "chry_pool.h"
struct chry_pool usbh_uvc_pool;
uint32_t usbh_uvc_pool_buf[10];
int usbh_uvc_frame_create(struct usbh_videoframe *frame, uint32_t count)
{
return chry_pool_create(&usbh_uvc_pool, usbh_uvc_pool_buf, frame, sizeof(struct usbh_videoframe), count);
}
struct usbh_videoframe *usbh_uvc_frame_alloc(void)
{
return (struct usbh_videoframe *)chry_pool_item_alloc(&usbh_uvc_pool);
}
int usbh_uvc_frame_free(struct usbh_videoframe *frame)
{
return chry_pool_item_free(&usbh_uvc_pool, (uintptr_t *)frame);
}
int usbh_uvc_frame_send(struct usbh_videoframe *frame)
{
return chry_pool_item_send(&usbh_uvc_pool, (uintptr_t *)frame);
}
int usbh_uvc_frame_recv(struct usbh_videoframe **frame, uint32_t timeout)
{
return chry_pool_item_recv(&usbh_uvc_pool, (uintptr_t **)frame, timeout);
}
uint8_t frame_buffer1[1]; /* just for test */
uint8_t frame_buffer2[1]; /* just for test */
struct usbh_videoframe frame_pool[2];
void usbh_uvc_frame_init(void)
{
frame_pool[0].frame_buf = frame_buffer1;
frame_pool[0].frame_bufsize = 640 * 480 * 2;
frame_pool[1].frame_buf = frame_buffer2;
frame_pool[1].frame_bufsize = 640 * 480 * 2;
usbh_uvc_frame_create(frame_pool, 2);
}
static void usbh_frame_thread(void *argument)
{
int ret;
struct usbh_videoframe *frame;
while (1) {
ret = usbh_uvc_frame_recv(&frame, 0xfffffff);
if (ret < 0) {
continue;
}
USB_LOG_RAW("frame buf:%p,frame len:%d\r\n", frame->frame_buf, frame->frame_size);
usbh_uvc_frame_free(frame);
}
}
void usbh_uvc_frame_alloc_send_test(void)
{
struct usbh_videoframe *frame = NULL;
frame = usbh_uvc_frame_alloc();
if (frame) {
frame->frame_size = 640 * 480 * 2;
usbh_uvc_frame_send(frame);
}
}
void usbh_uvc_frame_test(void)
{
usbh_uvc_frame_init();
usb_osal_thread_create("usbh_video", 3072, 5, usbh_frame_thread, NULL);
}

View File

@ -0,0 +1,18 @@
#ifndef USBH_UVC_QUEUE_H
#define USBH_UVC_QUEUE_H
#include "chry_pool.h"
#include "usbh_core.h"
#include "usbh_video.h"
int usbh_uvc_frame_create(struct usbh_videoframe *frame, uint32_t count);
struct usbh_videoframe *usbh_uvc_frame_alloc(void);
int usbh_uvc_frame_free(struct usbh_videoframe *frame);
int usbh_uvc_frame_send(struct usbh_videoframe *frame);
int usbh_uvc_frame_recv(struct usbh_videoframe **frame, uint32_t timeout);
/* test uvc frame */
void usbh_uvc_frame_alloc_send_test(void);
void usbh_uvc_frame_test(void);
#endif

View File

@ -22,12 +22,19 @@ struct usbh_video_format {
uint8_t num_of_frames;
};
struct usbh_videoframe {
uint8_t *frame_buf;
uint32_t frame_bufsize;
uint32_t frame_format;
uint32_t frame_size;
};
struct usbh_videostreaming {
struct usbh_videoframe *frame;
uint32_t frame_format;
uint32_t bufoffset;
uint32_t buflen;
uint16_t width;
uint16_t heigth;
void (*video_one_frame_callback)(struct usbh_videostreaming *stream);
uint16_t height;
};
struct usbh_video {

49
third_party/cherrypool/chry_pool.c vendored Normal file
View File

@ -0,0 +1,49 @@
#include "chry_pool.h"
int chry_pool_create(struct chry_pool *pool, uint32_t *ringbuf, void *item, uint32_t item_size, uint32_t item_count)
{
uintptr_t addr;
chry_ringbuffer_init(&pool->rb, ringbuf, sizeof(uintptr_t) * item_count);
for (uint32_t i = 0; i < item_count; i++) {
addr = ((uintptr_t)item + i * item_size);
chry_ringbuffer_write(&pool->rb, &addr, sizeof(uintptr_t));
}
pool->mq = usb_osal_mq_create(item_count);
if (pool->mq == NULL) {
return -1;
}
return 0;
}
uintptr_t *chry_pool_item_alloc(struct chry_pool *pool)
{
uintptr_t *addr;
bool ret;
ret = chry_ringbuffer_read(&pool->rb, (uintptr_t *)&addr, sizeof(uintptr_t));
if (ret == false) {
return NULL;
} else {
return addr;
}
}
int chry_pool_item_free(struct chry_pool *pool, uintptr_t *item)
{
uintptr_t addr;
addr = (uintptr_t)item;
return chry_ringbuffer_write(&pool->rb, &addr, sizeof(uintptr_t));
}
int chry_pool_item_send(struct chry_pool *pool, uintptr_t *frame)
{
return usb_osal_mq_send(pool->mq, (uintptr_t)frame);
}
int chry_pool_item_recv(struct chry_pool *pool, uintptr_t **frame, uint32_t timeout)
{
return usb_osal_mq_recv(pool->mq, (uintptr_t *)frame, timeout);
}

19
third_party/cherrypool/chry_pool.h vendored Normal file
View File

@ -0,0 +1,19 @@
#ifndef CHRY_POOL_H
#define CHRY_POOL_H
#include "usb_config.h"
#include "usb_osal.h"
#include "chry_ringbuffer.h"
struct chry_pool {
chry_ringbuffer_t rb;
usb_osal_mq_t mq;
};
int chry_pool_create(struct chry_pool *pool, uint32_t *ringbuf, void *item, uint32_t item_size, uint32_t item_count);
uintptr_t *chry_pool_item_alloc(struct chry_pool *pool);
int chry_pool_item_free(struct chry_pool *pool, uintptr_t *item);
int chry_pool_item_send(struct chry_pool *pool, uintptr_t *frame);
int chry_pool_item_recv(struct chry_pool *pool, uintptr_t **frame, uint32_t timeout);
#endif

9
third_party/cherryrb/CHANGELOG.md vendored Normal file
View File

@ -0,0 +1,9 @@
# Change Log
## [1.0.0] - 2023-06-28:
All changes
### Added
- first commit
- add linear r/w setup done api, for dma use

201
third_party/cherryrb/LICENSE vendored Normal file
View File

@ -0,0 +1,201 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

206
third_party/cherryrb/README.md vendored Normal file
View File

@ -0,0 +1,206 @@
# CherryRingBuffer
[中文版](./README_zh.md)
CherryRingBuffer is an efficient and easy-to-use ringbuffer that is based on the same principle as kfifo.
## Brief
### 1.Create and initialize a RingBuffer
```c
chry_ringbuffer_t rb;
uint8_t mempool[1024];
int main(void)
{
/**
* Note that the third parameter of the init function is
* the size of the memory pool (in bytes)
* It is also the depth of the ringbuffer,
* which must be a power of 2 !!!
* Example: 4, 16, 32, 64, 128, 1024, 8192, 65536, etc.
*/
if(0==chry_ringbuffer_init(&rb, mempool, 1024)){
printf("success\r\n");
}else{
printf("error\r\n");
}
return 0;
}
```
### 2.Lock-free use of the producer-consumer model
The read and write APIs, except for overwrite, do not require locking if they satisfy the requirement that the ringbuffer is read in only one thread and written in only one thread, because the read and write operations only operate on the read or write pointers separately.
```c
void thread_producer(void* param)
{
char *data = "hello world";
while(1){
uint32_t len = chry_ringbuffer_write(&rb, data, 11);
if(11 == len){
printf("[P] write success\r\n");
}else{
printf("[P] write faild, only write %d byte\r\n", len);
}
vTaskDelay(100);
}
}
void thread_consumer(void* param)
{
char data[1024];
while(1){
uint32_t len =chry_ringbuffer_read(&rb, data, 11);
if (len){
printf("[C] read success, read %d byte\r\n",len);
data[11]='\0';
printf("%s\r\n",data);
}else{
printf("[C] read faild, no data in ringbuffer\r\n");
}
vTaskDelay(100);
}
}
```
### 3. API Introduction
```c
/**
* Used to clear ringbuffer, while operating read and write pointers,
* multi-threaded need to add lock
*/
chry_ringbuffer_reset(&rb);
/**
* Used to clear ringbuffer, only operates on read pointers,
* called without locking in the only read thread
*/
chry_ringbuffer_reset_read(&rb);
/**
* Get the total ringbuffer size (bytes)
*/
uint32_t size = chry_ringbuffer_get_size(&rb);
/**
* Get ringbuffer used size (bytes)
*/
uint32_t used = chry_ringbuffer_get_used(&rb);
/**
* Get ringbuffer free size (bytes)
*/
uint32_t free = chry_ringbuffer_get_free(&rb);
/**
* Check if ringbuffer is full, full returns true
*/
bool is_full = chry_ringbuffer_check_full(&rb);
/**
* Check if ringbuffer is empty, empty returns true
*/
bool is_empty = chry_ringbuffer_check_empty(&rb);
uint8_t data = 0x55;
/**
* Write a byte
* No locking on the only write thread call
* Success returns true, full returns false
*/
chry_ringbuffer_write_byte(&rb, data);
/**
* overwriting write one byte,
* when ringbuffer is full will overwrite the earliest data
* The non-full case is the same as chry_ringbuffer_write_byte
* This API may operate on read and write pointers at the same time,
* so multiple threads need to add locks
* Always return true
*/
chry_ringbuffer_overwrite_byte(&rb, data);
/**
* Peek one byte from ringbuffer
* The data is still retained in the ringbuffer
* No locking on the only read thread call
* Success returns true, empty returns false
*/
chry_ringbuffer_peek_byte(&rb, &data);
/**
* Read one byte from ringbuffer
* Data retrieval from ringbuffer
* No locking on the only read thread call
* Success returns true, empty returns false
*/
chry_ringbuffer_read_byte(&rb, &data);
/**
* Drop one byte from ringbuffer
* Data retrieval from ringbuffer
* No locking on the only read thread call
* Success returns true, empty returns false
*/
chry_ringbuffer_drop_byte(&rb);
/**
* Write multi-byte, otherwise same as single byte write
* Returns the actual length of the write
*/
chry_ringbuffer_write(&rb, data, 1);
/**
* Overwrite multi-byte, otherwise same as single byte overwrite
* Returns the actual length of the write
*/
chry_ringbuffer_overwrite(&rb, data, 1);
/**
* Peek multiple bytes, otherwise same as single byte peek
* Returns the length of the actual peek
*/
chry_ringbuffer_peek(&rb, data, 1);
/**
* Read multiple bytes, otherwise same as single byte read
* Returns the length of the actual read
*/
chry_ringbuffer_read(&rb, data, 1);
/**
* Drop multiple bytes, otherwise same as single byte Drop
* Returns the length of the actual drop
*/
chry_ringbuffer_drop(&rb, 1);
void *pdata;
uint32_t size;
/**
* For dma start, get read memory address and max linear read size
*/
pdata = chry_ringbuffer_linear_read_setup(&rb, &size);
/**
* For dma done, add read pointer
* Returns the length of the actual add
*/
size = chry_ringbuffer_linear_read_done(&rb, 512);
/**
* For dma start, get write memory address and max linear write size
*/
pdata = chry_ringbuffer_linear_write_setup(&rb, &size);
/**
* For dma done, add write pointer
* Returns the length of the actual add
*/
size = chry_ringbuffer_linear_write_done(&rb, 512);
```

200
third_party/cherryrb/README_zh.md vendored Normal file
View File

@ -0,0 +1,200 @@
# CherryRingBuffer
[English](./README.md)
CherryRingBuffer 是一个的高效、易用的环形缓冲区其原理与kfifo一致。
## 简介
### 1.创建并初始化一个RingBuffer
```c
chry_ringbuffer_t rb;
uint8_t mempool[1024];
int main(void)
{
/**
* 需要注意的点是init 函数第三个参数是内存池的大小(字节为单位)
* 也是ringbuffer的深度必须为 2 的幂次!!!。
* 例如 4、16、32、64、128、1024、8192、65536等
*/
if(0 == chry_ringbuffer_init(&rb, mempool, 1024)){
printf("success\r\n");
}else{
printf("error\r\n");
}
return 0;
}
```
### 2.生产者消费者模型的无锁使用
读和写的API除了overwrite外如果满足ringbuffer只在一个线程里进行读并且只在一个线程里面写那么无须加锁因为读和写操作只单独操作读或者写指针。
```c
void thread_producer(void* param)
{
char *data = "hello world";
while(1){
uint32_t len = chry_ringbuffer_write(&rb, data, 11);
if(11 == len){
printf("[P] write success\r\n");
}else{
printf("[P] write faild, only write %d byte\r\n", len);
}
vTaskDelay(100);
}
}
void thread_consumer(void* param)
{
char data[1024];
while(1){
uint32_t len =chry_ringbuffer_read(&rb, data, 11);
if (len){
printf("[C] read success, read %d byte\r\n",len);
data[11]='\0';
printf("%s\r\n",data);
}else{
printf("[C] read faild, no data in ringbuffer\r\n");
}
vTaskDelay(100);
}
}
```
### 3. API简介
```c
/**
* 用于清空,同时操作读写指针,多线程需要加锁
*/
chry_ringbuffer_reset(&rb);
/**
* 用于清空,只操作读指针,在唯一的读线程调用无须加锁
*/
chry_ringbuffer_reset_read(&rb);
/**
* 获取ringbuffer总大小字节
*/
uint32_t size = chry_ringbuffer_get_size(&rb);
/**
* 获取ringbuffer使用大小字节
*/
uint32_t used = chry_ringbuffer_get_used(&rb);
/**
* 获取ringbuffer空闲大小字节
*/
uint32_t free = chry_ringbuffer_get_free(&rb);
/**
* 检查ringbuffer是否为满满返回true
*/
bool is_full = chry_ringbuffer_check_full(&rb);
/**
* 检查ringbuffer是否为空空返回true
*/
bool is_empty = chry_ringbuffer_check_empty(&rb);
uint8_t data = 0x55;
/**
* 写一字节
* 在唯一的写线程调用无须加锁
* 写成功返回true满返回false
*/
chry_ringbuffer_write_byte(&rb, data);
/**
* 覆盖写一字节当ringbuffer满的时候会覆盖最早的数据
* 非满情况与 chry_ringbuffer_write_byte 一致。
* 此API可能会同时操作读写指针所以多线程需要加锁
* 始终返回true
*/
chry_ringbuffer_overwrite_byte(&rb, data);
/**
* 从ringbuffer中复制出来一字节
* 数据仍然保留在ringbuffer中
* 在唯一的读线程调用无须加锁
* 成功返回true空返回false
*/
chry_ringbuffer_peek_byte(&rb, &data);
/**
* 从ringbuffer中读取出来一字节
* 数据从ringbuffer中取出
* 在唯一的读线程调用无须加锁
* 成功返回true空返回false
*/
chry_ringbuffer_read_byte(&rb, &data);
/**
* 丢弃ringbuffer中一字节
* 数据从ringbuffer中取出
* 在唯一的读线程调用无须加锁
* 成功返回true空返回false
*/
chry_ringbuffer_drop_byte(&rb);
/**
* 写多字节,其他与单字节写相同
* 返回实际写入的长度
*/
chry_ringbuffer_write(&rb, data, 1);
/**
* 覆盖写多字节,其他与单字节覆盖写相同
* 返回实际写入的长度
*/
chry_ringbuffer_overwrite(&rb, data, 1);
/**
* 复制多字节,其他与单字节复制相同
* 返回实际复制的长度
*/
chry_ringbuffer_peek(&rb, data, 1);
/**
* 读取多字节,其他与单字节读取相同
* 返回实际读取的长度
*/
chry_ringbuffer_read(&rb, data, 1);
/**
* 丢弃多字节,其他与单字节丢弃相同
* 返回实际丢弃的长度
*/
chry_ringbuffer_drop(&rb, 1);
void *pdata;
uint32_t size;
/**
* 用于启动DMA获取读取起始内存地址和最大线性可读取长度
*/
pdata = chry_ringbuffer_linear_read_setup(&rb, &size);
/**
* 用于DMA完成增加读指针
* 返回实际增加长度
*/
size = chry_ringbuffer_linear_read_done(&rb, 512);
/**
* 用于启动DMA获取写入起始内存地址和最大线性可写入长度
*/
pdata = chry_ringbuffer_linear_write_setup(&rb, &size);
/**
* 用于DMA完成增加写指针
* 返回实际增加长度
*/
size = chry_ringbuffer_linear_write_done(&rb, 512);
```

474
third_party/cherryrb/chry_ringbuffer.c vendored Normal file
View File

@ -0,0 +1,474 @@
/*
* Copyright (c) 2022, Egahp
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <string.h>
#include "chry_ringbuffer.h"
/*****************************************************************************
* @brief init ringbuffer
*
* @param[in] rb ringbuffer instance
* @param[in] pool memory pool address
* @param[in] size memory size in byte,
* must be power of 2 !!!
*
* @retval int 0:Success -1:Error
*****************************************************************************/
int chry_ringbuffer_init(chry_ringbuffer_t *rb, void *pool, uint32_t size)
{
if (NULL == rb) {
return -1;
}
if (NULL == pool) {
return -1;
}
if ((size < 2) || (size & (size - 1))) {
return -1;
}
rb->in = 0;
rb->out = 0;
rb->mask = size - 1;
rb->pool = pool;
return 0;
}
/*****************************************************************************
* @brief reset ringbuffer, clean all data,
* should be add lock in multithread
*
* @param[in] rb ringbuffer instance
*
*****************************************************************************/
void chry_ringbuffer_reset(chry_ringbuffer_t *rb)
{
rb->in = 0;
rb->out = 0;
}
/*****************************************************************************
* @brief reset ringbuffer, clean all data,
* should be add lock in multithread,
* in single read thread not need lock
*
* @param[in] rb ringbuffer instance
*
*****************************************************************************/
void chry_ringbuffer_reset_read(chry_ringbuffer_t *rb)
{
rb->out = rb->in;
}
/*****************************************************************************
* @brief get ringbuffer total size in byte
*
* @param[in] rb ringbuffer instance
*
* @retval uint32_t total size in byte
*****************************************************************************/
uint32_t chry_ringbuffer_get_size(chry_ringbuffer_t *rb)
{
return rb->mask + 1;
}
/*****************************************************************************
* @brief get ringbuffer used size in byte
*
* @param[in] rb ringbuffer instance
*
* @retval uint32_t used size in byte
*****************************************************************************/
uint32_t chry_ringbuffer_get_used(chry_ringbuffer_t *rb)
{
return rb->in - rb->out;
}
/*****************************************************************************
* @brief get ringbuffer free size in byte
*
* @param[in] rb ringbuffer instance
*
* @retval uint32_t free size in byte
*****************************************************************************/
uint32_t chry_ringbuffer_get_free(chry_ringbuffer_t *rb)
{
return (rb->mask + 1) - (rb->in - rb->out);
}
/*****************************************************************************
* @brief check if ringbuffer is full
*
* @param[in] rb ringbuffer instance
*
* @retval true full
* @retval false not full
*****************************************************************************/
bool chry_ringbuffer_check_full(chry_ringbuffer_t *rb)
{
return chry_ringbuffer_get_used(rb) > rb->mask;
}
/*****************************************************************************
* @brief check if ringbuffer is empty
*
* @param[in] rb ringbuffer instance
*
* @retval true empty
* @retval false not empty
*****************************************************************************/
bool chry_ringbuffer_check_empty(chry_ringbuffer_t *rb)
{
return rb->in == rb->out;
}
/*****************************************************************************
* @brief write one byte to ringbuffer,
* should be add lock in multithread,
* in single write thread not need lock
*
* @param[in] rb ringbuffer instance
* @param[in] byte data
*
* @retval true Success
* @retval false ringbuffer is full
*****************************************************************************/
bool chry_ringbuffer_write_byte(chry_ringbuffer_t *rb, uint8_t byte)
{
if (chry_ringbuffer_check_full(rb)) {
return false;
}
((uint8_t *)(rb->pool))[rb->in & rb->mask] = byte;
rb->in++;
return true;
}
/*****************************************************************************
* @brief overwrite one byte to ringbuffer, drop oldest data,
* should be add lock always
*
* @param[in] rb ringbuffer instance
* @param[in] byte data
*
* @retval true Success
* @retval false always return true
*****************************************************************************/
bool chry_ringbuffer_overwrite_byte(chry_ringbuffer_t *rb, uint8_t byte)
{
if (chry_ringbuffer_check_full(rb)) {
rb->out++;
}
((uint8_t *)(rb->pool))[rb->in & rb->mask] = byte;
rb->in++;
return true;
}
/*****************************************************************************
* @brief peek one byte from ringbuffer,
* should be add lock in multithread,
* in single read thread not need lock
*
* @param[in] rb ringbuffer instance
* @param[in] byte pointer to save data
*
* @retval true Success
* @retval false ringbuffer is empty
*****************************************************************************/
bool chry_ringbuffer_peek_byte(chry_ringbuffer_t *rb, uint8_t *byte)
{
if (chry_ringbuffer_check_empty(rb)) {
return false;
}
*byte = ((uint8_t *)(rb->pool))[rb->out & rb->mask];
return true;
}
/*****************************************************************************
* @brief read one byte from ringbuffer,
* should be add lock in multithread,
* in single read thread not need lock
*
* @param[in] rb ringbuffer instance
* @param[in] byte pointer to save data
*
* @retval true Success
* @retval false ringbuffer is empty
*****************************************************************************/
bool chry_ringbuffer_read_byte(chry_ringbuffer_t *rb, uint8_t *byte)
{
bool ret;
ret = chry_ringbuffer_peek_byte(rb, byte);
rb->out += ret;
return ret;
}
/*****************************************************************************
* @brief drop one byte from ringbuffer,
* should be add lock in multithread,
* in single read thread not need lock
*
* @param[in] rb ringbuffer instance
*
* @retval true Success
* @retval false ringbuffer is empty
*****************************************************************************/
bool chry_ringbuffer_drop_byte(chry_ringbuffer_t *rb)
{
if (chry_ringbuffer_check_empty(rb)) {
return false;
}
rb->out += 1;
return true;
}
/*****************************************************************************
* @brief write data to ringbuffer,
* should be add lock in multithread,
* in single write thread not need lock
*
* @param[in] rb ringbuffer instance
* @param[in] data data pointer
* @param[in] size size in byte
*
* @retval uint32_t actual write size in byte
*****************************************************************************/
uint32_t chry_ringbuffer_write(chry_ringbuffer_t *rb, void *data, uint32_t size)
{
uint32_t unused;
uint32_t offset;
uint32_t remain;
unused = (rb->mask + 1) - (rb->in - rb->out);
if (size > unused) {
size = unused;
}
offset = rb->in & rb->mask;
remain = rb->mask + 1 - offset;
remain = remain > size ? size : remain;
memcpy(((uint8_t *)(rb->pool)) + offset, data, remain);
memcpy(rb->pool, (uint8_t *)data + remain, size - remain);
rb->in += size;
return size;
}
/*****************************************************************************
* @brief write data to ringbuffer,
* should be add lock always
*
* @param[in] rb ringbuffer instance
* @param[in] data data pointer
* @param[in] size size in byte
*
* @retval uint32_t actual write size in byte
*****************************************************************************/
uint32_t chry_ringbuffer_overwrite(chry_ringbuffer_t *rb, void *data, uint32_t size)
{
uint32_t unused;
uint32_t offset;
uint32_t remain;
unused = (rb->mask + 1) - (rb->in - rb->out);
if (size > unused) {
if (size > (rb->mask + 1)) {
size = rb->mask + 1;
}
rb->out += size - unused;
}
offset = rb->in & rb->mask;
remain = rb->mask + 1 - offset;
remain = remain > size ? size : remain;
memcpy(((uint8_t *)(rb->pool)) + offset, data, remain);
memcpy(rb->pool, (uint8_t *)data + remain, size - remain);
rb->in += size;
return size;
}
/*****************************************************************************
* @brief peek data from ringbuffer
* should be add lock in multithread,
* in single read thread not need lock
*
* @param[in] rb ringbuffer instance
* @param[in] data data pointer
* @param[in] size size in byte
*
* @retval uint32_t actual peek size in byte
*****************************************************************************/
uint32_t chry_ringbuffer_peek(chry_ringbuffer_t *rb, void *data, uint32_t size)
{
uint32_t used;
uint32_t offset;
uint32_t remain;
used = rb->in - rb->out;
if (size > used) {
size = used;
}
offset = rb->out & rb->mask;
remain = rb->mask + 1 - offset;
remain = remain > size ? size : remain;
memcpy(data, ((uint8_t *)(rb->pool)) + offset, remain);
memcpy((uint8_t *)data + remain, rb->pool, size - remain);
return size;
}
/*****************************************************************************
* @brief read data from ringbuffer
* should be add lock in multithread,
* in single read thread not need lock
*
* @param[in] rb ringbuffer instance
* @param[in] data data pointer
* @param[in] size size in byte
*
* @retval uint32_t actual read size in byte
*****************************************************************************/
uint32_t chry_ringbuffer_read(chry_ringbuffer_t *rb, void *data, uint32_t size)
{
size = chry_ringbuffer_peek(rb, data, size);
rb->out += size;
return size;
}
/*****************************************************************************
* @brief drop data from ringbuffer
* should be add lock in multithread,
* in single read thread not need lock
*
* @param[in] rb ringbuffer instance
* @param[in] size size in byte
*
* @retval uint32_t actual drop size in byte
*****************************************************************************/
uint32_t chry_ringbuffer_drop(chry_ringbuffer_t *rb, uint32_t size)
{
uint32_t used;
used = rb->in - rb->out;
if (size > used) {
size = used;
}
rb->out += size;
return size;
}
/*****************************************************************************
* @brief linear write setup, get write pointer and max linear size.
*
* @param[in] rb ringbuffer instance
* @param[in] size pointer to store max linear size in byte
*
* @retval void* write memory pointer
*****************************************************************************/
void *chry_ringbuffer_linear_write_setup(chry_ringbuffer_t *rb, uint32_t *size)
{
uint32_t unused;
uint32_t offset;
uint32_t remain;
unused = (rb->mask + 1) - (rb->in - rb->out);
offset = rb->in & rb->mask;
remain = rb->mask + 1 - offset;
remain = remain > unused ? unused : remain;
if (remain) {
*size = remain;
return ((uint8_t *)(rb->pool)) + offset;
} else {
*size = unused - remain;
return rb->pool;
}
}
/*****************************************************************************
* @brief linear read setup, get read pointer and max linear size.
*
* @param[in] rb ringbuffer instance
* @param[in] size pointer to store max linear size in byte
*
* @retval void*
*****************************************************************************/
void *chry_ringbuffer_linear_read_setup(chry_ringbuffer_t *rb, uint32_t *size)
{
uint32_t used;
uint32_t offset;
uint32_t remain;
used = rb->in - rb->out;
offset = rb->out & rb->mask;
remain = rb->mask + 1 - offset;
remain = remain > used ? used : remain;
if (remain) {
*size = remain;
return ((uint8_t *)(rb->pool)) + offset;
} else {
*size = used - remain;
return rb->pool;
}
}
/*****************************************************************************
* @brief linear write done, add write pointer only
*
* @param[in] rb ringbuffer instance
* @param[in] size write size in byte
*
* @retval uint32_t actual write size in byte
*****************************************************************************/
uint32_t chry_ringbuffer_linear_write_done(chry_ringbuffer_t *rb, uint32_t size)
{
uint32_t unused;
unused = (rb->mask + 1) - (rb->in - rb->out);
if (size > unused) {
size = unused;
}
rb->in += size;
return size;
}
/*****************************************************************************
* @brief linear read done, add read pointer only
*
* @param[in] rb ringbuffer instance
* @param[in] size read size in byte
*
* @retval uint32_t actual read size in byte
*****************************************************************************/
uint32_t chry_ringbuffer_linear_read_done(chry_ringbuffer_t *rb, uint32_t size)
{
return chry_ringbuffer_drop(rb, size);
}

56
third_party/cherryrb/chry_ringbuffer.h vendored Normal file
View File

@ -0,0 +1,56 @@
/*
* Copyright (c) 2022, Egahp
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef CHRY_RINGBUFFER_H
#define CHRY_RINGBUFFER_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#include <stdbool.h>
typedef struct {
uint32_t in; /*!< Define the write pointer. */
uint32_t out; /*!< Define the read pointer. */
uint32_t mask; /*!< Define the write and read pointer mask. */
void *pool; /*!< Define the memory pointer. */
} chry_ringbuffer_t;
extern int chry_ringbuffer_init(chry_ringbuffer_t *rb, void *pool, uint32_t size);
extern void chry_ringbuffer_reset(chry_ringbuffer_t *rb);
extern void chry_ringbuffer_reset_read(chry_ringbuffer_t *rb);
extern uint32_t chry_ringbuffer_get_size(chry_ringbuffer_t *rb);
extern uint32_t chry_ringbuffer_get_used(chry_ringbuffer_t *rb);
extern uint32_t chry_ringbuffer_get_free(chry_ringbuffer_t *rb);
extern bool chry_ringbuffer_check_full(chry_ringbuffer_t *rb);
extern bool chry_ringbuffer_check_empty(chry_ringbuffer_t *rb);
extern bool chry_ringbuffer_write_byte(chry_ringbuffer_t *rb, uint8_t byte);
extern bool chry_ringbuffer_overwrite_byte(chry_ringbuffer_t *rb, uint8_t byte);
extern bool chry_ringbuffer_peek_byte(chry_ringbuffer_t *rb, uint8_t *byte);
extern bool chry_ringbuffer_read_byte(chry_ringbuffer_t *rb, uint8_t *byte);
extern bool chry_ringbuffer_drop_byte(chry_ringbuffer_t *rb);
extern uint32_t chry_ringbuffer_write(chry_ringbuffer_t *rb, void *data, uint32_t size);
extern uint32_t chry_ringbuffer_overwrite(chry_ringbuffer_t *rb, void *data, uint32_t size);
extern uint32_t chry_ringbuffer_peek(chry_ringbuffer_t *rb, void *data, uint32_t size);
extern uint32_t chry_ringbuffer_read(chry_ringbuffer_t *rb, void *data, uint32_t size);
extern uint32_t chry_ringbuffer_drop(chry_ringbuffer_t *rb, uint32_t size);
extern void *chry_ringbuffer_linear_write_setup(chry_ringbuffer_t *rb, uint32_t *size);
extern void *chry_ringbuffer_linear_read_setup(chry_ringbuffer_t *rb, uint32_t *size);
extern uint32_t chry_ringbuffer_linear_write_done(chry_ringbuffer_t *rb, uint32_t size);
extern uint32_t chry_ringbuffer_linear_read_done(chry_ringbuffer_t *rb, uint32_t size);
#ifdef __cplusplus
}
#endif
#endif