summaryrefslogtreecommitdiff
path: root/bsp/nxp/mcx/mcxn/libraries/drivers/drv_pwm.c
blob: 73752a1dfb7851f0c6d1ff8ff61644365f9041f8 (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
/*
 * Copyright (c) 2006-2024, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2024-02-26     Yilin Sun    Initial version.
 */

#include <rtthread.h>
#include <rtdevice.h>

#include "fsl_ctimer.h"

#define MCX_PWM_MATCH_COUNT 4

#ifdef RT_USING_PWM

typedef struct
{
    struct rt_device_pwm pwm_device;
    CTIMER_Type *ct_instance;
    uint32_t counter_period_ps;
} mcx_pwm_obj_t;

static CTIMER_Type *mcx_pwm_instances[] = CTIMER_BASE_PTRS;

static mcx_pwm_obj_t mcx_pwm_list[ARRAY_SIZE(mcx_pwm_instances)];

static int mcx_pwm_current_period_channel(mcx_pwm_obj_t *pwm)
{
    CTIMER_Type *ct = (CTIMER_Type *)pwm->ct_instance;

    uint32_t mcr = ct->MCR;

    for (uint8_t i = 0; i < 4; i++)
    {
        if (mcr & (1U << (3 * i + CTIMER_MCR_MR0R_SHIFT)))
        {
            return i;
        }
    }

    return -1;
}

static int mcx_pwm_first_free_channel(mcx_pwm_obj_t *pwm)
{
    CTIMER_Type *ct = (CTIMER_Type *)pwm->ct_instance;

    int pc = mcx_pwm_current_period_channel(pwm);

    uint32_t pwmc = ct->PWMC;

    for (uint8_t i = 0; i < 4; i++)
    {
        if (pwmc & (1U << i))
        {
            /* Skip this channel if there's an active PWM output */
            continue;
        }

        if (i == pc)
        {
            /* Skip this channel if this channel is the current period channel */
            continue;
        }

        return i;
    }

    /* There are no free channels left. */
    return -1;
}

static int mcx_pwm_period_set(mcx_pwm_obj_t *pwm, uint32_t period_ns)
{
    CTIMER_Type *ct = (CTIMER_Type *)pwm->ct_instance;

    int p_channel = mcx_pwm_current_period_channel(pwm);
    if (p_channel < 0)
    {
        return -EINVAL;
    }

    /* Store new values in shadow registers */
    ct->MSR[p_channel] = period_ns * 1000 / pwm->counter_period_ps;

    /* Enable period channel interrupt to check a reload event occurs.
     * Since interrupts are not configured from NVIC, so no ISR will occur.
     * Check IR[MRnINT] for reload point.
     */
    uint32_t mcr_mask = (CTIMER_MCR_MR0RL_MASK << p_channel) | (CTIMER_MCR_MR0I_MASK << (3 * p_channel));

    for (uint8_t i = 0; i < 4; i++)
    {
        if (ct->PWMC & (1U << i))
        {
            /* Channel PWM output is enabled, calculate new values and store into shadow registers */
            uint32_t new_mr = ct->MR[i] * ct->MSR[p_channel] / ct->MR[p_channel];
            ct->MSR[i] = new_mr;

            /* Update MRnRL map */
            mcr_mask |= CTIMER_MCR_MR0RL_MASK << i;
        }
    }

    /* Reload MRs on next counter reset, enable reload MR interrupt */
    ct->MCR |= mcr_mask;

    while ((ct->IR & (CTIMER_IR_MR0INT_MASK << p_channel)) == 0U)
    {
        /* -- */
    }

    /* Disable reload channel interrupt and MSR synchronization */
    ct->MCR &= ~mcr_mask;

    /* Clear interrupt flags. */
    ct->IR |= (CTIMER_IR_MR0INT_MASK << p_channel);

    return 0;
}

static int mcx_pwm_period_get(mcx_pwm_obj_t *pwm, uint32_t *period_ns)
{
    CTIMER_Type *ct = (CTIMER_Type *)pwm->ct_instance;

    int p_channel = mcx_pwm_current_period_channel(pwm);
    if (p_channel < 0)
    {
        return -1;
    }

    *period_ns = ct->MR[p_channel] * pwm->counter_period_ps / 1000;

    return 0;
}

static int mcx_pwm_pulse_set(mcx_pwm_obj_t *pwm, uint8_t channel, uint32_t pulse_ns)
{
    CTIMER_Type *ct = (CTIMER_Type *)pwm->ct_instance;

    int p_channel = mcx_pwm_current_period_channel(pwm);
    if (p_channel < 0)
    {
        return -1;
    }

    /* Up-counting counters, the polarity is inversed */
    ct->MSR[channel] = ct->MR[p_channel] - pulse_ns * 1000 / pwm->counter_period_ps;

    /* Reload MRn on the next cycle */
    ct->MCR |= (CTIMER_MCR_MR0RL_MASK << channel);

    /* Wait for new duty cycle loaded into the MRn */
    while (ct->MR[channel] != ct->MSR[channel])
    {
        /* -- */
    }

    /* Disable shadow register updates */
    ct->MCR &= ~(CTIMER_MCR_MR0RL_MASK << channel);

    return 0;
}

static int mcx_pwm_pulse_get(mcx_pwm_obj_t *pwm, uint8_t channel, uint32_t *pulse_ns)
{
    CTIMER_Type *ct = (CTIMER_Type *)pwm->ct_instance;

    int p_channel = mcx_pwm_current_period_channel(pwm);
    if (p_channel < 0)
    {
        return -1;
    }

    /* Up-counting counters, the polarity is inversed */
    *pulse_ns = (ct->MR[p_channel] - ct->MR[channel]) * pwm->counter_period_ps / 1000;

    return 0;
}

static rt_err_t mcx_drv_pwm_get(mcx_pwm_obj_t *pwm, struct rt_pwm_configuration *configuration)
{
    if (mcx_pwm_period_get(pwm, &configuration->period) < 0)
    {
        return -RT_EFAULT;
    }

    if (mcx_pwm_pulse_get(pwm, configuration->channel, &configuration->pulse) < 0)
    {
        return -RT_EFAULT;
    }

    return RT_EOK;
}

static rt_err_t mcx_drv_pwm_set(mcx_pwm_obj_t *pwm, struct rt_pwm_configuration *configuration)
{
    CTIMER_Type *ct = pwm->ct_instance;

    uint32_t period = configuration->period * 1000 / pwm->counter_period_ps;
    uint8_t channel = configuration->channel;

    if ((ct->TCR & CTIMER_TCR_CEN_MASK) == 0U)
    {
        /* There's two conditions for a all-zero TCR: either a reset condition or timer is stopped. */
        /* In either case, we need to initialize the timer instance (AHB RST CTRL). */

        /* TODO: Do not use SDK functions */

        ctimer_config_t ct_cfg =
        {
            .mode = kCTIMER_TimerMode,
            .prescale = 1U,
        };

        /* Frequency: 150MHz max., we got 32bit counters, we can take that. */
        /* Approx. maximum period: 28.6 seconds. */

        CTIMER_Init(ct, &ct_cfg);

        /* Current timer is not running, we are the first channel being configured. */

        ct->TC = 0U;   /* Reset counter */
        ct->PC = 0U;   /* Reset prescaler counter */
        ct->PR = 0U;   /* Prescaler, divide by 1 to get best resolution */
        ct->MCR = 0U;  /* Reset interrupt and reset condition */
        ct->EMR = 0U;  /* Do nothing on match event and output 0 as default state */
        ct->PWMC = 0U; /* Disable all PWM channels, outputs will be controlled by EMn */

        /* Here, we have a favoritism of using channel 3 as period channel, unless channel 3 is used for output */
        if (channel != 3)
        {
            ct->MR[3] = period;
            ct->MCR |= CTIMER_MCR_MR3R_MASK;
        }
        else
        {
            /* Use channel 2 as period channel. */
            ct->MR[2] = period;
            ct->MCR |= CTIMER_MCR_MR2R_MASK;
        }

        /* Start counter */
        ct->TCR |= CTIMER_TCR_CEN_MASK;
    }
    else
    {
        /*
         * Due to the nature of the CTimer, one of the 4 match channels is needed for period control (frequency)
         * To find out which one is the current period channel, check the MRxR bit for each match output.
         * If we are configuring the same match being used as periodic channel, configure the next free match as period
         * then current channel can be re-used. If all 4 channels are in use then the function will fail with an errno.
         */

        /* The timer is running, check whether we need to re-locate the period channel */
        int p_channel = mcx_pwm_current_period_channel(pwm);
        if (p_channel < 0)
        {
            return -RT_EINVAL;
        }

        if (p_channel == channel)
        {
            /* We need to re-locate the period channel */

            int f_channel = mcx_pwm_first_free_channel(pwm);
            if (f_channel < 0)
            {
                /* There's no free channel, bail out. */
                return -RT_EBUSY;
            }

            /* Transfer the period channel to first free channel */

            /* Step 1: Copy current period to first free channel */
            ct->MR[f_channel] = ct->MR[p_channel];

            /* Step 2: Enable reset for new period channel */
            /* Note: it's safe doing it here since both old and new channel MRs contains same value */
            ct->MCR |= (CTIMER_MCR_MR0R_MASK << (3 * f_channel));

            /* Step 3: Disable reset for old period channel */
            ct->MCR &= ~(CTIMER_MCR_MR0R_MASK << (3 * p_channel));

            /* The old period channel is now available for PWM output */
            p_channel = f_channel;
        }

        if (mcx_pwm_period_set(pwm, configuration->period) < 0)
        {
            return -RT_EINVAL;
        }
    }

    if (mcx_pwm_pulse_set(pwm, channel, configuration->pulse) < 0)
    {
        return -RT_EINVAL;
    }

    return 0;
}

static rt_err_t mcx_drv_pwm_enable(mcx_pwm_obj_t *pwm, struct rt_pwm_configuration *configuration)
{
    CTIMER_Type *ct = (CTIMER_Type *)pwm->ct_instance;

    ct->PWMC |= (1U << configuration->channel);

    return 0;
}

static rt_err_t mcx_drv_pwm_disable(mcx_pwm_obj_t *pwm, struct rt_pwm_configuration *configuration)
{
    CTIMER_Type *ct = (CTIMER_Type *)pwm->ct_instance;

    ct->PWMC &= ~(1U << configuration->channel);

    return 0;
}

static rt_err_t mcx_drv_pwm_control(struct rt_device_pwm *device, int cmd, void *args)
{
    mcx_pwm_obj_t *pwm = device->parent.user_data;
    struct rt_pwm_configuration *configuration = (struct rt_pwm_configuration *)args;

    switch (cmd)
    {
    case PWM_CMD_ENABLE:
        return mcx_drv_pwm_enable(pwm, configuration);

    case PWM_CMD_DISABLE:
        return mcx_drv_pwm_disable(pwm, configuration);

    case PWM_CMD_SET:
        return mcx_drv_pwm_set(pwm, configuration);

    case PWM_CMD_GET:
        return mcx_drv_pwm_get(pwm, configuration);

    default:
        return -RT_EINVAL;
    }

    return RT_EOK;
}

static struct rt_pwm_ops mcx_pwm_ops =
{
    .control = mcx_drv_pwm_control,
};

int mcx_pwm_init(void)
{
    rt_err_t ret;
    char name_buf[8];

    for (uint8_t i = 0; i < ARRAY_SIZE(mcx_pwm_instances); i++)
    {
        mcx_pwm_list[i].ct_instance = mcx_pwm_instances[i];
        mcx_pwm_list[i].counter_period_ps = 1000000000000ULL / CLOCK_GetCTimerClkFreq(i);

        rt_snprintf(name_buf, sizeof(name_buf), "pwm%d", i);

        ret = rt_device_pwm_register(&mcx_pwm_list[i].pwm_device, name_buf, &mcx_pwm_ops, &mcx_pwm_list[i]);
        if (ret != RT_EOK)
        {
            return ret;
        }
    }

    return RT_EOK;
}

INIT_DEVICE_EXPORT(mcx_pwm_init);

#endif /* RT_USING_PWM */