Skip to content

Commit

Permalink
Merge pull request adafruit#591 from ogatatsu/analogReadVDD
Browse files Browse the repository at this point in the history
Add analogReadVDD() function
  • Loading branch information
hathach authored Oct 12, 2020
2 parents 9f0f424 + fdded90 commit 405d48c
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 47 deletions.
8 changes: 8 additions & 0 deletions cores/nRF5/wiring_analog.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ extern void analogWrite( uint32_t ulPin, uint32_t ulValue ) ;
*/
extern uint32_t analogRead( uint32_t ulPin ) ;

/*
* \brief Read the value from the vdd pin.
*
* \return Read value from vdd pin, if no error.
*/
extern uint32_t analogReadVDD( void ) ;


/*
* \brief Set the resolution of analogRead return values. Default is 10 bits (range from 0 to 1023).
*
Expand Down
106 changes: 59 additions & 47 deletions cores/nRF5/wiring_analog_nRF52.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,56 +140,12 @@ void analogOversampling( uint32_t ulOversampling )
}
}

uint32_t analogRead( uint32_t ulPin )
static uint32_t analogRead_internal( uint32_t psel )
{
uint32_t pin = SAADC_CH_PSELP_PSELP_NC;
uint32_t saadcResolution;
uint32_t resolution;
volatile int16_t value = 0;

if (ulPin >= PINS_COUNT) {
return 0;
}

ulPin = g_ADigitalPinMap[ulPin];

switch ( ulPin ) {
case 2:
pin = SAADC_CH_PSELP_PSELP_AnalogInput0;
break;

case 3:
pin = SAADC_CH_PSELP_PSELP_AnalogInput1;
break;

case 4:
pin = SAADC_CH_PSELP_PSELP_AnalogInput2;
break;

case 5:
pin = SAADC_CH_PSELP_PSELP_AnalogInput3;
break;

case 28:
pin = SAADC_CH_PSELP_PSELP_AnalogInput4;
break;

case 29:
pin = SAADC_CH_PSELP_PSELP_AnalogInput5;
break;

case 30:
pin = SAADC_CH_PSELP_PSELP_AnalogInput6;
break;

case 31:
pin = SAADC_CH_PSELP_PSELP_AnalogInput7;
break;

default:
return 0;
}

if (readResolution <= 8) {
resolution = 8;
saadcResolution = SAADC_RESOLUTION_VAL_8bit;
Expand Down Expand Up @@ -218,8 +174,8 @@ uint32_t analogRead( uint32_t ulPin )
| ((SAADC_CH_CONFIG_TACQ_3us << SAADC_CH_CONFIG_TACQ_Pos) & SAADC_CH_CONFIG_TACQ_Msk)
| ((SAADC_CH_CONFIG_MODE_SE << SAADC_CH_CONFIG_MODE_Pos) & SAADC_CH_CONFIG_MODE_Msk)
| ((saadcBurst << SAADC_CH_CONFIG_BURST_Pos) & SAADC_CH_CONFIG_BURST_Msk);
NRF_SAADC->CH[0].PSELN = pin;
NRF_SAADC->CH[0].PSELP = pin;
NRF_SAADC->CH[0].PSELN = psel;
NRF_SAADC->CH[0].PSELP = psel;


NRF_SAADC->RESULT.PTR = (uint32_t)&value;
Expand Down Expand Up @@ -249,6 +205,62 @@ uint32_t analogRead( uint32_t ulPin )
return mapResolution(value, resolution, readResolution);
}


uint32_t analogRead( uint32_t ulPin )
{
uint32_t psel = SAADC_CH_PSELP_PSELP_NC;

if (ulPin >= PINS_COUNT) {
return 0;
}

ulPin = g_ADigitalPinMap[ulPin];

switch ( ulPin ) {
case 2:
psel = SAADC_CH_PSELP_PSELP_AnalogInput0;
break;

case 3:
psel = SAADC_CH_PSELP_PSELP_AnalogInput1;
break;

case 4:
psel = SAADC_CH_PSELP_PSELP_AnalogInput2;
break;

case 5:
psel = SAADC_CH_PSELP_PSELP_AnalogInput3;
break;

case 28:
psel = SAADC_CH_PSELP_PSELP_AnalogInput4;
break;

case 29:
psel = SAADC_CH_PSELP_PSELP_AnalogInput5;
break;

case 30:
psel = SAADC_CH_PSELP_PSELP_AnalogInput6;
break;

case 31:
psel = SAADC_CH_PSELP_PSELP_AnalogInput7;
break;

default:
return 0;
}

return analogRead_internal(psel);
}

uint32_t analogReadVDD( void )
{
return analogRead_internal(SAADC_CH_PSELP_PSELP_VDD);
}

#ifdef __cplusplus
}
#endif
Expand Down
11 changes: 11 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ dwt_enable KEYWORD2
dwt_disable KEYWORD2
delay_ns KEYWORD2

analogOversampling KEYWORD2
analogReadVDD KEYWORD2

# RTOS
rtos_malloc KEYWORD2
rtos_free KEYWORD2
Expand Down Expand Up @@ -122,3 +125,11 @@ PIN_A7 LITERAL1
PIN_AREF LITERAL1

ISR_DEFERRED LITERAL1

AR_DEFAULT LITERAL1
AR_INTERNAL LITERAL1
AR_INTERNAL_3_0 LITERAL1
AR_INTERNAL_2_4 LITERAL1
AR_INTERNAL_1_8 LITERAL1
AR_INTERNAL_1_2 LITERAL1
AR_VDD4 LITERAL1

0 comments on commit 405d48c

Please sign in to comment.