summaryrefslogtreecommitdiff
path: root/bsp/lpc824/Libraries/peri_driver/uart/ring_buffer.h
blob: ef02dc01f2a13e9d83e2fb0bd5989387debaa68b (plain)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
 * @brief Common ring buffer support functions
 *
 * @note
 * Copyright(C) NXP Semiconductors, 2012
 * All rights reserved.
 *
 * @par
 * Software that is described herein is for illustrative purposes only
 * which provides customers with programming information regarding the
 * LPC products.  This software is supplied "AS IS" without any warranties of
 * any kind, and NXP Semiconductors and its licensor disclaim any and
 * all warranties, express or implied, including all implied warranties of
 * merchantability, fitness for a particular purpose and non-infringement of
 * intellectual property rights.  NXP Semiconductors assumes no responsibility
 * or liability for the use of the software, conveys no license or rights under any
 * patent, copyright, mask work right, or any other intellectual property rights in
 * or to any products. NXP Semiconductors reserves the right to make changes
 * in the software without notification. NXP Semiconductors also makes no
 * representation or warranty that such application will be suitable for the
 * specified use without further testing or modification.
 *
 * @par
 * Permission to use, copy, modify, and distribute this software and its
 * documentation is hereby granted, under NXP Semiconductors' and its
 * licensor's relevant copyrights in the software, without fee, provided that it
 * is used in conjunction with NXP Semiconductors microcontrollers.  This
 * copyright, permission, and disclaimer notice must appear in all copies of
 * this code.
 */

#ifndef __RING_BUFFER_H_
#define __RING_BUFFER_H_
#include <stdint.h>
#include <string.h>

#define RINGBUF_IRQ_SAFE
#ifdef RINGBUF_IRQ_SAFE
#include <cmsis.h>
#define INIT_CRITICAL() uint32_t priMask = __get_PRIMASK()
#define ENTER_CRITICAL() __set_PRIMASK(1)
#define LEAVE_CRITICAL() __set_PRIMASK(priMask)
#else
#define INIT_CRITICAL()
#define ENTER_CRITICAL()
#define LEAVE_CRITICAL()

#endif

typedef struct
{
	uint8_t *pBuf;
	uint32_t size;
	uint32_t cnt;
	uint32_t rNdx;
	uint32_t wNdx;
  
} ring_buffer_t, RINGBUFF_T;

#ifndef MIN
#define MIN(x,y)  ((x) < (y) ? (x) : (y))
#endif /* ifndef MIN */

/**
 * @brief	Create and initialize a ring buffer
 * @param	pRB	: pointer to ring buffer instance
 * @param	pBuffer: pointer to the buffer for ring buffer data
 * @param   size: The size of buffer pointed by pBuffer
 * @return	>=0:Success ; <0:Failed
 */
int32_t RingBuf_Init(ring_buffer_t *pRB, uint8_t *pBuffer, uint32_t size);

/**
 * @brief	Write new data to buffer
 * @param	pRB	: pointer to the ring buffer instance
 * @param	pcData: point to data array that will be written to ring buffer
 * @param   dataBytes: bytes to write
 * @return	>=0:Bytes written ; <0:Failed
 * @remark  This function updates the ring buffer
 */
int32_t RingBuf_Write(ring_buffer_t* pRB, const uint8_t *pcData, uint32_t dataBytes);

/**
 * @brief	Write 1 new byte data to buffer
 * @param	pRB	: pointer to the ring buffer instance
 * @param	pcData: point to data byte that will be written to ring buffer
 * @return	1:success; otherwise failed
 * @remark  This function updates the ring buffer. Optimized for byte-by-byte write
 */
int32_t RingBuf_Write1Byte(ring_buffer_t* pRB, const uint8_t *pcData);

/**
 * @brief	Read (copy and remove) data from ring buffer
 * @param	pRB	: pointer to the ring buffer instance
 * @param	pData : pointer to data array that receives read data
 * @param   dataBytes: bytes to copy
 * @return	>=0:Bytes read ; <0:Failed
 * @remark  This function updates the ring buffer. 
 */
int32_t RingBuf_Read(ring_buffer_t* pRB, uint8_t *pData, uint32_t dataBytes);

/**
 * @brief	Read (copy and remove) 1 oldest byte data from buffer
 * @param	pRB	: pointer to the ring buffer instance
 * @param	pData: point to data byte that will receive the oldest byte
 * @return	1:success ; otherwise failed
 * @remark  This function updates the ring buffer. Optimized for byte-by-byte read
 */
int32_t RingBuf_Read1Byte(ring_buffer_t* pRB, uint8_t *pData);

/**
 * @brief	Copy but does NOT remove data from ring buffer
 * @param	pRB	: pointer to the ring buffer instance
 * @param	pData : pointer to data array that receives read data
 * @param   dataBytes: bytes to read
 * @return	>=0:Read bytes ; <0:Failed
 */
int32_t RingBuf_Copy(ring_buffer_t* pRB, uint8_t *pData, uint32_t dataBytes);

/**
 * @brief	Get data pointer to oldest byte in ring buffer, and contigous byte count 
 * @param	pRB	: pointer to the ring buffer instance
 * @param	ppData : pointer to pointer variable that will be updated to point to oldest byte
 * @param   contiguous_bytes: Congigous bytes until roll back
 * @return	>=0:Contiuous bytes until roll back or whole data (if roll back won't happen) ; <0:Failed
 * @remak   Use this function if performance is critical since it does NOT copy data
 *          Use RingBuf_Free() to free (remove) data after use
 */
int32_t RingBuf_Peek(ring_buffer_t* pRB, uint8_t **ppData);

/**
 * @brief	Free (remove) data from ring buffer
 * @param	pRB	: pointer to the ring buffer instance
 * @param	bytesToFree : Bytes to free (remove)
 * @remak  Use this function to free data after data get from RingBuf_Peek() is used
 */
int32_t RingBuf_Free(ring_buffer_t* pRB, uint32_t bytesToFree);

/**
 * @brief	Get free bytes of ring buffer
 * @param	pRB	: pointer to the ring buffer instance
 * @return	>=0:Free bytes ; <0:Failed
 */
int32_t RingBuf_GetFreeBytes(ring_buffer_t* pRB);

/**
 * @brief	Get free bytes of ring buffer
 * @param	pRB	: pointer to the ring buffer instance
 * @return	>=0:Used bytes ; <0:Failed
 */
int32_t RingBuf_GetUsedBytes(ring_buffer_t* pRB);

/**
 * @}
 */

#endif /* __RING_BUFFER_H_ */