LUFA USB Connection Timeout

Go To Last Post
3 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hey you all,

I have a function that builds a table (stored on heap) with 256 elementes received on my Usb connection and the program takes to long to generates this table that my USB connection brokes (i hear the Windows sound when you unplug a device). I call this function after HID_Device_USBTask() and USB_USBTask() functions inside the while loop but as you can imagine, it didn´t work well.

This situation gets worse when i call the function to compute the 256 elements of data.

That's what i do: i receive a block of 8 bytes of data and append each block to my big ass table! My code works with a short tables like 16 bytes or so, but with a big one like 256 bytes it goes to hell!

It seems that the USB conection it ran a time-out or something.

 

There is my pseudo-code:

 

    uint8_t *p_data_to_save = NULL;
    uint8_t *p_data_to_host = NULL;

    int main(void)
    {
        p_data_from_host = (uint8_t*)calloc(8, sizeof(uint8_t));
        p_data_to_save = (uint8_t*)calloc(256, sizeof(uint8_t));

        SetupHardware();
        LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
        GlobalInterruptEnable();

        for (;;)
        {
            HID_Device_USBTask(&Generic_HID_Interface);
            USB_USBTask();

            if (new_bytes_recived == true )
            {
                 // Append 8 bytes received to my 256 bytes table
                if(indx_data < 255)
                {
                   for(int i=0; i<8; i++)
                   {
                      p_data_to_save[indx_data] = p_data_from_host[i];
                      indx_data++;
    
                   }
                }

                new_bytes_recived = false;
             }

             if(table_completed == true)
             {

                 // Process the 256 bytes of data
                 Process_table();
              }

              (...)  // other smal if-cases
        }
        free(p_data_from_host);
        p_data_to_host=NULL;
         free(p_data_to_save);
         p_data_to_save=NULL;
    }

 On CALLBACK_HID_Device_ProcessHIDReport():

 

     void CALLBACK_HID_Device_ProcessHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo,
                                          const uint8_t ReportID,
                                          const uint8_t ReportType,
                                          const void* ReportData,
                                          const uint16_t ReportSize)
     {
         unsigned int i;
         uint8_t* Data       = (uint8_t*)ReportData;
         for (i=0;i<8;i++)
         {
             p_data_from_host[i]=Data[i];
         }
    
         new_bytes_recived = true;
    }

 

 

Any solution to this?

Thank you all

 

PS: I'm using an Atmega16u2.

 

This topic has a solution.

Last Edited: Mon. Jul 27, 2015 - 10:40 PM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

JNeto06 wrote:

I have a function that builds a table (stored on heap) with 256 elementes received on my Usb connection and the program takes to long to generates this table that my USB connection brokes

 

I don't see why appending 8 bytes to a table would take so long as to break your USB connection.

I'm guessing it is because you are using the heap.  Instead just use a 256 byte array of bytes.

 

As for processing the table when it's full, break it up into a sequence of small steps.

Process_table() will be called repeatedly when table_completed is true.

Each time Process_table() is called, it performs just one step and returns, leaving  table_completed true.

Thus, HID_Device_USBTask() and USB_USBTask() are called after each step.

 

Use a static variable inside your function to keep track of what step to do next.

When the task is complete (all steps performed), reset this variable to its initial value and then set table_completed to false.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hey Chuck99.

Sorry for the late response. You were right.

The problem seems to be the table allocation on the heap memory. The memory management is a critical point on my project, so the only solution was follow your advice re-code the all project again.

I broke my code into small code groups, got rid of my big ass table of 256 bytes and now i work only with mini tables of 8 bytes.

I was not easy to re-code all again and the algorithm it's not the most efficient but it gets the job done :D

Thank you very much for your support.