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.
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.
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.
Thank you for the replies. Does this explain why my "address" variable is getting overwritten? In this case PARAMETER_BLOCK_SIZE = 10 and system_parameters is based on a struct with 9 elements. FrankL's suggestion was correct in regards to the loop execution and it also corrects the address overwrite problem. Setting compiler optimization to 03 fixed my issue but FrankL's suggestion fixes the issue as well. Thanks again.
©2003–2009 Renesas Technology Corp. All rights reserved. Using Our Website | Privacy
Contact us