add tusbd_cdc_is_busy
enhance cdc serial device demo refractor fifo.c
This commit is contained in:
@@ -39,6 +39,9 @@
|
||||
#include "cdcd_app.h"
|
||||
|
||||
#if TUSB_CFG_DEVICE_CDC
|
||||
|
||||
#include "common/fifo.h" // TODO refractor
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// INCLUDE
|
||||
//--------------------------------------------------------------------+
|
||||
@@ -54,18 +57,16 @@ OSAL_SEM_DEF(cdcd_semaphore);
|
||||
|
||||
static osal_semaphore_handle_t sem_hdl;
|
||||
|
||||
static uint8_t cdcd_app_recv_buffer[CDCD_APP_BUFFER_SIZE] TUSB_CFG_ATTR_USBRAM;
|
||||
static uint8_t cdcd_app_send_buffer[CDCD_APP_BUFFER_SIZE] TUSB_CFG_ATTR_USBRAM;
|
||||
|
||||
static uint16_t received_bytes; // set by transfer complete callback
|
||||
static uint8_t serial_rx_buffer[CDCD_APP_BUFFER_SIZE] TUSB_CFG_ATTR_USBRAM;
|
||||
static uint8_t serial_tx_buffer[CDCD_APP_BUFFER_SIZE] TUSB_CFG_ATTR_USBRAM;
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// INTERNAL OBJECT & FUNCTION DECLARATION
|
||||
//--------------------------------------------------------------------+
|
||||
FIFO_DEF(fifo_serial, CDCD_APP_BUFFER_SIZE, uint8_t, true);
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// IMPLEMENTATION
|
||||
//--------------------------------------------------------------------+
|
||||
void cdcd_serial_app_init(void)
|
||||
{
|
||||
sem_hdl = osal_semaphore_create( OSAL_SEM_REF(cdcd_semaphore) );
|
||||
@@ -78,8 +79,7 @@ void tusbd_cdc_mounted_cb(uint8_t coreid)
|
||||
{
|
||||
osal_semaphore_reset(sem_hdl);
|
||||
|
||||
received_bytes = 0;
|
||||
tusbd_cdc_receive(coreid, cdcd_app_recv_buffer, CDCD_APP_BUFFER_SIZE, true);
|
||||
tusbd_cdc_receive(coreid, serial_rx_buffer, CDCD_APP_BUFFER_SIZE, true);
|
||||
}
|
||||
|
||||
void tusbd_cdc_xfer_isr(uint8_t coreid, tusb_event_t event, cdc_pipeid_t pipe_id, uint32_t xferred_bytes)
|
||||
@@ -90,9 +90,12 @@ void tusbd_cdc_xfer_isr(uint8_t coreid, tusb_event_t event, cdc_pipeid_t pipe_id
|
||||
switch(event)
|
||||
{
|
||||
case TUSB_EVENT_XFER_COMPLETE:
|
||||
received_bytes = min16_of(xferred_bytes, CDCD_APP_BUFFER_SIZE); // discard overflow bytes
|
||||
for(uint8_t i=0; i<xferred_bytes; i++)
|
||||
{
|
||||
fifo_write(&fifo_serial, serial_rx_buffer+i);
|
||||
}
|
||||
osal_semaphore_post(sem_hdl); // notify main task
|
||||
break;
|
||||
break;
|
||||
|
||||
case TUSB_EVENT_XFER_ERROR:
|
||||
xferred_bytes = 0; // ignore
|
||||
@@ -122,14 +125,23 @@ OSAL_TASK_FUNCTION( cdcd_serial_app_task ) (void* p_task_para)
|
||||
|
||||
if ( tusbd_cdc_is_configured(0) )
|
||||
{
|
||||
if ( received_bytes )
|
||||
{ // echo back
|
||||
memcpy(cdcd_app_send_buffer, cdcd_app_recv_buffer, received_bytes);
|
||||
tusbd_cdc_send(0, cdcd_app_send_buffer, received_bytes, false);
|
||||
received_bytes = 0;
|
||||
// echo back data in the fifo
|
||||
if ( !tusbd_cdc_is_busy(0, CDC_PIPE_DATA_IN) )
|
||||
{
|
||||
uint16_t count=0;
|
||||
while( fifo_read(&fifo_serial, &serial_tx_buffer[count]) )
|
||||
{
|
||||
count++;
|
||||
}
|
||||
|
||||
if (count)
|
||||
{
|
||||
tusbd_cdc_send(0, serial_tx_buffer, count, false);
|
||||
}
|
||||
}
|
||||
|
||||
tusbd_cdc_receive(0, cdcd_app_recv_buffer, CDCD_APP_BUFFER_SIZE, true);
|
||||
// getting next data from host
|
||||
tusbd_cdc_receive(0, serial_rx_buffer, CDCD_APP_BUFFER_SIZE, true);
|
||||
}
|
||||
|
||||
OSAL_TASK_LOOP_END
|
||||
|
||||
@@ -448,7 +448,7 @@
|
||||
|
||||
<Group>
|
||||
<GroupName>app</GroupName>
|
||||
<tvExp>0</tvExp>
|
||||
<tvExp>1</tvExp>
|
||||
<tvExpOptDlg>0</tvExpOptDlg>
|
||||
<cbSel>0</cbSel>
|
||||
<RteFlg>0</RteFlg>
|
||||
@@ -474,10 +474,10 @@
|
||||
<FileType>1</FileType>
|
||||
<tvExp>0</tvExp>
|
||||
<Focus>0</Focus>
|
||||
<ColumnNumber>0</ColumnNumber>
|
||||
<ColumnNumber>3</ColumnNumber>
|
||||
<tvExpOptDlg>0</tvExpOptDlg>
|
||||
<TopLine>1</TopLine>
|
||||
<CurrentLine>1</CurrentLine>
|
||||
<CurrentLine>2</CurrentLine>
|
||||
<bDave2>0</bDave2>
|
||||
<PathWithFileName>..\src\cdc_serial_app.c</PathWithFileName>
|
||||
<FilenameWithoutPath>cdc_serial_app.c</FilenameWithoutPath>
|
||||
@@ -736,7 +736,7 @@
|
||||
|
||||
<Group>
|
||||
<GroupName>tinyusb</GroupName>
|
||||
<tvExp>0</tvExp>
|
||||
<tvExp>1</tvExp>
|
||||
<tvExpOptDlg>0</tvExpOptDlg>
|
||||
<cbSel>0</cbSel>
|
||||
<RteFlg>0</RteFlg>
|
||||
@@ -744,7 +744,7 @@
|
||||
<GroupNumber>3</GroupNumber>
|
||||
<FileNumber>18</FileNumber>
|
||||
<FileType>1</FileType>
|
||||
<tvExp>0</tvExp>
|
||||
<tvExp>1</tvExp>
|
||||
<Focus>0</Focus>
|
||||
<ColumnNumber>64</ColumnNumber>
|
||||
<tvExpOptDlg>0</tvExpOptDlg>
|
||||
@@ -924,8 +924,8 @@
|
||||
<Focus>0</Focus>
|
||||
<ColumnNumber>0</ColumnNumber>
|
||||
<tvExpOptDlg>0</tvExpOptDlg>
|
||||
<TopLine>0</TopLine>
|
||||
<CurrentLine>0</CurrentLine>
|
||||
<TopLine>54</TopLine>
|
||||
<CurrentLine>62</CurrentLine>
|
||||
<bDave2>0</bDave2>
|
||||
<PathWithFileName>..\..\..\tinyusb\common\fifo.c</PathWithFileName>
|
||||
<FilenameWithoutPath>fifo.c</FilenameWithoutPath>
|
||||
@@ -1156,7 +1156,7 @@
|
||||
<Focus>0</Focus>
|
||||
<ColumnNumber>0</ColumnNumber>
|
||||
<tvExpOptDlg>0</tvExpOptDlg>
|
||||
<TopLine>1705</TopLine>
|
||||
<TopLine>1688</TopLine>
|
||||
<CurrentLine>1713</CurrentLine>
|
||||
<bDave2>0</bDave2>
|
||||
<PathWithFileName>..\..\..\vendor\fatfs\ff.c</PathWithFileName>
|
||||
|
||||
@@ -92,7 +92,7 @@ void tusbh_cdc_xfer_isr(uint8_t dev_addr, tusb_event_t event, cdc_pipeid_t pipe_
|
||||
break;
|
||||
|
||||
case TUSB_EVENT_XFER_ERROR:
|
||||
xferred_bytes = 0; // ignore
|
||||
received_bytes = 0; // ignore
|
||||
break;
|
||||
|
||||
case TUSB_EVENT_XFER_STALLED:
|
||||
|
||||
Reference in New Issue
Block a user