Contiki 2.6

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