Contiki 3.x
contiki-main.c
1 #include <stdint.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <stdarg.h>
5 
6 #include "contiki-conf.h"
7 #include "contiki.h"
8 #include "platform-conf.h"
9 #include "sys/autostart.h"
10 #include "dev/leds.h"
11 
12 #include <MKL25Z4.h>
13 #include "nvic.h"
14 #include "debug-uart.h"
15 #include "cpu.h"
16 #include "spi.h"
17 #include "gpio.h"
18 #include "cc1120.h"
19 #include "cc1120-arch.h"
20 
21 //#include "dev/slip.h"
22 #include "dev/watchdog.h"
23 #include "sys/clock.h"
24 #include "lib/random.h"
25 #include "net/netstack.h"
26 #include "net/mac/frame802154.h"
27 
28 #if NETSTACK_CONF_WITH_IPV6
29 #include "net/ipv6/uip-ds6.h"
30 #endif /* NETSTACK_CONF_WITH_IPV6 */
31 
32 #include "net/rime/rime.h"
33 
34 #include "sys/node-id.h"
35 
36 #define DEBUG 1
37 #if DEBUG
38 #include <stdio.h>
39 #define PRINTF(...) printf(__VA_ARGS__)
40 #else
41 #define PRINTF(...)
42 #endif
43 
44 #ifndef NETSTACK_CONF_WITH_IPV4
45 #define NETSTACK_CONF_WITH_IPV4 0
46 #endif
47 
48 #if NETSTACK_CONF_WITH_IPV4
49 #include "net/ip/uip.h"
50 #include "net/ipv4/uip-fw.h"
51 #include "net/uip-fw-drv.h"
52 #include "net/ipv4/uip-over-mesh.h"
53 static struct uip_fw_netif slipif =
54 { UIP_FW_NETIF(192, 168, 1, 2, 255, 255, 255, 255, slip_send) };
55 static struct uip_fw_netif meshif =
56 { UIP_FW_NETIF(172, 16, 0, 0, 255, 255, 0, 0, uip_over_mesh_send) };
57 
58 #endif /* NETSTACK_CONF_WITH_IPV4 */
59 
60 #define UIP_OVER_MESH_CHANNEL 8
61 #if NETSTACK_CONF_WITH_IPV4
62 static uint8_t is_gateway;
63 #endif /* NETSTACK_CONF_WITH_IPV4 */
64 
65 unsigned short node_id = 0x55;
66 unsigned char node_mac[8];
67 
68 unsigned int idle_count = 0;
69 
70 void
71 uip_log(char *msg)
72 {
73  puts(msg);
74 }
75 
76 static void
77 set_rime_addr(void)
78 {
79  linkaddr_t addr;
80  //int i;
81 
82  memset(&addr, 0, sizeof(linkaddr_t));
83 #if NETSTACK_CONF_WITH_IPV6
84  memcpy(addr.u8, node_mac, sizeof(addr.u8));
85 #else
86  if(node_id == 0) {
87  for(i = 0; i < sizeof(linkaddr_t); ++i) {
88  addr.u8[i] = node_mac[7 - i];
89  }
90  } else {
91  addr.u8[0] = node_id & 0xff;
92  addr.u8[1] = node_id >> 8;
93  }
94 #endif
96 
97 }
98 
99 int
100 main(void)
101 {
102  cpu_init();
103  watchdog_stop();
104  port_enable(PORTB_EN_MASK | PORTC_EN_MASK | PORTD_EN_MASK | PORTE_EN_MASK);
105 
106  dbg_setup_uart();
107  clock_init();
108  rtimer_init();
109 
110  cpu_reboot_src(); /* Print the cause of the reboot. */
111 
112  gpio_init();
113  leds_init();
114 
115  leds_on(LEDS_RED);
116 
117 
118  node_mac[0] = 0xc1; /* Hardcoded for Z1 */
119  node_mac[1] = 0x0c; /* Hardcoded for Revision C */
120  node_mac[2] = 0x00; /* Hardcoded to arbitrary even number so that
121  the 802.15.4 MAC address is compatible with
122  an Ethernet MAC address - byte 0 (byte 2 in
123  the DS ID) */
124  node_mac[3] = 0x00; /* Hardcoded */
125  node_mac[4] = 0x00; /* Hardcoded */
126  node_mac[5] = 0x00; /* Hardcoded */
127  node_mac[6] = (uint8_t)(12345678 >> 8);
128  node_mac[7] = (uint8_t)(12345678 & 0xff);
129 
130  process_init();
131  process_start(&etimer_process, NULL);
132  ctimer_init();
133  SPI0_init();
134  set_rime_addr();
135 
136  leds_on(LEDS_GREEN);
137 
138  NETSTACK_CONF_RADIO.init();
139 
140  leds_off(LEDS_RED);
141 
142  printf(CONTIKI_VERSION_STRING " started. ");
143 
144 #if WITH_UIP6
145  memcpy(&uip_lladdr.addr, node_mac, sizeof(uip_lladdr.addr));
146 
147  /* Setup X-MAC for 802.15.4 */
148  queuebuf_init();
149 
150  //NETSTACK_RDC.init();
151  //NETSTACK_MAC.init();
152  //NETSTACK_NETWORK.init();
153  netstack_init();
154 
155  PRINTF(" Net: %s\n", NETSTACK_NETWORK.name);
156 
157  printf("%s %s, channel check rate %d Hz, radio channel %d\n",
158  NETSTACK_MAC.name, NETSTACK_RDC.name,
159  CLOCK_SECOND / (NETSTACK_RDC.channel_check_interval() == 0 ? 1:
160  NETSTACK_RDC.channel_check_interval()),
161  RF_CHANNEL);
162 
163  queuebuf_init();
164  process_start(&tcpip_process, NULL);
165 
166  printf("Tentative link-local IPv6 address ");
167  {
168  uip_ds6_addr_t *lladdr;
169  int i;
170  lladdr = uip_ds6_get_link_local(-1);
171 
172  for(i = 0; i < 7; ++i) {
173  printf("%02x%02x:", lladdr->ipaddr.u8[i * 2],
174  lladdr->ipaddr.u8[i * 2 + 1]);
175  }
176 
177  printf("%02x%02x\n", lladdr->ipaddr.u8[14], lladdr->ipaddr.u8[15]);
178  }
179 
180  if(!UIP_CONF_IPV6_RPL) {
181  uip_ipaddr_t ipaddr;
182  int i;
183  uip_ip6addr(&ipaddr, 0xaaaa, 0, 0, 0, 0, 0, 0, 0);
184  uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr);
185  uip_ds6_addr_add(&ipaddr, 0, ADDR_TENTATIVE);
186  printf("Tentative global IPv6 address ");
187 
188  for(i = 0; i < 7; ++i) {
189  printf("%02x%02x:",
190  ipaddr.u8[i * 2], ipaddr.u8[i * 2 + 1]);
191  }
192  printf("%02x%02x\n",
193  ipaddr.u8[7 * 2], ipaddr.u8[7 * 2 + 1]);
194  }
195 
196 #else /* WITH_UIP6 */
197 
198  //NETSTACK_RDC.init();
199  //NETSTACK_MAC.init();
200  //NETSTACK_NETWORK.init();
201  netstack_init();
202 
203  printf("%s %s, channel check rate %lu Hz, radio channel %u\n",
204  NETSTACK_MAC.name, NETSTACK_RDC.name,
205  CLOCK_SECOND / (NETSTACK_RDC.channel_check_interval() == 0? 1:
206  NETSTACK_RDC.channel_check_interval()),
207  RF_CHANNEL);
208 #endif /* WITH_UIP6 */
209 
210 #if NETSTACK_CONF_WITH_IPV4
211  process_start(&tcpip_process, NULL);
212  process_start(&uip_fw_process, NULL); /* Start IP output */
213  {
214  uip_ipaddr_t hostaddr, netmask;
215 
216  uip_init();
217 
218  uip_ipaddr(&hostaddr, 172,16,
219  rimeaddr_node_addr.u8[0],rimeaddr_node_addr.u8[1]);
220  uip_ipaddr(&netmask, 255,255,0,0);
221  uip_ipaddr_copy(&meshif.ipaddr, &hostaddr);
222 
223  uip_sethostaddr(&hostaddr);
224  uip_setnetmask(&netmask);
225  uip_over_mesh_set_net(&hostaddr, &netmask);
226  /* uip_fw_register(&slipif);*/
227  //uip_over_mesh_set_gateway_netif(&slipif);
228  uip_fw_default(&meshif);
229  uip_over_mesh_init(UIP_OVER_MESH_CHANNEL);
230  printf("uIP started with IP address %d.%d.%d.%d\n",
231  uip_ipaddr_to_quad(&hostaddr));
232  }
233 #endif /* WITH_UIP */
234 
235  leds_on(LEDS_BLUE);
236 
237  energest_init();
238  ENERGEST_ON(ENERGEST_TYPE_CPU);
239 
240  leds_off(LEDS_GREEN);
241 
242  autostart_start(autostart_processes);
243 
244  watchdog_start();
245 
246  leds_off(LEDS_BLUE);
247 
248  while(1) {
249  uint8_t r;
250 
251  do {
252  /* Reset watchdog. */
254  r = process_run();
255  } while(r > 0);
256 
257  cpu_wait();
258  }
259 
260  return 0;
261 }
void uip_log(char *m)
Print out a uIP log message.
Definition: contiki-main.c:187
void gpio_init()
Initialise the GPIO module.
Definition: gpio.c:13
#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
#define uip_ipaddr(addr, addr0, addr1, addr2, addr3)
Construct an IP address from four bytes.
Definition: uip.h:956
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.
uip_ipaddr_t ipaddr
The IP address of this interface.
Definition: uip-fw.h:57
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
CMSIS Peripheral Access Layer for MKL25Z4.
#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
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
int main(void)
This is main...
Definition: contiki-main.c:456
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 rtimer_init(void)
Initialize the real-time scheduler.
Definition: rtimer.c:61
CCIF uip_lladdr_t uip_lladdr
Host L2 address.
Definition: uip.c:118
void uip_init(void)
uIP initialization function.
Definition: uip.c:363
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
Platform configuration for the Z1-feshie platform
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
Representation of a uIP network interface.
Definition: uip-fw.h:54