1.更新内置命令名。2.移除驱动类型。3.设备回调支持每个设备描述符独立挂载,不限上限。4.offset修改为position,更强调写入位置概念。5.设备中断概念修正,父系设备中断不仅唤起自身的回调函数,同时也将递归唤醒子设备回调函数(更强调依赖关系)。6.增强doxygen规范,生成文档更完整。
This commit is contained in:
60
device/pwm.c
60
device/pwm.c
@@ -10,17 +10,15 @@
|
||||
|
||||
#ifdef MR_USING_PWM
|
||||
|
||||
static int pwm_channel_set_configure(struct mr_pwm *pwm, int channel, struct mr_pwm_config config)
|
||||
MR_INLINE int pwm_channel_set_configure(struct mr_pwm *pwm, int channel, struct mr_pwm_config config)
|
||||
{
|
||||
struct mr_pwm_ops *ops = (struct mr_pwm_ops *)pwm->dev.drv->ops;
|
||||
|
||||
/* Check channel is valid */
|
||||
if (channel < 0 || channel >= 32)
|
||||
{
|
||||
return MR_EINVAL;
|
||||
}
|
||||
|
||||
/* Configure the channel */
|
||||
int ret = ops->channel_configure(pwm, channel, config.state, config.polarity);
|
||||
if (ret < 0)
|
||||
{
|
||||
@@ -31,8 +29,6 @@ static int pwm_channel_set_configure(struct mr_pwm *pwm, int channel, struct mr_
|
||||
if (config.state == MR_ENABLE)
|
||||
{
|
||||
MR_BIT_SET(pwm->channel, (1 << channel));
|
||||
|
||||
/* Configure the polarity */
|
||||
if (config.polarity == MR_PWM_POLARITY_NORMAL)
|
||||
{
|
||||
MR_BIT_CLR(pwm->channel_polarity, (1 << channel));
|
||||
@@ -48,9 +44,8 @@ static int pwm_channel_set_configure(struct mr_pwm *pwm, int channel, struct mr_
|
||||
return MR_EOK;
|
||||
}
|
||||
|
||||
static int pwm_channel_get_configure(struct mr_pwm *pwm, int channel, struct mr_pwm_config *config)
|
||||
MR_INLINE int pwm_channel_get_configure(struct mr_pwm *pwm, int channel, struct mr_pwm_config *config)
|
||||
{
|
||||
/* Check channel is valid */
|
||||
if (channel < 0 || channel >= 32)
|
||||
{
|
||||
return MR_EINVAL;
|
||||
@@ -62,13 +57,11 @@ static int pwm_channel_get_configure(struct mr_pwm *pwm, int channel, struct mr_
|
||||
return MR_EOK;
|
||||
}
|
||||
|
||||
static int pwm_calculate(struct mr_pwm *pwm, uint32_t freq)
|
||||
MR_INLINE int pwm_calculate(struct mr_pwm *pwm, uint32_t freq)
|
||||
{
|
||||
uint32_t clk = pwm->info->clk, psc_max = pwm->info->prescaler_max, per_max = pwm->info->period_max;
|
||||
uint32_t psc_best = 1, per_best = 1;
|
||||
int error_min = INT32_MAX;
|
||||
|
||||
/* Check the clock and frequency */
|
||||
if ((clk == 0) || (freq == 0))
|
||||
{
|
||||
return MR_EINVAL;
|
||||
@@ -84,12 +77,12 @@ static int pwm_calculate(struct mr_pwm *pwm, uint32_t freq)
|
||||
per_best = MR_BOUND(product, 1, per_max);
|
||||
} else
|
||||
{
|
||||
int error_min = INT32_MAX;
|
||||
|
||||
/* Calculate the least error prescaler and period */
|
||||
for (uint32_t psc = MR_BOUND(product / per_max, 1, psc_max); psc < psc_max; psc++)
|
||||
{
|
||||
uint32_t per = MR_BOUND(product / psc, 1, per_max);
|
||||
|
||||
/* Calculate the frequency error */
|
||||
int error = (int)((clk / psc / per) - freq);
|
||||
|
||||
/* Found a valid and optimal solution */
|
||||
@@ -147,7 +140,7 @@ static int mr_pwm_close(struct mr_dev *dev)
|
||||
return ops->configure(pwm, MR_DISABLE);
|
||||
}
|
||||
|
||||
static ssize_t mr_pwm_read(struct mr_dev *dev, int off, void *buf, size_t size, int async)
|
||||
static ssize_t mr_pwm_read(struct mr_dev *dev, void *buf, size_t count)
|
||||
{
|
||||
struct mr_pwm *pwm = (struct mr_pwm *)dev;
|
||||
struct mr_pwm_ops *ops = (struct mr_pwm_ops *)dev->drv->ops;
|
||||
@@ -156,24 +149,23 @@ static ssize_t mr_pwm_read(struct mr_dev *dev, int off, void *buf, size_t size,
|
||||
|
||||
#ifdef MR_USING_PWM_CHANNEL_CHECK
|
||||
/* Check if the channel is enabled */
|
||||
if (MR_BIT_IS_SET(pwm->channel, (1 << off)) == MR_DISABLE)
|
||||
if (MR_BIT_IS_SET(pwm->channel, (1 << dev->position)) == MR_DISABLE)
|
||||
{
|
||||
return MR_EINVAL;
|
||||
}
|
||||
#endif /* MR_USING_PWM_CHANNEL_CHECK */
|
||||
|
||||
MR_BIT_CLR(size, sizeof(*rd_buf) - 1);
|
||||
for (rd_size = 0; rd_size < size; rd_size += sizeof(*rd_buf))
|
||||
for (rd_size = 0; rd_size < MR_ALIGN_DOWN(count, sizeof(*rd_buf)); rd_size += sizeof(*rd_buf))
|
||||
{
|
||||
/* Calculate the duty */
|
||||
uint32_t compare_value = ops->read(pwm, off);
|
||||
uint32_t compare_value = ops->read(pwm, dev->position);
|
||||
*rd_buf = (uint32_t)(((float)compare_value / (float)pwm->period) * 1000000.0f);
|
||||
rd_buf++;
|
||||
}
|
||||
return rd_size;
|
||||
}
|
||||
|
||||
static ssize_t mr_pwm_write(struct mr_dev *dev, int off, const void *buf, size_t size, int async)
|
||||
static ssize_t mr_pwm_write(struct mr_dev *dev, const void *buf, size_t count)
|
||||
{
|
||||
struct mr_pwm *pwm = (struct mr_pwm *)dev;
|
||||
struct mr_pwm_ops *ops = (struct mr_pwm_ops *)dev->drv->ops;
|
||||
@@ -182,39 +174,38 @@ static ssize_t mr_pwm_write(struct mr_dev *dev, int off, const void *buf, size_t
|
||||
|
||||
#ifdef MR_USING_PWM_CHANNEL_CHECK
|
||||
/* Check if the channel is enabled */
|
||||
if (MR_BIT_IS_SET(pwm->channel, (1 << off)) == MR_DISABLE)
|
||||
if (MR_BIT_IS_SET(pwm->channel, (1 << dev->position)) == MR_DISABLE)
|
||||
{
|
||||
return MR_EINVAL;
|
||||
}
|
||||
#endif /* MR_USING_PWM_CHANNEL_CHECK */
|
||||
|
||||
MR_BIT_CLR(size, sizeof(*wr_buf) - 1);
|
||||
for (wr_size = 0; wr_size < size; wr_size += sizeof(*wr_buf))
|
||||
for (wr_size = 0; wr_size < MR_ALIGN_DOWN(count, sizeof(*wr_buf)); wr_size += sizeof(*wr_buf))
|
||||
{
|
||||
/* Calculate the compare value */
|
||||
uint32_t compare_value = MR_BOUND((uint32_t)(((float)*wr_buf / 1000000.0f) * (float)(pwm->period)),
|
||||
0,
|
||||
pwm->period);
|
||||
ops->write(pwm, off, compare_value);
|
||||
ops->write(pwm, dev->position, compare_value);
|
||||
wr_buf++;
|
||||
}
|
||||
return wr_size;
|
||||
}
|
||||
|
||||
static int mr_pwm_ioctl(struct mr_dev *dev, int off, int cmd, void *args)
|
||||
static int mr_pwm_ioctl(struct mr_dev *dev, int cmd, void *args)
|
||||
{
|
||||
struct mr_pwm *pwm = (struct mr_pwm *)dev;
|
||||
struct mr_pwm_ops *ops = (struct mr_pwm_ops *)dev->drv->ops;
|
||||
|
||||
switch (cmd)
|
||||
{
|
||||
case MR_CTL_PWM_SET_CHANNEL_CONFIG:
|
||||
case MR_IOC_PWM_SET_CHANNEL_CONFIG:
|
||||
{
|
||||
if (args != MR_NULL)
|
||||
{
|
||||
struct mr_pwm_config config = *((struct mr_pwm_config *)args);
|
||||
|
||||
int ret = pwm_channel_set_configure(pwm, off, config);
|
||||
int ret = pwm_channel_set_configure(pwm, dev->position, config);
|
||||
if (ret < 0)
|
||||
{
|
||||
return ret;
|
||||
@@ -223,7 +214,7 @@ static int mr_pwm_ioctl(struct mr_dev *dev, int off, int cmd, void *args)
|
||||
}
|
||||
return MR_EINVAL;
|
||||
}
|
||||
case MR_CTL_PWM_SET_FREQ:
|
||||
case MR_IOC_PWM_SET_FREQ:
|
||||
{
|
||||
if (args != MR_NULL)
|
||||
{
|
||||
@@ -257,14 +248,13 @@ static int mr_pwm_ioctl(struct mr_dev *dev, int off, int cmd, void *args)
|
||||
}
|
||||
return MR_EINVAL;
|
||||
}
|
||||
|
||||
case MR_CTL_PWM_GET_CHANNEL_CONFIG:
|
||||
case MR_IOC_PWM_GET_CHANNEL_CONFIG:
|
||||
{
|
||||
if (args != MR_NULL)
|
||||
{
|
||||
struct mr_pwm_config *config = ((struct mr_pwm_config *)args);
|
||||
|
||||
int ret = pwm_channel_get_configure(pwm, off, config);
|
||||
int ret = pwm_channel_get_configure(pwm, dev->position, config);
|
||||
if (ret < 0)
|
||||
{
|
||||
return ret;
|
||||
@@ -273,7 +263,7 @@ static int mr_pwm_ioctl(struct mr_dev *dev, int off, int cmd, void *args)
|
||||
}
|
||||
return MR_EINVAL;
|
||||
}
|
||||
case MR_CTL_PWM_GET_FREQ:
|
||||
case MR_IOC_PWM_GET_FREQ:
|
||||
{
|
||||
if (args != MR_NULL)
|
||||
{
|
||||
@@ -295,13 +285,13 @@ static int mr_pwm_ioctl(struct mr_dev *dev, int off, int cmd, void *args)
|
||||
* @brief This function registers a pwm.
|
||||
*
|
||||
* @param pwm The pwm.
|
||||
* @param name The name of the pwm.
|
||||
* @param path The path of the pwm.
|
||||
* @param drv The driver of the pwm.
|
||||
* @param info The information of the pwm.
|
||||
*
|
||||
* @return MR_EOK on success, otherwise an error code.
|
||||
* @return 0 on success, otherwise an error code.
|
||||
*/
|
||||
int mr_pwm_register(struct mr_pwm *pwm, const char *name, struct mr_drv *drv, struct mr_pwm_info *info)
|
||||
int mr_pwm_register(struct mr_pwm *pwm, const char *path, struct mr_drv *drv, struct mr_pwm_info *info)
|
||||
{
|
||||
static struct mr_dev_ops ops =
|
||||
{
|
||||
@@ -314,7 +304,7 @@ int mr_pwm_register(struct mr_pwm *pwm, const char *name, struct mr_drv *drv, st
|
||||
};
|
||||
|
||||
MR_ASSERT(pwm != MR_NULL);
|
||||
MR_ASSERT(name != MR_NULL);
|
||||
MR_ASSERT(path != MR_NULL);
|
||||
MR_ASSERT(drv != MR_NULL);
|
||||
MR_ASSERT(drv->ops != MR_NULL);
|
||||
MR_ASSERT(info != MR_NULL);
|
||||
@@ -328,7 +318,7 @@ int mr_pwm_register(struct mr_pwm *pwm, const char *name, struct mr_drv *drv, st
|
||||
pwm->info = info;
|
||||
|
||||
/* Register the pwm */
|
||||
return mr_dev_register(&pwm->dev, name, Mr_Dev_Type_PWM, MR_SFLAG_RDWR, &ops, drv);
|
||||
return mr_dev_register(&pwm->dev, path, MR_DEV_TYPE_PWM, MR_O_RDWR, &ops, drv);
|
||||
}
|
||||
|
||||
#endif /* MR_USING_PWM */
|
||||
|
||||
Reference in New Issue
Block a user