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.

piFaceOld.c 4.3 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. }