优化
This commit is contained in:
+2
-8
@@ -12,10 +12,7 @@ os_err_t fifo_put(fifo_t* self, uint8_t data){
|
|||||||
|
|
||||||
os_size_t write_idx = self->write_idx;
|
os_size_t write_idx = self->write_idx;
|
||||||
|
|
||||||
os_size_t next_write_idx = write_idx + 1;
|
os_size_t next_write_idx = fifo_next(self, self->write_idx);
|
||||||
if(next_write_idx >= self->buffer_size){
|
|
||||||
next_write_idx = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(next_write_idx == self->read_idx){
|
if(next_write_idx == self->read_idx){
|
||||||
return OS_ERR_FULL;
|
return OS_ERR_FULL;
|
||||||
@@ -38,10 +35,7 @@ os_err_t fifo_get(fifo_t* self, uint8_t* data){
|
|||||||
*data = self->buffer[read_idx];
|
*data = self->buffer[read_idx];
|
||||||
}
|
}
|
||||||
|
|
||||||
os_size_t next_read_idx = read_idx + 1;
|
os_size_t next_read_idx = fifo_next(self, self->read_idx);
|
||||||
if(next_read_idx >= self->buffer_size){
|
|
||||||
next_read_idx = 0;
|
|
||||||
}
|
|
||||||
self->read_idx = next_read_idx;
|
self->read_idx = next_read_idx;
|
||||||
return OS_ERR_OK;
|
return OS_ERR_OK;
|
||||||
}
|
}
|
||||||
|
|||||||
+13
-7
@@ -22,11 +22,11 @@
|
|||||||
typedef struct fifo_t{
|
typedef struct fifo_t{
|
||||||
uint8_t* buffer;
|
uint8_t* buffer;
|
||||||
os_size_t buffer_size;
|
os_size_t buffer_size;
|
||||||
os_size_t write_idx;
|
volatile os_size_t write_idx;
|
||||||
os_size_t read_idx;
|
volatile os_size_t read_idx;
|
||||||
}fifo_t;
|
}fifo_t;
|
||||||
|
|
||||||
#define FIFO_INVALID_POS ((os_size_t)(-1))
|
#define FIFO_INVALID_POS ((os_size_t)(-1u))
|
||||||
|
|
||||||
/* ==================================================================================================== */
|
/* ==================================================================================================== */
|
||||||
/* 接口 */
|
/* 接口 */
|
||||||
@@ -61,11 +61,17 @@ os_size_t fifo_capacity(fifo_t* self){
|
|||||||
}
|
}
|
||||||
|
|
||||||
OS_STATIC_FORCE_INLINE
|
OS_STATIC_FORCE_INLINE
|
||||||
os_bool_t fifo_is_full(fifo_t* self){
|
os_size_t fifo_next(fifo_t* self, os_size_t idx){
|
||||||
os_size_t next_write_idx = self->write_idx + 1;
|
idx += 1;
|
||||||
if(next_write_idx>=self->buffer_size){
|
if(idx >= self->buffer_size){
|
||||||
next_write_idx = 0;
|
idx = 0;
|
||||||
}
|
}
|
||||||
|
return idx;
|
||||||
|
}
|
||||||
|
|
||||||
|
OS_STATIC_FORCE_INLINE
|
||||||
|
os_bool_t fifo_is_full(fifo_t* self){
|
||||||
|
os_size_t next_write_idx = fifo_next(self, self->write_idx);
|
||||||
return (next_write_idx==self->read_idx)?OS_TRUE:OS_FALSE;
|
return (next_write_idx==self->read_idx)?OS_TRUE:OS_FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user