• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  ****************************************************************************************
3  *
4  * @file utility.c
5  *
6  * @brief Header file - utility
7  *
8  ****************************************************************************************
9  * @attention
10   #####Copyright (c) 2019 GOODIX
11   All rights reserved.
12 
13     Redistribution and use in source and binary forms, with or without
14     modification, are permitted provided that the following conditions are met:
15   * Redistributions of source code must retain the above copyright
16     notice, this list of conditions and the following disclaimer.
17   * Redistributions in binary form must reproduce the above copyright
18     notice, this list of conditions and the following disclaimer in the
19     documentation and/or other materials provided with the distribution.
20   * Neither the name of GOODIX nor the names of its contributors may be used
21     to endorse or promote products derived from this software without
22     specific prior written permission.
23 
24   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27   ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
28   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34   POSSIBILITY OF SUCH DAMAGE.
35  *****************************************************************************************
36  */
37 
38 #ifndef __UTILITY_H__
39 #define __UTILITY_H__
40 
41 #include <stdint.h>
42 
43 /**
44  * @defgroup UTILITY_MAROC Defines
45  * @{
46  */
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
50 
51 #define pbDATA(addr)           ((uint8_t   *) (addr))          /* byte pointer */
52 #define pwDATA(addr)           ((uint16_t  *) (addr))          /* word pointer */
53 #define pdwDATA(addr)          ((uint32_t  *) (addr))          /* double word pointer */
54 
55 #define pcDATA(addr)           ((int8_t   *) (addr))           /* char pointer */
56 #define psDATA(addr)           ((int16_t  *) (addr))           /* short pointer */
57 #define plDATA(addr)           ((int32_t  *) (addr))           /* long pointer */
58 
59 #define bDATA(addr)            (*(volatile uint8_t *) (addr))   /* byte */
60 #define wDATA(addr)            (*(volatile uint16_t *) (addr))  /* word */
61 #define dwDATA(addr)           (*(volatile uint32_t *) (addr))  /* double word */
62 
63 #define cDATA(addr)            (*(volatile int8_t *) (addr))    /* char */
64 #define sDATA(addr)            (*(volatile int16_t *) (addr))   /* short */
65 #define lDATA(addr)            (*(volatile int32_t *) (addr))   /* ong */
66 
67 #define W_SUCCESS    (uint16_t)0
68 #define W_FAIL       ((uint16_t)0xFFFF)
69 
70 #define B_SUCCESS    (uint8_t)0
71 #define B_FAIL       ((uint8_t)0xFF)
72 
73 #ifndef BV
BV(uint32_t n)74 static inline uint8_t BV(uint32_t n)
75 {
76     return (uint8_t)(1 << n);
77 }
78 #endif
79 
80 #ifndef BF
BF(uint32_t x,uint32_t b,uint32_t s)81 static inline uint8_t BF(uint32_t x, uint32_t b, uint32_t s)
82 {
83     return ((uint8_t)((x) & (b)) >> (s));
84 }
85 #endif
86 
87 #ifndef MIN
MIN(uint32_t n,uint32_t m)88 static inline uint32_t MIN(uint32_t n, uint32_t m)
89 {
90     return (((n) < (m)) ? (n) : (m));
91 }
92 #endif
93 
94 #ifndef MAX
MAX(uint32_t n,uint32_t m)95 static inline uint32_t MAX(uint32_t n, uint32_t m)
96 {
97     return (((n) < (m)) ? (m) : (n));
98 }
99 #endif
100 
101 #ifndef ABS
ABS(int32_t n)102 static inline uint32_t ABS(int32_t n)
103 {
104     return (((n) < 0) ? -(n) : (n));
105 }
106 #endif
107 
108 #ifndef ALIGN_NUM
109 #define ALIGN_NUM(align, num) (((num) - 1) + (align) - (((num) - 1) % (align)))
110 #endif
111 
112 #define BIT_MASK(n)     (uint8_t)(((1) << (n)) - 1)
113 
114 /* takes a byte out of a uint32:var -uint32, ByteNum - byte tao take out(0-3) */
115 #define BREAK_U32(var, ByteNum) (uint8_t)((uint32_t)(((var) >> ((uint8_t)((ByteNum) * 8))) & 0x00FF))
116 
117 #define BUILD_U32(Byte0, Byte1, Byte2, Byte3) \
118         ((uint32_t)((uint32_t)((Byte0) & 0x00FF) + \
119         ((uint32_t)((Byte1) & 0x00FF) << 8) + \
120         ((uint32_t)((Byte2) & 0x00FF) << 16) + \
121         ((uint32_t)((Byte3) & 0x00FF) << 24)))
122 
123 #define HI_UINT32_T(a) (((a) >> 24) & 0xFF)
124 #define L3_UINT32_T(a) (((a) >> 16) & 0xFF)
125 #define L2_UINT32_T(a) (((a) >> 8) & 0xFF)
126 #define LO_UINT32_T(a) ((a) & 0xFF)
127 
128 #define BUILD_U16(loByte, hiByte)  ((uint16_t)(((loByte) & 0x00FF) + (((hiByte) & 0x00FF) << 8)))
129 
130 #define HI_U16(a) (uint8_t)(((uint16_t)(a) >> 8) & 0xFF)
131 #define LO_U16(a) (uint8_t)((uint16_t)(a) & 0xFF)
132 
133 #define BUILD_U8(hiByte, loByte) ((uint8_t)(((loByte) & 0x0F) + (((hiByte) & 0x0F) << 4)))
134 
135 #ifndef HI_U8
136 #define HI_U8(a) (((uint8_t)(a) >> 4) & 0x0F)
137 #endif
138 
139 #ifndef LO_U8
140 #define LO_U8(a) ((uint8_t)(a) & 0x0F)
141 #endif
142 
143 #ifndef GET_BIT
144 #define GET_BIT(var, Idx)  (((uint8_t)(var) & BV((Idx) % 8)) ? 1 : 0)
145 #endif
146 
147 #ifndef SET_BIT
148 #define SET_BIT(var, Idx)  ((uint8_t)(var) |= BV(((Idx) % 8)))
149 #endif
150 
151 #ifndef CLR_BIT
152 #define CLR_BIT(var, Idx)  ((uint8_t)(var) &= ((BV((Idx) % 8) ^ 0xFF)))
153 #endif
154 
155 #ifndef GET_BITFIELD
156 #define GET_BITFIELD(var, MSB, LSB)  ((uint8_t)((var) << (7 - (MSB))) >> ((7 - (MSB)) + (LSB)))
157 #endif
158 
159 #ifndef SET_BITFIELD
160 #define SET_BITFIELD(var, MSB, LSB, value)  ((uint8_t)(var) = \
161         (uint8_t)(((var) & (~(BIT_MASK(((MSB) - (LSB)))<<(LSB)))) | (((value) & BIT_MASK(((MSB) - (LSB)))) << (LSB))))
162 #endif
163 
164 #ifndef CLR_BITFIELD
165 #define CLR_BITFIELD(var, MSB, LSB)  ((uint8_t)(var) &=  (uint8_t)(~(BIT_MASK((MSB) - (LSB))<<(LSB))))
166 #endif
167 
168 #ifndef CONTAINER_OF
169 #define CONTAINER_OF(ptr, type, field) \
170     ((type *)(((char *)(ptr)) - offsetof(type, field)))
171 #endif
172 
173 #ifndef ARRAY_SIZE
174 #define ARRAY_SIZE(array) \
175     ((unsigned long) ((sizeof(array) / sizeof((array)[0]))))
176 #endif
177 
178 #define UNUSED_VARIABLE(x)  ((void)(x))
179 #define UNUSED_PARAMETER(x) UNUSED_VARIABLE(x)
180 #define UNUSED_RETURN_VALUE(x) UNUSED_VARIABLE(x)
181 
182 #undef htole16
183 #undef htole32
184 #undef htole64
185 #undef le16toh
186 #undef le32toh
187 #undef le64toh
188 #undef htobe16
189 #undef htobe32
190 #undef htobe64
191 #undef be16toh
192 #undef be32toh
193 #undef be64toh
194 #undef get_u8_inc
195 #undef get_u16_inc
196 #undef get_u32_inc
197 #undef put_u8_inc
198 #undef put_u16_inc
199 #undef put_u32_inc
200 /** @} */
201 
202 /**
203  * @defgroup UTILITY_FUNCTION Functions
204  * @{
205  */
206 /**
207  *****************************************************************************************
208  * @brief Function for transforming a 16 digit number into a array according to Little-Endian.
209  *
210  * @param[out] p_buf: Pointer to a array.
211  * @param[in]  x:     The 16 digit number need to be transformed.
212  *****************************************************************************************
213  */
214 void htole16(uint8_t *p_buf, uint16_t x);
215 
216 /**
217  *****************************************************************************************
218  * @brief Function for transforming a 32 digit number into a array according to Little-Endian.
219  *
220  * @param[out] p_buf: Pointer to a array.
221  * @param[in]  x:     The 32 digit number need to be transformed.
222  *****************************************************************************************
223  */
224 void htole32(uint8_t *p_buf, uint32_t x);
225 
226 /**
227  *****************************************************************************************
228  * @brief Function for transforming a 64 digit number into a array according to Little-Endian.
229  *
230  * @param[out] p_buf: Pointer to a array.
231  * @param[in]  x:     The 64 digit number need to be transformed.
232  *****************************************************************************************
233  */
234 void htole64(uint8_t *p_buf, uint64_t x);
235 
236 /**
237  *****************************************************************************************
238  * @brief Function for transforming a array into a 16 digit unsigned number according to Little-Endian.
239  *
240  * @param[in] p_buf: Pointer to a array need to be transformed.
241  *
242  * @retval ::The result of transforming
243  *****************************************************************************************
244  */
245 uint16_t le16toh(const uint8_t *p_buf);
246 
247 /**
248  *****************************************************************************************
249  * @brief Function for transforming a array into a 32 digit unsigned number according to Little-Endian.
250  *
251  * @param[in] p_buf: Pointer to a array need to be transformed.
252  *
253  * @retval ::The result of transforming
254  *****************************************************************************************
255  */
256 uint32_t le32toh(const uint8_t *p_buf);
257 
258 /**
259  *****************************************************************************************
260  * @brief Function for transforming a array into a 64 digit unsigned number according to Little-Endian.
261  *
262  * @param[in] p_buf: Pointer to a array need to be transformed.
263  *
264  * @retval ::The result of transforming
265  *****************************************************************************************
266  */
267 uint64_t le64toh(const uint8_t *p_buf);
268 
269 /**
270  *****************************************************************************************
271  * @brief Function for transforming a 16 digit number into a array according to Big-Endian.
272  *
273  * @param[out] p_buf: Pointer to a array.
274  * @param[in]  x:     The 16 digit number need to be transformed.
275  *****************************************************************************************
276  */
277 void htobe16(uint8_t *p_buf, uint16_t x);
278 
279 /**
280  *****************************************************************************************
281  * @brief Function for transforming a 32 digit number into a array according to Big-Endian.
282  *
283  * @param[out] p_buf: Pointer to a array.
284  * @param[in]  x:     The 16 digit number need to be transformed.
285  *****************************************************************************************
286  */
287 void htobe32(uint8_t *p_buf, uint32_t x);
288 
289 /**
290  *****************************************************************************************
291  * @brief Function for transforming a 64 digit number into a array according to Big-Endian.
292  *
293  * @param[out] p_buf: Pointer to a array.
294  * @param[in]  x:     The 16 digit number need to be transformed.
295  *****************************************************************************************
296  */
297 void htobe64(uint8_t *p_buf, uint64_t x);
298 
299 /**
300  *****************************************************************************************
301  * @brief Function for transforming a array into a 16 digit unsigned number according to Big-Endian.
302  *
303  * @param[in] p_buf: Pointer to a array need to be transformed.
304  *
305  * @retval ::The result of transforming
306  *****************************************************************************************
307  */
308 uint16_t be16toh(const uint8_t *p_buf);
309 
310 /**
311  *****************************************************************************************
312  * @brief Function for transforming a array into a 32 digit unsigned number according to Big-Endian.
313  *
314  * @param[in] p_buf: Pointer to a array need to be transformed.
315  *
316  * @retval ::The result of transforming
317  *****************************************************************************************
318  */
319 uint32_t be32toh(const uint8_t *p_buf);
320 
321 /**
322  *****************************************************************************************
323  * @brief Function for transforming a array into a 64 digit unsigned number according to Big-Endian.
324  *
325  * @param[in] p_buf: Pointer to a array need to be transformed.
326  *
327  * @retval ::The result of transforming
328  *****************************************************************************************
329  */
330 uint64_t be64toh(const uint8_t *p_buf);
331 
332 /**
333  *****************************************************************************************
334  * @brief Function for getting the first 8 bits of an address.
335  *
336  * @param[in] pp_buf: Pointer to an address.
337  *
338  * @retval ::The result of getting
339  *****************************************************************************************
340  */
341 uint8_t get_u8_inc(const uint8_t **pp_buf);
342 
343 /**
344  *****************************************************************************************
345  * @brief Function for getting the first 16 bits of an address.
346  *
347  * @param[in] pp_buf: Pointer to an address.
348  *
349  * @retval ::The result of getting
350  *****************************************************************************************
351  */
352 uint16_t get_u16_inc(const uint8_t **pp_buf);
353 
354 /**
355  *****************************************************************************************
356  * @brief Function for getting the first 32 bits of an address.
357  *
358  * @param[in] pp_buf: Pointer to an address.
359  *
360  * @retval ::The result of getting
361  *****************************************************************************************
362  */
363 uint32_t get_u32_inc(const uint8_t **pp_buf);
364 
365 /**
366  *****************************************************************************************
367  * @brief Function for putting a 8 digit unsigned number to an address.
368  *
369  * @param[out] pp_buf: Pointer to an address.
370  * @param[in]  x:      The 8 digit number need to be transformed.
371  *****************************************************************************************
372  */
373 void put_u8_inc(uint8_t **pp_buf, uint8_t x);
374 
375 /**
376  *****************************************************************************************
377  * @brief Function for putting a 16 digit unsigned number to an address.
378  *
379  * @param[out] pp_buf: Pointer to an address.
380  * @param[in]  x:      The 16 digit number need to be transformed.
381  *****************************************************************************************
382  */
383 void put_u16_inc(uint8_t **pp_buf, uint16_t x);
384 
385 /**
386  *****************************************************************************************
387  * @brief Function for putting a 32 digit unsigned number to an address.
388  *
389  * @param[out] pp_buf: Pointer to an address.
390  * @param[in]  x:      The 32 digit number need to be transformed.
391  *****************************************************************************************
392  */
393 void put_u32_inc(uint8_t **pp_buf, uint32_t x);
394 /** @} */
395 #ifdef __cplusplus
396 }
397 #endif
398 #endif    /* __UTILITY_H__ */
399 
400