Contiki 3.x
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 linkaddr_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 const 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(RPL_DEFAULT_INSTANCE,(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 
196  }
197 
198  PROCESS_END();
199 }
200 #endif /* RPL_BORDER_ROUTER */
201 
202 #endif /* UIP_CONF_IPV6_RPL */
203 
204 /*-------------------------------------------------------------------------*/
205 /*----------------------Configuration of the .elf file---------------------*/
206 #if 1
207 /* The proper way to set the signature is */
208 #include <avr/signature.h>
209 #else
210 /* Older avr-gcc's may not define the needed SIGNATURE bytes. Do it manually if you get an error */
211 typedef struct {const unsigned char B2;const unsigned char B1;const unsigned char B0;} __signature_t;
212 #define SIGNATURE __signature_t __signature __attribute__((section (".signature")))
213 SIGNATURE = {
214  .B2 = 0x82,//SIGNATURE_2, //AT90USB128x
215  .B1 = 0x97,//SIGNATURE_1, //128KB flash
216  .B0 = 0x1E,//SIGNATURE_0, //Atmel
217 };
218 #endif
219 
220 FUSES ={.low = 0xde, .high = 0x99, .extended = 0xff,};
221 
222 /* Save the default settings into program flash memory */
223 const uint8_t default_mac_address[8] PROGMEM = {0x02, 0x12, 0x13, 0xff, 0xfe, 0x14, 0x15, 0x16};
224 #ifdef CHANNEL_802_15_4
225 const uint8_t default_channel PROGMEM = CHANNEL_802_15_4;
226 #else
227 const uint8_t default_channel PROGMEM = 26;
228 #endif
229 #ifdef IEEE802154_PANID
230 const uint16_t default_panid PROGMEM = IEEE802154_PANID;
231 #else
232 const uint16_t default_panid PROGMEM = 0xABCD;
233 #endif
234 #ifdef IEEE802154_PANADDR
235 const uint16_t default_panaddr PROGMEM = IEEE802154_PANID;
236 #else
237 const uint16_t default_panaddr PROGMEM = 0;
238 #endif
239 #ifdef RF230_MAX_TX_POWER
240 const uint8_t default_txpower PROGMEM = RF230_MAX_TX_POWER;
241 #else
242 const uint8_t default_txpower PROGMEM = 0;
243 #endif
244 
245 #if JACKDAW_CONF_RANDOM_MAC
246 #include "rng.h"
247 static void
248 generate_new_eui64(uint8_t eui64[8]) {
249  eui64[0] = 0x02;
250  eui64[1] = rng_get_uint8();
251  eui64[2] = rng_get_uint8();
252  eui64[3] = 0xFF;
253  eui64[4] = 0xFE;
254  eui64[5] = rng_get_uint8();
255  eui64[6] = rng_get_uint8();
256  eui64[7] = rng_get_uint8();
257 }
258 #endif /* JACKDAW_CONF_RANDOM_MAC */
259 
260 #if !JACKDAW_CONF_USE_SETTINGS
261 /****************************No settings manager*****************************/
262 /* If not using the settings manager, put the default values into EEMEM
263  * These can be manually changed and kept over program reflash.
264  * The channel and bit complement are used to check EEMEM integrity,
265  * If corrupt all values will be rewritten with the default flash values.
266  * To make this work, get the channel before anything else.
267  */
268 
269 uint8_t eemem_mac_address[8] EEMEM = {0x02, 0x12, 0x13, 0xff, 0xfe, 0x14, 0x15, 0x16};
270 #ifdef CHANNEL_802_15_4
271 uint8_t eemem_channel[2] EEMEM = {CHANNEL_802_15_4, ~CHANNEL_802_15_4};
272 #else
273 uint8_t eemem_channel[2] EMEM = {26, ~26};
274 #endif
275 #ifdef IEEE802154_PANID
276 uint16_t eemem_panid EEMEM = IEEE802154_PANID;
277 #else
278 uint16_t eemem_panid EEMEM = 0xABCD;
279 #endif
280 #ifdef IEEE802154_PANADDR
281 uint16_t eemem_panaddr EEMEM = IEEE802154_PANADDR;
282 #else
283 uint16_t eemem_panaddr EEMEM = 0;
284 #endif
285 #ifdef RF230_MAX_TX_POWER
286 uint8_t eemem_txpower EEMEM = RF230_MAX_TX_POWER;
287 #else
288 uint8_t eemem_txpower EEMEM = 0;
289 #endif
290 static uint8_t get_channel_from_eeprom() {
291  uint8_t x[2];
292  *(uint16_t *)x = eeprom_read_word ((uint16_t *)&eemem_channel);
293  if((uint8_t)x[0]!=(uint8_t)~x[1]) {//~x[1] can promote comparison to 16 bit
294 /* Verification fails, rewrite everything */
295  uint8_t mac[8];
296 #if JACKDAW_CONF_RANDOM_MAC
297  PRINTA("Generating random MAC address.\n");
298  generate_new_eui64(&mac);
299 #else
300  {uint8_t i; for (i=0;i<8;i++) mac[i] = pgm_read_byte_near(default_mac_address+i);}
301 #endif
302  eeprom_write_block(&mac, &eemem_mac_address, 8);
303  eeprom_write_word(&eemem_panid , pgm_read_word_near(&default_panid));
304  eeprom_write_word(&eemem_panaddr, pgm_read_word_near(&default_panaddr));
305  eeprom_write_byte(&eemem_txpower, pgm_read_byte_near(&default_txpower));
306  x[0] = pgm_read_byte_near(&default_channel);
307  x[1]= ~x[0];
308  eeprom_write_word((uint16_t *)&eemem_channel, *(uint16_t *)x);
309  }
310  return x[0];
311 }
312 static bool get_eui64_from_eeprom(uint8_t macptr[8]) {
313  eeprom_read_block ((void *)macptr, &eemem_mac_address, 8);
314  return macptr[0]!=0xFF;
315 }
316 static uint16_t get_panid_from_eeprom(void) {
317  return eeprom_read_word(&eemem_panid);
318 }
319 static uint16_t get_panaddr_from_eeprom(void) {
320  return eeprom_read_word (&eemem_panaddr);
321 }
322 static uint8_t get_txpower_from_eeprom(void)
323 {
324  return eeprom_read_byte(&eemem_txpower);
325 }
326 
327 #else /* !JACKDAW_CONF_USE_SETTINGS */
328 /******************************Settings manager******************************/
329 static uint8_t get_channel_from_eeprom() {
330  uint8_t x = settings_get_uint8(SETTINGS_KEY_CHANNEL, 0);
331  if(!x) x = pgm_read_byte_near(&default_channel);
332  return x;
333 }
334 static bool get_eui64_from_eeprom(uint8_t macptr[8]) {
335  size_t size = 8;
336  if(settings_get(SETTINGS_KEY_EUI64, 0, (unsigned char*)macptr, &size)==SETTINGS_STATUS_OK) {
337  PRINTD("<=Get EEPROM MAC address.\n");
338  return true;
339  }
340 #if JACKDAW_CONF_RANDOM_MAC
341  PRINTA("--Generating random MAC address.\n");
342  generate_new_eui64(macptr);
343 #else
344  {uint8_t i;for (i=0;i<8;i++) macptr[i] = pgm_read_byte_near(default_mac_address+i);}
345 #endif
346  settings_add(SETTINGS_KEY_EUI64,(unsigned char*)macptr,8);
347  PRINTA("->Set EEPROM MAC address.\n");
348  return true;
349 }
350 static uint16_t get_panid_from_eeprom(void) {
351  uint16_t x;
353  x = settings_get_uint16(SETTINGS_KEY_PAN_ID,0);
354  PRINTD("<-Get EEPROM PAN ID of %04x.\n",x);
355  } else {
356  x=pgm_read_word_near(&default_panid);
357  if (settings_add_uint16(SETTINGS_KEY_PAN_ID,x)==SETTINGS_STATUS_OK) {
358  PRINTA("->Set EEPROM PAN ID to %04x.\n",x);
359  }
360  }
361  return x;
362 }
363 static uint16_t get_panaddr_from_eeprom(void) {
364  uint16_t x;
366  x = settings_get_uint16(SETTINGS_KEY_PAN_ADDR,0);
367  PRINTD("<-Get EEPROM PAN address of %04x.\n",x);
368  } else {
369  x=pgm_read_word_near(&default_panaddr);
370  if (settings_add_uint16(SETTINGS_KEY_PAN_ADDR,x)==SETTINGS_STATUS_OK) {
371  PRINTA("->Set EEPROM PAN address to %04x.\n",x);
372  }
373  }
374  return x;
375 }
376 static uint8_t get_txpower_from_eeprom(void) {
377  uint8_t x;
379  x = settings_get_uint8(SETTINGS_KEY_TXPOWER,0);
380  PRINTD("<-Get EEPROM tx power of %d. (0=max)\n",x);
381  } else {
382  x=pgm_read_byte_near(&default_txpower);
383  if (settings_add_uint8(SETTINGS_KEY_TXPOWER,x)==SETTINGS_STATUS_OK) {
384  PRINTA("->Set EEPROM tx power of %d. (0=max)\n",x);
385  }
386  }
387  return x;
388 }
389 #endif /* !JACKDAW_CONF_USE_SETTINGS */
390 
391 /*-------------------------------------------------------------------------*/
392 /*-----------------------------Low level initialization--------------------*/
393 static void initialize(void) {
394 
395  watchdog_init();
396  watchdog_start();
397 
398 #if CONFIG_STACK_MONITOR
399  /* Simple stack pointer highwater monitor. The 'm' command in cdc_task.c
400  * looks for the first overwritten magic number.
401  */
402 {
403 extern uint16_t __bss_end;
404 uint16_t p=(uint16_t)&__bss_end;
405  do {
406  *(uint16_t *)p = 0x4242;
407  p+=100;
408  } while (p<SP-100); //don't overwrite our own stack
409 }
410 #endif
411 
412  /* Initialize hardware */
413  // Checks for "finger", jumps to DFU if present.
414  init_lowlevel();
415 
416  /* Clock */
417  clock_init();
418 
419  /* Leds are referred to by number to prevent any possible confusion :) */
420  /* Led0 Blue Led1 Red Led2 Green Led3 Yellow */
421  Leds_init();
422  Led1_on();
423 
424 /* Get a random (or probably different) seed for the 802.15.4 packet sequence number.
425  * Some layers will ignore duplicates found in a history (e.g. Contikimac)
426  * causing the initial packets to be ignored after a short-cycle restart.
427  */
428  ADMUX =0x1E; //Select AREF as reference, measure 1.1 volt bandgap reference.
429  ADCSRA=1<<ADEN; //Enable ADC, not free running, interrupt disabled, fastest clock
430  ADCSRA|=1<<ADSC; //Start conversion
431  while (ADCSRA&(1<<ADSC)); //Wait till done
432  PRINTD("ADC=%d\n",ADC);
433  random_init(ADC);
434  ADCSRA=0; //Disable ADC
435 
436 #if USB_CONF_RS232
437  /* Use rs232 port for serial out (tx, rx, gnd are the three pads behind jackdaw leds */
438  rs232_init(RS232_PORT_0, USART_BAUD_57600,USART_PARITY_NONE | USART_STOP_BITS_1 | USART_DATA_BITS_8);
439  /* Redirect stdout to second port */
440  rs232_redirect_stdout(RS232_PORT_0);
441 #if ANNOUNCE
442  PRINTA("\n\n*******Booting %s*******\n",CONTIKI_VERSION_STRING);
443 #endif
444 #endif
445 
446  /* rtimer init needed for low power protocols */
447  rtimer_init();
448 
449  /* Process subsystem. */
450  process_init();
451 
452  /* etimer process must be started before USB or ctimer init */
453  process_start(&etimer_process, NULL);
454 
455  Led2_on();
456  /* Now we can start USB enumeration */
457  process_start(&usb_process, NULL);
458 
459  /* Start CDC enumeration, bearing in mind that it may fail */
460  /* Hopefully we'll get a stdout for startup messages, if we don't already */
461 #if USB_CONF_SERIAL
462  process_start(&cdc_process, NULL);
463 {unsigned short i;
464  for (i=0;i<65535;i++) {
465  process_run();
467  if (stdout) break;
468  }
469 #if !USB_CONF_RS232
470  PRINTA("\n\n*******Booting %s*******\n",CONTIKI_VERSION_STRING);
471 #endif
472 }
473 #endif
474  if (!stdout) Led3_on();
475 
476 #if RF230BB
477 #if JACKDAW_CONF_USE_SETTINGS
478  PRINTA("Settings manager will be used.\n");
479 #else
480 {uint8_t x[2];
481  *(uint16_t *)x = eeprom_read_word((uint16_t *)&eemem_channel);
482  if((uint8_t)x[0]!=(uint8_t)~x[1]) {
483  PRINTA("Invalid EEPROM settings detected. Rewriting with default values.\n");
484  get_channel_from_eeprom();
485  }
486 }
487 #endif
488 
489  ctimer_init();
490  /* Start radio and radio receive process */
491  /* Note this starts RF230 process, so must be done after process_init */
492  NETSTACK_RADIO.init();
493 
494  /* Set addresses BEFORE starting tcpip process */
495 
496  memset(&tmp_addr, 0, sizeof(linkaddr_t));
497 
498  if(get_eui64_from_eeprom(tmp_addr.u8));
499 
500  //Fix MAC address
501  init_net();
502 
503 #if UIP_CONF_IPV6
504  memcpy(&uip_lladdr.addr, &tmp_addr.u8, 8);
505 #endif
506 
507  rf230_set_pan_addr(
508  get_panid_from_eeprom(),
509  get_panaddr_from_eeprom(),
510  (uint8_t *)&tmp_addr.u8
511  );
512 
513  rf230_set_channel(get_channel_from_eeprom());
514  rf230_set_txpower(get_txpower_from_eeprom());
515 
516  linkaddr_set_node_addr(&tmp_addr);
517 
518  /* Initialize stack protocols */
519  queuebuf_init();
520  NETSTACK_RDC.init();
521  NETSTACK_MAC.init();
522  NETSTACK_NETWORK.init();
523 
524 #if ANNOUNCE
525  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]);
526  PRINTA("%s %s, channel %u",NETSTACK_MAC.name, NETSTACK_RDC.name,rf230_get_channel());
527  if (NETSTACK_RDC.channel_check_interval) {
528  unsigned short tmp;
529  tmp=CLOCK_SECOND / (NETSTACK_RDC.channel_check_interval == 0 ? 1:\
530  NETSTACK_RDC.channel_check_interval());
531  if (tmp<65535) PRINTA(", check rate %u Hz",tmp);
532  }
533  PRINTA("\n");
534 #endif
535 
536 #if UIP_CONF_IPV6_RPL
537 #if RPL_BORDER_ROUTER
538  process_start(&tcpip_process, NULL);
539  process_start(&border_router_process, NULL);
540  PRINTD ("RPL Border Router Started\n");
541 #else
542  process_start(&tcpip_process, NULL);
543  PRINTD ("RPL Started\n");
544 #endif
545 #if RPL_HTTPD_SERVER
546  extern struct process httpd_process;
547  process_start(&httpd_process, NULL);
548  PRINTD ("Webserver Started\n");
549 #endif
550 #endif /* UIP_CONF_IPV6_RPL */
551 
552 #else /* RF230BB */
553 /* The order of starting these is important! */
554  process_start(&mac_process, NULL);
555  process_start(&tcpip_process, NULL);
556 #endif /* RF230BB */
557 
558  /* Start ethernet network and storage process */
559  process_start(&usb_eth_process, NULL);
560 #if USB_CONF_STORAGE
561  process_start(&storage_process, NULL);
562 #endif
563 
564  /* Autostart other processes */
565  /* There are none in the default build so autostart_processes will be unresolved in the link. */
566  /* The AUTOSTART_PROCESSES macro which defines it can only be used in the .co module. */
567  /* See /examples/ravenusbstick/ravenusb.c for an autostart template. */
568 #if 0
569  autostart_start(autostart_processes);
570 #endif
571 
572 #if ANNOUNCE
573 #if USB_CONF_RS232
574  PRINTA("Online.\n");
575 #else
576  PRINTA("Online. Type ? for Jackdaw menu.\n");
577 #endif
578 #endif
579 
580 Leds_off();
581 }
582 
583 /*-------------------------------------------------------------------------*/
584 /*---------------------------------Main Routine----------------------------*/
585 int
586 main(void)
587 {
588  /* GCC depends on register r1 set to 0 (?) */
589  asm volatile ("clr r1");
590 
591  /* Initialize in a subroutine to maximize stack space */
592  initialize();
593 
594 #if DEBUG
595 {struct process *p;
596  for(p = PROCESS_LIST();p != NULL; p = ((struct process *)p->next)) {
597  PRINTA("Process=%p Thread=%p Name=\"%s\" \n",p,p->thread,PROCESS_NAME_STRING(p));
598  }
599 }
600 #endif
601 
602  while(1) {
603  process_run();
604 
606 
607 /* Print rssi of all received packets, useful for range testing */
608 #ifdef RF230_MIN_RX_POWER
609  uint8_t lastprint;
610  if (rf230_last_rssi != lastprint) { //can be set in halbb.c interrupt routine
611  PRINTA("%u ",rf230_last_rssi);
612  lastprint=rf230_last_rssi;
613  }
614 #endif
615 
616 #if 0
617 /* Clock.c can trigger a periodic PLL calibration in the RF230BB driver.
618  * This can show when that happens.
619  */
620  extern uint8_t rf230_calibrated;
621  if (rf230_calibrated) {
622  PRINTA("\nRF230 calibrated!\n");
623  rf230_calibrated=0;
624  }
625 #endif
626 
627 #if TESTRTIMER
628 /* Timeout can be increased up to 8 seconds maximum.
629  * A one second cycle is convenient for triggering the various debug printouts.
630  * The triggers are staggered to avoid printing everything at once.
631  * My Jackdaw is 4% slow.
632  */
633  if (rtimerflag) {
634  rtimer_set(&rt, RTIMER_NOW()+ RTIMER_ARCH_SECOND*1UL, 1,(void *) rtimercycle, NULL);
635  rtimerflag=0;
636 
637 #if STAMPS
638 if ((rtime%STAMPS)==0) {
639  PRINTA("%us ",rtime);
640  if (rtime%STAMPS*10) PRINTA("\n");
641 }
642 #endif
643  rtime+=1;
644 
645 #if PINGS && UIP_CONF_IPV6_RPL
646 extern void raven_ping6(void);
647 if ((rtime%PINGS)==1) {
648  PRINTA("**Ping\n");
649  raven_ping6();
650 }
651 #endif
652 
653 #if ROUTES && UIP_CONF_IPV6_RPL
654 if ((rtime%ROUTES)==2) {
655 
656 extern uip_ds6_netif_t uip_ds6_if;
657 
658  uint8_t i,j;
659  uip_ds6_nbr_t *nbr;
660 
661  PRINTA("\nAddresses [%u max]\n",UIP_DS6_ADDR_NB);
662  for (i=0;i<UIP_DS6_ADDR_NB;i++) {
663  if (uip_ds6_if.addr_list[i].isused) {
664  uip_debug_ipaddr_print(&uip_ds6_if.addr_list[i].ipaddr);
665  PRINTA("\n");
666  }
667  }
668  PRINTA("\nNeighbors [%u max]\n",NBR_TABLE_MAX_NEIGHBORS);
669 
670  for(nbr = nbr_table_head(ds6_neighbors);
671  nbr != NULL;
672  nbr = nbr_table_next(ds6_neighbors, nbr)) {
673  uip_debug_ipaddr_print(&nbr->ipaddr);
674  PRINTA("\n");
675  j=0;
676  }
677  if (j) PRINTA(" <none>");
678  PRINTA("\nRoutes [%u max]\n",UIP_DS6_ROUTE_NB);
679  uip_ds6_route_t *r;
680  for(r = uip_ds6_route_head();
681  r != NULL;
682  r = uip_ds6_route_next(r)) {
683  if(r->isused) {
684  uip_debug_ipaddr_print(&r->ipaddr);
685  PRINTA("/%u (via ", r->length);
686  uip_debug_ipaddr_print(uip_ds6_route_nexthop(r));
687  // if(r->state.lifetime < 600) {
688  PRINTA(") %lus\n", r->state.lifetime);
689  // } else {
690  // PRINTA(")\n");
691  // }
692  j=0;
693  }
694  }
695  if (j) PRINTA(" <none>");
696  PRINTA("\n---------\n");
697 }
698 #endif
699 
700 #if STACKMONITOR && CONFIG_STACK_MONITOR
701 if ((rtime%STACKMONITOR)==3) {
702  extern uint16_t __bss_end;
703  uint16_t p=(uint16_t)&__bss_end;
704  do {
705  if (*(uint16_t *)p != 0x4242) {
706  PRINTA("Never-used stack > %d bytes\n",p-(uint16_t)&__bss_end);
707  break;
708  }
709  p+=100;
710  } while (p<RAMEND-10);
711 }
712 #endif
713 
714  }
715 #endif /* TESTRTIMER */
716 
717 //Use with RF230BB DEBUGFLOW to show path through driver
718 #if RF230BB&&0
719 extern uint8_t debugflowsize,debugflow[]; //in rf230bb.c
720  if (debugflowsize) {
721  debugflow[debugflowsize]=0;
722  PRINTA("%s",debugflow);
723  debugflowsize=0;
724  }
725 #endif
726 
727  }
728  return 0;
729 }
void rs232_init(void)
Initialize the RS232 module.
Definition: rs232.c:50
settings_status_t settings_get(settings_key_t key, uint8_t index, uint8_t *value, settings_length_t *value_size)
Fetches the value associated with the given key.
uip_len
The length of the packet in the uip_buf buffer.
Definition: tcp_loader.c:75
An entry in the routing table.
#define SETTINGS_KEY_CHANNEL
Definition: settings.h:119
CCIF uip_lladdr_t uip_lladdr
Host L2 address.
Definition: uip6.c:115
802.15.4 frame creation and parsing functions
This file manages the USB task either device/host or both.
#define PROCESS_BEGIN()
Define the beginning of a process.
Definition: process.h:120
void watchdog_start(void)
Starts the WDT in watchdog mode if enabled by user configuration, maximum interval.
Definition: watchdog.c:49
Header file for the managed memory allocator
int rtimer_set(struct rtimer *rtimer, rtimer_clock_t time, rtimer_clock_t duration, rtimer_callback_t func, void *ptr)
Post a real-time task.
Definition: rtimer.c:67
void rtimer_init(void)
Initialize the real-time scheduler.
Definition: rtimer.c:61
uint8_t settings_check(settings_key_t key, uint8_t index)
Checks to see if the given key exists.
Example glue code between the existing MAC code and the Contiki mac interface.
This file contains the USB driver routines.
#define NULL
The null pointer.
void clock_init(void)
Initialize the clock library.
Definition: clock.c:76
#define SETTINGS_KEY_TXPOWER
Definition: settings.h:120
int main(void)
This is main...
Definition: ethconfig.c:49
uint64_t macLongAddr
Our own long address.
Definition: mac.c:145
void process_init(void)
Initialize the process module.
Definition: process.c:208
const uint32_t OIDSupportedList[] PROGMEM
List of supported RNDIS OID&#39;s.
Definition: rndis.c:96
#define PROCESS_THREAD(name, ev, data)
Define the body of a process.
Definition: process.h:273
#define PROCESS_END()
Define the end of a process.
Definition: process.h:131
Header file for the 6lowpan implementation (RFC4944 and draft-hui-6lowpan-hc-01) ...
void random_init(unsigned short seed)
Seed the cc2430 random number generator.
Definition: random.c:41
Settings Manager.
This file manages the RNDIS task.
void watchdog_periodic(void)
Writes the WDT clear sequence.
Definition: watchdog.c:64
settings_status_t settings_add(settings_key_t key, const uint8_t *value, settings_length_t value_size)
Adds the given key-value pair to the end of the settings store.
void ctimer_init(void)
Initialize the callback timer library.
Definition: ctimer.c:91
#define PROCESS_PAUSE()
Yield the process for a short while.
Definition: process.h:221
#define SETTINGS_KEY_EUI64
EUI64 Address, 8 bytes.
Definition: settings.h:117
#define PROCESS(name, strname)
Declare a process.
Definition: process.h:307
This file manages the CDC task for the virtual COM port.
void process_start(struct process *p, process_data_t data)
Start a process.
Definition: process.c:99
#define RTIMER_NOW()
Get the current clock time.
Definition: rtimer.h:133
Interface structure (contains all the interface variables)
Definition: uip-ds6.h:212
#define PROCESS_YIELD()
Yield the currently running process.
Definition: process.h:164
int process_run(void)
Run the system once - call poll handlers and process one event.
Definition: process.c:302
#define SETTINGS_KEY_PAN_ID
Definition: settings.h:121
void linkaddr_set_node_addr(linkaddr_t *t)
Set the address of the current node.
Definition: linkaddr.c:72
void mac_LowpanToEthernet(void)
Take a packet received over the 802.15.4 link, and send it out over ethernet, performing any translat...
Representation of a real-time task.
Definition: rtimer.h:84
#define uip_ip6addr(addr, addr0, addr1, addr2, addr3, addr4, addr5, addr6, addr7)
Construct an IPv6 address from eight 16-bit words.
Definition: uip.h:969
void watchdog_init(void)
Copyright (c) 2014, Analog Devices, Inc.
Definition: watchdog.c:42
MAC driver header file
#define SETTINGS_KEY_PAN_ADDR
Definition: settings.h:122
#define CLOCK_SECOND
A second, measured in system clock time.
Definition: clock.h:82
An entry in the nbr cache.
Definition: uip-ds6-nbr.h:70