2 #include <aversive/wait.h>
8 #define BYPASS_ENABLE 14
9 #define BYPASS_DISABLE 15
14 * A command is stored on 2 bytes. The first one has its msb to 0, and the
15 * second one to 1. The first received byte contains the command number, and the
16 * msb of the servo value. The second byte contains the lsb of the servo value.
18 * Commands 0 to NB_SERVO are to set the value of servo.
19 * Command 14 is to enable bypass mode.
20 * Command 15 is to disable bypass mode.
22 static volatile union {
25 /* inverted: little endian */
32 static volatile union {
35 /* inverted: little endian */
41 void spi_servo_init(void)
48 /* remove power reduction on spi */
49 PRR0 &= ~(1 << PRSPI);
51 /* Enable SPI, Master, set clock rate fck/64 */
52 SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1);
56 spi_servo_set(BYPASS_DISABLE, 0);
59 void spi_servo_set(uint8_t num, uint16_t val)
61 byte0.val_msb = val >> 7;
69 /* Wait for transmission complete */
70 while(!(SPSR & (1<<SPIF)));
77 /* Wait for transmission complete */
78 while(!(SPSR & (1<<SPIF)));
82 void spi_servo_bypass(uint8_t enable)
85 spi_servo_set(BYPASS_ENABLE, 0);
87 spi_servo_set(BYPASS_DISABLE, 0);