Contiki 2.6

contiki-main.c

00001 #include <AT91SAM7S64.h>
00002 #include <interrupt-utils.h>
00003 #include <string.h>
00004 #include <debug-uart.h>
00005 #include <ctype.h>
00006 #include <stdio.h>
00007 #include <dev/cc2420.h>
00008 #include <dev/cc2420_const.h>
00009 #include <dev/spi.h>
00010 #include <dev/leds.h>
00011 #include <sys/process.h>
00012 #include <sys/procinit.h>
00013 #include <sys/autostart.h>
00014 #include <sys/etimer.h>
00015 #include <net/psock.h>
00016 #include <unistd.h>
00017 
00018 #include "net/mac/nullmac.h"
00019 #include "net/rime.h"
00020 
00021 #include "contiki-main.h"
00022 
00023 #ifndef RF_CHANNEL
00024 #define RF_CHANNEL 15
00025 #endif
00026 
00027 
00028 #if WITH_UIP
00029 #include "net/uip.h"
00030 #include "net/uip-fw.h"
00031 #include "net/uip-fw-drv.h"
00032 #include "net/uip-over-mesh.h"
00033 
00034 
00035 static struct uip_fw_netif meshif =
00036   {UIP_FW_NETIF(172,16,0,0, 255,255,0,0, uip_over_mesh_send)};
00037 
00038 #define UIP_OVER_MESH_CHANNEL 9
00039 
00040 uint8_t is_gateway = 0;
00041 struct trickle_conn gateway_trickle;
00042 
00043 #endif /* WITH_UIP */
00044 
00045 extern char __heap_end__;
00046 extern char __heap_start__;
00047 
00048 
00049 rimeaddr_t node_addr __attribute__((weak)) = {{0,2}};
00050 
00051 /*--------------------------------------------------------------------------*/
00052 #if WITH_UIP
00053 
00054 /* Receive address of gateway */
00055 
00056 static void
00057 trickle_recv(struct trickle_conn *c)
00058 {
00059   if (!is_gateway) {
00060     struct gateway_msg *msg;
00061     msg = rimebuf_dataptr();
00062     printf("%d.%d: gateway message: %d.%d\n",
00063            rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1],
00064            msg->gateway.u8[0], msg->gateway.u8[1]);
00065     
00066     uip_over_mesh_set_gateway(&msg->gateway);
00067   }
00068 }
00069 
00070 const static struct trickle_callbacks trickle_call = {trickle_recv};
00071 
00072 /*---------------------------------------------------------------------------*/
00073 #endif
00074 
00075 #if 0
00076 /* Wathcdog is  already disabled in startup code */
00077 static void
00078 wdt_setup()
00079 {
00080 
00081 }
00082 #endif
00083 
00084 static void
00085 wdt_reset()
00086 {
00087   *AT91C_WDTC_WDCR = (0xa5<<24) | AT91C_WDTC_WDRSTT;
00088 }
00089 
00090 #if 0
00091 static uip_ipaddr_t gw_addr = {{172,16,0,1}};
00092 #endif
00093 
00094 
00095 int
00096 main()
00097 {
00098   disableIRQ();
00099   disableFIQ();
00100   *AT91C_AIC_IDCR = 0xffffffff;
00101   *AT91C_PMC_PCDR = 0xffffffff;
00102   *AT91C_PMC_PCER = (1 << AT91C_ID_PIOA);
00103   
00104   dbg_setup_uart();
00105   printf("Initialising\n");
00106   leds_init();
00107   clock_init();
00108   process_init();
00109   process_start(&etimer_process, NULL);
00110 
00111   ctimer_init();
00112 
00113   enableIRQ();
00114 
00115   printf("Beginning CC2420 setup\n");
00116   cc2420_init();
00117   cc2420_set_pan_addr(0x2024, 0, NULL);
00118   cc2420_set_channel(RF_CHANNEL);
00119   rime_init(nullmac_init(&cc2420_driver));
00120   printf("CC2420 setup done\n");
00121 
00122   rimeaddr_set_node_addr(&node_addr);
00123   printf("Rime started with address %d.%d\n", node_addr.u8[0], node_addr.u8[1]);
00124   
00125   #if WITH_UIP
00126   {
00127     uip_ipaddr_t hostaddr, netmask;
00128     
00129     uip_init();
00130 
00131     uip_ipaddr(&hostaddr, 172,16,
00132                rimeaddr_node_addr.u8[0],rimeaddr_node_addr.u8[1]);
00133     uip_ipaddr(&netmask, 255,255,0,0);
00134     uip_ipaddr_copy(&meshif.ipaddr, &hostaddr);
00135     printf("Host addr\n");
00136     uip_sethostaddr(&hostaddr);
00137     uip_setnetmask(&netmask);
00138     uip_over_mesh_set_net(&hostaddr, &netmask);
00139     /*    uip_fw_register(&slipif);*/
00140     /*uip_over_mesh_set_gateway_netif(&slipif);*/
00141     uip_fw_register(&meshif);
00142     uip_fw_default(&meshif);
00143     printf("Mesh init\n");
00144     uip_over_mesh_init(UIP_OVER_MESH_CHANNEL);
00145     printf("uIP started with IP address %d.%d.%d.%d\n",
00146            uip_ipaddr_to_quad(&hostaddr));
00147   }
00148 #endif /* WITH_UIP */
00149 
00150 
00151  
00152 #if WITH_UIP
00153   process_start(&tcpip_process, NULL);
00154   process_start(&uip_fw_process, NULL); /* Start IP output */
00155   
00156   trickle_open(&gateway_trickle, CLOCK_SECOND * 4, GATEWAY_TRICKLE_CHANNEL,
00157                &trickle_call);
00158 #endif /* WITH_UIP */
00159 
00160   printf("Heap size: %ld bytes\n", &__heap_end__ - (char*)sbrk(0));
00161   printf("Started\n");
00162 
00163   /* procinit_init(); */
00164 
00165   autostart_start(autostart_processes);
00166   printf("Processes running\n");
00167   while(1) {
00168     do {
00169       /* Reset watchdog. */
00170       wdt_reset();
00171     } while(process_run() > 0);
00172     /* Idle! */
00173     /* Stop processor clock */
00174     *AT91C_PMC_SCDR |= AT91C_PMC_PCK;
00175   }
00176   return 0;
00177 }