5 #include <aversive/wait.h>
13 * The goal of this code is to send the servo commands to the slave
14 * through the SPI bus. As the slave runs in polling mode to be
15 * precise when generating servo signals, we cannot send data very
16 * fast. We send one byte every ms, this is enough as we have at most
17 * 6 servos (2 bytes) to update every 20ms
19 * When a new servo value is received, we send the first byte to the
20 * SPI bus and store the next one. It will be transmitted by a
21 * callback 1ms later. If new servos values are received during this
22 * time, they are just saved but not transmitted until the first
23 * command is issued. Once all commands have been transmitted, the
24 * callback is unloaded.
28 #define SPI_EVT_PERIOD (10000UL/SCHEDULER_UNIT)
33 #define BYPASS_BIT 0x02
36 uint16_t servo[N_SERVO+1]; /* one more for control channel */
38 uint8_t next_byte; /* next byte to send, 0 if nothing in pipe */
41 static struct spi_servo_tx spi_servo_tx;
44 uint16_t servo[N_SERVO];
47 static struct spi_servo_rx spi_servo_rx;
52 * A command is stored on 2 bytes. The first one has its msb to 0, and the
53 * second one to 1. The first received byte contains the command number, and the
54 * msb of the servo value. The second byte contains the lsb of the servo value.
56 * Command 0 is only one byte long, it means "I have nothing to say".
57 * Commands 1 to N_SERVO (included) are to set the value of servo.
58 * Command N_SERVO+1 is:
59 * - to enable/disable ppm generation in place of last servo.
60 * - to enable/disable bypass mode
65 /* inverted: little endian */
75 /* inverted: little endian */
81 #define SS_HIGH() PORTB |= (1 << 4)
82 #define SS_LOW() PORTB &= (~(1 << 4))
84 static void spi_send_byte(uint8_t byte)
88 /* Wait for transmission complete (active loop is fine because
89 * the clock is high) */
90 while(!(SPSR & (1<<SPIF)));
94 static void spi_send_one_servo(uint8_t num, uint16_t val)
96 union spi_byte0 byte0;
97 union spi_byte1 byte1;
99 byte0.val_msb = val >> 7;
100 byte0.cmd_num = num + 1;
105 /* save the second byte */
106 spi_servo_tx.next_byte = byte1.u8;
108 /* send the first byte */
109 spi_send_byte(byte0.u8);
112 static void decode_rx_servo(union spi_byte0 byte0, union spi_byte1 byte1)
117 num = byte0.cmd_num - 1;
123 val |= byte1.val_lsb;
125 spi_servo_rx.servo[num] = val;
128 /* called by the scheduler */
129 static void spi_servo_cb(void *arg)
132 union spi_byte0 byte0;
133 union spi_byte1 byte1;
137 /* get the value from the slave */
140 if (byte0.zero == 0) {
141 spi_servo_rx.prev_byte = byte0.u8;
144 byte0.u8 = spi_servo_rx.prev_byte;
145 decode_rx_servo(byte0, byte1);
148 /* if next byte is set, send it */
149 if (spi_servo_tx.next_byte != 0) {
150 spi_send_byte(spi_servo_tx.next_byte);
151 spi_servo_tx.next_byte = 0;
155 /* if there is no updated servo, send 0 and return. */
156 if (spi_servo_tx.cmd_mask == 0) {
161 /* else find it and send it */
162 idx = spi_servo_tx.cur_idx;
165 if (idx > N_SERVO + 1)
168 if (spi_servo_tx.cmd_mask & (1 << (uint16_t)idx))
172 spi_send_one_servo(idx, spi_servo_tx.servo[idx]);
173 spi_servo_tx.cmd_mask &= (~(1 << idx));
174 spi_servo_tx.cur_idx = idx;
177 void spi_servo_init(void)
182 /* remove power reduction on spi */
183 PRR0 &= ~(1 << PRSPI);
185 /* Enable SPI, Master, set clock rate fck/64 */
186 SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1);
190 scheduler_add_periodical_event_priority(&spi_servo_cb, NULL,
191 SPI_EVT_PERIOD, SPI_PRIO);
195 void spi_servo_set(uint8_t num, uint16_t val)
203 spi_servo_tx.servo[num] = val;
204 spi_servo_tx.cmd_mask |= (1 << num);
208 uint16_t spi_servo_get(uint8_t num)
217 val = spi_servo_rx.servo[num];
223 void spi_servo_bypass(uint8_t enable)
228 spi_servo_tx.cmd_mask |= (1 << N_SERVO);
230 spi_servo_tx.servo[N_SERVO] |= BYPASS_BIT;
232 spi_servo_tx.servo[N_SERVO] &= (~BYPASS_BIT);
233 spi_servo_tx.cmd_mask |= (1 << N_SERVO);
237 void spi_servo_ppm(uint8_t enable)
242 spi_servo_tx.cmd_mask |= (1 << N_SERVO);
244 spi_servo_tx.servo[N_SERVO] |= PPM_BIT;
246 spi_servo_tx.servo[N_SERVO] &= (~PPM_BIT);
247 spi_servo_tx.cmd_mask |= (1 << N_SERVO);