Contiki 2.5
robot-main.c
1 #include <AT91SAM7S64.h>
2 #include <interrupt-utils.h>
3 #include <string.h>
4 #include <debug-uart.h>
5 #include <ctype.h>
6 #include <stdio.h>
7 #include <dev/cc2420.h>
8 #include <dev/cc2420_const.h>
9 #include <dev/spi.h>
10 #include <dev/leds.h>
11 #include <sys/process.h>
12 #include <sys/procinit.h>
13 #include <sys/autostart.h>
14 #include <sys/etimer.h>
15 #include <net/psock.h>
16 #include <unistd.h>
17 
18 #include <stepper-steps.h>
19 #include <stepper.h>
20 #include <stepper-move.h>
21 
22 #include "net/mac/nullmac.h"
23 #include "net/rime.h"
24 
25 
26 #ifndef RF_CHANNEL
27 #define RF_CHANNEL 15
28 #endif
29 
30 #ifndef WITH_UIP
31 #define WITH_UIP 1
32 #endif
33 
34 #if WITH_UIP
35 #include "net/uip.h"
36 #include "net/uip-fw.h"
37 #include "net/uip-fw-drv.h"
38 #include "net/uip-over-mesh.h"
39 
40 static struct uip_fw_netif meshif =
41  {UIP_FW_NETIF(172,16,0,0, 255,255,0,0, uip_over_mesh_send)};
42 
43 #define UIP_OVER_MESH_CHANNEL 9
44 
45 #endif /* WITH_UIP */
46 
47 static rimeaddr_t node_addr = {{0,2}};
48 
49 extern char __heap_end__;
50 extern char __heap_start__;
51 
52 
53 static const uint32_t stepper0_steps_acc[] = MICRO_STEP(0,3);
54 static const uint32_t stepper0_steps_run[] = MICRO_STEP(0,2);
55 static const uint32_t stepper0_steps_hold[] = MICRO_STEP(0,1);
56 
57 static const uint32_t stepper1_steps_acc[] = MICRO_STEP(1,3);
58 static const uint32_t stepper1_steps_run[] = MICRO_STEP(1,2);
59 static const uint32_t stepper1_steps_hold[] = MICRO_STEP(1,1);
60 
61 static StepperAccSeq seq_heap[40];
62 
63 static void
64 init_seq_heap()
65 {
66  unsigned int i;
67  for(i = 0; i < sizeof(seq_heap)/sizeof(seq_heap[0]); i++) {
68  seq_heap[i].next = NULL;
69  stepper_free_seq(&seq_heap[i]);
70  }
71 }
72 
73 static void
74 robot_stepper_init()
75 {
76  init_seq_heap();
77  stepper_init(AT91C_BASE_TC0, AT91C_ID_TC0);
78  *AT91C_PIOA_OER = STEPPER_INHIBIT;
79  *AT91C_PIOA_MDER = STEPPER_INHIBIT; /* | STEPPER0_IOMASK; */
80  *AT91C_PIOA_CODR = STEPPER_INHIBIT;
81  stepper_init_io(1, STEPPER_IOMASK(0), stepper0_steps_acc,
82  stepper0_steps_run, stepper0_steps_hold,
83  (sizeof(stepper0_steps_run) / sizeof(stepper0_steps_run[0])));
84  stepper_init_io(0, STEPPER_IOMASK(1), stepper1_steps_acc,
85  stepper1_steps_run, stepper1_steps_hold,
86  (sizeof(stepper1_steps_run) / sizeof(stepper1_steps_run[0])));}
87 
88 
89 #if 0
90 /* Wathcdog is already disabled in startup code */
91 static void
92 wdt_setup()
93 {
94 
95 }
96 #endif
97 
98 static void
99 wdt_reset()
100 {
101  *AT91C_WDTC_WDCR = (0xa5<<24) | AT91C_WDTC_WDRSTT;
102 }
103 
104 #if 0
105 static uip_ipaddr_t gw_addr = {{172,16,0,1}};
106 #endif
107 
108 
109 int
111 {
112  disableIRQ();
113  disableFIQ();
114  *AT91C_AIC_IDCR = 0xffffffff;
115  *AT91C_PMC_PCDR = 0xffffffff;
116  *AT91C_PMC_PCER = (1 << AT91C_ID_PIOA);
117 
118  dbg_setup_uart();
119  printf("Initialising\n");
120  leds_arch_init();
121  clock_init();
122  process_init();
123  process_start(&etimer_process, NULL);
124  ctimer_init();
125 
126  robot_stepper_init();
127 
128  enableIRQ();
129 
130  cc2420_init();
131  cc2420_set_pan_addr(0x2024, 0, &uip_hostaddr.u16[1]);
132  cc2420_set_channel(RF_CHANNEL);
133  rime_init(nullmac_init(&cc2420_driver));
134  printf("CC2420 setup done\n");
135 
136  rimeaddr_set_node_addr(&node_addr);
137 
138  #if WITH_UIP
139  {
140  uip_ipaddr_t hostaddr, netmask;
141 
142  uip_init();
143 
144  uip_ipaddr(&hostaddr, 172,16,
146  uip_ipaddr(&netmask, 255,255,0,0);
147  uip_ipaddr_copy(&meshif.ipaddr, &hostaddr);
148  printf("Host addr\n");
149  uip_sethostaddr(&hostaddr);
150  uip_setnetmask(&netmask);
151  uip_over_mesh_set_net(&hostaddr, &netmask);
152  /* uip_fw_register(&slipif);*/
153  /*uip_over_mesh_set_gateway_netif(&slipif);*/
154  uip_fw_default(&meshif);
155  printf("Mesh init\n");
156  uip_over_mesh_init(UIP_OVER_MESH_CHANNEL);
157  printf("uIP started with IP address %d.%d.%d.%d\n",
158  uip_ipaddr_to_quad(&hostaddr));
159  }
160 #endif /* WITH_UIP */
161 
162 
163 #if WITH_UIP
164  process_start(&tcpip_process, NULL);
165  process_start(&uip_fw_process, NULL); /* Start IP output */
166 #endif /* WITH_UIP */
167 
168  printf("Heap size: %ld bytes\n", &__heap_end__ - (char*)sbrk(0));
169  printf("Started\n");
170 
171  autostart_start(autostart_processes);
172  printf("Processes running\n");
173  while(1) {
174  do {
175  /* Reset watchdog. */
176  wdt_reset();
177  } while(process_run() > 0);
178  /* Idle! */
179  /* Stop processor clock */
180  *AT91C_PMC_SCDR |= AT91C_PMC_PCK;
181  }
182  return 0;
183 }