tm_i2c.c 2.5 KB

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