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