<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>8-bit MicrocontrollersのトピックRe: failure to return from function?</title>
    <link>https://community.nxp.com/t5/8-bit-Microcontrollers/failure-to-return-from-function/m-p/161791#M9850</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Rob,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I &lt;EM&gt;think&lt;/EM&gt; you are overwriting your function's return address.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If your "eeprom" array is PARAMETERS bytes long, then the last byte in the array is at "eeprom[PARAMETERS-1]", yet you are allowing the pointer to be incremented to&amp;nbsp; "eeprom + PARAMETERS", which is one byte outside the array.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would think it would only corrupt the local variable "addr", but if "addr" is optimized off the stack then you would be corrupting the return address.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 10 Nov 2010 07:13:32 GMT</pubDate>
    <dc:creator>rocco</dc:creator>
    <dc:date>2010-11-10T07:13:32Z</dc:date>
    <item>
      <title>failure to return from function?</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/failure-to-return-from-function/m-p/161790#M9849</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Sorry for the lack of details in that subject line. I'm working with the MC9S08QG8 and have written a function to access external EEPROM and print to UART the results.&amp;nbsp; This function uses a local array of 14 bytes.&amp;nbsp; I have a local pointer to this array which I use to do the heavy lifting.&amp;nbsp; See below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;void RawMemory(void){&amp;nbsp; word addr = 0;&amp;nbsp; byte eeprom[PARAMETERS] = {0};&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // EEPROM memory array&amp;nbsp; byte* eeprom_ptr = eeprom;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // pointer to array&amp;nbsp; NewLineChars();&amp;nbsp; SendString("Memory:"); NewLineChars();&amp;nbsp; do&amp;nbsp; {&amp;nbsp;&amp;nbsp;&amp;nbsp; i2c_error = I2CRandomRead(EEPROM_ADDR,addr,eeprom_ptr);&amp;nbsp;&amp;nbsp;&amp;nbsp; if (i2c_error)&amp;nbsp;&amp;nbsp;&amp;nbsp; {&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; i2c_clk_dly = I2C_CLK_DLY2;&amp;nbsp;&amp;nbsp; // try slower I2C clocking speed and retry&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; addr = 0;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; eeprom_ptr = eeprom;&amp;nbsp;&amp;nbsp;&amp;nbsp; // reset RAM array pointer&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; i2c_error = 0;&amp;nbsp;&amp;nbsp;&amp;nbsp; // reset flag&amp;nbsp;&amp;nbsp;&amp;nbsp; }&amp;nbsp;&amp;nbsp;&amp;nbsp; else if (*eeprom_ptr == BLANK_BYTE)&amp;nbsp;&amp;nbsp;&amp;nbsp; {&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; NewLineChars();&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; break;&amp;nbsp; // stop reporting once blank spot found&amp;nbsp;&amp;nbsp;&amp;nbsp; }&amp;nbsp;&amp;nbsp;&amp;nbsp; else&amp;nbsp;&amp;nbsp;&amp;nbsp; {&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SendAscByte(*eeprom_ptr);&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // report EEPROM value&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; TxChar(SPACE);&amp;nbsp;&amp;nbsp;&amp;nbsp; }&amp;nbsp;&amp;nbsp;&amp;nbsp; if (eeprom_ptr == eeprom + PARAMETERS)&amp;nbsp;&amp;nbsp;&amp;nbsp; {&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; CursorString();&amp;nbsp;&amp;nbsp; // command line&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; eeprom_ptr = eeprom;&amp;nbsp;&amp;nbsp;&amp;nbsp; // reset RAM array pointer&amp;nbsp;&amp;nbsp;&amp;nbsp; }&amp;nbsp;&amp;nbsp;&amp;nbsp; else eeprom_ptr++;&amp;nbsp; } while (++addr &amp;lt; EEPROM_BUFF); // end while loop} // end RawMemory()&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This function works as it should.&amp;nbsp; However, when returning from the function, the program "runs off into the weeds."&amp;nbsp; I've tried upping my stack pointer in the .prm file, as high as the compiler would let me.&amp;nbsp; No effect.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Assuming it was the size of the local array, I even tried a different approach with a single byte variable for 'eeprom'.&amp;nbsp; Still the same problem.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;RAM problems have always been a bit mysterious to me in terms of troubleshooting.&amp;nbsp; Any recommendations?&amp;nbsp; Thanks!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 29 Oct 2020 09:07:31 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/failure-to-return-from-function/m-p/161790#M9849</guid>
      <dc:creator>irob</dc:creator>
      <dc:date>2020-10-29T09:07:31Z</dc:date>
    </item>
    <item>
      <title>Re: failure to return from function?</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/failure-to-return-from-function/m-p/161791#M9850</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Rob,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I &lt;EM&gt;think&lt;/EM&gt; you are overwriting your function's return address.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If your "eeprom" array is PARAMETERS bytes long, then the last byte in the array is at "eeprom[PARAMETERS-1]", yet you are allowing the pointer to be incremented to&amp;nbsp; "eeprom + PARAMETERS", which is one byte outside the array.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would think it would only corrupt the local variable "addr", but if "addr" is optimized off the stack then you would be corrupting the return address.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 10 Nov 2010 07:13:32 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/failure-to-return-from-function/m-p/161791#M9850</guid>
      <dc:creator>rocco</dc:creator>
      <dc:date>2010-11-10T07:13:32Z</dc:date>
    </item>
    <item>
      <title>Re: failure to return from function?</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/failure-to-return-from-function/m-p/161792#M9851</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;May I suggest rewriting it into something like the following seemingly equivalent code?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;BOOL RawMemory(void){  byte eeprom_val = 0;  word i;  BOOL result = TRUE;  NewLineChars();  SendString("Memory:");  NewLineChars();  for(i=0; i&amp;lt;EEPROM_BUFF; i++)  {    if(I2CRandomRead(EEPROM_ADDR, i, &amp;amp;eeprom_val) == FALSE)    {      result = FALSE;      break;    }    else if (eeprom_val == BLANK_BYTE)    {      NewLineChars();      break;  // stop reporting once blank spot found    }    else    {      SendAscByte(eeprom_val);      // report EEPROM value      TxChar(SPACE);    }        if (i == PARAMETERS)    {      CursorString();   // command line    }  }    return result;}

/* I2C_driver.c */
static Something i2C_error;

void I2CSlowDown (void)
{
  i2c_clk_dly = I2C_CLK_DLY2;  
  i2c_error = 0;
}

BOOL I2CRandomRead (...)
{
}



/* caller: */while (RawMemory() == FALSE){
  I2CSlowDown();
}
&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 29 Oct 2020 09:07:33 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/failure-to-return-from-function/m-p/161792#M9851</guid>
      <dc:creator>Lundin</dc:creator>
      <dc:date>2020-10-29T09:07:33Z</dc:date>
    </item>
    <item>
      <title>Re: failure to return from function?</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/failure-to-return-from-function/m-p/161793#M9852</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You guys were right.&amp;nbsp; I was incrementing my pointer too far.&amp;nbsp; Wow, didn't quite know that was possible, corrupting the RTS on the stack.&amp;nbsp; Makes sense now.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Lundin, I like your function edits.&amp;nbsp; I had to make one change for the CursorString call.&amp;nbsp; I wanted each multiple of PARAMETERS to output a newline and a cursor.&amp;nbsp; See below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;BOOL RawMemory(void)
{
  byte eeprom_val = 0;
  word i;
  BOOL result = TRUE;

  NewLineChars();
  SendString("Memory:");
  NewLineChars();

  for(i=0; i&amp;lt;EEPROM_BUFF; i++)
  {
    if(I2CRandomRead(EEPROM_ADDR, i, &amp;amp;eeprom_val) == FALSE)
    {
      result = FALSE;
      break;
    }
    else if (eeprom_val == BLANK_BYTE)
    {
      NewLineChars();
      break;  // stop reporting once blank spot found
    }
    else
    {
      SendAscByte(eeprom_val);      // report EEPROM value
      TxChar(SPACE);
    }
   
    if ((i+1) % PARAMETERS == 0) // is index at the end of a line?
    {
      CursorString();   // command line
    }
  }
 
  return result;
}&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Works nicely.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 29 Oct 2020 09:07:35 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/failure-to-return-from-function/m-p/161793#M9852</guid>
      <dc:creator>irob</dc:creator>
      <dc:date>2020-10-29T09:07:35Z</dc:date>
    </item>
  </channel>
</rss>

