nrf_adc.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /* Copyright (c) 2014 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */
  12. /**
  13. * @file
  14. * @brief ADC HAL implementation
  15. */
  16. #include "nrf_adc.h"
  17. #ifndef NRF52
  18. /**
  19. * @brief Function for configuring ADC.
  20. *
  21. * This function powers on ADC and configures it. ADC is in DISABLE state after configuration,
  22. * so it should be enabled before using it.
  23. *
  24. * @param[in] config Requested configuration.
  25. */
  26. void nrf_adc_configure(nrf_adc_config_t * config)
  27. {
  28. uint32_t config_reg = 0;
  29. config_reg |= ((uint32_t)config->resolution << ADC_CONFIG_RES_Pos) & ADC_CONFIG_RES_Msk;
  30. config_reg |= ((uint32_t)config->scaling << ADC_CONFIG_INPSEL_Pos) & ADC_CONFIG_INPSEL_Msk;
  31. config_reg |= ((uint32_t)config->reference << ADC_CONFIG_REFSEL_Pos) & ADC_CONFIG_REFSEL_Msk;
  32. if (config->reference & ADC_CONFIG_EXTREFSEL_Msk)
  33. {
  34. config_reg |= config->reference & ADC_CONFIG_EXTREFSEL_Msk;
  35. }
  36. /* select input */
  37. nrf_adc_input_select(NRF_ADC_CONFIG_INPUT_DISABLED);
  38. /* set new configuration keeping selected input */
  39. NRF_ADC->CONFIG = config_reg | (NRF_ADC->CONFIG & ADC_CONFIG_PSEL_Msk);
  40. }
  41. /**
  42. * @brief Blocking function for executing single ADC conversion.
  43. *
  44. * This function selects the desired input, starts a single conversion,
  45. * waits for it to finish, and returns the result.
  46. * ADC is left in STOP state, the given input is selected.
  47. * This function does not check if ADC is initialized and powered.
  48. *
  49. * @param[in] input Requested input to be selected.
  50. *
  51. * @return Conversion result
  52. */
  53. int32_t nrf_adc_convert_single(nrf_adc_config_input_t input)
  54. {
  55. int32_t val;
  56. nrf_adc_input_select(input);
  57. nrf_adc_start();
  58. while (!nrf_adc_conversion_finished())
  59. {
  60. }
  61. nrf_adc_conversion_event_clean();
  62. val = nrf_adc_result_get();
  63. nrf_adc_stop();
  64. return val;
  65. }
  66. #endif