contiki-sensinode-main.c

00001 #include <stdio.h>
00002 
00003 #include "contiki.h"
00004 #include "sys/clock.h"
00005 #include "sys/autostart.h"
00006 
00007 #include "dev/serial-line.h"
00008 #include "dev/bus.h"
00009 #include "dev/leds.h"
00010 #include "dev/uart.h"
00011 #include "dev/models.h"
00012 #include "dev/cc2430_rf.h"
00013 #include "net/mac/sicslowmac.h"
00014 #include "net/mac/frame802154.h"
00015 #include "net/rime.h"
00016 
00017 volatile int i, a;
00018 unsigned short node_id = 0;                     /* Manually sets MAC address when > 0 */
00019 
00020 /*---------------------------------------------------------------------------*/
00021 static void
00022 print_processes(struct process * const processes[])
00023 {
00024   printf("Starting");
00025   while(*processes != NULL) {
00026     printf(" '%s'", (*processes)->name);
00027     processes++;
00028   }
00029   printf("\n");
00030 }
00031 /*---------------------------------------------------------------------------*/
00032 void
00033 putchar(char c)
00034 {
00035   /* UART1 used for debugging on Sensinode products. */
00036   uart1_writeb(c);
00037 }
00038 /*---------------------------------------------------------------------------*/
00039 static void
00040 fade(int l)
00041 {
00042   int k, j;
00043   for(k = 0; k < 400; ++k) {
00044     j = k > 200? 400 - k: k;
00045 
00046     leds_on(l);
00047     for(i = 0; i < j; ++i) {
00048       a = i;
00049     }
00050     leds_off(l);
00051     for(i = 0; i < 200 - j; ++i) {
00052       a = i;
00053     }
00054   }
00055 }
00056 /*---------------------------------------------------------------------------*/
00057 static void
00058 set_rime_addr(void)
00059 {
00060   rimeaddr_t addr;
00061   uint8_t ft_buffer[8];
00062   uint8_t *addr_long = NULL;
00063   uint16_t addr_short = 0;
00064   int i;
00065 
00066   /* TODO: This flash_read routine currently gets a different address
00067    * than nano_programmer -m... something broken or misconfigured...
00068    */
00069 
00070   flash_read(&ft_buffer[0], 0x1FFF8, 8);
00071 
00072   printf("Read MAC from flash: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
00073          ft_buffer[0], ft_buffer[1], ft_buffer[2], ft_buffer[3],
00074          ft_buffer[4], ft_buffer[5], ft_buffer[6], ft_buffer[7]);
00075 
00076   memset(&addr, 0, sizeof(rimeaddr_t));
00077 
00078 #if UIP_CONF_IPV6
00079   memcpy(addr.u8, ft_buffer, sizeof(addr.u8));
00080 #else
00081   if(node_id == 0) {
00082     for(i = 0; i < sizeof(rimeaddr_t); ++i) {
00083       addr.u8[i] = ft_buffer[7 - i];
00084     }
00085   } else {
00086     printf("Setting manual address from node_id\n");
00087     addr.u8[1] = node_id >> 8;
00088     addr.u8[0] = node_id & 0xff;
00089   }
00090 #endif
00091 
00092   rimeaddr_set_node_addr(&addr);
00093   printf("Rime configured with address ");
00094   for(i = (sizeof(addr.u8)) - 1; i > 0; i--) {
00095     printf("%02x:", addr.u8[i]);
00096   }
00097   printf("%02x\n", addr.u8[i]);
00098 
00099   /* Set the cc2430 RF addresses */
00100   if (sizeof(addr.u8) == 6)
00101           addr_long = (uint8_t *) addr.u8;
00102   else
00103           addr_short = (addr.u8[1] * 256) + addr.u8[0];
00104 
00105   cc2430_rf_set_addr(0xffff, addr_short, addr_long);
00106 }
00107 /*---------------------------------------------------------------------------*/
00108 int
00109 main(void)
00110 {
00111 
00112   /* Hardware initialization */
00113   bus_init();
00114 
00115   leds_init();
00116   fade(LEDS_GREEN);
00117 
00118   uart1_init(115200);
00119   uart1_set_input(serial_line_input_byte);
00120 
00121   /* initialize process manager. */
00122   process_init();
00123 
00124   serial_line_init();
00125 
00126   printf("\n" CONTIKI_VERSION_STRING " started\n");
00127   printf("model: " SENSINODE_MODEL "\n\n");
00128 
00129   /* initialize the radio driver */
00130 
00131   cc2430_rf_init();
00132   rime_init(sicslowmac_init(&cc2430_rf_driver));
00133   set_rime_addr();
00134 
00135   /* start services */
00136   process_start(&etimer_process, NULL);
00137 
00138   fade(LEDS_RED);
00139 
00140   autostart_start(autostart_processes);
00141 
00142   while(1) {
00143     process_run();
00144     etimer_request_poll();
00145   }
00146 }
00147 /*---------------------------------------------------------------------------*/

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