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.
27 /* 1 scheduler unit (682us at 12Mhz) */
28 #define SPI_EVT_PERIOD (1)
31 #define BYPASS_BIT 0x02
34 uint16_t servo[N_SERVO+1]; /* one more for control channel */
36 uint8_t next_byte; /* next byte to send, 0 if nothing in pipe */
39 static struct spi_servo_tx spi_servo_tx;
42 uint16_t servo[N_SERVO];
45 static struct spi_servo_rx spi_servo_rx;
50 * A command is stored on 2 bytes. The first one has its msb to 0, and the
51 * second one to 1. The first received byte contains the command number, and the
52 * msb of the servo value. The second byte contains the lsb of the servo value.
54 * Command 0 is only one byte long, it means "I have nothing to say".
55 * Commands 1 to N_SERVO (included) are to set the value of servo.
56 * Command N_SERVO+1 is:
57 * - to enable/disable ppm generation in place of last servo.
58 * - to enable/disable bypass mode
63 /* inverted: little endian */
73 /* inverted: little endian */
79 #define SS_HIGH() PORTB |= (1 << 4)
80 #define SS_LOW() PORTB &= (~(1 << 4))
82 static void spi_send_byte(uint8_t byte)
86 /* Wait for transmission complete (active loop is fine because
87 * the clock is high) */
88 while(!(SPSR & (1<<SPIF)));
92 static void spi_send_one_servo(uint8_t num, uint16_t val)
94 union spi_byte0 byte0;
95 union spi_byte1 byte1;
97 byte0.val_msb = val >> 7;
98 byte0.cmd_num = num + 1;
103 /* save the second byte */
104 spi_servo_tx.next_byte = byte1.u8;
106 /* send the first byte */
107 spi_send_byte(byte0.u8);
110 static void decode_rx_servo(union spi_byte0 byte0, union spi_byte1 byte1)
115 num = byte0.cmd_num - 1;
121 val |= byte1.val_lsb;
123 spi_servo_rx.servo[num] = val;
126 /* called by the scheduler */
127 static void spi_servo_cb(void *arg)
130 union spi_byte0 byte0;
131 union spi_byte1 byte1;
135 /* get the value from the slave */
138 if (byte0.zero == 0) {
139 spi_servo_rx.prev_byte = byte0.u8;
142 byte0.u8 = spi_servo_rx.prev_byte;
143 decode_rx_servo(byte0, byte1);
146 /* if next byte is set, send it */
147 if (spi_servo_tx.next_byte != 0) {
148 spi_send_byte(spi_servo_tx.next_byte);
149 spi_servo_tx.next_byte = 0;
153 /* if there is no updated servo, send 0 and return. */
154 if (spi_servo_tx.cmd_mask == 0) {
159 /* else find it and send it */
160 idx = spi_servo_tx.cur_idx;
163 if (idx > N_SERVO + 1)
166 if (spi_servo_tx.cmd_mask & (1 << (uint16_t)idx))
170 spi_send_one_servo(idx, spi_servo_tx.servo[idx]);
171 spi_servo_tx.cmd_mask &= (~(1 << idx));
172 spi_servo_tx.cur_idx = idx;
175 void spi_servo_init(void)
180 /* remove power reduction on spi */
181 PRR0 &= ~(1 << PRSPI);
183 /* Enable SPI, Master, set clock rate fck/64 */
184 SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1);
188 scheduler_add_periodical_event_priority(&spi_servo_cb, NULL,
189 SPI_EVT_PERIOD, SPI_PRIO);
190 spi_servo_set_bypass(1);
193 void spi_servo_set(uint8_t num, uint16_t val)
201 spi_servo_tx.servo[num] = val;
202 spi_servo_tx.cmd_mask |= (1 << num);
206 uint16_t spi_servo_get(uint8_t num)
215 val = spi_servo_rx.servo[num];
221 uint8_t spi_servo_get_bypass(void)
223 return !!(spi_servo_tx.servo[N_SERVO] & BYPASS_BIT);
226 uint8_t spi_servo_get_ppm(void)
228 return !!(spi_servo_tx.servo[N_SERVO] & PPM_BIT);
231 void spi_servo_set_bypass(uint8_t enable)
236 spi_servo_tx.cmd_mask |= (1 << N_SERVO);
238 spi_servo_tx.servo[N_SERVO] |= BYPASS_BIT;
240 spi_servo_tx.servo[N_SERVO] &= (~BYPASS_BIT);
241 spi_servo_tx.cmd_mask |= (1 << N_SERVO);
245 void spi_servo_set_ppm(uint8_t enable)
250 spi_servo_tx.cmd_mask |= (1 << N_SERVO);
252 spi_servo_tx.servo[N_SERVO] |= PPM_BIT;
254 spi_servo_tx.servo[N_SERVO] &= (~PPM_BIT);
255 spi_servo_tx.cmd_mask |= (1 << N_SERVO);
259 void spi_servo_dump(void)
263 for (i = 0; i < N_SERVO; i++)
264 printf_P(PSTR("%d: rx=%4.4d tx=%4.4d\r\n"), i,
265 spi_servo_get(i), spi_servo_tx.servo[i]);
266 printf_P(PSTR("bypass=%d ppm=%d\n"),
267 spi_servo_get_bypass(), spi_servo_get_ppm());