Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

piFaceOld.c 4.3 KiB

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