9 #ifndef NRFCXX_NRF52_IMPL_HPP 10 #define NRFCXX_NRF52_IMPL_HPP 14 #include <nrf52_bitfields.h> 16 #include <nrf52840_bitfields.h> 18 #error Unsupported NRF52 MCU 44 static constexpr IRQn_Type IRQn = SAADC_IRQn;
47 static constexpr
unsigned int VBG_mV = 600;
119 make_config (
unsigned int refsel = SAADC_CH_CONFIG_REFSEL_Internal,
120 unsigned int gain = SAADC_CH_CONFIG_GAIN_Gain1_6,
121 unsigned int tacq = SAADC_CH_CONFIG_TACQ_10us,
123 unsigned int resp = SAADC_CH_CONFIG_RESP_Bypass,
124 bool differential =
false,
125 unsigned int resn = SAADC_CH_CONFIG_RESN_Bypass)
128 | (SAADC_CH_CONFIG_RESP_Msk & (resp << SAADC_CH_CONFIG_RESP_Pos))
129 | (SAADC_CH_CONFIG_RESN_Msk & (resn << SAADC_CH_CONFIG_RESN_Pos))
130 | (SAADC_CH_CONFIG_GAIN_Msk & (gain << SAADC_CH_CONFIG_GAIN_Pos))
131 | (SAADC_CH_CONFIG_REFSEL_Msk & (refsel << SAADC_CH_CONFIG_REFSEL_Pos))
132 | (SAADC_CH_CONFIG_TACQ_Msk & (tacq << SAADC_CH_CONFIG_TACQ_Pos))
133 | (SAADC_CH_CONFIG_MODE_Msk & (differential << SAADC_CH_CONFIG_MODE_Pos))
134 | (SAADC_CH_CONFIG_BURST_Msk & (burst << SAADC_CH_CONFIG_BURST_Pos));
137 static constexpr uint32_t inten = 0
138 | (SAADC_INTENSET_CALIBRATEDONE_Set << SAADC_INTENSET_CALIBRATEDONE_Pos)
139 | (SAADC_INTENSET_STARTED_Set << SAADC_INTENSET_STARTED_Pos)
140 | (SAADC_INTENSET_DONE_Set << SAADC_INTENSET_DONE_Pos)
141 | (SAADC_INTENSET_RESULTDONE_Set << SAADC_INTENSET_RESULTDONE_Pos)
142 | (SAADC_INTENSET_END_Set << SAADC_INTENSET_END_Pos)
143 | (SAADC_INTENSET_STOPPED_Set << SAADC_INTENSET_STOPPED_Pos);
147 return nrf5::SAADC->STATUS;
196 static void enable_bi ()
198 nrf5::SAADC->EVENTS_STARTED = 0;
199 nrf5::SAADC->EVENTS_END = 0;
200 nrf5::SAADC->EVENTS_DONE = 0;
201 nrf5::SAADC->EVENTS_STOPPED = 0;
202 nrf5::SAADC->EVENTS_CALIBRATEDONE = 0;
203 nrf5::SAADC->RESULT.PTR = reinterpret_cast<uint32_t>(result_ptr_);
204 nrf5::SAADC->RESULT.MAXCNT = result_maxcnt_;
205 nrf5::SAADC->INTENSET = inten;
208 nrf5::SAADC->ENABLE = 1;
211 static bool calibrating_bi_;
213 static void disable_bi ()
215 nrf5::SAADC->ENABLE = 0;
217 nrf5::SAADC->INTENCLR = inten;
220 static int calibrate_bi ()
223 calibrating_bi_ =
true;
224 nrf5::SAADC->TASKS_CALIBRATEOFFSET = 1;
228 static int start_bi ()
231 calibrating_bi_ =
false;
232 nrf5::SAADC->TASKS_START = 1;
236 static void stopped_bi ()
248 volatile uint16_t* rp = result_ptr_;
249 volatile uint16_t*
const rpe = rp + result_maxcnt_;
255 unsigned int shift16 = 8 - 2 * ((nrf5::SAADC->RESOLUTION & 0x03) >> SAADC_RESOLUTION_VAL_Pos);
263 using ADC_Variant = SAADC_Peripheral;