From e34fca3e01d75552ad8d712879c3ccd6a6168584 Mon Sep 17 00:00:00 2001 From: Evan Green Date: Mon, 23 Sep 2019 11:02:41 -0700 Subject: builtin: Introduce and use inttypes.h In order to pass the right printf format specifiers for certain types that are compiled both in 32-bit EC and 64-bit host environments, standard macros PRIx64 and PRId64 must be introduced. These specify the correct printf format specifier in the given compilation environment for printing a 64-bit value. On the host, inttypes.h already exists. Add an inttypes.h for the EC codebase so that these macros can be used where they're needed. BUG=chromium:984041 TEST=make -j buildall BRANCH=none Change-Id: I76e3bdc88aef7da6e5234d5b86b595f7138ea9a1 Signed-off-by: Evan Green Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/1819642 Reviewed-by: caveh jalali Reviewed-by: Jack Rosenthal --- builtin/inttypes.h | 12 ++++++++++++ common/charge_state_v2.c | 2 +- common/keyboard_8042.c | 4 ++-- common/usb_pd_protocol.c | 3 ++- include/common.h | 1 + include/host_command.h | 6 ++++-- test/entropy.c | 2 +- test/utils.c | 12 ++++++------ 8 files changed, 29 insertions(+), 13 deletions(-) create mode 100644 builtin/inttypes.h diff --git a/builtin/inttypes.h b/builtin/inttypes.h new file mode 100644 index 0000000000..c442fbe499 --- /dev/null +++ b/builtin/inttypes.h @@ -0,0 +1,12 @@ +/* Copyright 2019 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#ifndef __CROS_EC_INTTYPES_H__ +#define __CROS_EC_INTTYPES_H__ + +#define PRIx64 "llx" +#define PRId64 "lld" + +#endif /* __CROS_EC_INTTYPES_H__ */ diff --git a/common/charge_state_v2.c b/common/charge_state_v2.c index 11d11d1aa7..b02b6ca819 100644 --- a/common/charge_state_v2.c +++ b/common/charge_state_v2.c @@ -172,7 +172,7 @@ static void problem(enum problem_type p, int v) if (last_prob_val[p] != v) { t_now = get_time(); t_diff.val = t_now.val - last_prob_time[p].val; - CPRINTS("charge problem: %s, 0x%x -> 0x%x after %.6lds", + CPRINTS("charge problem: %s, 0x%x -> 0x%x after %.6" PRId64 "s", prob_text[p], last_prob_val[p], v, t_diff.val); last_prob_val[p] = v; last_prob_time[p] = t_now; diff --git a/common/keyboard_8042.c b/common/keyboard_8042.c index 596c66d317..69669a740a 100644 --- a/common/keyboard_8042.c +++ b/common/keyboard_8042.c @@ -916,8 +916,8 @@ static int command_typematic(int argc, char **argv) ccprintf("From host: 0x%02x\n", typematic_value_from_host); ccprintf("First delay: %3d ms\n", typematic_first_delay / 1000); ccprintf("Inter delay: %3d ms\n", typematic_inter_delay / 1000); - ccprintf("Now: %.6ld\n", get_time().val); - ccprintf("Deadline: %.6ld\n", typematic_deadline.val); + ccprintf("Now: %.6" PRId64 "\n", get_time().val); + ccprintf("Deadline: %.6" PRId64 "\n", typematic_deadline.val); ccputs("Repeat scan code: {"); for (i = 0; i < typematic_len; ++i) diff --git a/common/usb_pd_protocol.c b/common/usb_pd_protocol.c index 9885bc6540..f554630870 100644 --- a/common/usb_pd_protocol.c +++ b/common/usb_pd_protocol.c @@ -2898,7 +2898,8 @@ void pd_task(void *u) if (tcpm_get_chip_info(port, 0, &info) == EC_SUCCESS) { - CPRINTS("TCPC p%d VID:0x%x PID:0x%x DID:0x%x FWV:0x%lx", + CPRINTS("TCPC p%d VID:0x%x PID:0x%x DID:0x%x " + "FWV:0x%" PRIx64, port, info->vendor_id, info->product_id, info->device_id, info->fw_version_number); } diff --git a/include/common.h b/include/common.h index 7bf4b39215..ee95bf5f15 100644 --- a/include/common.h +++ b/include/common.h @@ -9,6 +9,7 @@ #define __CROS_EC_COMMON_H #include +#include #include "compile_time_macros.h" /* diff --git a/include/host_command.h b/include/host_command.h index 2be94cdb83..10d7fc37ea 100644 --- a/include/host_command.h +++ b/include/host_command.h @@ -125,8 +125,10 @@ struct host_command { #ifdef CONFIG_HOST_EVENT64 typedef uint64_t host_event_t; -#define HOST_EVENT_CPRINTS(str, e) CPRINTS("%s 0x%016lx", str, e) -#define HOST_EVENT_CCPRINTF(str, e) ccprintf("%s 0x%016lx\n", str, e) +#define HOST_EVENT_CPRINTS(str, e) CPRINTS("%s 0x%016" PRIx64, str, e) +#define HOST_EVENT_CCPRINTF(str, e) \ + ccprintf("%s 0x%016" PRIx64 "\n", str, e) + #else typedef uint32_t host_event_t; #define HOST_EVENT_CPRINTS(str, e) CPRINTS("%s 0x%08x", str, e) diff --git a/test/entropy.c b/test/entropy.c index 7498d4edf9..a19a8eb33f 100644 --- a/test/entropy.c +++ b/test/entropy.c @@ -51,7 +51,7 @@ void run_test(void) } t1 = get_time(); if (i == 0) - ccprintf("Got %d bytes in %ld us\n", + ccprintf("Got %zd bytes in %" PRId64 " us\n", sizeof(buffer), t1.val - t0.val); for (j = 0; j < sizeof(buffer); j++) diff --git a/test/utils.c b/test/utils.c index cd05aa9f3e..201f607f0c 100644 --- a/test/utils.c +++ b/test/utils.c @@ -35,13 +35,13 @@ static int test_memmove(void) memmove(buf + 101, buf, len); /* unaligned */ t1 = get_time(); TEST_ASSERT_ARRAY_EQ(buf + 101, buf, len); - ccprintf(" (speed gain: %d ->", t1.val-t0.val); + ccprintf(" (speed gain: %" PRId64 " ->", t1.val-t0.val); t2 = get_time(); for (i = 0; i < iteration; ++i) memmove(buf + 100, buf, len); /* aligned */ t3 = get_time(); - ccprintf(" %d us) ", t3.val-t2.val); + ccprintf(" %" PRId64 " us) ", t3.val-t2.val); TEST_ASSERT_ARRAY_EQ(buf + 100, buf, len); /* Expected about 4x speed gain. Use 3x because it fluctuates */ @@ -86,13 +86,13 @@ static int test_memcpy(void) memcpy(buf + dest_offset + 1, buf, len); /* unaligned */ t1 = get_time(); TEST_ASSERT_ARRAY_EQ(buf + dest_offset + 1, buf, len); - ccprintf(" (speed gain: %d ->", t1.val-t0.val); + ccprintf(" (speed gain: %" PRId64 " ->", t1.val-t0.val); t2 = get_time(); for (i = 0; i < iteration; ++i) memcpy(buf + dest_offset, buf, len); /* aligned */ t3 = get_time(); - ccprintf(" %d us) ", t3.val-t2.val); + ccprintf(" %" PRId64 " us) ", t3.val-t2.val); TEST_ASSERT_ARRAY_EQ(buf + dest_offset, buf, len); /* Expected about 4x speed gain. Use 3x because it fluctuates */ @@ -148,14 +148,14 @@ static int test_memset(void) dumb_memset(buf, 1, len); t1 = get_time(); TEST_ASSERT_MEMSET(buf, (char)1, len); - ccprintf(" (speed gain: %d ->", t1.val-t0.val); + ccprintf(" (speed gain: %" PRId64 " ->", t1.val-t0.val); t2 = get_time(); for (i = 0; i < iteration; ++i) memset(buf, 1, len); t3 = get_time(); TEST_ASSERT_MEMSET(buf, (char)1, len); - ccprintf(" %d us) ", t3.val-t2.val); + ccprintf(" %" PRId64 " us) ", t3.val-t2.val); /* Expected about 4x speed gain. Use 3x because it fluctuates */ #ifndef EMU_BUILD -- cgit v1.2.1