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  */
33 /*---------------------------------------------------------------------------*/
34 /**
35 * \file
36 * Contiki main file.
37 * \author
38 * Salvatore Pitrulli <salvopitru@users.sourceforge.net>
39 */
40 /*---------------------------------------------------------------------------*/
41 
42 
43 #include PLATFORM_HEADER
44 #include "hal/error.h"
45 #include "hal/hal.h"
46 #include BOARD_HEADER
47 #include "micro/adc.h"
48 
49 #include <stdio.h>
50 
51 
52 #include "contiki.h"
53 
54 #include "dev/watchdog.h"
55 #include "dev/leds.h"
56 #include "dev/button-sensor.h"
57 #include "dev/temperature-sensor.h"
58 #include "dev/acc-sensor.h"
59 #include "dev/uart1.h"
60 #include "dev/serial-line.h"
61 
62 #include "dev/stm32w-radio.h"
63 #include "net/netstack.h"
64 #include "net/rime/rimeaddr.h"
65 #include "net/rime.h"
66 #include "net/rime/rime-udp.h"
67 #include "net/uip.h"
68 
69 
70 #define DEBUG 1
71 #if DEBUG
72 #include <stdio.h>
73 #define PRINTF(...) printf(__VA_ARGS__)
74 #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])
75 #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])
76 #else
77 #define PRINTF(...)
78 #define PRINT6ADDR(addr)
79 #define PRINTLLADDR(addr)
80 #endif
81 
82 
83 #if UIP_CONF_IPV6
84 PROCINIT(&etimer_process, &tcpip_process, &sensors_process);
85 #else
86 PROCINIT(&etimer_process, &sensors_process);
87 #warning "No TCP/IP process!"
88 #endif
89 
90 SENSORS(&button_sensor,&temperature_sensor,&acc_sensor);
91 
92 /*---------------------------------------------------------------------------*/
93 static void
94 set_rime_addr(void)
95 {
96  int i;
97  union {
98  u8_t u8[8];
99  }eui64;
100 
101  //rimeaddr_t lladdr;
102 
103  int8u *stm32w_eui64 = ST_RadioGetEui64();
104  {
105  int8u c;
106  for(c = 0; c < 8; c++) { // Copy the EUI-64 to lladdr converting from Little-Endian to Big-Endian.
107  eui64.u8[c] = stm32w_eui64[7 - c];
108  }
109  }
110  PRINTF("\n\rRadio EUI-64:");
111  PRINTLLADDR(eui64);
112  PRINTF("\n\r");
113 
114 #if UIP_CONF_IPV6
115  memcpy(&uip_lladdr.addr, &eui64, sizeof(uip_lladdr.addr));
116 #endif
117 
118 #if UIP_CONF_IPV6
119  rimeaddr_set_node_addr((rimeaddr_t *)&eui64);
120 #else
121  rimeaddr_set_node_addr((rimeaddr_t *)&eui64.u8[8-RIMEADDR_SIZE]);
122 #endif
123 
124  printf("Rime started with address ");
125  for(i = 0; i < sizeof(rimeaddr_t) - 1; i++) {
126  printf("%d.", rimeaddr_node_addr.u8[i]);
127  }
128  printf("%d\n", rimeaddr_node_addr.u8[i]);
129 
130 }
131 /*---------------------------------------------------------------------------*/
132 int
133 main(void)
134 {
135 
136  /*
137  * Initialize hardware.
138  */
139  halInit();
140  clock_init();
141 
142  uart1_init(115200);
143 
144  // Led initialization
145  leds_init();
146 
147  INTERRUPTS_ON();
148 
149  PRINTF("\r\nStarting ");
150  PRINTF(CONTIKI_VERSION_STRING);
151  PRINTF(" on %s\r\n",boardDescription->name);
152 
153  /*
154  * Initialize Contiki and our processes.
155  */
156 
157  process_init();
158 
159 #if WITH_SERIAL_LINE_INPUT
160  uart1_set_input(serial_line_input_byte);
161  serial_line_init();
162 #endif
163 
164  netstack_init();
165 #if !UIP_CONF_IPV6
166  ST_RadioEnableAutoAck(FALSE); // Because frames are not 802.15.4 compatible.
167  ST_RadioEnableAddressFiltering(FALSE);
168 #endif
169 
170  set_rime_addr();
171 
172  ctimer_init();
173  rtimer_init();
174 
175  procinit_init();
176 
177  energest_init();
178  ENERGEST_ON(ENERGEST_TYPE_CPU);
179 
180  autostart_start(autostart_processes);
181 
182 
183  watchdog_start();
184 
185  while(1){
186 
187  int r;
188 
189  do {
190  /* Reset watchdog. */
191  watchdog_periodic();
192  r = process_run();
193  } while(r > 0);
194 
195 
196 
197  ENERGEST_OFF(ENERGEST_TYPE_CPU);
198  //watchdog_stop();
199  ENERGEST_ON(ENERGEST_TYPE_LPM);
200  /* Go to idle mode. */
201  halSleepWithOptions(SLEEPMODE_IDLE,0);
202  /* We are awake. */
203  //watchdog_start();
204  ENERGEST_OFF(ENERGEST_TYPE_LPM);
205  ENERGEST_ON(ENERGEST_TYPE_CPU);
206 
207  }
208 
209 }
210 
211 
212 
213 /*int8u errcode __attribute__(( section(".noinit") ));
214 
215 void halBaseBandIsr(){
216 
217  errcode = 1;
218  leds_on(LEDS_RED);
219 }
220 
221 void BusFault_Handler(){
222 
223  errcode = 2;
224  leds_on(LEDS_RED);
225 }
226 
227 void halDebugIsr(){
228 
229  errcode = 3;
230  leds_on(LEDS_RED);
231 }
232 
233 void DebugMon_Handler(){
234 
235  errcode = 4;
236  //leds_on(LEDS_RED);
237 }
238 
239 void HardFault_Handler(){
240 
241  errcode = 5;
242  //leds_on(LEDS_RED);
243  //halReboot();
244 }
245 
246 void MemManage_Handler(){
247 
248  errcode = 6;
249  //leds_on(LEDS_RED);
250  //halReboot();
251 }
252 
253 void UsageFault_Handler(){
254 
255  errcode = 7;
256  //leds_on(LEDS_RED);
257  //halReboot();
258 }*/
259 
260 void Default_Handler()
261 {
262  //errcode = 8;
263  leds_on(LEDS_RED);
264  halReboot();
265 }