+ uint8_t c;
+ uint16_t servo;
+ static union byte0 byte0_rx;
+ union byte1 byte1_rx;
+ union byte0 byte0_tx;
+ static union byte1 byte1_tx;
+
+ /* reception complete ? */
+ if (!(SPSR & (1<<SPIF)))
+ return;
+
+ c = SPDR;
+
+ /* prepare next TX */
+
+ if ((spi_out_idx & 1) == 0) {
+ servo = icp_servos[spi_out_idx >> 1];
+ byte0_tx.val_msb = servo >> 7;
+ byte0_tx.cmd_num = (spi_out_idx >> 1) + 1;
+ byte0_tx.zero = 0;
+ byte1_tx.val_lsb = servo & 0x7f;
+ byte1_tx.one = 1;
+ SPDR = byte0_tx.u8;
+ }
+ else {
+ SPDR = byte1_tx.u8;
+ }
+ spi_out_idx ++;
+ if (spi_out_idx >= N_SERVO * 2)
+ spi_out_idx = 0;
+
+ /* RX */
+
+ if ((rxidx == 0) && (c & 0x80)) {
+ rxidx = 0;
+ return; /* drop */
+ }
+ if ((rxidx == 1) && ((c & 0x80) == 0)) {
+ rxidx = 0;
+ return; /* drop */
+ }
+
+ if (rxidx == 0) {
+ byte0_rx.u8 = c;
+
+ /* command num 0 is ignored */
+ if (byte0_rx.cmd_num == 0)
+ return;
+ }
+ else {
+ uint16_t val;
+
+ byte1_rx.u8 = c;
+
+ /* process command */
+
+ val = (uint16_t)byte0_rx.val_msb << 7;
+ val += byte1_rx.val_lsb;
+
+ if (byte0_rx.cmd_num < N_SERVO+1) {
+ servo_table[byte0_rx.cmd_num-1].command = val;
+ }
+ else if (byte0_rx.cmd_num == N_SERVO+1) {
+ if (val & PPM_BIT)
+ ppm_enabled = 1;
+ else
+ ppm_enabled = 0;
+ if (val & BYPASS_BIT)
+ bypass = 1;
+ else
+ bypass = 0;
+ }
+ }
+
+ rxidx ^= 1;
+}
+
+static void poll_input_capture(void)
+{
+ uint16_t icp, diff;
+
+ /* no new sample, return */
+ if ((TIFR1 & _BV(ICF1)) == 0)
+ return;
+
+ cli();
+ icp = ICR1;
+ sei();
+
+ /* clear the flag by writing a one */
+ TIFR1 = TIFR1 | _BV(ICF1);
+
+ diff = icp - icp_prev;
+ icp_prev = icp;
+
+ /* a rising edge with at least 2ms of state 0 means that we
+ * get the first servo */
+ if (diff > 3000) {
+ icp_idx = 0;
+ return;
+ }
+
+ /* get the value for the servo */
+ if (icp_idx < N_SERVO) {
+ if (diff < 1000)
+ icp_servos[icp_idx] = 0;
+ else if (diff > 2023)
+ icp_servos[icp_idx] = 1023;
+ else
+ icp_servos[icp_idx] = diff - 1000;
+ icp_idx++;
+ }
+}
+
+static void poll(void)
+{
+ poll_spi();
+ poll_input_capture();