My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pinsDebug_STM32duino.h 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. *
  18. */
  19. #pragma once
  20. #include <Arduino.h>
  21. #ifdef NUM_DIGITAL_PINS // Only in ST's Arduino core (STM32duino, STM32Core)
  22. /**
  23. * Life gets complicated if you want an easy to use 'M43 I' output (in port/pin order)
  24. * because the variants in this platform do not always define all the I/O port/pins
  25. * that a CPU has.
  26. *
  27. * VARIABLES:
  28. * Ard_num - Arduino pin number - defined by the platform. It is used by digitalRead and
  29. * digitalWrite commands and by M42.
  30. * - does not contain port/pin info
  31. * - is not in port/pin order
  32. * - typically a variant will only assign Ard_num to port/pins that are actually used
  33. * Index - M43 counter - only used to get Ard_num
  34. * x - a parameter/argument used to search the pin_array to try to find a signal name
  35. * associated with a Ard_num
  36. * Port_pin - port number and pin number for use with CPU registers and printing reports
  37. *
  38. * Since M43 uses digitalRead and digitalWrite commands, only the Port_pins with an Ard_num
  39. * are accessed and/or displayed.
  40. *
  41. * Three arrays are used.
  42. *
  43. * digitalPin[] is provided by the platform. It consists of the Port_pin numbers in
  44. * Arduino pin number order.
  45. *
  46. * pin_array is a structure generated by the pins/pinsDebug.h header file. It is generated by
  47. * the preprocessor. Only the signals associated with enabled options are in this table.
  48. * It contains:
  49. * - name of the signal
  50. * - the Ard_num assigned by the pins_YOUR_BOARD.h file using the platform defines.
  51. * EXAMPLE: "#define KILL_PIN PB1" results in Ard_num of 57. 57 is then used as the
  52. * argument to digitalPinToPinName(IO) to get the Port_pin number
  53. * - if it is a digital or analog signal. PWMs are considered digital here.
  54. *
  55. * pin_xref is a structure generated by this header file. It is generated by the
  56. * preprocessor. It is in port/pin order. It contains just the port/pin numbers defined by the
  57. * platform for this variant.
  58. * - Ard_num
  59. * - printable version of Port_pin
  60. *
  61. * Routines with an "x" as a parameter/argument are used to search the pin_array to try to
  62. * find a signal name associated with a port/pin.
  63. *
  64. * NOTE - the Arduino pin number is what is used by the M42 command, NOT the port/pin for that
  65. * signal. The Arduino pin number is listed by the M43 I command.
  66. */
  67. ////////////////////////////////////////////////////////
  68. //
  69. // make a list of the Arduino pin numbers in the Port/Pin order
  70. //
  71. #define _PIN_ADD_2(NAME_ALPHA, ARDUINO_NUM) { {NAME_ALPHA}, ARDUINO_NUM },
  72. #define _PIN_ADD(NAME_ALPHA, ARDUINO_NUM) { NAME_ALPHA, ARDUINO_NUM },
  73. #define PIN_ADD(NAME) _PIN_ADD(#NAME, NAME)
  74. typedef struct {
  75. char Port_pin_alpha[5];
  76. pin_t Ard_num;
  77. } XrefInfo;
  78. const XrefInfo pin_xref[] PROGMEM = {
  79. #include "pins_Xref.h"
  80. };
  81. ////////////////////////////////////////////////////////////
  82. #define MODE_PIN_INPUT 0 // Input mode (reset state)
  83. #define MODE_PIN_OUTPUT 1 // General purpose output mode
  84. #define MODE_PIN_ALT 2 // Alternate function mode
  85. #define MODE_PIN_ANALOG 3 // Analog mode
  86. #define PIN_NUM(P) (P & 0x000F)
  87. #define PIN_NUM_ALPHA_LEFT(P) (((P & 0x000F) < 10) ? ('0' + (P & 0x000F)) : '1')
  88. #define PIN_NUM_ALPHA_RIGHT(P) (((P & 0x000F) > 9) ? ('0' + (P & 0x000F) - 10) : 0 )
  89. #define PORT_NUM(P) ((P >> 4) & 0x0007)
  90. #define PORT_ALPHA(P) ('A' + (P >> 4))
  91. /**
  92. * Translation of routines & variables used by pinsDebug.h
  93. */
  94. #define NUMBER_PINS_TOTAL NUM_DIGITAL_PINS
  95. #define VALID_PIN(ANUM) ((ANUM) >= 0 && (ANUM) < NUMBER_PINS_TOTAL)
  96. #define digitalRead_mod(Ard_num) extDigitalRead(Ard_num) // must use Arduino pin numbers when doing reads
  97. #define PRINT_PIN(Q)
  98. #define PRINT_PORT(ANUM) port_print(ANUM)
  99. #define DIGITAL_PIN_TO_ANALOG_PIN(ANUM) -1 // will report analog pin number in the print port routine
  100. #define GET_PIN_MAP_PIN_M43(Index) pin_xref[Index].Ard_num
  101. // x is a variable used to search pin_array
  102. #define GET_ARRAY_IS_DIGITAL(x) ((bool) pin_array[x].is_digital)
  103. #define GET_ARRAY_PIN(x) ((pin_t) pin_array[x].pin)
  104. #define PRINT_ARRAY_NAME(x) do{ sprintf_P(buffer, PSTR("%-" STRINGIFY(MAX_NAME_LENGTH) "s"), pin_array[x].name); SERIAL_ECHO(buffer); }while(0)
  105. #define MULTI_NAME_PAD 33 // space needed to be pretty if not first name assigned to a pin
  106. #ifndef M43_NEVER_TOUCH
  107. #define _M43_NEVER_TOUCH(Index) (Index >= 9 && Index <= 12) // SERIAL/USB pins: PA9(TX) PA10(RX) PA11(USB_DM) PA12(USB_DP)
  108. #ifdef KILL_PIN
  109. #define M43_NEVER_TOUCH(Index) m43_never_touch(Index)
  110. bool m43_never_touch(const pin_t Index) {
  111. static pin_t M43_kill_index = -1;
  112. if (M43_kill_index < 0)
  113. for (M43_kill_index = 0; M43_kill_index < NUMBER_PINS_TOTAL; M43_kill_index++)
  114. if (KILL_PIN == GET_PIN_MAP_PIN_M43(M43_kill_index)) break;
  115. return _M43_NEVER_TOUCH(Index) || Index == M43_kill_index; // KILL_PIN and SERIAL/USB
  116. }
  117. #else
  118. #define M43_NEVER_TOUCH(Index) _M43_NEVER_TOUCH(Index)
  119. #endif
  120. #endif
  121. uint8_t get_pin_mode(const pin_t Ard_num) {
  122. uint32_t mode_all = 0;
  123. const PinName dp = digitalPinToPinName(Ard_num);
  124. switch (PORT_ALPHA(dp)) {
  125. case 'A' : mode_all = GPIOA->MODER; break;
  126. case 'B' : mode_all = GPIOB->MODER; break;
  127. case 'C' : mode_all = GPIOC->MODER; break;
  128. case 'D' : mode_all = GPIOD->MODER; break;
  129. #ifdef PE_0
  130. case 'E' : mode_all = GPIOE->MODER; break;
  131. #elif defined(PF_0)
  132. case 'F' : mode_all = GPIOF->MODER; break;
  133. #elif defined(PG_0)
  134. case 'G' : mode_all = GPIOG->MODER; break;
  135. #elif defined(PH_0)
  136. case 'H' : mode_all = GPIOH->MODER; break;
  137. #elif defined(PI_0)
  138. case 'I' : mode_all = GPIOI->MODER; break;
  139. #elif defined(PJ_0)
  140. case 'J' : mode_all = GPIOJ->MODER; break;
  141. #elif defined(PK_0)
  142. case 'K' : mode_all = GPIOK->MODER; break;
  143. #elif defined(PL_0)
  144. case 'L' : mode_all = GPIOL->MODER; break;
  145. #endif
  146. }
  147. return (mode_all >> (2 * uint8_t(PIN_NUM(dp)))) & 0x03;
  148. }
  149. bool GET_PINMODE(const pin_t Ard_num) {
  150. const uint8_t pin_mode = get_pin_mode(Ard_num);
  151. return pin_mode == MODE_PIN_OUTPUT || pin_mode == MODE_PIN_ALT; // assume all alt definitions are PWM
  152. }
  153. int8_t digital_pin_to_analog_pin(pin_t Ard_num) {
  154. Ard_num -= NUM_ANALOG_FIRST;
  155. return (Ard_num >= 0 && Ard_num < NUM_ANALOG_INPUTS) ? Ard_num : -1;
  156. }
  157. bool IS_ANALOG(const pin_t Ard_num) {
  158. return get_pin_mode(Ard_num) == MODE_PIN_ANALOG;
  159. }
  160. bool is_digital(const pin_t x) {
  161. const uint8_t pin_mode = get_pin_mode(pin_array[x].pin);
  162. return pin_mode == MODE_PIN_INPUT || pin_mode == MODE_PIN_OUTPUT;
  163. }
  164. void port_print(const pin_t Ard_num) {
  165. char buffer[16];
  166. pin_t Index;
  167. for (Index = 0; Index < NUMBER_PINS_TOTAL; Index++)
  168. if (Ard_num == GET_PIN_MAP_PIN_M43(Index)) break;
  169. const char * ppa = pin_xref[Index].Port_pin_alpha;
  170. sprintf_P(buffer, PSTR("%s"), ppa);
  171. SERIAL_ECHO(buffer);
  172. if (ppa[3] == '\0') SERIAL_CHAR(' ');
  173. // print analog pin number
  174. const int8_t Port_pin = digital_pin_to_analog_pin(Ard_num);
  175. if (Port_pin >= 0) {
  176. sprintf_P(buffer, PSTR(" (A%d) "), Port_pin);
  177. SERIAL_ECHO(buffer);
  178. if (Port_pin < 10) SERIAL_CHAR(' ');
  179. }
  180. else
  181. SERIAL_ECHO_SP(7);
  182. // Print number to be used with M42
  183. sprintf_P(buffer, PSTR(" M42 P%d "), Ard_num);
  184. SERIAL_ECHO(buffer);
  185. if (Ard_num < 10) SERIAL_CHAR(' ');
  186. if (Ard_num < 100) SERIAL_CHAR(' ');
  187. }
  188. bool pwm_status(const pin_t Ard_num) {
  189. return get_pin_mode(Ard_num) == MODE_PIN_ALT;
  190. }
  191. void pwm_details(const pin_t Ard_num) {
  192. if (pwm_status(Ard_num)) {
  193. uint32_t alt_all = 0;
  194. const PinName dp = digitalPinToPinName(Ard_num);
  195. pin_t pin_number = uint8_t(PIN_NUM(dp));
  196. const bool over_7 = pin_number >= 8;
  197. const uint8_t ind = over_7 ? 1 : 0;
  198. switch (PORT_ALPHA(dp)) { // get alt function
  199. case 'A' : alt_all = GPIOA->AFR[ind]; break;
  200. case 'B' : alt_all = GPIOB->AFR[ind]; break;
  201. case 'C' : alt_all = GPIOC->AFR[ind]; break;
  202. case 'D' : alt_all = GPIOD->AFR[ind]; break;
  203. #ifdef PE_0
  204. case 'E' : alt_all = GPIOE->AFR[ind]; break;
  205. #elif defined (PF_0)
  206. case 'F' : alt_all = GPIOF->AFR[ind]; break;
  207. #elif defined (PG_0)
  208. case 'G' : alt_all = GPIOG->AFR[ind]; break;
  209. #elif defined (PH_0)
  210. case 'H' : alt_all = GPIOH->AFR[ind]; break;
  211. #elif defined (PI_0)
  212. case 'I' : alt_all = GPIOI->AFR[ind]; break;
  213. #elif defined (PJ_0)
  214. case 'J' : alt_all = GPIOJ->AFR[ind]; break;
  215. #elif defined (PK_0)
  216. case 'K' : alt_all = GPIOK->AFR[ind]; break;
  217. #elif defined (PL_0)
  218. case 'L' : alt_all = GPIOL->AFR[ind]; break;
  219. #endif
  220. }
  221. if (over_7) pin_number -= 8;
  222. uint8_t alt_func = (alt_all >> (4 * pin_number)) & 0x0F;
  223. SERIAL_ECHOPAIR("Alt Function: ", alt_func);
  224. if (alt_func < 10) SERIAL_CHAR(' ');
  225. SERIAL_ECHOPGM(" - ");
  226. switch (alt_func) {
  227. case 0 : SERIAL_ECHOPGM("system (misc. I/O)"); break;
  228. case 1 : SERIAL_ECHOPGM("TIM1/TIM2 (probably PWM)"); break;
  229. case 2 : SERIAL_ECHOPGM("TIM3..5 (probably PWM)"); break;
  230. case 3 : SERIAL_ECHOPGM("TIM8..11 (probably PWM)"); break;
  231. case 4 : SERIAL_ECHOPGM("I2C1..3"); break;
  232. case 5 : SERIAL_ECHOPGM("SPI1/SPI2"); break;
  233. case 6 : SERIAL_ECHOPGM("SPI3"); break;
  234. case 7 : SERIAL_ECHOPGM("USART1..3"); break;
  235. case 8 : SERIAL_ECHOPGM("USART4..6"); break;
  236. case 9 : SERIAL_ECHOPGM("CAN1/CAN2, TIM12..14 (probably PWM)"); break;
  237. case 10 : SERIAL_ECHOPGM("OTG"); break;
  238. case 11 : SERIAL_ECHOPGM("ETH"); break;
  239. case 12 : SERIAL_ECHOPGM("FSMC, SDIO, OTG"); break;
  240. case 13 : SERIAL_ECHOPGM("DCMI"); break;
  241. case 14 : SERIAL_ECHOPGM("unused (shouldn't see this)"); break;
  242. case 15 : SERIAL_ECHOPGM("EVENTOUT"); break;
  243. }
  244. }
  245. } // pwm_details
  246. #endif // NUM_DIGITAL_PINS