drv_adc.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2019-04-20 tyustli the first version.
  9. * 2019-07-15 Magicoe The first version for LPC55S6x
  10. *
  11. */
  12. #include <rtthread.h>
  13. #ifdef BSP_USING_ADC
  14. #if !defined(BSP_USING_ADC0_CH0)
  15. #error "Please define at least one BSP_USING_ADCx_CH0"
  16. #endif
  17. #define LOG_TAG "drv.adc"
  18. #include <drv_log.h>
  19. #include "drv_adc.h"
  20. #include "fsl_power.h"
  21. #include "fsl_lpadc.h"
  22. #include <rtdevice.h>
  23. static rt_err_t lpc_lpadc_enabled(struct rt_adc_device *device, rt_uint32_t channel, rt_bool_t enabled)
  24. {
  25. return RT_EOK;
  26. }
  27. static rt_err_t lpc_lpadc_convert(struct rt_adc_device *device, rt_uint32_t channel, rt_uint32_t *value)
  28. {
  29. lpadc_conv_trigger_config_t mLpadcTriggerConfigStruct;
  30. lpadc_conv_command_config_t mLpadcCommandConfigStruct;
  31. lpadc_conv_result_t mLpadcResultConfigStruct;
  32. ADC_Type *base;
  33. base = (ADC_Type *)(device->parent.user_data);
  34. /* Set conversion CMD configuration. */
  35. LPADC_GetDefaultConvCommandConfig(&mLpadcCommandConfigStruct);
  36. mLpadcCommandConfigStruct.channelNumber = channel;
  37. LPADC_SetConvCommandConfig(base, 1U, &mLpadcCommandConfigStruct);
  38. /* Set trigger configuration. */
  39. LPADC_GetDefaultConvTriggerConfig(&mLpadcTriggerConfigStruct);
  40. mLpadcTriggerConfigStruct.targetCommandId = 1U;
  41. mLpadcTriggerConfigStruct.enableHardwareTrigger = false;
  42. LPADC_SetConvTriggerConfig(base, 0U, &mLpadcTriggerConfigStruct); /* Configurate the trigger0. */
  43. LPADC_DoSoftwareTrigger(base, 1U); /* 1U is trigger0 mask. */
  44. while (!LPADC_GetConvResult(base, &mLpadcResultConfigStruct, 0U));
  45. *value = ((mLpadcResultConfigStruct.convValue) >> 3U);
  46. return RT_EOK;
  47. }
  48. static struct rt_adc_ops lpc_adc_ops =
  49. {
  50. .enabled = lpc_lpadc_enabled,
  51. .convert = lpc_lpadc_convert,
  52. };
  53. #if defined(BSP_USING_ADC0_CH0)
  54. static struct rt_adc_device adc0_device;
  55. #endif /* BSP_USING_ADC0_CH0 */
  56. int rt_hw_adc_init(void)
  57. {
  58. int result = RT_EOK;
  59. #if defined(BSP_USING_ADC0_CH0)
  60. lpadc_config_t mLpadcConfigStruct;
  61. CLOCK_SetClkDiv(kCLOCK_DivAdcAsyncClk, 16U, true);
  62. CLOCK_AttachClk(kMAIN_CLK_to_ADC_CLK);
  63. /* Disable LDOGPADC power down */
  64. POWER_DisablePD(kPDRUNCFG_PD_LDOGPADC);
  65. LPADC_GetDefaultConfig(&mLpadcConfigStruct);
  66. mLpadcConfigStruct.enableAnalogPreliminary = true;
  67. mLpadcConfigStruct.referenceVoltageSource = kLPADC_ReferenceVoltageAlt2;
  68. mLpadcConfigStruct.conversionAverageMode = kLPADC_ConversionAverage128;
  69. LPADC_Init(ADC0, &mLpadcConfigStruct);
  70. /* Request offset calibration. */
  71. //LPADC_DoOffsetCalibration(ADC0);
  72. LPADC_SetOffsetValue(ADC0, 10U, 10U);
  73. /* Request gain calibration. */
  74. LPADC_DoAutoCalibration(ADC0);
  75. result = rt_hw_adc_register(&adc0_device, "adc0", &lpc_adc_ops, ADC0);
  76. if (result != RT_EOK)
  77. {
  78. LOG_E("register adc0 device failed error code = %d\n", result);
  79. }
  80. #endif /* BSP_USING_ADC0_CH0 */
  81. return result;
  82. }
  83. INIT_DEVICE_EXPORT(rt_hw_adc_init);
  84. #endif /* BSP_USING_ADC */