summaryrefslogtreecommitdiff
path: root/components/drivers/misc/rt_drv_pwm.c
blob: ef08048207d676e1549c95ab38164e779f0f26b3 (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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/*
 * Copyright (c) 2006-2023, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2018-05-07     aozima       the first version
 * 2022-05-14     Stanley Lwin add pwm function
 * 2022-07-25     liYony       fix complementary outputs and add usage information in finsh
 * 2022-08-31     liYony       Add complementary output section to framework for management
 * 2022-09-24     qiyu         Add dead-time and phase configuration
 * 2023-12-23     1ridic       Add second-level command completion
 */

#include <rtdevice.h>

static rt_err_t _pwm_control(rt_device_t dev, int cmd, void *args)
{
    rt_err_t result = RT_EOK;
    struct rt_device_pwm *pwm = (struct rt_device_pwm *)dev;
    struct rt_pwm_configuration *configuration = (struct rt_pwm_configuration *)args;

    switch (cmd)
    {
    case PWMN_CMD_ENABLE:
        configuration->complementary = RT_TRUE;
        break;
    case PWMN_CMD_DISABLE:
        configuration->complementary = RT_FALSE;
        break;
    default:
        if (pwm->ops->control)
            result = pwm->ops->control(pwm, cmd, args);
        break;
    }

    return result;
}

/*
pos: channel
void *buffer: rt_uint32_t pulse[size]
size : number of pulse, only set to sizeof(rt_uint32_t).
*/
static rt_ssize_t _pwm_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
{
    rt_err_t result = RT_EOK;
    struct rt_device_pwm *pwm = (struct rt_device_pwm *)dev;
    rt_uint32_t *pulse = (rt_uint32_t *)buffer;
    struct rt_pwm_configuration configuration = {0};

    configuration.channel = (pos > 0) ? (pos) : (-pos);

    if (pwm->ops->control)
    {
        result = pwm->ops->control(pwm, PWM_CMD_GET,  &configuration);
        if (result != RT_EOK)
        {
            return 0;
        }

        *pulse = configuration.pulse;
    }

    return size;
}

/*
pos: channel
void *buffer: rt_uint32_t pulse[size]
size : number of pulse, only set to sizeof(rt_uint32_t).
*/
static rt_ssize_t _pwm_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
{
    rt_err_t result = RT_EOK;
    struct rt_device_pwm *pwm = (struct rt_device_pwm *)dev;
    rt_uint32_t *pulse = (rt_uint32_t *)buffer;
    struct rt_pwm_configuration configuration = {0};

    configuration.channel = (pos > 0) ? (pos) : (-pos);

    if (pwm->ops->control)
    {
        result = pwm->ops->control(pwm, PWM_CMD_GET, &configuration);
        if (result != RT_EOK)
        {
            return 0;
        }

        configuration.pulse = *pulse;

        result = pwm->ops->control(pwm, PWM_CMD_SET, &configuration);
        if (result != RT_EOK)
        {
            return 0;
        }
    }

    return size;
}

#ifdef RT_USING_DEVICE_OPS
static const struct rt_device_ops pwm_device_ops =
{
    RT_NULL,
    RT_NULL,
    RT_NULL,
    _pwm_read,
    _pwm_write,
    _pwm_control
};
#endif /* RT_USING_DEVICE_OPS */

rt_err_t rt_device_pwm_register(struct rt_device_pwm *device, const char *name, const struct rt_pwm_ops *ops, const void *user_data)
{
    rt_err_t result = RT_EOK;

#ifndef RT_USING_DM
    rt_memset(device, 0, sizeof(struct rt_device_pwm));
#endif

#ifdef RT_USING_DEVICE_OPS
    device->parent.ops = &pwm_device_ops;
#else
    device->parent.init = RT_NULL;
    device->parent.open = RT_NULL;
    device->parent.close = RT_NULL;
    device->parent.read  = _pwm_read;
    device->parent.write = _pwm_write;
    device->parent.control = _pwm_control;
#endif /* RT_USING_DEVICE_OPS */

    device->parent.type         = RT_Device_Class_PWM;
    device->ops                 = ops;
    device->parent.user_data    = (void *)user_data;

    result = rt_device_register(&device->parent, name, RT_DEVICE_FLAG_RDWR);

    return result;
}

rt_err_t rt_pwm_enable(struct rt_device_pwm *device, int channel)
{
    rt_err_t result = RT_EOK;
    struct rt_pwm_configuration configuration = {0};

    if (!device)
    {
        return -RT_EIO;
    }

    /* Make it is positive num forever */
    configuration.channel = (channel > 0) ? (channel) : (-channel);

    /* If channel is a positive number (0 ~ n), it means using normal output pin.
     * If channel is a negative number (0 ~ -n), it means using complementary output pin. */
    if (channel > 0)
    {
        result = rt_device_control(&device->parent, PWMN_CMD_DISABLE, &configuration);
    }
    else
    {
        result = rt_device_control(&device->parent, PWMN_CMD_ENABLE, &configuration);
    }

    result = rt_device_control(&device->parent, PWM_CMD_ENABLE, &configuration);

    return result;
}

rt_err_t rt_pwm_disable(struct rt_device_pwm *device, int channel)
{
    rt_err_t result = RT_EOK;
    struct rt_pwm_configuration configuration = {0};

    if (!device)
    {
        return -RT_EIO;
    }

    /* Make it is positive num forever */
    configuration.channel = (channel > 0) ? (channel) : (-channel);

    /* If channel is a positive number (0 ~ n), it means using normal output pin.
     * If channel is a negative number (0 ~ -n), it means using complementary output pin. */
    if (channel > 0)
    {
        result = rt_device_control(&device->parent, PWMN_CMD_DISABLE, &configuration);
    }
    else
    {
        result = rt_device_control(&device->parent, PWMN_CMD_ENABLE, &configuration);
    }

    result = rt_device_control(&device->parent, PWM_CMD_DISABLE, &configuration);

    return result;
}

rt_err_t rt_pwm_set(struct rt_device_pwm *device, int channel, rt_uint32_t period, rt_uint32_t pulse)
{
    rt_err_t result = RT_EOK;
    struct rt_pwm_configuration configuration = {0};

    if (!device)
    {
        return -RT_EIO;
    }

    configuration.channel = (channel > 0) ? (channel) : (-channel);
    configuration.period = period;
    configuration.pulse = pulse;
    result = rt_device_control(&device->parent, PWM_CMD_SET, &configuration);

    return result;
}

rt_err_t rt_pwm_set_period(struct rt_device_pwm *device, int channel, rt_uint32_t period)
{
    rt_err_t result = RT_EOK;
    struct rt_pwm_configuration configuration = {0};

    if (!device)
    {
        return -RT_EIO;
    }

    configuration.channel = (channel > 0) ? (channel) : (-channel);
    configuration.period = period;
    result = rt_device_control(&device->parent, PWM_CMD_SET_PERIOD, &configuration);

    return result;
}

rt_err_t rt_pwm_set_pulse(struct rt_device_pwm *device, int channel, rt_uint32_t pulse)
{
    rt_err_t result = RT_EOK;
    struct rt_pwm_configuration configuration = {0};

    if (!device)
    {
        return -RT_EIO;
    }

    configuration.channel = (channel > 0) ? (channel) : (-channel);
    configuration.pulse = pulse;
    result = rt_device_control(&device->parent, PWM_CMD_SET_PULSE, &configuration);

    return result;
}

rt_err_t rt_pwm_set_dead_time(struct rt_device_pwm *device, int channel, rt_uint32_t dead_time)
{
    rt_err_t result = RT_EOK;
    struct rt_pwm_configuration configuration = {0};

    if (!device)
    {
        return -RT_EIO;
    }

    configuration.channel = (channel > 0) ? (channel) : (-channel);
    configuration.dead_time = dead_time;
    result = rt_device_control(&device->parent, PWM_CMD_SET_DEAD_TIME, &configuration);

    return result;
}


rt_err_t rt_pwm_set_phase(struct rt_device_pwm *device, int channel, rt_uint32_t phase)
{
    rt_err_t result = RT_EOK;
    struct rt_pwm_configuration configuration = {0};

    if (!device)
    {
        return -RT_EIO;
    }

    configuration.channel = (channel > 0) ? (channel) : (-channel);
    configuration.phase = phase;
    result = rt_device_control(&device->parent, PWM_CMD_SET_PHASE, &configuration);

    return result;
}

static rt_err_t rt_pwm_get(struct rt_device_pwm *device, struct rt_pwm_configuration *cfg)
{
    rt_err_t result = RT_EOK;

    if (!device)
    {
        return -RT_EIO;
    }

    result = rt_device_control(&device->parent, PWM_CMD_GET, cfg);

    return result;
}

#ifdef RT_USING_FINSH
#include <stdlib.h>
#include <string.h>
#include <finsh.h>

enum pwm_list_parameters
{
    PWM_LIST_PROBE = 1,
    PWM_LIST_ENABLE,
    PWM_LIST_DISABLE,
    PWM_LIST_GET,
    PWM_LIST_SET,
    PWM_LIST_PHASE,
    PWM_LIST_DEAD_TIME,
};

CMD_OPTIONS_STATEMENT(pwm_list)

static int pwm_list(int argc, char **argv)
{
    rt_err_t result = -RT_ERROR;
    char *result_str;
    static struct rt_device_pwm *pwm_device = RT_NULL;
    struct rt_pwm_configuration cfg = {0};

    if (argc > 1)
    {
        if (MSH_OPT_ID_GET(pwm_list) == PWM_LIST_PROBE)
        {
            if (argc == 3)
            {
                pwm_device = (struct rt_device_pwm *)rt_device_find(argv[2]);
                result_str = (pwm_device == RT_NULL) ? "failure" : "success";
                rt_kprintf("probe %s %s\n", argv[2], result_str);
                return (pwm_device == RT_NULL) ? -RT_ERROR : RT_EOK;
            }
            else
            {
                rt_kprintf("pwm probe <device name>                  - probe pwm by name\n");
                return -RT_EINVAL;
            }
        }
        else if (pwm_device == RT_NULL)
        {
            rt_kprintf("Please using 'pwm probe <device name>' first.\n");
            return -RT_ERROR;
        }

        switch (MSH_OPT_ID_GET(pwm_list))
        {
        case PWM_LIST_ENABLE:
            if (argc == 3)
            {
                result = rt_pwm_enable(pwm_device, atoi(argv[2]));
                result_str = (result == RT_EOK) ? "success" : "failure";
                rt_kprintf("%s channel %d is enabled %s \n", pwm_device->parent.parent.name, atoi(argv[2]), result_str);
            }
            else
            {
                rt_kprintf("pwm enable <channel>                     - enable pwm channel\n");
                rt_kprintf("    e.g. MSH >pwm enable  1              - PWM_CH1  nomal\n");
                rt_kprintf("    e.g. MSH >pwm enable -1              - PWM_CH1N complememtary\n");
            }
            break;

        case PWM_LIST_DISABLE:
            if (argc == 3)
            {
                result = rt_pwm_disable(pwm_device, atoi(argv[2]));
            }
            else
            {
                rt_kprintf("pwm disable <channel>                    - disable pwm channel\n");
            }
            break;

        case PWM_LIST_GET:
            cfg.channel = atoi(argv[2]);
            result = rt_pwm_get(pwm_device, &cfg);
            if (result == RT_EOK)
            {
                rt_kprintf("Info of device [%s] channel [%d]:\n", pwm_device, atoi(argv[2]));
                rt_kprintf("period      : %d\n", cfg.period);
                rt_kprintf("pulse       : %d\n", cfg.pulse);
                rt_kprintf("Duty cycle  : %d%%\n", (int)(((double)(cfg.pulse) / (cfg.period)) * 100));
            }
            else
            {
                rt_kprintf("Get info of device: [%s] error.\n", pwm_device);
            }
            break;

        case PWM_LIST_SET:
            if (argc == 5)
            {
                result = rt_pwm_set(pwm_device, atoi(argv[2]), atoi(argv[3]), atoi(argv[4]));
                rt_kprintf("pwm info set on %s at channel %d\n", pwm_device, (rt_base_t)atoi(argv[2]));
            }
            else
            {
                rt_kprintf("Set info of device: [%s] error\n", pwm_device);
                rt_kprintf("Usage: pwm set <channel> <period> <pulse>\n");
            }
            break;

        case PWM_LIST_PHASE:
            if (argc == 4)
            {
                result = rt_pwm_set_phase(pwm_device, atoi(argv[2]), atoi(argv[3]));
                result_str = (result == RT_EOK) ? "success" : "failure";
                rt_kprintf("%s phase is set %d \n", pwm_device->parent.parent.name, (rt_base_t)atoi(argv[3]));
            }
            break;

        case PWM_LIST_DEAD_TIME:
            if (argc == 4)
            {
                result = rt_pwm_set_dead_time(pwm_device, atoi(argv[2]), atoi(argv[3]));
                result_str = (result == RT_EOK) ? "success" : "failure";
                rt_kprintf("%s dead_time is set %d \n", pwm_device->parent.parent.name, (rt_base_t)atoi(argv[3]));
            }
            break;

        default:
            goto _usage;
        }
    }
    else
    {
        goto _usage;
    }
    return result;

_usage:
    rt_kprintf("Usage: \n");
    rt_kprintf("pwm probe      <device name>                - probe pwm by name\n");
    rt_kprintf("pwm enable     <channel>                    - enable pwm channel\n");
    rt_kprintf("pwm disable    <channel>                    - disable pwm channel\n");
    rt_kprintf("pwm get        <channel>                    - get pwm channel info\n");
    rt_kprintf("pwm set        <channel> <period> <pulse>   - set pwm channel info\n");
    rt_kprintf("pwm phase      <channel> <phase>            - set pwm phase\n");
    rt_kprintf("pwm dead_time  <channel> <dead_time>        - set pwm dead time\n");
    result = -RT_ERROR;
    return result;
}
CMD_OPTIONS_NODE_START(pwm_list)
CMD_OPTIONS_NODE(PWM_LIST_PROBE, probe, probe pwm by name)
CMD_OPTIONS_NODE(PWM_LIST_ENABLE, enable, enable pwm channel)
CMD_OPTIONS_NODE(PWM_LIST_DISABLE, disable, disable pwm channel)
CMD_OPTIONS_NODE(PWM_LIST_GET, get, get pwm channel info)
CMD_OPTIONS_NODE(PWM_LIST_SET, set, set pwm channel info)
CMD_OPTIONS_NODE(PWM_LIST_PHASE, phase, set pwm phase)
CMD_OPTIONS_NODE(PWM_LIST_DEAD_TIME, dead_time, set pwm dead time)
CMD_OPTIONS_NODE_END
MSH_CMD_EXPORT_ALIAS(pwm_list, pwm, control pwm device, optenable);

#endif /* RT_USING_FINSH */