1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
/**************************************************************************//**
* @copyright (C) 2020 Nuvoton Technology Corp. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2022-3-16 Wayne First version
*
******************************************************************************/
#include <rtconfig.h>
#if defined(BSP_USING_DAC)
#include <rtdevice.h>
#include "NuMicro.h"
/* Private define ---------------------------------------------------------------*/
enum
{
DAC_START = -1,
#if defined(BSP_USING_DAC0)
DAC0_IDX,
#endif
#if defined(BSP_USING_DAC1)
DAC1_IDX,
#endif
DAC_CNT
};
/* Private Typedef --------------------------------------------------------------*/
struct nu_dac
{
struct rt_dac_device dev;
char *name;
DAC_T *base;
uint32_t chn_msk;
uint32_t max_chn_num;
};
typedef struct nu_dac *nu_dac_t;
/* Private functions ------------------------------------------------------------*/
static rt_err_t nu_dac_enabled(struct rt_dac_device *device, rt_uint32_t channel);
static rt_err_t nu_dac_disabled(struct rt_dac_device *device, rt_uint32_t channel);
static rt_err_t nu_dac_convert(struct rt_dac_device *device, rt_uint32_t channel, rt_uint32_t *value);
/* Public functions ------------------------------------------------------------*/
int rt_hw_dac_init(void);
/* Private variables ------------------------------------------------------------*/
static struct nu_dac nu_dac_arr [] =
{
#if defined(BSP_USING_DAC0)
{
.name = "dac0", .base = DAC0, .chn_msk = 0, .max_chn_num = 1,
},
#endif
#if defined(BSP_USING_DAC1)
{
.name = "dac1", .base = DAC1, .chn_msk = 0, .max_chn_num = 1,
},
#endif
};
/* nu_dac_enabled - Enable DAC engine and wait for ready */
static rt_err_t nu_dac_enabled(struct rt_dac_device *device, rt_uint32_t channel)
{
nu_dac_t psNuDAC = (nu_dac_t)device;
RT_ASSERT(device);
if (channel >= psNuDAC->max_chn_num)
return -(RT_EINVAL);
if (!(psNuDAC->chn_msk & (0x1 << channel)))
{
DAC_Open(psNuDAC->base, channel, DAC_SOFTWARE_TRIGGER);
/* The DAC conversion settling time is 1us */
DAC_SetDelayTime(psNuDAC->base, 1);
DAC_ENABLE_RIGHT_ALIGN(psNuDAC->base);
psNuDAC->chn_msk |= (0x1 << channel);
}
return RT_EOK;
}
static rt_err_t nu_dac_disabled(struct rt_dac_device *device, rt_uint32_t channel)
{
nu_dac_t psNuDAC = (nu_dac_t)device;
RT_ASSERT(device);
if (channel >= psNuDAC->max_chn_num)
return -(RT_EINVAL);
if (psNuDAC->chn_msk & (0x1 << channel))
{
DAC_Close(psNuDAC->base, channel);
psNuDAC->chn_msk &= ~(0x1 << channel);
}
return RT_EOK;
}
static rt_err_t nu_dac_convert(struct rt_dac_device *device, rt_uint32_t channel, rt_uint32_t *value)
{
nu_dac_t psNuDAC = (nu_dac_t)device;
rt_err_t ret = -RT_ERROR;
RT_ASSERT(device);
RT_ASSERT(value);
if (channel >= psNuDAC->max_chn_num)
{
ret = -RT_EINVAL;
goto exit_nu_dac_convert;
}
if (!(psNuDAC->chn_msk & (1 << channel)))
{
goto exit_nu_dac_convert;
}
/* Set DAC 12-bit holding data */
DAC_WRITE_DATA(psNuDAC->base, 0, (uint16_t)*value);
/* Start A/D conversion */
DAC_START_CONV(psNuDAC->base);
ret = RT_EOK;
exit_nu_dac_convert:
return -(ret);
}
static const struct rt_dac_ops nu_dac_ops =
{
.disabled = nu_dac_disabled,
.enabled = nu_dac_enabled,
.convert = nu_dac_convert,
};
int rt_hw_dac_init(void)
{
int i;
rt_err_t result;
for (i = (DAC_START + 1); i < DAC_CNT; i++)
{
result = rt_hw_dac_register(&nu_dac_arr[i].dev, nu_dac_arr[i].name, &nu_dac_ops, NULL);
RT_ASSERT(result == RT_EOK);
}
return 0;
}
INIT_BOARD_EXPORT(rt_hw_dac_init);
#endif //#if defined(BSP_USING_DAC)
|