mcp23s08.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * mcp23s08.c:
  3. * Extend wiringPi with the MCP 23s08 SPI GPIO expander chip
  4. * Copyright (c) 2013 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 <stdio.h>
  25. #include <stdint.h>
  26. #include "wiringPi.h"
  27. #include "wiringPiSPI.h"
  28. #include "mcp23x0817.h"
  29. #include "mcp23s08.h"
  30. #define MCP_SPEED 4000000
  31. /*
  32. * writeByte:
  33. * Write a byte to a register on the MCP23s08 on the SPI bus.
  34. *********************************************************************************
  35. */
  36. static void writeByte (uint8_t spiPort, uint8_t devId, uint8_t reg, uint8_t data)
  37. {
  38. uint8_t spiData [4] ;
  39. spiData [0] = CMD_WRITE | ((devId & 7) << 1) ;
  40. spiData [1] = reg ;
  41. spiData [2] = data ;
  42. wiringPiSPIDataRW (spiPort, spiData, 3) ;
  43. }
  44. /*
  45. * readByte:
  46. * Read a byte from a register on the MCP23s08 on the SPI bus.
  47. *********************************************************************************
  48. */
  49. static uint8_t readByte (uint8_t spiPort, uint8_t devId, uint8_t reg)
  50. {
  51. uint8_t spiData [4] ;
  52. spiData [0] = CMD_READ | ((devId & 7) << 1) ;
  53. spiData [1] = reg ;
  54. wiringPiSPIDataRW (spiPort, spiData, 3) ;
  55. return spiData [2] ;
  56. }
  57. /*
  58. * myPinMode:
  59. *********************************************************************************
  60. */
  61. static void myPinMode (struct wiringPiNodeStruct *node, int pin, int mode)
  62. {
  63. int mask, old, reg ;
  64. reg = MCP23x08_IODIR ;
  65. mask = 1 << (pin - node->pinBase) ;
  66. old = readByte (node->data0, node->data1, reg) ;
  67. if (mode == OUTPUT)
  68. old &= (~mask) ;
  69. else
  70. old |= mask ;
  71. writeByte (node->data0, node->data1, reg, old) ;
  72. }
  73. /*
  74. * myPullUpDnControl:
  75. *********************************************************************************
  76. */
  77. static void myPullUpDnControl (struct wiringPiNodeStruct *node, int pin, int mode)
  78. {
  79. int mask, old, reg ;
  80. reg = MCP23x08_GPPU ;
  81. mask = 1 << (pin - node->pinBase) ;
  82. old = readByte (node->data0, node->data1, reg) ;
  83. if (mode == PUD_UP)
  84. old |= mask ;
  85. else
  86. old &= (~mask) ;
  87. writeByte (node->data0, node->data1, reg, old) ;
  88. }
  89. /*
  90. * myDigitalWrite:
  91. *********************************************************************************
  92. */
  93. static void myDigitalWrite (struct wiringPiNodeStruct *node, int pin, int value)
  94. {
  95. int bit, old ;
  96. bit = 1 << ((pin - node->pinBase) & 7) ;
  97. old = node->data2 ;
  98. if (value == LOW)
  99. old &= (~bit) ;
  100. else
  101. old |= bit ;
  102. writeByte (node->data0, node->data1, MCP23x08_GPIO, old) ;
  103. node->data2 = old ;
  104. }
  105. /*
  106. * myDigitalRead:
  107. *********************************************************************************
  108. */
  109. static int myDigitalRead (struct wiringPiNodeStruct *node, int pin)
  110. {
  111. int mask, value ;
  112. mask = 1 << ((pin - node->pinBase) & 7) ;
  113. value = readByte (node->data0, node->data1, MCP23x08_GPIO) ;
  114. if ((value & mask) == 0)
  115. return LOW ;
  116. else
  117. return HIGH ;
  118. }
  119. /*
  120. * mcp23s08Setup:
  121. * Create a new instance of an MCP23s08 SPI GPIO interface. We know it
  122. * has 8 pins, so all we need to know here is the SPI address and the
  123. * user-defined pin base.
  124. *********************************************************************************
  125. */
  126. int mcp23s08Setup (const int pinBase, const int spiPort, const int devId)
  127. {
  128. struct wiringPiNodeStruct *node ;
  129. if (wiringPiSPISetup (spiPort, MCP_SPEED) < 0)
  130. return FALSE ;
  131. writeByte (spiPort, devId, MCP23x08_IOCON, IOCON_INIT) ;
  132. node = wiringPiNewNode (pinBase, 8) ;
  133. node->data0 = spiPort ;
  134. node->data1 = devId ;
  135. node->pinMode = myPinMode ;
  136. node->pullUpDnControl = myPullUpDnControl ;
  137. node->digitalRead = myDigitalRead ;
  138. node->digitalWrite = myDigitalWrite ;
  139. node->data2 = readByte (spiPort, devId, MCP23x08_OLAT) ;
  140. return TRUE ;
  141. }