drv_adc.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. * Copyright (c) 2006-2022, Synwit Technology Co.,Ltd.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-07-01 lik first version
  9. */
  10. #include "drv_adc.h"
  11. #ifdef RT_USING_ADC
  12. #ifdef BSP_USING_ADC
  13. //#define DRV_DEBUG
  14. #define LOG_TAG "drv.adc"
  15. #include <drv_log.h>
  16. #if !defined(BSP_USING_ADC0) && !defined(BSP_USING_ADC1)
  17. #error "Please define at least one BSP_USING_ADCx"
  18. /* this driver can be disabled at menuconfig ? RT-Thread Components ? Device Drivers */
  19. #endif
  20. #ifdef BSP_USING_ADC0
  21. #ifndef ADC0_CFG
  22. #define ADC0_CFG \
  23. { \
  24. .name = "adc0", \
  25. .ADCx = ADC0, \
  26. .ADC_initstruct.clk_src = ADC_CLKSRC_HRC_DIV8, \
  27. .ADC_initstruct.samplAvg = ADC_AVG_SAMPLE1, \
  28. .ADC_initstruct.EOC_IEn = 0, \
  29. .ADC_initstruct.HalfIEn = 0, \
  30. .ADC_SEQ_initstruct.trig_src = ADC_TRIGGER_SW, \
  31. .ADC_SEQ_initstruct.conv_cnt = 1, \
  32. .ADC_SEQ_initstruct.samp_tim = ADC_SAMPLE_1CLOCK, \
  33. }
  34. #endif /* ADC0_CFG */
  35. #endif /* BSP_USING_ADC0 */
  36. #ifdef BSP_USING_ADC1
  37. #ifndef ADC1_CFG
  38. #define ADC1_CFG \
  39. { \
  40. .name = "adc1", \
  41. .ADCx = ADC1, \
  42. .ADC_initstruct.clk_src = ADC_CLKSRC_HRC_DIV8, \
  43. .ADC_initstruct.samplAvg = ADC_AVG_SAMPLE1, \
  44. .ADC_initstruct.EOC_IEn = 0, \
  45. .ADC_initstruct.HalfIEn = 0, \
  46. .ADC_SEQ_initstruct.trig_src = ADC_TRIGGER_SW, \
  47. .ADC_SEQ_initstruct.conv_cnt = 1, \
  48. .ADC_SEQ_initstruct.samp_tim = ADC_SAMPLE_1CLOCK, \
  49. }
  50. #endif /* ADC1_CFG */
  51. #endif /* BSP_USING_ADC1 */
  52. struct swm_adc_cfg
  53. {
  54. const char *name;
  55. ADC_TypeDef *ADCx;
  56. ADC_InitStructure ADC_initstruct;
  57. ADC_SEQ_InitStructure ADC_SEQ_initstruct;
  58. };
  59. struct swm_adc_device
  60. {
  61. struct swm_adc_cfg *adc_cfg;
  62. struct rt_adc_device adc_device;
  63. };
  64. static struct swm_adc_cfg swm_adc_cfg[] =
  65. {
  66. #ifdef BSP_USING_ADC0
  67. ADC0_CFG,
  68. #endif
  69. #ifdef BSP_USING_ADC1
  70. ADC1_CFG,
  71. #endif
  72. };
  73. static struct swm_adc_device adc_obj[sizeof(swm_adc_cfg) / sizeof(swm_adc_cfg[0])];
  74. static rt_uint32_t swm_adc_get_channel(rt_uint32_t channel)
  75. {
  76. rt_uint32_t swm_channel = 0;
  77. switch (channel)
  78. {
  79. case 0:
  80. swm_channel = ADC_CH0;
  81. break;
  82. case 1:
  83. swm_channel = ADC_CH1;
  84. break;
  85. case 2:
  86. swm_channel = ADC_CH2;
  87. break;
  88. case 3:
  89. swm_channel = ADC_CH3;
  90. break;
  91. case 4:
  92. swm_channel = ADC_CH4;
  93. break;
  94. case 5:
  95. swm_channel = ADC_CH5;
  96. break;
  97. case 6:
  98. swm_channel = ADC_CH6;
  99. break;
  100. case 7:
  101. swm_channel = ADC_CH7;
  102. break;
  103. case 8:
  104. swm_channel = ADC_CH8;
  105. break;
  106. case 9:
  107. swm_channel = ADC_CH9;
  108. break;
  109. case 10:
  110. swm_channel = ADC_CH10;
  111. break;
  112. case 11:
  113. swm_channel = ADC_CH11;
  114. break;
  115. }
  116. return swm_channel;
  117. }
  118. static rt_err_t swm_adc_enabled(struct rt_adc_device *adc_device, rt_uint32_t channel, rt_bool_t enabled)
  119. {
  120. uint32_t adc_chn;
  121. struct swm_adc_cfg *adc_cfg;
  122. RT_ASSERT(adc_device != RT_NULL);
  123. adc_cfg = adc_device->parent.user_data;
  124. if (channel < 12)
  125. {
  126. /* set swm ADC channel */
  127. adc_chn = swm_adc_get_channel(channel);
  128. }
  129. else
  130. {
  131. LOG_E("ADC channel must be between 0 and 11.");
  132. return -RT_ERROR;
  133. }
  134. if (enabled)
  135. {
  136. adc_cfg->ADCx->SEQCHN0 |= adc_chn;
  137. }
  138. else
  139. {
  140. adc_cfg->ADCx->SEQCHN0 &= ~adc_chn;
  141. }
  142. return RT_EOK;
  143. }
  144. static rt_err_t swm_adc_convert(struct rt_adc_device *adc_device, rt_uint32_t channel, rt_uint32_t *value)
  145. {
  146. uint32_t chn, val, adc_chn;
  147. struct swm_adc_cfg *adc_cfg;
  148. RT_ASSERT(adc_device != RT_NULL);
  149. RT_ASSERT(value != RT_NULL);
  150. adc_cfg = adc_device->parent.user_data;
  151. if (channel < 12)
  152. {
  153. /* set swm ADC channel */
  154. adc_chn = swm_adc_get_channel(channel);
  155. }
  156. else
  157. {
  158. LOG_E("ADC channel must be between 0 and 11.");
  159. return -RT_ERROR;
  160. }
  161. *value = 0xFFFFFFFF;
  162. /* start ADC */
  163. ADC_Start(adc_cfg->ADCx, ADC_SEQ0);
  164. /* Wait for the ADC to convert */
  165. while (adc_cfg->ADCx->GO & ADC_GO_BUSY_Msk)
  166. __NOP();
  167. while ((adc_cfg->ADCx->SEQ[0].SR & ADC_SR_EMPTY_Msk) == 0)
  168. {
  169. val = ADC_Read(adc_cfg->ADCx, ADC_SEQ0, &chn);
  170. if (chn == adc_chn)
  171. {
  172. *value = val;
  173. }
  174. }
  175. if (*value == 0xFFFFFFFF)
  176. {
  177. LOG_E("ADC channel can not find.");
  178. return -RT_ERROR;
  179. }
  180. return RT_EOK;
  181. }
  182. static const struct rt_adc_ops swm_adc_ops =
  183. {
  184. .enabled = swm_adc_enabled,
  185. .convert = swm_adc_convert,
  186. };
  187. int swm_adc_init(void)
  188. {
  189. int i = 0;
  190. int result = RT_EOK;
  191. for (i = 0; i < sizeof(swm_adc_cfg) / sizeof(swm_adc_cfg[0]); i++)
  192. {
  193. /* ADC init */
  194. adc_obj[i].adc_cfg = &swm_adc_cfg[i];
  195. if (adc_obj[i].adc_cfg->ADCx == ADC0)
  196. {
  197. #ifdef BSP_USING_ADC0_CHN0
  198. adc_obj[i].adc_cfg->ADC_SEQ_initstruct.channels |= ADC_CH0;
  199. PORT_Init(PORTC, PIN6, PORTC_PIN6_ADC0_CH0, 0); //PC.6 => ADC.CH0
  200. #endif
  201. #ifdef BSP_USING_ADC0_CHN1
  202. adc_obj[i].adc_cfg->ADC_SEQ_initstruct.channels |= ADC_CH1;
  203. PORT_Init(PORTC, PIN5, PORTC_PIN5_ADC0_CH1, 0); //PC.5 => ADC.CH1
  204. #endif
  205. #ifdef BSP_USING_ADC0_CHN2
  206. adc_obj[i].adc_cfg->ADC_SEQ_initstruct.channels |= ADC_CH2;
  207. PORT_Init(PORTC, PIN4, PORTC_PIN4_ADC0_CH2, 0); //PC.4 => ADC.CH2
  208. #endif
  209. #ifdef BSP_USING_ADC0_CHN3
  210. adc_obj[i].adc_cfg->ADC_SEQ_initstruct.channels |= ADC_CH3;
  211. PORT_Init(PORTC, PIN3, PORTC_PIN3_ADC0_CH3, 0); //PC.3 => ADC.CH3
  212. #endif
  213. #ifdef BSP_USING_ADC0_CHN4
  214. adc_obj[i].adc_cfg->ADC_SEQ_initstruct.channels |= ADC_CH4;
  215. PORT_Init(PORTC, PIN2, PORTC_PIN2_ADC0_CH4, 0); //PC.2 => ADC.CH4
  216. #endif
  217. #ifdef BSP_USING_ADC0_CHN5
  218. adc_obj[i].adc_cfg->ADC_SEQ_initstruct.channels |= ADC_CH5;
  219. PORT_Init(PORTC, PIN1, PORTC_PIN1_ADC0_CH5, 0); //PC.1 => ADC.CH5
  220. #endif
  221. #ifdef BSP_USING_ADC0_CHN6
  222. adc_obj[i].adc_cfg->ADC_SEQ_initstruct.channels |= ADC_CH6;
  223. PORT_Init(PORTC, PIN0, PORTC_PIN0_ADC0_CH6, 0); //PC.0 => ADC.CH6
  224. #endif
  225. #ifdef BSP_USING_ADC0_CHN7
  226. adc_obj[i].adc_cfg->ADC_SEQ_initstruct.channels |= ADC_CH7;
  227. PORT_Init(PORTA, PIN15, PORTA_PIN15_ADC0_CH7, 0); //PA.15 => ADC.CH7
  228. #endif
  229. #ifdef BSP_USING_ADC0_CHN8
  230. adc_obj[i].adc_cfg->ADC_SEQ_initstruct.channels |= ADC_CH8;
  231. PORT_Init(PORTA, PIN14, PORTA_PIN14_ADC0_CH8, 0); //PA.14 => ADC.CH8
  232. #endif
  233. #ifdef BSP_USING_ADC0_CHN9
  234. adc_obj[i].adc_cfg->ADC_SEQ_initstruct.channels |= ADC_CH9;
  235. PORT_Init(PORTA, PIN13, PORTA_PIN13_ADC0_CH9, 0); //PA.13 => ADC.CH9
  236. #endif
  237. #ifdef BSP_USING_ADC0_CHN10
  238. adc_obj[i].adc_cfg->ADC_SEQ_initstruct.channels |= ADC_CH10;
  239. PORT_Init(PORTA, PIN12, PORTA_PIN12_ADC0_CH10, 0); //PA.12 => ADC.CH10
  240. #endif
  241. #ifdef BSP_USING_ADC0_CHN11
  242. adc_obj[i].adc_cfg->ADC_SEQ_initstruct.channels |= ADC_CH11;
  243. PORT_Init(PORTA, PIN10, PORTA_PIN10_ADC0_CH11, 0); //PA.10 => ADC.CH11
  244. #endif
  245. }
  246. else if (adc_obj[i].adc_cfg->ADCx == ADC1)
  247. {
  248. #ifdef BSP_USING_ADC1_CHN0
  249. adc_obj[i].adc_cfg->ADC_SEQ_initstruct.channels |= ADC_CH0;
  250. PORT_Init(PORTD, PIN1, PORTD_PIN1_ADC1_CH0, 0); //PD.1 => ADC1.CH0
  251. #endif
  252. #ifdef BSP_USING_ADC1_CHN1
  253. adc_obj[i].adc_cfg->ADC_SEQ_initstruct.channels |= ADC_CH1;
  254. PORT_Init(PORTD, PIN0, PORTD_PIN0_ADC1_CH1, 0); //PD.0 => ADC1.CH1
  255. #endif
  256. #ifdef BSP_USING_ADC1_CHN2
  257. adc_obj[i].adc_cfg->ADC_SEQ_initstruct.channels |= ADC_CH2;
  258. PORT_Init(PORTC, PIN13, PORTC_PIN13_ADC1_CH2, 0); //PC.13 => ADC1.CH2
  259. #endif
  260. #ifdef BSP_USING_ADC1_CHN3
  261. adc_obj[i].adc_cfg->ADC_SEQ_initstruct.channels |= ADC_CH3;
  262. PORT_Init(PORTC, PIN12, PORTC_PIN12_ADC1_CH3, 0); //PC.12 => ADC1.CH3
  263. #endif
  264. #ifdef BSP_USING_ADC1_CHN4
  265. adc_obj[i].adc_cfg->ADC_SEQ_initstruct.channels |= ADC_CH4;
  266. PORT_Init(PORTC, PIN11, PORTC_PIN11_ADC1_CH4, 0); //PC.11 => ADC1.CH4
  267. #endif
  268. #ifdef BSP_USING_ADC1_CHN5
  269. adc_obj[i].adc_cfg->ADC_SEQ_initstruct.channels |= ADC_CH5;
  270. PORT_Init(PORTC, PIN10, PORTC_PIN10_ADC1_CH5, 0); //PC.10 => ADC1.CH5
  271. #endif
  272. #ifdef BSP_USING_ADC1_CHN6
  273. adc_obj[i].adc_cfg->ADC_SEQ_initstruct.channels |= ADC_CH6;
  274. PORT_Init(PORTC, PIN9, PORTC_PIN9_ADC1_CH6, 0); //PC.9 => ADC1.CH6
  275. #endif
  276. }
  277. ADC_Init(adc_obj[i].adc_cfg->ADCx, &(adc_obj[i].adc_cfg->ADC_initstruct));
  278. ADC_SEQ_Init(adc_obj[i].adc_cfg->ADCx, ADC_SEQ0, &(adc_obj[i].adc_cfg->ADC_SEQ_initstruct));
  279. ADC_Open(adc_obj[i].adc_cfg->ADCx);
  280. ADC_Calibrate(adc_obj[i].adc_cfg->ADCx);
  281. result = rt_hw_adc_register(&adc_obj[i].adc_device, adc_obj[i].adc_cfg->name, &swm_adc_ops, adc_obj[i].adc_cfg);
  282. if(result != RT_EOK)
  283. {
  284. LOG_E("%s register fail.", adc_obj[i].adc_cfg->name);
  285. }
  286. else
  287. {
  288. LOG_D("%s register success.", adc_obj[i].adc_cfg->name);
  289. }
  290. }
  291. return result;
  292. }
  293. INIT_BOARD_EXPORT(swm_adc_init);
  294. #endif /* BSP_USING_ADC */
  295. #endif /* RT_USING_ADC */