forked from felis/USB_Host_Shield_2.0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UHS2_gpio.cpp
74 lines (63 loc) · 2.48 KB
/
UHS2_gpio.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/* Copyright (C) 2011 Circuits At Home, LTD. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Contact information
-------------------
Circuits At Home, LTD
Web : http://www.circuitsathome.com
e-mail : [email protected]
UHS2_GPIO implements "wiring" style GPIO access. Implemented by Brian Walton [email protected]
*/
#include "UHS2_gpio.h"
/** @brief Implement an instance of a UHS2_GPIO object
* @param pUSB Pointer to a UHS2 USB object
*/
UHS2_GPIO::UHS2_GPIO(USB *pUsb) : m_pUsb(pUsb)
{
}
/** @brief Set a GPIO output value
* @param pin GPIO output pin on USB Host Shield to set
* @param val Value to set the pin to (zero value will clear output, non-zero value will assert output)
*/
void UHS2_GPIO::digitalWrite(uint8_t pin, uint8_t val) {
if(pin > 7)
return;
uint8_t nValue = m_pUsb->gpioRdOutput();
uint8_t nMask = 1 << pin;
nValue &= (~nMask);
if(val)
nValue |= (nMask);
m_pUsb->gpioWr(nValue);
}
/** @brief Read the value from a GPIO input pin
* @param pin GPIO input pin on USB Host Shield to read
* @retval int Value of GPIO input (-1 on fail)
*/
int UHS2_GPIO::digitalRead(uint8_t pin) {
if(pin > 7)
return -1;
uint8_t nMask = 1 << pin;
uint8_t nValue = m_pUsb->gpioRd();
return ((nValue & nMask)?1:0);
}
/** @brief Read the value from a GPIO output pin
* @param pin GPIO output pin on USB Host Shield to read
* @retval int Value of GPIO output (-1 on fail)
* @note Value of MAX3421E output register, i.e. what the device has been set to, not the physical value on the pin
*/
int UHS2_GPIO::digitalReadOutput(uint8_t pin) {
if(pin > 7)
return -1;
uint8_t nMask = 1 << pin;
uint8_t nValue = m_pUsb->gpioRdOutput();
return ((nValue & nMask)?1:0);
}