1.设备框架参数更新。

This commit is contained in:
MacRsh
2024-01-16 04:03:40 +08:00
parent 2a79c055d0
commit 8abdd4d218
24 changed files with 285 additions and 410 deletions

View File

@@ -10,7 +10,7 @@
#ifdef MR_USING_ADC
static int adc_channel_set_state(struct mr_adc *adc, int channel, int state)
static 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;
@@ -21,14 +21,14 @@ static int adc_channel_set_state(struct mr_adc *adc, int channel, int state)
}
/* Configure the channel */
int ret = ops->channel_configure(adc, channel, state);
if (ret != MR_EOK)
int ret = ops->channel_configure(adc, channel, config.state);
if (ret < 0)
{
return ret;
}
/* Enable or disable the channel */
if (state == MR_ADC_STATE_ENABLE)
if (config.state == MR_ENABLE)
{
MR_BIT_SET(adc->channel, (1 << channel));
} else
@@ -38,7 +38,7 @@ static int adc_channel_set_state(struct mr_adc *adc, int channel, int state)
return MR_EOK;
}
static int adc_channel_get_state(struct mr_adc *adc, int channel)
static int adc_channel_get_configure(struct mr_adc *adc, int channel, struct mr_adc_config *config)
{
/* Check channel is valid */
if (channel < 0 || channel >= 32)
@@ -46,8 +46,9 @@ static int adc_channel_get_state(struct mr_adc *adc, int channel)
return MR_EINVAL;
}
/* Check if the channel is enabled */
return MR_BIT_IS_SET(adc->channel, (1 << channel));
/* Get configure */
config->state = MR_BIT_IS_SET(adc->channel, (1 << channel));
return MR_EOK;
}
static int mr_adc_open(struct mr_dev *dev)
@@ -102,39 +103,41 @@ static ssize_t mr_adc_read(struct mr_dev *dev, int off, void *buf, size_t size,
return rd_size;
}
static int mr_adc_ioctl(struct mr_dev *dev, int off, int cmd, void *args)
static ssize_t mr_adc_ioctl(struct mr_dev *dev, int off, int cmd, void *args)
{
struct mr_adc *adc = (struct mr_adc *)dev;
switch (cmd)
{
case MR_CTL_ADC_SET_CHANNEL_STATE:
case MR_CTL_ADC_SET_CHANNEL_CONFIG:
{
if (args != MR_NULL)
{
struct mr_adc_config config = *((struct mr_adc_config *)args);
return adc_channel_set_state(adc, off, config.state);
int ret = adc_channel_set_configure(adc, off, config);
if (ret < 0)
{
return ret;
}
return sizeof(config);
}
return MR_EINVAL;
}
case MR_CTL_ADC_GET_CHANNEL_STATE:
case MR_CTL_ADC_GET_CHANNEL_CONFIG:
{
if (args != MR_NULL)
{
struct mr_adc_config *config = (struct mr_adc_config *)args;
int ret = adc_channel_get_state(adc, off);
int ret = adc_channel_get_configure(adc, off, config);
if (ret < 0)
{
return ret;
}
config->state = ret;
return MR_EOK;
return sizeof(*config);
}
}
default:
{
return MR_ENOTSUP;

View File

@@ -74,7 +74,6 @@ static ssize_t mr_can_bus_isr(struct mr_dev *dev, int event, void *args)
}
return MR_EOK;
}
default:
{
return MR_ENOTSUP;
@@ -140,7 +139,7 @@ MR_INLINE int can_dev_take_bus(struct mr_can_dev *can_dev)
if (can_dev->config.baud_rate != can_bus->config.baud_rate)
{
int ret = ops->configure(can_bus, &can_dev->config);
if (ret != MR_EOK)
if (ret < 0)
{
return ret;
}
@@ -203,7 +202,7 @@ static int mr_can_dev_open(struct mr_dev *dev)
/* Allocate FIFO buffers */
int ret = mr_ringbuf_allocate(&can_dev->rd_fifo, can_dev->rd_bufsz);
if (ret != MR_EOK)
if (ret < 0)
{
return ret;
}
@@ -226,7 +225,7 @@ static ssize_t mr_can_dev_read(struct mr_dev *dev, int off, void *buf, size_t si
struct mr_can_dev *can_dev = (struct mr_can_dev *)dev;
ssize_t ret = can_dev_take_bus(can_dev);
if (ret != MR_EOK)
if (ret < 0)
{
return ret;
}
@@ -254,7 +253,7 @@ static ssize_t mr_can_dev_write(struct mr_dev *dev, int off, const void *buf, si
}
ssize_t ret = can_dev_take_bus(can_dev);
if (ret != MR_EOK)
if (ret < 0)
{
return ret;
}
@@ -270,7 +269,7 @@ static ssize_t mr_can_dev_write(struct mr_dev *dev, int off, const void *buf, si
return ret;
}
static int mr_can_dev_ioctl(struct mr_dev *dev, int off, int cmd, void *args)
static ssize_t mr_can_dev_ioctl(struct mr_dev *dev, int off, int cmd, void *args)
{
struct mr_can_dev *can_dev = (struct mr_can_dev *)dev;
@@ -289,9 +288,8 @@ static int mr_can_dev_ioctl(struct mr_dev *dev, int off, int cmd, void *args)
can_bus->hold = MR_FALSE;
can_bus->owner = MR_NULL;
}
can_dev->config = config;
return MR_EOK;
return sizeof(config);
}
return MR_EINVAL;
}
@@ -303,15 +301,15 @@ static int mr_can_dev_ioctl(struct mr_dev *dev, int off, int cmd, void *args)
int ret = mr_ringbuf_allocate(&can_dev->rd_fifo, bufsz);
can_dev->rd_bufsz = 0;
if (ret == MR_EOK)
if (ret < 0)
{
can_dev->rd_bufsz = bufsz;
return ret;
}
return ret;
can_dev->rd_bufsz = bufsz;
return sizeof(bufsz);
}
return MR_EINVAL;
}
case MR_CTL_GET_CONFIG:
{
if (args != MR_NULL)
@@ -319,7 +317,7 @@ static int mr_can_dev_ioctl(struct mr_dev *dev, int off, int cmd, void *args)
struct mr_can_config *config = (struct mr_can_config *)args;
*config = can_dev->config;
return MR_EOK;
return sizeof(*config);
}
return MR_EINVAL;
}
@@ -327,12 +325,13 @@ static int mr_can_dev_ioctl(struct mr_dev *dev, int off, int cmd, void *args)
{
if (args != MR_NULL)
{
*(size_t *)args = can_dev->rd_bufsz;
return MR_EOK;
size_t *bufsz = (size_t *)args;
*bufsz = can_dev->rd_bufsz;
return sizeof(*bufsz);
}
return MR_EINVAL;
}
default:
{
return MR_ENOTSUP;

View File

@@ -10,7 +10,7 @@
#ifdef MR_USING_DAC
static int dac_channel_set_state(struct mr_dac *dac, int channel, int state)
static int dac_channel_set_configure(struct mr_dac *dac, int channel, struct mr_dac_config config)
{
struct mr_dac_ops *ops = (struct mr_dac_ops *)dac->dev.drv->ops;
@@ -21,14 +21,14 @@ static int dac_channel_set_state(struct mr_dac *dac, int channel, int state)
}
/* Configure the channel */
int ret = ops->channel_configure(dac, channel, state);
if (ret != MR_EOK)
int ret = ops->channel_configure(dac, channel, config.state);
if (ret < 0)
{
return ret;
}
/* Enable or disable the channel */
if (state == MR_DAC_STATE_ENABLE)
if (config.state == MR_ENABLE)
{
MR_BIT_SET(dac->channel, (1 << channel));
} else
@@ -38,7 +38,7 @@ static int dac_channel_set_state(struct mr_dac *dac, int channel, int state)
return MR_EOK;
}
static int dac_channel_get_state(struct mr_dac *dac, int channel)
static int dac_channel_get_configure(struct mr_dac *dac, int channel, struct mr_dac_config *config)
{
/* Check channel is valid */
if (channel < 0 || channel >= 32)
@@ -46,8 +46,9 @@ static int dac_channel_get_state(struct mr_dac *dac, int channel)
return MR_EINVAL;
}
/* Check if the channel is enabled */
return MR_BIT_IS_SET(dac->channel, (1 << channel));
/* Get configure */
config->state = MR_BIT_IS_SET(dac->channel, (1 << channel));
return MR_EOK;
}
static int mr_dac_open(struct mr_dev *dev)
@@ -102,40 +103,42 @@ static ssize_t mr_dac_write(struct mr_dev *dev, int off, const void *buf, size_t
return wr_size;
}
static int mr_dac_ioctl(struct mr_dev *dev, int off, int cmd, void *args)
static ssize_t mr_dac_ioctl(struct mr_dev *dev, int off, int cmd, void *args)
{
struct mr_dac *dac = (struct mr_dac *)dev;
switch (cmd)
{
case MR_CTL_DAC_SET_CHANNEL_STATE:
case MR_CTL_DAC_SET_CHANNEL_CONFIG:
{
if (args != MR_NULL)
{
struct mr_dac_config config = *((struct mr_dac_config *)args);
return dac_channel_set_state(dac, off, config.state);
}
return MR_EINVAL;
}
case MR_CTL_DAC_GET_CHANNEL_STATE:
{
if (args != MR_NULL)
{
int *state = (int *)args;
int ret = dac_channel_get_state(dac, off);
int ret = dac_channel_set_configure(dac, off, config);
if (ret < 0)
{
return ret;
}
*state = ret;
return MR_EOK;
return sizeof(config);
}
return MR_EINVAL;
}
case MR_CTL_DAC_GET_CHANNEL_CONFIG:
{
if (args != MR_NULL)
{
struct mr_dac_config *config = (struct mr_dac_config *)args;
int ret = dac_channel_get_configure(dac, off, config);
if (ret < 0)
{
return ret;
}
return sizeof(*config);
}
return MR_EINVAL;
}
default:
{
return MR_ENOTSUP;

View File

@@ -62,7 +62,6 @@ static ssize_t mr_i2c_bus_isr(struct mr_dev *dev, int event, void *args)
}
return MR_EOK;
}
default:
{
return MR_ENOTSUP;
@@ -126,7 +125,7 @@ MR_INLINE int i2c_dev_take_bus(struct mr_i2c_dev *i2c_dev)
{
int addr = (i2c_dev->config.host_slave == MR_I2C_SLAVE) ? i2c_dev->addr : 0x00;
int ret = ops->configure(i2c_bus, &i2c_dev->config, addr, i2c_dev->addr_bits);
if (ret != MR_EOK)
if (ret < 0)
{
return ret;
}
@@ -165,11 +164,8 @@ MR_INLINE void i2c_dev_send_addr(struct mr_i2c_dev *i2c_dev, int rdwr)
int addr = 0, addr_bits = MR_I2C_ADDR_BITS_7;
/* Get the address, otherwise use the 0x00 */
if (i2c_dev != MR_NULL)
{
addr = i2c_dev->addr;
addr_bits = i2c_dev->addr_bits;
}
addr = i2c_dev->addr;
addr_bits = i2c_dev->addr_bits;
/* Set the read command */
if (rdwr == MR_I2C_RD)
@@ -239,7 +235,7 @@ static ssize_t mr_i2c_dev_read(struct mr_dev *dev, int off, void *buf, size_t si
struct mr_i2c_dev *i2c_dev = (struct mr_i2c_dev *)dev;
ssize_t ret = i2c_dev_take_bus(i2c_dev);
if (ret != MR_EOK)
if (ret < 0)
{
return ret;
}
@@ -276,7 +272,7 @@ static ssize_t mr_i2c_dev_write(struct mr_dev *dev, int off, const void *buf, si
struct mr_i2c_dev *i2c_dev = (struct mr_i2c_dev *)dev;
ssize_t ret = i2c_dev_take_bus(i2c_dev);
if (ret != MR_EOK)
if (ret < 0)
{
return ret;
}
@@ -302,7 +298,7 @@ static ssize_t mr_i2c_dev_write(struct mr_dev *dev, int off, const void *buf, si
return ret;
}
static int mr_i2c_dev_ioctl(struct mr_dev *dev, int off, int cmd, void *args)
static ssize_t mr_i2c_dev_ioctl(struct mr_dev *dev, int off, int cmd, void *args)
{
struct mr_i2c_dev *i2c_dev = (struct mr_i2c_dev *)dev;
@@ -327,12 +323,12 @@ static int mr_i2c_dev_ioctl(struct mr_dev *dev, int off, int cmd, void *args)
if (config.host_slave == MR_I2C_SLAVE)
{
int ret = i2c_dev_take_bus(i2c_dev);
if (ret != MR_EOK)
if (ret < 0)
{
return ret;
}
}
return MR_EOK;
return sizeof(config);
}
return MR_EINVAL;
}
@@ -344,11 +340,12 @@ static int mr_i2c_dev_ioctl(struct mr_dev *dev, int off, int cmd, void *args)
int ret = mr_ringbuf_allocate(&i2c_dev->rd_fifo, bufsz);
i2c_dev->rd_bufsz = 0;
if (ret == MR_EOK)
if (ret < 0)
{
i2c_dev->rd_bufsz = bufsz;
return ret;
}
return ret;
i2c_dev->rd_bufsz = bufsz;
return sizeof(bufsz);
}
return MR_EINVAL;
}
@@ -357,7 +354,6 @@ static int mr_i2c_dev_ioctl(struct mr_dev *dev, int off, int cmd, void *args)
mr_ringbuf_reset(&i2c_dev->rd_fifo);
return MR_EOK;
}
case MR_CTL_I2C_GET_CONFIG:
{
if (args != MR_NULL)
@@ -365,7 +361,7 @@ static int mr_i2c_dev_ioctl(struct mr_dev *dev, int off, int cmd, void *args)
struct mr_i2c_config *config = (struct mr_i2c_config *)args;
*config = i2c_dev->config;
return MR_EOK;
return sizeof(*config);
}
return MR_EINVAL;
}
@@ -373,8 +369,10 @@ static int mr_i2c_dev_ioctl(struct mr_dev *dev, int off, int cmd, void *args)
{
if (args != MR_NULL)
{
*(size_t *)args = i2c_dev->rd_bufsz;
return MR_EOK;
size_t *bufsz = (size_t *)args;
*bufsz = i2c_dev->rd_bufsz;
return sizeof(*bufsz);
}
return MR_EINVAL;
}
@@ -382,13 +380,12 @@ static int mr_i2c_dev_ioctl(struct mr_dev *dev, int off, int cmd, void *args)
{
if (args != MR_NULL)
{
size_t *size = (size_t *)args;
size_t *datasz = (size_t *)args;
*size = mr_ringbuf_get_bufsz(&i2c_dev->rd_fifo);
return MR_EOK;
*datasz = mr_ringbuf_get_bufsz(&i2c_dev->rd_fifo);
return sizeof(*datasz);
}
}
default:
{
return MR_ENOTSUP;

View File

@@ -33,7 +33,7 @@ static int pin_set_mode(struct mr_pin *pin, int number, int mode)
/* Configure pin mode */
int ret = ops->configure(pin, number, mode);
if (ret != MR_EOK)
if (ret < 0)
{
return ret;
}
@@ -137,7 +137,7 @@ static ssize_t mr_pin_write(struct mr_dev *dev, int off, const void *buf, size_t
return wr_size;
}
static int mr_pin_ioctl(struct mr_dev *dev, int off, int cmd, void *args)
static ssize_t mr_pin_ioctl(struct mr_dev *dev, int off, int cmd, void *args)
{
struct mr_pin *pin = (struct mr_pin *)dev;
@@ -149,11 +149,15 @@ static int mr_pin_ioctl(struct mr_dev *dev, int off, int cmd, void *args)
{
struct mr_pin_config config = *((struct mr_pin_config *)args);
return pin_set_mode(pin, off, config.mode);
int ret = pin_set_mode(pin, off, config.mode);
if (ret < 0)
{
return ret;
}
return sizeof(config);
}
return MR_EINVAL;
}
default:
{
return MR_ENOTSUP;
@@ -183,7 +187,6 @@ static ssize_t mr_pin_isr(struct mr_dev *dev, int event, void *args)
}
return MR_ENOTFOUND;
}
default:
{
return MR_ENOTSUP;
@@ -224,134 +227,4 @@ int mr_pin_register(struct mr_pin *pin, const char *name, struct mr_drv *drv)
return mr_dev_register(&pin->dev, name, Mr_Dev_Type_Pin, MR_SFLAG_RDWR, &ops, drv);
}
#ifdef MR_USING_MSH
#include "include/components/mr_msh.h"
static int desc = -1;
static int msh_pin_mode(const char *arg)
{
/* Parse mode */
int mode;
if (strncmp(arg, "in", 2) == 0)
{
mode = MR_PIN_MODE_INPUT;
} else if (strncmp(arg, "out", 3) == 0)
{
mode = MR_PIN_MODE_OUTPUT;
} else
{
return MR_EINVAL;
}
/* Set mode */
int ret = mr_dev_ioctl(desc, MR_CTL_PIN_SET_MODE, &mode);
mr_msh_printf("%s\r\n", mr_strerror((ret < 0 ? ret : MR_EOK)));
return ret;
}
static int msh_pin_write(const char *arg)
{
/* Parse level */
uint8_t level;
if ((strncmp(arg, "high", 4) == 0) || (strncmp(arg, "1", 1) == 0))
{
level = MR_PIN_HIGH_LEVEL;
} else if ((strncmp(arg, "low", 3) == 0) || (strncmp(arg, "0", 1) == 0))
{
level = MR_PIN_LOW_LEVEL;
} else
{
return MR_EINVAL;
}
/* Write level */
int ret = (int)mr_dev_write(desc, &level, sizeof(level));
mr_msh_printf("%s\r\n", mr_strerror((ret < 0 ? ret : MR_EOK)));
return ret;
}
static int msh_pin_read(void)
{
/* Read level */
uint8_t level;
int ret = (int)mr_dev_read(desc, &level, sizeof(level));
if (ret < 0)
{
mr_msh_printf("%s\r\n", mr_strerror(ret));
} else
{
mr_msh_printf("%s\r\n", (level ? "high" : "low"));
}
return ret;
}
static int msh_pin_toggle(void)
{
/* Read level */
uint8_t level;
int ret = (int)mr_dev_read(desc, &level, sizeof(level));
if (ret < 0)
{
mr_msh_printf("%s\r\n", mr_strerror(ret));
return ret;
}
/* Toggle level */
level = !level;
ret = (int)mr_dev_write(desc, &level, sizeof(level));
mr_msh_printf("%s\r\n", mr_strerror((ret < 0 ? ret : MR_EOK)));
return ret;
}
static void msh_pin_printf_usage(void)
{
mr_msh_printf("Usage: pin mode <number> <mode>\r\n");
mr_msh_printf(" pin write <number> <level>\r\n");
mr_msh_printf(" pin read <number>\r\n");
mr_msh_printf(" pin toggle <number>\r\n");
}
static int msh_pin(int argc, void *argv)
{
/* Open the pin */
if (desc < 0)
{
desc = mr_dev_open("pin", MR_OFLAG_RDWR);
if (desc < 0)
{
return desc;
}
}
/* Check the arguments */
if (argc < 2)
{
msh_pin_printf_usage();
return MR_EINVAL;
}
/* Parse number */
int number = -1;
sscanf(MR_MSH_GET_ARG(1), "%d", &number);
int ret = mr_dev_ioctl(desc, MR_CTL_PIN_SET_NUMBER, &number);
if (ret < 0)
{
mr_msh_printf("%s\r\n", mr_strerror(ret));
return ret;
}
/* Parse command */
if (strncmp(MR_MSH_GET_ARG(0), "read", 4) == 0)
{
return msh_pin_read();
} else if (strncmp(MR_MSH_GET_ARG(0), "toggle", 6) == 0)
{
return msh_pin_toggle();
}
if (argc >= 3)
{
if (strncmp(MR_MSH_GET_ARG(0), "mode", 4) == 0)
{
return msh_pin_mode(MR_MSH_GET_ARG(2));
} else if (strncmp(MR_MSH_GET_ARG(0), "write", 5) == 0)
{
return msh_pin_write(MR_MSH_GET_ARG(2));
}
}
msh_pin_printf_usage();
return MR_EINVAL;
}
MR_MSH_CMD_EXPORT(pin, msh_pin, "Pin control.");
#endif /* MR_USING_MSH */
#endif /* MR_USING_PIN */

View File

@@ -16,12 +16,12 @@ static int mr_serial_open(struct mr_dev *dev)
struct mr_serial_ops *ops = (struct mr_serial_ops *)dev->drv->ops;
int ret = mr_ringbuf_allocate(&serial->rd_fifo, serial->rd_bufsz);
if (ret != MR_EOK)
if (ret < 0)
{
return ret;
}
ret = mr_ringbuf_allocate(&serial->wr_fifo, serial->wr_bufsz);
if (ret != MR_EOK)
if (ret < 0)
{
return ret;
}
@@ -88,7 +88,7 @@ static ssize_t mr_serial_write(struct mr_dev *dev, int off, const void *buf, siz
return wr_size;
}
static int mr_serial_ioctl(struct mr_dev *dev, int off, int cmd, void *args)
static ssize_t mr_serial_ioctl(struct mr_dev *dev, int off, int cmd, void *args)
{
struct mr_serial *serial = (struct mr_serial *)dev;
struct mr_serial_ops *ops = (struct mr_serial_ops *)dev->drv->ops;
@@ -102,11 +102,12 @@ static int mr_serial_ioctl(struct mr_dev *dev, int off, int cmd, void *args)
struct mr_serial_config config = *(struct mr_serial_config *)args;
int ret = ops->configure(serial, &config);
if (ret == MR_EOK)
if (ret < 0)
{
serial->config = config;
return ret;
}
return ret;
serial->config = config;
return sizeof(config);
}
return MR_EINVAL;
}
@@ -118,11 +119,12 @@ static int mr_serial_ioctl(struct mr_dev *dev, int off, int cmd, void *args)
int ret = mr_ringbuf_allocate(&serial->rd_fifo, bufsz);
serial->rd_bufsz = 0;
if (ret == MR_EOK)
if (ret < 0)
{
serial->rd_bufsz = bufsz;
return ret;
}
return ret;
serial->rd_bufsz = bufsz;
return sizeof(bufsz);
}
return MR_EINVAL;
}
@@ -134,11 +136,12 @@ static int mr_serial_ioctl(struct mr_dev *dev, int off, int cmd, void *args)
int ret = mr_ringbuf_allocate(&serial->wr_fifo, bufsz);
serial->wr_bufsz = 0;
if (ret == MR_EOK)
if (ret < 0)
{
serial->wr_bufsz = bufsz;
return ret;
}
return ret;
serial->wr_bufsz = bufsz;
return sizeof(bufsz);
}
return MR_EINVAL;
}
@@ -152,7 +155,6 @@ static int mr_serial_ioctl(struct mr_dev *dev, int off, int cmd, void *args)
mr_ringbuf_reset(&serial->wr_fifo);
return MR_EOK;
}
case MR_CTL_SERIAL_GET_CONFIG:
{
if (args != MR_NULL)
@@ -160,7 +162,7 @@ static int mr_serial_ioctl(struct mr_dev *dev, int off, int cmd, void *args)
struct mr_serial_config *config = (struct mr_serial_config *)args;
*config = serial->config;
return MR_EOK;
return sizeof(*config);
}
return MR_EINVAL;
}
@@ -168,8 +170,10 @@ static int mr_serial_ioctl(struct mr_dev *dev, int off, int cmd, void *args)
{
if (args != MR_NULL)
{
*(size_t *)args = serial->rd_bufsz;
return MR_EOK;
size_t *bufsz = (size_t *)args;
*bufsz = serial->rd_bufsz;
return sizeof(*bufsz);
}
return MR_EINVAL;
}
@@ -177,8 +181,10 @@ static int mr_serial_ioctl(struct mr_dev *dev, int off, int cmd, void *args)
{
if (args != MR_NULL)
{
*(size_t *)args = serial->wr_bufsz;
return MR_EOK;
size_t *bufsz = (size_t *)args;
*bufsz = serial->wr_bufsz;
return sizeof(*bufsz);
}
return MR_EINVAL;
}
@@ -189,7 +195,7 @@ static int mr_serial_ioctl(struct mr_dev *dev, int off, int cmd, void *args)
size_t *datasz = (size_t *)args;
*datasz = mr_ringbuf_get_data_size(&serial->rd_fifo);
return MR_EOK;
return sizeof(*datasz);
}
return MR_EINVAL;
}
@@ -200,11 +206,10 @@ static int mr_serial_ioctl(struct mr_dev *dev, int off, int cmd, void *args)
size_t *datasz = (size_t *)args;
*datasz = mr_ringbuf_get_data_size(&serial->wr_fifo);
return MR_EOK;
return sizeof(*datasz);
}
return MR_EINVAL;
}
default:
{
return MR_ENOTSUP;
@@ -227,7 +232,6 @@ static ssize_t mr_serial_isr(struct mr_dev *dev, int event, void *args)
return (ssize_t)mr_ringbuf_get_data_size(&serial->rd_fifo);
}
case MR_ISR_SERIAL_WR_INT:
{
/* Write data from FIFO, if FIFO is empty, stop transmit */
@@ -242,7 +246,6 @@ static ssize_t mr_serial_isr(struct mr_dev *dev, int event, void *args)
return (ssize_t)mr_ringbuf_get_data_size(&serial->wr_fifo);
}
default:
{
return MR_ENOTSUP;

View File

@@ -103,7 +103,9 @@ static int mr_soft_i2c_bus_configure(struct mr_i2c_bus *i2c_bus, struct mr_i2c_c
/* Configure SCL pin */
mr_dev_ioctl(soft_i2c_bus->desc, MR_CTL_PIN_SET_NUMBER, &soft_i2c_bus->scl_pin);
int ret = mr_dev_ioctl(soft_i2c_bus->desc, MR_CTL_PIN_SET_MODE, MR_MAKE_LOCAL(int, MR_PIN_MODE_OUTPUT_OD));
int ret = (int)mr_dev_ioctl(soft_i2c_bus->desc,
MR_CTL_PIN_SET_MODE,
MR_MAKE_LOCAL(int, MR_PIN_MODE_OUTPUT_OD));
if (ret < 0)
{
return ret;
@@ -111,7 +113,7 @@ static int mr_soft_i2c_bus_configure(struct mr_i2c_bus *i2c_bus, struct mr_i2c_c
/* Configure SDA pin */
mr_dev_ioctl(soft_i2c_bus->desc, MR_CTL_PIN_SET_NUMBER, &soft_i2c_bus->sda_pin);
ret = mr_dev_ioctl(soft_i2c_bus->desc, MR_CTL_PIN_SET_MODE, MR_MAKE_LOCAL(int, MR_PIN_MODE_OUTPUT_OD));
ret = (int)mr_dev_ioctl(soft_i2c_bus->desc, MR_CTL_PIN_SET_MODE, MR_MAKE_LOCAL(int, MR_PIN_MODE_OUTPUT_OD));
if (ret < 0)
{
return ret;
@@ -124,7 +126,7 @@ static int mr_soft_i2c_bus_configure(struct mr_i2c_bus *i2c_bus, struct mr_i2c_c
{
/* Reconfigure SCL pin */
mr_dev_ioctl(soft_i2c_bus->desc, MR_CTL_PIN_SET_NUMBER, &soft_i2c_bus->scl_pin);
int ret = mr_dev_ioctl(soft_i2c_bus->desc, MR_CTL_PIN_SET_MODE, MR_MAKE_LOCAL(int, MR_PIN_MODE_NONE));
int ret = (int)mr_dev_ioctl(soft_i2c_bus->desc, MR_CTL_PIN_SET_MODE, MR_MAKE_LOCAL(int, MR_PIN_MODE_NONE));
if (ret < 0)
{
return ret;
@@ -132,7 +134,7 @@ static int mr_soft_i2c_bus_configure(struct mr_i2c_bus *i2c_bus, struct mr_i2c_c
/* Reconfigure SDA pin */
mr_dev_ioctl(soft_i2c_bus->desc, MR_CTL_PIN_SET_NUMBER, &soft_i2c_bus->sda_pin);
ret = mr_dev_ioctl(soft_i2c_bus->desc, MR_CTL_PIN_SET_MODE, MR_MAKE_LOCAL(int, MR_PIN_MODE_NONE));
ret = (int)mr_dev_ioctl(soft_i2c_bus->desc, MR_CTL_PIN_SET_MODE, MR_MAKE_LOCAL(int, MR_PIN_MODE_NONE));
if (ret < 0)
{
return ret;

View File

@@ -26,7 +26,6 @@ static int mr_spi_bus_open(struct mr_dev *dev)
#ifdef MR_USING_PIN
spi_bus->cs_desc = mr_dev_open("pin", MR_OFLAG_RDWR);
#endif /* MR_USING_PIN */
return ops->configure(spi_bus, &spi_bus->config);
}
@@ -43,7 +42,6 @@ static int mr_spi_bus_close(struct mr_dev *dev)
spi_bus->cs_desc = -1;
}
#endif /* MR_USING_PIN */
return ops->configure(spi_bus, &close_config);
}
@@ -91,7 +89,6 @@ static ssize_t mr_spi_bus_isr(struct mr_dev *dev, int event, void *args)
}
return MR_EOK;
}
default:
{
return MR_ENOTSUP;
@@ -220,7 +217,7 @@ MR_INLINE int spi_dev_take_bus(struct mr_spi_dev *spi_dev)
|| spi_dev->config.bit_order != spi_bus->config.bit_order)
{
int ret = ops->configure(spi_bus, &spi_dev->config);
if (ret != MR_EOK)
if (ret < 0)
{
return ret;
}
@@ -305,7 +302,6 @@ static ssize_t spi_dev_transfer(struct mr_spi_dev *spi_dev, void *rd_buf, const
}
break;
}
default:
{
return MR_EINVAL;
@@ -351,7 +347,6 @@ static ssize_t spi_dev_transfer(struct mr_spi_dev *spi_dev, void *rd_buf, const
}
break;
}
default:
{
return MR_EINVAL;
@@ -403,7 +398,6 @@ static ssize_t spi_dev_transfer(struct mr_spi_dev *spi_dev, void *rd_buf, const
}
break;
}
default:
{
return MR_EINVAL;
@@ -443,7 +437,7 @@ static ssize_t mr_spi_dev_read(struct mr_dev *dev, int off, void *buf, size_t si
struct mr_spi_dev *spi_dev = (struct mr_spi_dev *)dev;
ssize_t ret = spi_dev_take_bus(spi_dev);
if (ret != MR_EOK)
if (ret < 0)
{
return ret;
}
@@ -479,7 +473,7 @@ static ssize_t mr_spi_dev_write(struct mr_dev *dev, int off, const void *buf, si
struct mr_spi_dev *spi_dev = (struct mr_spi_dev *)dev;
ssize_t ret = spi_dev_take_bus(spi_dev);
if (ret != MR_EOK)
if (ret < 0)
{
return ret;
}
@@ -504,7 +498,7 @@ static ssize_t mr_spi_dev_write(struct mr_dev *dev, int off, const void *buf, si
return ret;
}
static int mr_spi_dev_ioctl(struct mr_dev *dev, int off, int cmd, void *args)
static ssize_t mr_spi_dev_ioctl(struct mr_dev *dev, int off, int cmd, void *args)
{
struct mr_spi_dev *spi_dev = (struct mr_spi_dev *)dev;
@@ -538,12 +532,12 @@ static int mr_spi_dev_ioctl(struct mr_dev *dev, int off, int cmd, void *args)
if (config.host_slave == MR_SPI_SLAVE)
{
int ret = spi_dev_take_bus(spi_dev);
if (ret != MR_EOK)
if (ret < 0)
{
return ret;
}
}
return MR_EOK;
return sizeof(config);
}
return MR_EINVAL;
}
@@ -555,11 +549,12 @@ static int mr_spi_dev_ioctl(struct mr_dev *dev, int off, int cmd, void *args)
int ret = mr_ringbuf_allocate(&spi_dev->rd_fifo, bufsz);
spi_dev->rd_bufsz = 0;
if (ret == MR_EOK)
if (ret < 0)
{
spi_dev->rd_bufsz = bufsz;
return ret;
}
return ret;
spi_dev->rd_bufsz = bufsz;
return sizeof(bufsz);
}
return MR_EINVAL;
}
@@ -575,7 +570,7 @@ static int mr_spi_dev_ioctl(struct mr_dev *dev, int off, int cmd, void *args)
struct mr_spi_transfer transfer = *(struct mr_spi_transfer *)args;
int ret = spi_dev_take_bus(spi_dev);
if (ret != MR_EOK)
if (ret < 0)
{
return ret;
}
@@ -603,7 +598,6 @@ static int mr_spi_dev_ioctl(struct mr_dev *dev, int off, int cmd, void *args)
}
return MR_EINVAL;
}
case MR_CTL_SPI_GET_CONFIG:
{
if (args != MR_NULL)
@@ -611,7 +605,7 @@ static int mr_spi_dev_ioctl(struct mr_dev *dev, int off, int cmd, void *args)
struct mr_spi_config *config = (struct mr_spi_config *)args;
*config = spi_dev->config;
return MR_EOK;
return sizeof(*config);
}
return MR_EINVAL;
}
@@ -619,8 +613,10 @@ static int mr_spi_dev_ioctl(struct mr_dev *dev, int off, int cmd, void *args)
{
if (args != MR_NULL)
{
*(size_t *)args = spi_dev->rd_bufsz;
return MR_EOK;
size_t *bufsz = (size_t *)args;
*bufsz = spi_dev->rd_bufsz;
return sizeof(*bufsz);
}
return MR_EINVAL;
}
@@ -631,11 +627,10 @@ static int mr_spi_dev_ioctl(struct mr_dev *dev, int off, int cmd, void *args)
size_t *datasz = (size_t *)args;
*datasz = mr_ringbuf_get_data_size(&spi_dev->rd_fifo);
return MR_EOK;
return sizeof(*datasz);
}
return MR_EINVAL;
}
default:
{
return MR_ENOTSUP;

View File

@@ -166,7 +166,7 @@ static ssize_t mr_timer_write(struct mr_dev *dev, int off, const void *buf, size
{
/* Calculate prescaler and period */
int ret = timer_calculate(timer, timeout);
if (ret != MR_EOK)
if (ret < 0)
{
return ret;
}
@@ -177,7 +177,7 @@ static ssize_t mr_timer_write(struct mr_dev *dev, int off, const void *buf, size
return wr_size;
}
static int mr_timer_ioctl(struct mr_dev *dev, int off, int cmd, void *args)
static ssize_t mr_timer_ioctl(struct mr_dev *dev, int off, int cmd, void *args)
{
struct mr_timer *timer = (struct mr_timer *)dev;
@@ -190,11 +190,10 @@ static int mr_timer_ioctl(struct mr_dev *dev, int off, int cmd, void *args)
struct mr_timer_config config = *(struct mr_timer_config *)args;
timer->config = config;
return MR_EOK;
return sizeof(config);
}
return MR_EINVAL;
}
case MR_CTL_TIMER_GET_MODE:
{
if (args != MR_NULL)
@@ -202,11 +201,10 @@ static int mr_timer_ioctl(struct mr_dev *dev, int off, int cmd, void *args)
struct mr_timer_config *config = (struct mr_timer_config *)args;
*config = timer->config;
return MR_EOK;
return sizeof(*config);
}
return MR_EINVAL;
}
default:
{
return MR_ENOTSUP;
@@ -237,7 +235,6 @@ static ssize_t mr_timer_isr(struct mr_dev *dev, int event, void *args)
}
return MR_EBUSY;
}
default:
{
return MR_ENOTSUP;

View File

@@ -8,7 +8,7 @@
* [关闭ADC设备](#关闭adc设备)
* [控制ADC设备](#控制adc设备)
* [设置/获取通道编号](#设置获取通道编号)
* [设置/获取通道状态](#设置获取通道状态)
* [设置/获取通道配置](#设置获取通道配置)
* [读取ADC设备通道值](#读取adc设备通道值)
* [使用示例:](#使用示例)
<!-- TOC -->
@@ -45,7 +45,7 @@ int mr_dev_close(int desc);
| `=0` | 关闭成功 |
| `<0` | 错误码 |
注:关闭设备时所有的通道都将被自动恢复到默认状态,重新打开后需要重新配置通道。
注:关闭设备时所有的通道都将被自动恢复到默认配置,重新打开后需要重新配置通道(可关闭此功能)
## 控制ADC设备
@@ -64,9 +64,9 @@ int mr_dev_ioctl(int desc, int cmd, void *args);
- `cmd`:命令码,支持以下命令:
- `MR_CTL_ADC_SET_CHANNEL`:设置通道编号。
- `MR_CTL_ADC_SET_CHANNEL_STATE`:设置通道状态
- `MR_CTL_ADC_SET_CHANNEL_CONFIG`:设置通道配置
- `MR_CTL_ADC_GET_CHANNEL`:获取通道编号。
- `MR_CTL_ADC_GET_CHANNEL_STATE`:获取通道状态
- `MR_CTL_ADC_GET_CHANNEL_CONFIG`:获取通道配置
### 设置/获取通道编号
@@ -84,20 +84,20 @@ int number;
mr_dev_ioctl(ds, MR_CTL_ADC_GET_CHANNEL, &number);
```
### 设置/获取通道状态
### 设置/获取通道配置
通道状态
通道配置
- `MR_ADC_STATE_DISABLE`:禁用通道。
- `MR_ADC_STATE_ENABLE`:启用通道。
- `MR_DISABLE`:禁用通道。
- `MR_ENABLE`:启用通道。
```c
/* 设置通道状态 */
mr_dev_ioctl(ds, MR_CTL_ADC_SET_CHANNEL_STATE, MR_MAKE_LOCAL(int, MR_ADC_STATE_ENABLE));
/* 设置通道配置 */
mr_dev_ioctl(ds, MR_CTL_ADC_SET_CHANNEL_CONFIG, MR_MAKE_LOCAL(int, MR_ENABLE));
/* 获取通道状态 */
/* 获取通道配置 */
int state;
mr_dev_ioctl(ds, MR_CTL_ADC_GET_CHANNEL_STATE, &state);
mr_dev_ioctl(ds, MR_CTL_ADC_GET_CHANNEL_CONFIG, &state);
```
## 读取ADC设备通道值
@@ -155,7 +155,7 @@ int adc_init(void)
/* 设置到通道5 */
mr_dev_ioctl(adc_ds, MR_CTL_ADC_SET_CHANNEL, MR_MAKE_LOCAL(int, CHANNEL_NUMBER));
/* 设置通道使能 */
ret = mr_dev_ioctl(adc_ds, MR_CTL_ADC_SET_CHANNEL_STATE, MR_MAKE_LOCAL(int, MR_ADC_STATE_ENABLE));
ret = mr_dev_ioctl(adc_ds, MR_CTL_ADC_SET_CHANNEL_CONFIG, MR_MAKE_LOCAL(int, MR_ENABLE));
if (ret < 0)
{
mr_printf("Channel5 enable failed: %s\r\n", mr_strerror(ret));

View File

@@ -8,7 +8,7 @@
* [Close ADC Device](#close-adc-device)
* [Control ADC Device](#control-adc-device)
* [Set/Get Channel Number](#setget-channel-number)
* [Set/Get Channel Status](#setget-channel-status)
* [Set/Get Channel configure](#setget-channel-configure)
* [Read ADC Device Channel Value](#read-adc-device-channel-value)
* [Usage Example:](#usage-example)
<!-- TOC -->
@@ -30,7 +30,9 @@ int mr_dev_open(const char *name, int oflags);
- `name`: ADC device name usually is: `adcx`, `adc1`, `adc2`.
- `oflags`: Open device flags, support `MR_OFLAG_RDONLY`.
Note: When using, the ADC device should be opened separately for different tasks according to actual situations, and the appropriate `oflags` should be used for management and permission control to ensure they will not interfere with each other.
Note: When using, the ADC device should be opened separately for different tasks according to actual situations, and the
appropriate `oflags` should be used for management and permission control to ensure they will not interfere with each
other.
## Close ADC Device
@@ -45,7 +47,8 @@ int mr_dev_close(int desc);
| `=0` | Close successfully |
| `<0` | Error code |
Note: When closing the device, all channels will be automatically restored to the default state. The channel needs to be reconfigured after reopening.
Note: When closing the device, all channels will be automatically restored to the default state. The channel needs to be
reconfigured after reopening(This feature can be turned off).
## Control ADC Device
@@ -64,9 +67,9 @@ int mr_dev_ioctl(int desc, int cmd, void *args);
- `cmd`: Command code, supports the following commands:
- `MR_CTL_ADC_SET_CHANNEL`: Set channel number.
- `MR_CTL_ADC_SET_CHANNEL_STATE`: Set channel state.
- `MR_CTL_ADC_SET_CHANNEL_CONFIG`: Set channel configure.
- `MR_CTL_ADC_GET_CHANNEL`: Get channel number.
- `MR_CTL_ADC_GET_CHANNEL_STATE`: Get channel state.
- `MR_CTL_ADC_GET_CHANNEL_CONFIG`: Get channel configure.
### Set/Get Channel Number
@@ -84,20 +87,20 @@ int number;
mr_dev_ioctl(ds, MR_CTL_ADC_GET_CHANNEL, &number);
```
### Set/Get Channel Status
### Set/Get Channel Configure
Channel status:
Channel configure:
- `MR_ADC_STATE_DISABLE`: Disable channel.
- `MR_ADC_STATE_ENABLE`: Enable channel.
- `MR_DISABLE`: Disable channel.
- `MR_ENABLE`: Enable channel.
```c
/* Set channel status */
mr_dev_ioctl(ds, MR_CTL_ADC_SET_CHANNEL_STATE, MR_MAKE_LOCAL(int, MR_ADC_STATE_ENABLE));
/* Set channel configure */
mr_dev_ioctl(ds, MR_CTL_ADC_SET_CHANNEL_CONFIG, MR_MAKE_LOCAL(int, MR_ENABLE));
/* Get channel status */
/* Get channel configure */
int state;
mr_dev_ioctl(ds, MR_CTL_ADC_GET_CHANNEL_STATE, &state);
mr_dev_ioctl(ds, MR_CTL_ADC_GET_CHANNEL_CONFIG, &state);
```
## Read ADC Device Channel Value
@@ -156,7 +159,7 @@ int adc_init(void)
/* Set to channel 5 */
mr_dev_ioctl(adc_ds, MR_CTL_ADC_SET_CHANNEL, MR_MAKE_LOCAL(int, CHANNEL_NUMBER));
/* Set channel enable */
ret = mr_dev_ioctl(adc_ds, MR_CTL_ADC_SET_CHANNEL_STATE, MR_MAKE_LOCAL(int, MR_ADC_STATE_ENABLE));
ret = mr_dev_ioctl(adc_ds, MR_CTL_ADC_SET_CHANNEL_CONFIG, MR_MAKE_LOCAL(int, MR_ENABLE));
if (ret < 0)
{
mr_printf("Channel5 enable failed: %s\r\n", mr_strerror(ret));

View File

@@ -8,7 +8,7 @@
* [关闭DAC设备](#关闭dac设备)
* [控制DAC设备](#控制dac设备)
* [设置/获取通道编号](#设置获取通道编号)
* [设置/获取通道状态](#设置获取通道状态)
* [设置/获取通道配置](#设置获取通道配置)
* [写入DAC设备通道值](#写入dac设备通道值)
* [使用示例:](#使用示例)
<!-- TOC -->
@@ -45,7 +45,7 @@ int mr_dev_close(int desc);
| `=0` | 关闭成功 |
| `<0` | 错误码 |
注:关闭设备时所有的通道都将被自动恢复到默认状态,重新打开后需要重新配置通道。
注:关闭设备时所有的通道都将被自动恢复到默认状态,重新打开后需要重新配置通道(可关闭此功能)
## 控制DAC设备
@@ -64,9 +64,9 @@ int mr_dev_ioctl(int desc, int cmd, void *args);
- `cmd`:命令码,支持以下命令:
- `MR_CTL_DAC_SET_CHANNEL`:设置通道编号。
- `MR_CTL_DAC_SET_CHANNEL_STATE`:设置通道状态
- `MR_CTL_DAC_SET_CHANNEL_CONFIG`:设置通道配置
- `MR_CTL_DAC_GET_CHANNEL`:获取通道编号。
- `MR_CTL_DAC_GET_CHANNEL_STATE`:获取通道状态
- `MR_CTL_DAC_GET_CHANNEL_CONFIG`:获取通道配置
### 设置/获取通道编号
@@ -84,20 +84,20 @@ int number;
mr_dev_ioctl(ds, MR_CTL_DAC_GET_CHANNEL, &number);
```
### 设置/获取通道状态
### 设置/获取通道配置
通道状态
通道配置
- `MR_DAC_STATE_DISABLE`:禁用通道。
- `MR_DAC_STATE_ENABLE`:启用通道。
- `MR_DISABLE`:禁用通道。
- `MR_ENABLE`:启用通道。
```c
/* 设置通道状态 */
mr_dev_ioctl(ds, MR_CTL_DAC_SET_CHANNEL_STATE, MR_MAKE_LOCAL(int, MR_DAC_STATE_ENABLE));
/* 设置通道配置 */
mr_dev_ioctl(ds, MR_CTL_DAC_SET_CHANNEL_CONFIG, MR_MAKE_LOCAL(int, MR_ENABLE));
/* 获取通道状态 */
/* 获取通道配置 */
int state;
mr_dev_ioctl(ds, MR_CTL_DAC_GET_CHANNEL_STATE, &state);
mr_dev_ioctl(ds, MR_CTL_DAC_GET_CHANNEL_CONFIG, &state);
```
## 写入DAC设备通道值
@@ -155,7 +155,7 @@ int dac_init(void)
/* 设置到通道5 */
mr_dev_ioctl(dac_ds, MR_CTL_DAC_SET_CHANNEL, MR_MAKE_LOCAL(int, CHANNEL_NUMBER));
/* 设置通道使能 */
ret = mr_dev_ioctl(dac_ds, MR_CTL_DAC_SET_CHANNEL_STATE, MR_MAKE_LOCAL(int, MR_DAC_STATE_ENABLE));
ret = mr_dev_ioctl(dac_ds, MR_CTL_DAC_SET_CHANNEL_CONFIG, MR_MAKE_LOCAL(int, MR_ENABLE));
if (ret < 0)
{
mr_printf("Channel5 enable failed: %s\r\n", mr_strerror(ret));

View File

@@ -8,7 +8,7 @@
* [Close DAC Device](#close-dac-device)
* [Control DAC Device](#control-dac-device)
* [Set/Get Channel Number](#setget-channel-number)
* [Set/Get Channel Status](#setget-channel-status)
* [Set/Get Channel Configure](#setget-channel-configure)
* [Write DAC Device Channel Value](#write-dac-device-channel-value)
* [Usage Example:](#usage-example)
<!-- TOC -->
@@ -30,7 +30,9 @@ int mr_dev_open(const char *name, int oflags);
- `name`: DAC device name usually is: `dacx`, `dac1`, `dac2`.
- `oflags`: Open device flags, support `MR_OFLAG_WRONLY`.
Note: When using, the DAC device should be opened separately for different tasks according to actual situations, and the appropriate `oflags` should be used for management and permission control to ensure they will not interfere with each other.
Note: When using, the DAC device should be opened separately for different tasks according to actual situations, and the
appropriate `oflags` should be used for management and permission control to ensure they will not interfere with each
other.
## Close DAC Device
@@ -45,7 +47,8 @@ int mr_dev_close(int desc);
| `=0` | Close successfully |
| `<0` | Error code |
Note: When closing the device, all channels will be automatically restored to the default state. The channel needs to be reconfigured after reopening.
Note: When closing the device, all channels will be automatically restored to the default state. The channel needs to be
reconfigured after reopening(This feature can be turned off).
## Control DAC Device
@@ -64,9 +67,9 @@ int mr_dev_ioctl(int desc, int cmd, void *args);
- `cmd`: Command code, supports the following commands:
- `MR_CTL_DAC_SET_CHANNEL`: Set channel number.
- `MR_CTL_DAC_SET_CHANNEL_STATE`: Set channel state.
- `MR_CTL_DAC_SET_CHANNEL_CONFIG`: Set channel configure.
- `MR_CTL_DAC_GET_CHANNEL`: Get channel number.
- `MR_CTL_DAC_GET_CHANNEL_STATE`: Get channel state.
- `MR_CTL_DAC_GET_CHANNEL_CONFIG`: Get channel configure.
### Set/Get Channel Number
@@ -84,20 +87,20 @@ int number;
mr_dev_ioctl(ds, MR_CTL_DAC_GET_CHANNEL, &number);
```
### Set/Get Channel Status
### Set/Get Channel Configure
Channel status:
Channel configure:
- `MR_DAC_STATE_DISABLE`: Disable channel.
- `MR_DAC_STATE_ENABLE`: Enable channel.
- `MR_DISABLE`: Disable channel.
- `MR_ENABLE`: Enable channel.
```c
/* Set channel status */
mr_dev_ioctl(ds, MR_CTL_DAC_SET_CHANNEL_STATE, MR_MAKE_LOCAL(int, MR_DAC_STATE_ENABLE));
mr_dev_ioctl(ds, MR_CTL_DAC_SET_CHANNEL_CONFIG, MR_MAKE_LOCAL(int, MR_ENABLE));
/* Get channel status */
int state;
mr_dev_ioctl(ds, MR_CTL_DAC_GET_CHANNEL_STATE, &state);
mr_dev_ioctl(ds, MR_CTL_DAC_GET_CHANNEL_CONFIG, &state);
```
## Write DAC Device Channel Value
@@ -155,7 +158,7 @@ int dac_init(void)
/* Set to channel 5 */
mr_dev_ioctl(dac_ds, MR_CTL_DAC_SET_CHANNEL, MR_MAKE_LOCAL(int, CHANNEL_NUMBER));
/* Set channel enable */
ret = mr_dev_ioctl(dac_ds, MR_CTL_DAC_SET_CHANNEL_STATE, MR_MAKE_LOCAL(int, MR_DAC_STATE_ENABLE));
ret = mr_dev_ioctl(dac_ds, MR_CTL_DAC_SET_CHANNEL_CONFIG, MR_MAKE_LOCAL(int, MR_ENABLE));
if (ret < 0)
{
mr_printf("Channel5 enable failed: %s\r\n", mr_strerror(ret));
@@ -191,5 +194,5 @@ int main(void)
}
```
Enable DAC1 channel 5, output the DAC value every 500ms and print it
Enable DAC1 channel 5, output the DAC value every 500ms and print it
(the output value increases by 500 each time until reaching the maximum value 4095).

View File

@@ -37,11 +37,12 @@ int mr_i2c_dev_register(struct mr_i2c_dev *i2c_dev, const char *name, int addr,
| `=0` | Registration succeeds |
| `<0` | Error code |
- `name`: The I2C device needs to bind to the specified I2C bus, and the name needs to add the bus name, such as: `i2c1/dev-name`.
- `name`: The I2C device needs to bind to the specified I2C bus, and the name needs to add the bus name, such
as: `i2c1/dev-name`.
- `addr`: Device address (the lowest bit is read/write bit, please pass the address shifted to the left).
- `addr_bits`: Device address bits:
- `MR_I2C_ADDR_BITS_7`: 7-bit address.
- `MR_I2C_ADDR_BITS_10`: 10-bit address.
- `MR_I2C_ADDR_BITS_7`: 7-bit address.
- `MR_I2C_ADDR_BITS_10`: 10-bit address.
## Open I2C Device
@@ -57,10 +58,13 @@ int mr_dev_open(const char *name, int oflags);
| `>=0` | Device descriptor |
| `<0` | Error code |
- `name`: The I2C device is bound to the I2C bus and needs to add the bus name, such as: `i2cx/dev-name`,`i2c1/dev-name`.
- `name`: The I2C device is bound to the I2C bus and needs to add the bus name, such
as: `i2cx/dev-name`,`i2c1/dev-name`.
- `oflags`: Open device flags, support `MR_OFLAG_RDONLY`, `MR_OFLAG_WRONLY`, `MR_OFLAG_RDWR`.
Note: When using, the I2C device should be opened separately for different tasks according to the actual situation, and the appropriate `oflags` should be used for management and permission control to ensure that they will not interfere with each other.
Note: When using, the I2C device should be opened separately for different tasks according to the actual situation, and
the appropriate `oflags` should be used for management and permission control to ensure that they will not interfere
with each other.
## Close I2C Device
@@ -91,16 +95,16 @@ int mr_dev_ioctl(int desc, int cmd, void *args);
| `<0` | Error code |
- `cmd`: Command code, supports the following commands:
- `MR_CTL_I2C_SET_CONFIG`: Set I2C device configuration.
- `MR_CTL_I2C_SET_REG`: Set register value.
- `MR_CTL_I2C_SET_RD_BUFSZ`: Set read buffer size.
- `MR_CTL_I2C_CLR_RD_BUF`: Clear read buffer.
- `MR_CTL_I2C_SET_RD_CALL`:Set read callback function.
- `MR_CTL_I2C_GET_CONFIG`: Get I2C device configuration.
- `MR_CTL_I2C_GET_REG`: Get register value.
- `MR_CTL_I2C_GET_RD_BUFSZ`: Get read buffer size.
- `MR_CTL_I2C_GET_RD_DATASZ`: Get read buffer data size.
- `MR_CTL_I2C_GET_RD_CALL`:Get read callback function.
- `MR_CTL_I2C_SET_CONFIG`: Set I2C device configuration.
- `MR_CTL_I2C_SET_REG`: Set register value.
- `MR_CTL_I2C_SET_RD_BUFSZ`: Set read buffer size.
- `MR_CTL_I2C_CLR_RD_BUF`: Clear read buffer.
- `MR_CTL_I2C_SET_RD_CALL`:Set read callback function.
- `MR_CTL_I2C_GET_CONFIG`: Get I2C device configuration.
- `MR_CTL_I2C_GET_REG`: Get register value.
- `MR_CTL_I2C_GET_RD_BUFSZ`: Get read buffer size.
- `MR_CTL_I2C_GET_RD_DATASZ`: Get read buffer data size.
- `MR_CTL_I2C_GET_RD_CALL`:Get read callback function.
### Set/Get I2C Device Configuration
@@ -123,10 +127,12 @@ mr_dev_ioctl(ds, MR_CTL_I2C_GET_CONFIG, &config);
Note:
- If not manually configured, the default configuration is:
- Baud rate: `100000`
- Master/slave mode: `MR_I2C_HOST`
- Register bits: `MR_I2C_REG_BITS_8`
- When an I2C device on the I2C bus is configured to slave mode, it will continuously occupy the I2C bus. At this point, other I2C devices cannot perform read/write operations until the I2C device in slave mode is reconfigured to master mode.
- Baud rate: `100000`
- Master/slave mode: `MR_I2C_HOST`
- Register bits: `MR_I2C_REG_BITS_8`
- When an I2C device on the I2C bus is configured to slave mode, it will continuously occupy the I2C bus. At this point,
other I2C devices cannot perform read/write operations until the I2C device in slave mode is reconfigured to master
mode.
### Set/Get Register Value
@@ -158,7 +164,8 @@ mr_dev_ioctl(ds, MR_CTL_I2C_SET_RD_BUFSZ, &size);
mr_dev_ioctl(ds, MR_CTL_I2C_GET_RD_BUFSZ, &size);
```
Note: If not manually configured, it will use the size configured in `Kconfig` (default 32Byte). The read buffer is only used in slave mode.
Note: If not manually configured, it will use the size configured in `Kconfig` (default 32Byte). The read buffer is only
used in slave mode.
### Clear Read Buffer
@@ -225,8 +232,11 @@ if (size < 0)
Note:
- In host mode, data is read synchronously in polling mode. In slave mode, if the read buffer is not set, the data is read synchronously in polling mode. After the read buffer is set, the specified amount of data is read from the read buffer (the size of the actual read data is returned).
- When the register parameter is not negative, the write operation of the register value is inserted before the read operation.
- In host mode, data is read synchronously in polling mode. In slave mode, if the read buffer is not set, the data is
read synchronously in polling mode. After the read buffer is set, the specified amount of data is read from the read
buffer (the size of the actual read data is returned).
- When the register parameter is not negative, the write operation of the register value is inserted before the read
operation.
## Write I2C Device Data
@@ -254,7 +264,8 @@ if (size < 0)
}
```
Note: When the register parameter is not negative, a register value write operation will be inserted before the write operation.
Note: When the register parameter is not negative, a register value write operation will be inserted before the write
operation.
## Usage Example:
@@ -351,7 +362,8 @@ int main(void)
}
```
Connect I2C1 and I2C2 and perform sending and receiving test. I2C1 works as master and I2C2 works as slave. I2C2 will compare the received data with the sent data.
Connect I2C1 and I2C2 and perform sending and receiving test. I2C1 works as master and I2C2 works as slave. I2C2 will
compare the received data with the sent data.
Since the register value is set, the register value will be received first before the data writing.
## Software I2C

View File

@@ -67,11 +67,11 @@ int mr_dev_ioctl(int desc, int cmd, void *args);
| `<0` | 错误码 |
- `cmd`:命令码,支持以下命令:
- `MR_CTL_PIN_SET_NUMBER`:设置引脚编号。
- `MR_CTL_PIN_SET_MODE`:设置引脚模式。
- `MR_CTL_PIN_SET_EXTI_CALL`:设置外部中断回调函数。
- `MR_CTL_PIN_GET_NUMBER`:获取引脚编号。
- `MR_CTL_PIN_GET_EXTI_CALL`:获取外部中断回调函数。
- `MR_CTL_PIN_SET_NUMBER`:设置引脚编号。
- `MR_CTL_PIN_SET_MODE`:设置引脚模式。
- `MR_CTL_PIN_SET_EXTI_CALL`:设置外部中断回调函数。
- `MR_CTL_PIN_GET_NUMBER`:获取引脚编号。
- `MR_CTL_PIN_GET_EXTI_CALL`:获取外部中断回调函数。
### 设置/获取引脚编号
@@ -144,7 +144,7 @@ int call(int desc, void *args)
mr_dev_ioctl(ds, MR_CTL_PIN_SET_EXTI_CALL, call);
/* 获取外部中断回调函数 */
int (*callback)(int, void *args);
int (*callback)(int desc, void *args);
mr_dev_ioctl(ds, MR_CTL_PIN_GET_EXTI_CALL, &callback);
```
@@ -194,11 +194,11 @@ ssize_t mr_dev_write(int desc, const void *buf, size_t size);
| `>=0` | 写入数据大小 |
| `<0` | 错误码 |
- 电平:`0`:低电平;`1`:高电平,推荐使用宏:`MR_PIN_LOW_LEVEL``MR_PIN_HIGH_LEVEL`
- 电平:`0`:低电平;`1`:高电平。
```c
/* 写入引脚电平 */
uint8_t pin_level = MR_PIN_HIGH_LEVEL;
uint8_t pin_level = 1;
int ret = mr_dev_write(ds, &pin_level, sizeof(pin_level));
/* 是否写入成功 */
if (ret != sizeof(pin_level))

View File

@@ -34,7 +34,8 @@ int mr_dev_open(const char *name, int oflags);
- `name`: The PIN device name is usually `"pin"`.
- `oflags`: Open device flags, supports `MR_OFLAG_RDONLY`, `MR_OFLAG_WRONLY`, `MR_OFLAG_RDWR`.
Note: When using, different tasks should open the PIN device separately according to actual situations and use appropriate `oflags` for management and permission control to ensure they won't interfere with each other.
Note: When using, different tasks should open the PIN device separately according to actual situations and use
appropriate `oflags` for management and permission control to ensure they won't interfere with each other.
## Close PIN Device
@@ -49,7 +50,8 @@ int mr_dev_close(int desc);
| `=0` | Close successfully |
| `<0` | Error code |
Note: Closing the device will not restore the previous configuration by default. The user needs to restore it according to the actual situation.
Note: Closing the device will not restore the previous configuration by default. The user needs to restore it according
to the actual situation.
## Control PIN Device
@@ -77,9 +79,11 @@ int mr_dev_ioctl(int desc, int cmd, void *args);
#### Pin Number
Different MCUs have different GPIO quantities, functions, naming rules, etc. So MR uses digital numbers to define GPIO pins for unified interfaces on different MCUs.
Different MCUs have different GPIO quantities, functions, naming rules, etc. So MR uses digital numbers to define GPIO
pins for unified interfaces on different MCUs.
The default calculation formula is: `Number = Port * 16 + Pin`, where `Port` is the GPIO port number and `Pin` is the GPIO pin number.
The default calculation formula is: `Number = Port * 16 + Pin`, where `Port` is the GPIO port number and `Pin` is the
GPIO pin number.
For example, `PC13` corresponds to `Port` C, `Pin` 13, then `Number = (C - A) * 16 + 13 = 32 + 13 = 45`.
Note: This rule may not apply to all MCUs. Special requirements need to check the low-level driver settings.
@@ -144,14 +148,16 @@ int call(int desc, void *args)
mr_dev_ioctl(ds, MR_CTL_PIN_SET_EXTI_CALL, call);
/* Get external interrupt callback function */
int (*callback)(int, void *args);
int (*callback)(int desc, void *args);
mr_dev_ioctl(ds, MR_CTL_PIN_GET_EXTI_CALL, &callback);
```
Note:
- Before setting the external interrupt mode, you need to configure the callback function, otherwise, it becomes a callback-free interrupt.
- Even if the PIN device is closed, the callback function will not be invalid until the pin is set to a common mode (external interrupts will be ignored when the PIN device is closed).
- Before setting the external interrupt mode, you need to configure the callback function, otherwise, it becomes a
callback-free interrupt.
- Even if the PIN device is closed, the callback function will not be invalid until the pin is set to a common mode (
external interrupts will be ignored when the PIN device is closed).
## Read PIN Device Pin Level
@@ -194,11 +200,11 @@ ssize_t mr_dev_write(int desc, const void *buf, size_t size);
| `>=0` | Write data size |
| `<0` | Error code |
- Level: `0`: Low level; `1`: High level, recommended to use macros:`MR_PIN_LOW_LEVEL`, `MR_PIN_HIGH_LEVEL`.
- Level: `0`: Low level; `1`: High level.
```c
/* Write pin level */
uint8_t pin_level = MR_PIN_HIGH_LEVEL;
uint8_t pin_level = 1;
int ret = mr_dev_write(ds, &pin_level, sizeof(pin_level));
/* Check if write successfully */
if (ret != sizeof(pin_level))
@@ -295,6 +301,8 @@ int main(void)
```
After pressing the KEY, the LED will flip.
Observe the serial port print, you can see the LED and KEY descriptors. Although the external interrupt of the KEY pin is configured by the KEY,
no callback function is configured. Therefore, the KEY pin callback function inherits the previous configuration, that is, the callback function of the LED configuration,
Observe the serial port print, you can see the LED and KEY descriptors. Although the external interrupt of the KEY pin
is configured by the KEY,
no callback function is configured. Therefore, the KEY pin callback function inherits the previous configuration, that
is, the callback function of the LED configuration,
so the descriptor printed in the KEY callback function is the descriptor of the LED.

View File

@@ -134,6 +134,7 @@ if (size < 0)
```
注:
- 时间单位为微妙。单次读取最小单位为`uint32_t`即4个字节。
- 运行时间指定时器启动后运行的时间,超时后重置。
@@ -153,6 +154,7 @@ ssize_t mr_dev_write(int desc, const void *buf, size_t size);
| `<0` | 错误码 |
注:
- 时间单位为微妙。单次写入最小单位为`uint32_t`即4个字节。
- 当有一次写入多个单位数据时,仅实际生效最后一个有效数据(如果最后一个有效数据为`0`将关闭定时器)。

View File

@@ -17,29 +17,22 @@ extern "C" {
#ifdef MR_USING_ADC
/**
* @brief ADC channel state.
*/
#define MR_ADC_STATE_DISABLE MR_DISABLE /**< ADC disabled */
#define MR_ADC_STATE_ENABLE MR_ENABLE /**< ADC enabled */
/**
* @brief ADC configuration structure.
*/
struct mr_adc_config
{
uint32_t state: 1; /**< Channel state */
uint32_t reserved: 31; /**< Reserved */
int state; /**< Channel state */
};
/**
* @brief ADC control command.
*/
#define MR_CTL_ADC_SET_CHANNEL MR_CTL_SET_OFFSET /**< Set channel */
#define MR_CTL_ADC_SET_CHANNEL_STATE MR_CTL_SET_CONFIG /**< Set channel state */
#define MR_CTL_ADC_SET_CHANNEL_CONFIG MR_CTL_SET_CONFIG /**< Set channel config */
#define MR_CTL_ADC_GET_CHANNEL MR_CTL_GET_OFFSET /**< Get channel */
#define MR_CTL_ADC_GET_CHANNEL_STATE MR_CTL_GET_CONFIG /**< Get channel state */
#define MR_CTL_ADC_GET_CHANNEL_CONFIG MR_CTL_GET_CONFIG /**< Get channel config */
/**
* @brief ADC data type.

View File

@@ -17,29 +17,22 @@ extern "C" {
#ifdef MR_USING_DAC
/**
* @brief DAC channel state.
*/
#define MR_DAC_STATE_DISABLE MR_DISABLE /**< DAC disabled */
#define MR_DAC_STATE_ENABLE MR_ENABLE /**< DAC enabled */
/**
* @brief DAC configuration structure.
*/
struct mr_dac_config
{
uint32_t state: 1; /**< Channel state */
uint32_t reserved: 31; /**< Reserved */
int state; /**< Channel state */
};
/**
* @brief DAC control command.
*/
#define MR_CTL_DAC_SET_CHANNEL MR_CTL_SET_OFFSET /**< Set channel */
#define MR_CTL_DAC_SET_CHANNEL_STATE MR_CTL_SET_CONFIG /**< Set channel state */
#define MR_CTL_DAC_SET_CHANNEL_CONFIG MR_CTL_SET_CONFIG /**< Set channel config */
#define MR_CTL_DAC_GET_CHANNEL MR_CTL_GET_OFFSET /**< Get channel */
#define MR_CTL_DAC_GET_CHANNEL_STATE MR_CTL_GET_CONFIG /**< Get channel state */
#define MR_CTL_DAC_GET_CHANNEL_CONFIG MR_CTL_GET_CONFIG /**< Get channel config */
/**
* @brief DAC data type.

View File

@@ -46,9 +46,8 @@ extern "C" {
struct mr_i2c_config
{
uint32_t baud_rate; /**< Baud rate */
uint32_t host_slave: 1; /**< Host/slave */
uint32_t reg_bits: 6; /**< Register bits */
uint32_t reserved: 25;
int host_slave; /**< Host/slave */
int reg_bits; /**< Register bits */
};
/**

View File

@@ -17,12 +17,6 @@ extern "C" {
#ifdef MR_USING_PIN
/**
* @brief PIN level.
*/
#define MR_PIN_LOW_LEVEL (0) /* Low level */
#define MR_PIN_HIGH_LEVEL (1) /* High level */
/**
* @brief PIN mode.
*/
@@ -47,8 +41,7 @@ extern "C" {
*/
struct mr_pin_config
{
uint32_t mode: 4; /**< Mode */
uint32_t reserved: 28; /**< Reserved */
int mode; /**< Mode */
};
/**

View File

@@ -71,12 +71,11 @@ extern "C" {
struct mr_serial_config
{
uint32_t baud_rate; /**< Baud rate */
uint32_t data_bits: 4; /**< Data bits */
uint32_t stop_bits: 3; /**< Stop bits */
uint32_t parity: 2; /**< Parity */
uint32_t bit_order: 1; /**< Bit order */
uint32_t polarity: 1; /**< Polarity */
uint32_t reserved: 21;
int data_bits; /**< Data bits */
int stop_bits; /**< Stop bits */
int parity; /**< Parity */
int bit_order; /**< Bit order */
int polarity; /**< Polarity */
};
/**

View File

@@ -70,12 +70,11 @@ extern "C" {
struct mr_spi_config
{
uint32_t baud_rate; /**< Baud rate */
uint32_t host_slave: 1; /**< Host/slave */
uint32_t mode: 2; /**< Mode */
uint32_t data_bits: 6; /**< Data bits */
uint32_t bit_order: 1; /**< Bit order */
uint32_t reg_bits: 6; /**< Register bits */
uint32_t reserved: 16;
int host_slave; /**< Host/slave */
int mode; /**< Mode */
int data_bits; /**< Data bits */
int bit_order; /**< Bit order */
int reg_bits; /**< Register bits */
};
/**

View File

@@ -36,8 +36,7 @@ extern "C" {
*/
struct mr_timer_config
{
uint32_t mode: 1; /**< Mode */
uint32_t reserved: 31;
int mode; /**< Mode */
};
/**