/* * ======== txData ======== */ static uint32_t txData(const uint8_t *pStr, uint32_t length) { unsigned long buffAvailSize; uint32_t bufferedCount = 0; uint32_t sendCount = 0; uint8_t *sendPtr; while (bufferedCount != length) { /* Determine the buffer size available */ buffAvailSize = USBBufferSpaceAvailable(&txBuffer); /* Determine how much needs to be sent */ if ((length - bufferedCount) > buffAvailSize) { sendCount = buffAvailSize; } else { sendCount = length - bufferedCount; } /* Adjust the pointer to the data */ sendPtr = (uint8_t *)pStr + bufferedCount; /* Place the contents into the USB BUffer */ bufferedCount += USBBufferWrite(&txBuffer, sendPtr, sendCount); } return (bufferedCount); }
//**************************************************************************** // // This function will print out to the console UART and not the echo UART. // //**************************************************************************** void CommandPrint(const char *pcStr) { int iIdx; const char cCR = 0xd; iIdx = 0; while(pcStr[iIdx] != 0) { // // Wait for space for two bytes in case there is a need to send out // the line feed plus the carriage return. // while(USBBufferSpaceAvailable(&g_sTxBuffer) < 2) { } // // Print the next character. // USBBufferWrite(&g_sTxBuffer, (const unsigned char *)&pcStr[iIdx], 1); // // If this is a line feed then send a carriage return as well. // if(pcStr[iIdx] == 0xa) { USBBufferWrite(&g_sTxBuffer, (const unsigned char *)&cCR, 1); } iIdx++; } }
/****************************************************************************** * * * \brief Read as many characters from the UART FIFO as we can and move the * * into the CDC transmit buffer.\n * * * * \param none. * * * * \return UART error flags read during data reception. * * * ******************************************************************************/ static int ReadUARTData(void) { int lChar, lErrors; unsigned char ucChar; unsigned int ulSpace; /* Clear our error indicator. */ lErrors = 0; /* How much space do we have in the buffer? */ ulSpace = USBBufferSpaceAvailable((tUSBBuffer *)&g_sTxBuffer); /* Read data from the UART FIFO until there is none left or we run out of space in our receive buffer. */ while(ulSpace && UARTCharsAvail(USB_UART_BASE)) { /* Read a character from the UART FIFO into the ring buffer if no errors are reported. */ lChar = UARTCharGetNonBlocking(USB_UART_BASE); /* If the character did not contain any error notifications, copy it to the output buffer. */ if(!(lChar & ~0xFF)) { ucChar = (unsigned char)(lChar & 0xFF); USBBufferWrite((tUSBBuffer *)&g_sTxBuffer, (unsigned char *)&ucChar, 1); /* Decrement the number of bytes we know the buffer can accept. */ ulSpace--; } else { /* Update our error accumulator. */ lErrors |= lChar; } /* Update our count of bytes received via the UART. */ g_ulUARTRxCount++; } /* Pass back the accumulated error indicators. */ return(lErrors); }
//***************************************************************************** // // Read as many characters from the UART FIFO as possible and move them into // the CDC transmit buffer. // // \return Returns UART error flags read during data reception. // //***************************************************************************** static long ReadUARTData(void) { long lChar, lErrors; unsigned char ucChar; unsigned long ulSpace; // // Clear the error indicator. // lErrors = 0; // // How much space is available in the buffer? // ulSpace = USBBufferSpaceAvailable(&g_sTxBuffer); // // Read data from the UART FIFO until there is none left or there is no // more space in the receive buffer. // while(ulSpace && ROM_UARTCharsAvail(UART0_BASE)) { // // Read a character from the UART FIFO into the ring buffer if no // errors are reported. // lChar = ROM_UARTCharGetNonBlocking(UART0_BASE); // // If the character did not contain any error notifications, // copy it to the output buffer. // if(!(lChar & ~0xFF)) { ucChar = (unsigned char)(lChar & 0xFF); USBBufferWrite(&g_sTxBuffer, &ucChar, 1); // // Decrement the number of bytes the buffer can accept. // ulSpace--; } else { // // Update the error accumulator. // lErrors |= lChar; } } // // Pass back the accumulated error indicators. // return(lErrors); }
void USB_resetPacketAssembler(void){ g_ucReadMode = WAITING; g_ulCharIndex = 0; g_ulParamIndex = 0; g_ulReceiveMode = REC_MODE_NORMAL; #ifdef __DEBUG__ UARTprintf("\nTx Buffer Space Available = %d",USBBufferSpaceAvailable((tUSBBuffer *)&g_sTxBuffer)); UARTprintf("\nRx Buffer Space Available = %d",USBBufferSpaceAvailable((tUSBBuffer *)&g_sRxBuffer)); UARTprintf("\nCLEARING USB BUFFERS..."); #endif USBBufferFlush((tUSBBuffer *)&g_sTxBuffer); USBBufferFlush((tUSBBuffer *)&g_sRxBuffer); #ifdef __DEBUG__ UARTprintf("\nTx Buffer Space Available = %d",USBBufferSpaceAvailable((tUSBBuffer *)&g_sTxBuffer)); UARTprintf("\nRx Buffer Space Available = %d",USBBufferSpaceAvailable((tUSBBuffer *)&g_sRxBuffer)); #endif }
/************************************************************************************//** ** \brief Checks if there is still data left to transmit and if so submits it ** for transmission with the USB endpoint. ** \return none. ** ****************************************************************************************/ static void UsbTransmitPipeBulkIN(void) { tUSBRingBufObject txRing; blt_int32u txSpace; blt_int32u txWriteIdx; blt_int8u nr_of_bytes_for_tx_endpoint; blt_int8u byte_counter; blt_int8u byte_value; blt_bool result; /* read how many bytes should be transmitted */ nr_of_bytes_for_tx_endpoint = UsbFifoMgrScan(fifoPipeBulkIN.handle); /* only continue if there is actually data left to transmit */ if (nr_of_bytes_for_tx_endpoint == 0) { return; } /* get information about the USB transmit buffer */ USBBufferInfoGet(&g_sTxBuffer, &txRing); /* determine how many bytes will still fit in the tx buffer */ txSpace = USBBufferSpaceAvailable(&g_sTxBuffer); /* only transmit the amount of bytes that will fit in the tx buffer */ if (nr_of_bytes_for_tx_endpoint > txSpace) { nr_of_bytes_for_tx_endpoint = txSpace; } /* determine write index for the tx buffer */ txWriteIdx = txRing.ui32WriteIndex; /* copy the transmit data to the transmit buffer */ for (byte_counter=0; byte_counter < nr_of_bytes_for_tx_endpoint; byte_counter++) { /* obtain data from the fifo */ result = UsbFifoMgrRead(fifoPipeBulkIN.handle, &byte_value); ASSERT_RT(result == BLT_TRUE); /* store it in the transmit buffer */ g_pui8USBTxBuffer[txWriteIdx] = byte_value; /* increment index with wrapping */ txWriteIdx++; if (txWriteIdx >= BULK_BUFFER_SIZE) { txWriteIdx = 0; } } /* inform the usb library that new data are ready for transmission */ USBBufferDataWritten(&g_sTxBuffer, nr_of_bytes_for_tx_endpoint); } /*** end of UsbTransmitPipeBulkIN ***/
/* * ======== USBCDCD_LoggerIdle_sendData ======== */ int USBCDCD_LoggerIdle_sendData(unsigned char *pStr, int length) { unsigned int bufAvailSize; /* Determine the buffer size available and length to send */ bufAvailSize = USBBufferSpaceAvailable(&txBuffer); if (!bufAvailSize || state != USBCDCD_STATE_IDLE) { return (0); } else if (bufAvailSize < length) { /* Write largest multiple of 4 bytes */ length = bufAvailSize - (bufAvailSize % 4); } /* Place the contents into the USB BUffer */ return (USBBufferWrite(&txBuffer, pStr, length)); }
TASKSTATUS UsbTxTaskMgr(UsbTxTask *pusbobj) { uint32_t ulSpace, ulWriteIndex; tUSBRingBufObject sTxRing; uint8_t i; USBBufferInfoGet(&g_sTxBuffer, &sTxRing); ulSpace = USBBufferSpaceAvailable(&g_sTxBuffer); if (ulSpace < pusbobj->Len) return TASKSTS_ONGOING; ulWriteIndex = sTxRing.ui32WriteIndex; for (i=0; i<pusbobj->Len; i++) { g_pui8USBTxBuffer[ulWriteIndex++] = *(pusbobj->pBuf + i); ulWriteIndex = (ulWriteIndex == BULK_BUFFER_SIZE) ? 0 : ulWriteIndex; } USBBufferDataWritten(&g_sTxBuffer, pusbobj->Len); free(pusbobj->pBuf); pusbobj->MemProd--; ASSERT(pusbobj->MemProd == 1); free(pusbobj); return TASKSTS_FINISH; }
//***************************************************************************** // // Writes a character to the USB interface. // //***************************************************************************** void USBIFWrite(unsigned char ucChar) { // // Delay until there is some space in the output buffer. // while(g_bUSBConnected) { if(USBBufferSpaceAvailable(&g_sTxBuffer) != 0) { break; } } // // If there is still a USB connection, write this character into the output // buffer. // if(g_bUSBConnected) { USBBufferWrite(&g_sTxBuffer, &ucChar, 1); } }
//***************************************************************************** // // Receive new data and echo it back to the host. // // \param psDevice points to the instance data for the device whose data is to // be processed. // \param pi8Data points to the newly received data in the USB receive buffer. // \param ui32NumBytes is the number of bytes of data available to be processed. // // This function is called whenever we receive a notification that data is // available from the host. We read the data, byte-by-byte and swap the case // of any alphabetical characters found then write it back out to be // transmitted back to the host. // // \return Returns the number of bytes of data processed. // //***************************************************************************** static uint32_t EchoNewDataToHost(tUSBDBulkDevice *psDevice, uint8_t *pi8Data, uint_fast32_t ui32NumBytes) { uint_fast32_t ui32Loop, ui32Space, ui32Count; uint_fast32_t ui32ReadIndex; uint_fast32_t ui32WriteIndex; tUSBRingBufObject sTxRing; // // Get the current buffer information to allow us to write directly to // the transmit buffer (we already have enough information from the // parameters to access the receive buffer directly). // USBBufferInfoGet(&g_sTxBuffer, &sTxRing); // // How much space is there in the transmit buffer? // ui32Space = USBBufferSpaceAvailable(&g_sTxBuffer); // // How many characters can we process this time round? // ui32Loop = (ui32Space < ui32NumBytes) ? ui32Space : ui32NumBytes; ui32Count = ui32Loop; // // Update our receive counter. // g_ui32RxCount += ui32NumBytes; // // Dump a debug message. // DEBUG_PRINT("Received %d bytes\n", ui32NumBytes); // // Set up to process the characters by directly accessing the USB buffers. // ui32ReadIndex = (uint32_t)(pi8Data - g_pui8USBRxBuffer); ui32WriteIndex = sTxRing.ui32WriteIndex; while(ui32Loop) { // // Copy from the receive buffer to the transmit buffer converting // character case on the way. // // // Is this a lower case character? // if((g_pui8USBRxBuffer[ui32ReadIndex] >= 'a') && (g_pui8USBRxBuffer[ui32ReadIndex] <= 'z')) { // // Convert to upper case and write to the transmit buffer. // g_pui8USBTxBuffer[ui32WriteIndex] = (g_pui8USBRxBuffer[ui32ReadIndex] - 'a') + 'A'; } else { // // Is this an upper case character? // if((g_pui8USBRxBuffer[ui32ReadIndex] >= 'A') && (g_pui8USBRxBuffer[ui32ReadIndex] <= 'Z')) { // // Convert to lower case and write to the transmit buffer. // g_pui8USBTxBuffer[ui32WriteIndex] = (g_pui8USBRxBuffer[ui32ReadIndex] - 'Z') + 'z'; } else { // // Copy the received character to the transmit buffer. // g_pui8USBTxBuffer[ui32WriteIndex] = g_pui8USBRxBuffer[ui32ReadIndex]; } } // // Move to the next character taking care to adjust the pointer for // the buffer wrap if necessary. // ui32WriteIndex++; ui32WriteIndex = (ui32WriteIndex == BULK_BUFFER_SIZE) ? 0 : ui32WriteIndex; ui32ReadIndex++; ui32ReadIndex = (ui32ReadIndex == BULK_BUFFER_SIZE) ? 0 : ui32ReadIndex; ui32Loop--; } // // We've processed the data in place so now send the processed data // back to the host. // USBBufferDataWritten(&g_sTxBuffer, ui32Count); DEBUG_PRINT("Wrote %d bytes\n", ui32Count); // // We processed as much data as we can directly from the receive buffer so // we need to return the number of bytes to allow the lower layer to // update its read pointer appropriately. // return(ui32Count); }
//***************************************************************************** // // Read as many characters from the UART FIFO as we can and move them into // the CDC transmit buffer. // // \return Returns UART error flags read during data reception. // //***************************************************************************** static int32_t ReadUARTData(void) { int32_t i32Char, i32Errors; uint8_t ui8Char; uint32_t ui32Space; // // Clear our error indicator. // i32Errors = 0; // // How much space do we have in the buffer? // ui32Space = USBBufferSpaceAvailable((tUSBBuffer *)&g_sTxBuffer); // // Read data from the UART FIFO until there is none left or we run // out of space in our receive buffer. // while(ui32Space && ROM_UARTCharsAvail(USB_UART_BASE)) { // // Read a character from the UART FIFO into the ring buffer if no // errors are reported. // i32Char = ROM_UARTCharGetNonBlocking(USB_UART_BASE); // // If the character did not contain any error notifications, // copy it to the output buffer. // if(!(i32Char & ~0xFF)) { ui8Char = (uint8_t)(i32Char & 0xFF); USBBufferWrite((tUSBBuffer *)&g_sTxBuffer, (uint8_t *)&ui8Char, 1); // // Decrement the number of bytes we know the buffer can accept. // ui32Space--; } else { #ifdef DEBUG // // Increment our receive error counter. // g_ui32UARTRxErrors++; #endif // // Update our error accumulator. // i32Errors |= i32Char; } // // Update our count of bytes received via the UART. // g_ui32UARTRxCount++; } // // Pass back the accumulated error indicators. // return(i32Errors); }
//***************************************************************************** // // Receive new data and echo it back to the host. // // \param psDevice points to the instance data for the device whose data is to // be processed. // \param pcData points to the newly received data in the USB receive buffer. // \param ulNumBytes is the number of bytes of data available to be processed. // // This function is called whenever there is data available from the host. The // data is read byte-by-byte, the case of any alphabetical characters is // swapped, and then it is written back out to be transmitted back to the host. // // \return Returns the number of bytes of data processed. // //***************************************************************************** static unsigned long EchoNewDataToHost(tUSBDBulkDevice *psDevice, unsigned char *pcData, unsigned long ulNumBytes) { unsigned long ulLoop, ulSpace, ulCount; unsigned long ulReadIndex; unsigned long ulWriteIndex; tUSBRingBufObject sTxRing; // // Get the current buffer information to allow us to write directly to the // transmit buffer (there is already have enough information from the // parameters to access the receive buffer directly). // USBBufferInfoGet(&g_sTxBuffer, &sTxRing); // // How much space is there in the transmit buffer? // ulSpace = USBBufferSpaceAvailable(&g_sTxBuffer); // // How many characters can be processed this time round? // ulLoop = (ulSpace < ulNumBytes) ? ulSpace : ulNumBytes; ulCount = ulLoop; // // Update our receive counter. // g_ulRxCount += ulNumBytes; // // Set up to process the characters by directly accessing the USB buffers. // ulReadIndex = (unsigned long)(pcData - g_pucUSBRxBuffer); ulWriteIndex = sTxRing.ulWriteIndex; // // Copy from the receive buffer to the transmit buffer converting character // case on the way. // while(ulLoop--) { // // Is this a lower case character? // if((g_pucUSBRxBuffer[ulReadIndex] >= 'a') && (g_pucUSBRxBuffer[ulReadIndex] <= 'z')) { // // Convert to upper case and write to the transmit buffer. // g_pucUSBTxBuffer[ulWriteIndex] = (g_pucUSBRxBuffer[ulReadIndex] - 'a') + 'A'; } else { // // Is this an upper case character? // if((g_pucUSBRxBuffer[ulReadIndex] >= 'A') && (g_pucUSBRxBuffer[ulReadIndex] <= 'Z')) { // // Convert to lower case and write to the transmit buffer. // g_pucUSBTxBuffer[ulWriteIndex] = (g_pucUSBRxBuffer[ulReadIndex] - 'Z') + 'z'; } else { // // Copy the received character to the transmit buffer. // g_pucUSBTxBuffer[ulWriteIndex] = g_pucUSBRxBuffer[ulReadIndex]; } } // // Move to the next character taking care to adjust the pointer for // the buffer wrap if necessary. // ulWriteIndex++; ulWriteIndex = (ulWriteIndex == BULK_BUFFER_SIZE) ? 0 : ulWriteIndex; ulReadIndex++; ulReadIndex = (ulReadIndex == BULK_BUFFER_SIZE) ? 0 : ulReadIndex; } // // The data has been processed in place so now send the processed data back // to the host. // USBBufferDataWritten(&g_sTxBuffer, ulCount); // // Return the number of bytes processed to allow the bulk device driver to // update its read pointer appropriately. // return(ulCount); }