adc.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. * 2018-05-07 aozima the first version
  9. * 2018-11-16 Ernest Chen add finsh command and update adc function
  10. */
  11. #include <rtthread.h>
  12. #include <rtdevice.h>
  13. #include <string.h>
  14. #include <stdlib.h>
  15. #define DBG_TAG "adc"
  16. #define DBG_LVL DBG_INFO
  17. #include <rtdbg.h>
  18. static rt_size_t _adc_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
  19. {
  20. rt_err_t result = RT_EOK;
  21. rt_size_t i;
  22. struct rt_adc_device *adc = (struct rt_adc_device *)dev;
  23. rt_uint32_t *value = (rt_uint32_t *)buffer;
  24. for (i = 0; i < size; i += sizeof(int))
  25. {
  26. result = adc->ops->convert(adc, pos + i, value);
  27. if (result != RT_EOK)
  28. {
  29. return 0;
  30. }
  31. value++;
  32. }
  33. return i;
  34. }
  35. static rt_err_t _adc_control(rt_device_t dev, int cmd, void *args)
  36. {
  37. rt_err_t result = -RT_EINVAL;
  38. rt_adc_device_t adc = (struct rt_adc_device *)dev;
  39. if (cmd == RT_ADC_CMD_ENABLE && adc->ops->enabled)
  40. {
  41. result = adc->ops->enabled(adc, (rt_uint32_t)args, RT_TRUE);
  42. }
  43. else if (cmd == RT_ADC_CMD_DISABLE && adc->ops->enabled)
  44. {
  45. result = adc->ops->enabled(adc, (rt_uint32_t)args, RT_FALSE);
  46. }
  47. else if (cmd == RT_ADC_CMD_GET_RESOLUTION && adc->ops->get_resolution)
  48. {
  49. rt_uint8_t resolution = adc->ops->get_resolution(adc);
  50. if(resolution != 0)
  51. {
  52. *((rt_uint8_t*)args) = resolution;
  53. LOG_D("resolution: %d bits", resolution);
  54. result = RT_EOK;
  55. }
  56. }
  57. return result;
  58. }
  59. #ifdef RT_USING_DEVICE_OPS
  60. const static struct rt_device_ops adc_ops =
  61. {
  62. RT_NULL,
  63. RT_NULL,
  64. RT_NULL,
  65. _adc_read,
  66. RT_NULL,
  67. _adc_control,
  68. };
  69. #endif
  70. rt_err_t rt_hw_adc_register(rt_adc_device_t device, const char *name, const struct rt_adc_ops *ops, const void *user_data)
  71. {
  72. rt_err_t result = RT_EOK;
  73. RT_ASSERT(ops != RT_NULL && ops->convert != RT_NULL);
  74. device->parent.type = RT_Device_Class_ADC;
  75. device->parent.rx_indicate = RT_NULL;
  76. device->parent.tx_complete = RT_NULL;
  77. #ifdef RT_USING_DEVICE_OPS
  78. device->parent.ops = &adc_ops;
  79. #else
  80. device->parent.init = RT_NULL;
  81. device->parent.open = RT_NULL;
  82. device->parent.close = RT_NULL;
  83. device->parent.read = _adc_read;
  84. device->parent.write = RT_NULL;
  85. device->parent.control = _adc_control;
  86. #endif
  87. device->ops = ops;
  88. device->parent.user_data = (void *)user_data;
  89. result = rt_device_register(&device->parent, name, RT_DEVICE_FLAG_RDWR);
  90. return result;
  91. }
  92. rt_uint32_t rt_adc_read(rt_adc_device_t dev, rt_uint32_t channel)
  93. {
  94. rt_uint32_t value;
  95. RT_ASSERT(dev);
  96. dev->ops->convert(dev, channel, &value);
  97. return value;
  98. }
  99. rt_err_t rt_adc_enable(rt_adc_device_t dev, rt_uint32_t channel)
  100. {
  101. rt_err_t result = RT_EOK;
  102. RT_ASSERT(dev);
  103. if (dev->ops->enabled != RT_NULL)
  104. {
  105. result = dev->ops->enabled(dev, channel, RT_TRUE);
  106. }
  107. else
  108. {
  109. result = -RT_ENOSYS;
  110. }
  111. return result;
  112. }
  113. rt_err_t rt_adc_disable(rt_adc_device_t dev, rt_uint32_t channel)
  114. {
  115. rt_err_t result = RT_EOK;
  116. RT_ASSERT(dev);
  117. if (dev->ops->enabled != RT_NULL)
  118. {
  119. result = dev->ops->enabled(dev, channel, RT_FALSE);
  120. }
  121. else
  122. {
  123. result = -RT_ENOSYS;
  124. }
  125. return result;
  126. }
  127. #ifdef RT_USING_FINSH
  128. static int adc(int argc, char **argv)
  129. {
  130. int value = 0;
  131. int result = RT_EOK;
  132. static rt_adc_device_t adc_device = RT_NULL;
  133. char *result_str;
  134. if (argc > 1)
  135. {
  136. if (!strcmp(argv[1], "probe"))
  137. {
  138. if (argc == 3)
  139. {
  140. adc_device = (rt_adc_device_t)rt_device_find(argv[2]);
  141. result_str = (adc_device == RT_NULL) ? "failure" : "success";
  142. rt_kprintf("probe %s %s \n", argv[2], result_str);
  143. }
  144. else
  145. {
  146. rt_kprintf("adc probe <adc_name> - probe adc by name\n");
  147. }
  148. }
  149. else
  150. {
  151. if (adc_device == RT_NULL)
  152. {
  153. rt_kprintf("Please using 'adc probe <adc_name>' first\n");
  154. return -RT_ERROR;
  155. }
  156. if (!strcmp(argv[1], "enable"))
  157. {
  158. if (argc == 3)
  159. {
  160. result = rt_adc_enable(adc_device, atoi(argv[2]));
  161. result_str = (result == RT_EOK) ? "success" : "failure";
  162. rt_kprintf("%s channel %d enables %s \n", adc_device->parent.parent.name, atoi(argv[2]), result_str);
  163. }
  164. else
  165. {
  166. rt_kprintf("adc enable <channel> - enable adc channel\n");
  167. }
  168. }
  169. else if (!strcmp(argv[1], "read"))
  170. {
  171. if (argc == 3)
  172. {
  173. value = rt_adc_read(adc_device, atoi(argv[2]));
  174. rt_kprintf("%s channel %d read value is 0x%08X \n", adc_device->parent.parent.name, atoi(argv[2]), value);
  175. }
  176. else
  177. {
  178. rt_kprintf("adc read <channel> - read adc value on the channel\n");
  179. }
  180. }
  181. else if (!strcmp(argv[1], "disable"))
  182. {
  183. if (argc == 3)
  184. {
  185. result = rt_adc_disable(adc_device, atoi(argv[2]));
  186. result_str = (result == RT_EOK) ? "success" : "failure";
  187. rt_kprintf("%s channel %d disable %s \n", adc_device->parent.parent.name, atoi(argv[2]), result_str);
  188. }
  189. else
  190. {
  191. rt_kprintf("adc disable <channel> - disable adc channel\n");
  192. }
  193. }
  194. else
  195. {
  196. rt_kprintf("Unknown command. Please enter 'adc' for help\n");
  197. }
  198. }
  199. }
  200. else
  201. {
  202. rt_kprintf("Usage: \n");
  203. rt_kprintf("adc probe <adc_name> - probe adc by name\n");
  204. rt_kprintf("adc read <channel> - read adc value on the channel\n");
  205. rt_kprintf("adc disable <channel> - disable adc channel\n");
  206. rt_kprintf("adc enable <channel> - enable adc channel\n");
  207. result = -RT_ERROR;
  208. }
  209. return RT_EOK;
  210. }
  211. MSH_CMD_EXPORT(adc, adc function);
  212. #endif /* RT_USING_FINSH */