drv_dac.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright (c) 2006-2022, Synwit Technology Co.,Ltd.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-02-10 lik first version
  9. */
  10. #include "drv_dac.h"
  11. #ifdef BSP_USING_DAC
  12. //#define DRV_DEBUG
  13. #define LOG_TAG "drv.dac"
  14. #include <drv_log.h>
  15. static struct rt_dac_device swm_dac_device;
  16. static rt_err_t swm_dac_enabled(struct rt_dac_device *device, rt_uint32_t channel)
  17. {
  18. RT_ASSERT(device != RT_NULL);
  19. if (channel != 0)
  20. {
  21. LOG_E("dac channel must be 0.");
  22. return -RT_ERROR;
  23. }
  24. DAC_Open(DAC);
  25. return RT_EOK;
  26. }
  27. static rt_err_t swm_dac_disabled(struct rt_dac_device *device, rt_uint32_t channel)
  28. {
  29. RT_ASSERT(device != RT_NULL);
  30. if (channel != 0)
  31. {
  32. LOG_E("dac channel must be 0.");
  33. return -RT_ERROR;
  34. }
  35. DAC_Close(DAC);
  36. return RT_EOK;
  37. }
  38. static rt_err_t swm_dac_convert(struct rt_dac_device *device, rt_uint32_t channel, rt_uint32_t *value)
  39. {
  40. RT_ASSERT(device != RT_NULL);
  41. RT_ASSERT(value != RT_NULL);
  42. if (channel != 0)
  43. {
  44. LOG_E("dac channel must be 0.");
  45. return -RT_ERROR;
  46. }
  47. DAC->DHR = *value;
  48. while(DAC->SR & DAC_SR_DHRFULL_Msk) __NOP();
  49. return RT_EOK;
  50. }
  51. static const struct rt_dac_ops swm_dac_ops =
  52. {
  53. .disabled = swm_dac_disabled,
  54. .enabled = swm_dac_enabled,
  55. .convert = swm_dac_convert,
  56. };
  57. int swm_dac_init(void)
  58. {
  59. int result = RT_EOK;
  60. PORT_Init(PORTD, PIN2, PORTD_PIN2_DAC_OUT, 0);
  61. DAC_Init(DAC, DAC_FORMAT_LSB12B);
  62. SYS->DACCR &= ~SYS_DACCR_VRADJ_Msk;
  63. SYS->DACCR |= (17 << SYS_DACCR_VRADJ_Pos);
  64. /* register dac device */
  65. result = rt_hw_dac_register(&swm_dac_device, "dac", &swm_dac_ops, RT_NULL);
  66. if(result != RT_EOK)
  67. {
  68. LOG_E("dac register fail.");
  69. }
  70. else
  71. {
  72. LOG_D("dac register success.");
  73. }
  74. return result;
  75. }
  76. INIT_DEVICE_EXPORT(swm_dac_init);
  77. #endif /* BSP_USING_DAC */