contiki-esb-main.c
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
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
00056
00057 #endif
00058
00059 #ifdef DCOSYNCH_CONF_PERIOD
00060 #define DCOSYNCH_PERIOD DCOSYNCH_CONF_PERIOD
00061 #else
00062 #define DCOSYNCH_PERIOD 30
00063 #endif
00064
00065 #ifdef DCOSYNCH_CONF_ENABLED
00066 #define DCOSYNCH_ENABLED DCOSYNCH_CONF_ENABLED
00067 #else
00068 #define DCOSYNCH_ENABLED 0
00069 #endif
00070
00071 #if DCOSYNCH_ENABLED
00072 static struct timer dco_timer;
00073 #endif
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
00111 process_start(&uip_fw_process, NULL);
00112
00113 if (node_id > 0) {
00114
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
00122
00123 uip_fw_default(&tr1001if);
00124 }
00125 #endif
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
00136 putchar('\n');
00137 }
00138
00139 static void init_ports_toberemoved() {
00140
00141 P1SEL = 0x00;
00142 P1DIR = 0x81;
00143
00144
00145 P1OUT = 0x00;
00146
00147
00148 P2SEL = 0x00;
00149 P2DIR = 0x7F;
00150
00151 P2OUT = 0x77;
00152
00153
00154 P3SEL = 0xE0;
00155 P3DIR = 0x5F;
00156
00157 P3OUT = 0xE0;
00158
00159
00160 P4SEL = 0x00;
00161 P4DIR = 0xFF;
00162 P4OUT = 0x00;
00163
00164
00165 P5SEL = 0x00;
00166 P5DIR = 0xDA;
00167 P5OUT = 0x0F;
00168
00169
00170 P6SEL = 0x00;
00171 P6DIR = 0x00;
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
00228
00229 #if PROFILE_CONF_ON
00230 profile_init();
00231 #endif
00232
00233 #if ENERGEST_CONF_ON
00234 energest_init();
00235 ENERGEST_ON(ENERGEST_TYPE_CPU);
00236 #endif
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
00245
00246
00247
00248
00249 watchdog_start();
00250 while(1) {
00251 int r;
00252 #if PROFILE_CONF_ON
00253 profile_episode_start();
00254 #endif
00255 do {
00256
00257 watchdog_periodic();
00258 r = process_run();
00259 } while(r > 0);
00260 #if PROFILE_CONF_ON
00261 profile_episode_end();
00262 #endif
00263
00264
00265
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
00274
00275 #if DCOSYNCH_CONF_ENABLED
00276
00277 if(timer_expired(&dco_timer)) {
00278 timer_reset(&dco_timer);
00279 msp430_sync_dco();
00280 }
00281 #endif
00282
00283 #if ENERGEST_CONF_ON
00284
00285 ENERGEST_OFF(ENERGEST_TYPE_CPU);
00286 ENERGEST_ON(ENERGEST_TYPE_LPM);
00287
00288
00289
00290
00291 energest_type_set(ENERGEST_TYPE_IRQ, irq_energest);
00292 #endif
00293
00294 watchdog_stop();
00295 _BIS_SR(GIE | SCG0 | CPUOFF);
00296
00297 #if ENERGEST_CONF_ON
00298
00299
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
00306
00307 watchdog_start();
00308 }
00309 }
00310
00311 return 0;
00312 }
00313
00314
00315
00316
00317
00318 #if UIP_LOGGING
00319 void
00320 uip_log(char *m)
00321 {
00322 printf("uIP log: '%s'\n", m);
00323 }
00324 #endif