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  * $Id: contiki-main.c,v 1.2 2010/10/27 14:05:24 salvopitru Exp $
00033  */
00034 /*---------------------------------------------------------------------------*/
00035 /**
00036 * \file
00037 *                       Contiki main file.
00038 * \author
00039 *                       Salvatore Pitrulli <salvopitru@users.sourceforge.net>
00040 *                       Chi-Anh La <la@imag.fr>
00041 */
00042 /*---------------------------------------------------------------------------*/
00043 
00044 
00045 #include PLATFORM_HEADER
00046 #include "hal/error.h"
00047 #include "hal/hal.h"
00048 #include BOARD_HEADER
00049 #include "micro/adc.h"
00050 
00051 #include <stdio.h>
00052 
00053 
00054 #include "contiki.h"
00055 
00056 #include "dev/watchdog.h"
00057 #include "dev/leds.h"
00058 #include "dev/button-sensor.h"
00059 #include "dev/temperature-sensor.h"
00060 #include "dev/acc-sensor.h"
00061 #include "dev/uart1.h"
00062 #include "dev/serial-line.h"
00063 
00064 #include "dev/stm32w-radio.h"
00065 #include "net/netstack.h"
00066 #include "net/rime/rimeaddr.h"
00067 #include "net/rime.h"
00068 #include "net/rime/rime-udp.h"
00069 #include "net/uip.h"
00070 
00071 #if WITH_UIP6
00072 #include "net/uip-ds6.h"
00073 #endif /* WITH_UIP6 */
00074 
00075 #define DEBUG 1
00076 #if DEBUG
00077 #include <stdio.h>
00078 #define PRINTF(...) printf(__VA_ARGS__)
00079 #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])
00080 #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])
00081 #else
00082 #define PRINTF(...)
00083 #define PRINT6ADDR(addr)
00084 #define PRINTLLADDR(addr)
00085 #endif
00086 
00087 
00088 #if UIP_CONF_IPV6
00089 PROCINIT(&tcpip_process, &sensors_process);
00090 #else
00091 PROCINIT(&sensors_process);
00092 #warning "No TCP/IP process!"
00093 #endif
00094 
00095 SENSORS(&button_sensor, &temperature_sensor, &acc_sensor);
00096 
00097 /* The default CCA threshold is set to -77, which is the same as the
00098    default setting on the TI CC2420. */
00099 #define DEFAULT_RADIO_CCA_THRESHOLD -77
00100 
00101 /*---------------------------------------------------------------------------*/
00102 static void
00103 set_rime_addr(void)
00104 {
00105   int i;
00106   union {
00107     uint8_t u8[8];
00108   } eui64;
00109   
00110   int8u *stm32w_eui64 = ST_RadioGetEui64();
00111   {
00112     int8u c;
00113     /* Copy the EUI-64 to lladdr converting from Little-Endian to
00114        Big-Endian. */
00115     for(c = 0; c < 8; c++) {
00116       eui64.u8[c] = stm32w_eui64[7 - c];
00117     }
00118   }
00119 
00120 #if UIP_CONF_IPV6
00121   memcpy(&uip_lladdr.addr, &eui64, sizeof(uip_lladdr.addr));
00122 #endif
00123 
00124 #if UIP_CONF_IPV6
00125   rimeaddr_set_node_addr((rimeaddr_t *)&eui64);
00126 #else
00127   rimeaddr_set_node_addr((rimeaddr_t *)&eui64.u8[8 - RIMEADDR_SIZE]);
00128 #endif
00129 
00130   printf("Rime started with address ");
00131   for(i = 0; i < sizeof(rimeaddr_t) - 1; i++) {
00132     printf("%d.", rimeaddr_node_addr.u8[i]);
00133   }
00134   printf("%d\n", rimeaddr_node_addr.u8[i]);
00135 }
00136 /*---------------------------------------------------------------------------*/
00137 int
00138 main(void)
00139 {
00140   
00141   /*
00142    * Initalize hardware.
00143    */
00144   halInit();
00145   clock_init();
00146   
00147   uart1_init(115200);
00148   
00149   /* Led initialization */
00150   leds_init();
00151     
00152   INTERRUPTS_ON(); 
00153 
00154   PRINTF("\r\nStarting ");
00155   PRINTF(CONTIKI_VERSION_STRING);
00156   PRINTF(" on MB851\r\n"); 
00157 
00158   /*
00159    * Initialize Contiki and our processes.
00160    */
00161   
00162   process_init();
00163   
00164 #if WITH_SERIAL_LINE_INPUT
00165   uart1_set_input(serial_line_input_byte);
00166   serial_line_init();
00167 #endif
00168   /* rtimer and ctimer should be initialized before radio duty cycling
00169      layers */
00170   rtimer_init();
00171   /* etimer_process should be initialized before ctimer */
00172   process_start(&etimer_process, NULL);  
00173   ctimer_init();
00174 
00175   netstack_init();
00176 
00177   set_rime_addr();
00178 
00179   printf("%s %s, channel check rate %lu Hz\n",
00180          NETSTACK_MAC.name, NETSTACK_RDC.name,
00181          CLOCK_SECOND / (NETSTACK_RDC.channel_check_interval() == 0 ? 1:
00182                                   NETSTACK_RDC.channel_check_interval()));
00183   printf("802.15.4 PAN ID 0x%x, EUI-%d:",
00184       IEEE802154_CONF_PANID, UIP_CONF_LL_802154?64:16);
00185   uip_debug_lladdr_print(&rimeaddr_node_addr);
00186   printf(", radio channel %u\n", RF_CHANNEL);
00187 
00188   procinit_init();
00189 
00190   energest_init();
00191   ENERGEST_ON(ENERGEST_TYPE_CPU);
00192 
00193   /* Set the Clear Channel Assessment (CCA) threshold of the
00194      radio. The CCA threshold is used both for sending packets and for
00195      waking up ContikiMAC nodes. If the CCA threshold is too high,
00196      ContikiMAC will not wake up from neighbor transmissions. If the
00197      CCA threshold is too low, transmissions will be too restrictive
00198      and no packets will be sent. DEFAULT_RADIO_CCA_THRESHOLD is
00199      defined in this file. */
00200   ST_RadioSetEdCcaThreshold(DEFAULT_RADIO_CCA_THRESHOLD);
00201   
00202   autostart_start(autostart_processes);
00203 #if UIP_CONF_IPV6
00204   printf("Tentative link-local IPv6 address ");
00205   {
00206     uip_ds6_addr_t *lladdr;
00207     int i;
00208     lladdr = uip_ds6_get_link_local(-1);
00209     for(i = 0; i < 7; ++i) {
00210       printf("%02x%02x:", lladdr->ipaddr.u8[i * 2],
00211              lladdr->ipaddr.u8[i * 2 + 1]);
00212     }
00213     printf("%02x%02x\n", lladdr->ipaddr.u8[14], lladdr->ipaddr.u8[15]);
00214   }
00215 
00216 
00217   if(!UIP_CONF_IPV6_RPL) {
00218     uip_ipaddr_t ipaddr;
00219     int i;
00220     uip_ip6addr(&ipaddr, 0xaaaa, 0, 0, 0, 0, 0, 0, 0);
00221     uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr);
00222     uip_ds6_addr_add(&ipaddr, 0, ADDR_TENTATIVE);
00223     printf("Tentative global IPv6 address ");
00224     for(i = 0; i < 7; ++i) {
00225       printf("%02x%02x:",
00226              ipaddr.u8[i * 2], ipaddr.u8[i * 2 + 1]);
00227     }
00228     printf("%02x%02x\n",
00229            ipaddr.u8[7 * 2], ipaddr.u8[7 * 2 + 1]);
00230   }
00231 #endif /* UIP_CONF_IPV6 */
00232   
00233   watchdog_start();
00234   
00235   while(1) {
00236     
00237     int r;    
00238     
00239     do {
00240       /* Reset watchdog. */
00241       watchdog_periodic();
00242       r = process_run();
00243     } while(r > 0);
00244     
00245     
00246     
00247     ENERGEST_OFF(ENERGEST_TYPE_CPU);
00248     /* watchdog_stop(); */
00249     ENERGEST_ON(ENERGEST_TYPE_LPM);
00250     /* Go to idle mode. */
00251     halSleepWithOptions(SLEEPMODE_IDLE,0);
00252     /* We are awake. */
00253     /* watchdog_start(); */
00254     ENERGEST_OFF(ENERGEST_TYPE_LPM);
00255     ENERGEST_ON(ENERGEST_TYPE_CPU);  
00256     
00257   }
00258   
00259 }
00260 
00261 
00262 
00263 /*int8u errcode __attribute__(( section(".noinit") ));
00264 
00265 void halBaseBandIsr(){
00266   
00267   errcode = 1;
00268   leds_on(LEDS_RED);
00269 }
00270 
00271 void BusFault_Handler(){
00272   
00273   errcode = 2; 
00274   leds_on(LEDS_RED);
00275 }
00276 
00277 void halDebugIsr(){
00278   
00279   errcode = 3;
00280   leds_on(LEDS_RED);  
00281 }
00282 
00283 void DebugMon_Handler(){
00284   
00285   errcode = 4;
00286   //leds_on(LEDS_RED);  
00287 }
00288 
00289 void HardFault_Handler(){
00290   
00291   errcode = 5; 
00292   //leds_on(LEDS_RED);
00293   //halReboot();
00294 }
00295 
00296 void MemManage_Handler(){
00297   
00298   errcode = 6; 
00299   //leds_on(LEDS_RED);
00300   //halReboot();
00301 }
00302 
00303 void UsageFault_Handler(){
00304   
00305   errcode = 7; 
00306   //leds_on(LEDS_RED);
00307   //halReboot();
00308 }
00309 
00310 void Default_Handler() 
00311 { 
00312   //errcode = 8; 
00313   leds_on(LEDS_RED);
00314   halReboot();
00315 }*/