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.h 11KB

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