tm_i2c.c 2.4 KB

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