R1 code for micro:bit based train controller code, requires second micro:bit running tx code to operate - see https://meanderingpi.wordpress.com/ for more information
Fork of mbed-dev-bin by
TARGET_NRF51_MICROBIT/TARGET_NORDIC/TARGET_MCU_NRF51822/Lib/nordic_sdk/components/libraries/util/app_util.h@4:1a27ae39e33f, 2017-05-23 (annotated)
- Committer:
- DavidMS
- Date:
- Tue May 23 10:42:56 2017 +0000
- Revision:
- 4:1a27ae39e33f
- Parent:
- 0:e1a608bb55e8
First working micro:bit motor control rx code, need be run with matching tx code
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jamesadevine | 0:e1a608bb55e8 | 1 | /* Copyright (c) 2012 Nordic Semiconductor. All Rights Reserved. |
jamesadevine | 0:e1a608bb55e8 | 2 | * |
jamesadevine | 0:e1a608bb55e8 | 3 | * The information contained herein is property of Nordic Semiconductor ASA. |
jamesadevine | 0:e1a608bb55e8 | 4 | * Terms and conditions of usage are described in detail in NORDIC |
jamesadevine | 0:e1a608bb55e8 | 5 | * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT. |
jamesadevine | 0:e1a608bb55e8 | 6 | * |
jamesadevine | 0:e1a608bb55e8 | 7 | * Licensees are granted free, non-transferable use of the information. NO |
jamesadevine | 0:e1a608bb55e8 | 8 | * WARRANTY of ANY KIND is provided. This heading must NOT be removed from |
jamesadevine | 0:e1a608bb55e8 | 9 | * the file. |
jamesadevine | 0:e1a608bb55e8 | 10 | * |
jamesadevine | 0:e1a608bb55e8 | 11 | */ |
jamesadevine | 0:e1a608bb55e8 | 12 | |
jamesadevine | 0:e1a608bb55e8 | 13 | /** @file |
jamesadevine | 0:e1a608bb55e8 | 14 | * |
jamesadevine | 0:e1a608bb55e8 | 15 | * @defgroup app_util Utility Functions and Definitions |
jamesadevine | 0:e1a608bb55e8 | 16 | * @{ |
jamesadevine | 0:e1a608bb55e8 | 17 | * @ingroup app_common |
jamesadevine | 0:e1a608bb55e8 | 18 | * |
jamesadevine | 0:e1a608bb55e8 | 19 | * @brief Various types and definitions available to all applications. |
jamesadevine | 0:e1a608bb55e8 | 20 | */ |
jamesadevine | 0:e1a608bb55e8 | 21 | |
jamesadevine | 0:e1a608bb55e8 | 22 | #ifndef APP_UTIL_H__ |
jamesadevine | 0:e1a608bb55e8 | 23 | #define APP_UTIL_H__ |
jamesadevine | 0:e1a608bb55e8 | 24 | |
jamesadevine | 0:e1a608bb55e8 | 25 | #include <stdint.h> |
jamesadevine | 0:e1a608bb55e8 | 26 | #include <stdbool.h> |
jamesadevine | 0:e1a608bb55e8 | 27 | #include "compiler_abstraction.h" |
jamesadevine | 0:e1a608bb55e8 | 28 | |
jamesadevine | 0:e1a608bb55e8 | 29 | enum |
jamesadevine | 0:e1a608bb55e8 | 30 | { |
jamesadevine | 0:e1a608bb55e8 | 31 | UNIT_0_625_MS = 625, /**< Number of microseconds in 0.625 milliseconds. */ |
jamesadevine | 0:e1a608bb55e8 | 32 | UNIT_1_25_MS = 1250, /**< Number of microseconds in 1.25 milliseconds. */ |
jamesadevine | 0:e1a608bb55e8 | 33 | UNIT_10_MS = 10000 /**< Number of microseconds in 10 milliseconds. */ |
jamesadevine | 0:e1a608bb55e8 | 34 | }; |
jamesadevine | 0:e1a608bb55e8 | 35 | |
jamesadevine | 0:e1a608bb55e8 | 36 | /**@brief Macro for doing static (i.e. compile time) assertion. |
jamesadevine | 0:e1a608bb55e8 | 37 | * |
jamesadevine | 0:e1a608bb55e8 | 38 | * @note If the assertion fails when compiling using Keil, the compiler will report error message |
jamesadevine | 0:e1a608bb55e8 | 39 | * "error: #94: the size of an array must be greater than zero" (while gcc will list the |
jamesadevine | 0:e1a608bb55e8 | 40 | * symbol static_assert_failed, making the error message more readable). |
jamesadevine | 0:e1a608bb55e8 | 41 | * If the supplied expression can not be evaluated at compile time, Keil will report |
jamesadevine | 0:e1a608bb55e8 | 42 | * "error: #28: expression must have a constant value". |
jamesadevine | 0:e1a608bb55e8 | 43 | * |
jamesadevine | 0:e1a608bb55e8 | 44 | * @note The macro is intentionally implemented not using do while(0), allowing it to be used |
jamesadevine | 0:e1a608bb55e8 | 45 | * outside function blocks (e.g. close to global type- and variable declarations). |
jamesadevine | 0:e1a608bb55e8 | 46 | * If used in a code block, it must be used before any executable code in this block. |
jamesadevine | 0:e1a608bb55e8 | 47 | * |
jamesadevine | 0:e1a608bb55e8 | 48 | * @param[in] EXPR Constant expression to be verified. |
jamesadevine | 0:e1a608bb55e8 | 49 | */ |
jamesadevine | 0:e1a608bb55e8 | 50 | |
jamesadevine | 0:e1a608bb55e8 | 51 | #if defined(__GNUC__) |
jamesadevine | 0:e1a608bb55e8 | 52 | #define STATIC_ASSERT(EXPR) typedef char __attribute__((unused)) static_assert_failed[(EXPR) ? 1 : -1] |
jamesadevine | 0:e1a608bb55e8 | 53 | #elif defined(__ICCARM__) |
jamesadevine | 0:e1a608bb55e8 | 54 | #define STATIC_ASSERT(EXPR) extern char static_assert_failed[(EXPR) ? 1 : -1] |
jamesadevine | 0:e1a608bb55e8 | 55 | #else |
jamesadevine | 0:e1a608bb55e8 | 56 | #define STATIC_ASSERT(EXPR) typedef char static_assert_failed[(EXPR) ? 1 : -1] |
jamesadevine | 0:e1a608bb55e8 | 57 | #endif |
jamesadevine | 0:e1a608bb55e8 | 58 | |
jamesadevine | 0:e1a608bb55e8 | 59 | |
jamesadevine | 0:e1a608bb55e8 | 60 | /**@brief type for holding an encoded (i.e. little endian) 16 bit unsigned integer. */ |
jamesadevine | 0:e1a608bb55e8 | 61 | typedef uint8_t uint16_le_t[2]; |
jamesadevine | 0:e1a608bb55e8 | 62 | |
jamesadevine | 0:e1a608bb55e8 | 63 | /**@brief type for holding an encoded (i.e. little endian) 32 bit unsigned integer. */ |
jamesadevine | 0:e1a608bb55e8 | 64 | typedef uint8_t uint32_le_t[4]; |
jamesadevine | 0:e1a608bb55e8 | 65 | |
jamesadevine | 0:e1a608bb55e8 | 66 | /**@brief Byte array type. */ |
jamesadevine | 0:e1a608bb55e8 | 67 | typedef struct |
jamesadevine | 0:e1a608bb55e8 | 68 | { |
jamesadevine | 0:e1a608bb55e8 | 69 | uint16_t size; /**< Number of array entries. */ |
jamesadevine | 0:e1a608bb55e8 | 70 | uint8_t * p_data; /**< Pointer to array entries. */ |
jamesadevine | 0:e1a608bb55e8 | 71 | } uint8_array_t; |
jamesadevine | 0:e1a608bb55e8 | 72 | |
jamesadevine | 0:e1a608bb55e8 | 73 | /**@brief Perform rounded integer division (as opposed to truncating the result). |
jamesadevine | 0:e1a608bb55e8 | 74 | * |
jamesadevine | 0:e1a608bb55e8 | 75 | * @param[in] A Numerator. |
jamesadevine | 0:e1a608bb55e8 | 76 | * @param[in] B Denominator. |
jamesadevine | 0:e1a608bb55e8 | 77 | * |
jamesadevine | 0:e1a608bb55e8 | 78 | * @return Rounded (integer) result of dividing A by B. |
jamesadevine | 0:e1a608bb55e8 | 79 | */ |
jamesadevine | 0:e1a608bb55e8 | 80 | #define ROUNDED_DIV(A, B) (((A) + ((B) / 2)) / (B)) |
jamesadevine | 0:e1a608bb55e8 | 81 | |
jamesadevine | 0:e1a608bb55e8 | 82 | /**@brief Check if the integer provided is a power of two. |
jamesadevine | 0:e1a608bb55e8 | 83 | * |
jamesadevine | 0:e1a608bb55e8 | 84 | * @param[in] A Number to be tested. |
jamesadevine | 0:e1a608bb55e8 | 85 | * |
jamesadevine | 0:e1a608bb55e8 | 86 | * @return true if value is power of two. |
jamesadevine | 0:e1a608bb55e8 | 87 | * @return false if value not power of two. |
jamesadevine | 0:e1a608bb55e8 | 88 | */ |
jamesadevine | 0:e1a608bb55e8 | 89 | #define IS_POWER_OF_TWO(A) ( ((A) != 0) && ((((A) - 1) & (A)) == 0) ) |
jamesadevine | 0:e1a608bb55e8 | 90 | |
jamesadevine | 0:e1a608bb55e8 | 91 | /**@brief To convert milliseconds to ticks. |
jamesadevine | 0:e1a608bb55e8 | 92 | * @param[in] TIME Number of milliseconds to convert. |
jamesadevine | 0:e1a608bb55e8 | 93 | * @param[in] RESOLUTION Unit to be converted to in [us/ticks]. |
jamesadevine | 0:e1a608bb55e8 | 94 | */ |
jamesadevine | 0:e1a608bb55e8 | 95 | #define MSEC_TO_UNITS(TIME, RESOLUTION) (((TIME) * 1000) / (RESOLUTION)) |
jamesadevine | 0:e1a608bb55e8 | 96 | |
jamesadevine | 0:e1a608bb55e8 | 97 | |
jamesadevine | 0:e1a608bb55e8 | 98 | /**@brief Perform integer division, making sure the result is rounded up. |
jamesadevine | 0:e1a608bb55e8 | 99 | * |
jamesadevine | 0:e1a608bb55e8 | 100 | * @details One typical use for this is to compute the number of objects with size B is needed to |
jamesadevine | 0:e1a608bb55e8 | 101 | * hold A number of bytes. |
jamesadevine | 0:e1a608bb55e8 | 102 | * |
jamesadevine | 0:e1a608bb55e8 | 103 | * @param[in] A Numerator. |
jamesadevine | 0:e1a608bb55e8 | 104 | * @param[in] B Denominator. |
jamesadevine | 0:e1a608bb55e8 | 105 | * |
jamesadevine | 0:e1a608bb55e8 | 106 | * @return Integer result of dividing A by B, rounded up. |
jamesadevine | 0:e1a608bb55e8 | 107 | */ |
jamesadevine | 0:e1a608bb55e8 | 108 | #define CEIL_DIV(A, B) \ |
jamesadevine | 0:e1a608bb55e8 | 109 | /*lint -save -e573 */ \ |
jamesadevine | 0:e1a608bb55e8 | 110 | ((((A) - 1) / (B)) + 1) \ |
jamesadevine | 0:e1a608bb55e8 | 111 | /*lint -restore */ |
jamesadevine | 0:e1a608bb55e8 | 112 | |
jamesadevine | 0:e1a608bb55e8 | 113 | /**@brief Function for encoding a uint16 value. |
jamesadevine | 0:e1a608bb55e8 | 114 | * |
jamesadevine | 0:e1a608bb55e8 | 115 | * @param[in] value Value to be encoded. |
jamesadevine | 0:e1a608bb55e8 | 116 | * @param[out] p_encoded_data Buffer where the encoded data is to be written. |
jamesadevine | 0:e1a608bb55e8 | 117 | * |
jamesadevine | 0:e1a608bb55e8 | 118 | * @return Number of bytes written. |
jamesadevine | 0:e1a608bb55e8 | 119 | */ |
jamesadevine | 0:e1a608bb55e8 | 120 | static __INLINE uint8_t uint16_encode(uint16_t value, uint8_t * p_encoded_data) |
jamesadevine | 0:e1a608bb55e8 | 121 | { |
jamesadevine | 0:e1a608bb55e8 | 122 | p_encoded_data[0] = (uint8_t) ((value & 0x00FF) >> 0); |
jamesadevine | 0:e1a608bb55e8 | 123 | p_encoded_data[1] = (uint8_t) ((value & 0xFF00) >> 8); |
jamesadevine | 0:e1a608bb55e8 | 124 | return sizeof(uint16_t); |
jamesadevine | 0:e1a608bb55e8 | 125 | } |
jamesadevine | 0:e1a608bb55e8 | 126 | |
jamesadevine | 0:e1a608bb55e8 | 127 | /**@brief Function for encoding a uint32 value. |
jamesadevine | 0:e1a608bb55e8 | 128 | * |
jamesadevine | 0:e1a608bb55e8 | 129 | * @param[in] value Value to be encoded. |
jamesadevine | 0:e1a608bb55e8 | 130 | * @param[out] p_encoded_data Buffer where the encoded data is to be written. |
jamesadevine | 0:e1a608bb55e8 | 131 | * |
jamesadevine | 0:e1a608bb55e8 | 132 | * @return Number of bytes written. |
jamesadevine | 0:e1a608bb55e8 | 133 | */ |
jamesadevine | 0:e1a608bb55e8 | 134 | static __INLINE uint8_t uint32_encode(uint32_t value, uint8_t * p_encoded_data) |
jamesadevine | 0:e1a608bb55e8 | 135 | { |
jamesadevine | 0:e1a608bb55e8 | 136 | p_encoded_data[0] = (uint8_t) ((value & 0x000000FF) >> 0); |
jamesadevine | 0:e1a608bb55e8 | 137 | p_encoded_data[1] = (uint8_t) ((value & 0x0000FF00) >> 8); |
jamesadevine | 0:e1a608bb55e8 | 138 | p_encoded_data[2] = (uint8_t) ((value & 0x00FF0000) >> 16); |
jamesadevine | 0:e1a608bb55e8 | 139 | p_encoded_data[3] = (uint8_t) ((value & 0xFF000000) >> 24); |
jamesadevine | 0:e1a608bb55e8 | 140 | return sizeof(uint32_t); |
jamesadevine | 0:e1a608bb55e8 | 141 | } |
jamesadevine | 0:e1a608bb55e8 | 142 | |
jamesadevine | 0:e1a608bb55e8 | 143 | /**@brief Function for decoding a uint16 value. |
jamesadevine | 0:e1a608bb55e8 | 144 | * |
jamesadevine | 0:e1a608bb55e8 | 145 | * @param[in] p_encoded_data Buffer where the encoded data is stored. |
jamesadevine | 0:e1a608bb55e8 | 146 | * |
jamesadevine | 0:e1a608bb55e8 | 147 | * @return Decoded value. |
jamesadevine | 0:e1a608bb55e8 | 148 | */ |
jamesadevine | 0:e1a608bb55e8 | 149 | static __INLINE uint16_t uint16_decode(const uint8_t * p_encoded_data) |
jamesadevine | 0:e1a608bb55e8 | 150 | { |
jamesadevine | 0:e1a608bb55e8 | 151 | return ( (((uint16_t)((uint8_t *)p_encoded_data)[0])) | |
jamesadevine | 0:e1a608bb55e8 | 152 | (((uint16_t)((uint8_t *)p_encoded_data)[1]) << 8 )); |
jamesadevine | 0:e1a608bb55e8 | 153 | } |
jamesadevine | 0:e1a608bb55e8 | 154 | |
jamesadevine | 0:e1a608bb55e8 | 155 | /**@brief Function for decoding a uint32 value. |
jamesadevine | 0:e1a608bb55e8 | 156 | * |
jamesadevine | 0:e1a608bb55e8 | 157 | * @param[in] p_encoded_data Buffer where the encoded data is stored. |
jamesadevine | 0:e1a608bb55e8 | 158 | * |
jamesadevine | 0:e1a608bb55e8 | 159 | * @return Decoded value. |
jamesadevine | 0:e1a608bb55e8 | 160 | */ |
jamesadevine | 0:e1a608bb55e8 | 161 | static __INLINE uint32_t uint32_decode(const uint8_t * p_encoded_data) |
jamesadevine | 0:e1a608bb55e8 | 162 | { |
jamesadevine | 0:e1a608bb55e8 | 163 | return ( (((uint32_t)((uint8_t *)p_encoded_data)[0]) << 0) | |
jamesadevine | 0:e1a608bb55e8 | 164 | (((uint32_t)((uint8_t *)p_encoded_data)[1]) << 8) | |
jamesadevine | 0:e1a608bb55e8 | 165 | (((uint32_t)((uint8_t *)p_encoded_data)[2]) << 16) | |
jamesadevine | 0:e1a608bb55e8 | 166 | (((uint32_t)((uint8_t *)p_encoded_data)[3]) << 24 )); |
jamesadevine | 0:e1a608bb55e8 | 167 | } |
jamesadevine | 0:e1a608bb55e8 | 168 | |
jamesadevine | 0:e1a608bb55e8 | 169 | /** @brief Function for converting the input voltage (in milli volts) into percentage of 3.0 Volts. |
jamesadevine | 0:e1a608bb55e8 | 170 | * |
jamesadevine | 0:e1a608bb55e8 | 171 | * @details The calculation is based on a linearized version of the battery's discharge |
jamesadevine | 0:e1a608bb55e8 | 172 | * curve. 3.0V returns 100% battery level. The limit for power failure is 2.1V and |
jamesadevine | 0:e1a608bb55e8 | 173 | * is considered to be the lower boundary. |
jamesadevine | 0:e1a608bb55e8 | 174 | * |
jamesadevine | 0:e1a608bb55e8 | 175 | * The discharge curve for CR2032 is non-linear. In this model it is split into |
jamesadevine | 0:e1a608bb55e8 | 176 | * 4 linear sections: |
jamesadevine | 0:e1a608bb55e8 | 177 | * - Section 1: 3.0V - 2.9V = 100% - 42% (58% drop on 100 mV) |
jamesadevine | 0:e1a608bb55e8 | 178 | * - Section 2: 2.9V - 2.74V = 42% - 18% (24% drop on 160 mV) |
jamesadevine | 0:e1a608bb55e8 | 179 | * - Section 3: 2.74V - 2.44V = 18% - 6% (12% drop on 300 mV) |
jamesadevine | 0:e1a608bb55e8 | 180 | * - Section 4: 2.44V - 2.1V = 6% - 0% (6% drop on 340 mV) |
jamesadevine | 0:e1a608bb55e8 | 181 | * |
jamesadevine | 0:e1a608bb55e8 | 182 | * These numbers are by no means accurate. Temperature and |
jamesadevine | 0:e1a608bb55e8 | 183 | * load in the actual application is not accounted for! |
jamesadevine | 0:e1a608bb55e8 | 184 | * |
jamesadevine | 0:e1a608bb55e8 | 185 | * @param[in] mvolts The voltage in mV |
jamesadevine | 0:e1a608bb55e8 | 186 | * |
jamesadevine | 0:e1a608bb55e8 | 187 | * @return Battery level in percent. |
jamesadevine | 0:e1a608bb55e8 | 188 | */ |
jamesadevine | 0:e1a608bb55e8 | 189 | static __INLINE uint8_t battery_level_in_percent(const uint16_t mvolts) |
jamesadevine | 0:e1a608bb55e8 | 190 | { |
jamesadevine | 0:e1a608bb55e8 | 191 | uint8_t battery_level; |
jamesadevine | 0:e1a608bb55e8 | 192 | |
jamesadevine | 0:e1a608bb55e8 | 193 | if (mvolts >= 3000) |
jamesadevine | 0:e1a608bb55e8 | 194 | { |
jamesadevine | 0:e1a608bb55e8 | 195 | battery_level = 100; |
jamesadevine | 0:e1a608bb55e8 | 196 | } |
jamesadevine | 0:e1a608bb55e8 | 197 | else if (mvolts > 2900) |
jamesadevine | 0:e1a608bb55e8 | 198 | { |
jamesadevine | 0:e1a608bb55e8 | 199 | battery_level = 100 - ((3000 - mvolts) * 58) / 100; |
jamesadevine | 0:e1a608bb55e8 | 200 | } |
jamesadevine | 0:e1a608bb55e8 | 201 | else if (mvolts > 2740) |
jamesadevine | 0:e1a608bb55e8 | 202 | { |
jamesadevine | 0:e1a608bb55e8 | 203 | battery_level = 42 - ((2900 - mvolts) * 24) / 160; |
jamesadevine | 0:e1a608bb55e8 | 204 | } |
jamesadevine | 0:e1a608bb55e8 | 205 | else if (mvolts > 2440) |
jamesadevine | 0:e1a608bb55e8 | 206 | { |
jamesadevine | 0:e1a608bb55e8 | 207 | battery_level = 18 - ((2740 - mvolts) * 12) / 300; |
jamesadevine | 0:e1a608bb55e8 | 208 | } |
jamesadevine | 0:e1a608bb55e8 | 209 | else if (mvolts > 2100) |
jamesadevine | 0:e1a608bb55e8 | 210 | { |
jamesadevine | 0:e1a608bb55e8 | 211 | battery_level = 6 - ((2440 - mvolts) * 6) / 340; |
jamesadevine | 0:e1a608bb55e8 | 212 | } |
jamesadevine | 0:e1a608bb55e8 | 213 | else |
jamesadevine | 0:e1a608bb55e8 | 214 | { |
jamesadevine | 0:e1a608bb55e8 | 215 | battery_level = 0; |
jamesadevine | 0:e1a608bb55e8 | 216 | } |
jamesadevine | 0:e1a608bb55e8 | 217 | |
jamesadevine | 0:e1a608bb55e8 | 218 | return battery_level; |
jamesadevine | 0:e1a608bb55e8 | 219 | } |
jamesadevine | 0:e1a608bb55e8 | 220 | |
jamesadevine | 0:e1a608bb55e8 | 221 | /**@brief Function for checking if a pointer value is aligned to a 4 byte boundary. |
jamesadevine | 0:e1a608bb55e8 | 222 | * |
jamesadevine | 0:e1a608bb55e8 | 223 | * @param[in] p Pointer value to be checked. |
jamesadevine | 0:e1a608bb55e8 | 224 | * |
jamesadevine | 0:e1a608bb55e8 | 225 | * @return TRUE if pointer is aligned to a 4 byte boundary, FALSE otherwise. |
jamesadevine | 0:e1a608bb55e8 | 226 | */ |
jamesadevine | 0:e1a608bb55e8 | 227 | static __INLINE bool is_word_aligned(void * p) |
jamesadevine | 0:e1a608bb55e8 | 228 | { |
jamesadevine | 0:e1a608bb55e8 | 229 | return (((uintptr_t)p & 0x03) == 0); |
jamesadevine | 0:e1a608bb55e8 | 230 | } |
jamesadevine | 0:e1a608bb55e8 | 231 | |
jamesadevine | 0:e1a608bb55e8 | 232 | #endif // APP_UTIL_H__ |
jamesadevine | 0:e1a608bb55e8 | 233 | |
jamesadevine | 0:e1a608bb55e8 | 234 | /** @} */ |