From 6cce19824b2cf13ed2153025e1dfd44f81e8a536 Mon Sep 17 00:00:00 2001 From: YOUR_NAME Date: Mon, 15 Nov 2021 09:58:34 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=8E=E4=B8=BB=E7=BA=BF=E5=90=8C=E6=AD=A5-P?= =?UTF-8?q?WM=203=E4=B8=AA=E5=A4=B1=E8=B4=A5=E6=A0=B7=E4=BE=8B=E5=A4=84?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: YOUR_NAME --- pwm/pwm_hi35xx.c | 62 ++++++++++++++++++++++++++---------------------- pwm/pwm_hi35xx.h | 7 +++--- 2 files changed, 38 insertions(+), 31 deletions(-) diff --git a/pwm/pwm_hi35xx.c b/pwm/pwm_hi35xx.c index fd90d1f..a77ae65 100644 --- a/pwm/pwm_hi35xx.c +++ b/pwm/pwm_hi35xx.c @@ -13,14 +13,14 @@ * limitations under the License. */ +#include "pwm_hi35xx.h" #include "device_resource_if.h" -#include "hdf_base.h" #include "hdf_log.h" #include "osal_io.h" #include "osal_mem.h" #include "pwm_core.h" -#include "pwm_hi35xx.h" -#include "pwm_if.h" + +#define HDF_LOG_TAG pwm_hi35xx struct HiPwm { struct PwmDev dev; @@ -32,48 +32,49 @@ struct HiPwm { int32_t HiPwmSetConfig(struct PwmDev *pwm, struct PwmConfig *config) { struct HiPwm *hp = (struct HiPwm *)pwm; - if (hp == NULL || hp->reg == NULL || config == NULL) { HDF_LOGE("%s: hp reg or config is null", __func__); return HDF_ERR_INVALID_PARAM; } - - if (pwm->cfg.polarity != config->polarity && !(hp->supportPolarity)) { - HDF_LOGE("%s: not support set pwm polarity", __func__); - return HDF_ERR_NOT_SUPPORT; - } - - if (config->status == PWM_DISABLE_STATUS) { - HiPwmDisable(hp->reg); - return HDF_SUCCESS; - } - if (config->polarity != PWM_NORMAL_POLARITY && config->polarity != PWM_INVERTED_POLARITY) { HDF_LOGE("%s: polarity %u is invalid", __func__, config->polarity); return HDF_ERR_INVALID_PARAM; } - if (config->period < PWM_MIN_PERIOD) { - HDF_LOGE("%s: period %u is not support, min period %u", __func__, config->period, PWM_MIN_PERIOD); + HDF_LOGE("%s: period %u is not support, min period %d", __func__, config->period, PWM_MIN_PERIOD); return HDF_ERR_INVALID_PARAM; } if (config->duty < 1 || config->duty > config->period) { - HDF_LOGE("%s: duty %u is not support, min dutyCycle 1 max dutyCycle %u", + HDF_LOGE("%s: duty %u is not support, duty must in [1, period = %u].", __func__, config->duty, config->period); return HDF_ERR_INVALID_PARAM; } - HiPwmDisable(hp->reg); + HDF_LOGI("%s: [HiPwmDisable] done.", __func__); if (pwm->cfg.polarity != config->polarity && hp->supportPolarity) { HiPwmSetPolarity(hp->reg, config->polarity); - } - HiPwmSetPeriod(hp->reg, config->period); - HiPwmSetDuty(hp->reg, config->duty); - if (config->number == 0) { - HiPwmAlwaysOutput(hp->reg); - } else { - HiPwmOutputNumberSquareWaves(hp->reg, config->number); - } + HDF_LOGI("%s: [HiPwmSetPolarity] done, polarity: %u -> %u.", __func__, pwm->cfg.polarity, config->polarity); + } + if (pwm->cfg.period != config->period) { + HiPwmSetPeriod(hp->reg, config->period); + HDF_LOGI("%s: [HiPwmSetPeriod] done, period: %u -> %u", __func__, pwm->cfg.period, config->period); + } + if (pwm->cfg.duty != config->duty) { + HiPwmSetDuty(hp->reg, config->duty); + HDF_LOGI("%s: [HiPwmSetDuty] done, duty: %u -> %u", __func__, pwm->cfg.duty, config->duty); + } + if (config->status == PWM_ENABLE_STATUS) { + if (config->number == 0) { + HiPwmAlwaysOutput(hp->reg); + HDF_LOGI("%s: [HiPwmAlwaysOutput] done, then enable.", __func__); + } else { + HiPwmOutputNumberSquareWaves(hp->reg, config->number); + HDF_LOGI("%s: [HiPwmOutputNumberSquareWaves] done, then enable.", __func__); + } + } + HDF_LOGI("%s: set PwmConfig done: number %u, period %u, duty %u, polarity %u, enable %u.", + __func__, config->number, config->period, config->duty, config->polarity, config->status); + HDF_LOGI("%s: success.", __func__); return HDF_SUCCESS; } @@ -117,7 +118,7 @@ static int32_t HiPwmProbe(struct HiPwm *hp, struct HdfDeviceObject *obj) } hp->reg = (struct HiPwmRegs *)hp->base; - hp->supportPolarity = false; + hp->supportPolarity = true; hp->dev.method = &g_pwmOps; hp->dev.cfg.duty = PWM_DEFAULT_DUTY_CYCLE; hp->dev.cfg.period = PWM_DEFAULT_PERIOD; @@ -127,8 +128,13 @@ static int32_t HiPwmProbe(struct HiPwm *hp, struct HdfDeviceObject *obj) hp->dev.busy = false; if (PwmDeviceAdd(obj, &(hp->dev)) != HDF_SUCCESS) { OsalIoUnmap((void *)hp->base); + HDF_LOGE("%s: [PwmDeviceAdd] failed.", __func__); return HDF_FAILURE; } + HDF_LOGI("%s: set PwmConfig: number %u, period %u, duty %u, polarity %u, enable %u.", __func__, + hp->dev.cfg.number, hp->dev.cfg.period, hp->dev.cfg.duty, hp->dev.cfg.polarity, hp->dev.cfg.status); + HDF_LOGI("%s: success.", __func__); + return HDF_SUCCESS; } diff --git a/pwm/pwm_hi35xx.h b/pwm/pwm_hi35xx.h index b0c3d42..50dc7ef 100644 --- a/pwm/pwm_hi35xx.h +++ b/pwm/pwm_hi35xx.h @@ -15,6 +15,7 @@ #ifndef PWM_HI35XX_H #define PWM_HI35XX_H +#include "hdf_base.h" #define PWM_CLK_HZ 3000000 // 3MHz #define PWM_CLK_PERIOD 333 // 333ns @@ -31,9 +32,9 @@ #define PWM_INV_OFFSET 1 #define PWM_KEEP_OFFSET 2 -#define PWM_DEFAULT_PERIOD 0x018F +#define PWM_DEFAULT_PERIOD 0x3E7 #define PWM_DEFAULT_POLARITY 0 -#define PWM_DEFAULT_DUTY_CYCLE 0x00C7 +#define PWM_DEFAULT_DUTY_CYCLE 0x14D struct HiPwmRegs { volatile uint32_t cfg0; @@ -47,7 +48,7 @@ struct HiPwmRegs { static inline void HiPwmDisable(struct HiPwmRegs *reg) { - reg->ctrl = PWM_DISABLE; + reg->ctrl &= ~1; } static inline void HiPwmAlwaysOutput(struct HiPwmRegs *reg) -- Gitee