contiki-msb430-main.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2007, 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  */
00032 
00033 /**
00034  * \file
00035  *      Main system file for the MSB-430 port.
00036  * \author
00037  *      Michael Baar <baar@inf.fu-berlin.de>, Nicolas Tsiftes <nvt@sics.se>
00038  */
00039 
00040 #include <io.h>
00041 #include <signal.h>
00042 
00043 #include <stdio.h>
00044 #include <string.h>
00045 
00046 #include "contiki.h"
00047 #include "contiki-msb430.h"
00048 #include "dev/adc.h"
00049 #include "dev/sd.h"
00050 #include "dev/serial-line.h"
00051 #include "dev/sht11.h"
00052 #include "dev/watchdog.h"
00053 
00054 extern volatile bool uart_edge;
00055 
00056 extern void init_net(void);
00057 
00058 SENSORS(NULL);
00059 
00060 static void
00061 msb_ports_init(void)
00062 {
00063   P1SEL = 0x00; P1OUT = 0x00; P1DIR = 0x00;
00064   P2SEL = 0x00; P2OUT = 0x18; P2DIR = 0x1A;
00065   P3SEL = 0x00; P3OUT = 0x09; P3DIR = 0x21;
00066   P4SEL = 0x00; P4OUT = 0x00; P4DIR = 0x00;
00067   P5SEL = 0x0E; P5OUT = 0xF9; P5DIR = 0xFD;
00068   P6SEL = 0x07; P6OUT = 0x00; P6DIR = 0xC8;
00069 }
00070 
00071 int
00072 main(void)
00073 {
00074 #if WITH_SD
00075   int r;
00076 #endif /* WITH_SD */
00077 
00078   msp430_cpu_init();    
00079   watchdog_stop();
00080 
00081   /* Platform-specific initialization. */
00082   msb_ports_init();
00083   adc_init();
00084 
00085   clock_init();
00086   rtimer_init();
00087 
00088   sht11_init();
00089   leds_init();
00090   leds_on(LEDS_ALL);
00091 
00092   process_init();
00093 
00094   /* serial interface */
00095   rs232_set_input(serial_line_input_byte);
00096   rs232_init();
00097   serial_line_init();
00098 
00099   uart_lock(UART_MODE_RS232);
00100   uart_unlock(UART_MODE_RS232);
00101 #if WITH_UIP
00102   slip_arch_init(BAUD2UBR(115200));
00103 #endif
00104 
00105 
00106 #if WITH_SD
00107   r = sd_initialize();
00108   if(r < 0) {
00109     printf("Failed to initialize the SD driver: %s\n", sd_error_string(r));
00110   } else {
00111     sd_offset_t capacity;
00112     printf("The SD driver was successfully initialized\n");
00113     capacity = sd_get_capacity();
00114     if(capacity < 0) {
00115       printf("Failed to get the SD card capacity: %s\n", sd_error_string(r));
00116     } else {
00117       printf("SD card capacity: %u MB\n",
00118         (unsigned)(capacity / (1024UL * 1024)));
00119     }
00120   }
00121 #endif
00122 
00123   node_id_restore();
00124 
00125   /* System timers */
00126   process_start(&etimer_process, NULL);
00127   ctimer_init();
00128 
00129   /* Networking stack. */
00130   NETSTACK_RADIO.init();
00131   NETSTACK_RDC.init();
00132   NETSTACK_MAC.init();
00133   NETSTACK_NETWORK.init();
00134   {
00135     rimeaddr_t rimeaddr;
00136   
00137     rimeaddr.u8[0] = node_id & 0xff;
00138     rimeaddr.u8[1] = node_id >> 8;
00139     rimeaddr_set_node_addr(&rimeaddr);
00140   }
00141 
00142   energest_init();
00143  
00144 #if PROFILE_CONF_ON
00145   profile_init();
00146 #endif /* PROFILE_CONF_ON */
00147  
00148   leds_off(LEDS_ALL);
00149 
00150   printf("Node %d.%d: %s %s, channel check rate %u Hz\n",
00151          rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1],
00152          NETSTACK_MAC.name, NETSTACK_RDC.name,
00153          CLOCK_SECOND / (NETSTACK_RDC.channel_check_interval() == 0 ?
00154                 1 : (unsigned)NETSTACK_RDC.channel_check_interval()));
00155 
00156   autostart_start(autostart_processes);
00157 
00158   /*
00159    * This is the scheduler loop.
00160    */
00161   ENERGEST_ON(ENERGEST_TYPE_CPU);
00162 
00163   while (1) {
00164     int r;
00165 #if PROFILE_CONF_ON
00166     profile_episode_start();
00167 #endif /* PROFILE_CONF_ON */
00168     do {
00169       /* Reset watchdog. */
00170       watchdog_periodic();
00171       r = process_run();
00172     } while(r > 0);
00173 
00174 #if PROFILE_CONF_ON
00175     profile_episode_end();
00176 #endif /* PROFILE_CONF_ON */
00177 
00178     /*
00179      * Idle processing.
00180      */
00181     int s = splhigh();          /* Disable interrupts. */
00182     if (process_nevents() != 0) {
00183       splx(s);                  /* Re-enable interrupts. */
00184     } else {
00185       static unsigned long irq_energest = 0;
00186       /* Re-enable interrupts and go to sleep atomically. */
00187       ENERGEST_OFF(ENERGEST_TYPE_CPU);
00188       ENERGEST_ON(ENERGEST_TYPE_LPM);
00189      /*
00190       * We only want to measure the processing done in IRQs when we
00191       * are asleep, so we discard the processing time done when we
00192       * were awake.
00193       */
00194       energest_type_set(ENERGEST_TYPE_IRQ, irq_energest);
00195 
00196       if (uart_edge) {
00197         _BIC_SR(LPM1_bits + GIE);
00198       } else {
00199         _BIS_SR(LPM1_bits + GIE);
00200       }
00201 
00202       /*
00203        * We get the current processing time for interrupts that was
00204        * done during the LPM and store it for next time around. 
00205        */
00206       dint();
00207       irq_energest = energest_type_time(ENERGEST_TYPE_IRQ);
00208       eint();
00209       ENERGEST_OFF(ENERGEST_TYPE_LPM);
00210       ENERGEST_ON(ENERGEST_TYPE_CPU);
00211 #if PROFILE_CONF_ON
00212       profile_clear_timestamps();
00213 #endif /* PROFILE_CONF_ON */
00214     }
00215   }
00216 
00217   return 0;
00218 }

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