lm73.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * (C) Copyright 2007-2008
  3. * Larry Johnson, lrj@acm.org
  4. *
  5. * based on dtt/lm75.c which is ...
  6. *
  7. * (C) Copyright 2001
  8. * Bill Hunter, Wave 7 Optics, williamhunter@mediaone.net
  9. *
  10. * See file CREDITS for list of people who contributed to this
  11. * project.
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License as
  15. * published by the Free Software Foundation; either version 2 of
  16. * the License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  26. * MA 02111-1307 USA
  27. */
  28. /*
  29. * National Semiconductor LM73 Temperature Sensor
  30. */
  31. #include <common.h>
  32. #include <i2c.h>
  33. #include <dtt.h>
  34. /*
  35. * Device code
  36. */
  37. #define DTT_I2C_DEV_CODE 0x48 /* National Semi's LM73 device */
  38. int dtt_read(int const sensor, int const reg)
  39. {
  40. int dlen;
  41. uint8_t data[2];
  42. /*
  43. * Validate 'reg' param and get register size.
  44. */
  45. switch (reg) {
  46. case DTT_CONFIG:
  47. case DTT_CONTROL:
  48. dlen = 1;
  49. break;
  50. case DTT_READ_TEMP:
  51. case DTT_TEMP_HIGH:
  52. case DTT_TEMP_LOW:
  53. case DTT_ID:
  54. dlen = 2;
  55. break;
  56. default:
  57. return -1;
  58. }
  59. /*
  60. * Try to read the register at the calculated sensor address.
  61. */
  62. if (0 !=
  63. i2c_read(DTT_I2C_DEV_CODE + (sensor & 0x07), reg, 1, data, dlen))
  64. return -1;
  65. /*
  66. * Handle 2 byte result.
  67. */
  68. if (2 == dlen)
  69. return (int)((unsigned)data[0] << 8 | (unsigned)data[1]);
  70. return (int)data[0];
  71. } /* dtt_read() */
  72. int dtt_write(int const sensor, int const reg, int const val)
  73. {
  74. int dlen;
  75. uint8_t data[2];
  76. /*
  77. * Validate 'reg' param and handle register size
  78. */
  79. switch (reg) {
  80. case DTT_CONFIG:
  81. case DTT_CONTROL:
  82. dlen = 1;
  83. data[0] = (uint8_t) val;
  84. break;
  85. case DTT_TEMP_HIGH:
  86. case DTT_TEMP_LOW:
  87. dlen = 2;
  88. data[0] = (uint8_t) (val >> 8); /* MSB first */
  89. data[1] = (uint8_t) val;
  90. break;
  91. default:
  92. return -1;
  93. }
  94. /*
  95. * Write value to register at the calculated sensor address.
  96. */
  97. return 0 != i2c_write(DTT_I2C_DEV_CODE + (sensor & 0x07), reg, 1, data,
  98. dlen);
  99. } /* dtt_write() */
  100. static int _dtt_init(int const sensor)
  101. {
  102. int val;
  103. /*
  104. * Validate the Identification register
  105. */
  106. if (0x0190 != dtt_read(sensor, DTT_ID))
  107. return -1;
  108. /*
  109. * Setup THIGH (upper-limit) and TLOW (lower-limit) registers
  110. */
  111. val = CFG_DTT_MAX_TEMP << 7;
  112. if (dtt_write(sensor, DTT_TEMP_HIGH, val))
  113. return -1;
  114. val = CFG_DTT_MIN_TEMP << 7;
  115. if (dtt_write(sensor, DTT_TEMP_LOW, val))
  116. return -1;
  117. /*
  118. * Setup configuraton register
  119. */
  120. /* config = alert active low, disabled, and reset */
  121. val = 0x64;
  122. if (dtt_write(sensor, DTT_CONFIG, val))
  123. return -1;
  124. /*
  125. * Setup control/status register
  126. */
  127. /* control = temp resolution 0.25C */
  128. val = 0x00;
  129. if (dtt_write(sensor, DTT_CONTROL, val))
  130. return -1;
  131. dtt_read(sensor, DTT_CONTROL); /* clear temperature flags */
  132. return 0;
  133. } /* _dtt_init() */
  134. int dtt_init(void)
  135. {
  136. int i;
  137. unsigned char sensors[] = CONFIG_DTT_SENSORS;
  138. const char *const header = "DTT: ";
  139. for (i = 0; i < sizeof(sensors); i++) {
  140. if (0 != _dtt_init(sensors[i]))
  141. printf("%s%d FAILED INIT\n", header, i + 1);
  142. else
  143. printf("%s%d is %i C\n", header, i + 1,
  144. dtt_get_temp(sensors[i]));
  145. }
  146. return 0;
  147. } /* dtt_init() */
  148. int dtt_get_temp(int const sensor)
  149. {
  150. int const ret = dtt_read(sensor, DTT_READ_TEMP);
  151. if (ret < 0) {
  152. printf("DTT temperature read failed.\n");
  153. return 0;
  154. }
  155. return (int)((int16_t) ret + 0x0040) >> 7;
  156. } /* dtt_get_temp() */