2 #include <aversive/wait.h>
7 #define BYPASS_ENABLE 14
8 #define BYPASS_DISABLE 15
13 * A command is stored on 2 bytes. The first one has its msb to 0, and the
14 * second one to 1. The first received byte contains the command number, and the
15 * msb of the servo value. The second byte contains the lsb of the servo value.
17 * Commands 0 to NB_SERVO are to set the value of servo.
18 * Command 14 is to enable bypass mode.
19 * Command 15 is to disable bypass mode.
21 static volatile union {
24 /* inverted: little endian */
31 static volatile union {
34 /* inverted: little endian */
40 void spi_servo_init(void)
47 /* remove power reduction on spi */
48 PRR0 &= ~(1 << PRSPI);
50 /* Enable SPI, Master, set clock rate fck/64 */
51 SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1);
55 spi_servo_set(BYPASS_DISABLE, 0);
58 void spi_servo_set(uint8_t num, uint16_t val)
60 byte0.val_msb = val >> 7;
68 /* Wait for transmission complete */
69 while(!(SPSR & (1<<SPIF)));
76 /* Wait for transmission complete */
77 while(!(SPSR & (1<<SPIF)));
81 void spi_servo_bypass(uint8_t enable)
84 spi_servo_set(BYPASS_ENABLE, 0);
86 spi_servo_set(BYPASS_DISABLE, 0);