Skip to content

Commit 74f76e9

Browse files
committed
Merge branch 'adc-write-res-fix' into zero
Conflicts: hardware/arduino/samd/cores/arduino/wiring_analog.c
2 parents 6c33c5d + 7c84589 commit 74f76e9

File tree

1 file changed

+29
-31
lines changed

1 file changed

+29
-31
lines changed

cores/arduino/wiring_analog.c

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ extern "C" {
2525
#endif
2626

2727
static int _readResolution = 10;
28-
static int _writeResolution = 10;
28+
static int _ADCResolution = 10;
29+
static int _writeResolution = 8;
2930

3031
// Wait for synchronization of registers between the clock domains
3132
static __inline__ void syncADC() __attribute__((always_inline, unused));
@@ -43,20 +44,23 @@ static void syncDAC() {
4344

4445
void analogReadResolution( int res )
4546
{
47+
_readResolution = res ;
4648
syncADC();
47-
switch ( res )
49+
if (res > 10)
4850
{
49-
case 12:
50-
ADC->CTRLB.bit.RESSEL = ADC_CTRLB_RESSEL_12BIT_Val;
51-
break;
52-
case 8:
53-
ADC->CTRLB.bit.RESSEL = ADC_CTRLB_RESSEL_8BIT_Val;
54-
break;
55-
default:
56-
ADC->CTRLB.bit.RESSEL = ADC_CTRLB_RESSEL_10BIT_Val;
57-
break;
51+
ADC->CTRLB.bit.RESSEL = ADC_CTRLB_RESSEL_12BIT_Val;
52+
_ADCResolution = 12;
53+
}
54+
else if (res > 8)
55+
{
56+
ADC->CTRLB.bit.RESSEL = ADC_CTRLB_RESSEL_10BIT_Val;
57+
_ADCResolution = 10;
58+
}
59+
else
60+
{
61+
ADC->CTRLB.bit.RESSEL = ADC_CTRLB_RESSEL_8BIT_Val;
62+
_ADCResolution = 8;
5863
}
59-
_readResolution = res ;
6064
}
6165

6266
void analogWriteResolution( int res )
@@ -179,7 +183,7 @@ uint32_t analogRead( uint32_t ulPin )
179183
ADC->CTRLA.bit.ENABLE = 0x00; // Disable ADC
180184
syncADC();
181185

182-
return valueRead;
186+
return mapResolution(valueRead, _ADCResolution, _readResolution);
183187
}
184188

185189

@@ -190,11 +194,6 @@ uint32_t analogRead( uint32_t ulPin )
190194
void analogWrite( uint32_t ulPin, uint32_t ulValue )
191195
{
192196
uint32_t attr = g_APinDescription[ulPin].ulPinAttribute ;
193-
// uint32_t pwm_name = g_APinDescription[ulPin].ulTCChannel ;
194-
uint8_t isTC = 0 ;
195-
uint8_t Channelx ;
196-
Tc* TCx = 0 ;
197-
Tcc* TCCx = 0 ;
198197

199198
if ( (attr & PIN_ATTR_ANALOG) == PIN_ATTR_ANALOG )
200199
{
@@ -203,10 +202,12 @@ void analogWrite( uint32_t ulPin, uint32_t ulValue )
203202
return;
204203
}
205204

205+
ulValue = mapResolution(ulValue, _writeResolution, 10);
206+
206207
syncDAC();
207208
DAC->DATA.reg = ulValue & 0x3FF; // DAC on 10 bits.
208209
syncDAC();
209-
DAC->CTRLA.bit.ENABLE = 0x01; //Enable ADC
210+
DAC->CTRLA.bit.ENABLE = 0x01; // Enable DAC
210211
syncDAC();
211212
return ;
212213
}
@@ -218,15 +219,15 @@ void analogWrite( uint32_t ulPin, uint32_t ulValue )
218219
pinPeripheral( ulPin, g_APinDescription[ulPin].ulPinType ) ;
219220
}
220221

221-
Channelx = GetTCChannelNumber( g_APinDescription[ulPin].ulPWMChannel ) ;
222+
Tc* TCx = 0 ;
223+
Tcc* TCCx = 0 ;
224+
uint8_t Channelx = GetTCChannelNumber( g_APinDescription[ulPin].ulPWMChannel ) ;
222225
if ( GetTCNumber( g_APinDescription[ulPin].ulPWMChannel ) >= TCC_INST_NUM )
223226
{
224-
isTC = 1 ;
225227
TCx = (Tc*) GetTC( g_APinDescription[ulPin].ulPWMChannel ) ;
226228
}
227229
else
228230
{
229-
isTC = 0 ;
230231
TCCx = (Tcc*) GetTC( g_APinDescription[ulPin].ulPWMChannel ) ;
231232
}
232233

@@ -263,12 +264,12 @@ void analogWrite( uint32_t ulPin, uint32_t ulValue )
263264
ulValue = mapResolution(ulValue, _writeResolution, 8);
264265

265266
// Set PORT
266-
if ( isTC )
267+
if ( TCx )
267268
{
268269
// -- Configure TC
269270

270-
// DISABLE TCx
271-
TCx->COUNT8.CTRLA.reg &=~(TC_CTRLA_ENABLE);
271+
// Disable TCx
272+
TCx->COUNT8.CTRLA.reg &= ~TC_CTRLA_ENABLE;
272273
// Set Timer counter Mode to 8 bits
273274
TCx->COUNT8.CTRLA.reg |= TC_CTRLA_MODE_COUNT8;
274275
// Set TCx as normal PWM
@@ -283,16 +284,15 @@ void analogWrite( uint32_t ulPin, uint32_t ulValue )
283284
else
284285
{
285286
// -- Configure TCC
286-
287-
// DISABLE TCCx
288-
TCCx->CTRLA.reg &=~(TCC_CTRLA_ENABLE);
287+
// Disable TCCx
288+
TCCx->CTRLA.reg &= ~TCC_CTRLA_ENABLE;
289289
// Set TCx as normal PWM
290290
TCCx->WAVE.reg |= TCC_WAVE_WAVEGEN_NPWM;
291291
// Set TCx in waveform mode Normal PWM
292292
TCCx->CC[Channelx].reg = (uint32_t)ulValue;
293293
// Set PER to maximum counter value (resolution : 0xFF)
294294
TCCx->PER.reg = 0xFF;
295-
// ENABLE TCCx
295+
// Enable TCCx
296296
TCCx->CTRLA.reg |= TCC_CTRLA_ENABLE ;
297297
}
298298

@@ -301,9 +301,7 @@ void analogWrite( uint32_t ulPin, uint32_t ulValue )
301301

302302
// -- Defaults to digital write
303303
pinMode( ulPin, OUTPUT ) ;
304-
305304
ulValue = mapResolution(ulValue, _writeResolution, 8);
306-
307305
if ( ulValue < 128 )
308306
{
309307
digitalWrite( ulPin, LOW ) ;

0 commit comments

Comments
 (0)