Zoltan Hudak / UsbHostMAX3421E

Dependents:   UsbHostMAX3421E_Hello

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers UHS2_gpio.cpp Source File

UHS2_gpio.cpp

00001 /* Copyright (C) 2011 Circuits At Home, LTD. All rights reserved.
00002 
00003 This program is free software; you can redistribute it and/or modify
00004 it under the terms of the GNU General Public License as published by
00005 the Free Software Foundation; either version 2 of the License, or
00006 (at your option) any later version.
00007 
00008 This program is distributed in the hope that it will be useful,
00009 but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 GNU General Public License for more details.
00012 
00013 You should have received a copy of the GNU General Public License
00014 along with this program; if not, write to the Free Software
00015 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00016 
00017 Contact information
00018 -------------------
00019 
00020 Circuits At Home, LTD
00021 Web      :  http://www.circuitsathome.com
00022 e-mail   :  support@circuitsathome.com
00023 
00024 UHS2_GPIO implements "wiring" style GPIO access. Implemented by Brian Walton brian@riban.co.uk
00025  */
00026 
00027 #include "UHS2_gpio.h"
00028 
00029 /** @brief  Implement an instance of a UHS2_GPIO object
00030 *   @param  pUSB Pointer to a UHS2 USB object
00031 */
00032 UHS2_GPIO::UHS2_GPIO(Usb *pUsb) : m_pUsb(pUsb)
00033 {
00034 }
00035 
00036 /** @brief  Set a GPIO output value
00037 *   @param  pin GPIO output pin on USB Host Shield to set
00038 *   @param  val Value to set the pin to (zero value will clear output, non-zero value will assert output)
00039 */
00040 void UHS2_GPIO::digitalWrite(uint8_t pin, uint8_t val) {
00041         if(pin > 7)
00042                 return;
00043         uint8_t nValue = m_pUsb->gpioRdOutput();
00044         uint8_t nMask = 1 << pin;
00045         nValue &= (~nMask);
00046         if(val)
00047                 nValue |= (nMask);
00048         m_pUsb->gpioWr(nValue);
00049 }
00050 
00051 /** @brief  Read the value from a GPIO input pin
00052 *   @param  pin GPIO input pin on USB Host Shield to read
00053 *   @retval int Value of GPIO input (-1 on fail)
00054 */
00055 int UHS2_GPIO::digitalRead(uint8_t pin) {
00056         if(pin > 7)
00057                 return -1;
00058         uint8_t nMask = 1 << pin;
00059         uint8_t nValue = m_pUsb->gpioRd();
00060         return ((nValue & nMask)?1:0);
00061 }
00062 
00063 /** @brief  Read the value from a GPIO output pin
00064 *   @param  pin GPIO output pin on USB Host Shield to read
00065 *   @retval int Value of GPIO output (-1 on fail)
00066 *   @note   Value of MAX3421E output register, i.e. what the device has been set to, not the physical value on the pin
00067 */
00068 int UHS2_GPIO::digitalReadOutput(uint8_t pin) {
00069         if(pin > 7)
00070                 return -1;
00071         uint8_t nMask = 1 << pin;
00072         uint8_t nValue = m_pUsb->gpioRdOutput();
00073         return ((nValue & nMask)?1:0);
00074 }