Contiki 2.5
contiki-avr-zigbit-main.c
1 /*
2  * Copyright (c) 2006, Technical University of Munich
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  * This file is part of the Contiki operating system.
30  *
31  * @(#)$$
32  */
33 
34 #include <avr/pgmspace.h>
35 #include <avr/fuse.h>
36 #include <avr/eeprom.h>
37 #include <stdio.h>
38 #include <string.h>
39 
40 #include "lib/mmem.h"
41 #include "loader/symbols-def.h"
42 #include "loader/symtab.h"
43 
44 #define ANNOUNCE_BOOT 0 //adds about 600 bytes to program size
45 #define DEBUG 0
46 #if DEBUG
47 #define PRINTF(FORMAT,args...) printf_P(PSTR(FORMAT),##args)
48 #define PRINTSHORT(FORMAT,args...) printf_P(PSTR(FORMAT),##args)
49 #else
50 #define PRINTF(...)
51 #define PRINTSHORT(...)
52 #endif
53 
54 #if RF230BB //radio driver using contiki core mac
55 #include "radio/rf230bb/rf230bb.h"
56 #include "net/mac/frame802154.h"
57 #include "net/mac/framer-802154.h"
58 #include "net/sicslowpan.h"
59 #else //radio driver using Atmel/Cisco 802.15.4'ish MAC
60 #include <stdbool.h>
61 #include "mac.h"
62 #include "sicslowmac.h"
63 #include "sicslowpan.h"
64 #include "ieee-15-4-manager.h"
65 #endif /*RF230BB*/
66 
67 #include "contiki.h"
68 #include "contiki-net.h"
69 #include "contiki-lib.h"
70 
71 #include "dev/rs232.h"
72 #include "dev/serial-line.h"
73 #include "dev/slip.h"
74 
75 #include "sicslowmac.h"
76 
77 FUSES =
78  {
79  .low = (FUSE_CKSEL0 & FUSE_CKSEL2 & FUSE_CKSEL3 & FUSE_SUT0), // 0xe2,
80  .high = (FUSE_BOOTSZ0 /*& FUSE_BOOTSZ1*/ & FUSE_SPIEN & FUSE_JTAGEN), //0x9D,
81  .extended = 0xff,
82  };
83 
84 #if RF230BB
85 //PROCINIT(&etimer_process, &tcpip_process );
86 #else
87 PROCINIT(&etimer_process, &mac_process, &tcpip_process );
88 #endif
89 /* Put default MAC address in EEPROM */
90 uint8_t mac_address[8] EEMEM = {0x02, 0x11, 0x22, 0xff, 0xfe, 0x33, 0x44, 0x55};
91 
92 
93 void
94 init_lowlevel(void)
95 {
96 
97  /* Second rs232 port for debugging */
98  rs232_init(RS232_PORT_1, USART_BAUD_115200,
99  USART_PARITY_NONE | USART_STOP_BITS_1 | USART_DATA_BITS_8);
100 
101  /* Redirect stdout to second port */
102  rs232_redirect_stdout(RS232_PORT_1);
103 
104  /* Clock */
105  clock_init();
106 
107  /* rtimers needed for radio cycling */
108  rtimer_init();
109 
110  /* Initialize process subsystem */
111  process_init();
112  /* etimers must be started before ctimer_init */
113  process_start(&etimer_process, NULL);
114 
115 #if RF230BB
116 
117  ctimer_init();
118  /* Start radio and radio receive process */
119  NETSTACK_RADIO.init();
120 
121  /* Set addresses BEFORE starting tcpip process */
122 
123  rimeaddr_t addr;
124  memset(&addr, 0, sizeof(rimeaddr_t));
125  eeprom_read_block ((void *)&addr.u8, &mac_address, 8);
126 
127 #if UIP_CONF_IPV6
128  memcpy(&uip_lladdr.addr, &addr.u8, 8);
129 #endif
130  rf230_set_pan_addr(IEEE802154_PANID, 0, (uint8_t *)&addr.u8);
131 #ifdef CHANNEL_802_15_4
132  rf230_set_channel(CHANNEL_802_15_4);
133 #else
134  rf230_set_channel(26);
135 #endif
136 
137  rimeaddr_set_node_addr(&addr);
138 
139  PRINTF("MAC address %x:%x:%x:%x:%x:%x:%x:%x\n",addr.u8[0],addr.u8[1],addr.u8[2],addr.u8[3],addr.u8[4],addr.u8[5],addr.u8[6],addr.u8[7]);
140 
141  /* Initialize stack protocols */
142  queuebuf_init();
143  NETSTACK_RDC.init();
144  NETSTACK_MAC.init();
145  NETSTACK_NETWORK.init();
146 
147 #if ANNOUNCE_BOOT
148  printf_P(PSTR("%s %s, channel %u"),NETSTACK_MAC.name, NETSTACK_RDC.name,rf230_get_channel());
149  if (NETSTACK_RDC.channel_check_interval) {//function pointer is zero for sicslowmac
150  unsigned short tmp;
151  tmp=CLOCK_SECOND / (NETSTACK_RDC.channel_check_interval == 0 ? 1:\
152  NETSTACK_RDC.channel_check_interval());
153  if (tmp<65535) printf_P(PSTR(", check rate %u Hz"),tmp);
154  }
155  printf_P(PSTR("\n"));
156 #endif
157 
158 #if UIP_CONF_ROUTER
159 #if ANNOUNCE_BOOT
160  printf_P(PSTR("Routing Enabled\n"));
161 #endif
162  rime_init(rime_udp_init(NULL));
163  uip_router_register(&rimeroute);
164 #endif
165 
166  process_start(&tcpip_process, NULL);
167 
168 #else
169 /* mac process must be started before tcpip process! */
170  process_start(&mac_process, NULL);
171  process_start(&tcpip_process, NULL);
172 #endif /*RF230BB*/
173 
174 }
175 
176 
177 int
178 main(void)
179 {
180  //calibrate_rc_osc_32k(); //CO: Had to comment this out
181 
182  /* Initialize hardware */
183  init_lowlevel();
184 
185  /* Register initial processes */
186 // procinit_init();
187 
188  /* Autostart processes */
189  autostart_start(autostart_processes);
190 
191  printf_P(PSTR("\n********BOOTING CONTIKI*********\n"));
192 
193  printf_P(PSTR("System online.\n"));
194 
195  /* Main scheduler loop */
196  while(1) {
197  process_run();
198  }
199 
200  return 0;
201 }