2 #include <aversive/wait.h>
9 static struct servo servo_table[] = {
35 #define N_SERVO (sizeof(servo_table)/sizeof(*servo_table))
37 /* we use the first servo for PPM output if enabled */
40 static uint8_t bypass;
41 static uint8_t ppm_enabled;
42 static volatile uint8_t done;
43 static uint8_t portval;
46 static uint8_t icp_idx = N_SERVO;
47 static uint16_t icp_servos[N_SERVO];
48 static uint16_t icp_prev;
50 static uint8_t spi_out_idx; /* current byte beeing sent */
53 #define BYPASS_BIT 0x02
55 #define LED_ON() do { PORTB |= 0x02; } while(0)
56 #define LED_OFF() do { PORTB &= ~0x02; } while(0)
61 * A command is stored on 2 bytes (except command 0). The first byte
62 * has its most significant bit to 0, and the second one to 1. The
63 * first received byte contains the command number, and the msb of the
64 * servo value. The second byte contains the lsb of the servo value.
66 * Command 0 is only one byte long, it means "I have nothing to say".
67 * Commands 1 to N_SERVO (included) are to set the value of servo.
68 * Command N_SERVO+1 is:
69 * - to enable/disable ppm generation in place of first servo.
70 * - to enable/disable bypass mode
75 /* inverted: little endian */
85 /* inverted: little endian */
91 SIGNAL(TIMER1_COMPA_vect)
94 TIMSK1 &= ~_BV(OCIE1A);
98 static void poll_spi(void)
102 static union byte0 byte0_rx;
103 union byte1 byte1_rx;
104 union byte0 byte0_tx;
105 static union byte1 byte1_tx;
107 /* reception complete ? */
108 if (!(SPSR & (1<<SPIF)))
113 /* prepare next TX */
115 if ((spi_out_idx & 1) == 0) {
116 servo = icp_servos[spi_out_idx >> 1];
117 byte0_tx.val_msb = servo >> 7;
118 byte0_tx.cmd_num = (spi_out_idx >> 1) + 1;
120 byte1_tx.val_lsb = servo & 0x7f;
128 if (spi_out_idx >= N_SERVO * 2)
133 if ((rxidx == 0) && (c & 0x80)) {
137 if ((rxidx == 1) && ((c & 0x80) == 0)) {
145 /* command num 0 is ignored */
146 if (byte0_rx.cmd_num == 0)
154 /* process command */
156 val = (uint16_t)byte0_rx.val_msb << 7;
157 val += byte1_rx.val_lsb;
159 if (byte0_rx.cmd_num < N_SERVO+1) {
160 servo_table[byte0_rx.cmd_num-1].command = val;
162 else if (byte0_rx.cmd_num == N_SERVO+1) {
167 if (val & BYPASS_BIT)
177 static void poll_input_capture(void)
181 /* no new sample, return */
182 if ((TIFR1 & _BV(ICF1)) == 0)
189 /* clear the flag by writing a one */
190 TIFR1 = TIFR1 | _BV(ICF1);
192 diff = icp - icp_prev;
195 /* a rising edge with at least 2ms of state 0 means that we
196 * get the first servo */
202 /* get the value for the servo */
203 if (icp_idx < N_SERVO) {
205 icp_servos[icp_idx] = 0;
206 else if (diff > 2023)
207 icp_servos[icp_idx] = 1023;
209 icp_servos[icp_idx] = diff - 1000;
214 static void poll(void)
217 poll_input_capture();
220 static void load_timer_at(uint16_t t)
223 TIMSK1 |= _BV(OCIE1A);
226 static void do_servos(void)
231 /* skip first servo if ppm is enabled */
240 for (; i < N_SERVO; i++) {
242 /* set servo and PPM bit */
243 portval = 1 << servo_table[i].bit;
245 portval |= (1 << servo_table[PPM].bit);
248 load_timer_at(start);
252 /* reset PPM bit after 300us */
253 portval = 1 << servo_table[i].bit;
255 load_timer_at(start + 300);
259 start = start + 1000 + servo_table[i].command;
262 /* set PPM bit only for last servo */
265 portval |= (1 << servo_table[PPM].bit);
268 load_timer_at(start);
272 /* reset PPM bit after 300us */
275 load_timer_at(start + 300);
286 /* use pull-up for inputs */
293 #if 1 /* disable for LED debug only */
302 /* servo outputs PD2-PD7 */
305 /* start timer1 at clk/8 (1Mhz), enable noise canceler on
306 * input capture, capture rising edge */
308 TCCR1B = _BV(CS11) | _BV(ICNC1) | _BV(ICES1);
310 /* start timer0 at clk/1024 (~8Khz) */
312 TCCR0B = _BV(CS02) | _BV(CS00);
314 /* enable spi (set MISO as output) */
336 while (bypass == 1) {