osd-contiki/examples/cc2538dk/cc2538-demo.c
George Oikonomou 7fb242f883 Merge pull request #493 from g-oikonomou/cc2538-demo-example-counter
Don't increment the counter when a button is pressed
2014-02-02 05:41:33 -08:00

161 lines
5.8 KiB
C

/*
* 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
* @{
*
* \defgroup cc2538-examples cc2538dk Example Projects
* @{
*
* \defgroup cc2538-demo cc2538dk Demo Project
*
* Example project demonstrating the cc2538dk functionality
*
* This assumes that you are using a SmartRF06EB with a cc2538 EM
*
* - Boot sequence: LEDs flashing, LED2 followed by LED3 then LED4
* - etimer/clock : Every LOOP_INTERVAL clock ticks the LED defined as
* LEDS_PERIODIC will turn on
* - rtimer : Exactly LEDS_OFF_HYSTERISIS rtimer ticks later,
* LEDS_PERIODIC will turn back off
* - Buttons :
* - BTN_DOWN turns on LEDS_REBOOT and causes a watchdog reboot
* - BTN_UP to soft reset (SYS_CTRL_PWRDBG::FORCE_WARM_RESET)
* - BTN_LEFT and BTN_RIGHT flash the LED defined as LEDS_BUTTON
* - UART : Every LOOP_INTERVAL the EM will print something over the
* UART. Receiving an entire line of text over UART (ending
* in \\r) will cause LEDS_SERIAL_IN to toggle
* - Radio comms : BTN_SELECT sends a rime broadcast. Reception of a rime
* packet will toggle LEDs defined as LEDS_RF_RX
*
* @{
*
* \file
* Example demonstrating the cc2538dk platform
*/
#include "contiki.h"
#include "cpu.h"
#include "sys/etimer.h"
#include "sys/rtimer.h"
#include "dev/leds.h"
#include "dev/uart.h"
#include "dev/button-sensor.h"
#include "dev/watchdog.h"
#include "dev/serial-line.h"
#include "dev/sys-ctrl.h"
#include "net/rime/broadcast.h"
#include <stdio.h>
#include <stdint.h>
/*---------------------------------------------------------------------------*/
#define LOOP_INTERVAL CLOCK_SECOND
#define LEDS_OFF_HYSTERISIS (RTIMER_SECOND >> 1)
#define LEDS_PERIODIC LEDS_YELLOW
#define LEDS_BUTTON LEDS_RED
#define LEDS_SERIAL_IN LEDS_ORANGE
#define LEDS_REBOOT LEDS_ALL
#define LEDS_RF_RX (LEDS_YELLOW | LEDS_ORANGE)
#define BROADCAST_CHANNEL 129
/*---------------------------------------------------------------------------*/
static struct etimer et;
static struct rtimer rt;
static uint16_t counter;
/*---------------------------------------------------------------------------*/
PROCESS(cc2538_demo_process, "cc2538 demo process");
AUTOSTART_PROCESSES(&cc2538_demo_process);
/*---------------------------------------------------------------------------*/
static void
broadcast_recv(struct broadcast_conn *c, const linkaddr_t *from)
{
leds_toggle(LEDS_RF_RX);
printf("Received %u bytes: '0x%04x'\n", packetbuf_datalen(),
*(uint16_t *)packetbuf_dataptr());
}
/*---------------------------------------------------------------------------*/
static const struct broadcast_callbacks bc_rx = { broadcast_recv };
static struct broadcast_conn bc;
/*---------------------------------------------------------------------------*/
void
rt_callback(struct rtimer *t, void *ptr)
{
leds_off(LEDS_PERIODIC);
}
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(cc2538_demo_process, ev, data)
{
PROCESS_EXITHANDLER(broadcast_close(&bc))
PROCESS_BEGIN();
counter = 0;
broadcast_open(&bc, BROADCAST_CHANNEL, &bc_rx);
etimer_set(&et, CLOCK_SECOND);
while(1) {
PROCESS_YIELD();
if(ev == PROCESS_EVENT_TIMER) {
leds_on(LEDS_PERIODIC);
printf("Counter = 0x%08x\n", counter);
etimer_set(&et, CLOCK_SECOND);
rtimer_set(&rt, RTIMER_NOW() + LEDS_OFF_HYSTERISIS, 1,
rt_callback, NULL);
counter++;
} else if(ev == sensors_event) {
if(data == &button_select_sensor) {
packetbuf_copyfrom(&counter, sizeof(counter));
broadcast_send(&bc);
} else if(data == &button_left_sensor || data == &button_right_sensor) {
leds_toggle(LEDS_BUTTON);
} else if(data == &button_down_sensor) {
cpu_cpsid();
leds_on(LEDS_REBOOT);
watchdog_reboot();
} else if(data == &button_up_sensor) {
sys_ctrl_reset();
}
} else if(ev == serial_line_event_message) {
leds_toggle(LEDS_SERIAL_IN);
}
}
PROCESS_END();
}
/*---------------------------------------------------------------------------*/
/**
* @}
* @}
* @}
*/