Contiki 2.5
contiki-main.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010, STMicroelectronics.
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
11  * copyright notice, this list of conditions and the following
12  * disclaimer in the documentation and/or other materials provided
13  * with the distribution.
14  * 3. The name of the author may not be used to endorse or promote
15  * products derived from this software without specific prior
16  * written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
19  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
24  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  * This file is part of the Contiki OS
31  *
32  * $Id: contiki-main.c,v 1.2 2010/10/27 14:05:24 salvopitru Exp $
33  */
34 /*---------------------------------------------------------------------------*/
35 /**
36 * \file
37 * Contiki main file.
38 * \author
39 * Salvatore Pitrulli <salvopitru@users.sourceforge.net>
40 */
41 /*---------------------------------------------------------------------------*/
42 
43 
44 #include PLATFORM_HEADER
45 #include "hal/error.h"
46 #include "hal/hal.h"
47 #include BOARD_HEADER
48 #include "micro/adc.h"
49 
50 #include <stdio.h>
51 
52 
53 #include "contiki.h"
54 
55 #include "dev/watchdog.h"
56 #include "dev/leds.h"
57 #include "dev/button-sensor.h"
58 #include "dev/temperature-sensor.h"
59 #include "dev/acc-sensor.h"
60 #include "dev/uart1.h"
61 #include "dev/serial-line.h"
62 
63 #include "dev/stm32w-radio.h"
64 #include "net/netstack.h"
65 #include "net/rime/rimeaddr.h"
66 #include "net/rime.h"
67 #include "net/rime/rime-udp.h"
68 #include "net/uip.h"
69 
70 
71 #define DEBUG 1
72 #if DEBUG
73 #include <stdio.h>
74 #define PRINTF(...) printf(__VA_ARGS__)
75 #define PRINT6ADDR(addr) PRINTF(" %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x ", ((u8_t *)addr)[0], ((u8_t *)addr)[1], ((u8_t *)addr)[2], ((u8_t *)addr)[3], ((u8_t *)addr)[4], ((u8_t *)addr)[5], ((u8_t *)addr)[6], ((u8_t *)addr)[7], ((u8_t *)addr)[8], ((u8_t *)addr)[9], ((u8_t *)addr)[10], ((u8_t *)addr)[11], ((u8_t *)addr)[12], ((u8_t *)addr)[13], ((u8_t *)addr)[14], ((u8_t *)addr)[15])
76 #define PRINTLLADDR(lladdr) PRINTF(" %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x ",lladdr.u8[0], lladdr.u8[1], lladdr.u8[2], lladdr.u8[3],lladdr.u8[4], lladdr.u8[5], lladdr.u8[6], lladdr.u8[7])
77 #else
78 #define PRINTF(...)
79 #define PRINT6ADDR(addr)
80 #define PRINTLLADDR(addr)
81 #endif
82 
83 
84 #if UIP_CONF_IPV6
85 PROCINIT(&etimer_process, &tcpip_process, &sensors_process);
86 #else
87 PROCINIT(&etimer_process, &sensors_process);
88 #warning "No TCP/IP process!"
89 #endif
90 
91 SENSORS(&button_sensor,&temperature_sensor,&acc_sensor);
92 
93 /*---------------------------------------------------------------------------*/
94 static void
95 set_rime_addr(void)
96 {
97  int i;
98  union {
99  u8_t u8[8];
100  }eui64;
101 
102  //rimeaddr_t lladdr;
103 
104  int8u *stm32w_eui64 = ST_RadioGetEui64();
105  {
106  int8u c;
107  for(c = 0; c < 8; c++) { // Copy the EUI-64 to lladdr converting from Little-Endian to Big-Endian.
108  eui64.u8[c] = stm32w_eui64[7 - c];
109  }
110  }
111  PRINTF("\n\rRadio EUI-64:");
112  PRINTLLADDR(eui64);
113  PRINTF("\n\r");
114 
115 #if UIP_CONF_IPV6
116  memcpy(&uip_lladdr.addr, &eui64, sizeof(uip_lladdr.addr));
117 #endif
118 
119 #if UIP_CONF_IPV6
120  rimeaddr_set_node_addr((rimeaddr_t *)&eui64);
121 #else
122  rimeaddr_set_node_addr((rimeaddr_t *)&eui64.u8[8-RIMEADDR_SIZE]);
123 #endif
124 
125  printf("Rime started with address ");
126  for(i = 0; i < sizeof(rimeaddr_t) - 1; i++) {
127  printf("%d.", rimeaddr_node_addr.u8[i]);
128  }
129  printf("%d\n", rimeaddr_node_addr.u8[i]);
130 
131 }
132 /*---------------------------------------------------------------------------*/
133 int
134 main(void)
135 {
136 
137  /*
138  * Initalize hardware.
139  */
140  halInit();
141  clock_init();
142 
143  uart1_init(115200);
144 
145  // Led initialization
146  leds_init();
147 
148  INTERRUPTS_ON();
149 
150  PRINTF("\r\nStarting ");
151  PRINTF(CONTIKI_VERSION_STRING);
152  PRINTF(" on MB851\r\n");
153 
154  /*
155  * Initialize Contiki and our processes.
156  */
157 
158  process_init();
159 
160 #if WITH_SERIAL_LINE_INPUT
161  uart1_set_input(serial_line_input_byte);
162  serial_line_init();
163 #endif
164 
165  netstack_init();
166 #if !UIP_CONF_IPV6
167  ST_RadioEnableAutoAck(FALSE); // Because frames are not 802.15.4 compatible.
168  ST_RadioEnableAddressFiltering(FALSE);
169 #endif
170 
171  set_rime_addr();
172 
173  ctimer_init();
174  rtimer_init();
175 
176  procinit_init();
177 
178  energest_init();
179  ENERGEST_ON(ENERGEST_TYPE_CPU);
180 
181  autostart_start(autostart_processes);
182 
183 
184  watchdog_start();
185 
186  while(1){
187 
188  int r;
189 
190  do {
191  /* Reset watchdog. */
192  watchdog_periodic();
193  r = process_run();
194  } while(r > 0);
195 
196 
197 
198  ENERGEST_OFF(ENERGEST_TYPE_CPU);
199  //watchdog_stop();
200  ENERGEST_ON(ENERGEST_TYPE_LPM);
201  /* Go to idle mode. */
202  halSleepWithOptions(SLEEPMODE_IDLE,0);
203  /* We are awake. */
204  //watchdog_start();
205  ENERGEST_OFF(ENERGEST_TYPE_LPM);
206  ENERGEST_ON(ENERGEST_TYPE_CPU);
207 
208  }
209 
210 }
211 
212 
213 
214 /*int8u errcode __attribute__(( section(".noinit") ));
215 
216 void halBaseBandIsr(){
217 
218  errcode = 1;
219  leds_on(LEDS_RED);
220 }
221 
222 void BusFault_Handler(){
223 
224  errcode = 2;
225  leds_on(LEDS_RED);
226 }
227 
228 void halDebugIsr(){
229 
230  errcode = 3;
231  leds_on(LEDS_RED);
232 }
233 
234 void DebugMon_Handler(){
235 
236  errcode = 4;
237  //leds_on(LEDS_RED);
238 }
239 
240 void HardFault_Handler(){
241 
242  errcode = 5;
243  //leds_on(LEDS_RED);
244  //halReboot();
245 }
246 
247 void MemManage_Handler(){
248 
249  errcode = 6;
250  //leds_on(LEDS_RED);
251  //halReboot();
252 }
253 
254 void UsageFault_Handler(){
255 
256  errcode = 7;
257  //leds_on(LEDS_RED);
258  //halReboot();
259 }*/
260 
261 void Default_Handler()
262 {
263  //errcode = 8;
264  leds_on(LEDS_RED);
265  halReboot();
266 }