Library for Modtronix NZ32 STM32 boards, like the NZ32-SC151, NZ32-SB072, NZ32-SE411 and others
mx_helpers.h@3:99cb87ee1792, 2015-08-30 (annotated)
- Committer:
- modtronix
- Date:
- Sun Aug 30 09:23:05 2015 +1000
- Revision:
- 3:99cb87ee1792
- Child:
- 5:e1297df8ee0d
Updated, added new files
Who changed what in which revision?
User | Revision | Line number | New 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 | 3:99cb87ee1792 | 54 | class MxHelpers { |
modtronix | 3:99cb87ee1792 | 55 | public: |
modtronix | 3:99cb87ee1792 | 56 | /** Get first(LSB) 8-bit byte(uint8_t) of given 16-bit Word |
modtronix | 3:99cb87ee1792 | 57 | * @return Returns the lower(LSB) byte of given value |
modtronix | 3:99cb87ee1792 | 58 | */ |
modtronix | 3:99cb87ee1792 | 59 | static inline uint8_t read_uint8_at_0(uint16_t val) { |
modtronix | 3:99cb87ee1792 | 60 | return ((uint8_t)((val)&0xFF)); |
modtronix | 3:99cb87ee1792 | 61 | } |
modtronix | 3:99cb87ee1792 | 62 | |
modtronix | 3:99cb87ee1792 | 63 | |
modtronix | 3:99cb87ee1792 | 64 | /** Get second(MSB) 8-bit byte(uint8_t) of given 16-bit Word. |
modtronix | 3:99cb87ee1792 | 65 | * @return Returns the upper(MSB) byte of given value |
modtronix | 3:99cb87ee1792 | 66 | */ |
modtronix | 3:99cb87ee1792 | 67 | static inline uint8_t read_uint8_at_1(uint16_t val) { |
modtronix | 3:99cb87ee1792 | 68 | return ((uint8_t)(((val)>>8)&0xFF)); |
modtronix | 3:99cb87ee1792 | 69 | } |
modtronix | 3:99cb87ee1792 | 70 | |
modtronix | 3:99cb87ee1792 | 71 | |
modtronix | 3:99cb87ee1792 | 72 | /** Get first(LSB) 8-bit byte(uint8_t) of given 32-bit Word |
modtronix | 3:99cb87ee1792 | 73 | * @return Returns the lower(LSB) byte of given value = byte at bit offset 0 to 7 |
modtronix | 3:99cb87ee1792 | 74 | */ |
modtronix | 3:99cb87ee1792 | 75 | static inline uint8_t read_uint8_at_0(uint32_t val) { |
modtronix | 3:99cb87ee1792 | 76 | return ((uint8_t)((val)&0xFF)); |
modtronix | 3:99cb87ee1792 | 77 | } |
modtronix | 3:99cb87ee1792 | 78 | |
modtronix | 3:99cb87ee1792 | 79 | |
modtronix | 3:99cb87ee1792 | 80 | /** Get second 8-bit byte(uint8_t) of given 32-bit Word |
modtronix | 3:99cb87ee1792 | 81 | * @return Returns the second byte of given value = byte at bit offset 8 to 15 |
modtronix | 3:99cb87ee1792 | 82 | */ |
modtronix | 3:99cb87ee1792 | 83 | static inline uint8_t read_uint8_at_1(uint32_t val) { |
modtronix | 3:99cb87ee1792 | 84 | return ((uint8_t)(((val)>>8)&0xFF)); |
modtronix | 3:99cb87ee1792 | 85 | } |
modtronix | 3:99cb87ee1792 | 86 | |
modtronix | 3:99cb87ee1792 | 87 | |
modtronix | 3:99cb87ee1792 | 88 | /** Get third 8-bit byte(uint8_t) of given 32-bit Word |
modtronix | 3:99cb87ee1792 | 89 | * @return Returns the third byte of given value = byte at bit offset 16 to 23 |
modtronix | 3:99cb87ee1792 | 90 | */ |
modtronix | 3:99cb87ee1792 | 91 | static inline uint8_t read_uint8_at_2(uint32_t val) { |
modtronix | 3:99cb87ee1792 | 92 | return ((uint8_t)(((val)>>16)&0xFF)); |
modtronix | 3:99cb87ee1792 | 93 | } |
modtronix | 3:99cb87ee1792 | 94 | |
modtronix | 3:99cb87ee1792 | 95 | |
modtronix | 3:99cb87ee1792 | 96 | /** Get forth(MSB) 8-bit byte(uint8_t) of given 32-bit Word |
modtronix | 3:99cb87ee1792 | 97 | * @return Returns the MSB byte of given value = byte at bit offset 24 to 31 |
modtronix | 3:99cb87ee1792 | 98 | */ |
modtronix | 3:99cb87ee1792 | 99 | static inline uint8_t read_uint8_at_3(uint32_t val) { |
modtronix | 3:99cb87ee1792 | 100 | return ((uint8_t)(((val)>>24)&0xFF)); |
modtronix | 3:99cb87ee1792 | 101 | } |
modtronix | 3:99cb87ee1792 | 102 | |
modtronix | 3:99cb87ee1792 | 103 | |
modtronix | 3:99cb87ee1792 | 104 | /** Write given byte to first(LSB) 8-bit byte(uint8_t) of given 16-bit Word |
modtronix | 3:99cb87ee1792 | 105 | * @param src Source 8-bit byte to write to destination |
modtronix | 3:99cb87ee1792 | 106 | * @param dest Destination |
modtronix | 3:99cb87ee1792 | 107 | */ |
modtronix | 3:99cb87ee1792 | 108 | static inline void write_uint8_at_0(uint8_t src, uint16_t dest) { |
modtronix | 3:99cb87ee1792 | 109 | ((uint8_t*)&dest)[0] = src; |
modtronix | 3:99cb87ee1792 | 110 | } |
modtronix | 3:99cb87ee1792 | 111 | |
modtronix | 3:99cb87ee1792 | 112 | |
modtronix | 3:99cb87ee1792 | 113 | /** Write given byte to MSB 8-bit byte(uint8_t) of given 16-bit Word |
modtronix | 3:99cb87ee1792 | 114 | * @param src Source 8-bit byte to write to destination |
modtronix | 3:99cb87ee1792 | 115 | * @param dest Destination |
modtronix | 3:99cb87ee1792 | 116 | */ |
modtronix | 3:99cb87ee1792 | 117 | static inline void write_uint8_at_1(uint8_t src, uint16_t dest) { |
modtronix | 3:99cb87ee1792 | 118 | ((uint8_t*)&dest)[1] = src; |
modtronix | 3:99cb87ee1792 | 119 | } |
modtronix | 3:99cb87ee1792 | 120 | |
modtronix | 3:99cb87ee1792 | 121 | |
modtronix | 3:99cb87ee1792 | 122 | /** Write given byte to first(LSB) 8-bit byte(uint8_t) of given 32-bit Word |
modtronix | 3:99cb87ee1792 | 123 | * @param src Source 8-bit byte to write to destination |
modtronix | 3:99cb87ee1792 | 124 | * @param dest Destination |
modtronix | 3:99cb87ee1792 | 125 | */ |
modtronix | 3:99cb87ee1792 | 126 | static inline void write_uint8_at_0(uint8_t src, uint32_t dest) { |
modtronix | 3:99cb87ee1792 | 127 | ((uint8_t*)&dest)[0] = src; |
modtronix | 3:99cb87ee1792 | 128 | } |
modtronix | 3:99cb87ee1792 | 129 | |
modtronix | 3:99cb87ee1792 | 130 | |
modtronix | 3:99cb87ee1792 | 131 | /** Write given byte to second 8-bit byte(uint8_t) of given 32-bit Word |
modtronix | 3:99cb87ee1792 | 132 | * @param src Source 8-bit byte to write to destination |
modtronix | 3:99cb87ee1792 | 133 | * @param dest Destination |
modtronix | 3:99cb87ee1792 | 134 | */ |
modtronix | 3:99cb87ee1792 | 135 | static inline void write_uint8_at_1(uint8_t src, uint32_t dest) { |
modtronix | 3:99cb87ee1792 | 136 | ((uint8_t*)&dest)[1] = src; |
modtronix | 3:99cb87ee1792 | 137 | } |
modtronix | 3:99cb87ee1792 | 138 | |
modtronix | 3:99cb87ee1792 | 139 | |
modtronix | 3:99cb87ee1792 | 140 | /** Write given byte to third 8-bit byte(uint8_t) of given 32-bit Word |
modtronix | 3:99cb87ee1792 | 141 | * @param src Source 8-bit byte to write to destination |
modtronix | 3:99cb87ee1792 | 142 | * @param dest Destination |
modtronix | 3:99cb87ee1792 | 143 | */ |
modtronix | 3:99cb87ee1792 | 144 | static inline void write_uint8_at_2(uint8_t src, uint32_t dest) { |
modtronix | 3:99cb87ee1792 | 145 | ((uint8_t*)&dest)[2] = src; |
modtronix | 3:99cb87ee1792 | 146 | } |
modtronix | 3:99cb87ee1792 | 147 | |
modtronix | 3:99cb87ee1792 | 148 | |
modtronix | 3:99cb87ee1792 | 149 | /** Write given byte to last(MSB) 8-bit byte(uint8_t) of given 32-bit Word |
modtronix | 3:99cb87ee1792 | 150 | * @param src Source 8-bit byte to write to destination |
modtronix | 3:99cb87ee1792 | 151 | * @param dest Destination |
modtronix | 3:99cb87ee1792 | 152 | */ |
modtronix | 3:99cb87ee1792 | 153 | static inline void write_uint8_at_3(uint8_t src, uint32_t dest) { |
modtronix | 3:99cb87ee1792 | 154 | ((uint8_t*)&dest)[3] = src; |
modtronix | 3:99cb87ee1792 | 155 | } |
modtronix | 3:99cb87ee1792 | 156 | |
modtronix | 3:99cb87ee1792 | 157 | |
modtronix | 3:99cb87ee1792 | 158 | /** |
modtronix | 3:99cb87ee1792 | 159 | * Converts a "2 character ASCII hex" string to a single packed byte. |
modtronix | 3:99cb87ee1792 | 160 | * |
modtronix | 3:99cb87ee1792 | 161 | * @param asciiChars - WORD_VAL where |
modtronix | 3:99cb87ee1792 | 162 | * asciiChars.v[0] is the ASCII value for the lower nibble and |
modtronix | 3:99cb87ee1792 | 163 | * asciiChars.v[1] is the ASCII value for the upper nibble. |
modtronix | 3:99cb87ee1792 | 164 | * Each must range from '0'-'9', 'A'-'F', or 'a'-'f'. |
modtronix | 3:99cb87ee1792 | 165 | * |
modtronix | 3:99cb87ee1792 | 166 | * @return Returns byte representation of given "2 character ASCII byte" |
modtronix | 3:99cb87ee1792 | 167 | */ |
modtronix | 3:99cb87ee1792 | 168 | static uint8_t ascii_hex_to_byte(WORD_VAL asciiChars); |
modtronix | 3:99cb87ee1792 | 169 | |
modtronix | 3:99cb87ee1792 | 170 | |
modtronix | 3:99cb87ee1792 | 171 | /** |
modtronix | 3:99cb87ee1792 | 172 | * Get the hex value for the given hex encoded character (0-9, a-f). |
modtronix | 3:99cb87ee1792 | 173 | * The returned value will be from 0 - 15. If the given byte is not a hex encoded |
modtronix | 3:99cb87ee1792 | 174 | * character, 0xff is returned! |
modtronix | 3:99cb87ee1792 | 175 | * |
modtronix | 3:99cb87ee1792 | 176 | * @param c A hex encoded character, value values are 0-9, A-F and a-f |
modtronix | 3:99cb87ee1792 | 177 | * |
modtronix | 3:99cb87ee1792 | 178 | * @return Returns a value from 0 to 15 representing the hex value of given parameter. Or, 0xff if error. |
modtronix | 3:99cb87ee1792 | 179 | */ |
modtronix | 3:99cb87ee1792 | 180 | static uint8_t ascii_hex_nibble_to_byte(uint8_t c); |
modtronix | 3:99cb87ee1792 | 181 | |
modtronix | 3:99cb87ee1792 | 182 | /** |
modtronix | 3:99cb87ee1792 | 183 | * Converts a byte to a "2-character uppercase ASCII hex" value. The 2 bytes |
modtronix | 3:99cb87ee1792 | 184 | * are returned in the bytes of the returned WORD_VAL. |
modtronix | 3:99cb87ee1792 | 185 | * For example, nzByteToAsciiHex(0xAE) will return 'E' in the LSB, and 'A' in |
modtronix | 3:99cb87ee1792 | 186 | * the MSB of the returned uint16_t. |
modtronix | 3:99cb87ee1792 | 187 | * |
modtronix | 3:99cb87ee1792 | 188 | * @param b The byte containing the nibble to convert |
modtronix | 3:99cb87ee1792 | 189 | * |
modtronix | 3:99cb87ee1792 | 190 | * @return The LSB of the returned word contains the ASCII value for the lower nibble. |
modtronix | 3:99cb87ee1792 | 191 | * The MSB of the returned word contains the ASCII value for the upper nibble. |
modtronix | 3:99cb87ee1792 | 192 | */ |
modtronix | 3:99cb87ee1792 | 193 | static inline uint16_t byte_to_ascii_hex(uint8_t b) { |
modtronix | 3:99cb87ee1792 | 194 | WORD_VAL w; |
modtronix | 3:99cb87ee1792 | 195 | w.byte.LB = low_nibble_to_ascii_hex(b); |
modtronix | 3:99cb87ee1792 | 196 | w.byte.HB = high_nibble_to_ascii_hex(b); |
modtronix | 3:99cb87ee1792 | 197 | return w.Val; |
modtronix | 3:99cb87ee1792 | 198 | } |
modtronix | 3:99cb87ee1792 | 199 | |
modtronix | 3:99cb87ee1792 | 200 | /** |
modtronix | 3:99cb87ee1792 | 201 | * Converts a byte to a "2-character uppercase ASCII hex" string. |
modtronix | 3:99cb87ee1792 | 202 | * For example, nzByteToAsciiHex(0xAE) will return the string "AE" (LSB='E'). |
modtronix | 3:99cb87ee1792 | 203 | * Will always return 2 characters, for example 0x5 will return "05". |
modtronix | 3:99cb87ee1792 | 204 | * A NULL termination is NOT added to the back of the returned 2 characters. |
modtronix | 3:99cb87ee1792 | 205 | * |
modtronix | 3:99cb87ee1792 | 206 | * @param b The byte containing the nibble to convert |
modtronix | 3:99cb87ee1792 | 207 | * |
modtronix | 3:99cb87ee1792 | 208 | * @param dst Pointer to buffer to write the result to. Will always write 2 |
modtronix | 3:99cb87ee1792 | 209 | * characters, without a NULL termination. |
modtronix | 3:99cb87ee1792 | 210 | */ |
modtronix | 3:99cb87ee1792 | 211 | static inline void byte_to_ascii_hex_str(uint8_t b, char* dst) { |
modtronix | 3:99cb87ee1792 | 212 | dst[0] = high_nibble_to_ascii_hex(b); |
modtronix | 3:99cb87ee1792 | 213 | dst[1] = low_nibble_to_ascii_hex(b); |
modtronix | 3:99cb87ee1792 | 214 | } |
modtronix | 3:99cb87ee1792 | 215 | |
modtronix | 3:99cb87ee1792 | 216 | /** |
modtronix | 3:99cb87ee1792 | 217 | * Converts the upper nibble of a binary value to a hexadecimal ASCII byte. |
modtronix | 3:99cb87ee1792 | 218 | * For example, nzHighNibbleToAsciiHex(0xAE) will return 'A'. |
modtronix | 3:99cb87ee1792 | 219 | * Same as Microchip "btohexa_high()" function |
modtronix | 3:99cb87ee1792 | 220 | * |
modtronix | 3:99cb87ee1792 | 221 | * @param b The byte containing the nibble to convert |
modtronix | 3:99cb87ee1792 | 222 | * |
modtronix | 3:99cb87ee1792 | 223 | * @return The converted ASCII hex character for the given nibble |
modtronix | 3:99cb87ee1792 | 224 | */ |
modtronix | 3:99cb87ee1792 | 225 | static inline uint8_t high_nibble_to_ascii_hex(uint8_t b) { |
modtronix | 3:99cb87ee1792 | 226 | b >>= 4; |
modtronix | 3:99cb87ee1792 | 227 | return (b > 0x9u) ? b + 'A' - 10 : b + '0'; |
modtronix | 3:99cb87ee1792 | 228 | } |
modtronix | 3:99cb87ee1792 | 229 | |
modtronix | 3:99cb87ee1792 | 230 | /** |
modtronix | 3:99cb87ee1792 | 231 | * Converts the lower nibble of a binary value to a hexadecimal ASCII byte. |
modtronix | 3:99cb87ee1792 | 232 | * For example, nzLowNibbleToAsciiHex(0xAE) will return 'E'. |
modtronix | 3:99cb87ee1792 | 233 | * Same as Microchip "btohexa_low()" function |
modtronix | 3:99cb87ee1792 | 234 | * |
modtronix | 3:99cb87ee1792 | 235 | * @param b The byte containing the nibble to convert |
modtronix | 3:99cb87ee1792 | 236 | * |
modtronix | 3:99cb87ee1792 | 237 | * @return The converted ASCII hex character for the given nibble |
modtronix | 3:99cb87ee1792 | 238 | */ |
modtronix | 3:99cb87ee1792 | 239 | static inline uint8_t low_nibble_to_ascii_hex(uint8_t b) { |
modtronix | 3:99cb87ee1792 | 240 | b &= 0x0F; |
modtronix | 3:99cb87ee1792 | 241 | return (b > 9u) ? b + 'A' - 10 : b + '0'; |
modtronix | 3:99cb87ee1792 | 242 | } |
modtronix | 3:99cb87ee1792 | 243 | |
modtronix | 3:99cb87ee1792 | 244 | |
modtronix | 3:99cb87ee1792 | 245 | /** Counts number of set bits |
modtronix | 3:99cb87ee1792 | 246 | * |
modtronix | 3:99cb87ee1792 | 247 | * @param val: Given parameter to count bit's that are 1 |
modtronix | 3:99cb87ee1792 | 248 | * @return Number of set bits |
modtronix | 3:99cb87ee1792 | 249 | */ |
modtronix | 3:99cb87ee1792 | 250 | static inline uint8_t count_ones(uint32_t val) { |
modtronix | 3:99cb87ee1792 | 251 | return __builtin_popcount(val); //Use GCC built in functions, see http://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html |
modtronix | 3:99cb87ee1792 | 252 | } |
modtronix | 3:99cb87ee1792 | 253 | |
modtronix | 3:99cb87ee1792 | 254 | /** |
modtronix | 3:99cb87ee1792 | 255 | * Counts number of bits in given array that are set to 1 |
modtronix | 3:99cb87ee1792 | 256 | * |
modtronix | 3:99cb87ee1792 | 257 | * @param p: Pointer to array whos bits we must count |
modtronix | 3:99cb87ee1792 | 258 | * @param size: Number of bytes in array we must count bits of |
modtronix | 3:99cb87ee1792 | 259 | */ |
modtronix | 3:99cb87ee1792 | 260 | static inline uint8_t count_ones_arr(uint8_t* p, uint8_t size) { |
modtronix | 3:99cb87ee1792 | 261 | uint8_t offset; |
modtronix | 3:99cb87ee1792 | 262 | uint8_t x; |
modtronix | 3:99cb87ee1792 | 263 | uint8_t count = 0; |
modtronix | 3:99cb87ee1792 | 264 | |
modtronix | 3:99cb87ee1792 | 265 | for (offset = 0; offset < size; offset++) { |
modtronix | 3:99cb87ee1792 | 266 | x = *p++; |
modtronix | 3:99cb87ee1792 | 267 | for (count = 0; x != 0; x >>= 1) { |
modtronix | 3:99cb87ee1792 | 268 | if (x & 0x01) |
modtronix | 3:99cb87ee1792 | 269 | count++; |
modtronix | 3:99cb87ee1792 | 270 | } |
modtronix | 3:99cb87ee1792 | 271 | } |
modtronix | 3:99cb87ee1792 | 272 | return count; |
modtronix | 3:99cb87ee1792 | 273 | } |
modtronix | 3:99cb87ee1792 | 274 | |
modtronix | 3:99cb87ee1792 | 275 | |
modtronix | 3:99cb87ee1792 | 276 | /** Count leading zeros |
modtronix | 3:99cb87ee1792 | 277 | * This function counts the number of leading zeros of a data value. |
modtronix | 3:99cb87ee1792 | 278 | * |
modtronix | 3:99cb87ee1792 | 279 | * @param val Value to count the leading zeros |
modtronix | 3:99cb87ee1792 | 280 | * @return number of leading zeros in value |
modtronix | 3:99cb87ee1792 | 281 | */ |
modtronix | 3:99cb87ee1792 | 282 | static inline uint8_t count_leading_zeros(uint32_t val) { |
modtronix | 3:99cb87ee1792 | 283 | return __CLZ(val); |
modtronix | 3:99cb87ee1792 | 284 | //return __builtin_clz(val); //Use GCC built in functions, see http://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html |
modtronix | 3:99cb87ee1792 | 285 | } |
modtronix | 3:99cb87ee1792 | 286 | |
modtronix | 3:99cb87ee1792 | 287 | /** Count trailing zeros |
modtronix | 3:99cb87ee1792 | 288 | * This function counts the number of trailing zeros of a data value. |
modtronix | 3:99cb87ee1792 | 289 | * See https://en.wikipedia.org/wiki/Find_first_set |
modtronix | 3:99cb87ee1792 | 290 | * |
modtronix | 3:99cb87ee1792 | 291 | * @param val Value to count the trailing zeros |
modtronix | 3:99cb87ee1792 | 292 | * @return number of trailing zeros in value |
modtronix | 3:99cb87ee1792 | 293 | */ |
modtronix | 3:99cb87ee1792 | 294 | static inline uint8_t count_trailing_zeros(uint32_t val) { |
modtronix | 3:99cb87ee1792 | 295 | //return __builtin_ctz(val); //Use GCC built in functions, see http://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html |
modtronix | 3:99cb87ee1792 | 296 | return (__CLZ(__RBIT(val))); |
modtronix | 3:99cb87ee1792 | 297 | } |
modtronix | 3:99cb87ee1792 | 298 | |
modtronix | 3:99cb87ee1792 | 299 | /** Count leading ones |
modtronix | 3:99cb87ee1792 | 300 | * This function counts the number of leading ones of a data value. |
modtronix | 3:99cb87ee1792 | 301 | * See https://en.wikipedia.org/wiki/Find_first_set |
modtronix | 3:99cb87ee1792 | 302 | * |
modtronix | 3:99cb87ee1792 | 303 | * @param val Value to count the leading ones |
modtronix | 3:99cb87ee1792 | 304 | * @return number of leading ones in value |
modtronix | 3:99cb87ee1792 | 305 | */ |
modtronix | 3:99cb87ee1792 | 306 | static inline uint8_t count_leading_ones(uint32_t val) { |
modtronix | 3:99cb87ee1792 | 307 | return __CLZ(~val); |
modtronix | 3:99cb87ee1792 | 308 | //return __builtin_clz(~val); //Use GCC built in functions, see http://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html |
modtronix | 3:99cb87ee1792 | 309 | } |
modtronix | 3:99cb87ee1792 | 310 | |
modtronix | 3:99cb87ee1792 | 311 | /** Count trailing ones |
modtronix | 3:99cb87ee1792 | 312 | * This function counts the number of trailing ones of a data value. |
modtronix | 3:99cb87ee1792 | 313 | * See https://en.wikipedia.org/wiki/Find_first_set |
modtronix | 3:99cb87ee1792 | 314 | * |
modtronix | 3:99cb87ee1792 | 315 | * @param val Value to count the leading ones |
modtronix | 3:99cb87ee1792 | 316 | * @return number of leading ones in value |
modtronix | 3:99cb87ee1792 | 317 | */ |
modtronix | 3:99cb87ee1792 | 318 | static inline uint8_t count_trailing_ones(uint32_t val) { |
modtronix | 3:99cb87ee1792 | 319 | //return __builtin_ctz(~val); //Use GCC built in functions, see http://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html |
modtronix | 3:99cb87ee1792 | 320 | return (__CLZ(__RBIT(~val))); |
modtronix | 3:99cb87ee1792 | 321 | } |
modtronix | 3:99cb87ee1792 | 322 | |
modtronix | 3:99cb87ee1792 | 323 | /** Reverse bit order of value |
modtronix | 3:99cb87ee1792 | 324 | * This function reverses the bit order of the given value. |
modtronix | 3:99cb87ee1792 | 325 | * |
modtronix | 3:99cb87ee1792 | 326 | * @param val Value to reverse bits of |
modtronix | 3:99cb87ee1792 | 327 | * @return Reversed value |
modtronix | 3:99cb87ee1792 | 328 | */ |
modtronix | 3:99cb87ee1792 | 329 | static inline uint32_t reverse_bits(uint32_t val) { |
modtronix | 3:99cb87ee1792 | 330 | return __RBIT(val); |
modtronix | 3:99cb87ee1792 | 331 | } |
modtronix | 3:99cb87ee1792 | 332 | |
modtronix | 3:99cb87ee1792 | 333 | |
modtronix | 3:99cb87ee1792 | 334 | /** |
modtronix | 3:99cb87ee1792 | 335 | * Get the value of the given decimal string. String value can have |
modtronix | 3:99cb87ee1792 | 336 | * leading 0s. Maximum value is 65,535. Following possible leading format specifiers are |
modtronix | 3:99cb87ee1792 | 337 | * checked for: |
modtronix | 3:99cb87ee1792 | 338 | * - x = Upper Case Hex value to follow |
modtronix | 3:99cb87ee1792 | 339 | * |
modtronix | 3:99cb87ee1792 | 340 | * Some examples: |
modtronix | 3:99cb87ee1792 | 341 | * "255" will return 0x00FF |
modtronix | 3:99cb87ee1792 | 342 | * "x010" will return 0x0010 |
modtronix | 3:99cb87ee1792 | 343 | * |
modtronix | 3:99cb87ee1792 | 344 | * @param str String containing hex value |
modtronix | 3:99cb87ee1792 | 345 | * |
modtronix | 3:99cb87ee1792 | 346 | * @param retFlags A value returned from this function. |
modtronix | 3:99cb87ee1792 | 347 | * - bit 0-3 is the number of bytes parsed by this function |
modtronix | 3:99cb87ee1792 | 348 | * - bits 4-7 are reserved for flags, like if the parsed value is a byte |
modtronix | 3:99cb87ee1792 | 349 | * or word for example. |
modtronix | 3:99cb87ee1792 | 350 | * |
modtronix | 3:99cb87ee1792 | 351 | * @return Parsed word value |
modtronix | 3:99cb87ee1792 | 352 | */ |
modtronix | 3:99cb87ee1792 | 353 | static uint16_t cvt_ascii_dec_to_uint16(const char* str, uint8_t* retFlags); |
modtronix | 3:99cb87ee1792 | 354 | |
modtronix | 3:99cb87ee1792 | 355 | |
modtronix | 3:99cb87ee1792 | 356 | /** |
modtronix | 3:99cb87ee1792 | 357 | * Get the value of the given Upper Case Hex, or decimal string. String value can have |
modtronix | 3:99cb87ee1792 | 358 | * leading 0s. Maximum value is 0xFFFF. Following possible leading format specifiers are |
modtronix | 3:99cb87ee1792 | 359 | * checked for: |
modtronix | 3:99cb87ee1792 | 360 | * - d = Decimal value to follow |
modtronix | 3:99cb87ee1792 | 361 | * |
modtronix | 3:99cb87ee1792 | 362 | * Some examples: |
modtronix | 3:99cb87ee1792 | 363 | * "A100" will return 0xA100 |
modtronix | 3:99cb87ee1792 | 364 | * "d010" will return 0xA |
modtronix | 3:99cb87ee1792 | 365 | * |
modtronix | 3:99cb87ee1792 | 366 | * @param str String containing hex value |
modtronix | 3:99cb87ee1792 | 367 | * |
modtronix | 3:99cb87ee1792 | 368 | * @param retFlags A value returned from this function. |
modtronix | 3:99cb87ee1792 | 369 | * - bit 0-3 is the number of bytes parsed by this function |
modtronix | 3:99cb87ee1792 | 370 | * - bits 4-7 are reserved for flags, like if the parsed value is a byte |
modtronix | 3:99cb87ee1792 | 371 | * or word for example. |
modtronix | 3:99cb87ee1792 | 372 | * |
modtronix | 3:99cb87ee1792 | 373 | * @return Parsed word value |
modtronix | 3:99cb87ee1792 | 374 | */ |
modtronix | 3:99cb87ee1792 | 375 | static uint16_t cvt_ascii_hex_to_uint16(const char* str, uint8_t* retFlags); |
modtronix | 3:99cb87ee1792 | 376 | |
modtronix | 3:99cb87ee1792 | 377 | |
modtronix | 3:99cb87ee1792 | 378 | /** |
modtronix | 3:99cb87ee1792 | 379 | * Converts a 16-bit unsigned integer to a null-terminated decimal string. |
modtronix | 3:99cb87ee1792 | 380 | * |
modtronix | 3:99cb87ee1792 | 381 | * @param val - The number to be converted |
modtronix | 3:99cb87ee1792 | 382 | * |
modtronix | 3:99cb87ee1792 | 383 | * @param buf - Pointer in which to store the converted string |
modtronix | 3:99cb87ee1792 | 384 | * |
modtronix | 3:99cb87ee1792 | 385 | * @param ret - Returns length of string written to buf. Does NOT include NULL terminator added |
modtronix | 3:99cb87ee1792 | 386 | */ |
modtronix | 3:99cb87ee1792 | 387 | static uint8_t cvt_uint16_to_ascii_str(uint16_t val, uint8_t* buf); |
modtronix | 3:99cb87ee1792 | 388 | |
modtronix | 3:99cb87ee1792 | 389 | |
modtronix | 3:99cb87ee1792 | 390 | /** |
modtronix | 3:99cb87ee1792 | 391 | * Converts a 16-bit signed integer to a null-terminated decimal string. |
modtronix | 3:99cb87ee1792 | 392 | * |
modtronix | 3:99cb87ee1792 | 393 | * @param val - The number to be converted |
modtronix | 3:99cb87ee1792 | 394 | * |
modtronix | 3:99cb87ee1792 | 395 | * @param buf - Pointer in which to store the converted string |
modtronix | 3:99cb87ee1792 | 396 | * |
modtronix | 3:99cb87ee1792 | 397 | * @param ret - Returns length of string written to buf. Does NOT include NULL terminator added |
modtronix | 3:99cb87ee1792 | 398 | */ |
modtronix | 3:99cb87ee1792 | 399 | static inline uint8_t cvt_int16_to_ascii_str(int16_t val, uint8_t* buf) { |
modtronix | 3:99cb87ee1792 | 400 | uint8_t retVal = 0; |
modtronix | 3:99cb87ee1792 | 401 | if(val < 0) { |
modtronix | 3:99cb87ee1792 | 402 | *buf = '-'; |
modtronix | 3:99cb87ee1792 | 403 | buf++; |
modtronix | 3:99cb87ee1792 | 404 | retVal++; |
modtronix | 3:99cb87ee1792 | 405 | } |
modtronix | 3:99cb87ee1792 | 406 | return retVal + cvt_uint16_to_ascii_str(abs(val), buf); |
modtronix | 3:99cb87ee1792 | 407 | } |
modtronix | 3:99cb87ee1792 | 408 | |
modtronix | 3:99cb87ee1792 | 409 | |
modtronix | 3:99cb87ee1792 | 410 | /** Rotate Right with Extend (32 bit) |
modtronix | 3:99cb87ee1792 | 411 | * This function moves each bit of a bitstring right by one bit. The carry input is shifted |
modtronix | 3:99cb87ee1792 | 412 | * in at the left end of the bitstring. |
modtronix | 3:99cb87ee1792 | 413 | * |
modtronix | 3:99cb87ee1792 | 414 | * @param value Value to rotate |
modtronix | 3:99cb87ee1792 | 415 | * @return Rotated value |
modtronix | 3:99cb87ee1792 | 416 | */ |
modtronix | 3:99cb87ee1792 | 417 | static inline uint32_t rotate_right(int32_t val) { |
modtronix | 3:99cb87ee1792 | 418 | return __RRX(val); |
modtronix | 3:99cb87ee1792 | 419 | } |
modtronix | 3:99cb87ee1792 | 420 | |
modtronix | 3:99cb87ee1792 | 421 | //Protected Data |
modtronix | 3:99cb87ee1792 | 422 | protected: |
modtronix | 3:99cb87ee1792 | 423 | |
modtronix | 3:99cb87ee1792 | 424 | }; |
modtronix | 3:99cb87ee1792 | 425 | |
modtronix | 3:99cb87ee1792 | 426 | #endif /* MX_HELPERS_H_ */ |