CANopenNode
CANopen protocol stack
Loading...
Searching...
No Matches
CO_fifo.h
Go to the documentation of this file.
1
21#ifndef CO_FIFO_H
22#define CO_FIFO_H
23
24#include "301/CO_driver.h"
25
26/* default configuration, see CO_config.h */
27#ifndef CO_CONFIG_FIFO
28#define CO_CONFIG_FIFO (0)
29#endif
30
31#if (((CO_CONFIG_FIFO)&CO_CONFIG_FIFO_ENABLE) != 0) || defined CO_DOXYGEN
32
33#ifdef __cplusplus
34extern "C" {
35#endif
36
55typedef struct {
57 size_t bufSize;
58 size_t writePtr;
59 size_t readPtr;
60#if (((CO_CONFIG_FIFO)&CO_CONFIG_FIFO_ALT_READ) != 0) || defined CO_DOXYGEN
61 size_t altReadPtr;
62#endif
63#if (((CO_CONFIG_FIFO)&CO_CONFIG_FIFO_ASCII_DATATYPES) != 0) || defined CO_DOXYGEN
66#endif
67} CO_fifo_t;
68
77void CO_fifo_init(CO_fifo_t* fifo, uint8_t* buf, size_t bufSize);
78
84static inline void
86 if (fifo != NULL) {
87 fifo->readPtr = 0;
88 fifo->writePtr = 0;
89#if ((CO_CONFIG_FIFO)&CO_CONFIG_FIFO_ASCII_DATATYPES) != 0
90 fifo->started = false;
91#endif
92 }
93
94 return;
95}
96
104static inline bool_t
106 if (fifo != NULL && fifo->readPtr != fifo->writePtr) {
107 fifo->readPtr = 0;
108 fifo->writePtr = 0;
109 return true;
110 }
111
112 return false;
113}
114
122static inline size_t
124 int sizeLeft = (int)fifo->readPtr - (int)fifo->writePtr - 1;
125 if (sizeLeft < 0) {
126 sizeLeft += (int)fifo->bufSize;
127 }
128
129 return (size_t)sizeLeft;
130}
131
139static inline size_t
141 int sizeOccupied = (int)fifo->writePtr - (int)fifo->readPtr;
142 if (sizeOccupied < 0) {
143 sizeOccupied += (int)fifo->bufSize;
144 }
145
146 return (size_t)sizeOccupied;
147}
148
157static inline bool_t
159 if (fifo != NULL && fifo->buf != NULL) {
160 size_t writePtrNext = fifo->writePtr + 1;
161 if (writePtrNext != fifo->readPtr && !(writePtrNext == fifo->bufSize && fifo->readPtr == 0)) {
162 fifo->buf[fifo->writePtr] = c;
163 fifo->writePtr = (writePtrNext == fifo->bufSize) ? 0 : writePtrNext;
164 return true;
165 }
166 }
167 return false;
168}
169
178static inline void
180 if (fifo != NULL && fifo->buf != NULL) {
181 fifo->buf[fifo->writePtr] = c;
182
183 if (++fifo->writePtr == fifo->bufSize) {
184 fifo->writePtr = 0;
185 }
186 if (fifo->readPtr == fifo->writePtr) {
187 if (++fifo->readPtr == fifo->bufSize) {
188 fifo->readPtr = 0;
189 }
190 }
191 }
192}
193
202static inline bool_t
204 if (fifo != NULL && buf != NULL && fifo->readPtr != fifo->writePtr) {
205 *buf = fifo->buf[fifo->readPtr];
206 if (++fifo->readPtr == fifo->bufSize) {
207 fifo->readPtr = 0;
208 }
209 return true;
210 }
211 return false;
212}
213
227size_t CO_fifo_write(CO_fifo_t* fifo, const uint8_t* buf, size_t count, uint16_t* crc);
228
243size_t CO_fifo_read(CO_fifo_t* fifo, uint8_t* buf, size_t count, bool_t* eof);
244
245#if (((CO_CONFIG_FIFO)&CO_CONFIG_FIFO_ALT_READ) != 0) || defined CO_DOXYGEN
254size_t CO_fifo_altBegin(CO_fifo_t* fifo, size_t offset);
255
263
271static inline size_t
273 int sizeOccupied = (int)fifo->writePtr - (int)fifo->altReadPtr;
274 if (sizeOccupied < 0) {
275 sizeOccupied += (int)fifo->bufSize;
276 }
277
278 return (size_t)sizeOccupied;
279}
280
295size_t CO_fifo_altRead(CO_fifo_t* fifo, uint8_t* buf, size_t count);
296#endif /* #if (CO_CONFIG_FIFO) & CO_CONFIG_FIFO_ALT_READ */
297
298#if (((CO_CONFIG_FIFO)&CO_CONFIG_FIFO_ASCII_COMMANDS) != 0) || defined CO_DOXYGEN
320
334
367size_t CO_fifo_readToken(CO_fifo_t* fifo, char* buf, size_t count, uint8_t* closed, bool_t* err);
368#endif /* (CO_CONFIG_FIFO) & CO_CONFIG_FIFO_ASCII_COMMANDS */
369
370#if (((CO_CONFIG_FIFO)&CO_CONFIG_FIFO_ASCII_DATATYPES) != 0) || defined CO_DOXYGEN
381size_t CO_fifo_readU82a(CO_fifo_t* fifo, char* buf, size_t count, bool_t end);
383size_t CO_fifo_readU162a(CO_fifo_t* fifo, char* buf, size_t count, bool_t end);
385size_t CO_fifo_readU322a(CO_fifo_t* fifo, char* buf, size_t count, bool_t end);
387size_t CO_fifo_readU642a(CO_fifo_t* fifo, char* buf, size_t count, bool_t end);
389size_t CO_fifo_readX82a(CO_fifo_t* fifo, char* buf, size_t count, bool_t end);
391size_t CO_fifo_readX162a(CO_fifo_t* fifo, char* buf, size_t count, bool_t end);
393size_t CO_fifo_readX322a(CO_fifo_t* fifo, char* buf, size_t count, bool_t end);
395size_t CO_fifo_readX642a(CO_fifo_t* fifo, char* buf, size_t count, bool_t end);
397size_t CO_fifo_readI82a(CO_fifo_t* fifo, char* buf, size_t count, bool_t end);
399size_t CO_fifo_readI162a(CO_fifo_t* fifo, char* buf, size_t count, bool_t end);
401size_t CO_fifo_readI322a(CO_fifo_t* fifo, char* buf, size_t count, bool_t end);
403size_t CO_fifo_readI642a(CO_fifo_t* fifo, char* buf, size_t count, bool_t end);
405size_t CO_fifo_readR322a(CO_fifo_t* fifo, char* buf, size_t count, bool_t end);
407size_t CO_fifo_readR642a(CO_fifo_t* fifo, char* buf, size_t count, bool_t end);
410size_t CO_fifo_readHex2a(CO_fifo_t* fifo, char* buf, size_t count, bool_t end);
415size_t CO_fifo_readVs2a(CO_fifo_t* fifo, char* buf, size_t count, bool_t end);
418size_t CO_fifo_readB642a(CO_fifo_t* fifo, char* buf, size_t count, bool_t end);
419
424#define CO_fifo_st_closed 0x01U
425#define CO_fifo_st_partial \
426 0x02U
428#define CO_fifo_st_errTok 0x10U
429#define CO_fifo_st_errVal 0x20U
430#define CO_fifo_st_errBuf 0x40U
431#define CO_fifo_st_errInt 0x80U
432#define CO_fifo_st_errMask 0xF0U
433 /* uint8_t Bitfields */
434
444size_t CO_fifo_cpyTok2U8(CO_fifo_t* dest, CO_fifo_t* src, uint8_t* status);
446size_t CO_fifo_cpyTok2U16(CO_fifo_t* dest, CO_fifo_t* src, uint8_t* status);
448size_t CO_fifo_cpyTok2U32(CO_fifo_t* dest, CO_fifo_t* src, uint8_t* status);
450size_t CO_fifo_cpyTok2U64(CO_fifo_t* dest, CO_fifo_t* src, uint8_t* status);
452size_t CO_fifo_cpyTok2I8(CO_fifo_t* dest, CO_fifo_t* src, uint8_t* status);
454size_t CO_fifo_cpyTok2I16(CO_fifo_t* dest, CO_fifo_t* src, uint8_t* status);
456size_t CO_fifo_cpyTok2I32(CO_fifo_t* dest, CO_fifo_t* src, uint8_t* status);
458size_t CO_fifo_cpyTok2I64(CO_fifo_t* dest, CO_fifo_t* src, uint8_t* status);
460size_t CO_fifo_cpyTok2R32(CO_fifo_t* dest, CO_fifo_t* src, uint8_t* status);
462size_t CO_fifo_cpyTok2R64(CO_fifo_t* dest, CO_fifo_t* src, uint8_t* status);
465size_t CO_fifo_cpyTok2Hex(CO_fifo_t* dest, CO_fifo_t* src, uint8_t* status);
469size_t CO_fifo_cpyTok2Vs(CO_fifo_t* dest, CO_fifo_t* src, uint8_t* status);
472size_t CO_fifo_cpyTok2B64(CO_fifo_t* dest, CO_fifo_t* src, uint8_t* status);
473
474#endif /* (CO_CONFIG_FIFO) & CO_CONFIG_FIFO_ASCII_DATATYPES */
475
476 /* CO_CANopen_301_fifo */
477
478#ifdef __cplusplus
479}
480#endif /* __cplusplus */
481
482#endif /* (CO_CONFIG_FIFO) & CO_CONFIG_FIFO_ENABLE */
483
484#endif /* CO_FIFO_H */
Interface between CAN hardware and CANopenNode.
size_t CO_fifo_readI82a(CO_fifo_t *fifo, char *buf, size_t count, bool_t end)
Read int8_t variable from fifo as ascii string, see CO_fifo_readU82a.
size_t CO_fifo_readI162a(CO_fifo_t *fifo, char *buf, size_t count, bool_t end)
Read int16_t variable from fifo as ascii string, see CO_fifo_readU82a.
static size_t CO_fifo_getSpace(CO_fifo_t *fifo)
Get free buffer space in CO_fifo_t object.
Definition CO_fifo.h:123
bool_t CO_fifo_CommSearch(CO_fifo_t *fifo, bool_t clear)
Search command inside FIFO.
size_t CO_fifo_cpyTok2U16(CO_fifo_t *dest, CO_fifo_t *src, uint8_t *status)
Copy ascii string to uint16_t variable, see CO_fifo_cpyTok2U8.
static bool_t CO_fifo_purge(CO_fifo_t *fifo)
Purge all data in fifo object, keep other properties.
Definition CO_fifo.h:105
size_t CO_fifo_cpyTok2I8(CO_fifo_t *dest, CO_fifo_t *src, uint8_t *status)
Copy ascii string to int8_t variable, see CO_fifo_cpyTok2U8.
size_t CO_fifo_cpyTok2U8(CO_fifo_t *dest, CO_fifo_t *src, uint8_t *status)
Read ascii string from src fifo and copy as uint8_t variable to dest fifo.
static size_t CO_fifo_altGetOccupied(CO_fifo_t *fifo)
Get alternate size of remaining data, see CO_fifo_altRead.
Definition CO_fifo.h:272
size_t CO_fifo_readX162a(CO_fifo_t *fifo, char *buf, size_t count, bool_t end)
Read uint16_t variable from fifo as ascii string, see CO_fifo_readU82a.
size_t CO_fifo_cpyTok2B64(CO_fifo_t *dest, CO_fifo_t *src, uint8_t *status)
Read ascii mime-base64 encoded string from src fifo and copy as binary data to dest fifo.
size_t CO_fifo_readX642a(CO_fifo_t *fifo, char *buf, size_t count, bool_t end)
Read uint64_t variable from fifo as ascii string, see CO_fifo_readU82a.
size_t CO_fifo_altRead(CO_fifo_t *fifo, uint8_t *buf, size_t count)
Alternate read data from CO_fifo_t object.
static size_t CO_fifo_getOccupied(CO_fifo_t *fifo)
Get size of data inside CO_fifo_t buffer object.
Definition CO_fifo.h:140
size_t CO_fifo_readToken(CO_fifo_t *fifo, char *buf, size_t count, uint8_t *closed, bool_t *err)
Get token from FIFO buffer.
size_t CO_fifo_readU82a(CO_fifo_t *fifo, char *buf, size_t count, bool_t end)
Read uint8_t variable from fifo and output as ascii string.
size_t CO_fifo_altBegin(CO_fifo_t *fifo, size_t offset)
Initializes alternate read with CO_fifo_altRead.
size_t CO_fifo_read(CO_fifo_t *fifo, uint8_t *buf, size_t count, bool_t *eof)
Read data from CO_fifo_t object.
static void CO_fifo_reset(CO_fifo_t *fifo)
Reset fifo object, make it empty.
Definition CO_fifo.h:85
size_t CO_fifo_readI322a(CO_fifo_t *fifo, char *buf, size_t count, bool_t end)
Read int32_t variable from fifo as ascii string, see CO_fifo_readU82a.
size_t CO_fifo_write(CO_fifo_t *fifo, const uint8_t *buf, size_t count, uint16_t *crc)
Write data into CO_fifo_t object.
size_t CO_fifo_readR322a(CO_fifo_t *fifo, char *buf, size_t count, bool_t end)
Read float32_t variable from fifo as ascii string, see CO_fifo_readU82a.
size_t CO_fifo_readB642a(CO_fifo_t *fifo, char *buf, size_t count, bool_t end)
Read data from fifo and output as mime-base64 encoded string.
void CO_fifo_altFinish(CO_fifo_t *fifo, uint16_t *crc)
Ends alternate read with CO_fifo_altRead and calculate crc checksum.
size_t CO_fifo_readX82a(CO_fifo_t *fifo, char *buf, size_t count, bool_t end)
Read uint8_t variable from fifo as ascii string, see CO_fifo_readU82a.
size_t CO_fifo_readVs2a(CO_fifo_t *fifo, char *buf, size_t count, bool_t end)
Read data from fifo and output as visible string.
size_t CO_fifo_cpyTok2Hex(CO_fifo_t *dest, CO_fifo_t *src, uint8_t *status)
Copy bytes written as two hex digits into to data.
size_t CO_fifo_readU642a(CO_fifo_t *fifo, char *buf, size_t count, bool_t end)
Read uint64_t variable from fifo as ascii string, see CO_fifo_readU82a.
size_t CO_fifo_cpyTok2U32(CO_fifo_t *dest, CO_fifo_t *src, uint8_t *status)
Copy ascii string to uint32_t variable, see CO_fifo_cpyTok2U8.
size_t CO_fifo_readX322a(CO_fifo_t *fifo, char *buf, size_t count, bool_t end)
Read uint32_t variable from fifo as ascii string, see CO_fifo_readU82a.
void CO_fifo_init(CO_fifo_t *fifo, uint8_t *buf, size_t bufSize)
Initialize fifo object.
size_t CO_fifo_readU162a(CO_fifo_t *fifo, char *buf, size_t count, bool_t end)
Read uint16_t variable from fifo as ascii string, see CO_fifo_readU82a.
size_t CO_fifo_readU322a(CO_fifo_t *fifo, char *buf, size_t count, bool_t end)
Read uint32_t variable from fifo as ascii string, see CO_fifo_readU82a.
size_t CO_fifo_cpyTok2Vs(CO_fifo_t *dest, CO_fifo_t *src, uint8_t *status)
Copy visible string to data.
size_t CO_fifo_cpyTok2I32(CO_fifo_t *dest, CO_fifo_t *src, uint8_t *status)
Copy ascii string to int32_t variable, see CO_fifo_cpyTok2U8.
size_t CO_fifo_cpyTok2I16(CO_fifo_t *dest, CO_fifo_t *src, uint8_t *status)
Copy ascii string to int16_t variable, see CO_fifo_cpyTok2U8.
bool_t CO_fifo_trimSpaces(CO_fifo_t *fifo, bool_t *insideComment)
Trim spaces inside FIFO.
size_t CO_fifo_cpyTok2U64(CO_fifo_t *dest, CO_fifo_t *src, uint8_t *status)
Copy ascii string to uint64_t variable, see CO_fifo_cpyTok2U8.
size_t CO_fifo_cpyTok2R64(CO_fifo_t *dest, CO_fifo_t *src, uint8_t *status)
Copy ascii string to float64_t variable, see CO_fifo_cpyTok2U8.
size_t CO_fifo_readR642a(CO_fifo_t *fifo, char *buf, size_t count, bool_t end)
Read float64_t variable from fifo as ascii string, see CO_fifo_readU82a.
static bool_t CO_fifo_getc(CO_fifo_t *fifo, uint8_t *buf)
Get one character from CO_fifo_t buffer object.
Definition CO_fifo.h:203
size_t CO_fifo_readHex2a(CO_fifo_t *fifo, char *buf, size_t count, bool_t end)
Read data from fifo and output as space separated two digit ascii string, see also CO_fifo_readU82a.
size_t CO_fifo_cpyTok2I64(CO_fifo_t *dest, CO_fifo_t *src, uint8_t *status)
Copy ascii string to int64_t variable, see CO_fifo_cpyTok2U8.
static bool_t CO_fifo_putc(CO_fifo_t *fifo, const uint8_t c)
Put one character into CO_fifo_t buffer object.
Definition CO_fifo.h:158
static void CO_fifo_putc_ov(CO_fifo_t *fifo, const uint8_t c)
Put one character into CO_fifo_t buffer object.
Definition CO_fifo.h:179
size_t CO_fifo_readI642a(CO_fifo_t *fifo, char *buf, size_t count, bool_t end)
Read int64_t variable from fifo as ascii string, see CO_fifo_readU82a.
size_t CO_fifo_cpyTok2R32(CO_fifo_t *dest, CO_fifo_t *src, uint8_t *status)
Copy ascii string to float32_t variable, see CO_fifo_cpyTok2U8.
#define NULL
NULL, for general usage.
Definition CO_driver.h:123
unsigned int uint16_t
UNSIGNED16 in CANopen (0006h), 16-bit unsigned integer.
Definition CO_driver.h:132
unsigned long int uint32_t
UNSIGNED32 in CANopen (0007h), 32-bit unsigned integer.
Definition CO_driver.h:133
uint_fast8_t bool_t
Boolean data type for general use.
Definition CO_driver.h:126
unsigned char uint8_t
UNSIGNED8 in CANopen (0005h), 8-bit unsigned integer.
Definition CO_driver.h:131
Fifo object.
Definition CO_fifo.h:55
size_t bufSize
Initialized by CO_fifo_init()
Definition CO_fifo.h:57
size_t readPtr
Location in the buffer, which will be next read.
Definition CO_fifo.h:59
size_t altReadPtr
Location in the buffer, which will be next read.
Definition CO_fifo.h:61
size_t writePtr
Location in the buffer, which will be next written.
Definition CO_fifo.h:58
uint8_t * buf
Buffer of size bufSize.
Definition CO_fifo.h:56
uint32_t aux
auxiliary variable, used in some functions.
Definition CO_fifo.h:65
bool_t started
helper variable, set to false in CO_fifo_reset(), used in some functions.
Definition CO_fifo.h:64