nrf_drv_csense.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. /**
  2. * Copyright (c) 2016 - 2018, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #include "sdk_common.h"
  41. #if NRF_MODULE_ENABLED(NRF_DRV_CSENSE)
  42. #include "nrf_drv_csense.h"
  43. #include "nrf_peripherals.h"
  44. #include "nrf_gpio.h"
  45. #include "app_error.h"
  46. #include "app_util_platform.h"
  47. #include "nrf_assert.h"
  48. #include "string.h"
  49. #include <stdio.h>
  50. #if defined(__CORTEX_M) && (__CORTEX_M < 4)
  51. #ifndef ARM_MATH_CM0PLUS
  52. #define ARM_MATH_CM0PLUS
  53. #endif
  54. /*lint -save -e689 */
  55. #include "arm_math.h"
  56. /*lint -restore */
  57. #endif
  58. #if USE_COMP
  59. #include "nrf_drv_comp.h"
  60. #include "nrf_drv_ppi.h"
  61. #include "nrf_drv_timer.h"
  62. #endif //USE_COMP
  63. #if USE_COMP == 0
  64. #ifdef ADC_PRESENT
  65. #include "nrfx_adc.h"
  66. /**
  67. * @defgroup adc_defines ADC defines to count input voltage.
  68. * @{
  69. */
  70. #define ADC_RES_10BIT 1024
  71. #define ADC_INPUT_PRESCALER 3
  72. #define ADC_REF_VBG_VOLTAGE 1.2
  73. /* @} */
  74. /* ADC channel used to call conversion. */
  75. static nrfx_adc_channel_t adc_channel = NRFX_ADC_DEFAULT_CHANNEL(NRF_ADC_CONFIG_INPUT_0);
  76. #elif defined(SAADC_PRESENT)
  77. #include "nrf_drv_saadc.h"
  78. /**
  79. * @defgroup saadc_defines SAADC defines to count input voltage.
  80. * @{
  81. */
  82. #define SAADC_RES_10BIT 1024
  83. #define SAADC_INPUT_PRESCALER 3
  84. #define SAADC_REF_VBG_VOLTAGE 0.6
  85. /* @} */
  86. /* SAADC channel used to call conversion. */
  87. static nrf_saadc_channel_config_t saadc_channel = NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN0);
  88. #endif //ADC_PRESENT
  89. #endif //USE_COMP
  90. #if USE_COMP
  91. /* Number of channels required by PPI. */
  92. #define PPI_REQUIRED_CHANNELS 3
  93. /* Array of PPI channels. */
  94. static nrf_ppi_channel_t m_ppi_channels[PPI_REQUIRED_CHANNELS];
  95. /**
  96. * @defgroup timer_instances Timer instances.
  97. * @{
  98. */
  99. static const nrf_drv_timer_t m_timer0 = NRF_DRV_TIMER_INSTANCE(TIMER0_FOR_CSENSE);
  100. static const nrf_drv_timer_t m_timer1 = NRF_DRV_TIMER_INSTANCE(TIMER1_FOR_CSENSE);
  101. /* @} */
  102. #endif //USE_COMP
  103. /* Configuration of the capacitive sensor module. */
  104. typedef struct
  105. {
  106. volatile nrfx_drv_state_t module_state; /**< State of the module. */
  107. nrf_drv_csense_event_handler_t event_handler; /**< Event handler for capacitor sensor events. */
  108. uint16_t analog_values[MAX_ANALOG_INPUTS]; /**< Array containing analog values measured on the corresponding COMP/ADC channel. */
  109. volatile bool busy; /**< Indicates state of module - busy if there are ongoing conversions. */
  110. volatile uint8_t cur_chann_idx; /**< Current channel to be read if enabled. */
  111. volatile uint8_t adc_channels_input_mask; /**< Enabled channels. */
  112. uint8_t output_pin; /**< Pin to generate signal charging capacitors. */
  113. uint8_t channels_to_read; /**< Mask of channels remaining to be read in the current measurement. */
  114. volatile bool timers_powered_on; /**< Flag to indicate if timers were already started. */
  115. }csense_t;
  116. static csense_t m_csense;
  117. /**
  118. * @brief Function for determining the next analog channel to be read.
  119. */
  120. __STATIC_INLINE void calculate_next_channel(void)
  121. {
  122. m_csense.cur_chann_idx = 31 - __CLZ(m_csense.channels_to_read);
  123. }
  124. /**
  125. * @brief Function for handling conversion values.
  126. *
  127. * @param[in] val Value received from ADC or COMP.
  128. */
  129. static void conversion_handler(uint16_t val)
  130. {
  131. nrf_drv_csense_evt_t event_struct;
  132. #if USE_COMP == 0
  133. nrf_gpio_pin_set(m_csense.output_pin);
  134. #endif //USE_COMP
  135. m_csense.analog_values[m_csense.cur_chann_idx] = val;
  136. event_struct.read_value = val;
  137. event_struct.analog_channel = m_csense.cur_chann_idx;
  138. m_csense.channels_to_read &= ~(1UL<<m_csense.cur_chann_idx);
  139. // decide if there will be more conversions
  140. if (m_csense.channels_to_read == 0)
  141. {
  142. m_csense.busy = false;
  143. #if USE_COMP == 0 && defined(SAADC_PRESENT)
  144. nrf_saadc_disable();
  145. #endif
  146. }
  147. m_csense.event_handler(&event_struct);
  148. if (m_csense.channels_to_read > 0) // Start new conversion.
  149. {
  150. ret_code_t err_code;
  151. calculate_next_channel();
  152. err_code = nrf_drv_csense_sample();
  153. if (err_code != NRF_SUCCESS)
  154. {
  155. return;
  156. }
  157. }
  158. }
  159. #if USE_COMP
  160. /**
  161. * @brief Timer0 interrupt handler.
  162. *
  163. * @param[in] event_type Timer event.
  164. * @param[in] p_context General purpose parameter set during initialization of
  165. * the timer. This parameter can be used to pass
  166. * additional information to the handler function, for
  167. * example, the timer ID.
  168. */
  169. static void counter_compare_handler(nrf_timer_event_t event_type, void* p_context)
  170. {
  171. if (event_type == NRF_TIMER_EVENT_COMPARE0)
  172. {
  173. uint16_t val = nrf_drv_timer_capture_get(&m_timer1, NRF_TIMER_CC_CHANNEL1);
  174. nrf_drv_timer_pause(&m_timer1);
  175. nrf_drv_timer_clear(&m_timer1);
  176. /* Handle finished measurement. */
  177. conversion_handler(val);
  178. }
  179. }
  180. /**
  181. * @brief Dummy handler.
  182. *
  183. * @param[in] event_type Timer event.
  184. * @param[in] p_context General purpose parameter set during initialization of
  185. * the timer. This parameter can be used to pass
  186. * additional information to the handler function, for
  187. * example, the timer ID.
  188. */
  189. static void dummy_handler(nrf_timer_event_t event_type, void* p_context){}
  190. /**
  191. * @brief Function for initializing timers.
  192. *
  193. * @retval NRF_ERROR_INTERNAL If there were error initializing timers.
  194. * @retval NRF_SUCCESS If timers were initialized successfully.
  195. */
  196. static ret_code_t timer_init(void)
  197. {
  198. ret_code_t err_code;
  199. //set first timer in timer mode to get period of relaxation oscillator
  200. nrf_drv_timer_config_t timer_config = NRF_DRV_TIMER_DEFAULT_CONFIG;
  201. timer_config.mode = NRF_TIMER_MODE_TIMER;
  202. err_code = nrf_drv_timer_init(&m_timer1, &timer_config, dummy_handler);
  203. if (err_code != NRF_SUCCESS)
  204. {
  205. return NRF_ERROR_INTERNAL;
  206. }
  207. //set second timer in counter mode and generate event on tenth period
  208. timer_config.mode = NRF_TIMER_MODE_COUNTER;
  209. err_code = nrf_drv_timer_init(&m_timer0, &timer_config, counter_compare_handler);
  210. if (err_code != NRF_SUCCESS)
  211. {
  212. return NRF_ERROR_INTERNAL;
  213. }
  214. nrf_drv_timer_extended_compare(&m_timer0, NRF_TIMER_CC_CHANNEL0, MEASUREMENT_PERIOD, (nrf_timer_short_mask_t)(NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK | NRF_TIMER_SHORT_COMPARE0_STOP_MASK), true);
  215. return NRF_SUCCESS;
  216. }
  217. /**
  218. * @brief Function for initializing and enabling PPI channels.
  219. *
  220. * @retval NRF_ERROR_INTERNAL If there were error initializing or enabling PPI channels.
  221. * @retval NRF_SUCCESS If PPI channels were initialized and enabled successfully.
  222. */
  223. static ret_code_t ppi_init(void)
  224. {
  225. ret_code_t err_code;
  226. uint8_t i;
  227. err_code = nrf_drv_ppi_init();
  228. if ((err_code != NRF_SUCCESS) && (err_code != NRF_ERROR_MODULE_ALREADY_INITIALIZED))
  229. {
  230. return NRF_ERROR_INTERNAL;
  231. }
  232. for (i = 0; i < PPI_REQUIRED_CHANNELS ; i++)
  233. {
  234. err_code = nrf_drv_ppi_channel_alloc(&m_ppi_channels[i]);
  235. if (NRF_SUCCESS != err_code)
  236. {
  237. return NRF_ERROR_INTERNAL;
  238. }
  239. }
  240. err_code = nrf_drv_ppi_channel_assign(m_ppi_channels[0], nrf_drv_comp_event_address_get(NRF_COMP_EVENT_CROSS), nrf_drv_timer_task_address_get(&m_timer0, NRF_TIMER_TASK_COUNT));
  241. if (NRF_SUCCESS != err_code)
  242. {
  243. return NRF_ERROR_INTERNAL;
  244. }
  245. err_code = nrf_drv_ppi_channel_assign(m_ppi_channels[1], nrf_drv_timer_event_address_get(&m_timer0, NRF_TIMER_EVENT_COMPARE0), nrf_drv_timer_task_address_get(&m_timer1, NRF_TIMER_TASK_CAPTURE1));
  246. if (NRF_SUCCESS != err_code)
  247. {
  248. return NRF_ERROR_INTERNAL;
  249. }
  250. err_code = nrf_drv_ppi_channel_fork_assign(m_ppi_channels[1], nrf_drv_comp_task_address_get(NRF_COMP_TASK_STOP));
  251. if (NRF_SUCCESS != err_code)
  252. {
  253. return NRF_ERROR_INTERNAL;
  254. }
  255. err_code = nrf_drv_ppi_channel_assign(m_ppi_channels[2], nrf_drv_comp_event_address_get(NRF_COMP_EVENT_READY), nrf_drv_timer_task_address_get(&m_timer0, NRF_TIMER_TASK_CLEAR));
  256. if (NRF_SUCCESS != err_code)
  257. {
  258. return NRF_ERROR_INTERNAL;
  259. }
  260. err_code = nrf_drv_ppi_channel_fork_assign(m_ppi_channels[2], nrf_drv_timer_task_address_get(&m_timer1, NRF_TIMER_TASK_CLEAR));
  261. if (NRF_SUCCESS != err_code)
  262. {
  263. return NRF_ERROR_INTERNAL;
  264. }
  265. for (i = 0; i < PPI_REQUIRED_CHANNELS ; i++)
  266. {
  267. err_code = nrf_drv_ppi_channel_enable(m_ppi_channels[i]);
  268. if (NRF_SUCCESS != err_code)
  269. {
  270. return NRF_ERROR_INTERNAL;
  271. }
  272. }
  273. return NRF_SUCCESS;
  274. }
  275. /**
  276. * @brief Dummy handler for COMP events.
  277. *
  278. * @param[in] event COMP event.
  279. */
  280. static void comp_event_handler(nrf_comp_event_t event){}
  281. /**
  282. * @brief Function for initializing COMP module in relaxation oscillator mode.
  283. *
  284. * @note The frequency of the oscillator depends on threshold voltages, current source and capacitance of pad and can be calculated as f_OSC = I_SOURCE / (2C·(VUP-VDOWN) ).
  285. *
  286. * @retval NRF_ERROR_INTERNAL If there were error while initializing COMP driver.
  287. * @retval NRF_SUCCESS If the COMP driver initialization was successful.
  288. */
  289. static ret_code_t comp_init(void)
  290. {
  291. ret_code_t err_code;
  292. nrf_drv_comp_config_t m_comp_config = NRF_DRV_COMP_DEFAULT_CONFIG(NRF_COMP_INPUT_0);
  293. /* Workaround for Errata 12 "COMP: Reference ladder is not correctly calibrated" found at the Errata document
  294. for your device located at https://infocenter.nordicsemi.com/ */
  295. *(volatile uint32_t *)0x40013540 = (*(volatile uint32_t *)0x10000324 & 0x00001F00) >> 8;
  296. m_comp_config.isource = NRF_COMP_ISOURCE_Ien10uA;
  297. err_code = nrf_drv_comp_init(&m_comp_config, comp_event_handler);
  298. if (err_code != NRF_SUCCESS)
  299. {
  300. return NRF_ERROR_INTERNAL;
  301. }
  302. return NRF_SUCCESS;
  303. }
  304. #endif //USE_COMP
  305. #if USE_COMP == 0
  306. #ifdef ADC_PRESENT
  307. /**
  308. * @brief ADC handler.
  309. *
  310. * @param[in] p_event Pointer to analog-to-digital converter driver event.
  311. */
  312. void adc_handler(nrfx_adc_evt_t const * p_event)
  313. {
  314. nrf_gpio_pin_set(m_csense.output_pin);
  315. uint16_t val;
  316. val = (uint16_t)(p_event->data.sample.sample *
  317. ADC_REF_VBG_VOLTAGE * 1000 *
  318. ADC_INPUT_PRESCALER / ADC_RES_10BIT);
  319. conversion_handler(val);
  320. }
  321. /**
  322. * @brief Function for initializing ADC.
  323. */
  324. static ret_code_t adc_init(void)
  325. {
  326. ret_code_t err_code;
  327. adc_channel.config.config.input = NRF_ADC_CONFIG_SCALING_INPUT_ONE_THIRD;
  328. nrfx_adc_config_t const adc_config = NRFX_ADC_DEFAULT_CONFIG;
  329. err_code = nrfx_adc_init(&adc_config, adc_handler);
  330. if (err_code != NRF_SUCCESS)
  331. {
  332. return NRF_ERROR_INTERNAL;
  333. }
  334. nrf_gpio_pin_set(m_csense.output_pin);
  335. return NRF_SUCCESS;
  336. }
  337. #elif defined(SAADC_PRESENT)
  338. /**
  339. * @brief SAADC handler.
  340. *
  341. * @param[in] p_event Pointer to analog-to-digital converter driver event.
  342. */
  343. void saadc_handler(nrf_drv_saadc_evt_t const * p_event)
  344. {
  345. nrf_gpio_pin_set(m_csense.output_pin);
  346. uint16_t val;
  347. (void)nrf_drv_saadc_buffer_convert(p_event->data.done.p_buffer, 1);
  348. val = (uint16_t)(*p_event->data.done.p_buffer *
  349. SAADC_REF_VBG_VOLTAGE * 1000 *
  350. SAADC_INPUT_PRESCALER / SAADC_RES_10BIT);
  351. conversion_handler(val);
  352. }
  353. /**
  354. * @brief Function for initializing SAADC.
  355. */
  356. static ret_code_t saadc_init(void)
  357. {
  358. ret_code_t err_code;
  359. static nrf_saadc_value_t saadc_value;
  360. saadc_channel.gain = NRF_SAADC_GAIN1_3;
  361. err_code = nrf_drv_saadc_init(NULL, saadc_handler);
  362. if (err_code != NRF_SUCCESS)
  363. {
  364. return NRF_ERROR_INTERNAL;
  365. }
  366. nrf_gpio_pin_set(m_csense.output_pin);
  367. err_code = nrf_drv_saadc_channel_init(0, &saadc_channel);
  368. if (err_code != NRF_SUCCESS)
  369. {
  370. return NRF_ERROR_INTERNAL;
  371. }
  372. err_code = nrf_drv_saadc_buffer_convert(&saadc_value, 1);
  373. if (err_code != NRF_SUCCESS)
  374. {
  375. return NRF_ERROR_INTERNAL;
  376. }
  377. nrf_saadc_disable();
  378. return NRF_SUCCESS;
  379. }
  380. #endif //ADC_PRESENT
  381. #endif //USE_COMP
  382. ret_code_t nrf_drv_csense_init(nrf_drv_csense_config_t const * p_config, nrf_drv_csense_event_handler_t event_handler)
  383. {
  384. ASSERT(m_csense.module_state == NRFX_DRV_STATE_UNINITIALIZED);
  385. ASSERT(p_config->output_pin <= NUMBER_OF_PINS);
  386. ret_code_t err_code;
  387. if (p_config == NULL)
  388. {
  389. return NRF_ERROR_INVALID_PARAM;
  390. }
  391. if (event_handler == NULL)
  392. {
  393. return NRF_ERROR_INVALID_PARAM;
  394. }
  395. m_csense.busy = false;
  396. #if USE_COMP == 0
  397. m_csense.output_pin = p_config->output_pin;
  398. nrf_gpio_cfg_output(m_csense.output_pin);
  399. nrf_gpio_pin_set(m_csense.output_pin);
  400. #endif //COMP_PRESENT
  401. m_csense.event_handler = event_handler;
  402. #if USE_COMP
  403. err_code = comp_init();
  404. if (err_code != NRF_SUCCESS)
  405. {
  406. return err_code;
  407. }
  408. err_code = timer_init();
  409. if (err_code != NRF_SUCCESS)
  410. {
  411. return err_code;
  412. }
  413. err_code = ppi_init();
  414. if (err_code != NRF_SUCCESS)
  415. {
  416. return err_code;
  417. }
  418. #else
  419. #ifdef ADC_PRESENT
  420. err_code = adc_init();
  421. if (err_code != NRF_SUCCESS)
  422. {
  423. return err_code;
  424. }
  425. #elif defined(SAADC_PRESENT)
  426. err_code = saadc_init();
  427. if (err_code != NRF_SUCCESS)
  428. {
  429. return err_code;
  430. }
  431. #endif //ADC_PRESENT
  432. #endif //USE_COMP
  433. m_csense.module_state = NRFX_DRV_STATE_INITIALIZED;
  434. return NRF_SUCCESS;
  435. }
  436. ret_code_t nrf_drv_csense_uninit(void)
  437. {
  438. ASSERT(m_csense.module_state != NRFX_DRV_STATE_UNINITIALIZED);
  439. nrf_drv_csense_channels_disable(0xFF);
  440. #if USE_COMP
  441. ret_code_t err_code;
  442. uint8_t i;
  443. nrf_drv_timer_uninit(&m_timer0);
  444. nrf_drv_timer_uninit(&m_timer1);
  445. nrf_drv_comp_uninit();
  446. for (i =0; i < 3; i++)
  447. {
  448. err_code = nrf_drv_ppi_channel_free(m_ppi_channels[i]);
  449. if (err_code != NRF_SUCCESS)
  450. {
  451. return err_code;
  452. }
  453. }
  454. err_code = nrf_drv_ppi_uninit();
  455. if (err_code != NRF_SUCCESS)
  456. {
  457. return err_code;
  458. }
  459. #else
  460. #ifdef ADC_PRESENT
  461. nrfx_adc_uninit();
  462. #elif defined(SAADC_PRESENT)
  463. nrf_drv_saadc_uninit();
  464. #endif //ADC_PRESENT
  465. #endif //USE_COMP
  466. m_csense.module_state = NRFX_DRV_STATE_UNINITIALIZED;
  467. memset((void*)&m_csense, 0, sizeof(m_csense));
  468. return NRF_SUCCESS;
  469. }
  470. void nrf_drv_csense_channels_enable(uint8_t channels_mask)
  471. {
  472. ASSERT(m_csense.module_state != NRFX_DRV_STATE_UNINITIALIZED);
  473. m_csense.busy = true;
  474. m_csense.module_state = NRFX_DRV_STATE_POWERED_ON;
  475. m_csense.adc_channels_input_mask |= channels_mask;
  476. m_csense.busy = false;
  477. }
  478. void nrf_drv_csense_channels_disable(uint8_t channels_mask)
  479. {
  480. ASSERT(m_csense.module_state == NRFX_DRV_STATE_POWERED_ON);
  481. m_csense.adc_channels_input_mask &= ~channels_mask;
  482. if (m_csense.adc_channels_input_mask == 0)
  483. {
  484. m_csense.module_state = NRFX_DRV_STATE_INITIALIZED;
  485. }
  486. }
  487. uint16_t nrf_drv_csense_channel_read(uint8_t csense_channel)
  488. {
  489. return m_csense.analog_values[csense_channel];
  490. }
  491. ret_code_t nrf_drv_csense_sample(void)
  492. {
  493. ASSERT(m_csense.module_state == NRFX_DRV_STATE_POWERED_ON);
  494. if (m_csense.adc_channels_input_mask != 0)
  495. {
  496. if (m_csense.channels_to_read == 0)
  497. {
  498. #if USE_COMP == 0 && defined(SAADC_PRESENT)
  499. nrf_saadc_enable();
  500. #endif
  501. if (nrf_drv_csense_is_busy() == true)
  502. {
  503. return NRF_ERROR_BUSY;
  504. }
  505. m_csense.busy = true;
  506. m_csense.channels_to_read = m_csense.adc_channels_input_mask;
  507. calculate_next_channel();
  508. }
  509. #if USE_COMP
  510. if (!m_csense.timers_powered_on)
  511. {
  512. nrf_drv_timer_enable(&m_timer0);
  513. nrf_drv_timer_enable(&m_timer1);
  514. m_csense.timers_powered_on = true;
  515. }
  516. else
  517. {
  518. nrf_drv_timer_resume(&m_timer0);
  519. nrf_drv_timer_resume(&m_timer1);
  520. }
  521. nrf_drv_comp_pin_select((nrf_comp_input_t)m_csense.cur_chann_idx);
  522. nrf_drv_comp_start(0, 0);
  523. #else
  524. ret_code_t err_code;
  525. #ifdef ADC_PRESENT
  526. adc_channel.config.config.ain = (nrf_adc_config_input_t)(1<<m_csense.cur_chann_idx);
  527. nrf_gpio_pin_clear(m_csense.output_pin);
  528. err_code = nrfx_adc_sample_convert(&adc_channel, NULL);
  529. #elif defined(SAADC_PRESENT)
  530. saadc_channel.pin_p = (nrf_saadc_input_t)(m_csense.cur_chann_idx + 1);
  531. nrf_saadc_channel_input_set(0, saadc_channel.pin_p, NRF_SAADC_INPUT_DISABLED);
  532. nrf_gpio_pin_clear(m_csense.output_pin);
  533. err_code = nrf_drv_saadc_sample();
  534. #endif //ADC_PRESENT
  535. if (err_code != NRF_SUCCESS)
  536. {
  537. return err_code;
  538. }
  539. #endif //USE_COMP
  540. }
  541. return NRF_SUCCESS;
  542. }
  543. bool nrf_drv_csense_is_busy(void)
  544. {
  545. return m_csense.busy;
  546. }
  547. #endif //NRF_MODULE_ENABLED(NRF_DRV_CSENSE)