Bizarre memory corruption on LPC11U68

10 Jul 2015

I'm working on a program for the LPC11U68 that scans a folder on an SD card, and copies all of the short filenames it finds into a list of structs defined like so:

#define MAX_FILES 100

struct FileEntry {
    char fileName[22];
    char someField[64];
    char someOtherField;
};

FileEntry fileList[MAX_FILES];


This worked fine until today, when I tried to increase the size of the array. For some reason, I'm able to cause all kinds of inconsistent memory problems simply by creating, and interacting with this array. If I increase MAX_FILES to between 140 and 155, the program deadlocks and won't even run. But if I increase it to 200 or more, the program will run fine up until it accesses items around the 150's. This is very strange, since the array is using less than half of the available RAM, even at 200 items! I was able to reproduce the issue using a test program:

[Repository '/users/neilt6/code/MemoryBug_TestApp/latest/' not found]

To reproduce the bug, I created a folder on an SD card with 1600 files in it. The test program scans the folder without using the list first, in order to prove that this isn't a FatFs-related stack overflow:

/media/uploads/neilt6/without_list.jpg

Then it scans the folder again using the list, and inconsistent memory corruption ensues! This particular run crashed gracefully at index 155:

/media/uploads/neilt6/with_list_1.jpg

This run, however, deadlocked after reading index 168:

/media/uploads/neilt6/with_list_2.jpg

Am I doing something wrong here? How do I even begin to troubleshoot this?

10 Jul 2015

UPDATE

I may have figured it out, it looks like the mbed-rtos library has incorrectly defined INITIAL_SP for the LPC11U68:

/media/uploads/neilt6/lpc11u68_initial_stack_pointer.jpg

According to the datasheet, INITIAL_SP should be 0x10008000, not 0x10004000:

/media/uploads/neilt6/lpc11u68_memory_map.jpg

I will test my findings tomorrow.

UPDATE 2

I just tried increasing INITIAL_SP to 0x10008000, and that solved my issue. I'll be submitting a pull-request over at GitHub.