8 static struct servo servo_table[] = {
22 #define NB_SERVO (sizeof(servo_table)/sizeof(*servo_table))
24 static volatile uint8_t bypass;
25 static volatile uint8_t done;
26 static volatile uint8_t portval;
27 static volatile uint8_t rxidx;
29 #define BYPASS_ENABLE 14
30 #define BYPASS_DISABLE 15
35 * A command is stored on 2 bytes. The first one has its msb to 0, and the
36 * second one to 1. The first received byte contains the command number, and the
37 * msb of the servo value. The second byte contains the lsb of the servo value.
39 * Commands 0 to NB_SERVO are to set the value of servo.
40 * Command 14 is to enable bypass mode.
41 * Command 15 is to disable bypass mode.
43 static volatile union {
46 /* inverted: little endian */
53 static volatile union {
56 /* inverted: little endian */
62 SIGNAL(TIMER1_COMPA_vect)
65 TIMSK1 &= ~_BV(OCIE1A);
69 static void poll_spi(void)
73 /* reception complete ? */
74 if (!(SPSR & (1<<SPIF)))
78 if ((rxidx == 0) && (c & 0x80)) {
82 if ((rxidx == 1) && ((c & 0x80) == 0)) {
97 if (byte0.cmd_num < NB_SERVO) {
98 val = (uint16_t)byte0.val_msb << 7;
100 servo_table[byte0.cmd_num].command = val;
102 else if (byte0.cmd_num == BYPASS_ENABLE) {
105 else if (byte0.cmd_num == BYPASS_DISABLE) {
113 static void load_timer_at(uint16_t t)
116 TIMSK1 |= _BV(OCIE1A);
119 static void do_one_servo(struct servo *s)
125 //portval = PORTC | (1 << s->bit);
126 portval = (1 << s->bit);
128 load_timer_at(t + 150);
135 //portval = PORTC & (~(1 << s->bit));
136 load_timer_at(t + 150 + 8000 + s->command * 8);
152 /* start timer1 at clk/1 (8Mhz) */
156 /* start timer0 at clk/1024 (~8Khz) */
158 TCCR0B = _BV(CS02) | _BV(CS00);
160 /* enable spi (don't set unused MISO as output) */
168 for (i = 0; i < NB_SERVO; i++) {
169 do_one_servo(&servo_table[i]);
179 while (bypass == 1) {