Library for Modtronix NZ32 STM32 boards, like the NZ32-SC151, NZ32-SB072, NZ32-SE411 and others

Committer:
modtronix-com
Date:
Mon Sep 14 15:16:41 2015 +1000
Revision:
9:5000feb4b46f
Parent:
6:33d25ef353f6
Child:
13:328bfac0e686
Added debugging macros

Who changed what in which revision?

UserRevisionLine numberNew contents of line
modtronix 3:99cb87ee1792 1 /**
modtronix 3:99cb87ee1792 2 * File: mx_helpers.h
modtronix 3:99cb87ee1792 3 *
modtronix 3:99cb87ee1792 4 * Author: Modtronix Engineering - www.modtronix.com
modtronix 3:99cb87ee1792 5 *
modtronix 3:99cb87ee1792 6 * Description:
modtronix 3:99cb87ee1792 7 *
modtronix 3:99cb87ee1792 8 * Software License Agreement:
modtronix 3:99cb87ee1792 9 * This software has been written or modified by Modtronix Engineering. The code
modtronix 3:99cb87ee1792 10 * may be modified and can be used free of charge for commercial and non commercial
modtronix 3:99cb87ee1792 11 * applications. If this is modified software, any license conditions from original
modtronix 3:99cb87ee1792 12 * software also apply. Any redistribution must include reference to 'Modtronix
modtronix 3:99cb87ee1792 13 * Engineering' and web link(www.modtronix.com) in the file header.
modtronix 3:99cb87ee1792 14 *
modtronix 3:99cb87ee1792 15 * THIS SOFTWARE IS PROVIDED IN AN 'AS IS' CONDITION. NO WARRANTIES, WHETHER EXPRESS,
modtronix 3:99cb87ee1792 16 * IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
modtronix 3:99cb87ee1792 17 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE
modtronix 3:99cb87ee1792 18 * COMPANY SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
modtronix 3:99cb87ee1792 19 * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
modtronix 3:99cb87ee1792 20 */
modtronix 3:99cb87ee1792 21 #ifndef MX_HELPERS_H_
modtronix 3:99cb87ee1792 22 #define MX_HELPERS_H_
modtronix 3:99cb87ee1792 23
modtronix 3:99cb87ee1792 24 //Defines for MXH_DEBUG - debugging for include file
modtronix 3:99cb87ee1792 25 #define DEBUG_ENABLE_MX_HELPERS 1
modtronix 3:99cb87ee1792 26 #define DEBUG_ENABLE_INFO_MX_HELPERS 0
modtronix 3:99cb87ee1792 27
modtronix 3:99cb87ee1792 28 #if !defined(MXH_DEBUG)
modtronix 3:99cb87ee1792 29 #if (DEBUG_ENABLE_MX_HELPERS == 1)
modtronix 3:99cb87ee1792 30 #define MXH_DEBUG MX_DEBUG
modtronix 3:99cb87ee1792 31 #if (DEBUG_ENABLE_INFO_MX_HELPERS == 1)
modtronix 3:99cb87ee1792 32 #define MXH_DEBUG_INFO MX_DEBUG
modtronix 3:99cb87ee1792 33 #else
modtronix 3:99cb87ee1792 34 #define MXH_DEBUG_INFO(format, args...) ((void)0)
modtronix 3:99cb87ee1792 35 #endif
modtronix 3:99cb87ee1792 36 #else
modtronix 3:99cb87ee1792 37 #define MXH_DEBUG(format, args...) ((void)0)
modtronix 3:99cb87ee1792 38 #define MXH_DEBUG_INFO(format, args...) ((void)0)
modtronix 3:99cb87ee1792 39 #endif // #if (DEBUG_ENABLE_MX_HELPERS == 1)
modtronix 3:99cb87ee1792 40 #endif // #if !defined(MXH_DEBUG)
modtronix 3:99cb87ee1792 41
modtronix 3:99cb87ee1792 42 typedef union
modtronix 3:99cb87ee1792 43 {
modtronix 3:99cb87ee1792 44 uint16_t Val;
modtronix 3:99cb87ee1792 45 uint8_t v[2] __packed;
modtronix 3:99cb87ee1792 46 struct
modtronix 3:99cb87ee1792 47 {
modtronix 3:99cb87ee1792 48 uint8_t LB;
modtronix 3:99cb87ee1792 49 uint8_t HB;
modtronix 3:99cb87ee1792 50 } byte;
modtronix 3:99cb87ee1792 51 } WORD_VAL;
modtronix 3:99cb87ee1792 52
modtronix 3:99cb87ee1792 53
modtronix 6:33d25ef353f6 54 #define GET_U8_AT0(val) MxHelpers::get_u8_at0(val)
modtronix 6:33d25ef353f6 55 #define GET_U8_AT1(val) MxHelpers::get_u8_at1(val)
modtronix 6:33d25ef353f6 56 #define GET_U8_AT2(val) MxHelpers::get_u8_at2(val)
modtronix 6:33d25ef353f6 57 #define GET_U8_AT3(val) MxHelpers::get_u8_at3(val)
modtronix 5:e1297df8ee0d 58
modtronix 6:33d25ef353f6 59 #define SET_U8_AT0(val) MxHelpers::set_u8_at0(val)
modtronix 6:33d25ef353f6 60 #define SET_U8_AT1(val) MxHelpers::set_u8_at1(val)
modtronix 6:33d25ef353f6 61 #define SET_U8_AT2(val) MxHelpers::set_u8_at2(val)
modtronix 6:33d25ef353f6 62 #define SET_U8_AT3(val) MxHelpers::set_u8_at3(val)
modtronix 5:e1297df8ee0d 63
modtronix 5:e1297df8ee0d 64
modtronix 3:99cb87ee1792 65 class MxHelpers {
modtronix 3:99cb87ee1792 66 public:
modtronix 3:99cb87ee1792 67 /** Get first(LSB) 8-bit byte(uint8_t) of given 16-bit Word
modtronix 3:99cb87ee1792 68 * @return Returns the lower(LSB) byte of given value
modtronix 3:99cb87ee1792 69 */
modtronix 6:33d25ef353f6 70 static inline uint8_t get_u8_at0(uint16_t val) {
modtronix 3:99cb87ee1792 71 return ((uint8_t)((val)&0xFF));
modtronix 3:99cb87ee1792 72 }
modtronix 3:99cb87ee1792 73
modtronix 3:99cb87ee1792 74
modtronix 3:99cb87ee1792 75 /** Get second(MSB) 8-bit byte(uint8_t) of given 16-bit Word.
modtronix 3:99cb87ee1792 76 * @return Returns the upper(MSB) byte of given value
modtronix 3:99cb87ee1792 77 */
modtronix 6:33d25ef353f6 78 static inline uint8_t get_u8_at1(uint16_t val) {
modtronix 3:99cb87ee1792 79 return ((uint8_t)(((val)>>8)&0xFF));
modtronix 3:99cb87ee1792 80 }
modtronix 3:99cb87ee1792 81
modtronix 3:99cb87ee1792 82
modtronix 3:99cb87ee1792 83 /** Get first(LSB) 8-bit byte(uint8_t) of given 32-bit Word
modtronix 3:99cb87ee1792 84 * @return Returns the lower(LSB) byte of given value = byte at bit offset 0 to 7
modtronix 3:99cb87ee1792 85 */
modtronix 6:33d25ef353f6 86 static inline uint8_t get_u8_at0(uint32_t val) {
modtronix 3:99cb87ee1792 87 return ((uint8_t)((val)&0xFF));
modtronix 3:99cb87ee1792 88 }
modtronix 3:99cb87ee1792 89
modtronix 3:99cb87ee1792 90
modtronix 3:99cb87ee1792 91 /** Get second 8-bit byte(uint8_t) of given 32-bit Word
modtronix 3:99cb87ee1792 92 * @return Returns the second byte of given value = byte at bit offset 8 to 15
modtronix 3:99cb87ee1792 93 */
modtronix 6:33d25ef353f6 94 static inline uint8_t get_u8_at1(uint32_t val) {
modtronix 3:99cb87ee1792 95 return ((uint8_t)(((val)>>8)&0xFF));
modtronix 3:99cb87ee1792 96 }
modtronix 3:99cb87ee1792 97
modtronix 3:99cb87ee1792 98
modtronix 3:99cb87ee1792 99 /** Get third 8-bit byte(uint8_t) of given 32-bit Word
modtronix 3:99cb87ee1792 100 * @return Returns the third byte of given value = byte at bit offset 16 to 23
modtronix 3:99cb87ee1792 101 */
modtronix 6:33d25ef353f6 102 static inline uint8_t get_u8_at2(uint32_t val) {
modtronix 3:99cb87ee1792 103 return ((uint8_t)(((val)>>16)&0xFF));
modtronix 3:99cb87ee1792 104 }
modtronix 3:99cb87ee1792 105
modtronix 3:99cb87ee1792 106
modtronix 3:99cb87ee1792 107 /** Get forth(MSB) 8-bit byte(uint8_t) of given 32-bit Word
modtronix 3:99cb87ee1792 108 * @return Returns the MSB byte of given value = byte at bit offset 24 to 31
modtronix 3:99cb87ee1792 109 */
modtronix 6:33d25ef353f6 110 static inline uint8_t get_u8_at3(uint32_t val) {
modtronix 3:99cb87ee1792 111 return ((uint8_t)(((val)>>24)&0xFF));
modtronix 3:99cb87ee1792 112 }
modtronix 3:99cb87ee1792 113
modtronix 3:99cb87ee1792 114
modtronix 3:99cb87ee1792 115 /** Write given byte to first(LSB) 8-bit byte(uint8_t) of given 16-bit Word
modtronix 3:99cb87ee1792 116 * @param src Source 8-bit byte to write to destination
modtronix 3:99cb87ee1792 117 * @param dest Destination
modtronix 3:99cb87ee1792 118 */
modtronix 6:33d25ef353f6 119 static inline void set_u8_at0(uint8_t src, uint16_t dest) {
modtronix 3:99cb87ee1792 120 ((uint8_t*)&dest)[0] = src;
modtronix 3:99cb87ee1792 121 }
modtronix 3:99cb87ee1792 122
modtronix 3:99cb87ee1792 123
modtronix 3:99cb87ee1792 124 /** Write given byte to MSB 8-bit byte(uint8_t) of given 16-bit Word
modtronix 3:99cb87ee1792 125 * @param src Source 8-bit byte to write to destination
modtronix 3:99cb87ee1792 126 * @param dest Destination
modtronix 3:99cb87ee1792 127 */
modtronix 6:33d25ef353f6 128 static inline void set_u8_at1(uint8_t src, uint16_t dest) {
modtronix 3:99cb87ee1792 129 ((uint8_t*)&dest)[1] = src;
modtronix 3:99cb87ee1792 130 }
modtronix 3:99cb87ee1792 131
modtronix 3:99cb87ee1792 132
modtronix 3:99cb87ee1792 133 /** Write given byte to first(LSB) 8-bit byte(uint8_t) of given 32-bit Word
modtronix 3:99cb87ee1792 134 * @param src Source 8-bit byte to write to destination
modtronix 3:99cb87ee1792 135 * @param dest Destination
modtronix 3:99cb87ee1792 136 */
modtronix 6:33d25ef353f6 137 static inline void set_u8_at0(uint8_t src, uint32_t dest) {
modtronix 3:99cb87ee1792 138 ((uint8_t*)&dest)[0] = src;
modtronix 3:99cb87ee1792 139 }
modtronix 3:99cb87ee1792 140
modtronix 3:99cb87ee1792 141
modtronix 3:99cb87ee1792 142 /** Write given byte to second 8-bit byte(uint8_t) of given 32-bit Word
modtronix 3:99cb87ee1792 143 * @param src Source 8-bit byte to write to destination
modtronix 3:99cb87ee1792 144 * @param dest Destination
modtronix 3:99cb87ee1792 145 */
modtronix 6:33d25ef353f6 146 static inline void set_u8_at1(uint8_t src, uint32_t dest) {
modtronix 3:99cb87ee1792 147 ((uint8_t*)&dest)[1] = src;
modtronix 3:99cb87ee1792 148 }
modtronix 3:99cb87ee1792 149
modtronix 3:99cb87ee1792 150
modtronix 3:99cb87ee1792 151 /** Write given byte to third 8-bit byte(uint8_t) of given 32-bit Word
modtronix 3:99cb87ee1792 152 * @param src Source 8-bit byte to write to destination
modtronix 3:99cb87ee1792 153 * @param dest Destination
modtronix 3:99cb87ee1792 154 */
modtronix 6:33d25ef353f6 155 static inline void set_u8_at2(uint8_t src, uint32_t dest) {
modtronix 3:99cb87ee1792 156 ((uint8_t*)&dest)[2] = src;
modtronix 3:99cb87ee1792 157 }
modtronix 3:99cb87ee1792 158
modtronix 3:99cb87ee1792 159
modtronix 3:99cb87ee1792 160 /** Write given byte to last(MSB) 8-bit byte(uint8_t) of given 32-bit Word
modtronix 3:99cb87ee1792 161 * @param src Source 8-bit byte to write to destination
modtronix 3:99cb87ee1792 162 * @param dest Destination
modtronix 3:99cb87ee1792 163 */
modtronix 6:33d25ef353f6 164 static inline void set_u8_at3(uint8_t src, uint32_t dest) {
modtronix 3:99cb87ee1792 165 ((uint8_t*)&dest)[3] = src;
modtronix 3:99cb87ee1792 166 }
modtronix 3:99cb87ee1792 167
modtronix 3:99cb87ee1792 168
modtronix 6:33d25ef353f6 169 /** Get the first fractional part of float. For example, will return 5 for 123.56
modtronix 6:33d25ef353f6 170 * @return Returns first fractional part of float. For example, will return 5 for 123.56
modtronix 6:33d25ef353f6 171 */
modtronix 6:33d25ef353f6 172 static inline uint8_t get_frac1(float val) {
modtronix 6:33d25ef353f6 173 return (uint8_t)(( val - ((float)((uint32_t)val)) )*10);
modtronix 6:33d25ef353f6 174 }
modtronix 6:33d25ef353f6 175
modtronix 6:33d25ef353f6 176
modtronix 3:99cb87ee1792 177 /**
modtronix 3:99cb87ee1792 178 * Converts a "2 character ASCII hex" string to a single packed byte.
modtronix 3:99cb87ee1792 179 *
modtronix 3:99cb87ee1792 180 * @param asciiChars - WORD_VAL where
modtronix 3:99cb87ee1792 181 * asciiChars.v[0] is the ASCII value for the lower nibble and
modtronix 3:99cb87ee1792 182 * asciiChars.v[1] is the ASCII value for the upper nibble.
modtronix 3:99cb87ee1792 183 * Each must range from '0'-'9', 'A'-'F', or 'a'-'f'.
modtronix 3:99cb87ee1792 184 *
modtronix 3:99cb87ee1792 185 * @return Returns byte representation of given "2 character ASCII byte"
modtronix 3:99cb87ee1792 186 */
modtronix 3:99cb87ee1792 187 static uint8_t ascii_hex_to_byte(WORD_VAL asciiChars);
modtronix 3:99cb87ee1792 188
modtronix 3:99cb87ee1792 189
modtronix 3:99cb87ee1792 190 /**
modtronix 3:99cb87ee1792 191 * Get the hex value for the given hex encoded character (0-9, a-f).
modtronix 3:99cb87ee1792 192 * The returned value will be from 0 - 15. If the given byte is not a hex encoded
modtronix 3:99cb87ee1792 193 * character, 0xff is returned!
modtronix 3:99cb87ee1792 194 *
modtronix 3:99cb87ee1792 195 * @param c A hex encoded character, value values are 0-9, A-F and a-f
modtronix 3:99cb87ee1792 196 *
modtronix 3:99cb87ee1792 197 * @return Returns a value from 0 to 15 representing the hex value of given parameter. Or, 0xff if error.
modtronix 3:99cb87ee1792 198 */
modtronix 3:99cb87ee1792 199 static uint8_t ascii_hex_nibble_to_byte(uint8_t c);
modtronix 3:99cb87ee1792 200
modtronix 3:99cb87ee1792 201 /**
modtronix 3:99cb87ee1792 202 * Converts a byte to a "2-character uppercase ASCII hex" value. The 2 bytes
modtronix 3:99cb87ee1792 203 * are returned in the bytes of the returned WORD_VAL.
modtronix 3:99cb87ee1792 204 * For example, nzByteToAsciiHex(0xAE) will return 'E' in the LSB, and 'A' in
modtronix 3:99cb87ee1792 205 * the MSB of the returned uint16_t.
modtronix 3:99cb87ee1792 206 *
modtronix 3:99cb87ee1792 207 * @param b The byte containing the nibble to convert
modtronix 3:99cb87ee1792 208 *
modtronix 3:99cb87ee1792 209 * @return The LSB of the returned word contains the ASCII value for the lower nibble.
modtronix 3:99cb87ee1792 210 * The MSB of the returned word contains the ASCII value for the upper nibble.
modtronix 3:99cb87ee1792 211 */
modtronix 3:99cb87ee1792 212 static inline uint16_t byte_to_ascii_hex(uint8_t b) {
modtronix 3:99cb87ee1792 213 WORD_VAL w;
modtronix 3:99cb87ee1792 214 w.byte.LB = low_nibble_to_ascii_hex(b);
modtronix 3:99cb87ee1792 215 w.byte.HB = high_nibble_to_ascii_hex(b);
modtronix 3:99cb87ee1792 216 return w.Val;
modtronix 3:99cb87ee1792 217 }
modtronix 3:99cb87ee1792 218
modtronix 3:99cb87ee1792 219 /**
modtronix 3:99cb87ee1792 220 * Converts a byte to a "2-character uppercase ASCII hex" string.
modtronix 3:99cb87ee1792 221 * For example, nzByteToAsciiHex(0xAE) will return the string "AE" (LSB='E').
modtronix 3:99cb87ee1792 222 * Will always return 2 characters, for example 0x5 will return "05".
modtronix 3:99cb87ee1792 223 * A NULL termination is NOT added to the back of the returned 2 characters.
modtronix 3:99cb87ee1792 224 *
modtronix 3:99cb87ee1792 225 * @param b The byte containing the nibble to convert
modtronix 3:99cb87ee1792 226 *
modtronix 3:99cb87ee1792 227 * @param dst Pointer to buffer to write the result to. Will always write 2
modtronix 3:99cb87ee1792 228 * characters, without a NULL termination.
modtronix 3:99cb87ee1792 229 */
modtronix 3:99cb87ee1792 230 static inline void byte_to_ascii_hex_str(uint8_t b, char* dst) {
modtronix 3:99cb87ee1792 231 dst[0] = high_nibble_to_ascii_hex(b);
modtronix 3:99cb87ee1792 232 dst[1] = low_nibble_to_ascii_hex(b);
modtronix 3:99cb87ee1792 233 }
modtronix 3:99cb87ee1792 234
modtronix 3:99cb87ee1792 235 /**
modtronix 3:99cb87ee1792 236 * Converts the upper nibble of a binary value to a hexadecimal ASCII byte.
modtronix 3:99cb87ee1792 237 * For example, nzHighNibbleToAsciiHex(0xAE) will return 'A'.
modtronix 3:99cb87ee1792 238 * Same as Microchip "btohexa_high()" function
modtronix 3:99cb87ee1792 239 *
modtronix 3:99cb87ee1792 240 * @param b The byte containing the nibble to convert
modtronix 3:99cb87ee1792 241 *
modtronix 3:99cb87ee1792 242 * @return The converted ASCII hex character for the given nibble
modtronix 3:99cb87ee1792 243 */
modtronix 3:99cb87ee1792 244 static inline uint8_t high_nibble_to_ascii_hex(uint8_t b) {
modtronix 3:99cb87ee1792 245 b >>= 4;
modtronix 3:99cb87ee1792 246 return (b > 0x9u) ? b + 'A' - 10 : b + '0';
modtronix 3:99cb87ee1792 247 }
modtronix 3:99cb87ee1792 248
modtronix 3:99cb87ee1792 249 /**
modtronix 3:99cb87ee1792 250 * Converts the lower nibble of a binary value to a hexadecimal ASCII byte.
modtronix 3:99cb87ee1792 251 * For example, nzLowNibbleToAsciiHex(0xAE) will return 'E'.
modtronix 3:99cb87ee1792 252 * Same as Microchip "btohexa_low()" function
modtronix 3:99cb87ee1792 253 *
modtronix 3:99cb87ee1792 254 * @param b The byte containing the nibble to convert
modtronix 3:99cb87ee1792 255 *
modtronix 3:99cb87ee1792 256 * @return The converted ASCII hex character for the given nibble
modtronix 3:99cb87ee1792 257 */
modtronix 3:99cb87ee1792 258 static inline uint8_t low_nibble_to_ascii_hex(uint8_t b) {
modtronix 3:99cb87ee1792 259 b &= 0x0F;
modtronix 3:99cb87ee1792 260 return (b > 9u) ? b + 'A' - 10 : b + '0';
modtronix 3:99cb87ee1792 261 }
modtronix 3:99cb87ee1792 262
modtronix 3:99cb87ee1792 263
modtronix 3:99cb87ee1792 264 /** Counts number of set bits
modtronix 3:99cb87ee1792 265 *
modtronix 3:99cb87ee1792 266 * @param val: Given parameter to count bit's that are 1
modtronix 3:99cb87ee1792 267 * @return Number of set bits
modtronix 3:99cb87ee1792 268 */
modtronix 3:99cb87ee1792 269 static inline uint8_t count_ones(uint32_t val) {
modtronix 3:99cb87ee1792 270 return __builtin_popcount(val); //Use GCC built in functions, see http://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html
modtronix 3:99cb87ee1792 271 }
modtronix 3:99cb87ee1792 272
modtronix 3:99cb87ee1792 273 /**
modtronix 3:99cb87ee1792 274 * Counts number of bits in given array that are set to 1
modtronix 3:99cb87ee1792 275 *
modtronix 3:99cb87ee1792 276 * @param p: Pointer to array whos bits we must count
modtronix 3:99cb87ee1792 277 * @param size: Number of bytes in array we must count bits of
modtronix 3:99cb87ee1792 278 */
modtronix 3:99cb87ee1792 279 static inline uint8_t count_ones_arr(uint8_t* p, uint8_t size) {
modtronix 3:99cb87ee1792 280 uint8_t offset;
modtronix 3:99cb87ee1792 281 uint8_t x;
modtronix 3:99cb87ee1792 282 uint8_t count = 0;
modtronix 3:99cb87ee1792 283
modtronix 3:99cb87ee1792 284 for (offset = 0; offset < size; offset++) {
modtronix 3:99cb87ee1792 285 x = *p++;
modtronix 3:99cb87ee1792 286 for (count = 0; x != 0; x >>= 1) {
modtronix 3:99cb87ee1792 287 if (x & 0x01)
modtronix 3:99cb87ee1792 288 count++;
modtronix 3:99cb87ee1792 289 }
modtronix 3:99cb87ee1792 290 }
modtronix 3:99cb87ee1792 291 return count;
modtronix 3:99cb87ee1792 292 }
modtronix 3:99cb87ee1792 293
modtronix 3:99cb87ee1792 294
modtronix 3:99cb87ee1792 295 /** Count leading zeros
modtronix 3:99cb87ee1792 296 * This function counts the number of leading zeros of a data value.
modtronix 3:99cb87ee1792 297 *
modtronix 3:99cb87ee1792 298 * @param val Value to count the leading zeros
modtronix 3:99cb87ee1792 299 * @return number of leading zeros in value
modtronix 3:99cb87ee1792 300 */
modtronix 3:99cb87ee1792 301 static inline uint8_t count_leading_zeros(uint32_t val) {
modtronix 3:99cb87ee1792 302 return __CLZ(val);
modtronix 3:99cb87ee1792 303 //return __builtin_clz(val); //Use GCC built in functions, see http://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html
modtronix 3:99cb87ee1792 304 }
modtronix 3:99cb87ee1792 305
modtronix 3:99cb87ee1792 306 /** Count trailing zeros
modtronix 3:99cb87ee1792 307 * This function counts the number of trailing zeros of a data value.
modtronix 3:99cb87ee1792 308 * See https://en.wikipedia.org/wiki/Find_first_set
modtronix 3:99cb87ee1792 309 *
modtronix 3:99cb87ee1792 310 * @param val Value to count the trailing zeros
modtronix 3:99cb87ee1792 311 * @return number of trailing zeros in value
modtronix 3:99cb87ee1792 312 */
modtronix 3:99cb87ee1792 313 static inline uint8_t count_trailing_zeros(uint32_t val) {
modtronix 3:99cb87ee1792 314 //return __builtin_ctz(val); //Use GCC built in functions, see http://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html
modtronix 3:99cb87ee1792 315 return (__CLZ(__RBIT(val)));
modtronix 3:99cb87ee1792 316 }
modtronix 3:99cb87ee1792 317
modtronix 3:99cb87ee1792 318 /** Count leading ones
modtronix 3:99cb87ee1792 319 * This function counts the number of leading ones of a data value.
modtronix 3:99cb87ee1792 320 * See https://en.wikipedia.org/wiki/Find_first_set
modtronix 3:99cb87ee1792 321 *
modtronix 3:99cb87ee1792 322 * @param val Value to count the leading ones
modtronix 3:99cb87ee1792 323 * @return number of leading ones in value
modtronix 3:99cb87ee1792 324 */
modtronix 3:99cb87ee1792 325 static inline uint8_t count_leading_ones(uint32_t val) {
modtronix 3:99cb87ee1792 326 return __CLZ(~val);
modtronix 3:99cb87ee1792 327 //return __builtin_clz(~val); //Use GCC built in functions, see http://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html
modtronix 3:99cb87ee1792 328 }
modtronix 3:99cb87ee1792 329
modtronix 3:99cb87ee1792 330 /** Count trailing ones
modtronix 3:99cb87ee1792 331 * This function counts the number of trailing ones of a data value.
modtronix 3:99cb87ee1792 332 * See https://en.wikipedia.org/wiki/Find_first_set
modtronix 3:99cb87ee1792 333 *
modtronix 3:99cb87ee1792 334 * @param val Value to count the leading ones
modtronix 3:99cb87ee1792 335 * @return number of leading ones in value
modtronix 3:99cb87ee1792 336 */
modtronix 3:99cb87ee1792 337 static inline uint8_t count_trailing_ones(uint32_t val) {
modtronix 3:99cb87ee1792 338 //return __builtin_ctz(~val); //Use GCC built in functions, see http://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html
modtronix 3:99cb87ee1792 339 return (__CLZ(__RBIT(~val)));
modtronix 3:99cb87ee1792 340 }
modtronix 3:99cb87ee1792 341
modtronix 3:99cb87ee1792 342 /** Reverse bit order of value
modtronix 3:99cb87ee1792 343 * This function reverses the bit order of the given value.
modtronix 3:99cb87ee1792 344 *
modtronix 3:99cb87ee1792 345 * @param val Value to reverse bits of
modtronix 3:99cb87ee1792 346 * @return Reversed value
modtronix 3:99cb87ee1792 347 */
modtronix 3:99cb87ee1792 348 static inline uint32_t reverse_bits(uint32_t val) {
modtronix 3:99cb87ee1792 349 return __RBIT(val);
modtronix 3:99cb87ee1792 350 }
modtronix 3:99cb87ee1792 351
modtronix 3:99cb87ee1792 352
modtronix 3:99cb87ee1792 353 /**
modtronix 3:99cb87ee1792 354 * Get the value of the given decimal string. String value can have
modtronix 3:99cb87ee1792 355 * leading 0s. Maximum value is 65,535. Following possible leading format specifiers are
modtronix 3:99cb87ee1792 356 * checked for:
modtronix 3:99cb87ee1792 357 * - x = Upper Case Hex value to follow
modtronix 3:99cb87ee1792 358 *
modtronix 3:99cb87ee1792 359 * Some examples:
modtronix 3:99cb87ee1792 360 * "255" will return 0x00FF
modtronix 3:99cb87ee1792 361 * "x010" will return 0x0010
modtronix 3:99cb87ee1792 362 *
modtronix 3:99cb87ee1792 363 * @param str String containing hex value
modtronix 3:99cb87ee1792 364 *
modtronix 3:99cb87ee1792 365 * @param retFlags A value returned from this function.
modtronix 3:99cb87ee1792 366 * - bit 0-3 is the number of bytes parsed by this function
modtronix 3:99cb87ee1792 367 * - bits 4-7 are reserved for flags, like if the parsed value is a byte
modtronix 3:99cb87ee1792 368 * or word for example.
modtronix 3:99cb87ee1792 369 *
modtronix 3:99cb87ee1792 370 * @return Parsed word value
modtronix 3:99cb87ee1792 371 */
modtronix 3:99cb87ee1792 372 static uint16_t cvt_ascii_dec_to_uint16(const char* str, uint8_t* retFlags);
modtronix 3:99cb87ee1792 373
modtronix 3:99cb87ee1792 374
modtronix 3:99cb87ee1792 375 /**
modtronix 3:99cb87ee1792 376 * Get the value of the given Upper Case Hex, or decimal string. String value can have
modtronix 3:99cb87ee1792 377 * leading 0s. Maximum value is 0xFFFF. Following possible leading format specifiers are
modtronix 3:99cb87ee1792 378 * checked for:
modtronix 3:99cb87ee1792 379 * - d = Decimal value to follow
modtronix 3:99cb87ee1792 380 *
modtronix 3:99cb87ee1792 381 * Some examples:
modtronix 3:99cb87ee1792 382 * "A100" will return 0xA100
modtronix 3:99cb87ee1792 383 * "d010" will return 0xA
modtronix 3:99cb87ee1792 384 *
modtronix 3:99cb87ee1792 385 * @param str String containing hex value
modtronix 3:99cb87ee1792 386 *
modtronix 3:99cb87ee1792 387 * @param retFlags A value returned from this function.
modtronix 3:99cb87ee1792 388 * - bit 0-3 is the number of bytes parsed by this function
modtronix 3:99cb87ee1792 389 * - bits 4-7 are reserved for flags, like if the parsed value is a byte
modtronix 3:99cb87ee1792 390 * or word for example.
modtronix 3:99cb87ee1792 391 *
modtronix 3:99cb87ee1792 392 * @return Parsed word value
modtronix 3:99cb87ee1792 393 */
modtronix 3:99cb87ee1792 394 static uint16_t cvt_ascii_hex_to_uint16(const char* str, uint8_t* retFlags);
modtronix 3:99cb87ee1792 395
modtronix 3:99cb87ee1792 396
modtronix 3:99cb87ee1792 397 /**
modtronix 3:99cb87ee1792 398 * Converts a 16-bit unsigned integer to a null-terminated decimal string.
modtronix 3:99cb87ee1792 399 *
modtronix 3:99cb87ee1792 400 * @param val - The number to be converted
modtronix 3:99cb87ee1792 401 *
modtronix 3:99cb87ee1792 402 * @param buf - Pointer in which to store the converted string
modtronix 3:99cb87ee1792 403 *
modtronix 3:99cb87ee1792 404 * @param ret - Returns length of string written to buf. Does NOT include NULL terminator added
modtronix 3:99cb87ee1792 405 */
modtronix 3:99cb87ee1792 406 static uint8_t cvt_uint16_to_ascii_str(uint16_t val, uint8_t* buf);
modtronix 3:99cb87ee1792 407
modtronix 3:99cb87ee1792 408
modtronix 3:99cb87ee1792 409 /**
modtronix 3:99cb87ee1792 410 * Converts a 16-bit signed integer to a null-terminated decimal string.
modtronix 3:99cb87ee1792 411 *
modtronix 3:99cb87ee1792 412 * @param val - The number to be converted
modtronix 3:99cb87ee1792 413 *
modtronix 3:99cb87ee1792 414 * @param buf - Pointer in which to store the converted string
modtronix 3:99cb87ee1792 415 *
modtronix 3:99cb87ee1792 416 * @param ret - Returns length of string written to buf. Does NOT include NULL terminator added
modtronix 3:99cb87ee1792 417 */
modtronix 3:99cb87ee1792 418 static inline uint8_t cvt_int16_to_ascii_str(int16_t val, uint8_t* buf) {
modtronix 3:99cb87ee1792 419 uint8_t retVal = 0;
modtronix 3:99cb87ee1792 420 if(val < 0) {
modtronix 3:99cb87ee1792 421 *buf = '-';
modtronix 3:99cb87ee1792 422 buf++;
modtronix 3:99cb87ee1792 423 retVal++;
modtronix 3:99cb87ee1792 424 }
modtronix 3:99cb87ee1792 425 return retVal + cvt_uint16_to_ascii_str(abs(val), buf);
modtronix 3:99cb87ee1792 426 }
modtronix 3:99cb87ee1792 427
modtronix 3:99cb87ee1792 428
modtronix 3:99cb87ee1792 429 /** Rotate Right with Extend (32 bit)
modtronix 3:99cb87ee1792 430 * This function moves each bit of a bitstring right by one bit. The carry input is shifted
modtronix 3:99cb87ee1792 431 * in at the left end of the bitstring.
modtronix 3:99cb87ee1792 432 *
modtronix 3:99cb87ee1792 433 * @param value Value to rotate
modtronix 3:99cb87ee1792 434 * @return Rotated value
modtronix 3:99cb87ee1792 435 */
modtronix 3:99cb87ee1792 436 static inline uint32_t rotate_right(int32_t val) {
modtronix 3:99cb87ee1792 437 return __RRX(val);
modtronix 3:99cb87ee1792 438 }
modtronix 3:99cb87ee1792 439
modtronix 3:99cb87ee1792 440 //Protected Data
modtronix 3:99cb87ee1792 441 protected:
modtronix 3:99cb87ee1792 442
modtronix 3:99cb87ee1792 443 };
modtronix 3:99cb87ee1792 444
modtronix-com 9:5000feb4b46f 445 #if defined(MXH_DEBUG)
modtronix-com 9:5000feb4b46f 446 #undef MXH_DEBUG
modtronix-com 9:5000feb4b46f 447 #endif
modtronix-com 9:5000feb4b46f 448 #if defined(MXH_DEBUG_INFO)
modtronix-com 9:5000feb4b46f 449 #undef MXH_DEBUG_INFO
modtronix-com 9:5000feb4b46f 450 #endif
modtronix-com 9:5000feb4b46f 451
modtronix-com 9:5000feb4b46f 452
modtronix 3:99cb87ee1792 453 #endif /* MX_HELPERS_H_ */