drv_dac.c 4.3 KB

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