From 21c3660f6d4f8365b8aea7683bb9d8d3b3bcb9d6 Mon Sep 17 00:00:00 2001 From: Arturo Guadalupi Date: Fri, 5 Dec 2014 16:32:33 +0100 Subject: [PATCH] This commit fixes the issue #2198: If an analog input is used and during the sketch modified in a digitalOutput the pinMode(AX, OUTPUT) isn't sensed because of this line of codes: wiring_analog.c line 135 // Enable the corresponding channel if (ulChannel != latestSelectedChannel) { adc_enable_channel( ADC, ulChannel ); if ( latestSelectedChannel != (uint32_t)-1 ) adc_disable_channel( ADC, latestSelectedChannel ); latestSelectedChannel = ulChannel; The channel is infact enabled only if different from the previous one because of speed problems. With my patch: when pinMode(PIN, OUTPUT) is declared a control about if the pin is an analog one is done and if so the ADC is released when pinMode(PIN, INPUT) is declared a control is as well done in order to enable the ADC. --- hardware/arduino/sam/cores/arduino/wiring_digital.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/hardware/arduino/sam/cores/arduino/wiring_digital.c b/hardware/arduino/sam/cores/arduino/wiring_digital.c index 7c958de8a56..ff5348bf872 100644 --- a/hardware/arduino/sam/cores/arduino/wiring_digital.c +++ b/hardware/arduino/sam/cores/arduino/wiring_digital.c @@ -32,6 +32,12 @@ extern void pinMode( uint32_t ulPin, uint32_t ulMode ) switch ( ulMode ) { case INPUT: + #if defined __SAM3X8E__ || defined __SAM3X8H__ + + if(g_APinDescription[ulPin].ulPinType != NO_ADC) + adc_enable_channel( ADC, g_APinDescription[ulPin].ulADCChannelNumber); + #endif + /* Enable peripheral for clocking input */ pmc_enable_periph_clk( g_APinDescription[ulPin].ulPeripheralId ) ; PIO_Configure( @@ -52,6 +58,12 @@ extern void pinMode( uint32_t ulPin, uint32_t ulMode ) break ; case OUTPUT: + #if defined __SAM3X8E__ || defined __SAM3X8H__ + + if(g_APinDescription[ulPin].ulPinType != NO_ADC) + adc_disable_channel( ADC, g_APinDescription[ulPin].ulADCChannelNumber); + #endif + PIO_Configure( g_APinDescription[ulPin].pPort, PIO_OUTPUT_1,