|
发表于 2018-3-3 18:00:44
|
显示全部楼层
/*
************linux2.4里面提取精简测试过的fifo模块********************************************************
*
*
* (c) Copyright 2007-2020; , Inc.; , FL
*
* All rights reserved. Protected by international copyright laws.
* Knowledge of the source code may NOT be used to develop a similar product.
* Please help us continue to provide the Embedded community with the finest
* software available. Your honesty is greatly appreciated.
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* includes
* Description :
* Note(s) :
*********************************************************************************************************
*/
#include <kfifo.h>
int __kfifo_init(struct __kfifo *fifo, void *buffer, unsigned int size)
{
fifo->in = 0;
fifo->out = 0;
fifo->mask = 0;
fifo->data = buffer;
if (size < 2) {
return -1;
}
if ((size & (size-1)) != 0)
{
return -2; /*** Must be a power of 2 (2,4,8,16,32,64,128,256,512,...) ***/
}
fifo->mask = size - 1;
return 0;
}
unsigned int __kfifo_push_char(struct __kfifo *fifo, const char *p)
{
if(( ( fifo->in - fifo->out ) & ( ~fifo->mask ) ) == 0 )
{
*( (char*)fifo->data + (fifo->in++ & fifo->mask) ) = *p;
return 1;
}
return 0;
}
unsigned int __kfifo_pop_char(struct __kfifo *fifo, char* p )
{
if( fifo->in != fifo->out )
{
*p = *( (char*)fifo->data + (fifo->out++ & fifo->mask) ) ;
return 1;
}
return 0;
}
/*
*********************************************************************************************************
*
*
* (c) Copyright 2007-2020; , Inc.; , FL
*
* All rights reserved. Protected by international copyright laws.
* Knowledge of the source code may NOT be used to develop a similar product.
* Please help us continue to provide the Embedded community with the finest
* software available. Your honesty is greatly appreciated.
*********************************************************************************************************
*/
#ifndef KFIFO_PRESENT /* See Note #1. */
#define KFIFO_PRESENT
#ifdef KFIFO_MODULE
#define KFIFO_EXT
#else
#define KFIFO_EXT extern
#endif
#ifdef __cplusplus
extern "C"
{
#endif
#include <cpu.h>
//typedef unsigned short CPU_INT16U; /* 16-bit unsigned integer */
/*
in out
必须为有符号数,
push() 中的这句 fifo->in 否则到了0xffff后,变为0x0000,再也不会增加
if(( ( fifo->in - fifo->out ) & ~fifo->mask ) == 0 )
*/
struct __kfifo {
int in;
int out;
int mask;
void *data;
};
#if defined ( __ICCARM__ )
#pragma inline
KFIFO_EXT CPU_INT16U kfifo_unused(struct __kfifo *fifo)
#else
KFIFO_EXT __inline CPU_INT16U kfifo_unused(struct __kfifo *fifo)
#endif
{
return (fifo->mask + 1) - (fifo->in - fifo->out);
}
KFIFO_EXT int __kfifo_init(struct __kfifo *fifo, void *buffer, unsigned int size);
KFIFO_EXT unsigned int __kfifo_push_char(struct __kfifo *fifo, const char *p);
KFIFO_EXT unsigned int __kfifo_pop_char(struct __kfifo *fifo, char *p );
#ifdef __cplusplus
}
#endif
/*
*********************************************************************************************************
* PRESENT END
*
* Note(s) : See 'PRESENT Note #1'.
*********************************************************************************************************
*/
#endif
|
|