From: Olivier MATZ Date: Thu, 23 Feb 2012 18:33:19 +0000 (+0100) Subject: rc_servos: support spi (not tested) X-Git-Url: http://git.droids-corp.org/?a=commitdiff_plain;h=face7352ff4e497c17590266455bda126f35d842;p=protos%2Frc_servos.git rc_servos: support spi (not tested) --- 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<