Cascoda SDK
Cascoda SDK for building software to run with CA-821x transceivers
ca821x_endian.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019, Cascoda Ltd.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * 1. Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * 3. Neither the name of the copyright holder nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
40 #ifndef DOXYGEN
41 // Force doxygen to document static inline
42 #define STATIC static
43 #endif
44 
45 #ifndef CA821X_API_INCLUDE_CA821X_ENDIAN_H_
46 #define CA821X_API_INCLUDE_CA821X_ENDIAN_H_
47 
48 #include <stdint.h>
49 
50 #ifdef __cplusplus
51 extern "C" {
52 #endif
53 
57 STATIC inline uint8_t LS_BYTE(uint16_t x)
58 {
59  return ((uint8_t)((x)&0xFF));
60 }
61 
65 STATIC inline uint8_t MS_BYTE(uint16_t x)
66 {
67  return ((uint8_t)(((x) >> 8) & 0xFF));
68 }
69 
73 STATIC inline uint8_t LS0_BYTE(uint16_t x)
74 {
75  return ((uint8_t)((x)&0xFF));
76 }
77 
81 STATIC inline uint8_t LS1_BYTE(uint32_t x)
82 {
83  return ((uint8_t)(((x) >> 8) & 0xFF));
84 }
85 
89 STATIC inline uint8_t LS2_BYTE(uint32_t x)
90 {
91  return ((uint8_t)(((x) >> 16) & 0xFF));
92 }
93 
97 STATIC inline uint8_t LS3_BYTE(uint32_t x)
98 {
99  return ((uint8_t)(((x) >> 24) & 0xFF));
100 }
101 
105 STATIC inline uint16_t GETLE16(const uint8_t *in)
106 {
107  return ((in[1] << 8) & 0xff00) | (in[0] & 0x00ff);
108 }
109 
113 STATIC inline uint32_t GETLE32(const uint8_t *in)
114 {
115  return (((uint32_t)in[3] << 24) + ((uint32_t)in[2] << 16) + ((uint32_t)in[1] << 8) + (uint32_t)in[0]);
116 }
117 
121 STATIC inline uint64_t GETLE64(const uint8_t *in)
122 {
123  return (((uint64_t)in[7] << 56) + ((uint64_t)in[6] << 48) + ((uint64_t)in[5] << 40) + ((uint64_t)in[4] << 32) +
124  ((uint64_t)in[3] << 24) + ((uint64_t)in[2] << 16) + ((uint64_t)in[1] << 8) + (uint64_t)in[0]);
125 }
126 
130 STATIC inline void PUTLE16(uint16_t in, uint8_t *out)
131 {
132  out[0] = in & 0xff;
133  out[1] = (in >> 8) & 0xff;
134 }
135 
139 STATIC inline void PUTLE32(uint32_t in, uint8_t *out)
140 {
141  out[0] = in & 0xff;
142  out[1] = (in >> 8) & 0xff;
143  out[2] = (in >> 16) & 0xff;
144  out[3] = (in >> 24) & 0xff;
145 }
146 
150 STATIC inline void PUTLE64(uint64_t in, uint8_t *out)
151 {
152  out[0] = in & 0xff;
153  out[1] = (in >> 8) & 0xff;
154  out[2] = (in >> 16) & 0xff;
155  out[3] = (in >> 24) & 0xff;
156  out[4] = (in >> 32) & 0xff;
157  out[5] = (in >> 40) & 0xff;
158  out[6] = (in >> 48) & 0xff;
159  out[7] = (in >> 56) & 0xff;
160 }
161 
165 STATIC inline uint16_t GETBE16(const uint8_t *in)
166 {
167  return ((in[0] << 8) & 0xff00) | (in[1] & 0x00ff);
168 }
169 
173 STATIC inline void PUTBE16(uint16_t in, uint8_t *out)
174 {
175  out[1] = in & 0xff;
176  out[0] = (in >> 8) & 0xff;
177 }
178 
182 STATIC inline uint32_t GETBE32(const uint8_t *in)
183 {
184  return (((uint32_t)in[0] << 24) + ((uint32_t)in[1] << 16) + ((uint32_t)in[2] << 8) + (uint32_t)in[3]);
185 }
186 
190 STATIC inline uint64_t GETBE64(const uint8_t *in)
191 {
192  return (((uint64_t)in[0] << 56) + ((uint64_t)in[1] << 48) + ((uint64_t)in[2] << 40) + ((uint64_t)in[3] << 32) +
193  ((uint64_t)in[4] << 24) + ((uint64_t)in[5] << 16) + ((uint64_t)in[6] << 8) + (uint64_t)in[7]);
194 }
195 
199 STATIC inline void PUTBE32(uint32_t in, uint8_t *out)
200 {
201  out[3] = in & 0xff;
202  out[2] = (in >> 8) & 0xff;
203  out[1] = (in >> 16) & 0xff;
204  out[0] = (in >> 24) & 0xff;
205 }
206 
210 STATIC inline void PUTBE64(uint64_t in, uint8_t *out)
211 {
212  out[7] = in & 0xff;
213  out[6] = (in >> 8) & 0xff;
214  out[5] = (in >> 16) & 0xff;
215  out[4] = (in >> 24) & 0xff;
216  out[3] = (in >> 32) & 0xff;
217  out[2] = (in >> 40) & 0xff;
218  out[1] = (in >> 48) & 0xff;
219  out[0] = (in >> 56) & 0xff;
220 }
221 
222 #ifdef __cplusplus
223 }
224 #endif
225 
230 #endif /* CA821X_API_INCLUDE_CA821X_ENDIAN_H_ */
STATIC void PUTBE16(uint16_t in, uint8_t *out)
Put a 16-bit value into a big-endian octet array.
Definition: ca821x_endian.h:173
STATIC uint64_t GETLE64(const uint8_t *in)
Extract a 64-bit value from a little-endian octet array.
Definition: ca821x_endian.h:121
STATIC uint32_t GETBE32(const uint8_t *in)
Extract a 32-bit value from a big-endian octet array.
Definition: ca821x_endian.h:182
STATIC uint64_t GETBE64(const uint8_t *in)
Extract a 64-bit value from a big-endian octet array.
Definition: ca821x_endian.h:190
STATIC uint8_t MS_BYTE(uint16_t x)
Extract the most significant octet of a 16-bit value.
Definition: ca821x_endian.h:65
STATIC uint16_t GETLE16(const uint8_t *in)
Extract a 16-bit value from a little-endian octet array.
Definition: ca821x_endian.h:105
STATIC void PUTBE64(uint64_t in, uint8_t *out)
Put a 64-bit value into a little-endian octet array.
Definition: ca821x_endian.h:210
STATIC uint32_t GETLE32(const uint8_t *in)
Extract a 32-bit value from a little-endian octet array.
Definition: ca821x_endian.h:113
STATIC uint8_t LS3_BYTE(uint32_t x)
Extract the fourth (little-endian) octet of a 32-bit value.
Definition: ca821x_endian.h:97
STATIC uint8_t LS1_BYTE(uint32_t x)
Extract the second (little-endian) octet of a 32-bit value.
Definition: ca821x_endian.h:81
STATIC uint8_t LS_BYTE(uint16_t x)
Extract the least significant octet of a 16-bit value.
Definition: ca821x_endian.h:57
STATIC void PUTLE16(uint16_t in, uint8_t *out)
Put a 16-bit value into a little-endian octet array.
Definition: ca821x_endian.h:130
STATIC uint8_t LS0_BYTE(uint16_t x)
Extract the first (little-endian) octet of a 32-bit value.
Definition: ca821x_endian.h:73
STATIC uint8_t LS2_BYTE(uint32_t x)
Extract the third (little-endian) octet of a 32-bit value.
Definition: ca821x_endian.h:89
STATIC void PUTBE32(uint32_t in, uint8_t *out)
Put a 32-bit value into a big-endian octet array.
Definition: ca821x_endian.h:199
STATIC uint16_t GETBE16(const uint8_t *in)
Extract a 16-bit value from a big-endian octet array.
Definition: ca821x_endian.h:165
STATIC void PUTLE64(uint64_t in, uint8_t *out)
Put a 64-bit value into a little-endian octet array.
Definition: ca821x_endian.h:150
STATIC void PUTLE32(uint32_t in, uint8_t *out)
Put a 32-bit value into a little-endian octet array.
Definition: ca821x_endian.h:139