From face7352ff4e497c17590266455bda126f35d842 Mon Sep 17 00:00:00 2001 From: Olivier MATZ Date: Thu, 23 Feb 2012 19:33:19 +0100 Subject: [PATCH] rc_servos: support spi (not tested) --- main.c | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 78 insertions(+), 4 deletions(-) diff --git a/main.c b/main.c index 53fd747..657e54d 100644 --- 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<