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