aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorKevin O'Connor <kevin@koconnor.net>2017-04-11 09:56:08 -0400
committerKevin O'Connor <kevin@koconnor.net>2017-04-11 10:01:22 -0400
commit7a7b98cc31dff8cb7f1a6a1ab886362a0802be66 (patch)
tree00cf5308e651d7f4b980408b3a0cc147376f04dc /src
parent15d5837322db5298f29d0d2ba92416fd5a40cc8c (diff)
downloadkutter-7a7b98cc31dff8cb7f1a6a1ab886362a0802be66.tar.gz
kutter-7a7b98cc31dff8cb7f1a6a1ab886362a0802be66.tar.xz
kutter-7a7b98cc31dff8cb7f1a6a1ab886362a0802be66.zip
sam3x8e: Rework adc pin search to be more clear
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Diffstat (limited to 'src')
-rw-r--r--src/sam3x8e/gpio.c38
1 files changed, 22 insertions, 16 deletions
diff --git a/src/sam3x8e/gpio.c b/src/sam3x8e/gpio.c
index f2e10069..d20f3f77 100644
--- a/src/sam3x8e/gpio.c
+++ b/src/sam3x8e/gpio.c
@@ -1,6 +1,6 @@
// GPIO functions on sam3x8e
//
-// Copyright (C) 2016 Kevin O'Connor <kevin@koconnor.net>
+// Copyright (C) 2016,2017 Kevin O'Connor <kevin@koconnor.net>
//
// This file may be distributed under the terms of the GNU GPLv3 license.
@@ -28,7 +28,7 @@ static Pio * const digital_regs[] = {
/****************************************************************
- * gpio functions
+ * General Purpose Input Output (GPIO) pins
****************************************************************/
void
@@ -116,6 +116,10 @@ gpio_in_read(struct gpio_in g)
}
+/****************************************************************
+ * Analog to Digital Converter (ADC) pins
+ ****************************************************************/
+
static const uint8_t adc_pins[] = {
GPIO('A', 2), GPIO('A', 3), GPIO('A', 4), GPIO('A', 6),
GPIO('A', 22), GPIO('A', 23), GPIO('A', 24), GPIO('A', 16),
@@ -129,22 +133,24 @@ DECL_CONSTANT(ADC_MAX, 4096);
struct gpio_adc
gpio_adc_setup(uint8_t pin)
{
+ // Find pin in adc_pins table
int chan;
- for (chan=0; chan<ARRAY_SIZE(adc_pins); chan++) {
- if (adc_pins[chan] != pin)
- continue;
- // Found PIN
- if (!(PMC->PMC_PCSR1 & (1 << (ID_ADC-32)))) {
- // Setup ADC
- PMC->PMC_PCER1 = 1 << (ID_ADC-32);
- uint32_t prescal = SystemCoreClock / (2 * ADC_FREQ_MAX) - 1;
- ADC->ADC_MR = (ADC_MR_PRESCAL(prescal)
- | ADC_MR_STARTUP_SUT768
- | ADC_MR_TRANSFER(1));
- }
- return (struct gpio_adc){ .bit = 1 << chan };
+ for (chan=0; ; chan++) {
+ if (chan >= ARRAY_SIZE(adc_pins))
+ shutdown("Not a valid ADC pin");
+ if (adc_pins[chan] == pin)
+ break;
+ }
+
+ if (!(PMC->PMC_PCSR1 & (1 << (ID_ADC-32)))) {
+ // Setup ADC
+ PMC->PMC_PCER1 = 1 << (ID_ADC-32);
+ uint32_t prescal = SystemCoreClock / (2 * ADC_FREQ_MAX) - 1;
+ ADC->ADC_MR = (ADC_MR_PRESCAL(prescal)
+ | ADC_MR_STARTUP_SUT768
+ | ADC_MR_TRANSFER(1));
}
- shutdown("Not a valid ADC pin");
+ return (struct gpio_adc){ .bit = 1 << chan };
}
// Try to sample a value. Returns zero if sample ready, otherwise