Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem


LPC2129 - DAbt Handler using FatFS

Posted by J.S. on July 25, 2012
Hi all,

I've inherited a project (I'm an ME, not CS/EE) running FreeRTOS and have been tasked with eliminating a few bugs.

Most have been simple, but never having used any RTOS before, some problems are more troubling. There's one thing in particular that I keep coming back to: data abort.

I had a function to stream a file over UART. The data transmitted fine, but at the end of every file when it came back to the "root" of the task, I got a stack overflow (checking was enabled). I couldn't figure out why, so I added a lot of debug prints.

Ok, that's interesting... it reinitializes all (already running) tasks and then overflows. But why would it reset? It turns out, all the exception handlers cascaded into the reset handler. I tried SUBS R15,R14,#8 to go back two instructions and resume, but it stays stuck. I guess printing R14 - 8 might tell me the value of the memory location that was accessed improperly, but I had no debugger and I'm no good with inline ASM. If someone can suggest how to read that to C, I can do a UART_puts or putc to see what it contains.

If I send a short string manually, it works fine. It's sort of an arbitrary cut off length. This runs in a task called Upload, and I have tried everything from portSAVE/RESTORE, task_ENTER_CRITICAL, vTaskSuspendAll, disabling interrupts... it still happens. Yes, FS_SHARE is enabled, but FS_REENTRANT is not as this is accessed from a single task. I have also tried declaring the file locally as well as globally.

The offending code is below, called within task 'Upload':

int SendFile(char* filetosend) {
FIL SD_File_Handle_2; // Can I use the same handle from SD_file_handle to reduce RAM use?
BYTE buffer[512]; //BYTE buffer[SD_BLOCK_SIZE];
uint32_t br;
int i;
int hadExtension;
hadExtension = 0;
if (strcmp(filetosend,"ACTIVE") == 0) { filetosend = filename; }
for (i = 0; i < 15; i ++) {
if (filetosend == '.') { hadExtension = 1; }
if (filetosend == '\r' || filetosend['i'] == '\n') { filetosend = '\0'; break; } // remove extra characters
if (hadExtension == 0 && i <= 11) { // Automagically add .TXT for things that have no extension
filetosend = '.';
filetosend[i+1] = 'T';
filetosend[i+2] = 'X';
filetosend[i+3] = 'T';
// This does not null-terminate, assuming that any /r/n would be overwritten by the first ".T"
gm862_upload("SENDING ",8); gm862_upload(filetosend,strlen(filetosend)); gm862_upload("...\r\n",5);
res = f_open(&SD_File_Handle_2, filetosend, FA_READ);
if (res != FR_OK) {
gm862_upload("OPEN FAILED\r\n",13);
return res;
else {
while (1) {
res = f_read(&SD_File_Handle_2, buffer, sizeof(buffer),(UINT *) &br);
if (res || br == 0) { gm862_upload("\r\n***END***\r\n",13); break; } // Error or EOF
int j;
for (j = 0; j < sizeof(buffer); j++) { gm862_addchar(buffer[j]); }
//if (br % 512 == 0) { debug_printf("\nSector edge\n"); }
return res;

Frankly, I don't care a whole heck of a lot of this is blocking (I'd rather it not be) - I just want it to send a file and keep on trucking! It does something similar for f_write, but I imagine solving this will solve that...


RE: LPC2129 - DAbt Handler using FatFS

Posted by Richard on July 25, 2012
What architecture are you running on?
Is you task stack big enough to have 512 bytes on it?
You should not call taskDISABLE/ENABLE_INTERRUPTS() - only taskENTER/EXIT_CRITICAL().
There is no point suspending the scheduler if interrupts are disabled.

If you are using FatFS, then presumably you also have a diskio interface, and that is a whole load of code. I think you first need to work out how to debug the data abort, and how to do that depends on the architecture you are using.


RE: LPC2129 - DAbt Handler using FatFS

Posted by J.S. on July 26, 2012
Hi Richard,

I am running an LPC2129 (ARM7TDMI) in ARM mode. The stack should be large enough - even if I change it to BYTE buffer[2], or even [1], it still fails.

Thanks for the note on taskENTER/EXIT. I will update my code accordingly.

As for FatFS, yes, I am using diskio.c and a home-grown SD driver that seems to work fairly reliably. I believe I could trace it if I could print the value of R14, but without knowledge of assembly or inline assembly, that may not be trivially easy for me to do.

RE: LPC2129 - DAbt Handler using FatFS

Posted by Richard on July 26, 2012
You should be able to view the value of R14 in the debugger. Make sure you are viewing the system mode R14 though, not the exception mode R14.


[ Back to the top ]    [ About FreeRTOS ]    [ Privacy ]    [ Sitemap ]    [ ]

Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.

Latest News

NXP tweet showing LPC5500 (ARMv8-M Cortex-M33) running FreeRTOS.

Meet Richard Barry and learn about running FreeRTOS on RISC-V at FOSDEM 2019

Version 10.1.1 of the FreeRTOS kernel is available for immediate download. MIT licensed.

View a recording of the "OTA Update Security and Reliability" webinar, presented by TI and AWS.


FreeRTOS and other embedded software careers at AWS.

FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Espressif ESP32

IAR Partner

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers


STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

OpenRTOS and SafeRTOS

Xilinx Microblaze and Zynq partner