Update platform drivers
inc/util.h@10:b5115cd6b916, 2020-06-17 (annotated)
- Committer:
- EndaKilgarriff
- Date:
- Wed Jun 17 14:54:14 2020 +0000
- Revision:
- 10:b5115cd6b916
- Parent:
- 9:9e247b9c9abf
Roll back delay.cpp to previous version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
EndaKilgarriff | 9:9e247b9c9abf | 1 | /***************************************************************************//** |
EndaKilgarriff | 9:9e247b9c9abf | 2 | * @file util.h |
EndaKilgarriff | 9:9e247b9c9abf | 3 | * @brief Implementation of utility functions. |
EndaKilgarriff | 9:9e247b9c9abf | 4 | * @author DBogdan (dragos.bogdan@analog.com) |
EndaKilgarriff | 9:9e247b9c9abf | 5 | ******************************************************************************** |
EndaKilgarriff | 9:9e247b9c9abf | 6 | * Copyright 2018, 2020(c) Analog Devices, Inc. |
EndaKilgarriff | 9:9e247b9c9abf | 7 | * |
EndaKilgarriff | 9:9e247b9c9abf | 8 | * All rights reserved. |
EndaKilgarriff | 9:9e247b9c9abf | 9 | * |
EndaKilgarriff | 9:9e247b9c9abf | 10 | * Redistribution and use in source and binary forms, with or without |
EndaKilgarriff | 9:9e247b9c9abf | 11 | * modification, are permitted provided that the following conditions are met: |
EndaKilgarriff | 9:9e247b9c9abf | 12 | * - Redistributions of source code must retain the above copyright |
EndaKilgarriff | 9:9e247b9c9abf | 13 | * notice, this list of conditions and the following disclaimer. |
EndaKilgarriff | 9:9e247b9c9abf | 14 | * - Redistributions in binary form must reproduce the above copyright |
EndaKilgarriff | 9:9e247b9c9abf | 15 | * notice, this list of conditions and the following disclaimer in |
EndaKilgarriff | 9:9e247b9c9abf | 16 | * the documentation and/or other materials provided with the |
EndaKilgarriff | 9:9e247b9c9abf | 17 | * distribution. |
EndaKilgarriff | 9:9e247b9c9abf | 18 | * - Neither the name of Analog Devices, Inc. nor the names of its |
EndaKilgarriff | 9:9e247b9c9abf | 19 | * contributors may be used to endorse or promote products derived |
EndaKilgarriff | 9:9e247b9c9abf | 20 | * from this software without specific prior written permission. |
EndaKilgarriff | 9:9e247b9c9abf | 21 | * - The use of this software may or may not infringe the patent rights |
EndaKilgarriff | 9:9e247b9c9abf | 22 | * of one or more patent holders. This license does not release you |
EndaKilgarriff | 9:9e247b9c9abf | 23 | * from the requirement that you obtain separate licenses from these |
EndaKilgarriff | 9:9e247b9c9abf | 24 | * patent holders to use this software. |
EndaKilgarriff | 9:9e247b9c9abf | 25 | * - Use of the software either in source or binary form, must be run |
EndaKilgarriff | 9:9e247b9c9abf | 26 | * on or directly connected to an Analog Devices Inc. component. |
EndaKilgarriff | 9:9e247b9c9abf | 27 | * |
EndaKilgarriff | 9:9e247b9c9abf | 28 | * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR |
EndaKilgarriff | 9:9e247b9c9abf | 29 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, |
EndaKilgarriff | 9:9e247b9c9abf | 30 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
EndaKilgarriff | 9:9e247b9c9abf | 31 | * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, |
EndaKilgarriff | 9:9e247b9c9abf | 32 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
EndaKilgarriff | 9:9e247b9c9abf | 33 | * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR |
EndaKilgarriff | 9:9e247b9c9abf | 34 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
EndaKilgarriff | 9:9e247b9c9abf | 35 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
EndaKilgarriff | 9:9e247b9c9abf | 36 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
EndaKilgarriff | 9:9e247b9c9abf | 37 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
EndaKilgarriff | 9:9e247b9c9abf | 38 | *******************************************************************************/ |
EndaKilgarriff | 9:9e247b9c9abf | 39 | #ifndef UTIL_H_ |
EndaKilgarriff | 9:9e247b9c9abf | 40 | #define UTIL_H_ |
EndaKilgarriff | 9:9e247b9c9abf | 41 | |
EndaKilgarriff | 9:9e247b9c9abf | 42 | /******************************************************************************/ |
EndaKilgarriff | 9:9e247b9c9abf | 43 | /***************************** Include Files **********************************/ |
EndaKilgarriff | 9:9e247b9c9abf | 44 | /******************************************************************************/ |
EndaKilgarriff | 9:9e247b9c9abf | 45 | #include <stdint.h> |
EndaKilgarriff | 9:9e247b9c9abf | 46 | #include <stdbool.h> |
EndaKilgarriff | 9:9e247b9c9abf | 47 | /******************************************************************************/ |
EndaKilgarriff | 9:9e247b9c9abf | 48 | /********************** Macros and Constants Definitions **********************/ |
EndaKilgarriff | 9:9e247b9c9abf | 49 | /******************************************************************************/ |
EndaKilgarriff | 9:9e247b9c9abf | 50 | #define BIT(x) (1 << (x)) |
EndaKilgarriff | 9:9e247b9c9abf | 51 | |
EndaKilgarriff | 9:9e247b9c9abf | 52 | #define ARRAY_SIZE(x) \ |
EndaKilgarriff | 9:9e247b9c9abf | 53 | (sizeof(x) / sizeof((x)[0])) |
EndaKilgarriff | 9:9e247b9c9abf | 54 | |
EndaKilgarriff | 9:9e247b9c9abf | 55 | #define DIV_ROUND_UP(x,y) \ |
EndaKilgarriff | 9:9e247b9c9abf | 56 | (((x) + (y) - 1) / (y)) |
EndaKilgarriff | 9:9e247b9c9abf | 57 | #define DIV_ROUND_CLOSEST(x, y) \ |
EndaKilgarriff | 9:9e247b9c9abf | 58 | (((x) + (y) / 2) / (y)) |
EndaKilgarriff | 9:9e247b9c9abf | 59 | #define DIV_ROUND_CLOSEST_ULL(x, y) \ |
EndaKilgarriff | 9:9e247b9c9abf | 60 | DIV_ROUND_CLOSEST(x, y) |
EndaKilgarriff | 9:9e247b9c9abf | 61 | |
EndaKilgarriff | 9:9e247b9c9abf | 62 | #define min(x, y) \ |
EndaKilgarriff | 9:9e247b9c9abf | 63 | (((x) < (y)) ? (x) : (y)) |
EndaKilgarriff | 9:9e247b9c9abf | 64 | #define min_t(type, x, y) \ |
EndaKilgarriff | 9:9e247b9c9abf | 65 | (type)min((type)(x), (type)(y)) |
EndaKilgarriff | 9:9e247b9c9abf | 66 | |
EndaKilgarriff | 9:9e247b9c9abf | 67 | #define max(x, y) \ |
EndaKilgarriff | 9:9e247b9c9abf | 68 | (((x) > (y)) ? (x) : (y)) |
EndaKilgarriff | 9:9e247b9c9abf | 69 | #define max_t(type, x, y) \ |
EndaKilgarriff | 9:9e247b9c9abf | 70 | (type)max((type)(x), (type)(y)) |
EndaKilgarriff | 9:9e247b9c9abf | 71 | |
EndaKilgarriff | 9:9e247b9c9abf | 72 | #define clamp(val, min_val, max_val) \ |
EndaKilgarriff | 9:9e247b9c9abf | 73 | (max(min((val), (max_val)), (min_val))) |
EndaKilgarriff | 9:9e247b9c9abf | 74 | #define clamp_t(type, val, min_val, max_val) \ |
EndaKilgarriff | 9:9e247b9c9abf | 75 | (type)clamp((type)(val), (type)(min_val), (type)(max_val)) |
EndaKilgarriff | 9:9e247b9c9abf | 76 | |
EndaKilgarriff | 9:9e247b9c9abf | 77 | #define abs(x) \ |
EndaKilgarriff | 9:9e247b9c9abf | 78 | ((x) < 0 ? (-(x)) : (x)) |
EndaKilgarriff | 9:9e247b9c9abf | 79 | |
EndaKilgarriff | 9:9e247b9c9abf | 80 | #define swap(x, y) \ |
EndaKilgarriff | 9:9e247b9c9abf | 81 | {typeof(x) _tmp_ = (x); (x) = (y); (y) = _tmp_;} |
EndaKilgarriff | 9:9e247b9c9abf | 82 | |
EndaKilgarriff | 9:9e247b9c9abf | 83 | #define round_up(x,y) \ |
EndaKilgarriff | 9:9e247b9c9abf | 84 | (((x)+(y)-1)/(y)) |
EndaKilgarriff | 9:9e247b9c9abf | 85 | |
EndaKilgarriff | 9:9e247b9c9abf | 86 | #define BITS_PER_LONG 32 |
EndaKilgarriff | 9:9e247b9c9abf | 87 | |
EndaKilgarriff | 9:9e247b9c9abf | 88 | #define GENMASK(h, l) ({ \ |
EndaKilgarriff | 9:9e247b9c9abf | 89 | uint32_t t = (~0UL); \ |
EndaKilgarriff | 9:9e247b9c9abf | 90 | t = t << (BITS_PER_LONG - (h - l + 1)); \ |
EndaKilgarriff | 9:9e247b9c9abf | 91 | t = t >> (BITS_PER_LONG - (h + 1)); \ |
EndaKilgarriff | 9:9e247b9c9abf | 92 | t; \ |
EndaKilgarriff | 9:9e247b9c9abf | 93 | }) |
EndaKilgarriff | 9:9e247b9c9abf | 94 | |
EndaKilgarriff | 9:9e247b9c9abf | 95 | #define bswap_constant_32(x) \ |
EndaKilgarriff | 9:9e247b9c9abf | 96 | ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \ |
EndaKilgarriff | 9:9e247b9c9abf | 97 | (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24)) |
EndaKilgarriff | 9:9e247b9c9abf | 98 | |
EndaKilgarriff | 9:9e247b9c9abf | 99 | #define U16_MAX ((uint16_t)~0U) |
EndaKilgarriff | 9:9e247b9c9abf | 100 | #define S16_MAX ((int16_t)(U16_MAX>>1)) |
EndaKilgarriff | 9:9e247b9c9abf | 101 | |
EndaKilgarriff | 9:9e247b9c9abf | 102 | #define DIV_U64(x, y) (x / y) |
EndaKilgarriff | 9:9e247b9c9abf | 103 | |
EndaKilgarriff | 9:9e247b9c9abf | 104 | #define UNUSED_PARAM(x) ((void)x) |
EndaKilgarriff | 9:9e247b9c9abf | 105 | |
EndaKilgarriff | 9:9e247b9c9abf | 106 | /******************************************************************************/ |
EndaKilgarriff | 9:9e247b9c9abf | 107 | /************************ Functions Declarations ******************************/ |
EndaKilgarriff | 9:9e247b9c9abf | 108 | /******************************************************************************/ |
EndaKilgarriff | 9:9e247b9c9abf | 109 | /* Find first set bit in word. */ |
EndaKilgarriff | 9:9e247b9c9abf | 110 | uint32_t find_first_set_bit(uint32_t word); |
EndaKilgarriff | 9:9e247b9c9abf | 111 | /* Find last set bit in word. */ |
EndaKilgarriff | 9:9e247b9c9abf | 112 | uint32_t find_last_set_bit(uint32_t word); |
EndaKilgarriff | 9:9e247b9c9abf | 113 | /* Locate the closest element in an array. */ |
EndaKilgarriff | 9:9e247b9c9abf | 114 | uint32_t find_closest(int32_t val, |
EndaKilgarriff | 9:9e247b9c9abf | 115 | const int32_t *array, |
EndaKilgarriff | 9:9e247b9c9abf | 116 | uint32_t size); |
EndaKilgarriff | 9:9e247b9c9abf | 117 | /* Shift the value and apply the specified mask. */ |
EndaKilgarriff | 9:9e247b9c9abf | 118 | uint32_t field_prep(uint32_t mask, uint32_t val); |
EndaKilgarriff | 9:9e247b9c9abf | 119 | /* Get a field specified by a mask from a word. */ |
EndaKilgarriff | 9:9e247b9c9abf | 120 | uint32_t field_get(uint32_t mask, uint32_t word); |
EndaKilgarriff | 9:9e247b9c9abf | 121 | /* Log base 2 of the given number. */ |
EndaKilgarriff | 9:9e247b9c9abf | 122 | int32_t log_base_2(uint32_t x); |
EndaKilgarriff | 9:9e247b9c9abf | 123 | /* Find greatest common divisor of the given two numbers. */ |
EndaKilgarriff | 9:9e247b9c9abf | 124 | uint32_t greatest_common_divisor(uint32_t a, |
EndaKilgarriff | 9:9e247b9c9abf | 125 | uint32_t b); |
EndaKilgarriff | 9:9e247b9c9abf | 126 | /* Calculate best rational approximation for a given fraction. */ |
EndaKilgarriff | 9:9e247b9c9abf | 127 | void rational_best_approximation(uint32_t given_numerator, |
EndaKilgarriff | 9:9e247b9c9abf | 128 | uint32_t given_denominator, |
EndaKilgarriff | 9:9e247b9c9abf | 129 | uint32_t max_numerator, |
EndaKilgarriff | 9:9e247b9c9abf | 130 | uint32_t max_denominator, |
EndaKilgarriff | 9:9e247b9c9abf | 131 | uint32_t *best_numerator, |
EndaKilgarriff | 9:9e247b9c9abf | 132 | uint32_t *best_denominator); |
EndaKilgarriff | 9:9e247b9c9abf | 133 | /* Calculate the number of set bits. */ |
EndaKilgarriff | 9:9e247b9c9abf | 134 | uint32_t hweight8(uint32_t word); |
EndaKilgarriff | 9:9e247b9c9abf | 135 | /* Calculate the quotient and the remainder of an integer division. */ |
EndaKilgarriff | 9:9e247b9c9abf | 136 | uint64_t do_div(uint64_t* n, |
EndaKilgarriff | 9:9e247b9c9abf | 137 | uint64_t base); |
EndaKilgarriff | 9:9e247b9c9abf | 138 | /* Unsigned 64bit divide with 64bit divisor and remainder */ |
EndaKilgarriff | 9:9e247b9c9abf | 139 | uint64_t div64_u64_rem(uint64_t dividend, uint64_t divisor, |
EndaKilgarriff | 9:9e247b9c9abf | 140 | uint64_t *remainder); |
EndaKilgarriff | 9:9e247b9c9abf | 141 | /* Unsigned 64bit divide with 32bit divisor with remainder */ |
EndaKilgarriff | 9:9e247b9c9abf | 142 | uint64_t div_u64_rem(uint64_t dividend, uint32_t divisor, uint32_t *remainder); |
EndaKilgarriff | 9:9e247b9c9abf | 143 | /* Unsigned 64bit divide with 32bit divisor */ |
EndaKilgarriff | 9:9e247b9c9abf | 144 | uint64_t div_u64(uint64_t dividend, uint32_t divisor); |
EndaKilgarriff | 9:9e247b9c9abf | 145 | /* Converts from string to int32_t */ |
EndaKilgarriff | 9:9e247b9c9abf | 146 | int32_t str_to_int32(const char *str); |
EndaKilgarriff | 9:9e247b9c9abf | 147 | /* Converts from string to uint32_t */ |
EndaKilgarriff | 9:9e247b9c9abf | 148 | uint32_t srt_to_uint32(const char *str); |
EndaKilgarriff | 9:9e247b9c9abf | 149 | #endif // UTIL_H_ |
EndaKilgarriff | 9:9e247b9c9abf | 150 |