drv_pwm.c 18.0 KB
Newer Older
lymzzyh's avatar
lymzzyh 已提交
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
/*
 * File      : drv_pwm.c
 * This file is part of RT-Thread RTOS
 * COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Change Logs:
 * Date           Author       Notes
 * 2018-07-15     ZYH          first version
 */
#include <rthw.h>
#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>
lymzzyh's avatar
lymzzyh 已提交
28

lymzzyh's avatar
lymzzyh 已提交
29 30 31
#define MAX_PERIOD 65535 
#define MIN_PERIOD 3
#define MIN_PULSE 2
lymzzyh's avatar
lymzzyh 已提交
32

lymzzyh's avatar
lymzzyh 已提交
33 34 35
static rt_err_t drv_pwm_control(struct rt_device_pwm *device, int cmd, void *arg);
static struct rt_pwm_ops drv_ops = 
{
lymzzyh's avatar
lymzzyh 已提交
36
    drv_pwm_control
lymzzyh's avatar
lymzzyh 已提交
37
};
lymzzyh's avatar
lymzzyh 已提交
38

lymzzyh's avatar
lymzzyh 已提交
39 40 41 42 43 44 45 46 47 48
static rt_err_t drv_pwm_enable(TIM_HandleTypeDef * htim, struct rt_pwm_configuration *configuration, rt_bool_t enable)
{
    rt_uint32_t channel = 0x04 * configuration->channel;
    if(!enable)
    {
        HAL_TIM_PWM_Stop(htim, channel);
    }
    HAL_TIM_PWM_Start(htim, channel);
    return RT_EOK;
}
lymzzyh's avatar
lymzzyh 已提交
49

lymzzyh's avatar
lymzzyh 已提交
50 51 52 53
static rt_err_t drv_pwm_get(TIM_HandleTypeDef * htim, struct rt_pwm_configuration *configuration)
{
    rt_uint32_t channel = 0x04 * configuration->channel;
    rt_uint32_t tim_clock;
lymzzyh's avatar
lymzzyh 已提交
54
#if (RT_HSE_HCLK > 100000000UL)//100M
lymzzyh's avatar
lymzzyh 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
    if(htim->Instance == TIM1 && htim->Instance == TIM8)
    {
        tim_clock = SystemCoreClock;
    }
    else
    {
        tim_clock = SystemCoreClock/2;
    }
#else
    tim_clock = SystemCoreClock;
#endif
    if(__HAL_TIM_GET_CLOCKDIVISION(htim) == TIM_CLOCKDIVISION_DIV2)
    {
        tim_clock = tim_clock / 2;
    }
    else if(__HAL_TIM_GET_CLOCKDIVISION(htim) == TIM_CLOCKDIVISION_DIV4)
    {
        tim_clock = tim_clock / 4;
    }
    tim_clock /= 1000000UL;
    configuration->period = (__HAL_TIM_GET_AUTORELOAD(htim) + 1) * (htim->Instance->PSC + 1) * 1000UL / tim_clock;
    configuration->pulse = (__HAL_TIM_GET_COMPARE(htim, channel) + 1) * (htim->Instance->PSC + 1) * 1000UL / tim_clock;
    return RT_EOK;
}
lymzzyh's avatar
lymzzyh 已提交
79

lymzzyh's avatar
lymzzyh 已提交
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
static rt_err_t drv_pwm_set(TIM_HandleTypeDef * htim, struct rt_pwm_configuration *configuration)
{
    rt_uint32_t period, pulse;
    rt_uint32_t tim_clock, psc;
    rt_uint32_t channel = 0x04 * configuration->channel;
#if (RT_HSE_HCLK > 100000000UL)//100M
    if(htim->Instance == TIM1 && htim->Instance == TIM8)
    {
        tim_clock = SystemCoreClock;
    }
    else
    {
        tim_clock = SystemCoreClock/2;
    }
#else
    tim_clock = SystemCoreClock;
#endif
    tim_clock /= 1000000UL;
    period = (unsigned long long)configuration->period * tim_clock / 1000ULL ;
    psc = period / MAX_PERIOD + 1;
    period = period / psc;
    __HAL_TIM_SET_PRESCALER(htim, psc - 1);
    if(period < MIN_PERIOD)
    {
        period = MIN_PERIOD;
    }
    __HAL_TIM_SET_AUTORELOAD(htim, period - 1);
    pulse = configuration->pulse * tim_clock / psc / 1000UL;
    if(pulse < MIN_PULSE)
    {
        pulse = MIN_PULSE;
    }
    else if(pulse > period)
    {
        pulse = period;
    }
    __HAL_TIM_SET_COMPARE(htim, channel, pulse - 1 );
    return RT_EOK;
}
lymzzyh's avatar
lymzzyh 已提交
119

lymzzyh's avatar
lymzzyh 已提交
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
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;
    TIM_HandleTypeDef * htim = (TIM_HandleTypeDef *)device->parent.user_data;
    
    switch(cmd)
    {
    case PWM_CMD_ENABLE:
        return drv_pwm_enable(htim, configuration, RT_TRUE);
    case PWM_CMD_DISABLE:
        return drv_pwm_enable(htim, configuration, RT_FALSE);
    case PWM_CMD_SET:
        return drv_pwm_set(htim, configuration);
    case PWM_CMD_GET:
        return drv_pwm_get(htim, configuration);
    default:
        return RT_EINVAL;
    }
}
lymzzyh's avatar
lymzzyh 已提交
139

lymzzyh's avatar
lymzzyh 已提交
140
static void HAL_TIM_MspPostInit(TIM_HandleTypeDef* timHandle);
lymzzyh's avatar
lymzzyh 已提交
141

lymzzyh's avatar
lymzzyh 已提交
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
#ifdef BSP_USING_PWM1
TIM_HandleTypeDef htim1;
#endif
#ifdef BSP_USING_PWM2
TIM_HandleTypeDef htim2;
#endif
#ifdef BSP_USING_PWM3
TIM_HandleTypeDef htim3;
#endif
#ifdef BSP_USING_PWM4
TIM_HandleTypeDef htim4;
#endif
#ifdef BSP_USING_PWM5
TIM_HandleTypeDef htim5;
#endif

#ifdef BSP_USING_PWM1
static void MX_TIM1_Init(void)
{
    TIM_MasterConfigTypeDef sMasterConfig;
    TIM_OC_InitTypeDef sConfigOC;
    TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig;

    htim1.Instance = TIM1;
    htim1.Init.Prescaler = 0;
    htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
    htim1.Init.Period = 0;
    htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
    htim1.Init.RepetitionCounter = 0;
    if (HAL_TIM_PWM_Init(&htim1) != HAL_OK)
    {
        RT_ASSERT(0);
    }

    sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
    sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
    if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
    {
        RT_ASSERT(0);
    }

    sConfigOC.OCMode = TIM_OCMODE_PWM1;
    sConfigOC.Pulse = 0;
    sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
    sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH;
    sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
    sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;
    sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;
lymzzyh's avatar
lymzzyh 已提交
190

lymzzyh's avatar
lymzzyh 已提交
191 192 193 194 195
#ifdef BSP_USING_PWM1_CH1
    if (HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
    {
        RT_ASSERT(0);
    }
lymzzyh's avatar
lymzzyh 已提交
196 197
#endif /* BSP_USING_PWM1_CH1 */

lymzzyh's avatar
lymzzyh 已提交
198 199 200 201 202
#ifdef BSP_USING_PWM1_CH2
    if (HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)
    {
        RT_ASSERT(0);
    }
lymzzyh's avatar
lymzzyh 已提交
203 204
#endif /* BSP_USING_PWM1_CH2 */

lymzzyh's avatar
lymzzyh 已提交
205 206 207 208 209
#ifdef BSP_USING_PWM1_CH3
    if (HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_3) != HAL_OK)
    {
        RT_ASSERT(0);
    }
lymzzyh's avatar
lymzzyh 已提交
210 211
#endif /* BSP_USING_PWM1_CH3 */

lymzzyh's avatar
lymzzyh 已提交
212 213 214 215 216
#ifdef BSP_USING_PWM1_CH4
    if (HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_4) != HAL_OK)
    {
        RT_ASSERT(0);
    }
lymzzyh's avatar
lymzzyh 已提交
217 218
#endif /* BSP_USING_PWM1_CH4 */

lymzzyh's avatar
lymzzyh 已提交
219 220 221 222 223 224 225
    sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE;
    sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE;
    sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;
    sBreakDeadTimeConfig.DeadTime = 0;
    sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE;
    sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH;
    sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;
lymzzyh's avatar
lymzzyh 已提交
226

lymzzyh's avatar
lymzzyh 已提交
227 228 229 230 231 232
    if (HAL_TIMEx_ConfigBreakDeadTime(&htim1, &sBreakDeadTimeConfig) != HAL_OK)
    {
        RT_ASSERT(0);
    }
    HAL_TIM_MspPostInit(&htim1);
}
lymzzyh's avatar
lymzzyh 已提交
233
#endif /* BSP_USING_PWM1 */
lymzzyh's avatar
lymzzyh 已提交
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

#ifdef BSP_USING_PWM2
static void MX_TIM2_Init(void)
{
    TIM_MasterConfigTypeDef sMasterConfig;
    TIM_OC_InitTypeDef sConfigOC;

    htim2.Instance = TIM2;
    htim2.Init.Prescaler = 0;
    htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
    htim2.Init.Period = 0;
    htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
    if (HAL_TIM_PWM_Init(&htim2) != HAL_OK)
    {
        RT_ASSERT(0);
    }

    sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
    sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
    if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
    {
        RT_ASSERT(0);
    }

    sConfigOC.OCMode = TIM_OCMODE_PWM1;
    sConfigOC.Pulse = 0;
    sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
    sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
lymzzyh's avatar
lymzzyh 已提交
262

lymzzyh's avatar
lymzzyh 已提交
263 264 265 266 267
#ifdef BSP_USING_PWM2_CH1
    if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
    {
        RT_ASSERT(0);
    }
lymzzyh's avatar
lymzzyh 已提交
268 269
#endif /* BSP_USING_PWM2_CH1 */

lymzzyh's avatar
lymzzyh 已提交
270 271 272 273 274
#ifdef BSP_USING_PWM2_CH2
    if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)
    {
        RT_ASSERT(0);
    }
lymzzyh's avatar
lymzzyh 已提交
275 276
#endif /* BSP_USING_PWM2_CH2 */

lymzzyh's avatar
lymzzyh 已提交
277 278 279 280 281
#ifdef BSP_USING_PWM2_CH3
    if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_3) != HAL_OK)
    {
        RT_ASSERT(0);
    }
lymzzyh's avatar
lymzzyh 已提交
282 283
#endif /* BSP_USING_PWM2_CH3 */

lymzzyh's avatar
lymzzyh 已提交
284 285 286 287 288
#ifdef BSP_USING_PWM2_CH4
    if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_4) != HAL_OK)
    {
        RT_ASSERT(0);
    }
lymzzyh's avatar
lymzzyh 已提交
289 290
#endif /* BSP_USING_PWM2_CH3 */

lymzzyh's avatar
lymzzyh 已提交
291 292
    HAL_TIM_MspPostInit(&htim2);
}
lymzzyh's avatar
lymzzyh 已提交
293 294
#endif /* BSP_USING_PWM2 */

lymzzyh's avatar
lymzzyh 已提交
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
#ifdef BSP_USING_PWM3
void MX_TIM3_Init(void)
{
    TIM_MasterConfigTypeDef sMasterConfig;
    TIM_OC_InitTypeDef sConfigOC;

    htim3.Instance = TIM3;
    htim3.Init.Prescaler = 0;
    htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
    htim3.Init.Period = 0;
    htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
    if (HAL_TIM_PWM_Init(&htim3) != HAL_OK)
    {
        RT_ASSERT(0);
    }

    sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
    sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
    if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
    {
        RT_ASSERT(0);
    }

    sConfigOC.OCMode = TIM_OCMODE_PWM1;
    sConfigOC.Pulse = 0;
    sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
    sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
#ifdef BSP_USING_PWM3_CH1
    if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
    {
        RT_ASSERT(0);
    }
lymzzyh's avatar
lymzzyh 已提交
327 328
#endif /* BSP_USING_PWM3_CH1 */

lymzzyh's avatar
lymzzyh 已提交
329 330 331 332 333
#ifdef BSP_USING_PWM3_CH2
    if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)
    {
        RT_ASSERT(0);
    }
lymzzyh's avatar
lymzzyh 已提交
334 335
#endif /* BSP_USING_PWM3_CH2 */

lymzzyh's avatar
lymzzyh 已提交
336 337 338 339 340
#ifdef BSP_USING_PWM3_CH3
    if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_3) != HAL_OK)
    {
        RT_ASSERT(0);
    }
lymzzyh's avatar
lymzzyh 已提交
341 342
#endif /* BSP_USING_PWM3_CH3 */

lymzzyh's avatar
lymzzyh 已提交
343 344 345 346 347
#ifdef BSP_USING_PWM3_CH4
    if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_4) != HAL_OK)
    {
        RT_ASSERT(0);
    }
lymzzyh's avatar
lymzzyh 已提交
348 349
#endif /* BSP_USING_PWM3_CH4 */

lymzzyh's avatar
lymzzyh 已提交
350 351
    HAL_TIM_MspPostInit(&htim3);
}
lymzzyh's avatar
lymzzyh 已提交
352 353
#endif /* BSP_USING_PWM3 */

lymzzyh's avatar
lymzzyh 已提交
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
#ifdef BSP_USING_PWM4
void MX_TIM4_Init(void)
{
    TIM_MasterConfigTypeDef sMasterConfig;
    TIM_OC_InitTypeDef sConfigOC;

    htim4.Instance = TIM4;
    htim4.Init.Prescaler = 0;
    htim4.Init.CounterMode = TIM_COUNTERMODE_UP;
    htim4.Init.Period = 0;
    htim4.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
    if (HAL_TIM_PWM_Init(&htim4) != HAL_OK)
    {
        RT_ASSERT(0);
    }

    sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
    sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
    if (HAL_TIMEx_MasterConfigSynchronization(&htim4, &sMasterConfig) != HAL_OK)
    {
        RT_ASSERT(0);
    }

    sConfigOC.OCMode = TIM_OCMODE_PWM1;
    sConfigOC.Pulse = 0;
    sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
    sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
#ifdef BSP_USING_PWM4_CH1
    if (HAL_TIM_PWM_ConfigChannel(&htim4, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
    {
        RT_ASSERT(0);
    }
lymzzyh's avatar
lymzzyh 已提交
386 387
#endif /* BSP_USING_PWM4_CH1 */

lymzzyh's avatar
lymzzyh 已提交
388 389 390 391 392
#ifdef BSP_USING_PWM4_CH2
    if (HAL_TIM_PWM_ConfigChannel(&htim4, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)
    {
        RT_ASSERT(0);
    }
lymzzyh's avatar
lymzzyh 已提交
393 394
#endif /* BSP_USING_PWM4_CH2 */

lymzzyh's avatar
lymzzyh 已提交
395 396 397 398 399
#ifdef BSP_USING_PWM4_CH3
    if (HAL_TIM_PWM_ConfigChannel(&htim4, &sConfigOC, TIM_CHANNEL_3) != HAL_OK)
    {
        RT_ASSERT(0);
    }
lymzzyh's avatar
lymzzyh 已提交
400 401
#endif /* BSP_USING_PWM4_CH3 */

lymzzyh's avatar
lymzzyh 已提交
402 403 404 405 406
#ifdef BSP_USING_PWM4_CH4
    if (HAL_TIM_PWM_ConfigChannel(&htim4, &sConfigOC, TIM_CHANNEL_4) != HAL_OK)
    {
        RT_ASSERT(0);
    }
lymzzyh's avatar
lymzzyh 已提交
407 408
#endif /* BSP_USING_PWM4_CH4 */

lymzzyh's avatar
lymzzyh 已提交
409 410
    HAL_TIM_MspPostInit(&htim4);
}
lymzzyh's avatar
lymzzyh 已提交
411 412
#endif /* BSP_USING_PWM4 */

lymzzyh's avatar
lymzzyh 已提交
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
#ifdef BSP_USING_PWM5
void MX_TIM5_Init(void)
{
    TIM_MasterConfigTypeDef sMasterConfig;
    TIM_OC_InitTypeDef sConfigOC;

    htim5.Instance = TIM5;
    htim5.Init.Prescaler = 0;
    htim5.Init.CounterMode = TIM_COUNTERMODE_UP;
    htim5.Init.Period = 0;
    htim5.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
    if (HAL_TIM_PWM_Init(&htim5) != HAL_OK)
    {
        RT_ASSERT(0);
    }

    sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
    sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
    if (HAL_TIMEx_MasterConfigSynchronization(&htim5, &sMasterConfig) != HAL_OK)
    {
        RT_ASSERT(0);
    }

    sConfigOC.OCMode = TIM_OCMODE_PWM1;
    sConfigOC.Pulse = 0;
    sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
    sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
#ifdef BSP_USING_PWM5_CH1
    if (HAL_TIM_PWM_ConfigChannel(&htim5, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
    {
        RT_ASSERT(0);
    }
lymzzyh's avatar
lymzzyh 已提交
445 446
#endif /* BSP_USING_PWM5_CH1 */

lymzzyh's avatar
lymzzyh 已提交
447 448 449 450 451
#ifdef BSP_USING_PWM5_CH2
    if (HAL_TIM_PWM_ConfigChannel(&htim5, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)
    {
        RT_ASSERT(0);
    }
lymzzyh's avatar
lymzzyh 已提交
452 453
#endif /* BSP_USING_PWM5_CH2 */

lymzzyh's avatar
lymzzyh 已提交
454 455 456 457 458
#ifdef BSP_USING_PWM5_CH3
    if (HAL_TIM_PWM_ConfigChannel(&htim5, &sConfigOC, TIM_CHANNEL_3) != HAL_OK)
    {
        RT_ASSERT(0);
    }
lymzzyh's avatar
lymzzyh 已提交
459 460
#endif /* BSP_USING_PWM5_CH3 */

lymzzyh's avatar
lymzzyh 已提交
461 462
    HAL_TIM_MspPostInit(&htim5);
}
lymzzyh's avatar
lymzzyh 已提交
463 464
#endif /* BSP_USING_PWM5 */

lymzzyh's avatar
lymzzyh 已提交
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef* tim_pwmHandle)
{
    if(tim_pwmHandle->Instance==TIM1)
    {
        __HAL_RCC_TIM1_CLK_ENABLE();
    }
    else if(tim_pwmHandle->Instance==TIM2)
    {
        __HAL_RCC_TIM2_CLK_ENABLE();
    }
    else if(tim_pwmHandle->Instance==TIM3)
    {
        __HAL_RCC_TIM3_CLK_ENABLE();
    }
    else if(tim_pwmHandle->Instance==TIM4)
    {
        __HAL_RCC_TIM4_CLK_ENABLE();
    }
    else if(tim_pwmHandle->Instance==TIM5)
    {
        __HAL_RCC_TIM5_CLK_ENABLE();
    }
}
lymzzyh's avatar
lymzzyh 已提交
488 489

static void HAL_TIM_MspPostInit(TIM_HandleTypeDef* timHandle)
lymzzyh's avatar
lymzzyh 已提交
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
{
    GPIO_InitTypeDef GPIO_InitStruct;
    if(timHandle->Instance==TIM1)
    {
        __HAL_RCC_GPIOA_CLK_ENABLE();
        /**TIM1 GPIO Configuration    
        PA8     ------> TIM1_CH1
        PA9     ------> TIM1_CH2
        PA10     ------> TIM1_CH3
        PA11     ------> TIM1_CH4 
        */
        GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11;
        GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
        GPIO_InitStruct.Pull = GPIO_NOPULL;
        GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
        GPIO_InitStruct.Alternate = GPIO_AF1_TIM1;
        HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
    }
    else if(timHandle->Instance==TIM2)
    {
        __HAL_RCC_GPIOA_CLK_ENABLE();
        __HAL_RCC_GPIOB_CLK_ENABLE();
        /**TIM2 GPIO Configuration    
        PA3     ------> TIM2_CH4
        PA5     ------> TIM2_CH1
        PB10     ------> TIM2_CH3
        PB3     ------> TIM2_CH2 
        */
        GPIO_InitStruct.Pin = GPIO_PIN_3|GPIO_PIN_5;
        GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
        GPIO_InitStruct.Pull = GPIO_NOPULL;
        GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
        GPIO_InitStruct.Alternate = GPIO_AF1_TIM2;
        HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

        GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_3;
        GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
        GPIO_InitStruct.Pull = GPIO_NOPULL;
        GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
        GPIO_InitStruct.Alternate = GPIO_AF1_TIM2;
        HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
    }
    else if(timHandle->Instance==TIM3)
    {
        __HAL_RCC_GPIOA_CLK_ENABLE();
        __HAL_RCC_GPIOB_CLK_ENABLE();
        /**TIM3 GPIO Configuration    
        PA6     ------> TIM3_CH1
        PA7     ------> TIM3_CH2
        PB0     ------> TIM3_CH3
        PB1     ------> TIM3_CH4 
        */
        GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7;
        GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
        GPIO_InitStruct.Pull = GPIO_NOPULL;
        GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
        GPIO_InitStruct.Alternate = GPIO_AF2_TIM3;
        HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

        GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1;
        GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
        GPIO_InitStruct.Pull = GPIO_NOPULL;
        GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
        GPIO_InitStruct.Alternate = GPIO_AF2_TIM3;
        HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
    }
    else if(timHandle->Instance==TIM4)
    {
        __HAL_RCC_GPIOB_CLK_ENABLE();
        /**TIM4 GPIO Configuration    
        PB6     ------> TIM4_CH1
        PB7     ------> TIM4_CH2
        PB8     ------> TIM4_CH3
        PB9     ------> TIM4_CH4 
        */
        GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9;
        GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
        GPIO_InitStruct.Pull = GPIO_NOPULL;
        GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
        GPIO_InitStruct.Alternate = GPIO_AF2_TIM4;
        HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
    }
    else if(timHandle->Instance==TIM5)
    {
        __HAL_RCC_GPIOA_CLK_ENABLE();
        /**TIM5 GPIO Configuration    
        PA0-WKUP     ------> TIM5_CH1
        PA1     ------> TIM5_CH2
        PA2     ------> TIM5_CH3 
        */
        GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2;
        GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
        GPIO_InitStruct.Pull = GPIO_NOPULL;
        GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
        GPIO_InitStruct.Alternate = GPIO_AF2_TIM5;
        HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
    }

}

void HAL_TIM_PWM_MspDeInit(TIM_HandleTypeDef* tim_pwmHandle)
{
    if(tim_pwmHandle->Instance==TIM1)
    {
        __HAL_RCC_TIM1_CLK_DISABLE();
    }
    else if(tim_pwmHandle->Instance==TIM2)
    {
        __HAL_RCC_TIM2_CLK_DISABLE();
    }
    else if(tim_pwmHandle->Instance==TIM3)
    {
        __HAL_RCC_TIM3_CLK_DISABLE();
    }
    else if(tim_pwmHandle->Instance==TIM4)
    {
        __HAL_RCC_TIM4_CLK_DISABLE();
    }
    else if(tim_pwmHandle->Instance==TIM5)
    {
        __HAL_RCC_TIM5_CLK_DISABLE();
    }
} 
int drv_pwm_init(void)
{
#ifdef BSP_USING_PWM1
    MX_TIM1_Init();
    rt_device_pwm_register(rt_calloc(1,sizeof(struct rt_device_pwm)), "pwm1", &drv_ops, &htim1);
#endif
lymzzyh's avatar
lymzzyh 已提交
619

lymzzyh's avatar
lymzzyh 已提交
620 621 622 623
#ifdef BSP_USING_PWM2
    MX_TIM2_Init();
    rt_device_pwm_register(rt_calloc(1,sizeof(struct rt_device_pwm)), "pwm2", &drv_ops, &htim2);
#endif
lymzzyh's avatar
lymzzyh 已提交
624

lymzzyh's avatar
lymzzyh 已提交
625 626 627 628
#ifdef BSP_USING_PWM3
    MX_TIM3_Init();
    rt_device_pwm_register(rt_calloc(1,sizeof(struct rt_device_pwm)), "pwm3", &drv_ops, &htim3);
#endif
lymzzyh's avatar
lymzzyh 已提交
629

lymzzyh's avatar
lymzzyh 已提交
630 631 632 633
#ifdef BSP_USING_PWM4
    MX_TIM4_Init();
    rt_device_pwm_register(rt_calloc(1,sizeof(struct rt_device_pwm)), "pwm4", &drv_ops, &htim4);
#endif
lymzzyh's avatar
lymzzyh 已提交
634

lymzzyh's avatar
lymzzyh 已提交
635 636 637 638 639 640 641
#ifdef BSP_USING_PWM5
    MX_TIM5_Init();
    rt_device_pwm_register(rt_calloc(1,sizeof(struct rt_device_pwm)), "pwm5", &drv_ops, &htim5);
#endif
    return 0;
}
INIT_DEVICE_EXPORT(drv_pwm_init);