You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

190 rivejä
4.6 KiB

  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. int x ;
  129. struct wiringPiNodeStruct *node ;
  130. if ((x = wiringPiSPISetup (spiPort, MCP_SPEED)) < 0)
  131. return x ;
  132. writeByte (spiPort, devId, MCP23x08_IOCON, IOCON_INIT) ;
  133. node = wiringPiNewNode (pinBase, 8) ;
  134. node->data0 = spiPort ;
  135. node->data1 = devId ;
  136. node->pinMode = myPinMode ;
  137. node->pullUpDnControl = myPullUpDnControl ;
  138. node->digitalRead = myDigitalRead ;
  139. node->digitalWrite = myDigitalWrite ;
  140. node->data2 = readByte (spiPort, devId, MCP23x08_OLAT) ;
  141. return 0 ;
  142. }