Skip to content

Commit 464565e

Browse files
committed
Support switching between usb host and device
1 parent af288cc commit 464565e

File tree

3 files changed

+70
-3
lines changed

3 files changed

+70
-3
lines changed

src/Hardware/SAME70/Devices.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,12 @@ void DeviceInit() noexcept
122122
#endif
123123

124124
#if CORE_USES_TINYUSB
125+
#if SUPPORT_USB_DRIVE && CFG_TUH_ENABLED
126+
CoreUsbInit(NvicPriorityUSB, UsbVBusPin, UsbPowerSwitchPin, UsbModePin, UsbDetectPin);
127+
#else
125128
CoreUsbInit(NvicPriorityUSB);
126-
usbDeviceTask.Create(CoreUsbDeviceTask, "USBD", nullptr, TaskPriority::UsbPriority);
129+
#endif
130+
usbDeviceTask.Create(CoreUsbDeviceTask, "USBHD", nullptr, TaskPriority::UsbPriority);
127131
#endif
128132
}
129133

@@ -134,6 +138,7 @@ void StopAnalogTask() noexcept
134138
void StopUsbTask() noexcept
135139
{
136140
#if CORE_USES_TINYUSB
141+
CoreUsbStop();
137142
usbDeviceTask.TerminateAndUnlink();
138143
#endif
139144
}

src/Platform/Platform.cpp

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@
4747
#include <Storage/SdCardVolume.h>
4848
#include <Accelerometers/Accelerometers.h>
4949

50+
#if SUPPORT_USB_DRIVE
51+
#include <TinyUsbInterface.h>
52+
#endif
53+
5054
#if SAM4E || SAM4S || SAME70
5155
# include <AnalogIn.h>
5256
using LegacyAnalogIn::AdcBits;
@@ -2039,6 +2043,48 @@ GCodeResult Platform::HandleM575(GCodeBuffer& gb, const StringRef& reply) THROWS
20392043
{
20402044
// Get the channel specified by the command and the corresponding GCode buffer
20412045
const size_t chan = gb.GetLimitedUIValue('P', NumSerialChannels);
2046+
2047+
bool modeChangeSeen = false;
2048+
bool hostMode = false;
2049+
gb.TryGetBValue('H', hostMode, modeChangeSeen);
2050+
2051+
#if SUPPORT_USB_DRIVE
2052+
if (chan == 0)
2053+
{
2054+
bool result = SetUsbHostMode(hostMode, reply);
2055+
2056+
// If setting to host mode then return result immediately. However, if setting to device,
2057+
// check that succeeded first so that the rest of the M575 code can be executed.
2058+
if (hostMode)
2059+
{
2060+
return result ? GCodeResult::ok : GCodeResult::error;
2061+
}
2062+
else
2063+
{
2064+
if (!result)
2065+
{
2066+
return GCodeResult::error;
2067+
}
2068+
}
2069+
}
2070+
else
2071+
{
2072+
// H=1 is only valid on channel 0, otherwise ignored.
2073+
if (hostMode)
2074+
{
2075+
reply.printf("USB host mode not supported on channel other than 0");
2076+
return GCodeResult::error;
2077+
}
2078+
}
2079+
#else
2080+
// H=0 is ignored when USB host not supported.
2081+
if (modeChangeSeen && hostMode)
2082+
{
2083+
reply.printf("USB host mode not supported");
2084+
return GCodeResult::error;
2085+
}
2086+
#endif
2087+
20422088
GCodeBuffer * const gbp = reprap.GetGCodes().GetSerialGCodeBuffer(chan);
20432089

20442090
#if HAS_AUX_DEVICES
@@ -2109,7 +2155,7 @@ GCodeResult Platform::HandleM575(GCodeBuffer& gb, const StringRef& reply) THROWS
21092155
if ( gbp != nullptr
21102156
&& newMode != AuxDevice::AuxMode::disabled
21112157
&& newMode != AuxDevice::AuxMode::device
2112-
)
2158+
)
21132159
{
21142160
gbp->Enable(val); // enable I/O and set the CRC and checksum requirements, also sets Marlin or PanelDue compatibility
21152161
}
@@ -2136,7 +2182,7 @@ GCodeResult Platform::HandleM575(GCodeBuffer& gb, const StringRef& reply) THROWS
21362182
{
21372183
if (!IsAuxEnabled(chan - 1)
21382184
&& (chan >= NumSerialChannels || auxDevices[chan - 1].GetMode() != AuxDevice::AuxMode::device)
2139-
)
2185+
)
21402186
{
21412187
reply.printf("Channel %u is disabled", chan);
21422188
}
@@ -2170,6 +2216,7 @@ GCodeResult Platform::HandleM575(GCodeBuffer& gb, const StringRef& reply) THROWS
21702216
}
21712217
}
21722218
}
2219+
21732220
return GCodeResult::ok;
21742221
}
21752222

@@ -3219,6 +3266,18 @@ void Platform::SetBaudRate(size_t chan, uint32_t br) noexcept
32193266
}
32203267
}
32213268

3269+
#if SUPPORT_USB_DRIVE
3270+
bool Platform::SetUsbHostMode(bool hostMode, const StringRef& reply) noexcept
3271+
{
3272+
#if CORE_USES_TINYUSB && CFG_TUH_ENABLED
3273+
return CoreUsbSetHostMode(hostMode, reply);
3274+
#else
3275+
reply.copy("Host mode not supported by USB stack");
3276+
return false; // unimplemented if not using tinyUSB
3277+
#endif
3278+
}
3279+
#endif
3280+
32223281
uint32_t Platform::GetBaudRate(size_t chan) const noexcept
32233282
{
32243283
return (chan != 0 && chan < NumSerialChannels) ? auxDevices[chan - 1].GetBaudRate() : 0;

src/Platform/Platform.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,9 @@ class Platform INHERIT_OBJECT_MODEL
280280
void SetGateWay(IPAddress gw) noexcept;
281281
IPAddress GateWay() const noexcept;
282282
void SetBaudRate(size_t chan, uint32_t br) noexcept;
283+
#if SUPPORT_USB_DRIVE
284+
bool SetUsbHostMode(bool host, const StringRef& reply) noexcept;
285+
#endif
283286
uint32_t GetBaudRate(size_t chan) const noexcept;
284287
void SetCommsProperties(size_t chan, uint32_t cp) noexcept;
285288
uint32_t GetCommsProperties(size_t chan) const noexcept;

0 commit comments

Comments
 (0)