|
| 1 | +#include "pxt.h" |
| 2 | +#include "nrf.h" |
| 3 | + |
| 4 | +namespace gdk { |
| 5 | + |
| 6 | +// DMA target for the one conversion result. Must live in RAM (EasyDMA). |
| 7 | +static volatile int16_t adcResult; |
| 8 | +static bool saadcCalibrated = false; |
| 9 | + |
| 10 | +// Busy-wait for a SAADC event register, with a hard timeout so a failed |
| 11 | +// conversion can never freeze the game loop. Returns true if the event fired. |
| 12 | +static bool saadcWait(volatile uint32_t *evt, uint32_t timeoutUs) { |
| 13 | + uint64_t start = pxt::current_time_us(); |
| 14 | + while (*evt == 0) { |
| 15 | + if (pxt::current_time_us() - start > timeoutUs) |
| 16 | + return false; |
| 17 | + } |
| 18 | + *evt = 0; |
| 19 | + return true; |
| 20 | +} |
| 21 | + |
| 22 | +// One-time offset calibration (recommended by Nordic before first use). |
| 23 | +static void saadcCalibrate() { |
| 24 | + NRF_SAADC->EVENTS_CALIBRATEDONE = 0; |
| 25 | + NRF_SAADC->TASKS_CALIBRATEOFFSET = 1; |
| 26 | + if (saadcWait(&NRF_SAADC->EVENTS_CALIBRATEDONE, 5000)) |
| 27 | + saadcCalibrated = true; |
| 28 | + // After calibration the SAADC raises STOPPED; clear it so the real read starts clean. |
| 29 | + NRF_SAADC->EVENTS_STOPPED = 0; |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * Blocking single-shot ADC read of P0 (P0.02 / AIN0). Returns 0..1023 (same |
| 34 | + * scale as pins.analogReadPin), or -1 on hardware timeout. |
| 35 | + * |
| 36 | + * Absolute internal 0.6V reference + gain 1/6 => full scale 3.6V (VDD-independent, |
| 37 | + * so the reading stays correct as the battery/VDD drains). 40us acquisition suits |
| 38 | + * the on-board ~82k divider. |
| 39 | + * |
| 40 | + * Owns the SAADC exclusively for the ~50us the conversion takes, then fully |
| 41 | + * disables it. It does NOT touch PPI/timers/IRQs, so it cannot disturb the |
| 42 | + * display or audio. The complete manual sequence is: |
| 43 | + * ENABLE -> START -> (STARTED) -> SAMPLE -> (END) -> STOP -> (STOPPED) -> DISABLE |
| 44 | + * The SAMPLE task is the critical step the previous version was missing. |
| 45 | + */ |
| 46 | +//% |
| 47 | +int readP0Adc() { |
| 48 | + NRF_SAADC->RESOLUTION = SAADC_RESOLUTION_VAL_14bit << SAADC_RESOLUTION_VAL_Pos; |
| 49 | + NRF_SAADC->OVERSAMPLE = SAADC_OVERSAMPLE_OVERSAMPLE_Bypass << SAADC_OVERSAMPLE_OVERSAMPLE_Pos; |
| 50 | + |
| 51 | + // P0 = P0.02 = AIN0 on channel 0; all other channels unused. |
| 52 | + NRF_SAADC->CH[0].PSELP = SAADC_CH_PSELP_PSELP_AnalogInput0 << SAADC_CH_PSELP_PSELP_Pos; |
| 53 | + NRF_SAADC->CH[0].PSELN = SAADC_CH_PSELN_PSELN_NC << SAADC_CH_PSELN_PSELN_Pos; |
| 54 | + NRF_SAADC->CH[0].CONFIG = |
| 55 | + (SAADC_CH_CONFIG_RESP_Bypass << SAADC_CH_CONFIG_RESP_Pos) | |
| 56 | + (SAADC_CH_CONFIG_RESN_Bypass << SAADC_CH_CONFIG_RESN_Pos) | |
| 57 | + (SAADC_CH_CONFIG_GAIN_Gain1_6 << SAADC_CH_CONFIG_GAIN_Pos) | |
| 58 | + (SAADC_CH_CONFIG_REFSEL_Internal << SAADC_CH_CONFIG_REFSEL_Pos) | |
| 59 | + (SAADC_CH_CONFIG_TACQ_40us << SAADC_CH_CONFIG_TACQ_Pos) | |
| 60 | + (SAADC_CH_CONFIG_MODE_SE << SAADC_CH_CONFIG_MODE_Pos) | |
| 61 | + (SAADC_CH_CONFIG_BURST_Disabled << SAADC_CH_CONFIG_BURST_Pos); |
| 62 | + for (int c = 1; c < 8; c++) { |
| 63 | + NRF_SAADC->CH[c].PSELP = SAADC_CH_PSELP_PSELP_NC << SAADC_CH_PSELP_PSELP_Pos; |
| 64 | + NRF_SAADC->CH[c].PSELN = SAADC_CH_PSELN_PSELN_NC << SAADC_CH_PSELN_PSELN_Pos; |
| 65 | + } |
| 66 | + |
| 67 | + adcResult = 0; |
| 68 | + NRF_SAADC->RESULT.PTR = (uint32_t)&adcResult; |
| 69 | + NRF_SAADC->RESULT.MAXCNT = 1; |
| 70 | + |
| 71 | + NRF_SAADC->EVENTS_STARTED = 0; |
| 72 | + NRF_SAADC->EVENTS_END = 0; |
| 73 | + NRF_SAADC->EVENTS_STOPPED = 0; |
| 74 | + |
| 75 | + NRF_SAADC->ENABLE = SAADC_ENABLE_ENABLE_Enabled << SAADC_ENABLE_ENABLE_Pos; |
| 76 | + |
| 77 | + if (!saadcCalibrated) |
| 78 | + saadcCalibrate(); |
| 79 | + |
| 80 | + // Ready the (D)MA buffer. |
| 81 | + NRF_SAADC->TASKS_START = 1; |
| 82 | + if (!saadcWait(&NRF_SAADC->EVENTS_STARTED, 5000)) { |
| 83 | + NRF_SAADC->ENABLE = 0; |
| 84 | + return -1; |
| 85 | + } |
| 86 | + |
| 87 | + // Take the actual sample -- THIS is what fills RESULT and raises END. |
| 88 | + NRF_SAADC->TASKS_SAMPLE = 1; |
| 89 | + if (!saadcWait(&NRF_SAADC->EVENTS_END, 5000)) { |
| 90 | + NRF_SAADC->TASKS_STOP = 1; |
| 91 | + NRF_SAADC->ENABLE = 0; |
| 92 | + return -1; |
| 93 | + } |
| 94 | + |
| 95 | + int sample = adcResult; |
| 96 | + if (sample < 0) |
| 97 | + sample = 0; // single-ended: clip small negative offset to 0 |
| 98 | + |
| 99 | + NRF_SAADC->TASKS_STOP = 1; |
| 100 | + saadcWait(&NRF_SAADC->EVENTS_STOPPED, 5000); |
| 101 | + NRF_SAADC->ENABLE = 0; |
| 102 | + |
| 103 | + // 14-bit full scale is 16383; scale to the familiar 0..1023 range. |
| 104 | + return sample / 16; |
| 105 | +} |
| 106 | + |
| 107 | +} // namespace gdk |
0 commit comments