tm_i2c.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright 2013 gluk <glukolog@mail.ru>
  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 "config.h"
  23. #include <fcntl.h>
  24. #include <sys/ioctl.h>
  25. #include <unistd.h>
  26. #ifdef NEED_LINUX_I2C_H
  27. #include <linux/i2c.h>
  28. #endif
  29. #include <linux/i2c-dev.h>
  30. #include "logging.h"
  31. #include "tm_i2c.h"
  32. static int tm_i2c_fd;
  33. float tm_i2c_Data2Temp(unsigned int ans) {
  34. float t = ans;
  35. return (t / 1023.0 * 3.3 * 2-2.73) * 100.0;
  36. }
  37. float tm_i2c_Data2Core(unsigned int ans) {
  38. float t = ans;
  39. return t / 1023.0 * 3.3;
  40. }
  41. int tm_i2c_init() {
  42. if ((tm_i2c_fd = open("/dev/i2c-1", O_RDWR)) < 0)
  43. return 1;
  44. else
  45. return 0;
  46. }
  47. void tm_i2c_close() {
  48. close(tm_i2c_fd);
  49. }
  50. unsigned int tm_i2c_req(int fd, unsigned char addr, unsigned char cmd, unsigned int data) {
  51. int i;
  52. unsigned char buf[16];
  53. struct i2c_msg msg;
  54. tm_struct *tm = (tm_struct *) buf;
  55. struct i2c_rdwr_ioctl_data msg_rdwr;
  56. unsigned int ret;
  57. //applog(LOG_DEBUG, "REQ from %02X cmd: %02X", addr, cmd);
  58. tm->cmd = cmd;
  59. tm->data_lsb = data & 0xFF;
  60. tm->data_msb = (data & 0xFF00) >> 8;
  61. /* Write CMD */
  62. msg.addr = addr;
  63. msg.flags = 0;
  64. msg.len = 3;
  65. msg.buf = (void*)tm;
  66. msg_rdwr.msgs = &msg;
  67. msg_rdwr.nmsgs = 1;
  68. if ((i = ioctl(fd, I2C_RDWR, &msg_rdwr)) < 0) {
  69. // perror("ioctl error");
  70. return -1;
  71. }
  72. /* Read result */
  73. msg.addr = addr;
  74. msg.flags = I2C_M_RD;
  75. msg.len = 3;
  76. msg.buf = (void*)tm;
  77. msg_rdwr.msgs = &msg;
  78. msg_rdwr.nmsgs = 1;
  79. if ((i = ioctl(fd, I2C_RDWR, &msg_rdwr)) < 0) {
  80. // perror("ioctl error");
  81. return -1;
  82. }
  83. //hexdump(buf, 10);
  84. ret = (tm->data_msb << 8) + tm->data_lsb;
  85. if (tm->cmd == cmd) return ret;
  86. return 0;
  87. }
  88. int tm_i2c_detect(unsigned char slot) {
  89. if (slot < 0 || slot > 31) return 0;
  90. return tm_i2c_req(tm_i2c_fd, (TM_ADDR >> 1) + slot, TM_GET_CORE0, 0);
  91. }
  92. float tm_i2c_getcore0(unsigned char slot) {
  93. if (slot < 0 || slot > 31) return 0;
  94. return tm_i2c_Data2Core(tm_i2c_req(tm_i2c_fd, (TM_ADDR >> 1) + slot, TM_GET_CORE0, 0));
  95. }
  96. float tm_i2c_getcore1(unsigned char slot) {
  97. if (slot < 0 || slot > 31) return 0;
  98. return tm_i2c_Data2Core(tm_i2c_req(tm_i2c_fd, (TM_ADDR >> 1) + slot, TM_GET_CORE1, 0));
  99. }
  100. float tm_i2c_gettemp(unsigned char slot) {
  101. if (slot < 0 || slot > 31) return 0;
  102. return tm_i2c_Data2Temp(tm_i2c_req(tm_i2c_fd, (TM_ADDR >> 1) + slot, TM_GET_TEMP, 0));
  103. }
  104. void tm_i2c_set_oe(unsigned char slot) {
  105. if (slot < 0 || slot > 31) return;
  106. tm_i2c_req(tm_i2c_fd, (TM_ADDR >> 1) + slot, TM_SET_OE, 0);
  107. }
  108. void tm_i2c_clear_oe(unsigned char slot) {
  109. if (slot < 0 || slot > 31) return;
  110. tm_i2c_req(tm_i2c_fd, (TM_ADDR >> 1) + slot, TM_SET_OE, 1);
  111. }
  112. unsigned char tm_i2c_slot2addr(unsigned char slot) {
  113. if (slot < 0 || slot > 31) return 0;
  114. return ((TM_ADDR >> 1) + slot);
  115. }