tm_i2c.c 3.7 KB

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