drv_dac.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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-07-28 rtthread qiu first version
  9. */
  10. #include "drv_dac.h"
  11. #include "drv_common.h"
  12. #include <rtthread.h>
  13. #if defined(BSP_USING_DAC1) || defined(BSP_USING_DAC2)
  14. #define LOG_TAG "drv.dac"
  15. #include <drv_log.h>
  16. struct cyp_dac
  17. {
  18. cy_stc_csdidac_config_t cyhal_dac_device;
  19. struct rt_dac_device cyp_dac_device;
  20. char *name;
  21. };
  22. static struct cyp_dac dac_config[] =
  23. {
  24. #ifdef BSP_USING_DAC1
  25. DAC1_CONFIG,
  26. #endif
  27. #ifdef BSP_USING_DAC2
  28. DAC2_CONFIG,
  29. #endif
  30. };
  31. /*get dac channel*/
  32. static rt_uint32_t cyp_dac_get_channel(rt_uint32_t channel)
  33. {
  34. rt_uint32_t cyp_dac_channel = 0;
  35. switch (channel)
  36. {
  37. case 1:
  38. cyp_dac_channel = CY_CSDIDAC_A;
  39. break;
  40. case 2:
  41. cyp_dac_channel = CY_CSDIDAC_B;
  42. break;
  43. default:
  44. RT_ASSERT(0);
  45. break;
  46. }
  47. return cyp_dac_channel;
  48. }
  49. struct cyp_dac cyp_adc_obj[sizeof(dac_config) / sizeof(dac_config[0])];
  50. cy_stc_csdidac_context_t csdidac_context;
  51. /*dac device enable*/
  52. static rt_err_t cyp_dac_enabled(struct rt_dac_device *device, rt_uint32_t channel)
  53. {
  54. cy_rslt_t result;
  55. rt_uint32_t cyp_channel;
  56. RT_ASSERT(device != RT_NULL);
  57. cyhal_dac_t *dac_device;
  58. dac_device = device->parent.user_data;
  59. /* get current dac channel*/
  60. cyp_channel = cyp_dac_get_channel(channel);
  61. /*DAC device init*/
  62. result = Cy_CSDIDAC_Init(&CSDIDAC_csdidac_config, &csdidac_context);
  63. if (result != RT_EOK)
  64. {
  65. LOG_E("Cy_CSDIDAC_Init fail = %d\n", result);
  66. return -RT_ENOSYS;
  67. }
  68. return RT_EOK;
  69. }
  70. /*dac device disable*/
  71. static rt_err_t cyp_dac_disable(struct rt_dac_device *device, rt_uint32_t channel)
  72. {
  73. rt_uint32_t cyp_channel;
  74. cy_rslt_t result;
  75. RT_ASSERT(device != RT_NULL);
  76. cyhal_dac_t *dac_device;
  77. dac_device = device->parent.user_data;
  78. cyp_channel = cyp_dac_get_channel(channel);
  79. /*DAC free device*/
  80. result = Cy_CSDIDAC_OutputDisable(cyp_channel, &csdidac_context);
  81. if (result != RT_EOK)
  82. {
  83. LOG_E("DAC Outputdisable failed. Error: %d\n", result);
  84. return -RT_ENOSYS;
  85. }
  86. return RT_EOK;
  87. }
  88. /*set dac output value*/
  89. static rt_err_t cyp_adc_convert(struct rt_dac_device *device, rt_uint32_t channel, rt_uint32_t *value)
  90. {
  91. RT_ASSERT(device != RT_NULL);
  92. cy_rslt_t result;
  93. rt_uint32_t cyp_channel;
  94. cyp_channel = cyp_dac_get_channel(channel);
  95. result = Cy_CSDIDAC_OutputEnable(cyp_channel, *value, &csdidac_context);
  96. if (result != RT_EOK)
  97. {
  98. LOG_E("DAC channel initialization failed. Error: %d\n", result);
  99. return -RT_ENOSYS;
  100. }
  101. return RT_EOK;
  102. }
  103. static const struct rt_dac_ops cyp_dac_ops =
  104. {
  105. .disabled = cyp_dac_disable,
  106. .enabled = cyp_dac_enabled,
  107. .convert = cyp_adc_convert,
  108. };
  109. /*dac device init*/
  110. static int rt_hw_dac_init(void)
  111. {
  112. int result = RT_EOK;
  113. /* save dac name */
  114. char name_buf[5] = {'d', 'a', 'c', '0', 0};
  115. int i = 0;
  116. i = sizeof(dac_config) / sizeof(dac_config[0]);
  117. for (i = 0; i < sizeof(dac_config) / sizeof(dac_config[0]); i++)
  118. {
  119. #ifdef BSP_USING_DAC1
  120. name_buf[3] = '1';
  121. #endif
  122. #ifdef BSP_USING_DAC2
  123. name_buf[3] = '2';
  124. #endif
  125. /* register DAC device */
  126. if (rt_hw_dac_register(&cyp_adc_obj[i].cyp_dac_device, name_buf, &cyp_dac_ops, RT_NULL) == RT_EOK)
  127. {
  128. LOG_E("dac device register success\n");
  129. }
  130. else
  131. {
  132. LOG_E("dac device register fail\n");
  133. result = -RT_ERROR;
  134. }
  135. }
  136. return result;
  137. }
  138. INIT_BOARD_EXPORT(rt_hw_dac_init);
  139. #endif /* BSP_USING_DAC1 /BSP_USING_DAC2 */