Contiki 2.5
contiki-raven-main.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2006, Technical University of Munich
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  * @(#)$$
32  */
33 
34 /**
35  * \file
36  * Contiki 2.4 kernel for Jackdaw USB stick
37  *
38  * \author
39  * Simon Barner <barner@in.tum.de>
40  * David Kopf <dak664@embarqmail.com>
41  */
42 
43 #define DEBUG 0
44 #if DEBUG
45 #define PRINTD(FORMAT,args...) printf_P(PSTR(FORMAT),##args)
46 #else
47 #define PRINTD(...)
48 #endif
49 
50 #include <avr/pgmspace.h>
51 #include <avr/fuse.h>
52 #include <avr/eeprom.h>
53 #include <avr/wdt.h>
54 #include <util/delay.h>
55 #include <stdio.h>
56 #include <string.h>
57 
58 #include "lib/mmem.h"
59 #include "loader/symbols-def.h"
60 #include "loader/symtab.h"
61 
62 #include "contiki.h"
63 #include "contiki-net.h"
64 #include "contiki-lib.h"
65 #include "contiki-raven.h"
66 
67 /* Set ANNOUNCE to send boot messages to USB or RS232 serial port */
68 #define ANNOUNCE 1
69 
70 /* But only if a serial port exists */
71 #if USB_CONF_SERIAL||USB_CONF_RS232
72 #define PRINTA(FORMAT,args...) printf_P(PSTR(FORMAT),##args)
73 #else
74 #define PRINTA(...)
75 #endif
76 
77 #include "usb_task.h"
78 #if USB_CONF_SERIAL
79 #include "cdc_task.h"
80 #endif
81 #if USB_CONF_RS232
82 #include "dev/rs232.h"
83 #endif
84 
85 #include "rndis/rndis_task.h"
86 #if USB_CONF_STORAGE
87 #include "storage/storage_task.h"
88 #endif
89 
90 #include "dev/watchdog.h"
91 #include "dev/usb/usb_drv.h"
92 
93 #if JACKDAW_CONF_USE_SETTINGS
94 #include "settings.h"
95 #endif
96 
97 #if RF230BB //radio driver using contiki core mac
98 #include "radio/rf230bb/rf230bb.h"
99 #include "net/mac/frame802154.h"
100 #define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN])
101 rimeaddr_t macLongAddr;
102 #define tmp_addr macLongAddr
103 #else //legacy radio driver using Atmel/Cisco 802.15.4'ish MAC
104 #include <stdbool.h>
105 #include "mac.h"
106 #include "sicslowmac.h"
107 #include "sicslowpan.h"
108 #include "ieee-15-4-manager.h"
109 #endif /* RF230BB */
110 
111 /* Test rtimers, also useful for pings, time stamps, routes, stack monitor */
112 #define TESTRTIMER 0
113 #if TESTRTIMER
114 #define PINGS 0
115 #define STAMPS 60
116 #define ROUTES 120
117 #define STACKMONITOR 600
118 uint8_t rtimerflag=1;
119 uint16_t rtime;
120 struct rtimer rt;
121 void rtimercycle(void) {rtimerflag=1;}
122 #endif /* TESTRTIMER */
123 
124 #if UIP_CONF_IPV6_RPL
125 /*---------------------------------------------------------------------------*/
126 /*--------------------------------- RPL ----------------------------------*/
127 /*---------------------------------------------------------------------------*/
128 /* TODO: Put rpl code into another file, once it stabilizes */
129 /* Set up fallback interface links to direct stack tcpip output to ethernet */
130 static void
131 init(void)
132 {
133 }
134 void mac_LowpanToEthernet(void);
135 static void
136 output(void)
137 {
138 // if(uip_ipaddr_cmp(&last_sender, &UIP_IP_BUF->srcipaddr)) {
139  /* Do not bounce packets back over USB if the packet was received from USB */
140 // PRINTA("JACKDAW router: Destination off-link but no route\n");
141  // } else {
142  PRINTD("SUT: %u\n", uip_len);
143  mac_LowpanToEthernet(); //bounceback trap is done in lowpanToEthernet
144 // }
145 }
146 const struct uip_fallback_interface rpl_interface = {
147  init, output
148 };
149 
150 #if RPL_BORDER_ROUTER
151 #include "net/rpl/rpl.h"
152 
153 // avr-objdump --section .bss -x ravenusbstick.elf
154 uint16_t dag_id[] PROGMEM = {0x1111, 0x1100, 0, 0, 0, 0, 0, 0x0011};
155 
156 PROCESS(border_router_process, "RPL Border Router");
157 PROCESS_THREAD(border_router_process, ev, data)
158 {
159 
160  PROCESS_BEGIN();
161 
162  PROCESS_PAUSE();
163 
164 { rpl_dag_t *dag;
165  char buf[sizeof(dag_id)];
166  memcpy_P(buf,dag_id,sizeof(dag_id));
167  dag = rpl_set_root((uip_ip6addr_t *)buf);
168 
169 /* Assign separate addresses to the jackdaw uip stack and the host network interface, but with the same prefix */
170 /* E.g. bbbb::200 to the jackdaw and bbbb::1 to the host network interface with $ip -6 address add bbbb::1/64 dev usb0 */
171 /* Otherwise the host will trap packets intended for the jackdaw, just as the jackdaw will trap RF packets intended for the host */
172 /* $ifconfig usb0 -arp on Ubuntu to skip the neighbor solicitations. Add explicit neighbors on other OSs */
173  if(dag != NULL) {
174  PRINTD("created a new RPL dag\n");
175 
176 #if UIP_CONF_ROUTER_RECEIVE_RA
177 //Contiki stack will shut down until assigned an address from the interface RA
178 //Currently this requires changes in the core rpl-icmp6.c to pass the link-local RA broadcast
179 
180 #else
181  uip_ip6addr_t ipaddr;
182  uip_ip6addr(&ipaddr, 0xbbbb, 0, 0, 0, 0, 0, 0, 0x200);
183  uip_ds6_addr_add(&ipaddr, 0, ADDR_MANUAL);
184  rpl_set_prefix(dag, &ipaddr, 64);
185 #endif
186  }
187 }
188  /* The border router runs with a 100% duty cycle in order to ensure high
189  packet reception rates. */
190  // NETSTACK_MAC.off(1);
191 
192  while(1) {
193  PROCESS_YIELD();
194  /* Local and global dag repair can be done from the jackdaw menu */
195  // rpl_set_prefix(rpl_get_dag(RPL_ANY_INSTANCE), &ipaddr, 64);
196  // rpl_repair_dag(rpl_get_dag(RPL_ANY_INSTANCE));
197 
198  }
199 
200  PROCESS_END();
201 }
202 #endif /* RPL_BORDER_ROUTER */
203 
204 #endif /* UIP_CONF_IPV6_RPL */
205 
206 /*-------------------------------------------------------------------------*/
207 /*----------------------Configuration of the .elf file---------------------*/
208 #if 1
209 /* The proper way to set the signature is */
210 #include <avr/signature.h>
211 #else
212 /* Older avr-gcc's may not define the needed SIGNATURE bytes. Do it manually if you get an error */
213 typedef struct {const unsigned char B2;const unsigned char B1;const unsigned char B0;} __signature_t;
214 #define SIGNATURE __signature_t __signature __attribute__((section (".signature")))
215 SIGNATURE = {
216  .B2 = 0x82,//SIGNATURE_2, //AT90USB128x
217  .B1 = 0x97,//SIGNATURE_1, //128KB flash
218  .B0 = 0x1E,//SIGNATURE_0, //Atmel
219 };
220 #endif
221 
222 FUSES ={.low = 0xde, .high = 0x99, .extended = 0xff,};
223 
224 /* Save the default settings into program flash memory */
225 uint8_t default_mac_address[8] PROGMEM = {0x02, 0x12, 0x13, 0xff, 0xfe, 0x14, 0x15, 0x16};
226 #ifdef CHANNEL_802_15_4
227 uint8_t default_channel PROGMEM = CHANNEL_802_15_4;
228 #else
229 uint8_t default_channel PROGMEM = 26;
230 #endif
231 #ifdef IEEE802154_PANID
232 uint16_t default_panid PROGMEM = IEEE802154_PANID;
233 #else
234 uint16_t default_panid PROGMEM = 0xABCD;
235 #endif
236 #ifdef IEEE802154_PANADDR
237 uint16_t default_panaddr PROGMEM = IEEE802154_PANID;
238 #else
239 uint16_t default_panaddr PROGMEM = 0;
240 #endif
241 #ifdef RF230_MAX_TX_POWER
242 uint8_t default_txpower PROGMEM = RF230_MAX_TX_POWER;
243 #else
244 uint8_t default_txpower PROGMEM = 0;
245 #endif
246 
247 #if JACKDAW_CONF_RANDOM_MAC
248 #include "rng.h"
249 static void
250 generate_new_eui64(uint8_t eui64[8]) {
251  eui64[0] = 0x02;
252  eui64[1] = rng_get_uint8();
253  eui64[2] = rng_get_uint8();
254  eui64[3] = 0xFF;
255  eui64[4] = 0xFE;
256  eui64[5] = rng_get_uint8();
257  eui64[6] = rng_get_uint8();
258  eui64[7] = rng_get_uint8();
259 }
260 #endif /* JACKDAW_CONF_RANDOM_MAC */
261 
262 #if !JACKDAW_CONF_USE_SETTINGS
263 /****************************No settings manager*****************************/
264 /* If not using the settings manager, put the default values into EEMEM
265  * These can be manually changed and kept over program reflash.
266  * The channel and bit complement are used to check EEMEM integrity,
267  * If corrupt all values will be rewritten with the default flash values.
268  * To make this work, get the channel before anything else.
269  */
270 
271 uint8_t eemem_mac_address[8] EEMEM = {0x02, 0x12, 0x13, 0xff, 0xfe, 0x14, 0x15, 0x16};
272 #ifdef CHANNEL_802_15_4
273 uint8_t eemem_channel[2] EEMEM = {CHANNEL_802_15_4, ~CHANNEL_802_15_4};
274 #else
275 uint8_t eemem_channel[2] EMEM = {26, ~26};
276 #endif
277 #ifdef IEEE802154_PANID
278 uint16_t eemem_panid EEMEM = IEEE802154_PANID;
279 #else
280 uint16_t eemem_panid EEMEM = 0xABCD;
281 #endif
282 #ifdef IEEE802154_PANADDR
283 uint16_t eemem_panaddr EEMEM = IEEE802154_PANADDR;
284 #else
285 uint16_t eemem_panaddr EEMEM = 0;
286 #endif
287 #ifdef RF230_MAX_TX_POWER
288 uint8_t eemem_txpower EEMEM = RF230_MAX_TX_POWER;
289 #else
290 uint8_t eemem_txpower EEMEM = 0;
291 #endif
292 static uint8_t get_channel_from_eeprom() {
293  uint8_t x[2];
294  *(uint16_t *)x = eeprom_read_word ((uint16_t *)&eemem_channel);
295  if((uint8_t)x[0]!=(uint8_t)~x[1]) {//~x[1] can promote comparison to 16 bit
296 /* Verification fails, rewrite everything */
297  uint8_t mac[8];
298 #if JACKDAW_CONF_RANDOM_MAC
299  PRINTA("Generating random MAC address.\n");
300  generate_new_eui64(&mac);
301 #else
302  {uint8_t i; for (i=0;i<8;i++) mac[i] = pgm_read_byte_near(default_mac_address+i);}
303 #endif
304  eeprom_write_block(&mac, &eemem_mac_address, 8);
305  eeprom_write_word(&eemem_panid , pgm_read_word_near(&default_panid));
306  eeprom_write_word(&eemem_panaddr, pgm_read_word_near(&default_panaddr));
307  eeprom_write_byte(&eemem_txpower, pgm_read_byte_near(&default_txpower));
308  x[0] = pgm_read_byte_near(&default_channel);
309  x[1]= ~x[0];
310  eeprom_write_word((uint16_t *)&eemem_channel, *(uint16_t *)x);
311  }
312  return x[0];
313 }
314 static bool get_eui64_from_eeprom(uint8_t macptr[8]) {
315  eeprom_read_block ((void *)macptr, &eemem_mac_address, 8);
316  return macptr[0]!=0xFF;
317 }
318 static uint16_t get_panid_from_eeprom(void) {
319  return eeprom_read_word(&eemem_panid);
320 }
321 static uint16_t get_panaddr_from_eeprom(void) {
322  return eeprom_read_word (&eemem_panaddr);
323 }
324 static uint8_t get_txpower_from_eeprom(void)
325 {
326  return eeprom_read_byte(&eemem_txpower);
327 }
328 
329 #else /* !JACKDAW_CONF_USE_SETTINGS */
330 /******************************Settings manager******************************/
331 static uint8_t get_channel_from_eeprom() {
332  uint8_t x = settings_get_uint8(SETTINGS_KEY_CHANNEL, 0);
333  if(!x) x = pgm_read_byte_near(&default_channel);
334  return x;
335 }
336 static bool get_eui64_from_eeprom(uint8_t macptr[8]) {
337  size_t size = 8;
338  if(settings_get(SETTINGS_KEY_EUI64, 0, (unsigned char*)macptr, &size)==SETTINGS_STATUS_OK) {
339  PRINTD("<=Get EEPROM MAC address.\n");
340  return true;
341  }
342 #if JACKDAW_CONF_RANDOM_MAC
343  PRINTA("--Generating random MAC address.\n");
344  generate_new_eui64(macptr);
345 #else
346  {uint8_t i;for (i=0;i<8;i++) macptr[i] = pgm_read_byte_near(default_mac_address+i);}
347 #endif
348  settings_add(SETTINGS_KEY_EUI64,(unsigned char*)macptr,8);
349  PRINTA("->Set EEPROM MAC address.\n");
350  return true;
351 }
352 static uint16_t get_panid_from_eeprom(void) {
353  uint16_t x;
354  if (settings_check(SETTINGS_KEY_PAN_ID,0)) {
355  x = settings_get_uint16(SETTINGS_KEY_PAN_ID,0);
356  PRINTD("<-Get EEPROM PAN ID of %04x.\n",x);
357  } else {
358  x=pgm_read_word_near(&default_panid);
359  if (settings_add_uint16(SETTINGS_KEY_PAN_ID,x)==SETTINGS_STATUS_OK) {
360  PRINTA("->Set EEPROM PAN ID to %04x.\n",x);
361  }
362  }
363  return x;
364 }
365 static uint16_t get_panaddr_from_eeprom(void) {
366  uint16_t x;
367  if (settings_check(SETTINGS_KEY_PAN_ADDR,0)) {
368  x = settings_get_uint16(SETTINGS_KEY_PAN_ADDR,0);
369  PRINTD("<-Get EEPROM PAN address of %04x.\n",x);
370  } else {
371  x=pgm_read_word_near(&default_panaddr);
372  if (settings_add_uint16(SETTINGS_KEY_PAN_ADDR,x)==SETTINGS_STATUS_OK) {
373  PRINTA("->Set EEPROM PAN address to %04x.\n",x);
374  }
375  }
376  return x;
377 }
378 static uint8_t get_txpower_from_eeprom(void) {
379  uint8_t x;
380  if (settings_check(SETTINGS_KEY_TXPOWER,0)) {
381  x = settings_get_uint8(SETTINGS_KEY_TXPOWER,0);
382  PRINTD("<-Get EEPROM tx power of %d. (0=max)\n",x);
383  } else {
384  x=pgm_read_byte_near(&default_txpower);
385  if (settings_add_uint8(SETTINGS_KEY_TXPOWER,x)==SETTINGS_STATUS_OK) {
386  PRINTA("->Set EEPROM tx power of %d. (0=max)\n",x);
387  }
388  }
389  return x;
390 }
391 #endif /* !JACKDAW_CONF_USE_SETTINGS */
392 
393 /*-------------------------------------------------------------------------*/
394 /*-----------------------------Low level initialization--------------------*/
395 static void initialize(void) {
396 
397  watchdog_init();
398  watchdog_start();
399 
400 #if CONFIG_STACK_MONITOR
401  /* Simple stack pointer highwater monitor. The 'm' command in cdc_task.c
402  * looks for the first overwritten magic number.
403  */
404 {
405 extern uint16_t __bss_end;
406 uint16_t p=(uint16_t)&__bss_end;
407  do {
408  *(uint16_t *)p = 0x4242;
409  p+=100;
410  } while (p<SP-100); //don't overwrite our own stack
411 }
412 #endif
413 
414  /* Initialize hardware */
415  // Checks for "finger", jumps to DFU if present.
416  init_lowlevel();
417 
418  /* Clock */
419  clock_init();
420 
421  /* Leds are referred to by number to prevent any possible confusion :) */
422  /* Led0 Blue Led1 Red Led2 Green Led3 Yellow */
423  Leds_init();
424  Led1_on();
425 
426 /* Get a random (or probably different) seed for the 802.15.4 packet sequence number.
427  * Some layers will ignore duplicates found in a history (e.g. Contikimac)
428  * causing the initial packets to be ignored after a short-cycle restart.
429  */
430  ADMUX =0x1E; //Select AREF as reference, measure 1.1 volt bandgap reference.
431  ADCSRA=1<<ADEN; //Enable ADC, not free running, interrupt disabled, fastest clock
432  ADCSRA|=1<<ADSC; //Start conversion
433  while (ADCSRA&(1<<ADSC)); //Wait till done
434  PRINTD("ADC=%d\n",ADC);
435  random_init(ADC);
436  ADCSRA=0; //Disable ADC
437 
438 #if USB_CONF_RS232
439  /* Use rs232 port for serial out (tx, rx, gnd are the three pads behind jackdaw leds */
440  rs232_init(RS232_PORT_0, USART_BAUD_57600,USART_PARITY_NONE | USART_STOP_BITS_1 | USART_DATA_BITS_8);
441  /* Redirect stdout to second port */
442  rs232_redirect_stdout(RS232_PORT_0);
443 #if ANNOUNCE
444  PRINTA("\n\n*******Booting %s*******\n",CONTIKI_VERSION_STRING);
445 #endif
446 #endif
447 
448  /* rtimer init needed for low power protocols */
449  rtimer_init();
450 
451  /* Process subsystem. */
452  process_init();
453 
454  /* etimer process must be started before USB or ctimer init */
455  process_start(&etimer_process, NULL);
456 
457  Led2_on();
458  /* Now we can start USB enumeration */
459  process_start(&usb_process, NULL);
460 
461  /* Start CDC enumeration, bearing in mind that it may fail */
462  /* Hopefully we'll get a stdout for startup messages, if we don't already */
463 #if USB_CONF_SERIAL
464  process_start(&cdc_process, NULL);
465 {unsigned short i;
466  for (i=0;i<65535;i++) {
467  process_run();
468  watchdog_periodic();
469  if (stdout) break;
470  }
471 #if !USB_CONF_RS232
472  PRINTA("\n\n*******Booting %s*******\n",CONTIKI_VERSION_STRING);
473 #endif
474 }
475 #endif
476  if (!stdout) Led3_on();
477 
478 #if RF230BB
479 #if JACKDAW_CONF_USE_SETTINGS
480  PRINTA("Settings manager will be used.\n");
481 #else
482 {uint8_t x[2];
483  *(uint16_t *)x = eeprom_read_word((uint16_t *)&eemem_channel);
484  if((uint8_t)x[0]!=(uint8_t)~x[1]) {
485  PRINTA("Invalid EEPROM settings detected. Rewriting with default values.\n");
486  get_channel_from_eeprom();
487  }
488 }
489 #endif
490 
491  ctimer_init();
492  /* Start radio and radio receive process */
493  /* Note this starts RF230 process, so must be done after process_init */
494  NETSTACK_RADIO.init();
495 
496  /* Set addresses BEFORE starting tcpip process */
497 
498  memset(&tmp_addr, 0, sizeof(rimeaddr_t));
499 
500  if(get_eui64_from_eeprom(tmp_addr.u8));
501 
502  //Fix MAC address
503  init_net();
504 
505 #if UIP_CONF_IPV6
506  memcpy(&uip_lladdr.addr, &tmp_addr.u8, 8);
507 #endif
508 
509  rf230_set_pan_addr(
510  get_panid_from_eeprom(),
511  get_panaddr_from_eeprom(),
512  (uint8_t *)&tmp_addr.u8
513  );
514 
515  rf230_set_channel(get_channel_from_eeprom());
516  rf230_set_txpower(get_txpower_from_eeprom());
517 
518  rimeaddr_set_node_addr(&tmp_addr);
519 
520  /* Initialize stack protocols */
521  queuebuf_init();
522  NETSTACK_RDC.init();
523  NETSTACK_MAC.init();
524  NETSTACK_NETWORK.init();
525 
526 #if ANNOUNCE
527  PRINTA("MAC address %x:%x:%x:%x:%x:%x:%x:%x\n\r",tmp_addr.u8[0],tmp_addr.u8[1],tmp_addr.u8[2],tmp_addr.u8[3],tmp_addr.u8[4],tmp_addr.u8[5],tmp_addr.u8[6],tmp_addr.u8[7]);
528  PRINTA("%s %s, channel %u",NETSTACK_MAC.name, NETSTACK_RDC.name,rf230_get_channel());
529  if (NETSTACK_RDC.channel_check_interval) {
530  unsigned short tmp;
531  tmp=CLOCK_SECOND / (NETSTACK_RDC.channel_check_interval == 0 ? 1:\
532  NETSTACK_RDC.channel_check_interval());
533  if (tmp<65535) PRINTA(", check rate %u Hz",tmp);
534  }
535  PRINTA("\n");
536 #endif
537 
538 #if UIP_CONF_IPV6_RPL
539 #if RPL_BORDER_ROUTER
540  process_start(&tcpip_process, NULL);
541  process_start(&border_router_process, NULL);
542  PRINTD ("RPL Border Router Started\n");
543 #else
544  process_start(&tcpip_process, NULL);
545  PRINTD ("RPL Started\n");
546 #endif
547 #if RPL_HTTPD_SERVER
548  extern struct process httpd_process;
549  process_start(&httpd_process, NULL);
550  PRINTD ("Webserver Started\n");
551 #endif
552 #endif /* UIP_CONF_IPV6_RPL */
553 
554 #else /* RF230BB */
555 /* The order of starting these is important! */
556  process_start(&mac_process, NULL);
557  process_start(&tcpip_process, NULL);
558 #endif /* RF230BB */
559 
560  /* Start ethernet network and storage process */
561  process_start(&usb_eth_process, NULL);
562 #if USB_CONF_STORAGE
563  process_start(&storage_process, NULL);
564 #endif
565 
566  /* Autostart other processes */
567  /* There are none in the default build so autostart_processes will be unresolved in the link. */
568  /* The AUTOSTART_PROCESSES macro which defines it can only be used in the .co module. */
569  /* See /examples/ravenusbstick/ravenusb.c for an autostart template. */
570 #if 0
571  autostart_start(autostart_processes);
572 #endif
573 
574 #if ANNOUNCE
575 #if USB_CONF_RS232
576  PRINTA("Online.\n");
577 #else
578  PRINTA("Online. Type ? for Jackdaw menu.\n");
579 #endif
580 #endif
581 
582 Leds_off();
583 }
584 
585 /*-------------------------------------------------------------------------*/
586 /*---------------------------------Main Routine----------------------------*/
587 int
588 main(void)
589 {
590  /* GCC depends on register r1 set to 0 (?) */
591  asm volatile ("clr r1");
592 
593  /* Initialize in a subroutine to maximize stack space */
594  initialize();
595 
596 #if DEBUG
597 {struct process *p;
598  for(p = PROCESS_LIST();p != NULL; p = ((struct process *)p->next)) {
599  PRINTA("Process=%p Thread=%p Name=\"%s\" \n",p,p->thread,PROCESS_NAME_STRING(p));
600  }
601 }
602 #endif
603 
604  while(1) {
605  process_run();
606 
607  watchdog_periodic();
608 
609 /* Print rssi of all received packets, useful for range testing */
610 #ifdef RF230_MIN_RX_POWER
611  uint8_t lastprint;
612  if (rf230_last_rssi != lastprint) { //can be set in halbb.c interrupt routine
613  PRINTA("%u ",rf230_last_rssi);
614  lastprint=rf230_last_rssi;
615  }
616 #endif
617 
618 #if 0
619 /* Clock.c can trigger a periodic PLL calibration in the RF230BB driver.
620  * This can show when that happens.
621  */
622  extern uint8_t rf230_calibrated;
623  if (rf230_calibrated) {
624  PRINTA("\nRF230 calibrated!\n");
625  rf230_calibrated=0;
626  }
627 #endif
628 
629 #if TESTRTIMER
630 /* Timeout can be increased up to 8 seconds maximum.
631  * A one second cycle is convenient for triggering the various debug printouts.
632  * The triggers are staggered to avoid printing everything at once.
633  * My Jackdaw is 4% slow.
634  */
635  if (rtimerflag) {
636  rtimer_set(&rt, RTIMER_NOW()+ RTIMER_ARCH_SECOND*1UL, 1,(void *) rtimercycle, NULL);
637  rtimerflag=0;
638 
639 #if STAMPS
640 if ((rtime%STAMPS)==0) {
641  PRINTA("%us ",rtime);
642  if (rtime%STAMPS*10) PRINTA("\n");
643 }
644 #endif
645  rtime+=1;
646 
647 #if PINGS && UIP_CONF_IPV6_RPL
648 extern void raven_ping6(void);
649 if ((rtime%PINGS)==1) {
650  PRINTA("**Ping\n");
651  raven_ping6();
652 }
653 #endif
654 
655 #if ROUTES && UIP_CONF_IPV6_RPL
656 if ((rtime%ROUTES)==2) {
657 
660 extern uip_ds6_netif_t uip_ds6_if;
661 
662  uint8_t i,j;
663  PRINTA("\nAddresses [%u max]\n",UIP_DS6_ADDR_NB);
664  for (i=0;i<UIP_DS6_ADDR_NB;i++) {
665  if (uip_ds6_if.addr_list[i].isused) {
666  uip_debug_ipaddr_print(&uip_ds6_if.addr_list[i].ipaddr);
667  PRINTA("\n");
668  }
669  }
670  PRINTA("\nNeighbors [%u max]\n",UIP_DS6_NBR_NB);
671  for(i = 0,j=1; i < UIP_DS6_NBR_NB; i++) {
672  if(uip_ds6_nbr_cache[i].isused) {
673  uip_debug_ipaddr_print(&uip_ds6_nbr_cache[i].ipaddr);
674  PRINTA("\n");
675  j=0;
676  }
677  }
678  if (j) PRINTA(" <none>");
679  PRINTA("\nRoutes [%u max]\n",UIP_DS6_ROUTE_NB);
680  for(i = 0,j=1; i < UIP_DS6_ROUTE_NB; i++) {
681  if(uip_ds6_routing_table[i].isused) {
682  uip_debug_ipaddr_print(&uip_ds6_routing_table[i].ipaddr);
683  PRINTA("/%u (via ", uip_ds6_routing_table[i].length);
684  uip_debug_ipaddr_print(&uip_ds6_routing_table[i].nexthop);
685  // if(uip_ds6_routing_table[i].state.lifetime < 600) {
686  PRINTA(") %lus\n", uip_ds6_routing_table[i].state.lifetime);
687  // } else {
688  // PRINTA(")\n");
689  // }
690  j=0;
691  }
692  }
693  if (j) PRINTA(" <none>");
694  PRINTA("\n---------\n");
695 }
696 #endif
697 
698 #if STACKMONITOR && CONFIG_STACK_MONITOR
699 if ((rtime%STACKMONITOR)==3) {
700  extern uint16_t __bss_end;
701  uint16_t p=(uint16_t)&__bss_end;
702  do {
703  if (*(uint16_t *)p != 0x4242) {
704  PRINTA("Never-used stack > %d bytes\n",p-(uint16_t)&__bss_end);
705  break;
706  }
707  p+=100;
708  } while (p<RAMEND-10);
709 }
710 #endif
711 
712  }
713 #endif /* TESTRTIMER */
714 
715 //Use with RF230BB DEBUGFLOW to show path through driver
716 #if RF230BB&&0
717 extern uint8_t debugflowsize,debugflow[]; //in rf230bb.c
718  if (debugflowsize) {
719  debugflow[debugflowsize]=0;
720  PRINTA("%s",debugflow);
721  debugflowsize=0;
722  }
723 #endif
724 
725  }
726  return 0;
727 }