Multi purpose buffer module.

Multipurpose ringbuffer

Since there weren't any ringbuffers available on the internet optimized for 32-Bit ARM operation without unix-calls and dynamic memory... I created one.

This module is a fixed ringbuffer, it does not allocate any memory, it can work as FIFO or LIFO or any other exotic mode depending on your imagination. With a fixed 32Bit element size it is optimized for 32 bit arm processors. Any smaller value will have overhead, any larger value will require a double entry. (not recommended)

I hope you can use it.

Information

This is not a C++ class, it is a C Module. It does work object oriented, however you cannot work on the object, you work with the object.

Import programxIFO_example

Small example for xIFO

Committer:
jeroen3
Date:
Mon Oct 28 18:39:36 2013 +0000
Revision:
0:a04dc0c57d20
Child:
2:6013f6d867e5
Initial

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jeroen3 0:a04dc0c57d20 1
jeroen3 0:a04dc0c57d20 2 /**
jeroen3 0:a04dc0c57d20 3 * @file xifo.h
jeroen3 0:a04dc0c57d20 4 * @brief xifo circular buffer
jeroen3 0:a04dc0c57d20 5 * @details xifo supplies object oriented circular buffer with 32 bit size elements. \n
jeroen3 0:a04dc0c57d20 6 * To use either as FIFO (First In First Out) or as FILO (First In Last Out)
jeroen3 0:a04dc0c57d20 7 *
jeroen3 0:a04dc0c57d20 8 * @author Jeroen Lodder
jeroen3 0:a04dc0c57d20 9 * @date April 2013
jeroen3 0:a04dc0c57d20 10 * @version 2
jeroen3 0:a04dc0c57d20 11 *
jeroen3 0:a04dc0c57d20 12 * @{
jeroen3 0:a04dc0c57d20 13 */
jeroen3 0:a04dc0c57d20 14
jeroen3 0:a04dc0c57d20 15 /**
jeroen3 0:a04dc0c57d20 16 * Code has been tested on Cortex M0 (LPC1114)
jeroen3 0:a04dc0c57d20 17 * Performance table, number of core clocks
jeroen3 0:a04dc0c57d20 18 * Measured with a timer before and after
jeroen3 0:a04dc0c57d20 19 * r1 = timer->TC
jeroen3 0:a04dc0c57d20 20 * test_function
jeroen3 0:a04dc0c57d20 21 * r2 = timer->TC
jeroen3 0:a04dc0c57d20 22 *
jeroen3 0:a04dc0c57d20 23 * Function Speed Worst Case
jeroen3 0:a04dc0c57d20 24 *
jeroen3 0:a04dc0c57d20 25 * write 69 71
jeroen3 0:a04dc0c57d20 26 * read_mr 59 59
jeroen3 0:a04dc0c57d20 27 * read_lr 63 70
jeroen3 0:a04dc0c57d20 28 * pop_mr 76 78
jeroen3 0:a04dc0c57d20 29 * pop_lr 45 45
jeroen3 0:a04dc0c57d20 30 *
jeroen3 0:a04dc0c57d20 31 */
jeroen3 0:a04dc0c57d20 32
jeroen3 0:a04dc0c57d20 33 #ifndef _xifo_H_
jeroen3 0:a04dc0c57d20 34 #define _xifo_H_
jeroen3 0:a04dc0c57d20 35
jeroen3 0:a04dc0c57d20 36 #include <inttypes.h>
jeroen3 0:a04dc0c57d20 37
jeroen3 0:a04dc0c57d20 38 #if defined(__arm__) || defined(__DOXYGEN__)
jeroen3 0:a04dc0c57d20 39 #pragma anon_unions /**< Allow unnamed unions */
jeroen3 0:a04dc0c57d20 40 #endif
jeroen3 0:a04dc0c57d20 41
jeroen3 0:a04dc0c57d20 42 /**
jeroen3 0:a04dc0c57d20 43 * @brief Circular Buffer object.
jeroen3 0:a04dc0c57d20 44 * @details This struct holds the object of a circular buffer
jeroen3 0:a04dc0c57d20 45 */
jeroen3 0:a04dc0c57d20 46 typedef struct {
jeroen3 0:a04dc0c57d20 47 /* Pointers: */
jeroen3 0:a04dc0c57d20 48 uint32_t *startpool; /**< @brief First element in pool */
jeroen3 0:a04dc0c57d20 49 uint32_t *endpool; /**< @brief Last element in pool */
jeroen3 0:a04dc0c57d20 50 uint32_t *read; /**< @brief Read pointer */
jeroen3 0:a04dc0c57d20 51 uint32_t *write; /**< @brief Write pointer */
jeroen3 0:a04dc0c57d20 52 /* Variables: */
jeroen3 0:a04dc0c57d20 53 uint32_t full; /**< @brief Flag indicating buffer is full */
jeroen3 0:a04dc0c57d20 54 uint32_t elementcount;/**< @brief Number of elements used */
jeroen3 0:a04dc0c57d20 55 uint32_t size; /**< @brief Size of buffer */
jeroen3 0:a04dc0c57d20 56 /* Locally used in functions to prevent stack use: */
jeroen3 0:a04dc0c57d20 57 /**< @brief union to prevent lvalue typecasting */
jeroen3 0:a04dc0c57d20 58 union {
jeroen3 0:a04dc0c57d20 59 uint32_t temp; /**< @brief temp variable and padding for even sized block */
jeroen3 0:a04dc0c57d20 60 uint32_t *ptemp; /**< @brief temp variable and padding for even sized block */
jeroen3 0:a04dc0c57d20 61 };
jeroen3 0:a04dc0c57d20 62 }xifo_t;
jeroen3 0:a04dc0c57d20 63
jeroen3 0:a04dc0c57d20 64 /**
jeroen3 0:a04dc0c57d20 65 * @brief Circular Buffer memory pool type.
jeroen3 0:a04dc0c57d20 66 */
jeroen3 0:a04dc0c57d20 67 typedef unsigned int xifo_pool_t;
jeroen3 0:a04dc0c57d20 68
jeroen3 0:a04dc0c57d20 69 #ifdef __cplusplus
jeroen3 0:a04dc0c57d20 70 extern "C" {
jeroen3 0:a04dc0c57d20 71 #endif
jeroen3 0:a04dc0c57d20 72 /* xifo Common */
jeroen3 0:a04dc0c57d20 73 void xifo_init(xifo_t *c, uint32_t size, uint32_t *startpool);
jeroen3 0:a04dc0c57d20 74 void xifo_clear(xifo_t *c);
jeroen3 0:a04dc0c57d20 75 uint32_t xifo_write(xifo_t *c, uint32_t data);
jeroen3 0:a04dc0c57d20 76 /* FIFO use */
jeroen3 0:a04dc0c57d20 77 uint32_t xifo_read_lr(xifo_t *c, uint32_t index);
jeroen3 0:a04dc0c57d20 78 uint32_t xifo_pop_lr(xifo_t *c);
jeroen3 0:a04dc0c57d20 79 /* LIFO use */
jeroen3 0:a04dc0c57d20 80 uint32_t xifo_read_mr(xifo_t *c, uint32_t index);
jeroen3 0:a04dc0c57d20 81 uint32_t xifo_pop_mr(xifo_t *c);
jeroen3 0:a04dc0c57d20 82 /* Extractors */
jeroen3 0:a04dc0c57d20 83 uint32_t xifo_get_size(xifo_t *c);
jeroen3 0:a04dc0c57d20 84 uint32_t xifo_get_used(xifo_t *c);
jeroen3 0:a04dc0c57d20 85 uint32_t xifo_get_full(xifo_t *c);
jeroen3 0:a04dc0c57d20 86 uint32_t xifo_get_free(xifo_t *c);
jeroen3 0:a04dc0c57d20 87 #ifdef __cplusplus
jeroen3 0:a04dc0c57d20 88 }
jeroen3 0:a04dc0c57d20 89 #endif
jeroen3 0:a04dc0c57d20 90 #endif //_xifo_H_
jeroen3 0:a04dc0c57d20 91
jeroen3 0:a04dc0c57d20 92 /** @} */