選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 

189 行
4.5 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://github.com/WiringPi/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. }