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
49 //#define SEND_RAWTIME
53 /* beacon identifier: must be odd, 3 bits */
55 #define BEACON_ID_MASK 0x7
56 #define BEACON_ID_SHIFT 0
59 #define FRAME_DATA_MASK 0x0FF8
60 #define FRAME_DATA_SHIFT 3
62 #if (defined MODUL_455KHZ)
66 #elif (defined MODUL_56KHZ)
68 #define N_CYCLES_0 143
69 #define N_CYCLES_1 143
70 #elif (defined MODUL_38KHZ)
72 #define N_CYCLES_0 210
73 #define N_CYCLES_1 210
75 #error "no freq defined"
79 #define MAX_DIST 3600.
80 #define LASER_DIST 25.
82 #define N_CYCLES_PERIOD (N_CYCLES_0 + N_CYCLES_1)
84 #define LED_PORT PORTD
90 #define LED_TOGGLE(port, bit) do { \
91 if (port & _BV(bit)) \
97 #define LED1_ON() sbi(LED_PORT, LED1_BIT)
98 #define LED1_OFF() cbi(LED_PORT, LED1_BIT)
99 #define LED1_TOGGLE() LED_TOGGLE(LED_PORT, LED1_BIT)
100 #define LED2_ON() sbi(LED_PORT, LED2_BIT)
101 #define LED2_OFF() cbi(LED_PORT, LED2_BIT)
102 #define LED2_TOGGLE() LED_TOGGLE(LED_PORT, LED2_BIT)
103 #define LED3_ON() sbi(LED_PORT, LED3_BIT)
104 #define LED3_OFF() cbi(LED_PORT, LED3_BIT)
105 #define LED3_TOGGLE() LED_TOGGLE(LED_PORT, LED3_BIT)
107 #define IR_PORT PORTB
111 #define LOCKBUS_PORT PORTD
112 #define LOCKBUS_PIN PIND
113 #define LOCKBUS_DDR DDRD
114 #define LOCKBUS_BIT 3
116 /* FRAME must be odd */
117 /* #define FRAME 0x0B /\* in little endian 1-1-0-1 *\/ */
118 /* #define FRAME_LEN 4 */
119 #define FRAME 0x621 /* in little endian */
122 /* pin returns !0 when nothing, and 0 when laser is on photodiode */
123 #define PHOTO_PIN PINB
125 #define READ_PHOTO() (!!(PHOTO_PIN & (_BV(PHOTO_BIT))))
127 /* IR_DELAY must be < 32768 */
128 #define MIN_INTER_TIME ((uint16_t)(100*16U)) /* t~=160us dist=400cm */
129 #define MAX_INTER_TIME ((uint16_t)(4000*16U)) /* t=4ms dist=16cm */
130 #define IR_DELAY ((uint16_t)(MAX_INTER_TIME/2))
131 #define INTER_LASER_TIME 10 /* in ms */
133 extern prog_uint16_t framedist_table[];
134 /* basic functions to transmit on IR */
136 static inline void xmit_0(void)
138 uint16_t t = ((N_CYCLES_PERIOD * N_PERIODS) / 4);
140 cbi(IR_PORT, IR_BIT);
145 _delay_loop_2(t); /* 4 cycles per loop */
148 static inline void xmit_1(void)
150 uint16_t t = ((N_CYCLES_PERIOD * N_PERIODS) / 4);
152 sbi(IR_PORT, IR_BIT);
154 TCCR1B = _BV(WGM13) | _BV(WGM12);
155 TCNT1 = N_CYCLES_PERIOD-1;
156 TCCR1A = _BV(COM1A1) | _BV(WGM11);
157 ICR1 = N_CYCLES_PERIOD;
158 TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10);
160 _delay_loop_2(t); /* 4 cycles per loop */
163 /* transmit in manchester code */
165 static inline void xmit_manchester_0(void)
171 static inline void xmit_manchester_1(void)
177 /* transmit a full frame. Each byte is lsb first. */
179 static void xmit_bits(uint32_t frame, uint8_t nbit)
183 for (i=0; i<nbit; i++) {
195 /* val is 12 bits. Return the 16 bits value that includes the 4 bits
197 static uint16_t do_cksum(uint16_t val)
202 /* add the three 4-bits blocks of x together */
206 cksum = (cksum & 0xf) + ((cksum & 0xf0) >> 4);
209 cksum = (cksum & 0xf) + ((cksum & 0xf0) >> 4);
210 cksum = (~cksum) & 0xf;
211 return (cksum << 12) + (val & 0xfff);
214 /* get the frame from laser time difference, return 0 on error (this
215 * frame cannot be sent). */
216 static uint32_t get_frame(uint16_t laserdiff)
219 uint16_t val, mid, min, max;
221 /* for calibration, return the time */
223 static uint16_t fr = 1;
228 return laserdiff | 1; /* frame must be odd */
234 mid = (max + min + 1) / 2;
235 val = pgm_read_word(&framedist_table[mid]);
243 frame |= ((uint32_t)((min << FRAME_DATA_SHIFT)
247 /* process cksum and return */
248 return do_cksum(frame);
251 /* Wait 2 consecutive rising edges on photodiode. Return 0 on success,
252 * in this case, the 'when' pointed area is assigned to the time when
253 * IR signal should be sent. The 'laserdiff' pointer is the time
254 * between the 2 lasers, in timer unit. */
255 static inline int8_t wait_laser(uint16_t *when, uint16_t *laserdiff)
257 uint16_t time1, time2;
260 /* set timer to 16Mhz, we will use ICP */
264 /* wait until all is off (inverted logic) */
265 while (READ_PHOTO() == 0);
268 /* wait falling edge */
269 while ((TIFR & _BV(ICF1)) == 0);
274 /* wait a bit to avoid oscillations */
275 while ((uint16_t)(TCNT1-time1) < MIN_INTER_TIME);
278 /* wait next falling edge + timeout */
279 while ((TIFR & _BV(ICF1)) == 0) {
280 diff = TCNT1 - time1;
281 if (diff > MAX_INTER_TIME)
287 /* get time of 2nd laser */
291 /* process time difference */
292 diff = time2 - time1;
293 if (diff > MAX_INTER_TIME)
298 * ir_delay is < 32768
299 * current time is ~time1 + diff
301 *when = time1 + (diff/2) + IR_DELAY;
313 uint32_t frame = FRAME;
319 LED_DDR = _BV(LED1_BIT) | _BV(LED2_BIT) | _BV(LED3_BIT);
320 IR_DDR |= _BV(IR_BIT);
323 fdevopen(uart0_dev_send, uart0_dev_recv);
329 c = uart_recv_nowait(0);
331 printf("%c", (char)(c+1));
348 #ifndef NO_MODULATION
350 ICR1 = N_CYCLES_PERIOD;
352 TCCR1A = _BV(COM1A1) | _BV(WGM11);
353 TCCR1B = _BV(WGM13) | _BV(WGM12);
358 ICR1 = N_CYCLES_PERIOD;
360 TCCR1B = _BV(WGM13) | _BV(WGM12);
361 TCNT1 = N_CYCLES_PERIOD-1;
362 TCCR1A = _BV(COM1A1) | _BV(WGM11);
363 TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10);
368 /* test freq ~ 400khz */
371 TCCR1B = _BV(WGM13) | _BV(WGM12);
373 TCCR1A = _BV(COM1A1) | _BV(WGM11);
374 TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10);
376 /* motor PWM 50%, 8khz */
379 TCCR2 = _BV(WGM21) | _BV(WGM20) | _BV(COM21) | _BV(CS21) ;
385 LOCKBUS_PORT &= (~_BV(LOCKBUS_BIT));
388 /* configure timer 0, prescaler = 64 */
389 TCCR0 = _BV(CS01) | _BV(CS00);
394 ret = wait_laser(&when, &diff);
401 #endif /* WAIT_LASER */
404 if ((LOCKBUS_PIN & _BV(LOCKBUS_BIT)) == 0)
406 LOCKBUS_DDR |= _BV(LOCKBUS_BIT);
412 frame = get_frame(diff);
413 #endif /* FIXED_FRAME */
414 /* cannot convert into frame... skip it */
416 wait_ms(INTER_LASER_TIME);
422 /* wait before IR xmit */
423 while ((int16_t)(when-TCNT1) > 0);
424 #endif /* WAIT_LASER */
427 /* ok, transmit frame */
428 xmit_bits(frame, FRAME_LEN);
433 LOCKBUS_DDR &= (~_BV(LOCKBUS_BIT));
436 /* don't watch a laser during this time */
437 wait_ms(INTER_LASER_TIME);