summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBill Richardson <wfrichar@chromium.org>2013-04-26 20:02:07 -0700
committerChromeBot <chrome-bot@google.com>2013-04-29 11:05:08 -0700
commitd9bddaa0722d2b95c9b72c9c3a24c33053381311 (patch)
tree9db87c5dbf220fed30d9caca8a10a0aa2a61039c
parent7cebaa0a017d734ae9dda19a39fd867463f785c6 (diff)
downloadchrome-ec-d9bddaa0722d2b95c9b72c9c3a24c33053381311.tar.gz
Improved the BUILD_ASSERT macro to work outside of functions.
This will let us check the size of static array initializers. Also moved this macro definition and ARRAY_SIZE into a new "tricks.h" header, so that userspace utils can use it too. BUG=none BRANCH=none TEST=manual Built everything, tested on Link. Tried various assertions. Change-Id: I612891108ea37dbca3572e0f25ab54a7bc0ed860 Signed-off-by: Bill Richardson <wfrichar@chromium.org> Reviewed-on: https://gerrit.chromium.org/gerrit/49417 Reviewed-by: Randall Spangler <rspangler@chromium.org>
-rw-r--r--chip/stm32/clock-stm32f100.c2
-rw-r--r--chip/stm32/clock-stm32l15x.c3
-rw-r--r--core/cortex-m/task.c8
-rw-r--r--include/compile_time_macros.h26
-rw-r--r--include/util.h13
-rw-r--r--util/ectool.c4
-rw-r--r--util/lbplay.c5
7 files changed, 36 insertions, 25 deletions
diff --git a/chip/stm32/clock-stm32f100.c b/chip/stm32/clock-stm32f100.c
index ae919ca1b1..0dd63f456b 100644
--- a/chip/stm32/clock-stm32f100.c
+++ b/chip/stm32/clock-stm32f100.c
@@ -141,6 +141,7 @@ DECLARE_IRQ(STM32_IRQ_RTC_ALARM, __rtc_alarm_irq, 1);
#else
#error "Need board-specific clock settings"
#endif
+BUILD_ASSERT(CPU_CLOCK == DESIRED_CPU_CLOCK);
static void config_hispeed_clock(void)
{
@@ -153,7 +154,6 @@ static void config_hispeed_clock(void)
;
}
- BUILD_ASSERT(CPU_CLOCK == DESIRED_CPU_CLOCK);
STM32_RCC_CFGR = RCC_CFGR;
/* Enable the PLL */
STM32_RCC_CR |= 1 << 24;
diff --git a/chip/stm32/clock-stm32l15x.c b/chip/stm32/clock-stm32l15x.c
index 687919f73b..057888f68f 100644
--- a/chip/stm32/clock-stm32l15x.c
+++ b/chip/stm32/clock-stm32l15x.c
@@ -10,6 +10,8 @@
#include "registers.h"
#include "util.h"
+BUILD_ASSERT(CPU_CLOCK == 16000000);
+
void enable_sleep(uint32_t mask)
{
/* low power mode not implemented */
@@ -60,7 +62,6 @@ void clock_init(void)
* stays on HSI, no prescaler, PLLSRC = HSI, PLLMUL = x3, PLLDIV = /3,
* no MCO => PLLVCO = 48 MHz and PLLCLK = 16 Mhz
*/
- BUILD_ASSERT(CPU_CLOCK == 16000000);
STM32_RCC_CFGR = 0x00800001;
/* Enable the PLL */
STM32_RCC_CR |= 1 << 24;
diff --git a/core/cortex-m/task.c b/core/cortex-m/task.c
index 47bbbee1e9..bd5736a3ae 100644
--- a/core/cortex-m/task.c
+++ b/core/cortex-m/task.c
@@ -109,6 +109,10 @@ static const struct {
/* Contexts for all tasks */
static task_ tasks[TASK_ID_COUNT];
+/* Sanity checks about static task invariants */
+BUILD_ASSERT(TASK_ID_COUNT <= sizeof(unsigned) * 8);
+BUILD_ASSERT(TASK_ID_COUNT < (1 << (sizeof(task_id_t) * 8)));
+
/* Stacks for all tasks */
#define TASK(n, r, d, s) + s
@@ -613,10 +617,6 @@ void task_pre_init(void)
((task_ *)scratchpad)->stack = (uint32_t *)scratchpad;
*(uint32_t *)scratchpad = STACK_UNUSED_VALUE;
- /* Sanity checks about static task invariants */
- BUILD_ASSERT(TASK_ID_COUNT <= sizeof(unsigned) * 8);
- BUILD_ASSERT(TASK_ID_COUNT < (1 << (sizeof(task_id_t) * 8)));
-
/* Initialize IRQs */
__nvic_init_irqs();
}
diff --git a/include/compile_time_macros.h b/include/compile_time_macros.h
new file mode 100644
index 0000000000..9db583a7f2
--- /dev/null
+++ b/include/compile_time_macros.h
@@ -0,0 +1,26 @@
+/* Copyright (c) 2013 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.
+ */
+
+/* Handy clever tricks */
+
+#ifndef __CROS_EC_TRICKS_H
+#define __CROS_EC_TRICKS_H
+
+/* Test an important condition at compile time, not run time */
+#define _BA1_(cond, line) \
+ extern int __build_assertion_ ## line[1 - 2*!(cond)] \
+ __attribute__ ((unused))
+#define _BA0_(c, x) _BA1_(c, x)
+#define BUILD_ASSERT(cond) _BA0_(cond, __LINE__)
+
+/* Number of elements in an array */
+#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
+
+/* Just in case - http://gcc.gnu.org/onlinedocs/gcc/Offsetof.html */
+#ifndef offsetof
+#define offsetof(type, member) __builtin_offsetof(type, member)
+#endif
+
+#endif /* __CROS_EC_TRICKS_H */
diff --git a/include/util.h b/include/util.h
index c953e468f8..9db42c2f34 100644
--- a/include/util.h
+++ b/include/util.h
@@ -9,16 +9,11 @@
#define __CROS_EC_UTIL_H
#include "common.h"
+#include "compile_time_macros.h"
#include "config.h"
#include "panic.h"
/**
- * Trigger a compilation failure if the condition
- * is not verified at build time.
- */
-#define BUILD_ASSERT(cond) ((void)sizeof(char[1 - 2*!(cond)]))
-
-/**
* Trigger a debug exception if the condition
* is not verified at runtime.
*/
@@ -41,12 +36,6 @@
/* Standard macros / definitions */
-#ifndef ARRAY_SIZE
-#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
-#endif
-#ifndef OFFSET_OF
-#define OFFSET_OF(struc, field) ((uint32_t)&(((const struc * const)0)->field))
-#endif
#ifndef MAX
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#endif
diff --git a/util/ectool.c b/util/ectool.c
index b1115aefcd..0315b6dbb1 100644
--- a/util/ectool.c
+++ b/util/ectool.c
@@ -14,13 +14,11 @@
#include "battery.h"
#include "comm-host.h"
+#include "compile_time_macros.h"
#include "ectool.h"
#include "lightbar.h"
#include "lock/gec_lock.h"
-/* Handy tricks */
-#define BUILD_ASSERT(cond) ((void)sizeof(char[1 - 2*!(cond)]))
-#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
/* Don't use a macro where an inline will do... */
static inline int MIN(int a, int b) { return a < b ? a : b; }
diff --git a/util/lbplay.c b/util/lbplay.c
index bcf90148e7..9cb853aa75 100644
--- a/util/lbplay.c
+++ b/util/lbplay.c
@@ -11,13 +11,10 @@
#include <unistd.h>
#include "comm-host.h"
+#include "compile_time_macros.h"
#include "lightbar.h"
#include "lock/gec_lock.h"
-/* Handy tricks */
-#define BUILD_ASSERT(cond) ((void)sizeof(char[1 - 2*!(cond)]))
-#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
-
#define LB_SIZES(SUBCMD) { \
sizeof(((struct ec_params_lightbar *)0)->SUBCMD) \
+ sizeof(((struct ec_params_lightbar *)0)->cmd), \