drv_dac.c 4.1 KB

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