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