From 7d177bb3e021f4944f3675dc8bc2cf103b4b0d62 Mon Sep 17 00:00:00 2001 From: Nicolas Schodet Date: Thu, 6 Mar 2008 22:04:45 +0100 Subject: * digital/asserv/src/asserv: - added external memory usage for counter reading. - added aux0 counter. - counter cleanups. --- digital/asserv/src/asserv/counter_ext.avr.c | 113 ++++++++++++++++++++-------- digital/asserv/src/asserv/counter_tcc.avr.c | 51 ++++--------- digital/asserv/src/asserv/simu.host.c | 6 -- digital/asserv/src/asserv/simu.host.h | 4 - 4 files changed, 94 insertions(+), 80 deletions(-) diff --git a/digital/asserv/src/asserv/counter_ext.avr.c b/digital/asserv/src/asserv/counter_ext.avr.c index c717fdc6..9a6ddec8 100644 --- a/digital/asserv/src/asserv/counter_ext.avr.c +++ b/digital/asserv/src/asserv/counter_ext.avr.c @@ -25,71 +25,118 @@ /** * This file add support for an external counter like the hdlcounter or - * avrcounter project. This can be better in order not to loose steps. + * avrcounter project. This can be better in order not to loose steps and + * support more counters. */ +/** Define the left counter address. */ +#define COUNTER_LEFT 0 +/** Define the right counter address. */ +#define COUNTER_RIGHT 1 +/** Define the first auxiliary counter address. */ +#define COUNTER_AUX0 2 + +/** Define to 1 to reverse the left counter. */ +#define COUNTER_LEFT_REVERSE 0 +/** Define to 1 to reverse the right counter. */ +#define COUNTER_RIGHT_REVERSE 0 +/** Define to 1 to reverse the first auxiliary counter. */ +#define COUNTER_AUX0_REVERSE 0 + +/** Define to 1 to use the AVR External Memory system, or 0 to use hand made + * signals. */ +#define COUNTER_USE_XMEM 1 + /** Last values. */ -static uint8_t counter_left_old, counter_right_old; +static uint8_t counter_left_old, counter_right_old, counter_aux0_old; /** Overall counter values. */ -static uint16_t counter_left, counter_right; +static uint16_t counter_left, counter_right, counter_aux0; /** Counter differences since last update. * Maximum of 7 significant bits, sign included. */ -static int16_t counter_left_diff, counter_right_diff; - -#define COUNTER_ALE _BV (2) -#define COUNTER_RD _BV (1) -#define COUNTER_WR _BV (0) +static int16_t counter_left_diff, counter_right_diff, counter_aux0_diff; -static inline void -counter_restart (void); - -/** Initialize the counters. */ -static inline void -counter_init (void) -{ - PORTG |= COUNTER_ALE | COUNTER_RD | COUNTER_WR; - DDRG |= COUNTER_ALE | COUNTER_RD | COUNTER_WR; - PORTA = 0; - DDRA = 0xff; - /* Begin with safe values. */ - counter_restart (); -} +#if !COUNTER_USE_XMEM +# define COUNTER_ALE _BV (2) +# define COUNTER_RD _BV (1) +# define COUNTER_WR _BV (0) +#endif /** Read an external counter. */ static inline uint8_t counter_read (uint8_t n) { +#if COUNTER_USE_XMEM + uint8_t * const ext = (void *) 0x1100; + return ext[n]; +#else uint8_t v; PORTA = n; PORTG &= ~COUNTER_ALE; PORTA = 0; DDRA = 0; PORTG &= ~COUNTER_RD; + utils_nop (); + utils_nop (); v = PINA; PORTG |= COUNTER_RD; PORTG |= COUNTER_ALE; DDRA = 0xff; return v; +#endif +} + +/** Initialize the counters. */ +static inline void +counter_init (void) +{ +#if COUNTER_USE_XMEM + /* Long wait-states. */ + XMCRA = _BV (SRW11); + /* Do not use port C for address. */ + XMCRB = _BV (XMM2) | _BV (XMM1) | _BV (XMM0); + /* Long wait-states and enable. */ + MCUCR |= _BV (SRE) | _BV (SRW10); +#else + PORTG |= COUNTER_ALE | COUNTER_RD | COUNTER_WR; + DDRG |= COUNTER_ALE | COUNTER_RD | COUNTER_WR; + PORTA = 0; + DDRA = 0xff; +#endif + /* Begin with safe values. */ + counter_left_old = counter_read (COUNTER_LEFT); + counter_right_old = counter_read (COUNTER_RIGHT); + counter_aux0_old = counter_read (COUNTER_AUX0); } /** Update overall counter values and compute diffs. */ static inline void counter_update (void) { - uint8_t left, right; - left = counter_read (0); + uint8_t left, right, aux0; + /* Sample counters. */ + left = counter_read (COUNTER_LEFT); + right = counter_read (COUNTER_RIGHT); + aux0 = counter_read (COUNTER_AUX0); + /* Left counter. */ +#if !COUNTER_LEFT_REVERSE counter_left_diff = (int8_t) (left - counter_left_old); +#else + counter_left_diff = (int8_t) (counter_left_old - left); +#endif counter_left += counter_left_diff; - right = counter_read (1); + /* Right counter. */ +#if !COUNTER_RIGHT_REVERSE counter_right_diff = (int8_t) (right - counter_right_old); +#else + counter_right_diff = (int8_t) (counter_right_old - right); +#endif counter_right += counter_right_diff; -} - -/** Restart counting. */ -static inline void -counter_restart (void) -{ - counter_left_old = counter_read (0); - counter_right_old = counter_read (1); + /* First auxiliary counter. */ +#if !COUNTER_AUX0_REVERSE + counter_aux0_diff = (int8_t) (aux0 - counter_aux0_old); +#else + counter_aux0_diff = (int8_t) (counter_aux0_old - aux0); +#endif + counter_aux0 += counter_aux0_diff; } diff --git a/digital/asserv/src/asserv/counter_tcc.avr.c b/digital/asserv/src/asserv/counter_tcc.avr.c index 5ce4058c..834147b9 100644 --- a/digital/asserv/src/asserv/counter_tcc.avr.c +++ b/digital/asserv/src/asserv/counter_tcc.avr.c @@ -30,9 +30,9 @@ */ /** Define to 1 to reverse the left counter. */ -#define COUNTER_REVERSE_LEFT 0 +#define COUNTER_LEFT_REVERSE 0 /** Define to 1 to reverse the right counter. */ -#define COUNTER_REVERSE_RIGHT 0 +#define COUNTER_RIGHT_REVERSE 0 /** Forward and reverse counter values. */ static uint8_t counter_left_frw, counter_left_rev, @@ -45,22 +45,6 @@ static uint16_t counter_left, counter_right; * Maximum of 9 significant bits, sign included. */ static int16_t counter_left_diff, counter_right_diff; -/* +AutoDec */ - -/** Initialize the counters. */ -static inline void -counter_init (void); - -/** Update overall counter values and compute diffs. */ -static inline void -counter_update (void); - -/** Restart counting. */ -static inline void -counter_restart (void); - -/* -AutoDec */ - /** Initialize the counters. */ static inline void counter_init (void) @@ -76,7 +60,12 @@ counter_init (void) TCCR3B = regv (ICNC3, ICES3, 5, WGM33, WGM32, CS32, CS31, CS30, 0, 0, 0, 0, 0, 1, 1, 1); /* Begin with safe values. */ - counter_restart (); + counter_left_frw = 0; + counter_left_rev = 0; + counter_left_old = TCNT2; + counter_right_frw = 0; + counter_right_rev = 0; + counter_right_old = TCNT3L; /* Interrupts for direction. */ EICRB = 0x05; EIFR = _BV (4) | _BV (5); @@ -91,12 +80,12 @@ SIGNAL (SIG_INTERRUPT4) if (PINE & _BV (4)) { counter_left_rev += c - counter_left_old; - GREEN_LED (!COUNTER_REVERSE_LEFT); + LED1 (!COUNTER_LEFT_REVERSE); } else { counter_left_frw += c - counter_left_old; - GREEN_LED (COUNTER_REVERSE_LEFT); + LED1 (COUNTER_LEFT_REVERSE); } counter_left_old = c; } @@ -109,12 +98,12 @@ SIGNAL (SIG_INTERRUPT5) if (PINE & _BV (5)) { counter_right_rev += c - counter_right_old; - RED_LED (!COUNTER_REVERSE_RIGHT); + LED2 (!COUNTER_RIGHT_REVERSE); } else { counter_right_frw += c - counter_right_old; - RED_LED (COUNTER_REVERSE_RIGHT); + LED2 (COUNTER_RIGHT_REVERSE); } counter_right_old = c; } @@ -141,7 +130,7 @@ counter_update (void) counter_right_rev += c - counter_right_old; counter_right_old = c; /* Update counter values and diffs. */ -#if COUNTER_REVERSE_LEFT == 0 +#if COUNTER_LEFT_REVERSE == 0 counter_left_diff = counter_left_frw; counter_left_diff -= counter_left_rev; #else @@ -151,7 +140,7 @@ counter_update (void) counter_left_frw = 0; counter_left_rev = 0; counter_left += counter_left_diff; -#if COUNTER_REVERSE_RIGHT == 0 +#if COUNTER_RIGHT_REVERSE == 0 counter_right_diff = counter_right_frw; counter_right_diff -= counter_right_rev; #else @@ -165,15 +154,3 @@ counter_update (void) EIMSK |= _BV (4) | _BV (5); } -/** Restart counting. */ -static inline void -counter_restart (void) -{ - counter_left_frw = 0; - counter_left_rev = 0; - counter_left_old = TCNT2; - counter_right_frw = 0; - counter_right_rev = 0; - counter_right_old = TCNT3L; -} - diff --git a/digital/asserv/src/asserv/simu.host.c b/digital/asserv/src/asserv/simu.host.c index 3e6f0412..2041d67a 100644 --- a/digital/asserv/src/asserv/simu.host.c +++ b/digital/asserv/src/asserv/simu.host.c @@ -165,12 +165,6 @@ counter_update (void) { } -/** Restart counting. */ -void -counter_restart (void) -{ -} - /** Initialise PWM generator. */ void pwm_init (void) diff --git a/digital/asserv/src/asserv/simu.host.h b/digital/asserv/src/asserv/simu.host.h index 69a120ac..45778bd6 100644 --- a/digital/asserv/src/asserv/simu.host.h +++ b/digital/asserv/src/asserv/simu.host.h @@ -77,10 +77,6 @@ counter_init (void); void counter_update (void); -/** Restart counting. */ -void -counter_restart (void); - /** Initialise PWM generator. */ void pwm_init (void); -- cgit v1.2.3