From 934856b4fdc6bece3b259bfc1184b4bc9f3be18d Mon Sep 17 00:00:00 2001 From: George Oikonomou Date: Sat, 4 Nov 2017 20:47:23 +0000 Subject: [PATCH 01/44] Introduce platform-independent HAL for buttons --- os/dev/button-hal.c | 192 ++++++++++++++++++++++++++++++++++ os/dev/button-hal.h | 248 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 440 insertions(+) create mode 100644 os/dev/button-hal.c create mode 100644 os/dev/button-hal.h diff --git a/os/dev/button-hal.c b/os/dev/button-hal.c new file mode 100644 index 000000000..8109739d6 --- /dev/null +++ b/os/dev/button-hal.c @@ -0,0 +1,192 @@ +/* + * Copyright (c) 2017, George Oikonomou - http://www.spd.gr + * 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 copyright holder 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 COPYRIGHT HOLDERS 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 + * COPYRIGHT HOLDER 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. + */ +/*---------------------------------------------------------------------------*/ +/** + * \addtogroup button_hal + * @{ + * + * \file + * Platform-independent button driver. + */ +/*---------------------------------------------------------------------------*/ +#include "contiki.h" +#include "sys/process.h" +#include "sys/ctimer.h" +#include "dev/gpio-hal.h" +#include "dev/button-hal.h" + +#include +#include +#include +/*---------------------------------------------------------------------------*/ +process_event_t button_hal_press_event; +process_event_t button_hal_release_event; +process_event_t button_hal_periodic_event; +/*---------------------------------------------------------------------------*/ +extern button_hal_button_t *button_hal_buttons[]; +/*---------------------------------------------------------------------------*/ +/* Common handler for all handler events, and register it with the GPIO HAL */ +static gpio_hal_event_handler_t button_event_handler; +/*---------------------------------------------------------------------------*/ +static uint8_t +get_state(button_hal_button_t *button) +{ + uint8_t pin_state = gpio_hal_arch_read_pin(button->pin); + + if((pin_state == 0 && button->negative_logic == true) || + (pin_state == 1 && button->negative_logic == false)) { + return BUTTON_HAL_STATE_PRESSED; + } + + return BUTTON_HAL_STATE_RELEASED; +} +/*---------------------------------------------------------------------------*/ +static void +duration_exceeded_callback(void *btn) +{ + button_hal_button_t *button = (button_hal_button_t *)btn; + + button->press_duration_seconds++; + ctimer_set(&button->duration_ctimer, CLOCK_SECOND, + duration_exceeded_callback, button); + process_post(PROCESS_BROADCAST, button_hal_periodic_event, button); +} +/*---------------------------------------------------------------------------*/ +static void +debounce_handler(void *btn) +{ + button_hal_button_t *button; + int expired; + uint8_t button_state; + + button = (button_hal_button_t *)btn; + + /* + * A new debounce may have been triggered after expiration of the previous + * one but before we got called. + */ + if(!ctimer_expired(&button->debounce_ctimer)) { + return; + } + + expired = ctimer_expired(&button->duration_ctimer); + + button_state = get_state(button); + + /* + * A debounce timer expired. Inspect the button's state. If the button's + * state is the same as it was before, then we ignore this as noise. + */ + if(button_state == BUTTON_HAL_STATE_PRESSED && expired) { + /* + * Button is pressed and no tick counter running. Treat as new press. + * Include the debounce duration in the first periodic, so that the + * callback will happen 1 second after the button press, not 1 second + * after the end of the debounce. Notify process about the press event. + */ + button->press_duration_seconds = 0; + ctimer_set(&button->duration_ctimer, + CLOCK_SECOND - BUTTON_HAL_DEBOUNCE_DURATION, + duration_exceeded_callback, button); + process_post(PROCESS_BROADCAST, button_hal_press_event, button); + } else if(button_state == BUTTON_HAL_STATE_RELEASED && expired == 0) { + /* + * Button is released and there is a duration_ctimer running. Treat this + * as a new release and notify processes. + */ + ctimer_stop(&button->duration_ctimer); + process_post(PROCESS_BROADCAST, button_hal_release_event, button); + } +} +/*---------------------------------------------------------------------------*/ +static void +press_release_handler(gpio_hal_pin_mask_t pin_mask) +{ + button_hal_button_t **button; + + for(button = button_hal_buttons; *button != NULL; button++) { + if(gpio_hal_pin_to_mask((*button)->pin) & pin_mask) { + /* Ignore all button presses/releases during its debounce */ + if(ctimer_expired(&(*button)->debounce_ctimer)) { + /* + * Here we merely set a debounce timer. At the end of the debounce we + * will inspect the button's state and we will take action only if it + * has changed. + * + * This is to prevent erroneous edge detections due to interference. + */ + ctimer_set(&(*button)->debounce_ctimer, BUTTON_HAL_DEBOUNCE_DURATION, + debounce_handler, *button); + } + } + } +} +/*---------------------------------------------------------------------------*/ +button_hal_button_t * +button_hal_get_by_id(uint8_t unique_id) +{ + button_hal_button_t **button; + + for(button = button_hal_buttons; *button != NULL; button++) { + if((*button)->unique_id == unique_id) { + return (*button); + } + } + + return NULL; +} +/*---------------------------------------------------------------------------*/ +void +button_hal_init() +{ + button_hal_button_t **button; + gpio_hal_pin_cfg_t cfg; + + button_hal_press_event = process_alloc_event(); + button_hal_release_event = process_alloc_event(); + button_hal_periodic_event = process_alloc_event(); + + button_event_handler.pin_mask = 0; + button_event_handler.handler = press_release_handler; + + for(button = button_hal_buttons; *button != NULL; button++) { + cfg = GPIO_HAL_PIN_CFG_EDGE_BOTH | GPIO_HAL_PIN_CFG_INT_ENABLE | + (*button)->pull; + gpio_hal_arch_pin_set_input((*button)->pin); + gpio_hal_arch_pin_cfg_set((*button)->pin, cfg); + gpio_hal_arch_interrupt_enable((*button)->pin); + button_event_handler.pin_mask |= gpio_hal_pin_to_mask((*button)->pin); + } + + gpio_hal_register_handler(&button_event_handler); +} +/*---------------------------------------------------------------------------*/ +/** @} */ diff --git a/os/dev/button-hal.h b/os/dev/button-hal.h new file mode 100644 index 000000000..7d38994f7 --- /dev/null +++ b/os/dev/button-hal.h @@ -0,0 +1,248 @@ +/* + * Copyright (c) 2017, George Oikonomou - http://www.spd.gr + * 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 copyright holder 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 COPYRIGHT HOLDERS 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 + * COPYRIGHT HOLDER 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. + */ +/*---------------------------------------------------------------------------*/ +/** + * \addtogroup dev + * @{ + */ +/*---------------------------------------------------------------------------*/ +/** + * \defgroup button_hal Button HAL + * + * Hardware abstraction layer for user buttons. + * + * This HAL enables an abstraction of general-purpose / user buttons or + * similar peripherals (e.g. a reed relay can also be abstracted through this + * HAL). The HAL handles software debounce timers internally, therefore the + * platform-specific button driver does not need to worry about debouncing. + * + * The platform developer needs to define a variable of type + * \c button_hal_button_t for each user button. Within this variable, the + * developer needs to specify the GPIO pin where the button is attached, + * whether the button uses negative logic, and whether the GPIO pin should + * be configured with internal pullup/down. The developer also needs to provide + * a unique index for each button, as well as a description. + * + * With those in place, the HAL will generate the following process events: + * + * - button_hal_press_event: Upon press of the button + * - button_hal_release_event: Upon release of the button + * - button_hal_periodic_event: Generated every second that the user button is + * kept pressed. + * + * With those events in place, an application can perform an action: + * + * - Immediately after the button gets pressed. + * - After the button has been pressed for N seconds. + * - Immediately upon release of the button. This action can vary depending + * on how long the button had been pressed for. + * + * A platform with user buttons can either implement this API (recommended) or + * the older button_sensor API. Some examples will not work if this API is not + * implemented. + * + * This API requires the platform to first support the GPIO HAL API. + * @{ + * + * \file + * Header file for the button HAL + */ +/*---------------------------------------------------------------------------*/ +#ifndef BUTTON_HAL_H_ +#define BUTTON_HAL_H_ +/*---------------------------------------------------------------------------*/ +#include "contiki.h" +#include "dev/gpio-hal.h" +#include "sys/clock.h" +#include "sys/ctimer.h" + +#include +#include +#include +/*---------------------------------------------------------------------------*/ +/** + * \brief Controls the software debounce timer duration. + * + * The platform can provide a more suitable value. This value will apply to + * all buttons. + */ +#ifdef BUTTON_HAL_CONF_DEBOUNCE_DURATION +#define BUTTON_HAL_DEBOUNCE_DURATION BUTTON_HAL_CONF_DEBOUNCE_DURATION +#else +#define BUTTON_HAL_DEBOUNCE_DURATION (CLOCK_SECOND >> 6) +#endif +/*---------------------------------------------------------------------------*/ +/** + * \brief Controls whether buttons will have human-readable names + * + * Define this to zero to save code space + */ +#if BUTTON_HAL_CONF_WITH_DESCRIPTION +#define BUTTON_HAL_WITH_DESCRIPTION BUTTON_HAL_CONF_WITH_DESCRIPTION +#else +#define BUTTON_HAL_WITH_DESCRIPTION 1 +#endif +/*---------------------------------------------------------------------------*/ +#define BUTTON_HAL_STATE_RELEASED 0 +#define BUTTON_HAL_STATE_PRESSED 1 +/*---------------------------------------------------------------------------*/ +/** + * Optional button IDs + */ +#define BUTTON_HAL_ID_BUTTON_ZERO 0x00 +#define BUTTON_HAL_ID_BUTTON_ONE 0x01 +#define BUTTON_HAL_ID_BUTTON_TWO 0x02 +#define BUTTON_HAL_ID_BUTTON_THREE 0x03 +#define BUTTON_HAL_ID_BUTTON_FOUR 0x04 +#define BUTTON_HAL_ID_BUTTON_FIVE 0x05 + +#define BUTTON_HAL_ID_USER_BUTTON BUTTON_HAL_ID_BUTTON_ZERO +/*---------------------------------------------------------------------------*/ +/** + * \brief A logical representation of a user button + */ +typedef struct button_hal_button_s button_hal_button_t; + +struct button_hal_button_s { + /** Used by the s/w debounce functionality */ + struct ctimer debounce_ctimer; + + /** A callback timer used to count duration of button presses */ + struct ctimer duration_ctimer; + +#if BUTTON_HAL_WITH_DESCRIPTION + /** + * \brief A textual description of the button + * + * This field may only be accessed using the BUTTON_HAL_GET_DESCRIPTION() + * macro. + */ + const char *description; +#endif + + /** True if the button uses negative logic (active: low) */ + const bool negative_logic; + + /** The gpio pin connected to the button */ + const gpio_hal_pin_t pin; + + /** The pin's pull configuration */ + const gpio_hal_pin_cfg_t pull; + + /** A counter of the duration (in seconds) of a button press */ + uint8_t press_duration_seconds; + + /** + * \brief A unique identifier for this button. + * + * The platform code is responsible of setting unique values here. This can + * be used later to determine which button generated an event. Many examples + * assume the existence of a button with ID == BUTTON_HAL_ID_BUTTON_ZERO, + * so it is good idea to use this ID for one of your platform's buttons. + */ + const uint8_t unique_id; +}; +/*---------------------------------------------------------------------------*/ +#if BUTTON_HAL_WITH_DESCRIPTION +/** + * \brief Define a button to be used by the HAL + * \param name The variable name for the button + * \param descr A textual description + * \param p The pin connected to the button + * \param nl True if the button is connected using negative logic + * \param u The button's pull configuration + * \param id A unique numeric identifier + */ +#define BUTTON_HAL_BUTTON(name, descr, p, u, id, nl) \ + static button_hal_button_t name = { \ + .description = descr, \ + .pin = p, \ + .pull = u, \ + .unique_id = id, \ + .negative_logic = nl, \ + } + +/** + * \brief Retrieve the textual description of a button + * \param b A pointer to the button button_hal_button_t + * + * This macro will return the value of the description field for b. If + * BUTTON_HAL_WITH_DESCRIPTION is 0 then this macro will return "" + */ +#define BUTTON_HAL_GET_DESCRIPTION(b) (b)->description +#else +#define BUTTON_HAL_BUTTON(name, descr, p, u, id, nl) \ + static button_hal_button_t name = { \ + .pin = p, \ + .pull = u, \ + .unique_id = id, \ + .negative_logic = nl, \ + } + +#define BUTTON_HAL_GET_DESCRIPTION(b) "" +#endif +/*---------------------------------------------------------------------------*/ +#define BUTTON_HAL_BUTTONS(...) \ + button_hal_button_t *button_hal_buttons[] = {__VA_ARGS__, NULL}; +/*---------------------------------------------------------------------------*/ +/** + * \brief A broadcast event generated when a button gets pressed + */ +extern process_event_t button_hal_press_event; + +/** + * \brief A broadcast event generated when a button gets released + */ +extern process_event_t button_hal_release_event; + +/** + * \brief A broadcast event generated every second while a button is kept pressed + */ +extern process_event_t button_hal_periodic_event; +/*---------------------------------------------------------------------------*/ +/** + * \brief Initialise the button HAL + */ +void button_hal_init(void); + +/** + * \brief Retrieve a button by ID + * \param unique_id The button unique ID to search for + * \return A pointer to the button or NULL if not found + */ +button_hal_button_t *button_hal_get_by_id(uint8_t unique_id); +/*---------------------------------------------------------------------------*/ +#endif /* BUTTON_HAL_H_ */ +/*---------------------------------------------------------------------------*/ +/** + * @} + * @} + */ From 3014e8cf07b535ada9e7f28c3e13c8907f362894 Mon Sep 17 00:00:00 2001 From: George Oikonomou Date: Sun, 5 Nov 2017 17:15:29 +0000 Subject: [PATCH 02/44] Start the sensors process conditionally (Not all boards have sensors) --- arch/platform/srf06-cc26xx/platform.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/arch/platform/srf06-cc26xx/platform.c b/arch/platform/srf06-cc26xx/platform.c index 4d2fca2fb..4c6b9443e 100644 --- a/arch/platform/srf06-cc26xx/platform.c +++ b/arch/platform/srf06-cc26xx/platform.c @@ -69,6 +69,7 @@ #include "button-sensor.h" #include "dev/serial-line.h" #include "net/mac/framer/frame802154.h" +#include "board-peripherals.h" #include "driverlib/driverlib_release.h" @@ -84,6 +85,12 @@ unsigned short node_id = 0; /** \brief Board specific iniatialisation */ void board_init(void); /*---------------------------------------------------------------------------*/ +#ifdef BOARD_CONF_HAS_SENSORS +#define BOARD_HAS_SENSORS BOARD_CONF_HAS_SENSORS +#else +#define BOARD_HAS_SENSORS 1 +#endif +/*---------------------------------------------------------------------------*/ static void fade(leds_mask_t l) { @@ -218,7 +225,10 @@ platform_init_stage_three() LOG_INFO(" Node ID: %d\n", node_id); +#if BOARD_HAS_SENSORS process_start(&sensors_process, NULL); +#endif + fade(LEDS_ORANGE); } /*---------------------------------------------------------------------------*/ From 3f4fef4625b8f223d89cfcc777ecb634f4861045 Mon Sep 17 00:00:00 2001 From: George Oikonomou Date: Sat, 4 Nov 2017 22:59:07 +0000 Subject: [PATCH 03/44] Implement the new button HAL: SmartRF06 + CC13xxEM/CC26xxEM --- .../srf06-cc26xx/srf06/Makefile.srf06 | 2 +- .../{button-sensor.h => board-buttons.c} | 41 +- .../srf06-cc26xx/srf06/button-sensor.c | 498 ------------------ .../srf06-cc26xx/srf06/cc13xx/board.h | 13 + .../srf06-cc26xx/srf06/cc26xx/board.h | 13 + .../srf06-cc26xx/srf06/srf06-sensors.c | 4 +- 6 files changed, 50 insertions(+), 521 deletions(-) rename arch/platform/srf06-cc26xx/srf06/{button-sensor.h => board-buttons.c} (64%) delete mode 100644 arch/platform/srf06-cc26xx/srf06/button-sensor.c diff --git a/arch/platform/srf06-cc26xx/srf06/Makefile.srf06 b/arch/platform/srf06-cc26xx/srf06/Makefile.srf06 index bcfe21bca..dde7e9672 100644 --- a/arch/platform/srf06-cc26xx/srf06/Makefile.srf06 +++ b/arch/platform/srf06-cc26xx/srf06/Makefile.srf06 @@ -2,7 +2,7 @@ CFLAGS += -DBOARD_SMARTRF06EB=1 CONTIKI_TARGET_DIRS += srf06 -BOARD_SOURCEFILES += srf06-sensors.c button-sensor.c board.c +BOARD_SOURCEFILES += srf06-sensors.c board-buttons.c board.c BOARD_SOURCEFILES += als-sensor.c ### Signal that we can be programmed with cc2538-bsl diff --git a/arch/platform/srf06-cc26xx/srf06/button-sensor.h b/arch/platform/srf06-cc26xx/srf06/board-buttons.c similarity index 64% rename from arch/platform/srf06-cc26xx/srf06/button-sensor.h rename to arch/platform/srf06-cc26xx/srf06/board-buttons.c index 1c810c96d..c0db8daaa 100644 --- a/arch/platform/srf06-cc26xx/srf06/button-sensor.h +++ b/arch/platform/srf06-cc26xx/srf06/board-buttons.c @@ -1,10 +1,11 @@ /* - * Copyright (c) 2014, Texas Instruments Incorporated - http://www.ti.com/ + * Copyright (c) 2017, George Oikonomou - http://www.spd.gr * 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 @@ -33,28 +34,30 @@ * @{ * * \file - * Header file for the SmartRF06EB + CC13xx/CC26xxEM Button Driver + * Defines SmarfRF06 + CC13xxEM/CC26xxEM buttons for use with the button HAL */ /*---------------------------------------------------------------------------*/ -#ifndef BUTTON_SENSOR_H_ -#define BUTTON_SENSOR_H_ -/*---------------------------------------------------------------------------*/ -#include "lib/sensors.h" -/*---------------------------------------------------------------------------*/ -#define BUTTON_SENSOR "Button" -/*---------------------------------------------------------------------------*/ -#define BUTTON_SENSOR_VALUE_STATE 0 -#define BUTTON_SENSOR_VALUE_DURATION 1 +#include "contiki.h" +#include "dev/button-hal.h" -#define BUTTON_SENSOR_VALUE_RELEASED 0 -#define BUTTON_SENSOR_VALUE_PRESSED 1 +#include "ti-lib.h" /*---------------------------------------------------------------------------*/ -extern const struct sensors_sensor button_select_sensor; -extern const struct sensors_sensor button_left_sensor; -extern const struct sensors_sensor button_right_sensor; -extern const struct sensors_sensor button_up_sensor; -extern const struct sensors_sensor button_down_sensor; +BUTTON_HAL_BUTTON(key_left, "Key Left", BOARD_IOID_KEY_LEFT, \ + GPIO_HAL_PIN_CFG_PULL_UP, BOARD_BUTTON_HAL_INDEX_KEY_LEFT, \ + true); +BUTTON_HAL_BUTTON(key_right, "Key Right", BOARD_IOID_KEY_RIGHT, \ + GPIO_HAL_PIN_CFG_PULL_UP, BOARD_BUTTON_HAL_INDEX_KEY_RIGHT, \ + true); +BUTTON_HAL_BUTTON(key_up, "Key Up", BOARD_IOID_KEY_UP, \ + GPIO_HAL_PIN_CFG_PULL_UP, BOARD_BUTTON_HAL_INDEX_KEY_UP, \ + true); +BUTTON_HAL_BUTTON(key_down, "Key Down", BOARD_IOID_KEY_DOWN, \ + GPIO_HAL_PIN_CFG_PULL_UP, BOARD_BUTTON_HAL_INDEX_KEY_DOWN, \ + true); +BUTTON_HAL_BUTTON(key_select, "Key Select", BOARD_IOID_KEY_SELECT, \ + GPIO_HAL_PIN_CFG_PULL_UP, \ + BOARD_BUTTON_HAL_INDEX_KEY_SELECT, true); /*---------------------------------------------------------------------------*/ -#endif /* BUTTON_SENSOR_H_ */ +BUTTON_HAL_BUTTONS(&key_left, &key_right, &key_up, &key_down, &key_select); /*---------------------------------------------------------------------------*/ /** @} */ diff --git a/arch/platform/srf06-cc26xx/srf06/button-sensor.c b/arch/platform/srf06-cc26xx/srf06/button-sensor.c deleted file mode 100644 index 1f4a2db3b..000000000 --- a/arch/platform/srf06-cc26xx/srf06/button-sensor.c +++ /dev/null @@ -1,498 +0,0 @@ -/* - * Copyright (c) 2014, Texas Instruments Incorporated - http://www.ti.com/ - * 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 copyright holder 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 COPYRIGHT HOLDERS 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 - * COPYRIGHT HOLDER 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. - */ -/*---------------------------------------------------------------------------*/ -/** - * \addtogroup srf06-common-peripherals - * @{ - * - * \file - * Driver for the SmartRF06EB buttons when a CC13xx/CC26xxEM is mounted on it - */ -/*---------------------------------------------------------------------------*/ -#include "contiki.h" -#include "lib/sensors.h" -#include "srf06/button-sensor.h" -#include "gpio-hal.h" -#include "sys/timer.h" -#include "lpm.h" - -#include "ti-lib.h" - -#include -/*---------------------------------------------------------------------------*/ -#ifdef BUTTON_SENSOR_CONF_ENABLE_SHUTDOWN -#define BUTTON_SENSOR_ENABLE_SHUTDOWN BUTTON_SENSOR_CONF_ENABLE_SHUTDOWN -#else -#define BUTTON_SENSOR_ENABLE_SHUTDOWN 1 -#endif -/*---------------------------------------------------------------------------*/ -#define BUTTON_GPIO_CFG (IOC_CURRENT_2MA | IOC_STRENGTH_AUTO | \ - IOC_IOPULL_UP | IOC_SLEW_DISABLE | \ - IOC_HYST_DISABLE | IOC_BOTH_EDGES | \ - IOC_INT_ENABLE | IOC_IOMODE_NORMAL | \ - IOC_NO_WAKE_UP | IOC_INPUT_ENABLE) -/*---------------------------------------------------------------------------*/ -#define DEBOUNCE_DURATION (CLOCK_SECOND >> 5) - -struct btn_timer { - struct timer debounce; - clock_time_t start; - clock_time_t duration; -}; - -static struct btn_timer sel_timer, left_timer, right_timer, up_timer, - down_timer; -/*---------------------------------------------------------------------------*/ -/** - * \brief Handler for SmartRF button presses - */ -static void -button_press_handler(gpio_hal_pin_mask_t pin_mask) -{ - if(pin_mask & gpio_hal_pin_to_mask(BOARD_IOID_KEY_SELECT)) { - if(!timer_expired(&sel_timer.debounce)) { - return; - } - - timer_set(&sel_timer.debounce, DEBOUNCE_DURATION); - - /* - * Start press duration counter on press (falling), notify on release - * (rising) - */ - if(ti_lib_gpio_read_dio(BOARD_IOID_KEY_SELECT) == 0) { - sel_timer.start = clock_time(); - sel_timer.duration = 0; - } else { - sel_timer.duration = clock_time() - sel_timer.start; - sensors_changed(&button_select_sensor); - } - } - - if(pin_mask & gpio_hal_pin_to_mask(BOARD_IOID_KEY_LEFT)) { - if(!timer_expired(&left_timer.debounce)) { - return; - } - - timer_set(&left_timer.debounce, DEBOUNCE_DURATION); - - /* - * Start press duration counter on press (falling), notify on release - * (rising) - */ - if(ti_lib_gpio_read_dio(BOARD_IOID_KEY_LEFT) == 0) { - left_timer.start = clock_time(); - left_timer.duration = 0; - } else { - left_timer.duration = clock_time() - left_timer.start; - sensors_changed(&button_left_sensor); - } - } - - if(pin_mask & gpio_hal_pin_to_mask(BOARD_IOID_KEY_RIGHT)) { - if(BUTTON_SENSOR_ENABLE_SHUTDOWN == 0) { - if(!timer_expired(&right_timer.debounce)) { - return; - } - - timer_set(&right_timer.debounce, DEBOUNCE_DURATION); - - /* - * Start press duration counter on press (falling), notify on release - * (rising) - */ - if(ti_lib_gpio_read_dio(BOARD_IOID_KEY_RIGHT) == 0) { - right_timer.start = clock_time(); - right_timer.duration = 0; - } else { - right_timer.duration = clock_time() - right_timer.start; - sensors_changed(&button_right_sensor); - } - } else { - lpm_shutdown(BOARD_IOID_KEY_RIGHT, IOC_IOPULL_UP, IOC_WAKE_ON_LOW); - } - } - - if(pin_mask & gpio_hal_pin_to_mask(BOARD_IOID_KEY_UP)) { - if(!timer_expired(&up_timer.debounce)) { - return; - } - - timer_set(&up_timer.debounce, DEBOUNCE_DURATION); - - /* - * Start press duration counter on press (falling), notify on release - * (rising) - */ - if(ti_lib_gpio_read_dio(BOARD_IOID_KEY_UP) == 0) { - up_timer.start = clock_time(); - up_timer.duration = 0; - } else { - up_timer.duration = clock_time() - up_timer.start; - sensors_changed(&button_up_sensor); - } - } - - if(pin_mask & gpio_hal_pin_to_mask(BOARD_IOID_KEY_DOWN)) { - if(!timer_expired(&down_timer.debounce)) { - return; - } - - timer_set(&down_timer.debounce, DEBOUNCE_DURATION); - - /* - * Start press duration counter on press (falling), notify on release - * (rising) - */ - if(ti_lib_gpio_read_dio(BOARD_IOID_KEY_DOWN) == 0) { - down_timer.start = clock_time(); - down_timer.duration = 0; - } else { - down_timer.duration = clock_time() - down_timer.start; - sensors_changed(&button_down_sensor); - } - } -} -/*---------------------------------------------------------------------------*/ -static gpio_hal_event_handler_t press_handler = { - .next = NULL, - .handler = button_press_handler, - .pin_mask = 0, -}; -/*---------------------------------------------------------------------------*/ -/** - * \brief Configuration function for the button sensor for all buttons. - * - * \param type This function does nothing unless type == SENSORS_ACTIVE - * \param c 0: disable the button, non-zero: enable - * \param key: One of BOARD_KEY_LEFT, BOARD_KEY_RIGHT etc - */ -static void -config_buttons(int type, int c, uint32_t key) -{ - switch(type) { - case SENSORS_HW_INIT: - ti_lib_gpio_clear_event_dio(key); - ti_lib_rom_ioc_pin_type_gpio_input(key); - ti_lib_rom_ioc_port_configure_set(key, IOC_PORT_GPIO, BUTTON_GPIO_CFG); - press_handler.pin_mask |= gpio_hal_pin_to_mask(key); - gpio_hal_register_handler(&press_handler); - break; - case SENSORS_ACTIVE: - if(c) { - ti_lib_gpio_clear_event_dio(key); - ti_lib_rom_ioc_pin_type_gpio_input(key); - ti_lib_rom_ioc_port_configure_set(key, IOC_PORT_GPIO, BUTTON_GPIO_CFG); - ti_lib_rom_ioc_int_enable(key); - } else { - ti_lib_rom_ioc_int_disable(key); - } - break; - default: - break; - } -} -/*---------------------------------------------------------------------------*/ -/** - * \brief Configuration function for the select button. - * - * Parameters are passed onto config_buttons, which does the actual - * configuration - * Parameters are ignored. They have been included because the prototype is - * dictated by the core sensor api. The return value is also required by - * the API but otherwise ignored. - * - * \param type passed to config_buttons as-is - * \param value passed to config_buttons as-is - * - * \return ignored - */ -static int -config_select(int type, int value) -{ - config_buttons(type, value, BOARD_IOID_KEY_SELECT); - - return 1; -} -/*---------------------------------------------------------------------------*/ -/** - * \brief Configuration function for the left button. - * - * Parameters are passed onto config_buttons, which does the actual - * configuration - * Parameters are ignored. They have been included because the prototype is - * dictated by the core sensor api. The return value is also required by - * the API but otherwise ignored. - * - * \param type passed to config_buttons as-is - * \param value passed to config_buttons as-is - * - * \return ignored - */ -static int -config_left(int type, int value) -{ - config_buttons(type, value, BOARD_IOID_KEY_LEFT); - - return 1; -} -/*---------------------------------------------------------------------------*/ -/** - * \brief Configuration function for the right button. - * - * Parameters are passed onto config_buttons, which does the actual - * configuration - * Parameters are ignored. They have been included because the prototype is - * dictated by the core sensor api. The return value is also required by - * the API but otherwise ignored. - * - * \param type passed to config_buttons as-is - * \param value passed to config_buttons as-is - * - * \return ignored - */ -static int -config_right(int type, int value) -{ - config_buttons(type, value, BOARD_IOID_KEY_RIGHT); - - return 1; -} -/*---------------------------------------------------------------------------*/ -/** - * \brief Configuration function for the up button. - * - * Parameters are passed onto config_buttons, which does the actual - * configuration - * Parameters are ignored. They have been included because the prototype is - * dictated by the core sensor api. The return value is also required by - * the API but otherwise ignored. - * - * \param type passed to config_buttons as-is - * \param value passed to config_buttons as-is - * - * \return ignored - */ -static int -config_up(int type, int value) -{ - config_buttons(type, value, BOARD_IOID_KEY_UP); - - return 1; -} -/*---------------------------------------------------------------------------*/ -/** - * \brief Configuration function for the down button. - * - * Parameters are passed onto config_buttons, which does the actual - * configuration - * Parameters are ignored. They have been included because the prototype is - * dictated by the core sensor api. The return value is also required by - * the API but otherwise ignored. - * - * \param type passed to config_buttons as-is - * \param value passed to config_buttons as-is - * - * \return ignored - */ -static int -config_down(int type, int value) -{ - config_buttons(type, value, BOARD_IOID_KEY_DOWN); - - return 1; -} -/*---------------------------------------------------------------------------*/ -static int -value_select(int type) -{ - if(type == BUTTON_SENSOR_VALUE_STATE) { - return ti_lib_gpio_read_dio(BOARD_IOID_KEY_SELECT) == 0 ? - BUTTON_SENSOR_VALUE_PRESSED : BUTTON_SENSOR_VALUE_RELEASED; - } else if(type == BUTTON_SENSOR_VALUE_DURATION) { - return (int)sel_timer.duration; - } - return 0; -} -/*---------------------------------------------------------------------------*/ -static int -value_left(int type) -{ - if(type == BUTTON_SENSOR_VALUE_STATE) { - return ti_lib_gpio_read_dio(BOARD_IOID_KEY_LEFT) == 0 ? - BUTTON_SENSOR_VALUE_PRESSED : BUTTON_SENSOR_VALUE_RELEASED; - } else if(type == BUTTON_SENSOR_VALUE_DURATION) { - return (int)left_timer.duration; - } - return 0; -} -/*---------------------------------------------------------------------------*/ -static int -value_right(int type) -{ - if(type == BUTTON_SENSOR_VALUE_STATE) { - return ti_lib_gpio_read_dio(BOARD_IOID_KEY_RIGHT) == 0 ? - BUTTON_SENSOR_VALUE_PRESSED : BUTTON_SENSOR_VALUE_RELEASED; - } else if(type == BUTTON_SENSOR_VALUE_DURATION) { - return (int)right_timer.duration; - } - return 0; -} -/*---------------------------------------------------------------------------*/ -static int -value_up(int type) -{ - if(type == BUTTON_SENSOR_VALUE_STATE) { - return ti_lib_gpio_read_dio(BOARD_IOID_KEY_UP) == 0 ? - BUTTON_SENSOR_VALUE_PRESSED : BUTTON_SENSOR_VALUE_RELEASED; - } else if(type == BUTTON_SENSOR_VALUE_DURATION) { - return (int)up_timer.duration; - } - return 0; -} -/*---------------------------------------------------------------------------*/ -static int -value_down(int type) -{ - if(type == BUTTON_SENSOR_VALUE_STATE) { - return ti_lib_gpio_read_dio(BOARD_IOID_KEY_DOWN) == 0 ? - BUTTON_SENSOR_VALUE_PRESSED : BUTTON_SENSOR_VALUE_RELEASED; - } else if(type == BUTTON_SENSOR_VALUE_DURATION) { - return (int)down_timer.duration; - } - return 0; -} -/*---------------------------------------------------------------------------*/ -/** - * \brief Status function for all buttons - * \param type SENSORS_ACTIVE or SENSORS_READY - * \param key_io_id BOARD_IOID_KEY_LEFT, BOARD_IOID_KEY_RIGHT etc - * \return 1 if the button's port interrupt is enabled (edge detect) - * - * This function will only be called by status_left, status_right and the - * called will pass the correct key_io_id - */ -static int -status(int type, uint32_t key_io_id) -{ - switch(type) { - case SENSORS_ACTIVE: - case SENSORS_READY: - if(ti_lib_ioc_port_configure_get(key_io_id) & IOC_INT_ENABLE) { - return 1; - } - break; - default: - break; - } - return 0; -} -/*---------------------------------------------------------------------------*/ -/** - * \brief Status function for the select button. - * \param type SENSORS_ACTIVE or SENSORS_READY - * \return 1 if the button's port interrupt is enabled (edge detect) - * - * This function will call status. It will pass type verbatim and it will also - * pass the correct key_io_id - */ -static int -status_select(int type) -{ - return status(type, BOARD_IOID_KEY_SELECT); -} -/*---------------------------------------------------------------------------*/ -/** - * \brief Status function for the left button. - * \param type SENSORS_ACTIVE or SENSORS_READY - * \return 1 if the button's port interrupt is enabled (edge detect) - * - * This function will call status. It will pass type verbatim and it will also - * pass the correct key_io_id - */ -static int -status_left(int type) -{ - return status(type, BOARD_IOID_KEY_LEFT); -} -/*---------------------------------------------------------------------------*/ -/** - * \brief Status function for the right button. - * \param type SENSORS_ACTIVE or SENSORS_READY - * \return 1 if the button's port interrupt is enabled (edge detect) - * - * This function will call status. It will pass type verbatim and it will also - * pass the correct key_io_id - */ -static int -status_right(int type) -{ - return status(type, BOARD_IOID_KEY_RIGHT); -} -/*---------------------------------------------------------------------------*/ -/** - * \brief Status function for the up button. - * \param type SENSORS_ACTIVE or SENSORS_READY - * \return 1 if the button's port interrupt is enabled (edge detect) - * - * This function will call status. It will pass type verbatim and it will also - * pass the correct key_io_id - */ -static int -status_up(int type) -{ - return status(type, BOARD_IOID_KEY_UP); -} -/*---------------------------------------------------------------------------*/ -/** - * \brief Status function for the down button. - * \param type SENSORS_ACTIVE or SENSORS_READY - * \return 1 if the button's port interrupt is enabled (edge detect) - * - * This function will call status. It will pass type verbatim and it will also - * pass the correct key_io_id - */ -static int -status_down(int type) -{ - return status(type, BOARD_IOID_KEY_DOWN); -} -/*---------------------------------------------------------------------------*/ -SENSORS_SENSOR(button_select_sensor, BUTTON_SENSOR, value_select, - config_select, status_select); -SENSORS_SENSOR(button_left_sensor, BUTTON_SENSOR, value_left, config_left, - status_left); -SENSORS_SENSOR(button_right_sensor, BUTTON_SENSOR, value_right, config_right, - status_right); -SENSORS_SENSOR(button_up_sensor, BUTTON_SENSOR, value_up, config_up, status_up); -SENSORS_SENSOR(button_down_sensor, BUTTON_SENSOR, value_down, config_down, - status_down); -/*---------------------------------------------------------------------------*/ -/** @} */ diff --git a/arch/platform/srf06-cc26xx/srf06/cc13xx/board.h b/arch/platform/srf06-cc26xx/srf06/cc13xx/board.h index bf12b2b09..9fece9180 100644 --- a/arch/platform/srf06-cc26xx/srf06/cc13xx/board.h +++ b/arch/platform/srf06-cc26xx/srf06/cc13xx/board.h @@ -232,6 +232,19 @@ #endif /** @} */ /*---------------------------------------------------------------------------*/ +/** + * \brief Board indices for the button HAL + * + * Those values are not meant to be modified by the user + * @{ + */ +#define BOARD_BUTTON_HAL_INDEX_KEY_LEFT 0x00 +#define BOARD_BUTTON_HAL_INDEX_KEY_RIGHT 0x01 +#define BOARD_BUTTON_HAL_INDEX_KEY_UP 0x02 +#define BOARD_BUTTON_HAL_INDEX_KEY_DOWN 0x03 +#define BOARD_BUTTON_HAL_INDEX_KEY_SELECT 0x04 +/** @} */ +/*---------------------------------------------------------------------------*/ /** * \name Device string used on startup * @{ diff --git a/arch/platform/srf06-cc26xx/srf06/cc26xx/board.h b/arch/platform/srf06-cc26xx/srf06/cc26xx/board.h index 44080f474..b9171388a 100644 --- a/arch/platform/srf06-cc26xx/srf06/cc26xx/board.h +++ b/arch/platform/srf06-cc26xx/srf06/cc26xx/board.h @@ -232,6 +232,19 @@ #endif /** @} */ /*---------------------------------------------------------------------------*/ +/** + * \brief Board indices for the button HAL + * + * Those values are not meant to be modified by the user + * @{ + */ +#define BOARD_BUTTON_HAL_INDEX_KEY_LEFT 0x00 +#define BOARD_BUTTON_HAL_INDEX_KEY_RIGHT 0x01 +#define BOARD_BUTTON_HAL_INDEX_KEY_UP 0x02 +#define BOARD_BUTTON_HAL_INDEX_KEY_DOWN 0x03 +#define BOARD_BUTTON_HAL_INDEX_KEY_SELECT 0x04 +/** @} */ +/*---------------------------------------------------------------------------*/ /** * \name Device string used on startup * @{ diff --git a/arch/platform/srf06-cc26xx/srf06/srf06-sensors.c b/arch/platform/srf06-cc26xx/srf06/srf06-sensors.c index 5f65a0cb2..642d2b103 100644 --- a/arch/platform/srf06-cc26xx/srf06/srf06-sensors.c +++ b/arch/platform/srf06-cc26xx/srf06/srf06-sensors.c @@ -38,13 +38,11 @@ */ /*---------------------------------------------------------------------------*/ #include "contiki.h" -#include "srf06/button-sensor.h" #include "srf06/als-sensor.h" #include /*---------------------------------------------------------------------------*/ /** \brief Exports a global symbol to be used by the sensor API */ -SENSORS(&button_select_sensor, &button_left_sensor, &button_right_sensor, - &button_up_sensor, &button_down_sensor, &als_sensor); +SENSORS(&als_sensor); /*---------------------------------------------------------------------------*/ /** @} */ From f199ff0a5f9c39fdfbd7f43fe5d161f3fe20bc7d Mon Sep 17 00:00:00 2001 From: George Oikonomou Date: Sat, 4 Nov 2017 22:59:17 +0000 Subject: [PATCH 04/44] Implement the new button HAL: Launchpad --- .../srf06-cc26xx/launchpad/Makefile.launchpad | 2 +- .../{launchpad-sensors.c => board-buttons.c} | 22 +- .../launchpad/board-peripherals.h | 2 + arch/platform/srf06-cc26xx/launchpad/board.c | 1 - .../srf06-cc26xx/launchpad/button-sensor.c | 225 ------------------ .../srf06-cc26xx/launchpad/button-sensor.h | 65 ----- .../srf06-cc26xx/launchpad/cc1310/board.h | 10 + .../srf06-cc26xx/launchpad/cc1350/board.h | 10 + .../srf06-cc26xx/launchpad/cc2650/board.h | 10 + 9 files changed, 48 insertions(+), 299 deletions(-) rename arch/platform/srf06-cc26xx/launchpad/{launchpad-sensors.c => board-buttons.c} (73%) delete mode 100644 arch/platform/srf06-cc26xx/launchpad/button-sensor.c delete mode 100644 arch/platform/srf06-cc26xx/launchpad/button-sensor.h diff --git a/arch/platform/srf06-cc26xx/launchpad/Makefile.launchpad b/arch/platform/srf06-cc26xx/launchpad/Makefile.launchpad index 224f0c2c5..f10f979da 100644 --- a/arch/platform/srf06-cc26xx/launchpad/Makefile.launchpad +++ b/arch/platform/srf06-cc26xx/launchpad/Makefile.launchpad @@ -2,7 +2,7 @@ CFLAGS += -DBOARD_LAUNCHPAD=1 CONTIKI_TARGET_DIRS += launchpad common -BOARD_SOURCEFILES += board.c launchpad-sensors.c button-sensor.c xmem.c +BOARD_SOURCEFILES += board.c board-buttons.c xmem.c BOARD_SOURCEFILES += ext-flash.c board-spi.c ### Signal that we can be programmed with cc2538-bsl diff --git a/arch/platform/srf06-cc26xx/launchpad/launchpad-sensors.c b/arch/platform/srf06-cc26xx/launchpad/board-buttons.c similarity index 73% rename from arch/platform/srf06-cc26xx/launchpad/launchpad-sensors.c rename to arch/platform/srf06-cc26xx/launchpad/board-buttons.c index c940ec68c..f07fff09e 100644 --- a/arch/platform/srf06-cc26xx/launchpad/launchpad-sensors.c +++ b/arch/platform/srf06-cc26xx/launchpad/board-buttons.c @@ -1,10 +1,11 @@ /* - * Copyright (c) 2015, Texas Instruments Incorporated - http://www.ti.com/ + * Copyright (c) 2017, George Oikonomou - http://www.spd.gr * 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 @@ -29,19 +30,26 @@ */ /*---------------------------------------------------------------------------*/ /** - * \addtogroup launchpad-peripherals + * \addtogroup launchpad-cc26xx-peripherals * @{ * * \file - * Generic module controlling LaunchPad sensors + * Defines CC13xx/CC26xx Launchpad buttons for use with the button HAL */ /*---------------------------------------------------------------------------*/ #include "contiki.h" -#include "launchpad/button-sensor.h" +#include "dev/button-hal.h" -#include +#include "ti-lib.h" /*---------------------------------------------------------------------------*/ -/** \brief Exports a global symbol to be used by the sensor API */ -SENSORS(&button_left_sensor, &button_right_sensor); +BUTTON_HAL_BUTTON(key_left, "Key Left", BOARD_IOID_KEY_LEFT, \ + GPIO_HAL_PIN_CFG_PULL_UP, BOARD_BUTTON_HAL_INDEX_KEY_LEFT, \ + true); + +BUTTON_HAL_BUTTON(key_right, "Key Right", BOARD_IOID_KEY_RIGHT, \ + GPIO_HAL_PIN_CFG_PULL_UP, BOARD_BUTTON_HAL_INDEX_KEY_RIGHT, \ + true); +/*---------------------------------------------------------------------------*/ +BUTTON_HAL_BUTTONS(&key_left, &key_right); /*---------------------------------------------------------------------------*/ /** @} */ diff --git a/arch/platform/srf06-cc26xx/launchpad/board-peripherals.h b/arch/platform/srf06-cc26xx/launchpad/board-peripherals.h index 3e1220234..0ad9c6b68 100644 --- a/arch/platform/srf06-cc26xx/launchpad/board-peripherals.h +++ b/arch/platform/srf06-cc26xx/launchpad/board-peripherals.h @@ -48,6 +48,8 @@ /*---------------------------------------------------------------------------*/ #include "ext-flash.h" /*---------------------------------------------------------------------------*/ +#define BOARD_CONF_HAS_SENSORS 0 +/*---------------------------------------------------------------------------*/ #endif /* BOARD_PERIPHERALS_H_ */ /*---------------------------------------------------------------------------*/ /** diff --git a/arch/platform/srf06-cc26xx/launchpad/board.c b/arch/platform/srf06-cc26xx/launchpad/board.c index 74b12bd3d..7baf71d47 100644 --- a/arch/platform/srf06-cc26xx/launchpad/board.c +++ b/arch/platform/srf06-cc26xx/launchpad/board.c @@ -37,7 +37,6 @@ */ /*---------------------------------------------------------------------------*/ #include "contiki.h" -#include "lib/sensors.h" #include "lpm.h" #include "ti-lib.h" #include "board-peripherals.h" diff --git a/arch/platform/srf06-cc26xx/launchpad/button-sensor.c b/arch/platform/srf06-cc26xx/launchpad/button-sensor.c deleted file mode 100644 index 43d07ad9c..000000000 --- a/arch/platform/srf06-cc26xx/launchpad/button-sensor.c +++ /dev/null @@ -1,225 +0,0 @@ -/* - * Copyright (c) 2015, Texas Instruments Incorporated - http://www.ti.com/ - * 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 copyright holder 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 COPYRIGHT HOLDERS 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 - * COPYRIGHT HOLDER 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. - */ -/*---------------------------------------------------------------------------*/ -/** - * \addtogroup launchpad-button-sensor - * @{ - * - * \file - * Driver for LaunchPad buttons - */ -/*---------------------------------------------------------------------------*/ -#include "contiki.h" -#include "lib/sensors.h" -#include "launchpad/button-sensor.h" -#include "gpio-hal.h" -#include "sys/timer.h" -#include "lpm.h" - -#include "ti-lib.h" - -#include -/*---------------------------------------------------------------------------*/ -#ifdef BUTTON_SENSOR_CONF_ENABLE_SHUTDOWN -#define BUTTON_SENSOR_ENABLE_SHUTDOWN BUTTON_SENSOR_CONF_ENABLE_SHUTDOWN -#else -#define BUTTON_SENSOR_ENABLE_SHUTDOWN 1 -#endif -/*---------------------------------------------------------------------------*/ -#define BUTTON_GPIO_CFG (IOC_CURRENT_2MA | IOC_STRENGTH_AUTO | \ - IOC_IOPULL_UP | IOC_SLEW_DISABLE | \ - IOC_HYST_DISABLE | IOC_BOTH_EDGES | \ - IOC_INT_ENABLE | IOC_IOMODE_NORMAL | \ - IOC_NO_WAKE_UP | IOC_INPUT_ENABLE) -/*---------------------------------------------------------------------------*/ -#define DEBOUNCE_DURATION (CLOCK_SECOND >> 5) - -struct btn_timer { - struct timer debounce; - clock_time_t start; - clock_time_t duration; -}; - -static struct btn_timer left_timer, right_timer; -/*---------------------------------------------------------------------------*/ -static void -button_press_handler(gpio_hal_pin_mask_t pin_mask) -{ - if(pin_mask & gpio_hal_pin_to_mask(BOARD_IOID_KEY_LEFT)) { - if(!timer_expired(&left_timer.debounce)) { - return; - } - - timer_set(&left_timer.debounce, DEBOUNCE_DURATION); - - /* - * Start press duration counter on press (falling), notify on release - * (rising) - */ - if(ti_lib_gpio_read_dio(BOARD_IOID_KEY_LEFT) == 0) { - left_timer.start = clock_time(); - left_timer.duration = 0; - } else { - left_timer.duration = clock_time() - left_timer.start; - sensors_changed(&button_left_sensor); - } - } - - if(pin_mask & gpio_hal_pin_to_mask(BOARD_IOID_KEY_RIGHT)) { - if(BUTTON_SENSOR_ENABLE_SHUTDOWN == 0) { - if(!timer_expired(&right_timer.debounce)) { - return; - } - - timer_set(&right_timer.debounce, DEBOUNCE_DURATION); - - /* - * Start press duration counter on press (falling), notify on release - * (rising) - */ - if(ti_lib_gpio_read_dio(BOARD_IOID_KEY_RIGHT) == 0) { - right_timer.start = clock_time(); - right_timer.duration = 0; - } else { - right_timer.duration = clock_time() - right_timer.start; - sensors_changed(&button_right_sensor); - } - } else { - lpm_shutdown(BOARD_IOID_KEY_RIGHT, IOC_IOPULL_UP, IOC_WAKE_ON_LOW); - } - } -} -/*---------------------------------------------------------------------------*/ -static gpio_hal_event_handler_t press_handler = { - .next = NULL, - .handler = button_press_handler, - .pin_mask = 0, -}; -/*---------------------------------------------------------------------------*/ -static void -config_buttons(int type, int c, uint32_t key) -{ - switch(type) { - case SENSORS_HW_INIT: - ti_lib_gpio_clear_event_dio(key); - ti_lib_rom_ioc_pin_type_gpio_input(key); - ti_lib_rom_ioc_port_configure_set(key, IOC_PORT_GPIO, BUTTON_GPIO_CFG); - press_handler.pin_mask |= gpio_hal_pin_to_mask(key); - gpio_hal_register_handler(&press_handler); - break; - case SENSORS_ACTIVE: - if(c) { - ti_lib_gpio_clear_event_dio(key); - ti_lib_rom_ioc_pin_type_gpio_input(key); - ti_lib_rom_ioc_port_configure_set(key, IOC_PORT_GPIO, BUTTON_GPIO_CFG); - ti_lib_rom_ioc_int_enable(key); - } else { - ti_lib_rom_ioc_int_disable(key); - } - break; - default: - break; - } -} -/*---------------------------------------------------------------------------*/ -static int -config_left(int type, int value) -{ - config_buttons(type, value, BOARD_IOID_KEY_LEFT); - - return 1; -} -/*---------------------------------------------------------------------------*/ -static int -config_right(int type, int value) -{ - config_buttons(type, value, BOARD_IOID_KEY_RIGHT); - - return 1; -} -/*---------------------------------------------------------------------------*/ -static int -status(int type, uint32_t key_io_id) -{ - switch(type) { - case SENSORS_ACTIVE: - case SENSORS_READY: - if(ti_lib_rom_ioc_port_configure_get(key_io_id) & IOC_INT_ENABLE) { - return 1; - } - break; - default: - break; - } - return 0; -} -/*---------------------------------------------------------------------------*/ -static int -value_left(int type) -{ - if(type == BUTTON_SENSOR_VALUE_STATE) { - return ti_lib_gpio_read_dio(BOARD_IOID_KEY_LEFT) == 0 ? - BUTTON_SENSOR_VALUE_PRESSED : BUTTON_SENSOR_VALUE_RELEASED; - } else if(type == BUTTON_SENSOR_VALUE_DURATION) { - return (int)left_timer.duration; - } - return 0; -} -/*---------------------------------------------------------------------------*/ -static int -value_right(int type) -{ - if(type == BUTTON_SENSOR_VALUE_STATE) { - return ti_lib_gpio_read_dio(BOARD_IOID_KEY_RIGHT) == 0 ? - BUTTON_SENSOR_VALUE_PRESSED : BUTTON_SENSOR_VALUE_RELEASED; - } else if(type == BUTTON_SENSOR_VALUE_DURATION) { - return (int)right_timer.duration; - } - return 0; -} -/*---------------------------------------------------------------------------*/ -static int -status_left(int type) -{ - return status(type, BOARD_IOID_KEY_LEFT); -} -/*---------------------------------------------------------------------------*/ -static int -status_right(int type) -{ - return status(type, BOARD_IOID_KEY_RIGHT); -} -/*---------------------------------------------------------------------------*/ -SENSORS_SENSOR(button_left_sensor, BUTTON_SENSOR, value_left, config_left, - status_left); -SENSORS_SENSOR(button_right_sensor, BUTTON_SENSOR, value_right, config_right, - status_right); -/*---------------------------------------------------------------------------*/ -/** @} */ diff --git a/arch/platform/srf06-cc26xx/launchpad/button-sensor.h b/arch/platform/srf06-cc26xx/launchpad/button-sensor.h deleted file mode 100644 index 0c945d3f7..000000000 --- a/arch/platform/srf06-cc26xx/launchpad/button-sensor.h +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright (c) 2015, Texas Instruments Incorporated - http://www.ti.com/ - * 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 copyright holder 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 COPYRIGHT HOLDERS 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 - * COPYRIGHT HOLDER 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. - */ -/*---------------------------------------------------------------------------*/ -/** - * \addtogroup launchpad-peripherals - * @{ - * - * \defgroup launchpad-button-sensor LaunchPad Button Driver - * - * One of the buttons can be configured as general purpose or as an on/off key - * @{ - * - * \file - * Header file for the LaunchPad Button Driver - */ -/*---------------------------------------------------------------------------*/ -#ifndef BUTTON_SENSOR_H_ -#define BUTTON_SENSOR_H_ -/*---------------------------------------------------------------------------*/ -#include "lib/sensors.h" -/*---------------------------------------------------------------------------*/ -#define BUTTON_SENSOR "Button" -/*---------------------------------------------------------------------------*/ -#define BUTTON_SENSOR_VALUE_STATE 0 -#define BUTTON_SENSOR_VALUE_DURATION 1 - -#define BUTTON_SENSOR_VALUE_RELEASED 0 -#define BUTTON_SENSOR_VALUE_PRESSED 1 -/*---------------------------------------------------------------------------*/ -extern const struct sensors_sensor button_left_sensor; -extern const struct sensors_sensor button_right_sensor; -/*---------------------------------------------------------------------------*/ -#endif /* BUTTON_SENSOR_H_ */ -/*---------------------------------------------------------------------------*/ -/** - * @} - * @} - */ diff --git a/arch/platform/srf06-cc26xx/launchpad/cc1310/board.h b/arch/platform/srf06-cc26xx/launchpad/cc1310/board.h index d8a87e53a..d97086415 100644 --- a/arch/platform/srf06-cc26xx/launchpad/cc1310/board.h +++ b/arch/platform/srf06-cc26xx/launchpad/cc1310/board.h @@ -188,6 +188,16 @@ } /** @} */ /*---------------------------------------------------------------------------*/ +/** + * \brief Board indices for the button HAL + * + * Those values are not meant to be modified by the user + * @{ + */ +#define BOARD_BUTTON_HAL_INDEX_KEY_LEFT 0x00 +#define BOARD_BUTTON_HAL_INDEX_KEY_RIGHT 0x01 +/** @} */ +/*---------------------------------------------------------------------------*/ /** * \name Device string used on startup * @{ diff --git a/arch/platform/srf06-cc26xx/launchpad/cc1350/board.h b/arch/platform/srf06-cc26xx/launchpad/cc1350/board.h index c80fb1326..72a7a180f 100644 --- a/arch/platform/srf06-cc26xx/launchpad/cc1350/board.h +++ b/arch/platform/srf06-cc26xx/launchpad/cc1350/board.h @@ -205,6 +205,16 @@ } /** @} */ /*---------------------------------------------------------------------------*/ +/** + * \brief Board indices for the button HAL + * + * Those values are not meant to be modified by the user + * @{ + */ +#define BOARD_BUTTON_HAL_INDEX_KEY_LEFT 0x00 +#define BOARD_BUTTON_HAL_INDEX_KEY_RIGHT 0x01 +/** @} */ +/*---------------------------------------------------------------------------*/ /** * \name Device string used on startup * @{ diff --git a/arch/platform/srf06-cc26xx/launchpad/cc2650/board.h b/arch/platform/srf06-cc26xx/launchpad/cc2650/board.h index 1ea40957a..deb79a7b1 100644 --- a/arch/platform/srf06-cc26xx/launchpad/cc2650/board.h +++ b/arch/platform/srf06-cc26xx/launchpad/cc2650/board.h @@ -189,6 +189,16 @@ } /** @} */ /*---------------------------------------------------------------------------*/ +/** + * \brief Board indices for the button HAL + * + * Those values are not meant to be modified by the user + * @{ + */ +#define BOARD_BUTTON_HAL_INDEX_KEY_LEFT 0x00 +#define BOARD_BUTTON_HAL_INDEX_KEY_RIGHT 0x01 +/** @} */ +/*---------------------------------------------------------------------------*/ /** * \name Device string used on startup * @{ From 8ac1cf92cefa54cc49a307d84def1f7fb2457083 Mon Sep 17 00:00:00 2001 From: George Oikonomou Date: Sat, 4 Nov 2017 22:59:27 +0000 Subject: [PATCH 05/44] Implement the new button HAL: Sensortag --- .../srf06-cc26xx/sensortag/Makefile.sensortag | 6 +- .../{reed-relay.h => board-buttons.c} | 43 +-- .../sensortag/board-peripherals.h | 1 - .../srf06-cc26xx/sensortag/button-sensor.c | 288 ------------------ .../srf06-cc26xx/sensortag/button-sensor.h | 65 ---- .../srf06-cc26xx/sensortag/cc1350/board.h | 11 + .../srf06-cc26xx/sensortag/cc2650/board.h | 11 + .../srf06-cc26xx/sensortag/reed-relay.c | 145 --------- .../sensortag/sensortag-sensors.c | 7 +- 9 files changed, 50 insertions(+), 527 deletions(-) rename arch/platform/srf06-cc26xx/sensortag/{reed-relay.h => board-buttons.c} (71%) delete mode 100644 arch/platform/srf06-cc26xx/sensortag/button-sensor.c delete mode 100644 arch/platform/srf06-cc26xx/sensortag/button-sensor.h delete mode 100644 arch/platform/srf06-cc26xx/sensortag/reed-relay.c diff --git a/arch/platform/srf06-cc26xx/sensortag/Makefile.sensortag b/arch/platform/srf06-cc26xx/sensortag/Makefile.sensortag index 1862f59fa..46cf25a68 100644 --- a/arch/platform/srf06-cc26xx/sensortag/Makefile.sensortag +++ b/arch/platform/srf06-cc26xx/sensortag/Makefile.sensortag @@ -3,8 +3,8 @@ CFLAGS += -DBACKDOOR_IOID=0x00000000 CONTIKI_TARGET_DIRS += sensortag common -BOARD_SOURCEFILES += sensortag-sensors.c sensor-common.c +BOARD_SOURCEFILES += sensortag-sensors.c board-buttons.c sensor-common.c BOARD_SOURCEFILES += bmp-280-sensor.c tmp-007-sensor.c opt-3001-sensor.c -BOARD_SOURCEFILES += hdc-1000-sensor.c mpu-9250-sensor.c button-sensor.c xmem.c -BOARD_SOURCEFILES += reed-relay.c ext-flash.c buzzer.c +BOARD_SOURCEFILES += hdc-1000-sensor.c mpu-9250-sensor.c xmem.c +BOARD_SOURCEFILES += ext-flash.c buzzer.c BOARD_SOURCEFILES += board.c board-spi.c board-i2c.c diff --git a/arch/platform/srf06-cc26xx/sensortag/reed-relay.h b/arch/platform/srf06-cc26xx/sensortag/board-buttons.c similarity index 71% rename from arch/platform/srf06-cc26xx/sensortag/reed-relay.h rename to arch/platform/srf06-cc26xx/sensortag/board-buttons.c index 0ed6dd819..4a1b02ed5 100644 --- a/arch/platform/srf06-cc26xx/sensortag/reed-relay.h +++ b/arch/platform/srf06-cc26xx/sensortag/board-buttons.c @@ -1,10 +1,11 @@ /* - * Copyright (c) 2014, Texas Instruments Incorporated - http://www.ti.com/ + * Copyright (c) 2017, George Oikonomou - http://www.spd.gr * 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 @@ -32,28 +33,30 @@ * \addtogroup sensortag-cc26xx-peripherals * @{ * - * \defgroup sensortag-cc26xx-reed-relay SensorTag 2.0 Reed Relay - * - * The reed relay acts like a button without a button. To trigger the reed, - * approach a magnet to the sensortag and a sensors_changed event will be - * generated, in a fashion similar to as if a button had been pressed - * - * @{ - * * \file - * Header file for the Sensortag Reed Relay + * Defines Sensortag buttons for use with the button HAL */ /*---------------------------------------------------------------------------*/ -#ifndef REED_RELAY_H -#define REED_RELAY_H +#include "contiki.h" +#include "dev/gpio-hal.h" +#include "dev/button-hal.h" + +#include "ti-lib.h" + +#include /*---------------------------------------------------------------------------*/ -#include "lib/sensors.h" +BUTTON_HAL_BUTTON(reed_relay, "Reed Relay", BOARD_IOID_REED_RELAY, \ + GPIO_HAL_PIN_CFG_PULL_DOWN, \ + BOARD_BUTTON_HAL_INDEX_REED_RELAY, true); + +BUTTON_HAL_BUTTON(key_left, "Key Left", BOARD_IOID_KEY_LEFT, \ + GPIO_HAL_PIN_CFG_PULL_UP, BOARD_BUTTON_HAL_INDEX_KEY_LEFT, \ + true); + +BUTTON_HAL_BUTTON(key_right, "Key Right", BOARD_IOID_KEY_RIGHT, \ + GPIO_HAL_PIN_CFG_PULL_UP, BOARD_BUTTON_HAL_INDEX_KEY_RIGHT, \ + true); /*---------------------------------------------------------------------------*/ -extern const struct sensors_sensor reed_relay_sensor; +BUTTON_HAL_BUTTONS(&reed_relay, &key_left, &key_right); /*---------------------------------------------------------------------------*/ -#endif /* REED_RELAY_H */ -/*---------------------------------------------------------------------------*/ -/** - * @} - * @} - */ +/** @} */ diff --git a/arch/platform/srf06-cc26xx/sensortag/board-peripherals.h b/arch/platform/srf06-cc26xx/sensortag/board-peripherals.h index 42e22d4c8..65c03198b 100644 --- a/arch/platform/srf06-cc26xx/sensortag/board-peripherals.h +++ b/arch/platform/srf06-cc26xx/sensortag/board-peripherals.h @@ -53,7 +53,6 @@ #include "opt-3001-sensor.h" #include "hdc-1000-sensor.h" #include "mpu-9250-sensor.h" -#include "reed-relay.h" #include "buzzer.h" #include "ext-flash.h" /*---------------------------------------------------------------------------*/ diff --git a/arch/platform/srf06-cc26xx/sensortag/button-sensor.c b/arch/platform/srf06-cc26xx/sensortag/button-sensor.c deleted file mode 100644 index f19704f81..000000000 --- a/arch/platform/srf06-cc26xx/sensortag/button-sensor.c +++ /dev/null @@ -1,288 +0,0 @@ -/* - * Copyright (c) 2014, Texas Instruments Incorporated - http://www.ti.com/ - * 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 copyright holder 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 COPYRIGHT HOLDERS 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 - * COPYRIGHT HOLDER 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. - */ -/*---------------------------------------------------------------------------*/ -/** - * \addtogroup sensortag-cc26xx-button-sensor - * @{ - * - * \file - * Driver for Sensortag buttons - */ -/*---------------------------------------------------------------------------*/ -#include "contiki.h" -#include "lib/sensors.h" -#include "sensortag/button-sensor.h" -#include "gpio-hal.h" -#include "sys/timer.h" -#include "lpm.h" - -#include "ti-lib.h" - -#include -/*---------------------------------------------------------------------------*/ -#ifdef BUTTON_SENSOR_CONF_ENABLE_SHUTDOWN -#define BUTTON_SENSOR_ENABLE_SHUTDOWN BUTTON_SENSOR_CONF_ENABLE_SHUTDOWN -#else -#define BUTTON_SENSOR_ENABLE_SHUTDOWN 1 -#endif -/*---------------------------------------------------------------------------*/ -#define BUTTON_GPIO_CFG (IOC_CURRENT_2MA | IOC_STRENGTH_AUTO | \ - IOC_IOPULL_UP | IOC_SLEW_DISABLE | \ - IOC_HYST_DISABLE | IOC_BOTH_EDGES | \ - IOC_INT_ENABLE | IOC_IOMODE_NORMAL | \ - IOC_NO_WAKE_UP | IOC_INPUT_ENABLE) -/*---------------------------------------------------------------------------*/ -#define DEBOUNCE_DURATION (CLOCK_SECOND >> 5) - -struct btn_timer { - struct timer debounce; - clock_time_t start; - clock_time_t duration; -}; - -static struct btn_timer left_timer, right_timer; -/*---------------------------------------------------------------------------*/ -/** - * \brief Handler for Sensortag-CC26XX button presses - */ -static void -button_press_handler(gpio_hal_pin_mask_t pin_mask) -{ - if(pin_mask & gpio_hal_pin_to_mask(BOARD_IOID_KEY_LEFT)) { - if(!timer_expired(&left_timer.debounce)) { - return; - } - - timer_set(&left_timer.debounce, DEBOUNCE_DURATION); - - /* - * Start press duration counter on press (falling), notify on release - * (rising) - */ - if(ti_lib_gpio_read_dio(BOARD_IOID_KEY_LEFT) == 0) { - left_timer.start = clock_time(); - left_timer.duration = 0; - } else { - left_timer.duration = clock_time() - left_timer.start; - sensors_changed(&button_left_sensor); - } - } - - if(pin_mask & gpio_hal_pin_to_mask(BOARD_IOID_KEY_RIGHT)) { - if(BUTTON_SENSOR_ENABLE_SHUTDOWN == 0) { - if(!timer_expired(&right_timer.debounce)) { - return; - } - - timer_set(&right_timer.debounce, DEBOUNCE_DURATION); - - /* - * Start press duration counter on press (falling), notify on release - * (rising) - */ - if(ti_lib_gpio_read_dio(BOARD_IOID_KEY_RIGHT) == 0) { - right_timer.start = clock_time(); - right_timer.duration = 0; - } else { - right_timer.duration = clock_time() - right_timer.start; - sensors_changed(&button_right_sensor); - } - } else { - lpm_shutdown(BOARD_IOID_KEY_RIGHT, IOC_IOPULL_UP, IOC_WAKE_ON_LOW); - } - } -} -/*---------------------------------------------------------------------------*/ -static gpio_hal_event_handler_t press_handler = { - .next = NULL, - .handler = button_press_handler, - .pin_mask = 0, -}; -/*---------------------------------------------------------------------------*/ -/** - * \brief Configuration function for the button sensor for all buttons. - * - * \param type This function does nothing unless type == SENSORS_ACTIVE - * \param c 0: disable the button, non-zero: enable - * \param key: One of BOARD_KEY_LEFT, BOARD_KEY_RIGHT etc - */ -static void -config_buttons(int type, int c, uint32_t key) -{ - switch(type) { - case SENSORS_HW_INIT: - ti_lib_gpio_clear_event_dio(key); - ti_lib_rom_ioc_pin_type_gpio_input(key); - ti_lib_rom_ioc_port_configure_set(key, IOC_PORT_GPIO, BUTTON_GPIO_CFG); - press_handler.pin_mask |= gpio_hal_pin_to_mask(key); - gpio_hal_register_handler(&press_handler); - break; - case SENSORS_ACTIVE: - if(c) { - ti_lib_gpio_clear_event_dio(key); - ti_lib_rom_ioc_pin_type_gpio_input(key); - ti_lib_rom_ioc_port_configure_set(key, IOC_PORT_GPIO, BUTTON_GPIO_CFG); - ti_lib_rom_ioc_int_enable(key); - } else { - ti_lib_rom_ioc_int_disable(key); - } - break; - default: - break; - } -} -/*---------------------------------------------------------------------------*/ -/** - * \brief Configuration function for the left button. - * - * Parameters are passed onto config_buttons, which does the actual - * configuration - * Parameters are ignored. They have been included because the prototype is - * dictated by the core sensor API. The return value is also required by - * the API but otherwise ignored. - * - * \param type passed to config_buttons as-is - * \param value passed to config_buttons as-is - * - * \return ignored - */ -static int -config_left(int type, int value) -{ - config_buttons(type, value, BOARD_IOID_KEY_LEFT); - - return 1; -} -/*---------------------------------------------------------------------------*/ -/** - * \brief Configuration function for the right button. - * - * Parameters are passed onto config_buttons, which does the actual - * configuration - * Parameters are ignored. They have been included because the prototype is - * dictated by the core sensor api. The return value is also required by - * the API but otherwise ignored. - * - * \param type passed to config_buttons as-is - * \param value passed to config_buttons as-is - * - * \return ignored - */ -static int -config_right(int type, int value) -{ - config_buttons(type, value, BOARD_IOID_KEY_RIGHT); - - return 1; -} -/*---------------------------------------------------------------------------*/ -/** - * \brief Status function for all buttons - * \param type SENSORS_ACTIVE or SENSORS_READY - * \param key_io_id BOARD_IOID_KEY_LEFT, BOARD_IOID_KEY_RIGHT etc - * \return 1 if the button's port interrupt is enabled (edge detect) - * - * This function will only be called by status_left, status_right and the - * called will pass the correct key_io_id - */ -static int -status(int type, uint32_t key_io_id) -{ - switch(type) { - case SENSORS_ACTIVE: - case SENSORS_READY: - if(ti_lib_ioc_port_configure_get(key_io_id) & IOC_INT_ENABLE) { - return 1; - } - break; - default: - break; - } - return 0; -} -/*---------------------------------------------------------------------------*/ -static int -value_left(int type) -{ - if(type == BUTTON_SENSOR_VALUE_STATE) { - return ti_lib_gpio_read_dio(BOARD_IOID_KEY_LEFT) == 0 ? - BUTTON_SENSOR_VALUE_PRESSED : BUTTON_SENSOR_VALUE_RELEASED; - } else if(type == BUTTON_SENSOR_VALUE_DURATION) { - return (int)left_timer.duration; - } - return 0; -} -/*---------------------------------------------------------------------------*/ -static int -value_right(int type) -{ - if(type == BUTTON_SENSOR_VALUE_STATE) { - return ti_lib_gpio_read_dio(BOARD_IOID_KEY_RIGHT) == 0 ? - BUTTON_SENSOR_VALUE_PRESSED : BUTTON_SENSOR_VALUE_RELEASED; - } else if(type == BUTTON_SENSOR_VALUE_DURATION) { - return (int)right_timer.duration; - } - return 0; -} -/*---------------------------------------------------------------------------*/ -/** - * \brief Status function for the left button. - * \param type SENSORS_ACTIVE or SENSORS_READY - * \return 1 if the button's port interrupt is enabled (edge detect) - * - * This function will call status. It will pass type verbatim and it will also - * pass the correct key_io_id - */ -static int -status_left(int type) -{ - return status(type, BOARD_IOID_KEY_LEFT); -} -/*---------------------------------------------------------------------------*/ -/** - * \brief Status function for the right button. - * \param type SENSORS_ACTIVE or SENSORS_READY - * \return 1 if the button's port interrupt is enabled (edge detect) - * - * This function will call status. It will pass type verbatim and it will also - * pass the correct key_io_id - */ -static int -status_right(int type) -{ - return status(type, BOARD_IOID_KEY_RIGHT); -} -/*---------------------------------------------------------------------------*/ -SENSORS_SENSOR(button_left_sensor, BUTTON_SENSOR, value_left, config_left, - status_left); -SENSORS_SENSOR(button_right_sensor, BUTTON_SENSOR, value_right, config_right, - status_right); -/*---------------------------------------------------------------------------*/ -/** @} */ diff --git a/arch/platform/srf06-cc26xx/sensortag/button-sensor.h b/arch/platform/srf06-cc26xx/sensortag/button-sensor.h deleted file mode 100644 index 1355a5855..000000000 --- a/arch/platform/srf06-cc26xx/sensortag/button-sensor.h +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright (c) 2014, Texas Instruments Incorporated - http://www.ti.com/ - * 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 copyright holder 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 COPYRIGHT HOLDERS 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 - * COPYRIGHT HOLDER 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. - */ -/*---------------------------------------------------------------------------*/ -/** - * \addtogroup sensortag-cc26xx-peripherals - * @{ - * - * \defgroup sensortag-cc26xx-button-sensor SensorTag 2.0 Button Sensor - * - * One of the buttons can be configured as general purpose or as an on/off key - * @{ - * - * \file - * Header file for the Sensortag Button Driver - */ -/*---------------------------------------------------------------------------*/ -#ifndef BUTTON_SENSOR_H_ -#define BUTTON_SENSOR_H_ -/*---------------------------------------------------------------------------*/ -#include "lib/sensors.h" -/*---------------------------------------------------------------------------*/ -#define BUTTON_SENSOR "Button" -/*---------------------------------------------------------------------------*/ -#define BUTTON_SENSOR_VALUE_STATE 0 -#define BUTTON_SENSOR_VALUE_DURATION 1 - -#define BUTTON_SENSOR_VALUE_RELEASED 0 -#define BUTTON_SENSOR_VALUE_PRESSED 1 -/*---------------------------------------------------------------------------*/ -extern const struct sensors_sensor button_left_sensor; -extern const struct sensors_sensor button_right_sensor; -/*---------------------------------------------------------------------------*/ -#endif /* BUTTON_SENSOR_H_ */ -/*---------------------------------------------------------------------------*/ -/** - * @} - * @} - */ diff --git a/arch/platform/srf06-cc26xx/sensortag/cc1350/board.h b/arch/platform/srf06-cc26xx/sensortag/cc1350/board.h index d9269c8ff..aa8719eea 100644 --- a/arch/platform/srf06-cc26xx/sensortag/cc1350/board.h +++ b/arch/platform/srf06-cc26xx/sensortag/cc1350/board.h @@ -238,6 +238,17 @@ #define SMARTRF_SETTINGS_CONF_OVERRIDE_TRIM_OFFSET 0x00018883 /** @} */ /*---------------------------------------------------------------------------*/ +/** + * \brief Board indices for the button HAL + * + * Those values are not meant to be modified by the user + * @{ + */ +#define BOARD_BUTTON_HAL_INDEX_KEY_LEFT 0x00 +#define BOARD_BUTTON_HAL_INDEX_KEY_RIGHT 0x01 +#define BOARD_BUTTON_HAL_INDEX_REED_RELAY 0xFF +/** @} */ +/*---------------------------------------------------------------------------*/ /** * \name Device string used on startup * @{ diff --git a/arch/platform/srf06-cc26xx/sensortag/cc2650/board.h b/arch/platform/srf06-cc26xx/sensortag/cc2650/board.h index 2794e633e..9543d2713 100644 --- a/arch/platform/srf06-cc26xx/sensortag/cc2650/board.h +++ b/arch/platform/srf06-cc26xx/sensortag/cc2650/board.h @@ -218,6 +218,17 @@ #define BOARD_IOID_AUDIO_CLK IOID_11 /** @} */ /*---------------------------------------------------------------------------*/ +/** + * \brief Board indices for the button HAL + * + * Those values are not meant to be modified by the user + * @{ + */ +#define BOARD_BUTTON_HAL_INDEX_KEY_LEFT 0x00 +#define BOARD_BUTTON_HAL_INDEX_KEY_RIGHT 0x01 +#define BOARD_BUTTON_HAL_INDEX_REED_RELAY 0xFF +/** @} */ +/*---------------------------------------------------------------------------*/ /** * \name Device string used on startup * @{ diff --git a/arch/platform/srf06-cc26xx/sensortag/reed-relay.c b/arch/platform/srf06-cc26xx/sensortag/reed-relay.c deleted file mode 100644 index 6cfb186ab..000000000 --- a/arch/platform/srf06-cc26xx/sensortag/reed-relay.c +++ /dev/null @@ -1,145 +0,0 @@ -/* - * Copyright (c) 2014, Texas Instruments Incorporated - http://www.ti.com/ - * 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 copyright holder 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 COPYRIGHT HOLDERS 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 - * COPYRIGHT HOLDER 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. - */ -/*---------------------------------------------------------------------------*/ -/** - * \addtogroup sensortag-cc26xx-reed-relay - * @{ - * - * \file - * Driver for the Sensortag Reed Relay - */ -/*---------------------------------------------------------------------------*/ -#include "contiki.h" -#include "sys/clock.h" -#include "sys/timer.h" -#include "dev/gpio-hal.h" -#include "lib/sensors.h" -#include "sensortag/reed-relay.h" -#include "sys/timer.h" - -#include "ti-lib.h" - -#include -/*---------------------------------------------------------------------------*/ -static struct timer debouncetimer; -/*---------------------------------------------------------------------------*/ -#define REED_IO_CFG (IOC_CURRENT_2MA | IOC_STRENGTH_AUTO | \ - IOC_IOPULL_DOWN | IOC_SLEW_DISABLE | \ - IOC_HYST_DISABLE | IOC_BOTH_EDGES | \ - IOC_INT_DISABLE | IOC_IOMODE_NORMAL | \ - IOC_NO_WAKE_UP | IOC_INPUT_ENABLE) -/*---------------------------------------------------------------------------*/ -/** - * \brief Handler for Sensortag-CC26XX reed interrupts - */ -static void -reed_interrupt_handler(gpio_hal_pin_mask_t pin_mask) -{ - if(!timer_expired(&debouncetimer)) { - return; - } - - sensors_changed(&reed_relay_sensor); - timer_set(&debouncetimer, CLOCK_SECOND / 2); -} -/*---------------------------------------------------------------------------*/ -static int -value(int type) -{ - return (int)ti_lib_gpio_read_dio(BOARD_IOID_REED_RELAY); -} -/*---------------------------------------------------------------------------*/ -static gpio_hal_event_handler_t event_handler = { - .next = NULL, - .handler = reed_interrupt_handler, - .pin_mask = gpio_hal_pin_to_mask(BOARD_IOID_REED_RELAY), -}; -/*---------------------------------------------------------------------------*/ -/** - * \brief Configuration function for the button sensor for all buttons. - * - * \param type SENSORS_HW_INIT: Initialise. SENSORS_ACTIVE: Enables/Disables - * depending on 'value' - * \param value 0: disable, non-zero: enable - * \return Always returns 1 - */ -static int -configure(int type, int value) -{ - switch(type) { - case SENSORS_HW_INIT: - ti_lib_ioc_int_disable(BOARD_IOID_REED_RELAY); - ti_lib_gpio_clear_event_dio(BOARD_IOID_REED_RELAY); - - /* Enable the GPIO clock when the CM3 is running */ - ti_lib_prcm_peripheral_run_enable(PRCM_PERIPH_GPIO); - - /* S/W control, input, pull-down */ - ti_lib_ioc_port_configure_set(BOARD_IOID_REED_RELAY, IOC_PORT_GPIO, - REED_IO_CFG); - - gpio_hal_register_handler(&event_handler); - break; - case SENSORS_ACTIVE: - if(value) { - ti_lib_ioc_int_enable(BOARD_IOID_REED_RELAY); - } else { - ti_lib_ioc_int_disable(BOARD_IOID_REED_RELAY); - } - break; - default: - break; - } - return 1; -} -/*---------------------------------------------------------------------------*/ -/** - * \brief Status function for the reed - * \param type SENSORS_ACTIVE or SENSORS_READY - * \return 1 Interrupt enabled, 0: Disabled - */ -static int -status(int type) -{ - switch(type) { - case SENSORS_ACTIVE: - case SENSORS_READY: - return (ti_lib_ioc_port_configure_get(BOARD_IOID_REED_RELAY) - & IOC_INT_ENABLE) == IOC_INT_ENABLE; - break; - default: - break; - } - return 0; -} -/*---------------------------------------------------------------------------*/ -SENSORS_SENSOR(reed_relay_sensor, "REED", value, configure, status); -/*---------------------------------------------------------------------------*/ -/** @} */ diff --git a/arch/platform/srf06-cc26xx/sensortag/sensortag-sensors.c b/arch/platform/srf06-cc26xx/sensortag/sensortag-sensors.c index 26d08591d..065ef6112 100644 --- a/arch/platform/srf06-cc26xx/sensortag/sensortag-sensors.c +++ b/arch/platform/srf06-cc26xx/sensortag/sensortag-sensors.c @@ -37,19 +37,16 @@ */ /*---------------------------------------------------------------------------*/ #include "contiki.h" -#include "sensortag/button-sensor.h" #include "sensortag/bmp-280-sensor.h" #include "sensortag/tmp-007-sensor.h" #include "sensortag/opt-3001-sensor.h" #include "sensortag/hdc-1000-sensor.h" #include "sensortag/mpu-9250-sensor.h" -#include "sensortag/reed-relay.h" #include /*---------------------------------------------------------------------------*/ /** \brief Exports a global symbol to be used by the sensor API */ -SENSORS(&button_left_sensor, &button_right_sensor, - &bmp_280_sensor, &tmp_007_sensor, &opt_3001_sensor, &hdc_1000_sensor, - &mpu_9250_sensor, &reed_relay_sensor); +SENSORS(&bmp_280_sensor, &tmp_007_sensor, &opt_3001_sensor, &hdc_1000_sensor, + &mpu_9250_sensor); /*---------------------------------------------------------------------------*/ /** @} */ From 1dcb8426d67fc406112b8f4ec6f6d11a2b3c9fb0 Mon Sep 17 00:00:00 2001 From: George Oikonomou Date: Mon, 18 Dec 2017 00:25:35 +0000 Subject: [PATCH 06/44] Use the button HAL for all CC13xx/CC26xx devices --- arch/platform/srf06-cc26xx/platform.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/arch/platform/srf06-cc26xx/platform.c b/arch/platform/srf06-cc26xx/platform.c index 4c6b9443e..54ca4c04a 100644 --- a/arch/platform/srf06-cc26xx/platform.c +++ b/arch/platform/srf06-cc26xx/platform.c @@ -68,6 +68,7 @@ #include "lib/sensors.h" #include "button-sensor.h" #include "dev/serial-line.h" +#include "dev/button-hal.h" #include "net/mac/framer/frame802154.h" #include "board-peripherals.h" @@ -195,6 +196,9 @@ platform_init_stage_two() #else ieee_addr_cpy_to(linkaddr_node_addr.u8, LINKADDR_SIZE); #endif + + button_hal_init(); + fade(LEDS_GREEN); } /*---------------------------------------------------------------------------*/ From 4fd90e1b6879f2c7771aeefcc428d5cb6cfffc94 Mon Sep 17 00:00:00 2001 From: George Oikonomou Date: Sun, 5 Nov 2017 17:07:00 +0000 Subject: [PATCH 07/44] Change the CC26xx demo to use the button HAL --- .../platform-specific/cc26xx/cc26xx-demo.c | 70 +++++++------------ .../platform-specific/cc26xx/project-conf.h | 3 - 2 files changed, 25 insertions(+), 48 deletions(-) diff --git a/examples/platform-specific/cc26xx/cc26xx-demo.c b/examples/platform-specific/cc26xx/cc26xx-demo.c index f5045a5bd..8c90905af 100644 --- a/examples/platform-specific/cc26xx/cc26xx-demo.c +++ b/examples/platform-specific/cc26xx/cc26xx-demo.c @@ -62,8 +62,8 @@ * - sensors : Some sensortag sensors are read asynchronously (see sensor * documentation). For those, this example will print out * readings in a staggered fashion at a random interval - * - Buttons : CC26XX_DEMO_SENSOR_1 button will toggle CC26XX_DEMO_LEDS_BUTTON - * - CC26XX_DEMO_SENSOR_2 turns on LEDS_REBOOT and causes a + * - Buttons : CC26XX_DEMO_TRIGGER_1 button will toggle CC26XX_DEMO_LEDS_BUTTON + * - CC26XX_DEMO_TRIGGER_2 turns on LEDS_REBOOT and causes a * watchdog reboot * - The remaining buttons will just print something * - The example also shows how to retrieve the duration of a @@ -81,6 +81,7 @@ #include "sys/ctimer.h" #include "dev/leds.h" #include "dev/watchdog.h" +#include "dev/button-hal.h" #include "random.h" #include "button-sensor.h" #include "batmon-sensor.h" @@ -97,23 +98,11 @@ #define CC26XX_DEMO_LEDS_BUTTON LEDS_RED #define CC26XX_DEMO_LEDS_REBOOT LEDS_ALL /*---------------------------------------------------------------------------*/ -#define CC26XX_DEMO_SENSOR_NONE (void *)0xFFFFFFFF - -#define CC26XX_DEMO_SENSOR_1 &button_left_sensor -#define CC26XX_DEMO_SENSOR_2 &button_right_sensor +#define CC26XX_DEMO_TRIGGER_1 BOARD_BUTTON_HAL_INDEX_KEY_LEFT +#define CC26XX_DEMO_TRIGGER_2 BOARD_BUTTON_HAL_INDEX_KEY_RIGHT #if BOARD_SENSORTAG -#define CC26XX_DEMO_SENSOR_3 CC26XX_DEMO_SENSOR_NONE -#define CC26XX_DEMO_SENSOR_4 CC26XX_DEMO_SENSOR_NONE -#define CC26XX_DEMO_SENSOR_5 &reed_relay_sensor -#elif BOARD_LAUNCHPAD -#define CC26XX_DEMO_SENSOR_3 CC26XX_DEMO_SENSOR_NONE -#define CC26XX_DEMO_SENSOR_4 CC26XX_DEMO_SENSOR_NONE -#define CC26XX_DEMO_SENSOR_5 CC26XX_DEMO_SENSOR_NONE -#else -#define CC26XX_DEMO_SENSOR_3 &button_up_sensor -#define CC26XX_DEMO_SENSOR_4 &button_down_sensor -#define CC26XX_DEMO_SENSOR_5 &button_select_sensor +#define CC26XX_DEMO_TRIGGER_3 BOARD_BUTTON_HAL_INDEX_REED_RELAY #endif /*---------------------------------------------------------------------------*/ static struct etimer et; @@ -343,10 +332,6 @@ get_sync_sensor_readings(void) static void init_sensors(void) { -#if BOARD_SENSORTAG - SENSORS_ACTIVATE(reed_relay_sensor); -#endif - SENSORS_ACTIVATE(batmon_sensor); } /*---------------------------------------------------------------------------*/ @@ -392,46 +377,41 @@ PROCESS_THREAD(cc26xx_demo_process, ev, data) etimer_set(&et, CC26XX_DEMO_LOOP_INTERVAL); } - } else if(ev == sensors_event) { - if(data == CC26XX_DEMO_SENSOR_1) { - printf("Left: Pin %d, press duration %d clock ticks\n", - (CC26XX_DEMO_SENSOR_1)->value(BUTTON_SENSOR_VALUE_STATE), - (CC26XX_DEMO_SENSOR_1)->value(BUTTON_SENSOR_VALUE_DURATION)); + } else if(ev == button_hal_periodic_event) { + button_hal_button_t *button = data; - if((CC26XX_DEMO_SENSOR_1)->value(BUTTON_SENSOR_VALUE_DURATION) > - CLOCK_SECOND) { - printf("Long button press!\n"); - } + printf("%s periodic event, duration %d seconds\n", + BUTTON_HAL_GET_DESCRIPTION(button), + button->press_duration_seconds); + } else if(ev == button_hal_release_event) { + button_hal_button_t *btn = (button_hal_button_t *)data; + printf("%s release event\n", BUTTON_HAL_GET_DESCRIPTION(btn)); + + if(btn->unique_id== CC26XX_DEMO_TRIGGER_1) { leds_toggle(CC26XX_DEMO_LEDS_BUTTON); - } else if(data == CC26XX_DEMO_SENSOR_2) { + } else if(btn->unique_id == CC26XX_DEMO_TRIGGER_2) { leds_on(CC26XX_DEMO_LEDS_REBOOT); watchdog_reboot(); - } else if(data == CC26XX_DEMO_SENSOR_3) { - printf("Up\n"); - } else if(data == CC26XX_DEMO_SENSOR_4) { - printf("Down\n"); - } else if(data == CC26XX_DEMO_SENSOR_5) { #if BOARD_SENSORTAG + } else if(btn->unique_id == CC26XX_DEMO_TRIGGER_3) { if(buzzer_state()) { buzzer_stop(); } else { buzzer_start(1000); } - } else if(ev == sensors_event && data == &bmp_280_sensor) { + } + } else if(ev == sensors_event) { + if(data == &bmp_280_sensor) { get_bmp_reading(); - } else if(ev == sensors_event && data == &opt_3001_sensor) { + } else if(data == &opt_3001_sensor) { get_light_reading(); - } else if(ev == sensors_event && data == &hdc_1000_sensor) { + } else if(data == &hdc_1000_sensor) { get_hdc_reading(); - } else if(ev == sensors_event && data == &tmp_007_sensor) { + } else if(data == &tmp_007_sensor) { get_tmp_reading(); - } else if(ev == sensors_event && data == &mpu_9250_sensor) { + } else if(data == &mpu_9250_sensor) { get_mpu_reading(); -#elif BOARD_SMARTRF06EB - printf("Sel: Pin %d, press duration %d clock ticks\n", - button_select_sensor.value(BUTTON_SENSOR_VALUE_STATE), - button_select_sensor.value(BUTTON_SENSOR_VALUE_DURATION)); #endif } } diff --git a/examples/platform-specific/cc26xx/project-conf.h b/examples/platform-specific/cc26xx/project-conf.h index fc2d23ab1..8231dd4ea 100644 --- a/examples/platform-specific/cc26xx/project-conf.h +++ b/examples/platform-specific/cc26xx/project-conf.h @@ -31,9 +31,6 @@ #ifndef PROJECT_CONF_H_ #define PROJECT_CONF_H_ /*---------------------------------------------------------------------------*/ -/* Disable button shutdown functionality */ -#define BUTTON_SENSOR_CONF_ENABLE_SHUTDOWN 0 -/*---------------------------------------------------------------------------*/ /* Enable the ROM bootloader */ #define ROM_BOOTLOADER_ENABLE 1 /*---------------------------------------------------------------------------*/ From 70dc73ff9d66e2c7d411c901ea398b95f4229487 Mon Sep 17 00:00:00 2001 From: George Oikonomou Date: Sun, 5 Nov 2017 19:47:26 +0000 Subject: [PATCH 08/44] Change the CC26xx very sleepy demo to use the button HAL --- .../cc26xx/very-sleepy-demo/very-sleepy-demo.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/examples/platform-specific/cc26xx/very-sleepy-demo/very-sleepy-demo.c b/examples/platform-specific/cc26xx/very-sleepy-demo/very-sleepy-demo.c index e24aac689..cac8033b7 100644 --- a/examples/platform-specific/cc26xx/very-sleepy-demo/very-sleepy-demo.c +++ b/examples/platform-specific/cc26xx/very-sleepy-demo/very-sleepy-demo.c @@ -34,7 +34,7 @@ #include "sys/process.h" #include "dev/leds.h" #include "dev/watchdog.h" -#include "button-sensor.h" +#include "dev/button-hal.h" #include "batmon-sensor.h" #include "board-peripherals.h" #include "net/netstack.h" @@ -63,6 +63,8 @@ #define VERY_SLEEPY_MODE_OFF 0 #define VERY_SLEEPY_MODE_ON 1 /*---------------------------------------------------------------------------*/ +#define BUTTON_TRIGGER BOARD_BUTTON_HAL_INDEX_KEY_LEFT +/*---------------------------------------------------------------------------*/ #define MAC_CAN_BE_TURNED_OFF 0 #define MAC_MUST_STAY_ON 1 @@ -358,7 +360,8 @@ PROCESS_THREAD(very_sleepy_demo_process, ev, data) PROCESS_YIELD(); - if(ev == sensors_event && data == &button_left_sensor) { + if(ev == button_hal_release_event && + ((button_hal_button_t *)data)->unique_id == BUTTON_TRIGGER) { switch_to_normal(); } @@ -368,7 +371,8 @@ PROCESS_THREAD(very_sleepy_demo_process, ev, data) } if((ev == PROCESS_EVENT_TIMER && data == &et_periodic) || - (ev == sensors_event && data == &button_left_sensor) || + (ev == button_hal_release_event && + ((button_hal_button_t *)data)->unique_id == BUTTON_TRIGGER) || (ev == event_new_config)) { /* From 5f190be6c9bc200b475838428af3e07ad6005635 Mon Sep 17 00:00:00 2001 From: George Oikonomou Date: Sun, 5 Nov 2017 20:14:34 +0000 Subject: [PATCH 09/44] Change the CC26xx web demo to use the button HAL --- .../cc26xx/cc26xx-web-demo/cc26xx-web-demo.c | 24 +++++++++---------- .../cc26xx/cc26xx-web-demo/cc26xx-web-demo.h | 8 +++---- .../cc26xx/cc26xx-web-demo/mqtt-client.c | 18 +++++++++----- 3 files changed, 27 insertions(+), 23 deletions(-) diff --git a/examples/platform-specific/cc26xx/cc26xx-web-demo/cc26xx-web-demo.c b/examples/platform-specific/cc26xx/cc26xx-web-demo/cc26xx-web-demo.c index ebbe66c29..306b54640 100644 --- a/examples/platform-specific/cc26xx/cc26xx-web-demo/cc26xx-web-demo.c +++ b/examples/platform-specific/cc26xx/cc26xx-web-demo/cc26xx-web-demo.c @@ -45,7 +45,7 @@ #include "lib/list.h" #include "sys/process.h" #include "net/ipv6/sicslowpan.h" -#include "button-sensor.h" +#include "dev/button-hal.h" #include "batmon-sensor.h" #include "httpd-simple.h" #include "cc26xx-web-demo.h" @@ -882,8 +882,6 @@ init_sensors(void) list_add(sensor_list, &mpu_gyro_x_reading); list_add(sensor_list, &mpu_gyro_y_reading); list_add(sensor_list, &mpu_gyro_z_reading); - - SENSORS_ACTIVATE(reed_relay_sensor); #endif } /*---------------------------------------------------------------------------*/ @@ -977,16 +975,16 @@ PROCESS_THREAD(cc26xx_web_demo_process, ev, data) } #endif - if(ev == sensors_event && data == CC26XX_WEB_DEMO_SENSOR_READING_TRIGGER) { - if((CC26XX_WEB_DEMO_SENSOR_READING_TRIGGER)->value( - BUTTON_SENSOR_VALUE_DURATION) > CLOCK_SECOND * 5) { - printf("Restoring defaults!\n"); - cc26xx_web_demo_restore_defaults(); - } else { - init_sensor_readings(); - - process_post(PROCESS_BROADCAST, cc26xx_web_demo_publish_event, NULL); - } + if(ev == button_hal_release_event && + ((button_hal_button_t *)data)->unique_id == + CC26XX_WEB_DEMO_SENSOR_READING_TRIGGER) { + init_sensor_readings(); + process_post(PROCESS_BROADCAST, cc26xx_web_demo_publish_event, NULL); + } else if(ev == button_hal_periodic_event && + ((button_hal_button_t *)data)->unique_id == + CC26XX_WEB_DEMO_SENSOR_READING_TRIGGER) { + printf("Restoring defaults!\n"); + cc26xx_web_demo_restore_defaults(); } else if(ev == httpd_simple_event_new_config) { save_config(); #if BOARD_SENSORTAG diff --git a/examples/platform-specific/cc26xx/cc26xx-web-demo/cc26xx-web-demo.h b/examples/platform-specific/cc26xx/cc26xx-web-demo/cc26xx-web-demo.h index e40cd1864..0761725c7 100644 --- a/examples/platform-specific/cc26xx/cc26xx-web-demo/cc26xx-web-demo.h +++ b/examples/platform-specific/cc26xx/cc26xx-web-demo/cc26xx-web-demo.h @@ -98,18 +98,18 @@ /*---------------------------------------------------------------------------*/ /* User configuration */ /* Take a sensor reading on button press */ -#define CC26XX_WEB_DEMO_SENSOR_READING_TRIGGER &button_left_sensor +#define CC26XX_WEB_DEMO_SENSOR_READING_TRIGGER BOARD_BUTTON_HAL_INDEX_KEY_LEFT /* Payload length of ICMPv6 echo requests used to measure RSSI with def rt */ #define CC26XX_WEB_DEMO_ECHO_REQ_PAYLOAD_LEN 20 #if BOARD_SENSORTAG /* Force an MQTT publish on sensor event */ -#define CC26XX_WEB_DEMO_MQTT_PUBLISH_TRIGGER &reed_relay_sensor +#define CC26XX_WEB_DEMO_MQTT_PUBLISH_TRIGGER BOARD_BUTTON_HAL_INDEX_REED_RELAY #elif BOARD_LAUNCHPAD -#define CC26XX_WEB_DEMO_MQTT_PUBLISH_TRIGGER &button_left_sensor +#define CC26XX_WEB_DEMO_MQTT_PUBLISH_TRIGGER BOARD_BUTTON_HAL_INDEX_KEY_LEFT #else -#define CC26XX_WEB_DEMO_MQTT_PUBLISH_TRIGGER &button_down_sensor +#define CC26XX_WEB_DEMO_MQTT_PUBLISH_TRIGGER BOARD_BUTTON_HAL_INDEX_KEY_DOWN #endif #define CC26XX_WEB_DEMO_STATUS_LED LEDS_GREEN diff --git a/examples/platform-specific/cc26xx/cc26xx-web-demo/mqtt-client.c b/examples/platform-specific/cc26xx/cc26xx-web-demo/mqtt-client.c index 7eb4ab5ee..7562fb89a 100644 --- a/examples/platform-specific/cc26xx/cc26xx-web-demo/mqtt-client.c +++ b/examples/platform-specific/cc26xx/cc26xx-web-demo/mqtt-client.c @@ -43,7 +43,7 @@ #include "sys/etimer.h" #include "sys/ctimer.h" #include "lib/sensors.h" -#include "button-sensor.h" +#include "dev/button-hal.h" #include "board-peripherals.h" #include "cc26xx-web-demo.h" #include "dev/leds.h" @@ -882,10 +882,14 @@ PROCESS_THREAD(mqtt_client_process, ev, data) PROCESS_YIELD(); - if(ev == sensors_event && data == CC26XX_WEB_DEMO_MQTT_PUBLISH_TRIGGER) { - if(state == MQTT_CLIENT_STATE_ERROR) { - connect_attempt = 1; - state = MQTT_CLIENT_STATE_REGISTERED; + if(ev == button_hal_release_event) { + button_hal_button_t *btn = (button_hal_button_t *)data; + + if(btn->unique_id == CC26XX_WEB_DEMO_MQTT_PUBLISH_TRIGGER) { + if(state == MQTT_CLIENT_STATE_ERROR) { + connect_attempt = 1; + state = MQTT_CLIENT_STATE_REGISTERED; + } } } @@ -901,7 +905,9 @@ PROCESS_THREAD(mqtt_client_process, ev, data) if((ev == PROCESS_EVENT_TIMER && data == &publish_periodic_timer) || ev == PROCESS_EVENT_POLL || ev == cc26xx_web_demo_publish_event || - (ev == sensors_event && data == CC26XX_WEB_DEMO_MQTT_PUBLISH_TRIGGER)) { + (ev == button_hal_release_event && + ((button_hal_button_t *)data)->unique_id == + CC26XX_WEB_DEMO_MQTT_PUBLISH_TRIGGER)) { state_machine(); } From 7bf667980611945b72188f96255538f6ae1d4888 Mon Sep 17 00:00:00 2001 From: George Oikonomou Date: Sun, 11 Mar 2018 23:29:57 +0000 Subject: [PATCH 10/44] Allow the GPIO HAL to enable corresponding port interrupt on the NVIC --- arch/cpu/cc2538/dev/gpio-hal-arch.c | 1 + arch/cpu/cc2538/dev/gpio-hal-arch.h | 6 ++++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/arch/cpu/cc2538/dev/gpio-hal-arch.c b/arch/cpu/cc2538/dev/gpio-hal-arch.c index dd8e5c513..bd2dfb92d 100644 --- a/arch/cpu/cc2538/dev/gpio-hal-arch.c +++ b/arch/cpu/cc2538/dev/gpio-hal-arch.c @@ -87,6 +87,7 @@ gpio_hal_arch_pin_cfg_set(gpio_hal_pin_t pin, gpio_hal_pin_cfg_t cfg) GPIO_DISABLE_INTERRUPT(port_base, pin_mask); } else if(tmp == GPIO_HAL_PIN_CFG_INT_ENABLE) { GPIO_ENABLE_INTERRUPT(port_base, pin_mask); + NVIC_EnableIRQ(port); } GPIO_SOFTWARE_CONTROL(port_base, pin_mask); diff --git a/arch/cpu/cc2538/dev/gpio-hal-arch.h b/arch/cpu/cc2538/dev/gpio-hal-arch.h index 78842634a..0b282c738 100644 --- a/arch/cpu/cc2538/dev/gpio-hal-arch.h +++ b/arch/cpu/cc2538/dev/gpio-hal-arch.h @@ -55,8 +55,10 @@ #define PIN_TO_PORT(pin) (pin >> 3) #define PIN_TO_PORT_BASE(pin) GPIO_PORT_TO_BASE(PIN_TO_PORT(pin)) /*---------------------------------------------------------------------------*/ -#define gpio_hal_arch_interrupt_enable(p) \ - GPIO_ENABLE_INTERRUPT(PIN_TO_PORT_BASE(p), GPIO_PIN_MASK((p) % 8)) +#define gpio_hal_arch_interrupt_enable(p) do { \ + GPIO_ENABLE_INTERRUPT(PIN_TO_PORT_BASE(p), GPIO_PIN_MASK((p) % 8)); \ + NVIC_EnableIRQ(PIN_TO_PORT(p)); \ +} while(0); #define gpio_hal_arch_interrupt_disable(p) \ GPIO_DISABLE_INTERRUPT(PIN_TO_PORT_BASE(p), GPIO_PIN_MASK((p) % 8)) From 974a7549e932c40a6cf6fc9e5726333a64dbfbf5 Mon Sep 17 00:00:00 2001 From: George Oikonomou Date: Sun, 5 Nov 2017 18:33:05 +0000 Subject: [PATCH 11/44] Use the new button HAL: CC2538DK --- arch/platform/cc2538dk/Makefile.cc2538dk | 2 +- arch/platform/cc2538dk/dev/board-buttons.c | 69 +++++ arch/platform/cc2538dk/dev/button-sensor.c | 265 ------------------- arch/platform/cc2538dk/dev/button-sensor.h | 66 ----- arch/platform/cc2538dk/dev/smartrf-sensors.c | 5 +- arch/platform/cc2538dk/platform.c | 7 +- 6 files changed, 75 insertions(+), 339 deletions(-) create mode 100644 arch/platform/cc2538dk/dev/board-buttons.c delete mode 100644 arch/platform/cc2538dk/dev/button-sensor.c delete mode 100644 arch/platform/cc2538dk/dev/button-sensor.h diff --git a/arch/platform/cc2538dk/Makefile.cc2538dk b/arch/platform/cc2538dk/Makefile.cc2538dk index e7be95c28..2b88479d2 100644 --- a/arch/platform/cc2538dk/Makefile.cc2538dk +++ b/arch/platform/cc2538dk/Makefile.cc2538dk @@ -9,7 +9,7 @@ CONTIKI_TARGET_DIRS = . dev CONTIKI_TARGET_SOURCEFILES += leds-arch.c CONTIKI_TARGET_SOURCEFILES += platform.c CONTIKI_TARGET_SOURCEFILES += sensors.c smartrf-sensors.c -CONTIKI_TARGET_SOURCEFILES += button-sensor.c als-sensor.c +CONTIKI_TARGET_SOURCEFILES += board-buttons.c als-sensor.c CONTIKI_SOURCEFILES += $(CONTIKI_TARGET_SOURCEFILES) diff --git a/arch/platform/cc2538dk/dev/board-buttons.c b/arch/platform/cc2538dk/dev/board-buttons.c new file mode 100644 index 000000000..6afc915df --- /dev/null +++ b/arch/platform/cc2538dk/dev/board-buttons.c @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2017, George Oikonomou - http://www.spd.gr + * 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 copyright holder 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 COPYRIGHT HOLDERS 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 + * COPYRIGHT HOLDER 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. + */ +/*---------------------------------------------------------------------------*/ +/** + * \addtogroup cc2538-smartrf + * @{ + * + * \defgroup cc2538-smartrf-buttons SmartRF06EB Buttons + * + * Generic module controlling buttons on the SmartRF06EB + * @{ + * + * \file + * Defines SmartRF06EB buttons for use with the button HAL + */ +/*---------------------------------------------------------------------------*/ +#include "contiki.h" +#include "dev/button-hal.h" +/*---------------------------------------------------------------------------*/ +BUTTON_HAL_BUTTON(key_left, "Key Left", \ + GPIO_PORT_PIN_TO_GPIO_HAL_PIN(BUTTON_LEFT_PORT, BUTTON_LEFT_PIN), \ + GPIO_HAL_PIN_CFG_PULL_UP, BUTTON_HAL_ID_BUTTON_ZERO, true); +BUTTON_HAL_BUTTON(key_right, "Key Right", \ + GPIO_PORT_PIN_TO_GPIO_HAL_PIN(BUTTON_RIGHT_PORT, BUTTON_RIGHT_PIN), \ + GPIO_HAL_PIN_CFG_PULL_UP, BUTTON_HAL_ID_BUTTON_ONE, true); +BUTTON_HAL_BUTTON(key_up, "Key Up", \ + GPIO_PORT_PIN_TO_GPIO_HAL_PIN(BUTTON_UP_PORT, BUTTON_UP_PIN), \ + GPIO_HAL_PIN_CFG_PULL_UP, BUTTON_HAL_ID_BUTTON_TWO, true); +BUTTON_HAL_BUTTON(key_down, "Key Down", \ + GPIO_PORT_PIN_TO_GPIO_HAL_PIN(BUTTON_DOWN_PORT, BUTTON_DOWN_PIN), \ + GPIO_HAL_PIN_CFG_PULL_UP, BUTTON_HAL_ID_BUTTON_THREE, true); +BUTTON_HAL_BUTTON(key_select, "Key Select", \ + GPIO_PORT_PIN_TO_GPIO_HAL_PIN(BUTTON_SELECT_PORT, BUTTON_SELECT_PIN), \ + GPIO_HAL_PIN_CFG_PULL_UP, BUTTON_HAL_ID_BUTTON_FOUR, true); +/*---------------------------------------------------------------------------*/ +BUTTON_HAL_BUTTONS(&key_left, &key_right, &key_up, &key_down, &key_select); +/*---------------------------------------------------------------------------*/ +/** + * @} + * @} + */ diff --git a/arch/platform/cc2538dk/dev/button-sensor.c b/arch/platform/cc2538dk/dev/button-sensor.c deleted file mode 100644 index 6c34bb847..000000000 --- a/arch/platform/cc2538dk/dev/button-sensor.c +++ /dev/null @@ -1,265 +0,0 @@ -/* - * Copyright (c) 2012, Texas Instruments Incorporated - http://www.ti.com/ - * 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 copyright holder 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 COPYRIGHT HOLDERS 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 - * COPYRIGHT HOLDER 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. - */ -/** - * \addtogroup cc2538dk-button-sensor - * @{ - * - * \file - * Driver for the SmartRF06EB buttons - */ -#include "contiki.h" -#include "dev/nvic.h" -#include "dev/ioc.h" -#include "dev/gpio.h" -#include "dev/button-sensor.h" -#include "sys/timer.h" - -#include -#include - -#define BUTTON_SELECT_PORT_BASE GPIO_PORT_TO_BASE(BUTTON_SELECT_PORT) -#define BUTTON_SELECT_PIN_MASK GPIO_PIN_MASK(BUTTON_SELECT_PIN) - -#define BUTTON_LEFT_PORT_BASE GPIO_PORT_TO_BASE(BUTTON_LEFT_PORT) -#define BUTTON_LEFT_PIN_MASK GPIO_PIN_MASK(BUTTON_LEFT_PIN) - -#define BUTTON_RIGHT_PORT_BASE GPIO_PORT_TO_BASE(BUTTON_RIGHT_PORT) -#define BUTTON_RIGHT_PIN_MASK GPIO_PIN_MASK(BUTTON_RIGHT_PIN) - -#define BUTTON_UP_PORT_BASE GPIO_PORT_TO_BASE(BUTTON_UP_PORT) -#define BUTTON_UP_PIN_MASK GPIO_PIN_MASK(BUTTON_UP_PIN) - -#define BUTTON_DOWN_PORT_BASE GPIO_PORT_TO_BASE(BUTTON_DOWN_PORT) -#define BUTTON_DOWN_PIN_MASK GPIO_PIN_MASK(BUTTON_DOWN_PIN) -/*---------------------------------------------------------------------------*/ -static struct timer debouncetimer; -/*---------------------------------------------------------------------------*/ -/** - * \brief Common initialiser for all buttons - * \param port_base GPIO port's register offset - * \param pin_mask Pin mask corresponding to the button's pin - */ -static void -config(uint32_t port_base, uint32_t pin_mask) -{ - /* Software controlled */ - GPIO_SOFTWARE_CONTROL(port_base, pin_mask); - - /* Set pin to input */ - GPIO_SET_INPUT(port_base, pin_mask); - - /* Enable edge detection */ - GPIO_DETECT_EDGE(port_base, pin_mask); - - /* Single edge */ - GPIO_TRIGGER_SINGLE_EDGE(port_base, pin_mask); - - /* Trigger interrupt on Falling edge */ - GPIO_DETECT_RISING(port_base, pin_mask); - - GPIO_ENABLE_INTERRUPT(port_base, pin_mask); -} -/*---------------------------------------------------------------------------*/ -static void -button_press_handler(gpio_hal_pin_mask_t pin_mask) -{ - if(!timer_expired(&debouncetimer)) { - return; - } - - timer_set(&debouncetimer, CLOCK_SECOND / 8); - - if(pin_mask & - (gpio_hal_pin_to_mask(BUTTON_SELECT_PIN) << (BUTTON_SELECT_PORT << 3))) { - sensors_changed(&button_select_sensor); - } else if(pin_mask & - (gpio_hal_pin_to_mask(BUTTON_LEFT_PIN) << (BUTTON_LEFT_PORT << 3))) { - sensors_changed(&button_left_sensor); - } else if(pin_mask & - (gpio_hal_pin_to_mask(BUTTON_RIGHT_PIN) << (BUTTON_RIGHT_PORT << 3))) { - sensors_changed(&button_right_sensor); - } else if(pin_mask & - (gpio_hal_pin_to_mask(BUTTON_UP_PIN) << (BUTTON_UP_PORT << 3))) { - sensors_changed(&button_up_sensor); - } else if(pin_mask & - (gpio_hal_pin_to_mask(BUTTON_DOWN_PIN) << (BUTTON_DOWN_PORT << 3))) { - sensors_changed(&button_down_sensor); - } -} -/*---------------------------------------------------------------------------*/ -static gpio_hal_event_handler_t press_handler = { - .next = NULL, - .handler = button_press_handler, - .pin_mask = 0, -}; -/*---------------------------------------------------------------------------*/ -static void -register_btn_callback(uint8_t port_num, uint8_t pin) -{ - press_handler.pin_mask |= - gpio_hal_pin_to_mask(pin) << (port_num << 3); - - gpio_hal_register_handler(&press_handler); -} -/*---------------------------------------------------------------------------*/ -/** - * \brief Init function for the select button. - * - * Parameters are ignored. They have been included because the prototype is - * dictated by the core sensor api. The return value is also not required by - * the API but otherwise ignored. - * - * \param type ignored - * \param value ignored - * \return ignored - */ -static int -config_select(int type, int value) -{ - config(BUTTON_SELECT_PORT_BASE, BUTTON_SELECT_PIN_MASK); - - ioc_set_over(BUTTON_SELECT_PORT, BUTTON_SELECT_PIN, IOC_OVERRIDE_PUE); - - NVIC_EnableIRQ(BUTTON_SELECT_VECTOR); - - register_btn_callback(BUTTON_SELECT_PORT, BUTTON_SELECT_PIN); - return 1; -} -/*---------------------------------------------------------------------------*/ -/** - * \brief Init function for the left button. - * - * Parameters are ignored. They have been included because the prototype is - * dictated by the core sensor api. The return value is also not required by - * the API but otherwise ignored. - * - * \param type ignored - * \param value ignored - * \return ignored - */ -static int -config_left(int type, int value) -{ - config(BUTTON_LEFT_PORT_BASE, BUTTON_LEFT_PIN_MASK); - - ioc_set_over(BUTTON_LEFT_PORT, BUTTON_LEFT_PIN, IOC_OVERRIDE_PUE); - - NVIC_EnableIRQ(BUTTON_LEFT_VECTOR); - - register_btn_callback(BUTTON_LEFT_PORT, BUTTON_LEFT_PIN); - return 1; -} -/*---------------------------------------------------------------------------*/ -/** - * \brief Init function for the right button. - * - * Parameters are ignored. They have been included because the prototype is - * dictated by the core sensor api. The return value is also not required by - * the API but otherwise ignored. - * - * \param type ignored - * \param value ignored - * \return ignored - */ -static int -config_right(int type, int value) -{ - config(BUTTON_RIGHT_PORT_BASE, BUTTON_RIGHT_PIN_MASK); - - ioc_set_over(BUTTON_RIGHT_PORT, BUTTON_RIGHT_PIN, IOC_OVERRIDE_PUE); - - NVIC_EnableIRQ(BUTTON_RIGHT_VECTOR); - - register_btn_callback(BUTTON_RIGHT_PORT, BUTTON_RIGHT_PIN); - return 1; -} -/*---------------------------------------------------------------------------*/ -/** - * \brief Init function for the up button. - * - * Parameters are ignored. They have been included because the prototype is - * dictated by the core sensor api. The return value is also not required by - * the API but otherwise ignored. - * - * \param type ignored - * \param value ignored - * \return ignored - */ -static int -config_up(int type, int value) -{ - config(BUTTON_UP_PORT_BASE, BUTTON_UP_PIN_MASK); - - ioc_set_over(BUTTON_UP_PORT, BUTTON_UP_PIN, IOC_OVERRIDE_PUE); - - NVIC_EnableIRQ(BUTTON_UP_VECTOR); - - register_btn_callback(BUTTON_UP_PORT, BUTTON_UP_PIN); - return 1; -} -/*---------------------------------------------------------------------------*/ -/** - * \brief Init function for the down button. - * - * Parameters are ignored. They have been included because the prototype is - * dictated by the core sensor api. The return value is also not required by - * the API but otherwise ignored. - * - * \param type ignored - * \param value ignored - * \return ignored - */ -static int -config_down(int type, int value) -{ - config(BUTTON_DOWN_PORT_BASE, BUTTON_DOWN_PIN_MASK); - - ioc_set_over(BUTTON_DOWN_PORT, BUTTON_DOWN_PIN, IOC_OVERRIDE_PUE); - - NVIC_EnableIRQ(BUTTON_DOWN_VECTOR); - - register_btn_callback(BUTTON_DOWN_PORT, BUTTON_DOWN_PIN); - return 1; -} -/*---------------------------------------------------------------------------*/ -void -button_sensor_init() -{ - timer_set(&debouncetimer, 0); -} -/*---------------------------------------------------------------------------*/ -SENSORS_SENSOR(button_select_sensor, BUTTON_SENSOR, NULL, config_select, NULL); -SENSORS_SENSOR(button_left_sensor, BUTTON_SENSOR, NULL, config_left, NULL); -SENSORS_SENSOR(button_right_sensor, BUTTON_SENSOR, NULL, config_right, NULL); -SENSORS_SENSOR(button_up_sensor, BUTTON_SENSOR, NULL, config_up, NULL); -SENSORS_SENSOR(button_down_sensor, BUTTON_SENSOR, NULL, config_down, NULL); - -/** @} */ diff --git a/arch/platform/cc2538dk/dev/button-sensor.h b/arch/platform/cc2538dk/dev/button-sensor.h deleted file mode 100644 index 266770d24..000000000 --- a/arch/platform/cc2538dk/dev/button-sensor.h +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright (c) 2012, Texas Instruments Incorporated - http://www.ti.com/ - * 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 copyright holder 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 COPYRIGHT HOLDERS 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 - * COPYRIGHT HOLDER 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. - */ -/** - * \addtogroup cc2538-smartrf-sensors - * @{ - * - * \defgroup cc2538dk-button-sensor cc2538dk Button Driver - * - * Driver for the SmartRF06EB buttons - * @{ - * - * \file - * Header file for the cc2538dk Button Driver - */ -#ifndef BUTTON_SENSOR_H_ -#define BUTTON_SENSOR_H_ - -#include "lib/sensors.h" -#include "dev/gpio.h" - -#define BUTTON_SENSOR "Button" - -#define button_sensor button_select_sensor -extern const struct sensors_sensor button_select_sensor; -extern const struct sensors_sensor button_left_sensor; -extern const struct sensors_sensor button_right_sensor; -extern const struct sensors_sensor button_up_sensor; -extern const struct sensors_sensor button_down_sensor; -/*---------------------------------------------------------------------------*/ -#endif /* BUTTON_SENSOR_H_ */ - -/** \brief Common initialiser for all SmartRF Buttons */ -void button_sensor_init(); - -/** - * @} - * @} - */ diff --git a/arch/platform/cc2538dk/dev/smartrf-sensors.c b/arch/platform/cc2538dk/dev/smartrf-sensors.c index 76841af3b..1b833e764 100644 --- a/arch/platform/cc2538dk/dev/smartrf-sensors.c +++ b/arch/platform/cc2538dk/dev/smartrf-sensors.c @@ -41,16 +41,13 @@ * Implementation of a generic module controlling SmartRF06EB sensors */ #include "contiki.h" -#include "dev/button-sensor.h" #include "dev/als-sensor.h" #include "dev/cc2538-sensors.h" #include /** \brief Exports a global symbol to be used by the sensor API */ -SENSORS(&button_select_sensor, &button_left_sensor, &button_right_sensor, - &button_up_sensor, &button_down_sensor, &als_sensor, - &cc2538_temp_sensor, &vdd3_sensor); +SENSORS(&als_sensor, &cc2538_temp_sensor, &vdd3_sensor); /** * @} diff --git a/arch/platform/cc2538dk/platform.c b/arch/platform/cc2538dk/platform.c index 8add14292..bf945bfdd 100644 --- a/arch/platform/cc2538dk/platform.c +++ b/arch/platform/cc2538dk/platform.c @@ -46,14 +46,15 @@ #include "dev/adc.h" #include "dev/leds.h" #include "dev/uart.h" -#include "dev/button-sensor.h" #include "dev/serial-line.h" #include "dev/slip.h" #include "dev/cc2538-rf.h" #include "dev/udma.h" #include "dev/crypto.h" +#include "dev/button-hal.h" #include "usb/usb-serial.h" #include "lib/random.h" +#include "lib/sensors.h" #include "net/netstack.h" #include "net/mac/framer/frame802154.h" #include "net/linkaddr.h" @@ -121,8 +122,6 @@ platform_init_stage_one(void) void platform_init_stage_two() { - button_sensor_init(); - /* * Character I/O Initialisation. * When the UART receives a character it will call serial_line_input_byte to @@ -159,6 +158,8 @@ platform_init_stage_two() /* Populate linkaddr_node_addr */ ieee_addr_cpy_to(linkaddr_node_addr.u8, LINKADDR_SIZE); + button_hal_init(); + INTERRUPTS_ENABLE(); fade(LEDS_GREEN); From d22ea1df9991aad79b065e62889e01c71d011464 Mon Sep 17 00:00:00 2001 From: George Oikonomou Date: Sun, 5 Nov 2017 18:46:35 +0000 Subject: [PATCH 12/44] Use the new button HAL: OpenMote-CC2538 --- .../openmote-cc2538/Makefile.openmote-cc2538 | 2 +- .../dev/{button-sensor.h => board-buttons.c} | 39 ++-- .../openmote-cc2538/dev/button-sensor.c | 178 ------------------ .../openmote-cc2538/dev/openmote-sensors.c | 2 +- .../openmote-cc2538/dev/openmote-sensors.h | 1 - arch/platform/openmote-cc2538/platform.c | 6 +- 6 files changed, 17 insertions(+), 211 deletions(-) rename arch/platform/openmote-cc2538/dev/{button-sensor.h => board-buttons.c} (65%) delete mode 100644 arch/platform/openmote-cc2538/dev/button-sensor.c diff --git a/arch/platform/openmote-cc2538/Makefile.openmote-cc2538 b/arch/platform/openmote-cc2538/Makefile.openmote-cc2538 index 37967fdde..14aa46f78 100644 --- a/arch/platform/openmote-cc2538/Makefile.openmote-cc2538 +++ b/arch/platform/openmote-cc2538/Makefile.openmote-cc2538 @@ -15,7 +15,7 @@ PLATFORM_ROOT_DIR = $(CONTIKI)/arch/platform/$(TARGET) ### Include CONTIKI_TARGET_SOURCEFILES += platform.c board.c -CONTIKI_TARGET_SOURCEFILES += leds-arch.c button-sensor.c openmote-sensors.c +CONTIKI_TARGET_SOURCEFILES += leds-arch.c board-buttons.c openmote-sensors.c CONTIKI_TARGET_SOURCEFILES += antenna.c adxl346.c max44009.c sht21.c tps62730.c CONTIKI_SOURCEFILES += $(CONTIKI_TARGET_SOURCEFILES) diff --git a/arch/platform/openmote-cc2538/dev/button-sensor.h b/arch/platform/openmote-cc2538/dev/board-buttons.c similarity index 65% rename from arch/platform/openmote-cc2538/dev/button-sensor.h rename to arch/platform/openmote-cc2538/dev/board-buttons.c index 9a08634ae..654302f24 100644 --- a/arch/platform/openmote-cc2538/dev/button-sensor.h +++ b/arch/platform/openmote-cc2538/dev/board-buttons.c @@ -1,16 +1,16 @@ /* - * Copyright (c) 2012, Texas Instruments Incorporated - http://www.ti.com/ + * Copyright (c) 2017, George Oikonomou - http://www.spd.gr * 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 copyright holder nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. @@ -27,46 +27,29 @@ * 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 file is part of the Contiki operating system. - * */ /*---------------------------------------------------------------------------*/ /** * \addtogroup openmote-cc2538 * @{ * - * \defgroup openmote-button-sensor OpenMote-CC2538 user button driver - * - * The user button will generate a sensors_changed event on press as - * well as on release. + * \defgroup openmote-cc2538-buttons OpenMote-CC2538 user button * + * Generic module controlling the user button on the OpenMote-CC2538 * @{ * * \file - * Header for the OpenMote-CC2538 button driver + * Defines the OpenMote-CC2538 user button for use with the button HAL */ /*---------------------------------------------------------------------------*/ -#ifndef BUTTON_SENSOR_H_ -#define BUTTON_SENSOR_H_ +#include "contiki.h" +#include "dev/button-hal.h" /*---------------------------------------------------------------------------*/ -#include "lib/sensors.h" +BUTTON_HAL_BUTTON(button_user, "User button", \ + GPIO_PORT_PIN_TO_GPIO_HAL_PIN(BUTTON_USER_PORT, BUTTON_USER_PIN), \ + GPIO_HAL_PIN_CFG_PULL_UP, BUTTON_HAL_ID_USER_BUTTON, true); /*---------------------------------------------------------------------------*/ -#define BUTTON_SENSOR "Button" - -extern const struct sensors_sensor button_sensor; -/*---------------------------------------------------------------------------*/ -extern process_event_t button_press_duration_exceeded; -/*---------------------------------------------------------------------------*/ -#define BUTTON_SENSOR_CONFIG_TYPE_INTERVAL 0x0100 - -#define BUTTON_SENSOR_VALUE_TYPE_LEVEL 0 -#define BUTTON_SENSOR_VALUE_TYPE_PRESS_DURATION 1 - -#define BUTTON_SENSOR_PRESSED_LEVEL 0 -#define BUTTON_SENSOR_RELEASED_LEVEL 8 -/*---------------------------------------------------------------------------*/ -#endif /* BUTTON_SENSOR_H_ */ +BUTTON_HAL_BUTTONS(&button_user); /*---------------------------------------------------------------------------*/ /** * @} diff --git a/arch/platform/openmote-cc2538/dev/button-sensor.c b/arch/platform/openmote-cc2538/dev/button-sensor.c deleted file mode 100644 index a6c5fe2d8..000000000 --- a/arch/platform/openmote-cc2538/dev/button-sensor.c +++ /dev/null @@ -1,178 +0,0 @@ -/* - * Copyright (c) 2012, Texas Instruments Incorporated - http://www.ti.com/ - * 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 copyright holder 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 COPYRIGHT HOLDERS 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 - * COPYRIGHT HOLDER 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 file is part of the Contiki operating system. - * - */ -/*---------------------------------------------------------------------------*/ -/** - * \addtogroup openmote-button-sensor - * @{ - * - * \file - * Driver for for the OpenMote-CC2538 user button - */ -/*---------------------------------------------------------------------------*/ -#include "contiki.h" -#include "dev/nvic.h" -#include "dev/ioc.h" -#include "dev/gpio.h" -#include "dev/button-sensor.h" -#include "sys/timer.h" -#include "sys/ctimer.h" -#include "sys/process.h" - -#include -#include -/*---------------------------------------------------------------------------*/ -#define BUTTON_USER_PORT_BASE GPIO_PORT_TO_BASE(BUTTON_USER_PORT) -#define BUTTON_USER_PIN_MASK GPIO_PIN_MASK(BUTTON_USER_PIN) -/*---------------------------------------------------------------------------*/ -#define DEBOUNCE_DURATION (CLOCK_SECOND >> 4) - -static struct timer debouncetimer; -/*---------------------------------------------------------------------------*/ -static clock_time_t press_duration = 0; -static struct ctimer press_counter; -static uint8_t press_event_counter; - -process_event_t button_press_duration_exceeded; -/*---------------------------------------------------------------------------*/ -static void -duration_exceeded_callback(void *data) -{ - press_event_counter++; - process_post(PROCESS_BROADCAST, button_press_duration_exceeded, - &press_event_counter); - ctimer_set(&press_counter, press_duration, duration_exceeded_callback, - NULL); -} -/*---------------------------------------------------------------------------*/ -/** - * \brief Retrieves the value of the button pin - * \param type Returns the pin level or the counter of press duration events. - * type == BUTTON_SENSOR_VALUE_TYPE_LEVEL or - * type == BUTTON_SENSOR_VALUE_TYPE_PRESS_DURATION - * respectively - */ -static int -value(int type) -{ - switch(type) { - case BUTTON_SENSOR_VALUE_TYPE_LEVEL: - return GPIO_READ_PIN(BUTTON_USER_PORT_BASE, BUTTON_USER_PIN_MASK); - case BUTTON_SENSOR_VALUE_TYPE_PRESS_DURATION: - return press_event_counter; - } - - return 0; -} -/*---------------------------------------------------------------------------*/ -static void -button_press_handler(gpio_hal_pin_mask_t pin_mask) -{ - if(!timer_expired(&debouncetimer)) { - return; - } - - timer_set(&debouncetimer, DEBOUNCE_DURATION); - - if(press_duration) { - press_event_counter = 0; - if(value(BUTTON_SENSOR_VALUE_TYPE_LEVEL) == BUTTON_SENSOR_PRESSED_LEVEL) { - ctimer_set(&press_counter, press_duration, duration_exceeded_callback, - NULL); - } else { - ctimer_stop(&press_counter); - } - } - - sensors_changed(&button_sensor); -} -/*---------------------------------------------------------------------------*/ -static gpio_hal_event_handler_t press_handler = { - .next = NULL, - .handler = button_press_handler, - .pin_mask = gpio_hal_pin_to_mask(BUTTON_USER_PIN) << (BUTTON_USER_PORT << 3), -}; -/*---------------------------------------------------------------------------*/ -/** - * \brief Init function for the User button. - * \param type SENSORS_ACTIVE: Activate / Deactivate the sensor (value == 1 - * or 0 respectively) - * - * \param value Depends on the value of the type argument - * \return Depends on the value of the type argument - */ -static int -config_user(int type, int value) -{ - switch(type) { - case SENSORS_HW_INIT: - button_press_duration_exceeded = process_alloc_event(); - - /* Software controlled */ - GPIO_SOFTWARE_CONTROL(BUTTON_USER_PORT_BASE, BUTTON_USER_PIN_MASK); - - /* Set pin to input */ - GPIO_SET_INPUT(BUTTON_USER_PORT_BASE, BUTTON_USER_PIN_MASK); - - /* Enable edge detection */ - GPIO_DETECT_EDGE(BUTTON_USER_PORT_BASE, BUTTON_USER_PIN_MASK); - - /* Both Edges */ - GPIO_TRIGGER_BOTH_EDGES(BUTTON_USER_PORT_BASE, BUTTON_USER_PIN_MASK); - - ioc_set_over(BUTTON_USER_PORT, BUTTON_USER_PIN, IOC_OVERRIDE_PUE); - - gpio_hal_register_handler(&press_handler); - break; - case SENSORS_ACTIVE: - if(value) { - GPIO_ENABLE_INTERRUPT(BUTTON_USER_PORT_BASE, BUTTON_USER_PIN_MASK); - NVIC_EnableIRQ(BUTTON_USER_VECTOR); - } else { - GPIO_DISABLE_INTERRUPT(BUTTON_USER_PORT_BASE, BUTTON_USER_PIN_MASK); - NVIC_DisableIRQ(BUTTON_USER_VECTOR); - } - return value; - case BUTTON_SENSOR_CONFIG_TYPE_INTERVAL: - press_duration = (clock_time_t)value; - break; - default: - break; - } - - return 1; -} -/*---------------------------------------------------------------------------*/ -SENSORS_SENSOR(button_sensor, BUTTON_SENSOR, value, config_user, NULL); -/*---------------------------------------------------------------------------*/ -/** @} */ diff --git a/arch/platform/openmote-cc2538/dev/openmote-sensors.c b/arch/platform/openmote-cc2538/dev/openmote-sensors.c index 3b55e4cbb..772087fa9 100644 --- a/arch/platform/openmote-cc2538/dev/openmote-sensors.c +++ b/arch/platform/openmote-cc2538/dev/openmote-sensors.c @@ -52,7 +52,7 @@ /** * \brief Exports a global symbol to be used by the sensor API */ -SENSORS(&button_sensor, &cc2538_temp_sensor); +SENSORS(&cc2538_temp_sensor); /*---------------------------------------------------------------------------*/ /** * @} diff --git a/arch/platform/openmote-cc2538/dev/openmote-sensors.h b/arch/platform/openmote-cc2538/dev/openmote-sensors.h index a765dbd3d..811518625 100644 --- a/arch/platform/openmote-cc2538/dev/openmote-sensors.h +++ b/arch/platform/openmote-cc2538/dev/openmote-sensors.h @@ -49,7 +49,6 @@ /*---------------------------------------------------------------------------*/ #include "lib/sensors.h" #include "dev/cc2538-sensors.h" -#include "dev/button-sensor.h" /*---------------------------------------------------------------------------*/ #endif /* OPENMOTE_SENSORS_H_ */ /*---------------------------------------------------------------------------*/ diff --git a/arch/platform/openmote-cc2538/platform.c b/arch/platform/openmote-cc2538/platform.c index 4b390e51b..c226785c6 100644 --- a/arch/platform/openmote-cc2538/platform.c +++ b/arch/platform/openmote-cc2538/platform.c @@ -55,8 +55,10 @@ #include "dev/cc2538-rf.h" #include "dev/udma.h" #include "dev/crypto.h" +#include "dev/button-hal.h" #include "usb/usb-serial.h" #include "lib/random.h" +#include "lib/sensors.h" #include "net/netstack.h" #include "net/mac/framer/frame802154.h" #include "net/linkaddr.h" @@ -157,6 +159,8 @@ platform_init_stage_two() /* Populate linkaddr_node_addr */ ieee_addr_cpy_to(linkaddr_node_addr.u8, LINKADDR_SIZE); + button_hal_init(); + INTERRUPTS_ENABLE(); fade(LEDS_BLUE); @@ -175,8 +179,6 @@ platform_init_stage_three() process_start(&sensors_process, NULL); - SENSORS_ACTIVATE(button_sensor); - fade(LEDS_GREEN); } /*---------------------------------------------------------------------------*/ From a5ea8d44e9c64882e8dc26a4b375d7858e99198b Mon Sep 17 00:00:00 2001 From: George Oikonomou Date: Sun, 5 Nov 2017 18:46:46 +0000 Subject: [PATCH 13/44] Use the new button HAL: Zoul --- arch/platform/zoul/Makefile.zoul | 2 +- .../dev/{button-sensor.h => board-buttons.c} | 48 ++--- arch/platform/zoul/dev/button-sensor.c | 178 ------------------ arch/platform/zoul/dev/zoul-sensors.c | 7 +- arch/platform/zoul/dev/zoul-sensors.h | 1 - arch/platform/zoul/platform.c | 10 +- 6 files changed, 21 insertions(+), 225 deletions(-) rename arch/platform/zoul/dev/{button-sensor.h => board-buttons.c} (56%) delete mode 100644 arch/platform/zoul/dev/button-sensor.c diff --git a/arch/platform/zoul/Makefile.zoul b/arch/platform/zoul/Makefile.zoul index 5f6bfba42..6bbcd675b 100644 --- a/arch/platform/zoul/Makefile.zoul +++ b/arch/platform/zoul/Makefile.zoul @@ -33,7 +33,7 @@ PLATFORM_ROOT_DIR = $(CONTIKI)/arch/platform/$(TARGET) ### Include CONTIKI_TARGET_SOURCEFILES += platform.c leds-arch.c CONTIKI_TARGET_SOURCEFILES += leds.c cc1200-zoul-arch.c -CONTIKI_TARGET_SOURCEFILES += adc-zoul.c button-sensor.c zoul-sensors.c +CONTIKI_TARGET_SOURCEFILES += adc-zoul.c board-buttons.c zoul-sensors.c CONTIKI_TARGET_SOURCEFILES += $(BOARD_SOURCEFILES) CONTIKI_SOURCEFILES += $(CONTIKI_TARGET_SOURCEFILES) diff --git a/arch/platform/zoul/dev/button-sensor.h b/arch/platform/zoul/dev/board-buttons.c similarity index 56% rename from arch/platform/zoul/dev/button-sensor.h rename to arch/platform/zoul/dev/board-buttons.c index 12e333c4a..93532caa4 100644 --- a/arch/platform/zoul/dev/button-sensor.h +++ b/arch/platform/zoul/dev/board-buttons.c @@ -1,7 +1,5 @@ /* - * Copyright (c) 2012, Texas Instruments Incorporated - http://www.ti.com/ - * Copyright (c) 2015, Zolertia - http://www.zolertia.com - * Copyright (c) 2015, University of Bristol - http://www.bristol.ac.uk + * Copyright (c) 2017, George Oikonomou - http://www.spd.gr * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -32,50 +30,30 @@ */ /*---------------------------------------------------------------------------*/ /** - * \addtogroup zoul-sensors + * \addtogroup zoul * @{ * - * \defgroup zoul-button-sensor Zoul User Button Driver + * \defgroup zoul-buttons Zoul user button * - * Driver for the Zoul user button - * - * The user button (on Zoul-based platforms like the RE-Mote and the Firefly) - * will generate a sensors_changed event on press as well as on release. - * - * Unlike many other platforms, the user button has the ability to - * generate events when the user keeps the button pressed. The user can - * configure the button driver with a timer interval in clock ticks. When the - * button is kept pressed, the driver will then generate a broadcast event - * each time the interval passes. For example the driver can be configured to - * generate an event every second while the button is kept pressed. This - * functionality can be enabled through the configure() function, by passing - * BUTTON_SENSOR_CONFIG_TYPE_INTERVAL as the type argument. + * Generic module controlling the user button on the Zoul * @{ * * \file - * Header file for the Zoul User Button Driver + * Defines the Zoul user button for use with the button HAL */ /*---------------------------------------------------------------------------*/ -#ifndef BUTTON_SENSOR_H_ -#define BUTTON_SENSOR_H_ +#include "contiki.h" +#include "dev/button-hal.h" /*---------------------------------------------------------------------------*/ -#include "lib/sensors.h" +#if PLATFORM_HAS_BUTTON /*---------------------------------------------------------------------------*/ -#define BUTTON_SENSOR "Button" - -extern const struct sensors_sensor button_sensor; +BUTTON_HAL_BUTTON(button_user, "User button", \ + GPIO_PORT_PIN_TO_GPIO_HAL_PIN(BUTTON_USER_PORT, BUTTON_USER_PIN), \ + GPIO_HAL_PIN_CFG_PULL_UP, BUTTON_HAL_ID_USER_BUTTON, true); /*---------------------------------------------------------------------------*/ -extern process_event_t button_press_duration_exceeded; +BUTTON_HAL_BUTTONS(&button_user); /*---------------------------------------------------------------------------*/ -#define BUTTON_SENSOR_CONFIG_TYPE_INTERVAL 0x0100 - -#define BUTTON_SENSOR_VALUE_TYPE_LEVEL 0 -#define BUTTON_SENSOR_VALUE_TYPE_PRESS_DURATION 1 - -#define BUTTON_SENSOR_PRESSED_LEVEL 0 -#define BUTTON_SENSOR_RELEASED_LEVEL 8 -/*---------------------------------------------------------------------------*/ -#endif /* BUTTON_SENSOR_H_ */ +#endif /*---------------------------------------------------------------------------*/ /** * @} diff --git a/arch/platform/zoul/dev/button-sensor.c b/arch/platform/zoul/dev/button-sensor.c deleted file mode 100644 index 9b410cdbf..000000000 --- a/arch/platform/zoul/dev/button-sensor.c +++ /dev/null @@ -1,178 +0,0 @@ -/* - * Copyright (c) 2012, Texas Instruments Incorporated - http://www.ti.com/ - * Copyright (c) 2015, Zolertia - http://www.zolertia.com - * Copyright (c) 2015, University of Bristol - http://www.bristol.ac.uk - * 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 copyright holder 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 COPYRIGHT HOLDERS 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 - * COPYRIGHT HOLDER 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. - */ -/*---------------------------------------------------------------------------*/ -/** - * \addtogroup zoul-button-sensor - * @{ - * - * \file - * Driver for the Zoul user button - */ -/*---------------------------------------------------------------------------*/ -#include "contiki.h" -#include "dev/nvic.h" -#include "dev/ioc.h" -#include "dev/gpio.h" -#include "dev/gpio-hal.h" -#include "dev/button-sensor.h" -#include "sys/timer.h" -#include "sys/ctimer.h" -#include "sys/process.h" - -#include -#include -/*---------------------------------------------------------------------------*/ -#define BUTTON_USER_PORT_BASE GPIO_PORT_TO_BASE(BUTTON_USER_PORT) -#define BUTTON_USER_PIN_MASK GPIO_PIN_MASK(BUTTON_USER_PIN) -/*---------------------------------------------------------------------------*/ -#define DEBOUNCE_DURATION (CLOCK_SECOND >> 4) - -static struct timer debouncetimer; -/*---------------------------------------------------------------------------*/ -static clock_time_t press_duration = 0; -static struct ctimer press_counter; -static uint8_t press_event_counter; - -process_event_t button_press_duration_exceeded; -/*---------------------------------------------------------------------------*/ -static void -duration_exceeded_callback(void *data) -{ - press_event_counter++; - process_post(PROCESS_BROADCAST, button_press_duration_exceeded, - &press_event_counter); - ctimer_set(&press_counter, press_duration, duration_exceeded_callback, - NULL); -} -/*---------------------------------------------------------------------------*/ -/** - * \brief Retrieves the value of the button pin - * \param type Returns the pin level or the counter of press duration events. - * type == BUTTON_SENSOR_VALUE_TYPE_LEVEL or - * type == BUTTON_SENSOR_VALUE_TYPE_PRESS_DURATION - * respectively - */ -static int -value(int type) -{ - switch(type) { - case BUTTON_SENSOR_VALUE_TYPE_LEVEL: - return GPIO_READ_PIN(BUTTON_USER_PORT_BASE, BUTTON_USER_PIN_MASK); - case BUTTON_SENSOR_VALUE_TYPE_PRESS_DURATION: - return press_event_counter; - } - - return 0; -} -/*---------------------------------------------------------------------------*/ -static void -button_press_handler(gpio_hal_pin_mask_t pin_mask) -{ - if(!timer_expired(&debouncetimer)) { - return; - } - - timer_set(&debouncetimer, DEBOUNCE_DURATION); - - if(press_duration) { - press_event_counter = 0; - if(value(BUTTON_SENSOR_VALUE_TYPE_LEVEL) == BUTTON_SENSOR_PRESSED_LEVEL) { - ctimer_set(&press_counter, press_duration, duration_exceeded_callback, - NULL); - } else { - ctimer_stop(&press_counter); - } - } - - sensors_changed(&button_sensor); -} -/*---------------------------------------------------------------------------*/ -static gpio_hal_event_handler_t press_handler = { - .next = NULL, - .handler = button_press_handler, - .pin_mask = gpio_hal_pin_to_mask(BUTTON_USER_PIN) << (BUTTON_USER_PORT << 3), -}; -/*---------------------------------------------------------------------------*/ -/** - * \brief Init function for the User button. - * \param type SENSORS_ACTIVE: Activate / Deactivate the sensor (value == 1 - * or 0 respectively) - * - * \param value Depends on the value of the type argument - * \return Depends on the value of the type argument - */ -static int -config_user(int type, int value) -{ - switch(type) { - case SENSORS_HW_INIT: - button_press_duration_exceeded = process_alloc_event(); - - /* Software controlled */ - GPIO_SOFTWARE_CONTROL(BUTTON_USER_PORT_BASE, BUTTON_USER_PIN_MASK); - - /* Set pin to input */ - GPIO_SET_INPUT(BUTTON_USER_PORT_BASE, BUTTON_USER_PIN_MASK); - - /* Enable edge detection */ - GPIO_DETECT_EDGE(BUTTON_USER_PORT_BASE, BUTTON_USER_PIN_MASK); - - /* Both Edges */ - GPIO_TRIGGER_BOTH_EDGES(BUTTON_USER_PORT_BASE, BUTTON_USER_PIN_MASK); - - ioc_set_over(BUTTON_USER_PORT, BUTTON_USER_PIN, IOC_OVERRIDE_PUE); - - gpio_hal_register_handler(&press_handler); - break; - case SENSORS_ACTIVE: - if(value) { - GPIO_ENABLE_INTERRUPT(BUTTON_USER_PORT_BASE, BUTTON_USER_PIN_MASK); - NVIC_EnableIRQ(BUTTON_USER_VECTOR); - } else { - GPIO_DISABLE_INTERRUPT(BUTTON_USER_PORT_BASE, BUTTON_USER_PIN_MASK); - NVIC_DisableIRQ(BUTTON_USER_VECTOR); - } - return value; - case BUTTON_SENSOR_CONFIG_TYPE_INTERVAL: - press_duration = (clock_time_t)value; - break; - default: - break; - } - - return 1; -} -/*---------------------------------------------------------------------------*/ -SENSORS_SENSOR(button_sensor, BUTTON_SENSOR, value, config_user, NULL); -/*---------------------------------------------------------------------------*/ -/** @} */ diff --git a/arch/platform/zoul/dev/zoul-sensors.c b/arch/platform/zoul/dev/zoul-sensors.c index da755fc79..8015a183e 100644 --- a/arch/platform/zoul/dev/zoul-sensors.c +++ b/arch/platform/zoul/dev/zoul-sensors.c @@ -47,12 +47,7 @@ #include /*---------------------------------------------------------------------------*/ /** \brief Exports global symbols for the sensor API */ -SENSORS(&vdd3_sensor, -#if PLATFORM_HAS_BUTTON - &button_sensor, -#endif - &cc2538_temp_sensor -); +SENSORS(&vdd3_sensor, &cc2538_temp_sensor); /*---------------------------------------------------------------------------*/ /** * @} diff --git a/arch/platform/zoul/dev/zoul-sensors.h b/arch/platform/zoul/dev/zoul-sensors.h index 5682c972c..92635810a 100644 --- a/arch/platform/zoul/dev/zoul-sensors.h +++ b/arch/platform/zoul/dev/zoul-sensors.h @@ -48,7 +48,6 @@ /*---------------------------------------------------------------------------*/ #include "lib/sensors.h" #include "dev/cc2538-sensors.h" -#include "dev/button-sensor.h" /*---------------------------------------------------------------------------*/ /** * \name Zoul sensor constants diff --git a/arch/platform/zoul/platform.c b/arch/platform/zoul/platform.c index 2b0a394b1..8cce149ee 100644 --- a/arch/platform/zoul/platform.c +++ b/arch/platform/zoul/platform.c @@ -54,8 +54,10 @@ #include "dev/udma.h" #include "dev/crypto.h" #include "dev/rtcc.h" +#include "dev/button-hal.h" #include "usb/usb-serial.h" #include "lib/random.h" +#include "lib/sensors.h" #include "net/netstack.h" #include "net/mac/framer/frame802154.h" #include "net/linkaddr.h" @@ -221,6 +223,10 @@ platform_init_stage_two() /* Populate linkaddr_node_addr */ ieee_addr_cpy_to(linkaddr_node_addr.u8, LINKADDR_SIZE); +#if PLATFORM_HAS_BUTTON + button_hal_init(); +#endif + INTERRUPTS_ENABLE(); fade(LEDS_BLUE); @@ -241,10 +247,6 @@ platform_init_stage_three() process_start(&sensors_process, NULL); -#if PLATFORM_HAS_BUTTON - SENSORS_ACTIVATE(button_sensor); -#endif - fade(LEDS_GREEN); } /*---------------------------------------------------------------------------*/ From 0445275825ebba71d10625acc57a0db9c3b9700f Mon Sep 17 00:00:00 2001 From: George Oikonomou Date: Sun, 5 Nov 2017 22:09:48 +0000 Subject: [PATCH 14/44] Change Zoul examples to use the button HAL --- .../zoul/orion/client/ifttt-client.c | 13 +++++------- examples/platform-specific/zoul/test-lcd.c | 21 +++++++------------ 2 files changed, 13 insertions(+), 21 deletions(-) diff --git a/examples/platform-specific/zoul/orion/client/ifttt-client.c b/examples/platform-specific/zoul/orion/client/ifttt-client.c index 7cc962c18..da2387a85 100644 --- a/examples/platform-specific/zoul/orion/client/ifttt-client.c +++ b/examples/platform-specific/zoul/orion/client/ifttt-client.c @@ -34,7 +34,7 @@ #include "ipv6/ip64-addr.h" #include "dev/leds.h" #include "net/routing/routing.h" -#include "dev/button-sensor.h" +#include "dev/button-hal.h" #include /*---------------------------------------------------------------------------*/ static struct http_socket s; @@ -138,13 +138,10 @@ PROCESS_THREAD(http_example_process, ev, data) while(1) { PROCESS_YIELD(); - if((ev == sensors_event) && (data == &button_sensor)) { - if(button_sensor.value(BUTTON_SENSOR_VALUE_TYPE_LEVEL) == - BUTTON_SENSOR_PRESSED_LEVEL) { - leds_on(LEDS_GREEN); - printf("Button pressed! sending a POST to IFTTT\n"); - http_socket_post(&s, url_buffer, NULL, 0, NULL, callback, NULL); - } + if(ev == button_hal_release_event) { + leds_on(LEDS_GREEN); + printf("Button pressed! sending a POST to IFTTT\n"); + http_socket_post(&s, url_buffer, NULL, 0, NULL, callback, NULL); } } diff --git a/examples/platform-specific/zoul/test-lcd.c b/examples/platform-specific/zoul/test-lcd.c index 6a17df99c..9b30ab25a 100644 --- a/examples/platform-specific/zoul/test-lcd.c +++ b/examples/platform-specific/zoul/test-lcd.c @@ -47,7 +47,7 @@ #include #include "contiki.h" #include "dev/rgb-bl-lcd.h" -#include "dev/button-sensor.h" +#include "dev/button-hal.h" /*---------------------------------------------------------------------------*/ #define SCROLL_PERIOD (CLOCK_SECOND / 6) /*---------------------------------------------------------------------------*/ @@ -104,18 +104,13 @@ PROCESS_THREAD(remote_lcd_process, ev, data) printf("Counter: %05u\n", counter); counter++; etimer_restart(&et); - } else if(ev == sensors_event) { - if(data == &button_sensor) { - if(button_sensor.value(BUTTON_SENSOR_VALUE_TYPE_LEVEL) == - BUTTON_SENSOR_PRESSED_LEVEL) { - printf("Button pressed!!\n"); - lcd_set_cursor(0, LCD_RGB_1ST_ROW); - lcd_write("Button pressed!!"); - } else { - lcd_set_cursor(0, LCD_RGB_1ST_ROW); - lcd_write("Press the button!"); - } - } + } else if(ev == button_hal_press_event) { + printf("Button pressed!!\n"); + lcd_set_cursor(0, LCD_RGB_1ST_ROW); + lcd_write("Button pressed!!"); + } else if(ev == button_hal_release_event) { + lcd_set_cursor(0, LCD_RGB_1ST_ROW); + lcd_write("Press the button!"); } } From 25afb83209113e79792e444e27a1579fb6ff0469 Mon Sep 17 00:00:00 2001 From: George Oikonomou Date: Sun, 5 Nov 2017 22:16:54 +0000 Subject: [PATCH 15/44] Change CC2538 MQTT demo to use the button HAL --- .../cc2538-common/mqtt-demo/mqtt-demo.c | 10 ++++++---- .../cc2538-common/mqtt-demo/project-conf.h | 1 - 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/examples/platform-specific/cc2538-common/mqtt-demo/mqtt-demo.c b/examples/platform-specific/cc2538-common/mqtt-demo/mqtt-demo.c index b818b88ca..5ed836cc1 100644 --- a/examples/platform-specific/cc2538-common/mqtt-demo/mqtt-demo.c +++ b/examples/platform-specific/cc2538-common/mqtt-demo/mqtt-demo.c @@ -50,7 +50,7 @@ #include "sys/etimer.h" #include "sys/ctimer.h" #include "lib/sensors.h" -#include "dev/button-sensor.h" +#include "dev/button-hal.h" #include "dev/leds.h" #include "dev/cc2538-sensors.h" @@ -138,7 +138,7 @@ static uint8_t state; #define DEFAULT_RSSI_MEAS_INTERVAL (CLOCK_SECOND * 30) /*---------------------------------------------------------------------------*/ /* Take a sensor reading on button press */ -#define PUBLISH_TRIGGER &button_sensor +#define PUBLISH_TRIGGER BUTTON_HAL_ID_USER_BUTTON /* Payload length of ICMPv6 echo requests used to measure RSSI with def rt */ #define ECHO_REQ_PAYLOAD_LEN 20 @@ -704,7 +704,8 @@ PROCESS_THREAD(mqtt_demo_process, ev, data) PROCESS_YIELD(); - if(ev == sensors_event && data == PUBLISH_TRIGGER) { + if(ev == button_hal_release_event && + ((button_hal_button_t *)data)->unique_id == PUBLISH_TRIGGER) { if(state == STATE_ERROR) { connect_attempt = 1; state = STATE_REGISTERED; @@ -713,7 +714,8 @@ PROCESS_THREAD(mqtt_demo_process, ev, data) if((ev == PROCESS_EVENT_TIMER && data == &publish_periodic_timer) || ev == PROCESS_EVENT_POLL || - (ev == sensors_event && data == PUBLISH_TRIGGER)) { + (ev == button_hal_release_event && + ((button_hal_button_t *)data)->unique_id == PUBLISH_TRIGGER)) { state_machine(); } diff --git a/examples/platform-specific/cc2538-common/mqtt-demo/project-conf.h b/examples/platform-specific/cc2538-common/mqtt-demo/project-conf.h index a3cc4073b..ac86990c6 100644 --- a/examples/platform-specific/cc2538-common/mqtt-demo/project-conf.h +++ b/examples/platform-specific/cc2538-common/mqtt-demo/project-conf.h @@ -45,7 +45,6 @@ /* User configuration */ #define MQTT_DEMO_STATUS_LED LEDS_GREEN -#define MQTT_DEMO_PUBLISH_TRIGGER &button_right_sensor /* If undefined, the demo will attempt to connect to IBM's quickstart */ #define MQTT_DEMO_BROKER_IP_ADDR "fd00::1" From 13c18011c7c097cb69ab3475a9514c33c8326f8d Mon Sep 17 00:00:00 2001 From: George Oikonomou Date: Sun, 11 Mar 2018 20:06:36 +0000 Subject: [PATCH 16/44] Change the GPIO HAL example to use the new button API --- examples/dev/gpio-hal/gpio-hal-example.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/examples/dev/gpio-hal/gpio-hal-example.c b/examples/dev/gpio-hal/gpio-hal-example.c index 83f3b6a2f..040df3b38 100644 --- a/examples/dev/gpio-hal/gpio-hal-example.c +++ b/examples/dev/gpio-hal/gpio-hal-example.c @@ -33,7 +33,7 @@ #include "dev/gpio-hal.h" #include "sys/etimer.h" #include "lib/sensors.h" -#include "dev/button-sensor.h" +#include "dev/button-hal.h" #include /*---------------------------------------------------------------------------*/ @@ -129,8 +129,9 @@ PROCESS_THREAD(gpio_hal_example, ev, data) counter++; etimer_set(&et, CLOCK_SECOND); - } else if(ev == sensors_event && data == &button_sensor) { - printf("Button event\n"); + } else if(ev == button_hal_release_event) { + printf("Button release event %s\n", + BUTTON_HAL_GET_DESCRIPTION((button_hal_button_t *)data)); } } From 155e2c9b0af9087424688aa1d55542179afeba20 Mon Sep 17 00:00:00 2001 From: George Oikonomou Date: Sun, 5 Nov 2017 22:38:45 +0000 Subject: [PATCH 17/44] Support both old and new button API in common examples This change is temporary so travis can pass until all platforms have been migrated to the new API --- arch/platform/cc2538dk/dev/board.h | 1 + arch/platform/openmote-cc2538/board.h | 1 + arch/platform/srf06-cc26xx/contiki-conf.h | 1 + arch/platform/zoul/contiki-conf.h | 2 ++ examples/coap/coap-example-client.c | 14 +++++++++-- examples/coap/coap-example-observe-client.c | 16 ++++++++++--- examples/coap/coap-example-server.c | 8 ++++++- os/services/ipso-objects/ipso-button.c | 24 ++++++++++++------- .../embedded/border-router-embedded.c | 10 ++++++++ 9 files changed, 63 insertions(+), 14 deletions(-) diff --git a/arch/platform/cc2538dk/dev/board.h b/arch/platform/cc2538dk/dev/board.h index 0b2a4cd09..80f036fd7 100644 --- a/arch/platform/cc2538dk/dev/board.h +++ b/arch/platform/cc2538dk/dev/board.h @@ -163,6 +163,7 @@ /* Notify various examples that we have Buttons */ #define PLATFORM_HAS_BUTTON 1 +#define PLATFORM_SUPPORTS_BUTTON_HAL 1 /** @} */ /*---------------------------------------------------------------------------*/ /** diff --git a/arch/platform/openmote-cc2538/board.h b/arch/platform/openmote-cc2538/board.h index f7fbfc6c2..abb89b8e2 100644 --- a/arch/platform/openmote-cc2538/board.h +++ b/arch/platform/openmote-cc2538/board.h @@ -128,6 +128,7 @@ #define BUTTON_USER_VECTOR GPIO_C_IRQn /* Notify various examples that we have Buttons */ #define PLATFORM_HAS_BUTTON 1 +#define PLATFORM_SUPPORTS_BUTTON_HAL 1 /** @} */ /*---------------------------------------------------------------------------*/ /** diff --git a/arch/platform/srf06-cc26xx/contiki-conf.h b/arch/platform/srf06-cc26xx/contiki-conf.h index d0e10f5db..4a1470b40 100644 --- a/arch/platform/srf06-cc26xx/contiki-conf.h +++ b/arch/platform/srf06-cc26xx/contiki-conf.h @@ -59,6 +59,7 @@ /* Notify various examples that we have Buttons */ #define PLATFORM_HAS_BUTTON 1 +#define PLATFORM_SUPPORTS_BUTTON_HAL 1 /* * Override button symbols from dev/button-sensor.h, for the examples that diff --git a/arch/platform/zoul/contiki-conf.h b/arch/platform/zoul/contiki-conf.h index b1f1664b9..098c37404 100644 --- a/arch/platform/zoul/contiki-conf.h +++ b/arch/platform/zoul/contiki-conf.h @@ -86,6 +86,8 @@ /*---------------------------------------------------------------------------*/ /* board.h assumes that basic configuration is done */ #include "board.h" + +#define PLATFORM_SUPPORTS_BUTTON_HAL PLATFORM_HAS_BUTTON /*---------------------------------------------------------------------------*/ /** * \name Radio Configuration diff --git a/examples/coap/coap-example-client.c b/examples/coap/coap-example-client.c index e17a0ffde..27efbbf6e 100644 --- a/examples/coap/coap-example-client.c +++ b/examples/coap/coap-example-client.c @@ -43,7 +43,11 @@ #include "contiki-net.h" #include "coap-engine.h" #include "coap-blocking-api.h" +#if PLATFORM_SUPPORTS_BUTTON_HAL +#include "dev/button-hal.h" +#else #include "dev/button-sensor.h" +#endif /* Log configuration */ #include "coap-log.h" @@ -94,9 +98,11 @@ PROCESS_THREAD(er_example_client, ev, data) etimer_set(&et, TOGGLE_INTERVAL * CLOCK_SECOND); #if PLATFORM_HAS_BUTTON +#if !PLATFORM_SUPPORTS_BUTTON_HAL SENSORS_ACTIVATE(button_sensor); - printf("Press a button to request %s\n", service_urls[uri_switch]); #endif + printf("Press a button to request %s\n", service_urls[uri_switch]); +#endif /* PLATFORM_HAS_BUTTON */ while(1) { PROCESS_YIELD(); @@ -122,7 +128,11 @@ PROCESS_THREAD(er_example_client, ev, data) etimer_reset(&et); #if PLATFORM_HAS_BUTTON +#if PLATFORM_SUPPORTS_BUTTON_HAL + } else if(ev == button_hal_release_event) { +#else } else if(ev == sensors_event && data == &button_sensor) { +#endif /* send a request to notify the end of the process */ @@ -140,7 +150,7 @@ PROCESS_THREAD(er_example_client, ev, data) printf("\n--Done--\n"); uri_switch = (uri_switch + 1) % NUMBER_OF_URLS; -#endif +#endif /* PLATFORM_HAS_BUTTON */ } } diff --git a/examples/coap/coap-example-observe-client.c b/examples/coap/coap-example-observe-client.c index 611658e69..1dbed0bd4 100644 --- a/examples/coap/coap-example-observe-client.c +++ b/examples/coap/coap-example-observe-client.c @@ -42,8 +42,11 @@ #include "contiki.h" #include "contiki-net.h" #include "coap-engine.h" +#if PLATFORM_SUPPORTS_BUTTON_HAL +#include "dev/button-hal.h" +#else #include "dev/button-sensor.h" - +#endif /*----------------------------------------------------------------------------*/ #define DEBUG 0 #if DEBUG @@ -147,9 +150,12 @@ PROCESS_THREAD(er_example_observe_client, ev, data) /* init timer and button (if available) */ etimer_set(&et, TOGGLE_INTERVAL * CLOCK_SECOND); #if PLATFORM_HAS_BUTTON +#if !PLATFORM_SUPPORTS_BUTTON_HAL SENSORS_ACTIVATE(button_sensor); - printf("Press a button to start/stop observation of remote resource\n"); #endif + printf("Press a button to start/stop observation of remote resource\n"); +#endif /* PLATFORM_HAS_BUTTON */ + /* toggle observation every time the timer elapses or the button is pressed */ while(1) { PROCESS_YIELD(); @@ -159,11 +165,15 @@ PROCESS_THREAD(er_example_observe_client, ev, data) printf("\n--Done--\n"); etimer_reset(&et); #if PLATFORM_HAS_BUTTON +#if PLATFORM_SUPPORTS_BUTTON_HAL + } else if(ev == button_hal_release_event) { +#else } else if(ev == sensors_event && data == &button_sensor) { +#endif printf("--Toggle tutton--\n"); toggle_observation(); printf("\n--Done--\n"); -#endif +#endif /* PLATFORM_HAS_BUTTON */ } } PROCESS_END(); diff --git a/examples/coap/coap-example-server.c b/examples/coap/coap-example-server.c index 3cbcbc746..41ea53744 100644 --- a/examples/coap/coap-example-server.c +++ b/examples/coap/coap-example-server.c @@ -42,7 +42,9 @@ #include "contiki.h" #include "coap-engine.h" -#if PLATFORM_HAS_BUTTON +#if PLATFORM_SUPPORTS_BUTTON_HAL +#include "dev/button-hal.h" +#else #include "dev/button-sensor.h" #endif @@ -165,7 +167,11 @@ PROCESS_THREAD(er_example_server, ev, data) while(1) { PROCESS_WAIT_EVENT(); #if PLATFORM_HAS_BUTTON +#if PLATFORM_SUPPORTS_BUTTON_HAL + if(ev == button_hal_release_event) { +#else if(ev == sensors_event && data == &button_sensor) { +#endif PRINTF("*******BUTTON*******\n"); /* Call the event_handler for this application-specific event. */ diff --git a/os/services/ipso-objects/ipso-button.c b/os/services/ipso-objects/ipso-button.c index 1f3cb2d9b..b0069c0aa 100644 --- a/os/services/ipso-objects/ipso-button.c +++ b/os/services/ipso-objects/ipso-button.c @@ -63,13 +63,12 @@ #define IPSO_INPUT_SENSOR_TYPE 5751 #if PLATFORM_HAS_BUTTON -#include "dev/button-sensor.h" - -#if BOARD_SENSORTAG -#include "sensortag/button-sensor.h" -#define IPSO_BUTTON_SENSOR button_left_sensor +#if PLATFORM_SUPPORTS_BUTTON_HAL +#include "dev/button-hal.h" #else +#include "dev/button-sensor.h" #define IPSO_BUTTON_SENSOR button_sensor +static struct etimer timer; #endif PROCESS(ipso_button_process, "ipso-button"); @@ -168,18 +167,26 @@ ipso_button_init(void) #if PLATFORM_HAS_BUTTON PROCESS_THREAD(ipso_button_process, ev, data) { - static struct etimer timer; - int32_t time; - PROCESS_BEGIN(); +#if !PLATFORM_SUPPORTS_BUTTON_HAL SENSORS_ACTIVATE(IPSO_BUTTON_SENSOR); +#endif while(1) { PROCESS_WAIT_EVENT(); +#if PLATFORM_SUPPORTS_BUTTON_HAL + /* ToDo */ + if(ev == button_hal_release_event) { + lwm2m_notify_object_observers(®_object, IPSO_INPUT_STATE); + } else if(ev == button_hal_periodic_event) { + lwm2m_notify_object_observers(®_object, IPSO_INPUT_COUNTER); + } +#else if(ev == sensors_event && data == &IPSO_BUTTON_SENSOR) { if(!input_state) { + int32_t time; input_state = 1; counter++; if((edge_selection & 2) != 0) { @@ -206,6 +213,7 @@ PROCESS_THREAD(ipso_button_process, ev, data) } } } +#endif } PROCESS_END(); diff --git a/os/services/rpl-border-router/embedded/border-router-embedded.c b/os/services/rpl-border-router/embedded/border-router-embedded.c index 884f60a97..6b565a7ff 100644 --- a/os/services/rpl-border-router/embedded/border-router-embedded.c +++ b/os/services/rpl-border-router/embedded/border-router-embedded.c @@ -37,7 +37,11 @@ #include "contiki.h" #include "net/routing/routing.h" +#if PLATFORM_SUPPORTS_BUTTON_HAL +#include "dev/button-hal.h" +#else #include "dev/button-sensor.h" +#endif #include "dev/slip.h" #include "rpl-border-router.h" @@ -68,7 +72,9 @@ PROCESS_THREAD(border_router_process, ev, data) PROCESS_PAUSE(); +#if !PLATFORM_SUPPORTS_BUTTON_HAL SENSORS_ACTIVATE(button_sensor); +#endif LOG_INFO("RPL-Border router started\n"); @@ -86,7 +92,11 @@ PROCESS_THREAD(border_router_process, ev, data) while(1) { PROCESS_YIELD(); +#if PLATFORM_SUPPORTS_BUTTON_HAL + if(ev == button_hal_release_event) { +#else if(ev == sensors_event && data == &button_sensor) { +#endif LOG_INFO("Initiating global repair\n"); NETSTACK_ROUTING.global_repair("Button press"); } From 4882077a75708e25ab89cac950cd05a2de7ced95 Mon Sep 17 00:00:00 2001 From: George Oikonomou Date: Sun, 11 Mar 2018 20:43:53 +0000 Subject: [PATCH 18/44] Add button HAL example --- examples/dev/button-hal/Makefile | 8 ++ examples/dev/button-hal/README.md | 15 ++++ examples/dev/button-hal/button-hal-example.c | 82 ++++++++++++++++++++ examples/dev/button-hal/project-conf.h | 39 ++++++++++ 4 files changed, 144 insertions(+) create mode 100644 examples/dev/button-hal/Makefile create mode 100644 examples/dev/button-hal/README.md create mode 100644 examples/dev/button-hal/button-hal-example.c create mode 100644 examples/dev/button-hal/project-conf.h diff --git a/examples/dev/button-hal/Makefile b/examples/dev/button-hal/Makefile new file mode 100644 index 000000000..071ec02ec --- /dev/null +++ b/examples/dev/button-hal/Makefile @@ -0,0 +1,8 @@ +CONTIKI_PROJECT = button-hal-example +CONTIKI = ../../.. + +all: $(CONTIKI_PROJECT) + +PLATFORMS_ONLY = srf06-cc26xx cc2538dk openmote-cc2538 zoul + +include $(CONTIKI)/Makefile.include diff --git a/examples/dev/button-hal/README.md b/examples/dev/button-hal/README.md new file mode 100644 index 000000000..d741ec73c --- /dev/null +++ b/examples/dev/button-hal/README.md @@ -0,0 +1,15 @@ +# Button HAL Example +This example demonstrates and tests the functionality of the Button HAL. +You can use this example to: + +* Understand the logic of the button HAL. +* Test your implementation of arch-specific button HAL components if you are +developing a new port. + +This example assumes a device with at least one switch (button or simiar). + +# Supported devices +This example is expected to work off-the-shelf on the following boards: + +* All CC13xx/CC26xx devices +* All CC2538 devices diff --git a/examples/dev/button-hal/button-hal-example.c b/examples/dev/button-hal/button-hal-example.c new file mode 100644 index 000000000..78024e28b --- /dev/null +++ b/examples/dev/button-hal/button-hal-example.c @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2018, George Oikonomou - http://www.spd.gr + * 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 copyright holder 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 COPYRIGHT HOLDERS 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 + * COPYRIGHT HOLDER 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. + */ +/*---------------------------------------------------------------------------*/ +#include "contiki.h" +#include "dev/button-hal.h" + +#include +/*---------------------------------------------------------------------------*/ +PROCESS(button_hal_example, "Button HAL Example"); +AUTOSTART_PROCESSES(&button_hal_example); +/*---------------------------------------------------------------------------*/ +PROCESS_THREAD(button_hal_example, ev, data) +{ + button_hal_button_t *btn; + + PROCESS_BEGIN(); + + btn = button_hal_get_by_id(BUTTON_HAL_ID_BUTTON_ZERO); + + printf("Button HAL example.\n"); + printf("%s on pin %u with ID=0, Logic=%s, Pull=%s\n", + BUTTON_HAL_GET_DESCRIPTION(btn), btn->pin, + btn->negative_logic ? "Negative" : "Positive", + btn->pull == GPIO_HAL_PIN_CFG_PULL_UP ? "Pull Up" : "Pull Down"); + + while(1) { + + PROCESS_YIELD(); + + if(ev == button_hal_press_event) { + btn = (button_hal_button_t *)data; + printf("Press event (%s)\n", BUTTON_HAL_GET_DESCRIPTION(btn)); + + if(btn == button_hal_get_by_id(BUTTON_HAL_ID_BUTTON_ZERO)) { + printf("This was button 0, on pin %u\n", btn->pin); + } + } else if(ev == button_hal_release_event) { + btn = (button_hal_button_t *)data; + printf("Release event (%s)\n", BUTTON_HAL_GET_DESCRIPTION(btn)); + } else if(ev == button_hal_periodic_event) { + btn = (button_hal_button_t *)data; + printf("Periodic event, %u seconds (%s)\n", btn->press_duration_seconds, + BUTTON_HAL_GET_DESCRIPTION(btn)); + + if(btn->press_duration_seconds > 5) { + printf("%s pressed for more than 5 secs. Do custom action\n", + BUTTON_HAL_GET_DESCRIPTION(btn)); + } + } + } + + PROCESS_END(); +} +/*---------------------------------------------------------------------------*/ diff --git a/examples/dev/button-hal/project-conf.h b/examples/dev/button-hal/project-conf.h new file mode 100644 index 000000000..457b42bfc --- /dev/null +++ b/examples/dev/button-hal/project-conf.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2018, George Oikonomou - http://www.spd.gr + * 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 copyright holder 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 COPYRIGHT HOLDERS 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 + * COPYRIGHT HOLDER 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 PROJECT_CONF_H_ +#define PROJECT_CONF_H_ +/*---------------------------------------------------------------------------*/ +/* Force button descriptions */ +#define BUTTON_HAL_CONF_WITH_DESCRIPTION 1 +/*---------------------------------------------------------------------------*/ +#endif /* PROJECT_CONF_H_ */ +/*---------------------------------------------------------------------------*/ From c87aadc390bead4413decf20392d9c72ffc08ad8 Mon Sep 17 00:00:00 2001 From: George Oikonomou Date: Sat, 17 Mar 2018 22:05:56 +0000 Subject: [PATCH 19/44] Change function to get button state to non-static --- os/dev/button-hal.c | 28 ++++++++++++++-------------- os/dev/button-hal.h | 8 ++++++++ 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/os/dev/button-hal.c b/os/dev/button-hal.c index 8109739d6..4dff3c2c4 100644 --- a/os/dev/button-hal.c +++ b/os/dev/button-hal.c @@ -56,19 +56,6 @@ extern button_hal_button_t *button_hal_buttons[]; /* Common handler for all handler events, and register it with the GPIO HAL */ static gpio_hal_event_handler_t button_event_handler; /*---------------------------------------------------------------------------*/ -static uint8_t -get_state(button_hal_button_t *button) -{ - uint8_t pin_state = gpio_hal_arch_read_pin(button->pin); - - if((pin_state == 0 && button->negative_logic == true) || - (pin_state == 1 && button->negative_logic == false)) { - return BUTTON_HAL_STATE_PRESSED; - } - - return BUTTON_HAL_STATE_RELEASED; -} -/*---------------------------------------------------------------------------*/ static void duration_exceeded_callback(void *btn) { @@ -99,7 +86,7 @@ debounce_handler(void *btn) expired = ctimer_expired(&button->duration_ctimer); - button_state = get_state(button); + button_state = button_hal_get_state(button); /* * A debounce timer expired. Inspect the button's state. If the button's @@ -164,6 +151,19 @@ button_hal_get_by_id(uint8_t unique_id) return NULL; } /*---------------------------------------------------------------------------*/ +uint8_t +button_hal_get_state(button_hal_button_t *button) +{ + uint8_t pin_state = gpio_hal_arch_read_pin(button->pin); + + if((pin_state == 0 && button->negative_logic == true) || + (pin_state == 1 && button->negative_logic == false)) { + return BUTTON_HAL_STATE_PRESSED; + } + + return BUTTON_HAL_STATE_RELEASED; +} +/*---------------------------------------------------------------------------*/ void button_hal_init() { diff --git a/os/dev/button-hal.h b/os/dev/button-hal.h index 7d38994f7..02fa8555f 100644 --- a/os/dev/button-hal.h +++ b/os/dev/button-hal.h @@ -239,6 +239,14 @@ void button_hal_init(void); * \return A pointer to the button or NULL if not found */ button_hal_button_t *button_hal_get_by_id(uint8_t unique_id); + +/** + * \brief Get the state of a button (pressed / released) + * \param button A pointer to the button + * \retval BUTTON_HAL_STATE_RELEASED The button is currently released + * \retval BUTTON_HAL_STATE_PRESSED The button is currently pressed + */ +uint8_t button_hal_get_state(button_hal_button_t *button); /*---------------------------------------------------------------------------*/ #endif /* BUTTON_HAL_H_ */ /*---------------------------------------------------------------------------*/ From 9b37e4b7fb8489083ee0e3ea226cf4edc1b1f3cc Mon Sep 17 00:00:00 2001 From: Niclas Finne Date: Sat, 17 Mar 2018 23:38:14 +0100 Subject: [PATCH 20/44] Updated IPSO button to use the button HAL pressed/released events. Removed support for the optional polarity resource to simplify the code. --- os/services/ipso-objects/ipso-button.c | 37 +++++++++++--------------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/os/services/ipso-objects/ipso-button.c b/os/services/ipso-objects/ipso-button.c index b0069c0aa..7993cbbb3 100644 --- a/os/services/ipso-objects/ipso-button.c +++ b/os/services/ipso-objects/ipso-button.c @@ -56,7 +56,6 @@ #define IPSO_INPUT_STATE 5500 #define IPSO_INPUT_COUNTER 5501 -#define IPSO_INPUT_POLARITY 5502 #define IPSO_INPUT_DEBOUNCE 5503 #define IPSO_INPUT_EDGE_SEL 5504 #define IPSO_INPUT_CTR_RESET 5505 @@ -79,13 +78,12 @@ static lwm2m_status_t lwm2m_callback(lwm2m_object_instance_t *object, lwm2m_context_t *ctx); static int input_state = 0; -static int polarity = 0; static int32_t counter = 0; static int32_t edge_selection = 3; /* both */ static int32_t debounce_time = 10; static const lwm2m_resource_id_t resources[] = { - RO(IPSO_INPUT_STATE), RO(IPSO_INPUT_COUNTER), RW(IPSO_INPUT_POLARITY), + RO(IPSO_INPUT_STATE), RO(IPSO_INPUT_COUNTER), RW(IPSO_INPUT_DEBOUNCE), RW(IPSO_INPUT_EDGE_SEL), EX(IPSO_INPUT_CTR_RESET), RO(IPSO_INPUT_SENSOR_TYPE) }; @@ -103,15 +101,8 @@ static lwm2m_object_instance_t reg_object = { static int read_state(void) { - int value; - if(polarity == 0) { - value = input_state ? 1 : 0; - } else { - value = input_state ? 0 : 1; - } - PRINTF("Read button state (polarity=%d, state=%d): %d\n", - polarity, input_state, value); - return value; + PRINTF("Read button state: %d\n", input_state); + return input_state; } /*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/ @@ -127,9 +118,6 @@ lwm2m_callback(lwm2m_object_instance_t *object, case IPSO_INPUT_COUNTER: lwm2m_object_write_int(ctx, counter); break; - case IPSO_INPUT_POLARITY: - lwm2m_object_write_int(ctx, polarity); - break; case IPSO_INPUT_DEBOUNCE: lwm2m_object_write_int(ctx, debounce_time); break; @@ -177,13 +165,20 @@ PROCESS_THREAD(ipso_button_process, ev, data) PROCESS_WAIT_EVENT(); #if PLATFORM_SUPPORTS_BUTTON_HAL - /* ToDo */ - if(ev == button_hal_release_event) { - lwm2m_notify_object_observers(®_object, IPSO_INPUT_STATE); - } else if(ev == button_hal_periodic_event) { + if(ev == button_hal_press_event) { + input_state = 1; + counter++; + if((edge_selection & 2) != 0) { + lwm2m_notify_object_observers(®_object, IPSO_INPUT_STATE); + } lwm2m_notify_object_observers(®_object, IPSO_INPUT_COUNTER); + } else if(ev == button_hal_release_event) { + input_state = 0; + if((edge_selection & 1) != 0) { + lwm2m_notify_object_observers(®_object, IPSO_INPUT_STATE); + } } -#else +#else /* PLATFORM_SUPPORTS_BUTTON_HAL */ if(ev == sensors_event && data == &IPSO_BUTTON_SENSOR) { if(!input_state) { int32_t time; @@ -213,7 +208,7 @@ PROCESS_THREAD(ipso_button_process, ev, data) } } } -#endif +#endif /* PLATFORM_SUPPORTS_BUTTON_HAL */ } PROCESS_END(); From d98d8f586fafbfad4b81d2af6ccadded806491d2 Mon Sep 17 00:00:00 2001 From: George Oikonomou Date: Sat, 17 Mar 2018 23:55:00 +0000 Subject: [PATCH 21/44] Fix code style --- os/dev/button-hal.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/os/dev/button-hal.c b/os/dev/button-hal.c index 4dff3c2c4..a21088a24 100644 --- a/os/dev/button-hal.c +++ b/os/dev/button-hal.c @@ -144,7 +144,7 @@ button_hal_get_by_id(uint8_t unique_id) for(button = button_hal_buttons; *button != NULL; button++) { if((*button)->unique_id == unique_id) { - return (*button); + return *button; } } From 4dc15c070688bd0850986facbaa7059067c9eecc Mon Sep 17 00:00:00 2001 From: George Oikonomou Date: Sat, 17 Mar 2018 23:55:22 +0000 Subject: [PATCH 22/44] Don't manipulete ctimers inside interrupt context --- os/dev/button-hal.c | 65 ++++++++++++++++++++++++++++++++------------- 1 file changed, 47 insertions(+), 18 deletions(-) diff --git a/os/dev/button-hal.c b/os/dev/button-hal.c index a21088a24..ec991fa48 100644 --- a/os/dev/button-hal.c +++ b/os/dev/button-hal.c @@ -40,6 +40,7 @@ #include "contiki.h" #include "sys/process.h" #include "sys/ctimer.h" +#include "sys/critical.h" #include "dev/gpio-hal.h" #include "dev/button-hal.h" @@ -47,10 +48,15 @@ #include #include /*---------------------------------------------------------------------------*/ +PROCESS(button_hal_process, "Button HAL process"); +/*---------------------------------------------------------------------------*/ process_event_t button_hal_press_event; process_event_t button_hal_release_event; process_event_t button_hal_periodic_event; /*---------------------------------------------------------------------------*/ +/* A mask of all pins that have changed state since the last process poll */ +static gpio_hal_pin_mask_t pmask; +/*---------------------------------------------------------------------------*/ extern button_hal_button_t *button_hal_buttons[]; /*---------------------------------------------------------------------------*/ /* Common handler for all handler events, and register it with the GPIO HAL */ @@ -117,24 +123,8 @@ debounce_handler(void *btn) static void press_release_handler(gpio_hal_pin_mask_t pin_mask) { - button_hal_button_t **button; - - for(button = button_hal_buttons; *button != NULL; button++) { - if(gpio_hal_pin_to_mask((*button)->pin) & pin_mask) { - /* Ignore all button presses/releases during its debounce */ - if(ctimer_expired(&(*button)->debounce_ctimer)) { - /* - * Here we merely set a debounce timer. At the end of the debounce we - * will inspect the button's state and we will take action only if it - * has changed. - * - * This is to prevent erroneous edge detections due to interference. - */ - ctimer_set(&(*button)->debounce_ctimer, BUTTON_HAL_DEBOUNCE_DURATION, - debounce_handler, *button); - } - } - } + pmask |= pin_mask; + process_poll(&button_hal_process); } /*---------------------------------------------------------------------------*/ button_hal_button_t * @@ -186,7 +176,46 @@ button_hal_init() button_event_handler.pin_mask |= gpio_hal_pin_to_mask((*button)->pin); } + process_start(&button_hal_process, NULL); + gpio_hal_register_handler(&button_event_handler); } /*---------------------------------------------------------------------------*/ +PROCESS_THREAD(button_hal_process, ev, data) +{ + int_master_status_t status; + gpio_hal_pin_mask_t pins; + button_hal_button_t **button; + + PROCESS_BEGIN(); + + while(1) { + PROCESS_YIELD_UNTIL(ev == PROCESS_EVENT_POLL); + + status = critical_enter(); + pins = pmask; + pmask = 0; + critical_exit(status); + + for(button = button_hal_buttons; *button != NULL; button++) { + if(gpio_hal_pin_to_mask((*button)->pin) & pins) { + /* Ignore all button presses/releases during its debounce */ + if(ctimer_expired(&(*button)->debounce_ctimer)) { + /* + * Here we merely set a debounce timer. At the end of the debounce we + * will inspect the button's state and we will take action only if it + * has changed. + * + * This is to prevent erroneous edge detections due to interference. + */ + ctimer_set(&(*button)->debounce_ctimer, BUTTON_HAL_DEBOUNCE_DURATION, + debounce_handler, *button); + } + } + } + } + + PROCESS_END(); +} +/*---------------------------------------------------------------------------*/ /** @} */ From 708127dfb816230d403c2727165d3508a004fc19 Mon Sep 17 00:00:00 2001 From: George Oikonomou Date: Sun, 18 Mar 2018 00:10:10 +0000 Subject: [PATCH 23/44] Change variable to volatile --- os/dev/button-hal.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/os/dev/button-hal.c b/os/dev/button-hal.c index ec991fa48..789100324 100644 --- a/os/dev/button-hal.c +++ b/os/dev/button-hal.c @@ -55,7 +55,7 @@ process_event_t button_hal_release_event; process_event_t button_hal_periodic_event; /*---------------------------------------------------------------------------*/ /* A mask of all pins that have changed state since the last process poll */ -static gpio_hal_pin_mask_t pmask; +static volatile gpio_hal_pin_mask_t pmask; /*---------------------------------------------------------------------------*/ extern button_hal_button_t *button_hal_buttons[]; /*---------------------------------------------------------------------------*/ From 180a52b4ffdae7cebbbe29e0c01fe68afc041897 Mon Sep 17 00:00:00 2001 From: George Oikonomou Date: Sun, 18 Mar 2018 12:37:44 +0000 Subject: [PATCH 24/44] Add a way to retrieve device button count --- examples/dev/button-hal/button-hal-example.c | 1 + os/dev/button-hal.h | 9 ++++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/examples/dev/button-hal/button-hal-example.c b/examples/dev/button-hal/button-hal-example.c index 78024e28b..f4919f19e 100644 --- a/examples/dev/button-hal/button-hal-example.c +++ b/examples/dev/button-hal/button-hal-example.c @@ -46,6 +46,7 @@ PROCESS_THREAD(button_hal_example, ev, data) btn = button_hal_get_by_id(BUTTON_HAL_ID_BUTTON_ZERO); printf("Button HAL example.\n"); + printf("Device button count: %u.\n", button_hal_button_count); printf("%s on pin %u with ID=0, Logic=%s, Pull=%s\n", BUTTON_HAL_GET_DESCRIPTION(btn), btn->pin, btn->negative_logic ? "Negative" : "Positive", diff --git a/os/dev/button-hal.h b/os/dev/button-hal.h index 02fa8555f..4f35437ed 100644 --- a/os/dev/button-hal.h +++ b/os/dev/button-hal.h @@ -211,7 +211,14 @@ struct button_hal_button_s { #endif /*---------------------------------------------------------------------------*/ #define BUTTON_HAL_BUTTONS(...) \ - button_hal_button_t *button_hal_buttons[] = {__VA_ARGS__, NULL}; + button_hal_button_t *button_hal_buttons[] = {__VA_ARGS__, NULL}; \ + const uint8_t button_hal_button_count = \ + (sizeof(button_hal_buttons) / sizeof(button_hal_buttons[0])) - 1; +/*---------------------------------------------------------------------------*/ +/** + * \brief The number of buttons on a device + */ +extern const uint8_t button_hal_button_count; /*---------------------------------------------------------------------------*/ /** * \brief A broadcast event generated when a button gets pressed From a8c9931257dcb995960db6e0d815d50f367497d1 Mon Sep 17 00:00:00 2001 From: George Oikonomou Date: Sun, 18 Mar 2018 12:39:19 +0000 Subject: [PATCH 25/44] Add a function to retrieve a button by index --- examples/dev/button-hal/button-hal-example.c | 2 +- os/dev/button-hal.c | 10 ++++++++++ os/dev/button-hal.h | 7 +++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/examples/dev/button-hal/button-hal-example.c b/examples/dev/button-hal/button-hal-example.c index f4919f19e..b4d584e3e 100644 --- a/examples/dev/button-hal/button-hal-example.c +++ b/examples/dev/button-hal/button-hal-example.c @@ -43,7 +43,7 @@ PROCESS_THREAD(button_hal_example, ev, data) PROCESS_BEGIN(); - btn = button_hal_get_by_id(BUTTON_HAL_ID_BUTTON_ZERO); + btn = button_hal_get_by_index(0); printf("Button HAL example.\n"); printf("Device button count: %u.\n", button_hal_button_count); diff --git a/os/dev/button-hal.c b/os/dev/button-hal.c index 789100324..58cded790 100644 --- a/os/dev/button-hal.c +++ b/os/dev/button-hal.c @@ -141,6 +141,16 @@ button_hal_get_by_id(uint8_t unique_id) return NULL; } /*---------------------------------------------------------------------------*/ +button_hal_button_t * +button_hal_get_by_index(uint8_t index) +{ + if(index >= button_hal_button_count) { + return NULL; + } + + return button_hal_buttons[index]; +} +/*---------------------------------------------------------------------------*/ uint8_t button_hal_get_state(button_hal_button_t *button) { diff --git a/os/dev/button-hal.h b/os/dev/button-hal.h index 4f35437ed..c1822ff8e 100644 --- a/os/dev/button-hal.h +++ b/os/dev/button-hal.h @@ -247,6 +247,13 @@ void button_hal_init(void); */ button_hal_button_t *button_hal_get_by_id(uint8_t unique_id); +/** + * \brief Retrieve a button by its index + * \param index The button's index (0, 1, ... button_hal_button_count - 1) + * \return A pointer to the button or NULL if not found + */ +button_hal_button_t *button_hal_get_by_index(uint8_t index); + /** * \brief Get the state of a button (pressed / released) * \param button A pointer to the button From e19a7f1a40aef9d59af75e6343c6f5b8b06de359 Mon Sep 17 00:00:00 2001 From: Simon Duquennoy Date: Wed, 21 Mar 2018 09:31:37 -0700 Subject: [PATCH 26/44] Fix a number of comments in rpl-lite/rpl-timers.c --- os/net/routing/rpl-lite/rpl-timers.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/os/net/routing/rpl-lite/rpl-timers.c b/os/net/routing/rpl-lite/rpl-timers.c index 9bb98ed60..f7a50b858 100644 --- a/os/net/routing/rpl-lite/rpl-timers.c +++ b/os/net/routing/rpl-lite/rpl-timers.c @@ -257,8 +257,8 @@ void rpl_timers_schedule_dao(void) { if(curr_instance.used && curr_instance.mop != RPL_MOP_NO_DOWNWARD_ROUTES) { - /* No need for aggregation delay as per RFC 6550 section 9.5, as this only - * serves storing mode. Use simply delay instead, with the only PURPOSE + /* No need for DAO aggregation delay as per RFC 6550 section 9.5, as this + * only serves storing mode. Use simple delay instead, with the only purpose * to reduce congestion. */ clock_time_t expiration_time = RPL_DAO_DELAY / 2 + (random_rand() % (RPL_DAO_DELAY)); /* Increment next seqno */ @@ -306,8 +306,10 @@ handle_dao_timer(void *ptr) rpl_icmp6_dao_output(curr_instance.default_lifetime); #if !RPL_WITH_DAO_ACK - /* There is DAO-ACK, schedule a refresh. Must be done after rpl_icmp6_dao_output, - because we increment curr_instance.dag.dao_curr_seqno for the next DAO (refresh) */ + /* There is no DAO-ACK, schedule a refresh. Must be done after rpl_icmp6_dao_output, + because we increment curr_instance.dag.dao_curr_seqno for the next DAO (refresh). + Where there is DAO-ACK, the refresh is scheduled after reception of the ACK. + Happens when handle_dao_timer is called again next. */ schedule_dao_refresh(); #endif /* !RPL_WITH_DAO_ACK */ } From 039d5b4c8da5e721aba910e7836916a33340e276 Mon Sep 17 00:00:00 2001 From: Yasuyuki Tanaka Date: Thu, 22 Mar 2018 17:36:10 +0000 Subject: [PATCH 27/44] shell: increase buffer size for SHELL_OUTPUT() --- os/services/shell/shell.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/os/services/shell/shell.h b/os/services/shell/shell.h index d89672a13..de8b312ab 100644 --- a/os/services/shell/shell.h +++ b/os/services/shell/shell.h @@ -70,7 +70,7 @@ /* Printf-formatted output via a given output function */ #define SHELL_OUTPUT(output_func, format, ...) do { \ - char buffer[128]; \ + char buffer[192]; \ snprintf(buffer, sizeof(buffer), format, ##__VA_ARGS__); \ (output_func)(buffer); \ } while(0); From fb5fa58ec300921d2ca5f5a4c923353ab155a92b Mon Sep 17 00:00:00 2001 From: Yasuyuki Tanaka Date: Thu, 22 Mar 2018 17:37:14 +0000 Subject: [PATCH 28/44] shell: replace TAB with spaces --- os/services/shell/shell.h | 42 +++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/os/services/shell/shell.h b/os/services/shell/shell.h index de8b312ab..4f1b4186c 100644 --- a/os/services/shell/shell.h +++ b/os/services/shell/shell.h @@ -51,28 +51,28 @@ /* Helper macros to parse arguments */ #define SHELL_ARGS_INIT(args, next_args) (next_args) = (args); -#define SHELL_ARGS_NEXT(args, next_args) do { \ - (args) = (next_args); \ - if((args) != NULL) { \ - if(*(args) == '\0') { \ - (args) = NULL; \ - } else { \ - (next_args) = strchr((args), ' '); \ - if((next_args) != NULL) { \ - *(next_args) = '\0'; \ - (next_args)++; \ - } \ - } \ - } else { \ - (next_args) = NULL; \ - } \ - } while(0) +#define SHELL_ARGS_NEXT(args, next_args) do { \ + (args) = (next_args); \ + if((args) != NULL) { \ + if(*(args) == '\0') { \ + (args) = NULL; \ + } else { \ + (next_args) = strchr((args), ' '); \ + if((next_args) != NULL) { \ + *(next_args) = '\0'; \ + (next_args)++; \ + } \ + } \ + } else { \ + (next_args) = NULL; \ + } \ + } while(0) /* Printf-formatted output via a given output function */ -#define SHELL_OUTPUT(output_func, format, ...) do { \ - char buffer[192]; \ - snprintf(buffer, sizeof(buffer), format, ##__VA_ARGS__); \ - (output_func)(buffer); \ +#define SHELL_OUTPUT(output_func, format, ...) do { \ + char buffer[192]; \ + snprintf(buffer, sizeof(buffer), format, ##__VA_ARGS__); \ + (output_func)(buffer); \ } while(0); typedef void (shell_output_func)(const char *str); @@ -85,7 +85,7 @@ void shell_init(void); /** * \brief A protothread that is spawned by a Shell driver when receiving a new line. */ - PT_THREAD(shell_input(struct pt *pt, shell_output_func output, const char *cmd)); +PT_THREAD(shell_input(struct pt *pt, shell_output_func output, const char *cmd)); /** * Prints an IPv6 address From ecd64846a83278b0523bfa87aa333e442897a916 Mon Sep 17 00:00:00 2001 From: Yasuyuki Tanaka Date: Thu, 22 Mar 2018 17:38:56 +0000 Subject: [PATCH 29/44] shell: add NULL pointer check --- os/services/shell/shell-commands.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/os/services/shell/shell-commands.c b/os/services/shell/shell-commands.c index 5e19a0dc8..c011b780c 100644 --- a/os/services/shell/shell-commands.c +++ b/os/services/shell/shell-commands.c @@ -218,7 +218,10 @@ PT_THREAD(cmd_ping(struct pt *pt, shell_output_func output, char *args)) /* Get argument (remote IPv6) */ SHELL_ARGS_NEXT(args, next_args); - if(uiplib_ipaddrconv(args, &remote_addr) == 0) { + if(args == NULL) { + SHELL_OUTPUT(output, "Destination IPv6 address is not specified\n"); + PT_EXIT(pt); + } else if(uiplib_ipaddrconv(args, &remote_addr) == 0) { SHELL_OUTPUT(output, "Invalid IPv6: %s\n", args); PT_EXIT(pt); } From 90248b6dc20c7734629631cc76cbaccb64a076e6 Mon Sep 17 00:00:00 2001 From: Yasuyuki Tanaka Date: Thu, 22 Mar 2018 17:42:21 +0000 Subject: [PATCH 30/44] shell: minor fix on shell output message --- os/services/shell/shell-commands.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/os/services/shell/shell-commands.c b/os/services/shell/shell-commands.c index c011b780c..dd4fb95dc 100644 --- a/os/services/shell/shell-commands.c +++ b/os/services/shell/shell-commands.c @@ -222,7 +222,7 @@ PT_THREAD(cmd_ping(struct pt *pt, shell_output_func output, char *args)) SHELL_OUTPUT(output, "Destination IPv6 address is not specified\n"); PT_EXIT(pt); } else if(uiplib_ipaddrconv(args, &remote_addr) == 0) { - SHELL_OUTPUT(output, "Invalid IPv6: %s\n", args); + SHELL_OUTPUT(output, "Invalid IPv6 address: %s\n", args); PT_EXIT(pt); } From 5b0c5b7007f0da5850e0de9e6bc4dc4eafed5381 Mon Sep 17 00:00:00 2001 From: Tim van der Lee Date: Thu, 22 Mar 2018 16:23:31 +0100 Subject: [PATCH 31/44] Changed keepalive callback to avoid sending immediate keepalive when the network is congested --- os/net/mac/tsch/tsch.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/os/net/mac/tsch/tsch.c b/os/net/mac/tsch/tsch.c index 9d0affb8a..1dc0bbc7c 100644 --- a/os/net/mac/tsch/tsch.c +++ b/os/net/mac/tsch/tsch.c @@ -275,7 +275,7 @@ keepalive_packet_sent(void *ptr, int status, int transmissions) LOG_INFO_(", st %d-%d\n", status, transmissions); /* We got no ack, try to recover by switching to the last neighbor we received an EB from */ - if(status != MAC_TX_OK) { + if(status == MAC_TX_NOACK) { if(linkaddr_cmp(&last_eb_nbr_addr, &linkaddr_null)) { LOG_WARN("not able to re-synchronize, received no EB from other neighbors\n"); if(sync_count == 0) { From 6ac6e460415f1c75bfdfed90cb3f339267bde934 Mon Sep 17 00:00:00 2001 From: George Oikonomou Date: Fri, 23 Mar 2018 18:23:55 +0000 Subject: [PATCH 32/44] Provide a string define for TARGET and BOARD --- Makefile.include | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Makefile.include b/Makefile.include index e633fe67b..a37c5ad6a 100644 --- a/Makefile.include +++ b/Makefile.include @@ -40,9 +40,11 @@ LOWERCASE = -abcdefghijklmnopqrstuvwxyz/ UPPERCASE = _ABCDEFGHIJKLMNOPQRSTUVWXYZ_ TARGET_UPPERCASE := ${strip ${shell echo $(TARGET) | sed y!$(LOWERCASE)!$(UPPERCASE)!}} CFLAGS += -DCONTIKI=1 -DCONTIKI_TARGET_$(TARGET_UPPERCASE)=1 +CFLAGS += -DCONTIKI_TARGET_STRING=\"$(TARGET)\" ifneq ($(BOARD),) TARGET_BOARD_UPPERCASE := ${strip ${shell echo $(BOARD) | sed y!$(LOWERCASE)!$(UPPERCASE)!}} CFLAGS += -DCONTIKI_BOARD_$(TARGET_BOARD_UPPERCASE)=1 +CFLAGS += -DCONTIKI_BOARD_STRING=\"$(BOARD)\" endif MODULES += os os/sys os/dev os/lib os/services From 59ebacd384336853f5377fe3b1ab4aade362b927 Mon Sep 17 00:00:00 2001 From: Simon Duquennoy Date: Fri, 23 Mar 2018 12:16:02 -0700 Subject: [PATCH 33/44] Platform Cooja: port to common main --- .../platform/cooja/Makefile.customrules-cooja | 4 +- arch/platform/cooja/contiki-conf.h | 2 + .../{contiki-cooja-main.c => platform.c} | 163 ++++++------------ 3 files changed, 54 insertions(+), 115 deletions(-) rename arch/platform/cooja/{contiki-cooja-main.c => platform.c} (77%) diff --git a/arch/platform/cooja/Makefile.customrules-cooja b/arch/platform/cooja/Makefile.customrules-cooja index de35d4185..42d6e125e 100644 --- a/arch/platform/cooja/Makefile.customrules-cooja +++ b/arch/platform/cooja/Makefile.customrules-cooja @@ -32,5 +32,5 @@ $(CONTIKI_APP).cooja: $(JNILIB) # Trickiness: GNU make matches this against the file base name. # Assume that the directory part is the standard location. -mtype%.o: contiki-cooja-main.o | $(OBJECTDIR) - mv contiki-cooja-main.o $@ +mtype%.o: platform.o | $(OBJECTDIR) + mv platform.o $@ diff --git a/arch/platform/cooja/contiki-conf.h b/arch/platform/cooja/contiki-conf.h index 5069e94e5..75a37f866 100644 --- a/arch/platform/cooja/contiki-conf.h +++ b/arch/platform/cooja/contiki-conf.h @@ -42,6 +42,8 @@ #include "subplatform-conf.h" #endif /* INCLUDE_SUBPLATFORM_CONF */ +#define PLATFORM_CONF_PROVIDES_MAIN_LOOP 1 + #define LOG_CONF_ENABLED 1 #define COOJA 1 diff --git a/arch/platform/cooja/contiki-cooja-main.c b/arch/platform/cooja/platform.c similarity index 77% rename from arch/platform/cooja/contiki-cooja-main.c rename to arch/platform/cooja/platform.c index 53c8eb7d8..9ef310bce 100644 --- a/arch/platform/cooja/contiki-cooja-main.c +++ b/arch/platform/cooja/platform.c @@ -45,7 +45,12 @@ #include "sys/clock.h" #include "sys/etimer.h" #include "sys/cooja_mt.h" + +/*---------------------------------------------------------------------------*/ +/* Log configuration */ #include "sys/log.h" +#define LOG_MODULE "Cooja" +#define LOG_LEVEL LOG_LEVEL_MAIN #include "lib/random.h" #include "lib/simEnvChange.h" @@ -71,7 +76,7 @@ /* JNI-defined functions, depends on the environment variable CLASSNAME */ #ifndef CLASSNAME -#error CLASSNAME is undefined, required by contiki-cooja-main.c +#error CLASSNAME is undefined, required by platform.c #endif /* CLASSNAME */ #define COOJA__QUOTEME(a,b,c) COOJA_QUOTEME(a,b,c) #define COOJA_QUOTEME(a,b,c) a##b##c @@ -82,15 +87,14 @@ #define Java_org_contikios_cooja_corecomm_CLASSNAME_tick COOJA__QUOTEME(COOJA_JNI_PATH,CLASSNAME,_tick) #define Java_org_contikios_cooja_corecomm_CLASSNAME_setReferenceAddress COOJA__QUOTEME(COOJA_JNI_PATH,CLASSNAME,_setReferenceAddress) -#ifndef NETSTACK_CONF_WITH_IPV6 -#define NETSTACK_CONF_WITH_IPV6 0 -#endif #if NETSTACK_CONF_WITH_IPV6 #include "net/ipv6/uip.h" #include "net/ipv6/uip-ds6.h" -#define PRINT6ADDR(addr) printf("%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x", ((uint8_t *)addr)[0], ((uint8_t *)addr)[1], ((uint8_t *)addr)[2], ((uint8_t *)addr)[3], ((uint8_t *)addr)[4], ((uint8_t *)addr)[5], ((uint8_t *)addr)[6], ((uint8_t *)addr)[7], ((uint8_t *)addr)[8], ((uint8_t *)addr)[9], ((uint8_t *)addr)[10], ((uint8_t *)addr)[11], ((uint8_t *)addr)[12], ((uint8_t *)addr)[13], ((uint8_t *)addr)[14], ((uint8_t *)addr)[15]) #endif /* NETSTACK_CONF_WITH_IPV6 */ +/* The main function, implemented in contiki-main.c */ +int main(void); + /* Simulation mote interfaces */ SIM_INTERFACE_NAME(moteid_interface); SIM_INTERFACE_NAME(vib_interface); @@ -124,18 +128,6 @@ long referenceVar; static struct cooja_mt_thread rtimer_thread; static struct cooja_mt_thread process_run_thread; -/*---------------------------------------------------------------------------*/ -static void -print_processes(struct process * const processes[]) -{ - /* const struct process * const * p = processes;*/ - printf("Starting"); - while(*processes != NULL) { - printf(" '%s'", (*processes)->name); - processes++; - } - putchar('\n'); -} /*---------------------------------------------------------------------------*/ static void rtimer_thread_loop(void *data) @@ -168,120 +160,65 @@ set_lladdr(void) addr.u8[1] = node_id >> 8; #endif /* NETSTACK_CONF_WITH_IPV6 */ linkaddr_set_node_addr(&addr); - - printf("Link-layer address "); - log_lladdr(&addr); - printf("\n"); } /*---------------------------------------------------------------------------*/ void -contiki_init() +platform_init_stage_one() +{ + return; +} +/*---------------------------------------------------------------------------*/ +void +platform_init_stage_two() { - /* Initialize random generator (moved to moteid.c) */ - - /* Start process handler */ - process_init(); - - - /* Start Contiki processes */ - - process_start(&etimer_process, NULL); - process_start(&sensors_process, NULL); - ctimer_init(); - - /* Print startup information */ - printf(CONTIKI_VERSION_STRING " started. "); - if(node_id > 0) { - printf("Node id is set to %u.\n", node_id); - } else { - printf("Node id is not set.\n"); - } - set_lladdr(); - { - uint8_t longaddr[8]; - - memset(longaddr, 0, sizeof(longaddr)); - linkaddr_copy((linkaddr_t *)&longaddr, &linkaddr_node_addr); - printf("MAC %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x ", - longaddr[0], longaddr[1], longaddr[2], longaddr[3], - longaddr[4], longaddr[5], longaddr[6], longaddr[7]); +} +/*---------------------------------------------------------------------------*/ +void +platform_init_stage_three() +{ + if(node_id > 0) { + LOG_INFO("Node id is set to %u.\n", node_id); + } else { + LOG_INFO("Node id is not set.\n"); } - - /* Initialize communication stack */ - netstack_init(); - printf("%s/%s\n", - NETSTACK_NETWORK.name, NETSTACK_MAC.name); - -#if NETSTACK_CONF_WITH_IPV6 - /* IPv6 CONFIGURATION */ - { - int i; - uip_ds6_addr_t *lladdr; - uint8_t addr[sizeof(uip_lladdr.addr)]; - for(i = 0; i < sizeof(uip_lladdr.addr); i += 2) { - addr[i + 1] = node_id & 0xff; - addr[i + 0] = node_id >> 8; - } - linkaddr_copy((linkaddr_t *)addr, &linkaddr_node_addr); - memcpy(&uip_lladdr.addr, addr, sizeof(uip_lladdr.addr)); - - process_start(&tcpip_process, NULL); - - lladdr = uip_ds6_get_link_local(-1); - printf("Tentative link-local IPv6 address "); - log_6addr(lladdr != NULL ? &lladdr->ipaddr : NULL); - printf("\n"); - } -#endif /* NETSTACK_CONF_WITH_IPV6 */ - /* Initialize eeprom */ eeprom_init(); - /* Start serial process */ serial_line_init(); +} +/*---------------------------------------------------------------------------*/ +void +platform_main_loop() +{ + while(1) + { + simProcessRunValue = process_run(); + while(simProcessRunValue-- > 0) { + process_run(); + } + simProcessRunValue = process_nevents(); -#if BUILD_WITH_RPL_BORDER_ROUTER - rpl_border_router_init(); -#endif /* BUILD_WITH_RPL_BORDER_ROUTER */ -#if BUILD_WITH_ORCHESTRA - orchestra_init(); -#endif /* BUILD_WITH_ORCHESTRA */ -#if BUILD_WITH_SHELL - serial_shell_init(); -#endif /* BUILD_WITH_SHELL */ + /* Check if we must stay awake */ + if(simDontFallAsleep) { + simDontFallAsleep = 0; + simProcessRunValue = 1; + } - /* Start autostart processes (defined in Contiki application) */ - print_processes(autostart_processes); - autostart_start(autostart_processes); + /* Return to COOJA */ + cooja_mt_yield(); + } } /*---------------------------------------------------------------------------*/ static void process_run_thread_loop(void *data) { - /* Yield once during bootup */ - simProcessRunValue = 1; - cooja_mt_yield(); + /* Yield once during bootup */ + simProcessRunValue = 1; + cooja_mt_yield(); - contiki_init(); - - while(1) - { - simProcessRunValue = process_run(); - while(simProcessRunValue-- > 0) { - process_run(); - } - simProcessRunValue = process_nevents(); - - /* Check if we must stay awake */ - if(simDontFallAsleep) { - simDontFallAsleep=0; - simProcessRunValue = 1; - } - - /* Return to COOJA */ - cooja_mt_yield(); - } + /* Then call common Contiki-NG main function */ + main(); } /*---------------------------------------------------------------------------*/ /** From 59df70fcd2f43fa7b103eac80df67e4f0415b08d Mon Sep 17 00:00:00 2001 From: George Oikonomou Date: Fri, 23 Mar 2018 21:18:20 +0000 Subject: [PATCH 34/44] Fix launchpad LED colour mappings --- arch/platform/srf06-cc26xx/launchpad/cc1310/board.h | 2 +- arch/platform/srf06-cc26xx/launchpad/cc1350/board.h | 2 +- arch/platform/srf06-cc26xx/launchpad/cc2650/board.h | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/arch/platform/srf06-cc26xx/launchpad/cc1310/board.h b/arch/platform/srf06-cc26xx/launchpad/cc1310/board.h index d97086415..7c58e1db8 100644 --- a/arch/platform/srf06-cc26xx/launchpad/cc1310/board.h +++ b/arch/platform/srf06-cc26xx/launchpad/cc1310/board.h @@ -62,7 +62,7 @@ */ #define LEDS_CONF_COUNT 2 #define LEDS_CONF_RED 1 -#define LEDS_CONF_YELLOW 2 +#define LEDS_CONF_GREEN 2 /** @} */ /*---------------------------------------------------------------------------*/ /** diff --git a/arch/platform/srf06-cc26xx/launchpad/cc1350/board.h b/arch/platform/srf06-cc26xx/launchpad/cc1350/board.h index 72a7a180f..63ad25374 100644 --- a/arch/platform/srf06-cc26xx/launchpad/cc1350/board.h +++ b/arch/platform/srf06-cc26xx/launchpad/cc1350/board.h @@ -62,7 +62,7 @@ */ #define LEDS_CONF_COUNT 2 #define LEDS_CONF_RED 1 -#define LEDS_CONF_YELLOW 2 +#define LEDS_CONF_GREEN 2 /** @} */ /*---------------------------------------------------------------------------*/ /** diff --git a/arch/platform/srf06-cc26xx/launchpad/cc2650/board.h b/arch/platform/srf06-cc26xx/launchpad/cc2650/board.h index deb79a7b1..39b906e84 100644 --- a/arch/platform/srf06-cc26xx/launchpad/cc2650/board.h +++ b/arch/platform/srf06-cc26xx/launchpad/cc2650/board.h @@ -62,7 +62,7 @@ */ #define LEDS_CONF_COUNT 2 #define LEDS_CONF_RED 1 -#define LEDS_CONF_YELLOW 2 +#define LEDS_CONF_GREEN 2 /** @} */ /*---------------------------------------------------------------------------*/ /** From 22b4a40dd6ee39f3525fdf690b22ce5c71ce9a7c Mon Sep 17 00:00:00 2001 From: George Oikonomou Date: Mon, 30 Oct 2017 23:34:51 +0000 Subject: [PATCH 35/44] Cross-platform MQTT client example --- arch/cpu/cc26xx-cc13xx/dev/batmon-sensor.h | 3 + examples/mqtt-client/Makefile | 13 ++ .../mqtt-demo => mqtt-client}/README.md | 11 +- .../arch/cpu/cc2538/builtin-sensors.c | 82 +++++++ .../arch/cpu/cc2538/builtin-sensors.h | 42 ++++ .../arch/cpu/cc26xx-cc13xx/builtin-sensors.c | 82 +++++++ .../arch/cpu/cc26xx-cc13xx/builtin-sensors.h | 42 ++++ .../arch/platform/cc2538dk/Makefile.cc2538dk | 1 + .../arch/platform/cc2538dk/als-extend.c | 61 ++++++ .../arch/platform/cc2538dk/als-extend.h | 41 ++++ .../cc2538dk/mqtt-client-extensions.c | 41 ++++ .../openmote-cc2538/Makefile.openmote-cc2538 | 1 + .../openmote-cc2538/mqtt-client-extensions.c | 39 ++++ .../srf06-cc26xx/Makefile.srf06-cc26xx | 1 + .../srf06-cc26xx/mqtt-client-extensions.c | 40 ++++ .../arch/platform/zoul/Makefile.zoul | 1 + .../platform/zoul/mqtt-client-extensions.c | 39 ++++ .../mqtt-demo.c => mqtt-client/mqtt-client.c} | 207 +++++++++--------- examples/mqtt-client/mqtt-client.h | 48 ++++ .../mqtt-demo => mqtt-client}/project-conf.h | 13 +- .../cc2538-common/mqtt-demo/Makefile | 9 - .../cc2538-common/mqtt-demo/Makefile.target | 1 - 22 files changed, 692 insertions(+), 126 deletions(-) create mode 100644 examples/mqtt-client/Makefile rename examples/{platform-specific/cc2538-common/mqtt-demo => mqtt-client}/README.md (90%) create mode 100644 examples/mqtt-client/arch/cpu/cc2538/builtin-sensors.c create mode 100644 examples/mqtt-client/arch/cpu/cc2538/builtin-sensors.h create mode 100644 examples/mqtt-client/arch/cpu/cc26xx-cc13xx/builtin-sensors.c create mode 100644 examples/mqtt-client/arch/cpu/cc26xx-cc13xx/builtin-sensors.h create mode 100644 examples/mqtt-client/arch/platform/cc2538dk/Makefile.cc2538dk create mode 100644 examples/mqtt-client/arch/platform/cc2538dk/als-extend.c create mode 100644 examples/mqtt-client/arch/platform/cc2538dk/als-extend.h create mode 100644 examples/mqtt-client/arch/platform/cc2538dk/mqtt-client-extensions.c create mode 100644 examples/mqtt-client/arch/platform/openmote-cc2538/Makefile.openmote-cc2538 create mode 100644 examples/mqtt-client/arch/platform/openmote-cc2538/mqtt-client-extensions.c create mode 100644 examples/mqtt-client/arch/platform/srf06-cc26xx/Makefile.srf06-cc26xx create mode 100644 examples/mqtt-client/arch/platform/srf06-cc26xx/mqtt-client-extensions.c create mode 100644 examples/mqtt-client/arch/platform/zoul/Makefile.zoul create mode 100644 examples/mqtt-client/arch/platform/zoul/mqtt-client-extensions.c rename examples/{platform-specific/cc2538-common/mqtt-demo/mqtt-demo.c => mqtt-client/mqtt-client.c} (81%) create mode 100644 examples/mqtt-client/mqtt-client.h rename examples/{platform-specific/cc2538-common/mqtt-demo => mqtt-client}/project-conf.h (87%) delete mode 100644 examples/platform-specific/cc2538-common/mqtt-demo/Makefile delete mode 100644 examples/platform-specific/cc2538-common/mqtt-demo/Makefile.target diff --git a/arch/cpu/cc26xx-cc13xx/dev/batmon-sensor.h b/arch/cpu/cc26xx-cc13xx/dev/batmon-sensor.h index 014e9a17d..cde7abc8e 100644 --- a/arch/cpu/cc26xx-cc13xx/dev/batmon-sensor.h +++ b/arch/cpu/cc26xx-cc13xx/dev/batmon-sensor.h @@ -44,6 +44,9 @@ #ifndef BATMON_SENSOR_H_ #define BATMON_SENSOR_H_ /*---------------------------------------------------------------------------*/ +#include "contiki.h" +#include "lib/sensors.h" +/*---------------------------------------------------------------------------*/ #define BATMON_SENSOR_TYPE_TEMP 1 #define BATMON_SENSOR_TYPE_VOLT 2 /*---------------------------------------------------------------------------*/ diff --git a/examples/mqtt-client/Makefile b/examples/mqtt-client/Makefile new file mode 100644 index 000000000..c4741bec9 --- /dev/null +++ b/examples/mqtt-client/Makefile @@ -0,0 +1,13 @@ +CONTIKI_PROJECT = mqtt-client +all: $(CONTIKI_PROJECT) + +MODULES += os/net/app-layer/mqtt + +CONTIKI = ../.. +-include $(CONTIKI)/Makefile.identify-target + +MODULES_REL += arch/platform/$(TARGET) + +PLATFORMS_ONLY = srf06-cc26xx cc2538dk openmote-cc2538 zoul + +include $(CONTIKI)/Makefile.include diff --git a/examples/platform-specific/cc2538-common/mqtt-demo/README.md b/examples/mqtt-client/README.md similarity index 90% rename from examples/platform-specific/cc2538-common/mqtt-demo/README.md rename to examples/mqtt-client/README.md index 8b4e3b043..44bba8877 100644 --- a/examples/platform-specific/cc2538-common/mqtt-demo/README.md +++ b/examples/mqtt-client/README.md @@ -1,15 +1,18 @@ -MQTT Demo -========= +MQTT Client Example +=================== The MQTT client can be used to: * Publish sensor readings to an MQTT broker. * Subscribe to a topic and receive commands from an MQTT broker -The demo will give some visual feedback with the green LED: +The demo will give some visual feedback with a LED (configurable): * Very fast blinking: Searching for a network * Fast blinking: Connecting to broker * Slow, long blinking: Sending a publish message +This example is known to work with all platforms that support the new button +API. + Publishing ---------- By default the example will attempt to publish readings to an MQTT broker @@ -20,7 +23,7 @@ running on the IPv6 address specified as `MQTT_DEMO_BROKER_IP_ADDR` in The publish messages include sensor readings but also some other information, such as device uptime in seconds and a message sequence number. The demo will publish to topic `iot-2/evt/status/fmt/json`. The device will connect using -client-id `d:quickstart:cc2538:`, where `` gets +client-id `d:contiki-ng:mqtt-client:`, where `` gets constructed from the device's IEEE address. Subscribing diff --git a/examples/mqtt-client/arch/cpu/cc2538/builtin-sensors.c b/examples/mqtt-client/arch/cpu/cc2538/builtin-sensors.c new file mode 100644 index 000000000..f22c33835 --- /dev/null +++ b/examples/mqtt-client/arch/cpu/cc2538/builtin-sensors.c @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2017, George Oikonomou - http://www.spd.gr + * 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 copyright holder 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 COPYRIGHT HOLDERS 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 + * COPYRIGHT HOLDER 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. + */ +/*---------------------------------------------------------------------------*/ +#include "contiki.h" +#include "dev/cc2538-sensors.h" +#include "mqtt-client.h" + +#include +#include +/*---------------------------------------------------------------------------*/ +#define TMP_BUF_SZ 32 +/*---------------------------------------------------------------------------*/ +char tmp_buf[TMP_BUF_SZ]; +/*---------------------------------------------------------------------------*/ +static char * +temp_reading(void) +{ + memset(tmp_buf, 0, TMP_BUF_SZ); + snprintf(tmp_buf, TMP_BUF_SZ, "\"On-Chip Temp (mC)\":%d", + cc2538_temp_sensor.value(CC2538_SENSORS_VALUE_TYPE_CONVERTED)); + return tmp_buf; +} +/*---------------------------------------------------------------------------*/ +static void +temp_init(void) +{ + SENSORS_ACTIVATE(cc2538_temp_sensor); +} +/*---------------------------------------------------------------------------*/ +const mqtt_client_extension_t builtin_sensors_cc2538_temp = { + temp_init, + temp_reading, +}; +/*---------------------------------------------------------------------------*/ +static char * +vdd3_reading(void) +{ + memset(tmp_buf, 0, TMP_BUF_SZ); + snprintf(tmp_buf, TMP_BUF_SZ, "\"VDD3 (mV)\":%d", + vdd3_sensor.value(CC2538_SENSORS_VALUE_TYPE_CONVERTED)); + return tmp_buf; +} +/*---------------------------------------------------------------------------*/ +static void +vdd3_init(void) +{ + SENSORS_ACTIVATE(vdd3_sensor); +} +/*---------------------------------------------------------------------------*/ +const mqtt_client_extension_t builtin_sensors_vdd3 = { + vdd3_init, + vdd3_reading, +}; +/*---------------------------------------------------------------------------*/ diff --git a/examples/mqtt-client/arch/cpu/cc2538/builtin-sensors.h b/examples/mqtt-client/arch/cpu/cc2538/builtin-sensors.h new file mode 100644 index 000000000..50a765b52 --- /dev/null +++ b/examples/mqtt-client/arch/cpu/cc2538/builtin-sensors.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2017, George Oikonomou - http://www.spd.gr + * 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 copyright holder 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 COPYRIGHT HOLDERS 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 + * COPYRIGHT HOLDER 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 BUILTIN_SENSORS_H_ +#define BUILTIN_SENSORS_H_ +/*---------------------------------------------------------------------------*/ +#include "contiki.h" +#include "mqtt-client.h" +/*---------------------------------------------------------------------------*/ +extern const mqtt_client_extension_t builtin_sensors_vdd3; +extern const mqtt_client_extension_t builtin_sensors_cc2538_temp; +/*---------------------------------------------------------------------------*/ +#endif /* BUILTIN_SENSORS_H_ */ +/*---------------------------------------------------------------------------*/ diff --git a/examples/mqtt-client/arch/cpu/cc26xx-cc13xx/builtin-sensors.c b/examples/mqtt-client/arch/cpu/cc26xx-cc13xx/builtin-sensors.c new file mode 100644 index 000000000..3b346fc1b --- /dev/null +++ b/examples/mqtt-client/arch/cpu/cc26xx-cc13xx/builtin-sensors.c @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2017, George Oikonomou - http://www.spd.gr + * 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 copyright holder 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 COPYRIGHT HOLDERS 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 + * COPYRIGHT HOLDER 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. + */ +/*---------------------------------------------------------------------------*/ +#include "contiki.h" +#include "batmon-sensor.h" +#include "mqtt-client.h" + +#include +#include +/*---------------------------------------------------------------------------*/ +#define TMP_BUF_SZ 32 +/*---------------------------------------------------------------------------*/ +char tmp_buf[TMP_BUF_SZ]; +/*---------------------------------------------------------------------------*/ +static char * +temp_reading(void) +{ + memset(tmp_buf, 0, TMP_BUF_SZ); + snprintf(tmp_buf, TMP_BUF_SZ, "\"On-Chip Temp (mC)\":%d", + batmon_sensor.value(BATMON_SENSOR_TYPE_TEMP)); + return tmp_buf; +} +/*---------------------------------------------------------------------------*/ +static void +temp_init(void) +{ + SENSORS_ACTIVATE(batmon_sensor); +} +/*---------------------------------------------------------------------------*/ +const mqtt_client_extension_t builtin_sensors_batmon_temp = { + temp_init, + temp_reading, +}; +/*---------------------------------------------------------------------------*/ +static char * +volt_reading(void) +{ + memset(tmp_buf, 0, TMP_BUF_SZ); + snprintf(tmp_buf, TMP_BUF_SZ, "\"Volt (mV)\":%d", + (batmon_sensor.value(BATMON_SENSOR_TYPE_VOLT) * 125) >> 5); + return tmp_buf; +} +/*---------------------------------------------------------------------------*/ +static void +volt_init(void) +{ + SENSORS_ACTIVATE(batmon_sensor); +} +/*---------------------------------------------------------------------------*/ +const mqtt_client_extension_t builtin_sensors_batmon_volt = { + volt_init, + volt_reading, +}; +/*---------------------------------------------------------------------------*/ diff --git a/examples/mqtt-client/arch/cpu/cc26xx-cc13xx/builtin-sensors.h b/examples/mqtt-client/arch/cpu/cc26xx-cc13xx/builtin-sensors.h new file mode 100644 index 000000000..38af3f62a --- /dev/null +++ b/examples/mqtt-client/arch/cpu/cc26xx-cc13xx/builtin-sensors.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2017, George Oikonomou - http://www.spd.gr + * 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 copyright holder 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 COPYRIGHT HOLDERS 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 + * COPYRIGHT HOLDER 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 BUILTIN_SENSORS_H_ +#define BUILTIN_SENSORS_H_ +/*---------------------------------------------------------------------------*/ +#include "contiki.h" +#include "mqtt-client.h" +/*---------------------------------------------------------------------------*/ +extern const mqtt_client_extension_t builtin_sensors_batmon_temp; +extern const mqtt_client_extension_t builtin_sensors_batmon_volt; +/*---------------------------------------------------------------------------*/ +#endif /* BUILTIN_SENSORS_H_ */ +/*---------------------------------------------------------------------------*/ diff --git a/examples/mqtt-client/arch/platform/cc2538dk/Makefile.cc2538dk b/examples/mqtt-client/arch/platform/cc2538dk/Makefile.cc2538dk new file mode 100644 index 000000000..fcdd0583a --- /dev/null +++ b/examples/mqtt-client/arch/platform/cc2538dk/Makefile.cc2538dk @@ -0,0 +1 @@ +MODULES_REL += arch/cpu/cc2538 diff --git a/examples/mqtt-client/arch/platform/cc2538dk/als-extend.c b/examples/mqtt-client/arch/platform/cc2538dk/als-extend.c new file mode 100644 index 000000000..7f9845a42 --- /dev/null +++ b/examples/mqtt-client/arch/platform/cc2538dk/als-extend.c @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2018, George Oikonomou - http://www.spd.gr + * 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 copyright holder 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 COPYRIGHT HOLDERS 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 + * COPYRIGHT HOLDER 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. + */ +/*---------------------------------------------------------------------------*/ +#include "contiki.h" +#include "als-extend.h" +#include "dev/als-sensor.h" + +#include +#include +/*---------------------------------------------------------------------------*/ +#define TMP_BUF_SZ 32 +/*---------------------------------------------------------------------------*/ +char tmp_buf[TMP_BUF_SZ]; +/*---------------------------------------------------------------------------*/ +static void +als_init(void) +{ + SENSORS_ACTIVATE(als_sensor); +} +/*---------------------------------------------------------------------------*/ +static char * +als_reading(void) +{ + memset(tmp_buf, 0, TMP_BUF_SZ); + snprintf(tmp_buf, TMP_BUF_SZ, "\"ALS (raw)\":%d", als_sensor.value(0)); + return tmp_buf; +} +/*---------------------------------------------------------------------------*/ +const mqtt_client_extension_t als_extend = { + als_init, + als_reading, +}; +/*---------------------------------------------------------------------------*/ diff --git a/examples/mqtt-client/arch/platform/cc2538dk/als-extend.h b/examples/mqtt-client/arch/platform/cc2538dk/als-extend.h new file mode 100644 index 000000000..f9b8dc9b0 --- /dev/null +++ b/examples/mqtt-client/arch/platform/cc2538dk/als-extend.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2018, George Oikonomou - http://www.spd.gr + * 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 copyright holder 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 COPYRIGHT HOLDERS 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 + * COPYRIGHT HOLDER 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 ALS_EXTEND_H_ +#define ALS_EXTEND_H_ +/*---------------------------------------------------------------------------*/ +#include "contiki.h" +#include "mqtt-client.h" +/*---------------------------------------------------------------------------*/ +extern const mqtt_client_extension_t als_extend; +/*---------------------------------------------------------------------------*/ +#endif /* ALS_EXTEND_H_ */ +/*---------------------------------------------------------------------------*/ diff --git a/examples/mqtt-client/arch/platform/cc2538dk/mqtt-client-extensions.c b/examples/mqtt-client/arch/platform/cc2538dk/mqtt-client-extensions.c new file mode 100644 index 000000000..835b95fa6 --- /dev/null +++ b/examples/mqtt-client/arch/platform/cc2538dk/mqtt-client-extensions.c @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2017, George Oikonomou - http://www.spd.gr + * 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 copyright holder 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 COPYRIGHT HOLDERS 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 + * COPYRIGHT HOLDER 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. + */ +/*---------------------------------------------------------------------------*/ +#include "contiki.h" +#include "builtin-sensors.h" +#include "als-extend.h" +#include "mqtt-client.h" + +#include +/*---------------------------------------------------------------------------*/ +MQTT_CLIENT_EXTENSIONS(&builtin_sensors_vdd3, &builtin_sensors_cc2538_temp, + &als_extend); +/*---------------------------------------------------------------------------*/ diff --git a/examples/mqtt-client/arch/platform/openmote-cc2538/Makefile.openmote-cc2538 b/examples/mqtt-client/arch/platform/openmote-cc2538/Makefile.openmote-cc2538 new file mode 100644 index 000000000..fcdd0583a --- /dev/null +++ b/examples/mqtt-client/arch/platform/openmote-cc2538/Makefile.openmote-cc2538 @@ -0,0 +1 @@ +MODULES_REL += arch/cpu/cc2538 diff --git a/examples/mqtt-client/arch/platform/openmote-cc2538/mqtt-client-extensions.c b/examples/mqtt-client/arch/platform/openmote-cc2538/mqtt-client-extensions.c new file mode 100644 index 000000000..022c28e84 --- /dev/null +++ b/examples/mqtt-client/arch/platform/openmote-cc2538/mqtt-client-extensions.c @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2017, George Oikonomou - http://www.spd.gr + * 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 copyright holder 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 COPYRIGHT HOLDERS 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 + * COPYRIGHT HOLDER 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. + */ +/*---------------------------------------------------------------------------*/ +#include "contiki.h" +#include "builtin-sensors.h" +#include "mqtt-client.h" + +#include +/*---------------------------------------------------------------------------*/ +MQTT_CLIENT_EXTENSIONS(&builtin_sensors_vdd3, &builtin_sensors_cc2538_temp); +/*---------------------------------------------------------------------------*/ diff --git a/examples/mqtt-client/arch/platform/srf06-cc26xx/Makefile.srf06-cc26xx b/examples/mqtt-client/arch/platform/srf06-cc26xx/Makefile.srf06-cc26xx new file mode 100644 index 000000000..dcec87bad --- /dev/null +++ b/examples/mqtt-client/arch/platform/srf06-cc26xx/Makefile.srf06-cc26xx @@ -0,0 +1 @@ +MODULES_REL += arch/cpu/cc26xx-cc13xx diff --git a/examples/mqtt-client/arch/platform/srf06-cc26xx/mqtt-client-extensions.c b/examples/mqtt-client/arch/platform/srf06-cc26xx/mqtt-client-extensions.c new file mode 100644 index 000000000..559a4ee9b --- /dev/null +++ b/examples/mqtt-client/arch/platform/srf06-cc26xx/mqtt-client-extensions.c @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2017, George Oikonomou - http://www.spd.gr + * 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 copyright holder 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 COPYRIGHT HOLDERS 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 + * COPYRIGHT HOLDER 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. + */ +/*---------------------------------------------------------------------------*/ +#include "contiki.h" +#include "builtin-sensors.h" +#include "mqtt-client.h" + +#include +/*---------------------------------------------------------------------------*/ +MQTT_CLIENT_EXTENSIONS(&builtin_sensors_batmon_temp, + &builtin_sensors_batmon_volt); +/*---------------------------------------------------------------------------*/ diff --git a/examples/mqtt-client/arch/platform/zoul/Makefile.zoul b/examples/mqtt-client/arch/platform/zoul/Makefile.zoul new file mode 100644 index 000000000..fcdd0583a --- /dev/null +++ b/examples/mqtt-client/arch/platform/zoul/Makefile.zoul @@ -0,0 +1 @@ +MODULES_REL += arch/cpu/cc2538 diff --git a/examples/mqtt-client/arch/platform/zoul/mqtt-client-extensions.c b/examples/mqtt-client/arch/platform/zoul/mqtt-client-extensions.c new file mode 100644 index 000000000..022c28e84 --- /dev/null +++ b/examples/mqtt-client/arch/platform/zoul/mqtt-client-extensions.c @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2017, George Oikonomou - http://www.spd.gr + * 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 copyright holder 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 COPYRIGHT HOLDERS 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 + * COPYRIGHT HOLDER 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. + */ +/*---------------------------------------------------------------------------*/ +#include "contiki.h" +#include "builtin-sensors.h" +#include "mqtt-client.h" + +#include +/*---------------------------------------------------------------------------*/ +MQTT_CLIENT_EXTENSIONS(&builtin_sensors_vdd3, &builtin_sensors_cc2538_temp); +/*---------------------------------------------------------------------------*/ diff --git a/examples/platform-specific/cc2538-common/mqtt-demo/mqtt-demo.c b/examples/mqtt-client/mqtt-client.c similarity index 81% rename from examples/platform-specific/cc2538-common/mqtt-demo/mqtt-demo.c rename to examples/mqtt-client/mqtt-client.c index 5ed836cc1..867ef9982 100644 --- a/examples/platform-specific/cc2538-common/mqtt-demo/mqtt-demo.c +++ b/examples/mqtt-client/mqtt-client.c @@ -1,5 +1,6 @@ /* * Copyright (c) 2014, Texas Instruments Incorporated - http://www.ti.com/ + * Copyright (c) 2017, George Oikonomou - http://www.spd.gr * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -28,19 +29,6 @@ * OF THE POSSIBILITY OF SUCH DAMAGE. */ /*---------------------------------------------------------------------------*/ -/** \addtogroup cc2538-examples - * @{ - * - * \defgroup cc2538-mqtt-demo CC2538 MQTT Demo Project - * - * Demonstrates MQTT functionality. Works with IBM Quickstart as well as - * mosquitto. - * @{ - * - * \file - * An MQTT example for the cc2538-based platforms - */ -/*---------------------------------------------------------------------------*/ #include "contiki.h" #include "net/routing/routing.h" #include "mqtt.h" @@ -52,11 +40,15 @@ #include "lib/sensors.h" #include "dev/button-hal.h" #include "dev/leds.h" -#include "dev/cc2538-sensors.h" +#include "os/sys/log.h" +#include "mqtt-client.h" #include #include /*---------------------------------------------------------------------------*/ +#define LOG_MODULE "mqtt-client" +#define LOG_LEVEL LOG_LEVEL_NONE +/*---------------------------------------------------------------------------*/ /* * IBM server: messaging.quickstart.internetofthings.ibmcloud.com * (184.172.124.189) mapped in an NAT64 (prefix 64:ff9b::/96) IPv6 address @@ -65,14 +57,20 @@ * Alternatively, publish to a local MQTT broker (e.g. mosquitto) running on * the node that hosts your border router */ -#ifdef MQTT_DEMO_BROKER_IP_ADDR -static const char *broker_ip = MQTT_DEMO_BROKER_IP_ADDR; -#define DEFAULT_ORG_ID "mqtt-demo" +#ifdef MQTT_CLIENT_CONF_BROKER_IP_ADDR +static const char *broker_ip = MQTT_CLIENT_CONF_BROKER_IP_ADDR; +#define DEFAULT_ORG_ID "contiki-ng" #else static const char *broker_ip = "0064:ff9b:0000:0000:0000:0000:b8ac:7cbd"; #define DEFAULT_ORG_ID "quickstart" #endif /*---------------------------------------------------------------------------*/ +#ifdef MQTT_CLIENT_CONF_STATUS_LED +#define MQTT_CLIENT_STATUS_LED MQTT_CLIENT_CONF_STATUS_LED +#else +#define MQTT_CLIENT_STATUS_LED LEDS_GREEN +#endif +/*---------------------------------------------------------------------------*/ /* * A timeout used when waiting for something to happen (e.g. to connect or to * disconnect) @@ -128,7 +126,7 @@ static uint8_t state; #define NO_NET_LED_DURATION (NET_CONNECT_PERIODIC >> 1) /*---------------------------------------------------------------------------*/ /* Default configuration values */ -#define DEFAULT_TYPE_ID "cc2538" +#define DEFAULT_TYPE_ID "mqtt-client" #define DEFAULT_AUTH_TOKEN "AUTHZ" #define DEFAULT_EVENT_TYPE_ID "status" #define DEFAULT_SUBSCRIBE_CMD_TYPE "+" @@ -137,14 +135,13 @@ static uint8_t state; #define DEFAULT_KEEP_ALIVE_TIMER 60 #define DEFAULT_RSSI_MEAS_INTERVAL (CLOCK_SECOND * 30) /*---------------------------------------------------------------------------*/ -/* Take a sensor reading on button press */ -#define PUBLISH_TRIGGER BUTTON_HAL_ID_USER_BUTTON - +#define MQTT_CLIENT_SENSOR_NONE (void *)0xFFFFFFFF +/*---------------------------------------------------------------------------*/ /* Payload length of ICMPv6 echo requests used to measure RSSI with def rt */ #define ECHO_REQ_PAYLOAD_LEN 20 /*---------------------------------------------------------------------------*/ -PROCESS_NAME(mqtt_demo_process); -AUTOSTART_PROCESSES(&mqtt_demo_process); +PROCESS_NAME(mqtt_client_process); +AUTOSTART_PROCESSES(&mqtt_client_process); /*---------------------------------------------------------------------------*/ /** * \brief Data structure declaration for the MQTT client configuration @@ -164,8 +161,6 @@ typedef struct mqtt_client_config { /* Maximum TCP segment size for outgoing segments of our socket */ #define MAX_TCP_SEGMENT_SIZE 32 /*---------------------------------------------------------------------------*/ -#define STATUS_LED LEDS_GREEN -/*---------------------------------------------------------------------------*/ /* * Buffers for Client ID and Topic. * Make sure they are large enough to hold the entire respective string @@ -202,9 +197,12 @@ static int def_rt_rssi = 0; /*---------------------------------------------------------------------------*/ static mqtt_client_config_t conf; /*---------------------------------------------------------------------------*/ -PROCESS(mqtt_demo_process, "MQTT Demo"); +extern const mqtt_client_extension_t *mqtt_client_extensions[]; +extern const uint8_t mqtt_client_extension_count; /*---------------------------------------------------------------------------*/ -int +PROCESS(mqtt_client_process, "MQTT Client"); +/*---------------------------------------------------------------------------*/ +static int ipaddr_sprintf(char *buf, uint8_t buf_len, const uip_ipaddr_t *addr) { uint16_t a; @@ -241,25 +239,25 @@ echo_reply_handler(uip_ipaddr_t *source, uint8_t ttl, uint8_t *data, static void publish_led_off(void *d) { - leds_off(STATUS_LED); + leds_off(MQTT_CLIENT_STATUS_LED); } /*---------------------------------------------------------------------------*/ static void pub_handler(const char *topic, uint16_t topic_len, const uint8_t *chunk, uint16_t chunk_len) { - DBG("Pub Handler: topic='%s' (len=%u), chunk_len=%u\n", topic, topic_len, - chunk_len); + LOG_DBG("Pub Handler: topic='%s' (len=%u), chunk_len=%u\n", topic, + topic_len, chunk_len); /* If we don't like the length, ignore */ if(topic_len != 23 || chunk_len != 1) { - printf("Incorrect topic or chunk len. Ignored\n"); + LOG_ERR("Incorrect topic or chunk len. Ignored\n"); return; } /* If the format != json, ignore */ if(strncmp(&topic[topic_len - 4], "json", 4) != 0) { - printf("Incorrect format\n"); + LOG_ERR("Incorrect format\n"); } if(strncmp(&topic[10], "leds", 4) == 0) { @@ -277,16 +275,16 @@ mqtt_event(struct mqtt_connection *m, mqtt_event_t event, void *data) { switch(event) { case MQTT_EVENT_CONNECTED: { - DBG("APP - Application has a MQTT connection\n"); + LOG_DBG("Application has a MQTT connection\n"); timer_set(&connection_life, CONNECTION_STABLE_TIME); state = STATE_CONNECTED; break; } case MQTT_EVENT_DISCONNECTED: { - DBG("APP - MQTT Disconnect. Reason %u\n", *((mqtt_event_t *)data)); + LOG_DBG("MQTT Disconnect. Reason %u\n", *((mqtt_event_t *)data)); state = STATE_DISCONNECTED; - process_poll(&mqtt_demo_process); + process_poll(&mqtt_client_process); break; } case MQTT_EVENT_PUBLISH: { @@ -295,29 +293,28 @@ mqtt_event(struct mqtt_connection *m, mqtt_event_t event, void *data) /* Implement first_flag in publish message? */ if(msg_ptr->first_chunk) { msg_ptr->first_chunk = 0; - DBG("APP - Application received a publish on topic '%s'. Payload " - "size is %i bytes. Content:\n\n", - msg_ptr->topic, msg_ptr->payload_length); + LOG_DBG("Application received publish for topic '%s'. Payload " + "size is %i bytes.\n", msg_ptr->topic, msg_ptr->payload_length); } - pub_handler(msg_ptr->topic, strlen(msg_ptr->topic), msg_ptr->payload_chunk, - msg_ptr->payload_length); + pub_handler(msg_ptr->topic, strlen(msg_ptr->topic), + msg_ptr->payload_chunk, msg_ptr->payload_length); break; } case MQTT_EVENT_SUBACK: { - DBG("APP - Application is subscribed to topic successfully\n"); + LOG_DBG("Application is subscribed to topic successfully\n"); break; } case MQTT_EVENT_UNSUBACK: { - DBG("APP - Application is unsubscribed to topic successfully\n"); + LOG_DBG("Application is unsubscribed to topic successfully\n"); break; } case MQTT_EVENT_PUBACK: { - DBG("APP - Publishing complete.\n"); + LOG_DBG("Publishing complete.\n"); break; } default: - DBG("APP - Application got a unhandled MQTT event: %i\n", event); + LOG_DBG("Application got a unhandled MQTT event: %i\n", event); break; } } @@ -330,7 +327,7 @@ construct_pub_topic(void) /* len < 0: Error. Len >= BUFFER_SIZE: Buffer too small */ if(len < 0 || len >= BUFFER_SIZE) { - printf("Pub Topic: %d, Buffer %d\n", len, BUFFER_SIZE); + LOG_INFO("Pub Topic: %d, Buffer %d\n", len, BUFFER_SIZE); return 0; } @@ -345,7 +342,7 @@ construct_sub_topic(void) /* len < 0: Error. Len >= BUFFER_SIZE: Buffer too small */ if(len < 0 || len >= BUFFER_SIZE) { - printf("Sub Topic: %d, Buffer %d\n", len, BUFFER_SIZE); + LOG_INFO("Sub Topic: %d, Buffer %d\n", len, BUFFER_SIZE); return 0; } @@ -363,7 +360,7 @@ construct_client_id(void) /* len < 0: Error. Len >= BUFFER_SIZE: Buffer too small */ if(len < 0 || len >= BUFFER_SIZE) { - printf("Client ID: %d, Buffer %d\n", len, BUFFER_SIZE); + LOG_ERR("Client ID: %d, Buffer %d\n", len, BUFFER_SIZE); return 0; } @@ -438,9 +435,9 @@ subscribe(void) status = mqtt_subscribe(&conn, NULL, sub_topic, MQTT_QOS_LEVEL_0); - DBG("APP - Subscribing!\n"); + LOG_DBG("Subscribing!\n"); if(status == MQTT_STATUS_OUT_QUEUE_FULL) { - DBG("APP - Tried to subscribe but command queue was full!\n"); + LOG_ERR("Tried to subscribe but command queue was full!\n"); } } /*---------------------------------------------------------------------------*/ @@ -450,6 +447,7 @@ publish(void) /* Publish MQTT topic in IBM quickstart format */ int len; int remaining = APP_BUFFER_SIZE; + int i; seq_nr_value++; @@ -458,13 +456,17 @@ publish(void) len = snprintf(buf_ptr, remaining, "{" "\"d\":{" - "\"myName\":\"%s\"," + "\"Platform\":\""CONTIKI_TARGET_STRING"\"," +#ifdef CONTIKI_BOARD_STRING + "\"Board\":\""CONTIKI_BOARD_STRING"\"," +#endif "\"Seq #\":%d," "\"Uptime (sec)\":%lu", - BOARD_STRING, seq_nr_value, clock_seconds()); + seq_nr_value, clock_seconds()); if(len < 0 || len >= remaining) { - printf("Buffer too short. Have %d, need %d + \\0\n", remaining, len); + LOG_ERR("Buffer too short. Have %d, need %d + \\0\n", remaining, + len); return; } @@ -476,47 +478,43 @@ publish(void) memset(def_rt_str, 0, sizeof(def_rt_str)); ipaddr_sprintf(def_rt_str, sizeof(def_rt_str), uip_ds6_defrt_choose()); - len = snprintf(buf_ptr, remaining, ",\"Def Route\":\"%s\",\"RSSI (dBm)\":%d", + len = snprintf(buf_ptr, remaining, + ",\"Def Route\":\"%s\",\"RSSI (dBm)\":%d", def_rt_str, def_rt_rssi); if(len < 0 || len >= remaining) { - printf("Buffer too short. Have %d, need %d + \\0\n", remaining, len); + LOG_ERR("Buffer too short. Have %d, need %d + \\0\n", remaining, + len); return; } remaining -= len; buf_ptr += len; - len = snprintf(buf_ptr, remaining, ",\"On-Chip Temp (mC)\":%d", - cc2538_temp_sensor.value(CC2538_SENSORS_VALUE_TYPE_CONVERTED)); + for(i = 0; i < mqtt_client_extension_count; i++) { + len = snprintf(buf_ptr, remaining, ",%s", + mqtt_client_extensions[i]->value()); - if(len < 0 || len >= remaining) { - printf("Buffer too short. Have %d, need %d + \\0\n", remaining, len); - return; + if(len < 0 || len >= remaining) { + LOG_ERR("Buffer too short. Have %d, need %d + \\0\n", remaining, + len); + return; + } + remaining -= len; + buf_ptr += len; } - remaining -= len; - buf_ptr += len; - - len = snprintf(buf_ptr, remaining, ",\"VDD3 (mV)\":%d", - vdd3_sensor.value(CC2538_SENSORS_VALUE_TYPE_CONVERTED)); - - if(len < 0 || len >= remaining) { - printf("Buffer too short. Have %d, need %d + \\0\n", remaining, len); - return; - } - remaining -= len; - buf_ptr += len; len = snprintf(buf_ptr, remaining, "}}"); if(len < 0 || len >= remaining) { - printf("Buffer too short. Have %d, need %d + \\0\n", remaining, len); + LOG_ERR("Buffer too short. Have %d, need %d + \\0\n", remaining, + len); return; } mqtt_publish(&conn, NULL, pub_topic, (uint8_t *)app_buffer, strlen(app_buffer), MQTT_QOS_LEVEL_0, MQTT_RETAIN_OFF); - DBG("APP - Publish!\n"); + LOG_DBG("Publish!\n"); } /*---------------------------------------------------------------------------*/ static void @@ -546,7 +544,7 @@ state_machine(void) switch(state) { case STATE_INIT: /* If we have just been configured register MQTT connection */ - mqtt_register(&conn, &mqtt_demo_process, client_id, mqtt_event, + mqtt_register(&conn, &mqtt_client_process, client_id, mqtt_event, MAX_TCP_SEGMENT_SIZE); /* @@ -555,7 +553,7 @@ state_machine(void) */ if(strncasecmp(conf.org_id, QUICKSTART, strlen(conf.org_id)) != 0) { if(strlen(conf.auth_token) == 0) { - printf("User name set, but empty auth token\n"); + LOG_ERR("User name set, but empty auth token\n"); state = STATE_ERROR; break; } else { @@ -569,31 +567,31 @@ state_machine(void) connect_attempt = 1; state = STATE_REGISTERED; - DBG("Init\n"); + LOG_DBG("Init\n"); /* Continue */ case STATE_REGISTERED: if(uip_ds6_get_global(ADDR_PREFERRED) != NULL) { /* Registered and with a public IP. Connect */ - DBG("Registered. Connect attempt %u\n", connect_attempt); + LOG_DBG("Registered. Connect attempt %u\n", connect_attempt); ping_parent(); connect_to_broker(); } else { - leds_on(STATUS_LED); + leds_on(MQTT_CLIENT_STATUS_LED); ctimer_set(&ct, NO_NET_LED_DURATION, publish_led_off, NULL); } etimer_set(&publish_periodic_timer, NET_CONNECT_PERIODIC); return; break; case STATE_CONNECTING: - leds_on(STATUS_LED); + leds_on(MQTT_CLIENT_STATUS_LED); ctimer_set(&ct, CONNECTING_LED_DURATION, publish_led_off, NULL); /* Not connected yet. Wait */ - DBG("Connecting (%u)\n", connect_attempt); + LOG_DBG("Connecting (%u)\n", connect_attempt); break; case STATE_CONNECTED: /* Don't subscribe unless we are a registered device */ if(strncasecmp(conf.org_id, QUICKSTART, strlen(conf.org_id)) == 0) { - DBG("Using 'quickstart': Skipping subscribe\n"); + LOG_DBG("Using 'quickstart': Skipping subscribe\n"); state = STATE_PUBLISHING; } /* Continue */ @@ -613,13 +611,12 @@ state_machine(void) subscribe(); state = STATE_PUBLISHING; } else { - leds_on(STATUS_LED); + leds_on(MQTT_CLIENT_STATUS_LED); ctimer_set(&ct, PUBLISH_LED_ON_DURATION, publish_led_off, NULL); + LOG_DBG("Publishing\n"); publish(); } etimer_set(&publish_periodic_timer, conf.pub_interval); - - DBG("Publishing\n"); /* Return here so we don't end up rescheduling the timer */ return; } else { @@ -632,12 +629,12 @@ state_machine(void) * trigger a new message and we wait for TCP to either ACK the entire * packet after retries, or to timeout and notify us. */ - DBG("Publishing... (MQTT state=%d, q=%u)\n", conn.state, - conn.out_queue_full); + LOG_DBG("Publishing... (MQTT state=%d, q=%u)\n", conn.state, + conn.out_queue_full); } break; case STATE_DISCONNECTED: - DBG("Disconnected\n"); + LOG_DBG("Disconnected\n"); if(connect_attempt < RECONNECT_ATTEMPTS || RECONNECT_ATTEMPTS == RETRY_FOREVER) { /* Disconnect and backoff */ @@ -648,7 +645,7 @@ state_machine(void) interval = connect_attempt < 3 ? RECONNECT_INTERVAL << connect_attempt : RECONNECT_INTERVAL << 3; - DBG("Disconnected. Attempt %u in %lu ticks\n", connect_attempt, interval); + LOG_DBG("Disconnected. Attempt %u in %lu ticks\n", connect_attempt, interval); etimer_set(&publish_periodic_timer, interval); @@ -657,23 +654,23 @@ state_machine(void) } else { /* Max reconnect attempts reached. Enter error state */ state = STATE_ERROR; - DBG("Aborting connection after %u attempts\n", connect_attempt - 1); + LOG_DBG("Aborting connection after %u attempts\n", connect_attempt - 1); } break; case STATE_CONFIG_ERROR: /* Idle away. The only way out is a new config */ - printf("Bad configuration.\n"); + LOG_ERR("Bad configuration.\n"); return; case STATE_ERROR: default: - leds_on(STATUS_LED); + leds_on(MQTT_CLIENT_STATUS_LED); /* * 'default' should never happen. * * If we enter here it's because of some error. Stop timers. The only thing * that can bring us out is a new config event */ - printf("Default case: State=0x%02x\n", state); + LOG_ERR("Default case: State=0x%02x\n", state); return; } @@ -681,17 +678,31 @@ state_machine(void) etimer_set(&publish_periodic_timer, STATE_MACHINE_PERIODIC); } /*---------------------------------------------------------------------------*/ -PROCESS_THREAD(mqtt_demo_process, ev, data) +static void +init_extensions(void) +{ + int i; + + for(i = 0; i < mqtt_client_extension_count; i++) { + if(mqtt_client_extensions[i]->init) { + mqtt_client_extensions[i]->init(); + } + } +} +/*---------------------------------------------------------------------------*/ +PROCESS_THREAD(mqtt_client_process, ev, data) { PROCESS_BEGIN(); - printf("MQTT Demo Process\n"); + printf("MQTT Client Process\n"); if(init_config() != 1) { PROCESS_EXIT(); } + init_extensions(); + update_config(); def_rt_rssi = 0x8000000; @@ -705,7 +716,7 @@ PROCESS_THREAD(mqtt_demo_process, ev, data) PROCESS_YIELD(); if(ev == button_hal_release_event && - ((button_hal_button_t *)data)->unique_id == PUBLISH_TRIGGER) { + ((button_hal_button_t *)data)->unique_id == BUTTON_HAL_ID_BUTTON_ZERO) { if(state == STATE_ERROR) { connect_attempt = 1; state = STATE_REGISTERED; @@ -715,7 +726,7 @@ PROCESS_THREAD(mqtt_demo_process, ev, data) if((ev == PROCESS_EVENT_TIMER && data == &publish_periodic_timer) || ev == PROCESS_EVENT_POLL || (ev == button_hal_release_event && - ((button_hal_button_t *)data)->unique_id == PUBLISH_TRIGGER)) { + ((button_hal_button_t *)data)->unique_id == BUTTON_HAL_ID_BUTTON_ZERO)) { state_machine(); } @@ -728,7 +739,3 @@ PROCESS_THREAD(mqtt_demo_process, ev, data) PROCESS_END(); } /*---------------------------------------------------------------------------*/ -/** - * @} - * @} - */ diff --git a/examples/mqtt-client/mqtt-client.h b/examples/mqtt-client/mqtt-client.h new file mode 100644 index 000000000..c551c701b --- /dev/null +++ b/examples/mqtt-client/mqtt-client.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2017, George Oikonomou - http://www.spd.gr + * 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 copyright holder 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 COPYRIGHT HOLDERS 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 + * COPYRIGHT HOLDER 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 MQTT_CLIENT_H_ +#define MQTT_CLIENT_H_ +/*---------------------------------------------------------------------------*/ +#include +/*---------------------------------------------------------------------------*/ +typedef struct mqtt_client_extension_s { + void (*init)(void); + char *(*value)(void); +} mqtt_client_extension_t; +/*---------------------------------------------------------------------------*/ +#define MQTT_CLIENT_EXTENSIONS(...) \ + const mqtt_client_extension_t *mqtt_client_extensions[] = {__VA_ARGS__}; \ + const uint8_t mqtt_client_extension_count = \ + (sizeof(mqtt_client_extensions) / sizeof(mqtt_client_extensions[0])); +/*---------------------------------------------------------------------------*/ +#endif /* MQTT_CLIENT_H_ */ +/*---------------------------------------------------------------------------*/ diff --git a/examples/platform-specific/cc2538-common/mqtt-demo/project-conf.h b/examples/mqtt-client/project-conf.h similarity index 87% rename from examples/platform-specific/cc2538-common/mqtt-demo/project-conf.h rename to examples/mqtt-client/project-conf.h index ac86990c6..7b041ae38 100644 --- a/examples/platform-specific/cc2538-common/mqtt-demo/project-conf.h +++ b/examples/mqtt-client/project-conf.h @@ -29,25 +29,14 @@ * OF THE POSSIBILITY OF SUCH DAMAGE. */ /*---------------------------------------------------------------------------*/ -/** - * \addtogroup cc2538-mqtt-demo - * @{ - * - * \file - * Project specific configuration defines for the MQTT demo - */ -/*---------------------------------------------------------------------------*/ #ifndef PROJECT_CONF_H_ #define PROJECT_CONF_H_ /*---------------------------------------------------------------------------*/ /* Enable TCP */ #define UIP_CONF_TCP 1 -/* User configuration */ -#define MQTT_DEMO_STATUS_LED LEDS_GREEN - /* If undefined, the demo will attempt to connect to IBM's quickstart */ -#define MQTT_DEMO_BROKER_IP_ADDR "fd00::1" +#define MQTT_CLIENT_CONF_BROKER_IP_ADDR "fd00::1" /*---------------------------------------------------------------------------*/ #endif /* PROJECT_CONF_H_ */ /*---------------------------------------------------------------------------*/ diff --git a/examples/platform-specific/cc2538-common/mqtt-demo/Makefile b/examples/platform-specific/cc2538-common/mqtt-demo/Makefile deleted file mode 100644 index ec29183b6..000000000 --- a/examples/platform-specific/cc2538-common/mqtt-demo/Makefile +++ /dev/null @@ -1,9 +0,0 @@ -CONTIKI_PROJECT = mqtt-demo -all: $(CONTIKI_PROJECT) - -PLATFORMS_ONLY = cc2538dk openmote-cc2538 zoul - -MODULES += os/net/app-layer/mqtt - -CONTIKI = ../../../.. -include $(CONTIKI)/Makefile.include diff --git a/examples/platform-specific/cc2538-common/mqtt-demo/Makefile.target b/examples/platform-specific/cc2538-common/mqtt-demo/Makefile.target deleted file mode 100644 index 777593c88..000000000 --- a/examples/platform-specific/cc2538-common/mqtt-demo/Makefile.target +++ /dev/null @@ -1 +0,0 @@ -TARGET = cc2538dk From e200b71b27fd792a8b52c74628acd7838403c740 Mon Sep 17 00:00:00 2001 From: George Oikonomou Date: Mon, 30 Oct 2017 23:59:16 +0000 Subject: [PATCH 36/44] Add travis tests for the MQTT client --- tests/02-compile-arm-ports-01/Makefile | 5 ++++- tests/03-compile-arm-ports-02/Makefile | 3 ++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/02-compile-arm-ports-01/Makefile b/tests/02-compile-arm-ports-01/Makefile index c78ed1714..51b232173 100644 --- a/tests/02-compile-arm-ports-01/Makefile +++ b/tests/02-compile-arm-ports-01/Makefile @@ -31,6 +31,10 @@ dev/leds/srf06-cc26xx:BOARD=launchpad/cc1310 \ dev/leds/srf06-cc26xx:BOARD=launchpad/cc1350 \ dev/leds/srf06-cc26xx:BOARD=launchpad/cc2650 \ 6tisch/etsi-plugtest-2017/srf06-cc26xx:BOARD=launchpad/cc2650 \ +mqtt-client/srf06-cc26xx:BOARD=srf06/cc26xx \ +mqtt-client/srf06-cc26xx:BOARD=launchpad/cc2650 \ +mqtt-client/srf06-cc26xx:BOARD=sensortag/cc2650 \ +mqtt-client/cc2538dk \ storage/cfs-coffee/cc2538dk \ sensniff/cc2538dk \ rpl-udp/cc2538dk \ @@ -41,7 +45,6 @@ multicast/cc2538dk \ dev/gpio-hal/cc2538dk \ dev/leds/cc2538dk \ platform-specific/cc2538-common/cc2538dk \ -platform-specific/cc2538-common/mqtt-demo/cc2538dk \ platform-specific/cc2538-common/crypto/cc2538dk \ platform-specific/cc2538-common/pka/cc2538dk \ hello-world/cc2538dk \ diff --git a/tests/03-compile-arm-ports-02/Makefile b/tests/03-compile-arm-ports-02/Makefile index c0f1b7704..a69ec44a0 100644 --- a/tests/03-compile-arm-ports-02/Makefile +++ b/tests/03-compile-arm-ports-02/Makefile @@ -4,7 +4,6 @@ TOOLSDIR=../../tools EXAMPLES = \ rpl-border-router/zoul \ platform-specific/cc2538-common/zoul \ -platform-specific/cc2538-common/mqtt-demo/zoul \ platform-specific/cc2538-common/crypto/zoul \ platform-specific/cc2538-common/pka/zoul \ platform-specific/zoul/orion/ip64-router/zoul:BOARD=orion \ @@ -51,6 +50,8 @@ dev/rgb-led/zoul:BOARD=remote-revb \ dev/rgb-led/zoul:BOARD=firefly-reva \ dev/rgb-led/zoul:BOARD=firefly \ dev/rgb-led/zoul:BOARD=orion \ +mqtt-client/zoul:BOARD=firefly \ +mqtt-client/openmote-cc2538 \ storage/cfs-coffee/openmote-cc2538 \ sensniff/openmote-cc2538 \ hello-world/openmote-cc2538 \ From c6a5819d8da4344420806d34de8db86df5eba426 Mon Sep 17 00:00:00 2001 From: Simon Duquennoy Date: Sun, 25 Mar 2018 06:31:01 -0700 Subject: [PATCH 37/44] RPL local repair: do not reset link statistics --- os/net/routing/rpl-lite/rpl-dag.c | 1 - 1 file changed, 1 deletion(-) diff --git a/os/net/routing/rpl-lite/rpl-dag.c b/os/net/routing/rpl-lite/rpl-dag.c index 65365ac77..7cf99e03b 100644 --- a/os/net/routing/rpl-lite/rpl-dag.c +++ b/os/net/routing/rpl-lite/rpl-dag.c @@ -220,7 +220,6 @@ rpl_local_repair(const char *str) curr_instance.dag.state = DAG_INITIALIZED; /* Reset DAG state */ } curr_instance.of->reset(); /* Reset OF */ - link_stats_reset(); /* Forget past link statistics */ rpl_neighbor_remove_all(); /* Remove all neighbors */ rpl_timers_dio_reset("Local repair"); /* Reset Trickle timer */ rpl_timers_schedule_state_update(); From 146427724aa3760d4b41e28c4dcdb33c4235c48f Mon Sep 17 00:00:00 2001 From: Simon Duquennoy Date: Sun, 25 Mar 2018 06:31:23 -0700 Subject: [PATCH 38/44] TSCH-RPL: reset link statistics when leaving network --- os/net/mac/tsch/tsch-rpl.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/os/net/mac/tsch/tsch-rpl.c b/os/net/mac/tsch/tsch-rpl.c index bc54de99c..fedb4ce58 100644 --- a/os/net/mac/tsch/tsch-rpl.c +++ b/os/net/mac/tsch/tsch-rpl.c @@ -50,6 +50,7 @@ #include "net/mac/tsch/tsch-schedule.h" #include "net/mac/tsch/tsch-log.h" #include "net/mac/tsch/tsch-rpl.h" +#include "net/link-stats.h" #if ROUTING_CONF_RPL_LITE #include "net/routing/rpl-lite/rpl.h" @@ -83,6 +84,11 @@ tsch_rpl_callback_joining_network(void) void tsch_rpl_callback_leaving_network(void) { + /* Forget past link statistics. If we are leaving a TSCH + network, there are changes we've been out of sync in the recent past, and + as a result have irrelevant link statistices. */ + link_stats_reset(); + /* RPL local repair */ NETSTACK_ROUTING.local_repair("TSCH leaving"); } /*---------------------------------------------------------------------------*/ From 20ac537defb23a19f17fe2adb8ab6d466decbd11 Mon Sep 17 00:00:00 2001 From: Simon Duquennoy Date: Mon, 26 Mar 2018 05:56:41 -0700 Subject: [PATCH 39/44] Fix nullnet-unicast.csc file --- examples/nullnet/nullnet-unicast.csc | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/examples/nullnet/nullnet-unicast.csc b/examples/nullnet/nullnet-unicast.csc index 56e860d82..5a93836cd 100644 --- a/examples/nullnet/nullnet-unicast.csc +++ b/examples/nullnet/nullnet-unicast.csc @@ -6,7 +6,7 @@ [APPS_DIR]/serial_socket [APPS_DIR]/powertracker - NullNet Broadcast Example + NullNet Unicast Example 123456 1000000 @@ -23,8 +23,8 @@ org.contikios.cooja.contikimote.ContikiMoteType mtype634 Cooja Mote Type #1 - [CONTIKI_DIR]/examples/nullnet/nullnet-broadcast.c - make nullnet-broadcast.cooja TARGET=cooja + [CONTIKI_DIR]/examples/nullnet/nullnet-unicast.c + make nullnet-unicast.cooja TARGET=cooja org.contikios.cooja.interfaces.Position org.contikios.cooja.interfaces.Battery org.contikios.cooja.contikimote.interfaces.ContikiVib @@ -175,4 +175,3 @@ 408 - From 49e5cf3c8c895a430871c621305bc1c60f9a3281 Mon Sep 17 00:00:00 2001 From: Yasuyuki Tanaka Date: Fri, 23 Mar 2018 17:51:27 +0000 Subject: [PATCH 40/44] sicslowpan: fix debug messages and remove TABs --- os/net/ipv6/sicslowpan.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/os/net/ipv6/sicslowpan.c b/os/net/ipv6/sicslowpan.c index 3a08df83a..3bc7a935e 100644 --- a/os/net/ipv6/sicslowpan.c +++ b/os/net/ipv6/sicslowpan.c @@ -353,7 +353,7 @@ add_fragment(uint16_t tag, uint16_t frag_size, uint8_t offset) for(i = 0; i < SICSLOWPAN_REASS_CONTEXTS; i++) { /* clear all fragment info with expired timer to free all fragment buffers */ if(frag_info[i].len > 0 && timer_expired(&frag_info[i].reass_timer)) { - clear_fragments(i); + clear_fragments(i); } /* We use len as indication on used or not used */ @@ -421,12 +421,12 @@ copy_frags2uip(int context) /* Copy from the fragment context info buffer first */ memcpy((uint8_t *)UIP_IP_BUF, (uint8_t *)frag_info[context].first_frag, - frag_info[context].first_frag_len); + frag_info[context].first_frag_len); for(i = 0; i < SICSLOWPAN_FRAGMENT_BUFFERS; i++) { /* And also copy all matching fragments */ if(frag_buf[i].len > 0 && frag_buf[i].index == context) { memcpy((uint8_t *)UIP_IP_BUF + (uint16_t)(frag_buf[i].offset << 3), - (uint8_t *)frag_buf[i].data, frag_buf[i].len); + (uint8_t *)frag_buf[i].data, frag_buf[i].len); } } /* deallocate all the fragments for this context */ @@ -676,9 +676,9 @@ compress_hdr_iphc(linkaddr_t *link_destaddr) LOG_DBG("before compression (%d): ", UIP_IP_BUF->len[1]); for(ndx = 0; ndx < UIP_IP_BUF->len[1] + 40; ndx++) { uint8_t data = ((uint8_t *) (UIP_IP_BUF))[ndx]; - LOG_DBG("%02x", data); + LOG_DBG_("%02x", data); } - LOG_DBG("\n"); + LOG_DBG_("\n"); } hc06_ptr = PACKETBUF_IPHC_BUF + 2; @@ -1000,9 +1000,9 @@ compress_hdr_iphc(linkaddr_t *link_destaddr) LOG_DBG("after compression %d: ", (int)(hc06_ptr - packetbuf_ptr)); for(ndx = 0; ndx < hc06_ptr - packetbuf_ptr; ndx++) { uint8_t data = ((uint8_t *) packetbuf_ptr)[ndx]; - LOG_DBG("%02x", data); + LOG_DBG_("%02x", data); } - LOG_DBG("\n"); + LOG_DBG_("\n"); } packetbuf_hdr_len = hc06_ptr - packetbuf_ptr; @@ -1059,7 +1059,7 @@ uncompress_hdr_iphc(uint8_t *buf, uint16_t ip_len) SICSLOWPAN_IP_BUF(buf)->vtc = 0x60 | ((tmp >> 2) & 0x0f); /* ECN rolled down two steps + lowest DSCP bits at top two bits */ SICSLOWPAN_IP_BUF(buf)->tcflow = ((tmp >> 2) & 0x30) | (tmp << 6) | - (SICSLOWPAN_IP_BUF(buf)->tcflow & 0x0f); + (SICSLOWPAN_IP_BUF(buf)->tcflow & 0x0f); } else { /* Traffic class is compressed (set version and no TC)*/ SICSLOWPAN_IP_BUF(buf)->vtc = 0x60; @@ -1074,7 +1074,7 @@ uncompress_hdr_iphc(uint8_t *buf, uint16_t ip_len) /* Version and flow label are compressed */ if((iphc0 & SICSLOWPAN_IPHC_TC_C) == 0) { /* Traffic class is inline */ - SICSLOWPAN_IP_BUF(buf)->vtc = 0x60 | ((*hc06_ptr >> 2) & 0x0f); + SICSLOWPAN_IP_BUF(buf)->vtc = 0x60 | ((*hc06_ptr >> 2) & 0x0f); SICSLOWPAN_IP_BUF(buf)->tcflow = ((*hc06_ptr << 6) & 0xC0) | ((*hc06_ptr >> 2) & 0x30); SICSLOWPAN_IP_BUF(buf)->flow = 0; hc06_ptr += 1; @@ -1902,16 +1902,16 @@ input(void) uip_len = packetbuf_payload_len + uncomp_hdr_len; #endif /* SICSLOWPAN_CONF_FRAG */ LOG_INFO("input: IP packet ready (length %d)\n", - uip_len); + uip_len); if(LOG_DBG_ENABLED) { uint16_t ndx; LOG_DBG("after decompression %u:", UIP_IP_BUF->len[1]); for (ndx = 0; ndx < UIP_IP_BUF->len[1] + 40; ndx++) { uint8_t data = ((uint8_t *) (UIP_IP_BUF))[ndx]; - LOG_DBG("%02x", data); + LOG_DBG_("%02x", data); } - LOG_DBG("\n"); + LOG_DBG_("\n"); } /* if callback is set then set attributes and call */ From fe680fbbc39a6569f1e43518eb7bda68c0a3856e Mon Sep 17 00:00:00 2001 From: Simon Duquennoy Date: Mon, 26 Mar 2018 12:23:49 -0700 Subject: [PATCH 41/44] RPL BR tests: add more time for initial convergence --- tests/17-tun-rpl-br/04-border-router-traceroute.sh | 2 +- tests/17-tun-rpl-br/test-border-router.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/17-tun-rpl-br/04-border-router-traceroute.sh b/tests/17-tun-rpl-br/04-border-router-traceroute.sh index a150c530f..33e239e08 100755 --- a/tests/17-tun-rpl-br/04-border-router-traceroute.sh +++ b/tests/17-tun-rpl-br/04-border-router-traceroute.sh @@ -23,7 +23,7 @@ make -C $CONTIKI/tools tunslip6 make -C $CONTIKI/examples/rpl-border-router/ connect-router-cooja TARGET=zoul >> $BASENAME.tunslip.log 2>&1 & MPID=$! echo "Waiting for network formation" -sleep 5 +sleep 20 # not in real-time, simulates at full speed # Do ping echo "Running Traceroute" diff --git a/tests/17-tun-rpl-br/test-border-router.sh b/tests/17-tun-rpl-br/test-border-router.sh index b8c8f018e..ff0e48cce 100755 --- a/tests/17-tun-rpl-br/test-border-router.sh +++ b/tests/17-tun-rpl-br/test-border-router.sh @@ -24,7 +24,7 @@ make -C $CONTIKI/tools tunslip6 make -C $CONTIKI/examples/rpl-border-router/ connect-router-cooja TARGET=zoul >> $BASENAME.tunslip.log 2>&1 & MPID=$! echo "Waiting for network formation" -sleep 5 +sleep 20 # not in real-time, simulates at full speed # Do ping echo "Pinging" From a2c76f63c3bf86995794adf9178849ca74650c70 Mon Sep 17 00:00:00 2001 From: Simon Duquennoy Date: Tue, 27 Mar 2018 02:01:41 -0700 Subject: [PATCH 42/44] Border router tests: make all simulations real-time --- tests/17-tun-rpl-br/01-border-router-cooja.csc | 3 ++- tests/17-tun-rpl-br/01-border-router-cooja.sh | 2 +- tests/17-tun-rpl-br/02-border-router-cooja-tsch.csc | 3 ++- tests/17-tun-rpl-br/02-border-router-cooja-tsch.sh | 3 ++- tests/17-tun-rpl-br/03-border-router-sky.csc | 4 +++- tests/17-tun-rpl-br/03-border-router-sky.sh | 2 +- tests/17-tun-rpl-br/04-border-router-traceroute.csc | 3 ++- tests/17-tun-rpl-br/04-border-router-traceroute.sh | 8 ++++++-- tests/17-tun-rpl-br/07-native-border-router-cooja.sh | 2 +- tests/17-tun-rpl-br/test-border-router.sh | 7 +++++-- .../{test-nbr.sh => test-native-border-router.sh} | 7 +++++-- tests/Makefile.script-test | 1 + 12 files changed, 31 insertions(+), 14 deletions(-) rename tests/17-tun-rpl-br/{test-nbr.sh => test-native-border-router.sh} (91%) diff --git a/tests/17-tun-rpl-br/01-border-router-cooja.csc b/tests/17-tun-rpl-br/01-border-router-cooja.csc index 02808aeec..b0d95600a 100644 --- a/tests/17-tun-rpl-br/01-border-router-cooja.csc +++ b/tests/17-tun-rpl-br/01-border-router-cooja.csc @@ -239,7 +239,8 @@ make -j hello-world.cooja TARGET=cooja org.contikios.cooja.plugins.ScriptRunner +/* Set simulaion speed to real time */ +sim.setSpeedLimit(1.0); true 600 diff --git a/tests/17-tun-rpl-br/01-border-router-cooja.sh b/tests/17-tun-rpl-br/01-border-router-cooja.sh index 6a781583d..11b2edc8a 100755 --- a/tests/17-tun-rpl-br/01-border-router-cooja.sh +++ b/tests/17-tun-rpl-br/01-border-router-cooja.sh @@ -6,4 +6,4 @@ CONTIKI=$1 # Simulation file BASENAME=01-border-router-cooja -bash test-border-router.sh $CONTIKI $BASENAME fd00::204:4:4:4 +bash test-border-router.sh $CONTIKI $BASENAME fd00::204:4:4:4 60 diff --git a/tests/17-tun-rpl-br/02-border-router-cooja-tsch.csc b/tests/17-tun-rpl-br/02-border-router-cooja-tsch.csc index 5b86b690c..61a6c96ce 100644 --- a/tests/17-tun-rpl-br/02-border-router-cooja-tsch.csc +++ b/tests/17-tun-rpl-br/02-border-router-cooja-tsch.csc @@ -239,7 +239,8 @@ make -j hello-world.cooja TARGET=cooja MAKE_MAC=MAKE_MAC_TSCH org.contikios.cooja.plugins.ScriptRunner +/* Set simulaion speed to real time */ +sim.setSpeedLimit(1.0); true 600 diff --git a/tests/17-tun-rpl-br/02-border-router-cooja-tsch.sh b/tests/17-tun-rpl-br/02-border-router-cooja-tsch.sh index b91a8d5c7..c7e61c8a7 100755 --- a/tests/17-tun-rpl-br/02-border-router-cooja-tsch.sh +++ b/tests/17-tun-rpl-br/02-border-router-cooja-tsch.sh @@ -6,4 +6,5 @@ CONTIKI=$1 # Simulation file BASENAME=02-border-router-cooja-tsch -bash test-border-router.sh $CONTIKI $BASENAME fd00::204:4:4:4 +# Add a little extra initial time to account for TSCH association time +bash test-border-router.sh $CONTIKI $BASENAME fd00::204:4:4:4 120 diff --git a/tests/17-tun-rpl-br/03-border-router-sky.csc b/tests/17-tun-rpl-br/03-border-router-sky.csc index cec948e0d..e19573d8d 100644 --- a/tests/17-tun-rpl-br/03-border-router-sky.csc +++ b/tests/17-tun-rpl-br/03-border-router-sky.csc @@ -222,7 +222,9 @@ make -j hello-world.sky TARGET=sky org.contikios.cooja.plugins.ScriptRunner - + true 600 diff --git a/tests/17-tun-rpl-br/03-border-router-sky.sh b/tests/17-tun-rpl-br/03-border-router-sky.sh index 798e0242b..7ffa4265e 100755 --- a/tests/17-tun-rpl-br/03-border-router-sky.sh +++ b/tests/17-tun-rpl-br/03-border-router-sky.sh @@ -6,4 +6,4 @@ CONTIKI=$1 # Simulation file BASENAME=03-border-router-sky -bash test-border-router.sh $CONTIKI $BASENAME fd00::0212:7404:0004:0404 +bash test-border-router.sh $CONTIKI $BASENAME fd00::0212:7404:0004:0404 60 diff --git a/tests/17-tun-rpl-br/04-border-router-traceroute.csc b/tests/17-tun-rpl-br/04-border-router-traceroute.csc index 02808aeec..b0d95600a 100644 --- a/tests/17-tun-rpl-br/04-border-router-traceroute.csc +++ b/tests/17-tun-rpl-br/04-border-router-traceroute.csc @@ -239,7 +239,8 @@ make -j hello-world.cooja TARGET=cooja org.contikios.cooja.plugins.ScriptRunner +/* Set simulaion speed to real time */ +sim.setSpeedLimit(1.0); true 600 diff --git a/tests/17-tun-rpl-br/04-border-router-traceroute.sh b/tests/17-tun-rpl-br/04-border-router-traceroute.sh index 33e239e08..e1e9d6cdd 100755 --- a/tests/17-tun-rpl-br/04-border-router-traceroute.sh +++ b/tests/17-tun-rpl-br/04-border-router-traceroute.sh @@ -8,6 +8,10 @@ BASENAME=04-border-router-traceroute # Destination IPv6 IPADDR=fd00::204:4:4:4 + +# Time allocated for toplogy formation +WAIT_TIME=60 + # The expected hop count TARGETHOPS=4 @@ -22,8 +26,8 @@ echo "Starting tunslip6" make -C $CONTIKI/tools tunslip6 make -C $CONTIKI/examples/rpl-border-router/ connect-router-cooja TARGET=zoul >> $BASENAME.tunslip.log 2>&1 & MPID=$! -echo "Waiting for network formation" -sleep 20 # not in real-time, simulates at full speed +printf "Waiting for network formation (%d seconds)\n" "$WAIT_TIME" +sleep $WAIT_TIME # Do ping echo "Running Traceroute" diff --git a/tests/17-tun-rpl-br/07-native-border-router-cooja.sh b/tests/17-tun-rpl-br/07-native-border-router-cooja.sh index d5d2642fe..8f91bd726 100755 --- a/tests/17-tun-rpl-br/07-native-border-router-cooja.sh +++ b/tests/17-tun-rpl-br/07-native-border-router-cooja.sh @@ -6,4 +6,4 @@ CONTIKI=$1 # Simulation file BASENAME=07-native-border-router-cooja -bash test-nbr.sh $CONTIKI $BASENAME fd00::204:4:4:4 +bash test-native-border-router.sh $CONTIKI $BASENAME fd00::204:4:4:4 60 diff --git a/tests/17-tun-rpl-br/test-border-router.sh b/tests/17-tun-rpl-br/test-border-router.sh index ff0e48cce..6bfb8cf62 100755 --- a/tests/17-tun-rpl-br/test-border-router.sh +++ b/tests/17-tun-rpl-br/test-border-router.sh @@ -9,6 +9,9 @@ BASENAME=$2 # Destination IPv6 IPADDR=$3 +# Time allocated for convergence +WAIT_TIME=$4 + # ICMP request-reply count COUNT=5 @@ -23,8 +26,8 @@ echo "Starting tunslip6" make -C $CONTIKI/tools tunslip6 make -C $CONTIKI/examples/rpl-border-router/ connect-router-cooja TARGET=zoul >> $BASENAME.tunslip.log 2>&1 & MPID=$! -echo "Waiting for network formation" -sleep 20 # not in real-time, simulates at full speed +printf "Waiting for network formation (%d seconds)\n" "$WAIT_TIME" +sleep $WAIT_TIME # Do ping echo "Pinging" diff --git a/tests/17-tun-rpl-br/test-nbr.sh b/tests/17-tun-rpl-br/test-native-border-router.sh similarity index 91% rename from tests/17-tun-rpl-br/test-nbr.sh rename to tests/17-tun-rpl-br/test-native-border-router.sh index 9a1824f7c..38dce1bae 100644 --- a/tests/17-tun-rpl-br/test-nbr.sh +++ b/tests/17-tun-rpl-br/test-native-border-router.sh @@ -9,6 +9,9 @@ BASENAME=$2 # Destination IPv6 IPADDR=$3 +# Time allocated for convergence +WAIT_TIME=$4 + # ICMP request-reply count COUNT=5 @@ -22,8 +25,8 @@ sleep 20 echo "Starting native border-router" nohup make -C $CONTIKI/examples/rpl-border-router/ connect-router-cooja TARGET=native >> $BASENAME.nbr.log 2>&1 & MPID=$! -echo "Waiting for network formation" -sleep 60 # runs in real time so we need to wait a bit +printf "Waiting for network formation (%d seconds)\n" "$WAIT_TIME" +sleep $WAIT_TIME # Do ping echo "Pinging" diff --git a/tests/Makefile.script-test b/tests/Makefile.script-test index 978809dfb..e4dd0da6a 100644 --- a/tests/Makefile.script-test +++ b/tests/Makefile.script-test @@ -11,6 +11,7 @@ summary: cooja $(TESTLOGS) @cat summary %.testlog: %.sh cooja + @echo "========== Running script test $(basename $@).sh ==========" @bash "$(basename $@).sh" "$(CONTIKI)" clean: From 710ea91f1afb88d2a32dc0b69df7690cbfa30585 Mon Sep 17 00:00:00 2001 From: Simon Duquennoy Date: Tue, 27 Mar 2018 16:11:57 +0200 Subject: [PATCH 43/44] Compile test log summary format --- tests/Makefile.compile-test | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Makefile.compile-test b/tests/Makefile.compile-test index 6c7df5835..1a5952033 100644 --- a/tests/Makefile.compile-test +++ b/tests/Makefile.compile-test @@ -42,8 +42,8 @@ define dooneexample @((cd $(EXAMPLESDIR)/$(1); \ make $(4) TARGET=$(2) clean && make -j $(4) TARGET=$(2) WERROR=1) > \ /dev/null 2>make.err && \ - (echo " -> OK" && printf "%-75s %-35s %-20s TEST OK\n" "$(1)" "$(4)" "$(2)" > $(3)-$(subst /,-,$(1))$(2).testlog) || \ - (echo " -> FAIL" && printf "%-75s %-35s %-20s TEST FAIL\n" "$(1)" "$(4)" "$(2)" > $(3)-$(subst /,-,$(1))$(2).testlog ; cat make.err)) + (echo " -> OK" && printf "%-75s %-40s %-20s TEST OK\n" "$(1)" "$(4)" "$(2)" > $(3)-$(subst /,-,$(1))$(2).testlog) || \ + (echo " -> FAIL" && printf "%-75s %-40s %-20s TEST FAIL\n" "$(1)" "$(4)" "$(2)" > $(3)-$(subst /,-,$(1))$(2).testlog ; cat make.err)) @rm -f make.err endef From 4206761ccff8a5410f426c86cc3e785c0c08f1e9 Mon Sep 17 00:00:00 2001 From: Yasuyuki Tanaka Date: Mon, 26 Mar 2018 20:51:30 +0200 Subject: [PATCH 44/44] shell: add missing UIP_CONF_IPV6_RPL guard --- os/services/shell/shell-commands.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/os/services/shell/shell-commands.c b/os/services/shell/shell-commands.c index dd4fb95dc..f8ff63660 100644 --- a/os/services/shell/shell-commands.c +++ b/os/services/shell/shell-commands.c @@ -337,6 +337,7 @@ PT_THREAD(cmd_help(struct pt *pt, shell_output_func output, char *args)) PT_END(pt); } +#if UIP_CONF_IPV6_RPL /*---------------------------------------------------------------------------*/ static PT_THREAD(cmd_rpl_set_root(struct pt *pt, shell_output_func output, char *args)) @@ -415,6 +416,7 @@ PT_THREAD(cmd_rpl_local_repair(struct pt *pt, shell_output_func output, char *ar PT_END(pt); } +#endif /* UIP_CONF_IPV6_RPL */ /*---------------------------------------------------------------------------*/ static PT_THREAD(cmd_ipaddr(struct pt *pt, shell_output_func output, char *args))