aboutsummaryrefslogtreecommitdiffstats
path: root/src/lpc176x
diff options
context:
space:
mode:
authorKevin O'Connor <kevin@koconnor.net>2019-06-19 16:17:40 -0400
committerKevin O'Connor <kevin@koconnor.net>2019-06-20 08:57:24 -0400
commit8951dc3d5131ec0c6fe3e33623d89be70eb28fb0 (patch)
treeff4f3008d3bc262688353c4c3c4808bd508b5a38 /src/lpc176x
parent8d029ad652f996e333ca08921cd7f92d9416c196 (diff)
downloadkutter-8951dc3d5131ec0c6fe3e33623d89be70eb28fb0.tar.gz
kutter-8951dc3d5131ec0c6fe3e33623d89be70eb28fb0.tar.xz
kutter-8951dc3d5131ec0c6fe3e33623d89be70eb28fb0.zip
lpc176x: Add more filtering to workaround adc hardware defect
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Diffstat (limited to 'src/lpc176x')
-rw-r--r--src/lpc176x/adc.c12
1 files changed, 11 insertions, 1 deletions
diff --git a/src/lpc176x/adc.c b/src/lpc176x/adc.c
index 26965fb2..4ea4e815 100644
--- a/src/lpc176x/adc.c
+++ b/src/lpc176x/adc.c
@@ -117,7 +117,8 @@ uint16_t
gpio_adc_read(struct gpio_adc g)
{
adc_status.chan |= ADC_DONE;
- // Perform median filter on 5 read samples
+ // The lpc176x adc has a defect that causes random reports near
+ // 0xfff. Work around that with a 5 sample median filter.
uint16_t *p = adc_status.samples;
uint32_t v0 = p[0], v4 = p[1], v1 = p[2], v3 = p[3], v2 = p[4];
ORDER(v0, v4);
@@ -127,6 +128,15 @@ gpio_adc_read(struct gpio_adc g)
ORDER(v1, v3);
ORDER(v1, v2);
ORDER(v2, v3);
+ if (v3 >= 0xff0 || v4 >= 0xff0) {
+ ORDER(v0, v1);
+ if (v2 >= 0xff0)
+ // At least 3 reports are clearly bogus - return the minimum sample
+ return v0;
+ // 1 or 2 bogus reports - return the median of the minimum 3 samples
+ return v1;
+ }
+ // Return the median of the 5 samples
return v2;
}