Contiki 2.5
contiki-main.c
1 #include <AT91SAM7S64.h>
2 #include <interrupt-utils.h>
3 #include <string.h>
4 #include <debug-uart.h>
5 #include <ctype.h>
6 #include <stdio.h>
7 #include <dev/cc2420.h>
8 #include <dev/cc2420_const.h>
9 #include <dev/spi.h>
10 #include <dev/leds.h>
11 #include <sys/process.h>
12 #include <sys/procinit.h>
13 #include <sys/autostart.h>
14 #include <sys/etimer.h>
15 #include <net/psock.h>
16 #include <unistd.h>
17 
18 #include "net/mac/nullmac.h"
19 #include "net/rime.h"
20 
21 #include "contiki-main.h"
22 
23 #ifndef RF_CHANNEL
24 #define RF_CHANNEL 15
25 #endif
26 
27 
28 #if WITH_UIP
29 #include "net/uip.h"
30 #include "net/uip-fw.h"
31 #include "net/uip-fw-drv.h"
32 #include "net/uip-over-mesh.h"
33 
34 
35 static struct uip_fw_netif meshif =
36  {UIP_FW_NETIF(172,16,0,0, 255,255,0,0, uip_over_mesh_send)};
37 
38 #define UIP_OVER_MESH_CHANNEL 9
39 
40 uint8_t is_gateway = 0;
41 struct trickle_conn gateway_trickle;
42 
43 #endif /* WITH_UIP */
44 
45 extern char __heap_end__;
46 extern char __heap_start__;
47 
48 
49 rimeaddr_t node_addr __attribute__((weak)) = {{0,2}};
50 
51 /*--------------------------------------------------------------------------*/
52 #if WITH_UIP
53 
54 /* Receive address of gateway */
55 
56 static void
57 trickle_recv(struct trickle_conn *c)
58 {
59  if (!is_gateway) {
60  struct gateway_msg *msg;
61  msg = rimebuf_dataptr();
62  printf("%d.%d: gateway message: %d.%d\n",
64  msg->gateway.u8[0], msg->gateway.u8[1]);
65 
66  uip_over_mesh_set_gateway(&msg->gateway);
67  }
68 }
69 
70 const static struct trickle_callbacks trickle_call = {trickle_recv};
71 
72 /*---------------------------------------------------------------------------*/
73 #endif
74 
75 #if 0
76 /* Wathcdog is already disabled in startup code */
77 static void
78 wdt_setup()
79 {
80 
81 }
82 #endif
83 
84 static void
85 wdt_reset()
86 {
87  *AT91C_WDTC_WDCR = (0xa5<<24) | AT91C_WDTC_WDRSTT;
88 }
89 
90 #if 0
91 static uip_ipaddr_t gw_addr = {{172,16,0,1}};
92 #endif
93 
94 
95 int
97 {
98  disableIRQ();
99  disableFIQ();
100  *AT91C_AIC_IDCR = 0xffffffff;
101  *AT91C_PMC_PCDR = 0xffffffff;
102  *AT91C_PMC_PCER = (1 << AT91C_ID_PIOA);
103 
104  dbg_setup_uart();
105  printf("Initialising\n");
106  leds_init();
107  clock_init();
108  process_init();
109  process_start(&etimer_process, NULL);
110 
111  ctimer_init();
112 
113  enableIRQ();
114 
115  printf("Beginning CC2420 setup\n");
116  cc2420_init();
117  cc2420_set_pan_addr(0x2024, 0, NULL);
118  cc2420_set_channel(RF_CHANNEL);
119  rime_init(nullmac_init(&cc2420_driver));
120  printf("CC2420 setup done\n");
121 
122  rimeaddr_set_node_addr(&node_addr);
123  printf("Rime started with address %d.%d\n", node_addr.u8[0], node_addr.u8[1]);
124 
125  #if WITH_UIP
126  {
127  uip_ipaddr_t hostaddr, netmask;
128 
129  uip_init();
130 
131  uip_ipaddr(&hostaddr, 172,16,
133  uip_ipaddr(&netmask, 255,255,0,0);
134  uip_ipaddr_copy(&meshif.ipaddr, &hostaddr);
135  printf("Host addr\n");
136  uip_sethostaddr(&hostaddr);
137  uip_setnetmask(&netmask);
138  uip_over_mesh_set_net(&hostaddr, &netmask);
139  /* uip_fw_register(&slipif);*/
140  /*uip_over_mesh_set_gateway_netif(&slipif);*/
141  uip_fw_register(&meshif);
142  uip_fw_default(&meshif);
143  printf("Mesh init\n");
144  uip_over_mesh_init(UIP_OVER_MESH_CHANNEL);
145  printf("uIP started with IP address %d.%d.%d.%d\n",
146  uip_ipaddr_to_quad(&hostaddr));
147  }
148 #endif /* WITH_UIP */
149 
150 
151 
152 #if WITH_UIP
153  process_start(&tcpip_process, NULL);
154  process_start(&uip_fw_process, NULL); /* Start IP output */
155 
156  trickle_open(&gateway_trickle, CLOCK_SECOND * 4, GATEWAY_TRICKLE_CHANNEL,
157  &trickle_call);
158 #endif /* WITH_UIP */
159 
160  printf("Heap size: %ld bytes\n", &__heap_end__ - (char*)sbrk(0));
161  printf("Started\n");
162 
163  /* procinit_init(); */
164 
165  autostart_start(autostart_processes);
166  printf("Processes running\n");
167  while(1) {
168  do {
169  /* Reset watchdog. */
170  wdt_reset();
171  } while(process_run() > 0);
172  /* Idle! */
173  /* Stop processor clock */
174  *AT91C_PMC_SCDR |= AT91C_PMC_PCK;
175  }
176  return 0;
177 }