diff --git a/platform/mikro-e/apps/test-motion/Makefile b/platform/mikro-e/apps/test-motion/Makefile new file mode 100755 index 00000000000..e6ddbe40d1a --- /dev/null +++ b/platform/mikro-e/apps/test-motion/Makefile @@ -0,0 +1,19 @@ +CONTIKI_PROJECT = test-motion +CONTIKI = ../../../.. + +MOTION_CLICK = motion-click + +CFLAGS += -DMOTION_CLICK + +APPS += motion-click +APPDIRS += ../../dev + +all: $(CONTIKI_PROJECT) + xc32-bin2hex $(CONTIKI_PROJECT).$(TARGET) + +distclean: cleanall + +cleanall: + rm -f symbols.* + +include $(CONTIKI)/Makefile.include diff --git a/platform/mikro-e/apps/test-motion/test-motion.c b/platform/mikro-e/apps/test-motion/test-motion.c new file mode 100644 index 00000000000..e7d532b1e49 --- /dev/null +++ b/platform/mikro-e/apps/test-motion/test-motion.c @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2016, Imagination Technologies Limited and/or its + * affiliated group companies. + * + * 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. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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. + */ + +/** + * \file + * A test application for motion-click. + * http://www.mikroe.com/click/motion/ + * + */ + +#include +#include +#include +#include "dev/common-clicks.h" +#include "dev/leds.h" +#include "sys/clock.h" +/*---------------------------------------------------------------------------*/ +PROCESS(test_motion, "Motion click test"); +AUTOSTART_PROCESSES(&test_motion); +/*---------------------------------------------------------------------------*/ +PROCESS_THREAD(test_motion, ev, data) +{ + PROCESS_EXITHANDLER(goto exit;) + PROCESS_BEGIN(); + static int i; + // static struct etimer timer; + SENSORS_ACTIVATE(motion_sensor); + while(1) { + PROCESS_WAIT_EVENT_UNTIL((ev == sensors_event)); + if (data == &motion_sensor) { + printf("Motion event received\n"); + leds_on(LEDS_ALL); + /* Delay for 500ms */ + for(i=0;i<=500;++i) { + clock_delay_usec(1000); + } + leds_off(LEDS_ALL); + } + } + exit: + SENSORS_DEACTIVATE(motion_sensor); + PROCESS_END(); +} +/*---------------------------------------------------------------------------*/ diff --git a/platform/mikro-e/apps/test-relay/Makefile b/platform/mikro-e/apps/test-relay/Makefile index 0a98828684a..cbe2eec0b43 100644 --- a/platform/mikro-e/apps/test-relay/Makefile +++ b/platform/mikro-e/apps/test-relay/Makefile @@ -1,10 +1,15 @@ -CONTIKI_PROJECT=test-relay -CONTIKI=../../../.. +CONTIKI_PROJECT = test-relay +CONTIKI = ../../../.. -APPS += dev -APPDIRS += ../.. +RELAY_CLICK = relay-click + +CFLAGS += -DRELAY_CLICK + +APPS += relay-click +APPDIRS += ../../dev all: $(CONTIKI_PROJECT) + xc32-bin2hex $(CONTIKI_PROJECT).$(TARGET) distclean: cleanall diff --git a/platform/mikro-e/apps/test-relay/test-relay.c b/platform/mikro-e/apps/test-relay/test-relay.c index d879c6f0984..575275101e6 100644 --- a/platform/mikro-e/apps/test-relay/test-relay.c +++ b/platform/mikro-e/apps/test-relay/test-relay.c @@ -37,7 +37,7 @@ */ #include "contiki.h" -#include "relay-click.h" +#include "dev/common-clicks.h" /*---------------------------------------------------------------------------*/ PROCESS(test_relay_click, "Test relay-click"); AUTOSTART_PROCESSES(&test_relay_click); diff --git a/platform/mikro-e/apps/test-relay/test-relay.mikro-e b/platform/mikro-e/apps/test-relay/test-relay.mikro-e deleted file mode 100755 index a9c8206bfa5..00000000000 Binary files a/platform/mikro-e/apps/test-relay/test-relay.mikro-e and /dev/null differ diff --git a/platform/mikro-e/contiki-mikro-e-main.c b/platform/mikro-e/contiki-mikro-e-main.c index b8c6bfa436d..d0394030f90 100755 --- a/platform/mikro-e/contiki-mikro-e-main.c +++ b/platform/mikro-e/contiki-mikro-e-main.c @@ -48,15 +48,21 @@ #include #include #include "button-sensor.h" +#include "dev/common-clicks.h" #define UART_DEBUG_BAUDRATE 115200 + +#ifdef MOTION_CLICK +SENSORS(&button_sensor, &button_sensor2, &motion_sensor); +#else SENSORS(&button_sensor, &button_sensor2); +#endif /*---------------------------------------------------------------------------*/ int main(int argc, char **argv) { - int32_t r; + int32_t r = 0; pic32_init(); watchdog_init(); @@ -103,6 +109,11 @@ ISR(_CHANGE_NOTICE_VECTOR) } else if(BUTTON2_CHECK_IRQ()) { /* Button2 was pressed */ button2_isr(); +#ifdef MOTION_CLICK + } else if(MOTION_SENSOR_CHECK_IRQ()) { + /* Motion was detected */ + motion_sensor_isr(); +#endif } } diff --git a/platform/mikro-e/dev/Makefile.dev b/platform/mikro-e/dev/Makefile.dev deleted file mode 100644 index baa5f7aee46..00000000000 --- a/platform/mikro-e/dev/Makefile.dev +++ /dev/null @@ -1 +0,0 @@ -dev_src = relay-click.c diff --git a/platform/mikro-e/dev/common-clicks.h b/platform/mikro-e/dev/common-clicks.h new file mode 100644 index 00000000000..5de35cfb541 --- /dev/null +++ b/platform/mikro-e/dev/common-clicks.h @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2016, Imagination Technologies Limited and/or its + * affiliated group companies. + * + * 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. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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 __COMMON_CLICKS_H__ +#define __COMMON_CLICKS_H__ + +#ifdef MOTION_CLICK +#include "motion-click/motion-click.h" +#endif + +#ifdef RELAY_CLICK +#include "relay-click/relay-click.h" +#endif + +#endif /* __COMMON_CLICKS_H__ */ diff --git a/platform/mikro-e/dev/motion-click/Makefile.motion-click b/platform/mikro-e/dev/motion-click/Makefile.motion-click new file mode 100644 index 00000000000..b963355450f --- /dev/null +++ b/platform/mikro-e/dev/motion-click/Makefile.motion-click @@ -0,0 +1,2 @@ + +motion-click_src += motion-click.c diff --git a/platform/mikro-e/dev/motion-click/motion-click.c b/platform/mikro-e/dev/motion-click/motion-click.c new file mode 100644 index 00000000000..1db8426fe90 --- /dev/null +++ b/platform/mikro-e/dev/motion-click/motion-click.c @@ -0,0 +1,117 @@ +/* + * Copyright (c) 2016, Imagination Technologies Limited and/or its affiliated + * group companies. + * + * 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. + */ + +/** + * \file + * Motion click driver. + * http://www.mikroe.com/click/motion/ + * + */ + +#include +#include "motion-click.h" + +static struct timer debouncetimer; +static int motion_sensor_mode; +static int motion_sensor_state; +/*---------------------------------------------------------------------------*/ +static void motion_sensor_read(const struct sensors_sensor *s) +{ + if (!motion_sensor_state) { + if (timer_expired(&debouncetimer)) { + motion_sensor_state = 1; + /* Set a timer for 100ms to ignore false notifications */ + timer_set(&debouncetimer, CLOCK_SECOND / 10); + /* Notify processes that Motion has been detected */ + sensors_changed(s); + } + } else { + if (timer_expired(&debouncetimer)) { + motion_sensor_state = 0; + /* Set a timer for 100ms to ignore false notifications */ + timer_set(&debouncetimer, CLOCK_SECOND / 10); + } + } +} + +void motion_sensor_isr(void) +{ + motion_sensor_read(&motion_sensor); + MOTION_SENSOR_CLEAR_IRQ(); +} +/*---------------------------------------------------------------------------*/ +static int +motion_sensor_configure(int type, int value) +{ + switch(type) { + case SENSORS_HW_INIT: + /* Configure gpio pins and initialize interrupt */ + MOTION_SENSOR_IRQ_INIT(); + motion_sensor_state = 0; + return 1; + + case SENSORS_ACTIVE: + if(value) { + if(!motion_sensor_mode) { + /* Enable interrupt for Motion sensor */ + timer_set(&debouncetimer, 0); + MOTION_SENSOR_IRQ_ENABLE(); + motion_sensor_mode = 1; + } + } else { + /* Disable interrupt for Motion sensor */ + MOTION_SENSOR_IRQ_DISABLE(); + motion_sensor_mode = 0; + } + return 1; + + default: + return 0; + } +} +/*---------------------------------------------------------------------------*/ +static int +motion_sensor_status(int type) +{ + return motion_sensor_mode; +} +/*---------------------------------------------------------------------------*/ +static int +motion_sensor_value(int type) +{ + return motion_sensor_state; +} +/*---------------------------------------------------------------------------*/ +SENSORS_SENSOR(motion_sensor, MOTION_SENSOR, motion_sensor_value, motion_sensor_configure, + motion_sensor_status); +/*---------------------------------------------------------------------------*/ diff --git a/platform/mikro-e/dev/motion-click/motion-click.h b/platform/mikro-e/dev/motion-click/motion-click.h new file mode 100644 index 00000000000..807408b1a7a --- /dev/null +++ b/platform/mikro-e/dev/motion-click/motion-click.h @@ -0,0 +1,91 @@ +/* + * Copyright (c) 2016, Imagination Technologies Limited and/or its + * affiliated group companies. + * + * 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. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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. + */ + +/** + * \file + * Motion click driver. + * http://www.mikroe.com/click/motion/ + * + */ + +#ifndef __MOTION_SENSOR_H__ +#define __MOTION_SENSOR_H__ + +#include + +/* To initialize interrupt on Motion detected */ +#define MOTION_SENSOR_IRQ_INIT() __MOTION_SENSOR_IRQ_INIT() +#define __MOTION_SENSOR_IRQ_INIT() \ + do { \ + GPIO_CONFIGURE_AS_INPUT(D, 0); \ + CNCONDSET = _CNCOND_ON_MASK; \ + IEC1SET = _IEC1_CNDIE_MASK; \ + IFS1CLR = _IFS1_CNDIF_MASK; \ + IPC8SET = (6 << _IPC8_CNIP_POSITION) | (0 << _IPC8_CNIS_POSITION); \ + (void)PORTD; \ + } while(0) + +/* To enable Motion sensor interrupt */ +#define MOTION_SENSOR_IRQ_ENABLE() __MOTION_SENSOR_IRQ_ENABLE() +#define __MOTION_SENSOR_IRQ_ENABLE() \ + do { \ + CNENDSET = _CNEND_CNIED0_MASK; \ + } while(0) + +/* To disable Motion sensor interrupt */ +#define MOTION_SENSOR_IRQ_DISABLE() __MOTION_SENSOR_IRQ_DISABLE() +#define __MOTION_SENSOR_IRQ_DISABLE() \ + do { \ + CNENDCLR = _CNEND_CNIED0_MASK; \ + } while(0) + +/* To clear interrupt */ +#define MOTION_SENSOR_CLEAR_IRQ() __MOTION_SENSOR_CLEAR_IRQ() +#define __MOTION_SENSOR_CLEAR_IRQ() \ + do { \ + (void)PORTD; \ + IFS1CLR = _IFS1_CNDIF_MASK; \ + CNSTATDCLR = _CNSTATD_CNSTATD0_MASK; \ + } while(0) + +/* Check if Motion was detected */ +#define MOTION_SENSOR_CHECK_IRQ() __MOTION_SENSOR_CHECK_IRQ() +#define __MOTION_SENSOR_CHECK_IRQ() \ + (IFS1bits.CNDIF & CNSTATDbits.CNSTATD0) + +/* Method to be called when Motion is detected */ +void motion_sensor_isr(void); + +#define MOTION_SENSOR "motion-sensor" + +extern const struct sensors_sensor motion_sensor; + +#endif /* __MOTION_SENSOR_H__ */ diff --git a/platform/mikro-e/dev/relay-click/Makefile.relay-click b/platform/mikro-e/dev/relay-click/Makefile.relay-click new file mode 100644 index 00000000000..0ab02290b85 --- /dev/null +++ b/platform/mikro-e/dev/relay-click/Makefile.relay-click @@ -0,0 +1,2 @@ + +relay-click_src += relay-click.c diff --git a/platform/mikro-e/dev/relay-click.c b/platform/mikro-e/dev/relay-click/relay-click.c similarity index 100% rename from platform/mikro-e/dev/relay-click.c rename to platform/mikro-e/dev/relay-click/relay-click.c diff --git a/platform/mikro-e/dev/relay-click.h b/platform/mikro-e/dev/relay-click/relay-click.h similarity index 100% rename from platform/mikro-e/dev/relay-click.h rename to platform/mikro-e/dev/relay-click/relay-click.h