Writing I2C application code on NuttX
2018-02-11 14:33
1606 查看
Writing I2C application code on NuttX
Here is an I2C example code on NuttX, I have tested it on Atmel SAMV71 XPlained Pro Ultra:int i2c_demo(int argc, char *argv[]) { int fd; uint8_t mac_offset; uint8_t mac[6]; struct i2c_msg_s i2c_msg[2]; struct i2c_transfer_s i2c_transfer; int ret; char i2c_path[] = "/dev/i2c0"; uint32_t trytime = 0; begin: fd = open(i2c_path, O_RDONLY); if (fd < 0) { ret = -errno; goto outs; } /************************************************************************/ /* AT24MAC402 Sequence Read - Reading address 0x9A */ /* (AT24MAC402 Address + Write) + (0x9A) + (AT24MAC402 Address + Read) */ /************************************************************************/ mac_offset = AT24XX_MACADDR_OFFSET; i2c_msg[0].addr = CONFIG_AT24XX_ADDR; i2c_msg[0].flags = 0; i2c_msg[0].buffer = &mac_offset; i2c_msg[0].length = 1; /* Write address of where we want to read to AT24 */ i2c_msg[0].frequency = 400000; /* 400K bsp */ i2c_msg[1].addr = CONFIG_AT24XX_ADDR; i2c_msg[1].flags = I2C_M_READ; /* Write command then sequence read data */ i2c_msg[1].buffer = mac; i2c_msg[1].length = 6; i2c_msg[1].frequency = 400000; /* 400K bsp */ i2c_transfer.msgv = (struct i2c_msg_s *)i2c_msg; i2c_transfer.msgc = 2; for (trytime = 0; trytime < 20; trytime++) { memset(mac,0,6); /* Read the MAC address */ ret = ioctl(fd, I2CIOC_TRANSFER, (unsigned long)&i2c_transfer); if (ret < 0) { fprintf(stderr, "ERROR: AT24 ioctl(I2CIOC_TRANSFER) failed: %d\n", ret); goto finish; } printf("MAC in EEPROM: %02x:%02x:%02x:%02x:%02x:%02x\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); sleep(2); } finish: ret = close(fd); outs: return ret; }
相关文章推荐
- Writing code that captures videos on Android(录制视频,带相机预览)
- Error creating Django application: Error on python side. Exit code: 1的解决办法
- The incident LOST_EVENTS occured on the master. Message: error writing to the binary log, Error_code
- First ASP.NET Core Application on a Mac Using Visual Studio Code
- The incident LOST_EVENTS occured on the master. Message: error writing to the binary log, Error_code
- The incident LOST_EVENTS occured on the master. Message: error writing to the binary log, Error_code
- Reading and Writing data on application server in SAP
- Reading and Writing data on application server in SAP
- Run Android Application on device
- Application tried to present a nil modal view controller on target “Current View Controller”解决方案
- arcgis server manager - An error has occured on the server. For details please check the Event (Application) log on the web.
- Hosting your code on launchpad and bazaar
- compile pcl 1.8.1 from source code on ubuntu 16.04
- Codeforce-242E-XOR on Segment(线段树区间更新)
- 13、主线程任务太多导致异常退出(The application may be doing too much work on its main thread)
- CodeBlocks Crashed on Mac
- Discuz7.2 BUG :"Warning: sprintf() [function.sprintf]: Too few arguments in xxxxxxxx/include/discuzcode.func.php on line 369"
- 解决onActivityResult()返回resultCode总是0的问题
- FAQ(56):JSP提取attribute内容出错:javax.el.PropertyNotFoundException: Property 'Code' not found on type com
- Building a Console Application on Windows Mobile