Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: UsbHostMAX3421E_Hello
UHS2_gpio.cpp@0:84353c479782, 2020-07-12 (annotated)
- Committer:
- hudakz
- Date:
- Sun Jul 12 20:39:26 2020 +0000
- Revision:
- 0:84353c479782
- Child:
- 1:2263e77400e9
MAX3421E-based USB Host Shield Library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hudakz | 0:84353c479782 | 1 | /* Copyright (C) 2011 Circuits At Home, LTD. All rights reserved. |
hudakz | 0:84353c479782 | 2 | |
hudakz | 0:84353c479782 | 3 | This program is free software; you can redistribute it and/or modify |
hudakz | 0:84353c479782 | 4 | it under the terms of the GNU General Public License as published by |
hudakz | 0:84353c479782 | 5 | the Free Software Foundation; either version 2 of the License, or |
hudakz | 0:84353c479782 | 6 | (at your option) any later version. |
hudakz | 0:84353c479782 | 7 | |
hudakz | 0:84353c479782 | 8 | This program is distributed in the hope that it will be useful, |
hudakz | 0:84353c479782 | 9 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
hudakz | 0:84353c479782 | 10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
hudakz | 0:84353c479782 | 11 | GNU General Public License for more details. |
hudakz | 0:84353c479782 | 12 | |
hudakz | 0:84353c479782 | 13 | You should have received a copy of the GNU General Public License |
hudakz | 0:84353c479782 | 14 | along with this program; if not, write to the Free Software |
hudakz | 0:84353c479782 | 15 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
hudakz | 0:84353c479782 | 16 | |
hudakz | 0:84353c479782 | 17 | Contact information |
hudakz | 0:84353c479782 | 18 | ------------------- |
hudakz | 0:84353c479782 | 19 | |
hudakz | 0:84353c479782 | 20 | Circuits At Home, LTD |
hudakz | 0:84353c479782 | 21 | Web : http://www.circuitsathome.com |
hudakz | 0:84353c479782 | 22 | e-mail : support@circuitsathome.com |
hudakz | 0:84353c479782 | 23 | |
hudakz | 0:84353c479782 | 24 | UHS2_GPIO implements "wiring" style GPIO access. Implemented by Brian Walton brian@riban.co.uk |
hudakz | 0:84353c479782 | 25 | */ |
hudakz | 0:84353c479782 | 26 | |
hudakz | 0:84353c479782 | 27 | #include "UHS2_gpio.h" |
hudakz | 0:84353c479782 | 28 | |
hudakz | 0:84353c479782 | 29 | /** @brief Implement an instance of a UHS2_GPIO object |
hudakz | 0:84353c479782 | 30 | * @param pUSB Pointer to a UHS2 USB object |
hudakz | 0:84353c479782 | 31 | */ |
hudakz | 0:84353c479782 | 32 | UHS2_GPIO::UHS2_GPIO(USB *pUsb) : m_pUsb(pUsb) |
hudakz | 0:84353c479782 | 33 | { |
hudakz | 0:84353c479782 | 34 | } |
hudakz | 0:84353c479782 | 35 | |
hudakz | 0:84353c479782 | 36 | /** @brief Set a GPIO output value |
hudakz | 0:84353c479782 | 37 | * @param pin GPIO output pin on USB Host Shield to set |
hudakz | 0:84353c479782 | 38 | * @param val Value to set the pin to (zero value will clear output, non-zero value will assert output) |
hudakz | 0:84353c479782 | 39 | */ |
hudakz | 0:84353c479782 | 40 | void UHS2_GPIO::digitalWrite(uint8_t pin, uint8_t val) { |
hudakz | 0:84353c479782 | 41 | if(pin > 7) |
hudakz | 0:84353c479782 | 42 | return; |
hudakz | 0:84353c479782 | 43 | uint8_t nValue = m_pUsb->gpioRdOutput(); |
hudakz | 0:84353c479782 | 44 | uint8_t nMask = 1 << pin; |
hudakz | 0:84353c479782 | 45 | nValue &= (~nMask); |
hudakz | 0:84353c479782 | 46 | if(val) |
hudakz | 0:84353c479782 | 47 | nValue |= (nMask); |
hudakz | 0:84353c479782 | 48 | m_pUsb->gpioWr(nValue); |
hudakz | 0:84353c479782 | 49 | } |
hudakz | 0:84353c479782 | 50 | |
hudakz | 0:84353c479782 | 51 | /** @brief Read the value from a GPIO input pin |
hudakz | 0:84353c479782 | 52 | * @param pin GPIO input pin on USB Host Shield to read |
hudakz | 0:84353c479782 | 53 | * @retval int Value of GPIO input (-1 on fail) |
hudakz | 0:84353c479782 | 54 | */ |
hudakz | 0:84353c479782 | 55 | int UHS2_GPIO::digitalRead(uint8_t pin) { |
hudakz | 0:84353c479782 | 56 | if(pin > 7) |
hudakz | 0:84353c479782 | 57 | return -1; |
hudakz | 0:84353c479782 | 58 | uint8_t nMask = 1 << pin; |
hudakz | 0:84353c479782 | 59 | uint8_t nValue = m_pUsb->gpioRd(); |
hudakz | 0:84353c479782 | 60 | return ((nValue & nMask)?1:0); |
hudakz | 0:84353c479782 | 61 | } |
hudakz | 0:84353c479782 | 62 | |
hudakz | 0:84353c479782 | 63 | /** @brief Read the value from a GPIO output pin |
hudakz | 0:84353c479782 | 64 | * @param pin GPIO output pin on USB Host Shield to read |
hudakz | 0:84353c479782 | 65 | * @retval int Value of GPIO output (-1 on fail) |
hudakz | 0:84353c479782 | 66 | * @note Value of MAX3421E output register, i.e. what the device has been set to, not the physical value on the pin |
hudakz | 0:84353c479782 | 67 | */ |
hudakz | 0:84353c479782 | 68 | int UHS2_GPIO::digitalReadOutput(uint8_t pin) { |
hudakz | 0:84353c479782 | 69 | if(pin > 7) |
hudakz | 0:84353c479782 | 70 | return -1; |
hudakz | 0:84353c479782 | 71 | uint8_t nMask = 1 << pin; |
hudakz | 0:84353c479782 | 72 | uint8_t nValue = m_pUsb->gpioRdOutput(); |
hudakz | 0:84353c479782 | 73 | return ((nValue & nMask)?1:0); |
hudakz | 0:84353c479782 | 74 | } |