rc_servos: support spi (not tested)
authorOlivier MATZ <zer0@platinum>
Thu, 23 Feb 2012 18:33:19 +0000 (19:33 +0100)
committerOlivier MATZ <zer0@platinum>
Thu, 23 Feb 2012 20:47:59 +0000 (21:47 +0100)
main.c

diff --git a/main.c b/main.c
index 53fd747..657e54d 100644 (file)
--- a/main.c
+++ b/main.c
@@ -19,12 +19,40 @@ static struct servo servo_table[] = {
                .command = 1023,
        },
 };
+#define NB_SERVO (sizeof(servo_table)/sizeof(*servo_table))
 
-static volatile uint8_t rxbuf[16];
-register uint8_t rxlen asm("r2");
+register uint8_t bypass asm("r2");
 register uint8_t done asm("r3");
 register uint8_t portval asm("r4");
-register uint8_t bypass asm("r5");
+register uint8_t rxidx asm("r5");
+
+/*
+ * SPI protocol:
+ *
+ * A command is stored on 2 bytes. The first one has its msb to 0, and the
+ * second one to 1. The first received byte contains the command number, and the
+ * msb of the servo value. The second byte contains the lsb of the servo value.
+ *
+ * Commands 0 to NB_SERVO are to set the value of servo.
+ * Command 14 is to enable bypass mode.
+ * Command 15 is to disable bypass mode.
+ */
+register union {
+       uint8_t u8;
+       struct {
+               uint16_t zero:1;
+               uint16_t cmd_num:4;
+               uint16_t val_msb:3;
+       };
+} byte0 asm("r6");
+
+register union {
+       uint8_t u8;
+       struct {
+               uint8_t one:1;
+               uint8_t val_lsb:7;
+       };
+} byte1 asm("r7");
 
 SIGNAL(TIMER1_COMPA_vect)
 {
@@ -35,6 +63,46 @@ SIGNAL(TIMER1_COMPA_vect)
 
 static void poll_spi(void)
 {
+       uint8_t c;
+
+       /* reception complete ? */
+       if (!(SPSR & (1<<SPIF)))
+               return;
+
+       c = SPDR;
+       if ((rxidx == 0) && (c & 0x80)) {
+               rxidx = 0;
+               return; /* drop */
+       }
+       if ((rxidx == 1) && ((c & 0x80) == 0)) {
+               rxidx = 0;
+               return; /* drop */
+       }
+
+       if (rxidx == 0) {
+               byte0.u8 = c;
+       }
+       else {
+               uint16_t val;
+
+               byte1.u8 = c;
+
+               /* process command */
+
+               if (byte0.cmd_num < NB_SERVO) {
+                       val = (uint16_t)byte0.val_msb << 7;
+                       val += byte1.val_lsb;
+                       servo_table[byte0.cmd_num].command = val;
+               }
+               else if (byte0.cmd_num == 14) {
+                       bypass = 1;
+               }
+               else if (byte0.cmd_num == 15) {
+                       bypass = 0;
+               }
+       }
+
+       rxidx ^= 1;
 }
 
 static void load_timer_at(uint16_t t)
@@ -70,7 +138,10 @@ int main(void)
        uint8_t i;
        uint8_t t, diff;
 
+       /* LED */
        DDRB = 0x20;
+
+       /* servo outputs */
        DDRC = 0x7;
 
        /* start timer1 at clk/1 (8Mhz) */
@@ -81,11 +152,14 @@ int main(void)
        TCNT0 = 0;
        TCCR0B = _BV(CS02) | _BV(CS00);
 
+       /* enable spi (don't set unused MISO as output) */
+       SPCR = _BV(SPE);
+
        sei();
 
        while (1) {
                t = TCNT0;
-               for (i = 0; i < sizeof(servo_table)/sizeof(*servo_table); i++) {
+               for (i = 0; i < NB_SERVO; i++) {
                        do_one_servo(&servo_table[i]);
                }
                /* wait 20 ms */