summaryrefslogtreecommitdiff
path: root/bsp/renesas/libraries/HAL_Drivers/drivers/drv_pwm.c
blob: b615f9285ca0f59f21b3885ae87de1ba07832bb1 (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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
 * Copyright (c) 2006-2025, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-10-25     KevinXu      first version
 */

#include "drv_pwm.h"

#ifdef BSP_USING_PWM

/* Declare the control function first */
static rt_err_t drv_pwm_control(struct rt_device_pwm *, int, void *);
static struct rt_pwm_ops drv_ops =
{
    drv_pwm_control
};

static struct ra_pwm ra6m4_pwm_obj[BSP_PWMS_NUM] =
{
#ifdef BSP_USING_PWM0
    [BSP_PWM0_INDEX] = PWM_DRV_INITIALIZER(0),
#endif
#ifdef BSP_USING_PWM1
    [BSP_PWM1_INDEX] = PWM_DRV_INITIALIZER(1),
#endif
#ifdef BSP_USING_PWM2
    [BSP_PWM2_INDEX] = PWM_DRV_INITIALIZER(2),
#endif
#ifdef BSP_USING_PWM3
    [BSP_PWM3_INDEX] = PWM_DRV_INITIALIZER(3),
#endif
#ifdef BSP_USING_PWM4
    [BSP_PWM4_INDEX] = PWM_DRV_INITIALIZER(4),
#endif
#ifdef BSP_USING_PWM5
    [BSP_PWM5_INDEX] = PWM_DRV_INITIALIZER(5),
#endif
#ifdef BSP_USING_PWM6
    [BSP_PWM6_INDEX] = PWM_DRV_INITIALIZER(6),
#endif
#ifdef BSP_USING_PWM7
    [BSP_PWM7_INDEX] = PWM_DRV_INITIALIZER(7),
#endif
#ifdef BSP_USING_PWM8
    [BSP_PWM8_INDEX] = PWM_DRV_INITIALIZER(8),
#endif
#ifdef BSP_USING_PWM9
    [BSP_PWM9_INDEX] = PWM_DRV_INITIALIZER(9),
#endif
#ifdef BSP_USING_PWM10
    [BSP_PWM10_INDEX] = PWM_DRV_INITIALIZER(10),
#endif
#ifdef BSP_USING_PWM11
    [BSP_PWM11_INDEX] = PWM_DRV_INITIALIZER(11),
#endif
#ifdef BSP_USING_PWM12
    [BSP_PWM12_INDEX] = PWM_DRV_INITIALIZER(12),
#endif
#ifdef BSP_USING_PWM13
    [BSP_PWM13_INDEX] = PWM_DRV_INITIALIZER(13),
#endif
};

#define RA_PWM_GTCCRC_INDEX                 2U
#define RA_PWM_GTCCRE_INDEX                 3U
#define RA_PWM_DUTY_MODE_REGISTER           0U
#define RA_PWM_DUTY_MODE_0_PERCENT          2U
#define RA_PWM_DUTY_MODE_100_PERCENT        3U

/* Convert the raw PWM period counts into ns */
static rt_uint32_t _convert_counts_ns(uint32_t clock_frequency, uint32_t raw)
{
    uint32_t ns = (uint32_t)(((uint64_t)raw * 1000000000ULL) / clock_frequency);
    return ns;
}

/* Convert ns into raw PWM period counts */
static rt_uint32_t _convert_ns_counts(uint32_t clock_frequency, uint32_t raw)
{
    uint32_t counts = (uint32_t)(((uint64_t)raw * (uint64_t)clock_frequency) / 1000000000ULL);
    return counts;
}

static rt_err_t drv_pwm_pin_pulse_counts_get(struct ra_pwm *device,
                                              uint32_t period_counts,
                                              gpt_io_pin_t pin,
                                              uint32_t *pulse_counts)
{
    uint32_t duty_mode;
    uint32_t gtior;
    uint32_t compare_index;
    rt_bool_t first_level_low;

    if (pin == GPT_IO_PIN_GTIOCA)
    {
        duty_mode = device->g_ctrl->p_reg->GTUDDTYC_b.OADTY;
        gtior = device->g_ctrl->p_reg->GTIOR_b.GTIOA;
        compare_index = RA_PWM_GTCCRC_INDEX;
    }
    else
    {
        duty_mode = device->g_ctrl->p_reg->GTUDDTYC_b.OBDTY;
        gtior = device->g_ctrl->p_reg->GTIOR_b.GTIOB;
        compare_index = RA_PWM_GTCCRE_INDEX;
    }

    if (duty_mode == RA_PWM_DUTY_MODE_REGISTER)
    {
        *pulse_counts = device->g_ctrl->p_reg->GTCCR[compare_index] + 1U;
        return RT_EOK;
    }
    if (duty_mode != RA_PWM_DUTY_MODE_0_PERCENT &&
        duty_mode != RA_PWM_DUTY_MODE_100_PERCENT)
    {
        return -RT_ERROR;
    }

    /* FSP represents 0% and 100% with a forced output level. */
    first_level_low = (gtior & 0xCU) == 0x4U;
    if ((first_level_low && duty_mode == RA_PWM_DUTY_MODE_0_PERCENT) ||
        (!first_level_low && duty_mode == RA_PWM_DUTY_MODE_100_PERCENT))
    {
        *pulse_counts = period_counts;
    }
    else
    {
        *pulse_counts = 0U;
    }

    return RT_EOK;
}


/* PWM_CMD_ENABLE or PWM_CMD_DISABLE */
static rt_err_t drv_pwm_enable(struct ra_pwm *device,
                               struct rt_pwm_configuration *configuration,
                               rt_bool_t enable)
{
    fsp_err_t err = FSP_SUCCESS;

    if (enable)
    {
        err = R_GPT_Start(device->g_ctrl);
    }
    else
    {
        err = R_GPT_Stop(device->g_ctrl);
    }

    return (err == FSP_SUCCESS) ? RT_EOK : -RT_ERROR;
}

/* PWM_CMD_GET */
static rt_err_t drv_pwm_get(struct ra_pwm *device,
                            struct rt_pwm_configuration *configuration)
{
    timer_info_t info;
    uint32_t period_counts;
    uint32_t pulse_counts_a;
    uint32_t pulse_counts_b;

    if (device->g_cfg->mode != TIMER_MODE_PWM)
    {
        return -RT_ENOSYS;
    }
    if (R_GPT_InfoGet(device->g_ctrl, &info) != FSP_SUCCESS ||
        info.clock_frequency == 0U)
    {
        return -RT_ERROR;
    }

    period_counts = device->g_ctrl->p_reg->GTPBR + 1U;
    if (drv_pwm_pin_pulse_counts_get(device, period_counts,
                                     GPT_IO_PIN_GTIOCA, &pulse_counts_a) != RT_EOK ||
        drv_pwm_pin_pulse_counts_get(device, period_counts,
                                     GPT_IO_PIN_GTIOCB, &pulse_counts_b) != RT_EOK ||
        pulse_counts_a != pulse_counts_b)
    {
        return -RT_ERROR;
    }

    configuration->pulse =
        _convert_counts_ns(info.clock_frequency, pulse_counts_a);
    configuration->period =
        _convert_counts_ns(info.clock_frequency, period_counts);
    configuration->channel = device->g_cfg->channel;

    return RT_EOK;
}

/* PWM_CMD_SET */
static rt_err_t drv_pwm_set(struct ra_pwm *device,
                            struct rt_pwm_configuration *conf)
{
    timer_info_t info;
    uint32_t counts;
    fsp_err_t fsp_erra;
    fsp_err_t fsp_errb;
    uint32_t pulse;
    uint32_t period;

    if (device->g_cfg->mode != TIMER_MODE_PWM)
    {
        return -RT_ENOSYS;
    }
    if (R_GPT_InfoGet(device->g_ctrl, &info) != FSP_SUCCESS ||
        info.clock_frequency == 0U)
    {
        return -RT_ERROR;
    }

    /* Pulse cannot last longer than period. */
    period = conf->period;
    pulse = (period >= conf->pulse) ? conf->pulse : period;

    /* Not to set period again if it's not changed. */
    if (period != _convert_counts_ns(info.clock_frequency,
                                     device->g_ctrl->p_reg->GTPBR + 1U))
    {
        counts = _convert_ns_counts(info.clock_frequency, period);
        fsp_erra = R_GPT_PeriodSet(device->g_ctrl, counts);
        if (fsp_erra != FSP_SUCCESS)
        {
            return -RT_ERROR;
        }
    }

    /* Two pins of a channel will not be separated. */
    counts = _convert_ns_counts(info.clock_frequency, pulse);
    fsp_erra = R_GPT_DutyCycleSet(device->g_ctrl, counts, GPT_IO_PIN_GTIOCA);
    fsp_errb = R_GPT_DutyCycleSet(device->g_ctrl, counts, GPT_IO_PIN_GTIOCB);
    if (fsp_erra != FSP_SUCCESS || fsp_errb != FSP_SUCCESS)
    {
        return -RT_ERROR;
    }

    return RT_EOK;
}

/**
 * Implement of control method in struct rt_pwm_ops.
 */
static rt_err_t drv_pwm_control(struct rt_device_pwm *device, int cmd, void *arg)
{
    struct rt_pwm_configuration *configuration = (struct rt_pwm_configuration *)arg;
    struct ra_pwm *pwm_device = (struct ra_pwm *)device->parent.user_data;

    /**
     * There's actually only one GPT timer with 10 channels. In this case, the
     * timer is separated into 10 PWM devices, so each device has only one
     * channel.
     */
    if (configuration->channel != 0)
    {
        return -RT_EINVAL;
    }

    switch (cmd)
    {
    case PWM_CMD_ENABLE:
        return drv_pwm_enable(pwm_device, configuration, RT_TRUE);
    case PWM_CMD_DISABLE:
        return drv_pwm_enable(pwm_device, configuration, RT_FALSE);
    case PWM_CMD_GET:
        return drv_pwm_get(pwm_device, configuration);
    case PWM_CMD_SET:
        return drv_pwm_set(pwm_device, configuration);
    default:
        return -RT_EINVAL;
    }

    return RT_EOK;
}

/**
 * This is to register the PWM device
 *
 * Note that the PWM driver only supports one fixed pin.
 */
int rt_hw_pwm_init(void)
{
    rt_err_t ret = RT_EOK;
    rt_err_t rt_err = RT_EOK;
    fsp_err_t fsp_err = FSP_SUCCESS;

    for (int i = 0; i < BSP_PWMS_NUM; i++)
    {
        fsp_err = R_GPT_Open(ra6m4_pwm_obj[i].g_ctrl,
                             ra6m4_pwm_obj[i].g_cfg);

        rt_err = rt_device_pwm_register(&ra6m4_pwm_obj[i].pwm_device,
                                        ra6m4_pwm_obj[i].name,
                                        &drv_ops,
                                        &ra6m4_pwm_obj[i]);

        if (fsp_err != FSP_SUCCESS || rt_err != RT_EOK)
        {
            ret = -RT_ERROR;
        }
    }

    return ret;
}
INIT_BOARD_EXPORT(rt_hw_pwm_init);
#endif /* BSP_USING_PWM */