Code imported from the SICS CVS server

This commit is contained in:
adamdunkels 2006-06-18 07:49:33 +00:00
parent 0e2a28da94
commit ba681f47d6
40 changed files with 5258 additions and 0 deletions

View File

@ -0,0 +1,99 @@
/*
* Copyright (c) 2006, Swedish Institute of Computer Science.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: battery-sensor.c,v 1.1 2006/06/18 07:49:33 adamdunkels Exp $
*
* -----------------------------------------------------------------
*
* Author : Adam Dunkels, Joakim Eriksson, Niclas Finne
* Created : 2005-11-01
* Updated : $Date: 2006/06/18 07:49:33 $
* $Revision: 1.1 $
*/
#include "dev/battery-sensor.h"
#include <io.h>
#include "dev/irq.h"
const struct sensors_sensor battery_sensor;
static unsigned int battery_value;
/*---------------------------------------------------------------------------*/
static void
init(void)
{
battery_value = 0;
}
/*---------------------------------------------------------------------------*/
static int
irq(void)
{
battery_value = ADC12MEM6;
return 0;
}
/*---------------------------------------------------------------------------*/
static void
activate(void)
{
irq_adc12_activate(&battery_sensor, 6, (INCH_4 + SREF_0));
}
/*---------------------------------------------------------------------------*/
static void
deactivate(void)
{
irq_adc12_deactivate(&battery_sensor, 6);
battery_value = 0;
}
/*---------------------------------------------------------------------------*/
static int
active(void)
{
return irq_adc12_active(6);
}
/*---------------------------------------------------------------------------*/
static unsigned int
value(int type)
{
return battery_value;
}
/*---------------------------------------------------------------------------*/
static int
configure(int type, void *c)
{
return 0;
}
/*---------------------------------------------------------------------------*/
static void *
status(int type)
{
return NULL;
}
/*---------------------------------------------------------------------------*/
SENSORS_SENSOR(battery_sensor, BATTERY_SENSOR,
init, irq, activate, deactivate, active,
value, configure, status);

View File

@ -0,0 +1,48 @@
/*
* Copyright (c) 2006, Swedish Institute of Computer Science.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: battery-sensor.h,v 1.1 2006/06/18 07:49:33 adamdunkels Exp $
*
* -----------------------------------------------------------------
*
* Author : Adam Dunkels, Joakim Eriksson, Niclas Finne
* Created : 2005-11-01
* Updated : $Date: 2006/06/18 07:49:33 $
* $Revision: 1.1 $
*/
#ifndef __BATTERY_SENSOR_H__
#define __BATTERY_SENSOR_H__
#include "lib/sensors.h"
extern const struct sensors_sensor battery_sensor;
#define BATTERY_SENSOR "Battery"
#endif /* __BATTERY_SENSOR_H__ */

164
platform/esb/dev/beep.c Normal file
View File

@ -0,0 +1,164 @@
/*
* Copyright (c) 2005, Swedish Institute of Computer Science
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* This file is part of the Contiki operating system.
*
* @(#)$Id: beep.c,v 1.1 2006/06/18 07:49:33 adamdunkels Exp $
*/
#include <io.h>
#include "contiki-esb.h"
/*
* Flag to indicate if any of these functions should generate a sound
* or not The function is used beep_off() to to change this flag so
* none of the functions will generate sounds.
*/
static unsigned char beep_flag = BEEP_ON;
/*
* BEEPER_BIT is the bit in the io-register that is connected to the actual
* beeper, setting the bit high vill generate a high pitch tone.
*/
#define BEEPER_BIT 0x08
/*-----------------------------------------------------------------------------------*/
void
beep_alarm(int alarmmode, int len)
{
len = len / 200;
while(len > 0) {
/*
* Check here if we should beep or not since if we do it outside the
* while loop the call to this function would take muck less time, i.e.
* beep_flag would have side effects that might not be predictable.
*/
if(beep_flag & BEEP_ON) {
if((alarmmode == BEEP_ALARM1) && ((len & 7) > 4)) {
P2OUT |= BEEPER_BIT;
} else if((alarmmode == BEEP_ALARM2) && ((len & 15) > 12)) {
P2OUT |= BEEPER_BIT;
} else {
P2OUT &= ~BEEPER_BIT;
}
}
clock_delay(200);
len--;
}
P2OUT &= ~BEEPER_BIT;
}
/*-----------------------------------------------------------------------------------*/
void
beep_beep(int i)
{
if(beep_flag & BEEP_ON) {
/* Beep. */
P2OUT |= BEEPER_BIT;
clock_delay(i);
P2OUT &= ~BEEPER_BIT;
}
}
/*-----------------------------------------------------------------------------------*/
void
beep(void)
{
beep_beep(20);
}
/*-----------------------------------------------------------------------------------*/
void
beep_down(int d)
{
int i;
for(i = 8; i < d; i += i / 8) {
beep_beep(10);
clock_delay(i);
}
}
/*-----------------------------------------------------------------------------------*/
void
beep_on(void)
{
beep_flag |= BEEP_ON;
}
/*-----------------------------------------------------------------------------------*/
void
beep_off(void)
{
beep_flag &= ~BEEP_ON;
}
/*-----------------------------------------------------------------------------------*/
void
beep_spinup(void)
{
unsigned int i;
for(i = 10000; i > 80; i -= i / 20) {
beep_beep(2);
clock_delay(i);
}
for(i = 4980; i > 2000; i -= 20) {
leds_on(LEDS_ALL);
clock_delay(5000 - i);
leds_off(LEDS_ALL);
clock_delay(i);
}
}
/*-----------------------------------------------------------------------------------*/
void
beep_quick(int n)
{
int i;
for(i = 0; i < n; ++i) {
beep_beep(2000);
clock_delay(20000);
}
}
/*-----------------------------------------------------------------------------------*/
void beep_long(clock_time_t len) {
/*
* Check if the beeper is turned on or off, i.e. if a call should generate
* a noise or not.
*/
if(beep_flag & BEEP_ON) {
/* Turn on the beeper. */
P2OUT |= BEEPER_BIT;
}
clock_wait(len);
if(beep_flag & BEEP_ON) {
/* Turn the beeper off. */
P2OUT &= ~BEEPER_BIT;
}
}
/*-----------------------------------------------------------------------------------*/

166
platform/esb/dev/beep.h Normal file
View File

@ -0,0 +1,166 @@
/*
* Copyright (c) 2005, Swedish Institute of Computer Science
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* This file is part of the Contiki operating system.
*
* @(#)$Id: beep.h,v 1.1 2006/06/18 07:49:33 adamdunkels Exp $
*/
/**
* \addtogroup esb
* @{
*/
/**
* \defgroup beeper Beeper interface
* @{
*/
/**
* \file
* Interface to the beeper.
* \author Adam Dunkels <adam@sics.se>
*
*/
#ifndef __BEEP_H__
#define __BEEP_H__
#define BEEP_ON 1
#define BEEP_OFF 0
#define BEEP_ALARM1 1
#define BEEP_ALARM2 2
#include "sys/clock.h"
/**
* Beep for a specified time.
*
* This function causes the beeper to beep for the specified time. The
* time is measured in the same units as for the clock_delay()
* function.
*
* \note This function will hang the CPU during the beep.
*
* \note This function will stop any beep that was on previously when this
* function ends.
*
* \note If the beeper is turned off with beep_off() this call will still
* take the same time, though it will be silent.
*
* \param len The length of the beep.
*
*/
void beep_beep(int len);
/**
* Beep an alarm for a specified time.
*
* This function causes the beeper to beep for the specified time. The
* time is measured in the same units as for the clock_delay()
* function.
*
* \note This function will hang the CPU during the beep.
*
* \note This function will stop any beep that was on previously when this
* function ends.
*
* \note If the beeper is turned off with beep_off() this call will still
* take the same time, though it will be silent.
*
* \param alarmmode The alarm mode (BEEP_ALARM1,BEEP_ALARM2)
* \param len The length of the beep.
*
*/
void beep_alarm(int alarmmode, int len);
/**
* Produces a quick click-like beep.
*
* This function produces a short beep that sounds like a click.
*
*/
void beep(void);
/**
* A beep with a pitch-bend down.
*
* This function produces a pitch-bend sound with deecreasing
* frequency.
*
* \param len The length of the pitch-bend.
*
*/
void beep_down(int len);
/**
* Turn the beeper on.
*
* This function turns on the beeper. The beeper is turned off with
* the beep_off() function.
*/
void beep_on(void);
/**
* Turn the beeper off.
*
* This function turns the beeper off after it has been turned on with
* beep_on().
*/
void beep_off(void);
/**
* Produce a sound similar to a hard-drive spinup.
*
* This function produces a sound that is intended to be similar to
* the sound a hard-drive makes when it starts.
*
*/
void beep_spinup(void);
/**
* Beep for a long time (seconds)
*
* This function produces a beep with the specified length and will
* not return until the beep is complete. The length of the beep is
* specified using CLOCK_SECOND: a two second beep is CLOCK_SECOND *
* 2, and a quarter second beep is CLOCK_SECOND / 4.
*
* \note If the beeper is turned off with beep_off() this call will still
* take the same time, though it will be silent.
*
* \param len The length of the beep, measured in units of CLOCK_SECOND
*/
void beep_long(clock_time_t len);
void beep_quick(int num);
/** @} */
/** @} */
#endif /* __BEEP_H__ */

View File

@ -0,0 +1,108 @@
/*
* Copyright (c) 2005, Swedish Institute of Computer Science
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* This file is part of the Contiki operating system.
*
* @(#)$Id: button-sensor.c,v 1.1 2006/06/18 07:49:33 adamdunkels Exp $
*/
#include "contiki-esb.h"
const struct sensors_sensor button_sensor;
static struct timer debouncetimer;
HWCONF_PIN(BUTTON, 2, 7);
HWCONF_IRQ(BUTTON, 2, 7);
/*---------------------------------------------------------------------------*/
static void
init(void)
{
timer_set(&debouncetimer, 0);
BUTTON_IRQ_EDGE_SELECTD();
BUTTON_SELECT();
BUTTON_MAKE_INPUT();
}
/*---------------------------------------------------------------------------*/
static int
irq(void)
{
if(BUTTON_CHECK_IRQ()) {
if(timer_expired(&debouncetimer)) {
timer_set(&debouncetimer, CLOCK_SECOND / 4);
sensors_changed(&button_sensor);
return 1;
}
}
return 0;
}
/*---------------------------------------------------------------------------*/
static void
activate(void)
{
sensors_add_irq(&button_sensor, BUTTON_IRQ_PORT());
BUTTON_ENABLE_IRQ();
}
/*---------------------------------------------------------------------------*/
static void
deactivate(void)
{
BUTTON_DISABLE_IRQ();
sensors_remove_irq(&button_sensor, BUTTON_IRQ_PORT());
}
/*---------------------------------------------------------------------------*/
static int
active(void)
{
return BUTTON_IRQ_ENABLED();
}
/*---------------------------------------------------------------------------*/
static unsigned int
value(int type)
{
return BUTTON_READ() || !timer_expired(&debouncetimer);
}
/*---------------------------------------------------------------------------*/
static int
configure(int type, void *c)
{
return 0;
}
/*---------------------------------------------------------------------------*/
static void *
status(int type)
{
return NULL;
}
/*---------------------------------------------------------------------------*/
SENSORS_SENSOR(button_sensor, BUTTON_SENSOR,
init, irq, activate, deactivate, active,
value, configure, status);

View File

@ -0,0 +1,42 @@
/*
* Copyright (c) 2005, Swedish Institute of Computer Science
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* This file is part of the Contiki operating system.
*
* @(#)$Id: button-sensor.h,v 1.1 2006/06/18 07:49:33 adamdunkels Exp $
*/
#ifndef __BUTTON_SENSOR_H__
#define __BUTTON_SENSOR_H__
#include "lib/sensors.h"
extern const struct sensors_sensor button_sensor;
#define BUTTON_SENSOR "Button"
#endif /* __BUTTON_SENSOR_H__ */

View File

@ -0,0 +1,155 @@
/*
* Copyright (c) 2005, Swedish Institute of Computer Science
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* This file is part of the Contiki operating system.
*
* @(#)$Id: ctsrts-sensor.c,v 1.1 2006/06/18 07:49:33 adamdunkels Exp $
*/
/**
* RTS/CTS (Request to Send/Clear to Send) are the signals used for hardware
* flow control. By setting the RTS line to "ON" the host tells the connected
* device that it is ready to receive data. Hardware flow control is not
* implemented yet. This implementation is just so some application can use
* the pins, it would also be possible for rs232.c to use it for hardware
* handshake but as said, that is not implemented yet.
*/
#include "contiki-esb.h"
const struct sensors_sensor ctsrts_sensor;
HWCONF_PIN(RS232RTS, 1, 7);
HWCONF_PIN(RS232CTS, 1, 6);
HWCONF_IRQ(RS232CTS, 1, 6);
/*---------------------------------------------------------------------------*/
static void
init(void)
{
RS232RTS_SELECT();
RS232RTS_MAKE_OUTPUT();
RS232RTS_CLEAR();
RS232CTS_SELECT();
RS232CTS_MAKE_INPUT();
}
/**
* Indicate to host/client we are NOT ready to receive data. Sets the RTS pin
* to low.
*/
void ctsrts_rts_clear(void) {
RS232RTS_CLEAR();
}
/**
* Request host/client to send data. Sets the RTS pin to high.
*/
void ctsrts_rts_set(void) {
RS232RTS_SET();
}
/*---------------------------------------------------------------------------*/
static int
irq(void)
{
if(RS232CTS_CHECK_IRQ()) {
/* Change the flank triggering for the irq so we will detect next
shift. */
if(RS232CTS_READ()) {
RS232CTS_IRQ_EDGE_SELECTD();
} else {
RS232CTS_IRQ_EDGE_SELECTU();
}
sensors_changed(&ctsrts_sensor);
return 1;
}
return 0;
}
/*---------------------------------------------------------------------------*/
static void
activate(void)
{
/*
* Check current status on the CTS pin and set IRQ flank so we will detect
* a shift.
*/
if(RS232CTS_READ()) {
RS232CTS_IRQ_EDGE_SELECTD();
} else {
RS232CTS_IRQ_EDGE_SELECTU();
}
sensors_add_irq(&ctsrts_sensor, RS232CTS_IRQ_PORT());
RS232CTS_ENABLE_IRQ();
}
/*---------------------------------------------------------------------------*/
static void
deactivate(void)
{
RS232CTS_DISABLE_IRQ();
sensors_remove_irq(&ctsrts_sensor, RS232CTS_IRQ_PORT());
}
/*---------------------------------------------------------------------------*/
static int
active(void)
{
return RS232CTS_IRQ_ENABLED();
}
/*---------------------------------------------------------------------------*/
static unsigned int
value(int type)
{
/*
* Invert the bit and return.
* This is strange, accordingly to the MSP430 manual section 9.2.1, Input
* Register PxIN the bit should be low when input is low. In RealTerm on
* the PC I set RTS which is coupled to the CTS on the esb and I read a 0.
* Maybe RTS is defined active LOW on the PC? //Kalle
*/
return RS232CTS_READ()?0:1;
}
/*---------------------------------------------------------------------------*/
static int
configure(int type, void *c)
{
return 0;
}
/*---------------------------------------------------------------------------*/
static void *
status(int type)
{
return NULL;
}
/*---------------------------------------------------------------------------*/
SENSORS_SENSOR(ctsrts_sensor, CTSRTS_SENSOR,
init, irq, activate, deactivate, active,
value, configure, status);

View File

@ -0,0 +1,62 @@
/*
* Copyright (c) 2005, Swedish Institute of Computer Science
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* This file is part of the Contiki operating system.
*
* @(#)$Id: ctsrts-sensor.h,v 1.1 2006/06/18 07:49:33 adamdunkels Exp $
*/
/**
* RTS/CTS (Request to Send/Clear to Send) are the signals used for hardware
* flow control. By setting the RTS line to "ON" the host tells the connected
* device that it is ready to receive data. Hardware flow control is not
* implemented yet. This implementation is just so some application can use
* the pins, it would also be possible for rs232.c to use it for hardware
* handshake but that is not implemented yet.
*/
#ifndef __CTSRTS_SENSOR_H__
#define __CTSRTS_SENSOR_H__
#include "contiki-esb.h"
extern const struct sensors_sensor ctsrts_sensor;
#define CTSRTS_SENSOR "CTSRTS"
/**
* Indicate to host/client we are NOT ready to receive data. Sets the RTS pin
* to low.
*/
void ctsrts_rts_clear(void);
/**
* Request host/client to send data. Sets the RTS pin to high.
*/
void ctsrts_rts_set(void);
#endif /* __CTSRTS_SENSOR_H__ */

229
platform/esb/dev/ds1629.c Normal file
View File

@ -0,0 +1,229 @@
/*
Copyright 2005, Freie Universitaet Berlin. All rights reserved.
These sources were developed at the Freie Universität Berlin, Computer
Systems and Telematics group.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
- Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
- 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.
- Neither the name of Freie Universitaet Berlin (FUB) 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 FUB and the contributors on an "as is"
basis, without any representations or warranties of any kind, express
or implied including, but not limited to, representations or
warranties of non-infringement, merchantability or fitness for a
particular purpose. In no event shall FUB 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 implementation was developed by the CST group at the FUB.
For documentation and questions please use the web site
http://scatterweb.mi.fu-berlin.de and the mailinglist
scatterweb@lists.spline.inf.fu-berlin.de (subscription via the Website).
Berlin, 2005
*/
/**
* Part of the source code from ScatterWeb 2.2 (ScatterWeb.{Data,System}.c)
* released by Freie Universitaet Berlin has been reworked and
* reformatted to fit the Contiki ESB port.
*/
#include "contiki-conf.h"
#include "dev/ds1629.h"
#include <io.h>
#define SDA_HIGH (P5OUT |= 0x01) /* RTC data line high */
#define SDA_LOW (P5OUT &= 0xFE) /* RTC data line low */
#define SCL_HIGH (P5OUT |= 0x02) /* RTC clock line high */
#define SCL_LOW (P5OUT &= 0xFD) /* RTC clock line low */
#define BUS_READ 0x9F
#define BUS_WRITE 0x9E
#define ACC_CSR 0xAC /* Access Configuration/Control Register */
#define ACC_CLOCK 0xC0 /* Access Clock Register */
#define ACC_CLOCK_ALARM 0xC7 /* Access Clock Alarm Register */
#define ACC_TH 0xA1 /* Access Thermostat Setpoint High */
#define ACC_TL 0xA2 /* Access Thermostat Setpoint Low */
#define ACC_CSRAM 0x17 /* Access Clock 32 byte SRAM */
#define ACC_RT 0xAA /* Access Read Temperatur Register */
#define CSR_OS1 (0x80)
#define CSR_OS0 (0x40)
#define CSR_A1 (0x20)
#define CSR_A0 (0x10)
#define CSR_CNV (0x04)
#define CSR_POL (0x02)
#define CSR_1SH (0x01)
#define CSR_DEFAULT (CSR_OS1 + CSR_OS0 + CSR_A1 + CSR_CNV + CSR_1SH + CSR_POL)
/**
* Temperature type (built on a signed int). It's a signed (twos complement)
* fixed point value with 8 bits before comma and 7 bits after. So Bit 15 is
* sign, Bit14-7 is before comma and Bit 6-0 after comma.
*
* @since 2.0
*/
typedef union { unsigned int u; signed int s; } temp_t;
/*--------------------------------------------------------------------------*/
/* Puts the start condition on bus. */
static void
cl_start(void)
{
P5DIR |= 0x03; /* ensure: P50(SDA), P51(SCL) output */
SCL_LOW; _NOP(); _NOP();
SDA_HIGH; _NOP(); _NOP();
SCL_HIGH; _NOP(); _NOP();
SDA_LOW; _NOP(); _NOP();
SCL_LOW; _NOP(); _NOP();
}
/*--------------------------------------------------------------------------*/
/* Puts the stop condition on bus. */
static void
cl_stop()
{
SCL_LOW; _NOP(); _NOP();
SDA_LOW; _NOP(); _NOP();
SCL_HIGH; _NOP(); _NOP();
SDA_HIGH; _NOP(); _NOP();
SCL_LOW; _NOP(); _NOP();
P5DIR &= ~0x03;
}
/*--------------------------------------------------------------------------*/
/* Writes a byte on the bus, returns the acknowledge bit. */
static u16_t
cl_writeOnBus(u8_t byte)
{
u16_t i, ack;
for(i=0;i<8;i++) {
if(byte & 0x80) SDA_HIGH; else SDA_LOW;
SCL_HIGH;
byte = byte << 1; _NOP();
SCL_LOW; _NOP();
}
/* check ack */
P5DIR &= 0xFE; /* P50(SDA) input */
SCL_HIGH;
if(P5IN & 0x01) ack = 0; else ack = 1; /* test if ack=0, else error */
_NOP();
SCL_LOW;
P5DIR |= 0x01; /* P50(SDA) output */
return ack;
}
/*--------------------------------------------------------------------------*/
static u8_t
cl_readFromBus(u16_t ack)
{
u16_t i;
u8_t byte = 0;
P5DIR &= 0xFE; /* P50(SDA) input */
for(i=0;i<8;i++) {
byte = byte << 1;
SCL_HIGH;
if(P5IN & 0x01) byte |= 0x01; else byte &= 0xFE;
SCL_LOW;
}
P5DIR |= 0x01; /* P50(SDA) output */
if(ack) SDA_LOW; else SDA_HIGH;
SCL_HIGH;
SCL_LOW;
return byte;
}
/*--------------------------------------------------------------------------*/
static u16_t
getReg16bit(u8_t acc, u16_t bitmask)
{
u16_t config = 0;
do cl_start();
while(!cl_writeOnBus(BUS_WRITE));
cl_writeOnBus(acc);
cl_start();
cl_writeOnBus(BUS_READ);
config = cl_readFromBus(1);
config = config << 8;
config += cl_readFromBus(0);
cl_stop();
config &= bitmask;
_NOP();
_NOP();
return config;
}
/*--------------------------------------------------------------------------*/
/* Only first 8 bit of Configuration Status Register can be set */
static void
setCSReg(u8_t setting)
{
do cl_start();
while(!cl_writeOnBus(BUS_WRITE));
cl_writeOnBus(ACC_CSR);
cl_writeOnBus(setting);
cl_stop();
_NOP();
_NOP();
_NOP();
_NOP();
}
/*--------------------------------------------------------------------------*/
static void
System_startConversion(void)
{
do cl_start(); /* do start until BUS_WRITE is acked */
while(!cl_writeOnBus(BUS_WRITE)); /* control byte */
cl_writeOnBus(0xEE); /* start conversion */
cl_stop();
}
/*--------------------------------------------------------------------------*/
/* RTC initialization. Initializes RTC with ::CSR_DEFAULT. */
static void
initClock(void)
{
u8_t csr = getReg16bit(ACC_CSR,0xFF00) >> 8;
if(csr!=CSR_DEFAULT) setCSReg(CSR_DEFAULT); /* if desired config isnt in clock => set it */
/* IMPORTANT: Ensure quartz is generating 32768 Hz */
/* (sometimes CH bit gets set when clock is read while reset) */
do cl_start(); /* Do start until BUS_WRITE is acked. */
while(!cl_writeOnBus(BUS_WRITE)); /* Send control byte */
cl_writeOnBus(ACC_CLOCK); /* Send command byte ::ACC_CLOCK. */
cl_writeOnBus(0x00); /* Send starting address 0x00. */
cl_writeOnBus(0x00); /* Set CH to 0, tseconds and seconds will also be reset! */
cl_stop(); /* Stop condition. */
}
/*--------------------------------------------------------------------------*/
void
ds1629_init()
{
initClock();
}
/*--------------------------------------------------------------------------*/
void
ds1629_start()
{
System_startConversion();
}
/*--------------------------------------------------------------------------*/
signed int
ds1629_temperature()
{
temp_t temperature;
ds1629_start();
temperature.u = getReg16bit(ACC_RT,0xFFFF);
return temperature.s;
}

72
platform/esb/dev/ds1629.h Normal file
View File

@ -0,0 +1,72 @@
/*
* Copyright (c) 2005, Swedish Institute of Computer Science.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* This file is part of the Configurable Sensor Network Application
* Architecture for sensor nodes running the Contiki operating system.
*
* $Id: ds1629.h,v 1.1 2006/06/18 07:49:33 adamdunkels Exp $
*
* -----------------------------------------------------------------
*
* Author : Adam Dunkels, Joakim Eriksson, Niclas Finne
* Created : 2005-11-08
* Updated : $Date: 2006/06/18 07:49:33 $
* $Revision: 1.1 $
*/
#ifndef __DS1629_H__
#define __DS1629_H__
/**
* DS1629 initialization function.
*
* This function should be called at startup to initialize the DS1629
* and its clock.
*/
void ds1629_init(void);
/**
* DS1629 start conversion function.
*
* This function should be called before requesting the temperature
* the first time.
*/
void ds1629_start(void);
/**
* Get the temperature.
*
* This function will return the current temperature as a signed 9 bit
* value in terms of a 0.5 degree Celsius least significant bit.
*
* MSB Sign 2^6 2^5 2^4 2^3 2^2 2^1 2^0
* LSB 2^-1 0 0 0 0 0 0 0
*/
signed int ds1629_temperature(void);
#endif /* __DS1629_H__ */

324
platform/esb/dev/eeprom.c Normal file
View File

@ -0,0 +1,324 @@
/**
* \file
* EEPROM functions.
* \author Adam Dunkels <adam@sics.se>
*/
/* Copyright (c) 2004 Swedish Institute of Computer Science.
* 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.
*
* $Id: eeprom.c,v 1.1 2006/06/18 07:49:33 adamdunkels Exp $
*
* Author: Adam Dunkels <adam@sics.se>
*
*/
/*
Copyright 2003/2004, Freie Universitaet Berlin. All rights reserved.
These sources were developed at the Freie Universit\x{FFFF}t Berlin, Computer
Systems and Telematics group.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
- Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
- 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.
- Neither the name of Freie Universitaet Berlin (FUB) 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 FUB and the contributors on an "as is"
basis, without any representations or warranties of any kind, express
or implied including, but not limited to, representations or
warranties of non-infringement, merchantability or fitness for a
particular purpose. In no event shall FUB 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 implementation was developed by the CST group at the FUB.
Contributors: Thomas Pietsch, Bjoern Lichtblau
For documentation and questions please use the web site
http://www.scatterweb.net and the mailinglist
scatterweb@lists.spline.inf.fu-berlin.de (subscription via the Website).
Berlin, 2003/2004
*/
#include <msp430x14x.h>
#include <io.h>
#include "dev/eeprom.h"
/* Temporary switch for the eeprom address. */
#define EEPROMADDRESS (0x00) /* use 8k EEPROM, future versions will have only one 64k eeprom at this address */
/*#define EEPROMADDRESS (0x02)*/ /* use 64k EEPROM */
/* must be set together with EEPROMADDRESS */
/*#define EEPROMPAGEMASK (0x1F) */ /* 8k EEPROM, 32b page writes possible */
#define EEPROMPAGEMASK (0x7F) /* 64k EEPROM, 128b page writes possible */
/*
* Macros for accessing the clock and data lines. Data is on P56 and
* clock on P57.
*/
#define SDA_HIGH (P5OUT |= 0x04) /**< EEPROM data line high */
#define SDA_LOW (P5OUT &= 0xFB) /**< EEPROM data line low */
#define SCL_HIGH (P5OUT |= 0x08) /**< EEPROM clock line high */
#define SCL_LOW (P5OUT &= 0xF7) /**< EEPROM clock line low */
/*-----------------------------------------------------------------------------------*/
/**
* \internal \name Basic functions for interfacing the i2c-like EEPROM bus.
*/
/** @{ */
/**
* \internal
* Put start condition on the bus.
*/
static void
start(void)
{
P5DIR |= 0x0C; /* ensure: P52(SDA), P53(SCL) output */
SCL_LOW; _NOP(); _NOP();
SDA_HIGH; _NOP(); _NOP();
SCL_HIGH; _NOP(); _NOP();
SDA_LOW; _NOP(); _NOP();
SCL_LOW; _NOP(); _NOP();
}
/*-----------------------------------------------------------------------------------*/
/**
* \internal
* Put stop condition on the bus.
*/
static void
stop(void)
{
//P5DIR |= 0x0C; /* ensure: P52(SDA), P53(SCL) output */
SCL_LOW; _NOP(); _NOP();
SDA_LOW; _NOP(); _NOP();
SCL_HIGH; _NOP(); _NOP();
SDA_HIGH; _NOP(); _NOP();
SCL_LOW; _NOP(); _NOP();
P5DIR &= ~0x0C;
}
/*-----------------------------------------------------------------------------------*/
/**
* \internal
* Write a byte on the bus, return the acknowledge bit.
*/
static int
write_bus(unsigned char byte)
{
int i, ack;
/* Write byte, one bit at a time. Start with the leftmost (most
significant) bit and roll in bits from the right. */
for(i = 0; i < 8; ++i) {
if(byte & 0x80) {
SDA_HIGH;
} else {
SDA_LOW;
}
_NOP();
_NOP();
SCL_HIGH;
_NOP();
_NOP();
byte = byte << 1;
SCL_LOW;
_NOP();
_NOP();
}
/* check ack */
P5DIR &= 0xFB; /* P52(SDA) input */
SCL_HIGH;
_NOP();
_NOP();
if(P5IN & 0x04) {
ack = 0;
} else {
ack = 1; /* test if ack=0, else error */
}
SCL_LOW;
_NOP();
_NOP();
P5DIR |= 0x04; /* P52(SDA) output */
_NOP();
_NOP();
_NOP();
return ack;
}
/*-----------------------------------------------------------------------------------*/
/**
* \internal
* Read one byte from the bus.
*
* \param ack If set, the ackbit after the received byte will be set.
*/
static unsigned char
read_bus(unsigned char ack)
{
int i;
unsigned char byte = 0;
P5DIR &= 0xFB; /* P52(SDA) input */
for(i = 0; i < 8; ++i) {
byte = byte << 1;
SCL_HIGH;
_NOP();
_NOP();
if(P5IN & 0x04) {
byte |= 0x01;
} else {
byte &= 0xFE;
}
_NOP();
SCL_LOW;
_NOP();
_NOP();
}
P5DIR |= 0x04; /* P52(SDA) output */
if(ack) {
SDA_LOW;
} else {
SDA_HIGH;
}
_NOP();
SCL_HIGH;
_NOP();
SCL_LOW;
_NOP();
return byte;
}
/** @} */
/*-----------------------------------------------------------------------------------*/
/**
* Read bytes from the EEPROM using sequential read.
*/
void
eeprom_read(unsigned short addr, unsigned char *buf, int size)
{
unsigned int i;
if(size <= 0) {
return;
}
do {
/* Wait if the writecycle has not finished. */
start();
/* 1010 control, 000 address, 0=write --- but only inits address */
} while(!write_bus(0xa0 | EEPROMADDRESS));
/* Write address to bus, low byte first. */
write_bus(addr >> 8);
write_bus(addr & 0xff);
start();
/* 1010 control, 000 address, 1=read */
write_bus(0xa1 | EEPROMADDRESS);
for(i = 0; i < (size - 1); ++i){
buf[i] = read_bus(1);
}
buf[size - 1] = read_bus(0);
stop();
}
/*-----------------------------------------------------------------------------------*/
/**
* Write bytes to EEPROM using sequencial write.
*/
void
eeprom_write(unsigned short addr, unsigned char *buf,
int size)
{
unsigned int i = 0;
unsigned int curaddr;
if(size <= 0) {
return;
}
/* Disable write protection. */
P5OUT &= 0xEF;
curaddr = addr;
for(i = 0; i < size; ++i) {
/* If we are writing the first byte or are on a 128b page boundary
we have to start a new write. */
if(i == 0 || (curaddr & EEPROMPAGEMASK) == 0) {
do {
start();
/* 1010 control, 000 addresse, 0=write */
} while(!write_bus(0xa0 | EEPROMADDRESS));
/* Write the new address to the bus. */
if(write_bus(curaddr >> 8) == 0) {
return;
}
if(write_bus(curaddr) == 0) {
return;
}
}
/* Write byte. */
if(write_bus(buf[i]) == 0) {
return;
}
/* If we are writing the last byte totally or of a 128b page
generate a stop condition */
if(i == size - 1 || (curaddr & EEPROMPAGEMASK) == EEPROMPAGEMASK) {
stop();
}
++curaddr;
}
/* Enable write protection. */
P5OUT |= 0x10;
}
/*-----------------------------------------------------------------------------------*/

View File

@ -0,0 +1,66 @@
/*
* Copyright (c) 2005, Swedish Institute of Computer Science.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* This file is part of the Contiki operating system.
*
* $Id: esb-sensors.c,v 1.1 2006/06/18 07:49:33 adamdunkels Exp $
*/
/**
* \file
* Functions for turning the ESB sensors on or off
* \author
* Adam Dunkels <adam@sics.se>
*
* This file will eventually be changed into a better API. This is
* sufficient for now.
*/
#include "contiki-esb.h"
HWCONF_PIN(SENSORSWITCH, 5, 5);
/*---------------------------------------------------------------------------*/
void
esb_sensors_init(void)
{
SENSORSWITCH_SELECT();
SENSORSWITCH_MAKE_OUTPUT();
}
/*---------------------------------------------------------------------------*/
void
esb_sensors_on(void)
{
SENSORSWITCH_CLEAR();
}
/*---------------------------------------------------------------------------*/
void
esb_sensors_off(void)
{
SENSORSWITCH_SET();
}
/*---------------------------------------------------------------------------*/

View File

@ -0,0 +1,48 @@
/*
* Copyright (c) 2005, Swedish Institute of Computer Science.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* This file is part of the Contiki operating system.
*
* $Id: esb-sensors.h,v 1.1 2006/06/18 07:49:33 adamdunkels Exp $
*/
/**
* \file
* A brief description of what this file is.
* \author
* Adam Dunkels <adam@sics.se>
*/
#ifndef __ESB_SENSORS_H__
#define __ESB_SENSORS_H__
void esb_sensors_init(void);
void esb_sensors_on(void);
void esb_sensors_off(void);
#endif /* __ESB_SENSORS_H__ */

316
platform/esb/dev/ir.c Normal file
View File

@ -0,0 +1,316 @@
/* The software in this file is based on code from FU Berlin. */
/*
Copyright 2003/2004, Freie Universitaet Berlin. All rights reserved.
These sources were developed at the Freie Universität Berlin, Computer
Systems and Telematics group.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
- Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
- 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.
- Neither the name of Freie Universitaet Berlin (FUB) 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 FUB and the contributors on an "as is"
basis, without any representations or warranties of any kind, express
or implied including, but not limited to, representations or
warranties of non-infringement, merchantability or fitness for a
particular purpose. In no event shall FUB 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 implementation was developed by the CST group at the FUB.
Contributors: Thomas Pietsch, Bjoern Lichtblau
*/
/* \file recir.c
** \ingroup Firmware
** \brief Receiving RC5 via IR Receiving Diode.
**
** \code
** RC5: 1780 us bitlength (manchester encoded, so half bitlength of 890 us is important)
** Transferred packet (2 start + toggle bit + 5 address bits + 6 comand bits)):
** | S | S | T | A4 | A3 | A2 | A1 | A0 | C5 | C4 | C3 | C2 | C1 | C0 |
** irdata format: | ? | ? | error | newData | T | A4 | A3 | A2 | A1 | A0 | C5 | C4 | C3 | C2 | C1 | C0 |
** \endcode
**
** <img src="../pics/rc5.jpg">
** See detailed description at <a href="http://users.pandora.be/davshomepage/rc5.htm">http://users.pandora.be/davshomepage/rc5.htm</a>
**
** Some common addresses and commands:
** \code
** Address: Device: Command:
** 0 TV1 0...9 Numbers 0...9 (channel select)
** 1 TV2 12 Standby
** 5 VCR1 16 Master Volume +
** 6 VCR2 17 Master Volume -
** 17 Tuner 18 Brightness +
** 18 Audio Tape 19 Brightness -
** 20 CD Player 50 Fast rewind
** 52 Fast run forward
** 53 Play
** 54 Stop
** 55 Recording
** \endcode
**/
#include <io.h>
#include <signal.h>
#include "dev/ir.h"
#include "dev/leds.h"
#include "dev/beep.h"
PROCESS(ir_process, "IR receiver");
process_event_t ir_event_received;
/*---------------------------------------------------------------------------*/
#define SIR1 (P1OUT |= 0x01) ///< MACRO: Puts IR sending diode high.
#define SIR0 (P1OUT &= 0xFE) ///< MACRO: Puts IR sending diode low.
#define BIT75 3282 ///< 3 quarters of a bit after start, 3282 cyc @ 2,4576Mhz = 1335us.
#define BIT50 2188 ///< Half of bit length, 2188 cyc @ 2,4576Mhz = 890 us.
/*---------------------------------------------------------------------------*/
/* Sends a logical one via IR, method is timed for the 2.4576Mhz SMCLK!!!
*/
static volatile void
send1bit(void)
{
volatile int i;
for(i = 0; i < 34; ++i) {
SIR1; SIR1; SIR1; SIR1;
SIR0; SIR0; SIR0; SIR0;
SIR0; SIR0; SIR0; SIR0;
SIR0;
}
}
/*---------------------------------------------------------------------------*/
/* Sends a logical 0 via IR, method is timed for the 2.4576Mhz SMCLK!!!
*/
static volatile void
send0bit(void)
{
volatile int i;
for(i = 0; i < 34; ++i) {
SIR0; SIR0; SIR0; SIR0;
SIR0; SIR0; SIR0; SIR0;
SIR0; SIR0; SIR0; SIR0;
SIR0;
}
}
/*---------------------------------------------------------------------------*/
/* Sends the lower 12 bits of data via IR, turns interrupt off while
it's sending.
*/
void
ir_send(unsigned short data)
{
volatile unsigned short mask = 0x2000;
data |= 0xF000;
dint();
while(mask != 0){
if(!(mask & data)){
send1bit();
send0bit();
} else {
send0bit();
send1bit();
}
mask /= 2;
}
eint();
}
/*---------------------------------------------------------------------------*/
/* Testroutine which repetedly sends two commands.
*/
/*void
ir_test_send(void)
{
volatile unsigned int i;
send12bits(0xF010);
for(i=0; i<0xFFFF; i++) nop();
send12bits(0xF011);
for(i=0; i<0xFFFF; i++) nop();
}*/
/*---------------------------------------------------------------------------*/
static void setErrorBit(void);
static void clearErrorBit(void);
static void setDataAvailableBit(void);
static void clearDataAvailableBit(void);
/// \name Internal variables.
//@{
static unsigned int ir_pos; ///< current position in frame
static unsigned int recvdata; ///< here a received packet is saved
static unsigned int recvdatabuffer; ///< temporary buffer for receiving
static unsigned char ir_temp; ///< saves the first half of the manchester bit
//@}
/// \name Public functions.
/// If ::recir_dataAvailable()==1 use the get* functions.
//@{
unsigned char recir_getCode(void){ return (recvdata & 0x003F); }
unsigned char recir_getAddress(void){ return ((recvdata & 0x07C0) >> 6); }
unsigned char recir_getToggle(void){ return ((recvdata & 0x0800) >> 11); }
unsigned char recir_getError(void){ return ((recvdata & 0x2000) >> 13); }
u16_t
ir_data(void)
{
return recvdata;
}
u8_t
ir_poll(void)
{
if(recvdata & 0x1000) {
clearDataAvailableBit();
return 1;
} else {
return 0;
}
}
///\name Internal functions.
//@{
static void setErrorBit(void){ recvdata |= 0x2000; }
static void clearErrorBit(void) { recvdata &= 0xDFFF; }
static void setDataAvailableBit(void){ recvdata |= 0x1000; }
static void clearDataAvailableBit(void){ recvdata &= 0xEFFF; }
/// Timer B0 interrupt service routine
interrupt(TIMERB1_VECTOR) Timer_B1 (void) {
/*P2OUT = (P2OUT & 0xf7) | (8 - (P2OUT & 0x08));*/
if(ir_pos <= 25) {
if(ir_pos % 2) { // odd position
if(ir_temp && !(P1IN & 0x04)) { // 1 - 0 --> write 1
recvdatabuffer +=1;
recvdatabuffer = recvdatabuffer << 1;
} else if(!ir_temp && (P1IN & 0x04)) { // 0 - 1 --> write 0
recvdatabuffer = recvdatabuffer << 1;
} else {
setErrorBit();
if(P1IN & 0x04) {
recvdatabuffer += 1;
}
recvdatabuffer = recvdatabuffer << 1;
}
} else { // even position
ir_temp = P1IN & 0x04;
}
}
if(ir_pos == 25) { // end reached
recvdatabuffer = recvdatabuffer >> 1;
if(!recir_getError() && ( (recvdatabuffer & 0x0FFF) != (recvdata & 0x0FFF) ) ){
recvdata = recvdatabuffer;
setDataAvailableBit();
} else {
_NOP();
}
}
if(ir_pos==27) {
TBCCTL1 &= ~CCIE;
//GREENOFF;
// temporary debug output
//sendRS232Address(recvdatabuffer);
//if(recir_getError()) sendRS232('E');
//sendRS232String("\r\n");
if(!recir_getError()) beep_beep(20);
// reenable interrupt for falling edge
P1IFG &= ~(0x04);
P1IE |= 0x04; // enable interrupt for recir RC5
leds_red(LEDS_OFF);
}
ir_pos++;
TBCCR1 += BIT50; // set new interrupt
TBCCTL1 &= ~CCIFG;
}
/** \brief IR Interrupt routine
**
** For the falling edge (start of RC5 packet)( mid of first start bit ), IRReceiver is on P12
** real interrupt routine, which calls this, is in sensors.c */
void
ir_irq(void)
{
if(P1IN & 0x04) return; // high again, just a peak
ir_pos = 0;
recvdatabuffer = 0;
clearErrorBit();
// the first timer interrupt will occur in the mid of the first half of the second start bit
TBCCR1 = TBR + BIT75; // set first TBCCR1 IRQ to 75% of RC5 bitlength
TBCCTL1 &= ~CCIFG; // clear previous compare flag
TBCCTL1 |= CCIE; // CCR0 interrupt enabled, interrupt occurs when timer equals CCR0
P1IE &= ~0x04; // disable interrupt for P12 ( ReceiveIR )
leds_red(LEDS_ON);
//GREENON;
}
//@}
/*---------------------------------------------------------------------*/
PROCESS_THREAD(ir_process, ev, data)
{
PROCESS_BEGIN();
// init TIMERB ccr0 to run continouslycreate the 5 ms interval
// ccr1 is used for ir receiving (RC5)
TBCTL = TBSSEL1 + TBCLR; // select SMCLK (2.4576MHz), clear TBR
TBCTL |= MC1; // Start Timer_A in continuous mode
P1IES |= 0x04; // Important for IR-RC5 receive to detect the first FALLING edge
ir_event_received = process_alloc_event();
while(1) {
PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_POLL);
if(ir_poll() == IR_DATA) {
unsigned short irdata;
irdata = ir_data() & 0x7ff;
process_post(PROCESS_BROADCAST, ir_event_received, (process_data_t)irdata);
}
}
PROCESS_END();
}
/*---------------------------------------------------------------------*/

71
platform/esb/dev/ir.h Normal file
View File

@ -0,0 +1,71 @@
/*
* Copyright (c) 2005, Swedish Institute of Computer Science
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* This file is part of the Contiki operating system.
*
* @(#)$Id: ir.h,v 1.1 2006/06/18 07:49:33 adamdunkels Exp $
*/
#ifndef __IR_H__
#define __IR_H__
#include "contiki.h"
#include "contiki-net.h"
/**
* Initialize the IR driver.
*
* This function should be called from the main boot-up procedure in
* order to initialize the IR device driver.
*
*/
void ir_init(void);
void ir_send(unsigned short low12bits);
u8_t ir_poll(void);
#define IR_NODATA 0
#define IR_DATA 1
u16_t ir_data(void);
void ir_irq(void);
#define IR_STANDBY 0x0c
#define IR_VOLUME_UP 0x10
#define IR_VOLUME_DOWN 0x11
#define IR_VCR_PLAY 0x35
#define IR_VCR_STOP 0x36
#define IR_VCR_RECORD 0x37
#define IR_VCR_REWIND 0x32
#define IR_VCR_WIND 0x34
extern process_event_t ir_event_received;
PROCESS_NAME(ir_process);
#endif /* __IR_H__ */

181
platform/esb/dev/irq.c Normal file
View File

@ -0,0 +1,181 @@
/*
* Copyright (c) 2005, Swedish Institute of Computer Science
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* This file is part of the Contiki operating system.
*
* @(#)$Id: irq.c,v 1.1 2006/06/18 07:49:33 adamdunkels Exp $
*/
#include "contiki-esb.h"
#include <signal.h>
#define ADC12MCTL_NO(adcno) ((unsigned char *) ADC12MCTL0_)[adcno]
static unsigned char adcflags;
/*---------------------------------------------------------------------------*/
interrupt(PORT1_VECTOR)
irq_p1(void)
{
if(sensors_handle_irq(IRQ_PORT1)) {
LPM_AWAKE();
}
P1IFG = 0x00;
}
/*---------------------------------------------------------------------------*/
interrupt(PORT2_VECTOR)
irq_p2(void)
{
if(sensors_handle_irq(IRQ_PORT2)) {
LPM_AWAKE();
}
P2IFG = 0x00;
}
/*---------------------------------------------------------------------------*/
interrupt (ADC_VECTOR)
irq_adc(void)
{
if(sensors_handle_irq(IRQ_ADC)) {
LPM_AWAKE();
}
}
/*---------------------------------------------------------------------------*/
void
irq_init(void)
{
adcflags = 0;
/* Setup ADC12, ref., sampling time */
ADC12CTL0 = REF2_5V | SHT0_10 | SHT1_10 | MSC;
/* Use sampling timer, repeat-sequence-of-channels */
/* ADC12CTL1 = SHP | CONSEQ_3 | ADC12DIV_3; */
ADC12CTL1 = SHP | CONSEQ_3;
}
/*---------------------------------------------------------------------------*/
/* Set lowest ADC to be start in sequence and highest to be interrupt
enabled and set end-of-sequence on the highest active ADC */
static void
sethilo(void)
{
int c;
/* Clear start of sequence */
ADC12CTL1 &= ~(CSTARTADD_15);
/* Set new start of sequence to lowest active memory holder */
for(c = 0; c < 8; c++) {
if(adcflags & (1 << c)) {
ADC12CTL1 |= (c * CSTARTADD_1);
break;
}
}
/* Clear all interrupts and end-of-sequences */
ADC12IE = 0;
for(c = 0; c < 8; c++) {
ADC12MCTL_NO(c) &= ~EOS;
}
/* Set highest interrupt and end-of-sequence. This will generate one
interrupt for each sequence of conversions. */
for(c = 0; c < 8; c++) {
if(adcflags & (128 >> c)) {
ADC12IE |= 128 >> c;
ADC12MCTL_NO(7 - c) |= EOS;
break;
}
}
}
/*---------------------------------------------------------------------------*/
void
irq_adc12_activate(const struct sensors_sensor *sensor,
unsigned char adcno, unsigned char config)
{
/* stop converting */
ADC12CTL0 &= ~ENC;
ADC12IE = 0;
/* wait for conversion to stop */
while(ADC12CTL0 & ADC12BUSY);
/* clear any pending interrupts */
ADC12IFG = 0;
adcflags |= (1 << adcno);
ADC12MCTL_NO(adcno) = config;
sethilo();
ADC12CTL0 |= ADC12ON | REFON;
sensors_add_irq(sensor, IRQ_ADC);
/* Delay */
clock_delay(20000);
ADC12CTL0 |= ENC | ADC12SC;
}
/*---------------------------------------------------------------------------*/
void
irq_adc12_deactivate(const struct sensors_sensor *sensor, unsigned char adcno)
{
/* stop converting */
ADC12CTL0 &= ~ENC;
ADC12IE = 0;
/* wait for conversion to stop */
while(ADC12CTL0 & ADC12BUSY);
/* clear any pending interrupts */
ADC12IFG = 0;
adcflags &= ~(1 << adcno);
ADC12MCTL_NO(adcno) = 0;
sethilo();
sensors_remove_irq(sensor, IRQ_ADC);
if(!adcflags) {
/* Turn off the ADC12 */
ADC12CTL0 &= ~(ADC12ON | REFON);
} else {
/* Still active. Turn on the conversion. */
ADC12CTL0 |= ENC | ADC12SC;
}
}
/*---------------------------------------------------------------------------*/
int
irq_adc12_active(unsigned char adcno)
{
return adcflags & (1 << adcno) ? 1 : 0;
}
/*---------------------------------------------------------------------------*/

44
platform/esb/dev/irq.h Normal file
View File

@ -0,0 +1,44 @@
/*
* Copyright (c) 2005, Swedish Institute of Computer Science
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* This file is part of the Contiki operating system.
*
* @(#)$Id: irq.h,v 1.1 2006/06/18 07:49:33 adamdunkels Exp $
*/
#ifndef __IRQ_H__
#define __IRQ_H__
void irq_adc12_activate(const struct sensors_sensor *sensor,
unsigned char adcno, unsigned char config);
void irq_adc12_deactivate(const struct sensors_sensor *sensor,
unsigned char adcno);
int irq_adc12_active(unsigned char adcno);
#endif /* __IRQ_H__ */

View File

@ -0,0 +1,103 @@
/*
* Copyright (c) 2005, Swedish Institute of Computer Science
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* This file is part of the Contiki operating system.
*
* @(#)$Id: pir-sensor.c,v 1.1 2006/06/18 07:49:33 adamdunkels Exp $
*/
#include "contiki-esb.h"
const struct sensors_sensor pir_sensor;
static unsigned int pir;
HWCONF_PIN(PIR, 1, 3);
HWCONF_IRQ(PIR, 1, 3);
/*---------------------------------------------------------------------------*/
static int
irq(void)
{
if(PIR_CHECK_IRQ()) {
++pir;
sensors_changed(&pir_sensor);
return 1;
}
return 0;
}
/*---------------------------------------------------------------------------*/
static void
init(void)
{
pir = 0;
PIR_SELECT();
PIR_MAKE_INPUT();
}
/*---------------------------------------------------------------------------*/
static void
activate(void)
{
sensors_add_irq(&pir_sensor, PIR_IRQ_PORT());
PIR_ENABLE_IRQ();
}
/*---------------------------------------------------------------------------*/
static void
deactivate(void)
{
PIR_DISABLE_IRQ();
sensors_remove_irq(&pir_sensor, PIR_IRQ_PORT());
}
/*---------------------------------------------------------------------------*/
static int
active(void)
{
return PIR_IRQ_ENABLED();
}
/*---------------------------------------------------------------------------*/
static unsigned int
value(int type)
{
return pir;
}
/*---------------------------------------------------------------------------*/
static int
configure(int type, void *c)
{
return 0;
}
/*---------------------------------------------------------------------------*/
static void *
status(int type)
{
return NULL;
}
/*---------------------------------------------------------------------------*/
SENSORS_SENSOR(pir_sensor, PIR_SENSOR,
init, irq, activate, deactivate, active,
value, configure, status);

View File

@ -0,0 +1,42 @@
/*
* Copyright (c) 2005, Swedish Institute of Computer Science
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* This file is part of the Contiki operating system.
*
* @(#)$Id: pir-sensor.h,v 1.1 2006/06/18 07:49:33 adamdunkels Exp $
*/
#ifndef __PIR_SENSOR_H__
#define __PIR_SENSOR_H__
#include "contiki-esb.h"
extern const struct sensors_sensor pir_sensor;
#define PIR_SENSOR "PIR"
#endif /* __PIR_SENSOR_H__ */

View File

@ -0,0 +1,98 @@
/*
* Copyright (c) 2005, Swedish Institute of Computer Science
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* This file is part of the Contiki operating system.
*
* @(#)$Id: radio-sensor.c,v 1.1 2006/06/18 07:49:33 adamdunkels Exp $
*/
#include "contiki-esb.h"
#include "dev/irq.h"
#include <io.h>
const struct sensors_sensor radio_sensor;
unsigned int radio_sensor_signal;
/*---------------------------------------------------------------------------*/
static void
init(void)
{
/* Initialization of ADC12 done by irq */
radio_sensor_signal = 0;
}
/*---------------------------------------------------------------------------*/
static int
irq(void)
{
radio_sensor_signal = ADC12MEM5;
/* sensors_changed(&radio_sensor);*/
return 0;
}
/*---------------------------------------------------------------------------*/
static void
activate(void)
{
irq_adc12_activate(&radio_sensor, 5, INCH_5 + SREF_0);
}
/*---------------------------------------------------------------------------*/
static void
deactivate(void)
{
irq_adc12_deactivate(&radio_sensor, 5);
radio_sensor_signal = 0;
}
/*---------------------------------------------------------------------------*/
static int
active(void)
{
return irq_adc12_active(5);
}
/*---------------------------------------------------------------------------*/
static unsigned int
value(int type)
{
return radio_sensor_signal;
}
/*---------------------------------------------------------------------------*/
static int
configure(int type, void *c)
{
return 0;
}
/*---------------------------------------------------------------------------*/
static void *
status(int type)
{
return NULL;
}
/*---------------------------------------------------------------------------*/
SENSORS_SENSOR(radio_sensor, RADIO_SENSOR,
init, irq, activate, deactivate, active,
value, configure, status);

View File

@ -0,0 +1,44 @@
/*
* Copyright (c) 2005, Swedish Institute of Computer Science
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* This file is part of the Contiki operating system.
*
* @(#)$Id: radio-sensor.h,v 1.1 2006/06/18 07:49:33 adamdunkels Exp $
*/
#ifndef __RADIO_SENSOR_H__
#define __RADIO_SENSOR_H__
#include "contiki-esb.h"
extern const struct sensors_sensor radio_sensor;
#define RADIO_SENSOR "Radio"
extern unsigned int radio_sensor_signal;
#endif /* __RADIO_SENSOR_H__ */

149
platform/esb/dev/rs232.c Normal file
View File

@ -0,0 +1,149 @@
/*
* Copyright (c) 2005, Swedish Institute of Computer Science
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* This file is part of the Contiki operating system.
*
* @(#)$Id: rs232.c,v 1.1 2006/06/18 07:49:33 adamdunkels Exp $
*/
/** \addtogroup esbrs232
* @{ */
/**
* \file
* RS232 communication device driver for the MSP430.
* \author Adam Dunkels <adam@sics.se>
*
* This file contains an RS232 device driver for the MSP430 microcontroller.
*
*/
#include <io.h>
#include <signal.h>
#include <string.h>
#include "contiki-esb.h"
static int (* input_handler)(unsigned char) = NULL;
/*---------------------------------------------------------------------------*/
interrupt(UART1RX_VECTOR)
rs232_rx_usart1(void)
{
/* Check status register for receive errors. - before reading RXBUF since
it clears the error and interrupt flags */
if(!(URCTL1 & RXERR) && input_handler != NULL) {
if(input_handler(RXBUF1)) {
LPM_AWAKE();
}
} else {
/* Else read out the char to clear the I-flags, etc. */
RXBUF1;
}
}
/*---------------------------------------------------------------------------*/
/**
* Initalize the RS232 port.
*
*/
void
rs232_init(void)
{
/* RS232 */
UCTL1 = CHAR; /* 8-bit character */
UTCTL1 = SSEL1; /* UCLK = MCLK */
rs232_set_speed(RS232_57600);
input_handler = NULL;
ME2 |= (UTXE1 | URXE1); /* Enable USART1 TXD/RXD */
IE2 |= URXIE1; /* Enable USART1 RX interrupt */
}
/*---------------------------------------------------------------------------*/
void
rs232_send(char c)
{
/* Loop until the transmission buffer is available. */
while ((IFG2 & UTXIFG1) == 0);
/* Transmit the data. */
TXBUF1 = c;
}
/*---------------------------------------------------------------------------*/
void
rs232_set_speed(unsigned char speed)
{
if(speed == RS232_19200) {
/* Set RS232 to 19200 */
UBR01 = 0x80; /* 2,457MHz/19200 = 128 -> 0x80 */
UBR11 = 0x00; /* */
UMCTL1 = 0x00; /* no modulation */
} else if(speed == RS232_38400) {
/* Set RS232 to 38400 */
UBR01 = 0x40; /* 2,457MHz/38400 = 64 -> 0x40 */
UBR11 = 0x00; /* */
UMCTL1 = 0x00; /* no modulation */
} else if(speed == RS232_57600) {
UBR01 = 0x2a; /* 2,457MHz/57600 = 42.7 -> 0x2A */
UBR11 = 0x00; /* */
UMCTL1 = 0x5b; /* */
} else if(speed == RS232_115200) {
UBR01 = 0x15; /* 2,457MHz/115200 = 21.4 -> 0x15 */
UBR11 = 0x00; /* */
UMCTL1 = 0x4a; /* */
} else {
rs232_set_speed(RS232_57600);
}
}
/*---------------------------------------------------------------------------*/
void
rs232_print(char *cptr)
{
while(*cptr != 0) {
rs232_send(*cptr);
++cptr;
}
}
/*---------------------------------------------------------------------------*/
void
rs232_set_input(int (*f)(unsigned char))
{
input_handler = f;
}
/*---------------------------------------------------------------------------*/
void
slip_arch_writeb(unsigned char c)
{
rs232_send(c);
}
/*---------------------------------------------------------------------------*/
/** @} */

119
platform/esb/dev/rs232.h Normal file
View File

@ -0,0 +1,119 @@
/*
* Copyright (c) 2005, Swedish Institute of Computer Science
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* This file is part of the Contiki operating system.
*
* @(#)$Id: rs232.h,v 1.1 2006/06/18 07:49:33 adamdunkels Exp $
*/
/** \addtogroup esb
* @{ */
/**
* \defgroup esbrs232 ESB RS232
*
* @{
*/
/**
* \file
* Header file for MSP430 RS232 driver.
* \author Adam Dunkels <adam@sics.se>
*
*/
#ifndef __RS232_H__
#define __RS232_H__
#define RS232_19200 1
#define RS232_38400 2
#define RS232_57600 3
#define RS232_115200 4
/**
* \brief Initialize the RS232 module
*
* This function is called from the boot up code to
* initalize the RS232 module.
*/
void rs232_init(void);
/**
* \brief Set an input handler for incoming RS232 data
* \param f A pointer to a byte input handler
*
* This function sets the input handler for incoming RS232
* data. The input handler function is called for every
* incoming data byte. The function is called from the
* RS232 interrupt handler, so care must be taken when
* implementing the input handler to avoid race
* conditions.
*
* The return value of the input handler affects the sleep
* mode of the CPU: if the input handler returns non-zero
* (true), the CPU is awakened to let other processing
* take place. If the input handler returns zero, the CPU
* is kept sleeping.
*/
void rs232_set_input(int (* f)(unsigned char));
/**
* \brief Configure the speed of the RS232 hardware
* \param speed The speed
*
* This function configures the speed of the RS232
* hardware. The allowed parameters are RS232_19200,
* RS232_38400, RS232_57600, and RS232_115200.
*/
void rs232_set_speed(unsigned char speed);
/**
* \brief Print a text string on RS232
* \param str A pointer to the string that is to be printed
*
* This function prints a string to RS232. The string must
* be terminated by a null byte. The RS232 module must be
* correctly initalized and configured for this function
* to work.
*/
void rs232_print(char *text);
/**
* \brief Print a character on RS232
* \param c The character to be printed
*
* This function prints a character to RS232. The RS232
* module must be correctly initalized and configured for
* this function to work.
*/
void rs232_send(char c);
#endif /* __RS232_H__ */
/** @} */
/** @} */

View File

@ -0,0 +1,91 @@
/*
* Copyright (c) 2005, Swedish Institute of Computer Science
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* This file is part of the Contiki operating system.
*
* @(#)$Id: sensor-template.c,v 1.1 2006/06/18 07:49:33 adamdunkels Exp $
*/
#include "contiki-esb.h"
#include "name_of.h"
const struct sensors_sensor name_of_sensor;
/*---------------------------------------------------------------------------*/
static void
init(void)
{
}
/*---------------------------------------------------------------------------*/
static int
irq(void)
{
return 0;
}
/*---------------------------------------------------------------------------*/
static void
activate(void)
{
}
/*---------------------------------------------------------------------------*/
static void
deactivate(void)
{
}
/*---------------------------------------------------------------------------*/
static int
active(void)
{
return 0;
}
/*---------------------------------------------------------------------------*/
static unsigned int
value(void)
{
return 0;
}
/*---------------------------------------------------------------------------*/
static int
configure(void *c)
{
return 0;
}
/*---------------------------------------------------------------------------*/
static void *
status(void)
{
return NULL;
}
/*---------------------------------------------------------------------------*/
SENSORS_SENSOR(name_of_sensor, "Sensor type",
init, irq, activate, deactivate, active,
value, configure, status);

View File

@ -0,0 +1,137 @@
/*
* Copyright (c) 2005, Swedish Institute of Computer Science
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* This file is part of the Contiki operating system.
*
* @(#)$Id: sound-sensor.c,v 1.1 2006/06/18 07:49:33 adamdunkels Exp $
*/
#include <stdlib.h>
#include "contiki-esb.h"
#include "dev/irq.h"
#define MIC_MIN_SENS 150
const struct sensors_sensor sound_sensor;
static unsigned int sound, micdiff, micmax, avgmax;
char sound_pause;
/*---------------------------------------------------------------------------*/
static void
init(void)
{
/* Initialization of ADC12 done by irq */
}
/*---------------------------------------------------------------------------*/
static int
irq(void)
{
if (!sound_pause) {
micdiff = micdiff + abs(ADC12MEM4 - sound) - (micdiff >> 3);
/* if (micdiff > MIC_MIN_SENS) { */
/* sensors_changed(&sound_sensor); */
/* } */
/* if (micdiff > (avgmax >> 2)) { */
/* if (micdiff % 10 == 0) beep_beep(10); */
/* // Subtract a little... */
/* micdiff = micdiff - (micdiff >> 4); */
/* } */
/* if (micmax < micdiff) { */
/* micmax = micdiff; */
/* } */
}
/* if (micdiff > 2000) { */
/* leds_on(LEDS_GREEN); */
/* } */
/* if (micdiff > 3000) { */
/* leds_on(LEDS_YELLOW); */
/* } */
/* if (micdiff > 4000) { */
/* leds_on(LEDS_RED); */
/* } */
sound = ADC12MEM4;
return 0;
}
/*---------------------------------------------------------------------------*/
static void
activate(void)
{
sound = micdiff = micmax = 0;
sound_pause = 0;
avgmax = 5000;
irq_adc12_activate(&sound_sensor, 4, INCH_0 + SREF_0);
}
/*---------------------------------------------------------------------------*/
static void
deactivate(void)
{
irq_adc12_deactivate(&sound_sensor, 4);
}
/*---------------------------------------------------------------------------*/
static int
active(void)
{
return irq_adc12_active(4);
}
/*---------------------------------------------------------------------------*/
static unsigned int
value(int type)
{
/* try returning the max to see what values we get... */
/* int mictmp = micmax; */
/* avgmax = avgmax + micmax - (avgmax >> 3); */
/* micmax = micdiff; */
/* return mictmp; */
return micdiff;
}
/*---------------------------------------------------------------------------*/
static int
configure(int type, void *c)
{
return 0;
}
/*---------------------------------------------------------------------------*/
static void *
status(int type)
{
return NULL;
}
/*---------------------------------------------------------------------------*/
SENSORS_SENSOR(sound_sensor, SOUND_SENSOR,
init, irq, activate, deactivate, active,
value, configure, status);

View File

@ -0,0 +1,42 @@
/*
* Copyright (c) 2005, Swedish Institute of Computer Science
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* This file is part of the Contiki operating system.
*
* @(#)$Id: sound-sensor.h,v 1.1 2006/06/18 07:49:33 adamdunkels Exp $
*/
#ifndef __SOUND_SENSOR_H__
#define __SOUND_SENSOR_H__
#include "contiki-esb.h"
extern const struct sensors_sensor sound_sensor;
#define SOUND_SENSOR "Sound"
#endif /* __SOUND_SENSOR_H__ */

View File

@ -0,0 +1,101 @@
/*
* Copyright (c) 2005, Swedish Institute of Computer Science.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: temperature-sensor.c,v 1.1 2006/06/18 07:49:33 adamdunkels Exp $
*
* -----------------------------------------------------------------
*
* Author : Adam Dunkels, Joakim Eriksson, Niclas Finne
* Created : 2005-11-01
* Updated : $Date: 2006/06/18 07:49:33 $
* $Revision: 1.1 $
*/
#include "dev/temperature-sensor.h"
#include "dev/ds1629.h"
const struct sensors_sensor temperature_sensor;
static unsigned char flags;
/*---------------------------------------------------------------------------*/
static void
init(void)
{
flags = 0;
ds1629_init();
}
/*---------------------------------------------------------------------------*/
static int
irq(void)
{
return 0;
}
/*---------------------------------------------------------------------------*/
static void
activate(void)
{
flags |= 1;
ds1629_start();
}
/*---------------------------------------------------------------------------*/
static void
deactivate(void)
{
flags &= ~1;
}
/*---------------------------------------------------------------------------*/
static int
active(void)
{
return (flags & 1);
}
/*---------------------------------------------------------------------------*/
static unsigned int
value(int type)
{
unsigned int temp;
signed int t = ds1629_temperature();
temp = ((t / 128) * 50);
return temp;
}
/*---------------------------------------------------------------------------*/
static int
configure(int type, void *c)
{
return 0;
}
/*---------------------------------------------------------------------------*/
static void *
status(int type)
{
return NULL;
}
/*---------------------------------------------------------------------------*/
SENSORS_SENSOR(temperature_sensor, TEMPERATURE_SENSOR,
init, irq, activate, deactivate, active,
value, configure, status);

View File

@ -0,0 +1,48 @@
/*
* Copyright (c) 2005, Swedish Institute of Computer Science.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: temperature-sensor.h,v 1.1 2006/06/18 07:49:33 adamdunkels Exp $
*
* -----------------------------------------------------------------
*
* Author : Adam Dunkels, Joakim Eriksson, Niclas Finne
* Created : 2005-11-01
* Updated : $Date: 2006/06/18 07:49:33 $
* $Revision: 1.1 $
*/
#ifndef __TEMPERATURE_SENSOR_H__
#define __TEMPERATURE_SENSOR_H__
#include "lib/sensors.h"
extern const struct sensors_sensor temperature_sensor;
#define TEMPERATURE_SENSOR "Temperature"
#endif /* __TEMPERATURE_SENSOR_H__ */

724
platform/esb/dev/tr1001.c Normal file
View File

@ -0,0 +1,724 @@
/*
* Copyright (c) 2005, Swedish Institute of Computer Science
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* This file is part of the Contiki operating system.
*
* @(#)$Id: tr1001.c,v 1.1 2006/06/18 07:49:33 adamdunkels Exp $
*/
/**
* \addtogroup esb
* @{
*/
/**
* \defgroup tr1001 TR1001 radio tranciever device driver
* @{
*/
/**
* \file
* Device driver and packet framing for the RFM-TR1001 radio module.
* \author Adam Dunkels <adam@sics.se>
*
* This file implements a device driver for the RFM-TR1001 radio
* tranciever.
*
*/
#include "contiki-esb.h"
#include "lib/me.h"
#include "lib/crc16.h"
#include "net/tr1001-drv.h"
#include <io.h>
#include <signal.h>
#include <string.h>
#define RXSTATE_READY 0
#define RXSTATE_RECEVING 1
#define RXSTATE_FULL 2
#define SYNCH1 0x3c
#define SYNCH2 0x03
#if TR1001_STATISTICS
static unsigned short sstrength_dropped,
sstrength_max, sstrength_min, tmp_sstrength_max, tmp_sstrength_min;
/* The number of dropped packets */
static unsigned short packets_err;
/* The number of accepted packets */
static unsigned short packets_ok;
#endif /* TR1001_STATISTICS */
/*
* The buffer which holds incoming data.
*/
#define RXBUFSIZE UIP_BUFSIZE
unsigned char tr1001_rxbuf[RXBUFSIZE];
/*
* The length of the packet that currently is being received.
*/
static unsigned short tr1001_rxlen = 0;
/*
* The reception state.
*/
volatile unsigned char tr1001_rxstate = RXSTATE_READY;
static u16_t rxcrc, rxcrctmp;
/*
* The structure of the packet header.
*/
struct tr1001_hdr {
u8_t len[2]; /**< The 16-bit length of the packet in network byte
order. */
};
/*
* The length of the packet header.
*/
#define TR1001_HDRLEN sizeof(struct tr1001_hdr)
#define BUF ((uip_tcpip_hdr *)&uip_buf[UIP_LLH_LEN])
#define OFF 0
#define ON 1
static u8_t onoroff = ON;
#define NUM_SYNCHBYTES 4
void tr1001_default_rxhandler(unsigned char c);
PT_THREAD(tr1001_default_rxhandler_pt(unsigned char c));
static struct pt rxhandler_pt;
/*
* This timer is used to keep track of when the last byte was received
* over the radio. If the inter-byte time is too large, the packet
* currently being received is discarded and a new packet reception is
* initiated.
*/
static struct timer rxtimer;
static unsigned short tmp_sstrength, sstrength;
#include <stdio.h>
#define LOG(...) /*printf(__VA_ARGS__)*/
/*---------------------------------------------------------------------------*/
#if TR1001_STATISTICS
#define PACKET_DROPPED(bytes) do { \
if(packets_err < ~0) {\
packets_err++;\
}\
sstrength_dropped = ((bytes) == 0 ? 0 : ((tmp_sstrength / (bytes)) << 1));\
} while(0)
#define PACKET_ACCEPTED() do {\
if(packets_ok < ~0) {\
packets_ok++;\
}\
} while(0);
#else
#define PACKET_DROPPED(bytes)
#define PACKET_ACCEPTED()
#endif /* TR1001_STATISTICS */
/*---------------------------------------------------------------------------*/
/*
* Turn on data transmission in On-Off-Keyed mode.
*/
static void
txook(void)
{
P3SEL = 0xf0;
P5OUT |= 0x40;
P5OUT &= 0x7f;
}
/*---------------------------------------------------------------------------*/
/*
* Turn on data reception for the radio tranceiver.
*/
static void
rxon(void)
{
P3SEL = 0xe0;
P5OUT |= 0xc0;
/* Enable the receiver. */
ME1 |= URXE0;
/* Turn off receive interrupt. */
IE1 |= URXIE0;
}
/*---------------------------------------------------------------------------*/
/*
* Turn on data reception for the radio tranceiver.
*/
static void
rxoff(void)
{
P5OUT &= 0x3f;
/* Disable the receiver. */
ME1 &= ~URXE0;
/* Turn off receive interrupt. */
IE1 &= ~URXIE0;
}
/*---------------------------------------------------------------------------*/
/*
* Clear the recevie buffer and reset the receiver state.
*/
static void
rxclear(void)
{
tr1001_rxstate = RXSTATE_READY;
}
/*---------------------------------------------------------------------------*/
/*
* Turn TR1001 radio transceiver off.
*/
/*---------------------------------------------------------------------------*/
void
radio_off(void)
{
onoroff = OFF;
rxoff();
rxclear();
}
/*---------------------------------------------------------------------------*/
/*
* Turn TR1001 radio transceiver on.
*/
/*---------------------------------------------------------------------------*/
void
radio_on(void)
{
onoroff = ON;
rxon();
rxclear();
}
/*---------------------------------------------------------------------------*/
/*
* Send a byte of data over the radio.
*
* \param b The byte to be sent.
*/
static void
send(unsigned char b)
{
clock_time_t start;
start = clock_time();
/* Wait until the USART0 TX buffer is ready. */
while((IFG1 & UTXIFG0) == 0) {
/* Wait no more than one second. */
if((clock_time_t)(clock_time() - start) > (clock_time_t)CLOCK_SECOND) {
break;
}
}
/* Send the byte. */
TXBUF0 = b;
}
/*---------------------------------------------------------------------------*/
/*
* Send a byte of data and its logical negation (all bits inverted)
* over the radio.
*
* \param b The byte to be sent.
*/
static void
send2(unsigned char b)
{
u16_t m;
m = me_encode(b);
send(m >> 8);
send(m & 0xff);
}
static u16_t
send2_crc16(unsigned char b, u16_t crcacc)
{
u16_t m;
m = me_encode(b);
send(m >> 8);
send(m & 0xff);
return crc16_add(b, crcacc);
}
/*---------------------------------------------------------------------------*/
void
tr1001_set_txpower(unsigned char p)
{
int i;
/* Clamp maximum power. */
if(p > 100) {
p = 100;
}
/* First, run the potentiometer down to zero so that we know the
start value of the potentiometer. */
P2OUT &= 0xDF; /* P25 = 0 (down selected) */
P2OUT &= 0xBF; /* P26 = 0 (chipselect on) */
for(i = 0; i < 102; ++i) {
P2OUT &= 0xEF; /* P24 = 0 (inc) */
P2OUT |= 0x10;
}
/* Now, start to increase the value of the potentiometer until it
reaches the desired value.*/
P2OUT |= 0x20; /* P25 = 1 (up selected) */
for(i = 0; i < p; ++i) {
P2OUT &= 0xEF; /* P24 = 0 (inc) */
P2OUT |= 0x10;
}
P2OUT |= 0x40; /* P26 = 1 (chipselect off) */
}
/*---------------------------------------------------------------------------*/
void
tr1001_init(void)
{
PT_INIT(&rxhandler_pt);
#if TR1001_STATISTICS
packets_ok = packets_err = 0;
sstrength_dropped = 0;
sstrength_min = 0xFFFF;
sstrength_max = 0;
#endif /* TR1001_STATISTICS */
UCTL0 = CHAR; /* 8-bit character */
UTCTL0 = SSEL1; /* UCLK = SMCLK */
tr1001_set_speed(TR1001_19200);
ME1 |= UTXE0 + URXE0; /* Enable USART0 TXD/RXD */
/* Turn on receive interrupt. */
IE1 |= URXIE0;
timer_set(&rxtimer, CLOCK_SECOND / 4);
radio_on();
tr1001_set_txpower(100);
/* Reset reception state. */
rxclear();
}
/*---------------------------------------------------------------------------*/
interrupt (UART0RX_VECTOR)
tr1001_rxhandler(void)
{
tr1001_default_rxhandler_pt(RXBUF0);
}
/*---------------------------------------------------------------------------*/
static void
dump_packet(int len)
{
int i;
for(i = 0; i < len; ++i) {
LOG("%d: 0x%02x\n", i, tr1001_rxbuf[i]);
}
}
/*---------------------------------------------------------------------------*/
PT_THREAD(tr1001_default_rxhandler_pt(unsigned char incoming_byte))
{
static unsigned char rxtmp, tmppos;
if(timer_expired(&rxtimer) && tr1001_rxstate != RXSTATE_FULL) {
PT_INIT(&rxhandler_pt);
}
timer_restart(&rxtimer);
if(tr1001_rxstate == RXSTATE_RECEVING) {
unsigned short signal = radio_sensor_signal;
tmp_sstrength += (signal >> 2);
if(signal < tmp_sstrength_min) {
tmp_sstrength_min = signal;
}
if(signal > tmp_sstrength_max) {
tmp_sstrength_max = signal;
}
}
PT_BEGIN(&rxhandler_pt);
while(1) {
/* Reset reception state. */
rxclear();
/* Wait until we receive the first syncronization byte. */
PT_WAIT_UNTIL(&rxhandler_pt, incoming_byte == SYNCH1);
tr1001_rxstate = RXSTATE_RECEVING;
/* Read all incoming syncronization bytes. */
PT_WAIT_WHILE(&rxhandler_pt, incoming_byte == SYNCH1);
/* We should receive the second synch byte by now, otherwise we'll
restart the protothread. */
if(incoming_byte != SYNCH2) {
PT_RESTART(&rxhandler_pt);
}
/* Start signal strength measurement */
tmp_sstrength = 0;
tmp_sstrength_max = 0;
tmp_sstrength_min = 0xFFFF;
/* Reset the CRC. */
rxcrc = 0xffff;
/* Read packet header. */
for(tmppos = 0; tmppos < TR1001_HDRLEN; ++tmppos) {
/* Wait for the first byte of the packet to arrive. */
PT_YIELD(&rxhandler_pt);
/* If the incoming byte isn't a valid Manchester encoded byte,
we start again from the beinning. */
if(!me_valid(incoming_byte)) {
beep_beep(1000);
LOG("Incorrect manchester in header at byte %d/1\n", tmppos);
PACKET_DROPPED(tmppos);
PT_RESTART(&rxhandler_pt);
}
rxtmp = me_decode8(incoming_byte);
/* Wait for the next byte to arrive. */
PT_YIELD(&rxhandler_pt);
if(!me_valid(incoming_byte)) {
beep_beep(1000);
LOG("Incorrect manchester in header at byte %d/2\n", tmppos);
PACKET_DROPPED(tmppos + 1);
PT_RESTART(&rxhandler_pt);
}
/* Put together the two bytes into a single Manchester decoded
byte. */
tr1001_rxbuf[tmppos] = (rxtmp << 4) | me_decode8(incoming_byte);
/* Calculate the CRC. */
rxcrc = crc16_add(tr1001_rxbuf[tmppos], rxcrc);
}
/* Since we've got the header, we can grab the length from it. */
tr1001_rxlen = ((((struct tr1001_hdr *)tr1001_rxbuf)->len[0] << 8) +
((struct tr1001_hdr *)tr1001_rxbuf)->len[1]);
/* If the length is longer than we can handle, we'll start from
the beginning. */
if(tmppos + tr1001_rxlen > sizeof(tr1001_rxbuf)) {
PACKET_DROPPED(tmppos);
PT_RESTART(&rxhandler_pt);
}
/* Read packet data. */
for(; tmppos < tr1001_rxlen + TR1001_HDRLEN; ++tmppos) {
PT_YIELD(&rxhandler_pt);
if(!me_valid(incoming_byte)) {
LOG("Incorrect manchester 0x%02x at byte %d/1\n", incoming_byte,
tmppos - TR1001_HDRLEN);
beep_beep(1000);
PACKET_DROPPED(tmppos);
PT_RESTART(&rxhandler_pt);
}
rxtmp = me_decode8(incoming_byte);
PT_YIELD(&rxhandler_pt);
if(!me_valid(incoming_byte)) {
LOG("Incorrect manchester at byte %d/2\n", tmppos - TR1001_HDRLEN);
beep_beep(1000);
PACKET_DROPPED(tmppos + 1);
PT_RESTART(&rxhandler_pt);
}
tr1001_rxbuf[tmppos] = (rxtmp << 4) | me_decode8(incoming_byte);
rxcrc = crc16_add(tr1001_rxbuf[tmppos], rxcrc);
}
/* Read the frame CRC. */
for(tmppos = 0; tmppos < 4; ++tmppos) {
PT_YIELD(&rxhandler_pt);
if(!me_valid(incoming_byte)) {
beep_beep(1000);
PACKET_DROPPED(tr1001_rxlen + TR1001_HDRLEN);
PT_RESTART(&rxhandler_pt);
}
rxcrctmp = (rxcrctmp << 4) | me_decode8(incoming_byte);
}
if(rxcrctmp == rxcrc) {
/* A full packet has been received and the CRC checks out. We'll
request the driver to take care of the incoming data. */
PACKET_ACCEPTED();
tr1001_drv_request_poll();
LPM_AWAKE();
/* We'll set the receive state flag to signal that a full frame
is present in the buffer, and we'll wait until the buffer has
been taken care of. */
tr1001_rxstate = RXSTATE_FULL;
PT_WAIT_UNTIL(&rxhandler_pt, tr1001_rxstate != RXSTATE_FULL);
} else {
LOG("Incorrect CRC");
beep_beep(1000);
PACKET_DROPPED(tr1001_rxlen + TR1001_HDRLEN);
}
}
PT_END(&rxhandler_pt);
}
/*---------------------------------------------------------------------------*/
/*
* Prepare a transmission.
*
* This function does the necessary setup before a packet can be sent
* out.
*/
static void
prepare_transmission(int synchbytes)
{
int i;
/* Delay the transmission for a short random duration. */
clock_delay(random_rand() & 0x3ff);
/* Check that we don't currently are receiveing a packet, and if so
we wait until the reception has been completed. Reception is done
with interrupts so it is OK for us to wait in a while() loop. */
while(tr1001_rxstate == RXSTATE_RECEVING &&
!timer_expired(&rxtimer)) {
/* Delay the transmission for a short random duration. */
clock_delay(random_rand() & 0x7ff);
}
/* Turn on OOK mode with transmission. */
txook();
/* According to the datasheet, the transmitter must wait for 12 us
in order to settle. Empirical tests show that is it better to
wait for something like 283 us... */
clock_delay(200);
/* Transmit preamble and synch bytes. */
for(i = 0; i < 20; ++i) {
send(0xaa);
}
/* send(0xaa);
send(0xaa);*/
send(0xff);
for(i = 0; i < synchbytes; ++i) {
send(SYNCH1);
}
send(SYNCH2);
}
/*---------------------------------------------------------------------------*/
u8_t
tr1001_send(u8_t *packet, u16_t len)
{
int i;
u16_t crc16;
LOG("tr1001_send: sending %d bytes\n", len);
/* Prepare the transmission. */
prepare_transmission(NUM_SYNCHBYTES);
crc16 = 0xffff;
/* Send packet header. */
crc16 = send2_crc16(len >> 8, crc16);
crc16 = send2_crc16(len & 0xff, crc16);
/* Send packet data. */
for(i = 0; i < len; ++i) {
crc16 = send2_crc16(packet[i], crc16);
}
/* Send CRC */
send2(crc16 >> 8);
send2(crc16 & 0xff);
/* Send trailing bytes. */
send(0x33);
send(0xcc);
send(0x33);
send(0xcc);
/* Turn on (or off) reception again. */
if(onoroff == ON) {
rxon();
rxclear();
} else {
rxoff();
rxclear();
}
return UIP_FW_OK;
}
/*---------------------------------------------------------------------------*/
unsigned short
tr1001_poll(void)
{
unsigned short tmplen;
if(tr1001_rxstate == RXSTATE_FULL) {
dump_packet(tr1001_rxlen + 2);
tmplen = tr1001_rxlen;
if(tmplen > UIP_BUFSIZE - (UIP_LLH_LEN - TR1001_HDRLEN)) {
tmplen = UIP_BUFSIZE - (UIP_LLH_LEN - TR1001_HDRLEN);
}
memcpy(&uip_buf[UIP_LLH_LEN], &tr1001_rxbuf[TR1001_HDRLEN],
tmplen);
/* header + content + CRC */
sstrength = (tmp_sstrength / (TR1001_HDRLEN + tr1001_rxlen + 2)) << 1;
sstrength_max = tmp_sstrength_max;
sstrength_min = tmp_sstrength_min;
rxclear();
LOG("tr1001_poll: got %d bytes\n", tmplen);
return tmplen;
}
return 0;
}
/*---------------------------------------------------------------------------*/
void
tr1001_set_speed(unsigned char speed)
{
if(speed == TR1001_19200) {
/* Set TR1001 to 19200 */
UBR00 = 0x80; /* 2,457MHz/19200 = 128 -> 0x80 */
UBR10 = 0x00; /* */
UMCTL0 = 0x00; /* no modulation */
} else if(speed == TR1001_38400) {
/* Set TR1001 to 38400 */
UBR00 = 0x40; /* 2,457MHz/38400 = 64 -> 0x40 */
UBR10 = 0x00; /* */
UMCTL0 = 0x00; /* no modulation */
} else if(speed == TR1001_57600) {
UBR00 = 0x2a; /* 2,457MHz/57600 = 42.7 -> 0x2A */
UBR10 = 0x00; /* */
UMCTL0 = 0x5b; /* */
} else if(speed == TR1001_115200) {
UBR00 = 0x15; /* 2,457MHz/115200 = 21.4 -> 0x15 */
UBR10 = 0x00; /* */
UMCTL0 = 0x4a; /* */
} else {
tr1001_set_speed(TR1001_19200);
}
}
/*---------------------------------------------------------------------------*/
unsigned short
tr1001_sstrength(void)
{
return sstrength;
}
/*---------------------------------------------------------------------------*/
#if TR1001_STATISTICS
unsigned short
tr1001_packets_ok(void)
{
return packets_ok;
}
#endif /* TR1001_STATISTICS */
/*---------------------------------------------------------------------------*/
#if TR1001_STATISTICS
unsigned short
tr1001_packets_dropped(void)
{
return packets_err;
}
#endif /* TR1001_STATISTICS */
/*---------------------------------------------------------------------------*/
#if TR1001_STATISTICS
void
tr1001_clear_packets(void)
{
packets_ok = packets_err = 0;
}
#endif /* TR1001_STATISTICS */
/*---------------------------------------------------------------------------*/
#if TR1001_STATISTICS
unsigned short
tr1001_sstrength_value(unsigned int type)
{
switch(type) {
case TR1001_SSTRENGTH_DROPPED:
return sstrength_dropped;
case TR1001_SSTRENGTH_MAX:
return sstrength_max;
case TR1001_SSTRENGTH_MIN:
return sstrength_min < sstrength_max ? sstrength_min : 0;
default:
return 0;
}
}
#endif /* TR1001_STATISTICS */
/*---------------------------------------------------------------------------*/
/** @} */
/** @} */

154
platform/esb/dev/tr1001.h Normal file
View File

@ -0,0 +1,154 @@
/*
* Copyright (c) 2005, Swedish Institute of Computer Science
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* This file is part of the Contiki operating system.
*
* @(#)$Id: tr1001.h,v 1.1 2006/06/18 07:49:33 adamdunkels Exp $
*/
#ifndef __TR1001_H__
#define __TR1001_H__
#include "contiki-net.h"
#include "dev/radio.h"
#include "contiki-conf.h"
/**
* Initialize the radio tranciever.
*
* Turns on reception of bytes and installs the receive interrupt
* handler.
*/
void tr1001_init(void);
/**
* Set the speed of the TR1001 radio device.
*
* This function sets the speed of the TR1001 radio tranciever. Both
* the sender and the receiver must have the same speed for
* communication to work.
*
* \param speed The speed of the TR1001 radio: TR1001_19200,
* TR1001_38400, TR1001_57600 or TR1001_115200.
*
*/
void tr1001_set_speed(unsigned char s);
#define TR1001_19200 1
#define TR1001_38400 2
#define TR1001_57600 3
#define TR1001_115200 4
/**
* Set the transmission power of the tranciever.
*
* The sensor board is equipped with a DS1804 100 position trimmer
* potentiometer which is used to set the transmission input current
* to the radio tranciever chip, thus setting the transmission power
* of the radio tranciever.
*
* This function sets the trimmer potentiometer to a value between 1
* and 100.
*
* \param p The power of the tranciever, between 1 (lowest) and 100
* (highest).
*/
void tr1001_set_txpower(unsigned char p);
/**
* \brief The highest transmission power
*/
#define TR1001_TXPOWER_HIGHEST 100
/**
* \brief The lowest transmission power
*/
#define TR1001_TXPOWER_LOWEST 1
void tr1001_set_sendtype(unsigned char t);
#define TR1001_SENDTYPE_NORMAL 0
#define TR1001_SENDTYPE_ACKED 1
void tr1001_set_numrexmit(unsigned char acks);
#define TR1001_NUMREXMIT_DEFAULT 2
/**
* Send a packet from the uip_buf buffer.
*
* This function causes a packet to be sent out after a small random
* delay, but without doing any MAC layer collision detection or
* back-offs. The packet is sent with a 4 byte header that contains a
* a "type" identifier, an 8-bit packet ID field and the length of the
* packet in network byte order.
*
* This function should normally not be called from user
* programs. Rather, the uIP TCP/IP stack should be used.
*/
u8_t tr1001_send(u8_t *packet, u16_t len);
/**
* Check if an incoming packet has been received.
*
* This function checks the receive buffer to see if an entire packet
* has been received. The actual reception is handled by an interrupt
* handler.
*
* \return The length of the received packet, or 0 if no packet has
* been received.
*/
unsigned short tr1001_poll(void);
extern unsigned char tr1001_rxbuf[];
extern volatile unsigned char tr1001_rxstate;
/**
* Calculate the signal strength of a received packet.
*
* This function calculates the recevied signal strength of the last
* received packet. This function typically is called when a packet
* has been received.
*/
unsigned short tr1001_sstrength(void);
#ifdef TR1001_CONF_STATISTICS
#define TR1001_STATISTICS TR1001_CONF_STATISTICS
#else
#define TR1001_STATISTICS 1
#endif
#if TR1001_STATISTICS
#define TR1001_SSTRENGTH_DROPPED 1
#define TR1001_SSTRENGTH_MAX 2
#define TR1001_SSTRENGTH_MIN 3
unsigned short tr1001_packets_ok(void);
unsigned short tr1001_packets_dropped(void);
void tr1001_clear_packets(void);
unsigned short tr1001_sstrength_value(unsigned int type);
#endif /* TR1001_STATISTICS */
#endif /* __TR1001_H__ */

View File

@ -0,0 +1,103 @@
/*
* Copyright (c) 2005, Swedish Institute of Computer Science
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* This file is part of the Contiki operating system.
*
* @(#)$Id: vib-sensor.c,v 1.1 2006/06/18 07:49:33 adamdunkels Exp $
*/
#include "contiki-esb.h"
const struct sensors_sensor vib_sensor;
static unsigned int vib;
HWCONF_PIN(VIB, 1, 4);
HWCONF_IRQ(VIB, 1, 4);
/*---------------------------------------------------------------------------*/
static int
irq(void)
{
if(VIB_CHECK_IRQ()) {
++vib;
sensors_changed(&vib_sensor);
return 1;
}
return 0;
}
/*---------------------------------------------------------------------------*/
static void
init(void)
{
vib = 0;
VIB_SELECT();
VIB_MAKE_INPUT();
}
/*---------------------------------------------------------------------------*/
static void
activate(void)
{
sensors_add_irq(&vib_sensor, VIB_IRQ_PORT());
VIB_ENABLE_IRQ();
}
/*---------------------------------------------------------------------------*/
static void
deactivate(void)
{
VIB_DISABLE_IRQ();
sensors_remove_irq(&vib_sensor, VIB_IRQ_PORT());
}
/*---------------------------------------------------------------------------*/
static int
active(void)
{
return VIB_IRQ_ENABLED();
}
/*---------------------------------------------------------------------------*/
static unsigned int
value(int type)
{
return vib;
}
/*---------------------------------------------------------------------------*/
static int
configure(int type, void *c)
{
return 0;
}
/*---------------------------------------------------------------------------*/
static void *
status(int type)
{
return NULL;
}
/*---------------------------------------------------------------------------*/
SENSORS_SENSOR(vib_sensor, VIB_SENSOR,
init, irq, activate, deactivate, active,
value, configure, status);

View File

@ -0,0 +1,42 @@
/*
* Copyright (c) 2005, Swedish Institute of Computer Science
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* This file is part of the Contiki operating system.
*
* @(#)$Id: vib-sensor.h,v 1.1 2006/06/18 07:49:33 adamdunkels Exp $
*/
#ifndef __VIB_SENSOR_H__
#define __VIB_SENSOR_H__
#include "contiki-esb.h"
extern const struct sensors_sensor vib_sensor;
#define VIB_SENSOR "Vibration"
#endif /* __VIB_SENSOR_H__ */

View File

@ -0,0 +1,54 @@
/*
* Copyright (c) 2005, Swedish Institute of Computer Science
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* This file is part of the Contiki operating system.
*
* @(#)$Id: watchdog.c,v 1.1 2006/06/18 07:49:33 adamdunkels Exp $
*/
#include <io.h>
#include "dev/watchdog.h"
/*------------------------------------------------------------------------------*/
void
watchdog_init(void)
{
WDTCTL = WDT_ARST_1000 + WDTNMI + WDTNMIES;
}
/*------------------------------------------------------------------------------*/
void
watchdog_restart(void)
{
WDTCTL = WDT_ARST_1000 + WDTNMI + WDTNMIES;
}
/*------------------------------------------------------------------------------*/
void
watchdog_stop(void)
{
WDTCTL = WDTPW + WDTHOLD + WDTNMI + WDTNMIES;
}
/*------------------------------------------------------------------------------*/

102
platform/esb/net/dtc-conf.h Normal file
View File

@ -0,0 +1,102 @@
/*
* Copyright (c) 2005, Swedish Institute of Computer Science
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* This file is part of the Contiki operating system.
*
* @(#)$Id: dtc-conf.h,v 1.1 2006/06/18 07:49:33 adamdunkels Exp $
*/
/**
* \file
* Configuration options for the distributed TCP caching module.
* \author Adam Dunkels <adam@sics.se>
*/
#ifndef __DTC_CONF_H__
#define __DTC_CONF_H__
#include "uipopt.h"
/**
* The maximum size of a TCP segment (excluding headers) that can be
* cached by the distributed TCP caching module.
*
* The distributed TCP caching module statically allocates memory for
* holding cached TCP segments. This configuration parameter sets the
* size of those blocks. The DTC_CONF_NUM_SEGMENTS configuration
* parameter sets the number of packet caches that are allocated.
*
* \hideinitializer
*/
#define DTC_CONF_MAX_SEGSIZE UIP_TCP_MSS
/**
* The number of TCP segments that the distributed TCP caching module can hold.
*
* This configuration parameter affects how much memory is allocated
* to the TCP segment cache. The size of each cache block is set with
* the DTC_CONF_MAX_SEGSIZE parameter.
*
* \hideinitializer
*/
#define DTC_CONF_NUM_SEGMENTS 1
/**
* The number of simulataneous TCP connections that the distributed TCP caching can handle simulataneously.
*
* This configuration parameter specifies the maximum amount of
* simultaneous TCP connections that can be handled by the distributed
* TCP caching module. Note that this parameter does not limit the
* amount of TCP connections that can go through the node, only how
* many connections that will benefit from the help of the distributed
* TCP caching mechanism.
*
* \hideinitializer
*/
#define DTC_CONF_NUM_CONNECTIONS 1
/**
* Type definition of the native unsigned 32-bit datatype.
*
* This configuration option defines the native 32-bit datatype for
* the platform on which the distributed TCP caching mechanism is to
* be run. Normally, the unsigned 32-bit datatype is "unsigned long",
* but check the manual for the C compiler to be sure.
*/
typedef unsigned long u32_t;
/**
* Type definition of the native signed 32-bit datatype.
*
* This configuration option defines the native 32-bit datatype for
* the platform on which the distributed TCP caching mechanism is to
* be run. Normally, the signed 32-bit datatype is "signed long", but
* check the manual for the C compiler to be sure.
*/
typedef signed long s32_t;
#endif /* __DTC_CONF_H__ */

View File

@ -0,0 +1,80 @@
/*
* Copyright (c) 2005, Swedish Institute of Computer Science
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* This file is part of the Contiki operating system.
*
* @(#)$Id: dtc-nullservice.c,v 1.1 2006/06/18 07:49:34 adamdunkels Exp $
*/
#include "contiki.h"
#include "dtc-service.h"
#include "uip-fw.h"
static u8_t input(void) {return UIP_FW_OK;}
static u8_t output(void) {return UIP_FW_OK;}
static const struct dtc_service_interface state =
{
DTC_SERVICE_VERSION,
input,
output
};
EK_EVENTHANDLER(eventhandler, ev, data);
/*EK_POLLHANDLER(pollhandler);*/
EK_PROCESS(p, DTC_SERVICE_NAME ": null", EK_PRIO_NORMAL,
eventhandler, NULL/*pollhandler*/, (void *)&state);
/*---------------------------------------------------------------------------*/
EK_PROCESS_INIT(dtc_service_init, arg)
{
ek_service_start(DTC_SERVICE_NAME, &p);
}
/*---------------------------------------------------------------------------*/
EK_EVENTHANDLER(eventhandler, ev, data)
{
switch(ev) {
case EK_EVENT_INIT:
case EK_EVENT_REPLACE:
break;
case EK_EVENT_REQUEST_REPLACE:
ek_replace((struct ek_proc *)data, NULL);
break;
case EK_EVENT_REQUEST_EXIT:
ek_exit();
break;
default:
break;
}
}
/*---------------------------------------------------------------------------*/
/*EK_POLLHANDLER(pollhandler)
{
}*/
/*---------------------------------------------------------------------------*/

View File

@ -0,0 +1,45 @@
/*
* Copyright (c) 2005, Swedish Institute of Computer Science
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* This file is part of the Contiki operating system.
*
* @(#)$Id: dtc-service.h,v 1.1 2006/06/18 07:49:34 adamdunkels Exp $
*/
#ifndef __DTC_SERVICE_H__
#define __DTC_SERVICE_H__
#include "contiki.h"
#define dtc_service_name "DTC"
SERVICE_INTERFACE(dtc_service, {
u8_t (* input)(u8_t *p, u16_t len); /* Called when data arrives. */
u8_t (* output)(void); /* Called to send data. */
});
#endif /* __DTC_SERVICE_H__ */

612
platform/esb/net/dtc.c Normal file
View File

@ -0,0 +1,612 @@
/*
* Copyright (c) 2005, Swedish Institute of Computer Science
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* This file is part of the Contiki operating system.
*
* @(#)$Id: dtc.c,v 1.1 2006/06/18 07:49:34 adamdunkels Exp $
*/
/**
* \defgroup dtc Distributed TCP caching
*
* Distributed TCP Caching (DTC) is a mechanism that helps to improve
* the energy efficiency of TCP in wireless sensor networks.
*
* @{
*/
/**
* \file
* Implementation of the distributed TCP caching mechanism for uIP.
* \author Adam Dunkels <adam@sics.se>
*
*/
#include "contiki.h"
#include "dtc-service.h"
#include "uip-fw.h"
#include "dtc.h"
#include "dtc-conf.h"
#include "uip.h"
#include "uip-fw.h"
#include "uip_arch.h"
#include "list.h"
#include "contiki.h"
/*------------------------------------------------------------------------------*/
/**
* \internal
* The TCP and IP headers.
*/
struct tcpip_hdr {
/* IP header. */
u8_t vhl,
tos;
u16_t len;
u8_t ipid[2],
ipoffset[2],
ttl,
proto;
u16_t ipchksum;
u32_t srcipaddr,
destipaddr;
/* TCP header. */
u16_t srcport,
destport;
u32_t seqno, ackno;
u8_t tcpoffset,
flags;
u16_t wnd;
u16_t tcpchksum;
u8_t urgp[2];
u8_t data[4];
};
/**
* \internal
* Structure for keeping the state of one TCP end-point.
*/
struct dtc_tcpend {
u32_t ipaddr; /**< The IP address of the end-point. */
u32_t seqno; /**< The next expected TCP sequence number from the
end-point. */
u32_t rttseq; /**< The sequence number for which an RTT
measurement is currently being made, or 0 if no
measurement is being made. */
u16_t port; /**< The TCP port number of the end-point. */
u16_t window; /**< The receiver window for the end-point. */
int sa; /**< The smoothed avergage for the RTT
estimations. */
int sv; /**< The smoothed variance for the RTT
estimations. */
u8_t rto; /**< The RTO generated from the RTT estimations. */
u8_t tmr; /**< Timer. */
u8_t nrtx; /**< Number of retransmissions performed by the
distributed TCP snoop module. */
};
/**
* \internal
* The structure holding the state of a TCP connection.
*/
struct dtc_conn {
struct dtc_conn *next;
list_t segments;
struct dtc_tcpend e1; /**< First TCP end-point. */
struct dtc_tcpend e2; /**< Second TCP end-point. */
u8_t tmr; /**< Timer. */
};
/**
* \internal
* The structure holding a cached TCP segment.
*/
struct dtc_segment {
struct dtc_segment *next;
struct tcpip_hdr hdr; /**< The TCP and IP headers of the
packet. */
struct dtc_conn *conn; /**< Pointer to the connection that the
segment belongs to. */
u16_t len; /**< The length of the segment,
measured in TCP bytes. */
u8_t data[DTC_CONF_MAX_SEGSIZE];
/**< The data contained in the
segment. */
u8_t tmr; /**< Timer - starts at 0 and counts
upward every time
dtc_periodic() is invoked. */
};
/**
* \internal
* The list of cached TCP segments.
*/
MEMB(segments, sizeof(struct dtc_segment), DTC_CONF_NUM_SEGMENTS);
/*static struct dtc_segment segments[DTC_CONF_NUM_SEGMENTS];*/
/**
* \internal
* The list of TCP connections for which the distributed TCP snoop
* mechanism is active.
*/
MEMB(conns, sizeof(struct dtc_conn), DTC_CONF_NUM_CONNECTIONS);
/*static struct dtc_conn conns[DTC_CONF_NUM_CONNECTIONS];*/
LIST(connlist);
#define TCP_SEQ_LT(a,b) ((s32_t)((a)-(b)) < 0)
#define TCP_SEQ_LEQ(a,b) ((s32_t)((a)-(b)) <= 0)
#define TCP_SEQ_GT(a,b) ((s32_t)((a)-(b)) > 0)
#define TCP_SEQ_GEQ(a,b) ((s32_t)((a)-(b)) >= 0)
#define TCP_FIN 0x01
#define TCP_SYN 0x02
#define TCP_RST 0x04
#define TCP_PSH 0x08
#define TCP_ACK 0x10
#define TCP_URG 0x20
#define TCP_CTL 0x3f
/**
* \internal
* The length of the standard IP header, without IP options.
*/
#define IP_HLEN 20
/**
* \internal
* The start value of the retransmission time-out.
*/
#define RTO 30
static u32_t htonl(u32_t n);
static u8_t input(u8_t *packetptr, u16_t len);
static u8_t output(void) {return UIP_FW_OK;}
SERVICE(dtc_service, dtc_service, { input, output });
PROCESS(dtc_process, "Distributed TCP Caching");
/*------------------------------------------------------------------------------*/
/**
* Initialize the distributed snoop module.
*
* This function initializes the distributed snoop module and must be
* called before any of the other functions are used.
*/
/*------------------------------------------------------------------------------*/
static void
init_dtc(void)
{
memb_init(&conns);
memb_init(&segments);
list_init(connlist);
/* list_init(segmentlist);*/
}
/*------------------------------------------------------------------------------*/
/**
* \internal
* Save a segment in the segment cache.
*
* \param hdr Pointer to the header of the segment.
* \param len The length of the segment.
*
* \return A pointer to the cached segment, or NULL if the cache was full.
*
*/
/*------------------------------------------------------------------------------*/
static struct dtc_segment *
cache_segment(struct dtc_conn *conn,
struct tcpip_hdr *hdr, u16_t len)
{
struct dtc_segment *s;
/* Check if there are any unused slots in the segment cache. */
s = (struct dtc_segment *)memb_alloc(&segments);
if(s == NULL) {
/* Here we might try to make room in the segment cache, but for
now we simply return. */
return NULL;
}
/* Do some sanity checks on the segment to be cached. */
if(len < 40 ||
len - 40 > DTC_CONF_MAX_SEGSIZE) {
return NULL;
}
/* Calculate the length of the segment in TCP bytes. We do this by
taking the length of the IP packet, and subtract the size of the
IP and TCP headers. Since we only allow IP packets without
options, we know that the IP header is 20 bytes large, and we use
the header offset field of the TCP header to calculate the length
of the TCP header + options.
Finally, if the segment has the SYN or FIN flags set, we add one
to the segment length. */
s->len = htons(hdr->len) - IP_HLEN - ((hdr->tcpoffset >> 4) << 2);
if((hdr->flags & (TCP_SYN | TCP_FIN)) != 0) {
++s->len;
}
/* If there is no TCP data in the segment, there is no need to cache
it. */
if(s->len == 0) {
return NULL;
}
s->tmr = 0;
/* Copy the TCP/IP headers. */
memcpy(&s->hdr, hdr, 40);
/* Copy the segment data. */
memcpy(&s->data, &hdr->data[0], len - 40);
/* Add segment to list of segments on the connection's list. */
list_add(conn->segments, s);
return s;
}
/*------------------------------------------------------------------------------*/
/**
* \internal
* Process an incoming TCP segment for a connection.
*/
/*------------------------------------------------------------------------------*/
static u8_t
process_segment(struct dtc_conn *conn,
struct dtc_tcpend *src,
struct dtc_tcpend *dst,
u8_t *packetptr, u16_t len)
{
struct tcpip_hdr *hdr;
struct dtc_segment *s;
int m;
hdr = (struct tcpip_hdr *)packetptr;
/* Check if the incoming segment acknowledges one in the segment
list of the connection. */
for(s = list_head(conn->segments); s != NULL; s = s->next) {
printf("process_segment: for this conn, ack %d, ack %lu, seq %lu\n",
(hdr->flags & TCP_ACK), htonl(hdr->ackno),
htonl(s->hdr.seqno) + s->len);
/* Check if the incoming segment acknowledges this one. If so,
we should drop the cached segment and possibly do RTT
measurements. */
if((s->hdr.srcipaddr == hdr->destipaddr) &&
(hdr->flags & TCP_ACK) != 0 &&
TCP_SEQ_GEQ(htonl(hdr->ackno), htonl(s->hdr.seqno) + s->len)) {
/* Do RTT estimation, unless we have done retransmissions. */
if(src->rttseq != 0 &&
TCP_SEQ_GEQ(htonl(hdr->ackno), src->rttseq) &&
src->nrtx == 0) {
printf("Performing RTT estimation for seqno %lu (timer %d)\n",
src->rttseq, src->tmr);
m = src->tmr;
/* This is taken directly from VJs original code in his paper */
m = m - (src->sa >> 3);
src->sa += m;
if(m < 0) {
m = -m;
}
m = m - (src->sv >> 2);
src->sv += m;
src->rto = (src->sa >> 3) + src->sv;
src->rttseq = 0;
printf("Performing RTO stuff, new rto %d\n", src->rto);
}
src->tmr = 0;
printf("process_segment: bropping acked segment.\n");
/* s->state = STATE_FREE;*/
list_remove(conn->segments, s);
memb_free(&segments, s);
/* Next, check if the incoming packet has the same sequence
number as one of the segments in the cache. If so, it is
either a retransmission or a duplicated packet. In any
case, the RTT estimation would be thwarted if we made
estimations, so we cancel any running RTT measurements. */
} else if(s->hdr.seqno == hdr->seqno) {
if(s->hdr.srcipaddr == hdr->srcipaddr) {
src->rttseq = 0;
} else {
dst->rttseq = 0;
}
}
}
s = cache_segment(conn, hdr, len);
if(s != NULL) {
s->conn = conn;
if(dst->rttseq == 0) {
dst->rttseq = htonl(hdr->seqno);
dst->tmr = 0;
printf("Starting RTT est for %lu\n", dst->rttseq);
}
printf("Process_segment: segment cached\n");
} else {
printf("Process_segment: segment not cached \n");
}
return uip_fw_forward();
}
/*------------------------------------------------------------------------------*/
/**
* Process an incoming packet by running it through the distributed
* TCP snooping mechanism, and possibly by forwarding it.
*
* This function processes an incoming packet, and should be called
* from the network device driver for every packet that arrives. The
* function might call uip_fw_output() to send out packets.
*
* \param packetptr A pointer to the first byte of the packet header
* of the incoming packet. The packet must follow contiguously the
* header in memory.
*
* \param len The length of the packet, including the packet header.
*
* \retval UIP_FW_OK If the packet should be further processed by the
* caller.
*
* \retval UIP_FW_FORWARDED If the packet was forwarded and therefor
* should not be further processed.
*/
/*------------------------------------------------------------------------------*/
static u8_t
input(u8_t *packetptr, u16_t len)
{
struct tcpip_hdr *hdr;
struct dtc_conn *c;
hdr = (struct tcpip_hdr *)packetptr;
/* XXX: when testing, only run dtc for connections on port 12347. */
if(hdr->proto != UIP_PROTO_TCP ||
(hdr->destport != HTONS(12347) && hdr->srcport != HTONS(12347))) {
return uip_fw_forward();
}
printf("dtc_input (%d, %d): vhl 0x%x proto %d len %d ipchk 0x%x\n",
node_x(), node_y(),
hdr->vhl, hdr->proto, HTONS(hdr->len),
uip_ipchksum());
/* First, do some initial checking on the packet to make sure that
it is an undamaged TCP packet. Note that the checks are placed in
an order with the most computationally expensive at the end. */
if(hdr->vhl == 0x45 && /* IP version and header length must be normal. */
/* It must be a TCP packet. */
hdr->proto == UIP_PROTO_TCP &&
/* We do not allow IP fragments. */
hdr->ipoffset[1] == 0 &&
(hdr->ipoffset[0] & 0x3f) == 0 &&
/* We do not process segments destined for ourselves. */
hdr->destipaddr != *(u32_t *)uip_hostaddr &&
/* The packet length must not exceed the size of the segment
cache. */
(DTC_CONF_MAX_SEGSIZE + 40) >= HTONS(hdr->len) &&
/* The IP checksum must be OK. */
uip_ipchksum() == 0xffff &&
/* The TCP checksum must be OK. */
uip_tcpchksum() == 0xffff) {
/* Check if this segment belongs to a connection for which DTC
currently is active. Also keep track of the first unused
connection, in case we need to allocated it further down. */
for(c = list_head(connlist); c != NULL; c = c->next) {
if(c->e1.ipaddr == hdr->destipaddr &&
c->e1.port == htons(hdr->destport) &&
c->e2.ipaddr == hdr->srcipaddr &&
c->e2.port == htons(hdr->srcport)) {
printf("dtc_input %d, %d: process segment 1\n",
node_x(), node_y());
return process_segment(c, &c->e2, &c->e1,
packetptr, len);
} else if(c->e2.ipaddr == hdr->destipaddr &&
c->e2.port == htons(hdr->destport) &&
c->e1.ipaddr == hdr->srcipaddr &&
c->e1.port == htons(hdr->srcport)) {
printf("dtc_input %d, %d: process segment 2\n", node_x(), node_y());
return process_segment(c, &c->e1, &c->e2,
packetptr, len);
/* } else {
printf("dtc_input %d, %d: not processing segment\n",
node_x(), node_y());
return UIP_FW_LOCAL;*/
}
}
/* The incoming segment did not belong to any active connections,
so we allocate a new connection if there is a free one that we
can use. */
printf("dtc_input %d, %d: creating new connection\n", node_x(), node_y());
c = (struct dtc_conn *)memb_alloc(&conns);
list_add(connlist, c);
/* Fill in the fields from the incoming header. */
c->e1.ipaddr = hdr->srcipaddr;
c->e1.seqno = htonl(hdr->seqno);
c->e1.rttseq = 0;
c->e1.port = htons(hdr->srcport);
c->e1.window = htons(hdr->wnd);
c->e1.sa = 16;
c->e1.sv = 0;
c->e1.rto = RTO;
c->e1.tmr = 0;
c->e1.nrtx = 0;
c->e2.ipaddr = hdr->destipaddr;
c->e2.seqno = htonl(hdr->ackno);
c->e2.rttseq = 0;
c->e2.port = htons(hdr->destport);
c->e2.window = 0;
c->e2.sa = 16;
c->e2.sv = 0;
c->e2.rto = RTO;
c->e2.tmr = 0;
c->e2.nrtx = 0;
return process_segment(c, &c->e1, &c->e2, packetptr, len);
}
return uip_fw_forward();
}
/*------------------------------------------------------------------------------*/
/**
* \internal
* Perform periodic processing for a specific connection end-point.
*
* The periodic processing does two things: update timers and
* retransmit segments, where the timers drive the
* retransmissions. This must be done on a per-TCP-endpoint basis,
* rather than on a per-TCP-connection basis, because we maintain an
* RTT estimate for each endpoint. The reason for not having a single
* RTT for each TCP connection is that this node may be located on a
* path with a very long delay to one end and a very short delay to
* the other.
*/
/*------------------------------------------------------------------------------*/
static void
periodic_endpoint(struct dtc_tcpend *c)
{
++c->tmr;
/* If the timer has reached its threshold value, we should
retransmit the segment with the lowest sequence number, if
cached. */
if(c->tmr == c->rto) {
printf("periodic_endpoint: should do retransmission.\n");
c->tmr = 0;
}
}
/*------------------------------------------------------------------------------*/
/**
* Perform periodic processing.
*
* This function does the periodic processing in the distributed snoop
* module. It should be called as often as the uIP periodic function
* is called, typically twice a second.
*
*/
/*------------------------------------------------------------------------------*/
static void
periodic(void)
{
struct dtc_segment *s;
struct dtc_conn *c;
/* for(i = 0; i < DTC_CONF_NUM_CONNECTIONS; ++i) {
c = &conns[i];
if(c->state == STATE_USED) {
periodic_endpoint(&c->e1);
periodic_endpoint(&c->e2);
}
}
for(i = 0; i < DTC_CONF_NUM_SEGMENTS; ++i) {
s = &segments[i];
if(s->state == STATE_USED) {
++s->tmr;
}
}*/
}
/*------------------------------------------------------------------------------*/
/**
* \internal
* Convert a 32-bit unsigned value from host to network byte order.
*
* \param n The 32-bit value to be converted.
*
* \return The converted 32-bit value.
*
*/
/*------------------------------------------------------------------------------*/
static u32_t
htonl(u32_t n)
{
#if BYTE_ORDER == BIG_ENDIAN
return n;
#else /* BYTE_ORDER == BIG_ENDIAN */
return (((u32_t)n & 0xff) << 24) |
(((u32_t)n & 0xff00) << 8) |
(((u32_t)n & 0xff0000) >> 8) |
(((u32_t)n & 0xff000000) >> 24);
#endif /* BYTE_ORDER == BIG_ENDIAN */
}
/*------------------------------------------------------------------------------*/
PROCESS_THREAD(dtc_process, ev, data)
{
static struct etimer et;
PROCESS_BEGIN();
init_dtc();
SERVICE_REGISTER(dtc_service);
while(1) {
etimer_set(&et, CLOCK_SECOND / 2);
PROCESS_YIELD_UNTIL(etimer_expired(&et));
periodic();
}
PROCESS_END();
}
/*---------------------------------------------------------------------------*/
/** @} */

61
platform/esb/net/dtc.h Normal file
View File

@ -0,0 +1,61 @@
/*
* Copyright (c) 2005, Swedish Institute of Computer Science
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* This file is part of the Contiki operating system.
*
* @(#)$Id: dtc.h,v 1.1 2006/06/18 07:49:34 adamdunkels Exp $
*/
/**
* \addtogroup dtc
*
* @{
*/
/**
* \file
* Header file for the distributed TCP caching implementation.
* \author Adam Dunkels <adam@sics.se>
*
*/
#ifndef __DTC_H__
#define __DTC_H__
#include "uip.h"
#include "uipopt.h"
void dtc_init(void);
u8_t dtc_input(u8_t *packetptr, u16_t len);
PROCESS_NAME(dtc_process);
#endif /* __DTC_H__ */
/** @} */

View File

@ -0,0 +1,72 @@
/*
* Copyright (c) 2005, Swedish Institute of Computer Science
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* This file is part of the Contiki operating system.
*
* @(#)$Id: rs232-slip-drv.c,v 1.1 2006/06/18 07:49:34 adamdunkels Exp $
*/
#include "contiki.h"
#include "uip.h"
#include "tcpip.h"
#include "rs232-slip.h"
EK_POLLHANDLER(pollhandler);
EK_EVENTHANDLER(eventhandler, ev, data);
EK_PROCESS(p, "SLIP driver", EK_PRIO_HIGH,
eventhandler, pollhandler, NULL);
static ek_id_t id = EK_ID_NONE;
/*---------------------------------------------------------------------------*/
EK_POLLHANDLER(pollhandler)
{
uip_len = rs232_slip_poll();
tcpip_input();
}
/*---------------------------------------------------------------------------*/
EK_PROCESS_INIT(rs232_slip_drv_init, arg)
{
arg_free(arg);
if(id == EK_ID_NONE) {
id = ek_start(&p);
}
}
/*---------------------------------------------------------------------------*/
EK_EVENTHANDLER(eventhandler, ev, data)
{
EK_EVENTHANDLER_ARGS(ev, data);
}
/*---------------------------------------------------------------------------*/
void
rs232_slip_drv_request_poll(void)
{
ek_request_poll(id);
}
/*---------------------------------------------------------------------------*/

View File

@ -0,0 +1,40 @@
/*
* Copyright (c) 2005, Swedish Institute of Computer Science
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* This file is part of the Contiki operating system.
*
* @(#)$Id: rs232-slip-drv.h,v 1.1 2006/06/18 07:49:34 adamdunkels Exp $
*/
#ifndef __RS232_SLIP_DRV_H__
#define __RS232_SLIP_DRV_H__
void rs232_slip_drv_init(void *);
void rs232_slip_drv_request_poll(void);
#endif /* __RS232_SLIP_DRV_H__ */