drv_pwm.c 9.6 KB
Newer Older
1 2 3 4 5 6 7
/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
1
192.168.1.134 已提交
8
 * 2021-11-11     breo.com     first version
9 10 11 12 13 14
 */

#include <board.h>
#include "drv_pwm.h"

#ifdef RT_USING_PWM
1
192.168.1.134 已提交
15 16 17 18 19 20 21 22 23 24 25 26
    #if !defined(BSP_USING_TIM1_CH1) && !defined(BSP_USING_TIM1_CH2) && \
        !defined(BSP_USING_TIM1_CH3) && !defined(BSP_USING_TIM1_CH4) && \
        !defined(BSP_USING_TIM2_CH1) && !defined(BSP_USING_TIM2_CH2) && \
        !defined(BSP_USING_TIM2_CH3) && !defined(BSP_USING_TIM2_CH4) && \
        !defined(BSP_USING_TIM3_CH1) && !defined(BSP_USING_TIM3_CH2) && \
        !defined(BSP_USING_TIM3_CH3) && !defined(BSP_USING_TIM3_CH4) && \
        !defined(BSP_USING_TIM4_CH1) && !defined(BSP_USING_TIM4_CH2) && \
        !defined(BSP_USING_TIM4_CH3) && !defined(BSP_USING_TIM4_CH4) && \
        !defined(BSP_USING_TIM5_CH1) && !defined(BSP_USING_TIM5_CH2) && \
        !defined(BSP_USING_TIM5_CH3) && !defined(BSP_USING_TIM5_CH4) && \
        !defined(BSP_USING_TIM8_CH1) && !defined(BSP_USING_TIM8_CH2) && \
        !defined(BSP_USING_TIM8_CH3) && !defined(BSP_USING_TIM8_CH4)
1
192.168.1.134 已提交
27 28
        #error "Please define at least one BSP_USING_TIMx_CHx"
    #endif
29 30 31
#endif /* RT_USING_PWM */

#define MAX_PERIOD 65535
1
192.168.1.134 已提交
32 33

#ifdef BSP_USING_PWM
34

L
linyuanbo_breo_server 已提交
35
struct n32_pwm
36
{
1
192.168.1.134 已提交
37
    TIM_Module *tim_handle;
1
192.168.1.134 已提交
38 39 40 41 42 43
    const char *name;
    struct rt_device_pwm pwm_device;
    int8_t tim_en;
    uint8_t ch_en;
    uint32_t period;
    uint32_t psc;
44 45
};

L
linyuanbo_breo_server 已提交
46
static struct n32_pwm n32_pwm_obj[] =
47
{
1
192.168.1.134 已提交
48 49 50 51 52 53
#if defined(BSP_USING_TIM1_CH1) || defined(BSP_USING_TIM1_CH2) || \
        defined(BSP_USING_TIM1_CH3) || defined(BSP_USING_TIM1_CH4)
    {
        .tim_handle = TIM1,
        .name = "tim1pwm",
    },
L
linyuanbo_breo_server 已提交
54
#endif
55

1
192.168.1.134 已提交
56 57 58 59 60 61
#if defined(BSP_USING_TIM2_CH1) || defined(BSP_USING_TIM2_CH2) || \
        defined(BSP_USING_TIM2_CH3) || defined(BSP_USING_TIM2_CH4)
    {
        .tim_handle = TIM2,
        .name = "tim2pwm",
    },
L
linyuanbo_breo_server 已提交
62
#endif
63

1
192.168.1.134 已提交
64 65 66 67 68 69
#if defined(BSP_USING_TIM3_CH1) || defined(BSP_USING_TIM3_CH2) || \
        defined(BSP_USING_TIM3_CH3) || defined(BSP_USING_TIM3_CH4)
    {
        .tim_handle = TIM3,
        .name = "tim3pwm",
    },
L
linyuanbo_breo_server 已提交
70
#endif
71

1
192.168.1.134 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
#if defined(BSP_USING_TIM4_CH1) || defined(BSP_USING_TIM4_CH2) || \
        defined(BSP_USING_TIM4_CH3) || defined(BSP_USING_TIM4_CH4)
    {
        .tim_handle = TIM4,
        .name = "tim4pwm",
    },
#endif

#if defined(BSP_USING_TIM5_CH1) || defined(BSP_USING_TIM5_CH2) || \
        defined(BSP_USING_TIM5_CH3) || defined(BSP_USING_TIM5_CH4)
    {
        .tim_handle = TIM5,
        .name = "tim5pwm",
    },
#endif

#if defined(BSP_USING_TIM8_CH1) || defined(BSP_USING_TIM8_CH2) || \
        defined(BSP_USING_TIM8_CH3) || defined(BSP_USING_TIM8_CH4)
    {
        .tim_handle = TIM8,
        .name = "tim8pwm",
    }
L
linyuanbo_breo_server 已提交
94
#endif
1
192.168.1.134 已提交
95

96 97 98 99 100 101 102 103
};

static rt_err_t drv_pwm_control(struct rt_device_pwm *device, int cmd, void *arg);
static struct rt_pwm_ops drv_ops =
{
    drv_pwm_control
};

1
192.168.1.134 已提交
104
static rt_err_t drv_pwm_enable(struct n32_pwm *pwm_dev, struct rt_pwm_configuration *configuration, rt_bool_t enable)
105 106 107
{
    /* Get the value of channel */
    rt_uint32_t channel = configuration->channel;
1
192.168.1.134 已提交
108 109 110 111 112 113 114 115 116 117
    TIM_Module *TIMx = pwm_dev->tim_handle;

    if (enable)
    {
        pwm_dev->ch_en |= 0x1 << channel;
    }
    else
    {
        pwm_dev->ch_en &= ~(0x1 << channel);
    }
118

1
192.168.1.134 已提交
119
    if (enable)
120
    {
1
192.168.1.134 已提交
121
        if (channel == 1)
122
        {
1
192.168.1.134 已提交
123
            TIM_EnableCapCmpCh(TIMx, TIM_CH_1, TIM_CAP_CMP_ENABLE);
124
        }
1
192.168.1.134 已提交
125
        else if (channel == 2)
126
        {
1
192.168.1.134 已提交
127
            TIM_EnableCapCmpCh(TIMx, TIM_CH_2, TIM_CAP_CMP_ENABLE);
128
        }
1
192.168.1.134 已提交
129
        else if (channel == 3)
130
        {
1
192.168.1.134 已提交
131
            TIM_EnableCapCmpCh(TIMx, TIM_CH_3, TIM_CAP_CMP_ENABLE);
132
        }
1
192.168.1.134 已提交
133
        else if (channel == 4)
134
        {
1
192.168.1.134 已提交
135
            TIM_EnableCapCmpCh(TIMx, TIM_CH_4, TIM_CAP_CMP_ENABLE);
136 137 138 139
        }
    }
    else
    {
1
192.168.1.134 已提交
140
        if (channel == 1)
141
        {
1
192.168.1.134 已提交
142
            TIM_EnableCapCmpCh(TIMx, TIM_CH_1, TIM_CAP_CMP_DISABLE);
143
        }
1
192.168.1.134 已提交
144
        else if (channel == 2)
145
        {
1
192.168.1.134 已提交
146
            TIM_EnableCapCmpCh(TIMx, TIM_CH_2, TIM_CAP_CMP_DISABLE);
147
        }
1
192.168.1.134 已提交
148
        else if (channel == 3)
149
        {
1
192.168.1.134 已提交
150
            TIM_EnableCapCmpCh(TIMx, TIM_CH_3, TIM_CAP_CMP_DISABLE);
151
        }
1
192.168.1.134 已提交
152
        else if (channel == 4)
153
        {
1
192.168.1.134 已提交
154
            TIM_EnableCapCmpCh(TIMx, TIM_CH_4, TIM_CAP_CMP_DISABLE);
155 156 157
        }
    }

1
192.168.1.134 已提交
158 159 160 161 162 163 164 165 166 167
    if (pwm_dev->ch_en)
    {
        pwm_dev->tim_en = 0x1;
        TIM_Enable(TIMx, ENABLE);
    }
    else
    {
        pwm_dev->tim_en = 0x0;
        TIM_Enable(TIMx, DISABLE);
    }
L
linyuanbo_breo_server 已提交
168

169 170 171
    return RT_EOK;
}

1
192.168.1.134 已提交
172
static rt_err_t drv_pwm_get(struct n32_pwm *pwm_dev, struct rt_pwm_configuration *configuration)
173
{
L
linyuanbo_breo_server 已提交
174
    RCC_ClocksType  RCC_Clockstruct;
175 176
    rt_uint32_t ar, div, cc1, cc2, cc3, cc4;
    rt_uint64_t tim_clock;
1
192.168.1.134 已提交
177 178
    rt_uint32_t channel = configuration->channel;
    TIM_Module *TIMx = pwm_dev->tim_handle;
179 180

    ar   = TIMx->AR;
L
linyuanbo_breo_server 已提交
181 182 183 184 185
    div  = TIMx->PSC;
    cc1  = TIMx->CCDAT1;
    cc2  = TIMx->CCDAT2;
    cc3  = TIMx->CCDAT3;
    cc4  = TIMx->CCDAT4;
186

L
linyuanbo_breo_server 已提交
187
    RCC_GetClocksFreqValue(&RCC_Clockstruct);
188

L
linyuanbo_breo_server 已提交
189
    tim_clock = RCC_Clockstruct.Pclk2Freq;
190 191 192 193

    /* Convert nanosecond to frequency and duty cycle. */
    tim_clock /= 1000000UL;
    configuration->period = (ar + 1) * (div + 1) * 1000UL / tim_clock;
1
192.168.1.134 已提交
194
    if (channel == 1)
L
linyuanbo_breo_server 已提交
195
        configuration->pulse = (cc1 + 1) * (div + 1) * 1000UL / tim_clock;
1
192.168.1.134 已提交
196 197 198
    if (channel == 2)
        configuration->pulse = (cc2 + 1) * (div + 1) * 1000UL / tim_clock;
    if (channel == 3)
L
linyuanbo_breo_server 已提交
199
        configuration->pulse = (cc3 + 1) * (div + 1) * 1000UL / tim_clock;
1
192.168.1.134 已提交
200
    if (channel == 4)
L
linyuanbo_breo_server 已提交
201 202
        configuration->pulse = (cc4 + 1) * (div + 1) * 1000UL / tim_clock;

203 204 205
    return RT_EOK;
}

1
192.168.1.134 已提交
206
static rt_err_t drv_pwm_set(struct n32_pwm *pwm_dev, struct rt_pwm_configuration *configuration)
207
{
1
192.168.1.134 已提交
208 209 210
    TIM_Module *TIMx = pwm_dev->tim_handle;
    rt_uint32_t channel = configuration->channel;

211
    /* Init timer pin and enable clock */
1
192.168.1.134 已提交
212
    void n32_msp_tim_init(void *Instance);
L
linyuanbo_breo_server 已提交
213
    n32_msp_tim_init(TIMx);
214

215 216 217 218 219 220 221 222 223 224
    RCC_ClocksType RCC_Clock;
    RCC_GetClocksFreqValue(&RCC_Clock);
    rt_uint64_t input_clock;
    if ((TIM1 == TIMx) || (TIM8 == TIMx))
    {
        RCC_ConfigTim18Clk(RCC_TIM18CLK_SRC_SYSCLK);
        input_clock = RCC_Clock.SysclkFreq;
    }
    else
    {
1
192.168.1.134 已提交
225
        if (1 == (RCC_Clock.HclkFreq / RCC_Clock.Pclk1Freq))
226 227 228 229 230
            input_clock = RCC_Clock.Pclk1Freq;
        else
            input_clock = RCC_Clock.Pclk1Freq * 2;
    }

231
    /* Convert nanosecond to frequency and duty cycle. */
232 233
    rt_uint32_t period = (unsigned long long)configuration->period ;
    rt_uint64_t psc = period / MAX_PERIOD + 1;
234
    period = period / psc;
235
    psc = psc * (input_clock / 1000000);
236

1
192.168.1.134 已提交
237 238 239 240 241 242 243 244 245 246 247
    if ((pwm_dev->period != period) || (pwm_dev->psc != psc))
    {
        /* TIMe base configuration */
        TIM_TimeBaseInitType TIM_TIMeBaseStructure;
        TIM_InitTimBaseStruct(&TIM_TIMeBaseStructure);
        TIM_TIMeBaseStructure.Period = period;
        TIM_TIMeBaseStructure.Prescaler = psc - 1;
        TIM_TIMeBaseStructure.ClkDiv = 0;
        TIM_TIMeBaseStructure.CntMode = TIM_CNT_MODE_UP;
        TIM_InitTimeBase(TIMx, &TIM_TIMeBaseStructure);
    }
248

249
    rt_uint32_t pulse = (unsigned long long)configuration->pulse;
250
    /* PWM1 Mode configuration: Channel1 */
251
    OCInitType  TIM_OCInitStructure;
L
linyuanbo_breo_server 已提交
252 253 254 255 256
    TIM_InitOcStruct(&TIM_OCInitStructure);
    TIM_OCInitStructure.OcMode = TIM_OCMODE_PWM1;
    TIM_OCInitStructure.OutputState = TIM_OUTPUT_STATE_ENABLE;
    TIM_OCInitStructure.Pulse = pulse;
    TIM_OCInitStructure.OcPolarity = TIM_OC_POLARITY_HIGH;
257

1
192.168.1.134 已提交
258
    if (channel == 1)
259
    {
L
linyuanbo_breo_server 已提交
260 261
        TIM_InitOc1(TIMx, &TIM_OCInitStructure);
        TIM_ConfigOc1Preload(TIMx, TIM_OC_PRE_LOAD_ENABLE);
1
192.168.1.134 已提交
262 263
        if (!(pwm_dev->ch_en & (0x1 << channel)))
            TIM_EnableCapCmpCh(TIMx, TIM_CH_1, TIM_CAP_CMP_DISABLE);
264
    }
1
192.168.1.134 已提交
265
    else if (channel == 2)
266
    {
L
linyuanbo_breo_server 已提交
267 268
        TIM_InitOc2(TIMx, &TIM_OCInitStructure);
        TIM_ConfigOc2Preload(TIMx, TIM_OC_PRE_LOAD_ENABLE);
1
192.168.1.134 已提交
269 270
        if (!(pwm_dev->ch_en & (0x1 << channel)))
            TIM_EnableCapCmpCh(TIMx, TIM_CH_2, TIM_CAP_CMP_DISABLE);
271
    }
1
192.168.1.134 已提交
272
    else if (channel == 3)
273
    {
L
linyuanbo_breo_server 已提交
274 275
        TIM_InitOc3(TIMx, &TIM_OCInitStructure);
        TIM_ConfigOc3Preload(TIMx, TIM_OC_PRE_LOAD_ENABLE);
1
192.168.1.134 已提交
276 277
        if (!(pwm_dev->ch_en & (0x1 << channel)))
            TIM_EnableCapCmpCh(TIMx, TIM_CH_3, TIM_CAP_CMP_DISABLE);
278
    }
1
192.168.1.134 已提交
279
    else if (channel == 4)
280
    {
L
linyuanbo_breo_server 已提交
281 282
        TIM_InitOc4(TIMx, &TIM_OCInitStructure);
        TIM_ConfigOc4Preload(TIMx, TIM_OC_PRE_LOAD_ENABLE);
1
192.168.1.134 已提交
283 284
        if (!(pwm_dev->ch_en & (0x1 << channel)))
            TIM_EnableCapCmpCh(TIMx, TIM_CH_4, TIM_CAP_CMP_DISABLE);
285 286
    }

L
linyuanbo_breo_server 已提交
287
    TIM_ConfigArPreload(TIMx, ENABLE);
288
    TIM_EnableCtrlPwmOutputs(TIMx, ENABLE);
289 290 291 292 293 294 295

    return RT_EOK;
}

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;
1
192.168.1.134 已提交
296
    struct n32_pwm *pwm_dev = (struct n32_pwm *)(device->parent.user_data);
297 298 299

    switch (cmd)
    {
1
192.168.1.134 已提交
300
    case PWM_CMD_ENABLE:
1
192.168.1.134 已提交
301
        return drv_pwm_enable(pwm_dev, configuration, RT_TRUE);
1
192.168.1.134 已提交
302
    case PWM_CMD_DISABLE:
1
192.168.1.134 已提交
303
        return drv_pwm_enable(pwm_dev, configuration, RT_FALSE);
1
192.168.1.134 已提交
304
    case PWM_CMD_SET:
1
192.168.1.134 已提交
305
        return drv_pwm_set(pwm_dev, configuration);
1
192.168.1.134 已提交
306
    case PWM_CMD_GET:
1
192.168.1.134 已提交
307
        return drv_pwm_get(pwm_dev, configuration);
1
192.168.1.134 已提交
308 309
    default:
        return RT_EINVAL;
310 311 312 313 314 315 316 317
    }
}

static int rt_hw_pwm_init(void)
{
    int i = 0;
    int result = RT_EOK;

1
192.168.1.134 已提交
318
    for (i = 0; i < sizeof(n32_pwm_obj) / sizeof(n32_pwm_obj[0]); i++)
319
    {
1
192.168.1.134 已提交
320 321
        if (rt_device_pwm_register(&n32_pwm_obj[i].pwm_device,
                                   n32_pwm_obj[i].name, &drv_ops, &(n32_pwm_obj[i])) == RT_EOK)
322 323 324 325
        {
        }
        else
        {
L
linyuanbo_breo_server 已提交
326
            result = -RT_ERROR;
327 328 329 330 331 332
        }
    }

    return result;
}
INIT_BOARD_EXPORT(rt_hw_pwm_init);
1
192.168.1.134 已提交
333 334 335

#endif