Allow build with original radio driver using $make RF230BB=0
[contiki-2.x.git] / platform / avr-raven / contiki-raven-main.c
blob609c7260dbd801abbe507c1bed1dbf8dc56f8178
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 * @(#)$$
33 #define DEBUG 1
34 #if DEBUG
35 #define PRINTF(FORMAT,args...) printf_P(PSTR(FORMAT),##args)
36 #define PRINTSHORT(FORMAT,args...) printf_P(PSTR(FORMAT),##args)
37 int pingtimer1=0,pingtimer2=0;
38 #if RF230BB
39 extern int rf230_interrupt_flag;
40 #endif
41 #else
42 #define PRINTF(...)
43 #define PRINTSHORT(...)
44 #endif
46 #include <avr/pgmspace.h>
47 #include <avr/fuse.h>
48 #include <avr/eeprom.h>
49 #include <stdio.h>
50 #include <string.h>
52 //#include "lib/mmem.h"
53 #include "loader/symbols-def.h"
54 #include "loader/symtab.h"
56 #if RF230BB //radio driver using contiki core mac
57 #include "radio/rf230bb/rf230bb.h"
58 #include "net/mac/frame802154.h"
59 #include "net/mac/framer-802154.h"
60 //#include "net/mac/framer-nullmac.h"
61 //#include "net/mac/framer.h"
62 #include "net/sicslowpan.h"
63 //#include "net/uip-netif.h"
64 //#include "net/mac/lpp.h"
65 //#include "net/mac/cxmac.h"
66 //#include "net/mac/sicslowmac.h"
67 //#include "dev/xmem.h"
68 //#include "net/rime.h"
70 #if 0
71 #if WITH_NULLMAC
72 #define MAC_DRIVER nullmac_driver
73 #endif /* WITH_NULLMAC */
75 #ifndef MAC_DRIVER
76 #ifdef MAC_CONF_DRIVER
77 #define MAC_DRIVER MAC_CONF_DRIVER
78 #else
79 #define MAC_DRIVER sicslowmac1_driver
80 //#define MAC_DRIVER cxmac_driver
81 #endif /* MAC_CONF_DRIVER */
82 #endif /* MAC_DRIVER */
83 #endif
85 #else //radio driver using Atmel/Cisco 802.15.4'ish MAC
86 #include <stdbool.h>
87 #include "mac.h"
88 #include "sicslowmac.h"
89 #include "sicslowpan.h"
90 #include "ieee-15-4-manager.h"
91 #endif /*RF230BB*/
93 #include "contiki.h"
94 #include "contiki-net.h"
95 #include "contiki-lib.h"
97 #include "dev/rs232.h"
98 #include "dev/serial-line.h"
99 #include "dev/slip.h"
101 #ifdef RAVEN_LCD_INTERFACE
102 #include "raven-lcd.h"
103 #endif
105 #if WEBSERVER
106 #include "httpd-fs.h"
107 #include "httpd-cgi.h"
108 #endif
110 #ifdef COFFEE_FILES
111 #include "cfs/cfs.h"
112 #include "cfs/cfs-coffee.h"
113 #endif
115 #if UIP_CONF_ROUTER
116 #include "net/routing/rimeroute.h"
117 #include "net/rime/rime-udp.h"
118 #endif
120 #include "net/rime.h"
121 //#include "node-id.h"
123 /*-------------------------------------------------------------------------*/
124 /*----------------------Configuration of the .elf file---------------------*/
125 typedef struct {unsigned char B2;unsigned char B1;unsigned char B0;} __signature_t;
126 #define SIGNATURE __signature_t __signature __attribute__((section (".signature")))
127 SIGNATURE = {
128 /* Older AVR-GCCs may not define the SIGNATURE_n bytes so use explicit 1284p values */
129 .B2 = 0x05,//SIGNATURE_2,
130 .B1 = 0x97,//SIGNATURE_1,
131 .B0 = 0x1E,//SIGNATURE_0,
133 FUSES ={.low = 0xe2, .high = 0x99, .extended = 0xff,};
135 /* Put default MAC address in EEPROM */
136 #if WEBSERVER
137 extern uint8_t mac_address[8]; //These are defined in httpd-fsdata.c via makefsdata.h
138 extern uint8_t server_name[16];
139 extern uint8_t domain_name[30];
140 #else
141 uint8_t mac_address[8] EEMEM = {0x02, 0x11, 0x22, 0xff, 0xfe, 0x33, 0x44, 0x55};
142 #endif
144 /*-------------------------Low level initialization------------------------*/
145 /*------Done in a subroutine to keep main routine stack usage small--------*/
146 void initialize(void)
148 //calibrate_rc_osc_32k(); //CO: Had to comment this out
150 #ifdef RAVEN_LCD_INTERFACE
151 /* First rs232 port for Raven 3290 port */
152 rs232_init(RS232_PORT_0, USART_BAUD_38400,USART_PARITY_NONE | USART_STOP_BITS_1 | USART_DATA_BITS_8);
153 /* Set input handler for 3290 port */
154 rs232_set_input(0,raven_lcd_serial_input);
155 #endif
157 /* Second rs232 port for debugging */
158 rs232_init(RS232_PORT_1, USART_BAUD_57600,USART_PARITY_NONE | USART_STOP_BITS_1 | USART_DATA_BITS_8);
159 /* Redirect stdout to second port */
160 rs232_redirect_stdout(RS232_PORT_1);
161 clock_init();
162 printf_P(PSTR("\n*******Booting %s*******\n"),CONTIKI_VERSION_STRING);
164 /* rtimers not used yet */
165 // rtimer_init();
167 /* Initialize process subsystem */
168 process_init();
169 /* etimers must be started before ctimer_init */
170 process_start(&etimer_process, NULL);
172 #if RF230BB
174 ctimer_init();
175 /* Start radio and radio receive process */
176 rf230_init();
178 /* Set addresses BEFORE starting tcpip process */
180 rimeaddr_t addr;
181 memset(&addr, 0, sizeof(rimeaddr_t));
182 AVR_ENTER_CRITICAL_REGION();
183 eeprom_read_block ((void *)&addr.u8, &mac_address, 8);
184 AVR_LEAVE_CRITICAL_REGION();
186 memcpy(&uip_lladdr.addr, &addr.u8, 8);
187 rf230_set_pan_addr(IEEE802154_PANID, 0, (uint8_t *)&addr.u8);
189 rf230_set_channel(24);
191 rimeaddr_set_node_addr(&addr);
192 // set_rime_addr();
193 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]);
195 framer_set(&framer_802154);
197 /* Setup X-MAC for 802.15.4 */
198 queuebuf_init();
199 NETSTACK_RDC.init(); //prints rs2048
200 NETSTACK_MAC.init();
201 NETSTACK_NETWORK.init();
202 //todo: makes raven reboot
203 // printf(" %s, channel check rate %d Hz, radio channel %u\n",
204 // sicslowpan_mac->name,
205 // CLOCK_SECOND / (sicslowpan_mac->channel_check_interval() == 0? 1:
206 // sicslowpan_mac->channel_check_interval()),
207 // RF_CHANNEL);
209 // uip_ip6addr(&ipprefix, 0xaaaa, 0, 0, 0, 0, 0, 0, 0);
210 // uip_netif_addr_add(&ipprefix, UIP_DEFAULT_PREFIX_LEN, 0, AUTOCONF);
211 // uip_nd6_prefix_add(&ipprefix, UIP_DEFAULT_PREFIX_LEN, 0);
212 // PRINTF("Prefix %x::/%u\n",ipprefix.u16[0],UIP_DEFAULT_PREFIX_LEN);
214 #if UIP_CONF_ROUTER
215 #warning Routing enabled
216 PRINTF("Routing Enabled\n")
217 rime_init(rime_udp_init(NULL));
218 uip_router_register(&rimeroute);
219 #endif
221 // sicslowpan_init(MAC_DRIVER.init(&rf230_driver));
223 // PRINTF("Driver: %s, Channel: %u\n\r", MAC_DRIVER.name, rf230_get_channel());
224 PRINTF("Driver: %s, Channel: %u\n", sicslowpan_mac->name, rf230_get_channel());
226 #endif /*RF230BB*/
228 #if RF230BB
229 process_start(&tcpip_process, NULL);
230 #else
231 /* mac process must be started before tcpip process! */
232 process_start(&mac_process, NULL);
233 process_start(&tcpip_process, NULL);
235 #endif
236 #ifdef RAVEN_LCD_INTERFACE
237 process_start(&raven_lcd_process, NULL);
238 #endif
240 /* Autostart other processes */
241 autostart_start(autostart_processes);
243 //Give ourselves a prefix
244 // init_net();
246 /*---If using coffee file system create initial web content if necessary---*/
247 #if COFFEE_FILES
248 int fa = cfs_open( "/index.html", CFS_READ);
249 if (fa<0) { //Make some default web content
250 printf_P(PSTR("No index.html file found, creating upload.html!\n"));
251 printf_P(PSTR("Formatting FLASH file system for coffee..."));
252 cfs_coffee_format();
253 printf_P(PSTR("Done!\n"));
254 fa = cfs_open( "/index.html", CFS_WRITE);
255 int r = cfs_write(fa, &"It works!", 9);
256 if (r<0) printf_P(PSTR("Can''t create /index.html!\n"));
257 cfs_close(fa);
258 // fa = cfs_open("upload.html"), CFW_WRITE);
259 // <html><body><form action="upload.html" enctype="multipart/form-data" method="post"><input name="userfile" type="file" size="50" /><input value="Upload" type="submit" /></form></body></html>
261 #endif
263 /*--------------------------Announce the configuration---------------------*/
264 #define ANNOUNCE_BOOT 1 //adds about 400 bytes to program size
266 #if ANNOUNCE_BOOT
268 #if WEBSERVER
270 uint8_t i;
271 char buf[80];
272 unsigned int size;
273 eeprom_read_block (buf,server_name, sizeof(server_name));
274 buf[sizeof(server_name)]=0;
275 printf_P(PSTR("%s"),buf);
276 eeprom_read_block (buf,domain_name, sizeof(domain_name));
277 buf[sizeof(domain_name)]=0;
278 size=httpd_fs_get_size();
279 #ifndef COFFEE_FILES
280 printf_P(PSTR(".%s online with fixed %u byte web content\n"),buf,size);
281 #elif COFFEE_FILES==1
282 printf_P(PSTR(".%s online with static %u byte EEPROM file system\n"),buf,size);
283 #elif COFFEE_FILES==2
284 printf_P(PSTR(".%s online with dynamic %u KB EEPROM file system\n"),buf,size>>10);
285 #elif COFFEE_FILES==3
286 printf_P(PSTR(".%s online with static %u byte program memory file system\n"),buf,size);
287 #elif COFFEE_FILES==4
288 printf_P(PSTR(".%s online with dynamic %u KB program memory file system\n"),buf,size>>10);
289 #endif
291 /* Add prefixes for testing */
292 #if 0
294 uip_ip6addr_t ipaddr;
295 uip_ip6addr(&ipaddr, 0xaaaa, 0, 0, 0, 0, 0, 0, 0);
296 uip_netif_addr_autoconf_set(&ipaddr, &uip_lladdr);
297 uip_netif_addr_add(&ipaddr, 16, 0, TENTATIVE);
299 #endif
300 #if 0
302 uip_ip6addr_t ipaddr;
303 uip_ip6addr(&ipaddr, 0xaaaa, 0, 0, 0, 0, 0, 0, 0);
304 uip_netif_addr_add(&ipaddr, UIP_DEFAULT_PREFIX_LEN, 0, AUTOCONF);
305 uip_nd6_prefix_add(&ipaddr, UIP_DEFAULT_PREFIX_LEN, 0);
307 #endif
309 for(i = 0; i < UIP_CONF_NETIF_MAX_ADDRESSES; i ++) {
310 if(uip_netif_physical_if.addresses[i].state != NOT_USED) {
311 httpd_cgi_sprint_ip6(*(uip_ipaddr_t*)&uip_netif_physical_if.addresses[i],buf);
312 printf_P(PSTR("IPv6 Address: %s\n"),buf);
315 #else
316 printf_P(PSTR("Online\n"));
317 #endif /* WEBSERVER */
319 #endif /* ANNOUNCE_BOOT */
322 #if RF230BB
323 extern uint8_t rf230processflag; //for debugging process call problems
324 #endif
325 /*---------------------------------------------------------------------------*/
326 void log_message(char *m1, char *m2)
328 printf_P(PSTR("%s%s\n"), m1, m2);
330 extern uint8_t rtimerworks;
331 /*-------------------------------------------------------------------------*/
332 /*------------------------- Main Scheduler loop----------------------------*/
333 /*-------------------------------------------------------------------------*/
335 main(void)
337 initialize();
338 while(1) {
339 process_run();
340 #if 0
341 if (rtimerworks>200) {
342 printf("%d",rtimerworks);
343 rtimerworks=0;
345 #endif
346 #if RF230BB && 0
347 if (rf230processflag) {
348 printf("rf230p%d",rf230processflag);
349 rf230processflag=0;
351 #endif
353 #if 0
354 if (rf230_interrupt_flag) {
355 // if (rf230_interrupt_flag!=11) {
356 // PRINTF("*****Radio interrupt %u\n",rf230_interrupt_flag);
357 PRINTSHORT("**RI%u",rf230_interrupt_flag);
358 rf230_interrupt_flag=0;
359 // }
361 #endif
362 #if PINGS
363 if (pingtimer1++==10000) {
364 pingtimer1=0;
365 if (pingtimer2++==1000) {
366 PRINTF("-------Ping\n");
367 pingtimer2=0;
368 raven_ping6();
371 #endif
374 return 0;