IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 嵌入式 -> 参考kfifo设计单片机的ring buffer -> 正文阅读

[嵌入式]参考kfifo设计单片机的ring buffer

#ifndef __RING_BUFFER_H_
#define __RING_BUFFER_H_

struct ring_buffer
{
	unsigned int in;
	unsigned int out;
	unsigned int size;
	unsigned char *buffer;
};

struct ring_buffer *ring_buffer_init(unsigned int buffer_size);
void ring_buffer_exit(struct ring_buffer *rb);
unsigned int ring_buffer_avail(struct ring_buffer *rb);
void ring_buffer_discard(struct ring_buffer *rb, unsigned int length);
unsigned int ring_buffer_read(struct ring_buffer *rb, unsigned char *buffer, unsigned int length);
unsigned int ring_buffer_write(struct ring_buffer *rb, const unsigned char *buffer, unsigned int length);
unsigned int ring_buffer_overwrite(struct ring_buffer *rb, const unsigned char *buffer, unsigned int length);

#endif

#include "ring_buffer.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

#define MIN(a, b) (((a) < (b)) ? (a) : (b))

struct ring_buffer *ring_buffer_init(unsigned int buffer_size)
{
	int i;
	struct ring_buffer *rb = NULL;
	
	rb = (struct ring_buffer *) malloc(sizeof(struct ring_buffer));
	if (NULL == rb)
	{
		printf("malloc struct ring_buffer failed! \n");
		return rb;
	}
	
	for (i=0; ; i++)
	{
		if (((1<<i) < buffer_size) && (buffer_size <= (1<<i+1)))
			break;
	}
	
	rb->buffer = (unsigned char *) malloc((1 << (i + 1)) * sizeof(unsigned char));
	if (NULL == rb->buffer)
	{
		printf("malloc buffer failed! \n");
		free(rb);
		rb = NULL;
		return rb;
	}
	
	printf("The ring buffer alloced is %d bytes. \n", 1 << (i + 1));
	
	rb->in = 0;
	rb->out = 0;
	rb->size = 1 << (i + 1);
	
	return rb;
}

void ring_buffer_exit(struct ring_buffer *rb)
{
	if (NULL == rb)
		return ;
	
	if (NULL != rb->buffer)
		free(rb->buffer);
	
	free(rb);
}

unsigned int ring_buffer_avail(struct ring_buffer *rb)
{
	return (rb->in - rb->out);
}

void ring_buffer_discard(struct ring_buffer *rb, unsigned int length)
{
	length = MIN(length, rb->in - rb->out);
	rb->out += length;
}

unsigned int ring_buffer_read(struct ring_buffer *rb, unsigned char *buffer, unsigned int length)
{
	unsigned int len;
	
	length = MIN(length, rb->in - rb->out);
	
	/* first get the data from fifo->out until the end of the buffer */
	len = MIN(length, rb->size - (rb->out & (rb->size - 1)));
	memcpy(buffer, rb->buffer + (rb->out & (rb->size - 1)), len);
	
	/* then get the rest (if any) from the beginning of the buffer */
	memcpy(buffer + len, rb->buffer, length - len);
	
	rb->out += length;
	return length;
}

unsigned int ring_buffer_write(struct ring_buffer *rb, const unsigned char *buffer, unsigned int length)
{
	unsigned int len;
	
	length = MIN(length, rb->size - rb->in + rb->out);
	
	/* first put the data starting from fifo->in to buffer end */
	len  = MIN(length, rb->size - (rb->in & (rb->size - 1)));
	memcpy(rb->buffer + (rb->in & (rb->size - 1)), buffer, len);
	
	/* then put the rest (if any) at the beginning of the buffer */
	memcpy(rb->buffer, buffer + len, length - len);
	
	rb->in += length;
	
	return length;
}

unsigned int ring_buffer_overwrite(struct ring_buffer *rb, const unsigned char *buffer, unsigned int length)
{
	unsigned int len;
	unsigned int unavail;
	unavail = rb->size - rb->in + rb->out;
	
	if (length < rb->size)
	{
		if (unavail < length)
		{
			ring_buffer_discard(rb, length - unavail);
		}
		
		/* first put the data starting from fifo->in to buffer end */
		len  = MIN(length, rb->size - (rb->in & (rb->size - 1)));
		memcpy(rb->buffer + (rb->in & (rb->size - 1)), buffer, len);
		
		/* then put the rest (if any) at the beginning of the buffer */
		memcpy(rb->buffer, buffer + len, length - len);
		
		rb->in += length;
	}
	else
	{
		memcpy(rb->buffer, buffer + length - rb->size, rb->size);
		rb->in = rb->size;
		rb->out = 0;
	}
	
	return length;
}

#include "ring_buffer.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char* arg[])
{
    unsigned int i = 0;
    unsigned int n, ret;
    char exit_q;
    unsigned char buff[100];
    
	printf("Please enter the size of ring buffer that you want to alloc: ");
    scanf("%d", &i);
    struct ring_buffer *ring_buffer_st = ring_buffer_init(i);
	if (NULL == ring_buffer_st)
		return -1;
	printf("\n");
    
    while (1)
    {
        memset(buff, 0, 100);
        i = 0;
        printf("Please enter the number of data you want to write: ");
        scanf("%d", &i);
        printf("Please input the data that you want to write: ");
        for (n = 0; n < i; n++)
            scanf("%d", &buff[n]);
        ret = ring_buffer_overwrite(ring_buffer_st, buff, i);
        printf("\t ring_buffer_overwrite return value = %d \n", ret);
		
		printf("\t ------------------------\n");
		printf("\t | ");
		for (i = 0; i < ring_buffer_st->size; i++)
			printf("%u ", ring_buffer_st->buffer[i]);
		printf("\n");
		printf("\t | out=%u, in=%u, \n", ring_buffer_st->out, ring_buffer_st->in);
		printf("\t ------------------------\n");
		        ret = ring_buffer_avail(ring_buffer_st);
        printf("\t ring_buffer_avail return value = %d \n\n", ret);
        
        memset(buff, 0, 100);
        i = 0;
        printf("Please enter the number of data you want to read: ");
        scanf("%d", &i);
        ret = ring_buffer_read(ring_buffer_st, buff, i);
        printf("The dats is: ");
        for (n = 0; n < ret; n++)
            printf("%d ", buff[n]);
        printf("\n");
        printf("\t ring_buffer_read return value = %d \n", ret);
        
		printf("\t ------------------------\n");
		printf("\t | ");
		for (i = 0; i < ring_buffer_st->size; i++)
			printf("%u ", ring_buffer_st->buffer[i]);
		printf("\n");
		printf("\t | out=%u, in=%u, \n", ring_buffer_st->out, ring_buffer_st->in);
		printf("\t ------------------------\n");
		        ret = ring_buffer_avail(ring_buffer_st);
        printf("\t ring_buffer_avail return value = %d \n\n", ret);
		
        printf("You can enter Q to exit the program ...\n");
        //fflush(stdin);    // For Windows
        setbuf(stdin, NULL);    // For Linux
        exit_q = getchar();
        if (exit_q == 'q' || exit_q == 'Q')
            break;
    }
    
    ring_buffer_exit(ring_buffer_st);
	ring_buffer_st = NULL;
}

  嵌入式 最新文章
基于高精度单片机开发红外测温仪方案
89C51单片机与DAC0832
基于51单片机宠物自动投料喂食器控制系统仿
《痞子衡嵌入式半月刊》 第 68 期
多思计组实验实验七 简单模型机实验
CSC7720
启明智显分享| ESP32学习笔记参考--PWM(脉冲
STM32初探
STM32 总结
【STM32】CubeMX例程四---定时器中断(附工
上一篇文章      下一篇文章      查看所有文章
加:2021-08-09 10:23:46  更:2021-08-09 10:23:50 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年12日历 -2024/12/28 0:56:03-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码
数据统计