Contiki 2.6

contiki-main.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2010, STMicroelectronics.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above
00011  *    copyright notice, this list of conditions and the following
00012  *    disclaimer in the documentation and/or other materials provided
00013  *    with the distribution.
00014  * 3. The name of the author may not be used to endorse or promote
00015  *    products derived from this software without specific prior
00016  *    written permission.
00017  *
00018  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
00019  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00020  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
00022  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00023  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00024  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00025  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00026  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00027  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00028  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029  *
00030  * This file is part of the Contiki OS
00031  *
00032  */
00033 /*---------------------------------------------------------------------------*/
00034 /**
00035 * \file
00036 *                       Contiki main file.
00037 * \author
00038 *                       Salvatore Pitrulli <salvopitru@users.sourceforge.net>
00039 *                       Chi-Anh La <la@imag.fr>
00040 */
00041 /*---------------------------------------------------------------------------*/
00042 
00043 
00044 #include PLATFORM_HEADER
00045 #include "hal/error.h"
00046 #include "hal/hal.h"
00047 #include BOARD_HEADER
00048 #include "micro/adc.h"
00049 
00050 #include <stdio.h>
00051 
00052 
00053 #include "contiki.h"
00054 
00055 #include "dev/watchdog.h"
00056 #include "dev/leds.h"
00057 #include "dev/button-sensor.h"
00058 #include "dev/temperature-sensor.h"
00059 #include "dev/acc-sensor.h"
00060 #include "dev/uart1.h"
00061 #include "dev/serial-line.h"
00062 
00063 #include "dev/stm32w-radio.h"
00064 #include "net/netstack.h"
00065 #include "net/rime/rimeaddr.h"
00066 #include "net/rime.h"
00067 #include "net/rime/rime-udp.h"
00068 #include "net/uip.h"
00069 #define DEBUG 1
00070 #if DEBUG
00071 #include <stdio.h>
00072 #define PRINTF(...) printf(__VA_ARGS__)
00073 #define PRINT6ADDR(addr) PRINTF(" %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x ", ((uint8_t *)addr)[0], ((uint8_t *)addr)[1], ((uint8_t *)addr)[2], ((uint8_t *)addr)[3], ((uint8_t *)addr)[4], ((uint8_t *)addr)[5], ((uint8_t *)addr)[6], ((uint8_t *)addr)[7], ((uint8_t *)addr)[8], ((uint8_t *)addr)[9], ((uint8_t *)addr)[10], ((uint8_t *)addr)[11], ((uint8_t *)addr)[12], ((uint8_t *)addr)[13], ((uint8_t *)addr)[14], ((uint8_t *)addr)[15])
00074 #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])
00075 #else
00076 #define PRINTF(...)
00077 #define PRINT6ADDR(addr)
00078 #define PRINTLLADDR(addr)
00079 #endif
00080 
00081 
00082 #if UIP_CONF_IPV6
00083 PROCINIT(&tcpip_process, &sensors_process);
00084 #else
00085 PROCINIT(&sensors_process);
00086 #warning "No TCP/IP process!"
00087 #endif
00088 
00089 SENSORS(&button_sensor,&temperature_sensor,&acc_sensor);
00090 
00091 /*---------------------------------------------------------------------------*/
00092 static void
00093 set_rime_addr(void)
00094 {
00095   int i;
00096   union {
00097     uint8_t u8[8];
00098   }eui64;
00099   
00100   //rimeaddr_t lladdr;
00101   
00102   int8u *stm32w_eui64 = ST_RadioGetEui64();
00103   {
00104           int8u c;
00105           for(c = 0; c < 8; c++) {      // Copy the EUI-64 to lladdr converting from Little-Endian to Big-Endian.
00106                   eui64.u8[c] = stm32w_eui64[7 - c];
00107           }
00108   }
00109   
00110 #if UIP_CONF_IPV6
00111   memcpy(&uip_lladdr.addr, &eui64, sizeof(uip_lladdr.addr));
00112 #endif
00113 
00114 #if UIP_CONF_IPV6
00115   rimeaddr_set_node_addr((rimeaddr_t *)&eui64);
00116 #else  
00117   rimeaddr_set_node_addr((rimeaddr_t *)&eui64.u8[8-RIMEADDR_SIZE]);
00118 #endif
00119 
00120   printf("Rime started with address ");
00121   for(i = 0; i < sizeof(rimeaddr_t) - 1; i++) {
00122     printf("%d.", rimeaddr_node_addr.u8[i]);
00123   }
00124   printf("%d\n", rimeaddr_node_addr.u8[i]);
00125   
00126 }
00127 /*---------------------------------------------------------------------------*/
00128 int
00129 main(void)
00130 {
00131   
00132   /*
00133    * Initialize hardware.
00134    */
00135   halInit();
00136   clock_init();
00137   
00138   uart1_init(115200);
00139   
00140   // Led initialization
00141   leds_init();
00142     
00143   INTERRUPTS_ON(); 
00144 
00145   PRINTF("\r\nStarting ");
00146   PRINTF(CONTIKI_VERSION_STRING);
00147   PRINTF(" on %s\r\n",boardDescription->name);
00148 
00149   /*
00150    * Initialize Contiki and our processes.
00151    */
00152   
00153   process_init();
00154   
00155 #if WITH_SERIAL_LINE_INPUT
00156   uart1_set_input(serial_line_input_byte);
00157   serial_line_init();
00158 #endif
00159   /* rtimer and ctimer should be initialized before radio duty cycling layers*/
00160   rtimer_init();
00161   /* etimer_process should be initialized before ctimer */
00162   process_start(&etimer_process, NULL);   
00163   ctimer_init();
00164   
00165   rtimer_init();
00166   netstack_init();
00167   set_rime_addr();
00168 
00169   printf("%s %s, channel check rate %lu Hz\n",
00170          NETSTACK_MAC.name, NETSTACK_RDC.name,
00171          CLOCK_SECOND / (NETSTACK_RDC.channel_check_interval() == 0 ? 1:
00172                                   NETSTACK_RDC.channel_check_interval()));
00173   printf("802.15.4 PAN ID 0x%x, EUI-%d:",
00174       IEEE802154_CONF_PANID, UIP_CONF_LL_802154?64:16);
00175   uip_debug_lladdr_print(&rimeaddr_node_addr);
00176   printf(", radio channel %u\n", RF_CHANNEL);
00177 
00178   procinit_init();    
00179 
00180   energest_init();
00181   ENERGEST_ON(ENERGEST_TYPE_CPU);
00182   
00183   autostart_start(autostart_processes);
00184    
00185   watchdog_start();
00186   
00187   while(1){
00188     
00189     int r;    
00190     
00191     do {
00192       /* Reset watchdog. */
00193       watchdog_periodic();
00194       r = process_run();
00195     } while(r > 0);
00196     
00197     
00198     
00199     ENERGEST_OFF(ENERGEST_TYPE_CPU);
00200     //watchdog_stop();    
00201     ENERGEST_ON(ENERGEST_TYPE_LPM);
00202     /* Go to idle mode. */
00203     halSleepWithOptions(SLEEPMODE_IDLE,0);
00204     /* We are awake. */
00205     //watchdog_start();
00206     ENERGEST_OFF(ENERGEST_TYPE_LPM);
00207     ENERGEST_ON(ENERGEST_TYPE_CPU);  
00208     
00209   }
00210   
00211 }
00212 
00213 
00214 
00215 /*int8u errcode __attribute__(( section(".noinit") ));
00216 
00217 void halBaseBandIsr(){
00218   
00219   errcode = 1;
00220   leds_on(LEDS_RED);
00221 }
00222 
00223 void BusFault_Handler(){
00224   
00225   errcode = 2; 
00226   leds_on(LEDS_RED);
00227 }
00228 
00229 void halDebugIsr(){
00230   
00231   errcode = 3;
00232   leds_on(LEDS_RED);  
00233 }
00234 
00235 void DebugMon_Handler(){
00236   
00237   errcode = 4;
00238   //leds_on(LEDS_RED);  
00239 }
00240 
00241 void HardFault_Handler(){
00242   
00243   errcode = 5; 
00244   //leds_on(LEDS_RED);
00245   //halReboot();
00246 }
00247 
00248 void MemManage_Handler(){
00249   
00250   errcode = 6; 
00251   //leds_on(LEDS_RED);
00252   //halReboot();
00253 }
00254 
00255 void UsageFault_Handler(){
00256   
00257   errcode = 7; 
00258   //leds_on(LEDS_RED);
00259   //halReboot();
00260 }*/
00261 
00262 void Default_Handler() 
00263 { 
00264   //errcode = 8; 
00265   leds_on(LEDS_RED);
00266   halReboot();
00267 }