From 8e718056542d2eca71c97c8600e719c530ca7998 Mon Sep 17 00:00:00 2001 From: MacRsh Date: Tue, 16 Jan 2024 04:11:54 +0800 Subject: [PATCH] =?UTF-8?q?1.pwm=E8=AF=BB=E5=86=99=E5=8F=82=E6=95=B0?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E7=94=B1float=E6=94=B9=E4=B8=BAuint32=5Ft?= =?UTF-8?q?=EF=BC=8C=E8=8C=83=E5=9B=B4=E7=94=B10-100%=E6=94=B9=E5=8F=98?= =?UTF-8?q?=E8=87=B30-1000000=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- device/pwm.c | 40 +++++++++++++++++++++------------------- include/device/mr_pwm.h | 12 +++--------- 2 files changed, 24 insertions(+), 28 deletions(-) diff --git a/device/pwm.c b/device/pwm.c index 32b6c5a..e6ec27e 100644 --- a/device/pwm.c +++ b/device/pwm.c @@ -22,13 +22,13 @@ static int pwm_channel_set_configure(struct mr_pwm *pwm, int channel, struct mr_ /* Configure the channel */ int ret = ops->channel_configure(pwm, channel, config.state, config.polarity); - if (ret != MR_EOK) + if (ret < 0) { return ret; } /* Enable or disable the channel */ - if (config.state == MR_PWM_ENABLE) + if (config.state == MR_ENABLE) { MR_BIT_SET(pwm->channel, (1 << channel)); @@ -59,8 +59,7 @@ static int pwm_channel_get_configure(struct mr_pwm *pwm, int channel, struct mr_ /* Get configure */ config->state = MR_BIT_IS_SET(pwm->channel, (1 << channel)); config->polarity = MR_BIT_IS_SET(pwm->channel_polarity, (1 << channel)); - - return config->state; + return MR_EOK; } static int pwm_calculate(struct mr_pwm *pwm, uint32_t freq) @@ -133,7 +132,7 @@ static int pwm_calculate(struct mr_pwm *pwm, uint32_t freq) pwm->prescaler = psc_best; pwm->period = per_best; - pwm->freq = clk / psc_best / per_best; + pwm->freq = (clk * 1000000) / psc_best / per_best; return MR_EOK; } @@ -169,7 +168,7 @@ static ssize_t mr_pwm_read(struct mr_dev *dev, int off, void *buf, size_t size, { struct mr_pwm *pwm = (struct mr_pwm *)dev; struct mr_pwm_ops *ops = (struct mr_pwm_ops *)dev->drv->ops; - float *rd_buf = (float *)buf; + uint32_t *rd_buf = (uint32_t *)buf; ssize_t rd_size; #ifdef MR_USING_PWM_CHANNEL_CHECK @@ -185,7 +184,7 @@ static ssize_t mr_pwm_read(struct mr_dev *dev, int off, void *buf, size_t size, { /* Calculate the duty */ uint32_t compare_value = ops->read(pwm, off); - *rd_buf = ((float)compare_value / (float)pwm->period) * 100.0f; + *rd_buf = (uint32_t)(((float)compare_value / (float)pwm->period) * 1000000.0f); rd_buf++; } return rd_size; @@ -195,7 +194,7 @@ static ssize_t mr_pwm_write(struct mr_dev *dev, int off, const void *buf, size_t { struct mr_pwm *pwm = (struct mr_pwm *)dev; struct mr_pwm_ops *ops = (struct mr_pwm_ops *)dev->drv->ops; - float *wr_buf = (float *)buf; + uint32_t *wr_buf = (uint32_t *)buf; ssize_t wr_size; #ifdef MR_USING_PWM_CHANNEL_CHECK @@ -210,7 +209,7 @@ static ssize_t mr_pwm_write(struct mr_dev *dev, int off, const void *buf, size_t for (wr_size = 0; wr_size < size; wr_size += sizeof(*wr_buf)) { /* Calculate the compare value */ - uint32_t compare_value = (uint32_t)((*wr_buf / 100.0f) * (float)(pwm->period)); + uint32_t compare_value = (uint32_t)(((float)*wr_buf / 1000000.0f) * (float)(pwm->period)); MR_BOUND(compare_value, 0, pwm->period); ops->write(pwm, off, compare_value); wr_buf++; @@ -218,7 +217,7 @@ static ssize_t mr_pwm_write(struct mr_dev *dev, int off, const void *buf, size_t return wr_size; } -static int mr_pwm_ioctl(struct mr_dev *dev, int off, int cmd, void *args) +static ssize_t mr_pwm_ioctl(struct mr_dev *dev, int off, int cmd, void *args) { struct mr_pwm *pwm = (struct mr_pwm *)dev; struct mr_pwm_ops *ops = (struct mr_pwm_ops *)dev->drv->ops; @@ -231,7 +230,12 @@ static int mr_pwm_ioctl(struct mr_dev *dev, int off, int cmd, void *args) { struct mr_pwm_config config = *((struct mr_pwm_config *)args); - return pwm_channel_set_configure(pwm, off, config); + int ret = pwm_channel_set_configure(pwm, off, config); + if (ret < 0) + { + return ret; + } + return sizeof(config); } return MR_EINVAL; } @@ -244,7 +248,7 @@ static int mr_pwm_ioctl(struct mr_dev *dev, int off, int cmd, void *args) /* Calculate prescaler and period */ int ret = pwm_calculate(pwm, freq); - if (ret != MR_EOK) + if (ret < 0) { return ret; } @@ -259,14 +263,13 @@ static int mr_pwm_ioctl(struct mr_dev *dev, int off, int cmd, void *args) { /* Get old duty */ uint32_t compare_value = ops->read(pwm, (int)i); - float duty = ((float)compare_value / (float)old_period) * 100.0f; /* Calculate new compare value */ - compare_value = (uint32_t)((duty / 100.0f) * (float)(pwm->period)); + compare_value = (uint32_t)(((float)compare_value / (float)old_period) * (float)(pwm->period)); ops->write(pwm, (int)i, compare_value); } } - return MR_EOK; + return sizeof(freq); } return MR_EINVAL; } @@ -282,7 +285,7 @@ static int mr_pwm_ioctl(struct mr_dev *dev, int off, int cmd, void *args) { return ret; } - return MR_EOK; + return sizeof(*config); } return MR_EINVAL; } @@ -293,11 +296,10 @@ static int mr_pwm_ioctl(struct mr_dev *dev, int off, int cmd, void *args) uint32_t *freq = (uint32_t *)args; *freq = pwm->freq; - return MR_EOK; + return sizeof(*freq); } return MR_EINVAL; } - default: { return MR_ENOTSUP; @@ -336,7 +338,7 @@ int mr_pwm_register(struct mr_pwm *pwm, const char *name, struct mr_drv *drv, st /* Initialize the fields */ pwm->freq = 0; pwm->prescaler = 0; - pwm->period = 0; + pwm->period = 1; pwm->channel = 0; pwm->channel_polarity = 0; pwm->info = info; diff --git a/include/device/mr_pwm.h b/include/device/mr_pwm.h index 0201a61..de98945 100644 --- a/include/device/mr_pwm.h +++ b/include/device/mr_pwm.h @@ -17,12 +17,6 @@ extern "C" { #ifdef MR_USING_PWM -/** - * @brief PWM channel state. - */ -#define MR_PWM_DISABLE MR_DISABLE /**< PWM disable */ -#define MR_PWM_ENABLE MR_ENABLE /**< PWM enable */ - /** * @brief PWM channel polarity. */ @@ -34,8 +28,8 @@ extern "C" { */ struct mr_pwm_config { - uint32_t state: 1; /**< Channel state */ - uint32_t polarity: 1; /**< Channel polarity */ + int state; /**< Channel state */ + int polarity; /**< Channel polarity */ }; /** @@ -52,7 +46,7 @@ struct mr_pwm_config /** * @brief PWM data type. */ -typedef float mr_pwm_data_t; /**< PWM read/write data type */ +typedef uint32_t mr_pwm_data_t; /**< PWM read/write data type */ /** * @brief PWM information structure.