drv_pwm.c 12.8 KB
Newer Older
S
sheltonyu 已提交
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 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 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
/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2022-03-10     shelton      first version
 */

#include "board.h"

#ifdef RT_USING_PWM
#include "drv_pwm.h"
#include <drivers/rt_drv_pwm.h>

//#define DRV_DEBUG
#define LOG_TAG                         "drv.pwm"
#include <drv_log.h>

#define MAX_PERIOD                      65535

struct rt_device_pwm pwm_device;

struct at32_pwm
{
    struct rt_device_pwm pwm_device;
    tmr_type* tmr_x;
    rt_uint8_t channel;
    char *name;
};

enum
{
#ifdef BSP_USING_PWM1
    PWM1_INDEX,
#endif
#ifdef BSP_USING_PWM2
    PWM2_INDEX,
#endif
#ifdef BSP_USING_PWM3
    PWM3_INDEX,
#endif
#ifdef BSP_USING_PWM4
    PWM4_INDEX,
#endif
#ifdef BSP_USING_PWM5
    PWM5_INDEX,
#endif
#ifdef BSP_USING_PWM6
    PWM6_INDEX,
#endif
#ifdef BSP_USING_PWM7
    PWM7_INDEX,
#endif
#ifdef BSP_USING_PWM8
    PWM8_INDEX,
#endif
#ifdef BSP_USING_PWM9
    PWM9_INDEX,
#endif
#ifdef BSP_USING_PWM10
    PWM10_INDEX,
#endif
#ifdef BSP_USING_PWM11
    PWM11_INDEX,
#endif
#ifdef BSP_USING_PWM12
    PWM12_INDEX,
#endif
#ifdef BSP_USING_PWM13
    PWM13_INDEX,
#endif
};

static struct at32_pwm at32_pwm_obj[] =
{
#ifdef BSP_USING_PWM1
    PWM1_CONFIG,
#endif

#ifdef BSP_USING_PWM2
    PWM2_CONFIG,
#endif

#ifdef BSP_USING_PWM3
    PWM3_CONFIG,
#endif

#ifdef BSP_USING_PWM4
    PWM4_CONFIG,
#endif

#ifdef BSP_USING_PWM5
    PWM5_CONFIG,
#endif

#ifdef BSP_USING_PWM6
    PWM6_CONFIG,
#endif

#ifdef BSP_USING_PWM7
    PWM7_CONFIG,
#endif

#ifdef BSP_USING_PWM8
    PWM8_CONFIG,
#endif

#ifdef BSP_USING_PWM9
    PWM9_CONFIG,
#endif

#ifdef BSP_USING_PWM10
    PWM10_CONFIG,
#endif

#ifdef BSP_USING_PWM11
    PWM11_CONFIG,
#endif

#ifdef BSP_USING_PWM12
    PWM12_CONFIG,
#endif

#ifdef BSP_USING_PWM13
    PWM13_CONFIG,
#endif
};

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
};

static void tmr_pclk_get(rt_uint32_t *pclk1_doubler, rt_uint32_t *pclk2_doubler)
{
    crm_clocks_freq_type  clocks_struct;

    *pclk1_doubler = 1;
    *pclk2_doubler = 1;

    crm_clocks_freq_get(&clocks_struct);

    if(clocks_struct.ahb_freq != clocks_struct.apb1_freq)
    {
        *pclk1_doubler = 2;
    }

    if(clocks_struct.ahb_freq != clocks_struct.apb2_freq)
    {
        *pclk2_doubler = 2;
    }
}

static rt_err_t drv_pwm_enable(tmr_type* tmr_x, struct rt_pwm_configuration *configuration, rt_bool_t enable)
{
    /* get the value of channel */
    rt_uint32_t channel = configuration->channel;

    if (!configuration->complementary)
    {
        if (!enable)
        {
            if(channel == 1)
            {
               tmr_channel_enable(tmr_x, TMR_SELECT_CHANNEL_1, FALSE);
            }
            else if(channel == 2)
            {
              tmr_channel_enable(tmr_x, TMR_SELECT_CHANNEL_2, FALSE);
            }
            else if(channel == 3)
            {
              tmr_channel_enable(tmr_x, TMR_SELECT_CHANNEL_3, FALSE);
            }
            else if(channel == 4)
            {
              tmr_channel_enable(tmr_x, TMR_SELECT_CHANNEL_4, FALSE);
            }
        }
        else
        {
            if(channel == 1)
            {
              tmr_channel_enable(tmr_x, TMR_SELECT_CHANNEL_1, TRUE);
            }
            else if(channel == 2)
            {
              tmr_channel_enable(tmr_x, TMR_SELECT_CHANNEL_2, TRUE);
            }
            else if(channel == 3)
            {
              tmr_channel_enable(tmr_x, TMR_SELECT_CHANNEL_3, TRUE);
            }
            else if(channel == 4)
            {
              tmr_channel_enable(tmr_x, TMR_SELECT_CHANNEL_4, TRUE);
            }
        }
    }
    else
    {
        if (!enable)
        {
            if(channel == 1)
            {
               tmr_channel_enable(tmr_x, TMR_SELECT_CHANNEL_1C, FALSE);
            }
            else if(channel == 2)
            {
              tmr_channel_enable(tmr_x, TMR_SELECT_CHANNEL_2C, FALSE);
            }
            else if(channel == 3)
            {
              tmr_channel_enable(tmr_x, TMR_SELECT_CHANNEL_3C, FALSE);
            }
        }
        else
        {
            if(channel == 1)
            {
              tmr_channel_enable(tmr_x, TMR_SELECT_CHANNEL_1C, TRUE);
            }
            else if(channel == 2)
            {
              tmr_channel_enable(tmr_x, TMR_SELECT_CHANNEL_2C, TRUE);
            }
            else if(channel == 3)
            {
              tmr_channel_enable(tmr_x, TMR_SELECT_CHANNEL_3C, TRUE);
            }
        }
    }

    /* tmr_x enable counter */
    tmr_counter_enable(tmr_x, TRUE);

    return RT_EOK;
}

static rt_err_t drv_pwm_get(tmr_type* tmr_x, struct rt_pwm_configuration *configuration)
{
    crm_clocks_freq_type clocks_struct;
    rt_uint32_t pr, div, c1dt, c2dt, c3dt, c4dt;
    rt_uint32_t pclk1_doubler = 0, pclk2_doubler = 0;
    rt_uint32_t channel = configuration->channel;
    rt_uint64_t tmr_clock;

    pr = tmr_x->pr;
    div = tmr_x->div;
    c1dt = tmr_x->c1dt;
    c2dt = tmr_x->c2dt;
    c3dt = tmr_x->c3dt;
    c4dt = tmr_x->c4dt;

    tmr_pclk_get(&pclk1_doubler, &pclk2_doubler);

    crm_clocks_freq_get(&clocks_struct);

    if((tmr_x == TMR1) || (tmr_x == TMR8) || (tmr_x == TMR9) || (tmr_x == TMR10) || (tmr_x == TMR11))
    {
        tmr_clock = clocks_struct.apb2_freq * pclk2_doubler;
    }
    else
    {
        tmr_clock = clocks_struct.apb1_freq * pclk1_doubler;
    }

    /* convert nanosecond to frequency and duty cycle. */
    tmr_clock /= 1000000UL;
    configuration->period = (pr + 1) * (div + 1) * 1000UL / tmr_clock;

    if(channel == 1)
        configuration->pulse = (c1dt) * (div + 1) * 1000UL / tmr_clock;
    if(channel == 2)
        configuration->pulse = (c2dt) * (div + 1) * 1000UL / tmr_clock;
    if(channel == 3)
        configuration->pulse = (c3dt) * (div + 1) * 1000UL / tmr_clock;
    if(channel == 4)
        configuration->pulse = (c4dt) * (div + 1) * 1000UL / tmr_clock;

    return RT_EOK;
}

static rt_err_t drv_pwm_set(tmr_type* tmr_x, struct rt_pwm_configuration *configuration)
{
    crm_clocks_freq_type clocks_struct;
    tmr_output_config_type tmr_oc_config_struct;
    tmr_channel_select_type channel_select;
    rt_uint32_t period, pulse, channel, tmr_clock;
    rt_uint32_t pclk1_doubler = 0, pclk2_doubler = 0;
    rt_uint64_t psc;

    /* init timer pin and enable clock */
    at32_msp_tmr_init(tmr_x);

    tmr_pclk_get(&pclk1_doubler, &pclk2_doubler);

    crm_clocks_freq_get(&clocks_struct);

    if((tmr_x == TMR1) || (tmr_x == TMR8) || (tmr_x == TMR9) || (tmr_x == TMR10) || (tmr_x == TMR11))
    {
        tmr_clock = clocks_struct.apb2_freq * pclk2_doubler;
    }
    else
    {
        tmr_clock = clocks_struct.apb1_freq * pclk1_doubler;
    }

    /* convert nanosecond to frequency and duty cycle. */
    tmr_clock /= 1000000UL;
    /* calculate pwm period */
    period = (unsigned long long)configuration->period * tmr_clock / 1000ULL;;
    psc = period / MAX_PERIOD + 1;
    period = period / psc;
    /* calculate pulse width */
    pulse = (unsigned long long)configuration->pulse * tmr_clock / psc / 1000ULL;
    /* get channel parameter */
    channel = configuration->channel;

    /* tmr base init */
    tmr_base_init(tmr_x, period - 1, psc - 1);
    tmr_clock_source_div_set(tmr_x, TMR_CLOCK_DIV1);

    /* pwm mode configuration */
    tmr_output_default_para_init(&tmr_oc_config_struct);
    /* config pwm mode */
    tmr_oc_config_struct.oc_mode = TMR_OUTPUT_CONTROL_PWM_MODE_A;

    if (!configuration->complementary)
    {
        tmr_oc_config_struct.oc_idle_state = FALSE;
        tmr_oc_config_struct.oc_output_state = FALSE;
        tmr_oc_config_struct.oc_polarity = TMR_OUTPUT_ACTIVE_HIGH;
    }
    else
    {
        tmr_oc_config_struct.occ_idle_state = FALSE;
        tmr_oc_config_struct.occ_output_state = FALSE;
        tmr_oc_config_struct.occ_polarity = TMR_OUTPUT_ACTIVE_HIGH;
    }

    if(channel == 1)
    {
        channel_select = TMR_SELECT_CHANNEL_1;
    }
    else if(channel == 2)
    {
        channel_select = TMR_SELECT_CHANNEL_2;
    }
    else if(channel == 3)
    {
        channel_select = TMR_SELECT_CHANNEL_3;
    }
    else if(channel == 4)
    {
        channel_select = TMR_SELECT_CHANNEL_4;
    }

    /* config tmr pwm output */
    tmr_output_channel_config(tmr_x, channel_select, &tmr_oc_config_struct);
    tmr_output_channel_buffer_enable(tmr_x, channel_select, TRUE);
    tmr_channel_value_set(tmr_x, channel_select, pulse);
    /* enable tmr period buffer */
    tmr_period_buffer_enable(tmr_x, TRUE);
    /* enable output */
    tmr_output_enable(tmr_x, TRUE);

    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;
    tmr_type *tmr_x = (tmr_type *)device->parent.user_data;

    switch (cmd)
    {
    case PWMN_CMD_ENABLE:
        configuration->complementary = RT_TRUE;
    case PWM_CMD_ENABLE:
        return drv_pwm_enable(tmr_x, configuration, RT_TRUE);
    case PWMN_CMD_DISABLE:
        configuration->complementary = RT_FALSE;
    case PWM_CMD_DISABLE:
        return drv_pwm_enable(tmr_x, configuration, RT_FALSE);
    case PWM_CMD_SET:
        return drv_pwm_set(tmr_x, configuration);
    case PWM_CMD_GET:
        return drv_pwm_get(tmr_x, configuration);
    default:
        return RT_EINVAL;
    }
}

static void pwm_get_channel(void)
{
#ifdef BSP_USING_PWM1_CH1
    at32_pwm_obj[PWM1_INDEX].channel = 1;
#endif
#ifdef BSP_USING_PWM1_CH2
    at32_pwm_obj[PWM1_INDEX].channel = 2;
#endif
#ifdef BSP_USING_PWM1_CH3
    at32_pwm_obj[PWM1_INDEX].channel = 3;
#endif
#ifdef BSP_USING_PWM1_CH4
    at32_pwm_obj[PWM1_INDEX].channel = 4;
#endif
#ifdef BSP_USING_PWM2_CH1
    at32_pwm_obj[PWM2_INDEX].channel = 1;
#endif
#ifdef BSP_USING_PWM2_CH2
    at32_pwm_obj[PWM2_INDEX].channel = 2;
#endif
#ifdef BSP_USING_PWM2_CH3
    at32_pwm_obj[PWM2_INDEX].channel = 3;
#endif
#ifdef BSP_USING_PWM2_CH4
    at32_pwm_obj[PWM2_INDEX].channel = 4;
#endif
#ifdef BSP_USING_PWM3_CH1
    at32_pwm_obj[PWM3_INDEX].channel = 1;
#endif
#ifdef BSP_USING_PWM3_CH2
    at32_pwm_obj[PWM3_INDEX].channel = 2;
#endif
#ifdef BSP_USING_PWM3_CH3
    at32_pwm_obj[PWM3_INDEX].channel = 3;
#endif
#ifdef BSP_USING_PWM3_CH4
    at32_pwm_obj[PWM3_INDEX].channel = 4;
#endif
#ifdef BSP_USING_PWM4_CH1
    at32_pwm_obj[PWM4_INDEX].channel = 1;
#endif
#ifdef BSP_USING_PWM4_CH2
    at32_pwm_obj[PWM4_INDEX].channel = 2;
#endif
#ifdef BSP_USING_PWM4_CH3
    at32_pwm_obj[PWM4_INDEX].channel = 3;
#endif
#ifdef BSP_USING_PWM4_CH4
    at32_pwm_obj[PWM4_INDEX].channel = 4;
#endif
#ifdef BSP_USING_PWM5_CH1
    at32_pwm_obj[PWM5_INDEX].channel = 1;
#endif
#ifdef BSP_USING_PWM5_CH2
    at32_pwm_obj[PWM5_INDEX].channel = 2;
#endif
#ifdef BSP_USING_PWM5_CH3
    at32_pwm_obj[PWM5_INDEX].channel = 3;
#endif
#ifdef BSP_USING_PWM5_CH4
    at32_pwm_obj[PWM5_INDEX].channel = 4;
#endif
#ifdef BSP_USING_PWM6_CH1
    at32_pwm_obj[PWM6_INDEX].channel = 1;
#endif
#ifdef BSP_USING_PWM6_CH2
    at32_pwm_obj[PWM6_INDEX].channel = 2;
#endif
#ifdef BSP_USING_PWM6_CH3
    at32_pwm_obj[PWM6_INDEX].channel = 3;
#endif
#ifdef BSP_USING_PWM6_CH4
    at32_pwm_obj[PWM6_INDEX].channel = 4;
#endif
#ifdef BSP_USING_PWM7_CH1
    at32_pwm_obj[PWM7_INDEX].channel = 1;
#endif
#ifdef BSP_USING_PWM7_CH2
    at32_pwm_obj[PWM7_INDEX].channel = 2;
#endif
#ifdef BSP_USING_PWM7_CH3
    at32_pwm_obj[PWM7_INDEX].channel = 3;
#endif
#ifdef BSP_USING_PWM7_CH4
    at32_pwm_obj[PWM7_INDEX].channel = 4;
#endif
#ifdef BSP_USING_PWM8_CH1
    at32_pwm_obj[PWM8_INDEX].channel = 1;
#endif
#ifdef BSP_USING_PWM8_CH2
    at32_pwm_obj[PWM8_INDEX].channel = 2;
#endif
#ifdef BSP_USING_PWM8_CH3
    at32_pwm_obj[PWM8_INDEX].channel = 3;
#endif
#ifdef BSP_USING_PWM8_CH4
    at32_pwm_obj[PWM8_INDEX].channel = 4;
#endif
#ifdef BSP_USING_PWM9_CH1
    at32_pwm_obj[PWM9_INDEX].channel = 1;
#endif
#ifdef BSP_USING_PWM9_CH2
    at32_pwm_obj[PWM9_INDEX].channel = 2;
#endif
#ifdef BSP_USING_PWM9_CH3
    at32_pwm_obj[PWM9_INDEX].channel = 3;
#endif
#ifdef BSP_USING_PWM9_CH4
    at32_pwm_obj[PWM9_INDEX].channel = 4;
#endif
#ifdef BSP_USING_PWM12_CH1
    at32_pwm_obj[PWM12_INDEX].channel = 1;
#endif
#ifdef BSP_USING_PWM12_CH2
    at32_pwm_obj[PWM12_INDEX].channel = 2;
#endif
}

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

    pwm_get_channel();

    for(i = 0; i < sizeof(at32_pwm_obj) / sizeof(at32_pwm_obj[0]); i++)
    {
        if(rt_device_pwm_register(&at32_pwm_obj[i].pwm_device, at32_pwm_obj[i].name, &drv_ops, at32_pwm_obj[i].tmr_x) == RT_EOK)
        {
          LOG_D("%s register success", at32_pwm_obj[i].name);
        }
        else
        {
          LOG_D("%s register failed", at32_pwm_obj[i].name);
          result = -RT_ERROR;
        }
    }

    return result;
}

INIT_BOARD_EXPORT(rt_hw_pwm_init);

#endif /* RT_USING_PWM */