From 401f4991e926b700ef695a8113971c0ab6dd827f Mon Sep 17 00:00:00 2001 From: riban Date: Sun, 14 Oct 2018 12:59:39 +0100 Subject: [PATCH 1/3] Adds UHS2_GPIO class providing simple access to GPIO ports. --- UHS2_gpio.cpp | 74 +++++++++++++++++++ UHS2_gpio.h | 45 +++++++++++ examples/GPIO/Blink/Blink.ino | 40 ++++++++++ .../GPIO/Blink_LowLevel/Blink_LowLevel.ino | 40 ++++++++++ examples/GPIO/Input/Input.ino | 38 ++++++++++ 5 files changed, 237 insertions(+) create mode 100644 UHS2_gpio.cpp create mode 100644 UHS2_gpio.h create mode 100644 examples/GPIO/Blink/Blink.ino create mode 100644 examples/GPIO/Blink_LowLevel/Blink_LowLevel.ino create mode 100644 examples/GPIO/Input/Input.ino diff --git a/UHS2_gpio.cpp b/UHS2_gpio.cpp new file mode 100644 index 00000000..04814f46 --- /dev/null +++ b/UHS2_gpio.cpp @@ -0,0 +1,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 : support@circuitsathome.com + +UHS2_GPIO implements "wiring" style GPIO access. Implemented by Brian Walton brian@riban.co.uk + */ + +#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); +} diff --git a/UHS2_gpio.h b/UHS2_gpio.h new file mode 100644 index 00000000..06f5e587 --- /dev/null +++ b/UHS2_gpio.h @@ -0,0 +1,45 @@ +/* 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 : support@circuitsathome.com + +UHS2_GPIO implements "wiring" style GPIO access. Implemented by Brian Walton brian@riban.co.uk + */ + +#if !defined(__USB2_GPIO_H__) +#define __USB2_GPIO_H__ + +#include "Usb.h" + +class UHS2_GPIO +{ + public: + UHS2_GPIO(USB *pUsb); + + void digitalWrite(uint8_t pin, uint8_t val); + int digitalRead(uint8_t pin); + int digitalReadOutput(uint8_t pin); + + private: + USB* m_pUsb; +}; + +#endif // __USB2_GPIO_H__ diff --git a/examples/GPIO/Blink/Blink.ino b/examples/GPIO/Blink/Blink.ino new file mode 100644 index 00000000..1b785fd3 --- /dev/null +++ b/examples/GPIO/Blink/Blink.ino @@ -0,0 +1,40 @@ +/* Example of reading and writing USB Host Shield GPI output + This example uses "Wiring" style interface. See Blink_LowLevel for example of using low-level UHS interface + Author: Brian Walton (brian@riban.co.uk) +*/ +#include + +// Satisfy the IDE, which needs to see the include statment in the ino too. +#ifdef dobogusinclude +#include +#endif + +#define OUTPUT_PIN 0 + +USB Usb; //Create an UHS2 interface object +UHS2_GPIO GPIO(&Usb); //Create a GPIO object + +void setup() +{ + Serial.begin( 115200 ); +#if !defined(__MIPSEL__) + while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection +#endif + Serial.println("Start"); + + if (Usb.Init() == -1) + Serial.println("OSC did not start."); + + delay( 200 ); +} + +void loop() +{ + //Get the current output value, toggle then wait half a second + int nValue = GPIO.digitalReadOutput(OUTPUT_PIN); + nValue = (nValue?0:1); + GPIO.digitalWrite(OUTPUT_PIN, nValue); + Serial.print(nValue?"+":"."); //Debug to show what the output should be doing + delay(500); +} + diff --git a/examples/GPIO/Blink_LowLevel/Blink_LowLevel.ino b/examples/GPIO/Blink_LowLevel/Blink_LowLevel.ino new file mode 100644 index 00000000..ca1dca31 --- /dev/null +++ b/examples/GPIO/Blink_LowLevel/Blink_LowLevel.ino @@ -0,0 +1,40 @@ +/* Example of reading and writing USB Host Shield GPI output using low-level functions + This example uses low-level UHS interface. See Blink for example of using "Wiring" style interface + Author: Brian Walton (brian@riban.co.uk) +*/ +#include + +// Satisfy the IDE, which needs to see the include statment in the ino too. +#ifdef dobogusinclude +#include +#endif +#include + +USB Usb; + +void setup() +{ + Serial.begin( 115200 ); +#if !defined(__MIPSEL__) + while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection +#endif + Serial.println("Start"); + + if (Usb.Init() == -1) + Serial.println("OSC did not start."); + + delay( 200 ); +} + +void loop() +{ + //Get the current output value, toggle then wait half a second + uint8_t nGPO = Usb.gpioRdOutput(); + uint8_t nValue = ((nGPO & 0x01) == 0x01)?0:1; + nGPO &= 0xFE; //clear bit 0 + nGPO |= nValue; + Usb.gpioWr(nGPO); + Serial.print(nValue?"+":"."); //Debug to show what the output should be doing + delay(500); +} + diff --git a/examples/GPIO/Input/Input.ino b/examples/GPIO/Input/Input.ino new file mode 100644 index 00000000..f7dd9c7d --- /dev/null +++ b/examples/GPIO/Input/Input.ino @@ -0,0 +1,38 @@ +/* Example of reading USB Host Shield GPI input + Author: Brian Walton (brian@riban.co.uk) +*/ +#include + +// Satisfy the IDE, which needs to see the include statment in the ino too. +#ifdef dobogusinclude +#include +#endif + +#define INPUT_PIN 0 +#define OUTPUT_PIN 0 + +USB Usb; //Create an UHS2 interface object +UHS2_GPIO GPIO(&Usb); //Create a GPIO object + +void setup() +{ + Serial.begin( 115200 ); +#if !defined(__MIPSEL__) + while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection +#endif + Serial.println("Start"); + + if (Usb.Init() == -1) + Serial.println("OSC did not start."); + + delay( 200 ); +} + +void loop() +{ + //Get the value of input, set value of output + int nValue = GPIO.digitalRead(INPUT_PIN); + nValue = (nValue?0:1); + GPIO.digitalWrite(OUTPUT_PIN, nValue); +} + From 1d7dbd640b3920874d5a2a031938352d39de0328 Mon Sep 17 00:00:00 2001 From: riban Date: Mon, 15 Oct 2018 08:18:16 +0100 Subject: [PATCH 2/3] Fixes coding style. Adds examples to travis. --- .travis.yml | 3 ++ UHS2_gpio.cpp | 36 +++++++++---------- UHS2_gpio.h | 7 ++-- examples/GPIO/Blink/Blink.ino | 10 +++--- .../GPIO/Blink_LowLevel/Blink_LowLevel.ino | 8 ++--- examples/GPIO/Input/Input.ino | 8 ++--- 6 files changed, 37 insertions(+), 35 deletions(-) diff --git a/.travis.yml b/.travis.yml index 2a46dfc5..3dc25114 100644 --- a/.travis.yml +++ b/.travis.yml @@ -39,6 +39,9 @@ env: - PLATFORMIO_CI_SRC=examples/board_qc - PLATFORMIO_CI_SRC=examples/cdc_XR21B1411/XR_terminal - PLATFORMIO_CI_SRC=examples/ftdi/USBFTDILoopback + - PLATFORMIO_CI_SRC=examples/GPIO/Blink + - PLATFORMIO_CI_SRC=examples/GPIO/Blink_LowLevel + - PLATFORMIO_CI_SRC=examples/GPIO/Input - PLATFORMIO_CI_SRC=examples/HID/le3dp - PLATFORMIO_CI_SRC=examples/HID/scale - PLATFORMIO_CI_SRC=examples/HID/SRWS1 diff --git a/UHS2_gpio.cpp b/UHS2_gpio.cpp index 04814f46..3957f15d 100644 --- a/UHS2_gpio.cpp +++ b/UHS2_gpio.cpp @@ -38,14 +38,14 @@ UHS2_GPIO::UHS2_GPIO(USB *pUsb) : m_pUsb(pUsb) * @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); + 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 @@ -53,11 +53,11 @@ void UHS2_GPIO::digitalWrite(uint8_t pin, uint8_t val) { * @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); + 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 @@ -66,9 +66,9 @@ int UHS2_GPIO::digitalRead(uint8_t pin) { * @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); + if(pin > 7) + return -1; + uint8_t nMask = 1 << pin; + uint8_t nValue = m_pUsb->gpioRdOutput(); + return ((nValue & nMask)?1:0); } diff --git a/UHS2_gpio.h b/UHS2_gpio.h index 06f5e587..498b99b8 100644 --- a/UHS2_gpio.h +++ b/UHS2_gpio.h @@ -29,16 +29,15 @@ UHS2_GPIO implements "wiring" style GPIO access. Implemented by Brian Walton bri #include "Usb.h" -class UHS2_GPIO -{ - public: +class UHS2_GPIO { +public: UHS2_GPIO(USB *pUsb); void digitalWrite(uint8_t pin, uint8_t val); int digitalRead(uint8_t pin); int digitalReadOutput(uint8_t pin); - private: +private: USB* m_pUsb; }; diff --git a/examples/GPIO/Blink/Blink.ino b/examples/GPIO/Blink/Blink.ino index 1b785fd3..d7bc594a 100644 --- a/examples/GPIO/Blink/Blink.ino +++ b/examples/GPIO/Blink/Blink.ino @@ -11,8 +11,8 @@ #define OUTPUT_PIN 0 -USB Usb; //Create an UHS2 interface object -UHS2_GPIO GPIO(&Usb); //Create a GPIO object +USB Usb; // Create an UHS2 interface object +UHS2_GPIO GPIO(&Usb); // Create a GPIO object void setup() { @@ -30,11 +30,11 @@ void setup() void loop() { - //Get the current output value, toggle then wait half a second + // Get the current output value, toggle then wait half a second int nValue = GPIO.digitalReadOutput(OUTPUT_PIN); - nValue = (nValue?0:1); + nValue = (nValue ? 0 : 1); GPIO.digitalWrite(OUTPUT_PIN, nValue); - Serial.print(nValue?"+":"."); //Debug to show what the output should be doing + Serial.print(nValue ? "+" : "."); // Debug to show what the output should be doing delay(500); } diff --git a/examples/GPIO/Blink_LowLevel/Blink_LowLevel.ino b/examples/GPIO/Blink_LowLevel/Blink_LowLevel.ino index ca1dca31..119b4780 100644 --- a/examples/GPIO/Blink_LowLevel/Blink_LowLevel.ino +++ b/examples/GPIO/Blink_LowLevel/Blink_LowLevel.ino @@ -28,13 +28,13 @@ void setup() void loop() { - //Get the current output value, toggle then wait half a second + // Get the current output value, toggle then wait half a second uint8_t nGPO = Usb.gpioRdOutput(); - uint8_t nValue = ((nGPO & 0x01) == 0x01)?0:1; - nGPO &= 0xFE; //clear bit 0 + uint8_t nValue = ((nGPO & 0x01) == 0x01) ? 0 : 1; + nGPO &= 0xFE; // Clear bit 0 nGPO |= nValue; Usb.gpioWr(nGPO); - Serial.print(nValue?"+":"."); //Debug to show what the output should be doing + Serial.print(nValue ? "+" : "."); // Debug to show what the output should be doing delay(500); } diff --git a/examples/GPIO/Input/Input.ino b/examples/GPIO/Input/Input.ino index f7dd9c7d..7306df93 100644 --- a/examples/GPIO/Input/Input.ino +++ b/examples/GPIO/Input/Input.ino @@ -11,8 +11,8 @@ #define INPUT_PIN 0 #define OUTPUT_PIN 0 -USB Usb; //Create an UHS2 interface object -UHS2_GPIO GPIO(&Usb); //Create a GPIO object +USB Usb; // Create an UHS2 interface object +UHS2_GPIO GPIO(&Usb); // Create a GPIO object void setup() { @@ -30,9 +30,9 @@ void setup() void loop() { - //Get the value of input, set value of output + // Get the value of input, set value of output int nValue = GPIO.digitalRead(INPUT_PIN); - nValue = (nValue?0:1); + nValue = (nValue ? LOW : HIGH); GPIO.digitalWrite(OUTPUT_PIN, nValue); } From 5046bc8bf23e83fcdadd6b0d027c4b30ea27b59e Mon Sep 17 00:00:00 2001 From: riban Date: Mon, 15 Oct 2018 09:13:30 +0100 Subject: [PATCH 3/3] Fixes name conflict in examples with ESP32 GPIO. Further coding style fixes (all more cuddly now). --- examples/GPIO/Blink/Blink.ino | 12 +++++------- examples/GPIO/Blink_LowLevel/Blink_LowLevel.ino | 6 ++---- examples/GPIO/Input/Input.ino | 12 +++++------- 3 files changed, 12 insertions(+), 18 deletions(-) diff --git a/examples/GPIO/Blink/Blink.ino b/examples/GPIO/Blink/Blink.ino index d7bc594a..fb42d9bd 100644 --- a/examples/GPIO/Blink/Blink.ino +++ b/examples/GPIO/Blink/Blink.ino @@ -12,10 +12,9 @@ #define OUTPUT_PIN 0 USB Usb; // Create an UHS2 interface object -UHS2_GPIO GPIO(&Usb); // Create a GPIO object +UHS2_GPIO Gpio(&Usb); // Create a GPIO object -void setup() -{ +void setup() { Serial.begin( 115200 ); #if !defined(__MIPSEL__) while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection @@ -28,12 +27,11 @@ void setup() delay( 200 ); } -void loop() -{ +void loop() { // Get the current output value, toggle then wait half a second - int nValue = GPIO.digitalReadOutput(OUTPUT_PIN); + int nValue = Gpio.digitalReadOutput(OUTPUT_PIN); nValue = (nValue ? 0 : 1); - GPIO.digitalWrite(OUTPUT_PIN, nValue); + Gpio.digitalWrite(OUTPUT_PIN, nValue); Serial.print(nValue ? "+" : "."); // Debug to show what the output should be doing delay(500); } diff --git a/examples/GPIO/Blink_LowLevel/Blink_LowLevel.ino b/examples/GPIO/Blink_LowLevel/Blink_LowLevel.ino index 119b4780..88c81f98 100644 --- a/examples/GPIO/Blink_LowLevel/Blink_LowLevel.ino +++ b/examples/GPIO/Blink_LowLevel/Blink_LowLevel.ino @@ -12,8 +12,7 @@ USB Usb; -void setup() -{ +void setup() { Serial.begin( 115200 ); #if !defined(__MIPSEL__) while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection @@ -26,8 +25,7 @@ void setup() delay( 200 ); } -void loop() -{ +void loop() { // Get the current output value, toggle then wait half a second uint8_t nGPO = Usb.gpioRdOutput(); uint8_t nValue = ((nGPO & 0x01) == 0x01) ? 0 : 1; diff --git a/examples/GPIO/Input/Input.ino b/examples/GPIO/Input/Input.ino index 7306df93..d46e1ade 100644 --- a/examples/GPIO/Input/Input.ino +++ b/examples/GPIO/Input/Input.ino @@ -12,10 +12,9 @@ #define OUTPUT_PIN 0 USB Usb; // Create an UHS2 interface object -UHS2_GPIO GPIO(&Usb); // Create a GPIO object +UHS2_GPIO Gpio(&Usb); // Create a GPIO object -void setup() -{ +void setup() { Serial.begin( 115200 ); #if !defined(__MIPSEL__) while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection @@ -28,11 +27,10 @@ void setup() delay( 200 ); } -void loop() -{ +void loop() { // Get the value of input, set value of output - int nValue = GPIO.digitalRead(INPUT_PIN); + int nValue = Gpio.digitalRead(INPUT_PIN); nValue = (nValue ? LOW : HIGH); - GPIO.digitalWrite(OUTPUT_PIN, nValue); + Gpio.digitalWrite(OUTPUT_PIN, nValue); }