Guest User

Untitled

a guest
Nov 23rd, 2017
594
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.65 KB | None | 0 0
  1. /* ======================================================================= *//**
  2. * @Component OMAPCONF
  3. * @Filename i2cget.c
  4. * @Description A user-space program to read an I2C register.
  5. * @Copyright GPL
  6. *//*======================================================================== */
  7. /*
  8. Small example using parts of the below mentioned sources
  9.  
  10.  
  11. i2cget.c - A user-space program to read an I2C register.
  12. Copyright (C) 2005-2010 Jean Delvare <khali@linux-fr.org>
  13.  
  14. Based on i2cset.c:
  15. Copyright (C) 2001-2003 Frodo Looijaard <frodol@dds.nl>, and
  16. Mark D. Studebaker <mdsxyz123@yahoo.com>
  17. Copyright (C) 2004-2005 Jean Delvare <khali@linux-fr.org>
  18.  
  19. This program is free software; you can redistribute it and/or modify
  20. it under the terms of the GNU General Public License as published by
  21. the Free Software Foundation; either version 2 of the License, or
  22. (at your option) any later version.
  23.  
  24. This program is distributed in the hope that it will be useful,
  25. but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. GNU General Public License for more details.
  28.  
  29. You should have received a copy of the GNU General Public License
  30. along with this program; if not, write to the Free Software
  31. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  32. MA 02110-1301 USA.
  33. */
  34.  
  35. #include <errno.h>
  36. #include <string.h>
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <unistd.h>
  40. #include "omapconf/i2c-tools/i2c-dev.h"
  41.  
  42. #include <sys/types.h>
  43. #include <sys/stat.h>
  44. #include <sys/param.h> /* for NAME_MAX */
  45. #include <string.h>
  46. #include <strings.h> /* for strcasecmp() */
  47. #include <stdio.h>
  48. #include <stdlib.h>
  49. #include <unistd.h>
  50. #include <limits.h>
  51. #include <dirent.h>
  52. #include <fcntl.h>
  53. #include <errno.h>
  54.  
  55. //#include <i2cbusses.h>
  56. //#include <util.h>
  57. //#include <version.h>
  58.  
  59. int
  60. open_i2c_dev (int i2cbus, char *filename, size_t size, int quiet)
  61. {
  62. int file;
  63.  
  64. snprintf (filename, size, "/dev/i2c/%d", i2cbus);
  65. filename[size - 1] = '\0';
  66. file = open (filename, O_RDWR);
  67.  
  68. if (file < 0 && (errno == ENOENT || errno == ENOTDIR))
  69. {
  70. sprintf (filename, "/dev/i2c-%d", i2cbus);
  71. file = open (filename, O_RDWR);
  72. }
  73.  
  74. if (file < 0 && !quiet)
  75. {
  76. if (errno == ENOENT)
  77. {
  78. fprintf (stderr, "Error: Could not open file "
  79. "`/dev/i2c-%d' or `/dev/i2c/%d': %s\n",
  80. i2cbus, i2cbus, strerror (ENOENT));
  81. }
  82. else
  83. {
  84. fprintf (stderr, "Error: Could not open file "
  85. "`%s': %s\n", filename, strerror (errno));
  86. if (errno == EACCES)
  87. fprintf (stderr, "Run as root?\n");
  88. }
  89. }
  90.  
  91. return file;
  92. }
  93.  
  94. int
  95. set_slave_addr (int file, int address, int force)
  96. {
  97. /* With force, let the user read from/write to the registers
  98. even when a driver is also running */
  99. if (ioctl (file, force ? I2C_SLAVE_FORCE : I2C_SLAVE, address) < 0)
  100. {
  101. fprintf (stderr,
  102. "Error: Could not set address to 0x%02x: %s\n",
  103. address, strerror (errno));
  104. return -errno;
  105. }
  106.  
  107. return 0;
  108. }
  109.  
  110. int
  111. ppma (int speed)
  112. {
  113. return 1500 + speed;
  114. }
  115.  
  116.  
  117. void
  118. setM (int file, int id, int speed)
  119. {
  120. // usleep (200);
  121. int ret = -1;
  122. while (1)
  123. {
  124. printf ("Setting motor %d to %d\n", id, speed);
  125. ret = i2c_smbus_write_word_data (file, id, speed);
  126. if (ret == 0)
  127. return;
  128. usleep (50);
  129. }
  130. }
  131.  
  132. int
  133. main (int argc, char *argv[])
  134. {
  135. char *end;
  136. int res, i2cbus, address, size, file;
  137. int daddress;
  138. char filename[20];
  139. int pec = 0;
  140. int flags = 0;
  141. int force = 0, yes = 0, version = 0;
  142.  
  143.  
  144. //I2C bus 1 filename /dev/i2c-1 address 41 Daddress 2 size 2 pec 0
  145.  
  146.  
  147.  
  148. i2cbus = 1; //lookup_i2c_bus("1");
  149. if (i2cbus < 0)
  150. return -1;
  151.  
  152. address = 41; // parse_i2c_address("0x29");
  153. if (address < 0)
  154. return -1;
  155.  
  156. daddress = 2;
  157.  
  158. /* 'w': */ size = I2C_SMBUS_WORD_DATA;
  159.  
  160.  
  161. file = open_i2c_dev (i2cbus, filename, sizeof (filename), 0);
  162. if (file < 0
  163. //|| check_funcs(file, size, daddress, pec)
  164. || set_slave_addr (file, address, force))
  165. exit (1);
  166.  
  167.  
  168. printf ("I2C bus %d filename %s address %d Daddress %d size %d pec %d\n",
  169. i2cbus, filename, address, daddress, size, pec);
  170.  
  171. // if (!yes && !confirm(filename, address, size, daddress, pec))
  172. // exit(0);
  173.  
  174. if (pec && ioctl (file, I2C_PEC, 1) < 0)
  175. {
  176. fprintf (stderr, "Error: Could not set PEC: %s\n", strerror (errno));
  177. close (file);
  178. exit (1);
  179. }
  180.  
  181.  
  182.  
  183.  
  184. /*
  185.  
  186.  
  187. daddress=0x4;
  188. int value = 1600;
  189. res = i2c_smbus_write_word_data(file, daddress, value);
  190.  
  191. for(int i =0; i < 1000; i++){
  192. daddress =0;
  193. res = i2c_smbus_read_word_data(file, daddress);
  194. printf("Left 0x%0*x ", size == I2C_SMBUS_WORD_DATA ? 4 : 2, res);
  195.  
  196. daddress =2;
  197. res = i2c_smbus_read_word_data(file, daddress);
  198. printf("Right 0x%0*x\n", size == I2C_SMBUS_WORD_DATA ? 4 : 2, res);
  199.  
  200. }
  201.  
  202.  
  203. daddress=0x4;
  204. value = 1500;
  205. res = i2c_smbus_write_word_data(file, daddress, value);
  206. */
  207.  
  208.  
  209. while (1)
  210. {
  211.  
  212. int sr = i2c_smbus_read_word_data (file, 2);
  213. int sl = i2c_smbus_read_word_data (file, 0);
  214.  
  215. usleep (100);
  216.  
  217. if (sr == -1 || sl == -1)
  218. continue;
  219.  
  220. //max is 500
  221. int tv = 400;
  222. int fs = 20;
  223. int wl = 0x04;
  224. int wr = 0x03;
  225. int fr = 30;
  226. int br = 5;
  227.  
  228.  
  229. // printf ("sr %d, sl %d\n", sr, sl);
  230.  
  231. if (sr < tv && sl < tv)
  232. {
  233. printf ("mode: both off line\n");
  234. // servoRight.setServo(-fs);
  235. setM (file, wr, ppma (-fs));
  236. // servoLeft.setServo(fs);
  237. setM (file, wl, ppma (fs));
  238. }
  239. else
  240. {
  241. // usleep (100);
  242. if ((sr < tv && sl > tv))
  243. {
  244. printf ("mode: left sensor at line\n");
  245. //servoRight.setServo(-(fr));
  246. setM (file, wr, ppma (-fr));
  247. //servoLeft.setServo(br);
  248. setM (file, wl, ppma (br));
  249. }
  250. // else
  251. {
  252. // usleep (100);
  253. if ((sr > tv && sl < tv))
  254. {
  255. printf ("mode: right sensor at line\n");
  256. // servoRight.setServo(-(br));
  257. setM (file, wr, ppma (-br));
  258. // servoLeft.setServo(fr);
  259. setM (file, wl, ppma (fr));
  260. }
  261. }
  262. }
  263.  
  264.  
  265. }
  266.  
  267.  
  268.  
  269. close (file);
  270.  
  271. if (res < 0)
  272. {
  273. fprintf (stderr, "Error: Read failed\n");
  274. exit (2);
  275. }
  276.  
  277.  
  278. exit (0);
  279. }
  280.  
  281.  
  282. /**
  283. * Function: i2cget
  284. * Role: read given data from given address of given device of given I2C bus
  285. * Parameters:
  286. * i2cbus: I2C bus number
  287. * address: I2C device address
  288. * daddress: I2C device register address
  289. * data: I2C device register content (returned)
  290. * Return:
  291. * 0 in case of success
  292. * -1 in case of incorrect argument
  293. * -8 in case of i2c dev cannot be opened
  294. * -4 in case of I2C read error
  295. */
  296. int
  297. i2cget (unsigned int i2cbus, unsigned int address, unsigned int daddress,
  298. unsigned int *data)
  299. {
  300. int res, file;
  301. char filename[20];
  302.  
  303. *data = 0;
  304. if (i2cbus > 0xFF)
  305. {
  306. printf ("Error: I2C bus out of range (0-255)!\n");
  307. return -1;
  308. }
  309.  
  310. if (address > 0xFF)
  311. {
  312. printf ("Error: Chip address invalid!\n");
  313. return -1;
  314. }
  315.  
  316. if (daddress > 0xFF)
  317. {
  318. printf ("Error: Data address invalid!\n");
  319. return -1;
  320. }
  321.  
  322. file = open_i2c_dev (i2cbus, filename, sizeof (filename), 1);
  323. if (file < 0
  324. //|| check_funcs(file, I2C_SMBUS_BYTE_DATA, daddress, 0)
  325. || set_slave_addr (file, address, 1))
  326. return -8;
  327.  
  328. res = i2c_smbus_read_byte_data (file, daddress);
  329. close (file);
  330. if (res < 0)
  331. {
  332. fprintf (stderr, "Error: I2C Read failed\n");
  333. return -4;
  334. }
  335. *data = res;
  336.  
  337. return 0;
  338. }
  339.  
  340.  
  341. /**
  342. * Function: i2cget_word
  343. * Role: read given data from given address of given device of given I2C bus
  344. * Parameters:
  345. * i2cbus: I2C bus number
  346. * address: I2C device address
  347. * daddress: I2C device register address
  348. * data: I2C device register content (returned)
  349. * Return:
  350. * 0 in case of success
  351. * -1 in case of incorrect argument
  352. * -8 in case of i2c dev cannot be opened
  353. * -4 in case of I2C read error
  354. */
  355. int
  356. i2cget_word (unsigned int i2cbus, unsigned int address, unsigned int daddress,
  357. unsigned int *data)
  358. {
  359. int res, file;
  360. char filename[20];
  361.  
  362. *data = 0;
  363. if (i2cbus > 0xFF)
  364. {
  365. printf ("Error: I2C bus out of range (0-255)!\n");
  366. return -1;
  367. }
  368.  
  369. if (address > 0xFF)
  370. {
  371. printf ("Error: Chip address invalid!\n");
  372. return -1;
  373. }
  374.  
  375. if (daddress > 0xFF)
  376. {
  377. printf ("Error: Data address invalid!\n");
  378. return -1;
  379. }
  380.  
  381. file = open_i2c_dev (i2cbus, filename, sizeof (filename), 1);
  382. if (file < 0
  383. //|| check_funcs(file, I2C_SMBUS_WORD_DATA, daddress, 0)
  384. || set_slave_addr (file, address, 1))
  385. return -8;
  386.  
  387. res = i2c_smbus_read_word_data (file, daddress);
  388. close (file);
  389. if (res < 0)
  390. {
  391. fprintf (stderr, "Error: I2C Read failed\n");
  392. return -4;
  393. }
  394. *data = res;
  395.  
  396. return 0;
  397. }
Add Comment
Please, Sign In to add comment