test_adc.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* Copyright 2020 Canaan Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. #include <rtthread.h>
  16. #include <rtdevice.h>
  17. #include <stdlib.h>
  18. #include "../interdrv/adc/drv_adc.h"
  19. #include "utest.h"
  20. /*
  21. * 测试 ADC 驱动的读操作
  22. * 1. 查找 ADC 设备。
  23. * 2. 启用 ADC 设备。
  24. * 3. 启用每个 ADC 通道并读取其值。
  25. * 4. 验证读取的值在预期范围内。
  26. * 5. 禁用每个 ADC 通道并验证读取值为 0。
  27. *
  28. * 本测试基于 01Studio 开发板,该开发板自带排针,并引出 SoC 的以下 4 个 ADC 通道
  29. * 板级排针编号 | SoC 的 ADC 通道编号 | 输入电压范围
  30. * -------------+---------------------+-------------
  31. * 32 | ADC0 | (0 ~ 3.6V)
  32. * 36 | ADC1 | (0 ~ 3.6V)
  33. * 38 | ADC2 | (0 ~ 1.8V)
  34. * 40 | ADC3 | (0 ~ 1.8V)
  35. * SoC 的 ADC 通道默认只支持最大 1.8V 的输入电压,对于 ADC0 和 ADC1 通道,开发板
  36. * 通过增加功放将最大支持电压提升到 3.6V(而且同样采用了分压机制,导致实际 ADC
  37. * 通道的输入电压只有板级排针电压的一半)。
  38. *
  39. * 测试时注意连接输入的最大电压不要超过额定值,否则可能会损坏 ADC 通道。
  40. *
  41. * 另外注意这个adc 只有 12bit,所以读取的值范围是 0 ~ 4095
  42. *
  43. * 具体测试最大 1.8V 的 ADC 通道(譬如 38/40)时,可以自己通过两个 10K 欧姆的电
  44. * 阻将模拟输入从 3.3V 分压(将可调电阻调制最大时万用表实测 A 点电压为 1.69V 左右):
  45. * +----------+ +---------------+
  46. * 3.3V ----| 10K 欧姆 |----| 可调 10K 欧姆 |---- 接地
  47. * +----------+ +---------------+
  48. * A
  49. * |
  50. * ADC2/ADC3
  51. *
  52. * 具体测试最大 3.6V 的 ADC 通道(譬如 32/36)时,可以直接引入 3.3V。
  53. * +---------------+
  54. * 3.3V --------------------| 可调 10K 欧姆 |---- 接地
  55. * +---------------+
  56. * A
  57. * |
  58. * ADC0/ADC1
  59. */
  60. static void test_read(void)
  61. {
  62. int i;
  63. rt_err_t ret = RT_EOK;
  64. rt_uint32_t value, vol;
  65. rt_adc_device_t adc_dev;
  66. adc_dev = (rt_adc_device_t)rt_device_find(K230_ADC_NAME);
  67. uassert_not_null(adc_dev);
  68. ret = rt_adc_enable(adc_dev, 0);
  69. uassert_int_equal(ret, RT_EOK);
  70. for (i = 0; i < 4; i++)
  71. {
  72. ret = rt_adc_enable(adc_dev, i);
  73. uassert_int_equal(ret, RT_EOK);
  74. value = rt_adc_read(adc_dev, i);
  75. /* 转换为对应电压值,对应 12 位 ADC 最大值 4095, 内部基准最大电压值 1.8V,数据精度乘以 100 保留 2 位小数 */
  76. vol = value * 180 / 4095;
  77. if (i == 0 || i == 1)
  78. vol = vol * 2; /* ADC0/ADC1 分压后实际电压是输入电压的二分之一 */
  79. LOG_I("ADC chan[%d] read value: %d, calculated voltage is: %d.%02dV\n",
  80. i, value, vol / 100, vol % 100);
  81. }
  82. for (i = 0; i < ADC_MAX_CHANNEL; i++)
  83. {
  84. ret = rt_adc_disable(adc_dev, i);
  85. uassert_int_equal(ret, RT_EOK);
  86. value = rt_adc_read(adc_dev, i);
  87. uassert_int_equal(value, 0);
  88. }
  89. return;
  90. }
  91. static rt_err_t utest_tc_init(void)
  92. {
  93. return RT_EOK;
  94. }
  95. static rt_err_t utest_tc_cleanup(void)
  96. {
  97. return RT_EOK;
  98. }
  99. static void testcase(void)
  100. {
  101. UTEST_UNIT_RUN(test_read);
  102. }
  103. UTEST_TC_EXPORT(testcase, "adc", utest_tc_init, utest_tc_cleanup, 100);