Contiki 2.5
contiki-msb430-main.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2007, Swedish Institute of Computer Science
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  */
32 
33 /**
34  * \file
35  * Main system file for the MSB-430 port.
36  * \author
37  * Michael Baar <baar@inf.fu-berlin.de>, Nicolas Tsiftes <nvt@sics.se>
38  */
39 #include <stdio.h>
40 #include <string.h>
41 
42 #include "contiki.h"
43 #include "contiki-msb430.h"
44 #include "dev/adc.h"
45 #include "dev/sd.h"
46 #include "dev/serial-line.h"
47 #include "dev/sht11.h"
48 #include "dev/watchdog.h"
49 
50 extern volatile bool uart_edge;
51 
52 extern void init_net(void);
53 
54 SENSORS(NULL);
55 
56 static void
57 msb_ports_init(void)
58 {
59  P1SEL = 0x00; P1OUT = 0x00; P1DIR = 0x00;
60  P2SEL = 0x00; P2OUT = 0x18; P2DIR = 0x1A;
61  P3SEL = 0x00; P3OUT = 0x09; P3DIR = 0x21;
62  P4SEL = 0x00; P4OUT = 0x00; P4DIR = 0x00;
63  P5SEL = 0x0E; P5OUT = 0xF9; P5DIR = 0xFD;
64  P6SEL = 0x07; P6OUT = 0x00; P6DIR = 0xC8;
65 }
66 
67 int
68 main(void)
69 {
70 #if WITH_SD
71  int r;
72 #endif /* WITH_SD */
73 
74  msp430_cpu_init();
75  watchdog_stop();
76 
77  /* Platform-specific initialization. */
78  msb_ports_init();
79  adc_init();
80 
81  clock_init();
82  rtimer_init();
83 
84  sht11_init();
85  leds_init();
86  leds_on(LEDS_ALL);
87 
88  process_init();
89 
90  /* serial interface */
91  rs232_set_input(serial_line_input_byte);
92  rs232_init();
93  serial_line_init();
94 
95  uart_lock(UART_MODE_RS232);
96  uart_unlock(UART_MODE_RS232);
97 #if WITH_UIP
98  slip_arch_init(BAUD2UBR(115200));
99 #endif
100 
101 
102 #if WITH_SD
103  r = sd_initialize();
104  if(r < 0) {
105  printf("Failed to initialize the SD driver: %s\n", sd_error_string(r));
106  } else {
107  sd_offset_t capacity;
108  printf("The SD driver was successfully initialized\n");
109  capacity = sd_get_capacity();
110  if(capacity < 0) {
111  printf("Failed to get the SD card capacity: %s\n", sd_error_string(r));
112  } else {
113  printf("SD card capacity: %u MB\n",
114  (unsigned)(capacity / (1024UL * 1024)));
115  }
116  }
117 #endif
118 
119  node_id_restore();
120 
121  /* System timers */
122  process_start(&etimer_process, NULL);
123  ctimer_init();
124 
125  /* Networking stack. */
126  NETSTACK_RADIO.init();
127  NETSTACK_RDC.init();
128  NETSTACK_MAC.init();
129  NETSTACK_NETWORK.init();
130  {
131  rimeaddr_t rimeaddr;
132 
133  rimeaddr.u8[0] = node_id & 0xff;
134  rimeaddr.u8[1] = node_id >> 8;
135  rimeaddr_set_node_addr(&rimeaddr);
136  }
137 
138  energest_init();
139 
140 #if PROFILE_CONF_ON
141  profile_init();
142 #endif /* PROFILE_CONF_ON */
143 
144  leds_off(LEDS_ALL);
145 
146  printf("Node %d.%d: %s %s, channel check rate %u Hz\n",
148  NETSTACK_MAC.name, NETSTACK_RDC.name,
149  CLOCK_SECOND / (NETSTACK_RDC.channel_check_interval() == 0 ?
150  1 : (unsigned)NETSTACK_RDC.channel_check_interval()));
151 
152  autostart_start(autostart_processes);
153 
154  /*
155  * This is the scheduler loop.
156  */
157  ENERGEST_ON(ENERGEST_TYPE_CPU);
158 
159  while (1) {
160  int r;
161 #if PROFILE_CONF_ON
162  profile_episode_start();
163 #endif /* PROFILE_CONF_ON */
164  do {
165  /* Reset watchdog. */
166  watchdog_periodic();
167  r = process_run();
168  } while(r > 0);
169 
170 #if PROFILE_CONF_ON
171  profile_episode_end();
172 #endif /* PROFILE_CONF_ON */
173 
174  /*
175  * Idle processing.
176  */
177  int s = splhigh(); /* Disable interrupts. */
178  if (process_nevents() != 0) {
179  splx(s); /* Re-enable interrupts. */
180  } else {
181  static unsigned long irq_energest = 0;
182  /* Re-enable interrupts and go to sleep atomically. */
183  ENERGEST_OFF(ENERGEST_TYPE_CPU);
184  ENERGEST_ON(ENERGEST_TYPE_LPM);
185  /*
186  * We only want to measure the processing done in IRQs when we
187  * are asleep, so we discard the processing time done when we
188  * were awake.
189  */
190  energest_type_set(ENERGEST_TYPE_IRQ, irq_energest);
191 
192  if (uart_edge) {
193  _BIC_SR(LPM1_bits + GIE);
194  } else {
195  _BIS_SR(LPM1_bits + GIE);
196  }
197 
198  /*
199  * We get the current processing time for interrupts that was
200  * done during the LPM and store it for next time around.
201  */
202  dint();
203  irq_energest = energest_type_time(ENERGEST_TYPE_IRQ);
204  eint();
205  ENERGEST_OFF(ENERGEST_TYPE_LPM);
206  ENERGEST_ON(ENERGEST_TYPE_CPU);
207 #if PROFILE_CONF_ON
208  profile_clear_timestamps();
209 #endif /* PROFILE_CONF_ON */
210  }
211  }
212 
213  return 0;
214 }