Contiki 3.x
contiki-z1-main.c
1 /*
2  * Copyright (c) 2006, Swedish Institute of Computer Science
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  */
30 
31 #include <stdio.h>
32 #include <string.h>
33 #include <stdarg.h>
34 
35 #include "contiki.h"
36 #include "cc2420.h"
37 #include "dev/leds.h"
38 #include "dev/serial-line.h"
39 #include "dev/slip.h"
40 #include "dev/uart0.h"
41 #include "dev/watchdog.h"
42 #include "dev/xmem.h"
43 #include "lib/random.h"
44 #include "net/netstack.h"
45 #include "net/mac/frame802154.h"
46 #include "dev/button-sensor.h"
47 #include "dev/adxl345.h"
48 #include "sys/clock.h"
49 
50 #if NETSTACK_CONF_WITH_IPV6
51 #include "net/ipv6/uip-ds6.h"
52 #endif /* NETSTACK_CONF_WITH_IPV6 */
53 
54 #include "net/rime/rime.h"
55 
56 #include "sys/node-id.h"
57 #include "cfs-coffee-arch.h"
58 #include "cfs/cfs-coffee.h"
59 #include "sys/autostart.h"
60 
61 #include "dev/battery-sensor.h"
62 #include "dev/button-sensor.h"
63 #include "dev/sht11/sht11-sensor.h"
64 
65 SENSORS(&button_sensor);
66 
67 extern unsigned char node_mac[8];
68 
69 #if DCOSYNCH_CONF_ENABLED
70 static struct timer mgt_timer;
71 #endif
72 
73 #ifndef NETSTACK_CONF_WITH_IPV4
74 #define NETSTACK_CONF_WITH_IPV4 0
75 #endif
76 
77 #if NETSTACK_CONF_WITH_IPV4
78 #include "net/ip/uip.h"
79 #include "net/ipv4/uip-fw.h"
80 #include "net/ipv4/uip-fw-drv.h"
81 #include "net/ipv4/uip-over-mesh.h"
82 static struct uip_fw_netif slipif =
83 { UIP_FW_NETIF(192, 168, 1, 2, 255, 255, 255, 255, slip_send) };
84 static struct uip_fw_netif meshif =
85 { UIP_FW_NETIF(172, 16, 0, 0, 255, 255, 0, 0, uip_over_mesh_send) };
86 
87 #endif /* NETSTACK_CONF_WITH_IPV4 */
88 
89 #define UIP_OVER_MESH_CHANNEL 8
90 #if NETSTACK_CONF_WITH_IPV4
91 static uint8_t is_gateway;
92 #endif /* NETSTACK_CONF_WITH_IPV4 */
93 
94 #ifdef EXPERIMENT_SETUP
95 #include "experiment-setup.h"
96 #endif
97 
98 #define DEBUG 1
99 #if DEBUG
100 #include <stdio.h>
101 #define PRINTF(...) printf(__VA_ARGS__)
102 #else
103 #define PRINTF(...)
104 #endif
105 
106 void init_platform(void);
107 
108 /*---------------------------------------------------------------------------*/
109 #if 0
110 int
111 force_float_inclusion()
112 {
113  extern int __fixsfsi;
114  extern int __floatsisf;
115  extern int __mulsf3;
116  extern int __subsf3;
117 
118  return __fixsfsi + __floatsisf + __mulsf3 + __subsf3;
119 }
120 #endif
121 /*---------------------------------------------------------------------------*/
122 void
123 uip_log(char *msg)
124 {
125  puts(msg);
126 }
127 /*---------------------------------------------------------------------------*/
128 #if 0
129 void
130 force_inclusion(int d1, int d2)
131 {
132  snprintf(NULL, 0, "%d", d1 % d2);
133 }
134 #endif
135 /*---------------------------------------------------------------------------*/
136 static void
137 set_rime_addr(void)
138 {
139  linkaddr_t addr;
140  int i;
141 
142  memset(&addr, 0, sizeof(linkaddr_t));
143 #if NETSTACK_CONF_WITH_IPV6
144  memcpy(addr.u8, node_mac, sizeof(addr.u8));
145 #else
146  if(node_id == 0) {
147  for(i = 0; i < sizeof(linkaddr_t); ++i) {
148  addr.u8[i] = node_mac[7 - i];
149  }
150  } else {
151  addr.u8[0] = node_id & 0xff;
152  addr.u8[1] = node_id >> 8;
153  }
154 #endif
155  linkaddr_set_node_addr(&addr);
156  printf("Rime started with address ");
157  for(i = 0; i < sizeof(addr.u8) - 1; i++) {
158  printf("%d.", addr.u8[i]);
159  }
160  printf("%d\n", addr.u8[i]);
161 }
162 /*---------------------------------------------------------------------------*/
163 static void
164 print_processes(struct process *const processes[])
165 {
166  /* const struct process * const * p = processes;*/
167  printf("Starting");
168  while(*processes != NULL) {
169  printf(" '%s'", (*processes)->name);
170  processes++;
171  }
172  putchar('\n');
173 }
174 /*--------------------------------------------------------------------------*/
175 #if NETSTACK_CONF_WITH_IPV4
176 static void
177 set_gateway(void)
178 {
179  if(!is_gateway) {
180  leds_on(LEDS_RED);
181  printf("%d.%d: making myself the IP network gateway.\n\n",
183  printf("IPv4 address of the gateway: %d.%d.%d.%d\n\n",
184  uip_ipaddr_to_quad(&uip_hostaddr));
185  uip_over_mesh_set_gateway(&linkaddr_node_addr);
186  uip_over_mesh_make_announced_gateway();
187  is_gateway = 1;
188  }
189 }
190 #endif /* NETSTACK_CONF_WITH_IPV4 */
191 /*---------------------------------------------------------------------------*/
192 int
193 main(int argc, char **argv)
194 {
195  /*
196  * Initalize hardware.
197  */
198  msp430_cpu_init();
199  clock_init();
200  leds_init();
201  leds_on(LEDS_RED);
202 
203  clock_wait(100);
204 
205  uart0_init(BAUD2UBR(115200)); /* Must come before first printf */
206 #if NETSTACK_CONF_WITH_IPV4
207  slip_arch_init(BAUD2UBR(115200));
208 #endif /* NETSTACK_CONF_WITH_IPV4 */
209 
210  xmem_init();
211 
212  rtimer_init();
213  /*
214  * Hardware initialization done!
215  */
216 
217  /* Restore node id if such has been stored in external mem */
218  node_id_restore();
219 
220  /* If no MAC address was burned, we use the node id or the Z1 product ID */
221  if(!(node_mac[0] | node_mac[1] | node_mac[2] | node_mac[3] |
222  node_mac[4] | node_mac[5] | node_mac[6] | node_mac[7])) {
223 
224 #ifdef SERIALNUM
225  if(!node_id) {
226  PRINTF("Node id is not set, using Z1 product ID\n");
227  node_id = SERIALNUM;
228  }
229 #endif
230  node_mac[0] = 0xc1; /* Hardcoded for Z1 */
231  node_mac[1] = 0x0c; /* Hardcoded for Revision C */
232  node_mac[2] = 0x00; /* Hardcoded to arbitrary even number so that
233  the 802.15.4 MAC address is compatible with
234  an Ethernet MAC address - byte 0 (byte 2 in
235  the DS ID) */
236  node_mac[3] = 0x00; /* Hardcoded */
237  node_mac[4] = 0x00; /* Hardcoded */
238  node_mac[5] = 0x00; /* Hardcoded */
239  node_mac[6] = node_id >> 8;
240  node_mac[7] = node_id & 0xff;
241  }
242 
243  /* Overwrite node MAC if desired at compile time */
244 #ifdef MACID
245 #warning "***** CHANGING DEFAULT MAC *****"
246  node_mac[0] = 0xc1; /* Hardcoded for Z1 */
247  node_mac[1] = 0x0c; /* Hardcoded for Revision C */
248  node_mac[2] = 0x00; /* Hardcoded to arbitrary even number so that
249  the 802.15.4 MAC address is compatible with
250  an Ethernet MAC address - byte 0 (byte 2 in
251  the DS ID) */
252  node_mac[3] = 0x00; /* Hardcoded */
253  node_mac[4] = 0x00; /* Hardcoded */
254  node_mac[5] = 0x00; /* Hardcoded */
255  node_mac[6] = MACID >> 8;
256  node_mac[7] = MACID & 0xff;
257 #endif
258 
259 #ifdef IEEE_802154_MAC_ADDRESS
260  /* for setting "hardcoded" IEEE 802.15.4 MAC addresses */
261  {
262  uint8_t ieee[] = IEEE_802154_MAC_ADDRESS;
263  memcpy(node_mac, ieee, sizeof(uip_lladdr.addr));
264  node_mac[7] = node_id & 0xff;
265  }
266 #endif /* IEEE_802154_MAC_ADDRESS */
267 
268  /*
269  * Initialize Contiki and our processes.
270  */
271  random_init(node_mac[6] + node_mac[7]);
272  process_init();
273  process_start(&etimer_process, NULL);
274 
275  ctimer_init();
276 
277  init_platform();
278 
279  set_rime_addr();
280 
281  cc2420_init();
282  accm_init();
283 
284  {
285  uint8_t longaddr[8];
286  uint16_t shortaddr;
287 
288  shortaddr = (linkaddr_node_addr.u8[0] << 8) +
289  linkaddr_node_addr.u8[1];
290  memset(longaddr, 0, sizeof(longaddr));
291  linkaddr_copy((linkaddr_t *)&longaddr, &linkaddr_node_addr);
292  printf("MAC %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x ",
293  longaddr[0], longaddr[1], longaddr[2], longaddr[3],
294  longaddr[4], longaddr[5], longaddr[6], longaddr[7]);
295 
296  cc2420_set_pan_addr(IEEE802154_PANID, shortaddr, longaddr);
297  }
298 
299  leds_off(LEDS_ALL);
300 
301 #ifdef SERIALNUM
302  PRINTF("Ref ID: %u\n", SERIALNUM);
303 #endif
304  PRINTF(CONTIKI_VERSION_STRING " started. ");
305 
306  if(node_id) {
307  PRINTF("Node id is set to %u.\n", node_id);
308  } else {
309  PRINTF("Node id not set\n");
310  }
311 
312 #if NETSTACK_CONF_WITH_IPV6
313  memcpy(&uip_lladdr.addr, node_mac, sizeof(uip_lladdr.addr));
314  /* Setup nullmac-like MAC for 802.15.4 */
315 /* sicslowpan_init(sicslowmac_init(&cc2420_driver)); */
316 /* printf(" %s channel %u\n", sicslowmac_driver.name, CC2420_CONF_CHANNEL); */
317 
318  /* Setup X-MAC for 802.15.4 */
319  queuebuf_init();
320 
321  NETSTACK_RDC.init();
322  NETSTACK_MAC.init();
323  NETSTACK_NETWORK.init();
324 
325  printf("%s %s, channel check rate %lu Hz, radio channel %u\n",
326  NETSTACK_MAC.name, NETSTACK_RDC.name,
327  CLOCK_SECOND / (NETSTACK_RDC.channel_check_interval() == 0 ? 1 :
328  NETSTACK_RDC.channel_check_interval()),
329  CC2420_CONF_CHANNEL);
330 
331  process_start(&tcpip_process, NULL);
332 
333  printf("Tentative link-local IPv6 address ");
334  {
335  uip_ds6_addr_t *lladdr;
336  int i;
337  lladdr = uip_ds6_get_link_local(-1);
338  for(i = 0; i < 7; ++i) {
339  printf("%02x%02x:", lladdr->ipaddr.u8[i * 2],
340  lladdr->ipaddr.u8[i * 2 + 1]);
341  }
342  printf("%02x%02x\n", lladdr->ipaddr.u8[14], lladdr->ipaddr.u8[15]);
343  }
344 
345  if(!UIP_CONF_IPV6_RPL) {
346  uip_ipaddr_t ipaddr;
347  int i;
348  uip_ip6addr(&ipaddr, UIP_DS6_DEFAULT_PREFIX, 0, 0, 0, 0, 0, 0, 0);
349  uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr);
350  uip_ds6_addr_add(&ipaddr, 0, ADDR_TENTATIVE);
351  printf("Tentative global IPv6 address ");
352  for(i = 0; i < 7; ++i) {
353  printf("%02x%02x:",
354  ipaddr.u8[i * 2], ipaddr.u8[i * 2 + 1]);
355  }
356  printf("%02x%02x\n",
357  ipaddr.u8[7 * 2], ipaddr.u8[7 * 2 + 1]);
358  }
359 
360 #else /* NETSTACK_CONF_WITH_IPV6 */
361 
362  NETSTACK_RDC.init();
363  NETSTACK_MAC.init();
364  NETSTACK_NETWORK.init();
365 
366  printf("%s %s, channel check rate %lu Hz, radio channel %u\n",
367  NETSTACK_MAC.name, NETSTACK_RDC.name,
368  CLOCK_SECOND / (NETSTACK_RDC.channel_check_interval() == 0 ? 1 :
369  NETSTACK_RDC.channel_check_interval()),
370  CC2420_CONF_CHANNEL);
371 #endif /* NETSTACK_CONF_WITH_IPV6 */
372 
373 #if !NETSTACK_CONF_WITH_IPV4 && !NETSTACK_CONF_WITH_IPV6
374  uart0_set_input(serial_line_input_byte);
375  serial_line_init();
376 #endif
377 
378  leds_off(LEDS_GREEN);
379 
380 #if TIMESYNCH_CONF_ENABLED
381  timesynch_init();
383 #endif /* TIMESYNCH_CONF_ENABLED */
384 
385 #if NETSTACK_CONF_WITH_IPV4
386  process_start(&tcpip_process, NULL);
387  process_start(&uip_fw_process, NULL); /* Start IP output */
388  process_start(&slip_process, NULL);
389 
390  slip_set_input_callback(set_gateway);
391 
392  {
393  uip_ipaddr_t hostaddr, netmask;
394 
395  uip_init();
396 
397  uip_ipaddr(&hostaddr, 172, 16,
399  uip_ipaddr(&netmask, 255, 255, 0, 0);
400  uip_ipaddr_copy(&meshif.ipaddr, &hostaddr);
401 
402  uip_sethostaddr(&hostaddr);
403  uip_setnetmask(&netmask);
404  uip_over_mesh_set_net(&hostaddr, &netmask);
405  /* uip_fw_register(&slipif);*/
406  uip_over_mesh_set_gateway_netif(&slipif);
407  uip_fw_default(&meshif);
408  uip_over_mesh_init(UIP_OVER_MESH_CHANNEL);
409  printf("uIP started with IP address %d.%d.%d.%d\n",
410  uip_ipaddr_to_quad(&hostaddr));
411  }
412 #endif /* NETSTACK_CONF_WITH_IPV4 */
413 
414  energest_init();
415  ENERGEST_ON(ENERGEST_TYPE_CPU);
416 
417  print_processes(autostart_processes);
418  autostart_start(autostart_processes);
419 
420  /*
421  * This is the scheduler loop.
422  */
423 #if DCOSYNCH_CONF_ENABLED
424  timer_set(&mgt_timer, DCOSYNCH_PERIOD * CLOCK_SECOND);
425 #endif
426  watchdog_start();
427  /* watchdog_stop();*/
428  while(1) {
429  int r;
430  do {
431  /* Reset watchdog. */
433  r = process_run();
434  } while(r > 0);
435 
436  /*
437  * Idle processing.
438  */
439  int s = splhigh(); /* Disable interrupts. */
440  /* uart0_active is for avoiding LPM3 when still sending or receiving */
441  if(process_nevents() != 0 || uart0_active()) {
442  splx(s); /* Re-enable interrupts. */
443  } else {
444  static unsigned long irq_energest = 0;
445 
446 #if DCOSYNCH_CONF_ENABLED
447  /* before going down to sleep possibly do some management */
448  if(timer_expired(&mgt_timer)) {
449  timer_reset(&mgt_timer);
450  msp430_sync_dco();
451  }
452 #endif
453 
454  /* Re-enable interrupts and go to sleep atomically. */
455  ENERGEST_SWITCH(ENERGEST_TYPE_CPU, ENERGEST_TYPE_LPM);
456  /* We only want to measure the processing done in IRQs when we
457  are asleep, so we discard the processing time done when we
458  were awake. */
459  energest_type_set(ENERGEST_TYPE_IRQ, irq_energest);
460  watchdog_stop();
461  _BIS_SR(GIE | SCG0 | SCG1 | CPUOFF); /* LPM3 sleep. This
462  statement will block
463  until the CPU is
464  woken up by an
465  interrupt that sets
466  the wake up flag. */
467 
468  /* We get the current processing time for interrupts that was
469  done during the LPM and store it for next time around. */
470  dint();
471  irq_energest = energest_type_time(ENERGEST_TYPE_IRQ);
472  eint();
473  watchdog_start();
474  ENERGEST_SWITCH(ENERGEST_TYPE_LPM, ENERGEST_TYPE_CPU);
475  }
476  }
477 
478  return 0;
479 }
480 /*---------------------------------------------------------------------------*/
481 #if LOG_CONF_ENABLED
482 void
483 log_message(char *m1, char *m2)
484 {
485  printf("%s%s\n", m1, m2);
486 }
487 #endif /* LOG_CONF_ENABLED */
488 
void uip_log(char *msg)
Print out a uIP log message.
#define uip_sethostaddr(addr)
Set the IP address of this host.
Definition: uip.h:192
static uip_ipaddr_t ipaddr
Pointer to prefix information option in uip_buf.
Definition: uip-nd6.c:129
void uart0_init(unsigned long ubr)
Initalize the RS232 port.
Definition: uart0.c:143
void timer_reset(struct timer *t)
Reset the timer with the same interval.
Definition: timer.c:85
#define uip_ipaddr(addr, addr0, addr1, addr2, addr3)
Construct an IP address from four bytes.
Definition: uip.h:956
void timesynch_set_authority_level(int level)
Set the authority level of the current time.
static uip_ds6_addr_t * addr
Pointer to a router list entry.
Definition: uip-nd6.c:124
uint8_t slip_send(void)
Send an IP packet from the uIP buffer with SLIP.
Definition: slip.c:193
Header file for IPv6-related data structures.
void timer_set(struct timer *t, clock_time_t interval)
Set a timer.
Definition: timer.c:64
uip_ipaddr_t ipaddr
The IP address of this interface.
Definition: uip-fw.h:57
A timer.
Definition: timer.h:86
void random_init(unsigned short seed)
Seed the cc2538 random number generator.
Definition: random.c:41
Coffee architecture-dependent header for the Zolertia Z1 platform.
void watchdog_stop(void)
Stops the WDT such that it won't timeout and cause MCU reset.
Definition: watchdog.c:58
Header file for tunnelling uIP over Rime mesh
void clock_init(void)
Initialize the clock library.
Definition: clock.c:76
#define uip_ipaddr_copy(dest, src)
Copy an IP address from one place to another.
Definition: uip.h:1027
int process_run(void)
Run the system once - call poll handlers and process one event.
Definition: process.c:302
Unicast address structure.
Definition: uip-ds6.h:202
void slip_arch_init(unsigned long ubr)
Initalize the RS232 port and the SLIP driver.
Definition: slip-arch.c:56
void slip_set_input_callback(void(*c)(void))
Set a function to be called when there is activity on the SLIP interface; used for detecting if a nod...
Definition: slip.c:143
SENSORS & button_sensor
Copyright (c) 2014, Analog Devices, Inc.
uip_ipaddr_t netmask
The netmask of the interface.
Definition: uip-fw.h:58
Header file for the Rime stack
#define uip_setnetmask(addr)
Set the netmask.
Definition: uip.h:236
#define NULL
The null pointer.
#define uip_ipaddr_to_quad(a)
Convert an IP address to four bytes separated by commas.
Definition: uip.h:928
802.15.4 frame creation and parsing functions
uIP packet forwarding header file.
void linkaddr_set_node_addr(linkaddr_t *t)
Set the address of the current node.
Definition: linkaddr.c:72
#define UIP_FW_NETIF(ip1, ip2, ip3, ip4, nm1, nm2, nm3, nm4, outputfunc)
Instantiating macro for a uIP network interface.
Definition: uip-fw.h:80
void watchdog_periodic(void)
Writes the WDT clear sequence.
Definition: watchdog.c:64
#define ADDR_TENTATIVE
Possible states for the an address (RFC 4862)
Definition: uip-ds6.h:153
#define CLOCK_SECOND
A second, measured in system clock time.
Definition: clock.h:82
#define uip_ip6addr(addr, addr0, addr1, addr2, addr3, addr4, addr5, addr6, addr7)
Construct an IPv6 address from eight 16-bit words.
Definition: uip.h:970
Header file for the uIP TCP/IP stack.
void process_start(struct process *p, process_data_t data)
Start a process.
Definition: process.c:99
Header for the Coffee file system.
uip_ds6_addr_t * uip_ds6_addr_add(uip_ipaddr_t *ipaddr, unsigned long vlifetime, uint8_t type)
Add a unicast address to the interface.
Definition: uip-ds6.c:326
Header file for module for automatically starting and exiting a list of processes.
void clock_wait(clock_time_t t)
Wait for a given number of ticks.
Definition: clock.c:162
void timesynch_init(void)
Initialize the timesynch module.
void rtimer_init(void)
Initialize the real-time scheduler.
Definition: rtimer.c:61
Generic serial I/O process header filer.
CCIF uip_lladdr_t uip_lladdr
Host L2 address.
Definition: uip.c:118
void uip_init(void)
uIP initialization function.
Definition: uip.c:363
int process_nevents(void)
Number of events waiting to be processed.
Definition: process.c:316
void process_init(void)
Initialize the process module.
Definition: process.c:208
void ctimer_init(void)
Initialize the callback timer library.
Definition: ctimer.c:91
void linkaddr_copy(linkaddr_t *dest, const linkaddr_t *src)
Copy a Rime address.
Definition: linkaddr.c:60
linkaddr_t linkaddr_node_addr
The Rime address of the node.
Definition: linkaddr.c:48
int serial_line_input_byte(unsigned char c)
Get one byte of input from the serial driver.
Definition: serial-line.c:60
void watchdog_start(void)
Starts the WDT in watchdog mode if enabled by user configuration, maximum interval.
Definition: watchdog.c:49
Include file for the Contiki low-layer network stack (NETSTACK)
void uip_ds6_set_addr_iid(uip_ipaddr_t *ipaddr, uip_lladdr_t *lladdr)
set the last 64 bits of an IP address based on the MAC address
Definition: uip-ds6.c:542
void uip_fw_default(struct uip_fw_netif *netif)
Register a default network interface.
Definition: uip-fw.c:515
int timer_expired(struct timer *t)
Check if a timer has expired.
Definition: timer.c:122
Representation of a uIP network interface.
Definition: uip-fw.h:54