summaryrefslogtreecommitdiff
path: root/bsp/Infineon/libraries/HAL_Drivers/drv_dac.c
blob: 609932482a45f04de3f4fbb6b18c30c598e0cdc2 (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
/*
 * Copyright (c) 2006-2024 RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author                 Notes
 * 2022-07-28     rtthread qiu       first version
 */
#include "drv_dac.h"
#include "drv_common.h"
#include <rtthread.h>

#if defined(BSP_USING_DAC1) || defined(BSP_USING_DAC2)

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

struct cyp_dac
{
    cy_stc_csdidac_config_t cyhal_dac_device;
    struct rt_dac_device cyp_dac_device;
    char *name;
};

static struct cyp_dac dac_config[] =
    {
#ifdef BSP_USING_DAC1
        DAC1_CONFIG,
#endif
#ifdef BSP_USING_DAC2
        DAC2_CONFIG,
#endif

};

/*get dac channel*/
static rt_uint32_t cyp_dac_get_channel(rt_uint32_t channel)
{
    rt_uint32_t cyp_dac_channel = 0;

    switch (channel)
    {
    case 1:
        cyp_dac_channel = CY_CSDIDAC_A;
        break;
    case 2:
        cyp_dac_channel = CY_CSDIDAC_B;
        break;
    default:
        RT_ASSERT(0);
        break;
    }

    return cyp_dac_channel;
}

struct cyp_dac cyp_adc_obj[sizeof(dac_config) / sizeof(dac_config[0])];

cy_stc_csdidac_context_t csdidac_context;

/*dac device enable*/
static rt_err_t cyp_dac_enabled(struct rt_dac_device *device, rt_uint32_t channel)
{
    cy_rslt_t result;

    rt_uint32_t cyp_channel;

    RT_ASSERT(device != RT_NULL);

    cyhal_dac_t *dac_device;

    dac_device = device->parent.user_data;

    /* get current dac channel*/
    cyp_channel = cyp_dac_get_channel(channel);

    /*DAC device init*/
    result = Cy_CSDIDAC_Init(&CSDIDAC_csdidac_config, &csdidac_context);

    if (result != RT_EOK)
    {
        LOG_E("Cy_CSDIDAC_Init fail = %d\n", result);
        return -RT_ENOSYS;
    }

    return RT_EOK;
}

/*dac device disable*/
static rt_err_t cyp_dac_disable(struct rt_dac_device *device, rt_uint32_t channel)
{
    rt_uint32_t cyp_channel;

    cy_rslt_t result;

    RT_ASSERT(device != RT_NULL);

    cyhal_dac_t *dac_device;

    dac_device = device->parent.user_data;

    cyp_channel = cyp_dac_get_channel(channel);

    /*DAC free device*/
    result = Cy_CSDIDAC_OutputDisable(cyp_channel, &csdidac_context);
    if (result != RT_EOK)
    {
        LOG_E("DAC Outputdisable failed. Error: %d\n", result);
        return -RT_ENOSYS;
    }
    return RT_EOK;
}

/*set dac output value*/
static rt_err_t cyp_adc_convert(struct rt_dac_device *device, rt_uint32_t channel, rt_uint32_t *value)
{
    RT_ASSERT(device != RT_NULL);

    cy_rslt_t result;

    rt_uint32_t cyp_channel;

    cyp_channel = cyp_dac_get_channel(channel);

    result = Cy_CSDIDAC_OutputEnable(cyp_channel, *value, &csdidac_context);
    if (result != RT_EOK)
    {
        LOG_E("DAC  channel initialization failed. Error: %d\n", result);
        return -RT_ENOSYS;
    }

    return RT_EOK;
}

static const struct rt_dac_ops cyp_dac_ops =
{
    .disabled = cyp_dac_disable,
    .enabled = cyp_dac_enabled,
    .convert = cyp_adc_convert,
};

/*dac device init*/
static int rt_hw_dac_init(void)
{
    int result = RT_EOK;

    /* save dac name */
    char name_buf[5] = {'d', 'a', 'c', '0', 0};

    int i = 0;

    i = sizeof(dac_config) / sizeof(dac_config[0]);

    for (i = 0; i < sizeof(dac_config) / sizeof(dac_config[0]); i++)
    {

#ifdef BSP_USING_DAC1
        name_buf[3] = '1';
#endif

#ifdef BSP_USING_DAC2
        name_buf[3] = '2';
#endif
        /* register DAC device */
        if (rt_hw_dac_register(&cyp_adc_obj[i].cyp_dac_device, name_buf, &cyp_dac_ops, RT_NULL) == RT_EOK)
        {
            LOG_E("dac device register success\n");
        }
        else
        {
            LOG_E("dac device register fail\n");
            result = -RT_ERROR;
        }
    }
    return result;
}

INIT_BOARD_EXPORT(rt_hw_dac_init);

#endif /* BSP_USING_DAC1 /BSP_USING_DAC2 */