我试图将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:硬件板是Nordic nf51822,带有8kB RAM,256kB闪存ROM
BTW:在这种电路板上禁止使用calloc和其他alloc功能吗? 因为当我尝试动态分配新空间时,电路板出错并自行重置。非常感谢
答案 0 :(得分:0)
看起来可疑的一件事是你将char * buffer转换为(cb_tlv_node_t *)并尝试使用它。 ARM要求32位整数在访问时正确对齐到4字节边界。 char * buffer指向的缓冲区很可能与1字节边界对齐。来自user694733
所以我改变了这个
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