Contiki 2.5
contiki-sensinode-main.c
1 #include <stdio.h>
2 
3 #include "contiki.h"
4 #include "sys/clock.h"
5 #include "sys/autostart.h"
6 
7 #include "dev/serial-line.h"
8 #include "dev/bus.h"
9 #include "dev/leds.h"
10 #include "dev/uart.h"
11 #include "dev/models.h"
12 #include "dev/cc2430_rf.h"
13 #include "net/mac/sicslowmac.h"
14 #include "net/mac/frame802154.h"
15 #include "net/rime.h"
16 
17 volatile int i, a;
18 unsigned short node_id = 0; /* Manually sets MAC address when > 0 */
19 
20 /*---------------------------------------------------------------------------*/
21 static void
22 print_processes(struct process * const processes[])
23 {
24  printf("Starting");
25  while(*processes != NULL) {
26  printf(" '%s'", (*processes)->name);
27  processes++;
28  }
29  printf("\n");
30 }
31 /*---------------------------------------------------------------------------*/
32 void
33 putchar(char c)
34 {
35  /* UART1 used for debugging on Sensinode products. */
36  uart1_writeb(c);
37 }
38 /*---------------------------------------------------------------------------*/
39 static void
40 fade(int l)
41 {
42  int k, j;
43  for(k = 0; k < 400; ++k) {
44  j = k > 200? 400 - k: k;
45 
46  leds_on(l);
47  for(i = 0; i < j; ++i) {
48  a = i;
49  }
50  leds_off(l);
51  for(i = 0; i < 200 - j; ++i) {
52  a = i;
53  }
54  }
55 }
56 /*---------------------------------------------------------------------------*/
57 static void
58 set_rime_addr(void)
59 {
60  rimeaddr_t addr;
61  uint8_t ft_buffer[8];
62  uint8_t *addr_long = NULL;
63  uint16_t addr_short = 0;
64  int i;
65 
66  /* TODO: This flash_read routine currently gets a different address
67  * than nano_programmer -m... something broken or misconfigured...
68  */
69 
70  flash_read(&ft_buffer[0], 0x1FFF8, 8);
71 
72  printf("Read MAC from flash: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
73  ft_buffer[0], ft_buffer[1], ft_buffer[2], ft_buffer[3],
74  ft_buffer[4], ft_buffer[5], ft_buffer[6], ft_buffer[7]);
75 
76  memset(&addr, 0, sizeof(rimeaddr_t));
77 
78 #if UIP_CONF_IPV6
79  memcpy(addr.u8, ft_buffer, sizeof(addr.u8));
80 #else
81  if(node_id == 0) {
82  for(i = 0; i < sizeof(rimeaddr_t); ++i) {
83  addr.u8[i] = ft_buffer[7 - i];
84  }
85  } else {
86  printf("Setting manual address from node_id\n");
87  addr.u8[1] = node_id >> 8;
88  addr.u8[0] = node_id & 0xff;
89  }
90 #endif
91 
93  printf("Rime configured with address ");
94  for(i = (sizeof(addr.u8)) - 1; i > 0; i--) {
95  printf("%02x:", addr.u8[i]);
96  }
97  printf("%02x\n", addr.u8[i]);
98 
99  /* Set the cc2430 RF addresses */
100  if (sizeof(addr.u8) == 6)
101  addr_long = (uint8_t *) addr.u8;
102  else
103  addr_short = (addr.u8[1] * 256) + addr.u8[0];
104 
105  cc2430_rf_set_addr(0xffff, addr_short, addr_long);
106 }
107 /*---------------------------------------------------------------------------*/
108 int
109 main(void)
110 {
111 
112  /* Hardware initialization */
113  bus_init();
114 
115  leds_init();
116  fade(LEDS_GREEN);
117 
118  uart1_init(115200);
119  uart1_set_input(serial_line_input_byte);
120 
121  /* initialize process manager. */
122  process_init();
123 
124  serial_line_init();
125 
126  printf("\n" CONTIKI_VERSION_STRING " started\n");
127  printf("model: " SENSINODE_MODEL "\n\n");
128 
129  /* initialize the radio driver */
130 
131  cc2430_rf_init();
132  rime_init(sicslowmac_init(&cc2430_rf_driver));
133  set_rime_addr();
134 
135  /* start services */
136  process_start(&etimer_process, NULL);
137 
138  fade(LEDS_RED);
139 
140  autostart_start(autostart_processes);
141 
142  while(1) {
143  process_run();
145  }
146 }
147 /*---------------------------------------------------------------------------*/