summaryrefslogtreecommitdiff
path: root/n/es-2006/src/sensor_rvb.c
diff options
context:
space:
mode:
Diffstat (limited to 'n/es-2006/src/sensor_rvb.c')
-rw-r--r--n/es-2006/src/sensor_rvb.c90
1 files changed, 56 insertions, 34 deletions
diff --git a/n/es-2006/src/sensor_rvb.c b/n/es-2006/src/sensor_rvb.c
index ac8b0aa..a354c53 100644
--- a/n/es-2006/src/sensor_rvb.c
+++ b/n/es-2006/src/sensor_rvb.c
@@ -23,26 +23,32 @@
*
* }}} */
#include "sensor_rvb.h"
+#include "es_config.h"
#include "io.h"
#include "modules/utils/utils.h" /* regv */
#include "modules/uart/uart.h"
+#include "modules/proto/proto.h"
/**
* Somes defines.
**/
-/** Color pins. */
+/** Colors selection pins. */
#define S1RVB 7
#define S0RVB 6
/** Max input capture before considering we can start the real capture. */
-#define RVB_MAX_FALSE_IC 3
+#define RVB_MAX_FALSE_IC 4
+/** Max overflow of the timer 1 before thinking the sensor is HS. */
+/* TODO Find a way to compute this value */
+#define RVB_MAX_OVERFLOW 250
/** Wait time between IO change in ns. */
#define RVB_SENSOR_DELAY_IO 125
-/** All the possible states */
+/** All the possible states. */
#define RVB_STATE_SLEEP 0 /* Doing nothing */
#define RVB_STATE_NEXT_SENSOR 1 /* Selecting a sensor */
#define RVB_STATE_WAIT_IC 20 /* Waiting for the first input capture */
-#define RVB_STATE_WAIT_IC2 (RVB_STATE_WAIT_IC + 1) /* 2nd input capture */
+#define RVB_STATE_WAIT_IC2 (RVB_STATE_WAIT_IC + 1) /* 2nd and more false
+ input captures */
#define RVB_STATE_START_CAPTURE 2 /* After waiting enough IC, starting capture */
#define RVB_STATE_STOP_CAPTURE 3 /* We can compute a value */
#define RVB_STATE_NEXT_COLOR 4 /* Selecting another color */
@@ -57,7 +63,7 @@ volatile uint8_t sensor_rvb_number_;
/** Color of the actual sensor. */
volatile uint8_t sensor_rvb_color_;
/** Number of overflow interruption since last edge change. */
-uint8_t sensor_rvb_overflow_count_;
+volatile uint8_t sensor_rvb_overflow_count_;
/** Results table for 9 RVB sensors (RVCB) */
volatile uint16_t sensor_rvb_values[RVB_MAX_SENSOR][4];
/** Geting stats only for one sensors and disabling computing the others. */
@@ -65,6 +71,9 @@ uint8_t sensor_rvb_enable;
/** Count the number of IC before starting a good capture. */
uint8_t sensor_rvb_ic_count_;
+/** Update everything for you. */
+void sensor_rvb_update (void);
+
/** Select a color :
* 0 : red ;
* 1 : blue ;
@@ -73,14 +82,22 @@ uint8_t sensor_rvb_ic_count_;
inline uint8_t
sensor_rvb_color_select (uint8_t sensor_rvb_color)
{
- /* Check */
- if (sensor_rvb_color > 3)
+ switch (sensor_rvb_color)
+ {
+ case RVB_INDEX_RED:
+ case RVB_INDEX_BLUE:
+ case RVB_INDEX_CLEAR:
+ case RVB_INDEX_GREEN:
+ /* Select the color */
+ PORTF = (PORTF & ~0xC0) | (sensor_rvb_color << 6);
+ /* Wait a little */
+ utils_delay_ns (RVB_SENSOR_DELAY_IO);
+ /* Color exists */
+ return 1;
+ default:
+ /* Error */
return 0;
- /* Select the color */
- PORTF = (PORTF & ~0xC0) | (sensor_rvb_color << 6);
- /* Wait a little */
- utils_delay_ns (RVB_SENSOR_DELAY_IO);
- return 1;
+ }
}
/** Enable a RVB sensor. */
@@ -117,9 +134,6 @@ sensor_rvb_sensor_select (uint8_t sensor_rvb_num)
return 1;
}
-/** Update everything for you. */
-void sensor_rvb_update (void);
-
/** Initialisation of the RVB sensors module. */
void
sensor_rvb_init (void)
@@ -133,14 +147,6 @@ sensor_rvb_init (void)
DDRD |= _BV(6);
/* Put color selector pins in output */
DDRF |= _BV(S0RVB) | _BV(S1RVB);
- /* Initialisation of counter :
- * Noise canceler, 1 for prescaling */
- TCCR1B |= _BV(ICNC1) | _BV(CS10);
- /* XXX Fast PWM 10-bit */
- TCCR1B |= _BV(WGM12);
- TCCR1A |= _BV(WGM11) | _BV(WGM10);
- /* XXX Overflow IR */
- TIMSK |= _BV (TOIE1);
/* Nothing to do for the moment */
sensor_rvb_state_ = RVB_STATE_SLEEP;
}
@@ -150,7 +156,7 @@ void
sensor_rvb_start_capture (void)
{
/* Are we already getting a sensor ? */
- if (!sensor_rvb_state_ && sensor_rvb_enable)
+ if ((sensor_rvb_state_ == RVB_STATE_SLEEP) && sensor_rvb_enable)
{
sensor_rvb_state_ = RVB_STATE_NEXT_SENSOR;
/* Start with sensor one */
@@ -165,6 +171,7 @@ void
sensor_rvb_watch (uint8_t sensor_num, uint8_t sensor_col)
{
// XXX Better approach.
+ return;
/* Disable all interrupt that could be enabled */
TIMSK &= ~_BV (TICIE1);
TIMSK &= ~_BV (TOIE1);
@@ -199,7 +206,7 @@ sensor_rvb_update (void)
else
{
sensor_rvb_state_ = RVB_STATE_WAIT_IC;
- /* Enable interrupt for IC1 and counter overflow */
+ /* Enable interrupt for IC1 */
TIMSK |= _BV (TICIE1);
}
}
@@ -210,30 +217,43 @@ sensor_rvb_update (void)
/* Select sensor */
if (!sensor_rvb_sensor_select (++sensor_rvb_number_))
{
- /* Disable IC interrupt */
+ /* Disable IC1 and TC1 overflow interrupts */
TIMSK &= ~_BV (TICIE1);
+ TIMSK &= ~_BV (TOIE1);
sensor_rvb_state_ = RVB_STATE_SLEEP;
return;
}
/* Start with first color */
sensor_rvb_color_select (sensor_rvb_color_ = 0);
sensor_rvb_state_ = RVB_STATE_WAIT_IC;
+ sensor_rvb_overflow_count_ = 0;
/* Enable interrupt for IC1 and counter overflow */
TIMSK |= _BV (TICIE1);
+ TIMSK |= _BV (TOIE1);
}
}
/** Timer 1 overflow. */
SIGNAL (SIG_OVERFLOW1)
{
- if ((sensor_rvb_state_ == RVB_STATE_STOP_CAPTURE) &&
- (++sensor_rvb_overflow_count_ == 4))
+ switch (sensor_rvb_state_)
{
- /* Disable IC interrupt */
- TIMSK &= ~_BV (TICIE1);
- /* Ask for next sensor */
- sensor_rvb_state_ = RVB_STATE_NEXT_SENSOR;
- sensor_rvb_update ();
+ case RVB_STATE_START_CAPTURE:
+ ++sensor_rvb_overflow_count_;
+ break;
+ case RVB_STATE_WAIT_IC:
+ case RVB_STATE_WAIT_IC2:
+ // XXX >= Sucks !
+ // TODO : check this >= and the IC2 used
+ if (++sensor_rvb_overflow_count_ >= RVB_MAX_OVERFLOW)
+ {
+ /* Disable IC interrupt */
+ TIMSK &= ~_BV (TICIE1);
+ /* Ask for next sensor */
+ sensor_rvb_state_ = RVB_STATE_NEXT_SENSOR;
+ sensor_rvb_update ();
+ }
+ break;
}
}
@@ -251,15 +271,17 @@ SIGNAL (SIG_INPUT_CAPTURE1)
sensor_rvb_state_ = RVB_STATE_START_CAPTURE;
break;
case RVB_STATE_START_CAPTURE:
+ sensor_rvb_overflow_count_ = 0;
/* Save capture start */
sensor_rvb_value_ = ICR1;
/* New capture */
- sensor_rvb_overflow_count_ = 0;
sensor_rvb_state_ = RVB_STATE_STOP_CAPTURE;
break;
case RVB_STATE_STOP_CAPTURE:
/* Disable IC interrupt */
TIMSK &= ~_BV (TICIE1);
+ /* Ensure we have no pending interrupt for IC1. */
+ TIFR = _BV (ICF1);
/* Compute value */
sensor_rvb_value_ = ICR1 + sensor_rvb_overflow_count_ * TC1_TOP -
sensor_rvb_value_;