/* Virtual com port write routine*/ uint32_t vcom_write(uint8_t *pBuf, uint32_t len) { VCOM_DATA_T *pVcom = &g_vCOM; uint32_t ret = 0; if ( (pVcom->tx_flags & VCOM_TX_CONNECTED) && ((pVcom->tx_flags & VCOM_TX_BUSY) == 0) ) { pVcom->tx_flags |= VCOM_TX_BUSY; /* enter critical section */ NVIC_DisableIRQ(USB0_IRQn); ret = USBD_API->hw->WriteEP(pVcom->hUsb, USB_CDC_IN_EP, pBuf, len); /* exit critical section */ NVIC_EnableIRQ(USB0_IRQn); } return ret; } |
Hi
before sending data to Vcom wait white busy flag is 1.
/* Virtual com port write routine*/
uint32_t vcom_write(uint8_t *pBuf, uint32_t len)
{
VCOM_DATA_T *pVcom = &g_vCOM;
uint32_t ret = 0;
int retry=0;
// if ((pVcom->tx_flags & VCOM_TX_CONNECTED) && ((pVcom->tx_flags & VCOM_TX_BUSY) == 0))
if (pVcom->tx_flags & VCOM_TX_CONNECTED)
{
retry=0;
while((pVcom->tx_flags & VCOM_TX_BUSY) && retry<50000 )
{
retry++;
}
/* enter critical section */
NVIC_DisableIRQ(USB0_IRQn);
pVcom->tx_flags |= VCOM_TX_BUSY;
ret = USBD_API->hw->WriteEP(pVcom->hUsb, USB_CDC_IN_EP, pBuf, len);
/* exit critical section */
NVIC_EnableIRQ(USB0_IRQn);
}
return ret;
}
if (ret == LPC_OK) { /* allocate transfer buffers */ g_vCOM.rx_buff = (uint8_t *) cdc_param.mem_base; cdc_param.mem_base += VCOM_RX_BUF_SZ; cdc_param.mem_size -= VCOM_RX_BUF_SZ; /* allocate transfer buffers */ g_vCOM.tx_buff = (uint8_t *) cdc_param.mem_base; //<-------- need to init the tx_buff cdc_param.mem_base += VCOM_TX_BUF_SZ; //<--------this will need a #define added as well cdc_param.mem_size -= VCOM_TX_BUF_SZ; |
/* Virtual com port write routine*/ uint32_t vcom_write(uint8_t *pBuf, uint32_t len) { VCOM_DATA_T *pVcom = &g_vCOM; uint32_t ret = 0; //if ( (pVcom->tx_flags & VCOM_TX_CONNECTED) && ((pVcom->tx_flags & VCOM_TX_BUSY) == 0) ) { if ( (pVcom->tx_flags & VCOM_TX_CONNECTED)) { while((pVcom->tx_flags & VCOM_TX_BUSY) != 0); pVcom->tx_flags |= VCOM_TX_BUSY; /* enter critical section */ NVIC_DisableIRQ(USB_IRQn); ret = USBD_API->hw->WriteEP(pVcom->hUsb, USB_CDC_IN_EP, pBuf, len); /* exit critical section */ NVIC_EnableIRQ(USB_IRQn); } return ret; } |
/** * Structure containing Virtual Comm port control data */ typedef struct VCOM_DATA { USBD_HANDLE_T hUsb; USBD_HANDLE_T hCdc; uint8_t *rx_buff; uint16_t rx_rd_count; uint16_t rx_count; uint16_t tx_buff_count;// uint8_t *tx_buff;// volatile uint16_t tx_flags; volatile uint16_t rx_flags; } VCOM_DATA_T; |
/* Virtual com port write routine*/ uint32_t vcom_write(uint8_t *pBuf, uint32_t len) { VCOM_DATA_T *pVcom = &g_vCOM; uint32_t ret = 0; if ( (pVcom->tx_flags & VCOM_TX_CONNECTED)) { if ((pVcom->tx_flags & VCOM_TX_BUSY) == 0) { //This flag is set to zero when in the bulk interrupt when no more data is ready to send pVcom->tx_flags |= VCOM_TX_BUSY; /* enter critical section */ NVIC_DisableIRQ(USB0_IRQn); ret = USBD_API->hw->WriteEP(pVcom->hUsb, USB_CDC_IN_EP, pBuf, len); /* exit critical section */ NVIC_EnableIRQ(USB0_IRQn); } else { if((len + (pVcom->tx_buff_count)) < VCOM_TX_BUF_SZ) { /* enter critical section */ NVIC_DisableIRQ(USB0_IRQn); memcpy(&pVcom->tx_buff[pVcom->tx_buff_count], pBuf, len); pVcom->tx_buff_count += len; ret = pVcom->tx_buff_count; /* exit critical section */ NVIC_EnableIRQ(USB0_IRQn); } } } return ret; } |
/* VCOM bulk EP_IN endpoint handler */ static ErrorCode_t VCOM_bulk_in_hdlr(USBD_HANDLE_T hUsb, void *data, uint32_t event) { VCOM_DATA_T *pVcom = (VCOM_DATA_T *) data; if (event == USB_EVT_IN) { if(pVcom->tx_buff_count > 0) { USBD_API->hw->WriteEP(pVcom->hUsb, USB_CDC_IN_EP, pVcom->tx_buff, pVcom->tx_buff_count); pVcom->tx_buff_count = 0; } else { pVcom->tx_flags &= ~VCOM_TX_BUSY; } } return LPC_OK; } |