繁体   English   中英

arm cortex m0 nf51822 c编程硬故障,真的很疑惑

[英]arm cortex m0 nf51822 c programing hard fault, really puzzled

我试图将char类型的缓冲区转换为我定义的结构,以便通过TLV分析缓冲区。 但是我一次又一次陷入了严重的错误。

代码是这些:有点长。

#define BigtoLittle32(A)   ((( (uint32_t)(A) & 0xff000000) >> 24) | \
                                   (( (uint32_t)(A) & 0x00ff0000) >> 8)   | \
                                   (( (uint32_t)(A) & 0x0000ff00) << 8)   | \
                                   (( (uint32_t)(A) & 0x000000ff) << 24))

enum {
    DISH = 0
} CB_DISH_TLV_TYPES;

typedef struct cb_tlv_node_s
{
    uint32_t type;
    uint32_t length;
    char value[0];
} cb_tlv_node_t;


typedef struct cb_ble_buffer_s {
    uint16_t total_length;
    uint16_t current_length;
    int8_t current_data_id;
    int8_t expect_package_id;
    char buffer[500];
} cb_ble_buffer_t;

static cb_ble_buffer_t                  current_buffer;

cb_tlv_node_t *
cb_tlv_read(char *buffer)
{
    uint32_t a,b,c,d;
    cb_tlv_node_t * tlv_p;
    tlv_p =  (cb_tlv_node_t *)buffer;
/*    tlv_p->length = BigtoLittle32(tlv_p->length);*/  //this code also cause hard fault
/*    tlv_p->type = BigtoLittle32(tlv_p->type);*/  //didn't test this one ,but high risk causing hard fault

    return tlv_p;
}

//ble packages are reorgnized and call this function. it excutes in the ble recieve interupt
int
cb_dish_data_processer(char * data, int length)
{
    assert(data != NULL);
    assert(length > 0);


    cb_tlv_node_t *tlv_p = cb_tlv_read(data);

    //test
    int a = sizeof(CB_DISH_TLV_TYPES);

    //if ((char)tlv_p->type != DISH) { //this code is fine and steady
    if (tlv_p->type != DISH) {         //this leads to hard fault
        return CB_PC_CODE_UNRECOGNIZE;
    }

    cb_dish_tlv_read(tlv_p->value, tlv_p->length);
    return CB_PC_CODE_PROCESSED;
}

拳头我确定指针不会丢失,因此,如果使用了非法地址,则不应有任何种类; 其次,我认为我的过程太长了,无法进入中断,因此我将代码移出了一侧并在主循环中激发,但是仍然没有用;

所以我被困在这里,找不到导致此的任何原因。 如果您需要更多信息,请留言,我将在线等待。 ps:硬件板为北欧nf51822,具有8kB RAM,256kB flash ROM

顺便说一句:这种板卡中是否禁止calloc和其他alloc函数? 因为当我尝试动态分配新空间时,开发板会出错并自行重置。

非常感谢

看起来可疑的一件事是将char *缓冲区转换为(cb_tlv_node_t *)并尝试使用它。 ARM要求在访问时将32位整数正确对齐到4字节边界。 char * buffer指向的缓冲区可能与1字节边界对齐。 由用户694733

所以我改变了这个

typedef struct cb_ble_buffer_s {
    uint16_t total_length;
    uint16_t current_length;
    int8_t current_data_id;
    int8_t expect_package_id;
    char buffer[500];
} cb_ble_buffer_t;

进入这个

typedef struct cb_ble_buffer_s {
    uint16_t total_length;
    uint16_t current_length;
    int16_t current_data_id;
    int16_t expect_package_id;
    char buffer[500];
} cb_ble_buffer_t;

而且它工作得很好而且很稳定,这要感谢@ user694733

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM