Deokgyu Yang | 2c7e86b | 2020-04-28 10:56:11 +0900 | [diff] [blame] | 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 | |
| 27 | |
| 28 | #include <stdio.h> |
| 29 | #include <stdint.h> |
| 30 | |
| 31 | #include <wiringPi.h> |
| 32 | #include <mcp23s17.h> |
| 33 | |
| 34 | #include "piFace.h" |
| 35 | |
| 36 | |
| 37 | /* |
| 38 | * myDigitalWrite: |
| 39 | * Perform the digitalWrite function on the PiFace board |
| 40 | ********************************************************************************* |
| 41 | */ |
| 42 | |
Luke go | dcf6f8a | 2022-04-11 14:59:29 +0900 | [diff] [blame] | 43 | void myDigitalWrite (__attribute__ ((unused))struct wiringPiNodeStruct *node, int pin, int value) |
Deokgyu Yang | 2c7e86b | 2020-04-28 10:56:11 +0900 | [diff] [blame] | 44 | { |
| 45 | digitalWrite (pin + 16, value) ; |
| 46 | } |
| 47 | |
| 48 | |
| 49 | /* |
| 50 | * myDigitalRead: |
| 51 | * Perform the digitalRead function on the PiFace board |
| 52 | * With a slight twist - if we read from base + 8, then we |
| 53 | * read from the output latch... |
| 54 | ********************************************************************************* |
| 55 | */ |
| 56 | |
| 57 | int myDigitalRead (struct wiringPiNodeStruct *node, int pin) |
| 58 | { |
| 59 | if ((pin - node->pinBase) >= 8) |
| 60 | return digitalRead (pin + 8) ; |
| 61 | else |
| 62 | return digitalRead (pin + 16 + 8) ; |
| 63 | } |
| 64 | |
| 65 | |
| 66 | /* |
| 67 | * myPullUpDnControl: |
| 68 | * Perform the pullUpDnControl function on the PiFace board |
| 69 | ********************************************************************************* |
| 70 | */ |
| 71 | |
Luke go | dcf6f8a | 2022-04-11 14:59:29 +0900 | [diff] [blame] | 72 | void myPullUpDnControl (__attribute__ ((unused))struct wiringPiNodeStruct *node, int pin, int pud) |
Deokgyu Yang | 2c7e86b | 2020-04-28 10:56:11 +0900 | [diff] [blame] | 73 | { |
| 74 | pullUpDnControl (pin + 16 + 8, pud) ; |
| 75 | } |
| 76 | |
| 77 | |
| 78 | /* |
| 79 | * piFaceSetup |
| 80 | * We're going to create an instance of the mcp23s17 here, then |
| 81 | * provide our own read/write routines on-top of it... |
| 82 | * The supplied PiFace code (in Pithon) treats it as an 8-bit device |
| 83 | * where you write the output ports and read the input port using the |
| 84 | * same pin numbers, however I have had a request to be able to read |
| 85 | * the output port, so reading 8..15 will read the output latch. |
| 86 | ********************************************************************************* |
| 87 | */ |
| 88 | |
| 89 | int piFaceSetup (const int pinBase) |
| 90 | { |
| 91 | int i ; |
| 92 | struct wiringPiNodeStruct *node ; |
| 93 | |
| 94 | // Create an mcp23s17 instance: |
| 95 | |
| 96 | mcp23s17Setup (pinBase + 16, 0, 0) ; |
| 97 | |
| 98 | // Set the direction bits |
| 99 | |
| 100 | for (i = 0 ; i < 8 ; ++i) |
| 101 | { |
| 102 | pinMode (pinBase + 16 + i, OUTPUT) ; // Port A is the outputs |
| 103 | pinMode (pinBase + 16 + 8 + i, INPUT) ; // Port B inputs. |
| 104 | } |
| 105 | |
| 106 | node = wiringPiNewNode (pinBase, 16) ; |
| 107 | node->digitalRead = myDigitalRead ; |
| 108 | node->digitalWrite = myDigitalWrite ; |
| 109 | node->pullUpDnControl = myPullUpDnControl ; |
| 110 | |
| 111 | return 0 ; |
| 112 | } |