From a5ea8d44e9c64882e8dc26a4b375d7858e99198b Mon Sep 17 00:00:00 2001 From: George Oikonomou Date: Sun, 5 Nov 2017 18:46:46 +0000 Subject: [PATCH] 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); } /*---------------------------------------------------------------------------*/