tm_i2c.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright 2013 Anatoly Legkodymov
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. */
  22. #include <fcntl.h>
  23. #include <sys/ioctl.h>
  24. #include <unistd.h>
  25. #include <linux/i2c.h>
  26. #include <linux/i2c-dev.h>
  27. #include "logging.h"
  28. #include "tm_i2c.h"
  29. static int tm_i2c_fd;
  30. float tm_i2c_Data2Temp(unsigned int ans) {
  31. float t = ans;
  32. return (t / 1023.0 * 3.3 * 2-2.73) * 100.0;
  33. }
  34. float tm_i2c_Data2Core(unsigned int ans) {
  35. float t = ans;
  36. return t / 1023.0 * 3.3;
  37. }
  38. int tm_i2c_init() {
  39. if ((tm_i2c_fd = open("/dev/i2c-1", O_RDWR)) < 0)
  40. return 1;
  41. else
  42. return 0;
  43. }
  44. void tm_i2c_close() {
  45. close(tm_i2c_fd);
  46. }
  47. unsigned int tm_i2c_req(int fd, unsigned char addr, unsigned char cmd, unsigned int data) {
  48. int i;
  49. unsigned char buf[16];
  50. struct i2c_msg msg;
  51. tm_struct *tm = (tm_struct *) buf;
  52. struct i2c_rdwr_ioctl_data msg_rdwr;
  53. unsigned int ret;
  54. //applog(LOG_DEBUG, "REQ from %02X cmd: %02X", addr, cmd);
  55. tm->cmd = cmd;
  56. tm->data_lsb = data & 0xFF;
  57. tm->data_msb = (data & 0xFF00) >> 8;
  58. /* Write CMD */
  59. msg.addr = addr;
  60. msg.flags = 0;
  61. msg.len = 3;
  62. msg.buf = buf;
  63. msg_rdwr.msgs = &msg;
  64. msg_rdwr.nmsgs = 1;
  65. if ((i = ioctl(fd, I2C_RDWR, &msg_rdwr)) < 0) {
  66. // perror("ioctl error");
  67. return -1;
  68. }
  69. /* Read result */
  70. msg.addr = addr;
  71. msg.flags = I2C_M_RD;
  72. msg.len = 3;
  73. msg.buf = buf;
  74. msg_rdwr.msgs = &msg;
  75. msg_rdwr.nmsgs = 1;
  76. if ((i = ioctl(fd, I2C_RDWR, &msg_rdwr)) < 0) {
  77. // perror("ioctl error");
  78. return -1;
  79. }
  80. //hexdump(buf, 10);
  81. ret = (tm->data_msb << 8) + tm->data_lsb;
  82. if (tm->cmd == cmd) return ret;
  83. return 0;
  84. }
  85. int tm_i2c_detect(unsigned char slot) {
  86. if (slot < 0 || slot > 31) return 0;
  87. return tm_i2c_req(tm_i2c_fd, (TM_ADDR >> 1) + slot, TM_GET_CORE0, 0);
  88. }
  89. float tm_i2c_getcore0(unsigned char slot) {
  90. if (slot < 0 || slot > 31) return 0;
  91. return tm_i2c_Data2Core(tm_i2c_req(tm_i2c_fd, (TM_ADDR >> 1) + slot, TM_GET_CORE0, 0));
  92. }
  93. float tm_i2c_getcore1(unsigned char slot) {
  94. if (slot < 0 || slot > 31) return 0;
  95. return tm_i2c_Data2Core(tm_i2c_req(tm_i2c_fd, (TM_ADDR >> 1) + slot, TM_GET_CORE1, 0));
  96. }
  97. float tm_i2c_gettemp(unsigned char slot) {
  98. if (slot < 0 || slot > 31) return 0;
  99. return tm_i2c_Data2Temp(tm_i2c_req(tm_i2c_fd, (TM_ADDR >> 1) + slot, TM_GET_TEMP, 0));
  100. }
  101. void tm_i2c_set_oe(unsigned char slot) {
  102. if (slot < 0 || slot > 31) return;
  103. tm_i2c_req(tm_i2c_fd, (TM_ADDR >> 1) + slot, TM_SET_OE, 0);
  104. }
  105. void tm_i2c_clear_oe(unsigned char slot) {
  106. if (slot < 0 || slot > 31) return;
  107. tm_i2c_req(tm_i2c_fd, (TM_ADDR >> 1) + slot, TM_SET_OE, 1);
  108. }
  109. unsigned char tm_i2c_slot2addr(unsigned char slot) {
  110. if (slot < 0 || slot > 31) return 0;
  111. return ((TM_ADDR >> 1) + slot);
  112. }