diff options
author | Harry Cutts <hcutts@chromium.org> | 2019-10-08 13:40:32 -0700 |
---|---|---|
committer | Commit Bot <commit-bot@chromium.org> | 2019-10-09 19:34:33 +0000 |
commit | ef1f88d4561b96c24485d910a402f59b3005b1e6 (patch) | |
tree | 84fcb0116aef45dab4d02411f96e890e9a05a6dd | |
parent | 1b6a44ad9e6c8f69dea9fc184a3b5054bad7df8c (diff) | |
download | chrome-ec-ef1f88d4561b96c24485d910a402f59b3005b1e6.tar.gz |
common: Add more macros for making unsigned ints
A `UINT32_FROM_BYTE_ARRAY_BE` macro would be good to have for
https://crrev.com/i/1898937, and since it seems there are use cases for
for a little-endian variant of that and a big-endian variant for
uint16_t, add those as well.
BRANCH=none
BUG=none
TEST=Add the following host command somewhere and manually verify the
results:
static int command_macro_test(int argc, char **argv)
{
uint8_t bytes[] = { 0x01, 0x23, 0x45, 0x67 };
CPRINTF("Bytes (all hex): %02X, %02X, %02X, %02X\n",
bytes[0], bytes[1], bytes[2], bytes[3]);
CPRINTF("16 bits (using first two bytes of array):\n");
CPRINTF("UINT16_FROM_BYTE_ARRAY_LE(bytes, 0): %04X\n",
UINT16_FROM_BYTE_ARRAY_LE(bytes, 0));
CPRINTF("UINT16_FROM_BYTE_ARRAY_BE(bytes, 0): %04X\n",
UINT16_FROM_BYTE_ARRAY_BE(bytes, 0));
CPRINTF("32 bits:\n");
CPRINTF("UINT32_FROM_BYTE_ARRAY_LE(bytes, 0): %08X\n",
UINT32_FROM_BYTE_ARRAY_LE(bytes, 0));
CPRINTF("UINT32_FROM_BYTE_ARRAY_BE(bytes, 0): %08X\n",
UINT32_FROM_BYTE_ARRAY_BE(bytes, 0));
return EC_SUCCESS;
}
DECLARE_CONSOLE_COMMAND(macro_test, command_macro_test, "",
"Test the UINT macros");
Change-Id: I7c2053c846f43d369402c01c0d46ce8546e4923a
Signed-off-by: Harry Cutts <hcutts@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/1848259
Reviewed-by: Daisuke Nojiri <dnojiri@chromium.org>
-rw-r--r-- | include/common.h | 18 |
1 files changed, 16 insertions, 2 deletions
diff --git a/include/common.h b/include/common.h index dfa7270233..2ed4e11b88 100644 --- a/include/common.h +++ b/include/common.h @@ -128,10 +128,24 @@ #endif #endif -/* Macros for combining bytes into uint16s. */ +/* + * Macros for combining bytes into larger integers. _LE and _BE signify little + * and big endian versions respectively. + */ #define UINT16_FROM_BYTES(lsb, msb) ((lsb) | (msb) << 8) #define UINT16_FROM_BYTE_ARRAY_LE(data, lsb_index) \ - UINT16_FROM_BYTES((data)[(lsb_index)], (data)[(lsb_index + 1)]) + UINT16_FROM_BYTES((data)[(lsb_index)], (data)[(lsb_index) + 1]) +#define UINT16_FROM_BYTE_ARRAY_BE(data, msb_index) \ + UINT16_FROM_BYTES((data)[(msb_index) + 1], (data)[(msb_index)]) + +#define UINT32_FROM_BYTES(lsb, byte1, byte2, msb) \ + ((lsb) | (byte1) << 8 | (byte2) << 16 | (msb) << 24) +#define UINT32_FROM_BYTE_ARRAY_LE(data, lsb_index) \ + UINT32_FROM_BYTES((data)[(lsb_index)], (data)[(lsb_index) + 1], \ + (data)[(lsb_index) + 2], (data)[(lsb_index) + 3]) +#define UINT32_FROM_BYTE_ARRAY_BE(data, msb_index) \ + UINT32_FROM_BYTES((data)[(msb_index) + 3], (data)[(msb_index) + 2], \ + (data)[(msb_index) + 1], (data)[(msb_index)]) /* There isn't really a better place for this */ #define C_TO_K(temp_c) ((temp_c) + 273) |