Skip to content
Snippets Groups Projects
Commit 74f76e9d authored by Cristian Maglie's avatar Cristian Maglie Committed by Cristian Maglie
Browse files

Merge branch 'adc-write-res-fix' into zero

Conflicts:
	hardware/arduino/samd/cores/arduino/wiring_analog.c
parents 6c33c5d7 7c845895
Branches
Tags
No related merge requests found
......@@ -25,7 +25,8 @@ extern "C" {
#endif
static int _readResolution = 10;
static int _writeResolution = 10;
static int _ADCResolution = 10;
static int _writeResolution = 8;
// Wait for synchronization of registers between the clock domains
static __inline__ void syncADC() __attribute__((always_inline, unused));
......@@ -43,20 +44,23 @@ static void syncDAC() {
void analogReadResolution( int res )
{
_readResolution = res ;
syncADC();
switch ( res )
if (res > 10)
{
case 12:
ADC->CTRLB.bit.RESSEL = ADC_CTRLB_RESSEL_12BIT_Val;
break;
case 8:
ADC->CTRLB.bit.RESSEL = ADC_CTRLB_RESSEL_8BIT_Val;
break;
default:
ADC->CTRLB.bit.RESSEL = ADC_CTRLB_RESSEL_10BIT_Val;
break;
ADC->CTRLB.bit.RESSEL = ADC_CTRLB_RESSEL_12BIT_Val;
_ADCResolution = 12;
}
else if (res > 8)
{
ADC->CTRLB.bit.RESSEL = ADC_CTRLB_RESSEL_10BIT_Val;
_ADCResolution = 10;
}
else
{
ADC->CTRLB.bit.RESSEL = ADC_CTRLB_RESSEL_8BIT_Val;
_ADCResolution = 8;
}
_readResolution = res ;
}
void analogWriteResolution( int res )
......@@ -179,7 +183,7 @@ uint32_t analogRead( uint32_t ulPin )
ADC->CTRLA.bit.ENABLE = 0x00; // Disable ADC
syncADC();
return valueRead;
return mapResolution(valueRead, _ADCResolution, _readResolution);
}
......@@ -190,11 +194,6 @@ uint32_t analogRead( uint32_t ulPin )
void analogWrite( uint32_t ulPin, uint32_t ulValue )
{
uint32_t attr = g_APinDescription[ulPin].ulPinAttribute ;
// uint32_t pwm_name = g_APinDescription[ulPin].ulTCChannel ;
uint8_t isTC = 0 ;
uint8_t Channelx ;
Tc* TCx = 0 ;
Tcc* TCCx = 0 ;
if ( (attr & PIN_ATTR_ANALOG) == PIN_ATTR_ANALOG )
{
......@@ -203,10 +202,12 @@ void analogWrite( uint32_t ulPin, uint32_t ulValue )
return;
}
ulValue = mapResolution(ulValue, _writeResolution, 10);
syncDAC();
DAC->DATA.reg = ulValue & 0x3FF; // DAC on 10 bits.
syncDAC();
DAC->CTRLA.bit.ENABLE = 0x01; //Enable ADC
DAC->CTRLA.bit.ENABLE = 0x01; // Enable DAC
syncDAC();
return ;
}
......@@ -218,15 +219,15 @@ void analogWrite( uint32_t ulPin, uint32_t ulValue )
pinPeripheral( ulPin, g_APinDescription[ulPin].ulPinType ) ;
}
Channelx = GetTCChannelNumber( g_APinDescription[ulPin].ulPWMChannel ) ;
Tc* TCx = 0 ;
Tcc* TCCx = 0 ;
uint8_t Channelx = GetTCChannelNumber( g_APinDescription[ulPin].ulPWMChannel ) ;
if ( GetTCNumber( g_APinDescription[ulPin].ulPWMChannel ) >= TCC_INST_NUM )
{
isTC = 1 ;
TCx = (Tc*) GetTC( g_APinDescription[ulPin].ulPWMChannel ) ;
}
else
{
isTC = 0 ;
TCCx = (Tcc*) GetTC( g_APinDescription[ulPin].ulPWMChannel ) ;
}
......@@ -263,12 +264,12 @@ void analogWrite( uint32_t ulPin, uint32_t ulValue )
ulValue = mapResolution(ulValue, _writeResolution, 8);
// Set PORT
if ( isTC )
if ( TCx )
{
// -- Configure TC
// DISABLE TCx
TCx->COUNT8.CTRLA.reg &=~(TC_CTRLA_ENABLE);
// Disable TCx
TCx->COUNT8.CTRLA.reg &= ~TC_CTRLA_ENABLE;
// Set Timer counter Mode to 8 bits
TCx->COUNT8.CTRLA.reg |= TC_CTRLA_MODE_COUNT8;
// Set TCx as normal PWM
......@@ -283,16 +284,15 @@ void analogWrite( uint32_t ulPin, uint32_t ulValue )
else
{
// -- Configure TCC
// DISABLE TCCx
TCCx->CTRLA.reg &=~(TCC_CTRLA_ENABLE);
// Disable TCCx
TCCx->CTRLA.reg &= ~TCC_CTRLA_ENABLE;
// Set TCx as normal PWM
TCCx->WAVE.reg |= TCC_WAVE_WAVEGEN_NPWM;
// Set TCx in waveform mode Normal PWM
TCCx->CC[Channelx].reg = (uint32_t)ulValue;
// Set PER to maximum counter value (resolution : 0xFF)
TCCx->PER.reg = 0xFF;
// ENABLE TCCx
// Enable TCCx
TCCx->CTRLA.reg |= TCC_CTRLA_ENABLE ;
}
......@@ -301,9 +301,7 @@ void analogWrite( uint32_t ulPin, uint32_t ulValue )
// -- Defaults to digital write
pinMode( ulPin, OUTPUT ) ;
ulValue = mapResolution(ulValue, _writeResolution, 8);
if ( ulValue < 128 )
{
digitalWrite( ulPin, LOW ) ;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment