/************************************************************************************//** ** \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 ***/
void AppCheck(void) { #if FW_UPDATE if (*(uint32_t *)pAppSig != FWBL) return; *(uint32_t *)pAppSig = FWAPP; uint8_t buffer[] = {0x10, 0x02, 0x10, 0x10, 0x19, 04, 0x09, 0x0e, 0x10, 0x10, 0x25, 0x00, 0x00, 0x10, 0x03}; uint8_t len = 15; uint32_t ulWriteIndex; tUSBRingBufObject sTxRing; uint8_t i; USBBufferInfoGet(&g_sTxBuffer, &sTxRing); ulWriteIndex = sTxRing.ui32WriteIndex; for (i = 0; i < len; i++) { g_pui8USBTxBuffer[ulWriteIndex++] = *(buffer + i); ulWriteIndex = (ulWriteIndex == BULK_BUFFER_SIZE) ? 0 : ulWriteIndex; } USBBufferDataWritten(&g_sTxBuffer, len); #endif }
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; }
//***************************************************************************** // // 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); }
//***************************************************************************** // // 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); }