drv_dac.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*****************************************************************************
  2. * Copyright (c) 2019, Nations Technologies Inc.
  3. *
  4. * All rights reserved.
  5. * ****************************************************************************
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the disclaimer below.
  12. *
  13. * Nations' name may not be used to endorse or promote products derived from
  14. * this software without specific prior written permission.
  15. *
  16. * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  19. * DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  22. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  25. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. * ****************************************************************************/
  27. /**
  28. * @file drv_dac.c
  29. * @author Nations
  30. * @version v1.0.0
  31. *
  32. * @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
  33. */
  34. #include <rtdbg.h>
  35. #include "drv_dac.h"
  36. #include "board.h"
  37. #ifdef RT_USING_DAC
  38. #if defined(BSP_USING_DAC1) || defined(BSP_USING_DAC2)
  39. /* this driver can be disabled at menuconfig -> Hardware Drivers Config -> On-chip Peripheral Drivers -> Enable DAC */
  40. static struct n32_dac_config dac_config[] =
  41. {
  42. #ifdef BSP_USING_DAC1
  43. {
  44. "dac1",
  45. DAC_CHANNEL_1,
  46. },
  47. #endif
  48. #ifdef BSP_USING_DAC2
  49. {
  50. "dac2",
  51. DAC_CHANNEL_2,
  52. },
  53. #endif
  54. };
  55. static struct n32_dac dac_obj[sizeof(dac_config) / sizeof(dac_config[0])];
  56. static void n32_dac_init(struct n32_dac_config *config)
  57. {
  58. DAC_InitType DAC_InitStructure;
  59. /* DAC Periph clock enable */
  60. RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_DAC, ENABLE);
  61. /* DAC channel Configuration */
  62. DAC_InitStructure.Trigger = DAC_TRG_SOFTWARE;
  63. DAC_InitStructure.WaveGen = DAC_WAVEGEN_NOISE;
  64. DAC_InitStructure.LfsrUnMaskTriAmp = DAC_UNMASK_LFSRBIT0;
  65. DAC_InitStructure.BufferOutput = DAC_BUFFOUTPUT_ENABLE;
  66. DAC_Init(config->dac_periph, &DAC_InitStructure);
  67. }
  68. static rt_err_t n32_dac_enabled(struct rt_dac_device *device, rt_uint32_t channel)
  69. {
  70. RT_ASSERT(device != RT_NULL);
  71. DAC_Enable(channel, ENABLE);
  72. return RT_EOK;
  73. }
  74. static rt_err_t n32_dac_disabled(struct rt_dac_device *device, rt_uint32_t channel)
  75. {
  76. RT_ASSERT(device != RT_NULL);
  77. DAC_Enable(channel, DISABLE);
  78. return RT_EOK;
  79. }
  80. static rt_err_t n32_set_dac_value(struct rt_dac_device *device, rt_uint32_t channel, rt_uint32_t *value)
  81. {
  82. RT_ASSERT(device != RT_NULL);
  83. rt_uint16_t set_value = 0;
  84. set_value = (rt_uint16_t)*value;
  85. if(set_value > 4096)
  86. {
  87. set_value = 4096;
  88. }
  89. /* Start DAC Channel conversion by software */
  90. if(channel == DAC_CHANNEL_1)
  91. {
  92. DAC_SetCh1Data(DAC_ALIGN_R_12BIT, set_value);
  93. DAC_SoftTrgEnable(DAC_CHANNEL_1, ENABLE);
  94. }
  95. else
  96. {
  97. DAC_SetCh2Data(DAC_ALIGN_R_12BIT, set_value);
  98. DAC_SoftTrgEnable(DAC_CHANNEL_2, ENABLE);
  99. }
  100. return RT_EOK;
  101. }
  102. static const struct rt_dac_ops n32_dac_ops =
  103. {
  104. .disabled = n32_dac_disabled,
  105. .enabled = n32_dac_enabled,
  106. .convert = n32_set_dac_value,
  107. };
  108. int rt_hw_dac_init(void)
  109. {
  110. int result = RT_EOK;
  111. /* save dac name */
  112. char name_buf[5] = {'d', 'a', 'c', '0', 0};
  113. int i = 0;
  114. for (i = 0; i < sizeof(dac_config) / sizeof(dac_config[0]); i++)
  115. {
  116. /* dac init */
  117. name_buf[3] = '0';
  118. dac_obj[i].config = &dac_config[i];
  119. #if defined(BSP_USING_DAC1)
  120. if (dac_obj[i].config->dac_periph == DAC_CHANNEL_1)
  121. {
  122. name_buf[3] = '1';
  123. }
  124. GPIOInit(GPIOA, GPIO_Mode_AIN, GPIO_Speed_50MHz, GPIO_PIN_4);
  125. #endif
  126. #if defined(BSP_USING_DAC2)
  127. if (dac_obj[i].config->dac_periph == DAC_CHANNEL_2)
  128. {
  129. name_buf[3] = '2';
  130. }
  131. GPIOInit(GPIOA, GPIO_Mode_AIN, GPIO_Speed_50MHz, GPIO_PIN_5);
  132. #endif
  133. /* register dac device */
  134. for (i = 0; i < sizeof(dac_obj) / sizeof(dac_obj[0]); i++)
  135. {
  136. n32_dac_init(&dac_config[i]);
  137. if (rt_hw_dac_register(&dac_obj[i].dac_device, name_buf, &n32_dac_ops, &dac_obj[i].config->dac_periph) == RT_EOK)
  138. {
  139. LOG_D("%s init success", name_buf);
  140. }
  141. else
  142. {
  143. LOG_E("%s register failed", name_buf);
  144. result = -RT_ERROR;
  145. }
  146. }
  147. }
  148. return result;
  149. }
  150. INIT_DEVICE_EXPORT(rt_hw_dac_init);
  151. #endif /* defined(BSP_USING_DAC1) || defined(BSP_USING_DAC2) */
  152. #endif /* RT_USING_DAC */