mbed:建立网络连接后访问 SD 卡时出现 HardFault 错误

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

我想结合一个 HTTP 客户端和一个 SD 卡-Reader。我的目标是从服务器下载文件并将该文件保存在 SD 卡上。不幸的是,由于硬故障 0x80FF013D,我卡在了路上。

我已经分解了代码,总结了一下:

核心信息os:

图书馆:

带有硬故障的串行输出:

[NWKH] Connecting to network...
[NWKH] Connected to the network
[NWKH] IP address: 192.168.188.29
Test SD-Card

++ MbedOS Fault Handler ++

FaultType: HardFault

Context:
R0 : 20000400
R1 : BFF39B82
R2 : 08025B6A
R3 : 00000003
R4 : 00000000
R5 : 2000FA34
R6 : 84551677
R7 : 7FFFFC00
R8 : 00000003
R9 : 08025B6A
R10 : 2000FA34
R11 : 00000000
R12 : 08013E6D
SP : 2000F9F8
LR : 0801A8E7
PC : A0000000
xPSR : 210B0000
PSP : 2000F990
MSP : 2004FFC0
CPUID: 410FC271
HFSR : 40000000
MMFSR: 00000001
BFSR : 00000000
UFSR : 00000000
DFSR : 0000000B
AFSR : 00000000
Mode : Thread
Priv : Privileged
Stack: PSP

-- MbedOS Fault Handler --

++ MbedOS Error Info ++
Error Status: 0x80FF013D Code: 317 Module: 255
Error Message: Fault exception
Location: 0x8012A7B
Error Value: 0xA0000000
Current Thread: Id: 0x2000DA34 Entry: 0x8012BEB StackSize: 0x2000 StackMem: 0x2000DA78 SP: 0x2004FF58
For more info, visit: https://armmbed.github.io/mbedos-error/?error=0x80FF013D
-- MbedOS Error Info --

我从 mbed https://os.mbed.com/teams/sandbox/code/http-example/file/2efadc4d8784/source/main-http-socket-reuse.cpp/shortlog/

的 http 示例开始

并从 sd 卡文件系统示例中添加了一些东西 https://os.mbed.com/cookbook/SD-Card-File-System

主要-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\""
        }
    }
}

我已经阅读了 "Analyzing Mbed OS crash dump" -> https://os.mbed.com/docs/v5.8/tutorials/analyzing-mbed-os-crash-dump.html,
的教程 但我仍然不知道我能做些什么,找到硬故障的原因。

我希望得到一些帮助。提前致谢。

这块板子的D11引脚有冲突。它由以太网和您的 SPI 使用。您需要使用另一个 SPI 引脚或按照 mbed 站点的 guide 来修复电路板:

If you use both SPI and Ethernet You have to patch the NUCLEO board on the back side:

  1. remove SB121 and close SB122 solder bridges. This will connect PB_5 to D11 instead of PA_7.
  2. Overwrite the d11_configuration by using the mbed_app.json file and use PB_5 (instead of PA_7 default value).