00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include <string.h>
00028
00029 #include <aversive.h>
00030 #include <aversive/parts.h>
00031
00032 #include <spi.h>
00033
00034 #include <encoders_spi.h>
00035 #include <encoders_spi_config.h>
00036
00037 static int32_t g_encoders_spi_values[ENCODERS_SPI_NUMBER];
00038 static int16_t g_encoders_spi_previous[ENCODERS_SPI_NUMBER];
00039
00040
00041
00042 void encoders_spi_init(void)
00043 {
00044 memset(g_encoders_spi_values, 0, sizeof(g_encoders_spi_values));
00045 memset(g_encoders_spi_previous, 0, sizeof(g_encoders_spi_previous));
00046 spi_register_ss_line(&ENCODERS_SPI_SS_PORT, ENCODERS_SPI_SS_BIT);
00047 spi_init(SPI_MODE_MASTER, ENCODERS_SPI_FORMAT, ENCODERS_SPI_CLK_RATE);
00048 spi_set_data_order(ENCODERS_SPI_DATA_ORDER);
00049 }
00050
00051
00052
00053 void encoders_spi_manage(void *dummy)
00054 {
00055 union {
00056 struct {
00057 uint8_t u8_lsb;
00058 uint8_t u8_msb;
00059 } s;
00060 int16_t s16;
00061 } enc;
00062 uint8_t i;
00063 int16_t diff;
00064 uint8_t flags;
00065
00066 spi_slave_select(0);
00067 for (i=0; i<ENCODERS_SPI_NUMBER; i++) {
00068 enc.s.u8_lsb = spi_receive_byte();
00069 enc.s.u8_msb = spi_receive_byte();
00070 diff = enc.s16 - g_encoders_spi_previous[i];
00071 g_encoders_spi_previous[i] = enc.s16;
00072 IRQ_LOCK(flags);
00073 g_encoders_spi_values[i] += diff;
00074 IRQ_UNLOCK(flags);
00075 }
00076 spi_slave_deselect(0);
00077 }
00078
00079
00080
00081
00082 int32_t encoders_spi_get_value(void *encoder)
00083 {
00084 int32_t value;
00085 uint8_t flags;
00086
00087 IRQ_LOCK(flags);
00088 value = g_encoders_spi_values[(int)encoder];
00089 IRQ_UNLOCK(flags);
00090
00091 return value;
00092 }
00093
00094
00095 void encoders_spi_set_value(void *encoder, int32_t val)
00096 {
00097 uint8_t flags;
00098
00099 IRQ_LOCK(flags);
00100 g_encoders_spi_values[(int)encoder] = val;
00101 IRQ_UNLOCK(flags);
00102 }