收集整理了一份《2024年最新物联网嵌入式全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升的朋友。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人
都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
documentation:LwRB latest-develop documentation
Features
- Written in ANSI C99, compatible with
size_t
for size data types - Platform independent, no architecture specific code
- FIFO (First In First Out) buffer implementation
- No dynamic memory allocation, data is static array
- Uses optimized memory copy instead of loops to read/write data from/to memory
- Thread safe when used as pipe with single write and single read entries
- Interrupt safe when used as pipe with single write and single read entries
- Suitable for DMA transfers from and to memory with zero-copy overhead between buffer and application memory
- Supports data peek, skip for read and advance for write
- Implements support for event notifications
- User friendly MIT license
该库其实只依赖两个文件:lwrb.c 和 lwrb.h (之前文件名是ringbuff.c和ringbuff.h,不知道为什么又改了文件名和函数名)
我们只需要将这两个文件添加到我们的项目中即可。不过注意要改一下lwrb.c中的头文件包含
#include "lwrb/lwrb.h" 改成 #include "lwrb.h"
因为CLion使用cmake来管理的,我们还要在CMakeLists.txt中添加lwrb.c
cmake_minimum_required(VERSION 3.21)
project(ringbuffer_clion C)
set(CMAKE_C_STANDARD 99)
add_executable(ringbuffer_clion main.c lwrb.c)
项目文件目录如下
实例代码
首先我们用帮助文档中的Example code来测试一下是否可以正常使用,修改main.c
#include <stdio.h>
#include "lwrb.h"
int main() {
/\* Declare rb instance & raw data \*/
lwrb_t buff;
uint8_t buff_data[8];
/\* Application variables \*/
uint8_t data[2];
size_t len;
/\* Application code ... \*/
lwrb\_init(&buff, buff_data, sizeof(buff_data)); /\* Initialize buffer \*/
/\* Write 4 bytes of data \*/
lwrb\_write(&buff, "0123", 4);
/\* Try to read buffer \*/
/\* len holds number of bytes read \*/
/\* Read until len == 0, when buffer is empty \*/
while ((len = lwrb\_read(&buff, data, sizeof(data))) > 0) {
printf("Successfully read %d bytes\r\n", (int)len);
}
}
发现是可以正常编译运行的,说明我们可以使用这个开源库了。
在这个环形缓冲库中存在一个单元测试模块,单元测试使用的是Unity测试框架。
Unity测试框架GitHub地址:Unity
Unity学习教程:Unity单元测试框架、C单元测试Unity
Unity是一个单元测试框架,Unity设计者团队的目标是让它保持小型化和功能性。核心的Unity测试框架只有三个文件:单个C文件和两个头文件。Unity提供了函数和宏,使得单元测试更加容易。
Unity被设计成跨平台,它努力坚持C标准,同时仍然支持许多违法规则的嵌入式C编译器。Unity可以在许多编译器环境中使用,比如GCC,IAR,Clang,Green Hills,Microchip, MS Visual Studio。适配Unity在一个新的目标平台协同工作并不需要很多工作量。
我们将Unity单元测试框架的三个文件加入到我们的项目中
将main.c函数修改成test.c中的内容
/\*\*
\* @file test.c
\*
\* Unit tests for the lwrb library
\*
\* @author Tofik Sonono (tofik@sonono.me)
\*
\*/
/\*======= Includes ==========================================================\*/
#include <stdint.h>
#include <stdlib.h>
#include "unity.h"
#include "lwrb.h"
/\*======= Local Macro Definitions ===========================================\*/
/\*======= Local function prototypes =========================================\*/
void basic\_read\_and\_write(lwrb_t \*buff, uint8_t \*data_to_write, size_t data_size);
/\*======= Local variable declarations =======================================\*/
/\*======= Global function implementations ===================================\*/
/\* Requires a definition for Unity to compile \*/
void setUp(void) { }
void tearDown(void) { }
/\*======= Tests ==============================================================\*/
void testNullInputToInit\_should\_fail(void) {
uint8_t ret;
lwrb_t buff = { 0 };
uint8_t buff_data[1];
ret = lwrb\_init(NULL, buff_data, sizeof(buff_data));
TEST\_ASSERT\_EQUAL(0, ret);
ret = lwrb\_init(&buff, NULL, sizeof(buff_data));
TEST\_ASSERT\_EQUAL(0, ret);
**收集整理了一份《2024年最新物联网嵌入式全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升的朋友。**
![img](https://img-blog.csdnimg.cn/img_convert/7b69744010574c0d7ab477f852206862.png)
![img](https://img-blog.csdnimg.cn/img_convert/1a5fa103fe5bed485b53e7365b2cb0bd.png)
**[如果你需要这些资料,可以戳这里获取](https://bbs.csdn.net/topics/618679757)**
**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人**
**都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**
e-1715689437530)]
**[如果你需要这些资料,可以戳这里获取](https://bbs.csdn.net/topics/618679757)**
**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人**
**都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**