From 9a1ce7cf7515db03717a6cdb24e5489a7430ed02 Mon Sep 17 00:00:00 2001 From: David Kopf Date: Wed, 31 Aug 2011 11:40:23 -0400 Subject: [PATCH 1/6] Use enumerated tx return values --- cpu/avr/radio/rf230bb/rf230bb.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/cpu/avr/radio/rf230bb/rf230bb.c b/cpu/avr/radio/rf230bb/rf230bb.c index 9889b4629..9af14387e 100644 --- a/cpu/avr/radio/rf230bb/rf230bb.c +++ b/cpu/avr/radio/rf230bb/rf230bb.c @@ -980,16 +980,18 @@ rf230_transmit(unsigned short payload_len) RELEASE_LOCK(); if (tx_result==1) { //success, data pending from adressee - tx_result=0; //Just show success? + tx_result = RADIO_TX_OK; //Just show success? } else if (tx_result==3) { //CSMA channel access failure DEBUGFLOW('m'); RIMESTATS_ADD(contentiondrop); PRINTF("rf230_transmit: Transmission never started\n"); +....tx_result = RADIO_TX_COLLISION; } else if (tx_result==5) { //Expected ACK, none received DEBUGFLOW('n'); -// tx_result=0; +....tx_result = RADIO_TX_NOACK; } else if (tx_result==7) { //Invalid (Can't happen since waited for idle above?) DEBUGFLOW('o'); +....tx_result = RADIO_TX_ERR; } return tx_result; @@ -1694,4 +1696,4 @@ void rf230_start_sneeze(void) { // while (hal_register_read(0x0f)!=1) {continue;} //wait for pll lock-hangs hal_register_write(0x02,0x02); //Set TRX_STATE to TX_START } -#endif \ No newline at end of file +#endif From 36b290a1cd81c33e1f9e6254e5136b193d761b16 Mon Sep 17 00:00:00 2001 From: David Kopf Date: Wed, 31 Aug 2011 11:47:17 -0400 Subject: [PATCH 2/6] Refactor with params.c, h --- .../Makefile.avr-atmega128rfa1 | 2 +- .../raven-webserver/httpd-fs/makefsdata.h | 11 +- platform/avr-atmega128rfa1/contiki-main.c | 422 +++++++++--------- platform/avr-atmega128rfa1/params.c | 264 +++++++++++ platform/avr-atmega128rfa1/params.h | 108 +++++ 5 files changed, 584 insertions(+), 223 deletions(-) create mode 100644 platform/avr-atmega128rfa1/params.c create mode 100644 platform/avr-atmega128rfa1/params.h diff --git a/platform/avr-atmega128rfa1/Makefile.avr-atmega128rfa1 b/platform/avr-atmega128rfa1/Makefile.avr-atmega128rfa1 index d845dd61f..46b03bf28 100644 --- a/platform/avr-atmega128rfa1/Makefile.avr-atmega128rfa1 +++ b/platform/avr-atmega128rfa1/Makefile.avr-atmega128rfa1 @@ -2,7 +2,7 @@ CONTIKI_TARGET_DIRS = . apps net loader CONTIKI_CORE=contiki-main CONTIKI_TARGET_MAIN = ${CONTIKI_CORE}.o -CONTIKI_TARGET_SOURCEFILES += contiki-main.c +CONTIKI_TARGET_SOURCEFILES += contiki-main.c params.c #Needed for slip CONTIKI_TARGET_SOURCEFILES += button-sensor.c sensors.c slip_uart0.c slip.c diff --git a/platform/avr-atmega128rfa1/apps/raven-webserver/httpd-fs/makefsdata.h b/platform/avr-atmega128rfa1/apps/raven-webserver/httpd-fs/makefsdata.h index 612ebf949..f532f75e3 100644 --- a/platform/avr-atmega128rfa1/apps/raven-webserver/httpd-fs/makefsdata.h +++ b/platform/avr-atmega128rfa1/apps/raven-webserver/httpd-fs/makefsdata.h @@ -1,7 +1,10 @@ #include -/* Link layer ipv6 address will become fe80::11:22ff:fe33:4455 */ -uint8_t mac_address[8] EEMEM = {0x02, 0x11, 0x22, 0xff, 0xfe, 0x33, 0x44, 0x55}; -uint8_t server_name[16] EEMEM = "Contiki-Raven"; -uint8_t domain_name[30] EEMEM = "localhost"; +/* Link layer ipv6 address will become fe80::ff:fe:1 */ +uint8_t default_mac_address[8] PROGMEM = {0x02, 0x00, 0x00, 0xff, 0xfe, 0x00, 0x00, 0x01}; +uint8_t default_server_name[16] PROGMEM = "ATMEGA128rfa1"; +uint8_t default_domain_name[30] PROGMEM = "localhost"; +uint8_t eemem_mac_address[8] EEMEM = {0x02, 0x00, 0x00, 0xff, 0xfe, 0x00, 0x00, 0x01}; +uint8_t eemem_server_name[16] EEMEM = "ATMEGA128rfa1"; +uint8_t eemem_domain_name[30] EEMEM = "localhost"; diff --git a/platform/avr-atmega128rfa1/contiki-main.c b/platform/avr-atmega128rfa1/contiki-main.c index b59befdf0..3b861ec7b 100644 --- a/platform/avr-atmega128rfa1/contiki-main.c +++ b/platform/avr-atmega128rfa1/contiki-main.c @@ -29,11 +29,23 @@ * This file is part of the Contiki operating system. * */ +#define PRINTF(FORMAT,args...) printf_P(PSTR(FORMAT),##args) + #define ANNOUNCE_BOOT 1 //adds about 600 bytes to program size +#if ANNOUNCE_BOOT +#define PRINTA(FORMAT,args...) printf_P(PSTR(FORMAT),##args) +#else +#define PRINTA(...) +#endif -#define DEBUG DEBUG_PRINT -#include "uip-debug.h" ////Does #define PRINTA(FORMAT,args...) printf_P(PSTR(FORMAT),##args) for AVR +#define DEBUG 0 +#if DEBUG +#define PRINTD(FORMAT,args...) printf_P(PSTR(FORMAT),##args) +#else +#define PRINTD(...) +#endif +/* Track interrupt flow through mac, rdc and radio driver */ #if DEBUGFLOWSIZE uint8_t debugflowsize,debugflow[DEBUGFLOWSIZE]; #define DEBUGFLOW(c) if (debugflowsize<(DEBUGFLOWSIZE-1)) debugflow[debugflowsize++]=c @@ -51,20 +63,12 @@ uint8_t debugflowsize,debugflow[DEBUGFLOWSIZE]; #include "loader/symbols-def.h" #include "loader/symtab.h" -#if RF230BB //radio driver using contiki core mac +#include "params.h" #include "radio/rf230bb/rf230bb.h" #include "net/mac/frame802154.h" #include "net/mac/framer-802154.h" #include "net/sicslowpan.h" -#else //radio driver using Atmel/Cisco 802.15.4'ish MAC -#include -#include "mac.h" -#include "sicslowmac.h" -#include "sicslowpan.h" -#include "ieee-15-4-manager.h" -#endif /*RF230BB*/ - #include "contiki.h" #include "contiki-net.h" #include "contiki-lib.h" @@ -73,7 +77,6 @@ uint8_t debugflowsize,debugflow[DEBUGFLOWSIZE]; #include "dev/serial-line.h" #include "dev/slip.h" -/* No 3290p to talk to but the lcd process still needed for uip stack ping callbacks */ #ifdef RAVEN_LCD_INTERFACE #include "raven-lcd.h" #endif @@ -97,11 +100,12 @@ uint8_t debugflowsize,debugflow[DEBUGFLOWSIZE]; /* Get periodic prints from idle loop, from clock seconds or rtimer interrupts */ /* Use of rtimer will conflict with other rtimer interrupts such as contikimac radio cycling */ +/* STAMPS will print ENERGEST outputs if that is enabled. */ #define PERIODICPRINTS 1 #if PERIODICPRINTS //#define PINGS 64 -#define ROUTES 128 -#define STAMPS 30 +#define ROUTES 600 +#define STAMPS 60 #define STACKMONITOR 1024 uint32_t clocktime; #define TESTRTIMER 0 @@ -112,111 +116,86 @@ void rtimercycle(void) {rtimerflag=1;} #endif #endif +uint16_t ledtimer; + /*-------------------------------------------------------------------------*/ /*----------------------Configuration of the .elf file---------------------*/ -typedef struct {unsigned char B2;unsigned char B1;unsigned char B0;} __signature_t; +#if 1 +/* The proper way to set the signature is */ +#include +#else +/* Older avr-gcc's may not define the needed SIGNATURE bytes. Do it manually if you get an error */ +typedef struct {const unsigned char B2;const unsigned char B1;const unsigned char B0;} __signature_t; #define SIGNATURE __signature_t __signature __attribute__((section (".signature"))) SIGNATURE = { -/* Older AVR-GCCs may not define the SIGNATURE_n bytes so use explicit ATmega128rfa1 values */ - .B2 = SIGNATURE_2,//0x01,//SIGNATURE_2, - .B1 = SIGNATURE_1,//0xA7,//SIGNATURE_1, - .B0 = SIGNATURE_0,//0x1E,//SIGNATURE_0, + .B2 = 0x01,//SIGNATURE_2, //ATMEGA128rfa1 + .B1 = 0xA7,//SIGNATURE_1, //128KB flash + .B0 = 0x1E,//SIGNATURE_0, //Atmel }; -//JTAG+SPI, Boot 4096 words @ $F000, Internal oscillator, startup 6 CK + 65 ms, Brownout disabled +#endif + +#if 1 +/* JTAG, SPI enabled, Internal RC osc, Boot flash size 4K, 6CK+65msec delay, brownout disabled */ FUSES ={.low = 0xe2, .high = 0x99, .extended = 0xff,}; -//JTAG+SPI, Boot 4096 words @ $F000, Internal oscillator, startup 6 CK +0 ms, Brownout 1.8 volts -//FUSES ={.low = 0xC2, .high = 0x99, .extended = 0xfe,}; - -/*----------------------Configuration of EEPROM---------------------------*/ -/* Use existing EEPROM if it passes the integrity test, else reinitialize with build values */ - -/* Put default MAC address in EEPROM */ -#if AVR_WEBSERVER -extern uint8_t mac_address[8]; //These are defined in httpd-fsdata.c via makefsdata.h -extern uint8_t server_name[16]; -extern uint8_t domain_name[30]; #else -uint8_t mac_address[8] EEMEM = {0x02, 0x11, 0x22, 0xff, 0xfe, 0x33, 0x44, 0x55}; +/* JTAG+SPI, Boot 4096 words @ $F000, Internal oscillator, startup 6 CK +0 ms, Brownout 1.8 volts */ +FUSES ={.low = 0xC2, .high = 0x99, .extended = 0xfe,}; #endif - -#ifdef CHANNEL_802_15_4 -uint8_t rf_channel[2] EEMEM = {CHANNEL_802_15_4, ~CHANNEL_802_15_4}; -//uint8_t rf_channel[2] EEMEM = {11, ~11}; //econotag test +uint8_t +rng_get_uint8(void) { +#if 1 + /* Upper two RSSI reg bits (RND_VALUE) are random in rf231 */ + uint8_t j; + j = (PHY_RSSI>>6) | (PHY_RSSI>>4) | (PHY_RSSI>>4) | PHY_RSSI; #else -uint8_t rf_channel[2] EEMEM = {26, ~26}; -#endif -static uint8_t get_channel_from_eeprom() { - uint8_t eeprom_channel; - uint8_t eeprom_check; - - eeprom_channel = eeprom_read_byte(&rf_channel[0]); - eeprom_check = eeprom_read_byte(&rf_channel[1]); - - if(eeprom_channel==~eeprom_check) - return eeprom_channel; - -#ifdef CHANNEL_802_15_4 -//return(11); - return(CHANNEL_802_15_4); -#else - return 26; +/* Get a pseudo random number using the ADC */ + uint8_t i,j; + ADCSRA=1< //#define delay_us( us ) ( _delay_loop_2(1+(us*F_CPU)/4000000UL) ) // delay_us(50000); } clock_init(); - watchdog_start(); } #endif -#if ANNOUNCE_BOOT PRINTA("\n*******Booting %s*******\n",CONTIKI_VERSION_STRING); -#endif /* rtimers needed for radio cycling */ rtimer_init(); /* Initialize process subsystem */ process_init(); - /* etimers must be started before ctimer_init */ + + /* etimers must be started before ctimer_init */ process_start(&etimer_process, NULL); - -#if RF230BB - ctimer_init(); + /* Start radio and radio receive process */ NETSTACK_RADIO.init(); -#if 1 -{uint8_t somebits; - /* Upper two RSSI reg bits (RND_VALUE) are random in rf231 */ - somebits= (PHY_RSSI>>6) | (PHY_RSSI>>4) | (PHY_RSSI>>4) | PHY_RSSI; - PRINTF("rnd=%d\n", somebits); - random_init(somebits); -} -#endif +/* Get a random seed for the 802.15.4 packet sequence number. + * Some layers will ignore duplicates found in a history (e.g. Contikimac) + * causing the initial packets to be ignored after a short-cycle restart. + */ + random_init(rng_get_uint8()); /* Set addresses BEFORE starting tcpip process */ rimeaddr_t addr; - memset(&addr, 0, sizeof(rimeaddr_t)); - get_mac_from_eeprom(addr.u8); + + if (params_get_eui64(addr.u8)) { + PRINTA("Random EUI64 address generated\n"); + } #if UIP_CONF_IPV6 - memcpy(&uip_lladdr.addr, &addr.u8, 8); + memcpy(&uip_lladdr.addr, &addr.u8, sizeof(rimeaddr_t)); +#elif WITH_NODE_ID + node_id=get_panaddr_from_eeprom(); + addr.u8[1]=node_id&0xff; + addr.u8[0]=(node_id&0xff00)>>8; + PRINTA("Node ID from eeprom: %X\n",node_id); #endif - rf230_set_pan_addr( - get_panid_from_eeprom(), - get_panaddr_from_eeprom(), - (uint8_t *)&addr.u8 - ); - rf230_set_channel(get_channel_from_eeprom()); - rimeaddr_set_node_addr(&addr); - PRINTF("MAC address %x:%x:%x:%x:%x:%x:%x:%x\n",addr.u8[0],addr.u8[1],addr.u8[2],addr.u8[3],addr.u8[4],addr.u8[5],addr.u8[6],addr.u8[7]); + rf230_set_pan_addr(params_get_panid(),params_get_panaddr(),(uint8_t *)&addr.u8); + rf230_set_channel(params_get_channel()); + rf230_set_txpower(params_get_txpower()); + +#if UIP_CONF_IPV6 + PRINTA("EUI-64 MAC: %x-%x-%x-%x-%x-%x-%x-%x\n",addr.u8[0],addr.u8[1],addr.u8[2],addr.u8[3],addr.u8[4],addr.u8[5],addr.u8[6],addr.u8[7]); +#else + PRINTA("MAC address "); + uint8_t i; + for (i=sizeof(rimeaddr_t); i>0; i--){ + PRINTA("%x:",addr.u8[i-1]); + } + PRINTA("\n"); +#endif /* Initialize stack protocols */ queuebuf_init(); @@ -319,7 +291,7 @@ uint8_t i; NETSTACK_NETWORK.init(); #if ANNOUNCE_BOOT - PRINTA("%s %s, channel %u",NETSTACK_MAC.name, NETSTACK_RDC.name,rf230_get_channel()); + PRINTA("%s %s, channel %u power %u",NETSTACK_MAC.name, NETSTACK_RDC.name,rf230_get_channel(),rf230_get_txpower()); if (NETSTACK_RDC.channel_check_interval) {//function pointer is zero for sicslowmac unsigned short tmp; tmp=CLOCK_SECOND / (NETSTACK_RDC.channel_check_interval == 0 ? 1:\ @@ -327,24 +299,18 @@ uint8_t i; if (tmp<65535) PRINTA(", check rate %u Hz",tmp); } PRINTA("\n"); -#endif +#if UIP_CONF_IPV6_RPL + PRINTA("RPL Enabled\n"); +#endif #if UIP_CONF_ROUTER -#if ANNOUNCE_BOOT PRINTA("Routing Enabled\n"); #endif -// rime_init(rime_udp_init(NULL)); -// uip_router_register(&rimeroute); -#endif + +#endif /* ANNOUNCE_BOOT */ process_start(&tcpip_process, NULL); - #else -/* mac process must be started before tcpip process! */ - process_start(&mac_process, NULL); - process_start(&tcpip_process, NULL); -#endif /*RF230BB*/ - #ifdef RAVEN_LCD_INTERFACE process_start(&raven_lcd_process, NULL); #endif @@ -352,20 +318,17 @@ uint8_t i; /* Autostart other processes */ autostart_start(autostart_processes); - //Give ourselves a prefix - // init_net(); - /*---If using coffee file system create initial web content if necessary---*/ #if COFFEE_FILES int fa = cfs_open( "/index.html", CFS_READ); if (fa<0) { //Make some default web content - PRINTF("No index.html file found, creating upload.html!\n"); + PRINTA("No index.html file found, creating upload.html!\n"); PRINTA("Formatting FLASH file system for coffee..."); cfs_coffee_format(); PRINTA("Done!\n"); fa = cfs_open( "/index.html", CFS_WRITE); int r = cfs_write(fa, &"It works!", 9); - if (r<0) PRINTF("Can''t create /index.html!\n"); + if (r<0) PRINTA("Can''t create /index.html!\n"); cfs_close(fa); // fa = cfs_open("upload.html"), CFW_WRITE); //
@@ -384,9 +347,8 @@ uint8_t i; /*--------------------------Announce the configuration---------------------*/ #if ANNOUNCE_BOOT - #if AVR_WEBSERVER - uint8_t i; +{ uint8_t i; char buf[80]; unsigned int size; @@ -396,11 +358,15 @@ uint8_t i; PRINTA("IPv6 Address: %s\n",buf); } } - eeprom_read_block (buf,server_name, sizeof(server_name)); - buf[sizeof(server_name)]=0; + cli(); + eeprom_read_block (buf,eemem_server_name, sizeof(eemem_server_name)); + sei(); + buf[sizeof(eemem_server_name)]=0; PRINTA("%s",buf); - eeprom_read_block (buf,domain_name, sizeof(domain_name)); - buf[sizeof(domain_name)]=0; + cli(); + eeprom_read_block (buf,eemem_domain_name, sizeof(eemem_domain_name)); + sei(); + buf[sizeof(eemem_domain_name)]=0; size=httpd_fs_get_size(); #ifndef COFFEE_FILES PRINTA(".%s online with fixed %u byte web content\n",buf,size); @@ -413,25 +379,40 @@ uint8_t i; #elif COFFEE_FILES==4 PRINTA(".%s online with dynamic %u KB program memory file system\n",buf,size>>10); #endif /* COFFEE_FILES */ - +} #else PRINTA("Online\n"); -#endif /* AVR_WEBSERVER */ - -#endif /* ANNOUNCE_BOOT */ -} - -/*---------------------------------------------------------------------------*/ -void log_message(char *m1, char *m2) -{ - PRINTA("%s%s\n", m1, m2); -} - -#if RF230BB -extern char rf230_interrupt_flag, rf230processflag; #endif +#endif /* ANNOUNCE_BOOT */ -uint16_t ledtimer; +#if RF230BB_CONF_LEDONPORTE1 + /* NB: PORTE1 conflicts with UART0 */ + DDRE|=(1<u8[i] << 8) + addr->u8[i + 1]; + if(a == 0 && f >= 0) { + if(f++ == 0) PRINTF("::"); + } else { + if(f > 0) { + f = -1; + } else if(i > 0) { + PRINTF(":"); + } + PRINTF("%x",a); + } + } +} +#endif /*-------------------------------------------------------------------------*/ /*------------------------- Main Scheduler loop----------------------------*/ @@ -439,37 +420,25 @@ uint16_t ledtimer; int main(void) { - initialize(); -#if RF230BB_CONF_LEDONPORTE1 - /* NB: PORTE1 conflicts with UART0 */ - DDRE|=(1<"); - PRINTA("\nRoutes [%u max]\n",UIP_DS6_ROUTE_NB); + if (j) PRINTF(" "); + PRINTF("\nRoutes [%u max]\n",UIP_DS6_ROUTE_NB); for(i = 0,j=1; i < UIP_DS6_ROUTE_NB; i++) { if(uip_ds6_routing_table[i].isused) { - uip_debug_ipaddr_print(&uip_ds6_routing_table[i].ipaddr); - PRINTA("/%u (via ", uip_ds6_routing_table[i].length); - uip_debug_ipaddr_print(&uip_ds6_routing_table[i].nexthop); + ipaddr_add(&uip_ds6_routing_table[i].ipaddr); + PRINTF("/%u (via ", uip_ds6_routing_table[i].length); + ipaddr_add(&uip_ds6_routing_table[i].nexthop); // if(uip_ds6_routing_table[i].state.lifetime < 600) { - PRINTA(") %lus\n", uip_ds6_routing_table[i].state.lifetime); + PRINTF(") %lus\n", uip_ds6_routing_table[i].state.lifetime); // } else { - // PRINTA(")\n"); + // PRINTF(")\n"); // } j=0; } } - if (j) PRINTA(" "); - PRINTA("\n---------\n"); + if (j) PRINTF(" "); + PRINTF("\n---------\n"); } #endif @@ -584,7 +561,7 @@ if ((clocktime%STACKMONITOR)==3) { uint16_t p=(uint16_t)&__bss_end; do { if (*(uint16_t *)p != 0x4242) { - PRINTA("Never-used stack > %d bytes\n",p-(uint16_t)&__bss_end); + PRINTF("Never-used stack > %d bytes\n",p-(uint16_t)&__bss_end); break; } p+=10; @@ -596,20 +573,29 @@ if ((clocktime%STACKMONITOR)==3) { #endif /* PERIODICPRINTS */ #if RF230BB&&0 +extern uint8_t rf230processflag; if (rf230processflag) { - PRINTA("rf230p%d",rf230processflag); + PRINTF("rf230p%d",rf230processflag); rf230processflag=0; } #endif #if RF230BB&&0 +extern uint8_t rf230_interrupt_flag; if (rf230_interrupt_flag) { // if (rf230_interrupt_flag!=11) { - PRINTA("**RI%u",rf230_interrupt_flag); + PRINTF("**RI%u",rf230_interrupt_flag); // } rf230_interrupt_flag=0; } #endif } return 0; -} \ No newline at end of file +} + +/*---------------------------------------------------------------------------*/ + +void log_message(char *m1, char *m2) +{ + PRINTF("%s%s\n", m1, m2); +} diff --git a/platform/avr-atmega128rfa1/params.c b/platform/avr-atmega128rfa1/params.c new file mode 100644 index 000000000..480ee3ee5 --- /dev/null +++ b/platform/avr-atmega128rfa1/params.c @@ -0,0 +1,264 @@ +/* + * Copyright (c) 2011, 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. + * + */ +#define PRINTF(FORMAT,args...) printf_P(PSTR(FORMAT),##args) + +#define DEBUG 1 +#if DEBUG +#define PRINTD(FORMAT,args...) printf_P(PSTR(FORMAT),##args) +#else +#define PRINTD(...) +#endif + +#include "contiki.h" +#include +#include +#include +#include + +#if AVR_WEBSERVER +//#include "httpd-fs.h" +//#include "httpd-cgi.h" +#endif + +#include "contiki-net.h" +#include "params.h" + +#if WITH_NODE_ID +uint16_t node_id; +#endif + +#if CONTIKI_CONF_RANDOM_MAC +extern uint8_t rng_get_uint8(void); +static void +generate_new_eui64(uint8_t eui64[8]) { + eui64[0] = 0x02; + eui64[1] = rng_get_uint8(); + eui64[2] = rng_get_uint8(); + eui64[3] = 0xFF; + eui64[4] = 0xFE; + eui64[5] = rng_get_uint8(); + eui64[6] = rng_get_uint8(); + eui64[7] = rng_get_uint8(); +} +#endif + +#if AVR_WEBSERVER +/* Webserver builds can set these in httpd-fsdata.c via makefsdata.h */ +extern uint8_t default_mac_address[8]; +extern uint8_t default_server_name[16]; +extern uint8_t default_domain_name[30]; +#else +uint8_t default_mac_address[8] PROGMEM = PARAMS_EUI64ADDR; +uint8_t default_server_name[] PROGMEM = PARAMS_SERVERNAME; +uint8_t default_domain_name[] PROGMEM = PARAMS_DOMAINNAME; +#endif + +#if PARAMETER_STORAGE==0 +/* 0 Hard coded, minmal program and eeprom usage. */ +uint8_t +params_get_eui64(uint8_t *eui64) { +#if CONTIKI_CONF_RANDOM_MAC + PRINTD("Generating random EUI64 MAC\n"); + generate_new_eui64(eui64); + return 1; +#else + uint8_t i; + for (i=0;i 26)) x[1]=x[0]; +/* Do exclusive or test on the two values read */ + if((uint8_t)x[0]!=(uint8_t)~x[1]) {//~x[1] can promote comparison to 16 bit +/* Verification fails, rewrite everything */ + uint8_t i,buffer[32]; + PRINTD("EEPROM is corrupt, rewriting with defaults.\n"); +#if CONTIKI_CONF_RANDOM_MAC + PRINTD("Generating random EUI64 MAC\n"); + generate_new_eui64(&buffer); + randomeui64=1; +#else + for (i=0;iSet EEPROM RF channel to %d\n",x); + } + } + return x; +} +uint8_t +params_get_eui64(uint8_t *eui64) { + size_t size = sizeof(rimeaddr_t); + if(settings_get(SETTINGS_KEY_EUI64, 0, (unsigned char*)eui64, &size) == SETTINGS_STATUS_OK) { + PRINTD("<-Get EUI64 MAC\n"); + return 0; + } +#if CONTIKI_CONF_RANDOM_MAC + PRINTD("Generating random EUI64 MAC\n"); + generate_new_eui64(eui64); +#else + {uint8_t i;for (i=0;i<8;i++) eui64[i] = pgm_read_byte_near(default_mac_address+i);} //test this +#endif + if (settings_add(SETTINGS_KEY_EUI64,(unsigned char*)eui64,8) == SETTINGS_STATUS_OK) { + PRINTD("->Set EEPROM MAC address\n"); + } +#if CONTIKI_CONF_RANDOM_MAC + return 1; +#else + return 0; +#endif +} +uint16_t +params_get_panid(void) { + uint16_t x; + size_t size = 2; + if (settings_get(SETTINGS_KEY_PAN_ID, 0,(unsigned char*)&x, &size) == SETTINGS_STATUS_OK) { + PRINTD("<-Get PAN ID of %04x\n",x); + } else { + x=PARAMS_PANID; + if (settings_add_uint16(SETTINGS_KEY_PAN_ID,x)==SETTINGS_STATUS_OK) { + PRINTD("->Set EEPROM PAN ID to %04x\n",x); + } + } + return x; +} +uint16_t +params_get_panaddr(void) { + uint16_t x; + size_t size = 2; + if (settings_get(SETTINGS_KEY_PAN_ADDR, 0,(unsigned char*)&x, &size) == SETTINGS_STATUS_OK) { + PRINTD("<-Get PAN address of %04x\n",x); + } else { + x=PARAMS_PANADDR; + if (settings_add_uint16(SETTINGS_KEY_PAN_ADDR,x)==SETTINGS_STATUS_OK) { + PRINTD("->Set EEPROM PAN address to %04x\n",x); + } + } + return x; +} +uint8_t +params_get_txpower(void) { + uint8_t x; + size_t size = 1; + if (settings_get(SETTINGS_KEY_TXPOWER, 0,(unsigned char*)&x, &size) == SETTINGS_STATUS_OK) { + PRINTD("<-Get tx power of %d (0=max)\n",x); + } else { + x=PARAMS_TXPOWER; + if (settings_add_uint8(SETTINGS_KEY_TXPOWER,x)==SETTINGS_STATUS_OK) { + PRINTD("->Set EEPROM tx power of %d (0=max)\n",x); + } + } + return x; +} +#endif /* CONTIKI_CONF_SETTINGS_MANAGER */ diff --git a/platform/avr-atmega128rfa1/params.h b/platform/avr-atmega128rfa1/params.h new file mode 100644 index 000000000..7a5e8aaae --- /dev/null +++ b/platform/avr-atmega128rfa1/params.h @@ -0,0 +1,108 @@ +#ifndef __PARAMS_H__ +#define __PARAMS_H__ +/* PARAMETER_STORAGE = + * 0 Hard coded, minmal program and eeprom usage. + * 1 Stored in fixed eeprom locations, rewritten from flash if corrupt. + * This allows parameter changes using a hardware programmer or custom application code. + * Corruption test is based on channel verify so get the channel before anything else! + * 2 Obtained from eeprom using the general settings manager and read from program flash if not present. + * Useful for for testing builds without wearing out flash memory. + * 3 Obtained from eeprom using the settings manager and rewritten from flash if not present. + * This ensures all parameters are present in upper eeprom flash. + * + * Note the parameters in this file can be changed without forcing a complete rebuild. + */ +#define CONTIKI_CONF_RANDOM_MAC 0 //adds 78 bytes +#define CONTIKI_CONF_SETTINGS_MANAGER 0 //adds 1696 bytes + +#if CONTIKI_CONF_SETTINGS_MANAGER +//#define PARAMETER_STORAGE 2 +#define PARAMETER_STORAGE 2 +#else +#define PARAMETER_STORAGE 1 +#endif + +/* Include settings.h, then dummy out the write routines */ +#include "settings.h" +#if PARAMETER_STORAGE==2 +#define settings_add(...) 0 +#define settings_add_uint8(...) 0 +#define settings_add_uint16(...) 0 +#endif + +#if AVR_WEBSERVER +/* Webserver builds can set some defaults in httpd-fsdata.c via makefsdata.h */ +extern uint8_t eemem_mac_address[8]; +extern uint8_t eemem_server_name[16]; +extern uint8_t eemem_domain_name[30]; +#endif + +#ifdef SERVER_NAME +#define PARAMS_SERVERNAME SERVER_NAME +#else +#define PARAMS_SERVERNAME "ATMEGA128rfa1" +#endif +#ifdef DOMAIN_NAME +#define PARAMS_DOMAINNAME DOMAIN_NAME +#else +#define PARAMS_DOMAINNAME "localhost" +#endif +#ifdef NODE_ID +#define PARAMS_NODEID NODE_ID +#else +#define PARAMS_NODEID 0 +#endif +#ifdef CHANNEL_802_15_4 +#define PARAMS_CHANNEL CHANNEL_802_15_4 +#else +#define PARAMS_CHANNEL 26 +#endif +#ifdef IEEE802154_PANID +#define PARAMS_PANID IEEE802154_PANID +#else +#define PARAMS_PANID 0xABCD +#endif +#ifdef IEEE802154_PANADDR +#define PARAMS_PANADDR IEEE802154_PANADDR +#else +#define PARAMS_PANADDR 0 +#endif +#ifdef RF230_MAX_TX_POWER +#define PARAMS_TXPOWER RF230_MAX_TX_POWER +#else +#define PARAMS_TXPOWER 0 +#endif +#ifdef EUI64_ADDRESS +#define PARAMS_EUI64ADDR EUI64_ADDRESS +#else +/* This form of of EUI64 mac allows full 6LoWPAN header compression from mac address */ +#if UIP_CONF_LL_802154 +//#define PARAMS_EUI64ADDR {0x02, 0xNN, 0xNN, 0xNN, 0xNN, 0xNN, 0xNN, 0xNN} +#define PARAMS_EUI64ADDR {0x02, 0x00, 0x00, 0xff, 0xfe, 0x00, 0x00, 0x01} +#else +//#define PARAMS_EUI64ADDR {0x02, 0xNN, 0xNN, 0xff, 0xfe, 0xNN, 0xNN, 0xNN} +#define PARAMS_EUI64ADDR {0x00, 0x00, 0x00, 0xff, 0xfe, 0x00, 0x00, 0x01} +#endif +/* This form of of EUI64 mac allows 16 bit 6LoWPAN header compression on multihops */ +//#define PARAMS_EUI64ADDR {0x02, 0x00, 0x00, 0xff, 0xfe, 0x00, 0xNN, 0xNN} +#endif + +uint8_t params_get_eui64(uint8_t *eui64); +#if PARAMETER_STORAGE==0 +/* Hard coded program flash parameters */ +#define params_get_servername(...) +#define params_get_nodeid(...) PARAMS_NODEID +#define params_get_channel(...) PARAMS_CHANNEL +#define params_get_panid(...) PARAMS_PANID +#define params_get_panaddr(...) PARAMS_PANADDR +#define params_get_txpower(...) PARAMS_TXPOWER +#else +/* Parameters stored in eeprom */ +uint16_t params_get_nodeid(void); +uint8_t params_get_channel(void); +uint16_t params_get_panid(void); +uint16_t params_get_panaddr(void); +uint8_t params_get_txpower(void); +#endif + +#endif /* __PARAMS_H__ */ From 470887eddd4d4835057d482a7e9daa54619ab03b Mon Sep 17 00:00:00 2001 From: David Kopf Date: Wed, 31 Aug 2011 11:50:14 -0400 Subject: [PATCH 3/6] Print elf size if ELF_SIZE is defined --- cpu/avr/Makefile.avr | 1 + examples/webserver-ipv6/Makefile | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/cpu/avr/Makefile.avr b/cpu/avr/Makefile.avr index a98c8e44d..8a94b9eb0 100644 --- a/cpu/avr/Makefile.avr +++ b/cpu/avr/Makefile.avr @@ -105,6 +105,7 @@ CC = avr-gcc LD = avr-gcc AS = avr-as AR = avr-ar +ELF_SIZE = avr-size -C --mcu=$(MCU) OBJCOPY = avr-objcopy STRIP = avr-strip CFLAGSNO = -Wall -mmcu=$(MCU) -gdwarf-2 -fno-strict-aliasing \ diff --git a/examples/webserver-ipv6/Makefile b/examples/webserver-ipv6/Makefile index 549656638..c87a5e85b 100644 --- a/examples/webserver-ipv6/Makefile +++ b/examples/webserver-ipv6/Makefile @@ -25,8 +25,12 @@ endif #copy output to e.g. webserver-nano.sky, raven-webserver.avr-raven $(WITH_WEBSERVER) : $(CONTIKI_PROJECT) cp $(CONTIKI_PROJECT).$(TARGET) $(WITH_WEBSERVER).$(TARGET) + @if (test -n "$(ELF_SIZE)");then $(ELF_SIZE) $(WITH_WEBSERVER).$(TARGET);fi else APPS=webserver + +all : $(CONTIKI_PROJECT) + @if (test -n "$(ELF_SIZE)");then $(ELF_SIZE) $(CONTIKI_PROJECT).$(TARGET);fi endif UIP_CONF_IPV6=1 From 407c76c5df687ad755f1debf4fbc158d4bbcbfa6 Mon Sep 17 00:00:00 2001 From: David Kopf Date: Wed, 31 Aug 2011 11:52:55 -0400 Subject: [PATCH 4/6] output buffer unused at present, save some RAM --- examples/ipv6/rpl-border-router/httpd-simple.h | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/examples/ipv6/rpl-border-router/httpd-simple.h b/examples/ipv6/rpl-border-router/httpd-simple.h index 5fda299e8..4a2efa30f 100644 --- a/examples/ipv6/rpl-border-router/httpd-simple.h +++ b/examples/ipv6/rpl-border-router/httpd-simple.h @@ -26,7 +26,6 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: httpd-simple.h,v 1.1 2010/05/09 12:52:05 nifi Exp $ */ /** @@ -43,8 +42,10 @@ #include "contiki-net.h" +/* The current internal border router webserver ignores the requested file name */ +/* and needs no per-connection output buffer, so save some RAM */ #ifndef WEBSERVER_CONF_CFS_PATHLEN -#define HTTPD_PATHLEN 80 +#define HTTPD_PATHLEN 2 #else /* WEBSERVER_CONF_CFS_CONNS */ #define HTTPD_PATHLEN WEBSERVER_CONF_CFS_PATHLEN #endif /* WEBSERVER_CONF_CFS_CONNS */ @@ -56,8 +57,8 @@ struct httpd_state { struct timer timer; struct psock sin, sout; struct pt outputpt; - char inputbuf[HTTPD_PATHLEN + 30]; - char outputbuf[UIP_TCP_MSS]; + char inputbuf[HTTPD_PATHLEN + 24]; +/*char outputbuf[UIP_TCP_MSS]; */ char filename[HTTPD_PATHLEN]; httpd_simple_script_t script; char state; From dfdae5b02b84ebf7588d1b5fe0918885e22b8267 Mon Sep 17 00:00:00 2001 From: David Kopf Date: Wed, 31 Aug 2011 12:08:01 -0400 Subject: [PATCH 5/6] This fixes commit 9a1ce7 for spaces instead of .... --- cpu/avr/radio/rf230bb/rf230bb.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cpu/avr/radio/rf230bb/rf230bb.c b/cpu/avr/radio/rf230bb/rf230bb.c index 9af14387e..0ac1f91d8 100644 --- a/cpu/avr/radio/rf230bb/rf230bb.c +++ b/cpu/avr/radio/rf230bb/rf230bb.c @@ -985,13 +985,13 @@ rf230_transmit(unsigned short payload_len) DEBUGFLOW('m'); RIMESTATS_ADD(contentiondrop); PRINTF("rf230_transmit: Transmission never started\n"); -....tx_result = RADIO_TX_COLLISION; + tx_result = RADIO_TX_COLLISION; } else if (tx_result==5) { //Expected ACK, none received DEBUGFLOW('n'); -....tx_result = RADIO_TX_NOACK; + tx_result = RADIO_TX_NOACK; } else if (tx_result==7) { //Invalid (Can't happen since waited for idle above?) DEBUGFLOW('o'); -....tx_result = RADIO_TX_ERR; + tx_result = RADIO_TX_ERR; } return tx_result; From 3520cc0bae66015568536b9417de513e3b628ac3 Mon Sep 17 00:00:00 2001 From: David Kopf Date: Wed, 31 Aug 2011 12:13:31 -0400 Subject: [PATCH 6/6] flash led on tcp get, fix pingbacks on rpl build, patch web data for params --- .../apps/raven-lcd-interface/raven-lcd.c | 6 +++++- .../apps/raven-webserver/httpd-fsdata.c | 13 +++++++++---- .../avr-atmega128rfa1/apps/raven-webserver/httpd.c | 4 ++++ 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/platform/avr-atmega128rfa1/apps/raven-lcd-interface/raven-lcd.c b/platform/avr-atmega128rfa1/apps/raven-lcd-interface/raven-lcd.c index 2d686024c..0ce1e9c6a 100644 --- a/platform/avr-atmega128rfa1/apps/raven-lcd-interface/raven-lcd.c +++ b/platform/avr-atmega128rfa1/apps/raven-lcd-interface/raven-lcd.c @@ -101,7 +101,11 @@ void rs232_send(uint8_t port, unsigned char c); void raven_ping6(void) { -#define PING_GOOGLE 0 +#if UIP_CONF_IPV6_RPL||1 +/* No default router, so pick on someone else */ +#define PING_GOOGLE 1 +seqno++; +#endif UIP_IP_BUF->vtc = 0x60; UIP_IP_BUF->tcflow = 1; diff --git a/platform/avr-atmega128rfa1/apps/raven-webserver/httpd-fsdata.c b/platform/avr-atmega128rfa1/apps/raven-webserver/httpd-fsdata.c index 47b714f6b..4480bb838 100644 --- a/platform/avr-atmega128rfa1/apps/raven-webserver/httpd-fsdata.c +++ b/platform/avr-atmega128rfa1/apps/raven-webserver/httpd-fsdata.c @@ -2,10 +2,15 @@ #include -/* Link layer ipv6 address will become fe80::2 */ -uint8_t mac_address[8] EEMEM = {0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02}; -uint8_t server_name[16] EEMEM = "huginn"; -uint8_t domain_name[30] EEMEM = "localhost"; + +/* Link layer ipv6 address will become fe80::ff:fe:1 */ +uint8_t default_mac_address[8] PROGMEM = {0x02, 0x00, 0x00, 0xff, 0xfe, 0x00, 0x00, 0x01}; +uint8_t default_server_name[16] PROGMEM = "ATMEGA128rfa1"; +uint8_t default_domain_name[30] PROGMEM = "localhost"; +uint8_t eemem_mac_address[8] EEMEM = {0x02, 0x00, 0x00, 0xff, 0xfe, 0x00, 0x00, 0x01}; +uint8_t eemem_server_name[16] EEMEM = "ATMEGA128rfa1"; +uint8_t eemem_domain_name[30] EEMEM = "localhost"; + const char data_404_html[140] PROGMEM = { /* /404.html */ diff --git a/platform/avr-atmega128rfa1/apps/raven-webserver/httpd.c b/platform/avr-atmega128rfa1/apps/raven-webserver/httpd.c index 17a1032ff..af2f1cd35 100644 --- a/platform/avr-atmega128rfa1/apps/raven-webserver/httpd.c +++ b/platform/avr-atmega128rfa1/apps/raven-webserver/httpd.c @@ -459,6 +459,10 @@ httpd_appcall(void *state) if (1) { #else struct httpd_state *s = (struct httpd_state *)state; +#if RF230BB_CONF_LEDONPORTE1 + extern uint16_t ledtimer; + PORTE|=(1<