Saturday, May 26, 2012

8Kb is not a lot

...but it just has to do.

The Arduino with the Atmega chip has only 8Kb bytes of RAM.  In writing my Arduino-MATLAB interface I came up hard against that limit.  My interface works by allowing a user to upload a small program to the Arduino that uses a locally defined function library.  I realized something was funny when programs will too many instructions failed.  This was because when program upload size exceeded a certain value, the Arduino would crash because it could no longer allocate memory for the incoming data.  By being more careful with memory, I was able to extend the amount I could send.

Also I found some nice ways to check the amount of memory available to malloc.  In the following code, x is there just to test that this function is working properly (i.e. detecting a prior malloc usage):


void funcFreeMem(IN_FREE_MEM_FUNC *in,OUT_FREE_MEM_FUNC *out)
{
  int sz = 0; 
  
  byte* x = (byte*) malloc(in->in * sizeof(byte));
  
  byte *buf;
  while ( (buf = (byte*) malloc (sz * sizeof(byte))) != NULL ) {
    sz++; // if allocation was successful, then up the count for the next try
    free(buf); // free memory after allocating it
  }
  
  free(buf);
  free(x);
  out->mem = sz;
}

No comments:

Post a Comment