1.完成版本适配。
This commit is contained in:
131
device/pwm/pwm.c
131
device/pwm/pwm.c
@@ -10,22 +10,34 @@
|
||||
|
||||
#include "device/pwm/pwm.h"
|
||||
|
||||
#if (MR_CONF_PWM == MR_CONF_ENABLE)
|
||||
#if (MR_CFG_PWM == MR_CFG_ENABLE)
|
||||
|
||||
static mr_err_t err_io_pwm_configure(mr_pwm_t pwm, mr_pwm_config_t config)
|
||||
{
|
||||
return -MR_ERR_IO;
|
||||
}
|
||||
|
||||
static void err_io_pwm_write(mr_pwm_t pwm, mr_pos_t channel, mr_uint32_t duty)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
static mr_uint32_t err_io_pwm_read(mr_pwm_t pwm, mr_pos_t channel)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
static mr_err_t mr_pwm_open(mr_device_t device)
|
||||
{
|
||||
mr_pwm_t pwm = (mr_pwm_t)device;
|
||||
struct mr_pwm_config default_config = MR_PWM_CONFIG_DEFAULT;
|
||||
|
||||
/* Setting pwm to default config, if the frequency not set */
|
||||
/* Enable pwm using default config */
|
||||
if (pwm->config.freq == 0)
|
||||
{
|
||||
struct mr_pwm_config config = MR_PWM_CONFIG_DEFAULT;
|
||||
pwm->config = config;
|
||||
pwm->config = default_config;
|
||||
}
|
||||
|
||||
/* Check the frequency */
|
||||
mr_limit(pwm->config.freq, pwm->info.min_freq, pwm->info.max_freq);
|
||||
|
||||
return pwm->ops->configure(pwm, &pwm->config);
|
||||
}
|
||||
|
||||
@@ -33,8 +45,9 @@ static mr_err_t mr_pwm_close(mr_device_t device)
|
||||
{
|
||||
mr_pwm_t pwm = (mr_pwm_t)device;
|
||||
|
||||
/* Setting pwm to close config */
|
||||
/* Disable pwm */
|
||||
pwm->config.freq = 0;
|
||||
pwm->config.channel.mask = 0;
|
||||
|
||||
return pwm->ops->configure(pwm, &pwm->config);
|
||||
}
|
||||
@@ -44,17 +57,13 @@ static mr_err_t mr_pwm_ioctl(mr_device_t device, int cmd, void *args)
|
||||
mr_pwm_t pwm = (mr_pwm_t)device;
|
||||
mr_err_t ret = MR_ERR_OK;
|
||||
|
||||
switch (cmd & _MR_CTRL_FLAG_MASK)
|
||||
switch (cmd & MR_CTRL_FLAG_MASK)
|
||||
{
|
||||
case MR_CTRL_SET_CONFIG:
|
||||
{
|
||||
if (args)
|
||||
{
|
||||
struct mr_pwm_config *config = (struct mr_pwm_config *)args;
|
||||
|
||||
/* Check the frequency */
|
||||
mr_limit(config->freq, pwm->info.min_freq, pwm->info.max_freq);
|
||||
|
||||
mr_pwm_config_t config = (mr_pwm_config_t)args;
|
||||
ret = pwm->ops->configure(pwm, config);
|
||||
if (ret == MR_ERR_OK)
|
||||
{
|
||||
@@ -65,6 +74,17 @@ static mr_err_t mr_pwm_ioctl(mr_device_t device, int cmd, void *args)
|
||||
return -MR_ERR_INVALID;
|
||||
}
|
||||
|
||||
case MR_CTRL_GET_CONFIG:
|
||||
{
|
||||
if (args)
|
||||
{
|
||||
mr_pwm_config_t config = (mr_pwm_config_t)args;
|
||||
*config = pwm->config;
|
||||
return MR_ERR_OK;
|
||||
}
|
||||
return -MR_ERR_INVALID;
|
||||
}
|
||||
|
||||
default:
|
||||
return -MR_ERR_UNSUPPORTED;
|
||||
}
|
||||
@@ -76,12 +96,7 @@ static mr_err_t mr_pwm_read(mr_device_t device, mr_pos_t pos, void *buffer, mr_s
|
||||
mr_uint32_t *read_buffer = (mr_uint32_t *)buffer;
|
||||
mr_size_t read_size = 0;
|
||||
|
||||
if (size < sizeof(*read_buffer))
|
||||
{
|
||||
return -MR_ERR_INVALID;
|
||||
}
|
||||
|
||||
for (read_size = 0; read_size < size; read_size += sizeof(*read_buffer))
|
||||
while ((read_size += sizeof(*read_buffer)) <= size)
|
||||
{
|
||||
*read_buffer = pwm->ops->read(pwm, pos);
|
||||
read_buffer++;
|
||||
@@ -96,12 +111,7 @@ static mr_err_t mr_pwm_write(mr_device_t device, mr_pos_t pos, const void *buffe
|
||||
mr_uint32_t *write_buffer = (mr_uint32_t *)buffer;
|
||||
mr_size_t write_size = 0;
|
||||
|
||||
if (size < sizeof(*write_buffer))
|
||||
{
|
||||
return -MR_ERR_INVALID;
|
||||
}
|
||||
|
||||
for (write_size = 0; write_size < size; write_size += sizeof(*write_buffer))
|
||||
while ((write_size += sizeof(*write_buffer)) <= size)
|
||||
{
|
||||
pwm->ops->write(pwm, pos, *write_buffer);
|
||||
write_buffer++;
|
||||
@@ -110,58 +120,43 @@ static mr_err_t mr_pwm_write(mr_device_t device, mr_pos_t pos, const void *buffe
|
||||
return (mr_ssize_t)write_size;
|
||||
}
|
||||
|
||||
static mr_err_t _err_io_pwm_configure(mr_pwm_t pwm, struct mr_pwm_config *config)
|
||||
/**
|
||||
* @brief This function adds the pwm device.
|
||||
*
|
||||
* @param pwm The pwm device to be added.
|
||||
* @param name The name of the pwm device.
|
||||
* @param ops The operations of the pwm device.
|
||||
* @param data The private data of the pwm device.
|
||||
*
|
||||
* @return MR_ERR_OK on success, otherwise an error code.
|
||||
*/
|
||||
mr_err_t mr_pwm_device_add(mr_pwm_t pwm, const char *name, struct mr_pwm_ops *ops, void *data)
|
||||
{
|
||||
MR_ASSERT(0);
|
||||
return -MR_ERR_IO;
|
||||
}
|
||||
|
||||
static mr_err_t _err_io_pwm_write(mr_pwm_t pwm, mr_pos_t channel, mr_uint32_t duty)
|
||||
{
|
||||
MR_ASSERT(0);
|
||||
return -MR_ERR_IO;
|
||||
}
|
||||
|
||||
static mr_uint32_t _err_io_pwm_read(mr_pwm_t pwm, mr_pos_t channel)
|
||||
{
|
||||
MR_ASSERT(0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
mr_err_t mr_pwm_device_add(mr_pwm_t pwm, const char *name, void *data, struct mr_pwm_ops *ops, struct mr_pwm_info *info)
|
||||
{
|
||||
const static struct mr_device_ops device_ops =
|
||||
{
|
||||
mr_pwm_open,
|
||||
mr_pwm_close,
|
||||
mr_pwm_ioctl,
|
||||
mr_pwm_read,
|
||||
mr_pwm_write,
|
||||
};
|
||||
static struct mr_device_ops device_ops =
|
||||
{
|
||||
mr_pwm_open,
|
||||
mr_pwm_close,
|
||||
mr_pwm_ioctl,
|
||||
mr_pwm_read,
|
||||
mr_pwm_write,
|
||||
};
|
||||
|
||||
MR_ASSERT(pwm != MR_NULL);
|
||||
MR_ASSERT(name != MR_NULL);
|
||||
MR_ASSERT(ops != MR_NULL);
|
||||
MR_ASSERT(info != MR_NULL);
|
||||
MR_ASSERT(info->min_freq > 0);
|
||||
MR_ASSERT(info->max_freq >= info->min_freq);
|
||||
|
||||
/* Initialize the private fields */
|
||||
pwm->device.type = MR_DEVICE_TYPE_PWM;
|
||||
pwm->device.data = data;
|
||||
pwm->device.ops = &device_ops;
|
||||
|
||||
pwm->config.freq = 0;
|
||||
pwm->info = *info;
|
||||
pwm->config.channel.mask = 0;
|
||||
|
||||
/* Set operations as protection-ops if ops is null */
|
||||
ops->configure = ops->configure ? ops->configure : _err_io_pwm_configure;
|
||||
ops->write = ops->write ? ops->write : _err_io_pwm_write;
|
||||
ops->read = ops->read ? ops->read : _err_io_pwm_read;
|
||||
/* Protect every operation of the pwm device */
|
||||
ops->configure = ops->configure ? ops->configure : err_io_pwm_configure;
|
||||
ops->write = ops->write ? ops->write : err_io_pwm_write;
|
||||
ops->read = ops->read ? ops->read : err_io_pwm_read;
|
||||
pwm->ops = ops;
|
||||
|
||||
/* Add to the container */
|
||||
return mr_device_add(&pwm->device, name, MR_OPEN_RDWR);
|
||||
/* Add the device */
|
||||
return mr_device_add(&pwm->device, name, Mr_Device_Type_PWM, MR_OPEN_RDWR, &device_ops, data);
|
||||
}
|
||||
|
||||
#endif /* MR_CONF_PWM */
|
||||
#endif
|
||||
@@ -13,12 +13,17 @@
|
||||
|
||||
#include "mrlib.h"
|
||||
|
||||
#if (MR_CONF_PWM == MR_CONF_ENABLE)
|
||||
#if (MR_CFG_PWM == MR_CFG_ENABLE)
|
||||
|
||||
/**
|
||||
* @def PWM device mode
|
||||
*/
|
||||
#define MR_PWM_MODE_NORMAL 0
|
||||
#define MR_PWM_MODE_COMPLEMENTARY 1
|
||||
|
||||
/* Default config for mr_pwm_config structure */
|
||||
/**
|
||||
* @def PWM device default config
|
||||
*/
|
||||
#define MR_PWM_CONFIG_DEFAULT \
|
||||
{ \
|
||||
1000, \
|
||||
@@ -27,83 +32,44 @@
|
||||
0, \
|
||||
}
|
||||
|
||||
/**
|
||||
* @struct PWM device config
|
||||
*/
|
||||
struct mr_pwm_config
|
||||
{
|
||||
mr_uint32_t freq;
|
||||
mr_uint8_t mode;
|
||||
mr_uint32_t dead_time;
|
||||
union
|
||||
{
|
||||
struct
|
||||
{
|
||||
mr_pos_t channel0: 1;
|
||||
mr_pos_t channel1: 1;
|
||||
mr_pos_t channel2: 1;
|
||||
mr_pos_t channel3: 1;
|
||||
mr_pos_t channel4: 1;
|
||||
mr_pos_t channel5: 1;
|
||||
mr_pos_t channel6: 1;
|
||||
mr_pos_t channel7: 1;
|
||||
mr_pos_t channel8: 1;
|
||||
mr_pos_t channel9: 1;
|
||||
mr_pos_t channel10: 1;
|
||||
mr_pos_t channel11: 1;
|
||||
mr_pos_t channel12: 1;
|
||||
mr_pos_t channel13: 1;
|
||||
mr_pos_t channel14: 1;
|
||||
mr_pos_t channel15: 1;
|
||||
mr_pos_t channel16: 1;
|
||||
mr_pos_t channel17: 1;
|
||||
mr_pos_t channel18: 1;
|
||||
mr_pos_t channel19: 1;
|
||||
mr_pos_t channel20: 1;
|
||||
mr_pos_t channel21: 1;
|
||||
mr_pos_t channel22: 1;
|
||||
mr_pos_t channel23: 1;
|
||||
mr_pos_t channel24: 1;
|
||||
mr_pos_t channel25: 1;
|
||||
mr_pos_t channel26: 1;
|
||||
mr_pos_t channel27: 1;
|
||||
mr_pos_t channel28: 1;
|
||||
mr_pos_t channel29: 1;
|
||||
mr_pos_t channel30: 1;
|
||||
mr_pos_t channel31: 1;
|
||||
};
|
||||
mr_pos_t _channel_mask;
|
||||
};
|
||||
};
|
||||
|
||||
struct mr_pwm_info
|
||||
{
|
||||
mr_uint32_t max_freq;
|
||||
mr_uint32_t min_freq;
|
||||
struct mr_device_channel channel;
|
||||
};
|
||||
typedef struct mr_pwm_config *mr_pwm_config_t;
|
||||
|
||||
typedef struct mr_pwm *mr_pwm_t;
|
||||
|
||||
/**
|
||||
* @struct PWM device operations
|
||||
*/
|
||||
struct mr_pwm_ops
|
||||
{
|
||||
mr_err_t (*configure)(mr_pwm_t pwm, struct mr_pwm_config *config);
|
||||
mr_err_t (*write)(mr_pwm_t pwm, mr_pos_t channel, mr_uint32_t duty);
|
||||
mr_err_t (*configure)(mr_pwm_t pwm, mr_pwm_config_t config);
|
||||
void (*write)(mr_pwm_t pwm, mr_pos_t channel, mr_uint32_t duty);
|
||||
mr_uint32_t (*read)(mr_pwm_t pwm, mr_pos_t channel);
|
||||
};
|
||||
|
||||
/**
|
||||
* @struct PWM device
|
||||
*/
|
||||
struct mr_pwm
|
||||
{
|
||||
struct mr_device device;
|
||||
|
||||
struct mr_pwm_config config;
|
||||
struct mr_pwm_info info;
|
||||
|
||||
const struct mr_pwm_ops *ops;
|
||||
};
|
||||
|
||||
mr_err_t mr_pwm_device_add(mr_pwm_t pwm,
|
||||
const char *name,
|
||||
void *data,
|
||||
struct mr_pwm_ops *ops,
|
||||
struct mr_pwm_info *info);
|
||||
mr_err_t mr_pwm_device_add(mr_pwm_t pwm, const char *name, struct mr_pwm_ops *ops, void *data);
|
||||
|
||||
#endif /* MR_CONF_PWM */
|
||||
#endif
|
||||
|
||||
#endif /* _PWM_H_ */
|
||||
#endif /* _PWM_H_ */
|
||||
|
||||
Reference in New Issue
Block a user