drv_dac.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-10-19 Nations first version
  9. */
  10. #include <rtdbg.h>
  11. #include "drv_dac.h"
  12. #ifdef RT_USING_DAC
  13. #if defined(BSP_USING_DAC) || defined(BSP_USING_DAC1) || defined(BSP_USING_DAC2)
  14. static struct n32_dac_config dac_config[] =
  15. {
  16. #if defined(SOC_N32L43X) || defined(SOC_N32L40X) || defined(SOC_N32G43X)
  17. #ifdef BSP_USING_DAC
  18. {
  19. "dac",
  20. },
  21. #endif
  22. #endif
  23. #ifdef BSP_USING_DAC1
  24. {
  25. "dac1",
  26. DAC_CHANNEL_1,
  27. },
  28. #endif
  29. #ifdef BSP_USING_DAC2
  30. {
  31. "dac2",
  32. DAC_CHANNEL_2,
  33. },
  34. #endif
  35. };
  36. static struct n32_dac dac_obj[sizeof(dac_config) / sizeof(dac_config[0])];
  37. static void n32_dac_init(struct n32_dac_config *config)
  38. {
  39. DAC_InitType DAC_InitStructure;
  40. /* DAC Periph clock enable */
  41. RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_DAC, ENABLE);
  42. /* DAC channel Configuration */
  43. DAC_InitStructure.Trigger = DAC_TRG_SOFTWARE;
  44. DAC_InitStructure.WaveGen = DAC_WAVEGEN_NOISE;
  45. DAC_InitStructure.LfsrUnMaskTriAmp = DAC_UNMASK_LFSRBIT0;
  46. DAC_InitStructure.BufferOutput = DAC_BUFFOUTPUT_ENABLE;
  47. #if defined(SOC_N32G45X) || defined(SOC_N32WB452) || defined(SOC_N32G4FR)
  48. DAC_Init(config->dac_periph, &DAC_InitStructure);
  49. #elif defined(SOC_N32L43X) || defined(SOC_N32L40X) || defined(SOC_N32G43X)
  50. DAC_Init(&DAC_InitStructure);
  51. /* Enable DAC */
  52. DAC_Enable(ENABLE);
  53. /* Set DAC Channel DR12CH register */
  54. DAC_SetChData(DAC_ALIGN_R_12BIT, 4094);
  55. #endif
  56. }
  57. static rt_err_t n32_dac_enabled(struct rt_dac_device *device, rt_uint32_t channel)
  58. {
  59. RT_ASSERT(device != RT_NULL);
  60. #if defined(SOC_N32G45X) || defined(SOC_N32WB452) || defined(SOC_N32G4FR)
  61. DAC_Enable(channel, ENABLE);
  62. #elif defined(SOC_N32L43X) || defined(SOC_N32L40X) || defined(SOC_N32G43X)
  63. DAC_Enable(ENABLE);
  64. #endif
  65. return RT_EOK;
  66. }
  67. static rt_err_t n32_dac_disabled(struct rt_dac_device *device, rt_uint32_t channel)
  68. {
  69. RT_ASSERT(device != RT_NULL);
  70. #if defined(SOC_N32G45X) || defined(SOC_N32WB452) || defined(SOC_N32G4FR)
  71. DAC_Enable(channel, DISABLE);
  72. #elif defined(SOC_N32L43X) || defined(SOC_N32L40X) || defined(SOC_N32G43X)
  73. DAC_Enable(DISABLE);
  74. #endif
  75. return RT_EOK;
  76. }
  77. static rt_err_t n32_set_dac_value(struct rt_dac_device *device, rt_uint32_t channel, rt_uint32_t *value)
  78. {
  79. RT_ASSERT(device != RT_NULL);
  80. rt_uint16_t set_value = 0;
  81. set_value = (rt_uint16_t)*value;
  82. #if defined(SOC_N32L43X) || defined(SOC_N32L40X) || defined(SOC_N32G43X)
  83. /* Start DAC Channel conversion by software */
  84. DAC_SoftTrgEnable(ENABLE);
  85. #endif
  86. if (set_value > 4096)
  87. {
  88. set_value = 4096;
  89. }
  90. #if defined(SOC_N32G45X) || defined(SOC_N32WB452) || defined(SOC_N32G4FR)
  91. /* Start DAC Channel conversion by software */
  92. if (channel == DAC_CHANNEL_1)
  93. {
  94. DAC_SetCh1Data(DAC_ALIGN_R_12BIT, set_value);
  95. DAC_SoftTrgEnable(DAC_CHANNEL_1, ENABLE);
  96. }
  97. else
  98. {
  99. DAC_SetCh2Data(DAC_ALIGN_R_12BIT, set_value);
  100. DAC_SoftTrgEnable(DAC_CHANNEL_2, ENABLE);
  101. }
  102. #elif defined(SOC_N32L43X) || defined(SOC_N32L40X) || defined(SOC_N32G43X)
  103. DAC_SetChData(DAC_ALIGN_R_12BIT, set_value);
  104. #endif
  105. return RT_EOK;
  106. }
  107. static const struct rt_dac_ops n32_dac_ops =
  108. {
  109. .disabled = n32_dac_disabled,
  110. .enabled = n32_dac_enabled,
  111. .convert = n32_set_dac_value,
  112. };
  113. int rt_hw_dac_init(void)
  114. {
  115. GPIO_InitType GPIO_InitStructure;
  116. int result = RT_EOK;
  117. /* save dac name */
  118. char name_buf[5] = {'d', 'a', 'c', '0', 0};
  119. int i = 0;
  120. for (i = 0; i < sizeof(dac_config) / sizeof(dac_config[0]); i++)
  121. {
  122. /* dac init */
  123. dac_obj[i].config = &dac_config[i];
  124. #if defined(SOC_N32L43X) || defined(SOC_N32L40X) || defined(SOC_N32G43X)
  125. #if defined(BSP_USING_DAC)
  126. name_buf[3] = '\0';
  127. /* Enable GPIO clock */
  128. RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA, ENABLE);
  129. GPIO_InitStruct(&GPIO_InitStructure);
  130. /* Config DAC chennel */
  131. GPIO_InitStructure.Pin = GPIO_PIN_4;
  132. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Input;
  133. GPIO_InitPeripheral(GPIOA, &GPIO_InitStructure);
  134. #endif
  135. #endif
  136. #ifdef BSP_USING_DAC1
  137. if (dac_obj[i].config->dac_periph == DAC_CHANNEL_1)
  138. {
  139. name_buf[3] = '1';
  140. RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA, ENABLE);
  141. GPIO_InitStruct(&GPIO_InitStructure);
  142. /* Configure PA4 DAC1 */
  143. GPIO_InitStructure.Pin = GPIO_PIN_4;
  144. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
  145. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  146. GPIO_InitPeripheral(GPIOA, &GPIO_InitStructure);
  147. }
  148. #endif
  149. #ifdef BSP_USING_DAC2
  150. if (dac_obj[i].config->dac_periph == DAC_CHANNEL_2)
  151. {
  152. name_buf[3] = '2';
  153. RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA, ENABLE);
  154. GPIO_InitStruct(&GPIO_InitStructure);
  155. /* Configure PA5 DAC1 */
  156. GPIO_InitStructure.Pin = GPIO_PIN_5;
  157. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
  158. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  159. GPIO_InitPeripheral(GPIOA, &GPIO_InitStructure);
  160. }
  161. #endif
  162. /* register dac device */
  163. n32_dac_init(&dac_config[i]);
  164. if (rt_hw_dac_register(&dac_obj[i].dac_device, name_buf, &n32_dac_ops, &dac_obj[i].config->dac_periph) == RT_EOK)
  165. {
  166. LOG_D("%s init success", name_buf);
  167. }
  168. else
  169. {
  170. LOG_E("%s register failed", name_buf);
  171. result = -RT_ERROR;
  172. }
  173. }
  174. return result;
  175. }
  176. INIT_DEVICE_EXPORT(rt_hw_dac_init);
  177. #endif /* defined(BSP_USING_DAC1) || defined(BSP_USING_DAC2) */
  178. #endif /* RT_USING_DAC */