tm_i2c.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #include <fcntl.h>
  2. #include <sys/ioctl.h>
  3. #include <unistd.h>
  4. #include <linux/i2c.h>
  5. #include <linux/i2c-dev.h>
  6. #include "tm_i2c.h"
  7. static int tm_i2c_fd;
  8. float tm_i2c_Data2Temp(unsigned int ans) {
  9. float t = ans;
  10. return (t / 1023.0 * 3.3 * 2-2.73) * 100.0;
  11. }
  12. float tm_i2c_Data2Core(unsigned int ans) {
  13. float t = ans;
  14. return t / 1023.0 * 3.3;
  15. }
  16. int tm_i2c_init() {
  17. if ((tm_i2c_fd = open("/dev/i2c-1", O_RDWR)) < 0)
  18. return 1;
  19. else
  20. return 0;
  21. }
  22. void tm_i2c_close() {
  23. close(tm_i2c_fd);
  24. }
  25. unsigned int tm_i2c_req(int fd, unsigned char addr, unsigned char cmd, unsigned int data) {
  26. int i;
  27. unsigned char buf[16];
  28. struct i2c_msg msg;
  29. tm_struct *tm = (tm_struct *) buf;
  30. struct i2c_rdwr_ioctl_data msg_rdwr;
  31. unsigned int ret;
  32. //printf("REQ from %02X cmd: %02X\n", addr, cmd);
  33. tm->cmd = cmd;
  34. tm->data_lsb = data & 0xFF;
  35. tm->data_msb = (data & 0xFF00) >> 8;
  36. /* Write CMD */
  37. msg.addr = addr;
  38. msg.flags = 0;
  39. msg.len = 3;
  40. msg.buf = buf;
  41. msg_rdwr.msgs = &msg;
  42. msg_rdwr.nmsgs = 1;
  43. if ((i = ioctl(fd, I2C_RDWR, &msg_rdwr)) < 0) {
  44. // perror("ioctl error");
  45. return -1;
  46. }
  47. /* Read result */
  48. msg.addr = addr;
  49. msg.flags = I2C_M_RD;
  50. msg.len = 3;
  51. msg.buf = buf;
  52. msg_rdwr.msgs = &msg;
  53. msg_rdwr.nmsgs = 1;
  54. if ((i = ioctl(fd, I2C_RDWR, &msg_rdwr)) < 0) {
  55. // perror("ioctl error");
  56. return -1;
  57. }
  58. //hexdump(buf, 10);
  59. ret = (tm->data_msb << 8) + tm->data_lsb;
  60. if (tm->cmd == cmd) return ret;
  61. return 0;
  62. }
  63. int tm_i2c_detect(unsigned char slot) {
  64. if (slot < 0 || slot > 31) return 0;
  65. return tm_i2c_req(tm_i2c_fd, (TM_ADDR >> 1) + slot, TM_GET_CORE0, 0);
  66. }
  67. float tm_i2c_getcore0(unsigned char slot) {
  68. if (slot < 0 || slot > 31) return 0;
  69. return tm_i2c_Data2Core(tm_i2c_req(tm_i2c_fd, (TM_ADDR >> 1) + slot, TM_GET_CORE0, 0));
  70. }
  71. float tm_i2c_getcore1(unsigned char slot) {
  72. if (slot < 0 || slot > 31) return 0;
  73. return tm_i2c_Data2Core(tm_i2c_req(tm_i2c_fd, (TM_ADDR >> 1) + slot, TM_GET_CORE1, 0));
  74. }
  75. float tm_i2c_gettemp(unsigned char slot) {
  76. if (slot < 0 || slot > 31) return 0;
  77. return tm_i2c_Data2Temp(tm_i2c_req(tm_i2c_fd, (TM_ADDR >> 1) + slot, TM_GET_TEMP, 0));
  78. }
  79. void tm_i2c_set_oe(unsigned char slot) {
  80. if (slot < 0 || slot > 31) return;
  81. tm_i2c_req(tm_i2c_fd, (TM_ADDR >> 1) + slot, TM_SET_OE, 0);
  82. }
  83. void tm_i2c_clear_oe(unsigned char slot) {
  84. if (slot < 0 || slot > 31) return;
  85. tm_i2c_req(tm_i2c_fd, (TM_ADDR >> 1) + slot, TM_SET_OE, 1);
  86. }
  87. unsigned char tm_i2c_slot2addr(unsigned char slot) {
  88. if (slot < 0 || slot > 31) return 0;
  89. return ((TM_ADDR >> 1) + slot);
  90. }