Skip to content

i2c read / write function #31

@TommyLin

Description

@TommyLin
  • Test SampleCode/i2c/sample.c
  • Move i2c-core.c to i2c-core.cpp
    Warning message
$ arm-linux-gnueabihf-g++ -std=c++11 -Wall  -I ../inc   -c i2c-core.cpp
i2c-core.cpp: In function 'int i2c_read(int, short unsigned int, unsigned char*, int)':
i2c-core.cpp:20:13: warning: narrowing conversion of '(((int)reg) >> 8)' from 'int' to 'unsigned char' inside { } [-Wnarrowing]
         reg >> 8,
             ^
i2c-core.cpp:21:13: warning: narrowing conversion of '(((int)reg) & 255)' from 'int' to 'unsigned char' inside { } [-Wnarrowing]
         reg & 0xff,
             ^
--- a/project/src/i2c-core.c
+++ b/project/src/i2c-core.c
@@ -17,8 +17,8 @@ int i2c_read(int fd, unsigned short reg, unsigned char *rbuf, int len){
     i2c_data.nmsgs = 2;
     i2c_data.msgs = msgs;
     unsigned char tbuf[] = {
-        reg >> 8,
-        reg & 0xff,
+        static_cast<unsigned char>(reg >> 8),
+        static_cast<unsigned char>(reg & 0xff),
     };
     msgs[0].flags = 0;                  //写标志
     msgs[0].addr  = DEVICE_I2C_ADDR;    //i2c地址

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions