drv_dac.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2023-04-18 shelton first version
  9. * 2024-08-30 shelton add support m412/416
  10. */
  11. #include "drv_common.h"
  12. #if defined(BSP_USING_DAC1)
  13. #include "drv_config.h"
  14. //#define DRV_DEBUG
  15. #define LOG_TAG "drv.dac"
  16. #include <drv_log.h>
  17. struct at32_dac {
  18. char *name;
  19. dac_type *dac_x;
  20. struct rt_dac_device dac_device;
  21. };
  22. enum {
  23. #ifdef BSP_USING_DAC1
  24. DAC1_INDEX,
  25. #endif
  26. };
  27. static struct at32_dac dac_config[] =
  28. {
  29. #ifdef BSP_USING_DAC1
  30. DAC1_CONFIG,
  31. #endif
  32. };
  33. static dac_select_type at32_dac_get_channel(rt_uint32_t channel)
  34. {
  35. dac_select_type at32_channel = DAC1_SELECT;
  36. switch (channel)
  37. {
  38. case 1:
  39. at32_channel = DAC1_SELECT;
  40. break;
  41. case 2:
  42. at32_channel = DAC2_SELECT;
  43. break;
  44. default:
  45. RT_ASSERT(0);
  46. break;
  47. }
  48. return at32_channel;
  49. }
  50. static rt_err_t at32_dac_enabled(struct rt_dac_device *device, rt_uint32_t channel)
  51. {
  52. dac_select_type dac_channel;
  53. dac_type *instance;
  54. RT_ASSERT(device != RT_NULL);
  55. instance = device->parent.user_data;
  56. /* prepare for mult dac instance */
  57. (void)instance;
  58. if ((channel <= 2) && (channel > 0))
  59. {
  60. /* set at32 dac channel */
  61. dac_channel = at32_dac_get_channel(channel);
  62. }
  63. else
  64. {
  65. LOG_E("dac channel must be 1 or 2.");
  66. return -RT_ERROR;
  67. }
  68. #if defined (SOC_SERIES_AT32M412) || defined (SOC_SERIES_AT32M416)
  69. dac_output_enable(dac_channel, TRUE);
  70. #endif
  71. dac_enable(dac_channel, TRUE);
  72. return RT_EOK;
  73. }
  74. static rt_err_t at32_dac_disabled(struct rt_dac_device *device, rt_uint32_t channel)
  75. {
  76. dac_select_type dac_channel;
  77. dac_type *instance;
  78. RT_ASSERT(device != RT_NULL);
  79. instance = device->parent.user_data;
  80. /* prepare for mult dac instance */
  81. (void)instance;
  82. if ((channel <= 2) && (channel > 0))
  83. {
  84. /* set at32 dac channel */
  85. dac_channel = at32_dac_get_channel(channel);
  86. }
  87. else
  88. {
  89. LOG_E("dac channel must be 1 or 2.");
  90. return -RT_ERROR;
  91. }
  92. #if defined (SOC_SERIES_AT32M412) || defined (SOC_SERIES_AT32M416)
  93. dac_output_enable(dac_channel, FALSE);
  94. #endif
  95. dac_enable(dac_channel, FALSE);
  96. return RT_EOK;
  97. }
  98. static rt_uint8_t at32_dac_get_resolution(struct rt_dac_device *device)
  99. {
  100. dac_type *instance;
  101. RT_ASSERT(device != RT_NULL);
  102. instance = device->parent.user_data;
  103. /* prepare for mult dac instance */
  104. (void)instance;
  105. /* Only has supported DAC_ALIGN_12B_R, so it will return 12 bits */
  106. return 12;
  107. }
  108. static rt_err_t at32_set_dac_value(struct rt_dac_device *device, rt_uint32_t channel, rt_uint32_t *value)
  109. {
  110. dac_select_type dac_channel;
  111. dac_type *instance;
  112. RT_ASSERT(device != RT_NULL);
  113. RT_ASSERT(value != RT_NULL);
  114. instance = device->parent.user_data;
  115. /* prepare for mult dac instance */
  116. (void)instance;
  117. if ((channel <= 2) && (channel > 0))
  118. {
  119. /* set at32 dac channel */
  120. dac_channel = at32_dac_get_channel(channel);
  121. }
  122. else
  123. {
  124. LOG_E("dac channel must be 1 or 2.");
  125. return -RT_ERROR;
  126. }
  127. #if !defined (SOC_SERIES_AT32M412) && !defined (SOC_SERIES_AT32M416)
  128. dac_output_buffer_enable(dac_channel, FALSE);
  129. #endif
  130. dac_trigger_enable(dac_channel, FALSE);
  131. /* set dac channel out value*/
  132. if(dac_channel == DAC1_SELECT)
  133. {
  134. #if !defined (SOC_SERIES_AT32M412) && !defined (SOC_SERIES_AT32M416)
  135. dac_1_data_set(DAC1_12BIT_RIGHT, *value);
  136. #else
  137. /* 6-bit */
  138. *value = *value >> 6;
  139. dac_1_data_set(*value);
  140. #endif
  141. }
  142. else
  143. {
  144. #if !defined (SOC_SERIES_AT32M412) && !defined (SOC_SERIES_AT32M416)
  145. dac_2_data_set(DAC2_12BIT_RIGHT, *value);
  146. #else
  147. /* 6-bit */
  148. *value = *value >> 6;
  149. dac_2_data_set(*value);
  150. #endif
  151. }
  152. /* start dac */
  153. dac_enable(dac_channel, TRUE);
  154. return RT_EOK;
  155. }
  156. static const struct rt_dac_ops at32_dac_ops =
  157. {
  158. .disabled = at32_dac_disabled,
  159. .enabled = at32_dac_enabled,
  160. .convert = at32_set_dac_value,
  161. .get_resolution = at32_dac_get_resolution,
  162. };
  163. static int at32_dac_init(void)
  164. {
  165. rt_size_t obj_num;
  166. int index;
  167. obj_num = sizeof(dac_config) / sizeof(struct at32_dac);
  168. rt_err_t result = 0;
  169. for (index = 0; index < obj_num; index++) {
  170. at32_msp_dac_init((void *)(dac_config[index].dac_x));
  171. /* reset dac */
  172. dac_reset();
  173. /* register dac device */
  174. if (rt_hw_dac_register(&dac_config[index].dac_device, dac_config[index].name, &at32_dac_ops, \
  175. &dac_config[index].dac_x) == RT_EOK)
  176. {
  177. LOG_D("%s init success", dac_config[index].name);
  178. }
  179. else
  180. {
  181. LOG_E("%s register failed", dac_config[index].name);
  182. result = -RT_ERROR;
  183. }
  184. }
  185. return result;
  186. }
  187. INIT_DEVICE_EXPORT(at32_dac_init);
  188. #endif /* BSP_USING_DAC */