Add conditional boot messages and file logging
[contiki-2.x.git] / platform / avr-raven / contiki-raven-main.c
blob039d834ea739b1edd0569eb812ff5ee6b53e0813
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 0
34 #if DEBUG
35 #define PRINTF(FORMAT,args...) printf_P(PSTR(FORMAT),##args)
36 int pingtimer1=0,pingtimer2=0;
37 #if RF230BB
38 extern int rf230_interrupt_flag;
39 #endif
40 #else
41 #define PRINTF(...)
42 #endif
44 #include <avr/pgmspace.h>
45 #include <avr/fuse.h>
46 #include <avr/eeprom.h>
47 #include <stdio.h>
48 #include <string.h>
50 //#include "lib/mmem.h"
51 #include "loader/symbols-def.h"
52 #include "loader/symtab.h"
54 #ifdef RF230BB //radio driver using contiki core mac
55 #include "radio/rf230bb/rf230bb.h"
56 #include "net/mac/frame802154.h"
57 #include "net/sicslowpan.h"
58 #include "net/uip-netif.h"
59 #include "net/mac/sicslowmac.h"
60 #else //radio driver using Atmel/Cisco 802.15.4'ish MAC
61 #include <stdbool.h>
62 #include "mac.h"
63 #include "sicslowmac.h"
64 #include "sicslowpan.h"
65 #include "ieee-15-4-manager.h"
66 #endif /*RF230BB*/
68 #include "contiki.h"
69 #include "contiki-net.h"
70 #include "contiki-lib.h"
72 #include "dev/rs232.h"
73 #include "dev/serial-line.h"
74 #include "dev/slip.h"
76 #ifdef RAVEN_LCD_INTERFACE
77 #include "raven-lcd.h"
78 #endif
80 #if WEBSERVER
81 #include "httpd-fs.h"
82 #include "httpd-cgi.h"
83 #endif
85 #ifdef COFFEE_FILES
86 #include "cfs/cfs.h"
87 #include "cfs/cfs-coffee.h"
88 #endif
90 #if UIP_CONF_ROUTER
91 #include "net/routing/rimeroute.h"
92 #include "net/rime/rime-udp.h"
93 #endif
95 #include "net/rime.h"
96 //#include "node-id.h"
98 /*-------------------------------------------------------------------------*/
99 /*----------------------Configuration of the .elf file---------------------*/
100 typedef struct {unsigned char B2;unsigned char B1;unsigned char B0;} __signature_t;
101 #define SIGNATURE __signature_t __signature __attribute__((section (".signature")))
102 SIGNATURE = {
103 /* Older AVR-GCCs may not define the SIGNATURE_n bytes so use explicit 1284p values */
104 .B2 = 0x05,//SIGNATURE_2,
105 .B1 = 0x97,//SIGNATURE_1,
106 .B0 = 0x1E,//SIGNATURE_0,
108 FUSES ={.low = 0xe2, .high = 0x99, .extended = 0xff,};
110 /* Put default MAC address in EEPROM */
111 #if WEBSERVER
112 extern uint8_t mac_address[8]; //These are defined in httpd-fsdata.c via makefsdata.h
113 extern uint8_t server_name[16];
114 extern uint8_t domain_name[30];
115 #else
116 uint8_t mac_address[8] EEMEM = {0x02, 0x11, 0x22, 0xff, 0xfe, 0x33, 0x44, 0x55};
117 #endif
119 /*-----------------------Initial contiki processes--------------------------*/
120 #ifdef RAVEN_LCD_INTERFACE
121 #ifdef RF230BB
122 PROCINIT(&etimer_process, &tcpip_process, &raven_lcd_process);
123 #else
124 PROCINIT(&etimer_process, &mac_process, &tcpip_process, &raven_lcd_process);
125 #endif /*RF230BB*/
126 #else
127 #ifdef RF230BB
128 PROCINIT(&etimer_process, &tcpip_process);
129 #elif WEBSERVER //TODO:get hello-world to compile with ipv6
130 PROCINIT(&etimer_process, &mac_process, &tcpip_process);
131 #else
132 PROCINIT(&etimer_process);
133 #endif /*RF230BB*/
134 #endif /*RAVEN_LCD_INTERFACE*/
136 /*-------------------------Low level initialization------------------------*/
137 /*------Done in a subroutine to keep main routine stack usage small--------*/
138 void initialize(void)
140 //calibrate_rc_osc_32k(); //CO: Had to comment this out
142 #ifdef RAVEN_LCD_INTERFACE
143 /* First rs232 port for Raven 3290 port */
144 rs232_init(RS232_PORT_0, USART_BAUD_38400,USART_PARITY_NONE | USART_STOP_BITS_1 | USART_DATA_BITS_8);
145 /* Set input handler for 3290 port */
146 rs232_set_input(0,raven_lcd_serial_input);
147 #endif
149 /* Second rs232 port for debugging */
150 rs232_init(RS232_PORT_1, USART_BAUD_57600,USART_PARITY_NONE | USART_STOP_BITS_1 | USART_DATA_BITS_8);
151 /* Redirect stdout to second port */
152 rs232_redirect_stdout(RS232_PORT_1);
153 clock_init();
154 printf_P(PSTR("\n*******Booting %s*******\n"),CONTIKI_VERSION_STRING);
156 /* Initialize process subsystem */
157 process_init();
159 #ifdef RF230BB
161 /* Start radio and radio receive process */
162 rf230_init();
163 sicslowpan_init(sicslowmac_init(&rf230_driver));
164 // ctimer_init();
165 // sicslowpan_init(lpp_init(&rf230_driver));
166 // rime_init(sicslowmac_driver.init(&rf230_driver));
167 // rime_init(lpp_init(&rf230_driver));
169 /* Set addresses BEFORE starting tcpip process */
171 rimeaddr_t addr;
172 memset(&addr, 0, sizeof(rimeaddr_t));
173 AVR_ENTER_CRITICAL_REGION();
174 eeprom_read_block ((void *)&addr.u8, &mac_address, 8);
175 AVR_LEAVE_CRITICAL_REGION();
177 memcpy(&uip_lladdr.addr, &addr.u8, 8);
178 rf230_set_pan_addr(IEEE802154_PANID, 0, (uint8_t *)&addr.u8);
180 rf230_set_channel(24);
181 rimeaddr_set_node_addr(&addr);
182 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]);
184 // uip_ip6addr(&ipprefix, 0xaaaa, 0, 0, 0, 0, 0, 0, 0);
185 // uip_netif_addr_add(&ipprefix, UIP_DEFAULT_PREFIX_LEN, 0, AUTOCONF);
186 // uip_nd6_prefix_add(&ipprefix, UIP_DEFAULT_PREFIX_LEN, 0);
187 // PRINTF("Prefix %x::/%u\n",ipprefix.u16[0],UIP_DEFAULT_PREFIX_LEN);
189 #if UIP_CONF_ROUTER
190 rime_init(rime_udp_init(NULL));
191 uip_router_register(&rimeroute);
192 #endif
194 PRINTF("Driver: %s, Channel: %u\n", sicslowmac_driver.name, rf230_get_channel());
196 #endif /*RF230BB*/
198 /* Register initial processes */
199 procinit_init();
201 /* Autostart processes */
202 autostart_start(autostart_processes);
204 //Give ourselves a prefix
205 // init_net();
207 /*---If using coffee file system create initial web content if necessary---*/
208 #if COFFEE_FILES
209 int fa = cfs_open( "/index.html", CFS_READ);
210 if (fa<0) { //Make some default web content
211 printf_P(PSTR("No index.html file found, creating upload.html!\n"));
212 printf_P(PSTR("Formatting FLASH file system for coffee..."));
213 cfs_coffee_format();
214 printf_P(PSTR("Done!\n"));
215 fa = cfs_open( "/index.html", CFS_WRITE);
216 int r = cfs_write(fa, &"It works!", 9);
217 if (r<0) printf_P(PSTR("Can''t create /index.html!\n"));
218 cfs_close(fa);
219 // fa = cfs_open("upload.html"), CFW_WRITE);
220 // <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>
222 #endif
224 /*--------------------------Announce the configuration---------------------*/
225 #define ANNOUNCE_BOOT 1 //adds about 400 bytes to program size
226 #if ANNOUNCE_BOOT
228 #if WEBSERVER
230 uint8_t i;
231 char buf[80];
232 unsigned int size;
233 eeprom_read_block (buf,server_name, sizeof(server_name));
234 buf[sizeof(server_name)]=0;
235 printf_P(PSTR("%s"),buf);
236 eeprom_read_block (buf,domain_name, sizeof(domain_name));
237 buf[sizeof(domain_name)]=0;
238 size=httpd_fs_get_size();
239 #ifndef COFFEE_FILES
240 printf_P(PSTR(".%s online with fixed %u byte web content\n"),buf,size);
241 #elif COFFEE_FILES==1
242 printf_P(PSTR(".%s online with static %u byte EEPROM file system\n"),buf,size);
243 #elif COFFEE_FILES==2
244 printf_P(PSTR(".%s online with dynamic %u KB EEPROM file system\n"),buf,size>>10);
245 #elif COFFEE_FILES==3
246 printf_P(PSTR(".%s online with static %u byte program memory file system\n"),buf,size);
247 #elif COFFEE_FILES==4
248 printf_P(PSTR(".%s online with dynamic %u KB program memory file system\n"),buf,size>>10);
249 #endif
251 /* Add prefixes for testing */
252 #if 0
254 uip_ip6addr_t ipaddr;
255 uip_ip6addr(&ipaddr, 0xaaaa, 0, 0, 0, 0, 0, 0, 0);
256 uip_netif_addr_autoconf_set(&ipaddr, &uip_lladdr);
257 uip_netif_addr_add(&ipaddr, 16, 0, TENTATIVE);
259 #endif
260 #if 0
262 uip_ip6addr_t ipaddr;
263 uip_ip6addr(&ipaddr, 0xaaaa, 0, 0, 0, 0, 0, 0, 0);
264 uip_netif_addr_add(&ipaddr, UIP_DEFAULT_PREFIX_LEN, 0, AUTOCONF);
265 uip_nd6_prefix_add(&ipaddr, UIP_DEFAULT_PREFIX_LEN, 0);
267 #endif
269 for(i = 0; i < UIP_CONF_NETIF_MAX_ADDRESSES; i ++) {
270 if(uip_netif_physical_if.addresses[i].state != NOT_USED) {
271 httpd_cgi_sprint_ip6(*(uip_ipaddr_t*)&uip_netif_physical_if.addresses[i],buf);
272 printf_P(PSTR("IPv6 Address: %s\n"),buf);
275 #else
276 printf_P(PSTR("Online\n"));
277 #endif /* WEBSERVER */
279 #endif /* ANNOUNCE_BOOT */
282 /*---------------------------------------------------------------------------*/
283 void log_message(char *m1, char *m2)
285 printf_P(PSTR("%s%s\n"), m1, m2);
288 /*-------------------------------------------------------------------------*/
289 /*------------------------- Main Scheduler loop----------------------------*/
290 /*-------------------------------------------------------------------------*/
292 main(void)
294 initialize();
295 while(1) {
296 process_run();
298 #if DEBUG
299 if (rf230_interrupt_flag) {
300 if (rf230_interrupt_flag!=11) {
301 PRINTF("*****Radio interrupt %u\n",rf230_interrupt_flag);
302 rf230_interrupt_flag=0;
305 #endif
306 #if PINGS
307 if (pingtimer1++==10000) {
308 pingtimer1=0;
309 if (pingtimer2++==1000) {
310 PRINTF("-------Ping\n");
311 pingtimer2=0;
312 raven_ping6();
315 #endif
318 return 0;