/* * Copyright (c) 2006, Technical University of Munich * 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 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 0 #if DEBUG #define PRINTD(FORMAT,args...) printf_P(PSTR(FORMAT),##args) #else #define PRINTD(...) #endif #include #include #include #include #include #include #include "loader/symbols-def.h" #include "loader/symtab.h" #include "params.h" #include "radio/rf230bb/rf230bb.h" #include "net/mac/frame802154.h" #include "net/mac/framer-802154.h" #include "contiki.h" #include "contiki-net.h" #include "contiki-lib.h" #include "dev/rs232.h" #include "dev/serial-line.h" #include "dev/slip.h" #ifdef RAVEN_LCD_INTERFACE #include "raven-lcd.h" #endif #if AVR_WEBSERVER #include "httpd-fs.h" #include "httpd-cgi.h" #endif #ifdef COFFEE_FILES #include "cfs/cfs.h" #include "cfs/cfs-coffee.h" #endif #if UIP_CONF_ROUTER&&0 #include "net/routing/rimeroute.h" #include "net/rime/rime-udp.h" #endif #include "net/rime.h" /* Track interrupt flow through mac, rdc and radio driver */ //#define DEBUGFLOWSIZE 32 #if DEBUGFLOWSIZE uint8_t debugflowsize,debugflow[DEBUGFLOWSIZE]; #define DEBUGFLOW(c) if (debugflowsize<(DEBUGFLOWSIZE-1)) debugflow[debugflowsize++]=c #else #define DEBUGFLOW(c) #endif /* 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 600 #define STAMPS 60 #define STACKMONITOR 1024 uint32_t clocktime; #define TESTRTIMER 0 #if TESTRTIMER uint8_t rtimerflag=1; struct rtimer rt; void rtimercycle(void) {rtimerflag=1;} #endif #endif uint16_t ledtimer; /*-------------------------------------------------------------------------*/ /*----------------------Configuration of the .elf file---------------------*/ #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 = { .B2 = 0x01,//SIGNATURE_2, //ATMEGA128rfa1 .B1 = 0xA7,//SIGNATURE_1, //128KB flash .B0 = 0x1E,//SIGNATURE_0, //Atmel }; #endif #if 1 /* JTAG+SPI enabled, External osc 1kck4ms1 , Boot 4096 words @ $1F000, TXC1K+4,1msec delay, Brownout 1.9 volts */ FUSES ={.low = 0xF6, .high = 0x98, .extended = 0xfd,}; #define BOOTLOADER_START = 0x1F000 #else /* 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 #include "lib/sensors.h" #include "dev/button-sensor.h" #include "dev/battery-sensor.h" #include "dev/pir-sensor.h" SENSORS(&button_sensor, &battery_sensor, &pir_sensor); 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&0xc0) + ((PHY_RSSI>>2)&0x30) + ((PHY_RSSI>>4)&0x0c) + ((PHY_RSSI>>6)&0x03); #else /* Get a pseudo random number using the ADC */ uint8_t i,j; ADCSRA=1<>4); } /*-------------------------Low level initialization------------------------*/ /*------Done in a subroutine to keep main routine stack usage small--------*/ void initialize(void) { watchdog_init(); watchdog_start(); /* The Raven implements a serial command and data interface via uart0 to a 3290p, * which could be duplicated using another host computer. */ #if !RF230BB_CONF_LEDONPORTE1 //Conflicts with USART0 #ifdef RAVEN_LCD_INTERFACE rs232_init(RS232_PORT_0, USART_BAUD_38400,USART_PARITY_NONE | USART_STOP_BITS_1 | USART_DATA_BITS_8); rs232_set_input(0,raven_lcd_serial_input); #else /* Generic or slip connection on uart0 */ rs232_init(RS232_PORT_0, USART_BAUD_38400,USART_PARITY_NONE | USART_STOP_BITS_1 | USART_DATA_BITS_8); #endif #endif /* Second rs232 port for debugging or slip alternative */ rs232_init(RS232_PORT_1, USART_BAUD_57600,USART_PARITY_NONE | USART_STOP_BITS_1 | USART_DATA_BITS_8); /* Redirect stdout */ #if RF230BB_CONF_LEDONPORTE1 || defined(RAVEN_LCD_INTERFACE) rs232_redirect_stdout(RS232_PORT_1); #else rs232_redirect_stdout(RS232_PORT_0); #endif clock_init(); if(MCUSR & (1< //#define delay_us( us ) ( _delay_loop_2(1+(us*F_CPU)/4000000UL) ) // delay_us(50000); } clock_init(); } #endif PRINTA("\n*******Booting %s*******\n",CONTIKI_VERSION_STRING); /* rtimers needed for radio cycling */ rtimer_init(); /* Initialize process subsystem */ process_init(); /* etimers must be started before ctimer_init */ process_start(&etimer_process, NULL); ctimer_init(); /* Start radio and radio receive process */ NETSTACK_RADIO.init(); /* 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; if (params_get_eui64(addr.u8)) { PRINTA("Random EUI64 address generated\n"); } #if UIP_CONF_IPV6 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 rimeaddr_set_node_addr(&addr); PRINTA("Panid:%u\n", params_get_panid()); framer_802154_set_panid(params_get_panid()); 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(); NETSTACK_RDC.init(); NETSTACK_MAC.init(); NETSTACK_NETWORK.init(); #if ANNOUNCE_BOOT PRINTA("%s %s, channel %u , check rate %u Hz tx power %u\n",NETSTACK_MAC.name, NETSTACK_RDC.name, rf230_get_channel(), CLOCK_SECOND / (NETSTACK_RDC.channel_check_interval() == 0 ? 1:NETSTACK_RDC.channel_check_interval()), rf230_get_txpower()); #if UIP_CONF_IPV6_RPL PRINTA("RPL Enabled\n"); #endif #if UIP_CONF_ROUTER PRINTA("Routing Enabled\n"); #endif #endif /* ANNOUNCE_BOOT */ process_start(&tcpip_process, NULL); #ifdef RAVEN_LCD_INTERFACE process_start(&raven_lcd_process, NULL); #endif process_start(&sensors_process, NULL); /* Autostart other processes */ autostart_start(autostart_processes); /*---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 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) PRINTA("Can''t create /index.html!\n"); cfs_close(fa); // fa = cfs_open("upload.html"), CFW_WRITE); //
} #endif /* COFFEE_FILES */ /* Add addresses for testing */ #if 0 { uip_ip6addr_t ipaddr; uip_ip6addr(&ipaddr, 0xaaaa, 0, 0, 0, 0, 0, 0, 0); uip_ds6_addr_add(&ipaddr, 0, ADDR_AUTOCONF); // uip_ds6_prefix_add(&ipaddr,64,0); } #endif /*--------------------------Announce the configuration---------------------*/ #if ANNOUNCE_BOOT #if AVR_WEBSERVER { uint8_t i; char buf[80]; unsigned int size; for (i=0;i>10); #elif COFFEE_FILES==3 PRINTA(".%s online with static %u byte program memory file system\n",buf,size); #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 #endif /* ANNOUNCE_BOOT */ #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----------------------------*/ /*-------------------------------------------------------------------------*/ int main(void) { initialize(); while(1) { process_run(); watchdog_periodic(); /* Turn off LED after a while */ if (ledtimer) { if (--ledtimer==0) { #if RF230BB_CONF_LEDONPORTE1 PORTE&=~(1<"); PRINTF("\nRoutes [%u max]\n",UIP_DS6_ROUTE_NB); { uip_ds6_route_t *r; PRINTF("\nRoutes [%u max]\n",UIP_DS6_ROUTE_NB); j = 1; for(r = uip_ds6_route_list_head(); r != NULL; r = list_item_next(r)) { ipaddr_add(&r->ipaddr); PRINTF("/%u (via ", r->length); ipaddr_add(&r->nexthop); // if(uip_ds6_routing_table[i].state.lifetime < 600) { PRINTF(") %lus\n", r->state.lifetime); // } else { // PRINTF(")\n"); // } j = 0; } } if (j) PRINTF(" "); PRINTF("\n---------\n"); } #endif #if STACKMONITOR if ((clocktime%STACKMONITOR)==3) { extern uint16_t __bss_end; uint16_t p=(uint16_t)&__bss_end; do { if (*(uint16_t *)p != 0x4242) { PRINTF("Never-used stack > %d bytes\n",p-(uint16_t)&__bss_end); break; } p+=10; } while (p