Contiki 3.x
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/ipv6/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 #if NETSTACK_CONF_WITH_IPV6 || NETSTACK_CONF_WITH_IPV4
86 //PROCINIT(&etimer_process, &tcpip_process );
87 #else
88 //PROCINIT(&etimer_process );
89 #endif
90 #else
91 #if NETSTACK_CONF_WITH_IPV6 || NETSTACK_CONF_WITH_IPV4
92 PROCINIT(&etimer_process, &mac_process, &tcpip_process );
93 #else
94 PROCINIT(&etimer_process, &mac_process );
95 #endif
96 #endif
97 /* Put default MAC address in EEPROM */
98 #if MY_NODE_ID
99 uint8_t mac_address[8] EEMEM = {0x02, 0x11, 0x22, 0xff, 0xfe, 0x33, 0x44,
100  MY_NODE_ID};
101 #else
102 uint8_t mac_address[8] EEMEM = {0x02, 0x11, 0x22, 0xff, 0xfe, 0x33, 0x44, 0x55};
103 #endif
104 
105 void
106 init_lowlevel(void)
107 {
108 
109  /* Second rs232 port for debugging */
110  rs232_init(RS232_PORT_1, USART_BAUD_38400,
111  USART_PARITY_NONE | USART_STOP_BITS_1 | USART_DATA_BITS_8);
112 
113  /* Redirect stdout to second port */
114  rs232_redirect_stdout(RS232_PORT_1);
115 
116  /* Clock */
117  clock_init();
118 
119  /* rtimers needed for radio cycling */
120  rtimer_init();
121 
122  /* Initialize process subsystem */
123  process_init();
124  /* etimers must be started before ctimer_init */
125  process_start(&etimer_process, NULL);
126 
127 #if RF230BB
128 
129  ctimer_init();
130  /* Start radio and radio receive process */
131  NETSTACK_RADIO.init();
132 
133  /* Set addresses BEFORE starting tcpip process */
134 
135  linkaddr_t addr;
136  memset(&addr, 0, sizeof(linkaddr_t));
137  eeprom_read_block ((void *)&addr.u8, &mac_address, 8);
138 
139 #if NETSTACK_CONF_WITH_IPV6
140  memcpy(&uip_lladdr.addr, &addr.u8, 8);
141 #endif
142  rf230_set_pan_addr(IEEE802154_PANID, 0, (uint8_t *)&addr.u8);
143 #ifdef CHANNEL_802_15_4
144  rf230_set_channel(CHANNEL_802_15_4);
145 #else
146  rf230_set_channel(26);
147 #endif
148 
149  linkaddr_set_node_addr(&addr);
150 
151  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]);
152 
153  /* Initialize stack protocols */
154  queuebuf_init();
155  NETSTACK_RDC.init();
156  NETSTACK_MAC.init();
157  NETSTACK_NETWORK.init();
158 
159 #if ANNOUNCE_BOOT
160  printf_P(PSTR("%s %s, channel %u"),NETSTACK_MAC.name, NETSTACK_RDC.name,rf230_get_channel());
161  if (NETSTACK_RDC.channel_check_interval) {//function pointer is zero for sicslowmac
162  unsigned short tmp;
163  tmp=CLOCK_SECOND / (NETSTACK_RDC.channel_check_interval == 0 ? 1:\
164  NETSTACK_RDC.channel_check_interval());
165  if (tmp<65535) printf_P(PSTR(", check rate %u Hz"),tmp);
166  }
167  printf_P(PSTR("\n"));
168 #endif
169 
170 #if UIP_CONF_ROUTER
171 #if ANNOUNCE_BOOT
172  printf_P(PSTR("Routing Enabled\n"));
173 #endif
174  //rime_init(rime_udp_init(NULL));
175  //uip_router_register(&rimeroute);
176 #endif
177 #if NETSTACK_CONF_WITH_IPV6 || NETSTACK_CONF_WITH_IPV4
178  process_start(&tcpip_process, NULL);
179 #endif
180 #else
181 /* mac process must be started before tcpip process! */
182  process_start(&mac_process, NULL);
183 #if NETSTACK_CONF_WITH_IPV6 || NETSTACK_CONF_WITH_IPV4
184  process_start(&tcpip_process, NULL);
185 #endif
186 #endif /*RF230BB*/
187 
188 }
189 
190 
191 int
192 main(void)
193 {
194  //calibrate_rc_osc_32k(); //CO: Had to comment this out
195 
196  /* Initialize hardware */
197  init_lowlevel();
198 
199  /* Register initial processes */
200 // procinit_init();
201 
202  printf_P(PSTR("\n********BOOTING CONTIKI*********\n"));
203 
204  printf_P(PSTR("System online.\n"));
205 
206  /* Autostart processes */
207  autostart_start(autostart_processes);
208 
209  /* Main scheduler loop */
210  while(1) {
211  process_run();
212  }
213 
214  return 0;
215 }
MAC driver header file
static uip_ds6_addr_t * addr
Pointer to a router list entry.
Definition: uip-nd6.c:124
Header file for the managed memory allocator
This file contains radio driver code.
void clock_init(void)
Initialize the clock library.
Definition: clock.c:76
Example glue code between the existing MAC code and the Contiki mac interface.
int process_run(void)
Run the system once - call poll handlers and process one event.
Definition: process.c:302
#define NULL
The null pointer.
802.15.4 frame creation and parsing functions
void rs232_init(void)
Initialize the RS232 module.
Definition: rs232.c:51
void linkaddr_set_node_addr(linkaddr_t *t)
Set the address of the current node.
Definition: linkaddr.c:72
#define CLOCK_SECOND
A second, measured in system clock time.
Definition: clock.h:82
void process_start(struct process *p, process_data_t data)
Start a process.
Definition: process.c:99
int main(void)
This is main...
void rtimer_init(void)
Initialize the real-time scheduler.
Definition: rtimer.c:61
Generic serial I/O process header filer.
CCIF uip_lladdr_t uip_lladdr
Host L2 address.
Definition: uip.c:118
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
Header file for the 6lowpan implementation (RFC4944 and draft-hui-6lowpan-hc-01) ...
A MAC framer for IEEE 802.15.4