专家
公告
财富商城
电子网
旗下网站
首页
问题库
专栏
标签库
话题
专家
NEW
门户
发布
提问题
发文章
STM32
串口自定义环形缓冲程序编写
2019-07-21 00:59
发布
×
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮
站内问答
/
STM32/STM8
3166
5
1453
再实际工程中,用程序编写一个环形缓冲区,防止数据被覆盖等意外现象。
但是没有头绪,不知该如何编写。请问有没有人能提供下建议或者代码?
友情提示:
此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
5条回答
warship
1楼-- · 2019-07-21 05:31
/**
************************************************************
* @file ringbuffer.c
* @brief Loop buffer processing
* @author Gizwits
* @date 2017-07-19
* @version V03030000
* @copyright Gizwits
*
*
@note
Gizwits is only for smart hardware
* Gizwits Smart Cloud for Smart Products
* Links | Value Added | Open | Neutral | Safety | Own | Free | Ecology
*
www.gizwits.com
*
***********************************************************/
#include "ringBuffer.h"
#include "common.h"
int8_t ICACHE_FLASH_ATTR rbCreate(rb_t* rb)
{
if(NULL == rb)
{
return -1;
}
rb->rbHead = rb->rbBuff;
rb->rbTail = rb->rbBuff;
return 0;
}
int8_t ICACHE_FLASH_ATTR rbDelete(rb_t* rb)
{
if(NULL == rb)
{
return -1;
}
rb->rbBuff = NULL;
rb->rbHead = NULL;
rb->rbTail = NULL;
rb->rbCapacity = 0;
return 0;
}
int32_t ICACHE_FLASH_ATTR rbCapacity(rb_t *rb)
{
if(NULL == rb)
{
return -1;
}
return rb->rbCapacity;
}
int32_t ICACHE_FLASH_ATTR rbCanRead(rb_t *rb)
{
if(NULL == rb)
{
return -1;
}
if (rb->rbHead == rb->rbTail)
{
return 0;
}
if (rb->rbHead < rb->rbTail)
{
return rb->rbTail - rb->rbHead;
}
return rbCapacity(rb) - (rb->rbHead - rb->rbTail);
}
int32_t ICACHE_FLASH_ATTR rbCanWrite(rb_t *rb)
{
if(NULL == rb)
{
return -1;
}
return rbCapacity(rb) - rbCanRead(rb);
}
int32_t ICACHE_FLASH_ATTR rbRead(rb_t *rb, void *data, size_t count)
{
int32_t copySz = 0;
if(NULL == rb)
{
return -1;
}
if(NULL == data)
{
return -1;
}
if (rb->rbHead < rb->rbTail)
{
copySz = min(count, rbCanRead(rb));
memcpy(data, rb->rbHead, copySz);
rb->rbHead += copySz;
return copySz;
}
else
{
if (count < rbCapacity(rb)-(rb->rbHead - rb->rbBuff))
{
copySz = count;
memcpy(data, rb->rbHead, copySz);
rb->rbHead += copySz;
return copySz;
}
else
{
copySz = rbCapacity(rb) - (rb->rbHead - rb->rbBuff);
memcpy(data, rb->rbHead, copySz);
rb->rbHead = rb->rbBuff;
copySz += rbRead(rb, (char*)data+copySz, count-copySz);
return copySz;
}
}
}
int32_t ICACHE_FLASH_ATTR rbWrite(rb_t *rb, const void *data, size_t count)
{
int32_t tailAvailSz = 0;
if((NULL == rb)||(NULL == data))
{
return -1;
}
if (count >= rbCanWrite(rb))
{
return -2;
}
if (rb->rbHead <= rb->rbTail)
{
tailAvailSz = rbCapacity(rb) - (rb->rbTail - rb->rbBuff);
if (count <= tailAvailSz)
{
memcpy(rb->rbTail, data, count);
rb->rbTail += count;
if (rb->rbTail == rb->rbBuff+rbCapacity(rb))
{
rb->rbTail = rb->rbBuff;
}
return count;
}
else
{
memcpy(rb->rbTail, data, tailAvailSz);
rb->rbTail = rb->rbBuff;
return tailAvailSz + rbWrite(rb, (char*)data+tailAvailSz, count-tailAvailSz);
}
}
else
{
memcpy(rb->rbTail, data, count);
rb->rbTail += count;
return count;
}
}
加载中...
DongInker
2楼-- · 2019-07-21 08:46
去看看环形队列
加载中...
www88988
3楼-- · 2019-07-21 11:54
#define BUFFERSIZE 1024 // 1K Buffer 如果数据量大的话可以多点但是不能大于64K(估计CPU的RAM也没那么大)
u16 InCount,OutCount; //进出计数
u8 USART_Buffer[BUFFERSIZE]; //环形缓冲
//调用函数 (这个函数得在WHILE中不断的查询缓存)
....
u8 redata=0;
if(OutCount!=InCount)
{
redata=GetData();
//你的数据处理代码
}
....
void USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
USART_ClearITPendingBit(USART1,USART_IT_RXNE);
USART_Buffer[InCount++] = USART_ReceiveData(USART1);
if(InCount>=BUFFERSIZE) InCount=0;
}
}
u8 GetData(void)
{
u8 data;
data=USART_Buffer[OutCount++];
if(OutCount>=BUFFERSIZE) OutCount=0;
return data;
}
加载中...
warship
4楼-- · 2019-07-21 12:00
/**
************************************************************
* @file ringbuffer.h
* @brief Loop buffer processing
*
@Author
Gizwits
* @date 2017-07-19
* @version V03030000
*
@Copyright
Gizwits
*
*
@note
Gizwits is only for smart hardware
* Gizwits Smart Cloud for Smart Products
* Links | Value Added | Open | Neutral | Safety | Own | Free | Ecology
*
www.gizwits.com
*
***********************************************************/
#ifndef _GIZWITS_RING_BUFFER_H
#define _GIZWITS_RING_BUFFER_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define min(a, b) (a)<(b)?(a)
b) ///< Calculate the minimum value
#pragma pack(1)
typedef struct {
size_t rbCapacity;
uint8_t *rbHead;
uint8_t *rbTail;
uint8_t *rbBuff;
}rb_t;
#pragma pack()
int8_t rbCreate(rb_t* rb);
int8_t rbDelete(rb_t* rb);
int32_t rbCapacity(rb_t *rb);
int32_t rbCanRead(rb_t *rb);
int32_t rbCanWrite(rb_t *rb);
int32_t rbRead(rb_t *rb, void *data, size_t count);
int32_t rbWrite(rb_t *rb, const void *data, size_t count);
#ifdef __cplusplus
}
#endif
#endif
加载中...
nashui_sx
5楼-- · 2019-07-21 14:02
精彩回答 2 元偷偷看……
加载中...
一周热门
更多
>
相关问题
STM32F4上I2C(在PROTEUS中模拟)调试不通的问题
6 个回答
芯片供应紧张,准备换个MCU,MM32L系列替换STM32L系列的怎么样?
7 个回答
STM32同时使用两个串口进行数据收发时数据丢包的问题
5 个回答
STM32F103串口通信死机问题
4 个回答
STM32WLE5CC连接SX1268在LoRa模式下能与 SX1278互通吗?
2 个回答
STM32开发板免费用活动
7 个回答
stm32 处理 DHT11占用太多时间,大家程序是怎么设计的
8 个回答
分享一个STM32单片机做的离线编程器代码
9 个回答
相关文章
ST公司第一款无线低功耗单片机模块有效提高物联网设计生产效率
0个评论
如何实现对单片机寄存器的访问
0个评论
通过USB用STM32片内自带Bootloader下载程序及注意事项
0个评论
欲练此功必先自宫之STM32汇编启动,放慢是为了更好的前行
0个评论
×
关闭
采纳回答
向帮助了您的网友说句感谢的话吧!
非常感谢!
确 认
×
关闭
编辑标签
最多设置5个标签!
STM32
保存
关闭
×
关闭
举报内容
检举类型
检举内容
检举用户
检举原因
广告推广
恶意灌水
回答内容与提问无关
抄袭答案
其他
检举说明(必填)
提交
关闭
×
关闭
您已邀请
15
人回答
查看邀请
擅长该话题的人
回答过该话题的人
我关注的人
************************************************************
* @file ringbuffer.c
* @brief Loop buffer processing
* @author Gizwits
* @date 2017-07-19
* @version V03030000
* @copyright Gizwits
*
* @note Gizwits is only for smart hardware
* Gizwits Smart Cloud for Smart Products
* Links | Value Added | Open | Neutral | Safety | Own | Free | Ecology
* www.gizwits.com
*
***********************************************************/
#include "ringBuffer.h"
#include "common.h"
int8_t ICACHE_FLASH_ATTR rbCreate(rb_t* rb)
{
if(NULL == rb)
{
return -1;
}
rb->rbHead = rb->rbBuff;
rb->rbTail = rb->rbBuff;
return 0;
}
int8_t ICACHE_FLASH_ATTR rbDelete(rb_t* rb)
{
if(NULL == rb)
{
return -1;
}
rb->rbBuff = NULL;
rb->rbHead = NULL;
rb->rbTail = NULL;
rb->rbCapacity = 0;
return 0;
}
int32_t ICACHE_FLASH_ATTR rbCapacity(rb_t *rb)
{
if(NULL == rb)
{
return -1;
}
return rb->rbCapacity;
}
int32_t ICACHE_FLASH_ATTR rbCanRead(rb_t *rb)
{
if(NULL == rb)
{
return -1;
}
if (rb->rbHead == rb->rbTail)
{
return 0;
}
if (rb->rbHead < rb->rbTail)
{
return rb->rbTail - rb->rbHead;
}
return rbCapacity(rb) - (rb->rbHead - rb->rbTail);
}
int32_t ICACHE_FLASH_ATTR rbCanWrite(rb_t *rb)
{
if(NULL == rb)
{
return -1;
}
return rbCapacity(rb) - rbCanRead(rb);
}
int32_t ICACHE_FLASH_ATTR rbRead(rb_t *rb, void *data, size_t count)
{
int32_t copySz = 0;
if(NULL == rb)
{
return -1;
}
if(NULL == data)
{
return -1;
}
if (rb->rbHead < rb->rbTail)
{
copySz = min(count, rbCanRead(rb));
memcpy(data, rb->rbHead, copySz);
rb->rbHead += copySz;
return copySz;
}
else
{
if (count < rbCapacity(rb)-(rb->rbHead - rb->rbBuff))
{
copySz = count;
memcpy(data, rb->rbHead, copySz);
rb->rbHead += copySz;
return copySz;
}
else
{
copySz = rbCapacity(rb) - (rb->rbHead - rb->rbBuff);
memcpy(data, rb->rbHead, copySz);
rb->rbHead = rb->rbBuff;
copySz += rbRead(rb, (char*)data+copySz, count-copySz);
return copySz;
}
}
}
int32_t ICACHE_FLASH_ATTR rbWrite(rb_t *rb, const void *data, size_t count)
{
int32_t tailAvailSz = 0;
if((NULL == rb)||(NULL == data))
{
return -1;
}
if (count >= rbCanWrite(rb))
{
return -2;
}
if (rb->rbHead <= rb->rbTail)
{
tailAvailSz = rbCapacity(rb) - (rb->rbTail - rb->rbBuff);
if (count <= tailAvailSz)
{
memcpy(rb->rbTail, data, count);
rb->rbTail += count;
if (rb->rbTail == rb->rbBuff+rbCapacity(rb))
{
rb->rbTail = rb->rbBuff;
}
return count;
}
else
{
memcpy(rb->rbTail, data, tailAvailSz);
rb->rbTail = rb->rbBuff;
return tailAvailSz + rbWrite(rb, (char*)data+tailAvailSz, count-tailAvailSz);
}
}
else
{
memcpy(rb->rbTail, data, count);
rb->rbTail += count;
return count;
}
}
u16 InCount,OutCount; //进出计数
u8 USART_Buffer[BUFFERSIZE]; //环形缓冲
//调用函数 (这个函数得在WHILE中不断的查询缓存)
....
u8 redata=0;
if(OutCount!=InCount)
{
redata=GetData();
//你的数据处理代码
}
....
void USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
USART_ClearITPendingBit(USART1,USART_IT_RXNE);
USART_Buffer[InCount++] = USART_ReceiveData(USART1);
if(InCount>=BUFFERSIZE) InCount=0;
}
}
u8 GetData(void)
{
u8 data;
data=USART_Buffer[OutCount++];
if(OutCount>=BUFFERSIZE) OutCount=0;
return data;
}
************************************************************
* @file ringbuffer.h
* @brief Loop buffer processing
* @Author Gizwits
* @date 2017-07-19
* @version V03030000
* @Copyright Gizwits
*
* @note Gizwits is only for smart hardware
* Gizwits Smart Cloud for Smart Products
* Links | Value Added | Open | Neutral | Safety | Own | Free | Ecology
* www.gizwits.com
*
***********************************************************/
#ifndef _GIZWITS_RING_BUFFER_H
#define _GIZWITS_RING_BUFFER_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define min(a, b) (a)<(b)?(a)b) ///< Calculate the minimum value
#pragma pack(1)
typedef struct {
size_t rbCapacity;
uint8_t *rbHead;
uint8_t *rbTail;
uint8_t *rbBuff;
}rb_t;
#pragma pack()
int8_t rbCreate(rb_t* rb);
int8_t rbDelete(rb_t* rb);
int32_t rbCapacity(rb_t *rb);
int32_t rbCanRead(rb_t *rb);
int32_t rbCanWrite(rb_t *rb);
int32_t rbRead(rb_t *rb, void *data, size_t count);
int32_t rbWrite(rb_t *rb, const void *data, size_t count);
#ifdef __cplusplus
}
#endif
#endif
一周热门 更多>