summaryrefslogtreecommitdiff
path: root/bsp/at32/libraries/rt_drivers/drv_dac.c
blob: dd2313d6c50475de935f104e2d638c84ed98df66 (plain)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
 * Copyright (c) 2006-2023, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2023-04-18     shelton      first version
 * 2024-08-30     shelton      add support m412/416
 */

#include "drv_common.h"

#if defined(BSP_USING_DAC1)
#include "drv_config.h"

//#define DRV_DEBUG
#define LOG_TAG             "drv.dac"
#include <drv_log.h>

struct at32_dac {
    char *name;
    dac_type *dac_x;
    struct rt_dac_device dac_device;
};

enum {
#ifdef BSP_USING_DAC1
    DAC1_INDEX,
#endif
};

static struct at32_dac dac_config[] =
{
#ifdef BSP_USING_DAC1
    DAC1_CONFIG,
#endif
};

static dac_select_type at32_dac_get_channel(rt_uint32_t channel)
{
    dac_select_type at32_channel = DAC1_SELECT;

    switch (channel)
    {
    case 1:
        at32_channel = DAC1_SELECT;
        break;
    case 2:
        at32_channel = DAC2_SELECT;
        break;
    default:
        RT_ASSERT(0);
        break;
    }

    return at32_channel;
}

static rt_err_t at32_dac_enabled(struct rt_dac_device *device, rt_uint32_t channel)
{
    dac_select_type dac_channel;
    dac_type *instance;

    RT_ASSERT(device != RT_NULL);
    instance = device->parent.user_data;
    /* prepare for mult dac instance */
    (void)instance;

    if ((channel <= 2) && (channel > 0))
    {
        /* set at32 dac channel */
        dac_channel =  at32_dac_get_channel(channel);
    }
    else
    {
      LOG_E("dac channel must be 1 or 2.");
      return -RT_ERROR;
    }
#if defined (SOC_SERIES_AT32M412) || defined (SOC_SERIES_AT32M416)
    dac_output_enable(dac_channel, TRUE);
#endif
    dac_enable(dac_channel, TRUE);

    return RT_EOK;
}

static rt_err_t at32_dac_disabled(struct rt_dac_device *device, rt_uint32_t channel)
{
    dac_select_type dac_channel;
    dac_type *instance;

    RT_ASSERT(device != RT_NULL);
    instance = device->parent.user_data;
    /* prepare for mult dac instance */
    (void)instance;

    if ((channel <= 2) && (channel > 0))
    {
        /* set at32 dac channel */
        dac_channel =  at32_dac_get_channel(channel);
    }
    else
    {
      LOG_E("dac channel must be 1 or 2.");
      return -RT_ERROR;
    }
#if defined (SOC_SERIES_AT32M412) || defined (SOC_SERIES_AT32M416)
    dac_output_enable(dac_channel, FALSE);
#endif
    dac_enable(dac_channel, FALSE);

    return RT_EOK;
}

static rt_uint8_t at32_dac_get_resolution(struct rt_dac_device *device)
{
    dac_type *instance;

    RT_ASSERT(device != RT_NULL);
    instance = device->parent.user_data;
    /* prepare for mult dac instance */
    (void)instance;

    /* Only has supported DAC_ALIGN_12B_R, so it will return 12 bits */
    return 12;
}

static rt_err_t at32_set_dac_value(struct rt_dac_device *device, rt_uint32_t channel, rt_uint32_t *value)
{
    dac_select_type dac_channel;
    dac_type *instance;

    RT_ASSERT(device != RT_NULL);
    RT_ASSERT(value != RT_NULL);

    instance = device->parent.user_data;
    /* prepare for mult dac instance */
    (void)instance;

    if ((channel <= 2) && (channel > 0))
    {
        /* set at32 dac channel */
        dac_channel =  at32_dac_get_channel(channel);
    }
    else
    {
        LOG_E("dac channel must be 1 or 2.");
        return -RT_ERROR;
    }
#if !defined (SOC_SERIES_AT32M412) && !defined (SOC_SERIES_AT32M416)
    dac_output_buffer_enable(dac_channel, FALSE);
#endif
    dac_trigger_enable(dac_channel, FALSE);

    /* set dac channel out value*/
    if(dac_channel == DAC1_SELECT)
    {
#if !defined (SOC_SERIES_AT32M412) && !defined (SOC_SERIES_AT32M416)
        dac_1_data_set(DAC1_12BIT_RIGHT, *value);
#else
        /* 6-bit */
        *value = *value >> 6;
        dac_1_data_set(*value);
#endif
    }
    else
    {
#if !defined (SOC_SERIES_AT32M412) && !defined (SOC_SERIES_AT32M416)
        dac_2_data_set(DAC2_12BIT_RIGHT, *value);
#else
        /* 6-bit */
        *value = *value >> 6;
        dac_2_data_set(*value);
#endif
    }

    /* start dac */
    dac_enable(dac_channel, TRUE);

    return RT_EOK;
}

static const struct rt_dac_ops at32_dac_ops =
{
    .disabled = at32_dac_disabled,
    .enabled  = at32_dac_enabled,
    .convert  = at32_set_dac_value,
    .get_resolution = at32_dac_get_resolution,
};

static int at32_dac_init(void)
{
    rt_size_t obj_num;
    int index;

    obj_num = sizeof(dac_config) / sizeof(struct at32_dac);
    rt_err_t result = 0;

    for (index = 0; index < obj_num; index++) {
        at32_msp_dac_init((void *)(dac_config[index].dac_x));
        /* reset dac */
        dac_reset();

        /* register dac device */
        if (rt_hw_dac_register(&dac_config[index].dac_device, dac_config[index].name, &at32_dac_ops, \
            &dac_config[index].dac_x) == RT_EOK)
        {
            LOG_D("%s init success", dac_config[index].name);
        }
        else
        {
            LOG_E("%s register failed", dac_config[index].name);
            result = -RT_ERROR;
        }
    }

    return result;
}

INIT_DEVICE_EXPORT(at32_dac_init);

#endif /* BSP_USING_DAC */