Contiki 2.6

robot-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 <stepper-steps.h>
00019 #include <stepper.h>
00020 #include <stepper-move.h>
00021 
00022 #include "net/mac/nullmac.h"
00023 #include "net/rime.h"
00024 
00025 
00026 #ifndef RF_CHANNEL
00027 #define RF_CHANNEL 15
00028 #endif
00029 
00030 #ifndef WITH_UIP
00031 #define WITH_UIP 1
00032 #endif
00033 
00034 #if WITH_UIP
00035 #include "net/uip.h"
00036 #include "net/uip-fw.h"
00037 #include "net/uip-fw-drv.h"
00038 #include "net/uip-over-mesh.h"
00039 
00040 static struct uip_fw_netif meshif =
00041   {UIP_FW_NETIF(172,16,0,0, 255,255,0,0, uip_over_mesh_send)};
00042 
00043 #define UIP_OVER_MESH_CHANNEL 9
00044 
00045 #endif /* WITH_UIP */
00046 
00047 static rimeaddr_t node_addr = {{0,2}};
00048 
00049 extern char __heap_end__;
00050 extern char __heap_start__;
00051 
00052 
00053 static const uint32_t stepper0_steps_acc[] = MICRO_STEP(0,3);
00054 static const uint32_t stepper0_steps_run[] = MICRO_STEP(0,2);
00055 static const uint32_t stepper0_steps_hold[] = MICRO_STEP(0,1);
00056 
00057 static const uint32_t stepper1_steps_acc[] = MICRO_STEP(1,3);
00058 static const uint32_t stepper1_steps_run[] = MICRO_STEP(1,2);
00059 static const uint32_t stepper1_steps_hold[] = MICRO_STEP(1,1);
00060 
00061 static StepperAccSeq seq_heap[40];
00062 
00063 static void
00064 init_seq_heap()
00065 {
00066   unsigned int i;
00067   for(i = 0; i < sizeof(seq_heap)/sizeof(seq_heap[0]); i++) {
00068     seq_heap[i].next = NULL;
00069     stepper_free_seq(&seq_heap[i]);
00070   }
00071 }
00072 
00073 static void
00074 robot_stepper_init()
00075 {
00076   init_seq_heap();
00077   stepper_init(AT91C_BASE_TC0, AT91C_ID_TC0);
00078   *AT91C_PIOA_OER = STEPPER_INHIBIT;
00079   *AT91C_PIOA_MDER = STEPPER_INHIBIT; /*  | STEPPER0_IOMASK; */
00080   *AT91C_PIOA_CODR = STEPPER_INHIBIT;
00081   stepper_init_io(1, STEPPER_IOMASK(0), stepper0_steps_acc,
00082                   stepper0_steps_run, stepper0_steps_hold,
00083                   (sizeof(stepper0_steps_run) / sizeof(stepper0_steps_run[0])));
00084   stepper_init_io(0, STEPPER_IOMASK(1), stepper1_steps_acc,
00085                   stepper1_steps_run, stepper1_steps_hold,
00086                   (sizeof(stepper1_steps_run) / sizeof(stepper1_steps_run[0])));}
00087 
00088 
00089 #if 0
00090 /* Wathcdog is  already disabled in startup code */
00091 static void
00092 wdt_setup()
00093 {
00094 
00095 }
00096 #endif
00097 
00098 static void
00099 wdt_reset()
00100 {
00101   *AT91C_WDTC_WDCR = (0xa5<<24) | AT91C_WDTC_WDRSTT;
00102 }
00103 
00104 #if 0
00105 static uip_ipaddr_t gw_addr = {{172,16,0,1}};
00106 #endif
00107 
00108 
00109 int
00110 main()
00111 {
00112   disableIRQ();
00113   disableFIQ();
00114   *AT91C_AIC_IDCR = 0xffffffff;
00115   *AT91C_PMC_PCDR = 0xffffffff;
00116   *AT91C_PMC_PCER = (1 << AT91C_ID_PIOA);
00117   
00118   dbg_setup_uart();
00119   printf("Initialising\n");
00120   leds_arch_init();
00121   clock_init();
00122   process_init();
00123   process_start(&etimer_process, NULL);
00124   ctimer_init();
00125 
00126   robot_stepper_init();
00127 
00128   enableIRQ();
00129 
00130   cc2420_init();
00131   cc2420_set_pan_addr(0x2024, 0, &uip_hostaddr.u16[1]);
00132   cc2420_set_channel(RF_CHANNEL);
00133   rime_init(nullmac_init(&cc2420_driver));
00134   printf("CC2420 setup done\n");
00135 
00136   rimeaddr_set_node_addr(&node_addr);
00137   
00138     #if WITH_UIP
00139   {
00140     uip_ipaddr_t hostaddr, netmask;
00141     
00142     uip_init();
00143 
00144     uip_ipaddr(&hostaddr, 172,16,
00145                rimeaddr_node_addr.u8[0],rimeaddr_node_addr.u8[1]);
00146     uip_ipaddr(&netmask, 255,255,0,0);
00147     uip_ipaddr_copy(&meshif.ipaddr, &hostaddr);
00148     printf("Host addr\n");
00149     uip_sethostaddr(&hostaddr);
00150     uip_setnetmask(&netmask);
00151     uip_over_mesh_set_net(&hostaddr, &netmask);
00152     /*    uip_fw_register(&slipif);*/
00153     /*uip_over_mesh_set_gateway_netif(&slipif);*/
00154     uip_fw_default(&meshif);
00155     printf("Mesh init\n");
00156     uip_over_mesh_init(UIP_OVER_MESH_CHANNEL);
00157     printf("uIP started with IP address %d.%d.%d.%d\n",
00158            uip_ipaddr_to_quad(&hostaddr));
00159   }
00160 #endif /* WITH_UIP */
00161 
00162 
00163 #if WITH_UIP
00164   process_start(&tcpip_process, NULL);
00165   process_start(&uip_fw_process, NULL); /* Start IP output */
00166 #endif /* WITH_UIP */
00167   
00168   printf("Heap size: %ld bytes\n", &__heap_end__ - (char*)sbrk(0));
00169   printf("Started\n");
00170 
00171   autostart_start(autostart_processes);
00172   printf("Processes running\n");
00173   while(1) {
00174     do {
00175       /* Reset watchdog. */
00176       wdt_reset();
00177     } while(process_run() > 0);
00178     /* Idle! */
00179     /* Stop processor clock */
00180     *AT91C_PMC_SCDR |= AT91C_PMC_PCK;
00181   }
00182   return 0;
00183 }