diff --git a/components/drivers/include/rtdevice.h b/components/drivers/include/rtdevice.h index f8a944f5a70daccaa02163c5ae269808013d53c8..a941b22131e61b71237d2b5c607757a7cb75102f 100644 --- a/components/drivers/include/rtdevice.h +++ b/components/drivers/include/rtdevice.h @@ -45,9 +45,9 @@ struct rt_ringbuffer /* use the msb of the {read,write}_index as mirror bit. You can see this as * if the buffer adds a virtual mirror and the pointers point either to the * normal or to the mirrored buffer. If the write_index has the same value - * with the read_index, but in differenct mirro, the buffer is full. While - * if the write_index and the read_index are the same and within the same - * mirror, the buffer is empty. The ASCII art of the ringbuffer is: + * with the read_index, but in a different mirror, the buffer is full. + * While if the write_index and the read_index are the same and within the + * same mirror, the buffer is empty. The ASCII art of the ringbuffer is: * * mirror = 0 mirror = 1 * +---+---+---+---+---+---+---+|+~~~+~~~+~~~+~~~+~~~+~~~+~~~+ @@ -73,29 +73,6 @@ struct rt_ringbuffer rt_int16_t buffer_size; }; -/** return the size of data in rb */ -rt_inline rt_uint16_t RT_RINGBUFFER_SIZE(struct rt_ringbuffer *rb) -{ - if (rb->read_index == rb->write_index) - { - if (rb->read_mirror == rb->write_mirror) - /* we are in the same side, the ringbuffer is empty. */ - return 0; - else - return rb->buffer_size; - } - else - { - if (rb->write_index > rb->read_index) - return rb->write_index - rb->read_index; - else - return rb->buffer_size - (rb->read_index - rb->write_index); - } -} - -/** return the size of empty space in rb */ -#define RT_RINGBUFFER_EMPTY(rb) ((rb)->buffer_size - RT_RINGBUFFER_SIZE(rb)) - /* pipe device */ #define PIPE_DEVICE(device) ((struct rt_pipe_device*)(device)) struct rt_pipe_device @@ -163,12 +140,36 @@ rt_size_t rt_ringbuffer_get(struct rt_ringbuffer *rb, rt_uint8_t *ptr, rt_uint16_t length); rt_size_t rt_ringbuffer_getchar(struct rt_ringbuffer *rb, rt_uint8_t *ch); + rt_inline rt_uint16_t rt_ringbuffer_get_size(struct rt_ringbuffer *rb) { RT_ASSERT(rb != RT_NULL); return rb->buffer_size; } +/** return the size of data in rb */ +rt_inline rt_uint16_t rt_ringbuffer_data_len(struct rt_ringbuffer *rb) +{ + if (rb->read_index == rb->write_index) + { + if (rb->read_mirror == rb->write_mirror) + /* we are in the same side, the ringbuffer is empty. */ + return 0; + else + return rb->buffer_size; + } + else + { + if (rb->write_index > rb->read_index) + return rb->write_index - rb->read_index; + else + return rb->buffer_size - (rb->read_index - rb->write_index); + } +} + +/** return the size of empty space in rb */ +#define rt_ringbuffer_space_len(rb) ((rb)->buffer_size - rt_ringbuffer_data_len(rb)) + /** * Pipe Device */ diff --git a/components/drivers/src/ringbuffer.c b/components/drivers/src/ringbuffer.c index 8e7e8c7895fa051f38964191a79271c962323c53..ecc25870420be0d982befb567695349f504b9250 100644 --- a/components/drivers/src/ringbuffer.c +++ b/components/drivers/src/ringbuffer.c @@ -53,7 +53,7 @@ rt_size_t rt_ringbuffer_put(struct rt_ringbuffer *rb, RT_ASSERT(rb != RT_NULL); /* whether has enough space */ - size = RT_RINGBUFFER_EMPTY(rb); + size = rt_ringbuffer_space_len(rb); /* no space */ if (size == 0) @@ -100,7 +100,7 @@ rt_size_t rt_ringbuffer_get(struct rt_ringbuffer *rb, RT_ASSERT(rb != RT_NULL); /* whether has enough data */ - size = RT_RINGBUFFER_SIZE(rb); + size = rt_ringbuffer_data_len(rb); /* no data */ if (size == 0) @@ -143,7 +143,7 @@ rt_size_t rt_ringbuffer_putchar(struct rt_ringbuffer *rb, const rt_uint8_t ch) RT_ASSERT(rb != RT_NULL); /* whether has enough space */ - if (!RT_RINGBUFFER_EMPTY(rb)) + if (!rt_ringbuffer_space_len(rb)) return 0; rb->buffer_ptr[rb->write_index] = ch; @@ -171,7 +171,7 @@ rt_size_t rt_ringbuffer_getchar(struct rt_ringbuffer *rb, rt_uint8_t *ch) RT_ASSERT(rb != RT_NULL); /* ringbuffer is empty */ - if (!RT_RINGBUFFER_SIZE(rb)) + if (!rt_ringbuffer_data_len(rb)) return 0; /* put character */ diff --git a/components/drivers/usb/usbdevice/class/cdc_vcom.c b/components/drivers/usb/usbdevice/class/cdc_vcom.c index 1dee3a4d79a7c0b055e3d6cf56d20dca5935c504..5f284b029413161ffdfe6b18245187b773bba1d1 100644 --- a/components/drivers/usb/usbdevice/class/cdc_vcom.c +++ b/components/drivers/usb/usbdevice/class/cdc_vcom.c @@ -188,7 +188,7 @@ static rt_err_t _ep_in_handler(udevice_t device, uclass_t cls, rt_size_t size) eps = (cdc_eps_t)cls->eps; level = rt_hw_interrupt_disable(); - remain = RT_RINGBUFFER_SIZE(&tx_ringbuffer); + remain = rt_ringbuffer_data_len(&tx_ringbuffer); if (remain != 0) { /* although vcom_in_sending is set in SOF handler in the very @@ -453,7 +453,7 @@ static rt_err_t _class_sof_handler(udevice_t device, uclass_t cls) eps = (cdc_eps_t)cls->eps; - size = RT_RINGBUFFER_SIZE(&tx_ringbuffer); + size = rt_ringbuffer_data_len(&tx_ringbuffer); if (size == 0) return -RT_EFULL; @@ -611,7 +611,7 @@ static int _vcom_putc(struct rt_serial_device *serial, char c) * data out soon. But we cannot rely on that and if we wait to long, just * return. */ for (cnt = 500; - RT_RINGBUFFER_EMPTY(&tx_ringbuffer) == 0 && cnt; + rt_ringbuffer_space_len(&tx_ringbuffer) == 0 && cnt; cnt--) { /*rt_kprintf("wait for %d\n", cnt);*/ @@ -628,7 +628,7 @@ static int _vcom_putc(struct rt_serial_device *serial, char c) } level = rt_hw_interrupt_disable(); - if (RT_RINGBUFFER_EMPTY(&tx_ringbuffer)) + if (rt_ringbuffer_space_len(&tx_ringbuffer)) { rt_ringbuffer_putchar(&tx_ringbuffer, c); } @@ -646,7 +646,7 @@ static int _vcom_getc(struct rt_serial_device *serial) result = -1; level = rt_hw_interrupt_disable(); - if (RT_RINGBUFFER_SIZE(&rx_ringbuffer)) + if (rt_ringbuffer_data_len(&rx_ringbuffer)) { rt_ringbuffer_getchar(&rx_ringbuffer, &ch); result = ch;