Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

mcp23s17.c 5.3 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * mcp23s17.c:
  3. * Extend wiringPi with the MCP 23s17 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 "mcp23s17.h"
  30. #define MCP_SPEED 4000000
  31. /*
  32. * writeByte:
  33. * Write a byte to a register on the MCP23s17 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 MCP23s17 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. pin -= node->pinBase ;
  65. if (pin < 8) // Bank A
  66. reg = MCP23x17_IODIRA ;
  67. else
  68. {
  69. reg = MCP23x17_IODIRB ;
  70. pin &= 0x07 ;
  71. }
  72. mask = 1 << pin ;
  73. old = readByte (node->data0, node->data1, reg) ;
  74. if (mode == OUTPUT)
  75. old &= (~mask) ;
  76. else
  77. old |= mask ;
  78. writeByte (node->data0, node->data1, reg, old) ;
  79. }
  80. /*
  81. * myPullUpDnControl:
  82. *********************************************************************************
  83. */
  84. static void myPullUpDnControl (struct wiringPiNodeStruct *node, int pin, int mode)
  85. {
  86. int mask, old, reg ;
  87. pin -= node->pinBase ;
  88. if (pin < 8) // Bank A
  89. reg = MCP23x17_GPPUA ;
  90. else
  91. {
  92. reg = MCP23x17_GPPUB ;
  93. pin &= 0x07 ;
  94. }
  95. mask = 1 << pin ;
  96. old = readByte (node->data0, node->data1, reg) ;
  97. if (mode == PUD_UP)
  98. old |= mask ;
  99. else
  100. old &= (~mask) ;
  101. writeByte (node->data0, node->data1, reg, old) ;
  102. }
  103. /*
  104. * myDigitalWrite:
  105. *********************************************************************************
  106. */
  107. static void myDigitalWrite (struct wiringPiNodeStruct *node, int pin, int value)
  108. {
  109. int bit, old ;
  110. pin -= node->pinBase ; // Pin now 0-15
  111. bit = 1 << (pin & 7) ;
  112. if (pin < 8) // Bank A
  113. {
  114. old = node->data2 ;
  115. if (value == LOW)
  116. old &= (~bit) ;
  117. else
  118. old |= bit ;
  119. writeByte (node->data0, node->data1, MCP23x17_GPIOA, old) ;
  120. node->data2 = old ;
  121. }
  122. else // Bank B
  123. {
  124. old = node->data3 ;
  125. if (value == LOW)
  126. old &= (~bit) ;
  127. else
  128. old |= bit ;
  129. writeByte (node->data0, node->data1, MCP23x17_GPIOB, old) ;
  130. node->data3 = old ;
  131. }
  132. }
  133. /*
  134. * myDigitalRead:
  135. *********************************************************************************
  136. */
  137. static int myDigitalRead (struct wiringPiNodeStruct *node, int pin)
  138. {
  139. int mask, value, gpio ;
  140. pin -= node->pinBase ;
  141. if (pin < 8) // Bank A
  142. gpio = MCP23x17_GPIOA ;
  143. else
  144. {
  145. gpio = MCP23x17_GPIOB ;
  146. pin &= 0x07 ;
  147. }
  148. mask = 1 << pin ;
  149. value = readByte (node->data0, node->data1, gpio) ;
  150. if ((value & mask) == 0)
  151. return LOW ;
  152. else
  153. return HIGH ;
  154. }
  155. /*
  156. * mcp23s17Setup:
  157. * Create a new instance of an MCP23s17 SPI GPIO interface. We know it
  158. * has 16 pins, so all we need to know here is the SPI address and the
  159. * user-defined pin base.
  160. *********************************************************************************
  161. */
  162. int mcp23s17Setup (const int pinBase, const int spiPort, const int devId)
  163. {
  164. struct wiringPiNodeStruct *node ;
  165. if (wiringPiSPISetup (spiPort, MCP_SPEED) < 0)
  166. return FALSE ;
  167. writeByte (spiPort, devId, MCP23x17_IOCON, IOCON_INIT | IOCON_HAEN) ;
  168. writeByte (spiPort, devId, MCP23x17_IOCONB, IOCON_INIT | IOCON_HAEN) ;
  169. node = wiringPiNewNode (pinBase, 16) ;
  170. node->data0 = spiPort ;
  171. node->data1 = devId ;
  172. node->pinMode = myPinMode ;
  173. node->pullUpDnControl = myPullUpDnControl ;
  174. node->digitalRead = myDigitalRead ;
  175. node->digitalWrite = myDigitalWrite ;
  176. node->data2 = readByte (spiPort, devId, MCP23x17_OLATA) ;
  177. node->data3 = readByte (spiPort, devId, MCP23x17_OLATB) ;
  178. return TRUE ;
  179. }