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