samples: wpan_serial: Refactor wpan_serial
Refactor code simplifying packet processing, removing unneeded semaphore, etc. Signed-off-by: Andrei Emeltchenko <andrei.emeltchenko@intel.com>
This commit is contained in:
parent
292efefaf3
commit
d78a06428a
2 changed files with 34 additions and 66 deletions
|
@ -18,3 +18,5 @@ CONFIG_NET_BUF_DATA_SIZE=128
|
||||||
CONFIG_NET_CONFIG_SETTINGS=y
|
CONFIG_NET_CONFIG_SETTINGS=y
|
||||||
CONFIG_NET_CONFIG_AUTO_INIT=n
|
CONFIG_NET_CONFIG_AUTO_INIT=n
|
||||||
CONFIG_NET_LOG=y
|
CONFIG_NET_LOG=y
|
||||||
|
|
||||||
|
CONFIG_TEST_RANDOM_GENERATOR=y
|
||||||
|
|
|
@ -13,20 +13,13 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <logging/log.h>
|
#include <logging/log.h>
|
||||||
LOG_MODULE_REGISTER(net_wpan_serial_sample, LOG_LEVEL_DBG);
|
LOG_MODULE_REGISTER(wpan_serial, CONFIG_USB_DEVICE_LOG_LEVEL);
|
||||||
|
|
||||||
#include <string.h>
|
#include <uart.h>
|
||||||
#include <device.h>
|
|
||||||
#include <drivers/uart.h>
|
|
||||||
#include <zephyr.h>
|
#include <zephyr.h>
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
#include <sys/printk.h>
|
|
||||||
|
|
||||||
#include <net/buf.h>
|
#include <net/buf.h>
|
||||||
|
|
||||||
#include <net_private.h>
|
#include <net_private.h>
|
||||||
|
|
||||||
#include <net/ieee802154_radio.h>
|
#include <net/ieee802154_radio.h>
|
||||||
|
|
||||||
#define SLIP_END 0300
|
#define SLIP_END 0300
|
||||||
|
@ -46,7 +39,6 @@ static K_THREAD_STACK_DEFINE(rx_stack, 1024);
|
||||||
static struct k_thread rx_thread_data;
|
static struct k_thread rx_thread_data;
|
||||||
|
|
||||||
/* TX queue */
|
/* TX queue */
|
||||||
static struct k_sem tx_sem;
|
|
||||||
static struct k_fifo tx_queue;
|
static struct k_fifo tx_queue;
|
||||||
static K_THREAD_STACK_DEFINE(tx_stack, 1024);
|
static K_THREAD_STACK_DEFINE(tx_stack, 1024);
|
||||||
static struct k_thread tx_thread_data;
|
static struct k_thread tx_thread_data;
|
||||||
|
@ -136,23 +128,11 @@ static int slip_process_byte(unsigned char c)
|
||||||
static void interrupt_handler(struct device *dev)
|
static void interrupt_handler(struct device *dev)
|
||||||
{
|
{
|
||||||
while (uart_irq_update(dev) && uart_irq_is_pending(dev)) {
|
while (uart_irq_update(dev) && uart_irq_is_pending(dev)) {
|
||||||
#ifdef VERBOSE_DEBUG
|
|
||||||
LOG_DBG("");
|
|
||||||
#endif
|
|
||||||
if (uart_irq_tx_ready(dev)) {
|
|
||||||
#ifdef VERBOSE_DEBUG
|
|
||||||
LOG_DBG("TX ready interrupt");
|
|
||||||
#endif
|
|
||||||
|
|
||||||
k_sem_give(&tx_sem);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (uart_irq_rx_ready(dev)) {
|
|
||||||
unsigned char byte;
|
unsigned char byte;
|
||||||
|
|
||||||
#ifdef VERBOSE_DEBUG
|
if (!uart_irq_rx_ready(dev)) {
|
||||||
LOG_DBG("RX ready interrupt");
|
continue;
|
||||||
#endif
|
}
|
||||||
|
|
||||||
while (uart_fifo_read(dev, &byte, sizeof(byte))) {
|
while (uart_fifo_read(dev, &byte, sizeof(byte))) {
|
||||||
if (slip_process_byte(byte)) {
|
if (slip_process_byte(byte)) {
|
||||||
|
@ -166,14 +146,14 @@ static void interrupt_handler(struct device *dev)
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
LOG_DBG("Full packet %p", pkt_curr);
|
LOG_DBG("Full packet %p, len %u", pkt_curr,
|
||||||
|
net_pkt_get_len(pkt_curr));
|
||||||
|
|
||||||
k_fifo_put(&rx_queue, pkt_curr);
|
k_fifo_put(&rx_queue, pkt_curr);
|
||||||
pkt_curr = NULL;
|
pkt_curr = NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Allocate and send data to USB Host */
|
/* Allocate and send data to USB Host */
|
||||||
|
@ -282,7 +262,7 @@ static void process_data(struct net_pkt *pkt)
|
||||||
|
|
||||||
static void set_channel(u8_t chan)
|
static void set_channel(u8_t chan)
|
||||||
{
|
{
|
||||||
LOG_DBG("Set channel %c", chan);
|
LOG_DBG("Set channel %u", chan);
|
||||||
|
|
||||||
radio_api->set_channel(ieee802154_dev, chan);
|
radio_api->set_channel(ieee802154_dev, chan);
|
||||||
}
|
}
|
||||||
|
@ -308,9 +288,9 @@ static void process_config(struct net_pkt *pkt)
|
||||||
|
|
||||||
static void rx_thread(void)
|
static void rx_thread(void)
|
||||||
{
|
{
|
||||||
LOG_INF("RX thread started");
|
LOG_DBG("RX thread started");
|
||||||
|
|
||||||
while (1) {
|
while (true) {
|
||||||
struct net_pkt *pkt;
|
struct net_pkt *pkt;
|
||||||
struct net_buf *buf;
|
struct net_buf *buf;
|
||||||
u8_t specifier;
|
u8_t specifier;
|
||||||
|
@ -318,7 +298,7 @@ static void rx_thread(void)
|
||||||
pkt = k_fifo_get(&rx_queue, K_FOREVER);
|
pkt = k_fifo_get(&rx_queue, K_FOREVER);
|
||||||
buf = net_buf_frag_last(pkt->buffer);
|
buf = net_buf_frag_last(pkt->buffer);
|
||||||
|
|
||||||
LOG_DBG("Got pkt %p buf %p", pkt, buf);
|
LOG_DBG("rx_queue pkt %p buf %p", pkt, buf);
|
||||||
|
|
||||||
LOG_HEXDUMP_DBG(buf->data, buf->len, "SLIP >");
|
LOG_HEXDUMP_DBG(buf->data, buf->len, "SLIP >");
|
||||||
|
|
||||||
|
@ -337,8 +317,6 @@ static void rx_thread(void)
|
||||||
}
|
}
|
||||||
|
|
||||||
net_pkt_unref(pkt);
|
net_pkt_unref(pkt);
|
||||||
|
|
||||||
k_yield();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -399,16 +377,11 @@ static void tx_thread(void)
|
||||||
{
|
{
|
||||||
LOG_DBG("TX thread started");
|
LOG_DBG("TX thread started");
|
||||||
|
|
||||||
/* Allow to send one TX */
|
while (true) {
|
||||||
k_sem_give(&tx_sem);
|
|
||||||
|
|
||||||
while (1) {
|
|
||||||
struct net_pkt *pkt;
|
struct net_pkt *pkt;
|
||||||
struct net_buf *buf;
|
struct net_buf *buf;
|
||||||
size_t len;
|
size_t len;
|
||||||
|
|
||||||
k_sem_take(&tx_sem, K_FOREVER);
|
|
||||||
|
|
||||||
pkt = k_fifo_get(&tx_queue, K_FOREVER);
|
pkt = k_fifo_get(&tx_queue, K_FOREVER);
|
||||||
buf = net_buf_frag_last(pkt->buffer);
|
buf = net_buf_frag_last(pkt->buffer);
|
||||||
len = net_pkt_get_len(pkt);
|
len = net_pkt_get_len(pkt);
|
||||||
|
@ -426,10 +399,6 @@ static void tx_thread(void)
|
||||||
try_write(slip_buf, len);
|
try_write(slip_buf, len);
|
||||||
|
|
||||||
net_pkt_unref(pkt);
|
net_pkt_unref(pkt);
|
||||||
|
|
||||||
#if 0
|
|
||||||
k_yield();
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -445,7 +414,6 @@ static void init_rx_queue(void)
|
||||||
|
|
||||||
static void init_tx_queue(void)
|
static void init_tx_queue(void)
|
||||||
{
|
{
|
||||||
k_sem_init(&tx_sem, 0, UINT_MAX);
|
|
||||||
k_fifo_init(&tx_queue);
|
k_fifo_init(&tx_queue);
|
||||||
|
|
||||||
k_thread_create(&tx_thread_data, tx_stack,
|
k_thread_create(&tx_thread_data, tx_stack,
|
||||||
|
@ -521,7 +489,7 @@ static bool init_ieee802154(void)
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef CONFIG_NET_CONFIG_SETTINGS
|
#ifdef CONFIG_NET_CONFIG_SETTINGS
|
||||||
LOG_INF("Set channel %x", CONFIG_NET_CONFIG_IEEE802154_CHANNEL);
|
LOG_INF("Set channel %u", CONFIG_NET_CONFIG_IEEE802154_CHANNEL);
|
||||||
radio_api->set_channel(ieee802154_dev,
|
radio_api->set_channel(ieee802154_dev,
|
||||||
CONFIG_NET_CONFIG_IEEE802154_CHANNEL);
|
CONFIG_NET_CONFIG_IEEE802154_CHANNEL);
|
||||||
#endif /* CONFIG_NET_CONFIG_SETTINGS */
|
#endif /* CONFIG_NET_CONFIG_SETTINGS */
|
||||||
|
@ -534,8 +502,7 @@ static bool init_ieee802154(void)
|
||||||
|
|
||||||
int net_recv_data(struct net_if *iface, struct net_pkt *pkt)
|
int net_recv_data(struct net_if *iface, struct net_pkt *pkt)
|
||||||
{
|
{
|
||||||
LOG_DBG("Got data, pkt %p, frags->len %d",
|
LOG_DBG("Received pkt %p, len %d", pkt, net_pkt_get_len(pkt));
|
||||||
pkt, net_pkt_get_len(pkt));
|
|
||||||
|
|
||||||
k_fifo_put(&tx_queue, pkt);
|
k_fifo_put(&tx_queue, pkt);
|
||||||
|
|
||||||
|
@ -548,6 +515,8 @@ void main(void)
|
||||||
u32_t baudrate, dtr = 0U;
|
u32_t baudrate, dtr = 0U;
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
|
LOG_INF("Starting wpan_serial application");
|
||||||
|
|
||||||
dev = device_get_binding("CDC_ACM_0");
|
dev = device_get_binding("CDC_ACM_0");
|
||||||
if (!dev) {
|
if (!dev) {
|
||||||
LOG_ERR("CDC ACM device not found");
|
LOG_ERR("CDC ACM device not found");
|
||||||
|
@ -569,9 +538,9 @@ void main(void)
|
||||||
|
|
||||||
ret = uart_line_ctrl_get(dev, UART_LINE_CTRL_BAUD_RATE, &baudrate);
|
ret = uart_line_ctrl_get(dev, UART_LINE_CTRL_BAUD_RATE, &baudrate);
|
||||||
if (ret) {
|
if (ret) {
|
||||||
printk("Failed to get baudrate, ret code %d\n", ret);
|
LOG_WRN("Failed to get baudrate, ret code %d", ret);
|
||||||
} else {
|
} else {
|
||||||
printk("Baudrate detected: %d\n", baudrate);
|
LOG_DBG("Baudrate detected: %d", baudrate);
|
||||||
}
|
}
|
||||||
|
|
||||||
LOG_INF("USB serial initialized");
|
LOG_INF("USB serial initialized");
|
||||||
|
@ -595,7 +564,4 @@ void main(void)
|
||||||
|
|
||||||
/* Enable rx interrupts */
|
/* Enable rx interrupts */
|
||||||
uart_irq_rx_enable(dev);
|
uart_irq_rx_enable(dev);
|
||||||
|
|
||||||
/* Enable tx interrupts */
|
|
||||||
uart_irq_tx_enable(dev);
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue