sam3x8e: Add support for ADC pins

Support analog-to-digital inputs.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2016-12-23 14:49:52 -05:00
parent 872b08601a
commit 618fe0e6fb
5 changed files with 90 additions and 3 deletions

View File

@ -89,8 +89,6 @@ Documentation
Hardware features Hardware features
================= =================
* Support ADC on Arduino Due.
* Port firmware to more architectures: * Port firmware to more architectures:
* Beagle Bone Black PRU * Beagle Bone Black PRU
* Smoothieboard / NXP LPC1769 (ARM cortex-M3) * Smoothieboard / NXP LPC1769 (ARM cortex-M3)

View File

@ -18,7 +18,7 @@ struct analog_in {
uint8_t state, sample_count; uint8_t state, sample_count;
}; };
static uint8_t static uint_fast8_t
analog_in_event(struct timer *timer) analog_in_event(struct timer *timer)
{ {
struct analog_in *a = container_of(timer, struct analog_in, timer); struct analog_in *a = container_of(timer, struct analog_in, timer);

View File

@ -2,6 +2,11 @@
if MACH_SAM3X8E if MACH_SAM3X8E
config SAM_SELECT
bool
default y
select HAVE_GPIO_ADC
config BOARD_DIRECTORY config BOARD_DIRECTORY
string string
default "sam3x8e" default "sam3x8e"

View File

@ -5,6 +5,7 @@
// This file may be distributed under the terms of the GNU GPLv3 license. // This file may be distributed under the terms of the GNU GPLv3 license.
#include <stdint.h> // uint32_t #include <stdint.h> // uint32_t
#include "autoconf.h" // CONFIG_CLOCK_FREQ
#include "command.h" // shutdown #include "command.h" // shutdown
#include "compiler.h" // ARRAY_SIZE #include "compiler.h" // ARRAY_SIZE
#include "gpio.h" // gpio_out_setup #include "gpio.h" // gpio_out_setup
@ -113,3 +114,78 @@ gpio_in_read(struct gpio_in g)
Pio *regs = g.regs; Pio *regs = g.regs;
return !!(regs->PIO_PDSR & g.bit); return !!(regs->PIO_PDSR & g.bit);
} }
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),
GPIO('B', 12), GPIO('B', 13), GPIO('B', 17), GPIO('B', 18),
GPIO('B', 19), GPIO('B', 20)
};
#define ADC_FREQ_MAX 20000000
DECL_CONSTANT(ADC_MAX, 4096);
struct gpio_adc
gpio_adc_setup(uint8_t pin)
{
int chan;
for (chan=0; chan<ARRAY_SIZE(adc_pins); chan++) {
if (adc_pins[chan] != pin)
continue;
// Found PIN
if (!(PMC->PMC_PCER1 & (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 };
}
shutdown("Not a valid ADC pin");
}
// Try to sample a value. Returns zero if sample ready, otherwise
// returns the number of clock ticks the caller should wait before
// retrying this function.
uint32_t
gpio_adc_sample(struct gpio_adc g)
{
uint32_t chsr = ADC->ADC_CHSR & 0xffff;
if (!chsr) {
// Start sample
ADC->ADC_CHER = g.bit;
ADC->ADC_CR = ADC_CR_START;
goto need_delay;
}
if (chsr != g.bit)
// Sampling in progress on another channel
goto need_delay;
if (!(ADC->ADC_ISR & ADC_ISR_DRDY))
// Conversion still in progress
goto need_delay;
// Conversion ready
return 0;
need_delay:
return ADC_FREQ_MAX * 1000ULL / CONFIG_CLOCK_FREQ;
}
// Read a value; use only after gpio_adc_sample() returns zero
uint16_t
gpio_adc_read(struct gpio_adc g)
{
ADC->ADC_CHDR = g.bit;
return ADC->ADC_LCDR;
}
// Cancel a sample that may have been started with gpio_adc_sample()
void
gpio_adc_cancel_sample(struct gpio_adc g)
{
irqstatus_t flag = irq_save();
if ((ADC->ADC_CHSR & 0xffff) == g.bit)
gpio_adc_read(g);
irq_restore(flag);
}

View File

@ -20,4 +20,12 @@ struct gpio_in {
struct gpio_in gpio_in_setup(uint8_t pin, int8_t pull_up); struct gpio_in gpio_in_setup(uint8_t pin, int8_t pull_up);
uint8_t gpio_in_read(struct gpio_in g); uint8_t gpio_in_read(struct gpio_in g);
struct gpio_adc {
uint32_t bit;
};
struct gpio_adc gpio_adc_setup(uint8_t pin);
uint32_t gpio_adc_sample(struct gpio_adc g);
uint16_t gpio_adc_read(struct gpio_adc g);
void gpio_adc_cancel_sample(struct gpio_adc g);
#endif // gpio.h #endif // gpio.h