6 years, 1 month ago.

HardFault Error when accessing SD-Card after building up network connection

Hi,

I want to combine a HTTP-Client and a SD-Card-Reader. My goal is to download a file from a server and save that file on the SD-Card. Unfortunately Im stuck on the way, because of an Hard Fault 0x80FF013D.

I haven broken down the code and recognized in summary:

  • network communication (GET-command) works fine solo
  • SD-Card access (Read and Write) works fine solo
  • SD-Card access works fine before building up the network connection
  • Hard Fault occures, when I access the SD-Card after building up the network connection

Core-Infos:

Serial Output with Hard Fault

[NWKH] Connecting to network...<\r><\n>
[NWKH] Connected to the network<\r><\n>
[NWKH] IP address: 192.168.188.29<\r><\n>
Test SD-Card<\r><\n>
<\r><\n>
++ MbedOS Fault Handler ++<\r><\n>
<\r><\n>
FaultType: HardFault<\r><\n>
<\r><\n>
Context:<\r><\n>
R0   : 20000400<\r><\n>
R1   : BFF39B82<\r><\n>
R2   : 08025B6A<\r><\n>
R3   : 00000003<\r><\n>
R4   : 00000000<\r><\n>
R5   : 2000FA34<\r><\n>
R6   : 84551677<\r><\n>
R7   : 7FFFFC00<\r><\n>
R8   : 00000003<\r><\n>
R9   : 08025B6A<\r><\n>
R10  : 2000FA34<\r><\n>
R11  : 00000000<\r><\n>
R12  : 08013E6D<\r><\n>
SP   : 2000F9F8<\r><\n>
LR   : 0801A8E7<\r><\n>
PC   : A0000000<\r><\n>
xPSR : 210B0000<\r><\n>
PSP  : 2000F990<\r><\n>
MSP  : 2004FFC0<\r><\n>
CPUID: 410FC271<\r><\n>
HFSR : 40000000<\r><\n>
MMFSR: 00000001<\r><\n>
BFSR : 00000000<\r><\n>
UFSR : 00000000<\r><\n>
DFSR : 0000000B<\r><\n>
AFSR : 00000000<\r><\n>
Mode : Thread<\r><\n>
Priv : Privileged<\r><\n>
Stack: PSP<\r><\n>
<\r><\n>
-- MbedOS Fault Handler --<\r><\n>
<\r><\n>
<\r><\n>
<\r><\n>
++ MbedOS Error Info ++<\r><\n>
Error Status: 0x80FF013D Code: 317 Module: 255<\r><\n>
Error Message: Fault exception<\r><\n>
Location: 0x8012A7B<\r><\n>
Error Value: 0xA0000000<\r><\n>
Current Thread: Id: 0x2000DA34 Entry: 0x8012BEB StackSize: 0x2000 StackMem: 0x2000DA78 SP: 0x2004FF58 <\r><\n>
For more info, visit: https://armmbed.github.io/mbedos-error/?error=0x80FF013D<\r><\n>
-- MbedOS Error Info --<\r><\n>

I have started with the http-example... <<https://os.mbed.com/teams/sandbox/code/http-example/file/2efadc4d8784/source/main-http-socket-reuse.cpp/shortlog/>>

...and added SDBlockDevice and FATFileSystem for the SD-Card access.

main-http.cpp

#include "select-demo.h"

#if DEMO == DEMO_HTTP

#include "mbed.h"
#include "http_request.h"
#include "network-helper.h"
#include "mbed_mem_trace.h"
#include "SDBlockDevice.h"
#include "FATFileSystem.h"
#include "DebouncedIn.h"

#define SD_MOUNT_PATH           "sd"
#define FULL_UPDATE_FILE_PATH   "/" SD_MOUNT_PATH "/" MBED_CONF_APP_UPDATE_FILE

SDBlockDevice sd(MBED_CONF_APP_SD_CARD_MOSI, MBED_CONF_APP_SD_CARD_MISO,
                 MBED_CONF_APP_SD_CARD_SCK, MBED_CONF_APP_SD_CARD_CS);
FATFileSystem fs(SD_MOUNT_PATH);

NetworkInterface* network;
DebouncedIn btn(USER_BUTTON);

FILE* file;

int main() 
{
/*------Init SD-Card-----------*/
	int r;
	//Init
    if ((r = sd.init()) != 0) {
        printf("Could not initialize SD driver (%d)\n", r);
        return 1;
    }
	
	//Mount
    if ((r = fs.mount(&sd)) != 0) {
        printf("Could not mount filesystem, is the SD card formatted as FAT? (%d)\n", r);
        return 1;
    }
	
 /*------Init Network-----------*/	
    network = connect_to_default_network_interface();
    if (!network) 
	{
        printf("Cannot connect to the network, see serial output\n");
        return 1;
    }

	//Write
	printf("Test SD-Card\n");
	char testbuffer2[] = { 'a' , 'b' , 'c' };
	file = fopen("/sd/test.bin", "wb");
	fwrite("abc",1,3,file);
	//fwrite(testbuffer2,1,sizeof(testbuffer2),file);
	fclose(file);

//Hauptschleife	
	while(1)
	{
		//Buttondruck
		if (btn.rising())
		{
			printf("Update wird gesucht, bitte warten\n");
		}
    }
}

#endif

mbed_app.json

{
    "config": {
        "main-stack-size": {
            "value": 8192
        },
		
		"update_file": {
            "help": "Path to the application update binary on the SD card",
            "value": "\"update.bin\""
        },
		
		"sd_card_mosi": {
            "help": "MCU pin connected to the SD card's SPI MOSI pin",
            "value": "D11"
        },
        "sd_card_miso": {
            "help": "MCU pin connected to the SD card's SPI MISO pin",
            "value": "D12"
        },
        "sd_card_sck": {
            "help": "MCU pin connected to the SD card's SPI SCK pin",
            "value": "D13"
        },
        "sd_card_cs": {
            "help": "MCU pin connected to the SD card's SPI CS pin",
            "value": "D10"
        }
    },
    "macros": [
        "MBEDTLS_MPI_MAX_SIZE=1024",
        "MBEDTLS_MPI_WINDOW_SIZE=1",
        "MBEDTLS_USER_CONFIG_FILE=\"mbedtls_entropy_config.h\"",
        "MBEDTLS_TEST_NULL_ENTROPY",
        "MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES",
        "MBED_HEAP_STATS_ENABLED=1"
    ],
    "target_overrides": {
        "*": {
            "platform.stdio-baud-rate": 115200,
            "platform.stdio-convert-newlines": true,
            "mbed-mesh-api.6lowpan-nd-channel-page": 0,
            "mbed-mesh-api.6lowpan-nd-channel": 12,
            "mbed-trace.enable": 1,
			"platform.error-hist-enabled": 1,
            "mbed-http.http-buffer-size": 2048,
            "nsapi.default-wifi-security": "WPA_WPA2",
            "nsapi.default-wifi-ssid": "\"SSID\"",
            "nsapi.default-wifi-password": "\"Password\""
        }
    }
}

I have already read the Tutorial for "Analyzing Mbed OS crash dump" -> https://os.mbed.com/docs/v5.8/tutorials/analyzing-mbed-os-crash-dump.html, but I have still no idea what I can do, to find the reason for the Hard Fault.

  • Error Status: "0x80FF013D" means "Hard Fault exception"
  • HFSR: 40000000 means "Forced Hard Fault"
  • MMFSR: 00000001 means "The processor attempted an instruction fetch from a location that does not permit execution."
  • UFSR: 00000000 means "everything is good"
  • BFSR: 00000000 means "everything is good"

I would appreciate some help. Thanks in advance.

Be the first to answer this question.