enhance serial host app

This commit is contained in:
hathach 2013-10-27 20:56:41 +07:00
parent 1d28b2bd10
commit a98bae0d19
1 changed files with 23 additions and 21 deletions

View File

@ -118,15 +118,14 @@ void cdc_serial_app_init(void)
//------------- main task -------------// //------------- main task -------------//
OSAL_TASK_FUNCTION( cdc_serial_app_task ) (void* p_task_para) OSAL_TASK_FUNCTION( cdc_serial_app_task ) (void* p_task_para)
{ {
static uint8_t dev_addr; // This task can be separated into 2 Task : sending & receiving.
OSAL_TASK_LOOP_BEGIN OSAL_TASK_LOOP_BEGIN
for(dev_addr=1; dev_addr <= TUSB_CFG_HOST_DEVICE_MAX; dev_addr++) //------------- send characters got from uart terminal to the first CDC device -------------//
for(uint8_t dev_addr=1; dev_addr <= TUSB_CFG_HOST_DEVICE_MAX; dev_addr++)
{ {
if ( tusbh_cdc_serial_is_mounted(dev_addr) ) if ( tusbh_cdc_serial_is_mounted(dev_addr) )
{ {
//------------- send characters got from uart terminal -------------//
int ch_tx = getchar(); int ch_tx = getchar();
if ( ch_tx > 0 ) if ( ch_tx > 0 )
{ // USB is much faster than serial, here we assume usb is always complete. There could be some characters missing { // USB is much faster than serial, here we assume usb is always complete. There could be some characters missing
@ -137,27 +136,30 @@ OSAL_TASK_FUNCTION( cdc_serial_app_task ) (void* p_task_para)
tusbh_cdc_send(dev_addr, serial_out_buffer, 1, false); // no need for interrupt on serial out pipe tusbh_cdc_send(dev_addr, serial_out_buffer, 1, false); // no need for interrupt on serial out pipe
} }
} }
//------------- print out received characters -------------//
tusb_error_t error;
osal_semaphore_wait(sem_hdl, 100, &error);
if ( TUSB_ERROR_NONE == error)
{
for(uint8_t i=0; i<received_bytes; i++)
{
printf("%c", serial_in_buffer[i]);
}
tusbh_cdc_receive(dev_addr, serial_in_buffer, sizeof(serial_in_buffer), true);
}
break; // demo app only communicate with the first CDC-capable device break; // demo app only communicate with the first CDC-capable device
} }
} }
if (dev_addr > TUSB_CFG_HOST_DEVICE_MAX) //------------- print out received characters -------------//
{ // there is no CDC device connected tusb_error_t error;
osal_task_delay(1000); osal_semaphore_wait(sem_hdl, 100, &error); // timeout to allow getchar from uart terminal can be executed
if ( TUSB_ERROR_NONE == error)
{
for(uint8_t i=0; i<received_bytes; i++)
{
printf("%c", serial_in_buffer[i]);
}
for(uint8_t dev_addr=1; dev_addr <= TUSB_CFG_HOST_DEVICE_MAX; dev_addr++)
{
if ( tusbh_cdc_serial_is_mounted(dev_addr) )
{
tusbh_cdc_receive(dev_addr, serial_in_buffer, sizeof(serial_in_buffer), true);
break; // demo app only communicate with the first CDC-capable device
}
}
} }
OSAL_TASK_LOOP_END OSAL_TASK_LOOP_END