work on beacon
[aversive.git] / projects / microb2010 / tests / static_beacon / coin.c
1 # 1 "/home/zer0/zavr/projects/microb2010/tests/static_beacon/static_beacon.c"
2 # 1 "/home/zer0/zavr/projects/microb2010/tests/static_beacon//"
3 # 1 "<built-in>"
4 # 1 "<command-line>"
5 # 1 "/home/zer0/zavr/projects/microb2010/tests/static_beacon/static_beacon.c"
6 # 23 "/home/zer0/zavr/projects/microb2010/tests/static_beacon/static_beacon.c"
7 # 1 "/home/zer0/zavr/include/aversive.h" 1
8 # 31 "/home/zer0/zavr/include/aversive.h"
9 # 1 "./autoconf.h" 1
10 # 32 "/home/zer0/zavr/include/aversive.h" 2
11
12
13 # 1 "/usr/lib/gcc/avr/4.3.2/../../../avr/include/avr/interrupt.h" 1 3
14 # 38 "/usr/lib/gcc/avr/4.3.2/../../../avr/include/avr/interrupt.h" 3
15 # 1 "/usr/lib/gcc/avr/4.3.2/../../../avr/include/avr/io.h" 1 3
16 # 94 "/usr/lib/gcc/avr/4.3.2/../../../avr/include/avr/io.h" 3
17 # 1 "/usr/lib/gcc/avr/4.3.2/../../../avr/include/avr/sfr_defs.h" 1 3
18 # 126 "/usr/lib/gcc/avr/4.3.2/../../../avr/include/avr/sfr_defs.h" 3
19 # 1 "/usr/lib/gcc/avr/4.3.2/../../../avr/include/inttypes.h" 1 3
20 # 37 "/usr/lib/gcc/avr/4.3.2/../../../avr/include/inttypes.h" 3
21 # 1 "/usr/lib/gcc/avr/4.3.2/../../../avr/include/stdint.h" 1 3
22 # 121 "/usr/lib/gcc/avr/4.3.2/../../../avr/include/stdint.h" 3
23 typedef int int8_t __attribute__((__mode__(__QI__)));
24 typedef unsigned int uint8_t __attribute__((__mode__(__QI__)));
25 typedef int int16_t __attribute__ ((__mode__ (__HI__)));
26 typedef unsigned int uint16_t __attribute__ ((__mode__ (__HI__)));
27 typedef int int32_t __attribute__ ((__mode__ (__SI__)));
28 typedef unsigned int uint32_t __attribute__ ((__mode__ (__SI__)));
29
30 typedef int int64_t __attribute__((__mode__(__DI__)));
31 typedef unsigned int uint64_t __attribute__((__mode__(__DI__)));
32 # 142 "/usr/lib/gcc/avr/4.3.2/../../../avr/include/stdint.h" 3
33 typedef int16_t intptr_t;
34
35
36
37
38 typedef uint16_t uintptr_t;
39 # 159 "/usr/lib/gcc/avr/4.3.2/../../../avr/include/stdint.h" 3
40 typedef int8_t int_least8_t;
41
42
43
44
45 typedef uint8_t uint_least8_t;
46
47
48
49
50 typedef int16_t int_least16_t;
51
52
53
54
55 typedef uint16_t uint_least16_t;
56
57
58
59
60 typedef int32_t int_least32_t;
61
62
63
64
65 typedef uint32_t uint_least32_t;
66
67
68
69
70
71
72
73 typedef int64_t int_least64_t;
74
75
76
77
78
79
80 typedef uint64_t uint_least64_t;
81 # 213 "/usr/lib/gcc/avr/4.3.2/../../../avr/include/stdint.h" 3
82 typedef int8_t int_fast8_t;
83
84
85
86
87 typedef uint8_t uint_fast8_t;
88
89
90
91
92 typedef int16_t int_fast16_t;
93
94
95
96
97 typedef uint16_t uint_fast16_t;
98
99
100
101
102 typedef int32_t int_fast32_t;
103
104
105
106
107 typedef uint32_t uint_fast32_t;
108
109
110
111
112
113
114
115 typedef int64_t int_fast64_t;
116
117
118
119
120
121
122 typedef uint64_t uint_fast64_t;
123 # 273 "/usr/lib/gcc/avr/4.3.2/../../../avr/include/stdint.h" 3
124 typedef int64_t intmax_t;
125
126
127
128
129 typedef uint64_t uintmax_t;
130 # 38 "/usr/lib/gcc/avr/4.3.2/../../../avr/include/inttypes.h" 2 3
131 # 77 "/usr/lib/gcc/avr/4.3.2/../../../avr/include/inttypes.h" 3
132 typedef int32_t int_farptr_t;
133
134
135
136 typedef uint32_t uint_farptr_t;
137 # 127 "/usr/lib/gcc/avr/4.3.2/../../../avr/include/avr/sfr_defs.h" 2 3
138 # 95 "/usr/lib/gcc/avr/4.3.2/../../../avr/include/avr/io.h" 2 3
139 # 229 "/usr/lib/gcc/avr/4.3.2/../../../avr/include/avr/io.h" 3
140 # 1 "/usr/lib/gcc/avr/4.3.2/../../../avr/include/avr/iom8.h" 1 3
141 # 230 "/usr/lib/gcc/avr/4.3.2/../../../avr/include/avr/io.h" 2 3
142 # 317 "/usr/lib/gcc/avr/4.3.2/../../../avr/include/avr/io.h" 3
143 # 1 "/usr/lib/gcc/avr/4.3.2/../../../avr/include/avr/portpins.h" 1 3
144 # 318 "/usr/lib/gcc/avr/4.3.2/../../../avr/include/avr/io.h" 2 3
145
146 # 1 "/usr/lib/gcc/avr/4.3.2/../../../avr/include/avr/common.h" 1 3
147 # 320 "/usr/lib/gcc/avr/4.3.2/../../../avr/include/avr/io.h" 2 3
148
149 # 1 "/usr/lib/gcc/avr/4.3.2/../../../avr/include/avr/version.h" 1 3
150 # 322 "/usr/lib/gcc/avr/4.3.2/../../../avr/include/avr/io.h" 2 3
151
152
153 # 1 "/usr/lib/gcc/avr/4.3.2/../../../avr/include/avr/fuse.h" 1 3
154 # 223 "/usr/lib/gcc/avr/4.3.2/../../../avr/include/avr/fuse.h" 3
155 typedef struct
156 {
157     unsigned char low;
158     unsigned char high;
159 } __fuse_t;
160 # 325 "/usr/lib/gcc/avr/4.3.2/../../../avr/include/avr/io.h" 2 3
161
162
163 # 1 "/usr/lib/gcc/avr/4.3.2/../../../avr/include/avr/lock.h" 1 3
164 # 328 "/usr/lib/gcc/avr/4.3.2/../../../avr/include/avr/io.h" 2 3
165 # 39 "/usr/lib/gcc/avr/4.3.2/../../../avr/include/avr/interrupt.h" 2 3
166 # 35 "/home/zer0/zavr/include/aversive.h" 2
167
168
169
170 # 1 "/home/zer0/zavr/include/aversive/types.h" 1
171 # 47 "/home/zer0/zavr/include/aversive/types.h"
172 typedef uint8_t u08;
173 typedef uint16_t u16;
174 typedef uint32_t u32;
175 typedef uint64_t u64;
176 typedef int8_t s08;
177 typedef int16_t s16;
178 typedef int32_t s32;
179 typedef int64_t s64;
180 # 39 "/home/zer0/zavr/include/aversive.h" 2
181 # 1 "/home/zer0/zavr/include/aversive/errno.h" 1
182 # 33 "/home/zer0/zavr/include/aversive/errno.h"
183 # 1 "/usr/lib/gcc/avr/4.3.2/../../../avr/include/errno.h" 1 3
184 # 55 "/usr/lib/gcc/avr/4.3.2/../../../avr/include/errno.h" 3
185 extern int errno;
186 # 34 "/home/zer0/zavr/include/aversive/errno.h" 2
187 # 40 "/home/zer0/zavr/include/aversive.h" 2
188 # 1 "/home/zer0/zavr/include/aversive/irq_lock.h" 1
189 # 41 "/home/zer0/zavr/include/aversive.h" 2
190 # 125 "/home/zer0/zavr/include/aversive.h"
191 struct extract32 {
192  union {
193   struct {
194
195    uint8_t u8_0;
196    uint8_t u8_1;
197    uint8_t u8_2;
198    uint8_t u8_3;
199
200
201
202
203
204
205   } __attribute__ ((packed)) u8;
206   struct {
207
208    uint16_t u16_0;
209    uint16_t u16_1;
210
211
212
213
214   } __attribute__ ((packed)) u16;
215   struct {
216
217    uint8_t u8_0;
218    uint16_t u16_mid;
219    uint8_t u8_3;
220
221
222
223
224
225   } __attribute__ ((packed)) u16_b;
226   uint32_t u32;
227  } __attribute__ ((packed)) u;
228 } __attribute__ ((packed));
229 # 174 "/home/zer0/zavr/include/aversive.h"
230 struct extract16 {
231  union {
232   struct {
233
234    uint8_t u8_0;
235    uint8_t u8_1;
236
237
238
239
240   } __attribute__ ((packed)) u8;
241   uint16_t u16;
242  } __attribute__ ((packed)) u;
243 } __attribute__ ((packed));
244 # 24 "/home/zer0/zavr/projects/microb2010/tests/static_beacon/static_beacon.c" 2
245 # 1 "/home/zer0/zavr/include/aversive/wait.h" 1
246 # 45 "/home/zer0/zavr/include/aversive/wait.h"
247 # 1 "/usr/lib/gcc/avr/4.3.2/../../../avr/include/util/delay.h" 1 3
248 # 39 "/usr/lib/gcc/avr/4.3.2/../../../avr/include/util/delay.h" 3
249 # 1 "/usr/lib/gcc/avr/4.3.2/../../../avr/include/util/delay_basic.h" 1 3
250 # 65 "/usr/lib/gcc/avr/4.3.2/../../../avr/include/util/delay_basic.h" 3
251 static inline void _delay_loop_1(uint8_t __count) __attribute__((always_inline));
252 static inline void _delay_loop_2(uint16_t __count) __attribute__((always_inline));
253 # 80 "/usr/lib/gcc/avr/4.3.2/../../../avr/include/util/delay_basic.h" 3
254 void
255 _delay_loop_1(uint8_t __count)
256 {
257  __asm__ volatile (
258   "1: dec %0" "\n\t"
259   "brne 1b"
260   : "=r" (__count)
261   : "0" (__count)
262  );
263 }
264 # 102 "/usr/lib/gcc/avr/4.3.2/../../../avr/include/util/delay_basic.h" 3
265 void
266 _delay_loop_2(uint16_t __count)
267 {
268  __asm__ volatile (
269   "1: sbiw %0,1" "\n\t"
270   "brne 1b"
271   : "=w" (__count)
272   : "0" (__count)
273  );
274 }
275 # 40 "/usr/lib/gcc/avr/4.3.2/../../../avr/include/util/delay.h" 2 3
276 # 79 "/usr/lib/gcc/avr/4.3.2/../../../avr/include/util/delay.h" 3
277 static inline void _delay_us(double __us) __attribute__((always_inline));
278 static inline void _delay_ms(double __ms) __attribute__((always_inline));
279 # 107 "/usr/lib/gcc/avr/4.3.2/../../../avr/include/util/delay.h" 3
280 void
281 _delay_us(double __us)
282 {
283  uint8_t __ticks;
284  double __tmp = ((((unsigned long)(16000000))) / 3e6) * __us;
285  if (__tmp < 1.0)
286   __ticks = 1;
287  else if (__tmp > 255)
288  {
289   _delay_ms(__us / 1000.0);
290   return;
291  }
292  else
293   __ticks = (uint8_t)__tmp;
294  _delay_loop_1(__ticks);
295 }
296 # 141 "/usr/lib/gcc/avr/4.3.2/../../../avr/include/util/delay.h" 3
297 void
298 _delay_ms(double __ms)
299 {
300  uint16_t __ticks;
301  double __tmp = ((((unsigned long)(16000000))) / 4e3) * __ms;
302  if (__tmp < 1.0)
303   __ticks = 1;
304  else if (__tmp > 65535)
305  {
306
307   __ticks = (uint16_t) (__ms * 10.0);
308   while(__ticks)
309   {
310
311    _delay_loop_2(((((unsigned long)(16000000))) / 4e3) / 10);
312    __ticks --;
313   }
314   return;
315  }
316  else
317   __ticks = (uint16_t)__tmp;
318  _delay_loop_2(__ticks);
319 }
320 # 46 "/home/zer0/zavr/include/aversive/wait.h" 2
321 # 59 "/home/zer0/zavr/include/aversive/wait.h"
322 static inline void wait_ms(uint16_t n)
323 {
324   while ( n -- )
325     _delay_loop_2(((unsigned long)(16000000))/4000);
326 }
327 # 25 "/home/zer0/zavr/projects/microb2010/tests/static_beacon/static_beacon.c" 2
328 # 100 "/home/zer0/zavr/projects/microb2010/tests/static_beacon/static_beacon.c"
329 static inline void xmit_0(void)
330 {
331  uint8_t t = (((17 + 17) * 10) / 3);
332  (*(volatile uint8_t *)((0x2E) + 0x20)) = 0;
333  (*(volatile uint8_t *)((0x2F) + 0x20)) = 0;
334  _delay_loop_1(t);
335 }
336
337 static inline void xmit_1(void)
338 {
339  uint8_t t = (((17 + 17) * 10) / 3);
340  (*(volatile uint8_t *)((0x2E) + 0x20)) = (1 << (4)) | (1 << (3));
341  (*(volatile uint16_t *)((0x2C) + 0x20)) = (17 + 17)-1;
342  (*(volatile uint8_t *)((0x2F) + 0x20)) = (1 << (7)) | (1 << (1));
343  (*(volatile uint8_t *)((0x2E) + 0x20)) = (1 << (4)) | (1 << (3)) | (1 << (0));
344  _delay_loop_1(t);
345 }
346
347
348
349 static inline void xmit_manchester_0(void)
350 {
351  xmit_0();
352  xmit_1();
353 }
354
355 static inline void xmit_manchester_1(void)
356 {
357  xmit_1();
358  xmit_0();
359 }
360
361
362
363 static void xmit_bits(uint8_t *buf, uint8_t nbit)
364 {
365  uint8_t i;
366  uint8_t byte = *buf;
367
368  for (i=0; i<nbit; i++) {
369   if (byte & 1)
370    xmit_manchester_1();
371   else
372    xmit_manchester_0();
373
374
375   byte >>= 1;
376
377
378   if (((i & 0x07) == 0) && (i != 0))
379    byte = *(++buf);
380  }
381  xmit_0();
382 }
383
384
385 static inline int8_t wait_laser(void)
386 {
387  uint8_t photos;
388  uint8_t time;
389
390
391
392  while (((*(volatile uint8_t *)((0x13) + 0x20)) & ((1 << (0)) | (1 << (1)))) != ((1 << (0)) | (1 << (1))));
393
394
395  while ((photos = ((*(volatile uint8_t *)((0x13) + 0x20)) & ((1 << (0)) | (1 << (1))))) == ((1 << (0)) | (1 << (1))));
396
397  if (photos != ((1 << (1))))
398   return -1;
399  time = (*(volatile uint8_t *)((0x32) + 0x20));
400  ( (*(volatile uint8_t *)((0x12) + 0x20)) |= (1 << (5)));
401
402
403  while ((photos = ((*(volatile uint8_t *)((0x13) + 0x20)) & ((1 << (0)) | (1 << (1))))) == ((1 << (1)))) {
404   if ((*(volatile uint8_t *)((0x32) + 0x20)) - time > ((uint8_t)25))
405    return -1;
406  }
407  if (photos != ((1 << (0)) | (1 << (1))))
408   return -1;
409
410
411
412  while ((photos = ((*(volatile uint8_t *)((0x13) + 0x20)) & ((1 << (0)) | (1 << (1))))) == ((1 << (0)) | (1 << (1)))) {
413   if ((*(volatile uint8_t *)((0x32) + 0x20)) - time > ((uint8_t)250))
414    return -1;
415  }
416  ( (*(volatile uint8_t *)((0x12) + 0x20)) |= (1 << (6)));
417  if ((*(volatile uint8_t *)((0x32) + 0x20)) - time < ((uint8_t)0))
418   return -1;
419  if (photos != ((1 << (0))))
420   return -1;
421 # 206 "/home/zer0/zavr/projects/microb2010/tests/static_beacon/static_beacon.c"
422  return 0;
423 }
424
425
426
427 int main(void)
428 {
429
430  uint8_t frame = 0x0B;
431  int8_t ret;
432
433
434  (*(volatile uint8_t *)((0x11) + 0x20)) = (1 << (5)) | (1 << (6)) | (1 << (7));
435  (*(volatile uint8_t *)((0x17) + 0x20)) |= (1 << (1));
436 # 240 "/home/zer0/zavr/projects/microb2010/tests/static_beacon/static_beacon.c"
437  (*(volatile uint16_t *)((0x26) + 0x20)) = (17 + 17);
438  (*(volatile uint16_t *)((0x2A) + 0x20)) = 17;
439  (*(volatile uint8_t *)((0x2F) + 0x20)) = (1 << (7)) | (1 << (1));
440  (*(volatile uint8_t *)((0x2E) + 0x20)) = (1 << (4)) | (1 << (3));
441
442
443  (*(volatile uint8_t *)((0x33) + 0x20)) = (1 << (1)) | (1 << (0));
444
445  while (1) {
446
447
448   ret = wait_laser();
449
450   ( (*(volatile uint8_t *)((0x12) + 0x20)) &= ~ (1 << (5)));
451   ( (*(volatile uint8_t *)((0x12) + 0x20)) &= ~ (1 << (6)));
452
453   if (ret)
454    continue;
455
456
457
458   ( (*(volatile uint8_t *)((0x12) + 0x20)) |= (1 << (7)));
459
460   xmit_bits(&frame, 4);
461
462
463   wait_ms(10);
464   ( (*(volatile uint8_t *)((0x12) + 0x20)) &= ~ (1 << (7)));
465
466
467
468
469
470  }
471  return 0;
472 }