drv_dac.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. * 2021-09-09 WCH the first version
  9. * 2022-09-17 hg0720 add some operation function
  10. */
  11. #include <board.h>
  12. #include <rtthread.h>
  13. #include <rtdevice.h>
  14. #include "drv_dac.h"
  15. #if defined BSP_USING_DAC
  16. //#define DRV_DEBUG
  17. #define LOG_TAG "drv.dac"
  18. #include <drv_log.h>
  19. static DAC_HandleTypeDef dac_config[] =
  20. {
  21. #ifdef BSP_USING_DAC_CHANNEL1
  22. { \
  23. .Instance = DAC, \
  24. .Init.DAC_Trigger = DAC_Trigger_None, \
  25. .Init.DAC_WaveGeneration = DAC_WaveGeneration_None, \
  26. .Init.DAC_LFSRUnmask_TriangleAmplitude = DAC_TriangleAmplitude_4095, \
  27. .Init.DAC_OutputBuffer = DAC_OutputBuffer_Enable, \
  28. .Channel = DAC_Channel_1,
  29. },
  30. #endif
  31. #ifdef BSP_USING_DAC_CHANNEL2
  32. {
  33. .Instance = DAC, \
  34. .Init.DAC_Trigger = DAC_Trigger_None, \
  35. .Init.DAC_WaveGeneration = DAC_WaveGeneration_None, \
  36. .Init.DAC_LFSRUnmask_TriangleAmplitude = DAC_TriangleAmplitude_4095, \
  37. .Init.DAC_OutputBuffer = DAC_OutputBuffer_Enable, \
  38. .Channel = DAC_Channel_2,
  39. }
  40. #endif
  41. };
  42. struct ch32_dac
  43. {
  44. DAC_HandleTypeDef DAC_Handler;
  45. struct rt_dac_device ch32_dac_device;
  46. };
  47. static struct ch32_dac ch32_dac_obj[sizeof(dac_config) / sizeof(dac_config[0])];
  48. static rt_err_t ch32_dac_enabled(struct rt_dac_device *device, rt_uint32_t channel)
  49. {
  50. DAC_HandleTypeDef *ch32_dac_handler;
  51. RT_ASSERT(device != RT_NULL);
  52. ch32_dac_handler = device->parent.user_data;
  53. DAC_Cmd(ch32_dac_handler->Channel, ENABLE);
  54. return RT_EOK;
  55. }
  56. static rt_err_t ch32_dac_disabled(struct rt_dac_device *device, rt_uint32_t channel)
  57. {
  58. DAC_HandleTypeDef *ch32_dac_handler;
  59. RT_ASSERT(device != RT_NULL);
  60. ch32_dac_handler = device->parent.user_data;
  61. DAC_Cmd(ch32_dac_handler->Channel, DISABLE);
  62. return RT_EOK;
  63. }
  64. static rt_uint8_t ch32_dac_get_resolution(struct rt_dac_device *device)
  65. {
  66. DAC_HandleTypeDef *ch32_dac_handler;
  67. RT_ASSERT(device != RT_NULL);
  68. ch32_dac_handler = device->parent.user_data;
  69. (void)ch32_dac_handler;
  70. /* Only has supported DAC_ALIGN_12B_R, so it will return 12 bits */
  71. return 12;
  72. }
  73. static rt_uint32_t ch32_dac_get_channel(rt_uint32_t channel)
  74. {
  75. rt_uint32_t ch32_channel = 0;
  76. switch (channel)
  77. {
  78. case 1:
  79. ch32_channel = DAC_Channel_1;
  80. break;
  81. case 2:
  82. ch32_channel = DAC_Channel_2;
  83. break;
  84. default:
  85. RT_ASSERT(0);
  86. break;
  87. }
  88. return ch32_channel;
  89. }
  90. static rt_err_t ch32_set_dac_value(struct rt_dac_device *device, rt_uint32_t channel, rt_uint32_t *value)
  91. {
  92. rt_uint32_t dac_channel;
  93. DAC_HandleTypeDef *ch32_dac_handler;
  94. RT_ASSERT(device != RT_NULL);
  95. RT_ASSERT(value != RT_NULL);
  96. ch32_dac_handler = device->parent.user_data;
  97. if ((channel <= 2) && (channel > 0))
  98. {
  99. /* set ch32 dac channel */
  100. dac_channel = ch32_dac_get_channel(channel);
  101. }
  102. else
  103. {
  104. LOG_E("dac channel must be 1 or 2.");
  105. return -RT_ERROR;
  106. }
  107. if (channel==1)
  108. {
  109. DAC_SetChannel1Data(DAC_Align_12b_R, *value);
  110. }
  111. else if (channel==2)
  112. {
  113. DAC_SetChannel2Data(DAC_Align_12b_R, *value);
  114. }
  115. return RT_EOK;
  116. }
  117. static const struct rt_dac_ops ch_dac_ops =
  118. {
  119. .disabled = ch32_dac_disabled,
  120. .enabled = ch32_dac_enabled,
  121. .convert = ch32_set_dac_value,
  122. .get_resolution = ch32_dac_get_resolution,
  123. };
  124. static int ch32_dac_init(void)
  125. {
  126. int result = RT_EOK;
  127. /* save dac name */
  128. char name_buf[6] = {'d', 'a', 'c', 'c','0', 0};
  129. int i = 0;
  130. GPIO_InitTypeDef GPIO_InitStructure={0};
  131. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE );
  132. RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE );
  133. for (i = 0; i < sizeof(dac_config) / sizeof(dac_config[0]); i++)
  134. {
  135. /* dac channel init */
  136. name_buf[4] = '0';
  137. ch32_dac_obj[i].DAC_Handler = dac_config[i];
  138. if (ch32_dac_obj[i].DAC_Handler.Channel==DAC_Channel_1)
  139. {
  140. name_buf[4] = '1';
  141. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
  142. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
  143. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  144. GPIO_Init(GPIOA, &GPIO_InitStructure);
  145. }
  146. if (ch32_dac_obj[i].DAC_Handler.Channel==DAC_Channel_2)
  147. {
  148. name_buf[4] = '2';
  149. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
  150. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
  151. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  152. GPIO_Init(GPIOA, &GPIO_InitStructure);
  153. }
  154. /* init dac channelx */
  155. DAC_Init(ch32_dac_obj[i].DAC_Handler.Channel,&ch32_dac_obj[i].DAC_Handler.Init);
  156. /* register dac device */
  157. if (rt_hw_dac_register(&ch32_dac_obj[i].ch32_dac_device, name_buf, &ch_dac_ops, &ch32_dac_obj[i].DAC_Handler) == RT_EOK)
  158. {
  159. LOG_D("%s init success", name_buf);
  160. }
  161. else
  162. {
  163. LOG_E("%s register failed", name_buf);
  164. result = -RT_ERROR;
  165. }
  166. }
  167. return result;
  168. }
  169. INIT_DEVICE_EXPORT(ch32_dac_init);
  170. #endif /* BSP_USING_DAC */