简体   繁体   English

为什么结构中的变量会发生变化?

[英]Why are my variables in my struct changing?

I am trying to set up an SD card on an Intel Board D2000 Quark. 我正在尝试在Intel Board D2000 Quark上设置SD卡。 The problem, I am encountering is that my variables in my struct change at a certain point in the program. 我遇到的问题是结构中的变量在程序中的某个点发生了变化。 I figured out when the variables are changing but I do not have an idea how to fix it. 我弄清楚了变量何时更改,但是我不知道如何解决它。

The reason I am using C instead of C++ is that the compiler of Intel Microcontrollers Studio does not let my use C++. 我使用C而不是C ++的原因是Intel Microcontrollers Studio的编译器不允许我使用C ++。

Below, I copied some of the relevant code. 在下面,我复制了一些相关代码。

SD* sdCard;
uint8_t readData(uint32_t block, uint16_t offset, uint16_t count, uint8_t* dst){
    if(count == 0){
        return true;
    }
    if((count + offset) > 512){
        goto fail;
    }
    if(!sdCard->inBlock_ || block != sdCard->block_ || offset < sdCard->offset_){
        sdCard->block_ = block;
        if(sdCard->type_ != SD_CARD_TYPE_SDHC){
            block <<=9;
        }
        uint8_t result = sendCommand(CMD17, block);
        if(result){
            goto fail;
        }
        if(!waitStartBlock()){
            goto fail;
        }
        sdCard->offset_ = 0;
        sdCard->inBlock_ = 1;
    }

    for(; sdCard->offset_ < offset; sdCard->offset_++){
        spiRecieve();
    }
    for(uint16_t i = 0; i < count; i++){
        dst[i] = spiRecieve();
    }
    sdCard->offset_ += count;
    if(!sdCard->partialBlockRead_ || sdCard->offset_ >= 512){
        readEnd();
    }
    QM_PUTS("RD FINISH");
    return true;

    fail:
    QM_PUTS("RD FAIL");
    return false;}

The moment the variables change is sdCard->block_ = block; 变量更改的时刻为sdCard->block_ = block; . First it is a certain value. 首先,它是一定值。 After this statement the value is 0xFFFFFFFF; 在该语句之后,值为0xFFFFFFFF; This happens to every variable in the struct. 这发生在结构中的每个变量上。

My struct looks like this: 我的结构看起来像这样:

typedef struct SDcard{
uint32_t block_ ;
uint8_t errorCode_;
uint8_t inBlock_;
uint16_t offset_;
uint8_t partialBlockRead_;
uint8_t status_;
uint8_t type_;
}SD;

Update for the comments: This is my temporary main: 更新评论:这是我的临时主干:

SD sdCard;

int main(void)
{
if(!SDInit(&sdCard)){
    QM_PRINTF("ERROR1\n");
}
while(1){}
}

If anyone knows a solution or has some questions, please let me know. 如果有人知道解决方案或有任何疑问,请告诉我。

You are improperly initializing sdCard . 您不正确地初始化sdCard Currently you are assigning the value of sdCard to be a pointer to its own location on the stack. 当前,您正在将sdCard的值分配为指向其在堆栈中的位置的指针。 Instead, do SDInit(malloc(sizeof(SD))); 而是执行SDInit(malloc(sizeof(SD))); .

Personally, I would not even have that initialization function. 就个人而言,我什至没有该初始化功能。 I would just do SD * sdCard = malloc(sizeof(SD)); 我只是做SD * sdCard = malloc(sizeof(SD));

EDIT: In response to Peter's point, you could also do this and ignore the instantiation function: 编辑:响应彼得的观点,您也可以这样做,并忽略实例化函数:

SD sdCard;

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

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