123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345 |
- #ifndef HAL_BUFFERS_H
- #define HAL_BUFFERS_H
- #if !defined(BUFFERS_CHUNKS_SIZE) || defined(__DOXYGEN__)
- #define BUFFERS_CHUNKS_SIZE 64
- #endif
- #if (BUFFERS_CHUNKS_SIZE & (BUFFERS_CHUNKS_SIZE - 1)) != 0
- #error "BUFFERS_CHUNKS_SIZE must be a power of two"
- #endif
- typedef struct io_buffers_queue io_buffers_queue_t;
- typedef void (*bqnotify_t)(io_buffers_queue_t *bqp);
- struct io_buffers_queue {
-
- threads_queue_t waiting;
-
- bool suspended;
-
- volatile size_t bcounter;
-
- uint8_t *bwrptr;
-
- uint8_t *brdptr;
-
- uint8_t *btop;
-
- size_t bsize;
-
- size_t bn;
-
- uint8_t *buffers;
-
- uint8_t *ptr;
-
- uint8_t *top;
-
- bqnotify_t notify;
-
- void *link;
- };
- typedef io_buffers_queue_t input_buffers_queue_t;
- typedef io_buffers_queue_t output_buffers_queue_t;
- #define BQ_BUFFER_SIZE(n, size) \
- (((size_t)(size) + sizeof (size_t)) * (size_t)(n))
- #define bqSizeX(bqp) ((bqp)->bn)
- #define bqSpaceI(bqp) ((bqp)->bcounter)
- #define bqGetLinkX(bqp) ((bqp)->link)
- #define bqSetLinkX(bqp, lk) ((bqp)->link = lk)
- #define bqIsSuspendedX(bqp) ((bqp)->suspended)
- #define bqSuspendI(bqp) { \
- (bqp)->suspended = true; \
- osalThreadDequeueAllI(&(bqp)->waiting, MSG_RESET); \
- }
- #define bqResumeX(bqp) { \
- (bqp)->suspended = false; \
- }
- #define ibqIsEmptyI(ibqp) ((bool)(bqSpaceI(ibqp) == 0U))
- #define ibqIsFullI(ibqp) \
- \
- ((bool)(((ibqp)->bwrptr == (ibqp)->brdptr) && ((ibqp)->bcounter != 0U))) \
-
- #define obqIsEmptyI(obqp) \
- \
- ((bool)(((obqp)->bwrptr == (obqp)->brdptr) && ((obqp)->bcounter != 0U))) \
-
- #define obqIsFullI(obqp) ((bool)(bqSpaceI(obqp) == 0U))
- #ifdef __cplusplus
- extern "C" {
- #endif
- void ibqObjectInit(input_buffers_queue_t *ibqp, bool suspended, uint8_t *bp,
- size_t size, size_t n, bqnotify_t infy, void *link);
- void ibqResetI(input_buffers_queue_t *ibqp);
- uint8_t *ibqGetEmptyBufferI(input_buffers_queue_t *ibqp);
- void ibqPostFullBufferI(input_buffers_queue_t *ibqp, size_t size);
- msg_t ibqGetFullBufferTimeout(input_buffers_queue_t *ibqp,
- sysinterval_t timeout);
- msg_t ibqGetFullBufferTimeoutS(input_buffers_queue_t *ibqp,
- sysinterval_t timeout);
- void ibqReleaseEmptyBuffer(input_buffers_queue_t *ibqp);
- void ibqReleaseEmptyBufferS(input_buffers_queue_t *ibqp);
- msg_t ibqGetTimeout(input_buffers_queue_t *ibqp, sysinterval_t timeout);
- size_t ibqReadTimeout(input_buffers_queue_t *ibqp, uint8_t *bp,
- size_t n, sysinterval_t timeout);
- void obqObjectInit(output_buffers_queue_t *obqp, bool suspended, uint8_t *bp,
- size_t size, size_t n, bqnotify_t onfy, void *link);
- void obqResetI(output_buffers_queue_t *obqp);
- uint8_t *obqGetFullBufferI(output_buffers_queue_t *obqp,
- size_t *sizep);
- void obqReleaseEmptyBufferI(output_buffers_queue_t *obqp);
- msg_t obqGetEmptyBufferTimeout(output_buffers_queue_t *obqp,
- sysinterval_t timeout);
- msg_t obqGetEmptyBufferTimeoutS(output_buffers_queue_t *obqp,
- sysinterval_t timeout);
- void obqPostFullBuffer(output_buffers_queue_t *obqp, size_t size);
- void obqPostFullBufferS(output_buffers_queue_t *obqp, size_t size);
- msg_t obqPutTimeout(output_buffers_queue_t *obqp, uint8_t b,
- sysinterval_t timeout);
- size_t obqWriteTimeout(output_buffers_queue_t *obqp, const uint8_t *bp,
- size_t n, sysinterval_t timeout);
- bool obqTryFlushI(output_buffers_queue_t *obqp);
- void obqFlush(output_buffers_queue_t *obqp);
- #ifdef __cplusplus
- }
- #endif
- #endif
|