diff --git a/cores/arduino/wiring.c b/cores/arduino/wiring.c index e7f6167c7afb06af141583af8d77e6f7ee0d978b..d16960d8684bcfd73219a496a9412d4f92906e31 100644 --- a/cores/arduino/wiring.c +++ b/cores/arduino/wiring.c @@ -32,6 +32,24 @@ extern "C" { */ uint32_t SystemCoreClock=1000000ul ; +void calibrateADC() +{ + volatile uint32_t valeur = 0; + + for(int i = 0; i < 5; ++i) + { + ADC->SWTRIG.bit.START = 1; + while( ADC->INTFLAG.bit.RESRDY == 0 || ADC->STATUS.bit.SYNCBUSY == 1 ) + { + // Waiting for a complete conversion and complete synchronization + } + + valeur += ADC->RESULT.bit.RESULT; + } + + valeur = valeur/5; +} + /* * Arduino Zero board initialization * @@ -79,10 +97,29 @@ void init( void ) // Todo // Initialize Analog Controller - // Todo + //Setting clock + GCLK->CLKCTRL.reg = GCLK_CLKCTRL_ID( GCM_ADC ) | // Generic Clock ADC + GCLK_CLKCTRL_GEN_GCLK0 | // Generic Clock Generator 0 is source + GCLK_CLKCTRL_CLKEN ; + + ADC->CTRLB.reg = ADC_CTRLB_PRESCALER_DIV128 | // Divide Clock by 512. + ADC_CTRLB_RESSEL_10BIT; // Result on 10 bits + + ADC->INPUTCTRL.reg = ADC_INPUTCTRL_MUXNEG_GND; // No Negative input (Internal Ground) + + // Averaging (see table 31-2 p.816 datasheet) + ADC->AVGCTRL.reg = ADC_AVGCTRL_SAMPLENUM_2 | // 2 samples + ADC_AVGCTRL_ADJRES(0x01ul); // Adjusting result by 1 + + ADC->REFCTRL.reg = ADC_REFCTRL_REFSEL_AREFA; // RReference AREFA (pin AREF) [default] + + ADC->CTRLA.bit.ENABLE = 1; // Enable ADC + while( ADC->STATUS.bit.SYNCBUSY == 1 ) + { + // Waiting for synchroinization + } } #ifdef __cplusplus } #endif - diff --git a/cores/arduino/wiring_analog.c b/cores/arduino/wiring_analog.c index 2a15282c8e858fba12e0df54c56ddf764516e402..2732a8dc9f7478826f326fabe15a63261214251b 100644 --- a/cores/arduino/wiring_analog.c +++ b/cores/arduino/wiring_analog.c @@ -23,35 +23,51 @@ extern "C" { #endif +void analogReference( eAnalogReference ulMode ) +{ + switch(ulMode) + { + case INTERNAL: + ADC->REFCTRL.bit.REFSEL = ADC_REFCTRL_REFSEL_INT1V_Val; + break; + + case AR_DEFAULT: + default: + ADC->REFCTRL.bit.REFSEL = ADC_REFCTRL_REFSEL_AREFA_Val; + break; + } +} uint32_t analogRead( uint32_t ulPin ) { - uint32_t ulValue = 0 ; -/* - uint32_t ulChannel ; + uint32_t valueRead = 0; + pinPeripheral(ulPin, g_APinDescription[ulPin].ulPinType); - ulChannel = g_APinDescription[ulPin].ulADCChannelNumber ; + ADC->INPUTCTRL.bit.MUXPOS = g_APinDescription[ulPin].ulADCChannelNumber; - static uint32_t latestSelectedChannel = -1; + // Start conversion + ADC->SWTRIG.bit.START = 1; - switch ( g_APinDescription[ulPin].ulAnalogChannel ) + while( ADC->INTFLAG.bit.RESRDY == 0 || ADC->STATUS.bit.SYNCBUSY == 1 ) { - // Handling ADC 12 bits channels - case ADC0 : - case ADC1 : - case ADC2 : - case ADC3 : - case ADC4 : - case ADC5 : - break; - // Compiler could yell because we don't handle DAC pin - default : - ulValue=0; - break; + // Waiting for a complete conversion and complete synchronization } -*/ - return ulValue; - } + // Store the value + valueRead = ADC->RESULT.reg; + + // Clear the Data Ready flag + ADC->INTFLAG.bit.RESRDY = 1; + + // Flush the ADC for further conversions + //ADC->SWTRIG.bit.FLUSH = 1; + + while( ADC->STATUS.bit.SYNCBUSY == 1 || ADC->SWTRIG.bit.FLUSH == 1 ) + { + // Waiting for synchronization + } + + return valueRead; +} // Right now, PWM output only works on the pins with diff --git a/cores/arduino/wiring_analog.h b/cores/arduino/wiring_analog.h index a87e7d35451542a71914feb21e090652e362e2b7..d443be6f4f37fd71ac22ae833ebc723e817f4aee 100644 --- a/cores/arduino/wiring_analog.h +++ b/cores/arduino/wiring_analog.h @@ -31,6 +31,7 @@ extern "C" { typedef enum _eAnalogReference { AR_DEFAULT, + INTERNAL } eAnalogReference ; /* diff --git a/cores/validation/validation_core/build_as6/test.cppproj b/cores/validation/validation_core/build_as6/test.cppproj index 935a79159b22a661795c6ad48a23faf82b4aa8a1..232d6310a1f636994e2474c8cbbd2539eb106ad9 100644 --- a/cores/validation/validation_core/build_as6/test.cppproj +++ b/cores/validation/validation_core/build_as6/test.cppproj @@ -37,23 +37,23 @@ </dependencies> </framework-data> </AsfFrameworkConfig> - <avrtool>com.atmel.avrdbg.tool.edbg</avrtool> + <avrtool>com.atmel.avrdbg.tool.samice</avrtool> <avrtoolinterface>SWD</avrtoolinterface> <com_atmel_avrdbg_tool_samice> <ToolOptions> <InterfaceProperties> + <SwdClock>4000000</SwdClock> </InterfaceProperties> <InterfaceName>SWD</InterfaceName> </ToolOptions> <ToolType>com.atmel.avrdbg.tool.samice</ToolType> - <ToolNumber>28001042</ToolNumber> + <ToolNumber>28010306</ToolNumber> <ToolName>SAM-ICE</ToolName> </com_atmel_avrdbg_tool_samice> <UseGdb>True</UseGdb> <com_atmel_avrdbg_tool_edbg> <ToolOptions> <InterfaceProperties> - <SwdClock>4000000</SwdClock> </InterfaceProperties> <InterfaceName>SWD</InterfaceName> </ToolOptions> diff --git a/cores/validation/validation_core/test.cpp b/cores/validation/validation_core/test.cpp index 0e2a384d7273cd6fa6c026e96aa64c1347d6250b..85e5315d712cc6d1db9b1902998178957ba4adf0 100644 --- a/cores/validation/validation_core/test.cpp +++ b/cores/validation/validation_core/test.cpp @@ -30,6 +30,11 @@ static uint32_t ul_Interrupt_Pin5 = 0 ; static uint32_t ul_Interrupt_Pin6 = 0 ; static uint32_t ul_Interrupt_Pin7 = 0 ; +int temps = 0; +int valX = 0; +int valY = 0; + + void setup( void ) { // Initialize the digital pin as an output. @@ -113,15 +118,15 @@ void loop( void ) analogWrite( 5, duty_cycle ) ; analogWrite( 4, duty_cycle ) ; - Serial5.print("Analog pins: "); + Serial5.print("\r\nAnalog pins: "); - for ( uint32_t i = A1 ; i <= A0+NUM_ANALOG_INPUTS ; i++ ) + for ( uint32_t i = A0 ; i <= A0+NUM_ANALOG_INPUTS ; i++ ) { -/* + int a = analogRead(i); Serial5.print(a, DEC); Serial5.print(" "); -*/ + } Serial5.println(); diff --git a/variants/arduino_zero/variant.h b/variants/arduino_zero/variant.h index 7ffbb3f56c63968cd7561bc317f4915ba3439c5d..3ca7c59716fee500594df6646b27c605163e43f4 100644 --- a/variants/arduino_zero/variant.h +++ b/variants/arduino_zero/variant.h @@ -153,12 +153,12 @@ static const uint8_t SCK = PIN_SPI_SCK; /* * Analog pins */ -static const uint8_t A0 = 20 ; -static const uint8_t A1 = 21 ; -static const uint8_t A2 = 22 ; -static const uint8_t A3 = 23 ; -static const uint8_t A4 = 24 ; -static const uint8_t A5 = 25 ; +static const uint8_t A0 = 24 ; +static const uint8_t A1 = 25 ; +static const uint8_t A2 = 26 ; +static const uint8_t A3 = 27 ; +static const uint8_t A4 = 28 ; +static const uint8_t A5 = 29 ; #define ADC_RESOLUTION 12 /*