drv_pwm.c 5.2 KB
Newer Older
Z
zohar123 已提交
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
/*
 * Copyright (c) 2006-2018, Synwit Technology Co.,Ltd.
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2018-12-10     Zohar_Lee    first version
 */

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

#define SWM320_PWM_DEVICE(pwm) (struct swm320_pwm_dev *)(pwm)

#define SWM320_PWM_TIMER_SET(time) ((time) / 1000.0 * 120)

struct swm320_pwm_dev
{
    struct rt_device_pwm parent;
    PWM_TypeDef *pwm_periph;
};

static rt_err_t swm320_pwm_enable(void *user_data,
                                  struct rt_pwm_configuration *cfg,
                                  rt_bool_t enable)
{
    rt_err_t ret = RT_EOK;

    if (RT_TRUE == enable)
    {
        if (2 == cfg->channel)
        {
            PWM_Start((PWM_TypeDef *)user_data, 1, 1);
        }
        if (1 == cfg->channel)
        {
            PWM_Start((PWM_TypeDef *)user_data, 0, 1);
        }
        if (0 == cfg->channel)
        {
            PWM_Start((PWM_TypeDef *)user_data, 1, 0);
        }
        if (3 == cfg->channel)
        {
            PWM_Start((PWM_TypeDef *)user_data, 0, 0);
        }
    }
    else if (RT_FALSE == enable)
    {
        if (2 == cfg->channel)
        {
            PWM_Stop((PWM_TypeDef *)user_data, 1, 1);
        }
        if (1 == cfg->channel)
        {
            PWM_Stop((PWM_TypeDef *)user_data, 0, 1);
        }
        if (0 == cfg->channel)
        {
            PWM_Stop((PWM_TypeDef *)user_data, 1, 0);
        }
        if (3 == cfg->channel)
        {
            PWM_Stop((PWM_TypeDef *)user_data, 0, 0);
        }
    }
    else
    {
        ret = RT_ERROR;
    }

    return ret;
}

static rt_err_t swm320_pwm_control(struct rt_device_pwm *device,
                                   int cmd,
                                   void *arg)
{
    rt_err_t ret = RT_EOK;
    struct swm320_pwm_dev *pwm = SWM320_PWM_DEVICE(device->parent.user_data);
    struct rt_pwm_configuration *cfg = (struct rt_pwm_configuration *)arg;

    RT_ASSERT(pwm != RT_NULL);

    switch (cmd)
    {
    case PWM_CMD_ENABLE:

        ret = swm320_pwm_enable((void *)pwm->pwm_periph, cfg, RT_TRUE);
        break;
    case PWM_CMD_DISABLE:

        ret = swm320_pwm_enable((void *)pwm->pwm_periph, cfg, RT_FALSE);
        break;
    case PWM_CMD_SET:
        PWM_SetHDuty(pwm->pwm_periph,
                     cfg->channel,
                     SWM320_PWM_TIMER_SET(cfg->pulse));
        PWM_SetCycle(pwm->pwm_periph,
                     cfg->channel,
                     SWM320_PWM_TIMER_SET(cfg->period));
        break;
    case PWM_CMD_GET:
        cfg->pulse = PWM_GetHDuty(pwm->pwm_periph, cfg->channel);
        break;
    default:
        break;
    }

    return ret;
}

const static struct rt_pwm_ops swm320_pwm_ops =
{
    swm320_pwm_control
};

int rt_hw_pwm_init(void)
{
    rt_err_t ret = RT_EOK;
    PWM_InitStructure PWM_initStruct;

    PWM_initStruct.clk_div = PWM_CLKDIV_1; /* F_PWM = 120M/1 = 120M */
    PWM_initStruct.mode = PWM_MODE_INDEP;  /* A路和B路独立输出 */
    PWM_initStruct.cycleA = SWM320_PWM_TIMER_SET(1000);
    PWM_initStruct.hdutyA = SWM320_PWM_TIMER_SET(500);
    PWM_initStruct.initLevelA = 1;
    PWM_initStruct.cycleB = SWM320_PWM_TIMER_SET(1000);
    PWM_initStruct.hdutyB = SWM320_PWM_TIMER_SET(250);
    PWM_initStruct.initLevelB = 1;
    PWM_initStruct.HEndAIEn = 0;
    PWM_initStruct.NCycleAIEn = 0;
    PWM_initStruct.HEndBIEn = 0;
    PWM_initStruct.NCycleBIEn = 0;

#ifdef BSP_USING_PWM0
    static struct swm320_pwm_dev pwm_dev0;
    pwm_dev0.pwm_periph = PWM0;
    PWM_Init(pwm_dev0.pwm_periph, &PWM_initStruct);
    PORT_Init(PORTA, PIN4, FUNMUX0_PWM0A_OUT, 0);
    PORT_Init(PORTA, PIN10, FUNMUX0_PWM0B_OUT, 0);
    ret = rt_device_pwm_register(&pwm_dev0.parent,
                                 "pwm0",
                                 &swm320_pwm_ops,
                                 &pwm_dev0);

#endif

#ifdef BSP_USING_PWM1
    static struct swm320_pwm_dev pwm_dev1;
    pwm_dev1.pwm_periph = PWM1;
    PWM_Init(pwm_dev1.pwm_periph, &PWM_initStruct);
    PORT_Init(PORTA, PIN5, FUNMUX1_PWM1A_OUT, 0);
    PORT_Init(PORTA, PIN9, FUNMUX1_PWM1B_OUT, 0);
    ret = rt_device_pwm_register(&pwm_dev1.parent,
                                 "pwm1",
                                 &swm320_pwm_ops,
                                 &pwm_dev1);
#endif

#ifdef BSP_USING_PWM2
    static struct swm320_pwm_dev pwm_dev2;
    pwm_dev2.pwm_periph = PWM2;
    PWM_Init(pwm_dev2.pwm_periph, &PWM_initStruct);
    PORT_Init(PORTP, PIN0, FUNMUX0_PWM2A_OUT, 0);
    PORT_Init(PORTP, PIN2, FUNMUX0_PWM2B_OUT, 0);
    ret = rt_device_pwm_register(&pwm_dev2.parent,
                                 "pwm2",
                                 &swm320_pwm_ops,
                                 &pwm_dev2);
#endif

#ifdef BSP_USING_PWM3
    static struct swm320_pwm_dev pwm_dev3;
    pwm_dev3.pwm_periph = PWM3;
    PWM_Init(pwm_dev3.pwm_periph, &PWM_initStruct);
    PORT_Init(PORTP, PIN1, FUNMUX1_PWM3A_OUT, 0);
    PORT_Init(PORTP, PIN3, FUNMUX1_PWM3B_OUT, 0);
    ret = rt_device_pwm_register(&pwm_dev3.parent,
                                 "pwm3",
                                 &swm320_pwm_ops,
                                 &pwm_dev3);
#endif

    return ret;
}
INIT_DEVICE_EXPORT(rt_hw_pwm_init);