32 #define PRINTF(FORMAT,args...) printf_P(PSTR(FORMAT),##args)
34 #define ANNOUNCE_BOOT 1 //adds about 600 bytes to program size
36 #define PRINTA(FORMAT,args...) printf_P(PSTR(FORMAT),##args)
45 #define PRINTD(FORMAT,args...) printf_P(PSTR(FORMAT),##args)
50 #include <avr/pgmspace.h>
52 #include <avr/eeprom.h>
55 #include <dev/watchdog.h>
57 #include "loader/symbols-def.h"
58 #include "loader/symtab.h"
67 #include "contiki-net.h"
68 #include "contiki-lib.h"
70 #include "dev/rs232.h"
74 #ifdef RAVEN_LCD_INTERFACE
75 #include "raven-lcd.h"
80 #include "httpd-cgi.h"
88 #if UIP_CONF_ROUTER&&0
89 #include "net/routing/rimeroute.h"
90 #include "net/rime/rime-udp.h"
98 uint8_t debugflowsize,debugflow[DEBUGFLOWSIZE];
99 #define DEBUGFLOW(c) if (debugflowsize<(DEBUGFLOWSIZE-1)) debugflow[debugflowsize++]=c
107 #define PERIODICPRINTS 1
112 #define STACKMONITOR 1024
116 uint8_t rtimerflag=1;
118 void rtimercycle(
void) {rtimerflag=1;}
138 #include <avr/signature.h>
141 typedef struct {
const unsigned char B2;
const unsigned char B1;
const unsigned char B0;} __signature_t;
142 #define SIGNATURE __signature_t __signature __attribute__((section (".signature")))
152 FUSES ={.low = 0xe2, .high = 0x99, .extended = 0xff,};
155 FUSES ={.low = 0xC2, .high = 0x99, .extended = 0xfe,};
159 rng_get_uint8(
void) {
163 j = (PHY_RSSI&0xc0) + ((PHY_RSSI>>2)&0x30) + ((PHY_RSSI>>4)&0x0c) + ((PHY_RSSI>>6)&0x03);
172 while (ADCSRA&(1<<ADSC));
177 PRINTD(
"rng issues %d\n",j);
183 void initialize(
void)
191 #if !RF230BB_CONF_LEDONPORTE1 //Conflicts with USART0
192 #ifdef RAVEN_LCD_INTERFACE
193 rs232_init(RS232_PORT_0, USART_BAUD_38400,USART_PARITY_NONE | USART_STOP_BITS_1 | USART_DATA_BITS_8);
197 rs232_init(RS232_PORT_0, USART_BAUD_38400,USART_PARITY_NONE | USART_STOP_BITS_1 | USART_DATA_BITS_8);
202 rs232_init(RS232_PORT_1, USART_BAUD_57600,USART_PARITY_NONE | USART_STOP_BITS_1 | USART_DATA_BITS_8);
204 #if RF230BB_CONF_LEDONPORTE1 || defined(RAVEN_LCD_INTERFACE)
205 rs232_redirect_stdout(RS232_PORT_1);
207 rs232_redirect_stdout(RS232_PORT_0);
211 if(MCUSR & (1<<PORF )) PRINTD(
"Power-on reset.\n");
212 if(MCUSR & (1<<EXTRF)) PRINTD(
"External reset!\n");
213 if(MCUSR & (1<<BORF )) PRINTD(
"Brownout reset!\n");
214 if(MCUSR & (1<<WDRF )) PRINTD(
"Watchdog reset!\n");
215 if(MCUSR & (1<<JTRF )) PRINTD(
"JTAG reset!\n");
223 extern uint16_t __bss_end;
224 uint16_t p=(uint16_t)&__bss_end;
226 *(uint16_t *)p = 0x4242;
232 #define CONF_CALIBRATE_OSCCAL 0
233 #if CONF_CALIBRATE_OSCCAL
236 extern uint8_t osccal_calibrated;
238 PRINTD(
"\nBefore calibration OSCCAL=%x\n",OSCCAL);
241 PRINTD(
"Calibrated=%x\n",osccal_calibrated);
250 PRINTA(
"\n*******Booting %s*******\n",CONTIKI_VERSION_STRING);
263 NETSTACK_RADIO.init();
279 rf230_set_channel(params_get_channel());
281 if (params_get_eui64(addr.u8)) {
282 PRINTA(
"Random EUI64 address generated\n");
288 PRINTA(
"Initial node_id %u\n",node_id);
293 addr.u8[3]=node_id&0xff;
294 addr.u8[4]=(node_id&0xff)>>8;
303 memcpy(&
uip_lladdr.addr, &addr.u8,
sizeof(linkaddr_t));
305 rf230_set_pan_addr(params_get_panid(),params_get_panaddr(),(uint8_t *)&addr.u8);
307 node_id=get_panaddr_from_eeprom();
308 addr.u8[1]=node_id&0xff;
309 addr.u8[0]=(node_id&0xff00)>>8;
310 PRINTA(
"Node ID from eeprom: %X\n",node_id);
311 uint16_t inv_node_id=((node_id&0xff00)>>8)+((node_id&0xff)<<8);
313 rf230_set_pan_addr(params_get_panid(),inv_node_id,
NULL);
316 rf230_set_pan_addr(params_get_panid(),params_get_panaddr(),(uint8_t *)&addr.u8);
320 rf230_set_pan_addr(params_get_panid(),params_get_panaddr(),(uint8_t *)&addr.u8);
324 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]);
326 PRINTA(
"MAC address ");
328 for (i=
sizeof(linkaddr_t); i>0; i--){
329 PRINTA(
"%x:",addr.u8[i-1]);
338 NETSTACK_NETWORK.init();
341 PRINTA(
"%s %s, channel %u , check rate %u Hz tx power %u\n",NETSTACK_MAC.name, NETSTACK_RDC.name, rf230_get_channel(),
342 CLOCK_SECOND / (NETSTACK_RDC.channel_check_interval() == 0 ? 1:NETSTACK_RDC.channel_check_interval()),
343 rf230_get_txpower());
344 #if UIP_CONF_IPV6_RPL
345 PRINTA(
"RPL Enabled\n");
348 PRINTA(
"Routing Enabled\n");
355 #ifdef RAVEN_LCD_INTERFACE
360 autostart_start(autostart_processes);
366 PRINTA(
"No index.html file found, creating upload.html!\n");
367 PRINTA(
"Formatting FLASH file system for coffee...");
371 int r = cfs_write(fa, &
"It works!", 9);
372 if (r<0) PRINTA(
"Can''t create /index.html!\n");
382 uip_ip6addr_t ipaddr;
384 uip_ds6_addr_add(&ipaddr, 0, ADDR_AUTOCONF);
396 for (i=0;i<UIP_DS6_ADDR_NB;i++) {
397 if (uip_ds6_if.addr_list[i].isused) {
398 httpd_cgi_sprint_ip6(uip_ds6_if.addr_list[i].ipaddr,buf);
399 PRINTA(
"IPv6 Address: %s\n",buf);
403 eeprom_read_block (buf,eemem_server_name,
sizeof(eemem_server_name));
405 buf[
sizeof(eemem_server_name)]=0;
408 eeprom_read_block (buf,eemem_domain_name,
sizeof(eemem_domain_name));
410 buf[
sizeof(eemem_domain_name)]=0;
411 size=httpd_fs_get_size();
413 PRINTA(
".%s online with fixed %u byte web content\n",buf,size);
414 #elif COFFEE_FILES==1
415 PRINTA(
".%s online with static %u byte EEPROM file system\n",buf,size);
416 #elif COFFEE_FILES==2
417 PRINTA(
".%s online with dynamic %u KB EEPROM file system\n",buf,size>>10);
418 #elif COFFEE_FILES==3
419 PRINTA(
".%s online with static %u byte program memory file system\n",buf,size);
420 #elif COFFEE_FILES==4
421 PRINTA(
".%s online with dynamic %u KB program memory file system\n",buf,size>>10);
429 #if RF230BB_CONF_LEDONPORTE1
436 #if ROUTES && UIP_CONF_IPV6
438 ipaddr_add(
const uip_ipaddr_t *addr)
442 for(i = 0, f = 0; i <
sizeof(uip_ipaddr_t); i += 2) {
443 a = (addr->u8[i] << 8) + addr->u8[i + 1];
444 if(a == 0 && f >= 0) {
445 if(f++ == 0) PRINTF(
"::");
476 #if RF230BB_CONF_LEDONPORTE1
479 #if defined(RAVEN_LCD_INTERFACE)&&0
481 extern void raven_ping6(
void);
496 NETSTACK_RDC.input();
503 extern uint8_t rf230_calibrated;
504 if (rf230_calibrated) {
505 PRINTD(
"\nRF230 calibrated!\n");
513 debugflow[debugflowsize]=0;
514 PRINTF(
"%s\n",debugflow);
534 if ((clocktime%STAMPS)==0) {
539 extern volatile unsigned long radioontime;
540 PRINTF(
"%u(%u)s\n",clocktime,radioontime);
542 PRINTF(
"%us\n",clocktime);
551 #if PINGS && UIP_CONF_IPV6
552 extern void raven_ping6(
void);
553 if ((clocktime%PINGS)==1) {
559 #if ROUTES && UIP_CONF_IPV6
560 if ((clocktime%ROUTES)==2) {
565 PRINTF(
"\nAddresses [%u max]\n",UIP_DS6_ADDR_NB);
566 for (i=0;i<UIP_DS6_ADDR_NB;i++) {
567 if (uip_ds6_if.addr_list[i].isused) {
568 ipaddr_add(&uip_ds6_if.addr_list[i].ipaddr);
572 PRINTF(
"\nNeighbors [%u max]\n",NBR_TABLE_MAX_NEIGHBORS);
574 for(nbr = nbr_table_head(ds6_neighbors);
576 nbr = nbr_table_next(ds6_neighbors, nbr)) {
577 ipaddr_add(&nbr->ipaddr);
581 if (j) PRINTF(
" <none>");
582 PRINTF(
"\nRoutes [%u max]\n",UIP_DS6_ROUTE_NB);
585 PRINTF(
"\nRoutes [%u max]\n",UIP_DS6_ROUTE_NB);
590 ipaddr_add(&r->ipaddr);
591 PRINTF(
"/%u (via ", r->length);
592 ipaddr_add(uip_ds6_route_nexthop(r));
593 PRINTF(
") %lus\n", r->state.lifetime);
597 if (j) PRINTF(
" <none>");
598 PRINTF(
"\n---------\n");
603 if ((clocktime%STACKMONITOR)==3) {
604 extern uint16_t __bss_end;
605 uint16_t p=(uint16_t)&__bss_end;
607 if (*(uint16_t *)p != 0x4242) {
608 PRINTF(
"Never-used stack > %d bytes\n",p-(uint16_t)&__bss_end);
612 }
while (p<RAMEND-10);
620 extern uint8_t rf230processflag;
621 if (rf230processflag) {
622 PRINTF(
"rf230p%d",rf230processflag);
628 extern uint8_t rf230_interrupt_flag;
629 if (rf230_interrupt_flag) {
631 PRINTF(
"**RI%u",rf230_interrupt_flag);
633 rf230_interrupt_flag=0;
642 void log_message(
char *m1,
char *m2)
644 PRINTF(
"%s%s\n", m1, m2);