LPCOpen Platform
LPCOpen Platform for NXP LPC Microcontrollers
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ring_buffer.c
Go to the documentation of this file.
1 /*
2  * @brief Common ring buffer support functions
3  *
4  * @note
5  * Copyright(C) NXP Semiconductors, 2012
6  * All rights reserved.
7  *
8  * @par
9  * Software that is described herein is for illustrative purposes only
10  * which provides customers with programming information regarding the
11  * LPC products. This software is supplied "AS IS" without any warranties of
12  * any kind, and NXP Semiconductors and its licensor disclaim any and
13  * all warranties, express or implied, including all implied warranties of
14  * merchantability, fitness for a particular purpose and non-infringement of
15  * intellectual property rights. NXP Semiconductors assumes no responsibility
16  * or liability for the use of the software, conveys no license or rights under any
17  * patent, copyright, mask work right, or any other intellectual property rights in
18  * or to any products. NXP Semiconductors reserves the right to make changes
19  * in the software without notification. NXP Semiconductors also makes no
20  * representation or warranty that such application will be suitable for the
21  * specified use without further testing or modification.
22  *
23  * @par
24  * Permission to use, copy, modify, and distribute this software and its
25  * documentation is hereby granted, under NXP Semiconductors' and its
26  * licensor's relevant copyrights in the software, without fee, provided that it
27  * is used in conjunction with NXP Semiconductors microcontrollers. This
28  * copyright, permission, and disclaimer notice must appear in all copies of
29  * this code.
30  */
31 
32 #include "ring_buffer.h"
33 
34 /*****************************************************************************
35  * Private types/enumerations/variables
36  ****************************************************************************/
37 
38 /*****************************************************************************
39  * Public types/enumerations/variables
40  ****************************************************************************/
41 
42 /*****************************************************************************
43  * Private functions
44  ****************************************************************************/
45 
46 /*****************************************************************************
47  * Public functions
48  ****************************************************************************/
49 
50 /* Initialize ring buffer */
51 void RingBuffer_Init(RINGBUFF_T *RingBuff, void *buffer, int itemSize, int count)
52 {
53  RingBuff->bufferBase = RingBuff->bufferIn = RingBuff->bufferOut = buffer;
54  RingBuff->bufferLast = RingBuff->bufferBase + (itemSize * count);
55  RingBuff->count = count;
56  RingBuff->itemSize = itemSize;
57  RingBuff->used = 0;
58 }
59 
60 /* Return empty status of ring buffer */
61 bool RingBuffer_Insert8(RINGBUFF_T *RingBuff, uint8_t data8)
62 {
63  bool full = RingBuffer_IsFull(RingBuff);
64 
65  if (!full) {
66  *RingBuff->bufferIn = data8;
67  RingBuff->used++;
68  RingBuff->bufferIn++;
69  if (RingBuff->bufferIn >= RingBuff->bufferLast) {
70  RingBuff->bufferIn = RingBuff->bufferBase;
71  }
72  }
73 
74  return (bool) !full;
75 }
76 
77 /* Insert 16-bit value in ring buffer */
78 bool RingBuffer_Insert16(RINGBUFF_T *RingBuff, uint16_t data16)
79 {
80  bool full = RingBuffer_IsFull(RingBuff);
81 
82  if (!full) {
83  uint16_t *buff16 = (uint16_t *) RingBuff->bufferIn;
84  *buff16 = data16;
85  RingBuff->used++;
86  buff16++;
87  RingBuff->bufferIn = (uint8_t *) buff16;
88  if (RingBuff->bufferIn >= RingBuff->bufferLast) {
89  RingBuff->bufferIn = RingBuff->bufferBase;
90  }
91  }
92 
93  return (bool) !full;
94 }
95 
96 /* Insert 32-bit value in ring buffer */
97 bool RingBuffer_Insert32(RINGBUFF_T *RingBuff, uint32_t data32)
98 {
99  bool full = RingBuffer_IsFull(RingBuff);
100 
101  if (!full) {
102  uint32_t *buff32 = (uint32_t *) RingBuff->bufferIn;
103  *buff32 = data32;
104  RingBuff->used++;
105  buff32++;
106  RingBuff->bufferIn = (uint8_t *) buff32;
107  if (RingBuff->bufferIn >= RingBuff->bufferLast) {
108  RingBuff->bufferIn = RingBuff->bufferBase;
109  }
110  }
111 
112  return (bool) !full;
113 }
114 
115 /* Pop a 8-bit value from the ring buffer */
116 bool RingBuffer_Pop8(RINGBUFF_T *RingBuff, uint8_t *data8)
117 {
118  bool empty = RingBuffer_IsEmpty(RingBuff);
119 
120  if (!empty) {
121  *data8 = *RingBuff->bufferOut;
122  RingBuff->used--;
123  RingBuff->bufferOut++;
124  if (RingBuff->bufferOut >= RingBuff->bufferLast) {
125  RingBuff->bufferOut = RingBuff->bufferBase;
126  }
127  }
128 
129  return (bool) !empty;
130 }
131 
132 /* Pop a 16-bit value from the ring buffer */
133 bool RingBuffer_Pop16(RINGBUFF_T *RingBuff, uint16_t *data16)
134 {
135  bool empty = RingBuffer_IsEmpty(RingBuff);
136 
137  if (!empty) {
138  uint16_t *buff16 = (uint16_t *) RingBuff->bufferOut;
139  *data16 = *buff16;
140  RingBuff->used--;
141  buff16++;
142  RingBuff->bufferOut = (uint8_t *) buff16;
143  if (RingBuff->bufferOut >= RingBuff->bufferLast) {
144  RingBuff->bufferOut = RingBuff->bufferBase;
145  }
146  }
147 
148  return (bool) !empty;
149 }
150 
151 /* Pop a 32-bit value from the ring buffer */
152 bool RingBuffer_Pop32(RINGBUFF_T *RingBuff, uint32_t *data32)
153 {
154  bool empty = RingBuffer_IsEmpty(RingBuff);
155 
156  if (!empty) {
157  uint32_t *buff32 = (uint32_t *) RingBuff->bufferOut;
158  *data32 = *buff32;
159  RingBuff->used--;
160  data32++;
161  RingBuff->bufferOut = (uint8_t *) data32;
162  if (RingBuff->bufferOut >= RingBuff->bufferLast) {
163  RingBuff->bufferOut = RingBuff->bufferBase;
164  }
165  }
166 
167  return (bool) !empty;
168 }