From 3b033db4afd54cef2f7af2a22ec5957422393722 Mon Sep 17 00:00:00 2001 From: Kevin <68612569+diyelectromusic@users.noreply.github.com> Date: Sat, 13 Jul 2024 17:21:09 +0100 Subject: [PATCH] Implement USBGadgetPin and appropriate boot messages. (#674) --- src/config.cpp | 27 ++++++++++++++++++--------- src/config.h | 7 ++++++- src/kernel.cpp | 28 ++++++++++++++++++++++++++-- src/minidexed.cpp | 32 +++++++++++++++++++++++++++++--- 4 files changed, 79 insertions(+), 15 deletions(-) diff --git a/src/config.cpp b/src/config.cpp index ad55d13..884d7cc 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -22,7 +22,6 @@ // #include "config.h" #include "../Synth_Dexed/src/dexed.h" -#include CConfig::CConfig (FATFS *pFileSystem) : m_Properties ("minidexed.ini", pFileSystem) @@ -37,14 +36,9 @@ void CConfig::Load (void) { m_Properties.Load (); - m_bUSBGadgetMode = m_Properties.GetNumber ("USBGadget", 0) != 0; - unsigned usbGadgetPinNumber = m_Properties.GetNumber ("USBGadgetPin", 26); // Default to GPIO pin 26 if not specified - CGPIOPin usbGadgetPin(usbGadgetPinNumber, GPIOModeInputPullUp); - - if (usbGadgetPin.Read() == 0) // If the pin is pulled down - { - m_bUSBGadgetMode = true; - } + m_bUSBGadget = m_Properties.GetNumber ("USBGadget", 0) != 0; + m_nUSBGadgetPin = m_Properties.GetNumber ("USBGadgetPin", 0); // Default OFF + SetUSBGadgetMode(m_bUSBGadget); // Might get overriden later by USBGadgetPin state m_SoundDevice = m_Properties.GetString ("SoundDevice", "pwm"); @@ -183,11 +177,26 @@ void CConfig::Load (void) m_bPerformanceSelectChannel = m_Properties.GetNumber ("PerformanceSelectChannel", 0); } +bool CConfig::GetUSBGadget (void) const +{ + return m_bUSBGadget; +} + +unsigned CConfig::GetUSBGadgetPin (void) const +{ + return m_nUSBGadgetPin; +} + bool CConfig::GetUSBGadgetMode (void) const { return m_bUSBGadgetMode; } +void CConfig::SetUSBGadgetMode (bool USBGadgetMode) +{ + m_bUSBGadgetMode = USBGadgetMode; +} + const char *CConfig::GetSoundDevice (void) const { return m_SoundDevice.c_str (); diff --git a/src/config.h b/src/config.h index 5944dd2..7497c21 100644 --- a/src/config.h +++ b/src/config.h @@ -68,7 +68,10 @@ public: void Load (void); // USB Mode - bool GetUSBGadgetMode (void) const; // true if in USB gadget mode + bool GetUSBGadget (void) const; + unsigned GetUSBGadgetPin (void) const; + bool GetUSBGadgetMode (void) const; // true if in USB gadget mode depending on USBGadget and USBGadgetPin + void SetUSBGadgetMode (bool USBGadgetMode); // Sound device const char *GetSoundDevice (void) const; @@ -192,6 +195,8 @@ public: private: CPropertiesFatFsFile m_Properties; + bool m_bUSBGadget; + unsigned m_nUSBGadgetPin; bool m_bUSBGadgetMode; std::string m_SoundDevice; diff --git a/src/kernel.cpp b/src/kernel.cpp index 33bc5f8..446747b 100644 --- a/src/kernel.cpp +++ b/src/kernel.cpp @@ -20,6 +20,7 @@ #include "kernel.h" #include #include +#include #include #include #include "usbminidexedmidigadget.h" @@ -92,8 +93,30 @@ bool CKernel::Initialize (void) m_pSPIMaster = nullptr; } } - - if (m_Config.GetUSBGadgetMode()) + + bool bUSBGadgetMode = false; + if (m_Config.GetUSBGadget()) + { + unsigned nUSBGadgetPin = m_Config.GetUSBGadgetPin(); + if (nUSBGadgetPin == 0) + { + // No hardware config option + bUSBGadgetMode = true; + } + else + { + // State of USB Gadget Mode determined by state of the pin. + // Pulled down = enable USB Gadget mode + CGPIOPin usbGadgetPin(nUSBGadgetPin, GPIOModeInputPullUp); + + if (usbGadgetPin.Read() == 0) + { + bUSBGadgetMode = true; + } + } + } + + if (bUSBGadgetMode) { #if RASPPI==5 #warning No support for USB Gadget Mode on RPI 5 yet @@ -107,6 +130,7 @@ bool CKernel::Initialize (void) // Run the USB stack in USB Host (default) mode m_pUSB = new CUSBHCIDevice (&mInterrupt, &mTimer, TRUE); } + m_Config.SetUSBGadgetMode(bUSBGadgetMode); if (!m_pUSB->Initialize ()) { diff --git a/src/minidexed.cpp b/src/minidexed.cpp index 429fddc..da9e598 100644 --- a/src/minidexed.cpp +++ b/src/minidexed.cpp @@ -102,17 +102,43 @@ CMiniDexed::CMiniDexed (CConfig *pConfig, CInterruptSystem *pInterrupt, m_pTG[i]->activate (); } - if (pConfig->GetUSBGadgetMode()) + unsigned nUSBGadgetPin = pConfig->GetUSBGadgetPin(); + bool bUSBGadget = pConfig->GetUSBGadget(); + bool bUSBGadgetMode = pConfig->GetUSBGadgetMode(); + + if (bUSBGadgetMode) { #if RASPPI==5 LOGNOTE ("USB Gadget (Device) Mode NOT supported on RPI 5"); #else - LOGNOTE ("USB In Gadget (Device) Mode"); + if (nUSBGadgetPin == 0) + { + LOGNOTE ("USB In Gadget (Device) Mode"); + } + else + { + LOGNOTE ("USB In Gadget (Device) Mode [USBGadgetPin %d = LOW]", nUSBGadgetPin); + } #endif } else { - LOGNOTE ("USB In Host Mode"); + if (bUSBGadget) + { + if (nUSBGadgetPin == 0) + { + // This shouldn't be possible... + LOGNOTE ("USB State Unknown"); + } + else + { + LOGNOTE ("USB In Host Mode [USBGadgetPin %d = HIGH]", nUSBGadgetPin); + } + } + else + { + LOGNOTE ("USB In Host Mode"); + } } for (unsigned i = 0; i < CConfig::MaxUSBMIDIDevices; i++)