I am trying to set up an SD card on an Intel Board D2000 Quark. 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++.
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;
. First it is a certain value. After this statement the value is 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
. Currently you are assigning the value of sdCard
to be a pointer to its own location on the stack. Instead, do SDInit(malloc(sizeof(SD)));
.
Personally, I would not even have that initialization function. I would just do SD * sdCard = malloc(sizeof(SD));
EDIT: In response to Peter's point, you could also do this and ignore the instantiation function:
SD sdCard;
User contributions licensed under CC BY-SA 3.0