Contiki 2.5
contiki-stk501-main.c
Go to the documentation of this file.
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 /**
35  * \file
36  * Sample Contiki kernel for STK 501 development board
37  *
38  * \author
39  * Simon Barner <barner@in.tum.de
40  */
41 
42  /* Patched to allow hello-world ipv4 and ipv6 build */
43 
44 #include <avr/pgmspace.h>
45 #include <stdio.h>
46 #include "net/uip_arp.h"
47 
48 #include "contiki-stk501.h"
49 //#include "../core/cfs/cfs-eeprom.h"
50 #include "cfs/cfs.h"
51 #include "dev/eeprom.h"
52 #include "lib/mmem.h"
53 #include "loader/symbols-def.h"
54 #include "loader/symtab.h"
55 #include "../apps/codeprop/codeprop.h"
56 #include "sys/mt.h"
57 
58 /* Uncomment to enable demonstration of multi-threading libary */
59 /* #define MT_DEMO */
60 
61 //TODO: What happened to cfs_eeprom_process?
62 //PROCINIT(&etimer_process, &tcpip_process, &uip_fw_process, &cfs_eeprom_process);
63 #if UIP_CONF_IPV6
64 PROCINIT(&etimer_process, &tcpip_process);
65 #else
66 PROCINIT(&etimer_process, &tcpip_process, &uip_fw_process);
67 #endif
68 
69 #ifdef MT_DEMO
70 static struct mt_thread threads[3];
71 
72 static
73 void thread_handler1 (void* data) {
74  while (1) {
75  rs232_print_p (RS232_PORT_1, PSTR ("Thread 1. Data: ") );
76  rs232_printf (RS232_PORT_1, "0x%x, %d\n", data, *(uint8_t*)data );
77  mt_yield ();
78  }
79 }
80 
81 static
82 void thread_handler2 (void* data) {
83  while (1) {
84  rs232_print_p (RS232_PORT_1, PSTR ("Thread 2. Data: "));
85  rs232_printf (RS232_PORT_1, "0x%x, %d\n", data, *(uint8_t*)data );
86  mt_yield ();
87  }
88 }
89 #endif
90 
91 PROCESS(contiki_stk501_main_init_process, "Contiki STK501 init process");
92 PROCESS_THREAD(contiki_stk501_main_init_process, ev, data)
93 {
94  PROCESS_BEGIN();
95 
96  /* Network support (uIP) */
97  init_net();
98 
99  /* Initalize heap allocator */
100  mmem_init ();
101 
102  /* Code propagator */
103  /* TODO: The core elfloader-avr.c has 16/32 bit pointer problems so this won't build */
104 //process_start(&codeprop_process, NULL);
105 
106  /* Multi-threading support */
107 #ifdef MT_DEMO
108  mt_init ();
109 #endif
110 
111  PROCESS_END();
112 }
113 
114 #ifdef MT_DEMO
115 static uint8_t d1=1, d2=2, d3=3;
116 #endif
117 
118 int
119 main(void)
120 {
121  /*
122  * GCC depends on register r1 set to 0.
123  */
124  asm volatile ("clr r1");
125 
126  /* Initialize hardware */
127  init_lowlevel();
128 
129  /* Clock */
130  clock_init();
131 
132  /* Process subsystem */
133  process_init();
134 
135  /* Register initial processes */
136  procinit_init();
137 
138  /* Perform rest of initializations */
139  process_start(&contiki_stk501_main_init_process, NULL);
140 
141  rs232_print_p (RS232_PORT_1, PSTR ("Initialized.\n"));
142 
143 #ifdef MT_DEMO
144  mt_start (&threads[0], thread_handler1, &d1);
145  mt_start (&threads[1], thread_handler2, &d2);
146  mt_start (&threads[2], thread_handler2, &d3);
147 
148  uint8_t i;
149 #endif
150 
151  /* Main scheduler loop */
152  while(1) {
153 
154  process_run();
155 
156 #ifdef MT_DEMO
157  for (i=0; i<3; ++i) {
158  mt_exec (&threads[i]);
159  }
160 #endif
161  }
162 
163  return 0;
164 }