简体   繁体   English

C中stm32l中的数据损坏

[英]Data corruption in stm32l in c

I am finding some data corruption in the variable u32baudrate, when passed from main to httpinit(). 从main传递到httpinit()时,我在变量u32baudrate中发现了一些数据损坏。 But other variables txsize and flowcontrol are coming out right. 但是其他变量txsize和flowcontrol正确地出现了。

typedef struct _test
{
  ...
  uint8_t txsize[5];
  uint32_t u32baudrate;
  uint8_t flowcontrol;
  ...
}test;

from main.c 来自main.c

test gtest;
gtest.u32baudrate=921600;
printf("baud: %d",gtest.u32baudrate);        //921600: this is coming out right

httpinit(&gtest);

in http.c 在http.c中

httpinit(test * gtest)
{
  printf("baud: %d",gtest->u32baudrate);     //268435456: this is coming out wrong
}

Found the problem, it was packing, fixed it by doing this--> 发现问题所在,是打包,通过执行此操作来解决->

typedef PACKED struct _test
{
  ...
  uint8_t txsize[5];
  uint32_t u32baudrate;
  uint8_t flowcontrol;
  ...
}test;

I think this may help, well not sure :) 我认为这可能会有所帮助,但不确定:)

typedef struct    //delete this so test is the struct type name// _test
{
  ...
  uint8_t txsize[5];
  uint32_t u32baudrate;
  uint8_t flowcontrol;
  ...
}test;

The problem is quite possibly in code you have not posted. 问题很可能出在您尚未发布的代码中。

Some printf() implementations use a relatively large amount of stack; 某些printf()实现使用相对大量的堆栈。 you may have a stack-overflow caused by your debug method. 您的调试方法可能会导致堆栈溢出。 Try removing the first printf call so that there is nothing between the assignment and the httptest() call. 尝试删除第一个printf调用,以使赋值和httptest()调用之间没有任何关系。 If it is then not corrupted, the printf() itself caused the problem, and you probably need to assign a larger stack. 如果未损坏,则printf()本身会引起问题,您可能需要分配更大的堆栈。

Otherwise an out-of-bounds access to txsize[] is a possibility. 否则,可能会txsize[]访问txsize[] Is this truly the code you are testing - with httptest() called directly after assignment - or have you elided the code presented? 这真的是您正在测试的代码-在分配后直接调用httptest() ,还是您删除了所提供的代码?

The printf() format specifier %d is used to print signed integers. printf()格式说明符%d用于打印带符号整数。 However, uint32_t is an unsigned integer type. 但是, uint32_t是无符号整数类型。 The usual format specifier for unsigned ints is %u . 无符号整数的常用格式说明符是%u

Packed has nothing to do with this error. 打包与该错误无关。 Usually, an error on a printf results form stack corruption. 通常,printf上的错误会导致堆栈损坏。 Consider this: 考虑一下:

struct wp_char{
      char wp_cval;
      short wp_font;
      short wp_psize;
}v1;

struct wp_char v2;

What is the difference between v1 and v2? v1和v2有什么区别? Are they different or same? 它们不同还是相同? Oops....pressed the enter key by mistake. 糟糕....误按了Enter键。

BTW the following code works for me perfectly. 顺便说一句,下面的代码对我来说很完美。

#include <stdio.h>
#include <limits.h>

typedef unsigned char uint8_t;
typedef unsigned int uint32_t;

typedef struct _test
{
    uint8_t txsize[5];
    uint32_t u32baudrate;
    uint8_t flowcontrol;
}test;

print_value(test *gtest)
{
        printf("%u\n", gtest->u32baudrate);
}

main()
{
        test gtest;

        gtest.u32baudrate = 921600;
        printf("%u\n", gtest.u32baudrate);

        print_value(&gtest);
}

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

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