Skip to content
Permalink
Browse files

samples: usb: cdc_acm_composite: Refactor sample

Use ring buffer and logger.

Signed-off-by: Andrei Emeltchenko <andrei.emeltchenko@intel.com>
  • Loading branch information...
finikorg authored and nashif committed Apr 18, 2019
1 parent 98c8a95 commit 88145db607ad0d47ceae4217a77364e4af079823
Showing with 59 additions and 25 deletions.
  1. +1 −0 samples/subsys/usb/cdc_acm_composite/prj.conf
  2. +58 −25 samples/subsys/usb/cdc_acm_composite/src/main.c
@@ -11,6 +11,7 @@ CONFIG_USB_DEVICE_PRODUCT="Zephyr CDC ACM sample"
CONFIG_USB_COMPOSITE_DEVICE=y
CONFIG_USB_CDC_ACM=y
CONFIG_USB_CDC_ACM_DEVICE_COUNT=2
CONFIG_USB_CDC_ACM_RINGBUF_SIZE=512

CONFIG_USB_DEVICE_LOG_LEVEL_ERR=y
CONFIG_USB_DRIVER_LOG_LEVEL_ERR=y
@@ -17,37 +17,69 @@
#include <device.h>
#include <uart.h>
#include <zephyr.h>
#include <ring_buffer.h>

static const char *banner = "Send characters to another UART device\r\n";
#include <logging/log.h>
LOG_MODULE_REGISTER(cdc_acm_composite, CONFIG_LOG_DEFAULT_LEVEL);

#define RING_BUF_SIZE (64 * 2)

u8_t buffer0[RING_BUF_SIZE];
u8_t buffer1[RING_BUF_SIZE];

static struct serial_data {
struct device *dev;
struct device *peer;
struct serial_data *peer_data;
struct ring_buf ringbuf;
} peers[2];

static void interrupt_handler(void *user_data)
{
const struct serial_data *dev_data = user_data;
struct serial_data *dev_data = user_data;
struct device *dev = dev_data->dev;


while (uart_irq_update(dev) && uart_irq_is_pending(dev)) {
struct device *peer = dev_data->peer;
u8_t byte;

if (!uart_irq_rx_ready(dev)) {
break;
}
LOG_DBG("dev %p dev_data %p", dev, dev_data);

uart_fifo_read(dev, &byte, sizeof(byte));
uart_poll_out(peer, byte);
if (uart_irq_rx_ready(dev)) {
u8_t buf[64];
size_t read, wrote;
struct ring_buf *ringbuf =
&dev_data->peer_data->ringbuf;

printk("dev %p -> dev %p Sent byte %c\n", dev, peer, byte);
}
}
read = uart_fifo_read(dev, buf, sizeof(buf));
if (read) {
wrote = ring_buf_put(ringbuf, buf, read);
if (wrote < read) {
LOG_ERR("Drop %u bytes", read - wrote);
}

static void write_data(struct device *dev, const char *buf, int len)
{
uart_fifo_fill(dev, (const u8_t *)buf, len);
uart_irq_tx_enable(dev_data->peer);

LOG_DBG("dev %p -> dev %p send %u bytes",
dev, peer, wrote);
}
}

if (uart_irq_tx_ready(dev)) {
u8_t buf[64];
size_t wrote, len;

len = ring_buf_get(&dev_data->ringbuf, buf,
sizeof(buf));
if (!len) {
LOG_DBG("dev %p TX buffer empty", dev);
uart_irq_tx_disable(dev);
} else {
wrote = uart_fifo_fill(dev, buf, len);
LOG_DBG("dev %p wrote len %d", dev, wrote);
}
}
}
}

static void uart_line_set(struct device *dev)
@@ -58,22 +90,22 @@ static void uart_line_set(struct device *dev)
/* They are optional, we use them to test the interrupt endpoint */
ret = uart_line_ctrl_set(dev, LINE_CTRL_DCD, 1);
if (ret) {
printf("Failed to set DCD, ret code %d\n", ret);
LOG_DBG("Failed to set DCD, ret code %d", ret);
}

ret = uart_line_ctrl_set(dev, LINE_CTRL_DSR, 1);
if (ret) {
printf("Failed to set DSR, ret code %d\n", ret);
LOG_DBG("Failed to set DSR, ret code %d", ret);
}

/* Wait 1 sec for the host to do all settings */
k_busy_wait(1000000);

ret = uart_line_ctrl_get(dev, LINE_CTRL_BAUD_RATE, &baudrate);
if (ret) {
printf("Failed to get baudrate, ret code %d\n", ret);
LOG_DBG("Failed to get baudrate, ret code %d", ret);
} else {
printf("Baudrate detected: %d\n", baudrate);
LOG_DBG("Baudrate detected: %d", baudrate);
}
}

@@ -86,17 +118,17 @@ void main(void)

dev0 = device_get_binding("CDC_ACM_0");
if (!dev0) {
printf("CDC_ACM_0 device not found\n");
LOG_DBG("CDC_ACM_0 device not found");
return;
}

dev1 = device_get_binding("CDC_ACM_1");
if (!dev1) {
printf("CDC_ACM_1 device not found\n");
LOG_DBG("CDC_ACM_1 device not found");
return;
}

printf("Wait for DTR\n");
LOG_DBG("Wait for DTR");

while (1) {
uart_line_ctrl_get(dev0, LINE_CTRL_DTR, &dtr);
@@ -116,23 +148,24 @@ void main(void)
k_sleep(100);
}

printf("DTR set, start test\n");
LOG_DBG("DTR set, start test");

uart_line_set(dev0);
uart_line_set(dev1);

dev_data0->dev = dev0;
dev_data0->peer = dev1;
dev_data0->peer_data = dev_data1;
ring_buf_init(&dev_data0->ringbuf, sizeof(buffer0), buffer0);

dev_data1->dev = dev1;
dev_data1->peer = dev0;
dev_data1->peer_data = dev_data0;
ring_buf_init(&dev_data1->ringbuf, sizeof(buffer1), buffer1);

uart_irq_callback_user_data_set(dev0, interrupt_handler, dev_data0);
uart_irq_callback_user_data_set(dev1, interrupt_handler, dev_data1);

write_data(dev0, banner, strlen(banner));
write_data(dev1, banner, strlen(banner));

/* Enable rx interrupts */
uart_irq_rx_enable(dev0);
uart_irq_rx_enable(dev1);

0 comments on commit 88145db

Please sign in to comment.
You can’t perform that action at this time.