Code problem in MOD54415-Structure in C

Discussion to talk about software related topics only.
Post Reply
nicobari
Posts: 110
Joined: Mon Jan 30, 2012 12:15 pm

Code problem in MOD54415-Structure in C

Post by nicobari »

Hi,
So I was trying to pass a struct to a function and change its value. Below is my struct definition

Code: Select all

struct FileRoutines{
	int drv;
	F_FILE* fp;
};
And my main looks like

Code: Select all

...
FileRoutines OpenOnboardSD;
OpenFileRoutine(&OpenOnboardSD);////////////////////////Output 1/////////////////////////
iprintf("Main Code drv value=%d\n",OpenOnboardSD.drv);////////////////////////Output 2////////////////////////
...
And my function looks like

Code: Select all

void OpenFileRoutine(struct FileRoutines* OpenOnboardSD) {
	f_enterFS();
	char File_name[6];
	OpenOnboardSD->drv = OpenOnBoardFlash();
	int *card_status = initOnBoardSD(OpenOnboardSD->drv);
	sprintf(File_name, "LOG%d.txt", card_status[1] + 1);

	OpenOnboardSD->fp = f_open(File_name, "w+");

	iprintf("Local drv value=%d\n", OpenOnboardSD->drv);////////////Output 1/////////////////

}
The problem is my Output 1 is 16 and Output 2 is 0 , when I think they should be same. What am I doing wrong? Thanks in advance.

Regards,
TM
rnixon
Posts: 833
Joined: Thu Apr 24, 2008 3:59 pm

Re: Code problem in MOD54415-Structure in C

Post by rnixon »

I don't have the answer to the value question, but I think you have much bigger problems to deal with first. From your function name OpenFileRoutine() it seems like you are calling f_enterFS() each time you open a file? That function is only used to keep track of the working directory for a task. Take a look at the effs-fat manual and the examples to get that set up right in your code. I would copy some of the text here but for some reason I can't copy from the pdf. Only execute that function once per task, and if you ever release a task you have to call f_releasefs() to free the resources first.

One other minor issue, you should use siprintf() instead of sprintf(). If you have even one call to printf or sprintf all the floating point stuff will be linked in. Since you use iprintf I'm assuming you were trying to avoid that.

As for the structure issue I would write a simpler function to set the value and experiment a bit. Its hard to see what is going on with the other function calls you have.
nicobari
Posts: 110
Joined: Mon Jan 30, 2012 12:15 pm

Re: Code problem in MOD54415-Structure in C

Post by nicobari »

Thanks for replying. I am calling the OpenFileRoutine() function just once at the start of the main code. Sorry for not explaining the code properly but I use f_enterFS() just once in the main task (when I call the OpenFileRoutine() ) and not repeatedly as I just open one file to write data to when Netburner powers up. Also thanks for the good suggestion regarding siprintf(), appreciate it.

Regards,
TM
User avatar
dciliske
Posts: 624
Joined: Mon Feb 06, 2012 9:37 am
Location: San Diego, CA
Contact:

Re: Code problem in MOD54415-Structure in C

Post by dciliske »

I'd second the notion of removing the f_enterFS call from the function from a code reuse standpoint. Basically, you don't want to have to rewrite this function for using it in a different project, or when design requirements change and you do need to call it again. Also, every function should do one thing, and one thing only; if you have to use the word 'and' to describe the function, it probably does to much.
Dan Ciliske
Project Engineer
Netburner, Inc
nicobari
Posts: 110
Joined: Mon Jan 30, 2012 12:15 pm

Re: Code problem in MOD54415-Structure in C

Post by nicobari »

Thanks, and I have removed f_enterFS(); from the function and I guess I am making things complicated so I am working on changing that but I did found out the solution. It seems like in my old code I was passing the address of the struct variable but in the function I was just changing the value of the local variable drv and fp. So I changed the code to the following

Code: Select all

void OpenFileRoutine(struct FileRoutines* OpenOnboardSD) {
	char File_name[20]={0};
	(*OpenOnboardSD).drv =OpenOnBoardFlash();
	int *card_status = initOnBoardSD((*OpenOnboardSD).drv);
	(*OpenOnboardSD).number_of_files=card_status[1] + 1;
	siprintf(File_name, "LOG%d.txt", (*OpenOnboardSD).number_of_files);

	(*OpenOnboardSD).fp = f_open(File_name, "w+");

}
and now it works, but I still agree I need to clear things up. Thanks for the help, appreciate it. You learn new thing everyday.

Regards,
TM
rnixon
Posts: 833
Joined: Thu Apr 24, 2008 3:59 pm

Re: Code problem in MOD54415-Structure in C

Post by rnixon »

This new code should be identical to the old code using the ->. This is strange. I would figure out what is going on with the -> method since that is the way everyone is used to doing things.

As a test how about going back to the original code and changing

OpenOnboardSD->drv = OpenOnBoardFlash();

to

OpenOnboardSD->drv = 1

Or some other constant.
nicobari
Posts: 110
Joined: Mon Jan 30, 2012 12:15 pm

Re: Code problem in MOD54415-Structure in C

Post by nicobari »

Yeah I tried that and it was still not same.
greengene
Posts: 164
Joined: Wed May 14, 2008 11:20 am
Location: Lakeside, CA

Re: Code problem in MOD54415-Structure in C

Post by greengene »

Is this one of those PClint tests?

The first problem I see is the memory leak from File_name[] that was fixed
in the second code set. Trashing the stack usually isn't a good thing. I'd try
that small change in the first code and see if that clears up the problem.
Whether there is another with card_status is dependent on the function
initOnBoardSD() but I couldn't find source for that.

As usually is the case, fix the things you know that are wrong and be amazed
at what other problems just seem to go away. ;)
Post Reply