max31855.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * max31855.c:
  3. * Extend wiringPi with the max31855 SPI Analog to Digital convertor
  4. * Copyright (c) 2012-2015 Gordon Henderson
  5. ***********************************************************************
  6. * This file is part of wiringPi:
  7. * https://projects.drogon.net/raspberry-pi/wiringpi/
  8. *
  9. * wiringPi is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Lesser General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * wiringPi is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with wiringPi.
  21. * If not, see <http://www.gnu.org/licenses/>.
  22. ***********************************************************************
  23. */
  24. #include <byteswap.h>
  25. #include <stdint.h>
  26. #include <wiringPi.h>
  27. #include <wiringPiSPI.h>
  28. #include "max31855.h"
  29. static int myAnalogRead (struct wiringPiNodeStruct *node, int pin)
  30. {
  31. uint32_t spiData ;
  32. int temp ;
  33. int chan = pin - node->pinBase ;
  34. wiringPiSPIDataRW (node->fd, (unsigned char *)&spiData, 4) ;
  35. spiData = __bswap_32(spiData) ;
  36. switch (chan)
  37. {
  38. case 0: // Existing read - return raw value * 4
  39. spiData >>= 18 ;
  40. temp = spiData & 0x1FFF ; // Bottom 13 bits
  41. if ((spiData & 0x2000) != 0) // Negative
  42. temp = -temp ;
  43. return temp ;
  44. case 1: // Return error bits
  45. return spiData & 0x7 ;
  46. case 2: // Return temp in C * 10
  47. spiData >>= 18 ;
  48. temp = spiData & 0x1FFF ; // Bottom 13 bits
  49. if ((spiData & 0x2000) != 0) // Negative
  50. temp = -temp ;
  51. return (int)((((double)temp * 25) + 0.5) / 10.0) ;
  52. case 3: // Return temp in F * 10
  53. spiData >>= 18 ;
  54. temp = spiData & 0x1FFF ; // Bottom 13 bits
  55. if ((spiData & 0x2000) != 0) // Negative
  56. temp = -temp ;
  57. return (int)((((((double)temp * 0.25 * 9.0 / 5.0) + 32.0) * 100.0) + 0.5) / 10.0) ;
  58. default: // Who knows...
  59. return 0 ;
  60. }
  61. }
  62. /*
  63. * max31855Setup:
  64. * Create a new wiringPi device node for an max31855 on the Pi's
  65. * SPI interface.
  66. *********************************************************************************
  67. */
  68. int max31855Setup (const int pinBase, int spiChannel)
  69. {
  70. struct wiringPiNodeStruct *node ;
  71. if (wiringPiSPISetup (spiChannel, 5000000) < 0) // 5MHz - prob 4 on the Pi
  72. return FALSE ;
  73. node = wiringPiNewNode (pinBase, 4) ;
  74. node->fd = spiChannel ;
  75. node->analogRead = myAnalogRead ;
  76. return TRUE ;
  77. }