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 WITH_UIP6
51 #include "net/ipv6/uip-ds6.h"
52 #endif /* WITH_UIP6 */
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 WITH_UIP
74 #define WITH_UIP 0
75 #endif
76 
77 #if WITH_UIP
78 #include "net/ip/uip.h"
79 #include "net/ipv4/uip-fw.h"
80 #include "net/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 /* WITH_UIP */
88 
89 #define UIP_OVER_MESH_CHANNEL 8
90 #if WITH_UIP
91 static uint8_t is_gateway;
92 #endif /* WITH_UIP */
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 UIP_CONF_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 WITH_UIP
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 /* WITH_UIP */
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 WITH_UIP
207  slip_arch_init(BAUD2UBR(115200));
208 #endif /* WITH_UIP */
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  process_init();
272  process_start(&etimer_process, NULL);
273 
274  ctimer_init();
275 
276  init_platform();
277 
278  set_rime_addr();
279 
280  cc2420_init();
281  accm_init();
282 
283  {
284  uint8_t longaddr[8];
285  uint16_t shortaddr;
286 
287  shortaddr = (linkaddr_node_addr.u8[0] << 8) +
288  linkaddr_node_addr.u8[1];
289  memset(longaddr, 0, sizeof(longaddr));
290  linkaddr_copy((linkaddr_t *)&longaddr, &linkaddr_node_addr);
291  printf("MAC %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x ",
292  longaddr[0], longaddr[1], longaddr[2], longaddr[3],
293  longaddr[4], longaddr[5], longaddr[6], longaddr[7]);
294 
295  cc2420_set_pan_addr(IEEE802154_PANID, shortaddr, longaddr);
296  }
297 
298  leds_off(LEDS_ALL);
299 
300 #ifdef SERIALNUM
301  PRINTF("Ref ID: %u\n", SERIALNUM);
302 #endif
303  PRINTF(CONTIKI_VERSION_STRING " started. ");
304 
305  if(node_id) {
306  PRINTF("Node id is set to %u.\n", node_id);
307  } else {
308  PRINTF("Node id not set\n");
309  }
310 
311 #if WITH_UIP6
312  memcpy(&uip_lladdr.addr, node_mac, sizeof(uip_lladdr.addr));
313  /* Setup nullmac-like MAC for 802.15.4 */
314 /* sicslowpan_init(sicslowmac_init(&cc2420_driver)); */
315 /* printf(" %s channel %u\n", sicslowmac_driver.name, CC2420_CONF_CHANNEL); */
316 
317  /* Setup X-MAC for 802.15.4 */
318  queuebuf_init();
319 
320  NETSTACK_RDC.init();
321  NETSTACK_MAC.init();
322  NETSTACK_NETWORK.init();
323 
324  printf("%s %s, channel check rate %lu Hz, radio channel %u\n",
325  NETSTACK_MAC.name, NETSTACK_RDC.name,
326  CLOCK_SECOND / (NETSTACK_RDC.channel_check_interval() == 0 ? 1 :
327  NETSTACK_RDC.channel_check_interval()),
328  CC2420_CONF_CHANNEL);
329 
330  process_start(&tcpip_process, NULL);
331 
332  printf("Tentative link-local IPv6 address ");
333  {
334  uip_ds6_addr_t *lladdr;
335  int i;
336  lladdr = uip_ds6_get_link_local(-1);
337  for(i = 0; i < 7; ++i) {
338  printf("%02x%02x:", lladdr->ipaddr.u8[i * 2],
339  lladdr->ipaddr.u8[i * 2 + 1]);
340  }
341  printf("%02x%02x\n", lladdr->ipaddr.u8[14], lladdr->ipaddr.u8[15]);
342  }
343 
344  if(!UIP_CONF_IPV6_RPL) {
345  uip_ipaddr_t ipaddr;
346  int i;
347  uip_ip6addr(&ipaddr, 0xaaaa, 0, 0, 0, 0, 0, 0, 0);
348  uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr);
349  uip_ds6_addr_add(&ipaddr, 0, ADDR_TENTATIVE);
350  printf("Tentative global IPv6 address ");
351  for(i = 0; i < 7; ++i) {
352  printf("%02x%02x:",
353  ipaddr.u8[i * 2], ipaddr.u8[i * 2 + 1]);
354  }
355  printf("%02x%02x\n",
356  ipaddr.u8[7 * 2], ipaddr.u8[7 * 2 + 1]);
357  }
358 
359 #else /* WITH_UIP6 */
360 
361  NETSTACK_RDC.init();
362  NETSTACK_MAC.init();
363  NETSTACK_NETWORK.init();
364 
365  printf("%s %s, channel check rate %lu Hz, radio channel %u\n",
366  NETSTACK_MAC.name, NETSTACK_RDC.name,
367  CLOCK_SECOND / (NETSTACK_RDC.channel_check_interval() == 0 ? 1 :
368  NETSTACK_RDC.channel_check_interval()),
369  CC2420_CONF_CHANNEL);
370 #endif /* WITH_UIP6 */
371 
372 #if !WITH_UIP && !WITH_UIP6
373  uart0_set_input(serial_line_input_byte);
374  serial_line_init();
375 #endif
376 
377  leds_off(LEDS_GREEN);
378 
379 #if TIMESYNCH_CONF_ENABLED
380  timesynch_init();
382 #endif /* TIMESYNCH_CONF_ENABLED */
383 
384 #if WITH_UIP
385  process_start(&tcpip_process, NULL);
386  process_start(&uip_fw_process, NULL); /* Start IP output */
387  process_start(&slip_process, NULL);
388 
389  slip_set_input_callback(set_gateway);
390 
391  {
392  uip_ipaddr_t hostaddr, netmask;
393 
394  uip_init();
395 
396  uip_ipaddr(&hostaddr, 172, 16,
398  uip_ipaddr(&netmask, 255, 255, 0, 0);
399  uip_ipaddr_copy(&meshif.ipaddr, &hostaddr);
400 
401  uip_sethostaddr(&hostaddr);
402  uip_setnetmask(&netmask);
403  uip_over_mesh_set_net(&hostaddr, &netmask);
404  /* uip_fw_register(&slipif);*/
405  uip_over_mesh_set_gateway_netif(&slipif);
406  uip_fw_default(&meshif);
407  uip_over_mesh_init(UIP_OVER_MESH_CHANNEL);
408  printf("uIP started with IP address %d.%d.%d.%d\n",
409  uip_ipaddr_to_quad(&hostaddr));
410  }
411 #endif /* WITH_UIP */
412 
413  energest_init();
414  ENERGEST_ON(ENERGEST_TYPE_CPU);
415 
416  print_processes(autostart_processes);
417  autostart_start(autostart_processes);
418 
419  /*
420  * This is the scheduler loop.
421  */
422 #if DCOSYNCH_CONF_ENABLED
423  timer_set(&mgt_timer, DCOSYNCH_PERIOD * CLOCK_SECOND);
424 #endif
425  watchdog_start();
426  /* watchdog_stop();*/
427  while(1) {
428  int r;
429  do {
430  /* Reset watchdog. */
432  r = process_run();
433  } while(r > 0);
434 
435  /*
436  * Idle processing.
437  */
438  int s = splhigh(); /* Disable interrupts. */
439  /* uart0_active is for avoiding LPM3 when still sending or receiving */
440  if(process_nevents() != 0 || uart0_active()) {
441  splx(s); /* Re-enable interrupts. */
442  } else {
443  static unsigned long irq_energest = 0;
444 
445 #if DCOSYNCH_CONF_ENABLED
446  /* before going down to sleep possibly do some management */
447  if(timer_expired(&mgt_timer)) {
448  timer_reset(&mgt_timer);
449  msp430_sync_dco();
450  }
451 #endif
452 
453  /* Re-enable interrupts and go to sleep atomically. */
454  ENERGEST_OFF(ENERGEST_TYPE_CPU);
455  ENERGEST_ON(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_OFF(ENERGEST_TYPE_LPM);
475  ENERGEST_ON(ENERGEST_TYPE_CPU);
476  }
477  }
478 
479  return 0;
480 }
481 /*---------------------------------------------------------------------------*/
482 #if LOG_CONF_ENABLED
483 void
484 log_message(char *m1, char *m2)
485 {
486  printf("%s%s\n", m1, m2);
487 }
488 #endif /* LOG_CONF_ENABLED */
489 
int serial_line_input_byte(unsigned char c)
Get one byte of input from the serial driver.
Definition: serial-line.c:60
#define uip_sethostaddr(addr)
Set the IP address of this host.
Definition: uip.h:195
linkaddr_t linkaddr_node_addr
The Rime address of the node.
Definition: linkaddr.c:48
uIP packet forwarding header file.
int process_nevents(void)
Number of events waiting to be processed.
Definition: process.c:316
A timer.
Definition: timer.h:86
CCIF uip_lladdr_t uip_lladdr
Host L2 address.
Definition: uip6.c:115
802.15.4 frame creation and parsing functions
#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
Representation of a uIP network interface.
Definition: uip-fw.h:54
#define LEDS_RED
LED1 (Red) -&gt; PC0.
Definition: board.h:89
Unicast address structure.
Definition: uip-ds6.h:172
void timesynch_init(void)
Initialize the timesynch module.
void watchdog_start(void)
Starts the WDT in watchdog mode if enabled by user configuration, maximum interval.
Definition: watchdog.c:49
uip_ipaddr_t ipaddr
The IP address of this interface.
Definition: uip-fw.h:57
Header file for the uIP TCP/IP stack.
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:535
void rtimer_init(void)
Initialize the real-time scheduler.
Definition: rtimer.c:61
void uart0_init(unsigned long ubr)
Initalize the RS232 port.
Definition: uart0.c:111
Network interface and stateless autoconfiguration (RFC 4862)
void timer_set(struct timer *t, clock_time_t interval)
Set a timer.
Definition: timer.c:64
#define NULL
The null pointer.
void uip_log(char *msg)
Print out a uIP log message.
Definition: uip-log.c:3
Header file for tunnelling uIP over Rime mesh
void clock_init(void)
Initialize the clock library.
Definition: clock.c:76
int main(void)
This is main...
Definition: ethconfig.c:49
void process_init(void)
Initialize the process module.
Definition: process.c:208
Header file for the Rime stack
Coffee architecture-dependent header for the Zolertia Z1 platform.
void timesynch_set_authority_level(int level)
Set the authority level of the current time.
#define LEDS_GREEN
LED3 (Green) -&gt; PC2.
Definition: board.h:81
void linkaddr_copy(linkaddr_t *dest, const linkaddr_t *src)
Copy a Rime address.
Definition: linkaddr.c:60
Device drivers header file for adxl345 accelerometer in Zolertia Z1.
void slip_arch_init(unsigned long ubr)
Initalize the RS232 port and the SLIP driver.
Definition: slip-arch.c:56
void watchdog_stop(void)
In watchdog mode, the WDT can not be stopped.
Definition: watchdog.c:58
Generic serial I/O process header filer.
void watchdog_periodic(void)
Writes the WDT clear sequence.
Definition: watchdog.c:64
Header file for module for automatically starting and exiting a list of processes.
#define uip_ipaddr_copy(dest, src)
Copy an IP address from one place to another.
Definition: uip.h:1026
void ctimer_init(void)
Initialize the callback timer library.
Definition: ctimer.c:91
#define uip_ipaddr_to_quad(a)
Convert an IP address to four bytes separated by commas.
Definition: uip.h:927
void uip_init(void)
uIP initialization function.
Definition: uip6.c:411
void uip_fw_default(struct uip_fw_netif *netif)
Register a default network interface.
Definition: uip-fw.c:515
uip_ipaddr_t netmask
The netmask of the interface.
Definition: uip-fw.h:58
void process_start(struct process *p, process_data_t data)
Start a process.
Definition: process.c:99
#define ADDR_TENTATIVE
Possible states for the an address (RFC 4862)
Definition: uip-ds6.h:123
int process_run(void)
Run the system once - call poll handlers and process one event.
Definition: process.c:302
void linkaddr_set_node_addr(linkaddr_t *t)
Set the address of the current node.
Definition: linkaddr.c:72
Header for the Coffee file system.
void clock_wait(clock_time_t t)
Wait for a given number of ticks.
Definition: clock.c:166
#define uip_ipaddr(addr, addr0, addr1, addr2, addr3)
Construct an IP address from four bytes.
Definition: uip.h:955
int timer_expired(struct timer *t)
Check if a timer has expired.
Definition: timer.c:121
#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
#define uip_setnetmask(addr)
Set the netmask.
Definition: uip.h:239
void timer_reset(struct timer *t)
Reset the timer with the same interval.
Definition: timer.c:84
Include file for the Contiki low-layer network stack (NETSTACK)
#define CLOCK_SECOND
A second, measured in system clock time.
Definition: clock.h:82