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 BYPASS_BIT 0x02
31 uint16_t servo[N_SERVO+1]; /* one more for control channel */
33 uint8_t next_byte; /* next byte to send, 0 if nothing in pipe */
36 static struct spi_servo_tx spi_servo_tx;
39 uint16_t servo[N_SERVO];
42 static struct spi_servo_rx spi_servo_rx;
47 * A command is stored on 2 bytes. The first one has its msb to 0, and the
48 * second one to 1. The first received byte contains the command number, and the
49 * msb of the servo value. The second byte contains the lsb of the servo value.
51 * Command 0 is only one byte long, it means "I have nothing to say".
52 * Commands 1 to N_SERVO (included) are to set the value of servo.
53 * Command N_SERVO+1 is:
54 * - to enable/disable ppm generation in place of last servo.
55 * - to enable/disable bypass mode
60 /* inverted: little endian */
70 /* inverted: little endian */
76 #define SS_HIGH() PORTB |= (1 << 4)
77 #define SS_LOW() PORTB &= (~(1 << 4))
79 static void spi_send_byte(uint8_t byte)
83 /* Wait for transmission complete (active loop is fine because
84 * the clock is high) */
85 while(!(SPSR & (1<<SPIF)));
89 static void spi_send_one_servo(uint8_t num, uint16_t val)
91 union spi_byte0 byte0;
92 union spi_byte1 byte1;
94 byte0.val_msb = val >> 7;
95 byte0.cmd_num = num + 1;
100 /* save the second byte */
101 spi_servo_tx.next_byte = byte1.u8;
103 /* send the first byte */
104 spi_send_byte(byte0.u8);
107 static void decode_rx_servo(union spi_byte0 byte0, union spi_byte1 byte1)
112 num = byte0.cmd_num - 1;
118 val |= byte1.val_lsb;
120 spi_servo_rx.servo[num] = val;
123 /* called by the scheduler */
124 static void spi_servo_cb(struct callout_mgr *cm, struct callout *tim, void *arg)
127 union spi_byte0 byte0;
128 union spi_byte1 byte1;
132 /* get the value from the slave */
135 if (byte0.zero == 0) {
136 spi_servo_rx.prev_byte = byte0.u8;
139 byte0.u8 = spi_servo_rx.prev_byte;
140 decode_rx_servo(byte0, byte1);
143 /* if next byte is set, send it */
144 if (spi_servo_tx.next_byte != 0) {
145 spi_send_byte(spi_servo_tx.next_byte);
146 spi_servo_tx.next_byte = 0;
150 /* if there is no updated servo, send 0 and return. */
151 if (spi_servo_tx.cmd_mask == 0) {
156 /* else find it and send it */
157 idx = spi_servo_tx.cur_idx;
160 if (idx > N_SERVO + 1)
163 if (spi_servo_tx.cmd_mask & (1 << (uint16_t)idx))
167 spi_send_one_servo(idx, spi_servo_tx.servo[idx]);
168 spi_servo_tx.cmd_mask &= (~(1 << idx));
169 spi_servo_tx.cur_idx = idx;
172 /* don't use callout_reschedule() here, we want to schedule in one tick
173 * relative to current time: 1 tick is 682us at 12Mhz */
174 callout_schedule(cm, tim, 0);
177 void spi_servo_init(void)
182 /* remove power reduction on spi */
183 PRR0 &= ~(1 << PRSPI);
185 /* Enable SPI, Master, set clock rate fck/16 */
186 SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0);
190 callout_init(&xbeeboard.spi_timer, spi_servo_cb, NULL, SPI_PRIO);
191 callout_schedule(&xbeeboard.intr_cm,
192 &xbeeboard.spi_timer, 0); /* immediate */
193 spi_servo_set_bypass(1);
196 void spi_servo_set(uint8_t num, uint16_t val)
204 spi_servo_tx.servo[num] = val;
205 spi_servo_tx.cmd_mask |= (1 << num);
209 uint16_t spi_servo_get(uint8_t num)
218 val = spi_servo_rx.servo[num];
224 uint8_t spi_servo_get_bypass(void)
226 return !!(spi_servo_tx.servo[N_SERVO] & BYPASS_BIT);
229 uint8_t spi_servo_get_ppm(void)
231 return !!(spi_servo_tx.servo[N_SERVO] & PPM_BIT);
234 void spi_servo_set_bypass(uint8_t enable)
239 spi_servo_tx.cmd_mask |= (1 << N_SERVO);
241 spi_servo_tx.servo[N_SERVO] |= BYPASS_BIT;
243 spi_servo_tx.servo[N_SERVO] &= (~BYPASS_BIT);
244 spi_servo_tx.cmd_mask |= (1 << N_SERVO);
248 void spi_servo_set_ppm(uint8_t enable)
253 spi_servo_tx.cmd_mask |= (1 << N_SERVO);
255 spi_servo_tx.servo[N_SERVO] |= PPM_BIT;
257 spi_servo_tx.servo[N_SERVO] &= (~PPM_BIT);
258 spi_servo_tx.cmd_mask |= (1 << N_SERVO);
262 void spi_servo_dump(void)
266 for (i = 0; i < N_SERVO; i++)
267 printf_P(PSTR("%d: rx=%4.4d tx=%4.4d\r\n"), i,
268 spi_servo_get(i), spi_servo_tx.servo[i]);
269 printf_P(PSTR("bypass=%d ppm=%d\n"),
270 spi_servo_get_bypass(), spi_servo_get_ppm());