drv_dac.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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-03-04 stevetong459 first version
  9. * 2022-07-15 Aligagago add apm32F4 serie MCU support
  10. */
  11. #include <board.h>
  12. #if defined(BSP_USING_DAC1)
  13. #define LOG_TAG "drv.dac"
  14. #define DBG_LVL DBG_INFO
  15. #include <rtdbg.h>
  16. struct apm32_dac
  17. {
  18. const char *name;
  19. DAC_T *dac;
  20. DAC_Config_T dac_conf;
  21. struct rt_dac_device dac_dev;
  22. };
  23. static struct apm32_dac dac_config[] =
  24. {
  25. #if defined(BSP_USING_DAC1)
  26. {
  27. "dac1",
  28. DAC,
  29. {
  30. DAC_TRIGGER_SOFT,
  31. DAC_OUTPUT_BUFFER_DISABLE,
  32. DAC_WAVE_GENERATION_NONE,
  33. DAC_TRIANGLE_AMPLITUDE_4095,
  34. },
  35. RT_NULL
  36. }
  37. #endif
  38. };
  39. /**
  40. * @brief This function will enable dac.
  41. *
  42. * @param device is a pointer to dac device.
  43. *
  44. * @param channel is the dac channel.
  45. *
  46. * @return RT_EOK indicates successful enable dac, other value indicates failed.
  47. */
  48. static rt_err_t _dac_enabled(struct rt_dac_device *device, rt_uint32_t channel)
  49. {
  50. GPIO_Config_T GPIO_ConfigStruct;
  51. struct apm32_dac *cfg = (struct apm32_dac *)device->parent.user_data;
  52. #ifdef APM32F10X_HD
  53. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOA);
  54. GPIO_ConfigStruct.mode = GPIO_MODE_ANALOG;
  55. #elif APM32F40X
  56. RCM_EnableAHB1PeriphClock(RCM_AHB1_PERIPH_GPIOA);
  57. GPIO_ConfigStruct.mode = GPIO_MODE_AN;
  58. #endif
  59. if (channel == 1)
  60. {
  61. GPIO_ConfigStruct.pin = GPIO_PIN_4;
  62. GPIO_Config(GPIOA, &GPIO_ConfigStruct);
  63. DAC_Config(DAC_CHANNEL_1, &cfg->dac_conf);
  64. DAC_Enable(DAC_CHANNEL_1);
  65. }
  66. else if (channel == 2)
  67. {
  68. GPIO_ConfigStruct.pin = GPIO_PIN_5;
  69. GPIO_Config(GPIOA, &GPIO_ConfigStruct);
  70. DAC_Config(DAC_CHANNEL_2, &cfg->dac_conf);
  71. DAC_Enable(DAC_CHANNEL_2);
  72. }
  73. else
  74. {
  75. LOG_E("dac channel must be 1 or 2.");
  76. return -RT_ERROR;
  77. }
  78. return RT_EOK;
  79. }
  80. /**
  81. * @brief This function will disable dac.
  82. *
  83. * @param device is a pointer to dac device.
  84. *
  85. * @param channel is the dac channel.
  86. *
  87. * @return RT_EOK indicates successful disable dac, other value indicates failed.
  88. */
  89. static rt_err_t _dac_disabled(struct rt_dac_device *device, rt_uint32_t channel)
  90. {
  91. if (channel == 1)
  92. {
  93. DAC_Disable(DAC_CHANNEL_1);
  94. }
  95. else if (channel == 2)
  96. {
  97. DAC_Disable(DAC_CHANNEL_2);
  98. }
  99. else
  100. {
  101. LOG_E("dac channel must be 1 or 2.");
  102. return -RT_ERROR;
  103. }
  104. return RT_EOK;
  105. }
  106. /**
  107. * @brief This function will set the vaule of dac.
  108. *
  109. * @param device is a pointer to dac device.
  110. *
  111. * @param channel is the dac channel.
  112. *
  113. * @param value is a pointer to dac value to be convert.
  114. *
  115. * @return RT_EOK indicates successful set dac value, other value indicates failed.
  116. */
  117. static rt_err_t _dac_set_value(struct rt_dac_device *device, rt_uint32_t channel, rt_uint32_t *value)
  118. {
  119. if (channel == 1)
  120. {
  121. DAC_ConfigChannel1Data(DAC_ALIGN_12BIT_R, *value);
  122. DAC_EnableSoftwareTrigger(DAC_CHANNEL_1);
  123. }
  124. else if (channel == 2)
  125. {
  126. DAC_ConfigChannel2Data(DAC_ALIGN_12BIT_R, *value);
  127. DAC_EnableSoftwareTrigger(DAC_CHANNEL_2);
  128. }
  129. else
  130. {
  131. LOG_E("dac channel must be 1 or 2.");
  132. return -RT_ERROR;
  133. }
  134. return RT_EOK;
  135. }
  136. static const struct rt_dac_ops _dac_ops =
  137. {
  138. .disabled = _dac_disabled,
  139. .enabled = _dac_enabled,
  140. .convert = _dac_set_value,
  141. };
  142. /**
  143. * @brief DAC initialization function.
  144. *
  145. * @return RT_EOK indicates successful initialization, other value indicates failed;
  146. */
  147. static int rt_hw_dac_init(void)
  148. {
  149. rt_err_t result = RT_EOK;
  150. rt_size_t obj_num = sizeof(dac_config) / sizeof(struct apm32_dac);
  151. rt_uint32_t i = 0;
  152. RCM_EnableAPB1PeriphClock(RCM_APB1_PERIPH_DAC);
  153. for (i = 0; i < obj_num; i++)
  154. {
  155. /* register dac device */
  156. if (rt_hw_dac_register(&dac_config[i].dac_dev, dac_config[i].name, &_dac_ops, dac_config) == RT_EOK)
  157. {
  158. LOG_D("%s init success", dac_config[i].name);
  159. }
  160. else
  161. {
  162. LOG_E("%s init failed", dac_config[i].name);
  163. result = -RT_ERROR;
  164. }
  165. }
  166. return result;
  167. }
  168. INIT_DEVICE_EXPORT(rt_hw_dac_init);
  169. #endif /* BSP_USING_DACX */