-
-static volatile uint8_t rxbuf[16];
-register uint8_t rxlen asm("r2");
-register uint8_t done asm("r3");
-register uint8_t portval asm("r4");
+#define NB_SERVO (sizeof(servo_table)/sizeof(*servo_table))
+
+static uint8_t bypass;
+static volatile uint8_t done;
+static uint8_t portval;
+static uint8_t rxidx;
+
+#define BYPASS_ENABLE 14
+#define BYPASS_DISABLE 15
+
+#define LED_ON() do { PORTB |= 0x02; } while(0)
+#define LED_OFF() do { PORTB &= ~0x02; } while(0)
+
+/*
+ * 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.
+ */
+static union {
+ uint8_t u8;
+ struct {
+ /* inverted: little endian */
+ uint8_t val_msb:3;
+ uint8_t cmd_num:4;
+ uint8_t zero:1;
+ };
+} byte0;
+
+static union {
+ uint8_t u8;
+ struct {
+ /* inverted: little endian */
+ uint8_t val_lsb:7;
+ uint8_t one:1;
+ };
+} byte1;