Update platform drivers

Committer:
EndaKilgarriff
Date:
Mon Jun 15 13:03:55 2020 +0000
Revision:
9:9e247b9c9abf
- Include the following libraries:; 	- crc; 	- crc8; 	- uart; 	- util; - Add microsecond delay; - Move baud rate definition to mbed_app.json file; - Add bit shift for I2C slave address; - Check error for redefinition; - Make gpio handling more robust;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
EndaKilgarriff 9:9e247b9c9abf 1 /***************************************************************************//**
EndaKilgarriff 9:9e247b9c9abf 2 * @file util.c
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(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
EndaKilgarriff 9:9e247b9c9abf 40 /******************************************************************************/
EndaKilgarriff 9:9e247b9c9abf 41 /***************************** Include Files **********************************/
EndaKilgarriff 9:9e247b9c9abf 42 /******************************************************************************/
EndaKilgarriff 9:9e247b9c9abf 43 #include <string.h>
EndaKilgarriff 9:9e247b9c9abf 44 #include <stdlib.h>
EndaKilgarriff 9:9e247b9c9abf 45 #include "platform_drivers.h"
EndaKilgarriff 9:9e247b9c9abf 46 #include "errno.h"
EndaKilgarriff 9:9e247b9c9abf 47 /******************************************************************************/
EndaKilgarriff 9:9e247b9c9abf 48 /************************** Functions Implementation **************************/
EndaKilgarriff 9:9e247b9c9abf 49 /******************************************************************************/
EndaKilgarriff 9:9e247b9c9abf 50
EndaKilgarriff 9:9e247b9c9abf 51 /**
EndaKilgarriff 9:9e247b9c9abf 52 * Find first set bit in word.
EndaKilgarriff 9:9e247b9c9abf 53 */
EndaKilgarriff 9:9e247b9c9abf 54 uint32_t find_first_set_bit(uint32_t word)
EndaKilgarriff 9:9e247b9c9abf 55 {
EndaKilgarriff 9:9e247b9c9abf 56 uint32_t first_set_bit = 0;
EndaKilgarriff 9:9e247b9c9abf 57
EndaKilgarriff 9:9e247b9c9abf 58 while (word) {
EndaKilgarriff 9:9e247b9c9abf 59 if (word & 0x1)
EndaKilgarriff 9:9e247b9c9abf 60 return first_set_bit;
EndaKilgarriff 9:9e247b9c9abf 61 word >>= 1;
EndaKilgarriff 9:9e247b9c9abf 62 first_set_bit ++;
EndaKilgarriff 9:9e247b9c9abf 63 }
EndaKilgarriff 9:9e247b9c9abf 64
EndaKilgarriff 9:9e247b9c9abf 65 return 32;
EndaKilgarriff 9:9e247b9c9abf 66 }
EndaKilgarriff 9:9e247b9c9abf 67
EndaKilgarriff 9:9e247b9c9abf 68 /**
EndaKilgarriff 9:9e247b9c9abf 69 * Find last set bit in word.
EndaKilgarriff 9:9e247b9c9abf 70 */
EndaKilgarriff 9:9e247b9c9abf 71 uint32_t find_last_set_bit(uint32_t word)
EndaKilgarriff 9:9e247b9c9abf 72 {
EndaKilgarriff 9:9e247b9c9abf 73 uint32_t bit = 0;
EndaKilgarriff 9:9e247b9c9abf 74 uint32_t last_set_bit = 32;
EndaKilgarriff 9:9e247b9c9abf 75
EndaKilgarriff 9:9e247b9c9abf 76 while (word) {
EndaKilgarriff 9:9e247b9c9abf 77 if (word & 0x1)
EndaKilgarriff 9:9e247b9c9abf 78 last_set_bit = bit;
EndaKilgarriff 9:9e247b9c9abf 79 word >>= 1;
EndaKilgarriff 9:9e247b9c9abf 80 bit ++;
EndaKilgarriff 9:9e247b9c9abf 81 }
EndaKilgarriff 9:9e247b9c9abf 82
EndaKilgarriff 9:9e247b9c9abf 83 return last_set_bit;
EndaKilgarriff 9:9e247b9c9abf 84 }
EndaKilgarriff 9:9e247b9c9abf 85
EndaKilgarriff 9:9e247b9c9abf 86 /**
EndaKilgarriff 9:9e247b9c9abf 87 * Locate the closest element in an array.
EndaKilgarriff 9:9e247b9c9abf 88 */
EndaKilgarriff 9:9e247b9c9abf 89 uint32_t find_closest(int32_t val,
EndaKilgarriff 9:9e247b9c9abf 90 const int32_t *array,
EndaKilgarriff 9:9e247b9c9abf 91 uint32_t size)
EndaKilgarriff 9:9e247b9c9abf 92 {
EndaKilgarriff 9:9e247b9c9abf 93 int32_t diff = abs(array[0] - val);
EndaKilgarriff 9:9e247b9c9abf 94 uint32_t ret = 0;
EndaKilgarriff 9:9e247b9c9abf 95 uint32_t i;
EndaKilgarriff 9:9e247b9c9abf 96
EndaKilgarriff 9:9e247b9c9abf 97 for (i = 1; i < size; i++) {
EndaKilgarriff 9:9e247b9c9abf 98 if (abs(array[i] - val) < diff) {
EndaKilgarriff 9:9e247b9c9abf 99 diff = abs(array[i] - val);
EndaKilgarriff 9:9e247b9c9abf 100 ret = i;
EndaKilgarriff 9:9e247b9c9abf 101 }
EndaKilgarriff 9:9e247b9c9abf 102 }
EndaKilgarriff 9:9e247b9c9abf 103
EndaKilgarriff 9:9e247b9c9abf 104 return ret;
EndaKilgarriff 9:9e247b9c9abf 105 }
EndaKilgarriff 9:9e247b9c9abf 106
EndaKilgarriff 9:9e247b9c9abf 107 /**
EndaKilgarriff 9:9e247b9c9abf 108 * Shift the value and apply the specified mask.
EndaKilgarriff 9:9e247b9c9abf 109 */
EndaKilgarriff 9:9e247b9c9abf 110 uint32_t field_prep(uint32_t mask, uint32_t val)
EndaKilgarriff 9:9e247b9c9abf 111 {
EndaKilgarriff 9:9e247b9c9abf 112 return (val << find_first_set_bit(mask)) & mask;
EndaKilgarriff 9:9e247b9c9abf 113 }
EndaKilgarriff 9:9e247b9c9abf 114
EndaKilgarriff 9:9e247b9c9abf 115 /**
EndaKilgarriff 9:9e247b9c9abf 116 * Get a field specified by a mask from a word.
EndaKilgarriff 9:9e247b9c9abf 117 */
EndaKilgarriff 9:9e247b9c9abf 118 uint32_t field_get(uint32_t mask, uint32_t word)
EndaKilgarriff 9:9e247b9c9abf 119 {
EndaKilgarriff 9:9e247b9c9abf 120 return (word & mask) >> find_first_set_bit(mask);
EndaKilgarriff 9:9e247b9c9abf 121 }
EndaKilgarriff 9:9e247b9c9abf 122
EndaKilgarriff 9:9e247b9c9abf 123 /**
EndaKilgarriff 9:9e247b9c9abf 124 * Log base 2 of the given number.
EndaKilgarriff 9:9e247b9c9abf 125 */
EndaKilgarriff 9:9e247b9c9abf 126 int32_t log_base_2(uint32_t x)
EndaKilgarriff 9:9e247b9c9abf 127 {
EndaKilgarriff 9:9e247b9c9abf 128 return find_last_set_bit(x);
EndaKilgarriff 9:9e247b9c9abf 129 }
EndaKilgarriff 9:9e247b9c9abf 130
EndaKilgarriff 9:9e247b9c9abf 131 /**
EndaKilgarriff 9:9e247b9c9abf 132 * Find greatest common divisor of the given two numbers.
EndaKilgarriff 9:9e247b9c9abf 133 */
EndaKilgarriff 9:9e247b9c9abf 134 uint32_t greatest_common_divisor(uint32_t a,
EndaKilgarriff 9:9e247b9c9abf 135 uint32_t b)
EndaKilgarriff 9:9e247b9c9abf 136 {
EndaKilgarriff 9:9e247b9c9abf 137 uint32_t div;
EndaKilgarriff 9:9e247b9c9abf 138 uint32_t common_div = 1;
EndaKilgarriff 9:9e247b9c9abf 139
EndaKilgarriff 9:9e247b9c9abf 140 if ((a == 0) || (b == 0))
EndaKilgarriff 9:9e247b9c9abf 141 return max(a, b);
EndaKilgarriff 9:9e247b9c9abf 142
EndaKilgarriff 9:9e247b9c9abf 143 for (div = 1; (div <= a) && (div <= b); div++)
EndaKilgarriff 9:9e247b9c9abf 144 if (!(a % div) && !(b % div))
EndaKilgarriff 9:9e247b9c9abf 145 common_div = div;
EndaKilgarriff 9:9e247b9c9abf 146
EndaKilgarriff 9:9e247b9c9abf 147 return common_div;
EndaKilgarriff 9:9e247b9c9abf 148 }
EndaKilgarriff 9:9e247b9c9abf 149
EndaKilgarriff 9:9e247b9c9abf 150 /**
EndaKilgarriff 9:9e247b9c9abf 151 * Calculate best rational approximation for a given fraction.
EndaKilgarriff 9:9e247b9c9abf 152 */
EndaKilgarriff 9:9e247b9c9abf 153 void rational_best_approximation(uint32_t given_numerator,
EndaKilgarriff 9:9e247b9c9abf 154 uint32_t given_denominator,
EndaKilgarriff 9:9e247b9c9abf 155 uint32_t max_numerator,
EndaKilgarriff 9:9e247b9c9abf 156 uint32_t max_denominator,
EndaKilgarriff 9:9e247b9c9abf 157 uint32_t *best_numerator,
EndaKilgarriff 9:9e247b9c9abf 158 uint32_t *best_denominator)
EndaKilgarriff 9:9e247b9c9abf 159 {
EndaKilgarriff 9:9e247b9c9abf 160 uint32_t gcd;
EndaKilgarriff 9:9e247b9c9abf 161
EndaKilgarriff 9:9e247b9c9abf 162 gcd = greatest_common_divisor(given_numerator, given_denominator);
EndaKilgarriff 9:9e247b9c9abf 163
EndaKilgarriff 9:9e247b9c9abf 164 *best_numerator = given_numerator / gcd;
EndaKilgarriff 9:9e247b9c9abf 165 *best_denominator = given_denominator / gcd;
EndaKilgarriff 9:9e247b9c9abf 166
EndaKilgarriff 9:9e247b9c9abf 167 if ((*best_numerator > max_numerator) ||
EndaKilgarriff 9:9e247b9c9abf 168 (*best_denominator > max_denominator)) {
EndaKilgarriff 9:9e247b9c9abf 169 *best_numerator = 0;
EndaKilgarriff 9:9e247b9c9abf 170 *best_denominator = 0;
EndaKilgarriff 9:9e247b9c9abf 171 }
EndaKilgarriff 9:9e247b9c9abf 172 }
EndaKilgarriff 9:9e247b9c9abf 173
EndaKilgarriff 9:9e247b9c9abf 174 /**
EndaKilgarriff 9:9e247b9c9abf 175 * Calculate the number of set bits.
EndaKilgarriff 9:9e247b9c9abf 176 */
EndaKilgarriff 9:9e247b9c9abf 177 uint32_t hweight8(uint32_t word)
EndaKilgarriff 9:9e247b9c9abf 178 {
EndaKilgarriff 9:9e247b9c9abf 179 uint32_t count = 0;
EndaKilgarriff 9:9e247b9c9abf 180
EndaKilgarriff 9:9e247b9c9abf 181 while (word) {
EndaKilgarriff 9:9e247b9c9abf 182 if (word & 0x1)
EndaKilgarriff 9:9e247b9c9abf 183 count++;
EndaKilgarriff 9:9e247b9c9abf 184 word >>= 1;
EndaKilgarriff 9:9e247b9c9abf 185 }
EndaKilgarriff 9:9e247b9c9abf 186
EndaKilgarriff 9:9e247b9c9abf 187 return count;
EndaKilgarriff 9:9e247b9c9abf 188 }
EndaKilgarriff 9:9e247b9c9abf 189
EndaKilgarriff 9:9e247b9c9abf 190 /**
EndaKilgarriff 9:9e247b9c9abf 191 * Calculate the quotient and the remainder of an integer division.
EndaKilgarriff 9:9e247b9c9abf 192 */
EndaKilgarriff 9:9e247b9c9abf 193 uint64_t do_div(uint64_t* n,
EndaKilgarriff 9:9e247b9c9abf 194 uint64_t base)
EndaKilgarriff 9:9e247b9c9abf 195 {
EndaKilgarriff 9:9e247b9c9abf 196 uint64_t mod = 0;
EndaKilgarriff 9:9e247b9c9abf 197
EndaKilgarriff 9:9e247b9c9abf 198 mod = *n % base;
EndaKilgarriff 9:9e247b9c9abf 199 *n = *n / base;
EndaKilgarriff 9:9e247b9c9abf 200
EndaKilgarriff 9:9e247b9c9abf 201 return mod;
EndaKilgarriff 9:9e247b9c9abf 202 }
EndaKilgarriff 9:9e247b9c9abf 203
EndaKilgarriff 9:9e247b9c9abf 204 /**
EndaKilgarriff 9:9e247b9c9abf 205 * Unsigned 64bit divide with 64bit divisor and remainder
EndaKilgarriff 9:9e247b9c9abf 206 */
EndaKilgarriff 9:9e247b9c9abf 207 uint64_t div64_u64_rem(uint64_t dividend, uint64_t divisor, uint64_t *remainder)
EndaKilgarriff 9:9e247b9c9abf 208 {
EndaKilgarriff 9:9e247b9c9abf 209 *remainder = dividend % divisor;
EndaKilgarriff 9:9e247b9c9abf 210
EndaKilgarriff 9:9e247b9c9abf 211 return dividend / divisor;
EndaKilgarriff 9:9e247b9c9abf 212 }
EndaKilgarriff 9:9e247b9c9abf 213
EndaKilgarriff 9:9e247b9c9abf 214 /**
EndaKilgarriff 9:9e247b9c9abf 215 * Unsigned 64bit divide with 32bit divisor with remainder
EndaKilgarriff 9:9e247b9c9abf 216 */
EndaKilgarriff 9:9e247b9c9abf 217 uint64_t div_u64_rem(uint64_t dividend, uint32_t divisor, uint32_t *remainder)
EndaKilgarriff 9:9e247b9c9abf 218 {
EndaKilgarriff 9:9e247b9c9abf 219 *remainder = do_div(&dividend, divisor);
EndaKilgarriff 9:9e247b9c9abf 220
EndaKilgarriff 9:9e247b9c9abf 221 return dividend;
EndaKilgarriff 9:9e247b9c9abf 222 }
EndaKilgarriff 9:9e247b9c9abf 223
EndaKilgarriff 9:9e247b9c9abf 224 /**
EndaKilgarriff 9:9e247b9c9abf 225 * Unsigned 64bit divide with 32bit divisor
EndaKilgarriff 9:9e247b9c9abf 226 */
EndaKilgarriff 9:9e247b9c9abf 227 uint64_t div_u64(uint64_t dividend, uint32_t divisor)
EndaKilgarriff 9:9e247b9c9abf 228 {
EndaKilgarriff 9:9e247b9c9abf 229 uint32_t remainder;
EndaKilgarriff 9:9e247b9c9abf 230
EndaKilgarriff 9:9e247b9c9abf 231 return div_u64_rem(dividend, divisor, &remainder);
EndaKilgarriff 9:9e247b9c9abf 232 }
EndaKilgarriff 9:9e247b9c9abf 233
EndaKilgarriff 9:9e247b9c9abf 234 /**
EndaKilgarriff 9:9e247b9c9abf 235 * Converts from string to int32_t
EndaKilgarriff 9:9e247b9c9abf 236 * @param *str
EndaKilgarriff 9:9e247b9c9abf 237 * @return int32_t
EndaKilgarriff 9:9e247b9c9abf 238 */
EndaKilgarriff 9:9e247b9c9abf 239 int32_t str_to_int32(const char *str)
EndaKilgarriff 9:9e247b9c9abf 240 {
EndaKilgarriff 9:9e247b9c9abf 241 char *end;
EndaKilgarriff 9:9e247b9c9abf 242 int32_t value = strtol(str, &end, 0);
EndaKilgarriff 9:9e247b9c9abf 243
EndaKilgarriff 9:9e247b9c9abf 244 if (end == str)
EndaKilgarriff 9:9e247b9c9abf 245 return -EINVAL;
EndaKilgarriff 9:9e247b9c9abf 246 else
EndaKilgarriff 9:9e247b9c9abf 247 return value;
EndaKilgarriff 9:9e247b9c9abf 248 }
EndaKilgarriff 9:9e247b9c9abf 249
EndaKilgarriff 9:9e247b9c9abf 250 /**
EndaKilgarriff 9:9e247b9c9abf 251 * Converts from string to uint32_t
EndaKilgarriff 9:9e247b9c9abf 252 * @param *str
EndaKilgarriff 9:9e247b9c9abf 253 * @return uint32_t
EndaKilgarriff 9:9e247b9c9abf 254 */
EndaKilgarriff 9:9e247b9c9abf 255 uint32_t srt_to_uint32(const char *str)
EndaKilgarriff 9:9e247b9c9abf 256 {
EndaKilgarriff 9:9e247b9c9abf 257 char *end;
EndaKilgarriff 9:9e247b9c9abf 258 uint32_t value = strtoul(str, &end, 0);
EndaKilgarriff 9:9e247b9c9abf 259
EndaKilgarriff 9:9e247b9c9abf 260 if (end == str)
EndaKilgarriff 9:9e247b9c9abf 261 return -EINVAL;
EndaKilgarriff 9:9e247b9c9abf 262 else
EndaKilgarriff 9:9e247b9c9abf 263 return value;
EndaKilgarriff 9:9e247b9c9abf 264 }