CANopenNode
CO_fifo.h
Go to the documentation of this file.
1 
26 #ifndef CO_FIFO_H
27 #define CO_FIFO_H
28 
29 #include "301/CO_driver.h"
30 
31 /* default configuration, see CO_config.h */
32 #ifndef CO_CONFIG_FIFO
33 #define CO_CONFIG_FIFO (0)
34 #endif
35 
36 #if ((CO_CONFIG_FIFO) & CO_CONFIG_FIFO_ENABLE) || defined CO_DOXYGEN
37 
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41 
60 typedef struct {
62  char *buf;
64  size_t bufSize;
66  size_t writePtr;
68  size_t readPtr;
69 #if ((CO_CONFIG_FIFO) & CO_CONFIG_FIFO_ALT_READ) || defined CO_DOXYGEN
70 
71  size_t altReadPtr;
72 #endif
73 #if ((CO_CONFIG_FIFO) & CO_CONFIG_FIFO_ASCII_DATATYPES) || defined CO_DOXYGEN
74 
79 #endif
80 } CO_fifo_t;
81 
82 
83 
92 void CO_fifo_init(CO_fifo_t *fifo, char *buf, size_t bufSize);
93 
94 
100 static inline void CO_fifo_reset(CO_fifo_t *fifo) {
101  if (fifo != NULL) {
102  fifo->readPtr = 0;
103  fifo->writePtr = 0;
104 #if (CO_CONFIG_FIFO) & CO_CONFIG_FIFO_ASCII_DATATYPES
105  fifo->started = false;
106 #endif
107  }
108 
109  return;
110 }
111 
112 
120 static inline bool_t CO_fifo_purge(CO_fifo_t *fifo) {
121  if (fifo != NULL && fifo->readPtr != fifo->writePtr) {
122  fifo->readPtr = 0;
123  fifo->writePtr = 0;
124  return true;
125  }
126 
127  return false;
128 }
129 
130 
138 static inline size_t CO_fifo_getSpace(CO_fifo_t *fifo) {
139  int sizeLeft = (int)fifo->readPtr - fifo->writePtr - 1;
140  if (sizeLeft < 0) {
141  sizeLeft += fifo->bufSize;
142  }
143 
144  return (size_t) sizeLeft;
145 }
146 
147 
155 static inline size_t CO_fifo_getOccupied(CO_fifo_t *fifo) {
156  int sizeOccupied = (int)fifo->writePtr - fifo->readPtr;
157  if (sizeOccupied < 0) {
158  sizeOccupied += fifo->bufSize;
159  }
160 
161  return (size_t) sizeOccupied;
162 }
163 
164 
173 static inline bool_t CO_fifo_putc(CO_fifo_t *fifo, const char c) {
174  if (fifo != NULL && fifo->buf != NULL) {
175  size_t writePtrNext = fifo->writePtr + 1;
176  if (writePtrNext != fifo->readPtr &&
177  !(writePtrNext == fifo->bufSize && fifo->readPtr == 0))
178  {
179  fifo->buf[fifo->writePtr] = c;
180  fifo->writePtr = (writePtrNext == fifo->bufSize) ? 0 : writePtrNext;
181  return true;
182  }
183  }
184  return false;
185 }
186 
187 
196 static inline void CO_fifo_putc_ov(CO_fifo_t *fifo, const char c) {
197  if (fifo != NULL && fifo->buf != NULL) {
198  fifo->buf[fifo->writePtr] = c;
199 
200  if (++fifo->writePtr == fifo->bufSize) fifo->writePtr = 0;
201  if (fifo->readPtr == fifo->writePtr) {
202  if (++fifo->readPtr == fifo->bufSize) fifo->readPtr = 0;
203  }
204  }
205 }
206 
207 
216 static inline bool_t CO_fifo_getc(CO_fifo_t *fifo, char *buf) {
217  if (fifo != NULL && buf != NULL && fifo->readPtr != fifo->writePtr) {
218  *buf = fifo->buf[fifo->readPtr];
219  if (++fifo->readPtr == fifo->bufSize) {
220  fifo->readPtr = 0;
221  }
222  return true;
223  }
224  return false;
225 }
226 
227 
243 size_t CO_fifo_write(CO_fifo_t *fifo,
244  const char *buf,
245  size_t count,
246  uint16_t *crc);
247 
248 
265 size_t CO_fifo_read(CO_fifo_t *fifo, char *buf, size_t count, bool_t *eof);
266 
267 
268 #if ((CO_CONFIG_FIFO) & CO_CONFIG_FIFO_ALT_READ) || defined CO_DOXYGEN
269 
277 size_t CO_fifo_altBegin(CO_fifo_t *fifo, size_t offset);
278 
279 
287 void CO_fifo_altFinish(CO_fifo_t *fifo, uint16_t *crc);
288 
289 
297 static inline size_t CO_fifo_altGetOccupied(CO_fifo_t *fifo) {
298  int sizeOccupied = (int)fifo->writePtr - fifo->altReadPtr;
299  if (sizeOccupied < 0) {
300  sizeOccupied += fifo->bufSize;
301  }
302 
303  return (size_t) sizeOccupied;
304 }
305 
306 
323 size_t CO_fifo_altRead(CO_fifo_t *fifo, char *buf, size_t count);
324 #endif /* #if (CO_CONFIG_FIFO) & CO_CONFIG_FIFO_ALT_READ */
325 
326 
327 #if ((CO_CONFIG_FIFO) & CO_CONFIG_FIFO_ASCII_COMMANDS) || defined CO_DOXYGEN
328 
353 
354 
369 bool_t CO_fifo_trimSpaces(CO_fifo_t *fifo, bool_t *insideComment);
370 
371 
409 size_t CO_fifo_readToken(CO_fifo_t *fifo,
410  char *buf,
411  size_t count,
412  char *closed,
413  bool_t *err);
414 #endif /* (CO_CONFIG_FIFO) & CO_CONFIG_FIFO_ASCII_COMMANDS */
415 
416 
417 #if ((CO_CONFIG_FIFO) & CO_CONFIG_FIFO_ASCII_DATATYPES) || defined CO_DOXYGEN
418 
428 size_t CO_fifo_readU82a (CO_fifo_t *fifo, char *buf, size_t count, bool_t end);
430 size_t CO_fifo_readU162a(CO_fifo_t *fifo, char *buf, size_t count, bool_t end);
432 size_t CO_fifo_readU322a(CO_fifo_t *fifo, char *buf, size_t count, bool_t end);
434 size_t CO_fifo_readU642a(CO_fifo_t *fifo, char *buf, size_t count, bool_t end);
436 size_t CO_fifo_readX82a (CO_fifo_t *fifo, char *buf, size_t count, bool_t end);
438 size_t CO_fifo_readX162a(CO_fifo_t *fifo, char *buf, size_t count, bool_t end);
440 size_t CO_fifo_readX322a(CO_fifo_t *fifo, char *buf, size_t count, bool_t end);
442 size_t CO_fifo_readX642a(CO_fifo_t *fifo, char *buf, size_t count, bool_t end);
444 size_t CO_fifo_readI82a (CO_fifo_t *fifo, char *buf, size_t count, bool_t end);
446 size_t CO_fifo_readI162a(CO_fifo_t *fifo, char *buf, size_t count, bool_t end);
448 size_t CO_fifo_readI322a(CO_fifo_t *fifo, char *buf, size_t count, bool_t end);
450 size_t CO_fifo_readI642a(CO_fifo_t *fifo, char *buf, size_t count, bool_t end);
452 size_t CO_fifo_readR322a(CO_fifo_t *fifo, char *buf, size_t count, bool_t end);
454 size_t CO_fifo_readR642a(CO_fifo_t *fifo, char *buf, size_t count, bool_t end);
457 size_t CO_fifo_readHex2a(CO_fifo_t *fifo, char *buf, size_t count, bool_t end);
464 size_t CO_fifo_readVs2a(CO_fifo_t *fifo, char *buf, size_t count, bool_t end);
468 size_t CO_fifo_readB642a(CO_fifo_t *fifo, char *buf, size_t count, bool_t end);
469 
470 
472 typedef enum {
488 } CO_fifo_st;
489 
499 size_t CO_fifo_cpyTok2U8 (CO_fifo_t *dest, CO_fifo_t *src, CO_fifo_st *status);
501 size_t CO_fifo_cpyTok2U16(CO_fifo_t *dest, CO_fifo_t *src, CO_fifo_st *status);
503 size_t CO_fifo_cpyTok2U32(CO_fifo_t *dest, CO_fifo_t *src, CO_fifo_st *status);
505 size_t CO_fifo_cpyTok2U64(CO_fifo_t *dest, CO_fifo_t *src, CO_fifo_st *status);
507 size_t CO_fifo_cpyTok2I8 (CO_fifo_t *dest, CO_fifo_t *src, CO_fifo_st *status);
509 size_t CO_fifo_cpyTok2I16(CO_fifo_t *dest, CO_fifo_t *src, CO_fifo_st *status);
511 size_t CO_fifo_cpyTok2I32(CO_fifo_t *dest, CO_fifo_t *src, CO_fifo_st *status);
513 size_t CO_fifo_cpyTok2I64(CO_fifo_t *dest, CO_fifo_t *src, CO_fifo_st *status);
515 size_t CO_fifo_cpyTok2R32(CO_fifo_t *dest, CO_fifo_t *src, CO_fifo_st *status);
517 size_t CO_fifo_cpyTok2R64(CO_fifo_t *dest, CO_fifo_t *src, CO_fifo_st *status);
520 size_t CO_fifo_cpyTok2Hex(CO_fifo_t *dest, CO_fifo_t *src, CO_fifo_st *status);
525 size_t CO_fifo_cpyTok2Vs(CO_fifo_t *dest, CO_fifo_t *src, CO_fifo_st *status);
529 size_t CO_fifo_cpyTok2B64(CO_fifo_t *dest, CO_fifo_t *src, CO_fifo_st *status);
530 
531 #endif /* (CO_CONFIG_FIFO) & CO_CONFIG_FIFO_ASCII_DATATYPES */
532  /* CO_CANopen_301_fifo */
534 
535 #ifdef __cplusplus
536 }
537 #endif /*__cplusplus*/
538 
539 #endif /* (CO_CONFIG_FIFO) & CO_CONFIG_FIFO_ENABLE */
540 
541 #endif /* CO_FIFO_H */
CO_fifo_readI82a
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.
CO_fifo_readU642a
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.
uint32_t
unsigned long int uint32_t
UNSIGNED32 in CANopen (0007h), 32-bit unsigned integer.
Definition: CO_driver.h:155
CO_fifo_readU322a
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.
CO_fifo_st_errVal
@ CO_fifo_st_errVal
Bit is set, if value is not valid or out of limits.
Definition: CO_fifo.h:481
CO_driver.h
Interface between CAN hardware and CANopenNode.
CO_fifo_cpyTok2Hex
size_t CO_fifo_cpyTok2Hex(CO_fifo_t *dest, CO_fifo_t *src, CO_fifo_st *status)
Copy bytes written as two hex digits into to data.
CO_fifo_readU82a
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.
CO_fifo_readX82a
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.
CO_fifo_t::writePtr
size_t writePtr
Location in the buffer, which will be next written.
Definition: CO_fifo.h:66
CO_fifo_cpyTok2B64
size_t CO_fifo_cpyTok2B64(CO_fifo_t *dest, CO_fifo_t *src, CO_fifo_st *status)
Read ascii mime-base64 encoded string from src fifo and copy as binary data to dest fifo.
CO_fifo_reset
static void CO_fifo_reset(CO_fifo_t *fifo)
Reset fifo object, make it empty.
Definition: CO_fifo.h:100
uint16_t
unsigned int uint16_t
UNSIGNED16 in CANopen (0006h), 16-bit unsigned integer.
Definition: CO_driver.h:153
CO_fifo_t::altReadPtr
size_t altReadPtr
Location in the buffer, which will be next read.
Definition: CO_fifo.h:71
CO_fifo_altGetOccupied
static size_t CO_fifo_altGetOccupied(CO_fifo_t *fifo)
Get alternate size of remaining data, see CO_fifo_altRead.
Definition: CO_fifo.h:297
CO_fifo_cpyTok2U32
size_t CO_fifo_cpyTok2U32(CO_fifo_t *dest, CO_fifo_t *src, CO_fifo_st *status)
Copy ascii string to uint32_t variable, see CO_fifo_cpyTok2U8.
bool_t
unsigned char bool_t
Boolean data type for general use.
Definition: CO_driver.h:141
CO_fifo_getc
static bool_t CO_fifo_getc(CO_fifo_t *fifo, char *buf)
Get one character from CO_fifo_t buffer object.
Definition: CO_fifo.h:216
CO_fifo_t::buf
char * buf
Buffer of size bufSize.
Definition: CO_fifo.h:62
CO_fifo_readR642a
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.
CO_fifo_t::started
bool_t started
helper variable, set to false in CO_fifo_reset(), used in some functions.
Definition: CO_fifo.h:76
CO_fifo_st_errInt
@ CO_fifo_st_errInt
Bit is set, if internal error.
Definition: CO_fifo.h:485
CO_fifo_readB642a
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.
CO_fifo_readR322a
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.
CO_fifo_cpyTok2U8
size_t CO_fifo_cpyTok2U8(CO_fifo_t *dest, CO_fifo_t *src, CO_fifo_st *status)
Read ascii string from src fifo and copy as uint8_t variable to dest fifo.
CO_fifo_t::bufSize
size_t bufSize
Initialized by CO_fifo_init()
Definition: CO_fifo.h:64
CO_fifo_st_errMask
@ CO_fifo_st_errMask
Bitmask for error bits.
Definition: CO_fifo.h:487
CO_fifo_st_errBuf
@ CO_fifo_st_errBuf
Bit is set, if destination buffer is to small.
Definition: CO_fifo.h:483
CO_fifo_init
void CO_fifo_init(CO_fifo_t *fifo, char *buf, size_t bufSize)
Initialize fifo object.
CO_fifo_st
CO_fifo_st
Bitfields for status argument from CO_fifo_cpyTok2U8 function and similar.
Definition: CO_fifo.h:472
CO_fifo_readI642a
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.
CO_fifo_cpyTok2Vs
size_t CO_fifo_cpyTok2Vs(CO_fifo_t *dest, CO_fifo_t *src, CO_fifo_st *status)
Copy visible string to data.
CO_fifo_getOccupied
static size_t CO_fifo_getOccupied(CO_fifo_t *fifo)
Get size of data inside CO_fifo_t buffer object.
Definition: CO_fifo.h:155
CO_fifo_cpyTok2I32
size_t CO_fifo_cpyTok2I32(CO_fifo_t *dest, CO_fifo_t *src, CO_fifo_st *status)
Copy ascii string to int32_t variable, see CO_fifo_cpyTok2U8.
CO_fifo_readU162a
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.
CO_fifo_altBegin
size_t CO_fifo_altBegin(CO_fifo_t *fifo, size_t offset)
Initializes alternate read with CO_fifo_altRead.
CO_fifo_cpyTok2I16
size_t CO_fifo_cpyTok2I16(CO_fifo_t *dest, CO_fifo_t *src, CO_fifo_st *status)
Copy ascii string to int16_t variable, see CO_fifo_cpyTok2U8.
CO_fifo_t::readPtr
size_t readPtr
Location in the buffer, which will be next read.
Definition: CO_fifo.h:68
CO_fifo_t
Fifo object.
Definition: CO_fifo.h:60
CO_fifo_putc
static bool_t CO_fifo_putc(CO_fifo_t *fifo, const char c)
Put one character into CO_fifo_t buffer object.
Definition: CO_fifo.h:173
CO_fifo_readX642a
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.
CO_fifo_cpyTok2I8
size_t CO_fifo_cpyTok2I8(CO_fifo_t *dest, CO_fifo_t *src, CO_fifo_st *status)
Copy ascii string to int8_t variable, see CO_fifo_cpyTok2U8.
CO_fifo_st_partial
@ CO_fifo_st_partial
Bit is set, if copy was partial and more data are available.
Definition: CO_fifo.h:477
CO_fifo_cpyTok2U16
size_t CO_fifo_cpyTok2U16(CO_fifo_t *dest, CO_fifo_t *src, CO_fifo_st *status)
Copy ascii string to uint16_t variable, see CO_fifo_cpyTok2U8.
CO_fifo_write
size_t CO_fifo_write(CO_fifo_t *fifo, const char *buf, size_t count, uint16_t *crc)
Write data into CO_fifo_t object.
CO_fifo_readX322a
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.
CO_fifo_readI322a
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.
CO_fifo_readVs2a
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.
CO_fifo_purge
static bool_t CO_fifo_purge(CO_fifo_t *fifo)
Purge all data in fifo object, keep other properties.
Definition: CO_fifo.h:120
CO_fifo_read
size_t CO_fifo_read(CO_fifo_t *fifo, char *buf, size_t count, bool_t *eof)
Read data from CO_fifo_t object.
CO_fifo_altRead
size_t CO_fifo_altRead(CO_fifo_t *fifo, char *buf, size_t count)
Alternate read data from CO_fifo_t object.
CO_fifo_altFinish
void CO_fifo_altFinish(CO_fifo_t *fifo, uint16_t *crc)
Ends alternate read with CO_fifo_altRead and calculate crc checksum.
CO_fifo_cpyTok2R64
size_t CO_fifo_cpyTok2R64(CO_fifo_t *dest, CO_fifo_t *src, CO_fifo_st *status)
Copy ascii string to float64_t variable, see CO_fifo_cpyTok2U8.
NULL
#define NULL
NULL, for general usage.
Definition: CO_driver.h:135
CO_fifo_t::aux
uint32_t aux
auxiliary variable, used in some functions.
Definition: CO_fifo.h:78
CO_fifo_cpyTok2I64
size_t CO_fifo_cpyTok2I64(CO_fifo_t *dest, CO_fifo_t *src, CO_fifo_st *status)
Copy ascii string to int64_t variable, see CO_fifo_cpyTok2U8.
CO_fifo_CommSearch
bool_t CO_fifo_CommSearch(CO_fifo_t *fifo, bool_t clear)
Search command inside FIFO.
CO_fifo_putc_ov
static void CO_fifo_putc_ov(CO_fifo_t *fifo, const char c)
Put one character into CO_fifo_t buffer object.
Definition: CO_fifo.h:196
CO_fifo_cpyTok2U64
size_t CO_fifo_cpyTok2U64(CO_fifo_t *dest, CO_fifo_t *src, CO_fifo_st *status)
Copy ascii string to uint64_t variable, see CO_fifo_cpyTok2U8.
CO_fifo_readI162a
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.
CO_fifo_cpyTok2R32
size_t CO_fifo_cpyTok2R32(CO_fifo_t *dest, CO_fifo_t *src, CO_fifo_st *status)
Copy ascii string to float32_t variable, see CO_fifo_cpyTok2U8.
CO_fifo_readToken
size_t CO_fifo_readToken(CO_fifo_t *fifo, char *buf, size_t count, char *closed, bool_t *err)
Get token from FIFO buffer.
CO_fifo_st_errTok
@ CO_fifo_st_errTok
Bit is set, if no valid token found.
Definition: CO_fifo.h:479
CO_fifo_readX162a
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.
CO_fifo_readHex2a
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.
CO_fifo_st_closed
@ CO_fifo_st_closed
Bit is set, if command delimiter is reached in src.
Definition: CO_fifo.h:474
CO_fifo_getSpace
static size_t CO_fifo_getSpace(CO_fifo_t *fifo)
Get free buffer space in CO_fifo_t object.
Definition: CO_fifo.h:138
CO_fifo_trimSpaces
bool_t CO_fifo_trimSpaces(CO_fifo_t *fifo, bool_t *insideComment)
Trim spaces inside FIFO.