2 avrdude -p atmega168p -P usb -c avrispmkii -U lfuse:w:0xe2:m -U hfuse:w:0xdf:m -U efuse:w:0xf9:m
3 -> it failed but I answered y, then make reset and it was ok
7 #include <aversive/wait.h>
14 static struct servo servo_table[] = {
40 #define N_SERVO (sizeof(servo_table)/sizeof(*servo_table))
42 /* we use the first servo for PPM output if enabled */
45 static uint8_t bypass;
46 static uint8_t ppm_enabled;
47 static volatile uint8_t done;
48 static uint8_t portval;
51 static uint8_t icp_idx = N_SERVO;
52 static uint16_t icp_servos[N_SERVO];
53 static uint16_t icp_prev;
55 static uint8_t spi_out_idx; /* current byte beeing sent */
58 #define BYPASS_BIT 0x02
60 #define LED_ON() do { PORTB |= 0x02; } while(0)
61 #define LED_OFF() do { PORTB &= ~0x02; } while(0)
66 * A command is stored on 2 bytes (except command 0). The first byte
67 * has its most significant bit to 0, and the second one to 1. The
68 * first received byte contains the command number, and the msb of the
69 * servo value. The second byte contains the lsb of the servo value.
71 * Command 0 is only one byte long, it means "I have nothing to say".
72 * Commands 1 to N_SERVO (included) are to set the value of servo.
73 * Command N_SERVO+1 is:
74 * - to enable/disable ppm generation in place of first servo.
75 * - to enable/disable bypass mode
80 /* inverted: little endian */
90 /* inverted: little endian */
96 SIGNAL(TIMER1_COMPA_vect)
99 TIMSK1 &= ~_BV(OCIE1A);
103 static void poll_spi(void)
107 static union byte0 byte0_rx;
108 union byte1 byte1_rx;
109 union byte0 byte0_tx;
110 static union byte1 byte1_tx;
112 /* reception complete ? */
113 if (!(SPSR & (1<<SPIF)))
118 /* prepare next TX */
120 if ((spi_out_idx & 1) == 0) {
121 servo = icp_servos[spi_out_idx >> 1];
122 byte0_tx.val_msb = servo >> 7;
123 byte0_tx.cmd_num = (spi_out_idx >> 1) + 1;
125 byte1_tx.val_lsb = servo & 0x7f;
133 if (spi_out_idx >= N_SERVO * 2)
138 if ((rxidx == 0) && (c & 0x80)) {
142 if ((rxidx == 1) && ((c & 0x80) == 0)) {
150 /* command num 0 is ignored */
151 if (byte0_rx.cmd_num == 0)
159 /* process command */
161 val = (uint16_t)byte0_rx.val_msb << 7;
162 val += byte1_rx.val_lsb;
164 if (byte0_rx.cmd_num < N_SERVO+1) {
165 servo_table[byte0_rx.cmd_num-1].command = val;
167 else if (byte0_rx.cmd_num == N_SERVO+1) {
172 if (val & BYPASS_BIT)
182 static void poll_input_capture(void)
186 /* no new sample, return */
187 if ((TIFR1 & _BV(ICF1)) == 0)
194 /* clear the flag by writing a one */
195 TIFR1 = TIFR1 | _BV(ICF1);
197 diff = icp - icp_prev;
200 /* a rising edge with at least 2ms of state 0 means that we
201 * get the first servo */
207 /* get the value for the servo */
208 if (icp_idx < N_SERVO) {
210 icp_servos[icp_idx] = 0;
211 else if (diff > 2023)
212 icp_servos[icp_idx] = 1023;
214 icp_servos[icp_idx] = diff - 1000;
219 static void poll(void)
222 poll_input_capture();
225 static void load_timer_at(uint16_t t)
228 TIMSK1 |= _BV(OCIE1A);
231 static void do_servos(void)
236 /* skip first servo if ppm is enabled */
245 for (; i < N_SERVO; i++) {
247 /* set servo and PPM bit */
248 portval = 1 << servo_table[i].bit;
250 portval |= (1 << servo_table[PPM].bit);
253 load_timer_at(start);
257 /* reset PPM bit after 300us */
258 portval = 1 << servo_table[i].bit;
260 load_timer_at(start + 300);
264 start = start + 1000 + servo_table[i].command;
267 /* set PPM bit only for last servo */
270 portval |= (1 << servo_table[PPM].bit);
273 load_timer_at(start);
277 /* reset PPM bit after 300us */
280 load_timer_at(start + 300);
291 /* use pull-up for inputs */
298 #if 1 /* disable for LED debug only */
307 /* servo outputs PD2-PD7 */
310 /* start timer1 at clk/8 (1Mhz), enable noise canceler on
311 * input capture, capture rising edge */
313 TCCR1B = _BV(CS11) | _BV(ICNC1) | _BV(ICES1);
315 /* start timer0 at clk/1024 (~8Khz) */
317 TCCR0B = _BV(CS02) | _BV(CS00);
319 /* enable spi (set MISO as output) */
341 while (bypass == 1) {