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

179 行
4.3 KiB

  1. /*
  2. * piFace.:
  3. * Arduino compatable (ish) Wiring library for the Raspberry Pi
  4. * Copyright (c) 2012-2013 Gordon Henderson
  5. *
  6. * This file to interface with the PiFace peripheral device which
  7. * has an MCP23S17 GPIO device connected via the SPI bus.
  8. ***********************************************************************
  9. * This file is part of wiringPi:
  10. * https://projects.drogon.net/raspberry-pi/wiringpi/
  11. *
  12. * wiringPi is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Lesser General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * wiringPi is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Lesser General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Lesser General Public
  23. * License along with wiringPi.
  24. * If not, see <http://www.gnu.org/licenses/>.
  25. ***********************************************************************
  26. */
  27. #include <stdio.h>
  28. #include <stdint.h>
  29. #include <wiringPi.h>
  30. #include <wiringPiSPI.h>
  31. #include "../wiringPi/mcp23x0817.h"
  32. #include "piFace.h"
  33. #define PIFACE_SPEED 4000000
  34. #define PIFACE_DEVNO 0
  35. /*
  36. * writeByte:
  37. * Write a byte to a register on the MCP23S17 on the SPI bus.
  38. *********************************************************************************
  39. */
  40. static void writeByte (uint8_t reg, uint8_t data)
  41. {
  42. uint8_t spiData [4] ;
  43. spiData [0] = CMD_WRITE ;
  44. spiData [1] = reg ;
  45. spiData [2] = data ;
  46. wiringPiSPIDataRW (PIFACE_DEVNO, spiData, 3) ;
  47. }
  48. /*
  49. * readByte:
  50. * Read a byte from a register on the MCP23S17 on the SPI bus.
  51. *********************************************************************************
  52. */
  53. static uint8_t readByte (uint8_t reg)
  54. {
  55. uint8_t spiData [4] ;
  56. spiData [0] = CMD_READ ;
  57. spiData [1] = reg ;
  58. wiringPiSPIDataRW (PIFACE_DEVNO, spiData, 3) ;
  59. return spiData [2] ;
  60. }
  61. /*
  62. * myDigitalWrite:
  63. * Perform the digitalWrite function on the PiFace board
  64. *********************************************************************************
  65. */
  66. void myDigitalWrite (struct wiringPiNodeStruct *node, int pin, int value)
  67. {
  68. uint8_t mask, old ;
  69. pin -= node->pinBase ;
  70. mask = 1 << pin ;
  71. old = readByte (MCP23x17_GPIOA) ;
  72. if (value == 0)
  73. old &= (~mask) ;
  74. else
  75. old |= mask ;
  76. writeByte (MCP23x17_GPIOA, old) ;
  77. }
  78. /*
  79. * myDigitalRead:
  80. * Perform the digitalRead function on the PiFace board
  81. *********************************************************************************
  82. */
  83. int myDigitalRead (struct wiringPiNodeStruct *node, int pin)
  84. {
  85. uint8_t mask, reg ;
  86. mask = 1 << ((pin - node->pinBase) & 7) ;
  87. if (pin < 8)
  88. reg = MCP23x17_GPIOB ; // Input regsiter
  89. else
  90. reg = MCP23x17_OLATA ; // Output latch regsiter
  91. if ((readByte (reg) & mask) != 0)
  92. return HIGH ;
  93. else
  94. return LOW ;
  95. }
  96. /*
  97. * myPullUpDnControl:
  98. * Perform the pullUpDnControl function on the PiFace board
  99. *********************************************************************************
  100. */
  101. void myPullUpDnControl (struct wiringPiNodeStruct *node, int pin, int pud)
  102. {
  103. uint8_t mask, old ;
  104. mask = 1 << (pin - node->pinBase) ;
  105. old = readByte (MCP23x17_GPPUB) ;
  106. if (pud == 0)
  107. old &= (~mask) ;
  108. else
  109. old |= mask ;
  110. writeByte (MCP23x17_GPPUB, old) ;
  111. }
  112. /*
  113. * piFaceSetup
  114. * Setup the SPI interface and initialise the MCP23S17 chip
  115. * We create one node with 16 pins - each if the first 8 pins being read
  116. * and write - although the operations actually go to different
  117. * hardware ports. The top 8 let you read the state of the output register.
  118. *********************************************************************************
  119. */
  120. int piFaceSetup (const int pinBase)
  121. {
  122. int x ;
  123. struct wiringPiNodeStruct *node ;
  124. if ((x = wiringPiSPISetup (PIFACE_DEVNO, PIFACE_SPEED)) < 0)
  125. return x ;
  126. // Setup the MCP23S17
  127. writeByte (MCP23x17_IOCON, IOCON_INIT) ;
  128. writeByte (MCP23x17_IODIRA, 0x00) ; // Port A -> Outputs
  129. writeByte (MCP23x17_IODIRB, 0xFF) ; // Port B -> Inputs
  130. node = wiringPiNewNode (pinBase, 16) ;
  131. node->digitalRead = myDigitalRead ;
  132. node->digitalWrite = myDigitalWrite ;
  133. node->pullUpDnControl = myPullUpDnControl ;
  134. return 0 ;
  135. }