Update jackdaw to new radio driver interface
[contiki-2.x.git] / platform / avr-ravenusb / contiki-raven-main.c
blobd6f6e072215cce2b05a795ff1f2584db7eed4629
1 /*
2 * Copyright (c) 2006, Technical University of Munich
3 * All rights reserved.
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.
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.
29 * This file is part of the Contiki operating system.
31 * @(#)$$
34 /**
35 * \file
36 * Sample Contiki kernel for STK 501 development board
38 * \author
39 * Simon Barner <barner@in.tum.de
42 #include <avr/pgmspace.h>
43 #include <avr/fuse.h>
44 #include <avr/eeprom.h>
45 #include <util/delay.h>
46 #include <stdio.h>
47 #include <string.h>
49 /* Set ANNOUNCE to send boot messages to USB serial port */
50 #define ANNOUNCE 1
52 #if RF230BB //radio driver using contiki core mac
53 #include "radio/rf230bb/rf230bb.h"
54 #include "net/mac/frame802154.h"
55 #include "net/mac/framer-802154.h"
56 //#include "net/mac/framer-nullmac.h"
57 //#include "net/mac/framer.h"
58 #include "net/sicslowpan.h"
59 #include "net/uip-netif.h"
60 #include "net/mac/lpp.h"
61 //#include "dev/xmem.h"
63 #if WITH_NULLMAC
64 #define MAC_DRIVER nullmac_driver
65 #endif /* WITH_NULLMAC */
67 #ifndef MAC_DRIVER
68 #ifdef MAC_CONF_DRIVER
69 #define MAC_DRIVER MAC_CONF_DRIVER
70 #else
71 #define MAC_DRIVER sicslowmac_driver
72 //#define MAC_DRIVER cxmac_driver
73 #endif /* MAC_CONF_DRIVER */
74 #endif /* MAC_DRIVER */
76 #include "net/mac/sicslowmac.h"
77 #include "net/mac/cxmac.h"
78 #else //radio driver using Atmel/Cisco 802.15.4'ish MAC
79 #include <stdbool.h>
80 #include "mac.h"
81 #include "sicslowmac.h"
82 #include "sicslowpan.h"
83 #include "ieee-15-4-manager.h"
84 #endif /*RF230BB*/
86 #include "lib/mmem.h"
87 #include "loader/symbols-def.h"
88 #include "loader/symtab.h"
90 #include "contiki.h"
91 #include "contiki-net.h"
92 #include "contiki-lib.h"
93 #include "contiki-raven.h"
95 #include "usb_task.h"
96 #include "serial/cdc_task.h"
97 #include "rndis/rndis_task.h"
98 #include "storage/storage_task.h"
100 #if RF230BB
101 //#warning Experimental RF230BB radio selected
102 #define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN])
103 extern int rf230_interrupt_flag;
104 extern uint8_t rf230processflag;
105 #endif /* RF230BB */
107 #if 1 //dummy tcpip process not needed?
108 PROCESS(tcpip_process, "tcpip dummy");
109 PROCESS_THREAD(tcpip_process, ev, data)
111 PROCESS_BEGIN();
112 PROCESS_END();
114 void
115 tcpip_ipv6_output(void)
117 printf("tcpipipv6output");
119 #endif
122 /*----------------------Configuration of the .elf file---------------------*/
123 typedef struct {unsigned char B2;unsigned char B1;unsigned char B0;} __signature_t;
124 #define SIGNATURE __signature_t __signature __attribute__((section (".signature")))
125 SIGNATURE = {
126 /* Older AVR-GCCs may not define the SIGNATURE_n bytes so use explicit values */
127 .B2 = 0x82,//SIGNATURE_2, //AT90USB128x
128 .B1 = 0x97,//SIGNATURE_1, //128KB flash
129 .B0 = 0x1E,//SIGNATURE_0, //Atmel
131 FUSES ={.low = 0xde, .high = 0x99, .extended = 0xff,};
133 /* Put default MAC address in EEPROM */
134 uint8_t mac_address[8] EEMEM = {0x02, 0x12, 0x13, 0xff, 0xfe, 0x14, 0x15, 0x16};
135 //uint8_t EEMEM mac_address[8]; //The raven webserver uses this EEMEM allocation
136 //uint8_t EEMEM server_name[16];
137 //uint8_t EEMEM domain_name[30];
139 #if RF230BB
140 rimeaddr_t macLongAddr;
141 #endif
143 //uint8_t rtimerworks;
145 main(void)
147 #if ANNOUNCE
148 uint32_t firsttime=0;
149 #endif
150 #if RF230BB
151 rimeaddr_t addr;
152 #endif
154 * GCC depends on register r1 set to 0.
156 asm volatile ("clr r1");
158 /* Initialize hardware */
159 init_lowlevel();
161 /* Clock */
162 clock_init();
164 /* rtimer init needed for low power protocols */
165 rtimer_init();
167 /* Process subsystem. */
168 process_init();
170 /* etimer process must be started before ctimer init */
171 process_start(&etimer_process, NULL);
173 #if RF230BB
175 ctimer_init();
176 /* Start radio and radio receive process */
177 /* Note this starts RF230 process, so must be done after process_init */
178 rf230_init();
180 /* Set addresses BEFORE starting tcpip process */
182 memset(&addr, 0, sizeof(rimeaddr_t));
183 AVR_ENTER_CRITICAL_REGION();
184 eeprom_read_block ((void *)&addr.u8, &mac_address, 8);
185 AVR_LEAVE_CRITICAL_REGION();
186 //RNDIS needs the mac address in reverse byte order
187 macLongAddr.u8[0]=addr.u8[7];
188 macLongAddr.u8[1]=addr.u8[6];
189 macLongAddr.u8[2]=addr.u8[5];
190 macLongAddr.u8[3]=addr.u8[4];
191 macLongAddr.u8[4]=addr.u8[3];
192 macLongAddr.u8[5]=addr.u8[2];
193 macLongAddr.u8[6]=addr.u8[1];
194 macLongAddr.u8[7]=addr.u8[0];
196 memcpy(&uip_lladdr.addr, &addr.u8, 8);
197 rf230_set_pan_addr(IEEE802154_PANID, 0, (uint8_t *)&addr.u8);
199 rf230_set_channel(24);
201 rimeaddr_set_node_addr(&addr);
202 // set_rime_addr();
203 // 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]);
205 framer_set(&framer_802154);
207 /* Setup X-MAC for 802.15.4 */
208 queuebuf_init();
209 NETSTACK_RDC.init();
210 NETSTACK_MAC.init();
211 NETSTACK_NETWORK.init();
212 // PRINTF("Driver: %s, Channel: %u\n", sicslowpan_mac->name, rf230_get_channel());
214 #if UIP_CONF_ROUTER
215 rime_init(rime_udp_init(NULL));
216 uip_router_register(&rimeroute);
217 #endif
219 #endif /*RF230BB*/
222 #if RF230BB
223 process_start(&tcpip_process, NULL);
224 #else
225 /* The order of starting these is important! */
226 process_start(&mac_process, NULL);
227 process_start(&tcpip_process, NULL);
229 #endif
231 /* Setup USB */
232 process_start(&usb_process, NULL);
233 process_start(&cdc_process, NULL);
234 process_start(&rndis_process, NULL);
235 process_start(&storage_process, NULL);
237 //Fix MAC address
238 init_net();
240 /* Main scheduler loop */
241 while(1) {
242 process_run();
243 #if 0
244 if (rtimerworks) {
245 printf("i");
246 rtimerworks=0;
248 #endif
250 /* Allow USB CDC to keep up with printfs */
251 #if ANNOUNCE
252 if (firsttime++==36000){
253 printf_P(PSTR("\n\n\n********BOOTING CONTIKI*********\n\r"));
254 #if RF230BB
255 } else if (firsttime==44000) {
256 printf("MAC address %x:%x:%x:%x:%x:%x:%x:%x\n\r",addr.u8[0],addr.u8[1],addr.u8[2],addr.u8[3],addr.u8[4],addr.u8[5],addr.u8[6],addr.u8[7]);
258 } else if (firsttime==52000) {
259 printf("Driver: %s, Channel: %u\n\r", sicslowpan_mac->name, rf230_get_channel());
260 #endif
261 } else if (firsttime==60000) {
262 printf_P(PSTR("System online.\n\r"));
265 #if DEBUG && 0
266 if (rf230processflag) {
267 printf("**RF230 process flag %u\n\r",rf230processflag);
268 rf230processflag=0;
270 if (rf230_interrupt_flag) {
271 // if (rf230_interrupt_flag!=11) {
272 printf("**RF230 Interrupt %u\n\r",rf230_interrupt_flag);
273 // }
274 rf230_interrupt_flag=0;
276 #endif /* DEBUG */
277 #endif /* ANNOUNCE */
280 return 0;