Skip to content

Commit 1fba91d

Browse files
committed
Support switching between usb host and device
1 parent 7842191 commit 1fba91d

File tree

4 files changed

+91
-49
lines changed

4 files changed

+91
-49
lines changed

src/GCodes/GCodes2.cpp

Lines changed: 66 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -3771,72 +3771,90 @@ bool GCodes::HandleMcode(GCodeBuffer& gb, const StringRef& reply) THROWS(GCodeEx
37713771
case 575: // Set communications parameters
37723772
{
37733773
const size_t chan = gb.GetLimitedUIValue('P', NumSerialChannels);
3774-
GCodeBuffer * const gbp = (chan == 0) ? UsbGCode() : (chan == 1) ? AuxGCode() : Aux2GCode();
3774+
bool hostMode = false;
37753775
bool seen = false;
3776-
if (gb.Seen('B'))
3776+
#if SUPPORT_USB_DRIVE
3777+
if (chan == 0 && gb.TryGetBValue('H', hostMode, seen)) // switch modes first
37773778
{
3778-
platform.SetBaudRate(chan, gb.GetUIValue());
3779-
seen = true;
3779+
if (!platform.SetUsbHostMode(hostMode, reply))
3780+
{
3781+
reply.printf("Unable to set to %s mode", hostMode ? "host" : "device");
3782+
result = GCodeResult::error;
3783+
}
37803784
}
3781-
3782-
if (gb.Seen('S'))
3785+
#else
3786+
reply.printf("USB host mode unsupported");
3787+
result = GCodeResult::error;
3788+
#endif
3789+
if (result == GCodeResult::ok && !hostMode) // switched to device mode with no error, handle other device mode args
37833790
{
3784-
const uint32_t val = gb.GetUIValue();
3785-
platform.SetCommsProperties(chan, val);
3786-
if (gbp != nullptr)
3791+
GCodeBuffer * const gbp = (chan == 0) ? UsbGCode() : (chan == 1) ? AuxGCode() : Aux2GCode();
3792+
if (gb.Seen('B'))
37873793
{
3788-
gbp->SetCommsProperties(val);
3789-
#if HAS_AUX_DEVICES
3790-
if (chan != 0)
3794+
platform.SetBaudRate(chan, gb.GetUIValue());
3795+
seen = true;
3796+
}
3797+
3798+
if (gb.Seen('S'))
3799+
{
3800+
const uint32_t val = gb.GetUIValue();
3801+
platform.SetCommsProperties(chan, val);
3802+
if (gbp != nullptr)
37913803
{
3792-
const bool rawMode = (val & 2u) != 0;
3793-
platform.SetAuxRaw(chan - 1, rawMode);
3794-
if (rawMode && !platform.IsAuxEnabled(chan - 1)) // if enabling aux for the first time and in raw mode, set Marlin compatibility
3804+
gbp->SetCommsProperties(val);
3805+
#if HAS_AUX_DEVICES
3806+
if (chan != 0)
37953807
{
3796-
gbp->LatestMachineState().compatibility = Compatibility::Marlin;
3808+
const bool rawMode = (val & 2u) != 0;
3809+
platform.SetAuxRaw(chan - 1, rawMode);
3810+
if (rawMode && !platform.IsAuxEnabled(chan - 1)) // if enabling aux for the first time and in raw mode, set Marlin compatibility
3811+
{
3812+
gbp->LatestMachineState().compatibility = Compatibility::Marlin;
3813+
}
37973814
}
3798-
}
37993815
#endif
3816+
}
3817+
seen = true;
38003818
}
3801-
seen = true;
3802-
}
38033819

3804-
if (seen)
3805-
{
3806-
#if HAS_AUX_DEVICES
3807-
if (chan != 0 && !platform.IsAuxEnabled(chan - 1))
3820+
if (seen)
38083821
{
3809-
platform.EnableAux(chan - 1);
3822+
#if HAS_AUX_DEVICES
3823+
if (chan != 0 && !platform.IsAuxEnabled(chan - 1))
3824+
{
3825+
platform.EnableAux(chan - 1);
3826+
}
3827+
else
3828+
{
3829+
platform.ResetChannel(chan);
3830+
}
38103831
}
3811-
else
3832+
else if (chan != 0 && !platform.IsAuxEnabled(chan - 1))
38123833
{
3813-
platform.ResetChannel(chan);
3814-
}
3815-
}
3816-
else if (chan != 0 && !platform.IsAuxEnabled(chan - 1))
3817-
{
3818-
reply.printf("Channel %u is disabled", chan);
3834+
reply.printf("Channel %u is disabled", chan);
38193835
#endif
3820-
}
3821-
else
3822-
{
3823-
const uint32_t cp = platform.GetCommsProperties(chan);
3824-
reply.printf("Channel %d: baud rate %" PRIu32 ", %s%s", chan, platform.GetBaudRate(chan),
3825-
(chan != 0 && platform.IsAuxRaw(chan - 1)) ? "raw mode, " : "",
3826-
(cp & 4) ? "requires CRC"
3827-
: (cp & 1) ? "requires checksum or CRC"
3828-
: "does not require checksum or CRC"
3829-
);
3830-
if (chan == 0 && SERIAL_MAIN_DEVICE.IsConnected())
3831-
{
3832-
reply.cat(", connected");
38333836
}
3834-
#if HAS_AUX_DEVICES
3835-
else if (chan != 0 && platform.IsAuxRaw(chan - 1))
3837+
else
38363838
{
3837-
reply.cat(", raw mode");
3838-
}
3839+
const uint32_t cp = platform.GetCommsProperties(chan);
3840+
reply.printf("Channel %d: baud rate %" PRIu32 ", %s%s", chan, platform.GetBaudRate(chan),
3841+
(chan != 0 && platform.IsAuxRaw(chan - 1)) ? "raw mode, " : "",
3842+
(cp & 4) ? "requires CRC"
3843+
: (cp & 1) ? "requires checksum or CRC"
3844+
: "does not require checksum or CRC"
3845+
);
3846+
3847+
if (chan == 0 && SERIAL_MAIN_DEVICE.IsConnected())
3848+
{
3849+
reply.cat(", connected");
3850+
}
3851+
#if HAS_AUX_DEVICES
3852+
else if (chan != 0 && platform.IsAuxRaw(chan - 1))
3853+
{
3854+
reply.cat(", raw mode");
3855+
}
38393856
#endif
3857+
}
38403858
}
38413859
}
38423860
break;

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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@
4646
#include <Storage/SdCardVolume.h>
4747
#include <Accelerometers/Accelerometers.h>
4848

49+
#if SUPPORT_USB_DRIVE
50+
#include <TinyUsbInterface.h>
51+
#endif
52+
4953
#if SAM4E || SAM4S || SAME70
5054
# include <AnalogIn.h>
5155
using LegacyAnalogIn::AdcBits;
@@ -3822,6 +3826,18 @@ void Platform::SetBaudRate(size_t chan, uint32_t br) noexcept
38223826
}
38233827
}
38243828

3829+
#if SUPPORT_USB_DRIVE
3830+
bool Platform::SetUsbHostMode(bool hostMode, const StringRef& reply) noexcept
3831+
{
3832+
#if CORE_USES_TINYUSB && CFG_TUH_ENABLED
3833+
return CoreUsbSetHostMode(hostMode, reply);
3834+
#else
3835+
reply.copy("Host mode not supported by USB stack");
3836+
return false; // unimplemented if not using tinyUSB
3837+
#endif
3838+
}
3839+
#endif
3840+
38253841
uint32_t Platform::GetBaudRate(size_t chan) const noexcept
38263842
{
38273843
return (chan < NumSerialChannels) ? baudRates[chan] : 0;

src/Platform/Platform.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,9 @@ class Platform INHERIT_OBJECT_MODEL
313313
void SetGateWay(IPAddress gw) noexcept;
314314
IPAddress GateWay() const noexcept;
315315
void SetBaudRate(size_t chan, uint32_t br) noexcept;
316+
#if SUPPORT_USB_DRIVE
317+
bool SetUsbHostMode(bool host, const StringRef& reply) noexcept;
318+
#endif
316319
uint32_t GetBaudRate(size_t chan) const noexcept;
317320
void SetCommsProperties(size_t chan, uint32_t cp) noexcept;
318321
uint32_t GetCommsProperties(size_t chan) const noexcept;

0 commit comments

Comments
 (0)