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 #define SS_HIGH() PORTB |= (1 << 4)
42 #define SS_LOW() PORTB &= (~(1 << 4))
44 void spi_servo_init(void)
49 /* remove power reduction on spi */
50 PRR0 &= ~(1 << PRSPI);
52 /* Enable SPI, Master, set clock rate fck/64 */
53 SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1);
57 spi_servo_set(BYPASS_DISABLE, 0);
60 void spi_servo_set(uint8_t num, uint16_t val)
62 byte0.val_msb = val >> 7;
70 /* Wait for transmission complete */
71 while(!(SPSR & (1<<SPIF)));
78 /* Wait for transmission complete */
79 while(!(SPSR & (1<<SPIF)));
83 void spi_servo_bypass(uint8_t enable)
86 spi_servo_set(BYPASS_ENABLE, 0);
88 spi_servo_set(BYPASS_DISABLE, 0);