131 Views 3 Replies Last post: Mar 3, 2010 9:32 AM by markhillig RSS
markhillig Newbie 26 posts since
Jan 21, 2010
Currently Being Moderated

Mar 2, 2010 6:22 PM

Variables getting mixed up.

I have a function that writes a block of data flash based on an indexing scheme.  I am testing using the emulator and I have had an issue with the address variable taking on the value of a generic counter variable.  This causes the FlashWrite operation to point at the wrong location.  I looked at the register window and I see that A0 holds the address value just before entering the for loop below.  When i is incremented the A0 value (and thereby the address) is cleared and takes on the value of i ( which is in R0).  If I step iterate the loop I see both A0 and R0 increment together with the same value and when i reaches the comparison value it gets cleared and goes back into the loop (not intended).  The emulator never exits the loop.  What is going on here?

 

unsigned char epp_write_parameters (void)

{

     unsigned char status = 9;

     unsigned short block_addr;

     unsigned char block_index;

     unsigned short address = 0;

     unsigned char data_buffer[PARAMETER_BLOCK_SIZE];

     unsigned char i = 0;

     unsigned char temp_data = 0;

     unsigned char *p_data = &temp_data;

 

    

     //Find the previously written block.

     block_addr = epp_get_address(PARAMETERS);

     block_index = *(unsigned char*)block_addr;

 

 

     //Load data portion of the buffer with the parameter data.

     for(i = 0; i <= (PARAMETER_BLOCK_SIZE - 1); i++)

     {

          data_buffer[i + 1] = system_parameters.data_byte[i];

     }

 

..... (extra lines left out)

}

 

Thanks.

gpatsil Novice 208 posts since
Feb 10, 2009
Currently Being Moderated
Mar 2, 2010 10:32 PM in response to: markhillig
Re: Variables getting mixed up.

What is the value for PARAMETER_BLOCK_SIZE and what type/size of structure is "system_parameters.data_byte[i]"

 

I think what happens is that you write past the data_buffer memory and overwrite i.

 

If you shift the definition of i and data_buffer and initialize i to 0 you might not see the problem... in that case you still will have a problem (and will need to fix it) with writing past the data_buffer variable but you wont have the infinite loop and you will know how to fix the problem.

FrankL Apprentice 1,123 posts since
Mar 16, 2009
Currently Being Moderated
Mar 3, 2010 1:03 AM in response to: markhillig
Re: Variables getting mixed up.

Quite simple, you overwrite i with an array access.

An array goes from array[0] to array[SIZE-1].

In your loop you count i up to [SIZE-1]. And then you write to i+1, which is array[SIZE]. This is one element beyond the array boundary and overwrites i.

More Like This

  • Retrieving data ...

Bookmarked By (0)