2 * Copyright Droids Corporation (2009)
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * Revision : $Id: encoders_spi.c,v 1.1.2.3 2009-04-07 20:00:46 zer0 Exp $
20 * Olivier MATZ <zer0@droids-corp.org>
23 /* This modules handles encoders: values are read through a SPI
24 * interface. Basically, frames are formatted with 4 words of 16 bits,
25 * describing the values of the 4 encoders. */
32 #include <aversive/parts.h>
36 #include <encoders_spi.h>
37 #include <encoders_spi_config.h>
39 static int32_t g_encoders_spi_values[ENCODERS_SPI_NUMBER];
40 /* static */int16_t g_encoders_spi_previous[ENCODERS_SPI_NUMBER];
43 /* Initialisation of encoders, variables */
44 void encoders_spi_init(void)
46 memset(g_encoders_spi_previous, 0, sizeof(g_encoders_spi_previous));
47 spi_register_ss_line(&ENCODERS_SPI_SS_PORT, ENCODERS_SPI_SS_BIT);
48 spi_init(SPI_MODE_MASTER, ENCODERS_SPI_FORMAT, ENCODERS_SPI_CLK_RATE);
49 spi_set_data_order(ENCODERS_SPI_DATA_ORDER);
50 encoders_spi_manage(NULL);
51 memset(g_encoders_spi_values, 0, sizeof(g_encoders_spi_values));
55 /* Update encoders values */
56 void encoders_spi_manage(__attribute__((unused)) void *dummy)
70 for (i=0; i<ENCODERS_SPI_NUMBER; i++) {
71 enc.s.u8_lsb = spi_receive_byte();
72 enc.s.u8_msb = spi_receive_byte();
73 diff = enc.s16 - g_encoders_spi_previous[i];
74 g_encoders_spi_previous[i] = enc.s16;
76 g_encoders_spi_values[i] += diff;
79 spi_slave_deselect(0);
84 /* Extract encoder value */
85 int32_t encoders_spi_get_value(void *encoder)
91 value = g_encoders_spi_values[(int)encoder];
97 /* Set an encoder value */
98 void encoders_spi_set_value(void *encoder, int32_t val)
103 g_encoders_spi_values[(int)encoder] = val;