1.编码风格修改,重新格式化。
This commit is contained in:
88
device/adc.c
88
device/adc.c
@@ -10,41 +10,40 @@
|
||||
|
||||
#ifdef MR_USING_ADC
|
||||
|
||||
MR_INLINE int adc_channel_set_configure(struct mr_adc *adc, int channel, struct mr_adc_config config)
|
||||
MR_INLINE int adc_channel_set_configure(struct mr_adc *adc,
|
||||
int channel,
|
||||
struct mr_adc_config config)
|
||||
{
|
||||
struct mr_adc_ops *ops = (struct mr_adc_ops *)adc->dev.drv->ops;
|
||||
|
||||
if (channel < 0 || channel >= 32)
|
||||
{
|
||||
if ((channel < 0) || (channel >= (sizeof(adc->channels) * 8))) {
|
||||
return MR_EINVAL;
|
||||
}
|
||||
|
||||
int ret = ops->channel_configure(adc, channel, config.state);
|
||||
if (ret < 0)
|
||||
{
|
||||
if (ret < 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Enable or disable the channel */
|
||||
if (config.state == MR_ENABLE)
|
||||
{
|
||||
MR_BIT_SET(adc->channel, (1 << channel));
|
||||
} else
|
||||
{
|
||||
MR_BIT_CLR(adc->channel, (1 << channel));
|
||||
if (config.state == MR_ENABLE) {
|
||||
MR_BIT_SET(adc->channels, (1 << channel));
|
||||
} else {
|
||||
MR_BIT_CLR(adc->channels, (1 << channel));
|
||||
}
|
||||
return MR_EOK;
|
||||
}
|
||||
|
||||
MR_INLINE int adc_channel_get_configure(struct mr_adc *adc, int channel, struct mr_adc_config *config)
|
||||
MR_INLINE int adc_channel_get_configure(struct mr_adc *adc,
|
||||
int channel,
|
||||
struct mr_adc_config *config)
|
||||
{
|
||||
if (channel < 0 || channel >= 32)
|
||||
{
|
||||
if ((channel < 0) || (channel >= (sizeof(adc->channels) * 8))) {
|
||||
return MR_EINVAL;
|
||||
}
|
||||
|
||||
/* Get configure */
|
||||
config->state = MR_BIT_IS_SET(adc->channel, (1 << channel));
|
||||
config->state = MR_BIT_IS_SET(adc->channels, (1 << channel));
|
||||
return MR_EOK;
|
||||
}
|
||||
|
||||
@@ -63,12 +62,10 @@ static int mr_adc_close(struct mr_dev *dev)
|
||||
|
||||
#ifdef MR_USING_ADC_AUTO_DISABLE
|
||||
/* Disable all channels */
|
||||
for (size_t i = 0; i < 32; i++)
|
||||
{
|
||||
if (MR_BIT_IS_SET(adc->channel, (1 << i)) == MR_ENABLE)
|
||||
{
|
||||
for (size_t i = 0; i < (sizeof(adc->channels) * 8); i++) {
|
||||
if (MR_BIT_IS_SET(adc->channels, (1 << i)) == MR_ENABLE) {
|
||||
ops->channel_configure(adc, (int)i, MR_DISABLE);
|
||||
MR_BIT_CLR(adc->channel, (1 << i));
|
||||
MR_BIT_CLR(adc->channels, (1 << i));
|
||||
}
|
||||
}
|
||||
#endif /* MR_USING_ADC_AUTO_DISABLE */
|
||||
@@ -85,17 +82,14 @@ static ssize_t mr_adc_read(struct mr_dev *dev, void *buf, size_t count)
|
||||
|
||||
#ifdef MR_USING_ADC_CHANNEL_CHECK
|
||||
/* Check if the channel is enabled */
|
||||
if (MR_BIT_IS_SET(adc->channel, (1 << dev->position)) == MR_DISABLE)
|
||||
{
|
||||
if ((dev->position < 0) || (MR_BIT_IS_SET(adc->channels, (1 << dev->position)) == MR_DISABLE)) {
|
||||
return MR_EINVAL;
|
||||
}
|
||||
#endif /* MR_USING_ADC_CHANNEL_CHECK */
|
||||
|
||||
for (rd_size = 0; rd_size < MR_ALIGN_DOWN(count, sizeof(*rd_buf)); rd_size += sizeof(*rd_buf))
|
||||
{
|
||||
for (rd_size = 0; rd_size < MR_ALIGN_DOWN(count, sizeof(*rd_buf)); rd_size += sizeof(*rd_buf)) {
|
||||
int ret = ops->read(adc, dev->position, rd_buf);
|
||||
if (ret < 0)
|
||||
{
|
||||
if (ret < 0) {
|
||||
return (rd_size == 0) ? ret : rd_size;
|
||||
}
|
||||
rd_buf++;
|
||||
@@ -107,39 +101,32 @@ static int mr_adc_ioctl(struct mr_dev *dev, int cmd, void *args)
|
||||
{
|
||||
struct mr_adc *adc = (struct mr_adc *)dev;
|
||||
|
||||
switch (cmd)
|
||||
{
|
||||
case MR_IOC_ADC_SET_CHANNEL_CONFIG:
|
||||
{
|
||||
if (args != MR_NULL)
|
||||
{
|
||||
switch (cmd) {
|
||||
case MR_IOC_ADC_SET_CHANNEL_CONFIG: {
|
||||
if (args != MR_NULL) {
|
||||
struct mr_adc_config config = *((struct mr_adc_config *)args);
|
||||
|
||||
int ret = adc_channel_set_configure(adc, dev->position, config);
|
||||
if (ret < 0)
|
||||
{
|
||||
if (ret < 0) {
|
||||
return ret;
|
||||
}
|
||||
return sizeof(config);
|
||||
}
|
||||
return MR_EINVAL;
|
||||
}
|
||||
case MR_IOC_ADC_GET_CHANNEL_CONFIG:
|
||||
{
|
||||
if (args != MR_NULL)
|
||||
{
|
||||
case MR_IOC_ADC_GET_CHANNEL_CONFIG: {
|
||||
if (args != MR_NULL) {
|
||||
struct mr_adc_config *config = (struct mr_adc_config *)args;
|
||||
|
||||
int ret = adc_channel_get_configure(adc, dev->position, config);
|
||||
if (ret < 0)
|
||||
{
|
||||
if (ret < 0) {
|
||||
return ret;
|
||||
}
|
||||
return sizeof(*config);
|
||||
}
|
||||
return MR_EINVAL;
|
||||
}
|
||||
default:
|
||||
{
|
||||
default: {
|
||||
return MR_ENOTSUP;
|
||||
}
|
||||
}
|
||||
@@ -156,15 +143,12 @@ static int mr_adc_ioctl(struct mr_dev *dev, int cmd, void *args)
|
||||
*/
|
||||
int mr_adc_register(struct mr_adc *adc, const char *path, struct mr_drv *drv)
|
||||
{
|
||||
static struct mr_dev_ops ops =
|
||||
{
|
||||
mr_adc_open,
|
||||
mr_adc_close,
|
||||
mr_adc_read,
|
||||
MR_NULL,
|
||||
mr_adc_ioctl,
|
||||
MR_NULL
|
||||
};
|
||||
static struct mr_dev_ops ops = {mr_adc_open,
|
||||
mr_adc_close,
|
||||
mr_adc_read,
|
||||
MR_NULL,
|
||||
mr_adc_ioctl,
|
||||
MR_NULL};
|
||||
|
||||
MR_ASSERT(adc != MR_NULL);
|
||||
MR_ASSERT(path != MR_NULL);
|
||||
@@ -172,7 +156,7 @@ int mr_adc_register(struct mr_adc *adc, const char *path, struct mr_drv *drv)
|
||||
MR_ASSERT(drv->ops != MR_NULL);
|
||||
|
||||
/* Initialize the fields */
|
||||
adc->channel = 0;
|
||||
adc->channels = 0;
|
||||
|
||||
/* Register the adc */
|
||||
return mr_dev_register(&adc->dev, path, MR_DEV_TYPE_ADC, MR_O_RDONLY, &ops, drv);
|
||||
|
||||
Reference in New Issue
Block a user