/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;
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
New contributor
add a comment |
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
New contributor
add a comment |
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
New contributor
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
gpio i2c
New contributor
New contributor
New contributor
asked 15 hours ago
Dmitry FedorkovDmitry Fedorkov
1133
1133
New contributor
New contributor
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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
add a comment |
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.
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
add a comment |
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
add a comment |
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
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
edited 13 hours ago
answered 14 hours ago
JankaJanka
1,414310
1,414310
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered 5 hours ago
domendomen
1413
1413
add a comment |
add a comment |
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.
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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