2 * Copyright Droids Corporation, Microb Technology, Eirbot (2005)
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_eirbot.c,v 1.4.4.3 2007-05-23 17:18:13 zer0 Exp $
22 /** \file encoders_eirbot.c
23 * \brief achieves acess to incremental encoderss through a xilinx interface
29 * this modules reads 8 bit encoders values on an external interface.
30 * there are two necessary busses with the interface logic: 8 bit data bus and a n bits selection bus.
31 * ( see the VDHL for the xilinx program)
32 * with n bits on the selection bus, you can acess to 2^n encoderss.
34 * modifié par lamygale le 10 octobre 2005. d'apres le code 2004 du codeur
43 #include "encoders_eirbot.h"
45 #include "encoders_eirbot_config.h"
47 #ifdef ENCODERS_MODE_XILINX
48 // mask for selection bits
49 #define ENCODERS_SELEC_MASK ((0xFF >> (8-ENCODERS_SELEC_NITS_NUM)) \
50 << ENCODERS_SELEC_BIT0)
51 #define ENCODERS_DATA_MASK ((0xFF >> (8-ENCODERS_DATA_NBBITS)) \
52 << ENCODERS_DATA_BIT0)
53 // bits qui ne sont pas des donnees sont a 1 (donnees sont alignees a droite)
54 #define DIFF_DATA_MASK ((0xFF >> ENCODERS_DATA_NBBITS) << ENCODERS_DATA_NBBITS)
56 volatile encoders g_codeur_valeurs[ENCODERS_NUMBER];
58 volatile int8_t g_codeur_preced[ENCODERS_NUMBER];
63 * fonction utilisée 2 fois seulement, acquiert un codeur
65 inline int8_t get_codeur(uint8_t num)
72 ENCODERS_SELEC_PORT = ( ENCODERS_SELEC_PORT & (~ENCODERS_SELEC_MASK) ) | ( num << ENCODERS_SELEC_BIT0);
74 nop(); // delay for IO & xilix delay time
77 val = (ENCODERS_PIN & ENCODERS_DATA_MASK) >> ENCODERS_DATA_BIT0;
84 /** Initialisation des codeurs, variables, et ports */
85 void encoders_init(void)
92 ENCODERS_SELEC_DDR |= ENCODERS_SELEC_MASK; // port de selection en sortie
93 ENCODERS_DATA_DDR &= (~ENCODERS_DATA_MASK); // port de donnees en entree
95 for(number = 0 ; number < ENCODERS_NUMBER; number ++)
97 g_codeur_valeurs[number] = 0;
98 g_codeur_preced[number] = get_codeur(number);
104 /** mise a jour des valeurs des codeurs, a faire réguliereement minimum a la fréquence (Fmax_codeur/127) */
105 void encoders_manage(void)
109 uint8_t nombre_a_ajouter; // pour gerer les nombres négatifs
112 for(number = 0 ; number < ENCODERS_NUMBER; number ++)
115 lu = get_codeur(number); // lecture
116 res = lu - g_codeur_preced[number]; // calcul diff
117 g_codeur_preced[number] = lu; // mise a jour ancienne valeur
119 // on recopie l'etat du MSb dans les bits != donnees
120 nombre_a_ajouter = ((res >> (ENCODERS_DATA_NBBITS - 1))&1) ? DIFF_DATA_MASK : 0; // si MSb = 1, on prend mask
121 res = (res & ~DIFF_DATA_MASK) + nombre_a_ajouter; // bits de poids faibles = donnees, les autres = signe
124 g_codeur_valeurs[number] += res;
129 /** Extraction d'une valeur de codeur */
130 encoders encoders_get_value(uint8_t number)
136 value = g_codeur_valeurs[number];
143 #error the irq version of the codeur module is not implemented yet