tm_i2cm.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. #include <sys/mman.h>
  33. #define TM_TRIES 3
  34. static int tm_i2c_fd;
  35. unsigned leds = 0xFF;
  36. static volatile unsigned *gpiom;
  37. #define INP_GPIO(g) *(gpiom+((g)/10)) &= ~(7<<(((g)%10)*3))
  38. #define OUT_GPIO(g) *(gpiom+((g)/10)) |= (1<<(((g)%10)*3))
  39. #define GPIO_SET *(gpiom+7)
  40. #define GPIO_CLR *(gpiom+10)
  41. unsigned char slotI2C(unsigned char slot) {
  42. #ifdef USE_METABANK2
  43. unsigned char x = (slot >> 1);
  44. if (x == 6) return 4;
  45. if (x == 4) return 6;
  46. return x;
  47. #else
  48. return slot;
  49. #endif
  50. }
  51. float tm_i2c_Data2Temp(unsigned int ans) {
  52. float t = ans;
  53. return (t / 1023.0 * 3.3 * 2.0 - 2.73) * 100.0;
  54. }
  55. float tm_i2c_Data2Core(unsigned int ans) {
  56. float t = ans;
  57. return t / 1023.0 * 3.3;
  58. }
  59. int tm_i2c_init() {
  60. int i;
  61. int fd;
  62. fd = open("/dev/mem",O_RDWR|O_SYNC);
  63. // if (fd < 0) { perror("/dev/mem trouble"); return (1); }
  64. if (fd < 0) return (1);
  65. gpiom = mmap(0,4096,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0x20200000);
  66. if (gpiom == MAP_FAILED) { perror("gpio mmap trouble"); return(1); }
  67. close(fd);
  68. for(i = 0; i < 4 ; i++) {
  69. INP_GPIO(i + 22); OUT_GPIO(i + 22);
  70. GPIO_CLR = 1 << (i + 22);
  71. }
  72. INP_GPIO(27); OUT_GPIO(27);
  73. INP_GPIO(17); OUT_GPIO(17);
  74. INP_GPIO(18); OUT_GPIO(18);
  75. GPIO_CLR = 1 << 27;
  76. usleep(1000);
  77. GPIO_SET = 1 << 27;
  78. if ((tm_i2c_fd = open("/dev/i2c-1", O_RDWR)) < 0)
  79. return 1;
  80. else
  81. return 0;
  82. }
  83. void leds_push() {
  84. int i;
  85. for (i = 0; i < 16; ++i)
  86. {
  87. if (leds & (1 << i))
  88. GPIO_SET = 1 << 17;
  89. else
  90. GPIO_CLR = 1 << 17;
  91. GPIO_SET = 1 << 18;
  92. usleep(10);
  93. GPIO_CLR = 1 << 18;
  94. }
  95. }
  96. void leds_set(unsigned char b) {
  97. leds &= ~(1 << b);
  98. leds_push();
  99. }
  100. void leds_clr(unsigned char b) {
  101. leds |= (1 << b);
  102. leds_push();
  103. }
  104. void tm_i2c_close() {
  105. close(tm_i2c_fd);
  106. }
  107. unsigned int tm_i2c_req(int fd, unsigned char addr, unsigned char cmd, unsigned int data) {
  108. int i;
  109. unsigned char buf[16];
  110. struct i2c_msg msg;
  111. tm_struct *tm = (tm_struct *) buf;
  112. struct i2c_rdwr_ioctl_data msg_rdwr;
  113. unsigned int ret;
  114. //applog(LOG_DEBUG, "fd: %d, REQ from %02X cmd: %02X", fd, addr, cmd);
  115. tm->cmd = cmd;
  116. tm->data_lsb = data & 0xFF;
  117. tm->data_msb = (data & 0xFF00) >> 8;
  118. /* Write CMD */
  119. msg.addr = addr;
  120. msg.flags = 0;
  121. msg.len = 3;
  122. msg.buf = (void*)tm;
  123. msg_rdwr.msgs = &msg;
  124. msg_rdwr.nmsgs = 1;
  125. if ((i = ioctl(fd, I2C_RDWR, &msg_rdwr)) < 0) {
  126. //perror("ioctl error");
  127. return -1;
  128. }
  129. /* Read result */
  130. msg.addr = addr;
  131. msg.flags = I2C_M_RD;
  132. msg.len = 3;
  133. msg.buf = (void*)tm;
  134. msg_rdwr.msgs = &msg;
  135. msg_rdwr.nmsgs = 1;
  136. if ((i = ioctl(fd, I2C_RDWR, &msg_rdwr)) < 0) {
  137. //perror("ioctl error");
  138. return -1;
  139. }
  140. //hexdump(buf, 10);
  141. ret = (tm->data_msb << 8) + tm->data_lsb;
  142. //printf("REQ from %02X, cmd: %02X, res: %04X\n", addr, cmd, ret);
  143. if (tm->cmd == cmd) return ret;
  144. return 0;
  145. }
  146. int tm_i2c_detect(unsigned char slot) {
  147. if (slot < 0 || slot > 31) return 0;
  148. leds_set(slot >> 1);
  149. //return tm_i2c_req(tm_i2c_fd, (TM_ADDR >> 1) + (slot >> 1), TM_GET_CORE0, 0);
  150. return 1;
  151. }
  152. float tm_i2c_getcore0(unsigned char slot) {
  153. int t = 0;
  154. float v;
  155. if (slot < 0 || slot > 31) return 0;
  156. do {
  157. usleep(10000);
  158. v = tm_i2c_Data2Core(tm_i2c_req(tm_i2c_fd, (TM_ADDR >> 1) + slotI2C(slot), TM_GET_CORE0, 0));
  159. t++;
  160. } while(t <= TM_TRIES && (v == 0 || v > 3.3));
  161. if (v == 0 || v > 3.3) v = -1;
  162. return v;
  163. }
  164. float tm_i2c_getcore1(unsigned char slot) {
  165. int t = 0;
  166. float v;
  167. if (slot < 0 || slot > 31) return 0;
  168. do {
  169. usleep(10000);
  170. v = tm_i2c_Data2Core(tm_i2c_req(tm_i2c_fd, (TM_ADDR >> 1) + slotI2C(slot), TM_GET_CORE1, 0));
  171. t++;
  172. } while(t <= TM_TRIES && (v == 0 || v > 3.3));
  173. if (v == 0 || v > 3.3) v = -1;
  174. return v;
  175. }
  176. float tm_i2c_gettemp(unsigned char slot) {
  177. if (slot < 0 || slot > 31) return 0;
  178. return tm_i2c_Data2Temp(tm_i2c_req(tm_i2c_fd, (TM_ADDR >> 1) + slotI2C(slot), TM_GET_TEMP, 0));
  179. }
  180. void tm_i2c_set_oe(unsigned char slot) {
  181. int i;
  182. if (slot < 0 || slot > 15) return;
  183. for(i = 0 ; i < 4 ; i++) {
  184. if (slot & (1 << i)) GPIO_SET = 1 << (i + 22);
  185. else GPIO_CLR = 1 << (i + 22);
  186. }
  187. GPIO_CLR = 1 << 27;
  188. leds ^= (1 << slot);
  189. leds_push();
  190. }
  191. void tm_i2c_clear_oe(unsigned char slot) {
  192. if (slot < 0 || slot > 31) return;
  193. GPIO_SET = 1 << 27;
  194. }
  195. unsigned char tm_i2c_slot2addr(unsigned char slot) {
  196. if (slot < 0 || slot > 31) return 0;
  197. return ((TM_ADDR >> 1) + slotI2C(slot));
  198. }
  199. #ifdef USE_METABANK2
  200. double tm_i2c_set_voltage_abs(unsigned char slot2, double voltage) {
  201. int vid = 0, core_id;
  202. double prev = 0.0, current = 0.0;
  203. float (*get_core)(unsigned char);
  204. unsigned char slot = slotI2C(slot2);
  205. double eps = 0.01, eps_max=0.02;
  206. if ((slot2 & 1)) {
  207. core_id = TM_SET_CORE1;
  208. get_core = &tm_i2c_getcore1;
  209. } else {
  210. core_id = TM_SET_CORE0;
  211. get_core = &tm_i2c_getcore0;
  212. }
  213. if (get_core(slot2) <= 0) return -1;
  214. do {
  215. prev = current;
  216. tm_i2c_req(tm_i2c_fd, (TM_ADDR >> 1) + slot, core_id, vid);
  217. usleep(100000);
  218. current = get_core(slot2);
  219. vid++;
  220. usleep(1000);
  221. } while (current <= voltage && vid < 16);
  222. if (current - voltage <= eps) return current;
  223. if (current - voltage > eps_max || current - voltage > voltage - prev) {
  224. int tr = 0;
  225. double volt_chk;
  226. //set low
  227. vid -= 2;
  228. do {
  229. tm_i2c_req(tm_i2c_fd, (TM_ADDR >> 1) + slot, core_id, vid);
  230. usleep(100000);
  231. volt_chk = get_core(slot2);
  232. tr++;
  233. } while(volt_chk > current && tr < TM_TRIES);
  234. }
  235. usleep(100000);
  236. return get_core(slot2);
  237. }
  238. unsigned int tm_i2c_set_vid(unsigned char slot, unsigned char vid) {
  239. int tr = 0;
  240. unsigned int res;
  241. if (slot < 0 || slot > 31) return -1;
  242. do {
  243. res = tm_i2c_req(tm_i2c_fd, (TM_ADDR >> 1) + slotI2C(slot), (slot & 1) ? TM_SET_CORE1 : TM_SET_CORE0, vid);
  244. tr++;
  245. } while(tr < TM_TRIES && res < 0);
  246. return res;
  247. }
  248. #endif