41
41
#include <init.h>
42
42
#include <uart.h>
43
43
#include <string.h>
44
+ #include <ring_buffer.h>
44
45
#include <misc/byteorder.h>
45
46
#include <usb/class/usb_cdc.h>
46
47
#include <usb/usb_device.h>
@@ -190,8 +191,7 @@ struct cdc_acm_dev_data_t {
190
191
bool tx_irq_ena ; /* Tx interrupt enable status */
191
192
bool rx_irq_ena ; /* Rx interrupt enable status */
192
193
u8_t rx_buf [CDC_ACM_BUFFER_SIZE ];/* Internal Rx buffer */
193
- u32_t rx_buf_head ; /* Head of the internal Rx buffer */
194
- u32_t rx_buf_tail ; /* Tail of the internal Rx buffer */
194
+ struct ring_buf * rx_ringbuf ;
195
195
/* Interface data buffer */
196
196
#ifndef CONFIG_USB_COMPOSITE_DEVICE
197
197
u8_t interface_data [CDC_CLASS_REQ_MAX_DATA_SIZE ];
@@ -314,9 +314,8 @@ static void cdc_acm_bulk_in(u8_t ep, enum usb_dc_ep_cb_status_code ep_status)
314
314
static void cdc_acm_bulk_out (u8_t ep , enum usb_dc_ep_cb_status_code ep_status )
315
315
{
316
316
struct cdc_acm_dev_data_t * dev_data ;
317
- u32_t bytes_to_read , i , j , buf_head ;
318
317
struct usb_dev_data * common ;
319
- u8_t tmp_buf [ 4 ] ;
318
+ u32_t bytes_to_read , read ;
320
319
321
320
ARG_UNUSED (ep_status );
322
321
@@ -329,35 +328,16 @@ static void cdc_acm_bulk_out(u8_t ep, enum usb_dc_ep_cb_status_code ep_status)
329
328
dev_data = CONTAINER_OF (common , struct cdc_acm_dev_data_t , common );
330
329
331
330
/* Check how many bytes were received */
332
- usb_read (ep , NULL , 0 , & bytes_to_read );
331
+ usb_read (ep , NULL , 0 , & read );
333
332
334
- buf_head = dev_data -> rx_buf_head ;
333
+ bytes_to_read = MIN ( read , sizeof ( dev_data -> rx_buf )) ;
335
334
336
- /*
337
- * Quark SE USB controller is always storing data
338
- * in the FIFOs per 32-bit words.
339
- */
340
- for (i = 0U ; i < bytes_to_read ; i += 4 ) {
341
- usb_read (ep , tmp_buf , 4 , NULL );
342
-
343
- for (j = 0U ; j < 4 ; j ++ ) {
344
- if (i + j == bytes_to_read ) {
345
- /* We read all the data */
346
- break ;
347
- }
348
-
349
- if (((buf_head + 1 ) % CDC_ACM_BUFFER_SIZE ) ==
350
- dev_data -> rx_buf_tail ) {
351
- /* FIFO full, discard data */
352
- LOG_ERR ("CDC buffer full!" );
353
- } else {
354
- dev_data -> rx_buf [buf_head ] = tmp_buf [j ];
355
- buf_head = (buf_head + 1 ) % CDC_ACM_BUFFER_SIZE ;
356
- }
357
- }
335
+ usb_read (ep , dev_data -> rx_buf , bytes_to_read , & read );
336
+
337
+ if (!ring_buf_put (dev_data -> rx_ringbuf , dev_data -> rx_buf , read )) {
338
+ LOG_ERR ("Ring buffer full" );
358
339
}
359
340
360
- dev_data -> rx_buf_head = buf_head ;
361
341
dev_data -> rx_ready = true;
362
342
363
343
/* Call callback only if rx irq ena */
@@ -627,34 +607,18 @@ static int cdc_acm_fifo_fill(struct device *dev,
627
607
*
628
608
* @return Number of bytes read.
629
609
*/
630
- static int cdc_acm_fifo_read (struct device * dev , u8_t * rx_data ,
631
- const int size )
610
+ static int cdc_acm_fifo_read (struct device * dev , u8_t * rx_data , const int size )
632
611
{
633
- u32_t avail_data , bytes_read , i ;
634
612
struct cdc_acm_dev_data_t * const dev_data = DEV_DATA (dev );
613
+ u32_t len ;
635
614
636
- avail_data = (CDC_ACM_BUFFER_SIZE + dev_data -> rx_buf_head -
637
- dev_data -> rx_buf_tail ) % CDC_ACM_BUFFER_SIZE ;
638
- if (avail_data > size ) {
639
- bytes_read = size ;
640
- } else {
641
- bytes_read = avail_data ;
642
- }
643
-
644
- for (i = 0U ; i < bytes_read ; i ++ ) {
645
- rx_data [i ] = dev_data -> rx_buf [(dev_data -> rx_buf_tail + i ) %
646
- CDC_ACM_BUFFER_SIZE ];
647
- }
648
-
649
- dev_data -> rx_buf_tail = (dev_data -> rx_buf_tail + bytes_read ) %
650
- CDC_ACM_BUFFER_SIZE ;
615
+ len = ring_buf_get (dev_data -> rx_ringbuf , rx_data , size );
651
616
652
- if (dev_data -> rx_buf_tail == dev_data -> rx_buf_head ) {
653
- /* Buffer empty */
617
+ if (ring_buf_is_empty (dev_data -> rx_ringbuf )) {
654
618
dev_data -> rx_ready = false;
655
619
}
656
620
657
- return bytes_read ;
621
+ return len ;
658
622
}
659
623
660
624
/**
@@ -1079,9 +1043,11 @@ static const struct uart_driver_api cdc_acm_driver_api = {
1079
1043
#endif /* CONFIG_USB_COMPOSITE_DEVICE */
1080
1044
1081
1045
#define DEFINE_CDC_ACM_DEV_DATA (x ) \
1046
+ RING_BUF_DECLARE(rx_ringbuf_##x, 512); \
1082
1047
static struct cdc_acm_dev_data_t cdc_acm_dev_data_##x = { \
1083
1048
.usb_status = USB_DC_UNKNOWN, \
1084
1049
.line_coding = CDC_ACM_DEFAUL_BAUDRATE, \
1050
+ .rx_ringbuf = &rx_ringbuf_##x, \
1085
1051
}
1086
1052
1087
1053
#define DEFINE_CDC_ACM_DEVICE (x ) \
0 commit comments