mirror of
https://github.com/thelsing/knx.git
synced 2025-08-26 13:51:28 +02:00
Restore ring_buffer code in the right place: ring_buffer.[h/c], add also macro for STATIC initialization using C++11
This commit is contained in:
parent
026e79db38
commit
0eef8ab8a3
@ -1,2 +1,55 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <stdint.h>
|
||||||
#include "ring_buffer.h"
|
#include "ring_buffer.h"
|
||||||
|
|
||||||
|
void init_static_ring_buffer(struct ring_buffer *buf, uint8_t *buffer, uint32_t maxlen)
|
||||||
|
{
|
||||||
|
buf->_buffer = buffer;
|
||||||
|
buf->_writer = buf->_buffer;
|
||||||
|
buf->_reader = buf->_buffer;
|
||||||
|
buf->_len = 0;
|
||||||
|
buf->_maxlen = maxlen;
|
||||||
|
}
|
||||||
|
|
||||||
|
int fifo_push(struct ring_buffer *buf, uint8_t data)
|
||||||
|
{
|
||||||
|
/* FIFO FULL */
|
||||||
|
if(buf->_len >= buf->_maxlen)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
*buf->_writer = data;
|
||||||
|
++buf->_len;
|
||||||
|
++buf->_writer;
|
||||||
|
|
||||||
|
/* If end of buffer, restart to begin */
|
||||||
|
if(buf->_writer > (buf->_buffer + buf->_maxlen))
|
||||||
|
{
|
||||||
|
buf->_writer = buf->_buffer;
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int fifo_pop(struct ring_buffer *buf, uint8_t *data)
|
||||||
|
{
|
||||||
|
/* FIFO EMPTY */
|
||||||
|
if(buf->_len == 0)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
*data = *buf->_reader;
|
||||||
|
++buf->_reader;
|
||||||
|
--buf->_len;
|
||||||
|
|
||||||
|
/* If end of buffer, restart to begin */
|
||||||
|
if(buf->_reader > (buf->_buffer + buf->_maxlen))
|
||||||
|
{
|
||||||
|
buf->_reader = buf->_buffer;
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t buffer_length(struct ring_buffer *buf)
|
||||||
|
{
|
||||||
|
return buf->_len;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -4,7 +4,15 @@
|
|||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
struct ring_buffer {
|
||||||
|
uint32_t _len;
|
||||||
|
uint32_t _maxlen;
|
||||||
|
uint8_t *_writer;
|
||||||
|
uint8_t *_reader;
|
||||||
|
uint8_t *_buffer;
|
||||||
|
};
|
||||||
|
|
||||||
|
#define BUFFER_STATIC_INIT(BUFF,LEN) {0, LEN, BUFF, BUFF, BUFF}
|
||||||
|
|
||||||
void init_static_ring_buffer(struct ring_buffer *buf, uint8_t *buffer, uint32_t maxlen);
|
void init_static_ring_buffer(struct ring_buffer *buf, uint8_t *buffer, uint32_t maxlen);
|
||||||
void init_buffer(struct ring_buffer *buf, uint32_t maxlen);
|
void init_buffer(struct ring_buffer *buf, uint32_t maxlen);
|
||||||
|
@ -30,71 +30,6 @@ static uint8_t __USER_MEMORY[USER_MEMORY_SIZE];
|
|||||||
volatile static int tx_it_enable = 0;
|
volatile static int tx_it_enable = 0;
|
||||||
unsigned char rx_byte;
|
unsigned char rx_byte;
|
||||||
|
|
||||||
#include <stdint.h>
|
|
||||||
|
|
||||||
#define BUFFER_LEN 64
|
|
||||||
|
|
||||||
struct ring_buffer {
|
|
||||||
uint32_t _len;
|
|
||||||
uint32_t _maxlen;
|
|
||||||
uint8_t *_writer;
|
|
||||||
uint8_t *_reader;
|
|
||||||
uint8_t *_buffer;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct ring_buffer rx_buffer;
|
|
||||||
struct ring_buffer tx_buffer;
|
|
||||||
|
|
||||||
|
|
||||||
void init_static_ring_buffer(struct ring_buffer *buf, uint8_t *buffer, uint32_t maxlen)
|
|
||||||
{
|
|
||||||
buf->_buffer = buffer;
|
|
||||||
buf->_writer = buf->_buffer;
|
|
||||||
buf->_reader = buf->_buffer;
|
|
||||||
buf->_len = 0;
|
|
||||||
buf->_maxlen = maxlen;
|
|
||||||
}
|
|
||||||
|
|
||||||
int fifo_push(struct ring_buffer *buf, uint8_t data)
|
|
||||||
{
|
|
||||||
/* FIFO FULL */
|
|
||||||
if(buf->_len >= buf->_maxlen)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
*buf->_writer = data;
|
|
||||||
++buf->_len;
|
|
||||||
++buf->_writer;
|
|
||||||
|
|
||||||
/* If end of buffer, restart to begin */
|
|
||||||
if(buf->_writer > (buf->_buffer + buf->_maxlen))
|
|
||||||
{
|
|
||||||
buf->_writer = buf->_buffer;
|
|
||||||
}
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
int fifo_pop(struct ring_buffer *buf, uint8_t *data)
|
|
||||||
{
|
|
||||||
/* FIFO EMPTY */
|
|
||||||
if(buf->_len == 0)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
*data = *buf->_reader;
|
|
||||||
++buf->_reader;
|
|
||||||
--buf->_len;
|
|
||||||
|
|
||||||
/* If end of buffer, restart to begin */
|
|
||||||
if(buf->_reader > (buf->_buffer + buf->_maxlen))
|
|
||||||
{
|
|
||||||
buf->_reader = buf->_buffer;
|
|
||||||
}
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint32_t buffer_length(struct ring_buffer *buf)
|
|
||||||
{
|
|
||||||
return buf->_len;
|
|
||||||
}
|
|
||||||
|
|
||||||
void delay(uint32_t delay)
|
void delay(uint32_t delay)
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user