Files
mr-library/device/adc.c

182 lines
4.4 KiB
C
Raw Normal View History

2023-11-11 02:07:22 +08:00
/*
2024-01-02 00:02:48 +08:00
* @copyright (c) 2023-2024, MR Development Team
2023-11-11 02:07:22 +08:00
*
* @license SPDX-License-Identifier: Apache-2.0
*
* @date 2023-11-06 MacRsh First version
*/
2024-01-02 00:02:48 +08:00
#include "include/device/mr_adc.h"
2023-11-11 02:07:22 +08:00
#ifdef MR_USING_ADC
MR_INLINE int adc_channel_set_configure(struct mr_adc *adc, int channel, struct mr_adc_config config)
2023-12-10 16:21:48 +08:00
{
struct mr_adc_ops *ops = (struct mr_adc_ops *)adc->dev.drv->ops;
if (channel < 0 || channel >= 32)
{
return MR_EINVAL;
}
2024-01-16 04:03:40 +08:00
int ret = ops->channel_configure(adc, channel, config.state);
if (ret < 0)
2023-12-10 16:21:48 +08:00
{
return ret;
}
/* Enable or disable the channel */
2024-01-16 04:03:40 +08:00
if (config.state == MR_ENABLE)
2023-12-10 16:21:48 +08:00
{
MR_BIT_SET(adc->channel, (1 << channel));
2023-12-10 16:21:48 +08:00
} else
{
MR_BIT_CLR(adc->channel, (1 << channel));
2023-12-10 16:21:48 +08:00
}
return MR_EOK;
}
MR_INLINE int adc_channel_get_configure(struct mr_adc *adc, int channel, struct mr_adc_config *config)
2023-12-10 16:21:48 +08:00
{
if (channel < 0 || channel >= 32)
{
return MR_EINVAL;
}
2024-01-16 04:03:40 +08:00
/* Get configure */
config->state = MR_BIT_IS_SET(adc->channel, (1 << channel));
return MR_EOK;
2023-12-10 16:21:48 +08:00
}
2023-11-11 02:07:22 +08:00
static int mr_adc_open(struct mr_dev *dev)
{
struct mr_adc *adc = (struct mr_adc *)dev;
struct mr_adc_ops *ops = (struct mr_adc_ops *)dev->drv->ops;
return ops->configure(adc, MR_ENABLE);
}
static int mr_adc_close(struct mr_dev *dev)
{
struct mr_adc *adc = (struct mr_adc *)dev;
struct mr_adc_ops *ops = (struct mr_adc_ops *)dev->drv->ops;
2024-01-10 16:41:40 +08:00
#ifdef MR_USING_ADC_AUTO_DISABLE
2023-11-11 02:07:22 +08:00
/* Disable all channels */
2023-12-31 16:32:01 +08:00
for (size_t i = 0; i < 32; i++)
2023-11-11 02:07:22 +08:00
{
if (MR_BIT_IS_SET(adc->channel, (1 << i)) == MR_ENABLE)
2023-11-11 02:07:22 +08:00
{
2023-12-31 16:32:01 +08:00
ops->channel_configure(adc, (int)i, MR_DISABLE);
MR_BIT_CLR(adc->channel, (1 << i));
2023-11-11 02:07:22 +08:00
}
}
2024-01-10 16:41:40 +08:00
#endif /* MR_USING_ADC_AUTO_DISABLE */
2023-11-11 02:07:22 +08:00
return ops->configure(adc, MR_DISABLE);
}
static ssize_t mr_adc_read(struct mr_dev *dev, void *buf, size_t count)
2023-11-11 02:07:22 +08:00
{
struct mr_adc *adc = (struct mr_adc *)dev;
struct mr_adc_ops *ops = (struct mr_adc_ops *)dev->drv->ops;
uint32_t *rd_buf = (uint32_t *)buf;
2023-12-31 16:32:01 +08:00
ssize_t rd_size;
2023-11-11 02:07:22 +08:00
2024-01-10 16:41:40 +08:00
#ifdef MR_USING_ADC_CHANNEL_CHECK
2023-11-11 02:07:22 +08:00
/* Check if the channel is enabled */
if (MR_BIT_IS_SET(adc->channel, (1 << dev->position)) == MR_DISABLE)
2023-11-11 02:07:22 +08:00
{
return MR_EINVAL;
}
2024-01-10 16:41:40 +08:00
#endif /* MR_USING_ADC_CHANNEL_CHECK */
2023-11-11 02:07:22 +08:00
for (rd_size = 0; rd_size < MR_ALIGN_DOWN(count, sizeof(*rd_buf)); rd_size += sizeof(*rd_buf))
2023-11-11 02:07:22 +08:00
{
int ret = ops->read(adc, dev->position, rd_buf);
if (ret < 0)
{
return (rd_size == 0) ? ret : rd_size;
}
2023-11-11 02:07:22 +08:00
rd_buf++;
}
return rd_size;
}
static int mr_adc_ioctl(struct mr_dev *dev, int cmd, void *args)
2023-11-11 02:07:22 +08:00
{
struct mr_adc *adc = (struct mr_adc *)dev;
switch (cmd)
{
case MR_IOC_ADC_SET_CHANNEL_CONFIG:
2023-12-10 16:21:48 +08:00
{
if (args != MR_NULL)
{
struct mr_adc_config config = *((struct mr_adc_config *)args);
int ret = adc_channel_set_configure(adc, dev->position, config);
2024-01-16 04:03:40 +08:00
if (ret < 0)
{
return ret;
}
return sizeof(config);
2023-12-10 16:21:48 +08:00
}
return MR_EINVAL;
}
case MR_IOC_ADC_GET_CHANNEL_CONFIG:
2023-12-10 16:21:48 +08:00
{
if (args != MR_NULL)
{
struct mr_adc_config *config = (struct mr_adc_config *)args;
2023-11-11 02:07:22 +08:00
int ret = adc_channel_get_configure(adc, dev->position, config);
2023-12-10 16:21:48 +08:00
if (ret < 0)
2023-11-11 02:07:22 +08:00
{
2023-11-12 00:33:40 +08:00
return ret;
2023-11-11 02:07:22 +08:00
}
2024-01-16 04:03:40 +08:00
return sizeof(*config);
2023-11-11 02:07:22 +08:00
}
}
default:
{
return MR_ENOTSUP;
2023-11-11 02:07:22 +08:00
}
}
}
/**
* @brief This function registers an adc.
*
* @param adc The adc.
* @param path The path of the adc.
2023-11-11 02:07:22 +08:00
* @param drv The driver of the adc.
*
* @return 0 on success, otherwise an error code.
2023-11-11 02:07:22 +08:00
*/
int mr_adc_register(struct mr_adc *adc, const char *path, struct mr_drv *drv)
2023-11-11 02:07:22 +08:00
{
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);
MR_ASSERT(drv != MR_NULL);
MR_ASSERT(drv->ops != MR_NULL);
2023-11-11 02:07:22 +08:00
/* Initialize the fields */
adc->channel = 0;
/* Register the adc */
return mr_dev_register(&adc->dev, path, MR_DEV_TYPE_ADC, MR_O_RDONLY, &ops, drv);
2023-11-11 02:07:22 +08:00
}
#endif /* MR_USING_ADC */