0

I am attempting to read the data from an external eeprom using atmega324p. The issue is coming with the for loop. I cannot get past reading 4 pages (4 x 64B) of EEPROM data. If I increase the loop counter even from 4 to 5 I get stuck in an infinite loop of UART0 transmits. Here is the code I have currently:

// create an array to store the logged off eeprom data to and for use 
//in sending bytes to teraterm. Location 0-15 hold first assertLog
UChar aucTeraTermMsgBuf[EXT_EEPROM_PAGE_LENGTH]; //length is 64B 

void TeraTermOutputMsg(void)
{        
//configure UART0 to what I want for my purposes
TeraTermConfigUART0();

//define the starting address to read from.  Each time the button pushed 
//we will begin reading from the very beginning of memory array (aka it is a full dump of EEPROM stored data)
UInt16 eepromReadAddress = 0x0000;

//Loop thru whole EEPROM 1 page of data at a time and send it.  
//Cant get past 4 pages though.... TODO FIX THIS 
for(UInt16 uspagesSent = 0; uspagesSent < 4; uspagesSent++)
 {
    //get one page of the log from the specified address in the eeprom and store it to an 
    //array of type UChar and length of 1 page (64B).
    TeraTermGetAssertLog(eepromReadAddress);

    //send the 1 page of eeprom data to teraterm
    TeraTermUART0Transmit();

    //update the next read address to pass the get log function
    eepromReadAddress += EXT_EEPROM_PAGE_LENGTH; //length is 64Bytes

 }

}

//Gets data from the EEPROM and puts it into an array in prep to send out UART0 to PC
void TeraTermGetAssertLog(UInt16 nextReadAddress)
{
//initialize the array we are storing the EEPROM data to
for(UInt16 i = 0; i < EXT_EEPROM_PAGE_LENGTH; i++)
    aucTeraTermMsgBuf[i] = 0;

//this will read the bytes from EEPROM and load them into an array
ExtEEPROM_Read(aucTeraTermMsgBuf, nextReadAddress);             
}

//function that will output string of data out to PC
void TeraTermUART0Transmit(void)
{
//array to encode the tera term message to using sprintf.  
//Takes each hex value (0x_ _ ) and converts it to ASCII code for those numbers/letters
UChar encodeMsgArray[2];
//next character to be transmitted
UChar nextChar;

//counter for bytes sent.  After 16 (1 log) want to send new line break point
UInt8 byteCount = 0x01;

//loop through the message buffer and send output to teraterm
for(UInt16 i = 0; i < EXT_EEPROM_PAGE_LENGTH; i++)
{
    //get next char from the TeraTerm output buffer array
    nextChar = aucTeraTermMsgBuf[i];

    //convert the char hex value into the ascii code for the 2 letters/numbers present in 1 hex value
    sprintf(encodeMsgArray, "%2.2x", nextChar);

    //transmit those two ascii codes on the uart0 to teraterm with a space after
    UART0_Transmit(encodeMsgArray[0]);
    UART0_Transmit(encodeMsgArray[1]);
    UART0_Transmit(" ");    //send a space so text file parser can distinguish between bytes of data


    //if we have sent a whole assert log, print next to new line
    if(byteCount == 16)
    {
        byteCount = 0x00;
        sprintf(encodeMsgArray, "\r\n");
        UART0_Transmit(encodeMsgArray[0]);
        UART0_Transmit(encodeMsgArray[1]);

    }

    ++byteCount;
}

}

I know it might be kind of a lot to read through, but I am stumped. When I put my loop counter variable (pagesSent) in the watch window, it gets optmized, when I declare as volatile it still gets optimized, but when the max value is set to 4 the loop exits properly, but if max value is higher than 4 get stuck... Thanks for the help.

lbeh
  • 53
  • 3

1 Answers1

0

I figured this out by looking at optmization posts. If the loop variable is not used, the compiler sets the variable to the max value. I do not completely understand compilers or optimization. But that being said, if I just pass the uspagesSent variable to TeraTermUART0Transmit and then multiply it by 64 in that function to get the next (page) address, all is well. Hopefully this is helpful to someone down the line. Here is link to helpful post:for loop being ignored (optimized?) out

Community
  • 1
  • 1
lbeh
  • 53
  • 3