/dev/mem vs /dev/i2c-1i2c data understandingPi4J i2c error: Error writing to /dev/i2c-1 at address 0x21. Got -20001Firmware 3.18.x breaks I²C, SPI, audio, lirc, 1-wire (e.g. /dev/i2c-1, No such file or directory)Accessing GPIO from lighttpd server not workingi2c on a rpi-basic-image created with yoctoHow to allow I2C access for non-root users?Using I2C commands in Python 2Permission issue accessing /dev/mem – gpio node-rpioHow to access to I2C software with wiringpi library-Earliest place in boot for output to I2C LCD (not HDMI)

Randomness of Python's random

Can there be a single technologically advance nation, in a continent full of non-technologically advance nations

A foe leaves the reach of my 5-foot reach sword. Can I make an Opportunity Attack with my 10-foot reach whip?

How did Arya get her dagger back from Sansa?

Should one double the thirds or the fifth in chords?

Endgame: Is there significance between this dialogue between Tony and his father?

Why do we use caret (^) as the symbol for ctrl/control?

Where can I go to avoid planes overhead?

Quoting Yourself

Type-check an expression

How encryption in SQL login authentication works

Is Cola "probably the best-known" Latin word in the world? If not, which might it be?

Virus Detected - Please execute anti-virus code

What does a yield inside a yield do?

Using DeleteCases with a defined function with two arguments as a pattern

Coefficients of linear dependency

How to get a product new from and to date in phtml file in magento 2

Did we get closer to another plane than we were supposed to, or was the pilot just protecting our delicate sensibilities?

Why is B♯ higher than C♭ in 31-ET?

What was the state of the German rail system in 1944?

I need a disease

Would glacier 'trees' be plausible?

What property of a transistor makes it an amplifier?

Transferring data speed of Fast Ethernet



/dev/mem vs /dev/i2c-1


i2c data understandingPi4J i2c error: Error writing to /dev/i2c-1 at address 0x21. Got -20001Firmware 3.18.x breaks I²C, SPI, audio, lirc, 1-wire (e.g. /dev/i2c-1, No such file or directory)Accessing GPIO from lighttpd server not workingi2c on a rpi-basic-image created with yoctoHow to allow I2C access for non-root users?Using I2C commands in Python 2Permission issue accessing /dev/mem – gpio node-rpioHow to access to I2C software with wiringpi library-Earliest place in boot for output to I2C LCD (not HDMI)






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








2















What are advantages and disadvantages in accessing I2C and other interfaces directly with /dev/mem (as in this code) instead of using /dev/i2c-1 (as in most examples like this)?










share|improve this question







New contributor




Dmitry Fedorkov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.


























    2















    What are advantages and disadvantages in accessing I2C and other interfaces directly with /dev/mem (as in this code) instead of using /dev/i2c-1 (as in most examples like this)?










    share|improve this question







    New contributor




    Dmitry Fedorkov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      2












      2








      2








      What are advantages and disadvantages in accessing I2C and other interfaces directly with /dev/mem (as in this code) instead of using /dev/i2c-1 (as in most examples like this)?










      share|improve this question







      New contributor




      Dmitry Fedorkov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.












      What are advantages and disadvantages in accessing I2C and other interfaces directly with /dev/mem (as in this code) instead of using /dev/i2c-1 (as in most examples like this)?







      gpio i2c






      share|improve this question







      New contributor




      Dmitry Fedorkov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      Dmitry Fedorkov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      Dmitry Fedorkov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 15 hours ago









      Dmitry FedorkovDmitry Fedorkov

      1133




      1133




      New contributor




      Dmitry Fedorkov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Dmitry Fedorkov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Dmitry Fedorkov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




















          2 Answers
          2






          active

          oldest

          votes


















          5














          Disadvantages using /dev/mem for accessing the I²C hardware of the Raspberry Pi directly:



          • It's not portable to other hardware platforms. It's not portable to additional USB I²C adapters. It's not portable to additional bitbanged I²C.

          • You limit yourself to one-application at a time which may use the I²C, while the kernel serializes requests on /dev/i2c-nnn from multiple applications.

          • Likewise, you will run into problems when the user wants to use a kernel driver on the same I²C, for example for the handling of an RTC.

          • You have to take care of I²C bus expanders in your application code.

          Advantages:



          • ~--~--~--~ insert tumbleweed ~--~--~--~


          In addition, I recommend to use the I2C_RDWR ioctl() instead of read() and write() as you can do multiple transfers in one transaction this way. This is neccessary if multiple applications are accessing the same I²C slave concurrently.



          #include <stdio.h>
          #include <sys/types.h>
          #include <sys/stat.h>
          #include <fcntl.h>
          #include <unistd.h>
          #include <inttypes.h>
          #include <linux/i2c-dev.h>

          int main(int argc, char* argv[]) address > 0x77)
          fprintf(stderr, "ds7505-readtemp: i2c slave address has to be in the range 0x08..0x77.n");
          return 127;


          /* Open I2C device. */
          fd = open(argv[1], O_RDWR);
          if (fd < 0)
          perror("ds7505-readtemp: i2c device open");
          return 2;


          /* Select temperature register. */
          i2c_msgs[0].addr = address;
          i2c_msgs[0].flags = 0;
          i2c_msgs[0].len = 1;
          i2c_msgs[0].buf = "x00";

          i2c_msgs[1].addr = address;
          i2c_msgs[1].flags = I2C_M_RD;
          i2c_msgs[1].len = 2;
          i2c_msgs[1].buf = (char*)&rdata;

          i2c_transfer.msgs = i2c_msgs;
          i2c_transfer.nmsgs = 2;

          if (ioctl(fd, I2C_RDWR, &i2c_transfer) < 0)
          perror("ds7505-readtemp: i2c temperature read");
          return 2;
          ;

          /* Print result. */
          printf("%.4fn", ((float)((int16_t) (rdata[0] << 8





          share|improve this answer
































            4














            In addition to @Janka's answer, more disadvantages:



            • No useful interrupt or DMA processing with /dev/mem

            • Whatever utility has the permission to interact with /dev/mem, can pretty much have complete control over the system. /dev/i2c would allow access only to I2C bus.

            Advantage of /dev/mem is that it can be used for very quick proof of concept, or as help for debugging. I'd advise against using it for anything else.






            share|improve this answer























              Your Answer






              StackExchange.ifUsing("editor", function ()
              return StackExchange.using("schematics", function ()
              StackExchange.schematics.init();
              );
              , "cicuitlab");

              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "447"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: false,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              imageUploader:
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              ,
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );






              Dmitry Fedorkov is a new contributor. Be nice, and check out our Code of Conduct.









              draft saved

              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fraspberrypi.stackexchange.com%2fquestions%2f98090%2fdev-mem-vs-dev-i2c-1%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              5














              Disadvantages using /dev/mem for accessing the I²C hardware of the Raspberry Pi directly:



              • It's not portable to other hardware platforms. It's not portable to additional USB I²C adapters. It's not portable to additional bitbanged I²C.

              • You limit yourself to one-application at a time which may use the I²C, while the kernel serializes requests on /dev/i2c-nnn from multiple applications.

              • Likewise, you will run into problems when the user wants to use a kernel driver on the same I²C, for example for the handling of an RTC.

              • You have to take care of I²C bus expanders in your application code.

              Advantages:



              • ~--~--~--~ insert tumbleweed ~--~--~--~


              In addition, I recommend to use the I2C_RDWR ioctl() instead of read() and write() as you can do multiple transfers in one transaction this way. This is neccessary if multiple applications are accessing the same I²C slave concurrently.



              #include <stdio.h>
              #include <sys/types.h>
              #include <sys/stat.h>
              #include <fcntl.h>
              #include <unistd.h>
              #include <inttypes.h>
              #include <linux/i2c-dev.h>

              int main(int argc, char* argv[]) address > 0x77)
              fprintf(stderr, "ds7505-readtemp: i2c slave address has to be in the range 0x08..0x77.n");
              return 127;


              /* Open I2C device. */
              fd = open(argv[1], O_RDWR);
              if (fd < 0)
              perror("ds7505-readtemp: i2c device open");
              return 2;


              /* Select temperature register. */
              i2c_msgs[0].addr = address;
              i2c_msgs[0].flags = 0;
              i2c_msgs[0].len = 1;
              i2c_msgs[0].buf = "x00";

              i2c_msgs[1].addr = address;
              i2c_msgs[1].flags = I2C_M_RD;
              i2c_msgs[1].len = 2;
              i2c_msgs[1].buf = (char*)&rdata;

              i2c_transfer.msgs = i2c_msgs;
              i2c_transfer.nmsgs = 2;

              if (ioctl(fd, I2C_RDWR, &i2c_transfer) < 0)
              perror("ds7505-readtemp: i2c temperature read");
              return 2;
              ;

              /* Print result. */
              printf("%.4fn", ((float)((int16_t) (rdata[0] << 8





              share|improve this answer





























                5














                Disadvantages using /dev/mem for accessing the I²C hardware of the Raspberry Pi directly:



                • It's not portable to other hardware platforms. It's not portable to additional USB I²C adapters. It's not portable to additional bitbanged I²C.

                • You limit yourself to one-application at a time which may use the I²C, while the kernel serializes requests on /dev/i2c-nnn from multiple applications.

                • Likewise, you will run into problems when the user wants to use a kernel driver on the same I²C, for example for the handling of an RTC.

                • You have to take care of I²C bus expanders in your application code.

                Advantages:



                • ~--~--~--~ insert tumbleweed ~--~--~--~


                In addition, I recommend to use the I2C_RDWR ioctl() instead of read() and write() as you can do multiple transfers in one transaction this way. This is neccessary if multiple applications are accessing the same I²C slave concurrently.



                #include <stdio.h>
                #include <sys/types.h>
                #include <sys/stat.h>
                #include <fcntl.h>
                #include <unistd.h>
                #include <inttypes.h>
                #include <linux/i2c-dev.h>

                int main(int argc, char* argv[]) address > 0x77)
                fprintf(stderr, "ds7505-readtemp: i2c slave address has to be in the range 0x08..0x77.n");
                return 127;


                /* Open I2C device. */
                fd = open(argv[1], O_RDWR);
                if (fd < 0)
                perror("ds7505-readtemp: i2c device open");
                return 2;


                /* Select temperature register. */
                i2c_msgs[0].addr = address;
                i2c_msgs[0].flags = 0;
                i2c_msgs[0].len = 1;
                i2c_msgs[0].buf = "x00";

                i2c_msgs[1].addr = address;
                i2c_msgs[1].flags = I2C_M_RD;
                i2c_msgs[1].len = 2;
                i2c_msgs[1].buf = (char*)&rdata;

                i2c_transfer.msgs = i2c_msgs;
                i2c_transfer.nmsgs = 2;

                if (ioctl(fd, I2C_RDWR, &i2c_transfer) < 0)
                perror("ds7505-readtemp: i2c temperature read");
                return 2;
                ;

                /* Print result. */
                printf("%.4fn", ((float)((int16_t) (rdata[0] << 8





                share|improve this answer



























                  5












                  5








                  5







                  Disadvantages using /dev/mem for accessing the I²C hardware of the Raspberry Pi directly:



                  • It's not portable to other hardware platforms. It's not portable to additional USB I²C adapters. It's not portable to additional bitbanged I²C.

                  • You limit yourself to one-application at a time which may use the I²C, while the kernel serializes requests on /dev/i2c-nnn from multiple applications.

                  • Likewise, you will run into problems when the user wants to use a kernel driver on the same I²C, for example for the handling of an RTC.

                  • You have to take care of I²C bus expanders in your application code.

                  Advantages:



                  • ~--~--~--~ insert tumbleweed ~--~--~--~


                  In addition, I recommend to use the I2C_RDWR ioctl() instead of read() and write() as you can do multiple transfers in one transaction this way. This is neccessary if multiple applications are accessing the same I²C slave concurrently.



                  #include <stdio.h>
                  #include <sys/types.h>
                  #include <sys/stat.h>
                  #include <fcntl.h>
                  #include <unistd.h>
                  #include <inttypes.h>
                  #include <linux/i2c-dev.h>

                  int main(int argc, char* argv[]) address > 0x77)
                  fprintf(stderr, "ds7505-readtemp: i2c slave address has to be in the range 0x08..0x77.n");
                  return 127;


                  /* Open I2C device. */
                  fd = open(argv[1], O_RDWR);
                  if (fd < 0)
                  perror("ds7505-readtemp: i2c device open");
                  return 2;


                  /* Select temperature register. */
                  i2c_msgs[0].addr = address;
                  i2c_msgs[0].flags = 0;
                  i2c_msgs[0].len = 1;
                  i2c_msgs[0].buf = "x00";

                  i2c_msgs[1].addr = address;
                  i2c_msgs[1].flags = I2C_M_RD;
                  i2c_msgs[1].len = 2;
                  i2c_msgs[1].buf = (char*)&rdata;

                  i2c_transfer.msgs = i2c_msgs;
                  i2c_transfer.nmsgs = 2;

                  if (ioctl(fd, I2C_RDWR, &i2c_transfer) < 0)
                  perror("ds7505-readtemp: i2c temperature read");
                  return 2;
                  ;

                  /* Print result. */
                  printf("%.4fn", ((float)((int16_t) (rdata[0] << 8





                  share|improve this answer















                  Disadvantages using /dev/mem for accessing the I²C hardware of the Raspberry Pi directly:



                  • It's not portable to other hardware platforms. It's not portable to additional USB I²C adapters. It's not portable to additional bitbanged I²C.

                  • You limit yourself to one-application at a time which may use the I²C, while the kernel serializes requests on /dev/i2c-nnn from multiple applications.

                  • Likewise, you will run into problems when the user wants to use a kernel driver on the same I²C, for example for the handling of an RTC.

                  • You have to take care of I²C bus expanders in your application code.

                  Advantages:



                  • ~--~--~--~ insert tumbleweed ~--~--~--~


                  In addition, I recommend to use the I2C_RDWR ioctl() instead of read() and write() as you can do multiple transfers in one transaction this way. This is neccessary if multiple applications are accessing the same I²C slave concurrently.



                  #include <stdio.h>
                  #include <sys/types.h>
                  #include <sys/stat.h>
                  #include <fcntl.h>
                  #include <unistd.h>
                  #include <inttypes.h>
                  #include <linux/i2c-dev.h>

                  int main(int argc, char* argv[]) address > 0x77)
                  fprintf(stderr, "ds7505-readtemp: i2c slave address has to be in the range 0x08..0x77.n");
                  return 127;


                  /* Open I2C device. */
                  fd = open(argv[1], O_RDWR);
                  if (fd < 0)
                  perror("ds7505-readtemp: i2c device open");
                  return 2;


                  /* Select temperature register. */
                  i2c_msgs[0].addr = address;
                  i2c_msgs[0].flags = 0;
                  i2c_msgs[0].len = 1;
                  i2c_msgs[0].buf = "x00";

                  i2c_msgs[1].addr = address;
                  i2c_msgs[1].flags = I2C_M_RD;
                  i2c_msgs[1].len = 2;
                  i2c_msgs[1].buf = (char*)&rdata;

                  i2c_transfer.msgs = i2c_msgs;
                  i2c_transfer.nmsgs = 2;

                  if (ioctl(fd, I2C_RDWR, &i2c_transfer) < 0)
                  perror("ds7505-readtemp: i2c temperature read");
                  return 2;
                  ;

                  /* Print result. */
                  printf("%.4fn", ((float)((int16_t) (rdata[0] << 8






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited 13 hours ago

























                  answered 14 hours ago









                  JankaJanka

                  1,414310




                  1,414310























                      4














                      In addition to @Janka's answer, more disadvantages:



                      • No useful interrupt or DMA processing with /dev/mem

                      • Whatever utility has the permission to interact with /dev/mem, can pretty much have complete control over the system. /dev/i2c would allow access only to I2C bus.

                      Advantage of /dev/mem is that it can be used for very quick proof of concept, or as help for debugging. I'd advise against using it for anything else.






                      share|improve this answer



























                        4














                        In addition to @Janka's answer, more disadvantages:



                        • No useful interrupt or DMA processing with /dev/mem

                        • Whatever utility has the permission to interact with /dev/mem, can pretty much have complete control over the system. /dev/i2c would allow access only to I2C bus.

                        Advantage of /dev/mem is that it can be used for very quick proof of concept, or as help for debugging. I'd advise against using it for anything else.






                        share|improve this answer

























                          4












                          4








                          4







                          In addition to @Janka's answer, more disadvantages:



                          • No useful interrupt or DMA processing with /dev/mem

                          • Whatever utility has the permission to interact with /dev/mem, can pretty much have complete control over the system. /dev/i2c would allow access only to I2C bus.

                          Advantage of /dev/mem is that it can be used for very quick proof of concept, or as help for debugging. I'd advise against using it for anything else.






                          share|improve this answer













                          In addition to @Janka's answer, more disadvantages:



                          • No useful interrupt or DMA processing with /dev/mem

                          • Whatever utility has the permission to interact with /dev/mem, can pretty much have complete control over the system. /dev/i2c would allow access only to I2C bus.

                          Advantage of /dev/mem is that it can be used for very quick proof of concept, or as help for debugging. I'd advise against using it for anything else.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered 5 hours ago









                          domendomen

                          1413




                          1413




















                              Dmitry Fedorkov is a new contributor. Be nice, and check out our Code of Conduct.









                              draft saved

                              draft discarded


















                              Dmitry Fedorkov is a new contributor. Be nice, and check out our Code of Conduct.












                              Dmitry Fedorkov is a new contributor. Be nice, and check out our Code of Conduct.











                              Dmitry Fedorkov is a new contributor. Be nice, and check out our Code of Conduct.














                              Thanks for contributing an answer to Raspberry Pi Stack Exchange!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid


                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.

                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fraspberrypi.stackexchange.com%2fquestions%2f98090%2fdev-mem-vs-dev-i2c-1%23new-answer', 'question_page');

                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Adding axes to figuresAdding axes labels to LaTeX figuresLaTeX equivalent of ConTeXt buffersRotate a node but not its content: the case of the ellipse decorationHow to define the default vertical distance between nodes?TikZ scaling graphic and adjust node position and keep font sizeNumerical conditional within tikz keys?adding axes to shapesAlign axes across subfiguresAdding figures with a certain orderLine up nested tikz enviroments or how to get rid of themAdding axes labels to LaTeX figures

                              Luettelo Yhdysvaltain laivaston lentotukialuksista Lähteet | Navigointivalikko

                              Gary (muusikko) Sisällysluettelo Historia | Rockin' High | Lähteet | Aiheesta muualla | NavigointivalikkoInfobox OKTuomas "Gary" Keskinen Ancaran kitaristiksiProjekti Rockin' High