建议使用以下浏览器,以获得最佳体验。 IE 9.0+以上版本 Chrome 31+ 谷歌浏览器 Firefox 30+ 火狐浏览器
请选择 进入手机版 | 继续访问电脑版
设置昵称

在此一键设置昵称,即可参与社区互动!

确定
我再想想
选择版块
直达楼层
标签
您还可以添加5个标签
  • 没有搜索到和“关键字”相关的标签
  • 云产品
  • 解决方案
  • 技术领域
  • 通用技术
  • 平台功能
取消

采纳成功

您已采纳当前回复为最佳回复

ttking

发帖: 455粉丝: 27

发消息 + 关注

发表于2021年04月30日 17:02:27 172 3
直达本楼层的链接
楼主
显示全部楼层
[实例分享] STM32HAL 移植一款通用FIFO轻量级环形缓冲管理器开源库lwrb(裸机开发)

## 概述 ringbuff 是一款通用FIFO环形缓冲区实现的开源库,并遵循 MIT 开源许可协议。 **ringbuff优点**: - 使用C99语法编写,并且没有平台相关代码; - 没有动态内存分配; - 使用更优的内存复制而不是循环从内存读取数据/向内存写入数据; GitHub:https://github.com/MaJerle/lwrb **硬件:**STM32F103CBT6最小系统板 **软件:**Keil 5.29  + STM32CubeMX6.01 ![image.png](https://bbs-img.huaweicloud.com/data/forums/attachment/forum/202104/30/165821rp0pqyltsb07domu.png) # 一、使用方法 详情请移步阅读官网文档 https://docs.majerle.eu/projects/lwrb/en/latest/ # 二、STM32CubeMx配置 ![image.png](https://bbs-img.huaweicloud.com/data/forums/attachment/forum/202104/30/1658470isykgn9hmkybofr.png) ![image.png](https://bbs-img.huaweicloud.com/data/forums/attachment/forum/202104/30/1658558lo3iqhbrizeasn0.png) # 三、Examples 1、进入GitHub拉取源码 ![image.png](https://bbs-img.huaweicloud.com/data/forums/attachment/forum/202104/30/165906oh1ta9a3dc3u3mms.png) 2、打开STM32CubeMx生成的keil工程,新建bsp文件夹,按照如下步骤进行。 ![image.png](https://bbs-img.huaweicloud.com/data/forums/attachment/forum/202104/30/165919k4lfgyhrunhucpdl.png) 3、把所有.c文件添加到Keil中来。 ![image.png](https://bbs-img.huaweicloud.com/data/forums/attachment/forum/202104/30/165933unmvcm83poanovbx.png) 4、添加头文件路径 ![image.png](https://bbs-img.huaweicloud.com/data/forums/attachment/forum/202104/30/165954z7elunde0yowfcvi.png) 5、编译工程 ![image.png](https://bbs-img.huaweicloud.com/data/forums/attachment/forum/202104/30/170003r6vtaupi7fkygngf.png) 6、解决报错问题 ![image.png](https://bbs-img.huaweicloud.com/data/forums/attachment/forum/202104/30/170014uvkiyjmhu2zcgz5y.png) 7、配置lwrb.h中LWRB_VOLATILE 定义类型,手动添加即可。 ![image.png](https://bbs-img.huaweicloud.com/data/forums/attachment/forum/202104/30/170024e0o4trqaj2to0jhh.png) 按照以上步骤进行,移植工作已完成。 **main函数文件** ```c /* USER CODE BEGIN Header */ /** ****************************************************************************** * @file : main.c * @brief : Main program body ****************************************************************************** * @attention * * <h2><center>&copy; Copyright (c) 2021 STMicroelectronics. * All rights reserved.</center></h2> * * This software component is licensed by ST under BSD 3-Clause license, * the "License"; You may not use this file except in compliance with the * License. You may obtain a copy of the License at: * opensource.org/licenses/BSD-3-Clause * ****************************************************************************** */ /* USER CODE END Header */ /* Includes ------------------------------------------------------------------*/ #include "main.h" #include "usart.h" #include "gpio.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include "stdio.h" #include "lwrb.h" /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ /* USER CODE BEGIN PTD */ /* USER CODE END PTD */ /* Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */ /* USER CODE END PD */ /* Private macro -------------------------------------------------------------*/ /* USER CODE BEGIN PM */ #define ENABLEUSARR_TEST 1 /* USER CODE END PM */ /* Private variables ---------------------------------------------------------*/ /* USER CODE BEGIN PV */ /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); /* USER CODE BEGIN PFP */ /* USER CODE END PFP */ /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ #if ENABLEUSARR_TEST //用于串口接收 uint8_t recv_data = 0; //用于存储从缓冲区读取出的数据 uint8_t read_data = 0; //用于串口1的ringbuff句柄 lwrb_t usart1_ringbuff; //开辟一块内存用于缓冲区 #define USART1_BUFFDATA_SIZE 200 uint8_t usart1_buffdata[USART1_BUFFDATA_SIZE]; #else uint8_t lwrb_data[8 + 1]; lwrb_t buff; static void debug_buff(uint8_t cmp, size_t r_w, size_t r_r, size_t r_f, size_t r_e); uint8_t tmp[8]; void my_buff_evt_fn(lwrb_t* buff, lwrb_evt_type_t type, size_t len) { switch (type) { case LWRB_EVT_RESET: printf("[EVT] Buffer reset event!\r\n"); break; case LWRB_EVT_READ: printf("[EVT] Buffer read event: %d byte(s)!\r\n", (int)len); break; case LWRB_EVT_WRITE: printf("[EVT] Buffer write event: %d byte(s)!\r\n", (int)len); break; default: break; } } static void debug_buff(uint8_t cmp, size_t r_w, size_t r_r, size_t r_f, size_t r_e) { /* Previous and current write, read pointers and full, empty values */ static size_t p_r, p_w, p_f, p_e; size_t r, w, f, e; r = buff.r; w = buff.w; f = lwrb_get_full(&buff); e = lwrb_get_free(&buff); printf("R: %3d, W: %3d, F: %3d, E: %3d\r\n", (int)r, (int)w, (int)f, (int)e); } #endif /* USER CODE END 0 */ /** * @brief The application entry point. * @retval int */ int main(void) { /* USER CODE BEGIN 1 */ #if ENABLEUSARR_TEST uint32_t i = 0; volatile uint8_t len = 0; uint8_t data_len = 0; #else size_t len; #endif /* USER CODE END 1 */ /* MCU Configuration--------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* USER CODE BEGIN Init */ /* USER CODE END Init */ /* Configure the system clock */ SystemClock_Config(); /* USER CODE BEGIN SysInit */ /* USER CODE END SysInit */ /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_USART1_UART_Init(); /* USER CODE BEGIN 2 */ #if ENABLEUSARR_TEST printf("lwrb Port By champion666\r\n"); //初始化ringbuff句柄 if(1 != lwrb_init(&usart1_ringbuff, (uint8_t*)usart1_buffdata, USART1_BUFFDATA_SIZE)) { printf("usart1 lwrb init fail.\r\n"); } lwrb_is_ready(&usart1_ringbuff); //使能串口中断接收 HAL_UART_Receive_IT(&huart1, (uint8_t*)&recv_data, 1); #else lwrb_init(&buff, lwrb_data, sizeof(lwrb_data)); lwrb_set_evt_fn(&buff, my_buff_evt_fn); lwrb_write(&buff, "abc", 3); lwrb_write(&buff, "abc", 3); lwrb_write(&buff, "abc", 3); len = lwrb_read(&buff, tmp, 9); buff.r = 0; buff.w = 0; memset(lwrb_get_linear_block_write_address(&buff), 'A', lwrb_get_linear_block_write_length(&buff)); lwrb_advance(&buff, lwrb_get_linear_block_write_length(&buff)); buff.r = 2; buff.w = 0; memset(lwrb_get_linear_block_write_address(&buff), 'B', lwrb_get_linear_block_write_length(&buff)); lwrb_advance(&buff, lwrb_get_linear_block_write_length(&buff)); buff.r = 3; buff.w = 3; memset(lwrb_get_linear_block_write_address(&buff), 'C', lwrb_get_linear_block_write_length(&buff)); lwrb_advance(&buff, lwrb_get_linear_block_write_length(&buff)); lwrb_reset(&buff); for (size_t r = 0; r < sizeof(lwrb_data); ++r) { void* ptr; for (size_t w = 0; w < sizeof(lwrb_data); ++w) { buff.r = r; buff.w = w; ptr = lwrb_get_linear_block_write_address(&buff); len = lwrb_get_linear_block_write_length(&buff); printf("W: %3d, R: %3d, LEN: %3d\r\n", (int)w, (int)r, (int)len); } } #endif /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ #if ENABLEUSARR_TEST while((len = lwrb_read(&usart1_ringbuff, (uint8_t*)&read_data, sizeof(read_data))) > 0) { /* 捕获起始标志 */ if(read_data == 0x35) { //读取数据字节数,最大支持0xFF if((len = lwrb_read(&usart1_ringbuff, (uint8_t*)&read_data, sizeof(read_data))) > 0) { data_len = read_data; printf("your data has %d byte(s):\r\n\t", data_len); } //提取data_len个数据 for(i = 0; i < data_len; i++) { if((len = lwrb_read(&usart1_ringbuff, (uint8_t*)&read_data, sizeof(read_data))) > 0) { printf("[0x%02x] ", read_data); } } //printf("overflow\r\n"); printf("\r\n"); } } HAL_Delay(200); #else #endif } /* USER CODE END 3 */ } ``` # 运行效果 ![image.png](https://bbs-img.huaweicloud.com/data/forums/attachment/forum/202104/30/170203ty6ixvik9o5uyrxi.png) **好了,就介绍到此,有了这个神器,特别是处理一些大量数据时非常适合这样场景。** ———————————————— 本文为CSDN博主「Ch_champion」的原创文章 原文链接:https://blog.csdn.net/qq_36075612/article/details/116144349
举报
分享

分享文章到朋友圈

分享文章到微博

采纳成功

您已采纳当前回复为最佳回复

极客潇

发帖: 521粉丝: 70

发消息 + 关注

发表于2021年04月30日 17:16:06
直达本楼层的链接
沙发
显示全部楼层

感谢分享

点赞 评论 引用 举报

采纳成功

您已采纳当前回复为最佳回复

柠檬PH=2

发帖: 475粉丝: 51

发消息 + 关注

发表于2021年04月30日 20:29:39
直达本楼层的链接
板凳
显示全部楼层

感谢分享

点赞 评论 引用 举报

采纳成功

您已采纳当前回复为最佳回复

o0龙龙0o

发帖: 415粉丝: 42

发消息 + 关注

发表于2021年05月05日 11:59:24
直达本楼层的链接
地板
显示全部楼层

这个不错,基于裸机使用LwIP的开发还是比较有用的。

点赞 评论 引用 举报

游客

富文本
Markdown
您需要登录后才可以回帖 登录 | 立即注册

结贴

您对问题的回复是否满意?
满意度
非常满意 满意 一般 不满意
我要反馈
0/200