优化:

1.write和read的错误码回传机制,改成log提示。
2.spi的cs引脚信息改为pin引脚编号。
新增:
1.接管pin框架中断。
2.驱动框架对log的支持。
This commit is contained in:
MacRsh
2023-04-30 00:51:05 +08:00
parent 2b949ba25e
commit 21ad63369e
13 changed files with 297 additions and 128 deletions

View File

@@ -12,6 +12,9 @@
#if (MR_DEVICE_ADC == MR_CONF_ENABLE)
#undef LOG_TAG
#define LOG_TAG "adc"
static mr_err_t mr_adc_open(mr_device_t device)
{
mr_adc_t adc = (mr_adc_t)device;
@@ -31,15 +34,24 @@ static mr_err_t mr_adc_ioctl(mr_device_t device, int cmd, void *args)
mr_adc_t adc = (mr_adc_t)device;
mr_err_t ret = MR_ERR_OK;
switch (cmd & _MR_CMD_MASK)
switch (cmd & _MR_CMD_MASK1)
{
case MR_CMD_CONFIG:
case MR_CMD_SET:
{
if (args)
ret = adc->ops->channel_configure(adc,
((struct mr_adc_config *)args)->channel,
((struct mr_adc_config *)args)->state);
switch (cmd & _MR_CMD_MASK2)
{
case MR_CMD_CONFIG:
{
if (args)
ret = adc->ops->channel_configure(adc,
((struct mr_adc_config *)args)->channel,
((struct mr_adc_config *)args)->state);
break;
}
default: ret = - MR_ERR_UNSUPPORTED;
}
break;
}
@@ -55,8 +67,11 @@ static mr_size_t mr_adc_read(mr_device_t device, mr_off_t pos, void *buffer, mr_
mr_uint16_t *recv_buffer = (mr_uint16_t *)buffer;
mr_size_t recv_size = size;
if (size < sizeof(mr_uint16_t))
if (size != sizeof(mr_uint16_t))
{
MR_LOG_D(LOG_TAG, "Device %s: Invalid read size %d\r\n", device->object.name, size);
return 0;
}
while (recv_size -= sizeof(mr_uint16_t))
{

View File

@@ -12,6 +12,9 @@
#if (MR_DEVICE_DAC == MR_CONF_ENABLE)
#undef LOG_TAG
#define LOG_TAG "dac"
static mr_err_t mr_dac_open(mr_device_t device)
{
mr_dac_t dac = (mr_dac_t)device;
@@ -31,15 +34,24 @@ static mr_err_t mr_dac_ioctl(mr_device_t device, int cmd, void *args)
mr_dac_t dac = (mr_dac_t)device;
mr_err_t ret = MR_ERR_OK;
switch (cmd & _MR_CMD_MASK)
switch (cmd & _MR_CMD_MASK1)
{
case MR_CMD_CONFIG:
case MR_CMD_SET:
{
if (args)
ret = dac->ops->channel_configure(dac,
((struct mr_dac_config *)args)->channel,
((struct mr_dac_config *)args)->state);
switch (cmd & _MR_CMD_MASK2)
{
case MR_CMD_CONFIG:
{
if (args)
ret = dac->ops->channel_configure(dac,
((struct mr_dac_config *)args)->channel,
((struct mr_dac_config *)args)->state);
break;
}
default: ret = - MR_ERR_UNSUPPORTED;
}
break;
}
@@ -55,8 +67,11 @@ static mr_size_t mr_dac_write(mr_device_t device, mr_off_t pos, const void *buff
mr_uint16_t *send_buffer = (mr_uint16_t *)buffer;
mr_size_t send_size = size;
if (size < sizeof(mr_uint16_t))
if (size != sizeof(mr_uint16_t))
{
MR_LOG_D(LOG_TAG, "Device %s: Invalid write size %d\r\n", device->object.name, size);
return 0;
}
while (send_size -= sizeof(mr_uint16_t))
{

View File

@@ -12,6 +12,9 @@
#if (MR_DEVICE_I2C == MR_CONF_ENABLE)
#undef LOG_TAG
#define LOG_TAG "i2c"
static mr_err_t mr_take_i2c_bus(mr_i2c_device_t i2c_device)
{
mr_err_t ret = MR_ERR_OK;
@@ -103,30 +106,39 @@ static mr_err_t mr_i2c_device_ioctl(mr_device_t device, int cmd, void *args)
mr_i2c_device_t i2c_device = (mr_i2c_device_t)device;
mr_err_t ret = MR_ERR_OK;
switch (cmd & _MR_CMD_MASK)
switch (cmd & _MR_CMD_MASK1)
{
case MR_CMD_CONFIG:
case MR_CMD_SET:
{
if (args)
i2c_device->config = *(struct mr_i2c_config *)args;
break;
}
case MR_CMD_ATTACH:
{
if (args)
switch (cmd & _MR_CMD_MASK2)
{
/* Find in the kernel i2c-bus */
mr_device_t i2c_bus = mr_device_find((char *)args);
if (i2c_bus == MR_NULL || i2c_bus->type != MR_DEVICE_TYPE_I2C_BUS)
case MR_CMD_CONFIG:
{
ret = - MR_ERR_NOT_FOUND;
if (args)
i2c_device->config = *(struct mr_i2c_config *)args;
break;
}
/* Open i2c-bus */
mr_device_open(i2c_bus, MR_OPEN_RDWR);
i2c_device->bus = (mr_i2c_bus_t)i2c_bus;
case MR_CMD_ATTACH:
{
if (args)
{
/* Find the i2c-bus */
mr_device_t i2c_bus = mr_device_find((char *)args);
if (i2c_bus == MR_NULL || i2c_bus->type != MR_DEVICE_TYPE_I2C_BUS)
{
ret = - MR_ERR_NOT_FOUND;
break;
}
/* Open the i2c-bus */
mr_device_open(i2c_bus, MR_OPEN_RDWR);
i2c_device->bus = (mr_i2c_bus_t)i2c_bus;
}
break;
}
default: ret = - MR_ERR_UNSUPPORTED;
}
break;
}
@@ -147,7 +159,10 @@ static mr_size_t mr_i2c_device_read(mr_device_t device, mr_off_t pos, void *buff
/* Take i2c-bus */
ret = mr_take_i2c_bus(i2c_device);
if (ret != MR_ERR_OK)
{
MR_LOG_D("Device %s: Failed to take i2c-bus\r\n", device->object.name);
return 0;
}
/* Send the i2c start signal and read cmd */
i2c_device->bus->ops->start(i2c_device->bus);
@@ -182,7 +197,10 @@ static mr_size_t mr_i2c_device_write(mr_device_t device, mr_off_t pos, const voi
/* Take i2c-bus */
ret = mr_take_i2c_bus(i2c_device);
if (ret != MR_ERR_OK)
{
MR_LOG_D("Device %s: Failed to take i2c-bus\r\n", device->object.name);
return 0;
}
/* Send the i2c start signal and write cmd */
i2c_device->bus->ops->start(i2c_device->bus);

View File

@@ -24,9 +24,9 @@
/* Default config for mr_i2c_config structure */
#define MR_I2C_CONFIG_DEFAULT \
{ \
3000000, \
MR_I2C_HOST, \
MR_I2C_ADDRESS_MODE_7 \
3000000, \
MR_I2C_HOST, \
MR_I2C_ADDRESS_MODE_7 \
}
struct mr_i2c_config

View File

@@ -12,24 +12,43 @@
#if (MR_DEVICE_PIN == MR_CONF_ENABLE)
#undef LOG_TAG
#define LOG_TAG "pin"
static mr_uint32_t mr_pin_irq_mask = 0;
static mr_err_t mr_pin_ioctl(mr_device_t device, int cmd, void *args)
{
mr_pin_t pin = (mr_pin_t)device;
mr_err_t ret = MR_ERR_OK;
switch (cmd & _MR_CMD_MASK)
switch (cmd & _MR_CMD_MASK1)
{
case MR_CMD_CONFIG:
case MR_CMD_SET:
{
if (args)
ret = pin->ops->configure(pin,
((struct mr_pin_config *)args)->number,
((struct mr_pin_config *)args)->mode);
switch (cmd & _MR_CMD_MASK2)
{
case MR_CMD_CONFIG:
{
if (args)
ret = pin->ops->configure(pin, args);
if (((struct mr_pin_config *)args)->irq_mode != MR_PIN_IRQ_MODE_NONE && ret == MR_ERR_OK)
mr_pin_irq_mask |= (1 << ((struct mr_pin_config *)args)->number % 16);
break;
}
case MR_CMD_RX_CB:
{
device->rx_cb = args;
break;
}
default: return - MR_ERR_UNSUPPORTED;
}
break;
}
default: ret = - MR_ERR_UNSUPPORTED;
default: return - MR_ERR_UNSUPPORTED;
}
return ret;
@@ -41,7 +60,10 @@ static mr_size_t mr_pin_read(mr_device_t device, mr_off_t pos, void *buffer, mr_
mr_uint8_t *recv_buffer = (mr_uint8_t *)buffer;
if (size != sizeof(mr_uint8_t))
{
MR_LOG_D(LOG_TAG, "Device %s: Invalid read size %d\r\n", device->object.name, size);
return 0;
}
*recv_buffer = pin->ops->read(pin, (mr_uint16_t)pos);
@@ -54,14 +76,17 @@ static mr_size_t mr_pin_write(mr_device_t device, mr_off_t pos, const void *buff
mr_uint8_t *send_buffer = (mr_uint8_t *)buffer;
if (size != sizeof(mr_uint8_t))
{
MR_LOG_D(LOG_TAG, "Device %s: Invalid write size %d\r\n", device->object.name, size);
return 0;
}
pin->ops->write(pin, (mr_uint16_t)pos, *send_buffer);
return size;
}
static mr_err_t _err_io_pin_configure(mr_pin_t pin, mr_uint16_t number, mr_uint16_t mode)
static mr_err_t _err_io_pin_configure(mr_pin_t pin, struct mr_pin_config *config)
{
MR_ASSERT(0);
return - MR_ERR_IO;
@@ -107,4 +132,16 @@ mr_err_t mr_hw_pin_add(mr_pin_t pin, const char *name, struct mr_pin_ops *ops, v
return MR_ERR_OK;
}
void mr_hw_pin_isr(mr_pin_t pin, mr_uint32_t Line)
{
if (mr_pin_irq_mask & (1 << Line))
{
/* Invoke the rx-cb function */
if (pin->device.rx_cb != MR_NULL)
{
pin->device.rx_cb(&pin->device, &Line);
}
}
}
#endif

View File

@@ -15,22 +15,31 @@
#if (MR_DEVICE_PIN == MR_CONF_ENABLE)
#define MR_PIN_MODE_OUT 0
#define MR_PIN_MODE_OUT_OD 1
#define MR_PIN_MODE_IN 2
#define MR_PIN_MODE_IN_UP 3
#define MR_PIN_MODE_IN_DOWN 4
#define MR_PIN_MODE_NONE 0
#define MR_PIN_MODE_OUT 1
#define MR_PIN_MODE_OUT_OD 2
#define MR_PIN_MODE_IN 3
#define MR_PIN_MODE_IN_UP 4
#define MR_PIN_MODE_IN_DOWN 5
#define MR_PIN_IRQ_MODE_NONE 0
#define MR_PIN_IRQ_MODE_RISING 1
#define MR_PIN_IRQ_MODE_FALLING 2
#define MR_PIN_IRQ_MODE_EDGE 3
#define MR_PIN_IRQ_MODE_HIGH 4
#define MR_PIN_IRQ_MODE_LOW 5
struct mr_pin_config
{
mr_uint16_t number;
mr_uint16_t mode;
mr_uint8_t mode;
mr_uint8_t irq_mode;
};
typedef struct mr_pin *mr_pin_t;
struct mr_pin_ops
{
mr_err_t (*configure)(mr_pin_t pin, mr_uint16_t number, mr_uint16_t mode);
mr_err_t (*configure)(mr_pin_t pin, struct mr_pin_config *config);
void (*write)(mr_pin_t pin, mr_uint16_t number, mr_uint8_t value);
mr_uint8_t (*read)(mr_pin_t pin, mr_uint16_t number);
};
@@ -43,6 +52,7 @@ struct mr_pin
};
mr_err_t mr_hw_pin_add(mr_pin_t pin, const char *name, struct mr_pin_ops *ops, void *data);
void mr_hw_pin_isr(mr_pin_t pin, mr_uint32_t Line);
#endif

View File

@@ -12,6 +12,9 @@
#if (MR_DEVICE_SERIAL == MR_CONF_ENABLE)
#undef LOG_TAG
#define LOG_TAG "serial"
static mr_err_t mr_serial_open(mr_device_t device)
{
mr_serial_t serial = (mr_serial_t)device;
@@ -77,30 +80,38 @@ static mr_err_t mr_serial_ioctl(mr_device_t device, int cmd, void *args)
mr_serial_t serial = (mr_serial_t)device;
mr_err_t ret = MR_ERR_OK;
switch (cmd & _MR_CMD_MASK)
switch (cmd & _MR_CMD_MASK1)
{
case MR_CMD_CONFIG:
case MR_CMD_SET:
{
if (args)
switch (cmd & _MR_CMD_MASK2)
{
ret = serial->ops->configure(serial, (struct mr_serial_config *)args);
if (ret == MR_ERR_OK)
serial->config = *(struct mr_serial_config *)args;
case MR_CMD_CONFIG:
{
if (args)
{
ret = serial->ops->configure(serial, (struct mr_serial_config *)args);
if (ret == MR_ERR_OK)
serial->config = *(struct mr_serial_config *)args;
}
break;
}
case MR_CMD_RX_CB:
{
device->rx_cb = args;
break;
}
case MR_CMD_TX_CB:
{
device->tx_cb = args;
break;
}
default: ret = - MR_ERR_UNSUPPORTED;
}
break;
}
case MR_CMD_SET_RX_CALLBACK:
{
if (args)
device->rx_cb = (mr_err_t (*)(mr_device_t device, void *args))args;
break;
}
case MR_CMD_SET_TX_CALLBACK:
{
if (args)
device->tx_cb = (mr_err_t (*)(mr_device_t device, void *args))args;
break;
}
@@ -209,7 +220,7 @@ mr_err_t mr_hw_serial_add(mr_serial_t serial, const char *name, struct mr_serial
return MR_ERR_OK;
}
void mr_hw_serial_isr(mr_serial_t serial, mr_uint16_t event)
void mr_hw_serial_isr(mr_serial_t serial, mr_uint32_t event)
{
switch (event & _MR_SERIAL_EVENT_MASK)
{

View File

@@ -98,7 +98,7 @@ struct mr_serial
};
mr_err_t mr_hw_serial_add(mr_serial_t serial, const char *name, struct mr_serial_ops *ops, void *data);
void mr_hw_serial_isr(mr_serial_t serial, mr_uint16_t event);
void mr_hw_serial_isr(mr_serial_t serial, mr_uint32_t event);
#endif

View File

@@ -112,30 +112,39 @@ static mr_err_t mr_spi_device_ioctl(mr_device_t device, int cmd, void *args)
mr_spi_device_t spi_device = (mr_spi_device_t)device;
mr_err_t ret = MR_ERR_OK;
switch (cmd & _MR_CMD_MASK)
switch (cmd & _MR_CMD_MASK1)
{
case MR_CMD_CONFIG:
case MR_CMD_SET:
{
if (args)
spi_device->config = *(struct mr_spi_config *)args;
break;
}
case MR_CMD_ATTACH:
{
if (args)
switch (cmd & _MR_CMD_MASK2)
{
/* Find in the kernel spi-bus */
mr_device_t spi_bus = mr_device_find((char *)args);
if (spi_bus == MR_NULL || spi_bus->type != MR_DEVICE_TYPE_SPI_BUS)
case MR_CMD_CONFIG:
{
ret = - MR_ERR_NOT_FOUND;
if (args)
spi_device->config = *(struct mr_spi_config *)args;
break;
}
/* Open spi-bus */
mr_device_open(spi_bus, MR_OPEN_RDWR);
spi_device->bus = (mr_spi_bus_t)spi_bus;
case MR_CMD_ATTACH:
{
if (args)
{
/* Find the spi-bus */
mr_device_t spi_bus = mr_device_find((char *)args);
if (spi_bus == MR_NULL || spi_bus->type != MR_DEVICE_TYPE_SPI_BUS)
{
ret = - MR_ERR_NOT_FOUND;
break;
}
/* Open the spi-bus */
mr_device_open(spi_bus, MR_OPEN_RDWR);
spi_device->bus = (mr_spi_bus_t)spi_bus;
}
break;
}
default: ret = - MR_ERR_UNSUPPORTED;
}
break;
}
@@ -155,7 +164,10 @@ static mr_size_t mr_spi_device_read(mr_device_t device, mr_off_t pos, void *buff
/* Take spi-bus */
ret = mr_take_spi_bus(spi_device);
if (ret != MR_ERR_OK)
{
MR_LOG_D("Device %s: Failed to take spi-bus\r\n", device->object.name);
return 0;
}
switch (spi_device->config.data_bits)
{
@@ -213,7 +225,10 @@ static mr_size_t mr_spi_device_write(mr_device_t device, mr_off_t pos, const voi
/* Take spi-bus */
ret = mr_take_spi_bus(spi_device);
if (ret != MR_ERR_OK)
{
MR_LOG_D("Device %s: Failed to take spi-bus\r\n", device->object.name);
return 0;
}
switch (spi_device->config.data_bits)
{
@@ -316,7 +331,7 @@ mr_err_t mr_hw_spi_bus_add(mr_spi_bus_t spi_bus, const char *name, struct mr_spi
mr_err_t mr_hw_spi_device_add(mr_spi_device_t spi_device,
const char *name,
mr_uint16_t support_flag,
void *cs_data)
mr_uint16_t cs_pin)
{
mr_err_t ret = MR_ERR_OK;
const static struct mr_device_ops device_ops =
@@ -332,13 +347,14 @@ mr_err_t mr_hw_spi_device_add(mr_spi_device_t spi_device,
MR_ASSERT(support_flag != MR_NULL);
/* Add the spi-device to the container */
ret = mr_device_add(&spi_device->device, name, MR_DEVICE_TYPE_SPI, support_flag, &device_ops, cs_data);
ret = mr_device_add(&spi_device->device, name, MR_DEVICE_TYPE_SPI, support_flag, &device_ops, MR_NULL);
if (ret != MR_ERR_OK)
return ret;
/* Initialize the spi-device fields */
spi_device->config.baud_rate = 0;
spi_device->bus = MR_NULL;
spi_device->cs_pin = cs_pin;
return MR_ERR_OK;
}

View File

@@ -32,16 +32,17 @@
#define MR_SPI_CS_ACTIVE_LOW 0
#define MR_SPI_CS_ACTIVE_HIGH 1
#define MR_SPI_CS_ACTIVE_NONE 2
/* Default config for mr_spi_config structure */
#define MR_SPI_CONFIG_DEFAULT \
{ \
3000000, \
MR_SPI_HOST, \
MR_SPI_MODE_0, \
MR_SPI_DATA_BITS_8, \
MR_SPI_BIT_ORDER_MSB, \
MR_SPI_CS_ACTIVE_LOW, \
3000000, \
MR_SPI_HOST, \
MR_SPI_MODE_0, \
MR_SPI_DATA_BITS_8, \
MR_SPI_BIT_ORDER_MSB, \
MR_SPI_CS_ACTIVE_LOW, \
}
struct mr_spi_config
@@ -62,6 +63,7 @@ struct mr_spi_device
struct mr_spi_config config;
struct mr_spi_bus *bus;
mr_uint16_t cs_pin;
};
typedef struct mr_spi_device *mr_spi_device_t;
@@ -87,7 +89,7 @@ mr_err_t mr_hw_spi_bus_add(mr_spi_bus_t spi_bus, const char *name, struct mr_spi
mr_err_t mr_hw_spi_device_add(mr_spi_device_t spi_device,
const char *name,
mr_uint16_t support_flag,
void *cs_data);
mr_uint16_t cs_pin);
#endif

View File

@@ -12,6 +12,9 @@
#if (MR_DEVICE_TIMER == MR_CONF_ENABLE)
#undef LOG_TAG
#define LOG_TAG "timer"
static mr_uint32_t mr_timer_timeout_calculate(mr_timer_t timer, mr_uint32_t timeout)
{
mr_uint32_t count = 0, timer_period = 0, reload = 0;
@@ -95,42 +98,45 @@ static mr_err_t mr_timer_ioctl(mr_device_t device, int cmd, void *args)
mr_timer_t timer = (mr_timer_t)device;
mr_err_t ret = MR_ERR_OK;
switch (cmd & _MR_CMD_MASK)
switch (cmd & _MR_CMD_MASK1)
{
case MR_CMD_CONFIG:
case MR_CMD_SET:
{
if (args)
switch (cmd & _MR_CMD_MASK2)
{
if (timer->config.freq > timer->information.max_freq)
return - MR_ERR_GENERIC;
ret = timer->ops->configure(timer, (struct mr_timer_config *)args);
if (ret == MR_ERR_OK)
timer->config = *(struct mr_timer_config *)args;
case MR_CMD_CONFIG:
{
if (args)
{
/* Check the frequency */
if (((struct mr_timer_config *)args)->freq > timer->information.max_freq)
return - MR_ERR_GENERIC;
ret = timer->ops->configure(timer, (struct mr_timer_config *)args);
if (ret == MR_ERR_OK)
timer->config = *(struct mr_timer_config *)args;
}
break;
}
case MR_CMD_RX_CB:
{
device->rx_cb = args;
break;
}
case MR_CMD_REBOOT:
{
timer->overflow = 0;
timer->cycles = timer->reload;
timer->ops->start(timer, timer->timeout / (1000000 / timer->config.freq));
break;
}
default: ret = - MR_ERR_UNSUPPORTED;
}
break;
}
case MR_CMD_SET_RX_CALLBACK:
{
if (args)
device->rx_cb = (mr_err_t (*)(mr_device_t device, void *args))args;
break;
}
case MR_CMD_REBOOT:
{
timer->overflow = 0;
timer->cycles = timer->reload;
timer->ops->start(timer, timer->timeout / (1000000 / timer->config.freq));
break;
}
case MR_CMD_STOP:
{
timer->ops->stop(timer);
break;
}
default: ret = - MR_ERR_UNSUPPORTED;
}
@@ -144,7 +150,10 @@ static mr_size_t mr_timer_read(mr_device_t device, mr_off_t pos, void *buffer, m
mr_uint32_t cut = 0;
if (size != sizeof(mr_uint32_t))
{
MR_LOG_D(LOG_TAG, "Device %s: Invalid read size %d\r\n", device->object.name, size);
return 0;
}
cut = timer->ops->get_count(timer);
if (timer->information.cut_mode == _MR_TIMER_CUT_MODE_DOWN)
@@ -162,10 +171,16 @@ static mr_size_t mr_timer_write(mr_device_t device, mr_off_t pos, const void *bu
mr_uint32_t period_reload = 0;
if (size != sizeof(mr_uint32_t))
{
MR_LOG_D(LOG_TAG, "Device %s: Invalid write size %d\r\n", device->object.name, size);
return 0;
}
if (timer->config.freq == 0)
{
MR_LOG_D(LOG_TAG, "Device %s: Invalid frequency %d\r\n", device->object.name, timer->config.freq);
return 0;
}
timer->ops->stop(timer);
period_reload = mr_timer_timeout_calculate(timer, *timeout);

View File

@@ -27,8 +27,8 @@
/* Default config for mr_timer_config structure */
#define MR_TIMER_CONFIG_DEFAULT \
{ \
1000, \
MR_TIMER_MODE_PERIOD, \
1000, \
MR_TIMER_MODE_PERIOD, \
}
struct mr_timer_config

View File

@@ -10,6 +10,9 @@
#include <mrlib.h>
#undef LOG_TAG
#define LOG_TAG "device"
/**
* @brief This function find the device.
*
@@ -66,6 +69,8 @@ mr_err_t mr_device_add(mr_device_t device,
/* Set operations as null-ops if ops is null */
device->ops = (ops == MR_NULL) ? &null_ops : ops;
MR_LOG_D("Add device %s", device->object.name);
return MR_ERR_OK;
}
@@ -83,12 +88,17 @@ mr_err_t mr_device_open(mr_device_t device, mr_uint16_t flags)
/* Check if the specified open flags are supported by the device */
if (flags != (flags & device->support_flag))
{
MR_LOG_E("Open device %s, unsupported flags %d", device->object.name, flags);
return - MR_ERR_UNSUPPORTED;
}
/* Update the device open-flag and refer-count */
device->open_flag |= (flags & _MR_OPEN_FLAG_MASK);
device->ref_count ++;
MR_LOG_D("Open device %s, ref count %d", device->object.name, device->ref_count);
/* Check if the device is already closed */
if ((device->open_flag & MR_OPEN_ACTIVE))
return MR_ERR_OK;
@@ -116,11 +126,16 @@ mr_err_t mr_device_close(mr_device_t device)
/* Check if the device is already closed */
if (device->open_flag == MR_OPEN_CLOSED)
{
MR_LOG_E("Close device %s, already closed", device->object.name);
return MR_ERR_OK;
}
/* Decrement the reference count */
device->ref_count --;
MR_LOG_D("Close device %s, ref count %d", device->object.name, device->ref_count);
/* If the reference count is still non-zero, return without closing the device */
if (device->ref_count != 0)
return MR_ERR_OK;
@@ -152,7 +167,10 @@ mr_err_t mr_device_ioctl(mr_device_t device, int cmd, void *args)
/* Call the device-ioctl function, if provided */
if (device->ops->ioctl == MR_NULL)
{
MR_LOG_D("Device %s ioctl function is null ", device->object.name);
return - MR_ERR_UNSUPPORTED;
}
return device->ops->ioctl(device, cmd, args);
}
@@ -165,7 +183,7 @@ mr_err_t mr_device_ioctl(mr_device_t device, int cmd, void *args)
* @param buffer The data buffer to be read to device.
* @param size The size of read.
*
* @return The size of the actual read on success, otherwise return 0.
* @return The size of the actual read on success, otherwise an error code.
*/
mr_size_t mr_device_read(mr_device_t device, mr_off_t pos, void *buffer, mr_size_t size)
{
@@ -174,11 +192,17 @@ mr_size_t mr_device_read(mr_device_t device, mr_off_t pos, void *buffer, mr_size
/* Check if the device is closed or unsupported */
if ((device->ref_count == 0) || ! (device->open_flag & MR_OPEN_RDONLY))
{
MR_LOG_D("Device %s unsupported read", device->object.name);
return 0;
}
/* Call the device-read function, if provided */
if (device->ops->read == MR_NULL)
{
MR_LOG_D("Device %s read function is null", device->object.name);
return 0;
}
return device->ops->read(device, pos, buffer, size);
}
@@ -200,11 +224,17 @@ mr_size_t mr_device_write(mr_device_t device, mr_off_t pos, const void *buffer,
/* Check if the device is closed or unsupported */
if ((device->ref_count == 0) || ! (device->open_flag & MR_OPEN_WRONLY))
{
MR_LOG_D("Device %s unsupported write", device->object.name);
return 0;
}
/* Call the device-write function, if provided */
if (device->ops->write == MR_NULL)
{
MR_LOG_D("Device %s write function is null", device->object.name);
return 0;
}
return device->ops->write(device, pos, buffer, size);
}