diff --git a/README.md b/README.md index 4bbc0f1..92024f9 100644 --- a/README.md +++ b/README.md @@ -76,7 +76,7 @@ int main(void) { /* 打开SPI1总线下的SPI10设备 */ - int ds = mr_dev_open("spi1/spi10", MR_OFLAG_RDWR); + int ds = mr_dev_open("spi1/spi10", MR_O_RDWR); /* 发送数据 */ uint8_t wr_buf[] = {0x01, 0x02, 0x03, 0x04}; @@ -180,7 +180,7 @@ int main(void) 注: - 支持`MDK5`、`Eclipse`。 - - `MDK`版本过低可能导致`GNU`配置失败。 + - `MDK`未先编译或版本过低可能导致`GNU`配置失败。 ## 配置菜单选项 @@ -220,11 +220,11 @@ int main(void) mr_auto_init(); /* 打开PIN设备 */ - int ds = mr_dev_open("pin", MR_OFLAG_RDWR); + int ds = mr_dev_open("pin", MR_O_WRONLY); /* 设置到LED引脚 */ - mr_dev_ioctl(ds, MR_CTL_PIN_SET_NUMBER, MR_MAKE_LOCAL(int, LED_PIN_NUMBER)); + mr_dev_ioctl(ds, MR_IOC_PIN_SET_NUMBER, MR_MAKE_LOCAL(int, LED_PIN_NUMBER)); /* 设置LED引脚为推挽输出模式 */ - mr_dev_ioctl(ds, MR_CTL_PIN_SET_MODE, MR_MAKE_LOCAL(int, MR_PIN_MODE_OUTPUT)); + mr_dev_ioctl(ds, MR_IOC_PIN_SET_MODE, MR_MAKE_LOCAL(int, MR_PIN_MODE_OUTPUT)); while(1) { @@ -248,7 +248,7 @@ int main(void) mr_auto_init(); /* 打开Serial-1设备 */ - int ds = mr_dev_open("serial1", MR_OFLAG_RDWR); + int ds = mr_dev_open("serial1", MR_O_RDWR); /* 输出Hello World */ mr_dev_write(ds, "Hello World\r\n", sizeof("Hello World\r\n")); diff --git a/README_EN.md b/README_EN.md index ae231dd..cc046d3 100644 --- a/README_EN.md +++ b/README_EN.md @@ -83,7 +83,7 @@ Example: int main(void) { /* Open SPI10 device under SPI1 bus line */ - int ds = mr_dev_open("spi1/spi10", MR_OFLAG_RDWR); + int ds = mr_dev_open("spi1/spi10", MR_O_RDWR); /* Send data */ uint8_t wr_buf[] = {0x01, 0x02, 0x03, 0x04}; @@ -196,7 +196,7 @@ Versions later than `3.11.7` are not supported). Note: - Supports `MDK5` and `Eclipse`. - - If the `MDK` version is too early, the `GNU` configuration may fail. + - `MDK` uncompiled or too low a version can cause the 'GNU' configuration to fail. ## Configure Menu Options @@ -239,11 +239,11 @@ int main(void) mr_auto_init(); /* Open the PIN device */ - int ds = mr_dev_open("pin", MR_OFLAG_RDWR); + int ds = mr_dev_open("pin", MR_O_WRONLY); /* Set to the LED pin */ - mr_dev_ioctl(ds, MR_CTL_PIN_SET_NUMBER, MR_MAKE_LOCAL(int, LED_PIN_NUMBER)); + mr_dev_ioctl(ds, MR_IOC_PIN_SET_NUMBER, MR_MAKE_LOCAL(int, LED_PIN_NUMBER)); /* Set the LED pin to push-pull output mode */ - mr_dev_ioctl(ds, MR_CTL_PIN_SET_MODE, MR_MAKE_LOCAL(int, MR_PIN_MODE_OUTPUT)); + mr_dev_ioctl(ds, MR_IOC_PIN_SET_MODE, MR_MAKE_LOCAL(int, MR_PIN_MODE_OUTPUT)); while(1) { @@ -267,7 +267,7 @@ int main(void) mr_auto_init(); /* Open the Serial-1 device */ - int ds = mr_dev_open("serial1", MR_OFLAG_RDWR); + int ds = mr_dev_open("serial1", MR_O_RDWR); /* Output Hello World */ mr_dev_write(ds, "Hello World\r\n", sizeof("Hello World\r\n")); diff --git a/device/Kconfig b/device/Kconfig index 5fca7dc..8281b69 100644 --- a/device/Kconfig +++ b/device/Kconfig @@ -1,13 +1,13 @@ menu "Device configure" # Device - config MR_CFG_DEV_NAME_MAX + config MR_CFG_DEV_NAME_LEN int "Name max length" range 4 1024 default 12 help "This option sets the max length of the name." - config MR_CFG_DESC_MAX + config MR_CFG_DESC_NUM int "Descriptors max number" range 16 1024 default 64 @@ -106,7 +106,7 @@ menu "Device configure" config MR_USING_SOFT_I2C bool "Use Soft I2C" - default y + default n help "Use this option allows for the use of soft I2C." endmenu diff --git a/device/adc.c b/device/adc.c index 054098c..b5f7a8c 100644 --- a/device/adc.c +++ b/device/adc.c @@ -10,17 +10,15 @@ #ifdef MR_USING_ADC -static 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; - /* Check channel is valid */ if (channel < 0 || channel >= 32) { return MR_EINVAL; } - /* Configure the channel */ int ret = ops->channel_configure(adc, channel, config.state); if (ret < 0) { @@ -38,9 +36,8 @@ static int adc_channel_set_configure(struct mr_adc *adc, int channel, struct mr_ return MR_EOK; } -static 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) { - /* Check channel is valid */ if (channel < 0 || channel >= 32) { return MR_EINVAL; @@ -79,7 +76,7 @@ static int mr_adc_close(struct mr_dev *dev) return ops->configure(adc, MR_DISABLE); } -static ssize_t mr_adc_read(struct mr_dev *dev, int off, void *buf, size_t size, int async) +static ssize_t mr_adc_read(struct mr_dev *dev, void *buf, size_t count) { struct mr_adc *adc = (struct mr_adc *)dev; struct mr_adc_ops *ops = (struct mr_adc_ops *)dev->drv->ops; @@ -88,34 +85,33 @@ static ssize_t mr_adc_read(struct mr_dev *dev, int off, void *buf, size_t size, #ifdef MR_USING_ADC_CHANNEL_CHECK /* Check if the channel is enabled */ - if (MR_BIT_IS_SET(adc->channel, (1 << off)) == MR_DISABLE) + if (MR_BIT_IS_SET(adc->channel, (1 << dev->position)) == MR_DISABLE) { return MR_EINVAL; } #endif /* MR_USING_ADC_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)) { - *rd_buf = ops->read(adc, off); + *rd_buf = ops->read(adc, dev->position); rd_buf++; } return rd_size; } -static int mr_adc_ioctl(struct mr_dev *dev, int off, int cmd, void *args) +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_CTL_ADC_SET_CHANNEL_CONFIG: + 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, off, config); + int ret = adc_channel_set_configure(adc, dev->position, config); if (ret < 0) { return ret; @@ -124,13 +120,13 @@ static int mr_adc_ioctl(struct mr_dev *dev, int off, int cmd, void *args) } return MR_EINVAL; } - case MR_CTL_ADC_GET_CHANNEL_CONFIG: + 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, off, config); + int ret = adc_channel_get_configure(adc, dev->position, config); if (ret < 0) { return ret; @@ -149,12 +145,12 @@ static int mr_adc_ioctl(struct mr_dev *dev, int off, int cmd, void *args) * @brief This function registers an adc. * * @param adc The adc. - * @param name The name of the adc. + * @param path The path of the adc. * @param drv The driver of the adc. * - * @return MR_EOK on success, otherwise an error code. + * @return 0 on success, otherwise an error code. */ -int mr_adc_register(struct mr_adc *adc, const char *name, struct mr_drv *drv) +int mr_adc_register(struct mr_adc *adc, const char *path, struct mr_drv *drv) { static struct mr_dev_ops ops = { @@ -167,7 +163,7 @@ int mr_adc_register(struct mr_adc *adc, const char *name, struct mr_drv *drv) }; MR_ASSERT(adc != MR_NULL); - MR_ASSERT(name != MR_NULL); + MR_ASSERT(path != MR_NULL); MR_ASSERT(drv != MR_NULL); MR_ASSERT(drv->ops != MR_NULL); @@ -175,7 +171,7 @@ int mr_adc_register(struct mr_adc *adc, const char *name, struct mr_drv *drv) adc->channel = 0; /* Register the adc */ - return mr_dev_register(&adc->dev, name, Mr_Dev_Type_ADC, MR_SFLAG_RDONLY, &ops, drv); + return mr_dev_register(&adc->dev, path, MR_DEV_TYPE_ADC, MR_O_RDONLY, &ops, drv); } #endif /* MR_USING_ADC */ diff --git a/device/dac.c b/device/dac.c index 1300982..b57bc7c 100644 --- a/device/dac.c +++ b/device/dac.c @@ -10,17 +10,15 @@ #ifdef MR_USING_DAC -static int dac_channel_set_configure(struct mr_dac *dac, int channel, struct mr_dac_config config) +MR_INLINE 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; - /* Check channel is valid */ if (channel < 0 || channel >= 32) { return MR_EINVAL; } - /* Configure the channel */ int ret = ops->channel_configure(dac, channel, config.state); if (ret < 0) { @@ -38,9 +36,8 @@ static int dac_channel_set_configure(struct mr_dac *dac, int channel, struct mr_ return MR_EOK; } -static int dac_channel_get_configure(struct mr_dac *dac, int channel, struct mr_dac_config *config) +MR_INLINE 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) { return MR_EINVAL; @@ -79,7 +76,7 @@ static int mr_dac_close(struct mr_dev *dev) return ops->configure(dac, MR_DISABLE); } -static ssize_t mr_dac_write(struct mr_dev *dev, int off, const void *buf, size_t size, int async) +static ssize_t mr_dac_write(struct mr_dev *dev, const void *buf, size_t count) { struct mr_dac *dac = (struct mr_dac *)dev; struct mr_dac_ops *ops = (struct mr_dac_ops *)dev->drv->ops; @@ -88,34 +85,33 @@ static ssize_t mr_dac_write(struct mr_dev *dev, int off, const void *buf, size_t #ifdef MR_USING_DAC_CHANNEL_CHECK /* Check if the channel is enabled */ - if (MR_BIT_IS_SET(dac->channel, (1 << off)) == MR_DISABLE) + if (MR_BIT_IS_SET(dac->channel, (1 << dev->position)) == MR_DISABLE) { return MR_EINVAL; } #endif /* MR_USING_DAC_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)) { - ops->write(dac, off, *wr_buf); + ops->write(dac, dev->position, *wr_buf); wr_buf++; } return wr_size; } -static int mr_dac_ioctl(struct mr_dev *dev, int off, int cmd, void *args) +static int mr_dac_ioctl(struct mr_dev *dev, int cmd, void *args) { struct mr_dac *dac = (struct mr_dac *)dev; switch (cmd) { - case MR_CTL_DAC_SET_CHANNEL_CONFIG: + case MR_IOC_DAC_SET_CHANNEL_CONFIG: { if (args != MR_NULL) { struct mr_dac_config config = *((struct mr_dac_config *)args); - int ret = dac_channel_set_configure(dac, off, config); + int ret = dac_channel_set_configure(dac, dev->position, config); if (ret < 0) { return ret; @@ -124,13 +120,13 @@ static int mr_dac_ioctl(struct mr_dev *dev, int off, int cmd, void *args) } return MR_EINVAL; } - case MR_CTL_DAC_GET_CHANNEL_CONFIG: + case MR_IOC_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); + int ret = dac_channel_get_configure(dac, dev->position, config); if (ret < 0) { return ret; @@ -150,12 +146,12 @@ static int mr_dac_ioctl(struct mr_dev *dev, int off, int cmd, void *args) * @brief This function registers an dac. * * @param dac The dac. - * @param name The name of the dac. + * @param path The path of the dac. * @param drv The driver of the dac. * - * @return MR_EOK on success, otherwise an error code. + * @return 0 on success, otherwise an error code. */ -int mr_dac_register(struct mr_dac *dac, const char *name, struct mr_drv *drv) +int mr_dac_register(struct mr_dac *dac, const char *path, struct mr_drv *drv) { static struct mr_dev_ops ops = { @@ -168,7 +164,7 @@ int mr_dac_register(struct mr_dac *dac, const char *name, struct mr_drv *drv) }; MR_ASSERT(dac != MR_NULL); - MR_ASSERT(name != MR_NULL); + MR_ASSERT(path != MR_NULL); MR_ASSERT(drv != MR_NULL); MR_ASSERT(drv->ops != MR_NULL); @@ -176,7 +172,7 @@ int mr_dac_register(struct mr_dac *dac, const char *name, struct mr_drv *drv) dac->channel = 0; /* Register the dac */ - return mr_dev_register(&dac->dev, name, Mr_Dev_Type_DAC, MR_SFLAG_WRONLY, &ops, drv); + return mr_dev_register(&dac->dev, path, MR_DEV_TYPE_DAC, MR_O_WRONLY, &ops, drv); } #endif /* MR_USING_DAC */ diff --git a/device/i2c.c b/device/i2c.c index d5f9826..580480d 100644 --- a/device/i2c.c +++ b/device/i2c.c @@ -31,12 +31,12 @@ static int mr_i2c_bus_close(struct mr_dev *dev) return ops->configure(i2c_bus, &close_config, 0x00, MR_I2C_ADDR_BITS_7); } -static ssize_t mr_i2c_bus_read(struct mr_dev *dev, int off, void *buf, size_t size, int async) +static ssize_t mr_i2c_bus_read(struct mr_dev *dev, void *buf, size_t count) { return MR_ENOTSUP; } -static ssize_t mr_i2c_bus_write(struct mr_dev *dev, int off, const void *buf, size_t size, int async) +static ssize_t mr_i2c_bus_write(struct mr_dev *dev, const void *buf, size_t count) { return MR_ENOTSUP; } @@ -53,14 +53,10 @@ static ssize_t mr_i2c_bus_isr(struct mr_dev *dev, int event, void *args) struct mr_i2c_dev *i2c_dev = (struct mr_i2c_dev *)i2c_bus->owner; uint8_t data = ops->read(i2c_bus, MR_ENABLE); - /* Read data to FIFO. if callback is set, call it */ + /* Read data to FIFO */ mr_ringbuf_push_force(&i2c_dev->rd_fifo, data); - if (i2c_dev->dev.rd_call.call != MR_NULL) - { - ssize_t size = (ssize_t)mr_ringbuf_get_data_size(&i2c_dev->rd_fifo); - i2c_dev->dev.rd_call.call(i2c_dev->dev.rd_call.desc, &size); - } - return MR_EOK; + + return (ssize_t)mr_ringbuf_get_data_size(&i2c_dev->rd_fifo); } default: { @@ -73,12 +69,12 @@ static ssize_t mr_i2c_bus_isr(struct mr_dev *dev, int event, void *args) * @brief This function registers a i2c-bus. * * @param i2c_bus The i2c-bus. - * @param name The name of the i2c-bus. + * @param path The path of the i2c-bus. * @param drv The driver of the i2c-bus. * - * @return MR_EOK on success, otherwise an error code. + * @return 0 on success, otherwise an error code. */ -int mr_i2c_bus_register(struct mr_i2c_bus *i2c_bus, const char *name, struct mr_drv *drv) +int mr_i2c_bus_register(struct mr_i2c_bus *i2c_bus, const char *path, struct mr_drv *drv) { static struct mr_dev_ops ops = { @@ -92,7 +88,7 @@ int mr_i2c_bus_register(struct mr_i2c_bus *i2c_bus, const char *name, struct mr_ struct mr_i2c_config default_config = MR_I2C_CONFIG_DEFAULT; MR_ASSERT(i2c_bus != MR_NULL); - MR_ASSERT(name != MR_NULL); + MR_ASSERT(path != MR_NULL); MR_ASSERT(drv != MR_NULL); MR_ASSERT(drv->ops != MR_NULL); @@ -102,7 +98,7 @@ int mr_i2c_bus_register(struct mr_i2c_bus *i2c_bus, const char *name, struct mr_ i2c_bus->hold = MR_FALSE; /* Register the i2c-bus */ - return mr_dev_register(&i2c_bus->dev, name, Mr_Dev_Type_I2C, MR_SFLAG_RDWR, &ops, drv); + return mr_dev_register(&i2c_bus->dev, path, MR_DEV_TYPE_I2C, MR_O_RDWR, &ops, drv); } MR_INLINE int i2c_dev_take_bus(struct mr_i2c_dev *i2c_dev) @@ -119,7 +115,6 @@ MR_INLINE int i2c_dev_take_bus(struct mr_i2c_dev *i2c_dev) /* If the owner changes, recheck the configuration */ if (i2c_dev != i2c_bus->owner) { - /* Reconfigure the bus */ if (i2c_dev->config.baud_rate != i2c_bus->config.baud_rate || i2c_dev->config.host_slave != i2c_bus->config.host_slave) { @@ -161,7 +156,7 @@ MR_INLINE void i2c_dev_send_addr(struct mr_i2c_dev *i2c_dev, int rdwr) { struct mr_i2c_bus *i2c_bus = (struct mr_i2c_bus *)i2c_dev->dev.parent; struct mr_i2c_bus_ops *ops = (struct mr_i2c_bus_ops *)i2c_bus->dev.drv->ops; - int addr = 0, addr_bits = MR_I2C_ADDR_BITS_7; + int addr, addr_bits; /* Get the address, otherwise use the 0x00 */ addr = i2c_dev->addr; @@ -185,29 +180,29 @@ MR_INLINE void i2c_dev_send_stop(struct mr_i2c_dev *i2c_dev) ops->stop(i2c_bus); } -MR_INLINE ssize_t i2c_dev_read(struct mr_i2c_dev *i2c_dev, uint8_t *buf, size_t size) +MR_INLINE ssize_t i2c_dev_read(struct mr_i2c_dev *i2c_dev, uint8_t *buf, size_t count) { struct mr_i2c_bus *i2c_bus = (struct mr_i2c_bus *)i2c_dev->dev.parent; struct mr_i2c_bus_ops *ops = (struct mr_i2c_bus_ops *)i2c_bus->dev.drv->ops; uint8_t *rd_buf = (uint8_t *)buf; ssize_t rd_size; - for (rd_size = 0; rd_size < size; rd_size += sizeof(*rd_buf)) + for (rd_size = 0; rd_size < count; rd_size += sizeof(*rd_buf)) { - *rd_buf = ops->read(i2c_bus, (size - rd_size) == sizeof(*rd_buf)); + *rd_buf = ops->read(i2c_bus, (count - rd_size) == sizeof(*rd_buf)); rd_buf++; } return rd_size; } -MR_INLINE ssize_t i2c_dev_write(struct mr_i2c_dev *i2c_dev, const uint8_t *buf, size_t size) +MR_INLINE ssize_t i2c_dev_write(struct mr_i2c_dev *i2c_dev, const uint8_t *buf, size_t count) { struct mr_i2c_bus *i2c_bus = (struct mr_i2c_bus *)i2c_dev->dev.parent; struct mr_i2c_bus_ops *ops = (struct mr_i2c_bus_ops *)i2c_bus->dev.drv->ops; uint8_t *wr_buf = (uint8_t *)buf; ssize_t wr_size; - for (wr_size = 0; wr_size < size; wr_size += sizeof(*wr_buf)) + for (wr_size = 0; wr_size < count; wr_size += sizeof(*wr_buf)) { ops->write(i2c_bus, *wr_buf); wr_buf++; @@ -230,7 +225,7 @@ static int mr_i2c_dev_close(struct mr_dev *dev) return MR_EOK; } -static ssize_t mr_i2c_dev_read(struct mr_dev *dev, int off, void *buf, size_t size, int async) +static ssize_t mr_i2c_dev_read(struct mr_dev *dev, void *buf, size_t count) { struct mr_i2c_dev *i2c_dev = (struct mr_i2c_dev *)dev; @@ -243,23 +238,23 @@ static ssize_t mr_i2c_dev_read(struct mr_dev *dev, int off, void *buf, size_t si if (i2c_dev->config.host_slave == MR_I2C_HOST) { /* Send the address of the register that needs to be read */ - if (off >= 0) + if (dev->position >= 0) { i2c_dev_send_addr(i2c_dev, MR_I2C_WR); - i2c_dev_write(i2c_dev, (uint8_t *)&off, (i2c_dev->config.reg_bits >> 3)); + i2c_dev_write(i2c_dev, (uint8_t *)&dev->position, (i2c_dev->config.reg_bits >> 3)); } i2c_dev_send_addr(i2c_dev, MR_I2C_RD); - ret = i2c_dev_read(i2c_dev, (uint8_t *)buf, size); + ret = i2c_dev_read(i2c_dev, (uint8_t *)buf, count); i2c_dev_send_stop(i2c_dev); } else { if (mr_ringbuf_get_bufsz(&i2c_dev->rd_fifo) == 0) { - ret = i2c_dev_read(i2c_dev, (uint8_t *)buf, size); + ret = i2c_dev_read(i2c_dev, (uint8_t *)buf, count); } else { - ret = (ssize_t)mr_ringbuf_read(&i2c_dev->rd_fifo, buf, size); + ret = (ssize_t)mr_ringbuf_read(&i2c_dev->rd_fifo, buf, count); } } @@ -267,7 +262,7 @@ static ssize_t mr_i2c_dev_read(struct mr_dev *dev, int off, void *buf, size_t si return ret; } -static ssize_t mr_i2c_dev_write(struct mr_dev *dev, int off, const void *buf, size_t size, int async) +static ssize_t mr_i2c_dev_write(struct mr_dev *dev, const void *buf, size_t count) { struct mr_i2c_dev *i2c_dev = (struct mr_i2c_dev *)dev; @@ -282,29 +277,29 @@ static ssize_t mr_i2c_dev_write(struct mr_dev *dev, int off, const void *buf, si i2c_dev_send_addr(i2c_dev, MR_I2C_WR); /* Send the address of the register that needs to be written */ - if (off >= 0) + if (dev->position >= 0) { - i2c_dev_write(i2c_dev, (uint8_t *)&off, (i2c_dev->config.reg_bits >> 3)); + i2c_dev_write(i2c_dev, (uint8_t *)&dev->position, (i2c_dev->config.reg_bits >> 3)); } - ret = i2c_dev_write(i2c_dev, (uint8_t *)buf, size); + ret = i2c_dev_write(i2c_dev, (uint8_t *)buf, count); i2c_dev_send_stop(i2c_dev); } else { - ret = i2c_dev_write(i2c_dev, (uint8_t *)buf, size); + ret = i2c_dev_write(i2c_dev, (uint8_t *)buf, count); } i2c_dev_release_bus(i2c_dev); return ret; } -static int mr_i2c_dev_ioctl(struct mr_dev *dev, int off, int cmd, void *args) +static int mr_i2c_dev_ioctl(struct mr_dev *dev, int cmd, void *args) { struct mr_i2c_dev *i2c_dev = (struct mr_i2c_dev *)dev; switch (cmd) { - case MR_CTL_I2C_SET_CONFIG: + case MR_IOC_I2C_SET_CONFIG: { if (args != MR_NULL) { @@ -332,7 +327,7 @@ static int mr_i2c_dev_ioctl(struct mr_dev *dev, int off, int cmd, void *args) } return MR_EINVAL; } - case MR_CTL_I2C_SET_RD_BUFSZ: + case MR_IOC_I2C_SET_RD_BUFSZ: { if (args != MR_NULL) { @@ -349,12 +344,12 @@ static int mr_i2c_dev_ioctl(struct mr_dev *dev, int off, int cmd, void *args) } return MR_EINVAL; } - case MR_CTL_I2C_CLR_RD_BUF: + case MR_IOC_I2C_CLR_RD_BUF: { mr_ringbuf_reset(&i2c_dev->rd_fifo); return MR_EOK; } - case MR_CTL_I2C_GET_CONFIG: + case MR_IOC_I2C_GET_CONFIG: { if (args != MR_NULL) { @@ -365,7 +360,7 @@ static int mr_i2c_dev_ioctl(struct mr_dev *dev, int off, int cmd, void *args) } return MR_EINVAL; } - case MR_CTL_I2C_GET_RD_BUFSZ: + case MR_IOC_I2C_GET_RD_BUFSZ: { if (args != MR_NULL) { @@ -376,7 +371,7 @@ static int mr_i2c_dev_ioctl(struct mr_dev *dev, int off, int cmd, void *args) } return MR_EINVAL; } - case MR_CTL_I2C_GET_RD_DATASZ: + case MR_IOC_I2C_GET_RD_DATASZ: { if (args != MR_NULL) { @@ -397,13 +392,13 @@ static int mr_i2c_dev_ioctl(struct mr_dev *dev, int off, int cmd, void *args) * @brief This function registers a i2c-device. * * @param i2c_dev The i2c-device. - * @param name The name of the i2c-device. + * @param path The path of the i2c-device. * @param addr The address of the i2c-device. * @param addr_bits The number of address bits of the i2c-device. * - * @return MR_EOK on success, otherwise an error code. + * @return 0 on success, otherwise an error code. */ -int mr_i2c_dev_register(struct mr_i2c_dev *i2c_dev, const char *name, int addr, int addr_bits) +int mr_i2c_dev_register(struct mr_i2c_dev *i2c_dev, const char *path, int addr, int addr_bits) { static struct mr_dev_ops ops = { @@ -417,7 +412,7 @@ int mr_i2c_dev_register(struct mr_i2c_dev *i2c_dev, const char *name, int addr, struct mr_i2c_config default_config = MR_I2C_CONFIG_DEFAULT; MR_ASSERT(i2c_dev != MR_NULL); - MR_ASSERT(name != MR_NULL); + MR_ASSERT(path != MR_NULL); MR_ASSERT((addr_bits == MR_I2C_ADDR_BITS_7) || (addr_bits == MR_I2C_ADDR_BITS_10)); /* Initialize the fields */ @@ -431,7 +426,7 @@ int mr_i2c_dev_register(struct mr_i2c_dev *i2c_dev, const char *name, int addr, i2c_dev->addr_bits = addr_bits; /* Register the i2c-device */ - return mr_dev_register(&i2c_dev->dev, name, Mr_Dev_Type_I2C, MR_SFLAG_RDWR | MR_SFLAG_NONDRV, &ops, MR_NULL); + return mr_dev_register(&i2c_dev->dev, path, MR_DEV_TYPE_I2C, MR_O_RDWR, &ops, MR_NULL); } #endif /* MR_USING_I2C */ diff --git a/device/pwm.c b/device/pwm.c index 9119d54..e2a96d0 100644 --- a/device/pwm.c +++ b/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 */ diff --git a/device/timer.c b/device/timer.c index 53dc3ab..1f31ebe 100644 --- a/device/timer.c +++ b/device/timer.c @@ -14,9 +14,7 @@ static int timer_calculate(struct mr_timer *timer, uint32_t timeout) { uint32_t clk = timer->info->clk, psc_max = timer->info->prescaler_max, per_max = timer->info->period_max; uint32_t psc_best = 1, per_best = 1, reload_best = 1; - int error_min = INT32_MAX; - /* Check the clock and timeout */ if ((clk == 0) || (timeout == 0)) { return MR_EINVAL; @@ -32,12 +30,12 @@ static int timer_calculate(struct mr_timer *timer, uint32_t timeout) 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, product); psc < UINT32_MAX; psc++) { uint32_t per = MR_BOUND(product / psc, 1, per_max); - - /* Calculate the timeout error */ int error = (int)(timeout - (per * psc)); /* Found a valid and optimal solution */ @@ -56,13 +54,12 @@ static int timer_calculate(struct mr_timer *timer, uint32_t timeout) /* Calculate the reload and prescaler product */ product = psc_best * (clk / 1000000); + error_min = INT32_MAX; /* Calculate the least error reload and prescaler */ for (uint32_t reload = MR_BOUND(product / psc_max, 1, product); reload < product; reload++) { uint32_t psc = MR_BOUND(product / reload, 1, psc_max); - - /* Calculate the product error */ int error = (int)product - (int)(reload * psc); /* Found a valid and optimal solution */ @@ -115,27 +112,25 @@ static int mr_timer_close(struct mr_dev *dev) return ops->configure(timer, MR_DISABLE); } -static ssize_t mr_timer_read(struct mr_dev *dev, int off, void *buf, size_t size, int async) +static ssize_t mr_timer_read(struct mr_dev *dev, void *buf, size_t count) { struct mr_timer *timer = (struct mr_timer *)dev; struct mr_timer_ops *ops = (struct mr_timer_ops *)dev->drv->ops; uint32_t *rd_buf = (uint32_t *)buf; ssize_t rd_size; - 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)) { - uint32_t count = ops->get_count(timer); + uint32_t cnt = ops->get_count(timer); - /* Calculate the passed time */ *rd_buf = (timer->reload - timer->count) * timer->timeout + - (uint32_t)(((float)count / (float)timer->period) * (float)timer->timeout); + (uint32_t)(((float)cnt / (float)timer->period) * (float)timer->timeout); rd_buf++; } return rd_size; } -static ssize_t mr_timer_write(struct mr_dev *dev, int off, const void *buf, size_t size, int async) +static ssize_t mr_timer_write(struct mr_dev *dev, const void *buf, size_t count) { struct mr_timer *timer = (struct mr_timer *)dev; struct mr_timer_ops *ops = (struct mr_timer_ops *)dev->drv->ops; @@ -144,8 +139,7 @@ static ssize_t mr_timer_write(struct mr_dev *dev, int off, const void *buf, size ssize_t wr_size; /* Only the last write is valid */ - 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)) { timeout = *wr_buf; wr_buf++; @@ -170,13 +164,13 @@ 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 int mr_timer_ioctl(struct mr_dev *dev, int cmd, void *args) { struct mr_timer *timer = (struct mr_timer *)dev; switch (cmd) { - case MR_CTL_TIMER_SET_MODE: + case MR_IOC_TIMER_SET_MODE: { if (args != MR_NULL) { @@ -187,7 +181,7 @@ static int mr_timer_ioctl(struct mr_dev *dev, int off, int cmd, void *args) } return MR_EINVAL; } - case MR_CTL_TIMER_GET_MODE: + case MR_IOC_TIMER_GET_MODE: { if (args != MR_NULL) { @@ -239,13 +233,13 @@ static ssize_t mr_timer_isr(struct mr_dev *dev, int event, void *args) * @brief This function register a timer. * * @param timer The timer. - * @param name The name of the timer. + * @param path The path of the timer. * @param drv The driver of the timer. * @param info The information of the timer. * - * @return MR_EOK on success, otherwise an error code. + * @return 0 on success, otherwise an error code. */ -int mr_timer_register(struct mr_timer *timer, const char *name, struct mr_drv *drv, struct mr_timer_info *info) +int mr_timer_register(struct mr_timer *timer, const char *path, struct mr_drv *drv, struct mr_timer_info *info) { static struct mr_dev_ops ops = { @@ -259,7 +253,7 @@ int mr_timer_register(struct mr_timer *timer, const char *name, struct mr_drv *d struct mr_timer_config default_config = MR_TIMER_CONFIG_DEFAULT; MR_ASSERT(timer != 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); @@ -274,7 +268,7 @@ int mr_timer_register(struct mr_timer *timer, const char *name, struct mr_drv *d timer->info = info; /* Register the timer */ - return mr_dev_register(&timer->dev, name, Mr_Dev_Type_Timer, MR_SFLAG_RDWR, &ops, drv); + return mr_dev_register(&timer->dev, path, MR_DEV_TYPE_TIMER, MR_O_RDWR, &ops, drv); } #endif /* MR_USING_TIMER */ diff --git a/document/coding_style/coding_style.md b/document/coding_style/coding_style.md index c9a8929..94cd088 100644 --- a/document/coding_style/coding_style.md +++ b/document/coding_style/coding_style.md @@ -208,11 +208,11 @@ * @param dev The device. * @param name The name of the device. * @param type The type of the device. - * @param sflags The support flags of the device. + * @param flags The support flags of the device. * @param ops The operations of the device. * @param drv The driver of the device. * - * @return MR_EOK on success, otherwise an error code. + * @return 0 on success, otherwise an error code. */ ``` @@ -239,13 +239,13 @@ ```c /** - * @addtogroup Device description. + * @addtogroup Device description * @{ */ - int mr_dev_open(const char *name, uint32_t oflags); + int mr_dev_open(const char *name, uint32_t flags); int mr_dev_close(int desc); - ssize_t mr_dev_read(int desc, void *buf, size_t size); - ssize_t mr_dev_write(int desc, const void *buf, size_t size); + ssize_t mr_dev_read(int desc, void *buf, size_t count); + ssize_t mr_dev_write(int desc, const void *buf, size_t count); int mr_dev_ioctl(int desc, int cmd, void *args); /** @} */ ``` diff --git a/document/coding_style/coding_style_EN.md b/document/coding_style/coding_style_EN.md index 9692741..e6b755a 100644 --- a/document/coding_style/coding_style_EN.md +++ b/document/coding_style/coding_style_EN.md @@ -36,7 +36,7 @@ ## Spaces and Empty Lines -1. Add a space on each side of operators, without spaces for pre/post increment/decrement. +1. Add a space on each side of operators, without spaces for pre- / post-increment/decrement. For example: ````c @@ -188,11 +188,11 @@ * @param dev The device. * @param name The name of the device. * @param type The type of the device. - * @param sflags The support flags of the device. + * @param flags The support flags of the device. * @param ops The operations of the device. * @param drv The driver of the device. * - * @return MR_EOK on success, otherwise an error code. + * @return 0 on success, otherwise an error code. */ ``` @@ -222,10 +222,10 @@ * @addtogroup Device description * @{ */ - int mr_dev_open(const char *name, uint32_t oflags); + int mr_dev_open(const char *name, uint32_t flags); int mr_dev_close(int desc); - ssize_t mr_dev_read(int desc, void *buf, size_t size); - ssize_t mr_dev_write(int desc, const void *buf, size_t size); + ssize_t mr_dev_read(int desc, void *buf, size_t count); + ssize_t mr_dev_write(int desc, const void *buf, size_t count); int mr_dev_ioctl(int desc, int cmd, void *args); /** @} */ ``` \ No newline at end of file diff --git a/document/device/adc/adc.md b/document/device/adc/adc.md index 725f295..0a02002 100644 --- a/document/device/adc/adc.md +++ b/document/device/adc/adc.md @@ -16,21 +16,21 @@ ## 打开ADC设备 ```c -int mr_dev_open(const char *name, int oflags); +int mr_dev_open(const char *path, int flags); ``` | 参数 | 描述 | |---------|---------| -| name | 设备名称 | -| oflags | 打开设备的标志 | +| path | 设备路径 | +| flags | 打开设备的标志 | | **返回值** | | | `>=0` | 设备描述符 | | `<0` | 错误码 | -- `name`:ADC设备名称一般为:`adcx`、`adc1`、`adc2`。 -- `oflags`:打开设备的标志,支持 `MR_OFLAG_RDONLY`。 +- `path`:ADC设备路径一般为:`adcx`、`adc1`、`adc2`。 +- `flags`:打开设备的标志,支持 `MR_O_RDONLY`。 -注:使用时应根据实际情况为不同的任务分别打开ADC设备,并使用适当的`oflags`进行管理和权限控制,以确保它们不会相互影响。 +注:使用时应根据实际情况为不同的任务分别打开ADC设备,并使用适当的`flags`进行管理和权限控制,以确保它们不会相互影响。 ## 关闭ADC设备 @@ -63,10 +63,10 @@ int mr_dev_ioctl(int desc, int cmd, void *args); | `<0` | 错误码 | - `cmd`:命令码,支持以下命令: - - `MR_CTL_ADC_SET_CHANNEL`:设置通道编号。 - - `MR_CTL_ADC_SET_CHANNEL_CONFIG`:设置通道配置。 - - `MR_CTL_ADC_GET_CHANNEL`:获取通道编号。 - - `MR_CTL_ADC_GET_CHANNEL_CONFIG`:获取通道配置。 + - `MR_IOC_ADC_SET_CHANNEL`:设置通道编号。 + - `MR_IOC_ADC_SET_CHANNEL_CONFIG`:设置通道配置。 + - `MR_IOC_ADC_GET_CHANNEL`:获取通道编号。 + - `MR_IOC_ADC_GET_CHANNEL_CONFIG`:获取通道配置。 ### 设置/获取通道编号 @@ -77,11 +77,11 @@ int mr_dev_ioctl(int desc, int cmd, void *args); #define CHANNEL_NUMBER 5 /* 设置通道编号 */ -mr_dev_ioctl(ds, MR_CTL_ADC_SET_CHANNEL, MR_MAKE_LOCAL(int, CHANNEL_NUMBER)); +mr_dev_ioctl(ds, MR_IOC_ADC_SET_CHANNEL, MR_MAKE_LOCAL(int, CHANNEL_NUMBER)); /* 获取通道编号 */ int number; -mr_dev_ioctl(ds, MR_CTL_ADC_GET_CHANNEL, &number); +mr_dev_ioctl(ds, MR_IOC_ADC_GET_CHANNEL, &number); ``` 不依赖ADC接口: @@ -91,11 +91,11 @@ mr_dev_ioctl(ds, MR_CTL_ADC_GET_CHANNEL, &number); #define CHANNEL_NUMBER 5 /* 设置通道编号 */ -mr_dev_ioctl(ds, MR_CTL_SET_OFFSET, MR_MAKE_LOCAL(int, CHANNEL_NUMBER)); +mr_dev_ioctl(ds, MR_IOC_SPOS, MR_MAKE_LOCAL(int, CHANNEL_NUMBER)); /* 获取通道编号 */ int number; -mr_dev_ioctl(ds, MR_CTL_GET_OFFSET, &number); +mr_dev_ioctl(ds, MR_IOC_GPOS, &number); ``` ### 设置/获取通道配置 @@ -107,28 +107,28 @@ mr_dev_ioctl(ds, MR_CTL_GET_OFFSET, &number); ```c /* 设置通道配置 */ -mr_dev_ioctl(ds, MR_CTL_ADC_SET_CHANNEL_CONFIG, MR_MAKE_LOCAL(int, MR_ENABLE)); +mr_dev_ioctl(ds, MR_IOC_ADC_SET_CHANNEL_CONFIG, MR_MAKE_LOCAL(int, MR_ENABLE)); /* 获取通道配置 */ int state; -mr_dev_ioctl(ds, MR_CTL_ADC_GET_CHANNEL_CONFIG, &state); +mr_dev_ioctl(ds, MR_IOC_ADC_GET_CHANNEL_CONFIG, &state); ``` 不依赖ADC接口: ```c /* 设置通道配置 */ -mr_dev_ioctl(ds, MR_CTL_SET_CONFIG, MR_MAKE_LOCAL(int, MR_ENABLE)); +mr_dev_ioctl(ds, MR_IOC_SCFG, MR_MAKE_LOCAL(int, MR_ENABLE)); /* 获取通道配置 */ int state; -mr_dev_ioctl(ds, MR_CTL_GET_CONFIG, &state); +mr_dev_ioctl(ds, MR_IOC_GCFG, &state); ``` ## 读取ADC设备通道值 ```c -ssize_t mr_dev_read(int desc, void *buf, size_t size); +ssize_t mr_dev_read(int desc, void *buf, size_t count); ``` | 参数 | 描述 | @@ -164,29 +164,27 @@ if (ret != sizeof(data)) /* 定义ADC设备描述符 */ int adc_ds = -1; -int adc_init(void) +void adc_init(void) { int ret = MR_EOK; /* 初始化ADC */ - adc_ds = mr_dev_open("adc1", MR_OFLAG_RDONLY); + adc_ds = mr_dev_open("adc1", MR_O_RDONLY); if (adc_ds < 0) { mr_printf("ADC1 open failed: %s\r\n", mr_strerror(adc_ds)); - return adc_ds; + return; } /* 打印ADC描述符 */ mr_printf("ADC1 desc: %d\r\n", adc_ds); /* 设置到通道5 */ - mr_dev_ioctl(adc_ds, MR_CTL_ADC_SET_CHANNEL, MR_MAKE_LOCAL(int, CHANNEL_NUMBER)); + mr_dev_ioctl(adc_ds, MR_IOC_ADC_SET_CHANNEL, MR_MAKE_LOCAL(int, CHANNEL_NUMBER)); /* 设置通道使能 */ - ret = mr_dev_ioctl(adc_ds, MR_CTL_ADC_SET_CHANNEL_CONFIG, MR_MAKE_LOCAL(int, MR_ENABLE)); + ret = mr_dev_ioctl(adc_ds, MR_IOC_ADC_SET_CHANNEL_CONFIG, MR_MAKE_LOCAL(int, MR_ENABLE)); if (ret < 0) { mr_printf("Channel5 enable failed: %s\r\n", mr_strerror(ret)); - return ret; } - return MR_EOK; } /* 导出到自动初始化(APP级) */ MR_INIT_APP_EXPORT(adc_init); diff --git a/document/device/adc/adc_EN.md b/document/device/adc/adc_EN.md index 608fac1..33a1671 100644 --- a/document/device/adc/adc_EN.md +++ b/document/device/adc/adc_EN.md @@ -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 configure](#setget-channel-configure) + * [Set/Get Channel Configure](#setget-channel-configure) * [Read ADC Device Channel Value](#read-adc-device-channel-value) * [Usage Example:](#usage-example) @@ -16,22 +16,22 @@ ## Open ADC Device ```c -int mr_dev_open(const char *name, int oflags); +int mr_dev_open(const char *path, int flags); ``` | Parameter | Description | |:----------------:|:-----------------:| -| name | Device name | -| oflags | Open device flags | +| path | Device path | +| flags | Open device flags | | **Return Value** | | | `>=0` | Device descriptor | | `<0` | Error code | -- `name`: ADC device name usually is: `adcx`, `adc1`, `adc2`. -- `oflags`: Open device flags, support `MR_OFLAG_RDONLY`. +- `path`: ADC device path usually is: `adcx`, `adc1`, `adc2`. +- `flags`: Open device flags, support `MR_O_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 +appropriate `flags` should be used for management and permission control to ensure they will not interfere with each other. ## Close ADC Device @@ -66,10 +66,10 @@ int mr_dev_ioctl(int desc, int cmd, void *args); | `<0` | Error code | - `cmd`: Command code, supports the following commands: - - `MR_CTL_ADC_SET_CHANNEL`: Set channel number. - - `MR_CTL_ADC_SET_CHANNEL_CONFIG`: Set channel configure. - - `MR_CTL_ADC_GET_CHANNEL`: Get channel number. - - `MR_CTL_ADC_GET_CHANNEL_CONFIG`: Get channel configure. + - `MR_IOC_ADC_SET_CHANNEL`: Set channel number. + - `MR_IOC_ADC_SET_CHANNEL_CONFIG`: Set channel configure. + - `MR_IOC_ADC_GET_CHANNEL`: Get channel number. + - `MR_IOC_ADC_GET_CHANNEL_CONFIG`: Get channel configure. ### Set/Get Channel Number @@ -80,11 +80,11 @@ Channel number range: `0` ~ `31`. #define CHANNEL_NUMBER 5 /* Set channel number */ -mr_dev_ioctl(ds, MR_CTL_ADC_SET_CHANNEL, MR_MAKE_LOCAL(int, CHANNEL_NUMBER)); +mr_dev_ioctl(ds, MR_IOC_ADC_SET_CHANNEL, MR_MAKE_LOCAL(int, CHANNEL_NUMBER)); /* Get channel number */ int number; -mr_dev_ioctl(ds, MR_CTL_ADC_GET_CHANNEL, &number); +mr_dev_ioctl(ds, MR_IOC_ADC_GET_CHANNEL, &number); ``` Independent of ADC interface: @@ -94,11 +94,11 @@ Independent of ADC interface: #define CHANNEL_NUMBER 5 /* Set channel number */ -mr_dev_ioctl(ds, MR_CTL_SET_OFFSET, MR_MAKE_LOCAL(int, CHANNEL_NUMBER)); +mr_dev_ioctl(ds, MR_IOC_SPOS, MR_MAKE_LOCAL(int, CHANNEL_NUMBER)); /* Get channel number */ int number; -mr_dev_ioctl(ds, MR_CTL_GET_OFFSET, &number); +mr_dev_ioctl(ds, MR_IOC_GPOS, &number); ``` ### Set/Get Channel Configure @@ -110,28 +110,28 @@ Channel configure: ```c /* Set channel configure */ -mr_dev_ioctl(ds, MR_CTL_ADC_SET_CHANNEL_CONFIG, MR_MAKE_LOCAL(int, MR_ENABLE)); +mr_dev_ioctl(ds, MR_IOC_ADC_SET_CHANNEL_CONFIG, MR_MAKE_LOCAL(int, MR_ENABLE)); /* Get channel configure */ int state; -mr_dev_ioctl(ds, MR_CTL_ADC_GET_CHANNEL_CONFIG, &state); +mr_dev_ioctl(ds, MR_IOC_ADC_GET_CHANNEL_CONFIG, &state); ``` Independent of ADC interface: ```c /* Set channel configure */ -mr_dev_ioctl(ds, MR_CTL_SET_CONFIG, MR_MAKE_LOCAL(int, MR_ENABLE)); +mr_dev_ioctl(ds, MR_IOC_SCFG, MR_MAKE_LOCAL(int, MR_ENABLE)); /* Get channel configure */ int state; -mr_dev_ioctl(ds, MR_CTL_GET_CONFIG, &state); +mr_dev_ioctl(ds, MR_IOC_GCFG, &state); ``` ## Read ADC Device Channel Value ```c -ssize_t mr_dev_read(int desc, void *buf, size_t size); +ssize_t mr_dev_read(int desc, void *buf, size_t count); ``` | Parameter | Description | @@ -168,29 +168,27 @@ Note: The read data is the raw ADC data. The minimum read unit is `uint32_t`, th /* Define ADC device descriptor */ int adc_ds = -1; -int adc_init(void) +void adc_init(void) { int ret = MR_EOK; /* Initialize ADC */ - adc_ds = mr_dev_open("adc1", MR_OFLAG_RDONLY); + adc_ds = mr_dev_open("adc1", MR_O_RDONLY); if (adc_ds < 0) { mr_printf("ADC1 open failed: %s\r\n", mr_strerror(adc_ds)); - return adc_ds; + return; } /* Print ADC descriptor */ mr_printf("ADC1 desc: %d\r\n", adc_ds); /* Set to channel 5 */ - mr_dev_ioctl(adc_ds, MR_CTL_ADC_SET_CHANNEL, MR_MAKE_LOCAL(int, CHANNEL_NUMBER)); + mr_dev_ioctl(adc_ds, MR_IOC_ADC_SET_CHANNEL, MR_MAKE_LOCAL(int, CHANNEL_NUMBER)); /* Set channel enable */ - ret = mr_dev_ioctl(adc_ds, MR_CTL_ADC_SET_CHANNEL_CONFIG, MR_MAKE_LOCAL(int, MR_ENABLE)); + ret = mr_dev_ioctl(adc_ds, MR_IOC_ADC_SET_CHANNEL_CONFIG, MR_MAKE_LOCAL(int, MR_ENABLE)); if (ret < 0) { mr_printf("Channel5 enable failed: %s\r\n", mr_strerror(ret)); - return ret; } - return MR_EOK; } /* Export to automatic initialization (APP level) */ MR_INIT_APP_EXPORT(adc_init); diff --git a/document/device/dac/dac.md b/document/device/dac/dac.md index 85eac1d..7f085cb 100644 --- a/document/device/dac/dac.md +++ b/document/device/dac/dac.md @@ -16,21 +16,21 @@ ## 打开DAC设备 ```c -int mr_dev_open(const char *name, int oflags); +int mr_dev_open(const char *path, int flags); ``` | 参数 | 描述 | |---------|---------| -| name | 设备名称 | -| oflags | 打开设备的标志 | +| path | 设备路径 | +| flags | 打开设备的标志 | | **返回值** | | | `>=0` | 设备描述符 | | `<0` | 错误码 | -- `name`:DAC设备名称一般为:`dacx`、`dac1`、`dac2`。 -- `oflags`:打开设备的标志,支持 `MR_OFLAG_WRONLY`。 +- `path`:DAC设备路径一般为:`dacx`、`dac1`、`dac2`。 +- `flags`:打开设备的标志,支持 `MR_O_WRONLY`。 -注:使用时应根据实际情况为不同的任务分别打开DAC设备,并使用适当的`oflags`进行管理和权限控制,以确保它们不会相互影响。 +注:使用时应根据实际情况为不同的任务分别打开DAC设备,并使用适当的`flags`进行管理和权限控制,以确保它们不会相互影响。 ## 关闭DAC设备 @@ -63,10 +63,10 @@ int mr_dev_ioctl(int desc, int cmd, void *args); | `<0` | 错误码 | - `cmd`:命令码,支持以下命令: - - `MR_CTL_DAC_SET_CHANNEL`:设置通道编号。 - - `MR_CTL_DAC_SET_CHANNEL_CONFIG`:设置通道配置。 - - `MR_CTL_DAC_GET_CHANNEL`:获取通道编号。 - - `MR_CTL_DAC_GET_CHANNEL_CONFIG`:获取通道配置。 + - `MR_IOC_DAC_SET_CHANNEL`:设置通道编号。 + - `MR_IOC_DAC_SET_CHANNEL_CONFIG`:设置通道配置。 + - `MR_IOC_DAC_GET_CHANNEL`:获取通道编号。 + - `MR_IOC_DAC_GET_CHANNEL_CONFIG`:获取通道配置。 ### 设置/获取通道编号 @@ -77,11 +77,11 @@ int mr_dev_ioctl(int desc, int cmd, void *args); #define CHANNEL_NUMBER 5 /* 设置通道编号 */ -mr_dev_ioctl(ds, MR_CTL_DAC_SET_CHANNEL, MR_MAKE_LOCAL(int, CHANNEL_NUMBER)); +mr_dev_ioctl(ds, MR_IOC_DAC_SET_CHANNEL, MR_MAKE_LOCAL(int, CHANNEL_NUMBER)); /* 获取通道编号 */ int number; -mr_dev_ioctl(ds, MR_CTL_DAC_GET_CHANNEL, &number); +mr_dev_ioctl(ds, MR_IOC_DAC_GET_CHANNEL, &number); ``` 不依赖DAC接口: @@ -91,11 +91,11 @@ mr_dev_ioctl(ds, MR_CTL_DAC_GET_CHANNEL, &number); #define CHANNEL_NUMBER 5 /* 设置通道编号 */ -mr_dev_ioctl(ds, MR_CTL_SET_OFFSET, MR_MAKE_LOCAL(int, CHANNEL_NUMBER)); +mr_dev_ioctl(ds, MR_IOC_SPOS, MR_MAKE_LOCAL(int, CHANNEL_NUMBER)); /* 获取通道编号 */ int number; -mr_dev_ioctl(ds, MR_CTL_GET_OFFSET, &number); +mr_dev_ioctl(ds, MR_IOC_GPOS, &number); ``` ### 设置/获取通道配置 @@ -107,28 +107,28 @@ mr_dev_ioctl(ds, MR_CTL_GET_OFFSET, &number); ```c /* 设置通道配置 */ -mr_dev_ioctl(ds, MR_CTL_DAC_SET_CHANNEL_CONFIG, MR_MAKE_LOCAL(int, MR_ENABLE)); +mr_dev_ioctl(ds, MR_IOC_DAC_SET_CHANNEL_CONFIG, MR_MAKE_LOCAL(int, MR_ENABLE)); /* 获取通道配置 */ int state; -mr_dev_ioctl(ds, MR_CTL_DAC_GET_CHANNEL_CONFIG, &state); +mr_dev_ioctl(ds, MR_IOC_DAC_GET_CHANNEL_CONFIG, &state); ``` 不依赖DAC接口: ```c /* 设置通道配置 */ -mr_dev_ioctl(ds, MR_CTL_SET_CONFIG, MR_MAKE_LOCAL(int, MR_ENABLE)); +mr_dev_ioctl(ds, MR_IOC_SCFG, MR_MAKE_LOCAL(int, MR_ENABLE)); /* 获取通道配置 */ int state; -mr_dev_ioctl(ds, MR_CTL_GET_CONFIG, &state); +mr_dev_ioctl(ds, MR_IOC_GCFG, &state); ``` ## 写入DAC设备通道值 ```c -ssize_t mr_dev_write(int desc, const void *buf, size_t size); +ssize_t mr_dev_write(int desc, const void *buf, size_t count); ``` | 参数 | 描述 | @@ -159,40 +159,38 @@ if (ret != sizeof(data)) #include "include/mr_lib.h" /* 定义通道编号 */ -#define CHANNEL_NUMBER 5 +#define CHANNEL_NUMBER 1 /* 定义DAC设备描述符 */ int dac_ds = -1; -int dac_init(void) +void dac_init(void) { int ret = MR_EOK; /* 初始化DAC */ - dac_ds = mr_dev_open("dac1", MR_OFLAG_RDONLY); + dac_ds = mr_dev_open("dac1", MR_O_WRONLY); if (dac_ds < 0) { mr_printf("DAC1 open failed: %s\r\n", mr_strerror(dac_ds)); - return dac_ds; + return; } /* 打印DAC描述符 */ mr_printf("DAC1 desc: %d\r\n", dac_ds); - /* 设置到通道5 */ - mr_dev_ioctl(dac_ds, MR_CTL_DAC_SET_CHANNEL, MR_MAKE_LOCAL(int, CHANNEL_NUMBER)); + /* 设置到通道1 */ + mr_dev_ioctl(dac_ds, MR_IOC_DAC_SET_CHANNEL, MR_MAKE_LOCAL(int, CHANNEL_NUMBER)); /* 设置通道使能 */ - ret = mr_dev_ioctl(dac_ds, MR_CTL_DAC_SET_CHANNEL_CONFIG, MR_MAKE_LOCAL(int, MR_ENABLE)); + ret = mr_dev_ioctl(dac_ds, MR_IOC_DAC_SET_CHANNEL_CONFIG, MR_MAKE_LOCAL(int, MR_ENABLE)); if (ret < 0) { - mr_printf("Channel5 enable failed: %s\r\n", mr_strerror(ret)); - return ret; + mr_printf("Channel%d enable failed: %s\r\n", CHANNEL_NUMBER, mr_strerror(ret)); } - return MR_EOK; } /* 导出到自动初始化(APP级) */ MR_INIT_APP_EXPORT(dac_init); /* 定义DAC数据最大值 */ -#define DAC_DATA_MAX 4095 +#define DAC_DATA_MAX 4000 int main(void) { @@ -202,7 +200,7 @@ int main(void) while(1) { uint32_t data = 0; - for (data = 0; data < DAC_DATA_MAX; data += 500) + for (data = 0; data <= DAC_DATA_MAX; data += 500) { int ret = mr_dev_write(dac_ds, &data, sizeof(data)); if (ret != sizeof(data)) @@ -216,4 +214,4 @@ int main(void) } ``` -DAC1通道5使能,间隔500毫秒输出一次DAC值并打印(输出值递增,步进500,直到达到最大值4095)。 +DAC1通道1使能,间隔500毫秒输出一次DAC值并打印(输出值递增,步进500,直到达到最大值)。 diff --git a/document/device/dac/dac_EN.md b/document/device/dac/dac_EN.md index 76539c9..658c0be 100644 --- a/document/device/dac/dac_EN.md +++ b/document/device/dac/dac_EN.md @@ -16,22 +16,22 @@ ## Open DAC Device ```c -int mr_dev_open(const char *name, int oflags); +int mr_dev_open(const char *path, int flags); ``` | Parameter | Description | |------------------|-------------------| -| name | Device name | -| oflags | Open device flags | +| path | Device path | +| flags | Open device flags | | **Return Value** | | | `>=0` | Device descriptor | | `<0` | Error code | -- `name`: DAC device name usually is: `dacx`, `dac1`, `dac2`. -- `oflags`: Open device flags, support `MR_OFLAG_WRONLY`. +- `path`: DAC device path usually is: `dacx`, `dac1`, `dac2`. +- `flags`: Open device flags, support `MR_O_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 +appropriate `flags` should be used for management and permission control to ensure they will not interfere with each other. ## Close DAC Device @@ -66,10 +66,10 @@ int mr_dev_ioctl(int desc, int cmd, void *args); | `<0` | Error code | - `cmd`: Command code, supports the following commands: - - `MR_CTL_DAC_SET_CHANNEL`: Set channel number. - - `MR_CTL_DAC_SET_CHANNEL_CONFIG`: Set channel configure. - - `MR_CTL_DAC_GET_CHANNEL`: Get channel number. - - `MR_CTL_DAC_GET_CHANNEL_CONFIG`: Get channel configure. + - `MR_IOC_DAC_SET_CHANNEL`: Set channel number. + - `MR_IOC_DAC_SET_CHANNEL_CONFIG`: Set channel configure. + - `MR_IOC_DAC_GET_CHANNEL`: Get channel number. + - `MR_IOC_DAC_GET_CHANNEL_CONFIG`: Get channel configure. ### Set/Get Channel Number @@ -80,11 +80,11 @@ Channel number range: `0` ~ `31`. #define CHANNEL_NUMBER 5 /* Set channel number */ -mr_dev_ioctl(ds, MR_CTL_DAC_SET_CHANNEL, MR_MAKE_LOCAL(int, CHANNEL_NUMBER)); +mr_dev_ioctl(ds, MR_IOC_DAC_SET_CHANNEL, MR_MAKE_LOCAL(int, CHANNEL_NUMBER)); /* Get channel number */ int number; -mr_dev_ioctl(ds, MR_CTL_DAC_GET_CHANNEL, &number); +mr_dev_ioctl(ds, MR_IOC_DAC_GET_CHANNEL, &number); ``` Independent of DAC interface: @@ -94,11 +94,11 @@ Independent of DAC interface: #define CHANNEL_NUMBER 5 /* Set channel number */ -mr_dev_ioctl(ds, MR_CTL_SET_OFFSET, MR_MAKE_LOCAL(int, CHANNEL_NUMBER)); +mr_dev_ioctl(ds, MR_IOC_SPOS, MR_MAKE_LOCAL(int, CHANNEL_NUMBER)); /* Get channel number */ int number; -mr_dev_ioctl(ds, MR_CTL_GET_OFFSET, &number); +mr_dev_ioctl(ds, MR_IOC_GPOS, &number); ``` ### Set/Get Channel Configure @@ -110,28 +110,28 @@ Channel configure: ```c /* Set channel status */ -mr_dev_ioctl(ds, MR_CTL_DAC_SET_CHANNEL_CONFIG, MR_MAKE_LOCAL(int, MR_ENABLE)); +mr_dev_ioctl(ds, MR_IOC_DAC_SET_CHANNEL_CONFIG, MR_MAKE_LOCAL(int, MR_ENABLE)); /* Get channel status */ int state; -mr_dev_ioctl(ds, MR_CTL_DAC_GET_CHANNEL_CONFIG, &state); +mr_dev_ioctl(ds, MR_IOC_DAC_GET_CHANNEL_CONFIG, &state); ``` Independent of DAC interface: ```c /* Set channel status */ -mr_dev_ioctl(ds, MR_CTL_SET_CONFIG, MR_MAKE_LOCAL(int, MR_ENABLE)); +mr_dev_ioctl(ds, MR_IOC_SCFG, MR_MAKE_LOCAL(int, MR_ENABLE)); /* Get channel status */ int state; -mr_dev_ioctl(ds, MR_CTL_GET_CONFIG, &state); +mr_dev_ioctl(ds, MR_IOC_GCFG, &state); ``` ## Write DAC Device Channel Value ```c -ssize_t mr_dev_write(int desc, const void *buf, size_t size); +ssize_t mr_dev_write(int desc, const void *buf, size_t count); ``` | Parameter | Description | @@ -162,7 +162,7 @@ Note: The write data is the raw DAC data. The minimum write unit is `uint32_t`, #include "include/mr_lib.h" /* Define channel number */ -#define CHANNEL_NUMBER 5 +#define CHANNEL_NUMBER 1 /* Define DAC device descriptor */ int dac_ds = -1; @@ -172,7 +172,7 @@ int dac_init(void) int ret = MR_EOK; /* Initialize DAC */ - dac_ds = mr_dev_open("dac1", MR_OFLAG_RDONLY); + dac_ds = mr_dev_open("dac1", MR_O_WRONLY); if (dac_ds < 0) { mr_printf("DAC1 open failed: %s\r\n", mr_strerror(dac_ds)); @@ -180,13 +180,13 @@ int dac_init(void) } /* Print DAC descriptor */ mr_printf("DAC1 desc: %d\r\n", dac_ds); - /* Set to channel 5 */ - mr_dev_ioctl(dac_ds, MR_CTL_DAC_SET_CHANNEL, MR_MAKE_LOCAL(int, CHANNEL_NUMBER)); + /* Set to channel 1 */ + mr_dev_ioctl(dac_ds, MR_IOC_DAC_SET_CHANNEL, MR_MAKE_LOCAL(int, CHANNEL_NUMBER)); /* Set channel enable */ - ret = mr_dev_ioctl(dac_ds, MR_CTL_DAC_SET_CHANNEL_CONFIG, MR_MAKE_LOCAL(int, MR_ENABLE)); + ret = mr_dev_ioctl(dac_ds, MR_IOC_DAC_SET_CHANNEL_CONFIG, MR_MAKE_LOCAL(int, MR_ENABLE)); if (ret < 0) { - mr_printf("Channel5 enable failed: %s\r\n", mr_strerror(ret)); + mr_printf("Channel%d enable failed: %s\r\n", CHANNEL_NUMBER, mr_strerror(ret)); return ret; } return MR_EOK; @@ -195,7 +195,7 @@ int dac_init(void) MR_INIT_APP_EXPORT(dac_init); /* Define DAC data maximum value */ -#define DAC_DATA_MAX 4095 +#define DAC_DATA_MAX 4000 int main(void) { @@ -205,7 +205,7 @@ int main(void) while(1) { uint32_t data = 0; - for (data = 0; data < DAC_DATA_MAX; data += 500) + for (data = 0; data <= DAC_DATA_MAX; data += 500) { int ret = mr_dev_write(dac_ds, &data, sizeof(data)); if (ret != sizeof(data)) @@ -219,5 +219,5 @@ int main(void) } ``` -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). +Enable DAC1 channel 1, output the DAC value every 500ms and print it +(the output value increases by 500 each time until reaching the maximum value). diff --git a/document/device/i2c/i2c.md b/document/device/i2c/i2c.md index d7e6964..7ddfd72 100644 --- a/document/device/i2c/i2c.md +++ b/document/device/i2c/i2c.md @@ -24,21 +24,21 @@ ## 注册I2C设备 ```c -int mr_i2c_dev_register(struct mr_i2c_dev *i2c_dev, const char *name, int addr, int addr_bits); +int mr_i2c_dev_register(struct mr_i2c_dev *i2c_dev, const char *path, int addr, int addr_bits); ``` | 参数 | 描述 | |-----------|------------| | i2c_dev | I2C设备结构体指针 | -| name | 设备名称 | +| path | 设备路径 | | addr | 设备地址 | | addr_bits | 设备地址位数 | | **返回值** | | | `=0` | 注册成功 | | `<0` | 错误码 | -- `name`:I2C设备要绑定到指定的I2C总线,命名需要加上总线名称,例如:`i2c1/dev-name`。 -- `addr`:设备地址(最低位为读写位,请传入向左移位后的地址)。 +- `path`:I2C设备要绑定到指定的I2C总线,路径需要加上总线,例如:`i2cx/dev-name`, `i2c1/i2c10`。 +- `addr`:设备地址(最低位为读写位,请传入向左移位后的地址)。作为主机时,地址为对方地址,作为从机时,地址为自身地址。 - `addr_bits`:设备地址位数: - `MR_I2C_ADDR_BITS_7`:7位地址。 - `MR_I2C_ADDR_BITS_10`:10位地址。 @@ -46,21 +46,21 @@ int mr_i2c_dev_register(struct mr_i2c_dev *i2c_dev, const char *name, int addr, ## 打开I2C设备 ```c -int mr_dev_open(const char *name, int oflags); +int mr_dev_open(const char *path, int flags); ``` | 参数 | 描述 | |---------|---------| -| name | 设备名称 | -| oflags | 打开设备的标志 | +| path | 设备路径 | +| flags | 打开设备的标志 | | **返回值** | | | `>=0` | 设备描述符 | | `<0` | 错误码 | -- `name`:I2C设备绑定在I2C总线上,需加上总线名称,例如:`i2cx/dev-name`,`i2c1/dev-name`。 -- `oflags`:打开设备的标志,支持 `MR_OFLAG_RDONLY`、 `MR_OFLAG_WRONLY`、 `MR_OFLAG_RDWR`。 +- `path`:I2C设备路径,例如:`i2cx/dev-name`,`i2c1/i2c10`。 +- `flags`:打开设备的标志,支持 `MR_O_RDONLY`、 `MR_O_WRONLY`、 `MR_O_RDWR`。 -注:使用时应根据实际情况为不同的任务分别打开I2C设备,并使用适当的`oflags`进行管理和权限控制,以确保它们不会相互影响。 +注:使用时应根据实际情况为不同的任务分别打开I2C设备,并使用适当的`flags`进行管理和权限控制,以确保它们不会相互影响。 ## 关闭I2C设备 @@ -91,16 +91,16 @@ int mr_dev_ioctl(int desc, int cmd, void *args); | `<0` | 错误码 | - `cmd`:命令码,支持以下命令: - - `MR_CTL_I2C_SET_CONFIG`: 设置I2C设备配置。 - - `MR_CTL_I2C_SET_REG`: 设置寄存器值。 - - `MR_CTL_I2C_SET_RD_BUFSZ`: 设置读缓冲区大小。 - - `MR_CTL_I2C_CLR_RD_BUF`: 清空读缓冲区。 - - `MR_CTL_I2C_SET_RD_CALL`:设置读回调函数。 - - `MR_CTL_I2C_GET_CONFIG`: 获取I2C设备配置。 - - `MR_CTL_I2C_GET_REG`: 获取寄存器值。 - - `MR_CTL_I2C_GET_RD_BUFSZ`: 获取读缓冲区大小。 - - `MR_CTL_I2C_GET_RD_DATASZ`: 获取读缓冲区数据大小。 - - `MR_CTL_I2C_GET_RD_CALL`:获取读回调函数。 + - `MR_IOC_I2C_SET_CONFIG`: 设置I2C设备配置。 + - `MR_IOC_I2C_SET_REG`: 设置寄存器值。 + - `MR_IOC_I2C_SET_RD_BUFSZ`: 设置读缓冲区大小。 + - `MR_IOC_I2C_CLR_RD_BUF`: 清空读缓冲区。 + - `MR_IOC_I2C_SET_RD_CALL`:设置读回调函数。 + - `MR_IOC_I2C_GET_CONFIG`: 获取I2C设备配置。 + - `MR_IOC_I2C_GET_REG`: 获取寄存器值。 + - `MR_IOC_I2C_GET_RD_BUFSZ`: 获取读缓冲区大小。 + - `MR_IOC_I2C_GET_RD_DATASZ`: 获取读缓冲区数据大小。 + - `MR_IOC_I2C_GET_RD_CALL`:获取读回调函数。 ### 设置/获取I2C设备配置 @@ -115,9 +115,9 @@ I2C设备配置: struct mr_i2c_config config = MR_I2C_CONFIG_DEFAULT; /* 设置I2C设备配置 */ -mr_dev_ioctl(ds, MR_CTL_I2C_SET_CONFIG, &config); +mr_dev_ioctl(ds, MR_IOC_I2C_SET_CONFIG, &config); /* 获取I2C设备配置 */ -mr_dev_ioctl(ds, MR_CTL_I2C_GET_CONFIG, &config); +mr_dev_ioctl(ds, MR_IOC_I2C_GET_CONFIG, &config); ``` 不依赖I2C接口: @@ -127,9 +127,9 @@ mr_dev_ioctl(ds, MR_CTL_I2C_GET_CONFIG, &config); int config[] = {100000, 0, 8}; /* 设置I2C设备配置 */ -mr_dev_ioctl(ds, MR_CTL_SET_CONFIG, &config); +mr_dev_ioctl(ds, MR_IOC_SCFG, &config); /* 获取I2C设备配置 */ -mr_dev_ioctl(ds, MR_CTL_GET_CONFIG, &config); +mr_dev_ioctl(ds, MR_IOC_GCFG, &config); ``` 注: @@ -146,22 +146,22 @@ mr_dev_ioctl(ds, MR_CTL_GET_CONFIG, &config); ```c /* 设置寄存器值 */ -mr_dev_ioctl(ds, MR_CTL_I2C_SET_REG, MR_MAKE_LOCAL(int, 0x12)); +mr_dev_ioctl(ds, MR_IOC_I2C_SET_REG, MR_MAKE_LOCAL(int, 0x12)); /* 获取寄存器值 */ uint8_t reg; -mr_dev_ioctl(ds, MR_CTL_I2C_GET_REG, ®); +mr_dev_ioctl(ds, MR_IOC_I2C_GET_REG, ®); ``` 不依赖I2C接口: ```c /* 设置寄存器值 */ -mr_dev_ioctl(ds, MR_CTL_SET_OFFSET, MR_MAKE_LOCAL(int, 0x12)); +mr_dev_ioctl(ds, MR_IOC_SPOS, MR_MAKE_LOCAL(int, 0x12)); /* 获取寄存器值 */ uint8_t reg; -mr_dev_ioctl(ds, MR_CTL_GET_OFFSET, ®); +mr_dev_ioctl(ds, MR_IOC_GPOS, ®); ``` 注: @@ -176,9 +176,9 @@ mr_dev_ioctl(ds, MR_CTL_GET_OFFSET, ®); size_t size = 256; /* 设置读缓冲区大小 */ -mr_dev_ioctl(ds, MR_CTL_I2C_SET_RD_BUFSZ, &size); +mr_dev_ioctl(ds, MR_IOC_I2C_SET_RD_BUFSZ, &size); /* 获取读缓冲区大小 */ -mr_dev_ioctl(ds, MR_CTL_I2C_GET_RD_BUFSZ, &size); +mr_dev_ioctl(ds, MR_IOC_I2C_GET_RD_BUFSZ, &size); ``` 不依赖I2C接口: @@ -187,9 +187,9 @@ mr_dev_ioctl(ds, MR_CTL_I2C_GET_RD_BUFSZ, &size); size_t size = 256; /* 设置读缓冲区大小 */ -mr_dev_ioctl(ds, MR_CTL_SET_RD_BUFSZ, &size); +mr_dev_ioctl(ds, MR_IOC_SRBSZ, &size); /* 获取读缓冲区大小 */ -mr_dev_ioctl(ds, MR_CTL_GET_RD_BUFSZ, &size); +mr_dev_ioctl(ds, MR_IOC_GRBSZ, &size); ``` 注:如未手动配置,将使用 `Kconfig`中配置的大小(默认为32Byte)。只有在从机模式下才使用读缓冲区。 @@ -197,13 +197,13 @@ mr_dev_ioctl(ds, MR_CTL_GET_RD_BUFSZ, &size); ### 清空读缓冲区 ```c -mr_dev_ioctl(ds, MR_CTL_I2C_CLR_RD_BUF, MR_NULL); +mr_dev_ioctl(ds, MR_IOC_I2C_CLR_RD_BUF, MR_NULL); ``` 不依赖I2C接口: ```c -mr_dev_ioctl(ds, MR_CTL_CLR_RD_BUF, MR_NULL); +mr_dev_ioctl(ds, MR_IOC_CRBD, MR_NULL); ``` ### 获取读缓冲区数据大小 @@ -212,7 +212,7 @@ mr_dev_ioctl(ds, MR_CTL_CLR_RD_BUF, MR_NULL); size_t size = 0; /* 获取读缓冲区数据大小 */ -mr_dev_ioctl(ds, MR_CTL_I2C_GET_RD_DATASZ, &size); +mr_dev_ioctl(ds, MR_IOC_I2C_GET_RD_DATASZ, &size); ``` 不依赖I2C接口: @@ -221,55 +221,51 @@ mr_dev_ioctl(ds, MR_CTL_I2C_GET_RD_DATASZ, &size); size_t size = 0; /* 获取读缓冲区数据大小 */ -mr_dev_ioctl(ds, MR_CTL_GET_RD_DATASZ, &size); +mr_dev_ioctl(ds, MR_IOC_GRBDSZ, &size); ``` ### 设置/获取读回调函数 ```c /* 定义回调函数 */ -int call(int desc, void *args) +void fn(int desc, void *args) { - /* 获取缓冲区数据大小 */ - ssize_t data_size = *(ssize_t *)args; - - /* 处理中断 */ - - return MR_EOK; + /* 获取缓冲区数据大小 */ + ssize_t data_size = *(ssize_t *)args; + + /* 处理中断 */ } -int (*callback)(int, void *args); +void (*callback)(int desc, void *args); /* 设置读回调函数 */ -mr_dev_ioctl(ds, MR_CTL_I2C_SET_RD_CALL, &call); +mr_dev_ioctl(ds, MR_IOC_I2C_SET_RD_CALL, &fn); /* 获取读回调函数 */ -mr_dev_ioctl(ds, MR_CTL_I2C_GET_RD_CALL, &callback); +mr_dev_ioctl(ds, MR_IOC_I2C_GET_RD_CALL, &callback); ``` 不依赖I2C接口: ```c /* 定义回调函数 */ -int call(int desc, void *args) +void fn(int desc, void *args) { - /* 获取缓冲区数据大小 */ - ssize_t data_size = *(ssize_t *)args; - - /* 处理中断 */ - - return MR_EOK; + /* 获取缓冲区数据大小 */ + ssize_t data_size = *(ssize_t *)args; + + /* 处理中断 */ } -int (*callback)(int, void *args); +void (*callback)(int desc, void *args); /* 设置读回调函数 */ -mr_dev_ioctl(ds, MR_CTL_SET_RD_CALL, &call); +mr_dev_ioctl(ds, MR_IOC_SRCB, &fn); /* 获取读回调函数 */ -mr_dev_ioctl(ds, MR_CTL_GET_RD_CALL, &callback); +mr_dev_ioctl(ds, MR_IOC_GRCB, &callback); ``` ## 读取I2C设备数据 ```c -ssize_t mr_dev_read(int desc, void *buf, size_t size); +ssize_t mr_dev_read(int desc, void *buf, size_t count); ``` | 参数 | 描述 | @@ -300,7 +296,7 @@ if (size < 0) ## 写入I2C设备数据 ```c -ssize_t mr_dev_write(int desc, const void *buf, size_t size); +ssize_t mr_dev_write(int desc, const void *buf, size_t count); ``` | 参数 | 描述 | @@ -338,7 +334,7 @@ struct mr_i2c_dev slave_dev; int host_ds = -1; int slave_ds = -1; -int i2c_init(void) +void i2c_init(void) { int ret = MR_EOK; @@ -347,7 +343,7 @@ int i2c_init(void) if (ret < 0) { mr_printf("host i2c device register failed: %d\r\n", mr_strerror(ret)); - return ret; + return; } /* 注册I2C-SLAVE设备 */ @@ -355,36 +351,34 @@ int i2c_init(void) if (ret < 0) { mr_printf("slave i2c device register failed: %d\r\n", mr_strerror(ret)); - return ret; + return; } /* 打开I2C-HOST设备 */ - host_ds = mr_dev_open("i2c1/host", MR_OFLAG_RDWR); + host_ds = mr_dev_open("i2c1/host", MR_O_RDWR); if (host_ds < 0) { mr_printf("host i2c device open failed: %d\r\n", mr_strerror(ret)); - return ret; + return; } /* 设置寄存器值 */ - mr_dev_ioctl(host_ds, MR_CTL_I2C_SET_REG, MR_MAKE_LOCAL(int, 0x12)); + mr_dev_ioctl(host_ds, MR_IOC_I2C_SET_REG, MR_MAKE_LOCAL(int, 0x12)); /* 打开I2C-SLAVE设备 */ - slave_ds = mr_dev_open("i2c2/slave", MR_OFLAG_RDWR); + slave_ds = mr_dev_open("i2c2/slave", MR_O_RDWR); if (slave_ds < 0) { mr_printf("slave i2c device open failed: %d\r\n", mr_strerror(ret)); - return ret; + return; } /* 设置为从机模式 */ struct mr_i2c_config config = MR_I2C_CONFIG_DEFAULT; config.host_slave = MR_I2C_SLAVE; - ret = mr_dev_ioctl(slave_ds, MR_CTL_I2C_SET_CONFIG, &config); + ret = mr_dev_ioctl(slave_ds, MR_IOC_I2C_SET_CONFIG, &config); if (ret < 0) { mr_printf("slave i2c device set config failed: %d\r\n", mr_strerror(ret)); - return ret; } - return MR_EOK; } /* 导出到自动初始化(APP级) */ MR_INIT_APP_EXPORT(i2c_init); @@ -400,7 +394,7 @@ int main(void) /* 接收测试数据 */ uint8_t rd_buf[128]; - ssize_t ret = mr_dev_read(slave_ds, rd_buf, sizeof(rd_buf)); + mr_dev_read(slave_ds, rd_buf, sizeof(rd_buf)); /* 比较寄存器值 */ if (rd_buf[0] == 0x12) diff --git a/document/device/i2c/i2c_EN.md b/document/device/i2c/i2c_EN.md index 6ec53ab..ec7fa08 100644 --- a/document/device/i2c/i2c_EN.md +++ b/document/device/i2c/i2c_EN.md @@ -24,13 +24,13 @@ ## Register I2C Device ```c -int mr_i2c_dev_register(struct mr_i2c_dev *i2c_dev, const char *name, int addr, int addr_bits); +int mr_i2c_dev_register(struct mr_i2c_dev *i2c_dev, const char *path, int addr, int addr_bits); ``` | Parameter | Description | |------------------|------------------------------| | i2c_dev | I2C device structure pointer | -| name | Device name | +| path | Device path | | addr | Device address | | addr_bits | Device address bits | | **Return Value** | | @@ -38,8 +38,9 @@ int mr_i2c_dev_register(struct mr_i2c_dev *i2c_dev, const char *name, int addr, | `<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`. + as: `i2cx/dev-name`, `i2c1/i2c10`. - `addr`: Device address (the lowest bit is read/write bit, please pass the address shifted to the left). + When serving as a host, the address is the peer's address. When serving as a slave, the address is its own address. - `addr_bits`: Device address bits: - `MR_I2C_ADDR_BITS_7`: 7-bit address. - `MR_I2C_ADDR_BITS_10`: 10-bit address. @@ -47,23 +48,22 @@ int mr_i2c_dev_register(struct mr_i2c_dev *i2c_dev, const char *name, int addr, ## Open I2C Device ```c -int mr_dev_open(const char *name, int oflags); +int mr_dev_open(const char *path, int flags); ``` | Parameter | Description | |------------------|-------------------| -| name | Device name | -| oflags | Open device flags | +| path | Device path | +| flags | Open device flags | | **Return Value** | | | `>=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`. -- `oflags`: Open device flags, support `MR_OFLAG_RDONLY`, `MR_OFLAG_WRONLY`, `MR_OFLAG_RDWR`. +- `path`: The I2C device path, such as: `i2cx/dev-name`,`i2c1/i2c10`. +- `flags`: Open device flags, support `MR_O_RDONLY`, `MR_O_WRONLY`, `MR_O_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 +the appropriate `flags` should be used for management and permission control to ensure that they will not interfere with each other. ## Close I2C Device @@ -95,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_IOC_I2C_SET_CONFIG`: Set I2C device configuration. + - `MR_IOC_I2C_SET_REG`: Set register value. + - `MR_IOC_I2C_SET_RD_BUFSZ`: Set read buffer size. + - `MR_IOC_I2C_CLR_RD_BUF`: Clear read buffer. + - `MR_IOC_I2C_SET_RD_CALL`:Set read callback function. + - `MR_IOC_I2C_GET_CONFIG`: Get I2C device configuration. + - `MR_IOC_I2C_GET_REG`: Get register value. + - `MR_IOC_I2C_GET_RD_BUFSZ`: Get read buffer size. + - `MR_IOC_I2C_GET_RD_DATASZ`: Get read buffer data size. + - `MR_IOC_I2C_GET_RD_CALL`:Get read callback function. ### Set/Get I2C Device Configuration @@ -119,9 +119,9 @@ I2C device configuration: struct mr_i2c_config config = MR_I2C_CONFIG_DEFAULT; /* Set I2C device configuration */ -mr_dev_ioctl(ds, MR_CTL_I2C_SET_CONFIG, &config); +mr_dev_ioctl(ds, MR_IOC_I2C_SET_CONFIG, &config); /* Get I2C device configuration */ -mr_dev_ioctl(ds, MR_CTL_I2C_GET_CONFIG, &config); +mr_dev_ioctl(ds, MR_IOC_I2C_GET_CONFIG, &config); ``` Independent of I2C interface: @@ -131,9 +131,9 @@ Independent of I2C interface: int config[] = {100000, 0, 8}; /* Set I2C device configuration */ -mr_dev_ioctl(ds, MR_CTL_SET_CONFIG, &config); +mr_dev_ioctl(ds, MR_IOC_SCFG, &config); /* Get I2C device configuration */ -mr_dev_ioctl(ds, MR_CTL_GET_CONFIG, &config); +mr_dev_ioctl(ds, MR_IOC_GCFG, &config); ``` Note: @@ -152,22 +152,22 @@ The register value will be written first (range: `0` ~ `INT32_MAX`) before readi ```c /* Set register value */ -mr_dev_ioctl(ds, MR_CTL_I2C_SET_REG, MR_MAKE_LOCAL(int, 0x12)); +mr_dev_ioctl(ds, MR_IOC_I2C_SET_REG, MR_MAKE_LOCAL(int, 0x12)); /* Get register value */ uint8_t reg; -mr_dev_ioctl(ds, MR_CTL_I2C_GET_REG, ®); +mr_dev_ioctl(ds, MR_IOC_I2C_GET_REG, ®); ``` Independent of I2C interface: ```c /* Set register value */ -mr_dev_ioctl(ds, MR_CTL_SET_OFFSET, MR_MAKE_LOCAL(int, 0x12)); +mr_dev_ioctl(ds, MR_IOC_SPOS, MR_MAKE_LOCAL(int, 0x12)); /* Get register value */ uint8_t reg; -mr_dev_ioctl(ds, MR_CTL_GET_OFFSET, ®); +mr_dev_ioctl(ds, MR_IOC_GPOS, ®); ``` Note: @@ -182,9 +182,9 @@ Note: size_t size = 256; /* Set read buffer size */ -mr_dev_ioctl(ds, MR_CTL_I2C_SET_RD_BUFSZ, &size); +mr_dev_ioctl(ds, MR_IOC_I2C_SET_RD_BUFSZ, &size); /* Get read buffer size */ -mr_dev_ioctl(ds, MR_CTL_I2C_GET_RD_BUFSZ, &size); +mr_dev_ioctl(ds, MR_IOC_I2C_GET_RD_BUFSZ, &size); ``` Independent of I2C interface: @@ -193,9 +193,9 @@ Independent of I2C interface: size_t size = 256; /* Set read buffer size */ -mr_dev_ioctl(ds, MR_CTL_SET_RD_BUFSZ, &size); +mr_dev_ioctl(ds, MR_IOC_SRBSZ, &size); /* Get read buffer size */ -mr_dev_ioctl(ds, MR_CTL_GET_RD_BUFSZ, &size); +mr_dev_ioctl(ds, MR_IOC_GRBSZ, &size); ``` Note: If not manually configured, it will use the size configured in `Kconfig` (default 32Byte). The read buffer is only @@ -204,13 +204,13 @@ used in slave mode. ### Clear Read Buffer ```c -mr_dev_ioctl(ds, MR_CTL_I2C_CLR_RD_BUF, MR_NULL); +mr_dev_ioctl(ds, MR_IOC_I2C_CLR_RD_BUF, MR_NULL); ``` Independent of I2C interface: ```c -mr_dev_ioctl(ds, MR_CTL_CLR_RD_BUF, MR_NULL); +mr_dev_ioctl(ds, MR_IOC_CRBD, MR_NULL); ``` ### Get Read Buffer Data Size @@ -219,7 +219,7 @@ mr_dev_ioctl(ds, MR_CTL_CLR_RD_BUF, MR_NULL); size_t size = 0; /* Get read buffer data size */ -mr_dev_ioctl(ds, MR_CTL_I2C_GET_RD_DATASZ, &size); +mr_dev_ioctl(ds, MR_IOC_I2C_GET_RD_DATASZ, &size); ``` Independent of I2C interface: @@ -228,55 +228,51 @@ Independent of I2C interface: size_t size = 0; /* Get read buffer data size */ -mr_dev_ioctl(ds, MR_CTL_GET_RD_DATASZ, &size); +mr_dev_ioctl(ds, MR_IOC_GRBDSZ, &size); ``` ### Set/Get Read Callback Function ```c /* Define callback function */ -int call(int desc, void *args) +void fn(int desc, void *args) { /* Get buffer data size */ ssize_t data_size = *(ssize_t *)args; /* Handle interrupt */ - - return MR_EOK; } -int (*callback)(int, void *args); +void (*callback)(int desc, void *args); /* Set read callback function */ -mr_dev_ioctl(ds, MR_CTL_I2C_SET_RD_CALL, &call); +mr_dev_ioctl(ds, MR_IOC_I2C_SET_RD_CALL, &fn); /* Get read callback function */ -mr_dev_ioctl(ds, MR_CTL_I2C_GET_RD_CALL, &callback); +mr_dev_ioctl(ds, MR_IOC_I2C_GET_RD_CALL, &callback); ``` Independent of I2C interface: ```c /* Define callback function */ -int call(int desc, void *args) +void fn(int desc, void *args) { /* Get buffer data size */ ssize_t data_size = *(ssize_t *)args; /* Handle interrupt */ - - return MR_EOK; } -int (*callback)(int, void *args); +void (*callback)(int desc, void *args); /* Set read callback function */ -mr_dev_ioctl(ds, MR_CTL_SET_RD_CALL, &call); +mr_dev_ioctl(ds, MR_IOC_SRCB, &fn); /* Get read callback function */ -mr_dev_ioctl(ds, MR_CTL_GET_RD_CALL, &callback); +mr_dev_ioctl(ds, MR_IOC_GRCB, &callback); ``` ## Read I2C Device Data ```c -ssize_t mr_dev_read(int desc, void *buf, size_t size); +ssize_t mr_dev_read(int desc, void *buf, size_t count); ``` | Parameter | Description | @@ -311,7 +307,7 @@ Note: ## Write I2C Device Data ```c -ssize_t mr_dev_write(int desc, const void *buf, size_t size); +ssize_t mr_dev_write(int desc, const void *buf, size_t count); ``` | Parameter | Description | @@ -350,7 +346,7 @@ struct mr_i2c_dev slave_dev; int host_ds = -1; int slave_ds = -1; -int i2c_init(void) +void i2c_init(void) { int ret = MR_EOK; @@ -359,7 +355,7 @@ int i2c_init(void) if (ret < 0) { mr_printf("host i2c device register failed: %d\r\n", mr_strerror(ret)); - return ret; + return; } /* Register I2C-SLAVE device */ @@ -367,36 +363,34 @@ int i2c_init(void) if (ret < 0) { mr_printf("slave i2c device register failed: %d\r\n", mr_strerror(ret)); - return ret; + return; } /* Open I2C-HOST device */ - host_ds = mr_dev_open("i2c1/host", MR_OFLAG_RDWR); + host_ds = mr_dev_open("i2c1/host", MR_O_RDWR); if (host_ds < 0) { mr_printf("host i2c device open failed: %d\r\n", mr_strerror(ret)); - return ret; + return; } /* Set register value */ - mr_dev_ioctl(host_ds, MR_CTL_I2C_SET_REG, MR_MAKE_LOCAL(int, 0x12)); + mr_dev_ioctl(host_ds, MR_IOC_I2C_SET_REG, MR_MAKE_LOCAL(int, 0x12)); /* Open I2C-SLAVE device */ - slave_ds = mr_dev_open("i2c2/slave", MR_OFLAG_RDWR); + slave_ds = mr_dev_open("i2c2/slave", MR_O_RDWR); if (slave_ds < 0) { mr_printf("slave i2c device open failed: %d\r\n", mr_strerror(ret)); - return ret; + return; } /* Set slave mode */ struct mr_i2c_config config = MR_I2C_CONFIG_DEFAULT; config.host_slave = MR_I2C_SLAVE; - ret = mr_dev_ioctl(slave_ds, MR_CTL_I2C_SET_CONFIG, &config); + ret = mr_dev_ioctl(slave_ds, MR_IOC_I2C_SET_CONFIG, &config); if (ret < 0) { mr_printf("slave i2c device set config failed: %d\r\n", mr_strerror(ret)); - return ret; } - return MR_EOK; } /* Export to auto init (APP level) */ MR_INIT_APP_EXPORT(i2c_init); @@ -412,7 +406,7 @@ int main(void) /* Receive test data */ uint8_t rd_buf[128]; - ssize_t ret = mr_dev_read(slave_ds, rd_buf, sizeof(rd_buf)); + mr_dev_read(slave_ds, rd_buf, sizeof(rd_buf)); /* Compare register value */ if (rd_buf[0] == 0x12) diff --git a/document/device/pwm/pwm.md b/document/device/pwm/pwm.md index ef5dc96..aa9c73f 100644 --- a/document/device/pwm/pwm.md +++ b/document/device/pwm/pwm.md @@ -18,21 +18,21 @@ ## 打开PWM设备 ```c -int mr_dev_open(const char *name, int oflags); +int mr_dev_open(const char *path, int flags); ``` | 参数 | 描述 | |---------|---------| -| name | 设备名称 | -| oflags | 打开设备的标志 | +| path | 设备路径 | +| flags | 打开设备的标志 | | **返回值** | | | `>=0` | 设备描述符 | | `<0` | 错误码 | -- `name`:PWM设备名称一般为:`pwmx`、`pwm1`、`pwm2`。 -- `oflags`:打开设备的标志,支持 `MR_OFLAG_RDONLY`、 `MR_OFLAG_WRONLY`、 `MR_OFLAG_RDWR`。 +- `name`:PWM设备路径一般为:`pwmx`、`pwm1`、`pwm2`。 +- `flags`:打开设备的标志,支持 `MR_O_RDONLY`、 `MR_O_WRONLY`、 `MR_O_RDWR`。 -注:使用时应根据实际情况为不同的任务分别打开PWM设备,并使用适当的`oflags`进行管理和权限控制,以确保它们不会相互影响。 +注:使用时应根据实际情况为不同的任务分别打开PWM设备,并使用适当的`flags`进行管理和权限控制,以确保它们不会相互影响。 ## 关闭PWM设备 @@ -65,12 +65,12 @@ int mr_dev_ioctl(int desc, int cmd, void *args); | `<0` | 错误码 | - `cmd`:命令码,支持以下命令: - - `MR_CTL_PWM_SET_CHANNEL`:设置通道编号。 - - `MR_CTL_PWM_SET_CHANNEL_CONFIG`:设置通道配置。 - - `MR_CTL_PWM_SET_FREQ`:设置频率。 - - `MR_CTL_PWM_GET_CHANNEL`:获取通道编号。 - - `MR_CTL_PWM_GET_CHANNEL_CONFIG`:获取通道配置。 - - `MR_CTL_PWM_GET_FREQ`:获取频率。 + - `MR_IOC_PWM_SET_CHANNEL`:设置通道编号。 + - `MR_IOC_PWM_SET_CHANNEL_CONFIG`:设置通道配置。 + - `MR_IOC_PWM_SET_FREQ`:设置频率。 + - `MR_IOC_PWM_GET_CHANNEL`:获取通道编号。 + - `MR_IOC_PWM_GET_CHANNEL_CONFIG`:获取通道配置。 + - `MR_IOC_PWM_GET_FREQ`:获取频率。 ### 设置/获取通道编号 @@ -81,11 +81,11 @@ int mr_dev_ioctl(int desc, int cmd, void *args); #define CHANNEL_NUMBER 1 /* 设置通道编号 */ -mr_dev_ioctl(ds, MR_CTL_PWM_SET_CHANNEL, MR_MAKE_LOCAL(int, CHANNEL_NUMBER)); +mr_dev_ioctl(ds, MR_IOC_PWM_SET_CHANNEL, MR_MAKE_LOCAL(int, CHANNEL_NUMBER)); /* 获取通道编号 */ int number; -mr_dev_ioctl(ds, MR_CTL_PWM_GET_CHANNEL, &number); +mr_dev_ioctl(ds, MR_IOC_PWM_GET_CHANNEL, &number); ``` 不依赖PWM接口: @@ -95,11 +95,11 @@ mr_dev_ioctl(ds, MR_CTL_PWM_GET_CHANNEL, &number); #define CHANNEL_NUMBER 1 /* 设置通道编号 */ -mr_dev_ioctl(ds, MR_CTL_SET_OFFSET, MR_MAKE_LOCAL(int, CHANNEL_NUMBER)); +mr_dev_ioctl(ds, MR_IOC_SPOS, MR_MAKE_LOCAL(int, CHANNEL_NUMBER)); /* 获取通道编号 */ int number; -mr_dev_ioctl(ds, MR_CTL_GET_OFFSET, &number); +mr_dev_ioctl(ds, MR_IOC_GPOS, &number); ``` ### 设置/获取通道配置 @@ -113,9 +113,9 @@ mr_dev_ioctl(ds, MR_CTL_GET_OFFSET, &number); struct mr_pwm_config config = {MR_ENABLE, MR_PWM_POLARITY_NORMAL}; /* 设置通道配置 */ -mr_dev_ioctl(ds, MR_CTL_PWM_SET_CHANNEL_CONFIG, &config); +mr_dev_ioctl(ds, MR_IOC_PWM_SET_CHANNEL_CONFIG, &config); /* 获取通道配置 */ -mr_dev_ioctl(ds, MR_CTL_PWM_GET_CHANNEL_CONFIG, &config); +mr_dev_ioctl(ds, MR_IOC_PWM_GET_CHANNEL_CONFIG, &config); ``` 不依赖PWM接口: @@ -124,9 +124,9 @@ mr_dev_ioctl(ds, MR_CTL_PWM_GET_CHANNEL_CONFIG, &config); int config[] = {MR_ENABLE, 0}; /* 设置通道配置 */ -mr_dev_ioctl(ds, MR_CTL_SET_CONFIG, &config); +mr_dev_ioctl(ds, MR_IOC_SCFG, &config); /* 获取通道配置 */ -mr_dev_ioctl(ds, MR_CTL_GET_CONFIG, &config); +mr_dev_ioctl(ds, MR_IOC_GCFG, &config); ``` ### 设置/获取频率 @@ -136,11 +136,11 @@ mr_dev_ioctl(ds, MR_CTL_GET_CONFIG, &config); #define PWM_FREQ 1000 /* 设置频率 */ -mr_dev_ioctl(ds, MR_CTL_PWM_SET_FREQ, MR_MAKE_LOCAL(uint32_t, PWM_FREQ)); +mr_dev_ioctl(ds, MR_IOC_PWM_SET_FREQ, MR_MAKE_LOCAL(uint32_t, PWM_FREQ)); /* 获取频率 */ uint32_t freq; -mr_dev_ioctl(ds, MR_CTL_PWM_GET_FREQ, &freq); +mr_dev_ioctl(ds, MR_IOC_PWM_GET_FREQ, &freq); ``` 不依赖PWM接口: @@ -150,17 +150,17 @@ mr_dev_ioctl(ds, MR_CTL_PWM_GET_FREQ, &freq); #define PWM_FREQ 1000 /* 设置频率 */ -mr_dev_ioctl(ds, (0x01 << 8), MR_MAKE_LOCAL(uint32_t, PWM_FREQ)); +mr_dev_ioctl(ds, (0x01), MR_MAKE_LOCAL(uint32_t, PWM_FREQ)); /* 获取频率 */ uint32_t freq; -mr_dev_ioctl(ds, (-(0x01 << 8)), &freq); +mr_dev_ioctl(ds, (-(0x01)), &freq); ``` ## 读取PWM通道占空比 ```c -ssize_t mr_dev_read(int desc, void *buf, size_t size); +ssize_t mr_dev_read(int desc, void *buf, size_t count); ``` | 参数 | 描述 | @@ -188,7 +188,7 @@ if (ret != sizeof(duty)) ## 写入PWM通道占空比 ```c -ssize_t mr_dev_write(int desc, const void *buf, size_t size); +ssize_t mr_dev_write(int desc, const void *buf, size_t count); ``` | 参数 | 描述 | @@ -225,35 +225,33 @@ if (ret != sizeof(duty)) /* 定义PWM设备描述符 */ int pwm_ds = -1; -int pwm_init(void) +void pwm_init(void) { int ret = MR_EOK; /* 初始化PWM */ - pwm_ds = mr_dev_open("pwm1", MR_OFLAG_RDWR); + pwm_ds = mr_dev_open("pwm1", MR_O_RDWR); if (pwm_ds < 0) { mr_printf("PWM1 open failed: %s\r\n", mr_strerror(pwm_ds)); - return pwm_ds; + return; } /* 打印PWM描述符 */ mr_printf("PWM1 desc: %d\r\n", pwm_ds); /* 设置到通道1 */ - mr_dev_ioctl(pwm_ds, MR_CTL_PWM_SET_CHANNEL, MR_MAKE_LOCAL(int, CHANNEL_NUMBER)); + mr_dev_ioctl(pwm_ds, MR_IOC_PWM_SET_CHANNEL, MR_MAKE_LOCAL(int, CHANNEL_NUMBER)); /* 设置通道使能 */ - ret = mr_dev_ioctl(pwm_ds, MR_CTL_PWM_SET_CHANNEL_CONFIG, MR_MAKE_LOCAL(struct mr_pwm_config, MR_ENABLE, MR_PWM_POLARITY_NORMAL)); + ret = mr_dev_ioctl(pwm_ds, MR_IOC_PWM_SET_CHANNEL_CONFIG, MR_MAKE_LOCAL(struct mr_pwm_config, MR_ENABLE, MR_PWM_POLARITY_NORMAL)); if (ret < 0) { - mr_printf("Channel1 enable failed: %s\r\n", mr_strerror(ret)); - return ret; + mr_printf("Channel%d enable failed: %s\r\n", CHANNEL_NUMBER, mr_strerror(ret)); + return; } - ret = mr_dev_ioctl(pwm_ds, MR_CTL_PWM_SET_FREQ, MR_MAKE_LOCAL(uint32_t, FREQ)); + ret = mr_dev_ioctl(pwm_ds, MR_IOC_PWM_SET_FREQ, MR_MAKE_LOCAL(uint32_t, FREQ)); if (ret < 0) { mr_printf("Freq configure failed: %s\r\n", mr_strerror(ret)); - return ret; } - return MR_EOK; } /* 导出到自动初始化(APP级) */ MR_INIT_APP_EXPORT(pwm_init); diff --git a/document/device/pwm/pwm_EN.md b/document/device/pwm/pwm_EN.md index b18694e..5eb133b 100644 --- a/document/device/pwm/pwm_EN.md +++ b/document/device/pwm/pwm_EN.md @@ -18,21 +18,21 @@ ## Open PWM Device ```c -int mr_dev_open(const char *name, int oflags); +int mr_dev_open(const char *path, int flags); ``` | Parameter | Description | |------------------|-------------------------| -| name | Device name | -| oflags | Flag for opening device | +| path | Device path | +| flags | Flag for opening device | | **Return Value** | | | `>=0` | Device descriptor | | `<0` | Error code | -- `name`: PWM device name usually is: `pwmx`、`pwm1`、`pwm2`. -- `oflags`: Flag for opening device, support `MR_OFLAG_RDONLY`、 `MR_OFLAG_WRONLY`、 `MR_OFLAG_RDWR`. +- `path`: PWM device path usually is: `pwmx`、`pwm1`、`pwm2`. +- `flags`: Flag for opening device, support `MR_O_RDONLY`、 `MR_O_WRONLY`、 `MR_O_RDWR`. -Note: When using, the PWM device should be opened separately for different tasks with the appropriate `oflags` +Note: When using, the PWM device should be opened separately for different tasks with the appropriate `flags` for management and permission control, to ensure they will not interfere with each other. ## Close PWM Device @@ -67,12 +67,12 @@ int mr_dev_ioctl(int desc, int cmd, void *args); | `<0` | Error code | - `cmd`: Command code, support: - - `MR_CTL_PWM_SET_CHANNEL`: Set channel number. - - `MR_CTL_PWM_SET_CHANNEL_CONFIG`: Set channel configuration. - - `MR_CTL_PWM_SET_FREQ`: Set frequency. - - `MR_CTL_PWM_GET_CHANNEL`: Get channel number. - - `MR_CTL_PWM_GET_CHANNEL_CONFIG`: Get channel configuration. - - `MR_CTL_PWM_GET_FREQ`: Get frequency. + - `MR_IOC_PWM_SET_CHANNEL`: Set channel number. + - `MR_IOC_PWM_SET_CHANNEL_CONFIG`: Set channel configuration. + - `MR_IOC_PWM_SET_FREQ`: Set frequency. + - `MR_IOC_PWM_GET_CHANNEL`: Get channel number. + - `MR_IOC_PWM_GET_CHANNEL_CONFIG`: Get channel configuration. + - `MR_IOC_PWM_GET_FREQ`: Get frequency. ### Set/Get Channel Number @@ -83,11 +83,11 @@ Channel number range: `0` ~ `31`. #define CHANNEL_NUMBER 1 /* Set channel number */ -mr_dev_ioctl(ds, MR_CTL_PWM_SET_CHANNEL, MR_MAKE_LOCAL(int, CHANNEL_NUMBER)); +mr_dev_ioctl(ds, MR_IOC_PWM_SET_CHANNEL, MR_MAKE_LOCAL(int, CHANNEL_NUMBER)); /* Get channel number */ int number; -mr_dev_ioctl(ds, MR_CTL_PWM_GET_CHANNEL, &number); +mr_dev_ioctl(ds, MR_IOC_PWM_GET_CHANNEL, &number); ``` Independent of PWM interface: @@ -97,11 +97,11 @@ Independent of PWM interface: #define CHANNEL_NUMBER 1 /* Set channel number */ -mr_dev_ioctl(ds, MR_CTL_SET_OFFSET, MR_MAKE_LOCAL(int, CHANNEL_NUMBER)); +mr_dev_ioctl(ds, MR_IOC_SPOS, MR_MAKE_LOCAL(int, CHANNEL_NUMBER)); /* Get channel number */ int number; -mr_dev_ioctl(ds, MR_CTL_GET_OFFSET, &number); +mr_dev_ioctl(ds, MR_IOC_GPOS, &number); ``` ### Set/Get Channel Configuration @@ -115,9 +115,9 @@ Channel configuration: struct mr_pwm_config config = {MR_ENABLE, MR_PWM_POLARITY_NORMAL}; /* Set channel configuration */ -mr_dev_ioctl(ds, MR_CTL_PWM_SET_CHANNEL_CONFIG, &config); +mr_dev_ioctl(ds, MR_IOC_PWM_SET_CHANNEL_CONFIG, &config); /* Get channel configuration */ -mr_dev_ioctl(ds, MR_CTL_PWM_GET_CHANNEL_CONFIG, &config); +mr_dev_ioctl(ds, MR_IOC_PWM_GET_CHANNEL_CONFIG, &config); ``` Independent of PWM interface: @@ -126,9 +126,9 @@ Independent of PWM interface: int config[] = {MR_ENABLE, 0}; /* Set channel configuration */ -mr_dev_ioctl(ds, MR_CTL_SET_CONFIG, &config); +mr_dev_ioctl(ds, MR_IOC_SCFG, &config); /* Get channel configuration */ -mr_dev_ioctl(ds, MR_CTL_GET_CONFIG, &config); +mr_dev_ioctl(ds, MR_IOC_GCFG, &config); ``` ### Set/Get Frequency @@ -138,11 +138,11 @@ mr_dev_ioctl(ds, MR_CTL_GET_CONFIG, &config); #define PWM_FREQ 1000 /* Set frequency */ -mr_dev_ioctl(ds, MR_CTL_PWM_SET_FREQ, MR_MAKE_LOCAL(uint32_t, PWM_FREQ)); +mr_dev_ioctl(ds, MR_IOC_PWM_SET_FREQ, MR_MAKE_LOCAL(uint32_t, PWM_FREQ)); /* Get frequency */ uint32_t freq; -mr_dev_ioctl(ds, MR_CTL_PWM_GET_FREQ, &freq); +mr_dev_ioctl(ds, MR_IOC_PWM_GET_FREQ, &freq); ``` Independent of PWM interface: @@ -152,17 +152,17 @@ Independent of PWM interface: #define PWM_FREQ 1000 /* Set frequency */ -mr_dev_ioctl(ds, (0x01 << 8), MR_MAKE_LOCAL(uint32_t, PWM_FREQ)); +mr_dev_ioctl(ds, (0x01), MR_MAKE_LOCAL(uint32_t, PWM_FREQ)); /* Get frequency */ uint32_t freq; -mr_dev_ioctl(ds, (-(0x01 << 8)), &freq); +mr_dev_ioctl(ds, (-(0x01)), &freq); ``` ## Read PWM Channel Duty Cycle ```c -ssize_t mr_dev_read(int desc, void *buf, size_t size); +ssize_t mr_dev_read(int desc, void *buf, size_t count); ``` | Parameter | Description | @@ -191,7 +191,7 @@ The minimum unit for single reading is `uint32_t`, which is 4 bytes. ## Write PWM Channel Duty Cycle ```c -ssize_t mr_dev_write(int desc, const void *buf, size_t size); +ssize_t mr_dev_write(int desc, const void *buf, size_t count); ``` | Parameter | Description | @@ -229,35 +229,33 @@ The minimum unit for single writing is `uint32_t`, which is 4 bytes. /* PWM device descriptor */ int pwm_ds = -1; -int pwm_init(void) +void pwm_init(void) { int ret = MR_EOK; /* PWM initialization */ - pwm_ds = mr_dev_open("pwm1", MR_OFLAG_RDWR); + pwm_ds = mr_dev_open("pwm1", MR_O_RDWR); if (pwm_ds < 0) { mr_printf("PWM1 open failed: %s\r\n", mr_strerror(pwm_ds)); - return pwm_ds; + return; } /* Print PWM descriptor */ mr_printf("PWM1 desc: %d\r\n", pwm_ds); /* Set to channel 1*/ - mr_dev_ioctl(pwm_ds, MR_CTL_PWM_SET_CHANNEL, MR_MAKE_LOCAL(int, CHANNEL_NUMBER)); + mr_dev_ioctl(pwm_ds, MR_IOC_PWM_SET_CHANNEL, MR_MAKE_LOCAL(int, CHANNEL_NUMBER)); /* Set channel enable */ - ret = mr_dev_ioctl(pwm_ds, MR_CTL_PWM_SET_CHANNEL_CONFIG, MR_MAKE_LOCAL(struct mr_pwm_config, MR_ENABLE, MR_PWM_POLARITY_NORMAL)); + ret = mr_dev_ioctl(pwm_ds, MR_IOC_PWM_SET_CHANNEL_CONFIG, MR_MAKE_LOCAL(struct mr_pwm_config, MR_ENABLE, MR_PWM_POLARITY_NORMAL)); if (ret < 0) { - mr_printf("Channel1 enable failed: %s\r\n", mr_strerror(ret)); - return ret; + mr_printf("Channel%d enable failed: %s\r\n", CHANNEL_NUMBER, mr_strerror(ret)); + return; } - ret = mr_dev_ioctl(pwm_ds, MR_CTL_PWM_SET_FREQ, MR_MAKE_LOCAL(uint32_t, FREQ)); + ret = mr_dev_ioctl(pwm_ds, MR_IOC_PWM_SET_FREQ, MR_MAKE_LOCAL(uint32_t, FREQ)); if (ret < 0) { mr_printf("Freq configure failed: %s\r\n", mr_strerror(ret)); - return ret; } - return MR_EOK; } /* Export to automatic initialization (APP level) */ MR_INIT_APP_EXPORT(pwm_init); diff --git a/document/device/timer/timer.md b/document/device/timer/timer.md index 2d40320..148fee5 100644 --- a/document/device/timer/timer.md +++ b/document/device/timer/timer.md @@ -17,21 +17,21 @@ ## 打开TIMER设备 ```c -int mr_dev_open(const char *name, int oflags); +int mr_dev_open(const char *path, int flags); ``` | 参数 | 描述 | |---------|---------| -| name | 设备名称 | -| oflags | 打开设备的标志 | +| path | 设备路径 | +| flags | 打开设备的标志 | | **返回值** | | | `>=0` | 设备描述符 | | `<0` | 错误码 | -- `name`:TIMER设备名称一般为:`timerx`,例如:`timer1`、`timer2`、`timer3`。 -- `oflags`:打开设备的标志,支持 `MR_OFLAG_RDONLY`、 `MR_OFLAG_WRONLY`、 `MR_OFLAG_RDWR`。 +- `path`:TIMER设备路径一般为:`timerx`,例如:`timer1`、`timer2`、`timer3`。 +- `flags`:打开设备的标志,支持 `MR_O_RDONLY`、 `MR_O_WRONLY`、 `MR_O_RDWR`。 -注:使用时应根据实际情况为不同的任务分别打开TIMER设备,并使用适当的`oflags`进行管理和权限控制,以确保它们不会相互影响。 +注:使用时应根据实际情况为不同的任务分别打开TIMER设备,并使用适当的`flags`进行管理和权限控制,以确保它们不会相互影响。 ## 关闭TIMER设备 @@ -62,10 +62,10 @@ int mr_dev_ioctl(int desc, int cmd, void *args); | `<0` | 错误码 | - `cmd`:命令码,支持以下命令: - - `MR_CTL_TIMER_SET_MODE`:设置TIMER设备模式。 - - `MR_CTL_TIMER_SET_TIMEOUT_CALL`:设置超时回调函数。 - - `MR_CTL_TIMER_GET_MODE`:获取TIMER设备模式。 - - `MR_CTL_TIMER_GET_TIMEOUT_CALL`:获取超时回调函数。 + - `MR_IOC_TIMER_SET_MODE`:设置TIMER设备模式。 + - `MR_IOC_TIMER_SET_TIMEOUT_CALL`:设置超时回调函数。 + - `MR_IOC_TIMER_GET_MODE`:获取TIMER设备模式。 + - `MR_IOC_TIMER_GET_TIMEOUT_CALL`:获取超时回调函数。 ### 设置/获取TIMER设备配置 @@ -78,11 +78,11 @@ TIMER设备配置: #define TIMER_MODE MR_TIMER_MODE_ONESHOT /* 设置TIMER设备模式 */ -mr_dev_ioctl(ds, MR_CTL_TIMER_SET_MODE, MR_MAKE_LOCAL(int, TIMER_MODE)); +mr_dev_ioctl(ds, MR_IOC_TIMER_SET_MODE, MR_MAKE_LOCAL(int, TIMER_MODE)); /* 获取TIMER设备模式 */ int mode; -mr_dev_ioctl(ds, MR_CTL_TIMER_GET_MODE, &mode); +mr_dev_ioctl(ds, MR_IOC_TIMER_GET_MODE, &mode); ``` 不依赖TIMER接口: @@ -92,11 +92,11 @@ mr_dev_ioctl(ds, MR_CTL_TIMER_GET_MODE, &mode); #define TIMER_MODE 1 /* 设置TIMER设备模式 */ -mr_dev_ioctl(ds, MR_CTL_SET_CONFIG, MR_MAKE_LOCAL(int, TIMER_MODE)); +mr_dev_ioctl(ds, MR_IOC_SCFG, MR_MAKE_LOCAL(int, TIMER_MODE)); /* 获取TIMER设备模式 */ int mode; -mr_dev_ioctl(ds, MR_CTL_GET_CONFIG, &mode); +mr_dev_ioctl(ds, MR_IOC_GCFG, &mode); ``` 注:如未手动配置,默认配置为: @@ -107,42 +107,38 @@ mr_dev_ioctl(ds, MR_CTL_GET_CONFIG, &mode); ```c /* 定义回调函数 */ -int call(int desc, void *args) +void fn(int desc, void *args) { /* 处理中断 */ - - return MR_EOK; } -int (*callback)(int, void *args); +void (*callback)(int desc, void *args); /* 设置超时回调函数 */ -mr_dev_ioctl(ds, MR_CTL_TIMER_SET_TIMEOUT_CALL, &call); +mr_dev_ioctl(ds, MR_IOC_TIMER_SET_TIMEOUT_CALL, &fn); /* 获取超时回调函数 */ -mr_dev_ioctl(ds, MR_CTL_TIMER_GET_TIMEOUT_CALL, &callback); +mr_dev_ioctl(ds, MR_IOC_TIMER_GET_TIMEOUT_CALL, &callback); ``` 不依赖TIMER接口: ```c /* 定义回调函数 */ -int call(int desc, void *args) +void fn(int desc, void *args) { /* 处理中断 */ - - return MR_EOK; } -int (*callback)(int, void *args); +void (*callback)(int desc, void *args); /* 设置超时回调函数 */ -mr_dev_ioctl(ds, MR_CTL_SET_RD_CALL, &call); +mr_dev_ioctl(ds, MR_IOC_SRCB, &fn); /* 获取超时回调函数 */ -mr_dev_ioctl(ds, MR_CTL_GET_RD_CALL, &callback); +mr_dev_ioctl(ds, MR_IOC_GRCB, &callback); ``` ## 读取TIMER设备运行时间 ```c -ssize_t mr_dev_read(int desc, void *buf, size_t size); +ssize_t mr_dev_read(int desc, void *buf, size_t count); ``` | 参数 | 描述 | @@ -173,7 +169,7 @@ if (size < 0) ## 写入TIMER设备定时时间 ```c -ssize_t mr_dev_write(int desc, const void *buf, size_t size); +ssize_t mr_dev_write(int desc, const void *buf, size_t count); ``` | 参数 | 描述 | @@ -195,38 +191,34 @@ ssize_t mr_dev_write(int desc, const void *buf, size_t size); ```c #include "include/mr_lib.h" -int timeout_call(int desc, void *args) +void timeout_call(int desc, void *args) { - /* 注意:请将CONSOLE的打开方式设置成非阻塞模式 */ mr_printf("Timeout\r\n"); - return MR_EOK; } /* 定时时间 */ #define TIMEOUT 500000 -int timer_init(void) +void timer_init(void) { /* 初始化TIMER */ - int timer_ds = mr_dev_open("timer1", MR_OFLAG_RDWR); + int timer_ds = mr_dev_open("timer1", MR_O_RDWR); if (timer_ds < 0) { mr_printf("timer open failed: %s\r\n", mr_strerror(timer_ds)); - return timer_ds; + return; } /* 打印TIMER描述符 */ mr_printf("TIMER desc: %d\r\n", timer_ds); /* 设置超时回调函数 */ - mr_dev_ioctl(timer_ds, MR_CTL_TIMER_SET_TIMEOUT_CALL, timeout_call); + mr_dev_ioctl(timer_ds, MR_IOC_TIMER_SET_TIMEOUT_CALL, timeout_call); /* 设置定时时间 */ uint32_t timeout = TIMEOUT; int ret = mr_dev_write(timer_ds, &timeout, sizeof(timeout)); if(ret < 0) { mr_printf("timer write failed: %s\r\n", mr_strerror(ret)); - return ret; } - return MR_EOK; } /* 导出到自动初始化(APP级) */ MR_INIT_APP_EXPORT(timer_init); diff --git a/document/device/timer/timer_EN.md b/document/device/timer/timer_EN.md index 6bb6eec..bd14652 100644 --- a/document/device/timer/timer_EN.md +++ b/document/device/timer/timer_EN.md @@ -17,22 +17,22 @@ ## Open TIMER Device ```c -int mr_dev_open(const char *name, int oflags); +int mr_dev_open(const char *path, int flags); ``` | Parameter | Description | |------------------|------------------------------| -| name | Device name | -| oflags | Flags for opening the device | +| path | Device path | +| flags | Flags for opening the device | | **Return Value** | | | `>=0` | Device descriptor | | `<0` | Error code | -- `name`: The TIMER device name is usually `timerx`, such as `timer1`, `timer2`, `timer3`. -- `oflags`: Flags for opening the device, supports `MR_OFLAG_RDONLY`, `MR_OFLAG_WRONLY`, `MR_OFLAG_RDWR`. +- `path`: The TIMER device path is usually `timerx`, such as `timer1`, `timer2`, `timer3`. +- `flags`: Flags for opening the device, supports `MR_O_RDONLY`, `MR_O_WRONLY`, `MR_O_RDWR`. Note: When using, the TIMER devices 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 that they will not interfere +the appropriate `flags` should be used for management and permission control to ensure that they will not interfere with each other. ## Close TIMER Device @@ -64,10 +64,10 @@ int mr_dev_ioctl(int desc, int cmd, void *args); | `<0` | Error code | - `cmd`: Command code, supports the following commands: - - `MR_CTL_TIMER_SET_MODE`: Set TIMER device mode. - - `MR_CTL_TIMER_SET_TIMEOUT_CALL`: Set timeout callback function. - - `MR_CTL_TIMER_GET_MODE`: Get TIMER device mode. - - `MR_CTL_TIMER_GET_TIMEOUT_CALL`: Get timeout callback function. + - `MR_IOC_TIMER_SET_MODE`: Set TIMER device mode. + - `MR_IOC_TIMER_SET_TIMEOUT_CALL`: Set timeout callback function. + - `MR_IOC_TIMER_GET_MODE`: Get TIMER device mode. + - `MR_IOC_TIMER_GET_TIMEOUT_CALL`: Get timeout callback function. ### Set/Get TIMER Device Configuration @@ -80,11 +80,11 @@ TIMER device configuration: #define TIMER_MODE MR_TIMER_MODE_ONESHOT /* Set TIMER device mode */ -mr_dev_ioctl(ds, MR_CTL_TIMER_SET_MODE, MR_MAKE_LOCAL(int, TIMER_MODE)); +mr_dev_ioctl(ds, MR_IOC_TIMER_SET_MODE, MR_MAKE_LOCAL(int, TIMER_MODE)); /* Get TIMER device mode */ int mode; -mr_dev_ioctl(ds, MR_CTL_TIMER_GET_MODE, &mode); +mr_dev_ioctl(ds, MR_IOC_TIMER_GET_MODE, &mode); ``` Independent of TIMER interface: @@ -94,11 +94,11 @@ Independent of TIMER interface: #define TIMER_MODE 1 /* Set TIMER device mode */ -mr_dev_ioctl(ds, MR_CTL_SET_CONFIG, MR_MAKE_LOCAL(int, TIMER_MODE)); +mr_dev_ioctl(ds, MR_IOC_SCFG, MR_MAKE_LOCAL(int, TIMER_MODE)); /* Get TIMER device mode */ int mode; -mr_dev_ioctl(ds, MR_CTL_GET_CONFIG, &mode); +mr_dev_ioctl(ds, MR_IOC_GCFG, &mode); ``` Note: The default configuration is: @@ -109,42 +109,38 @@ Note: The default configuration is: ```c /* Define callback function */ -int call(int desc, void *args) +void fn(int desc, void *args) { /* Handle interrupt */ - - return MR_EOK; } -int (*callback)(int, void *args); +void (*callback)(int desc, void *args); /* Set timeout callback function */ -mr_dev_ioctl(ds, MR_CTL_TIMER_SET_TIMEOUT_CALL, &call); +mr_dev_ioctl(ds, MR_IOC_TIMER_SET_TIMEOUT_CALL, &fn); /* Get timeout callback function */ -mr_dev_ioctl(ds, MR_CTL_TIMER_GET_TIMEOUT_CALL, &callback); +mr_dev_ioctl(ds, MR_IOC_TIMER_GET_TIMEOUT_CALL, &callback); ``` Independent of TIMER interface: ```c /* Define callback function */ -int call(int desc, void *args) +void fn(int desc, void *args) { /* Handle interrupt */ - - return MR_EOK; } -int (*callback)(int, void *args); +void (*callback)(int desc, void *args); /* Set timeout callback function */ -mr_dev_ioctl(ds, MR_CTL_SET_RD_CALL, &call); +mr_dev_ioctl(ds, MR_IOC_SRCB, &fn); /* Get timeout callback function */ -mr_dev_ioctl(ds, MR_CTL_GET_RD_CALL, &callback); +mr_dev_ioctl(ds, MR_IOC_GRCB, &callback); ``` ## Read Running Time of TIMER Device ```c -ssize_t mr_dev_read(int desc, void *buf, size_t size); +ssize_t mr_dev_read(int desc, void *buf, size_t count); ``` | Parameter | Description | @@ -175,7 +171,7 @@ Note: ## Write Timing Time to TIMER Device ```c -ssize_t mr_dev_write(int desc, const void *buf, size_t size); +ssize_t mr_dev_write(int desc, const void *buf, size_t count); ``` | Parameter | Description | @@ -198,38 +194,34 @@ Note: ```c #include "include/mr_lib.h" -int timeout_call(int desc, void *args) +void timeout_call(int desc, void *args) { - /* Note: Please set the CONSOLE opening mode to non-blocking */ mr_printf("Timeout\r\n"); - return MR_EOK; } /* Timing time */ #define TIMEOUT 500000 -int timer_init(void) +void timer_init(void) { /* Initialize timer */ - int timer_ds = mr_dev_open("timer1", MR_OFLAG_RDWR); + int timer_ds = mr_dev_open("timer1", MR_O_RDWR); if (timer_ds < 0) { mr_printf("timer open failed: %s\r\n", mr_strerror(timer_ds)); - return timer_ds; + return; } /* Print timer descriptor */ mr_printf("TIMER desc: %d\r\n", timer_ds); /* Set timeout callback function */ - mr_dev_ioctl(timer_ds, MR_CTL_TIMER_SET_TIMEOUT_CALL, timeout_call); + mr_dev_ioctl(timer_ds, MR_IOC_TIMER_SET_TIMEOUT_CALL, timeout_call); /* Set timing time */ uint32_t timeout = TIMEOUT; int ret = mr_dev_write(timer_ds, &timeout, sizeof(timeout)); if(ret < 0) { mr_printf("timer write failed: %s\r\n", mr_strerror(ret)); - return ret; } - return MR_EOK; } /* Export to automatic initialization (APP level) */ MR_INIT_APP_EXPORT(timer_init); diff --git a/include/device/mr_adc.h b/include/device/mr_adc.h index 11f816a..3762e0c 100644 --- a/include/device/mr_adc.h +++ b/include/device/mr_adc.h @@ -17,6 +17,11 @@ extern "C" { #ifdef MR_USING_ADC +/** + * @addtogroup ADC + * @{ + */ + /** * @brief ADC configuration structure. */ @@ -28,11 +33,11 @@ struct mr_adc_config /** * @brief ADC control command. */ -#define MR_CTL_ADC_SET_CHANNEL MR_CTL_SET_OFFSET /**< Set channel */ -#define MR_CTL_ADC_SET_CHANNEL_CONFIG MR_CTL_SET_CONFIG /**< Set channel config */ +#define MR_IOC_ADC_SET_CHANNEL MR_IOC_SPOS /**< Set channel command */ +#define MR_IOC_ADC_SET_CHANNEL_CONFIG MR_IOC_SCFG /**< Set channel configuration command */ -#define MR_CTL_ADC_GET_CHANNEL MR_CTL_GET_OFFSET /**< Get channel */ -#define MR_CTL_ADC_GET_CHANNEL_CONFIG MR_CTL_GET_CONFIG /**< Get channel config */ +#define MR_IOC_ADC_GET_CHANNEL MR_IOC_GPOS /**< Get channel command */ +#define MR_IOC_ADC_GET_CHANNEL_CONFIG MR_IOC_GCFG /**< Get channel configuration command */ /** * @brief ADC data type. @@ -59,11 +64,7 @@ struct mr_adc_ops uint32_t (*read)(struct mr_adc *adc, int channel); }; -/** - * @addtogroup ADC. - * @{ - */ -int mr_adc_register(struct mr_adc *adc, const char *name, struct mr_drv *drv); +int mr_adc_register(struct mr_adc *adc, const char *path, struct mr_drv *drv); /** @} */ #endif /* MR_USING_ADC */ diff --git a/include/device/mr_dac.h b/include/device/mr_dac.h index 9826ebb..053e4b0 100644 --- a/include/device/mr_dac.h +++ b/include/device/mr_dac.h @@ -17,6 +17,11 @@ extern "C" { #ifdef MR_USING_DAC +/** + * @addtogroup DAC + * @{ + */ + /** * @brief DAC configuration structure. */ @@ -28,11 +33,11 @@ struct mr_dac_config /** * @brief DAC control command. */ -#define MR_CTL_DAC_SET_CHANNEL MR_CTL_SET_OFFSET /**< Set channel */ -#define MR_CTL_DAC_SET_CHANNEL_CONFIG MR_CTL_SET_CONFIG /**< Set channel config */ +#define MR_IOC_DAC_SET_CHANNEL MR_IOC_SPOS /**< Set channel command */ +#define MR_IOC_DAC_SET_CHANNEL_CONFIG MR_IOC_SCFG /**< Set channel configuration command */ -#define MR_CTL_DAC_GET_CHANNEL MR_CTL_GET_OFFSET /**< Get channel */ -#define MR_CTL_DAC_GET_CHANNEL_CONFIG MR_CTL_GET_CONFIG /**< Get channel config */ +#define MR_IOC_DAC_GET_CHANNEL MR_IOC_GPOS /**< Get channel command */ +#define MR_IOC_DAC_GET_CHANNEL_CONFIG MR_IOC_GCFG /**< Get channel configuration command */ /** * @brief DAC data type. @@ -59,13 +64,8 @@ struct mr_dac_ops void (*write)(struct mr_dac *dac, int channel, uint32_t data); }; -/** - * @addtogroup DAC. - * @{ - */ -int mr_dac_register(struct mr_dac *dac, const char *name, struct mr_drv *drv); +int mr_dac_register(struct mr_dac *dac, const char *path, struct mr_drv *drv); /** @} */ - #endif /* MR_USING_DAC */ #ifdef __cplusplus diff --git a/include/device/mr_i2c.h b/include/device/mr_i2c.h index fed3f6c..39676da 100644 --- a/include/device/mr_i2c.h +++ b/include/device/mr_i2c.h @@ -17,6 +17,11 @@ extern "C" { #ifdef MR_USING_I2C +/** + * @addtogroup I2C + * @{ + */ + /** * @brief I2C host/slave. */ @@ -53,17 +58,17 @@ struct mr_i2c_config /** * @brief I2C control command. */ -#define MR_CTL_I2C_SET_CONFIG MR_CTL_SET_CONFIG /**< Set configuration */ -#define MR_CTL_I2C_SET_REG MR_CTL_SET_OFFSET /**< Set register */ -#define MR_CTL_I2C_SET_RD_BUFSZ MR_CTL_SET_RD_BUFSZ /**< Set read buffer size */ -#define MR_CTL_I2C_CLR_RD_BUF MR_CTL_CLR_RD_BUF /**< Clear read buffer */ -#define MR_CTL_I2C_SET_RD_CALL MR_CTL_SET_RD_CALL /**< Set read callback */ +#define MR_IOC_I2C_SET_CONFIG MR_IOC_SCFG /**< Set configuration command */ +#define MR_IOC_I2C_SET_REG MR_IOC_SPOS /**< Set register command */ +#define MR_IOC_I2C_SET_RD_BUFSZ MR_IOC_SRBSZ /**< Set read buffer size command */ +#define MR_IOC_I2C_CLR_RD_BUF MR_IOC_CRBD /**< Clear read buffer command */ +#define MR_IOC_I2C_SET_RD_CALL MR_IOC_SRCB /**< Set read callback command */ -#define MR_CTL_I2C_GET_CONFIG MR_CTL_GET_CONFIG /**< Get configuration */ -#define MR_CTL_I2C_GET_REG MR_CTL_GET_OFFSET /**< Get register */ -#define MR_CTL_I2C_GET_RD_BUFSZ MR_CTL_GET_RD_BUFSZ /**< Get read buffer size */ -#define MR_CTL_I2C_GET_RD_DATASZ MR_CTL_GET_RD_DATASZ /**< Get read data size */ -#define MR_CTL_I2C_GET_RD_CALL MR_CTL_GET_RD_CALL /**< Get read callback */ +#define MR_IOC_I2C_GET_CONFIG MR_IOC_GCFG /**< Get configuration command */ +#define MR_IOC_I2C_GET_REG MR_IOC_GPOS /**< Get register command */ +#define MR_IOC_I2C_GET_RD_BUFSZ MR_IOC_GRBSZ /**< Get read buffer size command */ +#define MR_IOC_I2C_GET_RD_DATASZ MR_IOC_GRBDSZ /**< Get read data size command */ +#define MR_IOC_I2C_GET_RD_CALL MR_IOC_GRCB /**< Get read callback command */ /** * @brief I2C data type. @@ -73,7 +78,7 @@ typedef uint8_t mr_i2c_data_t; /**< I2C rea /** * @brief I2C ISR events. */ -#define MR_ISR_I2C_RD_INT (MR_ISR_RD | (0x01 << 8)) /**< Read interrupt */ +#define MR_ISR_I2C_RD_INT (MR_ISR_RD | (0x01)) /**< Read interrupt */ /** * @brief I2C bus structure. @@ -120,12 +125,8 @@ struct mr_i2c_dev uint32_t addr_bits: 16; /**< Address bits */ }; -/** - * @addtogroup I2C. - * @{ - */ -int mr_i2c_bus_register(struct mr_i2c_bus *i2c_bus, const char *name, struct mr_drv *drv); -int mr_i2c_dev_register(struct mr_i2c_dev *i2c_dev, const char *name, int addr, int addr_bits); +int mr_i2c_bus_register(struct mr_i2c_bus *i2c_bus, const char *path, struct mr_drv *drv); +int mr_i2c_dev_register(struct mr_i2c_dev *i2c_dev, const char *path, int addr, int addr_bits); /** @} */ #endif /* MR_USING_I2C */ diff --git a/include/device/mr_pin.h b/include/device/mr_pin.h index 54bb234..0e00775 100644 --- a/include/device/mr_pin.h +++ b/include/device/mr_pin.h @@ -17,24 +17,29 @@ extern "C" { #ifdef MR_USING_PIN +/** + * @addtogroup PIN + * @{ + */ + /** * @brief PIN mode. */ -#define MR_PIN_MODE_NONE (0) /* No mode */ -#define MR_PIN_MODE_OUTPUT (1) /* Output push-pull mode */ -#define MR_PIN_MODE_OUTPUT_OD (2) /* Output open-drain mode */ -#define MR_PIN_MODE_INPUT (3) /* Input mode */ -#define MR_PIN_MODE_INPUT_DOWN (4) /* Input pull-down mode */ -#define MR_PIN_MODE_INPUT_UP (5) /* Input pull-up mode */ +#define MR_PIN_MODE_NONE (0) /**< No mode */ +#define MR_PIN_MODE_OUTPUT (1) /**< Output push-pull mode */ +#define MR_PIN_MODE_OUTPUT_OD (2) /**< Output open-drain mode */ +#define MR_PIN_MODE_INPUT (3) /**< Input mode */ +#define MR_PIN_MODE_INPUT_DOWN (4) /**< Input pull-down mode */ +#define MR_PIN_MODE_INPUT_UP (5) /**< Input pull-up mode */ /** * @brief PIN mode-interrupt. */ -#define MR_PIN_MODE_IRQ_RISING (6) /* Interrupt rising edge */ -#define MR_PIN_MODE_IRQ_FALLING (7) /* Interrupt falling edge */ -#define MR_PIN_MODE_IRQ_EDGE (8) /* Interrupt edge */ -#define MR_PIN_MODE_IRQ_LOW (9) /* Interrupt low level */ -#define MR_PIN_MODE_IRQ_HIGH (10) /* Interrupt high level */ +#define MR_PIN_MODE_IRQ_RISING (6) /**< Interrupt rising edge */ +#define MR_PIN_MODE_IRQ_FALLING (7) /**< Interrupt falling edge */ +#define MR_PIN_MODE_IRQ_EDGE (8) /**< Interrupt edge */ +#define MR_PIN_MODE_IRQ_LOW (9) /**< Interrupt low level */ +#define MR_PIN_MODE_IRQ_HIGH (10) /**< Interrupt high level */ /** * @brief PIN configuration structure. @@ -47,12 +52,12 @@ struct mr_pin_config /** * @brief PIN control command. */ -#define MR_CTL_PIN_SET_NUMBER MR_CTL_SET_OFFSET /**< Set pin number */ -#define MR_CTL_PIN_SET_MODE MR_CTL_SET_CONFIG /**< Set pin mode */ -#define MR_CTL_PIN_SET_EXTI_CALL MR_CTL_SET_RD_CALL /**< Set pin exti callback */ +#define MR_IOC_PIN_SET_NUMBER MR_IOC_SPOS /**< Set pin number command */ +#define MR_IOC_PIN_SET_MODE MR_IOC_SCFG /**< Set pin mode command */ +#define MR_IOC_PIN_SET_EXTI_CALL MR_IOC_SRCB /**< Set pin exti callback command */ -#define MR_CTL_PIN_GET_NUMBER MR_CTL_GET_OFFSET /**< Get pin number */ -#define MR_CTL_PIN_GET_EXTI_CALL MR_CTL_GET_RD_CALL /**< Get pin exti callback */ +#define MR_IOC_PIN_GET_NUMBER MR_IOC_GPOS /**< Get pin number command */ +#define MR_IOC_PIN_GET_EXTI_CALL MR_IOC_GRCB /**< Get pin exti callback command */ /** * @brief PIN data type. @@ -62,7 +67,7 @@ typedef uint8_t mr_pin_data_t; /**< PIN rea /** * @brief PIN ISR events. */ -#define MR_ISR_PIN_EXTI_INT (MR_ISR_RD | (0x01 << 8)) /**< Exti interrupt */ +#define MR_ISR_PIN_EXTI_INT (MR_ISR_RD | (0x01)) /**< Exti interrupt */ /** * @brief PIN structure. @@ -70,8 +75,6 @@ typedef uint8_t mr_pin_data_t; /**< PIN rea struct mr_pin { struct mr_dev dev; /**< Device */ - - struct mr_list irq_list; /**< IRQ list */ }; /** @@ -84,11 +87,7 @@ struct mr_pin_ops void (*write)(struct mr_pin *pin, int number, uint8_t value); }; -/** - * @addtogroup PIN. - * @{ - */ -int mr_pin_register(struct mr_pin *pin, const char *name, struct mr_drv *drv); +int mr_pin_register(struct mr_pin *pin, const char *path, struct mr_drv *drv); /** @} */ #endif /* MR_USING_PIN */ diff --git a/include/device/mr_pwm.h b/include/device/mr_pwm.h index c0a5a7d..6afb43f 100644 --- a/include/device/mr_pwm.h +++ b/include/device/mr_pwm.h @@ -17,6 +17,11 @@ extern "C" { #ifdef MR_USING_PWM +/** + * @addtogroup PWM + * @{ + */ + /** * @brief PWM channel polarity. */ @@ -35,13 +40,13 @@ struct mr_pwm_config /** * @brief PWM control command. */ -#define MR_CTL_PWM_SET_CHANNEL MR_CTL_SET_OFFSET /**< Set channel */ -#define MR_CTL_PWM_SET_CHANNEL_CONFIG MR_CTL_SET_CONFIG /**< Set channel config */ -#define MR_CTL_PWM_SET_FREQ (0x01) /**< Set frequency */ +#define MR_IOC_PWM_SET_CHANNEL MR_IOC_SPOS /**< Set channel command */ +#define MR_IOC_PWM_SET_CHANNEL_CONFIG MR_IOC_SCFG /**< Set channel configuration command */ +#define MR_IOC_PWM_SET_FREQ (0x01) /**< Set frequency command */ -#define MR_CTL_PWM_GET_CHANNEL MR_CTL_GET_OFFSET /**< Get channel */ -#define MR_CTL_PWM_GET_CHANNEL_CONFIG MR_CTL_GET_CONFIG /**< Get channel config */ -#define MR_CTL_PWM_GET_FREQ (-(0x01)) /**< Get frequency */ +#define MR_IOC_PWM_GET_CHANNEL MR_IOC_GPOS /**< Get channel command */ +#define MR_IOC_PWM_GET_CHANNEL_CONFIG MR_IOC_GCFG /**< Get channel configuration command */ +#define MR_IOC_PWM_GET_FREQ (-(0x01)) /**< Get frequency command */ /** * @brief PWM data type. @@ -86,11 +91,7 @@ struct mr_pwm_ops uint32_t (*read)(struct mr_pwm *pwm, int channel); }; -/** - * @addtogroup PWM. - * @{ - */ -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); /** @} */ #endif /* MR_USING_PWM */ diff --git a/include/device/mr_serial.h b/include/device/mr_serial.h index ba8d8a7..d4637a8 100644 --- a/include/device/mr_serial.h +++ b/include/device/mr_serial.h @@ -18,7 +18,12 @@ extern "C" { #ifdef MR_USING_SERIAL /** - * @brief SERIAL data bits. + * @addtogroup Serial + * @{ + */ + +/** + * @brief Serial data bits. */ #define MR_SERIAL_DATA_BITS_5 (5) /**< 5 bits data */ #define MR_SERIAL_DATA_BITS_6 (6) /**< 6 bits data */ @@ -26,7 +31,7 @@ extern "C" { #define MR_SERIAL_DATA_BITS_8 (8) /**< 8 bits data */ /** - * @brief SERIAL stop bits. + * @brief Serial stop bits. */ #define MR_SERIAL_STOP_BITS_1 (1) /**< 1 bit stop */ #define MR_SERIAL_STOP_BITS_2 (2) /**< 2 bit stop */ @@ -34,26 +39,26 @@ extern "C" { #define MR_SERIAL_STOP_BITS_4 (4) /**< 4 bit stop */ /** - * @brief SERIAL parity. + * @brief Serial parity. */ #define MR_SERIAL_PARITY_NONE (0) /**< No parity */ #define MR_SERIAL_PARITY_EVEN (1) /**< Even parity */ #define MR_SERIAL_PARITY_ODD (2) /**< Odd parity */ /** - * @brief SERIAL bit order. + * @brief Serial bit order. */ #define MR_SERIAL_BIT_ORDER_LSB (0) /**< LSB first */ #define MR_SERIAL_BIT_ORDER_MSB (1) /**< MSB first */ /** - * @brief SERIAL polarity. + * @brief Serial polarity. */ #define MR_SERIAL_POLARITY_NORMAL (0) /**< Normal polarity */ #define MR_SERIAL_POLARITY_INVERTED (1) /**< Inverted polarity */ /** - * @brief SERIAL default configuration. + * @brief Serial default configuration. */ #define MR_SERIAL_CONFIG_DEFAULT \ { \ @@ -66,7 +71,7 @@ extern "C" { } /** - * @brief SERIAL configuration structure. + * @brief Serial configuration structure. */ struct mr_serial_config { @@ -79,37 +84,37 @@ struct mr_serial_config }; /** - * @brief SERIAL control command. + * @brief Serial control command. */ -#define MR_CTL_SERIAL_SET_CONFIG MR_CTL_SET_CONFIG /**< Set configuration */ -#define MR_CTL_SERIAL_SET_RD_BUFSZ MR_CTL_SET_RD_BUFSZ /**< Set read buffer size */ -#define MR_CTL_SERIAL_SET_WR_BUFSZ MR_CTL_SET_WR_BUFSZ /**< Set write buffer size */ -#define MR_CTL_SERIAL_CLR_RD_BUF MR_CTL_CLR_RD_BUF /**< Clear read buffer */ -#define MR_CTL_SERIAL_CLR_WR_BUF MR_CTL_CLR_WR_BUF /**< Clear write buffer */ -#define MR_CTL_SERIAL_SET_RD_CALL MR_CTL_SET_RD_CALL /**< Set read callback */ -#define MR_CTL_SERIAL_SET_WR_CALL MR_CTL_SET_WR_CALL /**< Set write complete callback */ +#define MR_IOC_SERIAL_SET_CONFIG MR_IOC_SCFG /**< Set configuration command */ +#define MR_IOC_SERIAL_SET_RD_BUFSZ MR_IOC_SRBSZ /**< Set read buffer size command */ +#define MR_IOC_SERIAL_SET_WR_BUFSZ MR_IOC_SWBSZ /**< Set write buffer size command */ +#define MR_IOC_SERIAL_CLR_RD_BUF MR_IOC_CRBD /**< Clear read buffer command */ +#define MR_IOC_SERIAL_CLR_WR_BUF MR_IOC_CWBD /**< Clear write buffer command */ +#define MR_IOC_SERIAL_SET_RD_CALL MR_IOC_SRCB /**< Set read callback command */ +#define MR_IOC_SERIAL_SET_WR_CALL MR_IOC_SWCB /**< Set write callback command */ -#define MR_CTL_SERIAL_GET_CONFIG MR_CTL_GET_CONFIG /**< Get configuration */ -#define MR_CTL_SERIAL_GET_RD_BUFSZ MR_CTL_GET_RD_BUFSZ /**< Get read buffer size */ -#define MR_CTL_SERIAL_GET_WR_BUFSZ MR_CTL_GET_WR_BUFSZ /**< Get write buffer size */ -#define MR_CTL_SERIAL_GET_RD_DATASZ MR_CTL_GET_RD_DATASZ /**< Get read data size */ -#define MR_CTL_SERIAL_GET_WR_DATASZ MR_CTL_GET_WR_DATASZ /**< Get write data size */ -#define MR_CTL_SERIAL_GET_RD_CALL MR_CTL_GET_RD_CALL /**< Get read callback */ -#define MR_CTL_SERIAL_GET_WR_CALL MR_CTL_GET_WR_CALL /**< Get write complete callback */ +#define MR_IOC_SERIAL_GET_CONFIG MR_IOC_GCFG /**< Get configuration command */ +#define MR_IOC_SERIAL_GET_RD_BUFSZ MR_IOC_GRBSZ /**< Get read buffer size command */ +#define MR_IOC_SERIAL_GET_WR_BUFSZ MR_IOC_GWBSZ /**< Get write buffer size command */ +#define MR_IOC_SERIAL_GET_RD_DATASZ MR_IOC_GRBDSZ /**< Get read data size command */ +#define MR_IOC_SERIAL_GET_WR_DATASZ MR_IOC_GWBDSZ /**< Get write data size command */ +#define MR_IOC_SERIAL_GET_RD_CALL MR_IOC_GRCB /**< Get read callback command */ +#define MR_IOC_SERIAL_GET_WR_CALL MR_IOC_GWCB /**< Get write callback command */ /** - * @brief SERIAL data type. + * @brief Serial data type. */ -typedef uint8_t mr_serial_data_t; /**< SERIAL read/write data type */ +typedef uint8_t mr_serial_data_t; /**< Serial read/write data type */ /** - * @brief SERIAL ISR events. + * @brief Serial ISR events. */ -#define MR_ISR_SERIAL_RD_INT (MR_ISR_RD | (0x01 << 8)) /**< Read interrupt */ -#define MR_ISR_SERIAL_WR_INT (MR_ISR_WR | (0x02 << 8)) /**< Write interrupt */ +#define MR_ISR_SERIAL_RD_INT (MR_ISR_RD | (0x01)) /**< Read interrupt */ +#define MR_ISR_SERIAL_WR_INT (MR_ISR_WR | (0x02)) /**< Write interrupt */ /** - * @brief SERIAL structure. + * @brief Serial structure. */ struct mr_serial { @@ -123,7 +128,7 @@ struct mr_serial }; /** - * @brief SERIAL operations structure. + * @brief Serial operations structure. */ struct mr_serial_ops { @@ -134,11 +139,7 @@ struct mr_serial_ops void (*stop_tx)(struct mr_serial *serial); }; -/** - * @addtogroup SERIAL. - * @{ - */ -int mr_serial_register(struct mr_serial *serial, const char *name, struct mr_drv *drv); +int mr_serial_register(struct mr_serial *serial, const char *path, struct mr_drv *drv); /** @} */ #endif /* MR_USING_SERIAL */ diff --git a/include/device/mr_timer.h b/include/device/mr_timer.h index d98169f..bccc6df 100644 --- a/include/device/mr_timer.h +++ b/include/device/mr_timer.h @@ -17,6 +17,11 @@ extern "C" { #ifdef MR_USING_TIMER +/** + * @addtogroup Timer + * @{ + */ + /** * @brief Timer mode. */ @@ -42,11 +47,11 @@ struct mr_timer_config /** * @brief Timer control command. */ -#define MR_CTL_TIMER_SET_MODE MR_CTL_SET_CONFIG /**< Set timer mode */ -#define MR_CTL_TIMER_SET_TIMEOUT_CALL MR_CTL_SET_RD_CALL /**< Set timeout callback */ +#define MR_IOC_TIMER_SET_MODE MR_IOC_SCFG /**< Set timer mode command */ +#define MR_IOC_TIMER_SET_TIMEOUT_CALL MR_IOC_SRCB /**< Set timeout callback command */ -#define MR_CTL_TIMER_GET_MODE MR_CTL_GET_CONFIG /**< Get timer mode */ -#define MR_CTL_TIMER_GET_TIMEOUT_CALL MR_CTL_GET_RD_CALL /**< Get timeout callback */ +#define MR_IOC_TIMER_GET_MODE MR_IOC_GCFG /**< Get timer mode command */ +#define MR_IOC_TIMER_GET_TIMEOUT_CALL MR_IOC_GRCB /**< Get timeout callback command */ /** * @brief Timer data type. @@ -56,7 +61,7 @@ typedef uint32_t mr_timer_data_t; /**< Timer r /** * @brief Timer ISR events. */ -#define MR_ISR_TIMER_TIMEOUT_INT (MR_ISR_RD | (0x01 << 16)) /**< Timeout interrupt */ +#define MR_ISR_TIMER_TIMEOUT_INT (MR_ISR_RD | (0x01)) /**< Timeout interrupt */ /** * @brief Timer information structure. @@ -96,11 +101,7 @@ struct mr_timer_ops uint32_t (*get_count)(struct mr_timer *timer); }; -/** - * @addtogroup Timer. - * @{ - */ -int mr_timer_register(struct mr_timer *timer, const char *name, struct mr_drv *drv, struct mr_timer_info *info); +int mr_timer_register(struct mr_timer *timer, const char *path, struct mr_drv *drv, struct mr_timer_info *info); /** @} */ #endif /* MR_USING_TIMER */ diff --git a/include/mr_api.h b/include/mr_api.h index da7db72..cb686f6 100644 --- a/include/mr_api.h +++ b/include/mr_api.h @@ -17,24 +17,25 @@ extern "C" { #endif /* __cplusplus */ /** - * @addtogroup Interrupt. + * @addtogroup Interrupt + * @{ */ void mr_interrupt_disable(void); void mr_interrupt_enable(void); /** @} */ /** - * @addtogroup Delay. + * @addtogroup Delay + * @{ */ void mr_delay_us(uint32_t us); void mr_delay_ms(uint32_t ms); /** @} */ /** - * @addtogroup Memory. + * @addtogroup Memory * @{ */ -int mr_heap_init(void); void *mr_malloc(size_t size); void mr_free(void *memory); size_t mr_malloc_usable_size(void *memory); @@ -43,21 +44,23 @@ void *mr_realloc(void *memory, size_t size); /** @} */ /** - * @addtogroup Printf. + * @addtogroup String * @{ */ int mr_printf(const char *fmt, ...); const char *mr_strerror(int err); +const char *mr_strflags(int flags); /** @} */ /** - * @addtogroup Initialization. + * @addtogroup Auto-init + * @{ */ void mr_auto_init(void); /** @} */ /** - * @addtogroup Ringbuffer. + * @addtogroup Ringbuffer * @{ */ void mr_ringbuf_init(struct mr_ringbuf *ringbuf, void *pool, size_t size); @@ -76,7 +79,7 @@ size_t mr_ringbuf_write_force(struct mr_ringbuf *ringbuf, const void *buffer, si /** @} */ /** - * @addtogroup AVL tree. + * @addtogroup AVL-tree * @{ */ void mr_avl_init(struct mr_avl *node, uint32_t value); @@ -87,28 +90,21 @@ size_t mr_avl_get_length(struct mr_avl *tree); /** @} */ /** -* @addtogroup Device. -* @{ -*/ + * @addtogroup Device + * @{ + */ int mr_dev_register(struct mr_dev *dev, const char *path, int type, - int sflags, + int flags, struct mr_dev_ops *ops, struct mr_drv *drv); int mr_dev_isr(struct mr_dev *dev, int event, void *args); -/** @} */ - -/** - * @addtogroup Device description. - * @{ - */ -int mr_dev_open(const char *path, int oflags); +int mr_dev_open(const char *path, int flags); int mr_dev_close(int desc); -ssize_t mr_dev_read(int desc, void *buf, size_t size); -ssize_t mr_dev_write(int desc, const void *buf, size_t size); +ssize_t mr_dev_read(int desc, void *buf, size_t count); +ssize_t mr_dev_write(int desc, const void *buf, size_t count); int mr_dev_ioctl(int desc, int cmd, void *args); -int mr_dev_is_valid(int desc); /** @} */ #ifdef __cplusplus diff --git a/include/mr_service.h b/include/mr_service.h index 057e9af..5acebf4 100644 --- a/include/mr_service.h +++ b/include/mr_service.h @@ -15,6 +15,11 @@ extern "C" { #endif /* __cplusplus */ +/** + * @addtogroup Basic + * @{ + */ + /** * @brief This macro function concatenates two strings. * @@ -47,18 +52,24 @@ extern "C" { ((type *)((char *)(pointer) - (unsigned long)(&((type *)0)->member))) /** - * @brief This macro function aligns the size up to a multiple of 4. + * @brief This macro function aligns a value upwards. * - * @param size The size to align. + * @param value The value to align. + * @param align The alignment. + * + * @return The aligned value. */ -#define MR_ALIGN4_UP(size) (((size) + 3) & (~3)) +#define MR_ALIGN_UP(value, align) (((value) + (align) - 1) & ~((align) - 1)) /** - * @brief This macro function aligns a size down to a multiple of 4. + * @brief This macro function aligns a value downwards. * - * @param size The size to align. + * @param value The value to align. + * @param align The alignment. + * + * @return The aligned value. */ -#define MR_ALIGN4_DOWN(size) ((size) & (~3)) +#define MR_ALIGN_DOWN(value, align) ((value) & ~((align) - 1)) /** * @brief This macro function checks if a value is set. @@ -66,7 +77,7 @@ extern "C" { * @param value The value to check. * @param mask The mask to check. */ -#define MR_BIT_IS_SET(value, mask) (((value) & (mask)) == (mask)) +#define MR_BIT_IS_SET(value, mask) (((value) & (mask)) == (mask)) /** * @brief This macro function sets a value. @@ -74,7 +85,7 @@ extern "C" { * @param value The value to set. * @param mask The mask to set. */ -#define MR_BIT_SET(value, mask) ((value) |= (mask)) +#define MR_BIT_SET(value, mask) ((value) |= (mask)) /** * @brief This macro function clears a value. @@ -82,13 +93,13 @@ extern "C" { * @param value The value to clear. * @param mask The mask to clear. */ -#define MR_BIT_CLR(value, mask) ((value) &= ~(mask)) +#define MR_BIT_CLR(value, mask) ((value) &= ~(mask)) /** * @brief This macro function creates a local variable. * * @param type The type of the variable. - * @param value The value of the variable. + * @param ... The arguments. * * @return A pointer to the variable. * @@ -163,16 +174,22 @@ extern "C" { * @return The boolean value. */ #define MR_TO_BOOL(value) (!!(value)) +/** @} */ + +/** + * @addtogroup Log + * @{ + */ /** * @brief Log message with color. */ #ifdef MR_USING_LOG_COLOR -#define MR_LOG_COLOR_RED(str, fmt) "\033[31m"str, fmt"\033[0m" -#define MR_LOG_COLOR_YELLOW(str, fmt) "\033[33m"str, fmt"\033[0m" -#define MR_LOG_COLOR_BLUE(str, fmt) "\033[34m"str, fmt"\033[0m" -#define MR_LOG_COLOR_PURPLE(str, fmt) "\033[35m"str, fmt"\033[0m" -#define MR_LOG_COLOR_GREEN(str, fmt) "\033[32m"str, fmt"\033[0m" +#define MR_LOG_COLOR_RED(str, fmt) "\033[31m"str, fmt"\033[0m" /**< Log red color */ +#define MR_LOG_COLOR_YELLOW(str, fmt) "\033[33m"str, fmt"\033[0m" /**< Log yellow color */ +#define MR_LOG_COLOR_BLUE(str, fmt) "\033[34m"str, fmt"\033[0m" /**< Log blue color */ +#define MR_LOG_COLOR_PURPLE(str, fmt) "\033[35m"str, fmt"\033[0m" /**< Log purple color */ +#define MR_LOG_COLOR_GREEN(str, fmt) "\033[32m"str, fmt"\033[0m" /**< Log green color */ #else #define MR_LOG_COLOR_RED(str, fmt) str, fmt #define MR_LOG_COLOR_YELLOW(str, fmt) str, fmt @@ -181,34 +198,42 @@ extern "C" { #define MR_LOG_COLOR_GREEN(str, fmt) str, fmt #endif /* MR_USING_LOG_COLOR */ -/** - * @brief This macro function logs a error-warning-debug-info message. - */ #ifdef MR_USING_LOG_ERROR +/* Print error message */ #define MR_LOG_ERROR(fmt, ...) mr_printf("%-8s %s\r\n", MR_LOG_COLOR_RED("ERROR:", fmt), ##__VA_ARGS__) #else #define MR_LOG_ERROR(fmt, ...) #endif /* MR_USING_LOG_ERROR */ #ifdef MR_USING_LOG_WARN +/* Print warning message */ #define MR_LOG_WARN(fmt, ...) mr_printf("%-8s %s\r\n", MR_LOG_COLOR_YELLOW("WARNING:", fmt), ##__VA_ARGS__) #else #define MR_LOG_WARN(fmt, ...) #endif /* MR_USING_LOG_WARN */ #ifdef MR_USING_LOG_INFO +/* Print information message */ #define MR_LOG_INFO(fmt, ...) mr_printf("%-8s %s\r\n", MR_LOG_COLOR_BLUE("INFO:", fmt), ##__VA_ARGS__) #else #define MR_LOG_INFO(fmt, ...) #endif /* MR_USING_LOG_INFO */ #ifdef MR_USING_LOG_DEBUG +/* Print debug message */ #define MR_LOG_DEBUG(fmt, ...) mr_printf("%-8s %s\r\n", MR_LOG_COLOR_PURPLE("DEBUG:", fmt), ##__VA_ARGS__) #else #define MR_LOG_DEBUG(fmt, ...) #endif /* MR_USING_LOG_DEBUG */ #ifdef MR_USING_LOG_SUCCESS +/* Print success message */ #define MR_LOG_SUCCESS(fmt, ...) mr_printf("%-8s %s\r\n", MR_LOG_COLOR_GREEN("SUCCESS:", fmt), ##__VA_ARGS__) #else #define MR_LOG_SUCCESS(fmt, ...) #endif /* MR_USING_LOG_SUCCESS */ +/** @} */ + +/** + * @addtogroup Assert + * @{ + */ /** * @brief This macro function asserts a condition. @@ -237,6 +262,12 @@ extern "C" { #else #define MR_ASSERT(ex) #endif /* MR_USING_ASSERT */ +/** @} */ + +/** + * @addtogroup List + * @{ + */ /** * @brief This macro function initializes a list. @@ -315,19 +346,19 @@ MR_INLINE void mr_list_remove(struct mr_list *node) * * @return The length of the list. */ -MR_INLINE size_t mr_list_get_length(struct mr_list *list) +MR_INLINE size_t mr_list_get_len(struct mr_list *list) { - size_t length = 0; struct mr_list *node = list; + size_t len = 0; while (node->next != list) { node = node->next; - length++; + len++; } - - return length; + return len; } +/** @} */ #ifdef __cplusplus } diff --git a/source/service.c b/source/service.c index 90777b4..ef2eb7e 100644 --- a/source/service.c +++ b/source/service.c @@ -8,27 +8,25 @@ #include "include/mr_api.h" -static int start(void) +static void start(void) { - return 0; -} -_MR_INIT_EXPORT(start, "0"); -static int end(void) -{ - return 0; } -_MR_INIT_EXPORT(end, "5.end"); +MR_INIT_EXPORT(start, "0"); + +static void end(void) +{ + +} +MR_INIT_EXPORT(end, "5.end"); /** * @brief This function is auto initialized. - * - * @return MR_ERR_OK on success, otherwise an error code. */ void mr_auto_init(void) { /* Auto-initialization */ - for (volatile const mr_init_fn_t *fn = &_mr_auto_init_start; fn < &_mr_auto_init_end; fn++) + for (const mr_init_fn_t *fn = &_mr_auto_init_start; fn < &_mr_auto_init_end; fn++) { (*fn)(); } @@ -98,21 +96,25 @@ MR_WEAK int mr_printf_output(const char *buf, size_t size) { static int desc = -1; - if (mr_dev_is_valid(desc) == MR_FALSE) + /* Try to open the serial port */ + if (desc == -1) { #ifndef MR_CFG_PRINTF_DEV_NAME #define MR_CFG_PRINTF_DEV_NAME "serial1" #endif /* MR_CFG_PRINTF_DEV_NAME */ #ifndef MR_USING_PRINTF_NONBLOCKING - desc = mr_dev_open(MR_CFG_PRINTF_DEV_NAME, MR_OFLAG_RDWR); + int ret = mr_dev_open(MR_CFG_PRINTF_DEV_NAME, MR_O_RDWR); #else - desc = mr_dev_open(MR_CFG_PRINTF_DEV_NAME, MR_OFLAG_RDWR | MR_OFLAG_NONBLOCK); + int ret = mr_dev_open(MR_CFG_PRINTF_DEV_NAME, MR_O_RDWR | MR_O_NONBLOCK); #endif /* MR_USING_PRINTF_NONBLOCKING */ - if (mr_dev_is_valid(desc) == MR_FALSE) + if (ret < 0) { - return desc; + return ret; } + desc = ret; } + + /* Write data to the serial port */ return (int)mr_dev_write(desc, buf, size); } @@ -132,19 +134,21 @@ int mr_printf(const char *fmt, ...) char buf[MR_CFG_PRINTF_BUFSZ] = {0}; va_list args; + /* Format the string */ va_start(args, fmt); int ret = vsnprintf(buf, sizeof(buf) - 1, fmt, args); - ret = mr_printf_output(buf, ret); va_end(args); - return ret; + + /* Output the string */ + return mr_printf_output(buf, ret); } /** - * @brief This function get the error message. + * @brief This function get the error string representation. * * @param err The error code. * - * @return The error message. + * @return A pointer to the error string. */ const char *mr_strerror(int err) { @@ -171,6 +175,35 @@ const char *mr_strerror(int err) } } +/** + * @brief This function get the flags string representation. + * + * @param flags The flags code. + * + * @return A pointer to the flags string. + * + * @note This function returns a pointer to a static string and overwrites the buffer each time it is called. + */ +const char *mr_strflags(int flags) +{ + static char str[6] = {0}; + + strcpy(str, "-----"); + if (MR_BIT_IS_SET(flags, MR_O_RDONLY)) + { + str[1] = 'r'; + } + if (MR_BIT_IS_SET(flags, MR_O_WRONLY)) + { + str[2] = 'w'; + } + if (MR_BIT_IS_SET(flags, MR_O_NONBLOCK)) + { + str[3] = 'n'; + } + return str; +} + /** * @brief This function initialize the ringbuffer. * @@ -187,7 +220,6 @@ void mr_ringbuf_init(struct mr_ringbuf *ringbuf, void *pool, size_t size) ringbuf->write_index = 0; ringbuf->read_mirror = 0; ringbuf->write_mirror = 0; - ringbuf->size = size; ringbuf->buffer = pool; } @@ -204,8 +236,7 @@ int mr_ringbuf_allocate(struct mr_ringbuf *ringbuf, size_t size) { MR_ASSERT(ringbuf != MR_NULL); - /* Check the buffer size */ - if (mr_ringbuf_get_bufsz(ringbuf) == size) + if (size == mr_ringbuf_get_bufsz(ringbuf)) { mr_ringbuf_reset(ringbuf); return MR_EOK; @@ -215,6 +246,7 @@ int mr_ringbuf_allocate(struct mr_ringbuf *ringbuf, size_t size) if (ringbuf->size != 0) { mr_free(ringbuf->buffer); + mr_ringbuf_init(ringbuf, MR_NULL, 0); } /* Allocate new buffer */ @@ -561,7 +593,6 @@ size_t mr_ringbuf_write_force(struct mr_ringbuf *ringbuf, const void *buffer, si { ringbuf->read_index = ringbuf->write_index; } - return size; }