[英]arm cortex m0 nf51822 c programing hard fault, really puzzled
I was trying to transform a char type buffer into a struct defined by me in order to analyze the buffer by TLV. 我试图将char类型的缓冲区转换为我定义的结构,以便通过TLV分析缓冲区。 But i jumped into hard fault again and again.
但是我一次又一次陷入了严重的错误。
codes are these: a little bit long. 代码是这些:有点长。
#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;
}
Fist I am sure that pointers are not lost ,so there should not be any kind if illegal address been used; 拳头我确定指针不会丢失,因此,如果使用了非法地址,则不应有任何种类; Second I assume my process is too long to be in an interrupt so I moved my codes out side and excite in the main loop, but still useless;
其次,我认为我的过程太长了,无法进入中断,因此我将代码移出了一侧并在主循环中激发,但是仍然没有用;
So I am stucked here, I can't find any reason causing this. 所以我被困在这里,找不到导致此的任何原因。 if you need more info ,leave a message, I will be waiting online.
如果您需要更多信息,请留言,我将在线等待。 ps:the hardware board is Nordic nf51822 with 8kB RAM, 256kB flash ROM
ps:硬件板为北欧nf51822,具有8kB RAM,256kB flash ROM
BTW:Is it true that calloc and other alloc functions are forbidden in this kind of boards? 顺便说一句:这种板卡中是否禁止calloc和其他alloc函数? because when I try to alloc a new space dynamically, the board goes wrong and reset it self.
因为当我尝试动态分配新空间时,开发板会出错并自行重置。
Thanks a lot 非常感谢
One thing that looks suspicious is that you convert char* buffer to (cb_tlv_node_t *) and try to use it. 看起来可疑的一件事是将char *缓冲区转换为(cb_tlv_node_t *)并尝试使用它。 ARM requires that 32-bit integers are correctly aligned to 4-byte boundaries when accesing them.
ARM要求在访问时将32位整数正确对齐到4字节边界。 It is likely that buffer pointed by char *buffer is aligned to 1-byte boundaries.
char * buffer指向的缓冲区可能与1字节边界对齐。 by user694733
由用户694733
so i changed this 所以我改变了这个
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;
into this 进入这个
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;
and it just works fine and steady many thanks to @user694733 而且它工作得很好而且很稳定,这要感谢@ user694733
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.