概述
ringbuff 是一款通用FIFO环形缓冲区实现的开源库,并遵循 MIT 开源许可协议。
ringbuff优点:
- 使用C99语法编写,并且没有平台相关代码;
- 没有动态内存分配;
- 使用更优的内存复制而不是循环从内存读取数据/向内存写入数据;
GitHub:https://github.com/MaJerle/lwrb
硬件:STM32F103CBT6最小系统板
软件:Keil 5.29 + STM32CubeMX6.01
一、使用方法
详情请移步阅读官网文档
https://docs.majerle.eu/projects/lwrb/en/latest/
二、STM32CubeMx配置
三、Examples
1、进入GitHub拉取源码
2、打开STM32CubeMx生成的keil工程,新建bsp文件夹,按照如下步骤进行。
3、把所有.c文件添加到Keil中来。
4、添加头文件路径
5、编译工程
6、解决报错问题
7、配置lwrb.h中LWRB_VOLATILE 定义类型,手动添加即可。
按照以上步骤进行,移植工作已完成。
8、main.c文件
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* <h2><center>© 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 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
#if ENABLEUSARR_TEST
/* 中断回调函数 */
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
/* 判断是哪个串口触发的中断 */
if(huart ->Instance == USART1)
{
/* 将接收到的数据写入缓冲区 */
lwrb_write(&usart1_ringbuff, &recv_data, 1);
//重新使能串口接收中断
HAL_UART_Receive_IT(huart, (uint8_t*)&recv_data, 1);
}
}
#else
#endif
#ifdef __GNUC__
/* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
set to 'Yes') calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
/**
* @brief Retargets the C library printf function to the USART.
* @param None
* @retval None
*/
PUTCHAR_PROTOTYPE
{
/* Place your implementation of fputc here */
/* e.g. write a character to the EVAL_COM1 and Loop until the end of transmission */
HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xFFFF);
return ch;
}
int fgetc(FILE * f)
{
uint8_t ch = 0;
HAL_UART_Receive(&huart1, (uint8_t *)&ch, 1, 0xffff);
return ch;
}
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
四、运行结果
设置串口助手定时100ms发生一次数据,200ms读取数据。
传送门->代码
参考文章:https://mculover666.blog.csdn.net/article/details/106445879
五、总结
好了,就介绍到此,有了这个神器,特别是处理一些大量数据时非常适合这样场景。