前面我们实现了异步传输的demo,能够进行数据收发测试。但是还不够,现在的实现不方便应用层使用。对于应用层来说只需要启动,关闭,读,写这几个接口,无需关心USB相关的逻辑。使用FIFO来实现应用层和底层驱动的解耦是一个不错的方式,我们前面也有系列文章分享了FIFO的实现见:https://mp.weixin.qq.com/s/MvL9eDesyuxD60fnbl1nag
这一篇我们就在上一篇基础上增加FIFO,实现底层和应用的解耦,最后将我们之前设计的GUI和现在的框架合并起来,实现更加高效好用的最终版本。
我们在上一篇框图基础上修改
提供给应用层仅4个接口
usbdev_run
usbdev_stop 控制设备启动和停止,即控制数据收发线程的状态,现在收发线程默认启动就按照默认参数打开设备接口,进行端点的收发,通过这两个接口控制数据收发的启动和停止。今后还可以继续优化,数据收发线程维护一个状态机,划分为更多更细的状态,可以进行更多更细致的控制,比如打开某个接口,控制某个端点的收发,端点收发的启动停止,传输次数等。
而应用层往USB发送数据只要调用usbdev_write接口往TX_FIFO写数据即可,数据收发线程自动根据当前状态从TX_FIFO读出数据进行发送。
接收和上述相反,数据收发线程根据状态进行USB接收,接收到数据后在事件回调中将数据写入RX_FIFO中,应用层只需要调用usbdev_read读RX_FIFO即可。
以上就实现了应用层和底层的接口,接口很简单,应用非常方便。
新增usbdev_fifo.c/usbdev_fifo.h实现以上tx和rx的fifo实例。
而fifo.c/fifo.h是完全可移植的fifo实现代码,参考之前的文章。
usbd_cfg.h定义一些参数
extern "C" {
}
usbdev.c
static void* usb_event_thread(void *arg); /* USB事件线程处理函数 */
static void* usb_handle_thread(void *arg); /* USB业务线程处理函数 */
pthread_t s_usb_event_thread; /* USB事件处理线程句柄 */
pthread_t s_usb_handle_thread; /* USB业务处理线程句柄 */
libusb_device_handle *s_opened_handle = NULL; /* USB打开的设备句柄 */
struct libusb_transfer* s_tx_transfer = NULL; /* 发送传输 */
struct libusb_transfer* s_rx_transfer = NULL; /* 接收传输 */
static uint8_t s_tx_buffer[TRANSFER_SIZE]; /* 发送数据 */
static uint8_t s_rx_buffer[TRANSFER_SIZE]; /* 接收数据 */
static int s_tx_busy = 0; /* 发送忙标志 */
static int s_rx_busy = 0; /* 接收忙标志 */
sem_t s_sem;
int16_t vid = VID;
int16_t pid = PID;
int16_t itf = USB_ITF_ID;
int16_t in_ep = USB_IN_EP;
int16_t out_ep = USB_OUT_EP;
static usbdev_state_e s_usbdev_state = USBDEV_STATE_INITING;
int usbdev_run(void)
{
int r;
s_usbdev_state = USBDEV_STATE_INITING;
usbdev_fifo_init();
r = libusb_init_context(/*ctx=*/NULL, /*options=*/NULL, /*num_options=*/0);
if (r < 0)
{
//printf("failed to init context %d\r\n",r);
usbdev_fifo_deinit();
s_usbdev_state = USBDEV_STATE_STOPED;
return r;
}
s_opened_handle = libusb_open_device_with_vid_pid(NULL, vid, pid);
if (s_opened_handle == NULL)
{
//printf("open dev err\r\n");
libusb_exit(NULL);
usbdev_fifo_deinit();
s_usbdev_state = USBDEV_STATE_STOPED;
return r;
}
r = libusb_claim_interface(s_opened_handle,itf);
if (r < 0)
{
//printf("failed to claim interface %d\r\n",r);
libusb_close(s_opened_handle);
libusb_exit(NULL);
usbdev_fifo_deinit();
s_usbdev_state = USBDEV_STATE_STOPED;
return r;
}
sem_init(&s_sem, 0, 0);
/* 创建usb事件处理线程 */
r = pthread_create(&s_usb_event_thread,0,usb_event_thread,0);
if (r != 0)
{
//printf("failed to create usb event thread:%d\r\n",r);
libusb_release_interface(s_opened_handle,itf);
libusb_close(s_opened_handle);
libusb_exit(NULL);
usbdev_fifo_deinit();
s_usbdev_state = USBDEV_STATE_STOPED;
return r;
}
/* 创建usb业务处理线程 */
r = pthread_create(&s_usb_handle_thread,0,usb_handle_thread,0);
if (r != 0)
{
//printf("failed to create usb handle thread:%d\r\n",r);
libusb_release_interface(s_opened_handle,itf);
libusb_close(s_opened_handle);
libusb_exit(NULL);
usbdev_fifo_deinit();
s_usbdev_state = USBDEV_STATE_STOPED;
return r;
}
s_usbdev_state = USBDEV_STATE_RUNING;
/* 等待线程结束 */
void *res;
pthread_join(s_usb_event_thread,&res);
pthread_join(s_usb_handle_thread,&res);
sem_destroy(&s_sem);
libusb_release_interface(s_opened_handle,USB_ITF_ID);
libusb_close(s_opened_handle);
libusb_exit(NULL);
usbdev_fifo_deinit();
s_usbdev_state = USBDEV_STATE_STOPED;
return 0;
}
void tx_cb(struct libusb_transfer *transfer)
{
if (transfer->status == LIBUSB_TRANSFER_COMPLETED)
{
/* 成功 */
//printf("tx_cb ok\r\n");
}
else
{
/* 失败 */
//printf("tx_cb err %d\r\n",transfer->status);
//libusb_submit_transfer(transfer);
}
libusb_free_transfer(transfer);
s_tx_busy = 0;
}
void rx_cb(struct libusb_transfer *transfer)
{
if (transfer->status == LIBUSB_TRANSFER_COMPLETED)
{
/* 成功 */
//printf("rx_cb ok\r\n");
}
else
{
/* 失败 */
//printf("rx_cb err %d\r\n",transfer->status);
//libusb_submit_transfer(transfer);
}
if(transfer->actual_length > 0)
{
//printf("rx len %d\r\n",transfer->actual_length);
usbdev_rx_fifo_put(0, s_rx_buffer, transfer->actual_length);
}
libusb_free_transfer(transfer);
s_rx_busy = 0;
}
static void* usb_event_thread(void *arg)
{
while(1)
{
if(0 == sem_trywait(&s_sem))
{
return 0;
}
libusb_handle_events(0);
const struct timespec interval=
{
.tv_nsec = 1000000,
.tv_sec = 0,
};
pthread_delay_np(&interval);
}
return 0;
}
static void* usb_handle_thread(void *arg)
{
while(1)
{
int rc;
/* 发送处理 */
uint32_t len;
if(s_tx_busy == 0)
{
len = usbdev_tx_fifo_get(0, s_tx_buffer, sizeof(s_tx_buffer));
if(len > 0)
{
s_tx_busy = 1;
s_tx_transfer = libusb_alloc_transfer(0);
libusb_fill_bulk_transfer(s_tx_transfer,s_opened_handle,out_ep,s_tx_buffer,len,&tx_cb,0,100);
rc = libusb_submit_transfer(s_tx_transfer);
if(rc < 0)
{
s_tx_busy = 0;
libusb_free_transfer(s_tx_transfer);
s_tx_transfer = 0;
}
}
}
/* 接收处理 */
if(s_rx_busy == 0)
{
s_rx_busy = 1;
s_rx_transfer = libusb_alloc_transfer(0);
libusb_fill_bulk_transfer(s_rx_transfer,s_opened_handle,in_ep,s_rx_buffer,sizeof(s_rx_buffer),&rx_cb,0,100);
rc = libusb_submit_transfer(s_rx_transfer);
if(rc < 0)
{
s_rx_busy = 0;
libusb_free_transfer(s_rx_transfer);
s_rx_transfer = 0;
}
}
const struct timespec interval=
{
.tv_nsec = 1000000,
.tv_sec = 0,
};
pthread_delay_np(&interval);
}
return 0;
}
int usbdev_stop(void)
{
sem_post(&s_sem);
}
int usbdev_write(int id, uint8_t* buffer, uint32_t len)
{
return usbdev_tx_fifo_put(id, buffer, len);
}
int usbdev_read(int id, uint8_t* buffer, uint32_t len)
{
return usbdev_rx_fifo_get(id, buffer, len);
}
usbdev_state_e usbdev_state(void)
{
return s_usbdev_state;
}
ubsdev.h
extern "C" {
typedef enum
{
USBDEV_STATE_INITING = 0,
USBDEV_STATE_RUNING = 1,
USBDEV_STATE_STOPED = 2,
} usbdev_state_e;
int usbdev_run(void);
int usbdev_write(int id, uint8_t* buffer, uint32_t len);
int usbdev_read(int id, uint8_t* buffer, uint32_t len);
int usbdev_stop(void);
usbdev_state_e usbdev_state(void);
}
fifo.c
#include
#include "fifo.h"
/**
* in为写入索引 0~(buffer_len-1)。
* out为读出索引 0~(buffer_len-1)。
* in == out时可能是满,也可能是空,可以通过len有效数据长度来确认。
* 写数据in增加,直到追赶到out则满。
* 读数据则out增加,直到追赶到in则空。
* in大于out时则[out,in)区间是有效数据。
* in小于out时则[out,buffer_len)和[0,in)区间是有效数据。
***********************************************************
* 0 buffer_len-1 buffer_len
* (1)开始 in和out都是0
* | |
* in(0)
* out(0)
* len = 0
* (2)写入n字节数据 in变为n和out还是0 对应in大于out的情况
* | |
* out(0)————————————>in(n) |
* len = n
* (3)读出m字节数据(m
* | |
* out(m)————>in(n)
* len = n-m
* (4)继续写入数据,绕回到开头,对应in小于out的情况
* | |
* out(m)————————————————————————————————>
* ——>in(k)
* len = k + buffer_len-m
*/
uint32_t fifo_in(fifo_st* dev, uint8_t* buffer, uint32_t len)
{
uint32_t space = 0; /* 用于记录空闲空间大小 */
#if FIFO_PARAM_CHECK
/* 参数检查 */
if((dev == 0) || (buffer == 0) || (len == 0))
{
return 0;
}
if(dev->buffer == 0)
{
return 0;
}
#endif
#if FIFO_SUPPORT_LOCK
if(dev->mutex_lock != 0)
{
dev->mutex_lock(dev->mutex);
}
#endif
/* 限制len的最大长度为buffer大小 */
if(len > dev->buffer_len)
{
len = dev->buffer_len;
}
/* 计算空闲空间大小
* 正常dev->len不应该大于dev->buffer_len
*/
if(dev->buffer_len >= dev->len)
{
space = dev->buffer_len - dev->len;
}
else
{
/* 这里不应该出现, 出现则是异常 */
dev->len = 0;
space = dev->buffer_len;
}
/* 计算待写入大小, 如果len大于剩余空间则只写入剩余空间大小 */
len = (len >= space) ? space : len;
if(len == 0)
{
#if FIFO_SUPPORT_LOCK
if(dev->mutex_unlock != 0)
{
dev->mutex_unlock(dev->mutex);
}
#endif
return 0; /* 这里有可能无剩余空间,直接返回 */
}
/* 计算len的长度是否需要有绕回,需要分次写入 */
space = dev->buffer_len - dev->in; /* 当前写入位置in到缓存末尾剩余可写入空间 */
if(space >= len)
{
/* 当前写入位置in到缓存末尾足够一次写入 */
memcpy(dev->buffer+dev->in,buffer,len);
}
else
{
/* 当前写入位置in到缓存末尾不够,还需要绕回到前面写 */
memcpy(dev->buffer+dev->in,buffer,space); /* 先写入tail部分 */
memcpy(dev->buffer,buffer+space,len-space); /* 再写入绕回头部分 */
}
/* 更新写入索引和有效数据长度 */
dev->in += len;
if(dev->in >= dev->buffer_len)
{
dev->in -= dev->buffer_len; /* 判断加减法 替代 dev->in %= dev->buffer->len */
}
dev->len += len; /* dev->len最大dev->buffer->len,无需%= dev->buffer->len */
#if FIFO_SUPPORT_LOCK
if(dev->mutex_unlock != 0)
{
dev->mutex_unlock(dev->mutex);
}
#endif
return len;
}
uint32_t fifo_out(fifo_st* dev, uint8_t* buffer, uint32_t len)
{
uint32_t space = 0;
#if FIFO_PARAM_CHECK
/* 参数检查 */
if((dev == 0) || (buffer == 0) || (len == 0))
{
return 0;
}
if(dev->buffer == 0)
{
return 0;
}
#endif
#if FIFO_SUPPORT_LOCK
if(dev->mutex_lock != 0)
{
dev->mutex_lock(dev->mutex);
}
#endif
/* 判断是否有数据 */
if(dev->len == 0)
{
#if FIFO_SUPPORT_LOCK
if(dev->mutex_unlock != 0)
{
dev->mutex_unlock(dev->mutex);
}
#endif
return 0;
}
/* 可读出数据量取需要的和有的之间的小值 */
len = (dev->len) > len ? len : dev->len;
/* 计算len的长度是否需要有绕回,需要分次读出 */
space = dev->buffer_len - dev->out; /* 当前读出位置out到缓存末尾剩余可读出空间 */
if(space >= len)
{
/* 当前读出位置out到缓存末尾足够一次读出 */
memcpy(buffer,dev->buffer+dev->out,len);
}
else
{
/* 当前读出位置out到缓存末尾不够,还需要绕回到前面读 */
memcpy(buffer,dev->buffer+dev->out,space); /* 先读出tail部分 */
memcpy(buffer+space,dev->buffer,len-space); /* 再读出绕回头部分 */
}
/* 更新读出索引和有效数据长度 */
dev->out += len;
if(dev->out >= dev->buffer_len)
{
dev->out -= dev->buffer_len; /* 判断加减法 替代 dev->out %= dev->buffer->len */
}
dev->len -= len; /* 这里dev->len 不可能小于len,不会溢出 */
#if FIFO_SUPPORT_LOCK
if(dev->mutex_unlock != 0)
{
dev->mutex_unlock(dev->mutex);
}
#endif
return len;
}
uint32_t fifo_get_len(fifo_st* dev)
{
uint32_t len;
#if FIFO_PARAM_CHECK
/* 参数检查 */
if(dev == 0)
{
return -1;
}
#endif
#if FIFO_SUPPORT_LOCK
if(dev->mutex_lock != 0)
{
dev->mutex_lock(dev->mutex);
}
#endif
len = dev->len;
#if FIFO_SUPPORT_LOCK
if(dev->mutex_unlock != 0)
{
dev->mutex_unlock(dev->mutex);
}
#endif
return len;
}
int fifo_init(fifo_st* dev)
{
#if FIFO_PARAM_CHECK
/* 参数检查 */
if(dev == 0)
{
return -1;
}
#endif
#if FIFO_SUPPORT_LOCK
if(dev->mutex_init != 0)
{
dev->mutex_init(dev->mutex);
}
#endif
return 0;
}
int fifo_deinit(fifo_st* dev)
{
#if FIFO_PARAM_CHECK
/* 参数检查 */
if(dev == 0)
{
return -1;
}
#endif
#if FIFO_SUPPORT_LOCK
if(dev->mutex_destroy != 0)
{
dev->mutex_destroy(dev->mutex);
}
#endif
return 0;
}
fifo.h
extern "C" {
/**
* \struct fifo_st
* FIFO缓冲区结构.
*/
typedef struct
{
uint32_t in; /**< 写入索引 */
uint32_t out; /**< 读出索引 */
uint32_t len; /**< 有效数据长度 */
uint32_t buffer_len; /**< 有效长度 */
uint8_t* buffer; /**< 缓存,用户分配 */
/* 以下用于临界段管理 */
void* mutex; /**< 互斥量 */
void (*mutex_init)(void* mutex); /**< 互斥量初始化 */
void (*mutex_destroy)(void* mutex); /**< 删除互斥量 */
void (*mutex_lock)(void* mutex); /**< 获取互斥量 */
void (*mutex_unlock)(void* mutex); /**< 释放互斥量 */
} fifo_st;
/**
* \fn fifo_in
* 往fifo里写数据
* \param[in] dev \ref fifo_st
* \param[in] buffer 待写入的数据
* \param[in] len 待写入的长度
* \retval 返回实际写入的数据量
*/
uint32_t fifo_in(fifo_st* dev, uint8_t* buffer, uint32_t len);
/**
* \fn fifo_get_len
* 获取fifo中有效数据长度
* \param[in] dev \ref fifo_st
* \return uint32_t 数据长度
*/
uint32_t fifo_get_len(fifo_st* dev);
/**
* \fn fifo_out
* 从fifo读出数据
* \param[in] dev \ref fifo_st
* \param[in] buffer 存读出的数据
* \param[in] len 需要读出的数据长度
* \retval 返回实际读出的数据量
*/
uint32_t fifo_out(fifo_st* dev, uint8_t* buffer, uint32_t len);
/**
* \fn fifo_init
* 初始化fifo
* \param[in] dev \ref fifo_st
* \retval 0 成功
* \retval 其他值失败
*/
int fifo_init(fifo_st* dev);
/**
* \fn fifo_deinit
* 解除初始化fifo
* \param[in] dev \ref fifo_st
* \retval 0 成功
* \retval 其他值失败
*/
int fifo_deinit(fifo_st* dev);
}
usbdev_fifo.c
typedef CRITICAL_SECTION fifo_mutex_t;
static fifo_mutex_t s_fifo_tx_mutex[TX_FIFO_NUM];
static fifo_mutex_t s_fifo_rx_mutex[RX_FIFO_NUM];
static inline void fifo_mutex_init(fifo_mutex_t *mutex)
{
InitializeCriticalSection(mutex);
}
static inline void fifo_mutex_lock(fifo_mutex_t *mutex)
{
EnterCriticalSection(mutex);
}
static inline void fifo_mutex_unlock(fifo_mutex_t *mutex)
{
LeaveCriticalSection(mutex);
}
static inline void fifo_mutex_destroy(fifo_mutex_t *mutex)
{
DeleteCriticalSection(mutex);
}
static fifo_st s_tx_fifo[TX_FIFO_NUM] =
{
{
.buffer = 0,
.buffer_len = USBDEV_TX_FIFO_MAX_SIZE,
.in = 0,
.len = 0,
.out = 0,
.mutex = &s_fifo_tx_mutex,
.mutex_init = fifo_mutex_init,
.mutex_destroy = fifo_mutex_destroy,
.mutex_lock = fifo_mutex_lock,
.mutex_unlock = fifo_mutex_unlock,
},
{
.buffer = 0,
.buffer_len = USBDEV_TX_FIFO_MAX_SIZE,
.in = 0,
.len = 0,
.out = 0,
.mutex = &s_fifo_tx_mutex,
.mutex_init = fifo_mutex_init,
.mutex_destroy = fifo_mutex_destroy,
.mutex_lock = fifo_mutex_lock,
.mutex_unlock = fifo_mutex_unlock,
}
};
static fifo_st s_rx_fifo[RX_FIFO_NUM] =
{
{
.buffer = 0,
.buffer_len = USBDEV_RX_FIFO_MAX_SIZE,
.in = 0,
.len = 0,
.out = 0,
.mutex = &s_fifo_rx_mutex,
.mutex_init = fifo_mutex_init,
.mutex_destroy = fifo_mutex_destroy,
.mutex_lock = fifo_mutex_lock,
.mutex_unlock = fifo_mutex_unlock,
},
{
.buffer = 0,
.buffer_len = USBDEV_RX_FIFO_MAX_SIZE,
.in = 0,
.len = 0,
.out = 0,
.mutex = &s_fifo_rx_mutex,
.mutex_init = fifo_mutex_init,
.mutex_destroy = fifo_mutex_destroy,
.mutex_lock = fifo_mutex_lock,
.mutex_unlock = fifo_mutex_unlock,
}
};
void usbdev_fifo_init(void)
{
for(int i=0; i
{
if(s_tx_fifo[i].buffer == 0)
{
s_tx_fifo[i].buffer = malloc(USBDEV_TX_FIFO_MAX_SIZE);
}
fifo_init(&(s_tx_fifo[i]));
}
for(int i=0; i
{
if(s_rx_fifo[i].buffer == 0)
{
s_rx_fifo[i].buffer = malloc(USBDEV_RX_FIFO_MAX_SIZE);
}
fifo_init(&(s_rx_fifo[i]));
}
}
void usbdev_fifo_deinit(void)
{
for(int i=0; i
{
if(s_tx_fifo[i].buffer != 0)
{
free(s_tx_fifo[i].buffer);
}
fifo_deinit(&(s_tx_fifo[i]));
}
for(int i=0; i
{
if(s_rx_fifo[i].buffer != 0)
{
free(s_rx_fifo[i].buffer);
}
fifo_deinit(&(s_rx_fifo[i]));
}
}
uint32_t usbdev_tx_fifo_put(int i, uint8_t* buffer, uint32_t len)
{
return fifo_in(&(s_tx_fifo[i]),buffer,len);
}
uint32_t usbdev_tx_fifo_get(int i, uint8_t* buffer, uint32_t len)
{
return fifo_out(&(s_tx_fifo[i]),buffer,len);
}
uint32_t usbdev_rx_fifo_put(int i, uint8_t* buffer, uint32_t len)
{
return fifo_in(&(s_rx_fifo[i]),buffer,len);
}
uint32_t usbdev_rx_fifo_get(int i, uint8_t* buffer, uint32_t len)
{
return fifo_out(&(s_rx_fifo[i]),buffer,len);
}
uint32_t usbdev_rx_fifo_datalen(int i)
{
return fifo_get_len(&(s_rx_fifo[i]));
}
usbdev_fifo.h
extern "C" {
void usbdev_fifo_init(void);
void usbdev_fifo_deinit(void);
uint32_t usbdev_tx_fifo_put(int i,uint8_t* buffer, uint32_t len);
uint32_t usbdev_tx_fifo_get(int i,uint8_t* buffer, uint32_t len);
uint32_t usbdev_rx_fifo_put(int i,uint8_t* buffer, uint32_t len);
uint32_t usbdev_rx_fifo_get(int i,uint8_t* buffer, uint32_t len);
uint32_t usbdev_rx_fifo_datalen(int i);
}
测试代码usbdev_test.c如下
pthread_t s_test_thread;
static uint8_t s_tx_buffer[TRANSFER_SIZE]; /* 发送数据 */
static uint8_t s_rx_buffer[TRANSFER_SIZE]; /* 接收数据 */
void delay(uint32_t ms)
{
const struct timespec interval=
{
.tv_nsec = ms*1000,
.tv_sec = 0,
};
pthread_delay_np(&interval);
}
static void* test_thread(void *arg)
{
if(0 != usbdev_run())
{
printf("usbdev_run err%d\r\n");
return 0;
}
}
int usbdev_test_run(void)
{
for(size_t i=0; i<sizeof(s_tx_buffer); i++)
{
s_tx_buffer[i] = i;
}
uint32_t rx_len = 0;
int r = pthread_create(&s_test_thread,0,test_thread,0);
if (r != 0)
{
printf("failed to create test thread:%d\r\n",r);
return r;
}
while(usbdev_state() != USBDEV_STATE_RUNING)
{
delay(10);
}
while(1)
{
int len;
len = usbdev_write(0, s_tx_buffer, sizeof(s_tx_buffer));
len = usbdev_read(0, s_rx_buffer, sizeof(s_rx_buffer));
if(len > 0)
{
rx_len += len;
printf("get len:%d\r\n",len);
}
delay(100);
if(rx_len >= 1024)
{
break;
}
}
usbdev_stop();
printf("test done\r\n");
}
usbdev_test.h如下
extern "C" {
int usbdev_test_run(void);
}
main.cpp中调用该函数即可
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
usbdev_test_run();
return a.exec();
}
测试结果如下:
接收1024字节后退出
以上基于fifo实现了底层和应用层的解耦,方便应用层调用,后面就可以基于此合并之前gui框架,实现新版本的测试工具了。