Skip to content

Commit

Permalink
Implement USBGadgetPin and appropriate boot messages. (#674)
Browse files Browse the repository at this point in the history
  • Loading branch information
diyelectromusic authored Jul 13, 2024
1 parent 6664e63 commit 3b033db
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 15 deletions.
27 changes: 18 additions & 9 deletions src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
//
#include "config.h"
#include "../Synth_Dexed/src/dexed.h"
#include <circle/gpiopin.h>

CConfig::CConfig (FATFS *pFileSystem)
: m_Properties ("minidexed.ini", pFileSystem)
Expand All @@ -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");

Expand Down Expand Up @@ -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 ();
Expand Down
7 changes: 6 additions & 1 deletion src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ class CConfig // Configuration for MiniDexed
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;
Expand Down Expand Up @@ -192,6 +195,8 @@ class CConfig // Configuration for MiniDexed
private:
CPropertiesFatFsFile m_Properties;

bool m_bUSBGadget;
unsigned m_nUSBGadgetPin;
bool m_bUSBGadgetMode;

std::string m_SoundDevice;
Expand Down
28 changes: 26 additions & 2 deletions src/kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "kernel.h"
#include <circle/logger.h>
#include <circle/synchronize.h>
#include <circle/gpiopin.h>
#include <assert.h>
#include <circle/usb/usbhcidevice.h>
#include "usbminidexedmidigadget.h"
Expand Down Expand Up @@ -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
Expand All @@ -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 ())
{
Expand Down
32 changes: 29 additions & 3 deletions src/minidexed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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++)
Expand Down

0 comments on commit 3b033db

Please sign in to comment.