piFace.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * piFace.:
  3. * This file to interface with the PiFace peripheral device which
  4. * has an MCP23S17 GPIO device connected via the SPI bus.
  5. *
  6. * Copyright (c) 2012-2013 Gordon Henderson
  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 <mcp23s17.h>
  30. #include "piFace.h"
  31. /*
  32. * myDigitalWrite:
  33. * Perform the digitalWrite function on the PiFace board
  34. *********************************************************************************
  35. */
  36. void myDigitalWrite (struct wiringPiNodeStruct *node, int pin, int value)
  37. {
  38. digitalWrite (pin + 16, value) ;
  39. }
  40. /*
  41. * myDigitalRead:
  42. * Perform the digitalRead function on the PiFace board
  43. * With a slight twist - if we read from base + 8, then we
  44. * read from the output latch...
  45. *********************************************************************************
  46. */
  47. int myDigitalRead (struct wiringPiNodeStruct *node, int pin)
  48. {
  49. if ((pin - node->pinBase) >= 8)
  50. return digitalRead (pin + 8) ;
  51. else
  52. return digitalRead (pin + 16 + 8) ;
  53. }
  54. /*
  55. * myPullUpDnControl:
  56. * Perform the pullUpDnControl function on the PiFace board
  57. *********************************************************************************
  58. */
  59. void myPullUpDnControl (struct wiringPiNodeStruct *node, int pin, int pud)
  60. {
  61. pullUpDnControl (pin + 16 + 8, pud) ;
  62. }
  63. /*
  64. * piFaceSetup
  65. * We're going to create an instance of the mcp23s17 here, then
  66. * provide our own read/write routines on-top of it...
  67. * The supplied PiFace code (in Pithon) treats it as an 8-bit device
  68. * where you write the output ports and read the input port using the
  69. * same pin numbers, however I have had a request to be able to read
  70. * the output port, so reading 8..15 will read the output latch.
  71. *********************************************************************************
  72. */
  73. int piFaceSetup (const int pinBase)
  74. {
  75. int i ;
  76. struct wiringPiNodeStruct *node ;
  77. // Create an mcp23s17 instance:
  78. mcp23s17Setup (pinBase + 16, 0, 0) ;
  79. // Set the direction bits
  80. for (i = 0 ; i < 8 ; ++i)
  81. {
  82. pinMode (pinBase + 16 + i, OUTPUT) ; // Port A is the outputs
  83. pinMode (pinBase + 16 + 8 + i, INPUT) ; // Port B inputs.
  84. }
  85. node = wiringPiNewNode (pinBase, 16) ;
  86. node->digitalRead = myDigitalRead ;
  87. node->digitalWrite = myDigitalWrite ;
  88. node->pullUpDnControl = myPullUpDnControl ;
  89. return 0 ;
  90. }