@@ -435,32 +435,48 @@ void MotorDriver::setDCSignal(byte speedcode, uint8_t frequency /*default =0*/)
435435 }
436436}
437437void MotorDriver::throttleInrush (bool on) {
438- if (brakePin == UNUSED_PIN)
438+ byte throttlePin = brakePin;
439+ #if defined(ARDUINO_ARCH_ESP32)
440+ // On ESP32 we want to run inrush current throttling on the power
441+ // pin so we can use the brake pin for cutout. But we must check
442+ // before that the powerPin really is local IO and not a VPIN on
443+ // I2C. Check for inverted value as well. This is not as easy
444+ // on other architectures where cutout is tied to specific pins
445+ // and these pins can have high numbers we don't know here.
446+ const byte ESP32_MAX_PHYS_PIN=39 ;
447+ if (powerPin <= ESP32_MAX_PHYS_PIN && powerPin >= -(ESP32_MAX_PHYS_PIN)) {
448+ throttlePin = (byte)powerPin;
449+ } else {
450+ DIAG (F (" Warning: Can not produce a reliable cutout on brake pin %d because power pin %d "
451+ " can not be used for current inrush control" ), brakePin, powerPin);
452+ }
453+ #endif
454+ if (throttlePin == UNUSED_PIN)
439455 return ;
440456 if ( !(trackMode & (TRACK_MODE_MAIN | TRACK_MODE_PROG | TRACK_MODE_EXT | TRACK_MODE_BOOST)))
441457 return ;
442458 byte duty = on ? 207 : 0 ; // duty of 81% at 62500Hz this gives pauses of 3usec
443459#if defined(ARDUINO_ARCH_ESP32)
444460 if (on) {
445- DCCTimer::DCCEXInrushControlOn (brakePin , duty, invertBrake);
461+ DCCTimer::DCCEXInrushControlOn (throttlePin , duty, invertBrake);
446462 } else {
447- ledcDetachPin (brakePin ); // not DCCTimer::DCCEXledcDetachPin() as we have not
463+ ledcDetachPin (throttlePin ); // not DCCTimer::DCCEXledcDetachPin() as we have not
448464 // registered the pin in the pin to channel array
449465 }
450466#elif defined(ARDUINO_ARCH_STM32)
451467 if (on) {
452- DCCTimer::DCCEXanalogWriteFrequency (brakePin , 7 ); // 7 means max
453- DCCTimer::DCCEXanalogWrite (brakePin ,duty,invertBrake);
468+ DCCTimer::DCCEXanalogWriteFrequency (throttlePin , 7 ); // 7 means max
469+ DCCTimer::DCCEXanalogWrite (throttlePin ,duty,invertBrake);
454470 } else {
455- pinMode (brakePin , OUTPUT);
471+ pinMode (throttlePin , OUTPUT);
456472 }
457473#else // all AVR here
458474 if (invertBrake)
459475 duty = 255 -duty;
460476 if (on){
461- DCCTimer::DCCEXanalogWriteFrequency (brakePin , 7 ); // 7 means max
477+ DCCTimer::DCCEXanalogWriteFrequency (throttlePin , 7 ); // 7 means max
462478 }
463- analogWrite (brakePin ,duty);
479+ analogWrite (throttlePin ,duty);
464480#endif
465481}
466482unsigned int MotorDriver::raw2mA ( int raw) {
0 commit comments