From ff7c3ee0429fea758a9bde8ce86ea5b19f0512ad Mon Sep 17 00:00:00 2001 From: Simon Duquennoy Date: Fri, 20 Apr 2018 02:44:58 -0700 Subject: [PATCH 1/2] Make rtimer 32-bit by default --- arch/cpu/arm/arm-def.h | 2 -- arch/cpu/msp430/msp430-def.h | 50 ++++++++++++++------------ arch/cpu/msp430/rtimer-arch.h | 4 --- arch/platform/jn516x/dev/rtimer-arch.h | 14 +++----- arch/platform/jn516x/jn516x-def.h | 4 --- os/sys/rtimer.h | 5 +-- 6 files changed, 35 insertions(+), 44 deletions(-) diff --git a/arch/cpu/arm/arm-def.h b/arch/cpu/arm/arm-def.h index 92b19048d..c83cfd3fc 100644 --- a/arch/cpu/arm/arm-def.h +++ b/arch/cpu/arm/arm-def.h @@ -57,8 +57,6 @@ typedef uint32_t clock_time_t; typedef uint32_t uip_stats_t; -typedef uint32_t rtimer_clock_t; -#define RTIMER_CLOCK_DIFF(a, b) ((int32_t)((a) - (b))) /** @} */ /* diff --git a/arch/cpu/msp430/msp430-def.h b/arch/cpu/msp430/msp430-def.h index 93e575702..2801ec8c3 100644 --- a/arch/cpu/msp430/msp430-def.h +++ b/arch/cpu/msp430/msp430-def.h @@ -1,30 +1,30 @@ /* * Copyright (c) 2007, Swedish Institute of Computer Science - * All rights reserved. + * All rights reserved. * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the Institute nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. * - * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. */ #ifndef MSP430_DEF_H_ @@ -79,6 +79,10 @@ typedef long off_t; /* Our clock resolution, this is the same as Unix HZ. */ #define CLOCK_CONF_SECOND 128UL +/* Use 16-bit rtimer (default in Contiki-NG is 32) */ +typedef unsigned short rtimer_clock_t; +#define RTIMER_CLOCK_DIFF(a,b) ((signed short)((a)-(b))) + typedef int spl_t; spl_t splhigh_(void); diff --git a/arch/cpu/msp430/rtimer-arch.h b/arch/cpu/msp430/rtimer-arch.h index 6063292b1..9fd5b46ba 100644 --- a/arch/cpu/msp430/rtimer-arch.h +++ b/arch/cpu/msp430/rtimer-arch.h @@ -42,11 +42,7 @@ #include "sys/rtimer.h" -#ifdef RTIMER_CONF_SECOND -#define RTIMER_ARCH_SECOND RTIMER_CONF_SECOND -#else #define RTIMER_ARCH_SECOND (4096U*8) -#endif /* Do the math in 32bits to save precision. * Round to nearest integer rather than truncate. */ diff --git a/arch/platform/jn516x/dev/rtimer-arch.h b/arch/platform/jn516x/dev/rtimer-arch.h index 3ffafeeac..63212566b 100644 --- a/arch/platform/jn516x/dev/rtimer-arch.h +++ b/arch/platform/jn516x/dev/rtimer-arch.h @@ -43,19 +43,15 @@ #include "sys/rtimer.h" -#ifdef RTIMER_CONF_SECOND -# define RTIMER_ARCH_SECOND RTIMER_CONF_SECOND -#else #if RTIMER_USE_32KHZ -# if JN516X_EXTERNAL_CRYSTAL_OSCILLATOR -# define RTIMER_ARCH_SECOND 32768 -# else -# define RTIMER_ARCH_SECOND 32000 +#if JN516X_EXTERNAL_CRYSTAL_OSCILLATOR +#define RTIMER_ARCH_SECOND 32768 +#else +#define RTIMER_ARCH_SECOND 32000 #endif #else /* 32MHz CPU clock => 16MHz timer */ -# define RTIMER_ARCH_SECOND (F_CPU / 2) -#endif +#define RTIMER_ARCH_SECOND (F_CPU / 2) #endif #if RTIMER_USE_32KHZ diff --git a/arch/platform/jn516x/jn516x-def.h b/arch/platform/jn516x/jn516x-def.h index 0c76a4332..a4ee45c75 100644 --- a/arch/platform/jn516x/jn516x-def.h +++ b/arch/platform/jn516x/jn516x-def.h @@ -79,10 +79,6 @@ #define JN516X_EXTERNAL_CRYSTAL_OSCILLATOR (RTIMER_USE_32KHZ || JN516X_SLEEP_ENABLED) #endif /* JN516X_EXTERNAL_CRYSTAL_OSCILLATOR */ -/* Core rtimer.h defaults to 16 bit timer unless RTIMER_CLOCK_DIFF is defined */ -typedef uint32_t rtimer_clock_t; -#define RTIMER_CLOCK_DIFF(a, b) ((int32_t)((a) - (b))) - /* 8ms timer tick */ #define CLOCK_CONF_SECOND 125 diff --git a/os/sys/rtimer.h b/os/sys/rtimer.h index 95d11c33d..6cccaab49 100644 --- a/os/sys/rtimer.h +++ b/os/sys/rtimer.h @@ -56,8 +56,9 @@ #include "contiki.h" #ifndef RTIMER_CLOCK_DIFF -typedef unsigned short rtimer_clock_t; -#define RTIMER_CLOCK_DIFF(a,b) ((signed short)((a)-(b))) +/* By default: 32-bit rtimer. Set RTIMER_CLOCK_DIFF for custom rtimer width */ +typedef uint32_t rtimer_clock_t; +#define RTIMER_CLOCK_DIFF(a, b) ((int32_t)((a) - (b))) #endif /* RTIMER_CLOCK_DIFF */ #define RTIMER_CLOCK_LT(a, b) (RTIMER_CLOCK_DIFF((a),(b)) < 0) From f339747218f47315eec76f6d698ada87e8fe4c06 Mon Sep 17 00:00:00 2001 From: Simon Duquennoy Date: Fri, 20 Apr 2018 05:06:40 -0700 Subject: [PATCH 2/2] rtimer: homogenize clock size configuration --- arch/cpu/msp430/msp430-def.h | 3 +-- arch/platform/cooja/contiki-conf.h | 5 +++-- os/sys/rtimer.h | 32 +++++++++++++++++++++++++----- 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/arch/cpu/msp430/msp430-def.h b/arch/cpu/msp430/msp430-def.h index 2801ec8c3..28b482e9a 100644 --- a/arch/cpu/msp430/msp430-def.h +++ b/arch/cpu/msp430/msp430-def.h @@ -80,8 +80,7 @@ typedef long off_t; #define CLOCK_CONF_SECOND 128UL /* Use 16-bit rtimer (default in Contiki-NG is 32) */ -typedef unsigned short rtimer_clock_t; -#define RTIMER_CLOCK_DIFF(a,b) ((signed short)((a)-(b))) +#define RTIMER_CONF_CLOCK_SIZE 2 typedef int spl_t; spl_t splhigh_(void); diff --git a/arch/platform/cooja/contiki-conf.h b/arch/platform/cooja/contiki-conf.h index 048497462..9fb0a990a 100644 --- a/arch/platform/cooja/contiki-conf.h +++ b/arch/platform/cooja/contiki-conf.h @@ -113,8 +113,9 @@ typedef unsigned short uip_stats_t; #define CLOCK_CONF_SECOND 1000L typedef unsigned long clock_time_t; -typedef uint64_t rtimer_clock_t; -#define RTIMER_CLOCK_DIFF(a,b) ((int64_t)((a)-(b))) + +/* Use 64-bit rtimer (default in Contiki-NG is 32) */ +#define RTIMER_CONF_CLOCK_SIZE 8 #define RADIO_DELAY_BEFORE_TX 0 #define RADIO_DELAY_BEFORE_RX 0 diff --git a/os/sys/rtimer.h b/os/sys/rtimer.h index 6cccaab49..44ad92a2f 100644 --- a/os/sys/rtimer.h +++ b/os/sys/rtimer.h @@ -55,12 +55,34 @@ #include "contiki.h" -#ifndef RTIMER_CLOCK_DIFF -/* By default: 32-bit rtimer. Set RTIMER_CLOCK_DIFF for custom rtimer width */ -typedef uint32_t rtimer_clock_t; -#define RTIMER_CLOCK_DIFF(a, b) ((int32_t)((a) - (b))) -#endif /* RTIMER_CLOCK_DIFF */ +/** \brief The rtimer size (in bytes) */ +#ifdef RTIMER_CONF_CLOCK_SIZE +#define RTIMER_CLOCK_SIZE RTIMER_CONF_CLOCK_SIZE +#else /* RTIMER_CONF_CLOCK_SIZE */ +/* Default: 32bit rtimer*/ +#define RTIMER_CLOCK_SIZE 4 +#endif /* RTIMER_CONF_CLOCK_SIZE */ +#if RTIMER_CLOCK_SIZE == 2 +/* 16-bit rtimer */ +typedef uint16_t rtimer_clock_t; +#define RTIMER_CLOCK_DIFF(a,b) ((int16_t)((a)-(b))) + +#elif RTIMER_CLOCK_SIZE == 4 +/* 32-bit rtimer */ +typedef uint32_t rtimer_clock_t; +#define RTIMER_CLOCK_DIFF(a, b) ((int32_t)((a) - (b))) + +#elif RTIMER_CLOCK_SIZE == 8 +/* 64-bit rtimer */ +typedef uint64_t rtimer_clock_t; +#define RTIMER_CLOCK_DIFF(a,b) ((int64_t)((a)-(b))) + +#else +#error Unsupported rtimer size (check RTIMER_CLOCK_SIZE) +#endif + +#define RTIMER_CLOCK_MAX ((rtimer_clock_t)-1) #define RTIMER_CLOCK_LT(a, b) (RTIMER_CLOCK_DIFF((a),(b)) < 0) #include "rtimer-arch.h"