contiki-esb-main.c

00001 /*
00002  * Copyright (c) 2005, Swedish Institute of Computer Science
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 copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the Institute nor the names of its contributors
00014  *    may be used to endorse or promote products derived from this software
00015  *    without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  *
00029  * This file is part of the Contiki operating system.
00030  *
00031  * @(#)$Id: contiki-esb-main.c,v 1.19 2010/06/21 15:15:12 nifi Exp $
00032  */
00033 
00034 #include <io.h>
00035 #include <signal.h>
00036 #include <stdio.h>
00037 #include <string.h>
00038 
00039 #include "contiki.h"
00040 #include "contiki-esb.h"
00041 
00042 #include "dev/watchdog.h"
00043 #include "sys/autostart.h"
00044 #include "net/uip-driver.h"
00045 #include "net/netstack.h"
00046 
00047 #if WITH_UIP
00048 
00049 static struct uip_fw_netif tr1001if =
00050   {UIP_FW_NETIF(0,0,0,0, 0,0,0,0, uip_driver_send)};
00051 
00052 #if WITH_SLIP
00053 static struct uip_fw_netif slipif =
00054   {UIP_FW_NETIF(172,16,0,0, 255,255,255,0, slip_send)};
00055 #endif /* WITH_SLIP */
00056 
00057 #endif /* WITH_UIP */
00058 
00059 #ifdef DCOSYNCH_CONF_PERIOD
00060 #define DCOSYNCH_PERIOD DCOSYNCH_CONF_PERIOD
00061 #else
00062 #define DCOSYNCH_PERIOD 30
00063 #endif /* DCOSYNCH_CONF_PERIOD */
00064 
00065 #ifdef DCOSYNCH_CONF_ENABLED
00066 #define DCOSYNCH_ENABLED DCOSYNCH_CONF_ENABLED
00067 #else
00068 #define DCOSYNCH_ENABLED 0
00069 #endif /* DCOSYNCH_CONF_ENABLED */
00070 
00071 #if DCOSYNCH_ENABLED
00072 static struct timer dco_timer;
00073 #endif /* DCOSYNCH_ENABLED */
00074 
00075 SENSORS(&button_sensor, &sound_sensor, &vib_sensor,
00076         &pir_sensor, &radio_sensor, &battery_sensor, &ctsrts_sensor,
00077         &temperature_sensor);
00078 
00079 /*---------------------------------------------------------------------------*/
00080 static void
00081 set_rime_addr(void)
00082 {
00083   int i;
00084   rimeaddr_t rimeaddr;
00085 
00086   rimeaddr.u8[0] = node_id & 0xff;
00087   rimeaddr.u8[1] = node_id >> 8;
00088   rimeaddr_set_node_addr(&rimeaddr);
00089 
00090   printf("Rime started with address ");
00091   for(i = 0; i < sizeof(rimeaddr.u8) - 1; i++) {
00092     printf("%u.", rimeaddr.u8[i]);
00093   }
00094   printf("%u\n", rimeaddr.u8[i]);
00095 }
00096 /*---------------------------------------------------------------------------*/
00097 #if WITH_UIP
00098 static void
00099 init_uip_net(void)
00100 {
00101   uip_ipaddr_t hostaddr;
00102 
00103   uip_init();
00104   uip_fw_init();
00105 
00106   process_start(&tcpip_process, NULL);
00107 #if WITH_SLIP
00108   process_start(&slip_process, NULL);
00109   rs232_set_input(slip_input_byte);
00110 #endif /* WITH_SLIP */
00111   process_start(&uip_fw_process, NULL);
00112 
00113   if (node_id > 0) {
00114     /* node id is set, construct an ip address based on the node id */
00115     uip_ipaddr(&hostaddr, 172, 16, 1, node_id & 0xff);
00116     uip_sethostaddr(&hostaddr);
00117   }
00118 
00119 #if WITH_SLIP
00120   uip_fw_register(&slipif);
00121 #endif /* WITH_SLIP */
00122 
00123   uip_fw_default(&tr1001if);
00124 }
00125 #endif /* WITH_UIP */
00126 /*---------------------------------------------------------------------------*/
00127 static void
00128 print_processes(struct process * const processes[])
00129 {
00130   printf("Starting");
00131   while(*processes != NULL) {
00132     printf(" '%s'", (*processes)->name);
00133     processes++;
00134   }
00135   /* Needed to force link with putchar */
00136   putchar('\n');
00137 }
00138 /*---------------------------------------------------------------------------*/
00139 static void init_ports_toberemoved() {
00140     ////////// Port 1 ////
00141   P1SEL = 0x00;
00142   P1DIR = 0x81;       // Outputs: P10=IRSend, P17=RS232RTS
00143                       // Inputs: P11=Light, P12=IRRec, P13=PIR, P14=Vibration,
00144                       //         P15=Clockalarm, P16=RS232CTS
00145   P1OUT = 0x00;
00146 
00147   ////////// Port 2 ////
00148   P2SEL = 0x00;       // No Sels
00149   P2DIR = 0x7F;       // Outpus: P20..P23=Leds+Beeper, P24..P26=Poti
00150                       // Inputs: P27=Taster
00151   P2OUT = 0x77;
00152 
00153   ////////// Port 3 ////
00154   P3SEL = 0xE0;       // Sels for P34..P37 to activate UART,
00155   P3DIR = 0x5F;       // Inputs: P30..P33=CON4, P35/P37=RXD Transceiver/RS232
00156                       // OutPuts: P36/P38=TXD Transceiver/RS232
00157   P3OUT = 0xE0;       // Output a Zero on P34(TXD Transceiver) and turn SELECT off when receiving!!!
00158 
00159   ////////// Port 4 ////
00160   P4SEL = 0x00;       // CON5 Stecker
00161   P4DIR = 0xFF;
00162   P4OUT = 0x00;
00163 
00164   ////////// Port 5 ////
00165   P5SEL = 0x00;       // P50/P51= Clock SDA/SCL, P52/P53/P54=EEPROM SDA/SCL/WP
00166   P5DIR = 0xDA;       // P56/P57=Transceiver CNTRL0/1
00167   P5OUT = 0x0F;
00168 
00169   ////////// Port 6 ////
00170   P6SEL = 0x00;       // P60=Microphone, P61=PIR digital (same as P13), P62=PIR analog
00171   P6DIR = 0x00;       // P63=extern voltage, P64=battery voltage, P65=Receive power
00172   P6OUT = 0x00;
00173 }
00174 /*---------------------------------------------------------------------------*/
00175 int
00176 main(void)
00177 {
00178   msp430_cpu_init();
00179 
00180   init_ports_toberemoved();
00181   
00182   init_lowlevel();
00183 
00184   clock_init();
00185 
00186   rtimer_init();
00187   
00188   process_init();
00189 
00190   random_init(0);
00191 
00192   node_id_restore();
00193   
00194   process_start(&etimer_process, NULL);
00195   process_start(&sensors_process, NULL);
00196 
00197   ctimer_init();
00198 
00199   set_rime_addr();
00200 
00201   printf(CONTIKI_VERSION_STRING " started. ");
00202   if(node_id > 0) {
00203     printf("Node id is set to %u.\n", node_id);
00204   } else {
00205     printf("Node id is not set.\n");
00206   }
00207 
00208   netstack_init();
00209 
00210   printf("%s %s, channel check rate %lu Hz\n",
00211          NETSTACK_MAC.name, NETSTACK_RDC.name,
00212          CLOCK_SECOND / (NETSTACK_RDC.channel_check_interval() == 0 ? 1:
00213                          NETSTACK_RDC.channel_check_interval()));
00214 
00215   beep_spinup();
00216   leds_on(LEDS_RED);
00217   clock_delay(100);
00218   leds_off(LEDS_RED);
00219 
00220 #if !WITH_SLIP
00221   rs232_set_input(serial_line_input_byte);
00222   serial_line_init();
00223 #endif
00224 
00225 #if WITH_UIP
00226   init_uip_net();
00227 #endif /* WITH_UIP */
00228 
00229 #if PROFILE_CONF_ON
00230   profile_init();
00231 #endif /* PROFILE_CONF_ON */
00232 
00233 #if ENERGEST_CONF_ON
00234   energest_init();
00235   ENERGEST_ON(ENERGEST_TYPE_CPU);
00236 #endif /* ENERGEST_CONF_ON */
00237 
00238   init_apps();
00239   print_processes(autostart_processes);
00240   autostart_start(autostart_processes);
00241 
00242 #if DCOSYNCH_ENABLED
00243   timer_set(&dco_timer, DCOSYNCH_PERIOD * CLOCK_SECOND);
00244 #endif /* DCOSYNCH_ENABLED */
00245 
00246   /*
00247    * This is the scheduler loop.
00248    */
00249   watchdog_start();
00250   while(1) {
00251     int r;
00252 #if PROFILE_CONF_ON
00253     profile_episode_start();
00254 #endif /* PROFILE_CONF_ON */
00255     do {
00256       /* Reset watchdog. */
00257       watchdog_periodic();
00258       r = process_run();
00259     } while(r > 0);
00260 #if PROFILE_CONF_ON
00261     profile_episode_end();
00262 #endif /* PROFILE_CONF_ON */
00263 
00264     /*
00265      * Idle processing.
00266      */
00267     dint();
00268     if(process_nevents() != 0) {
00269       eint();
00270     } else {
00271 #if ENERGEST_CONF_ON
00272       static unsigned long irq_energest = 0;
00273 #endif /* ENERGEST_CONF_ON */
00274 
00275 #if DCOSYNCH_CONF_ENABLED
00276       /* before going down to sleep possibly do some management */
00277       if(timer_expired(&dco_timer)) {
00278         timer_reset(&dco_timer);
00279         msp430_sync_dco();
00280       }
00281 #endif /* DCOSYNCH_CONF_ENABLED */
00282 
00283 #if ENERGEST_CONF_ON
00284       /* Re-enable interrupts and go to sleep atomically. */
00285       ENERGEST_OFF(ENERGEST_TYPE_CPU);
00286       ENERGEST_ON(ENERGEST_TYPE_LPM);
00287 
00288       /* We only want to measure the processing done in IRQs when we
00289          are asleep, so we discard the processing time done when we
00290          were awake. */
00291       energest_type_set(ENERGEST_TYPE_IRQ, irq_energest);
00292 #endif /* ENERGEST_CONF_ON */
00293 
00294       watchdog_stop();
00295       _BIS_SR(GIE | SCG0 | CPUOFF); /* LPM1 sleep. */
00296 
00297 #if ENERGEST_CONF_ON
00298       /* We get the current processing time for interrupts that was
00299          done during the LPM and store it for next time around.  */
00300       dint();
00301       irq_energest = energest_type_time(ENERGEST_TYPE_IRQ);
00302       eint();
00303       ENERGEST_OFF(ENERGEST_TYPE_LPM);
00304       ENERGEST_ON(ENERGEST_TYPE_CPU);
00305 #endif /* ENERGEST_CONF_ON */
00306 
00307       watchdog_start();
00308     }
00309   }
00310 
00311   return 0;
00312 }
00313 /*---------------------------------------------------------------------------*/
00314 /* char *arg_alloc(char size) {return NULL;} */
00315 /* void  arg_init(void) {} */
00316 /* void  arg_free(char *arg) {} */
00317 /*---------------------------------------------------------------------------*/
00318 #if UIP_LOGGING
00319 void
00320 uip_log(char *m)
00321 {
00322   printf("uIP log: '%s'\n", m);
00323 }
00324 #endif /* UIP_LOGGING */

Generated on Mon Apr 11 14:23:43 2011 for Contiki 2.5 by  doxygen 1.6.1