2 * Copyright Droids Corporation (2009)
3 * Olivier Matz <zer0@droids-corp.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 * Revision : $Id: main.c,v 1.8 2009-05-02 10:08:09 zer0 Exp $
26 #include <aversive/wait.h>
27 #include <aversive/pgmspace.h>
33 * Photodiodes: PC0 PC1
38 * hfuse: RSTDISBL=1 WTDON=1 SPIEN=0 CKOPT=0 EESAVE=1 BOOTSZ1=0 BOOTSZ0=0 BOOTRST=1
39 * lfuse: BODLEVEL=1 BODEN=1 SUT1=1 SUT0=1 CKSEL3=1 CKSEL2=1 CKSEL1=1 CKSEL0=1
42 //#define NO_MODULATION
44 //#define MODUL_455KHZ
50 /* beacon identifier: must be odd, 3 bits */
52 #define BEACON_ID_MASK 0x7
53 #define BEACON_ID_SHIFT 0
56 #define FRAME_DATA_MASK 0x0FF8
57 #define FRAME_DATA_SHIFT 3
59 #if (defined MODUL_455KHZ)
63 #elif (defined MODUL_56KHZ)
65 #define N_CYCLES_0 143
66 #define N_CYCLES_1 143
67 #elif (defined MODUL_38KHZ)
69 #define N_CYCLES_0 210
70 #define N_CYCLES_1 210
72 #error "no freq defined"
76 #define MAX_DIST 3600.
77 #define LASER_DIST 25.
79 #define N_CYCLES_PERIOD (N_CYCLES_0 + N_CYCLES_1)
81 #define LED_PORT PORTD
87 #define LED_TOGGLE(port, bit) do { \
88 if (port & _BV(bit)) \
94 #define LED1_ON() sbi(LED_PORT, LED1_BIT)
95 #define LED1_OFF() cbi(LED_PORT, LED1_BIT)
96 #define LED1_TOGGLE() LED_TOGGLE(LED_PORT, LED1_BIT)
97 #define LED2_ON() sbi(LED_PORT, LED2_BIT)
98 #define LED2_OFF() cbi(LED_PORT, LED2_BIT)
99 #define LED2_TOGGLE() LED_TOGGLE(LED_PORT, LED2_BIT)
100 #define LED3_ON() sbi(LED_PORT, LED3_BIT)
101 #define LED3_OFF() cbi(LED_PORT, LED3_BIT)
102 #define LED3_TOGGLE() LED_TOGGLE(LED_PORT, LED3_BIT)
104 #define IR_PORT PORTB
108 /* FRAME must be odd */
109 /* #define FRAME 0x0B /\* in little endian 1-1-0-1 *\/ */
110 /* #define FRAME_LEN 4 */
111 #define FRAME 0xAA5B /* in little endian */
114 /* pin returns !0 when nothing, and 0 when laser is on photodiode */
115 #define PHOTO_PIN PINB
117 #define READ_PHOTO() (!!(PHOTO_PIN & (_BV(PHOTO_BIT))))
119 /* IR_DELAY **must** be < 32768 */
120 #if (defined SPEED_10RPS)
121 #define MIN_INTER_TIME ((uint16_t)(160*2)) /* t~=160us dist=350cm */
122 #define MAX_INTER_TIME ((uint16_t)(8000*2)) /* t=8ms dist=10cm */
123 #define IR_DELAY ((uint16_t)(8000*2))
124 #define INTER_LASER_TIME 30 /* in ms */
125 #elif (defined SPEED_40RPS)
126 #define MIN_INTER_TIME ((uint16_t)(40*16)) /* t~=40us dist=350cm */
127 #define MAX_INTER_TIME ((uint16_t)(2000*16)) /* t=2ms dist=10cm */
128 #define IR_DELAY ((uint16_t)(2000*16))
129 #define INTER_LASER_TIME 10 /* in ms */
131 #error "speed not defined"
134 extern prog_uint16_t framedist_table[];
135 /* basic functions to transmit on IR */
137 static inline void xmit_0(void)
139 uint16_t t = ((N_CYCLES_PERIOD * N_PERIODS) / 4);
141 cbi(IR_PORT, IR_BIT);
146 _delay_loop_2(t); /* 4 cycles per loop */
149 static inline void xmit_1(void)
151 uint16_t t = ((N_CYCLES_PERIOD * N_PERIODS) / 4);
153 sbi(IR_PORT, IR_BIT);
155 TCCR1B = _BV(WGM13) | _BV(WGM12);
156 TCNT1 = N_CYCLES_PERIOD-1;
157 TCCR1A = _BV(COM1A1) | _BV(WGM11);
158 ICR1 = N_CYCLES_PERIOD;
159 TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10);
161 _delay_loop_2(t); /* 4 cycles per loop */
164 /* transmit in manchester code */
166 static inline void xmit_manchester_0(void)
172 static inline void xmit_manchester_1(void)
178 /* transmit a full frame. Each byte is lsb first. */
180 static void xmit_bits(uint32_t frame, uint8_t nbit)
184 for (i=0; i<nbit; i++) {
197 /* val is 12 bits. Return the 16 bits value that includes the 4 bits
199 static uint16_t do_cksum(uint16_t val)
204 /* add the three 4-bits blocks of x together */
208 cksum = (cksum & 0xf) + ((cksum & 0xf0) >> 4);
211 cksum = (cksum & 0xf) + ((cksum & 0xf0) >> 4);
212 cksum = (~cksum) & 0xf;
213 return (cksum << 12) + (val & 0xfff);
216 /* get the frame from laser time difference, return 0 on error (this
217 * frame cannot be sent). */
218 static uint32_t get_frame(uint16_t laserdiff)
221 uint16_t frame_dist = 256;
224 /* for calibration, return the time */
228 for (step = 128; step != 0; step /= 2) {
229 val = pgm_read_word(&framedist_table[frame_dist]);
236 frame |= ((uint32_t)(frame_dist & FRAME_DATA_MASK) << FRAME_DATA_SHIFT);
238 /* process cksum and return */
239 return do_cksum(frame);
242 /* Wait 2 consecutive rising edges on photodiode. Return 0 on success,
243 * in this case, the 'when' pointed area is assigned to the time when
244 * IR signal should be sent. The 'laserdiff' pointer is the time
245 * between the 2 lasers, in timer unit. */
246 static inline int8_t wait_laser(uint16_t *when, uint16_t *laserdiff)
248 uint16_t time1, time2;
252 /* set timer to 16Mhz, we will use ICP */
256 /* set timer to 2Mhz, we will use ICP */
261 /* wait until all is off (inverted logic) */
262 while (READ_PHOTO() == 0);
265 /* wait falling edge */
266 while ((TIFR & _BV(ICF1)) == 0);
271 /* wait a bit to avoid oscillations */
272 while ((uint16_t)(TCNT1-time1) < MIN_INTER_TIME);
275 /* wait next falling edge + timeout */
276 while ((TIFR & _BV(ICF1)) == 0) {
277 diff = TCNT1 - time1;
278 if (diff > MAX_INTER_TIME)
284 /* get time of 2nd laser */
288 /* process time difference */
289 diff = time2 - time1;
290 if (diff < MIN_INTER_TIME)
293 *when = time1 + (diff/2) + IR_DELAY;
306 uint32_t frame = FRAME;
313 LED_DDR = _BV(LED1_BIT) | _BV(LED2_BIT) | _BV(LED3_BIT);
314 IR_DDR |= _BV(IR_BIT);
317 fdevopen(uart0_dev_send, uart0_dev_recv);
323 c = uart_recv_nowait(0);
325 printf("%c", (char)(c+1));
342 #ifndef NO_MODULATION
344 ICR1 = N_CYCLES_PERIOD;
346 TCCR1A = _BV(COM1A1) | _BV(WGM11);
347 TCCR1B = _BV(WGM13) | _BV(WGM12);
352 ICR1 = N_CYCLES_PERIOD;
354 TCCR1B = _BV(WGM13) | _BV(WGM12);
355 TCNT1 = N_CYCLES_PERIOD-1;
356 TCCR1A = _BV(COM1A1) | _BV(WGM11);
357 TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10);
362 /* configure timer 0, prescaler = 64 */
363 TCCR0 = _BV(CS01) | _BV(CS00);
368 ret = wait_laser(&when, &diff);
376 frame = get_frame(diff);
377 /* cannot convert into frame... skip it */
379 wait_ms(INTER_LASER_TIME);
383 /* wait before IR xmit */
384 while ((int16_t)(when-TCNT1) > 0);
388 /* ok, transmit frame */
389 xmit_bits(frame, FRAME_LEN);
393 /* don't watch a laser during this time */
394 wait_ms(INTER_LASER_TIME);