diff options
-rw-r--r-- | chip/stm32/clock-stm32f100.c | 2 | ||||
-rw-r--r-- | chip/stm32/clock-stm32l15x.c | 3 | ||||
-rw-r--r-- | core/cortex-m/task.c | 8 | ||||
-rw-r--r-- | include/compile_time_macros.h | 26 | ||||
-rw-r--r-- | include/util.h | 13 | ||||
-rw-r--r-- | util/ectool.c | 4 | ||||
-rw-r--r-- | util/lbplay.c | 5 |
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), \ |