Adding the avr-rss2 platform based on AtMega256RFR2

This commit is contained in:
Robert Olsson 2016-02-22 20:46:07 +01:00
parent d3980668ee
commit ce8e87d60e
91 changed files with 9349 additions and 0 deletions

View file

@ -0,0 +1,76 @@
/* Copyright Robert Olsson */
#include <avr/pgmspace.h>
#include <avr/fuse.h>
#include <avr/eeprom.h>
#include <stdio.h>
#include <string.h>
#include <dev/watchdog.h>
#include "params.h"
#include "rss2.h"
#include "contiki.h"
#include "adc.h"
#include "rss2.h"
uint16_t
adc_read(uint8_t pin)
{
uint16_t volt = 0;
int i;
ADMUX = pin; /* ADC pin PA0-PA7 -> 0-7 */
ADCSRB &= ~(1 << MUX5); /* Needs to write before ADMUX pp 418 */
ADMUX |= (1 << REFS1) | (1 << REFS0); /* 1.6 */
/* Maximal Track and Hold time */
ADCSRA = (1 << ADPS2) | (0 << ADPS1) | (1 << ADPS0); /* Prescaler /32 */
ADCSRA |= (1 << ADEN); /* Enable the ADC */
while(!(ADCSRB & (1 << AVDDOK))) ; /* Wait for AVDD ok */
while(!(ADCSRB & (1 << REFOK))) ; /* Wait for ref ok */
/* Ignore first result */
ADCSRA |= (1 << ADSC);
while(ADCSRA & _BV(ADSC)) {
}
ADCW = 0;
#define RES 4
for(i = 0; i < RES; i++) {
/* Start the conversion */
ADCSRA |= (1 << ADSC);
/* Wait for conversion */
while(ADCSRA & (1 << ADSC)) {
}
volt += ADCW;
}
ADMUX = 0; /* turn off internal vref */
volt = volt / RES;
/* Disable the ADC to save power */
ADCSRA &= ~_BV(ADEN);
return volt;
}
double
adc_read_v_in(void)
{
/* Special treatment for v_in. Schottky voltage drop add */
return ((double)adc_read(AV_IN)) * V_IN_FACTOR_SCHOTTKY + SCHOTTKY_DROP;
}
double
adc_read_a1(void)
{
return ((double)adc_read(A1)) * V_IN_FACTOR;
}
double
adc_read_a2(void)
{
return ((double)adc_read(A2)) * V_IN_FACTOR;
}

View file

@ -0,0 +1,16 @@
/* Copyright Robert Olsson */
#define V_IN_FACTOR 0.006256 /* 6.4/1023 */
#define V_IN_FACTOR_NONE 0.001564 /* 1.6/1023 */
#define SCHOTTKY_DROP 0.29
#define V_IN_FACTOR_SCHOTTKY 0.0292 /* 30.19-SCHOTTKY_DROP/1023 schottky corr */
uint16_t
adc_read(uint8_t pin);
double
adc_read_v_in(void);
double
adc_read_a1(void);
double
adc_read_a2(void);

View file

@ -0,0 +1,87 @@
/*
* 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 ADVISE OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*
* -----------------------------------------------------------------
*
* Author : Dag Björklund, Robert Olsson
* Created : 2005-11-01
* Updated : $Date: 2010/08/25 19:30:52 $
* $Revision: 1.11 $
*/
#include "contiki.h"
#include "dev/battery-sensor.h"
#include <util/delay_basic.h>
#define delay_us(us) (_delay_loop_2(1 + (us * F_CPU) / 4000000UL))
const struct sensors_sensor battery_sensor;
/* Returns the MCU voltage in mV read from the BATMON MCU register
* See AtMega chip docs for BATMON details.
*/
static int
value(int type)
{
uint16_t mv;
int i;
/* Resolution is 75mV if V>=2.55V; and 50mV if V<=2.45V */
mv = 3675;
for(i = 0x1f; 1; i--) {
BATMON = i;
delay_us(100);
if(BATMON & 0x20) {
break;
}
if(i == 0x10) { /* Range hi or lo */
mv = 2500;
}
if(i > 0x10) {
mv -= 75;
} else {
mv -= 50;
}
}
return (int)((float)mv);
}
static int
configure(int type, int c)
{
return 0;
}
static int
status(int type)
{
return 1;
}
SENSORS_SENSOR(battery_sensor, BATTERY_SENSOR, value, configure, status);

View file

@ -0,0 +1,41 @@
/* Dummy sensor routine */
#include "lib/sensors.h"
#include "dev/button-sensor.h"
const struct sensors_sensor button_sensor;
static int status(int type);
struct sensors_sensor *sensors[1];
unsigned char sensors_flags[1];
static int
value(int type)
{
return 0;
}
static int
configure(int type, int c)
{
switch(type) {
case SENSORS_ACTIVE:
if(c) {
if(!status(SENSORS_ACTIVE)) {
}
} else {
}
return 1;
}
return 0;
}
static int
status(int type)
{
switch(type) {
case SENSORS_ACTIVE:
case SENSORS_READY:
return 1;
}
return 0;
}
SENSORS_SENSOR(button_sensor, BUTTON_SENSOR,
value, configure, status);

View file

@ -0,0 +1,131 @@
/*
* Copyright (c) 2015, Copyright Markus Hidell, Robert Olsson
* 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.
*
*
* Authors : Markus Hidell, Robert Olsson {mahidell, roolss} @kth.se
* Created : 2015-10-27
* Updated : $Date: 2010/01/14 20:23:02 $
* $Revision: 1.2 $
*/
#include "contiki.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <i2c.h>
#include <avr/pgmspace.h>
#include <avr/sleep.h>
#include <avr/wdt.h>
#include <avr/boot.h>
#include <avr/eeprom.h>
#include "dev/watchdog.h"
#include "lib/list.h"
#include "lib/memb.h"
#include "co2_sa_kxx-sensor.h"
static int
status(int type)
{
return 0;
}
static int
configure(int type, int c)
{
return 0;
}
static int
value(int var)
{
int val, status;
uint8_t buf[2], csum;
int16_t res;
(void) status;
(void) csum;
res = 0;
i2c_start_wait(I2C_CO2SA_ADDR | I2C_WRITE);
if(res) {
goto err;
}
i2c_write(0x22);
i2c_write(0x00);
if(var == CO2_SA_KXX_CO2) {
i2c_write(0x08);
i2c_write(0x2A);
}
if(var == CO2_SA_KXX_TEMP) {
i2c_write(0x12);
i2c_write(0x34);
}
if(var == CO2_SA_KXX_RH) {
i2c_write(0x14);
i2c_write(0x36);
}
i2c_stop();
if(res) {
goto err;
}
clock_delay_msec(20);
res = 0;
i2c_start(I2C_CO2SA_ADDR | I2C_READ);
if(res) {
goto err;
}
status = i2c_readAck();
if(status & 0x01 == 0)
goto err;
buf[0] = i2c_readAck();
buf[1] = i2c_readAck();
csum = i2c_readNak();
i2c_stop();
val = ((int16_t)(buf[0] << 8)) | buf[1];
return val;
err:
i2c_stop();
return 0;
}
SENSORS_SENSOR(co2_sa_kxx_sensor, "CO2", value, configure, status);

View file

@ -0,0 +1,51 @@
/*
* Copyright (c) 2015, Copyright Markus Hidell, Robert Olsson
* 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.
*
*
* Authors : Markus Hidell, Robert Olsson {mahidell, roolss} @kth.se
* Created : 2015-10-27
* Updated : $Date: 2010/01/14 20:23:02 $
* $Revision: 1.2 $
*/
#ifndef CO2_SA_KXX_SENSOR_H_
#define CO2_SA_KXX_SENSOR_H_
#define CO2_SA_KXX_CO2 0
#define CO2_SA_KXX_TEMP 1
#define CO2_SA_KXX_RH 3
#include "lib/sensors.h"
extern const struct sensors_sensor co2_sa_kxx_sensor;
#define I2C_CO2SA_ADDR (0x68 << 1)
#endif /* CO2_SA_KXX-SENSOR_H_ */

View file

@ -0,0 +1,247 @@
/*
Contiki library for DS18B20 temperature sensor -
For more details see http://xxx
Author -
Author -
License - GPLv3
*/
#include "ds18b20.h"
/* probe_for_ds18b20 probes for the sensor. Returns 0 on failure, 1 on success */
/* Assumptions: only one sensor on the "1-wire bus", on port WSN_DS18B20_PORT */
/* BUG: THIS CODE DOES NOT WORK AS INTENDED! IT RETURNS "1" EVEN WHEN THERE IS NO */
/* SENSOR CONNECTED. */
uint8_t
ds18b20_probe(void)
{
uint8_t result = 0;
/* Reset 1W-bus */
/* Pull PIN low for 480 microseconds (us) */
/* Start with setting bit DS18B20_1_PIN to 0 */
OW_SET_PIN_LOW();
/* then set direction to OUT by setting DS18B20_1_DDR bit to 1 */
OW_SET_OUTPUT();
/* Delay 480 us */
clock_delay_usec(480);
/* See if sensor responds. First release the bus and switch to INput mode */
/* by setting DS18B20_1_DDR bit to 0 */
OW_SET_INPUT();
/* Activate internal pull-up by setting pin to HIGH (when in INput mode) */
/* OW_SET_PIN_HIGH(); */
/* Wait for the pin to go HIGH for 64 us */
clock_delay_usec(64);
/* Now the sensor, if present, pulls the pin LOW for 60-240 us */
/* Detect 0 on PIND bit DS18B20_1_PIN. Invert the result so a presence (aka a 0) */
/* sets "result" to 1 (for success) */
result = !OW_GET_PIN_STATE();
/* The sensor releases the pin so it goes HIGH after 240 us, add some */
/* for the signal to stabilize, say 300 usecs to be on the safe side? */
if(result) {
clock_delay_usec(300);
/* Now the bus should be HIGH again */
result = OW_GET_PIN_STATE();
}
return result;
}
/* Write 1 or 0 on the bus */
void
write_bit(uint8_t bit)
{
/* Set pin to 0 */
OW_SET_OUTPUT();
OW_SET_PIN_LOW();
/* Pin should be 0 for at least 1 us */
clock_delay_usec(2);
/* If we're writing a 1, let interna pull-up pull the bus high */
/* within 15 us of setting the bus to low */
if(bit) {
/* Internal pull-up is activated by setting direction to IN and the */
/* setting the pin to HIGH */
OW_SET_INPUT();
OW_SET_PIN_HIGH();
}
/* OK, now the bus is either LOW, or pulled HIGH by the internal pull-up */
/* Let this state remain for 60 us, then release the bus */
clock_delay_usec(60);
/* Release the bus */
OW_SET_PIN_HIGH();
OW_SET_INPUT();
/* Allow > 1 us between read/write operations */
clock_delay_usec(2);
}
/* */
/* Read one bit of information from the bus, and return it as 1 or 0 */
/* */
uint8_t
read_bit(void)
{
uint8_t bit = 0;
/* Set pin to 0 */
OW_SET_OUTPUT();
OW_SET_PIN_LOW();
/* Pin should be 0 for at least 1 us */
clock_delay_usec(2);
/* Now read the bus, start by setting in/out direction and activating internal */
/* pull-up resistor */
OW_SET_INPUT();
OW_SET_PIN_HIGH();
/* ds18b20 either keeps the pin down or releases the bus and the */
/* bus then goes high because of the interna pull-up resistor */
/* Check whichever happens before 15 us has passed */
clock_delay_usec(15 - 2 - 1);
bit = OW_GET_PIN_STATE();
/* The complete read cycle must last at least 60 us. We have now spent */
/* about 14-15 us in delays, so add another delay to reach >= 60 us */
clock_delay_usec(50);
/* Release bus */
OW_SET_PIN_HIGH();
OW_SET_INPUT();
/* Allow > 1 us between read/write operations */
clock_delay_usec(2);
return bit ? 1 : 0;
}
/* */
/* Read one byte of information. A byte is read least significant bit first */
/* */
uint8_t
read_byte(void)
{
uint8_t result = 0;
uint8_t bit;
int i;
for(i = 0; i < 8; i++) {
bit = read_bit();
result += (bit << i);
}
return result;
}
/* */
/* Write one byte of information. A byte is written least significant bit first */
/* */
void
write_byte(uint8_t byte)
{
int i;
for(i = 0; i < 8; i++) {
write_bit((byte >> i) & 1);
}
}
/* */
/* ds18b20_get_temp returns the temperature in "temp" (in degrees celsius) */
/* Returns 0 on failure (and then "temp" is left unchanged */
/* Returns 1 on success, and sets temp */
/* */
uint8_t
ds18b20_get_temp(float *temp)
{
uint8_t result = 0;
/* Reset bus by probing. Probe returns 1 on success/presence of sensor */
if(ds18b20_probe()) {
/* write command "skip rom" since we only have one sensor on the wire! */
write_byte(DS18B20_COMMAND_SKIP_ROM);
/* write command to start measurement */
write_byte(DS18B20_COMMAND_START_CONVERSION);
/* Wait for conversion to complete */
/* Conversion is 12-bit by default. */
/* Since we have external power to the sensor (ie not in "parasitic power" mode) */
/* the bus is held LOW by the sensor while the conversion is going on, and then HIGH */
/* when conversion is finished. */
OW_SET_INPUT();
int count = 0;
while(!OW_GET_PIN_STATE()) {
clock_delay_msec(10);
count++;
/* Longest conversion time is 750 ms (12-bit resolution) */
/* So if count > 80 (for a little margin!), we return -274.0 */
/* which indicates failure to read the temperature. */
if(count > 80) {
return 0;
}
}
/* The result is stored in the "scratch pad", a 9 byte memory block. */
/* The first two bytes are the conversion result. Reading the scratch pad */
/* can be terminated by sending a reset signal (but we read all 9 bytes) */
(void)ds18b20_probe();
write_byte(DS18B20_COMMAND_SKIP_ROM);
write_byte(DS18B20_COMMAND_READ_SCRATCH_PAD);
uint8_t i, sp_arr[9];
for(i = 0; i < 9; i++) {
sp_arr[i] = read_byte();
}
/* Check CRC, if mismatch, return 0 (failure to read temperature) */
uint8_t crc_cal = crc8_ds18b20(sp_arr, 8);
if(crc_cal != sp_arr[8]) {
return 0;
}
/* OK, now decode what the temperature reading is. This code assumes 12-bit resolution, */
/* so this must be modified if the code is modified to use any other resolution! */
int16_t temp_res;
uint8_t temp_lsb = sp_arr[0];
uint8_t temp_msb = sp_arr[1];
temp_res = (int16_t)temp_msb << 8 | temp_lsb;
*temp = (float)temp_res * 0.0625;
result = 1;
}
return result;
}
/* */
/* crc8 algorithm for ds18b20 */
/* http://www.miscel.dk/MiscEl/CRCcalculations.html */
/* */
uint8_t
crc8_ds18b20(uint8_t *buf, uint8_t buf_len)
{
uint8_t result = 0;
uint8_t i, b;
for(i = 0; i < buf_len; i++) {
result = result ^ buf[i];
for(b = 1; b < 9; b++) {
if(result & 0x1) {
result = (result >> 1) ^ 0x8C;
} else {
result = result >> 1;
}
}
}
return result;
}

View file

@ -0,0 +1,83 @@
/*
* Copyright (c) 2012-2013, Robert Olsson <robert@radio-sensors.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <avr/pgmspace.h>
#include <avr/pgmspace.h>
#include <avr/sleep.h>
#include "dev/watchdog.h"
#include "contiki.h"
#include "i2c.h"
#include <compat/twi.h>
#include <stdio.h>
#include <string.h>
#include "enc28j60_avr.h"
#include <util/delay_basic.h>
#define delay_us(us) (_delay_loop_2(1 + (us * F_CPU) / 4000000UL))
void
enc28j60_arch_spi_init(void)
{
CS_SPI_DDR |= (1 << SPI_CS);
CS_SPI_PORT |= (1 << SPI_CS);
SPI_DDR |= (1 << SPI_MOSI) | (1 << SPI_SCK);
SPI_DDR &= ~(1 << SPI_MISO);
SPI_PORT &= ~(1 << SPI_MOSI);
SPI_PORT &= ~(1 << SPI_SCK);
SPCR = (1 << SPE) | (1 << MSTR);
/* SPSR |= (1<<SPI2X); */
}
uint8_t
enc28j60_arch_spi_write(uint8_t data)
{
SPDR = data;
while(!(SPSR & (1 << SPIF))) ;
return SPDR;
}
uint8_t
enc28j60_arch_spi_read(void)
{
SPDR = 0xAA; /* dummy */
while(!(SPSR & (1 << SPIF))) ;
return SPDR;
}
void
enc28j60_arch_spi_select(void)
{
CS_SPI_PORT &= ~(1 << SPI_CS);
delay_us(1000);
}
void
enc28j60_arch_spi_deselect(void)
{
CS_SPI_PORT |= (1 << SPI_CS);
}

View file

@ -0,0 +1,53 @@
/*
* Copyright (c) 2012-2013, Robert Olsson <robert@radio-sensors.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#ifndef ENC28J60_AVR_H
#define ENC28J60_AVR_H
#define SPI_DDR DDRB /* Data Direction Register: Port B */
#define SPI_PORT PORTB /* Serial Peripheral Interface */
#define SPI_MOSI 2 /* PB2: Master Out Slave In */
#define SPI_MISO 3 /* PB3: Master In Slave out */
#define SPI_SCK 1 /* PB1: Serial Clock */
#define CS_SPI_DDR DDRD /* Data Direction Register: Port B */
#define CS_SPI_PORT PORTD /* Serial Peripheral Interface */
#define SPI_CS 6 /* PD6: OW2_PIN, Chip Select */
/* AVR specific's for enc28j60 */
void enc28j60_arch_spi_init(void);
uint8_t enc28j60_arch_spi_write(uint8_t data);
uint8_t enc28j60_arch_spi_read(void);
void enc28j60_arch_spi_select(void);
void enc28j60_arch_spi_deselect(void);
#endif /* ENC28J60_AVR_H */

228
platform/avr-rss2/dev/i2c.c Normal file
View file

@ -0,0 +1,228 @@
/*
* Copyright (c) 2010, 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.
*
*/
/**
* \file
* i2c core functions
* \author
* Robert Olsson <robert@radio-sensors.com>
*/
#include <avr/pgmspace.h>
#include <avr/pgmspace.h>
#include <avr/sleep.h>
#include <avr/wdt.h>
#include "dev/watchdog.h"
#include "contiki.h"
#include "i2c.h"
#include <compat/twi.h>
#include <stdio.h>
#include <string.h>
#include "dev/co2_sa_kxx-sensor.h"
void
i2c_init(uint32_t speed)
{
/* initialize TWI clock: 100 kHz clock, TWPS = 0 => prescaler = 1 */
TWSR = 0; /* no prescaler */
TWBR = ((F_CPU / speed) - 16) / 2; /* must be > 10 for stable operation */
}
uint8_t
i2c_start(uint8_t addr)
{
uint8_t twst;
uint32_t n;
/* Send START condition */
TWCR = (1 << TWINT) | (1 << TWSTA) | (1 << TWEN);
/* Wait until transmission completed */
for(n = 0; n < 100000 && !(TWCR & (1 << TWINT)); n++) {
}
if(n >= 100000) {
return 1;
}
/* check value of TWI Status Register. Mask prescaler bits. */
twst = TW_STATUS & 0xF8;
if((twst != TW_START) && (twst != TW_REP_START)) {
return 1;
}
/* send device address */
TWDR = addr;
TWCR = (1 << TWINT) | (1 << TWEN);
/* wail until transmission completed and ACK/NACK has been received */
for(n = 0; n < 100000 && !(TWCR & (1 << TWINT)); n++) {
}
if(n >= 100000) {
return 1;
}
/* check value of TWI Status Register. Mask prescaler bits. */
twst = TW_STATUS & 0xF8;
if((twst != TW_MT_SLA_ACK) && (twst != TW_MR_SLA_ACK)) {
return 1;
}
return 0;
}
void
i2c_start_wait(uint8_t addr)
{
uint8_t twst;
while ( 1 )
{
// send START condition
TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
// wait until transmission completed
while(!(TWCR & (1<<TWINT)));
// check value of TWI Status Register. Mask prescaler bits.
twst = TW_STATUS & 0xF8;
if ( (twst != TW_START) && (twst != TW_REP_START)) continue;
// send device address
TWDR = addr;
TWCR = (1<<TWINT) | (1<<TWEN);
// wail until transmission completed
while(!(TWCR & (1<<TWINT)));
// check value of TWI Status Register. Mask prescaler bits.
twst = TW_STATUS & 0xF8;
if ( (twst == TW_MT_SLA_NACK )||(twst ==TW_MR_DATA_NACK) )
{
/* device busy, send stop condition to terminate write operation */
TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
// wait until stop condition is executed and bus released
while(TWCR & (1<<TWSTO));
continue;
}
//if( twst != TW_MT_SLA_ACK) return 1;
break;
}
}
void
i2c_stop(void)
{
TWCR = (1 << TWINT) | (1 << TWSTO) | (1 << TWEN);
/* wait until ready */
while(TWCR & (1<<TWSTO));
}
void
i2c_write(uint8_t u8data)
{
TWDR = u8data;
TWCR = (1 << TWINT) | (1 << TWEN);
while((TWCR & (1 << TWINT)) == 0) ;
}
uint8_t
i2c_readAck(void)
{
TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWEA);
while((TWCR & (1 << TWINT)) == 0) ;
return TWDR;
}
uint8_t
i2c_readNak(void)
{
TWCR = (1 << TWINT) | (1 << TWEN);
while((TWCR & (1 << TWINT)) == 0) ;
return TWDR;
}
static void
print_delim(int p, char *s, const char *d)
{
if(p) {
printf("%s", d);
}
printf("%s", s);
}
void
i2c_write_mem(uint8_t addr, uint8_t reg, uint8_t value)
{
i2c_start(addr | I2C_WRITE);
i2c_write(reg);
i2c_write(value);
i2c_stop();
}
void
i2c_read_mem(uint8_t addr, uint8_t reg, uint8_t buf[], uint8_t bytes)
{
uint8_t i = 0;
i2c_start(addr | I2C_WRITE);
i2c_write(reg);
i2c_start(addr | I2C_READ);
for(i = 0; i < bytes; i++) {
if(i == bytes - 1) {
buf[i] = i2c_readNak();
} else {
buf[i] = i2c_readAck();
}
}
i2c_stop();
}
void
i2c_at24mac_read(char *buf, uint8_t eui64)
{
if(eui64) {
i2c_read_mem(I2C_AT24MAC_ADDR, 0x98, (uint8_t *)buf, 8);
}
/* 128bit unique serial number */
else {
i2c_read_mem(I2C_AT24MAC_ADDR, 0x80, (uint8_t *)buf, 16);
}
}
uint16_t
i2c_probe(void)
{
int p = 0;
const char *del = ",";
uint16_t probed = 0;
watchdog_periodic();
if(!i2c_start(I2C_AT24MAC_ADDR)) {
i2c_stop();
probed |= I2C_AT24MAC;
print_delim(p++, "AT24MAC", del);
}
watchdog_periodic();
if(!i2c_start(I2C_SHT2X_ADDR)) {
i2c_stop();
probed |= I2C_SHT2X;
print_delim(p++, "SHT2X", del);
}
watchdog_periodic();
if(!i2c_start(I2C_CO2SA_ADDR)) {
i2c_stop();
probed |= I2C_CO2SA;
print_delim(p++, "CO2SA", del);
}
return probed;
}

View file

@ -0,0 +1,65 @@
/*
* Copyright (c) 2010, 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.
*
*/
/**
* \file
* includes for i2c core functions
* \author
* Robert Olsson <robert@radio-sensors.com>
*/
#include "contiki.h"
/* Here we define the i2c address for dev we support */
#define I2C_AT24MAC_ADDR 0xB0 /* EUI64 ADDR */
#define I2C_SHT2X_ADDR (0x40 << 1) /* SHT2X ADDR */
/* Here we define a enumration for devices */
#define I2C_AT24MAC (1<<0)
#define I2C_SHT2X (1<<1)
#define I2C_CO2SA (1<<2) /* Sense-Air CO2 */
#define I2C_READ 1
#define I2C_WRITE 0
void i2c_init(uint32_t speed);
uint8_t i2c_start(uint8_t addr);
void i2c_start_wait(uint8_t addr);
void i2c_stop(void);
void i2c_write(uint8_t u8data);
uint8_t i2c_readAck(void);
uint8_t i2c_readNak(void);
uint8_t i2c_getstatus(void);
uint16_t i2c_probe(void);
void i2c_read_mem(uint8_t addr, uint8_t reg, uint8_t buf[], uint8_t bytes);
void i2c_write_mem(uint8_t addr, uint8_t reg, uint8_t value);
void i2c_at24mac_read(char *buf, uint8_t eui64);
extern uint16_t i2c_probed; /* i2c devices we have probed */

View file

@ -0,0 +1,45 @@
#include <avr/pgmspace.h>
#include "rss2.h"
#include "leds.h"
void
leds_init(void)
{
DDRE |= (1 << LED_RED);
DDRE |= (1 << LED_YELLOW);
/* Off */
leds_off(LEDS_ALL);
}
void
leds_on(unsigned char ledv)
{
if(ledv & LEDS_YELLOW) {
PORTE &= ~(1 << LED_YELLOW);
}
if(ledv & LEDS_RED) {
PORTE &= ~(1 << LED_RED);
}
}
void
leds_off(unsigned char ledv)
{
if(ledv & LEDS_YELLOW) {
PORTE |= (1 << LED_YELLOW);
}
if(ledv & LEDS_RED) {
PORTE |= (1 << LED_RED);
}
}
void
leds_toggle(unsigned char ledv)
{
}
void
leds_invert(unsigned char ledv)
{
}
unsigned char
leds_get(void)
{
return 0;
}

View file

@ -0,0 +1,17 @@
#ifndef CONTIKI_LED_H_
#define CONTIKI_LED_H_
#define LEDS_GREEN 1
#define LEDS_YELLOW 2
#define LEDS_RED 4
#define LEDS_ALL 7
void leds_init(void); /* Initialize the LEDs driver. */
unsigned char leds_get(void); /* Get the status of a LED. */
void leds_on(unsigned char ledv); /* Turn on a set of LEDs. */
void leds_off(unsigned char ledv); /* Turn off a set of LEDs. */
void leds_toggle(unsigned char ledv); /* Toggle a set of LEDs. */
void leds_invert(unsigned char ledv); /* Toggle a set of LEDs. */
#endif /* CONTIKI_LED_H_ */

View file

@ -0,0 +1,64 @@
/*
* Copyright (c) 2015, Copyright Robert Olsson
* 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.
*
*/
#include "contiki.h"
#include "lib/sensors.h"
#include "dev/light-sensor.h"
#include "rss2.h"
#include "adc.h"
const struct sensors_sensor light_sensor;
/*---------------------------------------------------------------------------*/
static int
value(int type)
{
return adc_read(A3);
}
/*---------------------------------------------------------------------------*/
static int
status(int type)
{
return 0;
}
/*---------------------------------------------------------------------------*/
static int
configure(int type, int c)
{
DDRF &= ~(1 << A3); /* Light sensor */
DDRF &= ~(1 << A3_PWR);
PORTF |= (1 << A3_PWR); /* Light sensor */
return 0;
}
/*---------------------------------------------------------------------------*/
SENSORS_SENSOR(light_sensor, "Light", value, configure, status);

View file

@ -0,0 +1,45 @@
/*
* Copyright (c) 2015, Copyright Robert Olsson
* 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.
*
*
* Author : Robert Olsson
* Created : 2015-10-27
* Updated : $Date: 2010/01/14 20:23:02 $
* $Revision: 1.2 $
*/
#ifndef LIGHT_SENSOR_H_
#define LIGHT_SENSOR_H_
#include "lib/sensors.h"
extern const struct sensors_sensor light_sensor;
#endif /* LIGHT-SENSOR_H_ */

View file

@ -0,0 +1,120 @@
/*
* Copyright (c) 2015, Copyright Robert Olsson / Radio Sensors AB
* 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.
*
*
* Author : Robert Olsson robert@radio-sensors.com
* Created : 2015-11-22
*/
#include "contiki.h"
#include "lib/sensors.h"
#include "dev/pulse-sensor.h"
#include "rss2.h"
const struct sensors_sensor pulse_sensor;
#define NP 2
uint32_t volatile pc[NP];
/*
* Note interrupt sources can be woken up from sleep mode PWR_SAVE
* Two interrupt ports, #0 green terminal block. #1 pin header via
* a comparator.
*/
static void
port_irq_ctrl(uint8_t on)
{
if(on) {
DDRD &= ~(1 << PD2);
PORTD &= ~(1 << PD2);
EIMSK = 0;
EICRA |= 0x20; /* Falling edge INT2 */
EIMSK |= (1 << PD2); /* Enable interrupt for pin */
/* p1 port */
DDRD &= ~(1 << PD3);
PORTD &= ~(1 << PD3);
EIMSK |= (1 << PD3); /* Enable interrupt for pin */
EICRA |= 0x80; /* Falling edge */
PCICR |= (1 << PCIE0); /* And enable irq PCINT 7:0 */
} else {
EICRA = 0;
PORTD |= (1 << PD2);
EIMSK &= ~(1 << PD2); /* Disable interrupt for pin */
PORTD |= (1 << PD3);
EIMSK &= ~(1 << PD3); /* Disable interrupt for pin */
}
}
ISR(INT2_vect)
{
if(!(PCICR & (1 << PCIE0))) {
return;
}
pc[0]++;
}
ISR(INT3_vect)
{
if(!(PCICR & (1 << PCIE0))) {
return;
}
pc[1]++;
}
/*---------------------------------------------------------------------------*/
static int
value(int type)
{
if(type == 0) {
return (int)pc[0];
}
if(type == 1) {
return (int)pc[1];
}
return -1;
}
/*---------------------------------------------------------------------------*/
static int
status(int type)
{
return 0;
}
/*---------------------------------------------------------------------------*/
static int
configure(int type, int c)
{
port_irq_ctrl(1); /* Enable pulse counts */
return 0;
}
/*---------------------------------------------------------------------------*/
SENSORS_SENSOR(pulse_sensor, "Pulse", value, configure, status);

View file

@ -0,0 +1,43 @@
/*
* Copyright (c) 2015, Copyright Robert Olsson / Radio Sensors AB
* 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.
*
*
* Author : Robert Olsson robert@radio-sensors.com
* Created : 2015-11-22
*/
#ifndef PULSE_SENSOR_H_
#define PULSE_SENSOR_H_
#include "lib/sensors.h"
extern const struct sensors_sensor pulse_sensor;
#endif /* PULSE-SENSOR_H_ */

View file

@ -0,0 +1,292 @@
/*
* Copyright (c) 2015, Copyright Per Lindgren <per.o.lindgren@gmail.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the 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.
*
*
* Author : Per Lindgren <per.o.lindgren@gmail.com>
* Hacked by: Robert Olsson robert@radio-sensors.com
* Created : 2015-11-22
*/
#include "contiki.h"
#include "dev/temp-sensor.h"
#include <util/delay_basic.h>
#define delay_us(us) (_delay_loop_2(1 + (us * F_CPU) / 4000000UL))
const struct sensors_sensor temp_mcu_sensor;
/* probe_for_ds18b20 probes for the sensor. Returns 0 on failure, 1 on success
* Assumptions: only one sensor on the "1-wire bus", on port WSN_DS18B20_PORT
* BUG: THIS CODE DOES NOT WORK AS INTENDED! IT RETURNS "1" EVEN WHEN THERE
* IS NO SENSOR CONNECTED.
*/
uint8_t
ds18b20_probe(void)
{
uint8_t result = 0;
/* Reset 1W-bus */
/* Pull PIN low for 480 microseconds (us)
* Start with setting bit DS18B20_1_PIN to 0 */
OW_SET_PIN_LOW();
/* then set direction to OUT by setting DS18B20_1_DDR bit to 1 */
OW_SET_OUTPUT();
/* Delay 480 us */
clock_delay_usec(480);
/* See if sensor responds. First release the bus and switch to INput mode
* by setting DS18B20_1_DDR bit to 0 */
OW_SET_INPUT();
/* Activate internal pull-up by setting pin to HIGH (when in INput mode)
* OW_SET_PIN_HIGH();
* Wait for the pin to go HIGH for 64 us */
clock_delay_usec(64);
/* Now the sensor, if present, pulls the pin LOW for 60-240 us
* Detect 0 on PIND bit DS18B20_1_PIN. Invert the result so a presence
* (aka * a 0) sets "result" to 1 (for success) */
result = !OW_GET_PIN_STATE();
/* The sensor releases the pin so it goes HIGH after 240 us, add some
for the signal to stabilize, say 300 usecs to be on the safe side? */
if(result) {
clock_delay_usec(300);
/* Now the bus should be HIGH again */
result = OW_GET_PIN_STATE();
}
return result;
}
/* Write 1 or 0 on the bus */
void
write_bit(uint8_t bit)
{
/* Set pin to 0 */
OW_SET_OUTPUT();
OW_SET_PIN_LOW();
/* Pin should be 0 for at least 1 us */
clock_delay_usec(2);
/* If we're writing a 1, let interna pull-up pull the bus high
* within 15 us of setting the bus to low */
if(bit) {
/* Internal pull-up is activated by setting direction to IN and the
* setting the pin to HIGH */
OW_SET_INPUT();
OW_SET_PIN_HIGH();
}
/* OK, now the bus is either LOW, or pulled HIGH by the internal pull-up
* Let this state remain for 60 us, then release the bus */
clock_delay_usec(60);
/* Release the bus */
OW_SET_PIN_HIGH();
OW_SET_INPUT();
/* Allow > 1 us between read/write operations */
clock_delay_usec(2);
}
/* Read one bit of information from the bus, and return it as 1 or 0 */
uint8_t
read_bit(void)
{
uint8_t bit = 0;
/* Set pin to 0 */
OW_SET_OUTPUT();
OW_SET_PIN_LOW();
/* Pin should be 0 for at least 1 us */
clock_delay_usec(2);
/* Now read the bus, start by setting in/out direction and activating
* internal pull-up resistor */
OW_SET_INPUT();
OW_SET_PIN_HIGH();
/* ds18b20 either keeps the pin down or releases the bus and the
* bus then goes high because of the interna pull-up resistor
* Check whichever happens before 15 us has passed */
clock_delay_usec(15 - 2 - 1);
bit = OW_GET_PIN_STATE();
/* The complete read cycle must last at least 60 us. We have now spent
* about 14-15 us in delays, so add another delay to reach >= 60 us */
clock_delay_usec(50);
/* Release bus */
OW_SET_PIN_HIGH();
OW_SET_INPUT();
/* Allow > 1 us between read/write operations */
clock_delay_usec(2);
return bit ? 1 : 0;
}
/* Read one byte of information. A byte is read least significant bit first */
uint8_t
read_byte(void)
{
uint8_t result = 0;
uint8_t bit;
int i;
for(i = 0; i < 8; i++) {
bit = read_bit();
result += (bit << i);
}
return result;
}
/* Write one byte of information. A byte is written least significant bit first */
void
write_byte(uint8_t byte)
{
int i;
for(i = 0; i < 8; i++) {
write_bit((byte >> i) & 1);
}
}
/* ds18b20_get_temp returns the temperature in "temp" (in degrees celsius)
* Returns 0 on failure (and then "temp" is left unchanged
* Returns 1 on success, and sets temp */
uint8_t
ds18b20_get_temp(double *temp)
{
uint8_t result = 0;
/* Reset bus by probing. Probe returns 1 on success/presence of sensor */
if(ds18b20_probe()) {
/* write command "skip rom" since we only have one sensor on the wire! */
write_byte(DS18B20_COMMAND_SKIP_ROM);
/* write command to start measurement */
write_byte(DS18B20_COMMAND_START_CONVERSION);
/* Wait for conversion to complete. Conversion is 12-bit by default.
* Since we have external power to the sensor (ie not in "parasitic power"
* mode) the bus is held LOW by the sensor while the conversion is going
* on, and then HIGH when conversion is finished. */
OW_SET_INPUT();
int count = 0;
while(!OW_GET_PIN_STATE()) {
clock_delay_msec(10);
count++;
/* Longest conversion time is 750 ms (12-bit resolution)
* So if count > 80 (for a little margin!), we return -274.0
* which indicates failure to read the temperature. */
if(count > 80) {
return 0;
}
}
/* The result is stored in the "scratch pad", a 9 byte memory block.
* The first two bytes are the conversion result. Reading the scratch pad
* can be terminated by sending a reset signal (but we read all 9 bytes) */
(void)ds18b20_probe();
write_byte(DS18B20_COMMAND_SKIP_ROM);
write_byte(DS18B20_COMMAND_READ_SCRATCH_PAD);
uint8_t i, sp_arr[9];
for(i = 0; i < 9; i++) {
sp_arr[i] = read_byte();
}
/* Check CRC, if mismatch, return 0 (failure to read temperature) */
uint8_t crc_cal = crc8_ds18b20(sp_arr, 8);
if(crc_cal != sp_arr[8]) {
return 0;
}
/* OK, now decode what the temperature reading is. This code assumes
* 12-bit resolution, so this must be modified if the code is modified
* to use any other resolution! */
int16_t temp_res;
uint8_t temp_lsb = sp_arr[0];
uint8_t temp_msb = sp_arr[1];
temp_res = (int16_t)temp_msb << 8 | temp_lsb;
*temp = (double)temp_res * 0.0625;
result = 1;
}
return result;
}
/* crc8 algorithm for ds18b20 */
/* http://www.miscel.dk/MiscEl/CRCcalculations.html */
uint8_t
crc8_ds18b20(uint8_t *buf, uint8_t buf_len)
{
uint8_t result = 0;
uint8_t i, b;
for(i = 0; i < buf_len; i++) {
result = result ^ buf[i];
for(b = 1; b < 9; b++) {
if(result & 0x1) {
result = (result >> 1) ^ 0x8C;
} else {
result = result >> 1;
}
}
}
return result;
}
static int
value(int type)
{
double t;
int ret;
ret = ds18b20_get_temp(&t);
/* Return temp multiplied by 100 for two decimals */
if(ret)
return (int) (t * 100);
/* Error return largest negative value */
return 0x8000;
}
static int
configure(int type, int c)
{
ds18b20_probe();
return 0;
}
static int
status(int type)
{
return 1;
}
SENSORS_SENSOR(temp_sensor, TEMP_SENSOR, value, configure, status);

View file

@ -0,0 +1,71 @@
/*
* Copyright (c) 2015, Copyright Per Lindgren <per.o.lindgren@gmail.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the 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.
*
*
* Author : Per Lindgren <per.o.lindgren@gmail.com>
* Hacked by: Robert Olsson robert@radio-sensors.com
* Created : 2015-11-22
*/
#ifndef TEMP_SENSOR_H_
#define TEMP_SENSOR_H_
#include "lib/sensors.h"
#include <sys/clock.h>
#include "contiki.h"
#include "rss2.h"
#define DS18B20_1_PIN OW_BUS_0
#define DS18B20_1_IN PIND
#define DS18B20_1_OUT PORTD
#define DS18B20_1_DDR DDRD
#define OW_SET_PIN_LOW() (DS18B20_1_OUT &= ~(1 << DS18B20_1_PIN))
#define OW_SET_PIN_HIGH() (DS18B20_1_OUT |= (1 << DS18B20_1_PIN))
#define OW_SET_OUTPUT() (DS18B20_1_DDR |= (1 << DS18B20_1_PIN))
#define OW_SET_INPUT() (DS18B20_1_DDR &= ~(1 << DS18B20_1_PIN))
#define OW_GET_PIN_STATE() ((DS18B20_1_IN & (1 << DS18B20_1_PIN)) ? 1 : 0)
#define DS18B20_COMMAND_READ_SCRATCH_PAD 0xBE
#define DS18B20_COMMAND_START_CONVERSION 0x44
#define DS18B20_COMMAND_SKIP_ROM 0xCC
/* probe_for_ds18b20 probes for the sensor. Returns 0 on failure, 1 on success
* Assumption: only one sensor on the "1-wire bus", on port DS18B20_1_PIN */
extern uint8_t ds18b20_probe(void);
extern uint8_t ds18b20_get_temp(double *temp);
extern uint8_t crc8_ds18b20(uint8_t *buf, uint8_t buf_len);
extern const struct sensors_sensor temp_sensor;
#define TEMP_SENSOR "temp"
#endif /* TEMP_SENSOR_H_ */

View file

@ -0,0 +1,90 @@
/*
* 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 ADVISE OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*
* -----------------------------------------------------------------
*
* Author : Robert Olsson
* Created : 2015-10-27
* Updated : $Date: 2010/08/25 19:30:52 $
* $Revision: 1.11 $
*/
#include "contiki.h"
#include "dev/temp_mcu-sensor.h"
#include <util/delay_basic.h>
#define delay_us(us) (_delay_loop_2(1 + (us * F_CPU) / 4000000UL))
const struct sensors_sensor temp_mcu_sensor;
/* Returns the MCU temp in C*10 read from the BATMON MCU register
* See AtMega chip docs for BATMON details.
*/
static int
value(int type)
{
uint16_t v;
ADCSRB |= (1 << MUX5); /* this bit buffered till ADMUX written to! */
ADMUX = 0xc9;
/* ADC on /32 ADC start */
ADCSRA = (1 << ADPS2) | (0 << ADPS1) | (1 << ADPS0);
ADCSRA |= (1 << ADEN); /* Enable the ADC */
while(!(ADCSRB & (1 << AVDDOK))) ; /* Wait for AVDD ok */
while(!(ADCSRB & (1 << REFOK))) ; /* Wait for ref ok */
ADCSRA |= (1 << ADSC); /* Throwaway conversion */
while
(ADCSRA & (1 << ADSC)) ;
ADCSRA |= (1 << ADSC); /* Start conversion */
while
(ADCSRA & (1 << ADSC)) ;
v = ADC;
/* Disable the ADC to save power */
ADCSRA &= ~_BV(ADEN);
/* ADMUX = 0; //turn off internal vref */
return (int)((double)(v * 1.13 - 272.8) * 10);
}
static int
configure(int type, int c)
{
return 0;
}
static int
status(int type)
{
return 1;
}
SENSORS_SENSOR(temp_mcu_sensor, TEMP_MCU_SENSOR, value, configure, status);

View file

@ -0,0 +1,47 @@
/*
* 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.
*
*
* -----------------------------------------------------------------
*
* Author : Robert Olsson
* Created : 2015-10-27
* Updated : $Date: 2007/11/13 20:36:40 $
* $Revision: 1.1 $
*/
#ifndef TEMP_MCU_SENSOR_H_
#define TEMP_MCU_SENSOR_H_
#include "lib/sensors.h"
extern const struct sensors_sensor temp_mcu_sensor;
#define TEMP_MCU_SENSOR "temp_mcu"
#endif /* TEMP_MCU_SENSOR_H_ */