Someone pro in LUFA framework and AVR microcontrollers?
I have a function that builds a table (stored on heap memory) 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.
The ATmega16U2 only has 512 bytes of RAM. You didn't show the definition of your table but I think it's safe to assume that your table takes up the entire RAM. It's too bad your linker didn't give you a warning about this. You'll have to get a better microcontroller (like the ATmega32U4) or make your table smaller.
Keep in mind that most local and global variables get stored in RAM by default, so you can't simply use up all of the RAM for your table.