更新内核代码,统一函数规范:
1.注册到内核容器的变量初始化函数统一命名为 xxx_add_to_container. 2.不注册到内核容器的变量初始化函数统一命名为 xxx_init.
This commit is contained in:
159
src/device.c
159
src/device.c
@@ -14,41 +14,14 @@ mr_device_t mr_device_find(const char *name)
|
||||
{
|
||||
mr_device_t device = MR_NULL;
|
||||
|
||||
/* Debug log */
|
||||
MR_LOG_D(name);
|
||||
|
||||
/* Find the device object from the device container */
|
||||
device = (mr_device_t) mr_object_find(name, MR_CONTAINER_TYPE_DEVICE);
|
||||
if (device == MR_NULL)
|
||||
{
|
||||
/* Error log */
|
||||
MR_LOG_E(name, -MR_ERR_NOT_FOUND);
|
||||
return MR_NULL;
|
||||
}
|
||||
|
||||
return device;
|
||||
}
|
||||
|
||||
void mr_device_init(mr_device_t device, const char *name)
|
||||
{
|
||||
/* Debug log */
|
||||
MR_LOG_D(name);
|
||||
|
||||
/* Initialize the object */
|
||||
mr_object_init(&device->object, name);
|
||||
|
||||
/* Initialize the private fields */
|
||||
device->rx_callback = MR_NULL;
|
||||
device->tx_callback = MR_NULL;
|
||||
device->type = MR_DEVICE_TYPE_NULL;
|
||||
device->support_flag = MR_OPEN_CLOSED;
|
||||
device->open_flag = MR_OPEN_CLOSED;
|
||||
device->ref_count = 0;
|
||||
device->data = MR_NULL;
|
||||
device->ops = MR_NULL;
|
||||
}
|
||||
|
||||
mr_err_t mr_device_add_to_container(mr_device_t device,
|
||||
const char *name,
|
||||
enum mr_device_type type,
|
||||
mr_uint16_t support_flag,
|
||||
const struct mr_device_ops *ops,
|
||||
@@ -57,69 +30,54 @@ mr_err_t mr_device_add_to_container(mr_device_t device,
|
||||
mr_err_t error_code = MR_ERR_OK;
|
||||
static struct mr_device_ops null_ops = {MR_NULL};
|
||||
|
||||
/* Debug log */
|
||||
MR_LOG_D(device->object.name);
|
||||
MR_ASSERT(device != MR_NULL);
|
||||
|
||||
/* Add the object to the container */
|
||||
error_code = mr_object_add_to_container(&device->object, name, MR_CONTAINER_TYPE_DEVICE);
|
||||
if (error_code != MR_ERR_OK)
|
||||
return error_code;
|
||||
|
||||
/* Initialize the private fields */
|
||||
device->rx_callback = MR_NULL;
|
||||
device->tx_callback = MR_NULL;
|
||||
device->type = type;
|
||||
device->support_flag = support_flag;
|
||||
device->open_flag = MR_NULL;
|
||||
device->data = data;
|
||||
|
||||
/* Set operations as null_ops if ops is null */
|
||||
device->ops = (ops == MR_NULL) ? &null_ops : ops;
|
||||
|
||||
/* Register the object to the container */
|
||||
error_code = mr_object_add_to_container(&device->object, MR_CONTAINER_TYPE_DEVICE);
|
||||
MR_LOG_E(device->object.name, error_code);
|
||||
|
||||
return error_code;
|
||||
}
|
||||
|
||||
mr_err_t mr_device_open(mr_device_t device, mr_uint16_t flags)
|
||||
{
|
||||
mr_err_t error_code = MR_ERR_OK;
|
||||
|
||||
/* Debug log */
|
||||
MR_LOG_D(device->object.name);
|
||||
MR_ASSERT(device != MR_NULL);
|
||||
|
||||
/* Check if the specified open flags are supported by the device */
|
||||
if (flags != (flags & device->support_flag))
|
||||
{
|
||||
/* Error log */
|
||||
MR_LOG_E(device->object.name, -MR_ERR_UNSUPPORTED);
|
||||
return -MR_ERR_UNSUPPORTED;
|
||||
}
|
||||
|
||||
/* Check if the device is already closed */
|
||||
if (!(device->open_flag & MR_OPEN_ACTIVE))
|
||||
{
|
||||
device->open_flag = (flags & _MR_OPEN_FLAG_MASK);
|
||||
if ((device->open_flag & MR_OPEN_ACTIVE))
|
||||
return MR_ERR_OK;
|
||||
|
||||
/* Call the device open function, if provided */
|
||||
if (device->ops->open != MR_NULL)
|
||||
{
|
||||
error_code = device->ops->open(device);
|
||||
/* Update the device open-flag and refer-count */
|
||||
device->open_flag = (flags & _MR_OPEN_FLAG_MASK);
|
||||
device->open_flag |= MR_OPEN_ACTIVE;
|
||||
device->ref_count++;
|
||||
|
||||
/* Error log */
|
||||
MR_LOG_E(device->object.name, error_code);
|
||||
if (error_code != MR_ERR_OK)
|
||||
return error_code;
|
||||
}
|
||||
/* Call the device open function, if provided */
|
||||
if (device->ops->open == MR_NULL)
|
||||
return MR_ERR_OK;
|
||||
|
||||
/* Update the device type as active */
|
||||
device->open_flag |= MR_OPEN_ACTIVE;
|
||||
device->ref_count++;
|
||||
}
|
||||
|
||||
return MR_ERR_OK;
|
||||
return device->ops->open(device);
|
||||
}
|
||||
|
||||
mr_err_t mr_device_close(mr_device_t device)
|
||||
{
|
||||
mr_err_t error_code = MR_ERR_OK;
|
||||
|
||||
/* Debug log */
|
||||
MR_LOG_D(device->object.name);
|
||||
MR_ASSERT(device != MR_NULL);
|
||||
|
||||
/* If the reference count is zero, the device has already been closed */
|
||||
if (device->ref_count == 0)
|
||||
@@ -136,96 +94,53 @@ mr_err_t mr_device_close(mr_device_t device)
|
||||
return MR_ERR_OK;
|
||||
|
||||
/* Call the device close function, if provided */
|
||||
if (device->ops->close != MR_NULL)
|
||||
{
|
||||
error_code = device->ops->close(device);
|
||||
if (device->ops->close == MR_NULL)
|
||||
return MR_ERR_OK;
|
||||
|
||||
/* Error log */
|
||||
MR_LOG_E(device->object.name, error_code);
|
||||
if (error_code != MR_ERR_OK)
|
||||
return error_code;
|
||||
}
|
||||
|
||||
return MR_ERR_OK;
|
||||
return device->ops->close(device);
|
||||
}
|
||||
|
||||
mr_err_t mr_device_ioctl(mr_device_t device, int cmd, void *args)
|
||||
{
|
||||
mr_err_t error_code = MR_ERR_OK;
|
||||
|
||||
/* Debug log */
|
||||
MR_LOG_D(device->object.name);
|
||||
MR_ASSERT(device != MR_NULL);
|
||||
|
||||
/* Check if the device is closed */
|
||||
if (device->ref_count == 0)
|
||||
{
|
||||
/* Error log */
|
||||
MR_LOG_E(device->object.name, -MR_ERR_UNSUPPORTED);
|
||||
return -MR_ERR_UNSUPPORTED;
|
||||
}
|
||||
|
||||
/* Call the device ioctl function, if provided */
|
||||
if (device->ops->ioctl != MR_NULL)
|
||||
{
|
||||
error_code = device->ops->ioctl(device, cmd, args);
|
||||
|
||||
/* Error log */
|
||||
MR_LOG_E(device->object.name, error_code);
|
||||
return error_code;
|
||||
} else
|
||||
{
|
||||
/* Error log */
|
||||
MR_LOG_E(device->object.name, -MR_ERR_IO);
|
||||
if (device->ops->ioctl == MR_NULL)
|
||||
return -MR_ERR_UNSUPPORTED;
|
||||
}
|
||||
|
||||
return device->ops->ioctl(device, cmd, args);
|
||||
}
|
||||
|
||||
mr_size_t mr_device_read(mr_device_t device, mr_off_t pos, void *buf, mr_size_t count)
|
||||
{
|
||||
/* Debug log */
|
||||
MR_LOG_D(device->object.name);
|
||||
MR_ASSERT(device != MR_NULL);
|
||||
|
||||
/* Check if the device is closed or unsupported */
|
||||
if ((device->ref_count == 0) || !(device->open_flag & MR_OPEN_RDONLY))
|
||||
{
|
||||
/* Error log */
|
||||
MR_LOG_E(device->object.name, -MR_ERR_UNSUPPORTED);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Call the device read function, if provided */
|
||||
if (device->ops->read != MR_NULL)
|
||||
{
|
||||
return device->ops->read(device, pos, buf, count);
|
||||
} else
|
||||
{
|
||||
/* Error log */
|
||||
MR_LOG_E(device->object.name, -MR_ERR_IO);
|
||||
if (device->ops->read == MR_NULL)
|
||||
return 0;
|
||||
}
|
||||
|
||||
return device->ops->read(device, pos, buf, count);
|
||||
}
|
||||
|
||||
mr_size_t mr_device_write(mr_device_t device, mr_off_t pos, const void *buf, mr_size_t count)
|
||||
{
|
||||
/* Debug log */
|
||||
MR_LOG_D(device->object.name);
|
||||
MR_ASSERT(device != MR_NULL);
|
||||
|
||||
/* Check if the device is closed or unsupported */
|
||||
if ((device->ref_count == 0) || !(device->open_flag & MR_OPEN_WRONLY))
|
||||
{
|
||||
/* Error log */
|
||||
MR_LOG_E(device->object.name, -MR_ERR_UNSUPPORTED);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Call the device write function, if provided */
|
||||
if (device->ops->write != MR_NULL)
|
||||
{
|
||||
return device->ops->write(device, pos, buf, count);
|
||||
} else
|
||||
{
|
||||
/* Error log */
|
||||
MR_LOG_E(device->object.name, -MR_ERR_IO);
|
||||
if (device->ops->write == MR_NULL)
|
||||
return 0;
|
||||
}
|
||||
|
||||
return device->ops->write(device, pos, buf, count);
|
||||
}
|
||||
249
src/kernel.c
249
src/kernel.c
@@ -22,9 +22,7 @@ static struct mr_container mr_kernel_container[_MR_CONTAINER_TYPE_MASK] =
|
||||
|
||||
mr_container_t mr_container_find(enum mr_container_type type)
|
||||
{
|
||||
/* Check if the kernel has this container type */
|
||||
if (type >= _MR_CONTAINER_TYPE_MASK)
|
||||
return MR_NULL;
|
||||
MR_ASSERT(type < _MR_CONTAINER_TYPE_MASK);
|
||||
|
||||
return &mr_kernel_container[type];
|
||||
}
|
||||
@@ -33,19 +31,15 @@ mr_object_t mr_object_find(const char *name, enum mr_container_type type)
|
||||
{
|
||||
mr_list_t list = MR_NULL;
|
||||
mr_container_t container = MR_NULL;
|
||||
mr_object_t object = MR_NULL;
|
||||
|
||||
/* Get corresponding container */
|
||||
container = mr_container_find(type);
|
||||
if (container == MR_NULL)
|
||||
return MR_NULL;
|
||||
|
||||
/* Walk through the container looking for objects */
|
||||
for (list = (container->list).next; list != &(container->list); list = list->next)
|
||||
{
|
||||
mr_object_t object;
|
||||
object = mr_container_of(list, struct mr_object, list);
|
||||
if (object == MR_NULL)
|
||||
continue;
|
||||
if (mr_strncmp(object->name, name, MR_NAME_MAX) == 0)
|
||||
return object;
|
||||
}
|
||||
@@ -53,56 +47,25 @@ mr_object_t mr_object_find(const char *name, enum mr_container_type type)
|
||||
return MR_NULL;
|
||||
}
|
||||
|
||||
void mr_object_init(mr_object_t object, const char *name)
|
||||
{
|
||||
/* Set the object list to its initial state */
|
||||
mr_list_init(&(object->list));
|
||||
|
||||
/* Copy the specified name to the object name */
|
||||
mr_strncpy(object->name, name, MR_NAME_MAX);
|
||||
|
||||
/* Set the object type to initialized */
|
||||
object->type = MR_OBJECT_TYPE_NULL;
|
||||
object->type |= MR_OBJECT_TYPE_STATIC;
|
||||
}
|
||||
|
||||
mr_object_t mr_object_create(const char *name)
|
||||
{
|
||||
mr_object_t object = MR_NULL;
|
||||
|
||||
/* Allocate memory for the object */
|
||||
object = (mr_object_t) mr_malloc(sizeof(struct mr_object));
|
||||
if (object == MR_NULL)
|
||||
return MR_NULL;
|
||||
|
||||
/* Initialize the memory */
|
||||
mr_memset(object, 0x0, sizeof(struct mr_object));
|
||||
|
||||
/* Set the object list to its initial state */
|
||||
mr_list_init(&(object->list));
|
||||
|
||||
/* Copy the specified name to the object name */
|
||||
mr_strncpy(object->name, name, MR_NAME_MAX);
|
||||
|
||||
return object;
|
||||
}
|
||||
|
||||
mr_err_t mr_object_add_to_container(mr_object_t object, enum mr_container_type container_type)
|
||||
mr_err_t mr_object_add_to_container(mr_object_t object, const char *name, enum mr_container_type container_type)
|
||||
{
|
||||
mr_container_t container = MR_NULL;
|
||||
|
||||
MR_ASSERT(object != MR_NULL);
|
||||
|
||||
/* Check if the object is already registered */
|
||||
if (object->type & MR_OBJECT_TYPE_REGISTER)
|
||||
return -MR_ERR_BUSY;
|
||||
|
||||
/* Check if the object already exists in the container */
|
||||
if (mr_object_find(name, container_type) != MR_NULL)
|
||||
return -MR_ERR_GENERIC;
|
||||
|
||||
/* Copy the specified name to the object name */
|
||||
mr_strncpy(object->name, name, MR_NAME_MAX);
|
||||
|
||||
/* Find the container for the specified type */
|
||||
container = mr_container_find(container_type);
|
||||
if (container == MR_NULL)
|
||||
return -MR_ERR_NOT_FOUND;
|
||||
|
||||
/* Check if the object already exists in the container */
|
||||
if (mr_object_find(object->name, container_type) != MR_NULL)
|
||||
return -MR_ERR_GENERIC;
|
||||
|
||||
/* Insert the object into the container's list */
|
||||
mr_list_insert_after(&(container->list), &(object->list));
|
||||
@@ -115,6 +78,8 @@ mr_err_t mr_object_remove_from_container(mr_object_t object)
|
||||
{
|
||||
mr_err_t error_code = MR_ERR_OK;
|
||||
|
||||
MR_ASSERT(object != MR_NULL);
|
||||
|
||||
/* Check if the object is registered */
|
||||
if ((object->type & MR_OBJECT_TYPE_REGISTER) == 0)
|
||||
return -MR_ERR_GENERIC;
|
||||
@@ -126,42 +91,43 @@ mr_err_t mr_object_remove_from_container(mr_object_t object)
|
||||
return error_code;
|
||||
}
|
||||
|
||||
mr_err_t mr_object_move(mr_object_t object, enum mr_container_type dest_type)
|
||||
mr_err_t mr_object_move(mr_object_t object, enum mr_container_type dst_type)
|
||||
{
|
||||
mr_err_t error_code = MR_ERR_OK;
|
||||
|
||||
MR_ASSERT(object != MR_NULL);
|
||||
|
||||
/* Remove the object from its current container */
|
||||
error_code = mr_object_remove_from_container(object);
|
||||
if (error_code != MR_ERR_OK)
|
||||
return error_code;
|
||||
|
||||
/* Add the object to the new container */
|
||||
error_code = mr_object_add_to_container(object, dest_type);
|
||||
error_code = mr_object_add_to_container(object, object->name, dst_type);
|
||||
|
||||
return error_code;
|
||||
}
|
||||
|
||||
void mr_object_rename(mr_object_t object, char *name)
|
||||
{
|
||||
mr_strncpy(object->name, name, MR_NAME_MAX);
|
||||
}
|
||||
MR_ASSERT(object != MR_NULL);
|
||||
|
||||
mr_bool_t mr_object_is_static(mr_object_t object)
|
||||
{
|
||||
if (object->type & MR_OBJECT_TYPE_STATIC)
|
||||
return MR_TRUE;
|
||||
else
|
||||
return MR_FALSE;
|
||||
mr_strncpy(object->name, name, MR_NAME_MAX);
|
||||
}
|
||||
|
||||
void mr_mutex_init(mr_mutex_t mutex)
|
||||
{
|
||||
MR_ASSERT(mutex != MR_NULL);
|
||||
|
||||
mutex->owner = MR_NULL;
|
||||
mutex->lock = MR_UNLOCK;
|
||||
}
|
||||
|
||||
mr_err_t mr_mutex_take(mr_mutex_t mutex, mr_object_t owner)
|
||||
{
|
||||
MR_ASSERT(mutex != MR_NULL);
|
||||
MR_ASSERT(owner != MR_NULL);
|
||||
|
||||
mr_hw_interrupt_disable();
|
||||
|
||||
if (mutex->owner != owner)
|
||||
@@ -184,6 +150,9 @@ mr_err_t mr_mutex_take(mr_mutex_t mutex, mr_object_t owner)
|
||||
|
||||
mr_err_t mr_mutex_release(mr_mutex_t mutex, mr_object_t owner)
|
||||
{
|
||||
MR_ASSERT(mutex != MR_NULL);
|
||||
MR_ASSERT(owner != MR_NULL);
|
||||
|
||||
mr_hw_interrupt_disable();
|
||||
|
||||
if (mutex->owner == owner)
|
||||
@@ -200,6 +169,9 @@ mr_err_t mr_mutex_release(mr_mutex_t mutex, mr_object_t owner)
|
||||
|
||||
void mr_ringbuffer_init(mr_ringbuffer_t ringbuffer, mr_uint8_t *pool, mr_size_t pool_size)
|
||||
{
|
||||
MR_ASSERT(ringbuffer != MR_NULL);
|
||||
MR_ASSERT(pool != MR_NULL);
|
||||
|
||||
ringbuffer->read_mirror = ringbuffer->read_index = 0;
|
||||
ringbuffer->write_mirror = ringbuffer->write_index = 0;
|
||||
|
||||
@@ -209,6 +181,8 @@ void mr_ringbuffer_init(mr_ringbuffer_t ringbuffer, mr_uint8_t *pool, mr_size_t
|
||||
|
||||
MR_INLINE enum mr_ringbuffer_state mr_ringbuffer_get_state(mr_ringbuffer_t ringbuffer)
|
||||
{
|
||||
MR_ASSERT(ringbuffer != MR_NULL);
|
||||
|
||||
if (ringbuffer->read_index == ringbuffer->write_index)
|
||||
{
|
||||
if (ringbuffer->read_mirror == ringbuffer->write_mirror)
|
||||
@@ -224,6 +198,8 @@ mr_size_t mr_ringbuffer_get_data_length(mr_ringbuffer_t ringbuffer)
|
||||
{
|
||||
mr_size_t wi, ri;
|
||||
|
||||
MR_ASSERT(ringbuffer != MR_NULL);
|
||||
|
||||
switch (mr_ringbuffer_get_state(ringbuffer))
|
||||
{
|
||||
case MR_RINGBUFFER_EMPTY:return 0;
|
||||
@@ -243,13 +219,39 @@ mr_size_t mr_ringbuffer_get_data_length(mr_ringbuffer_t ringbuffer)
|
||||
|
||||
MR_INLINE mr_size_t mr_ringbuffer_space_length(mr_ringbuffer_t ringbuffer)
|
||||
{
|
||||
MR_ASSERT(ringbuffer != MR_NULL);
|
||||
|
||||
return (ringbuffer->buffer_size - mr_ringbuffer_get_data_length(ringbuffer));
|
||||
}
|
||||
|
||||
mr_size_t mr_ringbuffer_write_byte(mr_ringbuffer_t ringbuffer, mr_uint8_t data)
|
||||
{
|
||||
/* Whether there is enough space */
|
||||
if (!mr_ringbuffer_space_length(ringbuffer))
|
||||
return 0;
|
||||
|
||||
ringbuffer->buffer[ringbuffer->write_index] = data;
|
||||
|
||||
/* flip mirror */
|
||||
if (ringbuffer->write_index == ringbuffer->buffer_size - 1)
|
||||
{
|
||||
ringbuffer->write_mirror = ~ringbuffer->write_mirror;
|
||||
ringbuffer->write_index = 0;
|
||||
} else
|
||||
{
|
||||
ringbuffer->write_index++;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
mr_size_t mr_ringbuffer_write(mr_ringbuffer_t ringbuffer, const mr_uint8_t *buffer, mr_size_t length)
|
||||
{
|
||||
mr_size_t space_length;
|
||||
|
||||
MR_ASSERT(ringbuffer != MR_NULL);
|
||||
MR_ASSERT(buffer != MR_NULL);
|
||||
|
||||
space_length = mr_ringbuffer_space_length(ringbuffer);
|
||||
if (space_length == 0)
|
||||
return 0;
|
||||
@@ -278,10 +280,43 @@ mr_size_t mr_ringbuffer_write(mr_ringbuffer_t ringbuffer, const mr_uint8_t *buff
|
||||
}
|
||||
}
|
||||
|
||||
mr_size_t mr_ringbuffer_write_byte_force(mr_ringbuffer_t ringbuffer, mr_uint8_t data)
|
||||
{
|
||||
enum mr_ringbuffer_state old_state;
|
||||
|
||||
MR_ASSERT(ringbuffer != MR_NULL);
|
||||
|
||||
old_state = mr_ringbuffer_get_state(ringbuffer);
|
||||
|
||||
ringbuffer->buffer[ringbuffer->write_index] = data;
|
||||
|
||||
/* flip mirror */
|
||||
if (ringbuffer->write_index == ringbuffer->buffer_size - 1)
|
||||
{
|
||||
ringbuffer->write_mirror = ~ringbuffer->write_mirror;
|
||||
ringbuffer->write_index = 0;
|
||||
if (old_state == MR_RINGBUFFER_FULL)
|
||||
{
|
||||
ringbuffer->read_mirror = ~ringbuffer->read_mirror;
|
||||
ringbuffer->read_index = ringbuffer->write_index;
|
||||
}
|
||||
} else
|
||||
{
|
||||
ringbuffer->write_index++;
|
||||
if (old_state == MR_RINGBUFFER_FULL)
|
||||
ringbuffer->read_index = ringbuffer->write_index;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
mr_size_t mr_ringbuffer_write_force(mr_ringbuffer_t ringbuffer, const mr_uint8_t *buffer, mr_size_t length)
|
||||
{
|
||||
mr_size_t space_length;
|
||||
|
||||
MR_ASSERT(ringbuffer != MR_NULL);
|
||||
MR_ASSERT(buffer != MR_NULL);
|
||||
|
||||
space_length = mr_ringbuffer_space_length(ringbuffer);
|
||||
if (space_length == 0)
|
||||
return 0;
|
||||
@@ -324,10 +359,37 @@ mr_size_t mr_ringbuffer_write_force(mr_ringbuffer_t ringbuffer, const mr_uint8_t
|
||||
}
|
||||
}
|
||||
|
||||
mr_size_t mr_ringbuffer_read_byte(mr_ringbuffer_t ringbuffer, mr_uint8_t *data)
|
||||
{
|
||||
MR_ASSERT(ringbuffer != MR_NULL);
|
||||
MR_ASSERT(data != MR_NULL);
|
||||
|
||||
/* ringbuffer is empty */
|
||||
if (!mr_ringbuffer_get_data_length(ringbuffer))
|
||||
return 0;
|
||||
|
||||
/* put byte */
|
||||
*data = ringbuffer->buffer[ringbuffer->read_index];
|
||||
|
||||
if (ringbuffer->read_index == ringbuffer->buffer_size - 1)
|
||||
{
|
||||
ringbuffer->read_mirror = ~ringbuffer->read_mirror;
|
||||
ringbuffer->read_index = 0;
|
||||
} else
|
||||
{
|
||||
ringbuffer->read_index++;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
mr_size_t mr_ringbuffer_read(mr_ringbuffer_t ringbuffer, mr_uint8_t *buffer, mr_size_t length)
|
||||
{
|
||||
mr_size_t count;
|
||||
|
||||
MR_ASSERT(ringbuffer != MR_NULL);
|
||||
MR_ASSERT(buffer != MR_NULL);
|
||||
|
||||
count = mr_ringbuffer_get_data_length(ringbuffer);
|
||||
if (count == 0)
|
||||
return 0;
|
||||
@@ -356,72 +418,3 @@ mr_size_t mr_ringbuffer_read(mr_ringbuffer_t ringbuffer, mr_uint8_t *buffer, mr_
|
||||
return length;
|
||||
}
|
||||
|
||||
mr_size_t mr_ringbuffer_write_byte(mr_ringbuffer_t ringbuffer, mr_uint8_t data)
|
||||
{
|
||||
/* Whether there is enough space */
|
||||
if (!mr_ringbuffer_space_length(ringbuffer))
|
||||
return 0;
|
||||
|
||||
ringbuffer->buffer[ringbuffer->write_index] = data;
|
||||
|
||||
/* flip mirror */
|
||||
if (ringbuffer->write_index == ringbuffer->buffer_size - 1)
|
||||
{
|
||||
ringbuffer->write_mirror = ~ringbuffer->write_mirror;
|
||||
ringbuffer->write_index = 0;
|
||||
} else
|
||||
{
|
||||
ringbuffer->write_index++;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
mr_size_t mr_ringbuffer_write_byte_force(mr_ringbuffer_t ringbuffer, mr_uint8_t data)
|
||||
{
|
||||
enum mr_ringbuffer_state old_state;
|
||||
|
||||
old_state = mr_ringbuffer_get_state(ringbuffer);
|
||||
|
||||
ringbuffer->buffer[ringbuffer->write_index] = data;
|
||||
|
||||
/* flip mirror */
|
||||
if (ringbuffer->write_index == ringbuffer->buffer_size - 1)
|
||||
{
|
||||
ringbuffer->write_mirror = ~ringbuffer->write_mirror;
|
||||
ringbuffer->write_index = 0;
|
||||
if (old_state == MR_RINGBUFFER_FULL)
|
||||
{
|
||||
ringbuffer->read_mirror = ~ringbuffer->read_mirror;
|
||||
ringbuffer->read_index = ringbuffer->write_index;
|
||||
}
|
||||
} else
|
||||
{
|
||||
ringbuffer->write_index++;
|
||||
if (old_state == MR_RINGBUFFER_FULL)
|
||||
ringbuffer->read_index = ringbuffer->write_index;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
mr_size_t mr_ringbuffer_read_byte(mr_ringbuffer_t ringbuffer, mr_uint8_t *data)
|
||||
{
|
||||
/* ringbuffer is empty */
|
||||
if (!mr_ringbuffer_get_data_length(ringbuffer))
|
||||
return 0;
|
||||
|
||||
/* put byte */
|
||||
*data = ringbuffer->buffer[ringbuffer->read_index];
|
||||
|
||||
if (ringbuffer->read_index == ringbuffer->buffer_size - 1)
|
||||
{
|
||||
ringbuffer->read_mirror = ~ringbuffer->read_mirror;
|
||||
ringbuffer->read_index = 0;
|
||||
} else
|
||||
{
|
||||
ringbuffer->read_index++;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
Reference in New Issue
Block a user