| 
19 | 19 | 
 
  | 
20 | 20 | #ifdef NRF51  | 
21 | 21 | 
 
  | 
 | 22 | +#include "nrf_adc.h"  | 
22 | 23 | #include "nrf_timer.h"  | 
23 | 24 | #include "nrf_gpio.h"  | 
24 | 25 | 
 
  | 
@@ -48,6 +49,8 @@ static struct PWMContext pwmContext[PWM_COUNT] = {  | 
48 | 49 | 
 
  | 
49 | 50 | static int timerEnabled = 0;  | 
50 | 51 | 
 
  | 
 | 52 | +static nrf_adc_config_reference_t adcReference = NRF_ADC_CONFIG_REF_VBG;  | 
 | 53 | + | 
51 | 54 | static uint32_t readResolution = 10;  | 
52 | 55 | static uint32_t writeResolution = 8;  | 
53 | 56 | 
 
  | 
@@ -86,6 +89,97 @@ static inline uint32_t mapResolution( uint32_t value, uint32_t from, uint32_t to  | 
86 | 89 |  */  | 
87 | 90 | void analogReference( eAnalogReference ulMode )  | 
88 | 91 | {  | 
 | 92 | +    switch ( ulMode ) {  | 
 | 93 | +    case AR_DEFAULT:  | 
 | 94 | +    case AR_VBG:  | 
 | 95 | +    default:  | 
 | 96 | +      adcReference = NRF_ADC_CONFIG_REF_VBG;  | 
 | 97 | +      break;  | 
 | 98 | + | 
 | 99 | +    case AR_SUPPLY_ONE_HALF:  | 
 | 100 | +      adcReference = NRF_ADC_CONFIG_REF_SUPPLY_ONE_HALF;  | 
 | 101 | +      break;  | 
 | 102 | + | 
 | 103 | +    case AR_SUPPLY_ONE_THIRD:  | 
 | 104 | +      adcReference = NRF_ADC_CONFIG_REF_SUPPLY_ONE_THIRD;  | 
 | 105 | +      break;  | 
 | 106 | + | 
 | 107 | +    case AR_EXT0:  | 
 | 108 | +      adcReference = NRF_ADC_CONFIG_REF_EXT_REF0;  | 
 | 109 | +      break;  | 
 | 110 | + | 
 | 111 | +    case AR_EXT1:  | 
 | 112 | +      adcReference = NRF_ADC_CONFIG_REF_EXT_REF1;  | 
 | 113 | +      break;  | 
 | 114 | +  }  | 
 | 115 | +}  | 
 | 116 | + | 
 | 117 | +uint32_t analogRead( uint32_t ulPin )  | 
 | 118 | +{  | 
 | 119 | +  nrf_adc_config_input_t pin = NRF_ADC_CONFIG_INPUT_DISABLED;  | 
 | 120 | +  nrf_adc_config_t config;  | 
 | 121 | +  nrf_adc_config_resolution_t adcResolution;  | 
 | 122 | +  uint32_t resolution;  | 
 | 123 | +  uint32_t value;  | 
 | 124 | + | 
 | 125 | +  if (ulPin >= PINS_COUNT) {  | 
 | 126 | +    return 0;  | 
 | 127 | +  }  | 
 | 128 | + | 
 | 129 | +  ulPin = g_ADigitalPinMap[ulPin];  | 
 | 130 | + | 
 | 131 | +  switch ( ulPin ) {  | 
 | 132 | +    case 1:  | 
 | 133 | +      pin = NRF_ADC_CONFIG_INPUT_2;  | 
 | 134 | +      break;  | 
 | 135 | + | 
 | 136 | +    case 2:  | 
 | 137 | +      pin = NRF_ADC_CONFIG_INPUT_3;  | 
 | 138 | +      break;  | 
 | 139 | + | 
 | 140 | +    case 3:  | 
 | 141 | +      pin = NRF_ADC_CONFIG_INPUT_4;  | 
 | 142 | +      break;  | 
 | 143 | + | 
 | 144 | +    case 4:  | 
 | 145 | +      pin = NRF_ADC_CONFIG_INPUT_5;  | 
 | 146 | +      break;  | 
 | 147 | + | 
 | 148 | +    case 5:  | 
 | 149 | +      pin = NRF_ADC_CONFIG_INPUT_6;  | 
 | 150 | +      break;  | 
 | 151 | + | 
 | 152 | +    case 6:  | 
 | 153 | +      pin = NRF_ADC_CONFIG_INPUT_7;  | 
 | 154 | +      break;  | 
 | 155 | + | 
 | 156 | +    default:  | 
 | 157 | +      return 0;  | 
 | 158 | +  }  | 
 | 159 | + | 
 | 160 | +  if (readResolution <= 8) {  | 
 | 161 | +    resolution = 8;  | 
 | 162 | +    adcResolution = NRF_ADC_CONFIG_RES_8BIT;  | 
 | 163 | +  } else if (readResolution <= 9) {  | 
 | 164 | +    resolution = 9;  | 
 | 165 | +    adcResolution = NRF_ADC_CONFIG_RES_9BIT;  | 
 | 166 | +  } else {  | 
 | 167 | +    resolution = 10;  | 
 | 168 | +    adcResolution = NRF_ADC_CONFIG_RES_10BIT;  | 
 | 169 | +  }  | 
 | 170 | + | 
 | 171 | +  config.resolution = adcResolution;  | 
 | 172 | +  config.scaling = NRF_ADC_CONFIG_SCALING_INPUT_TWO_THIRDS;  | 
 | 173 | +  config.reference = adcReference;  | 
 | 174 | + | 
 | 175 | +  nrf_adc_enable();  | 
 | 176 | + | 
 | 177 | +  nrf_adc_configure(&config);  | 
 | 178 | +  value = nrf_adc_convert_single(pin);  | 
 | 179 | + | 
 | 180 | +  nrf_adc_disable();  | 
 | 181 | + | 
 | 182 | +  return mapResolution(value, resolution, readResolution);  | 
89 | 183 | }  | 
90 | 184 | 
 
  | 
91 | 185 | // Right now, PWM output only works on the pins with  | 
 | 
0 commit comments