ESOS32
ESOSOn32-bitProcessors
esos_cb.c
Go to the documentation of this file.
1 /*
2  * "Copyright (c) 2019 J. W. Bruce ("AUTHOR(S)")"
3  * All rights reserved.
4  * (J. W. Bruce, jwbruce_AT_tntech.edu, Tennessee Tech University)
5  *
6  * Permission to use, copy, modify, and distribute this software and its
7  * documentation for any purpose, without fee, and without written agreement is
8  * hereby granted, provided that the above copyright notice, the following
9  * two paragraphs and the authors appear in all copies of this software.
10  *
11  * IN NO EVENT SHALL THE "AUTHORS" BE LIABLE TO ANY PARTY FOR
12  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
13  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE "AUTHORS"
14  * HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15  *
16  * THE "AUTHORS" SPECIFICALLY DISCLAIMS ANY WARRANTIES,
17  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
18  * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
19  * ON AN "AS IS" BASIS, AND THE "AUTHORS" HAS NO OBLIGATION TO
20  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
21  *
22  * Please maintain this header in its entirety when copying/modifying
23  * these files.
24  *
25  *
26  */
27 
34 #include "esos.h"
35 #include "esos_cb.h"
36 
37 // ******** G L O B A L S ***************
38 
39 /****************************************************************
40 ** F U N C T I O N S
41 ****************************************************************/
42 void __esos_CB_Init(CBUFFER* pst_CBuffer, uint8_t* pau8_ptr, uint16_t u16_Len) {
43  // setup (or reset) the circular buffer descriptors
44  pst_CBuffer->u16_Start = 0;
45  pst_CBuffer->u16_Count = 0;
46  pst_CBuffer->u16_Length = u16_Len;
47  pst_CBuffer->pau8_Data = pau8_ptr;
48 } // endof __esos_CB_Init()
49 
50 
51 /******************************************
52 *** LOCAL HELPER MACROS
53 ******************************************/
54 #define __WRITE_CB_UINT8(pstB,u8x,u16e) \
55  do { \
56  if ((pstB)->u16_Count < (pstB)->u16_Length) { \
57  (u16e) = ((pstB)->u16_Start + (pstB)->u16_Count) % (pstB)->u16_Length; \
58  (pstB)->pau8_Data[(u16e)] = (u8x); \
59  ++(pstB)->u16_Count; \
60  } \
61  }while(0)
62 
63 #define __OVERWRITE_CB_UINT8(pstB,u8x,u16e) \
64  do { \
65  (u16e) = ((pstB)->u16_Start + (pstB)->u16_Count) % (pstB)->u16_Length; \
66  (pstB)->pau8_Data[(u16e)] = (u8x); \
67  if ((pstB)->u16_Count == (pstB)->u16_Length) \
68  (pstB)->u16_Start = ((pstB)->u16_Start + 1) % (pstB)->u16_Length; \
69  else \
70  ++(pstB)->u16_Count; \
71  }while(0)
72 
73 #define __READ_CB_UINT8(pstB,u8x) \
74  do { \
75  (u8x) = (pstB)->pau8_Data[(pstB)->u16_Start]; \
76  (pstB)->u16_Start = ((pstB)->u16_Start + 1) % (pstB)->u16_Length; \
77  --(pstB)->u16_Count; \
78  } while(0)
79 
80 
81 /***************************************************************
82 **** WRITERs
83 ***************************************************************/
84 
95 void __esos_CB_WriteUINT8(CBUFFER* pst_CBuffer, uint8_t u8_x ) {
96  uint16_t u16_end;
97 
98  __WRITE_CB_UINT8(pst_CBuffer,u8_x,u16_end);
99 } // end __esos_CB_WriteUINT8()
100 
101 void __esos_CB_OverwriteUINT8(CBUFFER* pst_CBuffer, uint8_t u8_x ) {
102  uint16_t u16_end;
103 
104  __OVERWRITE_CB_UINT8(pst_CBuffer,u8_x,u16_end);
105 } // end __esos_CB_OverwriteUINT8()
106 
107 void __esos_CB_WriteUINT16(CBUFFER* pst_CBuffer, uint16_t u16_x ) {
108  uint16_t u16_end;
109  uint8_t u8_temp;
110 
111  u8_temp = (uint8_t) u16_x & 0xFF;
112  __WRITE_CB_UINT8(pst_CBuffer,u8_temp,u16_end);
113  u8_temp = (uint8_t) (u16_x>>8);
114  __WRITE_CB_UINT8(pst_CBuffer,u8_temp,u16_end);
115 } // end __esos_CB_WriteUINT16()
116 
117 void __esos_CB_WriteUINT32(CBUFFER* pst_CBuffer, uint32_t u32_x ) {
118  uint16_t u16_end;
119  uint8_t u8_temp;
120 
121  u8_temp = (uint8_t) u32_x & 0xFF;
122  __WRITE_CB_UINT8(pst_CBuffer,u8_temp,u16_end);
123  u8_temp = (uint8_t) (u32_x>>8);
124  __WRITE_CB_UINT8(pst_CBuffer,u8_temp,u16_end);
125  u8_temp = (uint8_t) (u32_x>>16);
126  __WRITE_CB_UINT8(pst_CBuffer,u8_temp,u16_end);
127  u8_temp = (uint8_t) (u32_x>>24);
128  __WRITE_CB_UINT8(pst_CBuffer,u8_temp,u16_end);
129 } // end __esos_CB_WriteUINT32()
130 
131 void __esos_CB_WriteUINT8Buffer(CBUFFER* pst_CBuffer, uint8_t* pu8_x, uint16_t u16_size ) {
132  uint16_t u16_i, u16_end;
133 
134  for (u16_i=0; u16_i<u16_size; u16_i++) {
135  __WRITE_CB_UINT8(pst_CBuffer, pu8_x[u16_i], u16_end);
136  }
137 } // end __esos_CB_WriteUINT8Buffer()
138 
139 /***************************************************************
140 **** READERs
141 ***************************************************************/
142 uint8_t __esos_CB_ReadUINT8(CBUFFER* pst_CBuffer ) {
143  uint8_t u8_retval;
144 
145  __READ_CB_UINT8(pst_CBuffer, u8_retval);
146  return(u8_retval);
147 } // __esos_CB_ReadUINT8()
148 
149 uint16_t __esos_CB_ReadUINT16(CBUFFER* pst_CBuffer ) {
150  uint8_t u8_temp;
151  uint16_t u16_retval;
152 
153  __READ_CB_UINT8(pst_CBuffer, u8_temp);
154  u16_retval = (uint16_t) u8_temp;
155  __READ_CB_UINT8(pst_CBuffer, u8_temp);
156  u16_retval += ((uint16_t) u8_temp)<<8;
157  return (u16_retval);
158 } // __esos_CB_ReadUINT16()
159 
160 uint32_t __esos_CB_ReadUINT32(CBUFFER* pst_CBuffer ) {
161  uint8_t u8_temp;
162  uint32_t u32_retval;
163 
164  __READ_CB_UINT8(pst_CBuffer, u8_temp);
165  u32_retval = (uint32_t) u8_temp;
166  __READ_CB_UINT8(pst_CBuffer, u8_temp);
167  u32_retval += (((uint32_t) u8_temp)<<8);
168  __READ_CB_UINT8(pst_CBuffer, u8_temp);
169  u32_retval += (((uint32_t) u8_temp)<<16);
170  __READ_CB_UINT8(pst_CBuffer, u8_temp);
171  u32_retval += (((uint32_t) u8_temp)<<24);
172  return (u32_retval);
173 } // __esos_CB_ReadUINT32()
174 
175 
176 void __esos_CB_ReadUINT8Buffer(CBUFFER* pst_CBuffer, uint8_t* pu8_x, uint16_t u16_size ) {
177  uint16_t u16_i;
178 
179  for (u16_i=0; u16_i<u16_size; u16_i++) {
180  __READ_CB_UINT8(pst_CBuffer, pu8_x[u16_i]);
181  }
182 } // end __esos_CB_ReadUINT8Buffer()
__stCIRCBUFF
Definition: esos_cb.h:56
esos.h
esos_cb.h
__esos_CB_WriteUINT8
void __esos_CB_WriteUINT8(CBUFFER *pst_CBuffer, uint8_t u8_x)
Definition: esos_cb.c:95