drv_adc.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * File : drv_adc.c
  3. * This file is part of GK710X BSP for RT-Thread distribution.
  4. *
  5. * Copyright (c) 2017 GOKE Microelectronics Co., Ltd.
  6. * All rights reserved
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. *
  22. * Visit http://www.goke.com to get contact with Goke.
  23. *
  24. * Change Logs:
  25. * Date Author Notes
  26. */
  27. #include "drv_adc.h"
  28. #ifdef RT_USING_ADC
  29. #include "gtypes.h"
  30. #include "gd_adc.h"
  31. #include "platform.h"
  32. #include <rtdef.h>
  33. #include <rtthread.h>
  34. #define GK_TEST_ADC
  35. #define IOCTL_GET_ADC_DATA 1
  36. #define IOCTL_ADC_POWER_DOWN 0xff
  37. #define ADC_WRAP_BASE (0xf1200000)
  38. #define ADC_IRQn (23)
  39. #define ADC_MAX_CONTROLLER (1)
  40. #define ADC_STATUS_COLESD (0)
  41. #define ADC_STATUS_OPEN (1)
  42. static rt_err_t gk_adc_read_data(struct wrap_adc_obj *adc, rt_uint32_t channel,
  43. rt_uint32_t *buf)
  44. {
  45. rt_err_t ret = RT_EOK;
  46. ret = GD_ADC_Read((GD_HANDLE*)&(adc->handle), buf);
  47. return ret;
  48. }
  49. static rt_err_t gk_adc_init(rt_device_t dev)
  50. {
  51. rt_err_t ret = RT_EOK;
  52. ret = (rt_err_t)GD_ADC_Init();
  53. return ret;
  54. }
  55. static rt_err_t gk_adc_open(rt_device_t dev, rt_uint16_t oflag)
  56. {
  57. rt_err_t ret = RT_EOK;
  58. GD_ADC_OPEN_PARAMS_S openParams;
  59. struct wrap_adc_obj *adc_pri = (struct wrap_adc_obj *)dev->user_data;
  60. rt_memset(&openParams, 0, sizeof(GD_ADC_OPEN_PARAMS_S));
  61. openParams.channel = adc_pri->active_channel_no;
  62. ret = (rt_err_t)GD_ADC_Open(&openParams, (GD_HANDLE*)&(adc_pri->handle));
  63. return ret;
  64. }
  65. static rt_err_t gk_adc_close(rt_device_t dev)
  66. {
  67. rt_err_t ret = RT_EOK;
  68. struct wrap_adc_obj *adc_pri = (struct wrap_adc_obj *)dev->user_data;
  69. ret = (rt_err_t)GD_ADC_Close((GD_HANDLE*)(adc_pri->handle));
  70. return ret;
  71. }
  72. static rt_err_t gk_adc_ioctl(rt_device_t dev, int cmd, void *arg)
  73. {
  74. rt_uint32_t control_reg;
  75. struct wrap_adc_obj *adc_pri = (struct wrap_adc_obj *)dev->user_data;
  76. rt_uint32_t ad_data;
  77. ADC_INFO *adc_info = (ADC_INFO *)arg;
  78. rt_err_t ret;
  79. switch (cmd)
  80. {
  81. case ADC_CMD_READ_RAW_DATA:
  82. ret = gk_adc_read_data(adc_pri, adc_info->channel, &ad_data);
  83. if (ret != RT_EOK)
  84. {
  85. return ret;
  86. }
  87. adc_info->adc_data = ad_data;
  88. break;
  89. case ADC_CMD_DISABLE:
  90. gk_adc_close(dev);
  91. break;
  92. default:
  93. rt_kprintf("wrong para...\n");
  94. return RT_EIO;
  95. }
  96. return RT_EOK;
  97. }
  98. int gk_adc_probe(void *priv_data)
  99. {
  100. rt_device_t adc_dev;
  101. // check if the hw is init already...
  102. // caution this is a read only data...if the driver want to use.malloc and
  103. // copy it..
  104. struct wrap_adc_obj *adc_obj = (struct wrap_adc_obj *)priv_data;
  105. if (adc_obj->init_flag == ADC_INIT_ALREADY) return RT_EFULL;
  106. // malloc a rt device..
  107. adc_dev = RT_KERNEL_MALLOC(sizeof(struct rt_device));
  108. if (!adc_dev)
  109. {
  110. return RT_ENOMEM;
  111. }
  112. rt_memset(adc_dev, 0, sizeof(struct rt_device));
  113. rt_kprintf("id:%d\n", adc_obj->id);
  114. // bind rtdev to obj data...
  115. // caution ...this is used to free mem when exit....
  116. // free step:1:get adc obj...2:free adc_obj->rt_dev->user_data..3:free
  117. // adc_obj->rt_dev 4:adc_obj->rt_dev = NULL
  118. adc_obj->rt_dev = adc_dev;
  119. // malloc a private data adc use only...copy data from platform...
  120. struct wrap_adc_obj *adc_pri =
  121. RT_KERNEL_MALLOC(sizeof(struct wrap_adc_obj));
  122. if (!adc_pri)
  123. {
  124. RT_KERNEL_FREE(adc_dev);
  125. return RT_ENOMEM;
  126. }
  127. // copy platform data to pri data..
  128. rt_memcpy(adc_pri, adc_obj, sizeof(struct wrap_adc_obj));
  129. rt_kprintf("id:%d\n", adc_pri->id);
  130. // bind pri data to rt_adc_dev...
  131. adc_dev->user_data = (void *)adc_pri;
  132. adc_dev->open = gk_adc_open;
  133. adc_dev->close = gk_adc_close;
  134. adc_dev->control = gk_adc_ioctl;
  135. adc_dev->init = gk_adc_init;
  136. adc_dev->type = RT_Device_Class_Miscellaneous;
  137. rt_device_register(adc_dev, "adc", RT_DEVICE_FLAG_RDWR);
  138. adc_obj->init_flag = ADC_INIT_ALREADY;
  139. return RT_EOK;
  140. }
  141. int gk_adc_exit(void *priv_data)
  142. {
  143. struct wrap_adc_obj *adc_obj = (struct wrap_adc_obj *)priv_data;
  144. struct wrap_adc_obj *adc_pri = adc_obj->rt_dev->user_data;
  145. RT_KERNEL_FREE(adc_obj->rt_dev->user_data);
  146. adc_obj->rt_dev->user_data = RT_NULL;
  147. RT_KERNEL_FREE(adc_obj->rt_dev);
  148. adc_obj->rt_dev = RT_NULL;
  149. GD_ADC_Exit();
  150. return 0;
  151. }
  152. struct gk_platform_driver adc_driver_ops = {
  153. .name = "adc", .probe = gk_adc_probe, .remove = gk_adc_exit,
  154. };
  155. void rt_hw_adc_init(void)
  156. {
  157. gk_platform_driver_init(&adc_driver_ops);
  158. }
  159. #ifdef GK_TEST_ADC
  160. int gk_adc_test(void)
  161. {
  162. rt_device_t adc_dev;
  163. ADC_INFO info;
  164. info.channel = 0;
  165. info.adc_data = 0;
  166. adc_dev = rt_device_find("adc");
  167. if (!adc_dev)
  168. {
  169. rt_kprintf("cann't find the adc dev\n");
  170. return -1;
  171. }
  172. adc_dev->init(adc_dev);
  173. adc_dev->open(adc_dev, 0);
  174. while (1)
  175. {
  176. adc_dev->control(adc_dev, ADC_CMD_READ_RAW_DATA, &info);
  177. rt_kprintf("channel:%d, data:0x%x\n", info.channel, info.adc_data);
  178. }
  179. return 0;
  180. }
  181. #endif
  182. #ifdef RT_USING_FINSH
  183. #include <finsh.h>
  184. #ifdef GK_TEST_ADC
  185. FINSH_FUNCTION_EXPORT(gk_adc_test, gk_adc_test);
  186. #endif
  187. #endif
  188. #endif