fwrite, fread behavior in i2c communication

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

fwrite, fread behavior in i2c communication

跳至解决方案
2,309 次查看
efl
Contributor I

Hi there,

 

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  i2c_mcf52277evb.mcp.

 

Why are fwrite and fread calls implemented in a while loop?

Examples are:

 

/* Write address within memory block */
      mem = addr & 0xFF;
      printf ("  Write to address 0x%02x ... ", mem);
      do
      {
         result = fwrite (&mem, 1, 1, fd);
      } while (result < 1);
      if (1 == result)
      {
         printf ("OK\n");
      } else {
         printf ("ERROR\n");
      }

 


      /* Page write of data */
      printf ("  Page write %d bytes ... ", length);
      result = 0;
      do
      {
         result += fwrite (buffer + result, 1, length - result, fd);
      } while (result < length);
      if (result == length)
      {
         printf ("OK\n");
      } else {
         printf ("ERROR\n");
      }

 

 

/* Read all data */
   printf ("  Read %d bytes ... ", n);
   result = 0;
   do
   {
      result += fread (buffer + result, 1, n - result, fd);
   } while (result < n);
   if (result == n)
   {
      printf ("OK\n");
   } else {
      printf ("ERROR\n");
   }

 

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?

Supposing there are multiple calls, isn't the starting address for the write/read operations becoming a problem? Or is this address adjusted automatically?

 

In the general c++  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.

 

Would be great if somebody could clarify this for me!

 

Best,

Florian

0 项奖励
回复
1 解答
1,648 次查看
PetrM
Senior Contributor I

The while loops do just the things you mentioned:

 

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 and the buffer addresses and bytes requested are adjusted according to amount previously processed.

 

It basically shows, that you can do also something else while transferring data over I2C.

 

PetrM

 

在原帖中查看解决方案

0 项奖励
回复
1 回复
1,649 次查看
PetrM
Senior Contributor I

The while loops do just the things you mentioned:

 

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 and the buffer addresses and bytes requested are adjusted according to amount previously processed.

 

It basically shows, that you can do also something else while transferring data over I2C.

 

PetrM

 

0 项奖励
回复