-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathRingBuffer.h
49 lines (42 loc) · 1.88 KB
/
RingBuffer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// SPDX-License-Identifier: GPL-2.0-only
/*
* Digital Voice Modem - Modem Firmware
* GPLv2 Open Source. Use is subject to license terms.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Copyright (C) 2017 Wojciech Krutnik, N0CALL
*
*/
/**
* @file RingBuffer.h
* @ingroup modem_fw
*/
#if !defined(__RING_BUFFER_H__)
#define __RING_BUFFER_H__
/*
* FIFO ring buffer source:
* http://stackoverflow.com/questions/6822548/correct-way-of-implementing-a-uart-receive-buffer-in-a-small-arm-microcontroller (modified)
*
*/
#include "Defines.h"
// ---------------------------------------------------------------------------
// Macros
// ---------------------------------------------------------------------------
#define RINGBUFF_SIZE(ringBuff) (ringBuff.size) // serial buffer in bytes (power 2)
#define RINGBUFF_MASK(ringBuff) (ringBuff.size-1U) // buffer size mask
// Buffer read / write macros
#define RINGBUFF_RESET(ringBuff) (ringBuff).rdIdx = ringBuff.wrIdx = 0
#define RINGBUFF_WRITE(ringBuff, dataIn) (ringBuff).data[RINGBUFF_MASK(ringBuff) & ringBuff.wrIdx++] = (dataIn)
#define RINGBUFF_READ(ringBuff) ((ringBuff).data[RINGBUFF_MASK(ringBuff) & ((ringBuff).rdIdx++)])
#define RINGBUFF_EMPTY(ringBuff) ((ringBuff).rdIdx == (ringBuff).wrIdx)
#define RINGBUFF_FULL(ringBuff) ((RINGBUFF_MASK(ringBuff) & ringFifo.rdIdx) == (RINGBUFF_MASK(ringBuff) & ringFifo.wrIdx))
#define RINGBUFF_COUNT(ringBuff) (RINGBUFF_MASK(ringBuff) & ((ringBuff).wrIdx - (ringBuff).rdIdx))
// Buffer type
#define DECLARE_RINGBUFFER_TYPE(name, _size) \
typedef struct { \
uint32_t size; \
uint32_t wrIdx; \
uint32_t rdIdx; \
uint8_t data[_size]; \
} name##_t
#endif // __RING_BUFFER_H__