Zephyr Project API  3.1.0
A Scalable Open Source RTOS
util.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2011-2014, Wind River Systems, Inc.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
14#ifndef ZEPHYR_INCLUDE_SYS_UTIL_H_
15#define ZEPHYR_INCLUDE_SYS_UTIL_H_
16
18
19/* needs to be outside _ASMLANGUAGE so 'true' and 'false' can turn
20 * into '1' and '0' for asm or linker scripts
21 */
22#include <stdbool.h>
23
24#ifndef _ASMLANGUAGE
25
26#include <zephyr/types.h>
27#include <stddef.h>
28
29#ifdef __cplusplus
30extern "C" {
31#endif
32
39#define POINTER_TO_UINT(x) ((uintptr_t) (x))
41#define UINT_TO_POINTER(x) ((void *) (uintptr_t) (x))
43#define POINTER_TO_INT(x) ((intptr_t) (x))
45#define INT_TO_POINTER(x) ((void *) (intptr_t) (x))
46
47#if !(defined(__CHAR_BIT__) && defined(__SIZEOF_LONG__))
48# error Missing required predefined macros for BITS_PER_LONG calculation
49#endif
50
52#define BITS_PER_LONG (__CHAR_BIT__ * __SIZEOF_LONG__)
53
58#define GENMASK(h, l) \
59 (((~0UL) - (1UL << (l)) + 1) & (~0UL >> (BITS_PER_LONG - 1 - (h))))
60
62#define LSB_GET(value) ((value) & -(value))
63
68#define FIELD_GET(mask, value) (((value) & (mask)) / LSB_GET(mask))
69
75#define FIELD_PREP(mask, value) (((value) * LSB_GET(mask)) & (mask))
76
78#define ZERO_OR_COMPILE_ERROR(cond) ((int) sizeof(char[1 - 2 * !(cond)]) - 1)
79
80#if defined(__cplusplus)
81
82/* The built-in function used below for type checking in C is not
83 * supported by GNU C++.
84 */
85#define ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0]))
86
87#else /* __cplusplus */
88
94#define IS_ARRAY(array) \
95 ZERO_OR_COMPILE_ERROR( \
96 !__builtin_types_compatible_p(__typeof__(array), \
97 __typeof__(&(array)[0])))
98
108#define ARRAY_SIZE(array) \
109 ((size_t) (IS_ARRAY(array) + (sizeof(array) / sizeof((array)[0]))))
110
111#endif /* __cplusplus */
112
123#define PART_OF_ARRAY(array, ptr) \
124 ((ptr) && ((ptr) >= &array[0] && (ptr) < &array[ARRAY_SIZE(array)]))
125
147#define CONTAINER_OF(ptr, type, field) \
148 ((type *)(((char *)(ptr)) - offsetof(type, field)))
149
154#define ROUND_UP(x, align) \
155 (((unsigned long)(x) + ((unsigned long)(align) - 1)) & \
156 ~((unsigned long)(align) - 1))
157
162#define ROUND_DOWN(x, align) \
163 ((unsigned long)(x) & ~((unsigned long)(align) - 1))
164
166#define WB_UP(x) ROUND_UP(x, sizeof(void *))
167
169#define WB_DN(x) ROUND_DOWN(x, sizeof(void *))
170
174#define ceiling_fraction(numerator, divider) \
175 (((numerator) + ((divider) - 1)) / (divider))
176
177#ifndef MAX
189#define MAX(a, b) (((a) > (b)) ? (a) : (b))
190#endif
191
192#ifndef MIN
204#define MIN(a, b) (((a) < (b)) ? (a) : (b))
205#endif
206
207#ifndef CLAMP
220#define CLAMP(val, low, high) (((val) <= (low)) ? (low) : MIN(val, high))
221#endif
222
235#define IN_RANGE(val, min, max) ((val) >= (min) && (val) <= (max))
236
242static inline bool is_power_of_two(unsigned int x)
243{
244 return (x != 0U) && ((x & (x - 1U)) == 0U);
245}
246
255{
256 int64_t sign_ext;
257
258 if (shift == 0U) {
259 return value;
260 }
261
262 /* extract sign bit */
263 sign_ext = (value >> 63) & 1;
264
265 /* make all bits of sign_ext be the same as the value's sign bit */
266 sign_ext = -sign_ext;
267
268 /* shift value and fill opened bit positions with sign bit */
269 return (value >> shift) | (sign_ext << (64 - shift));
270}
271
281static inline void bytecpy(void *dst, const void *src, size_t size)
282{
283 size_t i;
284
285 for (i = 0; i < size; ++i) {
286 ((volatile uint8_t *)dst)[i] = ((volatile const uint8_t *)src)[i];
287 }
288}
289
300static inline void byteswp(void *a, void *b, size_t size)
301{
302 uint8_t t;
303 uint8_t *aa = (uint8_t *)a;
304 uint8_t *bb = (uint8_t *)b;
305
306 for (; size > 0; --size) {
307 t = *aa;
308 *aa++ = *bb;
309 *bb++ = t;
310 }
311}
312
321int char2hex(char c, uint8_t *x);
322
331int hex2char(uint8_t x, char *c);
332
343size_t bin2hex(const uint8_t *buf, size_t buflen, char *hex, size_t hexlen);
344
355size_t hex2bin(const char *hex, size_t hexlen, uint8_t *buf, size_t buflen);
356
364static inline uint8_t bcd2bin(uint8_t bcd)
365{
366 return ((10 * (bcd >> 4)) + (bcd & 0x0F));
367}
368
376static inline uint8_t bin2bcd(uint8_t bin)
377{
378 return (((bin / 10) << 4) | (bin % 10));
379}
380
394uint8_t u8_to_dec(char *buf, uint8_t buflen, uint8_t value);
395
420char *utf8_trunc(char *utf8_str);
421
434char *utf8_lcpy(char *dst, const char *src, size_t n);
435
436#ifdef __cplusplus
437}
438#endif
439
440#endif /* !_ASMLANGUAGE */
441
443#ifdef _LINKER
444/* This is used in linker scripts so need to avoid type casting there */
445#define KB(x) ((x) << 10)
446#else
447#define KB(x) (((size_t)x) << 10)
448#endif
450#define MB(x) (KB(x) << 10)
452#define GB(x) (MB(x) << 10)
453
455#define KHZ(x) ((x) * 1000)
457#define MHZ(x) (KHZ(x) * 1000)
458
474#define WAIT_FOR(expr, timeout, delay_stmt) \
475 ({ \
476 uint32_t cycle_count = (sys_clock_hw_cycles_per_sec() / USEC_PER_SEC) * (timeout); \
477 uint32_t start = k_cycle_get_32(); \
478 while (!(expr) && (cycle_count > (k_cycle_get_32() - start))) { \
479 delay_stmt; \
480 } \
481 (expr); \
482 })
483
488#endif /* ZEPHYR_INCLUDE_SYS_UTIL_H_ */
irp nz macro MOVR cc s mov cc s endm endr irp aa
Definition: asm-macro-32-bit-gnu.h:16
char * utf8_trunc(char *utf8_str)
Properly truncate a NULL-terminated UTF-8 string.
static int64_t arithmetic_shift_right(int64_t value, uint8_t shift)
Arithmetic shift right.
Definition: util.h:254
size_t hex2bin(const char *hex, size_t hexlen, uint8_t *buf, size_t buflen)
Convert a hexadecimal string into a binary array.
static void bytecpy(void *dst, const void *src, size_t size)
byte by byte memcpy.
Definition: util.h:281
char * utf8_lcpy(char *dst, const char *src, size_t n)
Copies a UTF-8 encoded string from src to dst.
static uint8_t bin2bcd(uint8_t bin)
Convert a binary value to binary coded decimal (BCD 8421).
Definition: util.h:376
static void byteswp(void *a, void *b, size_t size)
byte by byte swap.
Definition: util.h:300
int hex2char(uint8_t x, char *c)
Convert a single hexadecimal nibble into a character.
static uint8_t bcd2bin(uint8_t bcd)
Convert a binary coded decimal (BCD 8421) value to binary.
Definition: util.h:364
int char2hex(char c, uint8_t *x)
Convert a single character into a hexadecimal nibble.
uint8_t u8_to_dec(char *buf, uint8_t buflen, uint8_t value)
Convert a uint8_t into a decimal string representation.
static bool is_power_of_two(unsigned int x)
Is x a power of two?
Definition: util.h:242
size_t bin2hex(const uint8_t *buf, size_t buflen, char *hex, size_t hexlen)
Convert a binary array into string representation.
struct k_thread t
Definition: kobject.c:1333
uint32_t hex
Definition: printk.c:77
char c
Definition: printk.c:71
__UINT8_TYPE__ uint8_t
Definition: stdint.h:88
__INT64_TYPE__ int64_t
Definition: stdint.h:75
Macro utilities.