Skip to content

Commit

Permalink
🐛 Fix STM32 Pins Debugging (MarlinFirmware#22896)
Browse files Browse the repository at this point in the history
  • Loading branch information
ellensp authored and tomek2k1 committed Jan 13, 2023
1 parent 6d587c1 commit e3701c5
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 30 deletions.
1 change: 0 additions & 1 deletion Marlin/src/HAL/DUE/pinsDebug.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ bool GET_PINMODE(int8_t pin) { // 1: output, 0: input
|| pwm_status(pin));
}


void pwm_details(int32_t pin) {
if (pwm_status(pin)) {
uint32_t chan = g_APinDescription[pin].ulPWMChannel;
Expand Down
47 changes: 29 additions & 18 deletions Marlin/src/HAL/STM32/pinsDebug.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@
// make a list of the Arduino pin numbers in the Port/Pin order
//

#define _PIN_ADD_2(NAME_ALPHA, ARDUINO_NUM) { {NAME_ALPHA}, ARDUINO_NUM },
#define _PIN_ADD(NAME_ALPHA, ARDUINO_NUM) { NAME_ALPHA, ARDUINO_NUM },
#define PIN_ADD(NAME) _PIN_ADD(#NAME, NAME)

Expand Down Expand Up @@ -108,7 +107,11 @@ const XrefInfo pin_xref[] PROGMEM = {
/**
* Translation of routines & variables used by pinsDebug.h
*/
#define NUMBER_PINS_TOTAL NUM_DIGITAL_PINS

#if PA0 >= NUM_DIGITAL_PINS
#define HAS_HIGH_ANALOG_PINS 1
#endif
#define NUMBER_PINS_TOTAL NUM_DIGITAL_PINS + TERN0(HAS_HIGH_ANALOG_PINS, NUM_ANALOG_INPUTS)
#define VALID_PIN(ANUM) ((ANUM) >= 0 && (ANUM) < NUMBER_PINS_TOTAL)
#define digitalRead_mod(Ard_num) extDigitalRead(Ard_num) // must use Arduino pin numbers when doing reads
#define PRINT_PIN(Q)
Expand Down Expand Up @@ -164,17 +167,20 @@ bool GET_PINMODE(const pin_t Ard_num) {
return pin_mode == MODE_PIN_OUTPUT || pin_mode == MODE_PIN_ALT; // assume all alt definitions are PWM
}

int8_t digital_pin_to_analog_pin(pin_t Ard_num) {
Ard_num -= NUM_ANALOG_FIRST;
return (Ard_num >= 0 && Ard_num < NUM_ANALOG_INPUTS) ? Ard_num : -1;
int8_t digital_pin_to_analog_pin(const pin_t Ard_num) {
if (WITHIN(Ard_num, NUM_ANALOG_FIRST, NUM_ANALOG_FIRST + NUM_ANALOG_INPUTS - 1))
return Ard_num - NUM_ANALOG_FIRST;

const uint32_t ind = digitalPinToAnalogInput(Ard_num);
return (ind < NUM_ANALOG_INPUTS) ? ind : -1;
}

bool IS_ANALOG(const pin_t Ard_num) {
return get_pin_mode(Ard_num) == MODE_PIN_ANALOG;
}

bool is_digital(const pin_t x) {
const uint8_t pin_mode = get_pin_mode(pin_array[x].pin);
bool is_digital(const pin_t Ard_num) {
const uint8_t pin_mode = get_pin_mode(pin_array[Ard_num].pin);
return pin_mode == MODE_PIN_INPUT || pin_mode == MODE_PIN_OUTPUT;
}

Expand All @@ -200,10 +206,15 @@ void port_print(const pin_t Ard_num) {
SERIAL_ECHO_SP(7);

// Print number to be used with M42
sprintf_P(buffer, PSTR(" M42 P%d "), Ard_num);
SERIAL_ECHO(buffer);
if (Ard_num < 10) SERIAL_CHAR(' ');
if (Ard_num < 100) SERIAL_CHAR(' ');
int calc_p = Ard_num % (NUM_DIGITAL_PINS + 1);
if (Ard_num > NUM_DIGITAL_PINS && calc_p > 7) calc_p += 8;
SERIAL_ECHOPGM(" M42 P", calc_p);
SERIAL_CHAR(' ');
if (calc_p < 100) {
SERIAL_CHAR(' ');
if (calc_p < 10)
SERIAL_CHAR(' ');
}
}

bool pwm_status(const pin_t Ard_num) {
Expand All @@ -225,19 +236,19 @@ void pwm_details(const pin_t Ard_num) {
case 'D' : alt_all = GPIOD->AFR[ind]; break;
#ifdef PE_0
case 'E' : alt_all = GPIOE->AFR[ind]; break;
#elif defined (PF_0)
#elif defined(PF_0)
case 'F' : alt_all = GPIOF->AFR[ind]; break;
#elif defined (PG_0)
#elif defined(PG_0)
case 'G' : alt_all = GPIOG->AFR[ind]; break;
#elif defined (PH_0)
#elif defined(PH_0)
case 'H' : alt_all = GPIOH->AFR[ind]; break;
#elif defined (PI_0)
#elif defined(PI_0)
case 'I' : alt_all = GPIOI->AFR[ind]; break;
#elif defined (PJ_0)
#elif defined(PJ_0)
case 'J' : alt_all = GPIOJ->AFR[ind]; break;
#elif defined (PK_0)
#elif defined(PK_0)
case 'K' : alt_all = GPIOK->AFR[ind]; break;
#elif defined (PL_0)
#elif defined(PL_0)
case 'L' : alt_all = GPIOL->AFR[ind]; break;
#endif
}
Expand Down
14 changes: 7 additions & 7 deletions Marlin/src/gcode/config/M43.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ void GcodeSuite::M43() {

// 'P' Get the range of pins to test or watch
uint8_t first_pin = PARSED_PIN_INDEX('P', 0),
last_pin = parser.seenval('P') ? first_pin : NUMBER_PINS_TOTAL - 1;
last_pin = parser.seenval('P') ? first_pin : TERN(HAS_HIGH_ANALOG_PINS, NUM_DIGITAL_PINS, NUMBER_PINS_TOTAL) - 1;

if (first_pin > last_pin) return;

Expand All @@ -333,12 +333,12 @@ void GcodeSuite::M43() {
if (M43_NEVER_TOUCH(i) || (!ignore_protection && pin_is_protected(pin))) continue;
pinMode(pin, INPUT_PULLUP);
delay(1);
/*
if (IS_ANALOG(pin))
pin_state[pin - first_pin] = analogRead(DIGITAL_PIN_TO_ANALOG_PIN(pin)); // int16_t pin_state[...]
else
//*/
pin_state[i - first_pin] = extDigitalRead(pin);
/*
if (IS_ANALOG(pin))
pin_state[pin - first_pin] = analogRead(DIGITAL_PIN_TO_ANALOG_PIN(pin)); // int16_t pin_state[...]
else
//*/
pin_state[i - first_pin] = extDigitalRead(pin);
}

#if HAS_RESUME_CONTINUE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,10 @@ extern "C" {
#define PG15 111 //D79

// This must be a literal with the same value as PEND
#define NUM_DIGITAL_PINS 125
#define NUM_DIGITAL_PINS 112
// This must be a literal with a value less than or equal to to MAX_ANALOG_INPUTS
#define NUM_ANALOG_INPUTS 13
#define NUM_ANALOG_FIRST 112
#define NUM_ANALOG_FIRST NUM_DIGITAL_PINS

//#define ADC_RESOLUTION 12

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,10 @@ extern "C" {
#define PG15 111 //D79

// This must be a literal with the same value as PEND
#define NUM_DIGITAL_PINS 125
#define NUM_DIGITAL_PINS 112
// This must be a literal with a value less than or equal to to MAX_ANALOG_INPUTS
#define NUM_ANALOG_INPUTS 13
#define NUM_ANALOG_FIRST 112
#define NUM_ANALOG_FIRST NUM_DIGITAL_PINS

//#define ADC_RESOLUTION 12

Expand Down

0 comments on commit e3701c5

Please sign in to comment.