2 #include <aversive/wait.h>
9 static struct servo servo_table[] = {
35 #define NB_SERVO (sizeof(servo_table)/sizeof(*servo_table))
37 static uint8_t bypass;
38 static volatile uint8_t done;
39 static uint8_t portval;
42 static uint8_t icp_idx = NB_SERVO;
43 static uint16_t icp_servos[NB_SERVO];
44 static uint16_t icp_prev;
46 #define BYPASS_ENABLE 14
47 #define BYPASS_DISABLE 15
49 #define LED_ON() do { PORTB |= 0x02; } while(0)
50 #define LED_OFF() do { PORTB &= ~0x02; } while(0)
55 * A command is stored on 2 bytes (except command 0). The first byte
56 * has its most significant bit to 0, and the second one to 1. The
57 * first received byte contains the command number, and the msb of the
58 * servo value. The second byte contains the lsb of the servo value.
60 * Command 0 is only one byte long, it means "I have nothing to say".
61 * Commands 1 to NB_SERVO+1 are to set the value of servo.
62 * Command 14 is to enable bypass mode.
63 * Command 15 is to disable bypass mode.
68 /* inverted: little endian */
78 /* inverted: little endian */
84 SIGNAL(TIMER1_COMPA_vect)
87 TIMSK1 &= ~_BV(OCIE1A);
91 static void poll_spi(void)
95 /* reception complete ? */
96 if (!(SPSR & (1<<SPIF)))
100 if ((rxidx == 0) && (c & 0x80)) {
104 if ((rxidx == 1) && ((c & 0x80) == 0)) {
112 /* command num 0 is ignored */
113 if (byte0.cmd_num == 0)
121 /* process command */
123 if (byte0.cmd_num < NB_SERVO+1) {
124 val = (uint16_t)byte0.val_msb << 7;
125 val += byte1.val_lsb;
126 servo_table[byte0.cmd_num-1].command = val;
128 else if (byte0.cmd_num == BYPASS_ENABLE) {
131 else if (byte0.cmd_num == BYPASS_DISABLE) {
139 static void poll_input_capture(void)
144 /* no new sample, return */
145 if ((TIFR1 & _BV(ICF1)) == 0)
152 rising = TCCR1B & _BV(ICES1);
154 /* change the edge type */
155 TCCR1B ^= _BV(ICES1);
158 TIFR1 = TIFR1 | _BV(ICF1);
160 diff = icp - icp_prev;
163 /* a rising edge with at least 2ms of state 0 means that we
164 * get the first servo */
165 if (rising == 1 && diff > 2000) {
170 /* get the value for the servo */
171 if (rising == 0 && icp_idx < NB_SERVO) {
173 icp_servos[icp_idx] = 0;
175 icp_servos[icp_idx] = diff - 1000;
180 static void poll(void)
183 poll_input_capture();
186 static void load_timer_at(uint16_t t)
189 TIMSK1 |= _BV(OCIE1A);
192 static void do_one_servo(struct servo *s)
198 //portval = PORTC | (1 << s->bit);
199 portval = (1 << s->bit);
201 load_timer_at(t + 20);
208 //portval = PORTC & (~(1 << s->bit));
209 load_timer_at(t + 20 + 1000 + s->command);
220 /* use pull-up for inputs */
226 #if 0 /* LED debug */
235 /* servo outputs PD2-PD7 */
238 /* start timer1 at clk/8 (1Mhz), enable noise canceler on
239 * input capture, capture rising edge */
241 TCCR1B = _BV(CS11) | _BV(ICNC1) | _BV(ICES1);
243 /* start timer0 at clk/1024 (~8Khz) */
245 TCCR0B = _BV(CS02) | _BV(CS00);
247 /* enable spi (don't set unused MISO as output) */
255 for (i = 0; i < NB_SERVO; i++) {
256 do_one_servo(&servo_table[i]);
269 while (bypass == 1) {