@@ -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 ;
0 commit comments