1 /*
2 * Copyright (c) 2006-2025, RT-Thread Development Team
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Change Logs:
7 * Date Author Notes
8 * 2021-08-19 Mr.Tiger first version
9 */
10
11 #include <rtthread.h>
12 #include "drv_config.h"
13 #ifdef RT_USING_DAC
14
15 //#define DRV_DEBUG
16 #define DBG_TAG "drv.dac"
17 #ifdef DRV_DEBUG
18 #define DBG_LVL DBG_LOG
19 #else
20 #define DBG_LVL DBG_INFO
21 #endif /* DRV_DEBUG */
22 #include <rtdbg.h>
23
24 struct ra_dac_map ra_dac[] =
25 {
26 #ifdef BSP_USING_DAC0
27 {'0', &g_dac0_cfg, &g_dac0_ctrl},
28 #endif
29 #ifdef BSP_USING_DAC1
30 {'1', &g_dac1_cfg, &g_dac1_ctrl},
31 #endif
32 };
33
34 #ifdef BSP_USING_DAC0
35 struct rt_dac_device dac0_device;
36 struct ra_dac_dev _ra_dac0_device = {.ra_dac_device_t = &dac0_device, .ra_dac_map_dev = &ra_dac[0]};
37 #endif
38
39 #ifdef BSP_USING_DAC1
40 struct rt_dac_device dac1_device;
41 struct ra_dac_dev _ra_dac1_device = {.ra_dac_device_t = &dac1_device, .ra_dac_map_dev = &ra_dac[1]};
42 #endif
43
ra_dac_disabled(struct rt_dac_device * device,rt_uint32_t channel)44 rt_err_t ra_dac_disabled(struct rt_dac_device *device, rt_uint32_t channel)
45 {
46 RT_ASSERT(device != RT_NULL);
47 struct ra_dac_map *dac = (struct ra_dac_map *)device->parent.user_data;
48 if (FSP_SUCCESS != R_DAC_Stop((dac_ctrl_t *)dac->g_ctrl))
49 {
50 LOG_E("dac%c stop failed.", dac->name);
51 return -RT_ERROR;
52 }
53 return RT_EOK;
54 }
55
ra_dac_enabled(struct rt_dac_device * device,rt_uint32_t channel)56 rt_err_t ra_dac_enabled(struct rt_dac_device *device, rt_uint32_t channel)
57 {
58 RT_ASSERT(device != RT_NULL);
59 struct ra_dac_map *dac = (struct ra_dac_map *)device->parent.user_data;
60 if (FSP_SUCCESS != R_DAC_Start((dac_ctrl_t *)dac->g_ctrl))
61 {
62 LOG_E("dac%c start failed.", dac->name);
63 return -RT_ERROR;
64 }
65 return RT_EOK;
66 }
67
ra_dac_write(struct rt_dac_device * device,rt_uint32_t channel,rt_uint32_t * value)68 rt_err_t ra_dac_write(struct rt_dac_device *device, rt_uint32_t channel, rt_uint32_t *value)
69 {
70 RT_ASSERT(device != RT_NULL);
71 struct ra_dac_map *dac = (struct ra_dac_map *)device->parent.user_data;
72 if (FSP_SUCCESS != R_DAC_Write((dac_ctrl_t *)dac->g_ctrl, *value))
73 {
74 LOG_E("dac%c set value failed.", dac->name);
75 return -RT_ERROR;
76 }
77 return RT_EOK;
78 }
79
80 struct rt_dac_ops ra_dac_ops =
81 {
82 .disabled = ra_dac_disabled,
83 .enabled = ra_dac_enabled,
84 .convert = ra_dac_write,
85 };
86
ra_dac_init(void)87 static int ra_dac_init(void)
88 {
89 #ifdef BSP_USING_DAC0
90 _ra_dac0_device.ra_dac_device_t->ops = &ra_dac_ops;
91 R_DAC_Open((dac_ctrl_t *)_ra_dac0_device.ra_dac_map_dev->g_ctrl, (dac_cfg_t const *)_ra_dac0_device.ra_dac_map_dev->g_cfg);
92 if (FSP_SUCCESS != rt_hw_dac_register(_ra_dac0_device.ra_dac_device_t, "dac0", &ra_dac_ops, (void *)_ra_dac0_device.ra_dac_map_dev))
93 {
94 LOG_E("dac0 register failed");
95 return -RT_ERROR;
96 }
97 #endif
98
99 #ifdef BSP_USING_DAC1
100 _ra_dac1_device.ra_dac_device_t->ops = &ra_dac_ops;
101 R_DAC_Open((dac_ctrl_t *)_ra_dac1_device.ra_dac_map_dev->g_ctrl, (dac_cfg_t const *) _ra_dac1_device.ra_dac_map_dev->g_cfg);
102 if (FSP_SUCCESS != rt_hw_dac_register(_ra_dac1_device.ra_dac_device_t, "dac1", &ra_dac_ops, (void *)_ra_dac1_device.ra_dac_map_dev))
103 {
104 LOG_E("dac1 register failed");
105 return -RT_ERROR;
106 }
107 #endif
108
109 return RT_EOK;
110 }
111 INIT_DEVICE_EXPORT(ra_dac_init);
112
113 #endif
114