<?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>MQX Software SolutionsのトピックRe: fwrite, fread behavior in i2c communication</title>
    <link>https://community.nxp.com/t5/MQX-Software-Solutions/fwrite-fread-behavior-in-i2c-communication/m-p/195126#M4021</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;The while loops do just the things you mentioned:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In interrupt mode, fread/fwrite are non-blocking and return actual amount of bytes read from/written to driver internal buffers. This value could be less then requested amount. So the while loops are there to complete the whole operation in several stages&amp;nbsp;and the buffer addresses and bytes requested&amp;nbsp;are adjusted according to amount previously processed.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It basically shows, that you can do&amp;nbsp;also something else while transferring data over I2C.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;PetrM&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 01 Apr 2011 16:27:06 GMT</pubDate>
    <dc:creator>PetrM</dc:creator>
    <dc:date>2011-04-01T16:27:06Z</dc:date>
    <item>
      <title>fwrite, fread behavior in i2c communication</title>
      <link>https://community.nxp.com/t5/MQX-Software-Solutions/fwrite-fread-behavior-in-i2c-communication/m-p/195125#M4020</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi there,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I got a question considering the usage of I/O drivers functions fwrite() and fread(). Specifically in the context of the i2c device. I considered the file "eeprom_int.h" in the mqx 3.6.2 example project&amp;nbsp; i2c_mcf52277evb.mcp.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Why are fwrite and fread calls implemented in a while loop?&lt;/P&gt;&lt;P&gt;Examples are:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/* Write address within memory block */&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mem = addr &amp;amp; 0xFF;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; printf ("&amp;nbsp; Write to address 0x%02x ... ", mem);&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; do&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; result = fwrite (&amp;amp;mem, 1, 1, fd);&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; } while (result &amp;lt; 1);&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (1 == result)&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; printf ("OK\n");&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; } else {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; printf ("ERROR\n");&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /* Page write of data */&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; printf ("&amp;nbsp; Page write %d bytes ... ", length);&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; result = 0;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; do&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; result += fwrite (buffer + result, 1, length - result, fd);&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; } while (result &amp;lt; length);&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (result == length)&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; printf ("OK\n");&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; } else {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; printf ("ERROR\n");&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/* Read all data */&lt;BR /&gt;&amp;nbsp;&amp;nbsp; printf ("&amp;nbsp; Read %d bytes ... ", n);&lt;BR /&gt;&amp;nbsp;&amp;nbsp; result = 0;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; do&lt;BR /&gt;&amp;nbsp;&amp;nbsp; {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; result += fread (buffer + result, 1, n - result, fd);&lt;BR /&gt;&amp;nbsp;&amp;nbsp; } while (result &amp;lt; n);&lt;BR /&gt;&amp;nbsp;&amp;nbsp; if (result == n)&lt;BR /&gt;&amp;nbsp;&amp;nbsp; {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; printf ("OK\n");&lt;BR /&gt;&amp;nbsp;&amp;nbsp; } else {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; printf ("ERROR\n");&lt;BR /&gt;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there any chance fwrite and fread functions return a non-negative value different from the specified amount of bytes to write/read? And in this case, there will be multiple fread/fwrite calls in a while clause?&lt;/P&gt;&lt;P&gt;Supposing there are multiple calls, isn't the starting address for the write/read operations becoming a problem? Or is this address adjusted automatically?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In the general c++&amp;nbsp; function description of fwrite it says that a return value different from the specified amount indicates an error. For fread, it might also be that the end of file was reached. Therefore i do not see the purpose of the while loops in the example.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Would be great if somebody could clarify this for me!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Best,&lt;/P&gt;&lt;P&gt;Florian&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 30 Mar 2011 13:52:32 GMT</pubDate>
      <guid>https://community.nxp.com/t5/MQX-Software-Solutions/fwrite-fread-behavior-in-i2c-communication/m-p/195125#M4020</guid>
      <dc:creator>efl</dc:creator>
      <dc:date>2011-03-30T13:52:32Z</dc:date>
    </item>
    <item>
      <title>Re: fwrite, fread behavior in i2c communication</title>
      <link>https://community.nxp.com/t5/MQX-Software-Solutions/fwrite-fread-behavior-in-i2c-communication/m-p/195126#M4021</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;The while loops do just the things you mentioned:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In interrupt mode, fread/fwrite are non-blocking and return actual amount of bytes read from/written to driver internal buffers. This value could be less then requested amount. So the while loops are there to complete the whole operation in several stages&amp;nbsp;and the buffer addresses and bytes requested&amp;nbsp;are adjusted according to amount previously processed.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It basically shows, that you can do&amp;nbsp;also something else while transferring data over I2C.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;PetrM&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 01 Apr 2011 16:27:06 GMT</pubDate>
      <guid>https://community.nxp.com/t5/MQX-Software-Solutions/fwrite-fread-behavior-in-i2c-communication/m-p/195126#M4021</guid>
      <dc:creator>PetrM</dc:creator>
      <dc:date>2011-04-01T16:27:06Z</dc:date>
    </item>
  </channel>
</rss>

