What could be the reason for a "wrong" return val?

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have the following struct in a C file that hosts functions to drive the DFPlayer MP3 module (this is not in main.c):

struct DFPlayerData
{
	uint8_t numberOfTracks;
	uint8_t currentTrack;
	uint8_t playbackMode;
	uint8_t eq;
	volatile bool isPlaying;
	volatile bool isOnline;
} DFPlayerData;

Upon startup, some of these values are being set by a function inside that same C file, based on the feedback from the module, like so:

bool _DFPlayerSendQuery(uint8_t query)
{
	uint8_t tempQuery[10] = {0x7E ,0xFF ,0x06 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00, 0xEF};
	
	tempQuery[3] = query;
	int16_t checksum = _DFPlayerChecksum(tempQuery);
	tempQuery[7] = (uint8_t)(checksum >> 8);
	tempQuery[8] = (uint8_t)checksum;
	
	UARTSendArray(tempQuery, 10);
	
	for (uint8_t i = 0; i < 10; i++)
		UARTGetByte(&tempQuery[i], false);
	
	if ((tempQuery[0] == 0x7E) && (tempQuery[9] == 0xEF))
	{
		if (query == DFPLAYER_QRY_EQ)
			DFPlayerData.eq = tempQuery[6];
		else if (query == DFPLAYER_QRY_TF_TRACKS)
			DFPlayerData.numberOfTracks = tempQuery[6];
		return true;
	}
	return false;
}

The array was checked in JTAG debug mode. When query == DFPLAYER_QRY_TF_TRACKS the value of tempQuery[6] = 0x0d which is correct.

 

 

That value is assigned to a variable in the structure: DFPlayerData.numberOfTracks.

 

When I want to retrieve the value I use the following function:

uint8_t DFPlayerGetNumberOfTracks(void)
{
	return DFPlayerData.numberOfTracks;
}

And when called it returns a value of 170 and not 13, as expected.

What could cause this?

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Some possibilities:

 

The value is no more the same as had been originally set, because something overwrites it (stack overflow, bad pointer)

 

The value has never been set actually, because different objects are in use (e.g. because of a static in the declaration and usage in different modules, or an object hiding a different one of same name).

 

A missing or wrong prototype so that argument or return values are passed incorrectly.

 

Undefined behaviour like signed overflow etc. or violating strict aliasing rules,  i.e.  your expectations re. what a piece of code will do does noz match the specification.
 

avrfreaks does not support Opera. Profile inactive.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have tried using a "simple" uint8_t outside the struct but the results were exactly the same + no other line of code changes that value.

Should I go with global variables? that reduces the stack size, no?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

slow_rider wrote:
I have tried using a "simple" uint8_t outside the struct but the results were exactly the same + no other line of code changes that value.

Should I go with global variables? that reduces the stack size, no?

As a practical matter, no.

Technically, yes, but it also. *at all times* reduces the amount of memory available for the stack.

Moving a variable from automatic to global could cause stack-smashing.

 

Also, you haven't given us a lot to go on, hence the boilerplate answers.

 

Edit: tipo

International Theophysical Year seems to have been forgotten..
Anyone remember the song Jukebox Band?

Last Edited: Tue. Dec 26, 2017 - 03:28 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I moved all the variables to a globals.h file and removed all the set & get functions. This is uglier but the code is short enough so I can manage not to do something silly. If I'll need to come back to it in about a year it will probably stop working after a minor revision and I'll have to debug it. I'm not proud of it but this helped finish the project!