X-Git-Url: http://git.droids-corp.org/?a=blobdiff_plain;f=main.c;h=b1083ea8d3d1a559ad5fb1547c44b4ca844c5a04;hb=dd3c29607ed2b303f0fcc8dfd7e4bb9a55dae3ef;hp=a27bf10b821ae74236ec19e6ba77324ea936a304;hpb=2fe7b2747b73f2ac9ddf3630843da19937c3b570;p=protos%2Frc_servos.git diff --git a/main.c b/main.c index a27bf10..b1083ea 100644 --- a/main.c +++ b/main.c @@ -19,11 +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 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 { + uint8_t zero:1; + uint8_t cmd_num:4; + uint8_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) { @@ -34,6 +63,46 @@ SIGNAL(TIMER1_COMPA_vect) static void poll_spi(void) { + uint8_t c; + + /* reception complete ? */ + if (!(SPSR & (1<