This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

FaultISR - Bulk Device



Hi, this is the function that I call to transmit USB

static unsigned long
Transmit_USB(USBbuffer Data,int size)
{
    unsigned long ulLoop, ulCount,ulLoop_buff;
    unsigned long ulWriteIndex;
    uint32_t buffer_stockage;
    volatile int shiftage;
    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 many characters can we process this time round?
    //
    ulCount = size;

    //
    // Set up to process the characters by directly accessing the USB buffers.
    //
    ulWriteIndex = sTxRing.ulWriteIndex;

    for(ulLoop = 0 ; ulLoop < ulCount ;ulLoop = ulLoop + 4)
    {

    	for(ulLoop_buff = 0 ; ulLoop_buff < 4; ulLoop_buff++){
    		buffer_stockage = Data.data[ulLoop+ulLoop_buff];
    		shiftage = 32 - ( ulLoop_buff + 1 ) * 8;
    		if(shiftage < 0){
    			shiftage = 0;
    		}
    		buffer_stockage = buffer_stockage<<shiftage;
    	}

        //
        // Move to the next character taking care to adjust the pointer for
        // the buffer wrap if necessary.
        //


    	g_pucUSBTxBuffer[ulWriteIndex] = buffer_stockage;
    	ulWriteIndex = ulWriteIndex + 4;
        ulWriteIndex = (ulWriteIndex == BULK_BUFFER_SIZE) ? 0 : ulWriteIndex;

    }
    USBBufferDataWritten(&g_sTxBuffer,size);
    return(ulCount);
}

The function seem to be working because if I check the Txbuffer into the debugger, he got fill with char. But eventually, the MCU fall into FaultISR error and I would like to know why.

  • Nothing specific but a few comments on the code

    Mathieu L. said:
    USBbuffer Data

    Definition of USBbuffer? Just from the way it's used I suspect it's quite large.  Passing it by value is a bad idea in that case.

    Mathieu L. said:
    volatile int shiftage;

    Why volatile?

    Mathieu L. said:
    shiftage = 32 - ( ulLoop_buff + 1 ) * 8;

    As written this appears to have a danger of overflowing. However, IF data is 8 bits then this block of code

    Mathieu L. said:
    for(ulLoop = 0 ; ulLoop < ulCount ;ulLoop = ulLoop + 4) { for(ulLoop_buff = 0 ; ulLoop_buff < 4; ulLoop_buff++){ buffer_stockage = Data.data[ulLoop+ulLoop_buff]; shiftage = 32 - ( ulLoop_buff + 1 ) * 8; if(shiftage < 0){ shiftage = 0; } buffer_stockage = buffer_stockage<<shiftage; } // // Move to the next character taking care to adjust the pointer for // the buffer wrap if necessary. // g_pucUSBTxBuffer[ulWriteIndex] = buffer_stockage; ulWriteIndex = ulWriteIndex + 4; ulWriteIndex = (ulWriteIndex == BULK_BUFFER_SIZE) ? 0 : ulWriteIndex; }

    would appear to simply be a memcpy.  I suggest not rewriting standard library functions without a very good reason.

    This

    Mathieu L. said:
    ulWriteIndex = (ulWriteIndex == BULK_BUFFER_SIZE) ? 0 : ulWriteIndex;

    may be correct but it seems odd to start filling a buffer part way through and then wrapping to the beginning.  It's not correct for a ring buffer and it's exceedingly odd for writing packets.

    Robert

  • Hi,
    USBbuffer is just a struct containing an array of 256 units. Why it is a bad idea?
    Regards.
    Mathieu
  • Hi

    what funciton are referring by "memcpy". I am new to Tivaware Library, so I don t know all the tools that I can use with the drivers.
    Regards.
    Mathieu
  • 256 units of what? Even if it's bytes you just dumped a lot of data on your limited stack space. It doesn't take much of that to blow your stack completely.

    Robert
  • I take it you are new to C?

    It's part of the standard library. You should be familiar with it if you are using C. If you are not familiar with it then I suggest a standard C reference. Maybe K&R or Harbison & Steele.

    Robert
  • Hi
    Yeah, it is 256 bytes.
    Regards
    Mathieu
  • Yeah, It is the first project where I use to programm a MCU. I did some C++ and Arduino before, so it isn't complete obscur stuff. But I am in a learning stage.