My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

HAL.cpp 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /**
  2. * Marlin 3D Printer Firmware
  3. *
  4. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  5. * Copyright (c) 2016 Bob Cousins bobcousins42@googlemail.com
  6. * Copyright (c) 2015-2016 Nico Tonnhofer wurstnase.reprap@gmail.com
  7. * Copyright (c) 2017 Victor Perez
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  21. *
  22. */
  23. /**
  24. * HAL for stm32duino.com based on Libmaple and compatible (STM32F1)
  25. */
  26. #ifdef __STM32F1__
  27. #include "../../inc/MarlinConfig.h"
  28. #include "HAL.h"
  29. #include <STM32ADC.h>
  30. // ------------------------
  31. // Types
  32. // ------------------------
  33. #define __I
  34. #define __IO volatile
  35. typedef struct {
  36. __I uint32_t CPUID; /*!< Offset: 0x000 (R/ ) CPUID Base Register */
  37. __IO uint32_t ICSR; /*!< Offset: 0x004 (R/W) Interrupt Control and State Register */
  38. __IO uint32_t VTOR; /*!< Offset: 0x008 (R/W) Vector Table Offset Register */
  39. __IO uint32_t AIRCR; /*!< Offset: 0x00C (R/W) Application Interrupt and Reset Control Register */
  40. __IO uint32_t SCR; /*!< Offset: 0x010 (R/W) System Control Register */
  41. __IO uint32_t CCR; /*!< Offset: 0x014 (R/W) Configuration Control Register */
  42. __IO uint8_t SHP[12]; /*!< Offset: 0x018 (R/W) System Handlers Priority Registers (4-7, 8-11, 12-15) */
  43. __IO uint32_t SHCSR; /*!< Offset: 0x024 (R/W) System Handler Control and State Register */
  44. __IO uint32_t CFSR; /*!< Offset: 0x028 (R/W) Configurable Fault Status Register */
  45. __IO uint32_t HFSR; /*!< Offset: 0x02C (R/W) HardFault Status Register */
  46. __IO uint32_t DFSR; /*!< Offset: 0x030 (R/W) Debug Fault Status Register */
  47. __IO uint32_t MMFAR; /*!< Offset: 0x034 (R/W) MemManage Fault Address Register */
  48. __IO uint32_t BFAR; /*!< Offset: 0x038 (R/W) BusFault Address Register */
  49. __IO uint32_t AFSR; /*!< Offset: 0x03C (R/W) Auxiliary Fault Status Register */
  50. __I uint32_t PFR[2]; /*!< Offset: 0x040 (R/ ) Processor Feature Register */
  51. __I uint32_t DFR; /*!< Offset: 0x048 (R/ ) Debug Feature Register */
  52. __I uint32_t ADR; /*!< Offset: 0x04C (R/ ) Auxiliary Feature Register */
  53. __I uint32_t MMFR[4]; /*!< Offset: 0x050 (R/ ) Memory Model Feature Register */
  54. __I uint32_t ISAR[5]; /*!< Offset: 0x060 (R/ ) Instruction Set Attributes Register */
  55. uint32_t RESERVED0[5];
  56. __IO uint32_t CPACR; /*!< Offset: 0x088 (R/W) Coprocessor Access Control Register */
  57. } SCB_Type;
  58. // ------------------------
  59. // Local defines
  60. // ------------------------
  61. #define SCS_BASE (0xE000E000UL) /*!< System Control Space Base Address */
  62. #define SCB_BASE (SCS_BASE + 0x0D00UL) /*!< System Control Block Base Address */
  63. #define SCB ((SCB_Type *) SCB_BASE ) /*!< SCB configuration struct */
  64. /* SCB Application Interrupt and Reset Control Register Definitions */
  65. #define SCB_AIRCR_VECTKEY_Pos 16 /*!< SCB AIRCR: VECTKEY Position */
  66. #define SCB_AIRCR_VECTKEY_Msk (0xFFFFUL << SCB_AIRCR_VECTKEY_Pos) /*!< SCB AIRCR: VECTKEY Mask */
  67. #define SCB_AIRCR_PRIGROUP_Pos 8 /*!< SCB AIRCR: PRIGROUP Position */
  68. #define SCB_AIRCR_PRIGROUP_Msk (7UL << SCB_AIRCR_PRIGROUP_Pos) /*!< SCB AIRCR: PRIGROUP Mask */
  69. // ------------------------
  70. // Public Variables
  71. // ------------------------
  72. #if defined(SERIAL_USB) && !HAS_SD_HOST_DRIVE
  73. USBSerial SerialUSB;
  74. DefaultSerial1 MSerial0(true, SerialUSB);
  75. #if ENABLED(EMERGENCY_PARSER)
  76. #include "../libmaple/usb/stm32f1/usb_reg_map.h"
  77. #include "libmaple/usb_cdcacm.h"
  78. // The original callback is not called (no way to retrieve address).
  79. // That callback detects a special STM32 reset sequence: this functionality is not essential
  80. // as M997 achieves the same.
  81. void my_rx_callback(unsigned int, void*) {
  82. // max length of 16 is enough to contain all emergency commands
  83. uint8 buf[16];
  84. //rx is usbSerialPart.endpoints[2]
  85. uint16 len = usb_get_ep_rx_count(USB_CDCACM_RX_ENDP);
  86. uint32 total = usb_cdcacm_data_available();
  87. if (len == 0 || total == 0 || !WITHIN(total, len, COUNT(buf)))
  88. return;
  89. // cannot get character by character due to bug in composite_cdcacm_peek_ex
  90. len = usb_cdcacm_peek(buf, total);
  91. for (uint32 i = 0; i < len; i++)
  92. emergency_parser.update(MSerial0.emergency_state, buf[i + total - len]);
  93. }
  94. #endif
  95. #endif
  96. uint16_t HAL_adc_result;
  97. // ------------------------
  98. // Private Variables
  99. // ------------------------
  100. STM32ADC adc(ADC1);
  101. const uint8_t adc_pins[] = {
  102. #if HAS_TEMP_ADC_0
  103. TEMP_0_PIN,
  104. #endif
  105. #if HAS_TEMP_ADC_PROBE
  106. TEMP_PROBE_PIN,
  107. #endif
  108. #if HAS_HEATED_BED
  109. TEMP_BED_PIN,
  110. #endif
  111. #if HAS_TEMP_CHAMBER
  112. TEMP_CHAMBER_PIN,
  113. #endif
  114. #if HAS_TEMP_COOLER
  115. TEMP_COOLER_PIN,
  116. #endif
  117. #if HAS_TEMP_ADC_1
  118. TEMP_1_PIN,
  119. #endif
  120. #if HAS_TEMP_ADC_2
  121. TEMP_2_PIN,
  122. #endif
  123. #if HAS_TEMP_ADC_3
  124. TEMP_3_PIN,
  125. #endif
  126. #if HAS_TEMP_ADC_4
  127. TEMP_4_PIN,
  128. #endif
  129. #if HAS_TEMP_ADC_5
  130. TEMP_5_PIN,
  131. #endif
  132. #if HAS_TEMP_ADC_6
  133. TEMP_6_PIN,
  134. #endif
  135. #if HAS_TEMP_ADC_7
  136. TEMP_7_PIN,
  137. #endif
  138. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  139. FILWIDTH_PIN,
  140. #endif
  141. #if HAS_ADC_BUTTONS
  142. ADC_KEYPAD_PIN,
  143. #endif
  144. #if HAS_JOY_ADC_X
  145. JOY_X_PIN,
  146. #endif
  147. #if HAS_JOY_ADC_Y
  148. JOY_Y_PIN,
  149. #endif
  150. #if HAS_JOY_ADC_Z
  151. JOY_Z_PIN,
  152. #endif
  153. #if ENABLED(POWER_MONITOR_CURRENT)
  154. POWER_MONITOR_CURRENT_PIN,
  155. #endif
  156. #if ENABLED(POWER_MONITOR_VOLTAGE)
  157. POWER_MONITOR_VOLTAGE_PIN,
  158. #endif
  159. };
  160. enum TempPinIndex : char {
  161. #if HAS_TEMP_ADC_0
  162. TEMP_0,
  163. #endif
  164. #if HAS_TEMP_ADC_PROBE
  165. TEMP_PROBE,
  166. #endif
  167. #if HAS_HEATED_BED
  168. TEMP_BED,
  169. #endif
  170. #if HAS_TEMP_CHAMBER
  171. TEMP_CHAMBER,
  172. #endif
  173. #if HAS_TEMP_COOLER
  174. TEMP_COOLER_PIN,
  175. #endif
  176. #if HAS_TEMP_ADC_1
  177. TEMP_1,
  178. #endif
  179. #if HAS_TEMP_ADC_2
  180. TEMP_2,
  181. #endif
  182. #if HAS_TEMP_ADC_3
  183. TEMP_3,
  184. #endif
  185. #if HAS_TEMP_ADC_4
  186. TEMP_4,
  187. #endif
  188. #if HAS_TEMP_ADC_5
  189. TEMP_5,
  190. #endif
  191. #if HAS_TEMP_ADC_6
  192. TEMP_6,
  193. #endif
  194. #if HAS_TEMP_ADC_7
  195. TEMP_7,
  196. #endif
  197. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  198. FILWIDTH,
  199. #endif
  200. #if HAS_ADC_BUTTONS
  201. ADC_KEY,
  202. #endif
  203. #if HAS_JOY_ADC_X
  204. JOY_X,
  205. #endif
  206. #if HAS_JOY_ADC_Y
  207. JOY_Y,
  208. #endif
  209. #if HAS_JOY_ADC_Z
  210. JOY_Z,
  211. #endif
  212. #if ENABLED(POWER_MONITOR_CURRENT)
  213. POWERMON_CURRENT,
  214. #endif
  215. #if ENABLED(POWER_MONITOR_VOLTAGE)
  216. POWERMON_VOLTS,
  217. #endif
  218. ADC_PIN_COUNT
  219. };
  220. uint16_t HAL_adc_results[ADC_PIN_COUNT];
  221. // ------------------------
  222. // Private functions
  223. // ------------------------
  224. static void NVIC_SetPriorityGrouping(uint32_t PriorityGroup) {
  225. uint32_t reg_value;
  226. uint32_t PriorityGroupTmp = (PriorityGroup & (uint32_t)0x07); /* only values 0..7 are used */
  227. reg_value = SCB->AIRCR; /* read old register configuration */
  228. reg_value &= ~(SCB_AIRCR_VECTKEY_Msk | SCB_AIRCR_PRIGROUP_Msk); /* clear bits to change */
  229. reg_value = (reg_value |
  230. ((uint32_t)0x5FA << SCB_AIRCR_VECTKEY_Pos) |
  231. (PriorityGroupTmp << 8)); /* Insert write key & priority group */
  232. SCB->AIRCR = reg_value;
  233. }
  234. // ------------------------
  235. // Public functions
  236. // ------------------------
  237. //
  238. // Leave PA11/PA12 intact if USBSerial is not used
  239. //
  240. #if SERIAL_USB
  241. namespace wirish { namespace priv {
  242. #if SERIAL_PORT > 0
  243. #if SERIAL_PORT2
  244. #if SERIAL_PORT2 > 0
  245. void board_setup_usb() {}
  246. #endif
  247. #else
  248. void board_setup_usb() {}
  249. #endif
  250. #endif
  251. } }
  252. #endif
  253. TERN_(POSTMORTEM_DEBUGGING, extern void install_min_serial());
  254. void HAL_init() {
  255. NVIC_SetPriorityGrouping(0x3);
  256. #if PIN_EXISTS(LED)
  257. OUT_WRITE(LED_PIN, LOW);
  258. #endif
  259. #if HAS_SD_HOST_DRIVE
  260. MSC_SD_init();
  261. #elif BOTH(SERIAL_USB, EMERGENCY_PARSER)
  262. usb_cdcacm_set_hooks(USB_CDCACM_HOOK_RX, my_rx_callback);
  263. #endif
  264. #if PIN_EXISTS(USB_CONNECT)
  265. OUT_WRITE(USB_CONNECT_PIN, !USB_CONNECT_INVERTING); // USB clear connection
  266. delay(1000); // Give OS time to notice
  267. WRITE(USB_CONNECT_PIN, USB_CONNECT_INVERTING);
  268. #endif
  269. TERN_(POSTMORTEM_DEBUGGING, install_min_serial()); // Install the minimal serial handler
  270. }
  271. // HAL idle task
  272. void HAL_idletask() {
  273. #if HAS_SHARED_MEDIA
  274. // If Marlin is using the SD card we need to lock it to prevent access from
  275. // a PC via USB.
  276. // Other HALs use IS_SD_PRINTING() and IS_SD_FILE_OPEN() to check for access but
  277. // this will not reliably detect delete operations. To be safe we will lock
  278. // the disk if Marlin has it mounted. Unfortunately there is currently no way
  279. // to unmount the disk from the LCD menu.
  280. // if (IS_SD_PRINTING() || IS_SD_FILE_OPEN())
  281. /* copy from lpc1768 framework, should be fixed later for process HAS_SD_HOST_DRIVE*/
  282. // process USB mass storage device class loop
  283. MarlinMSC.loop();
  284. #endif
  285. }
  286. void HAL_clear_reset_source() { }
  287. /**
  288. * TODO: Check this and change or remove.
  289. */
  290. uint8_t HAL_get_reset_source() { return RST_POWER_ON; }
  291. void _delay_ms(const int delay_ms) { delay(delay_ms); }
  292. extern "C" {
  293. extern unsigned int _ebss; // end of bss section
  294. }
  295. /**
  296. * TODO: Change this to correct it for libmaple
  297. */
  298. // return free memory between end of heap (or end bss) and whatever is current
  299. /*
  300. #include <wirish/syscalls.c>
  301. //extern caddr_t _sbrk(int incr);
  302. #ifndef CONFIG_HEAP_END
  303. extern char _lm_heap_end;
  304. #define CONFIG_HEAP_END ((caddr_t)&_lm_heap_end)
  305. #endif
  306. extern "C" {
  307. static int freeMemory() {
  308. char top = 't';
  309. return &top - reinterpret_cast<char*>(sbrk(0));
  310. }
  311. int freeMemory() {
  312. int free_memory;
  313. int heap_end = (int)_sbrk(0);
  314. free_memory = ((int)&free_memory) - ((int)heap_end);
  315. return free_memory;
  316. }
  317. }
  318. */
  319. // ------------------------
  320. // ADC
  321. // ------------------------
  322. // Init the AD in continuous capture mode
  323. void HAL_adc_init() {
  324. // configure the ADC
  325. adc.calibrate();
  326. #if F_CPU > 72000000
  327. adc.setSampleRate(ADC_SMPR_71_5); // 71.5 ADC cycles
  328. #else
  329. adc.setSampleRate(ADC_SMPR_41_5); // 41.5 ADC cycles
  330. #endif
  331. adc.setPins((uint8_t *)adc_pins, ADC_PIN_COUNT);
  332. adc.setDMA(HAL_adc_results, (uint16_t)ADC_PIN_COUNT, (uint32_t)(DMA_MINC_MODE | DMA_CIRC_MODE), nullptr);
  333. adc.setScanMode();
  334. adc.setContinuous();
  335. adc.startConversion();
  336. }
  337. void HAL_adc_start_conversion(const uint8_t adc_pin) {
  338. //TEMP_PINS pin_index;
  339. TempPinIndex pin_index;
  340. switch (adc_pin) {
  341. default: return;
  342. #if HAS_TEMP_ADC_0
  343. case TEMP_0_PIN: pin_index = TEMP_0; break;
  344. #endif
  345. #if HAS_TEMP_ADC_PROBE
  346. case TEMP_PROBE_PIN: pin_index = TEMP_PROBE; break;
  347. #endif
  348. #if HAS_HEATED_BED
  349. case TEMP_BED_PIN: pin_index = TEMP_BED; break;
  350. #endif
  351. #if HAS_TEMP_CHAMBER
  352. case TEMP_CHAMBER_PIN: pin_index = TEMP_CHAMBER; break;
  353. #endif
  354. #if HAS_TEMP_COOLER
  355. case TEMP_COOLER_PIN: pin_index = TEMP_COOLER; break;
  356. #endif
  357. #if HAS_TEMP_ADC_1
  358. case TEMP_1_PIN: pin_index = TEMP_1; break;
  359. #endif
  360. #if HAS_TEMP_ADC_2
  361. case TEMP_2_PIN: pin_index = TEMP_2; break;
  362. #endif
  363. #if HAS_TEMP_ADC_3
  364. case TEMP_3_PIN: pin_index = TEMP_3; break;
  365. #endif
  366. #if HAS_TEMP_ADC_4
  367. case TEMP_4_PIN: pin_index = TEMP_4; break;
  368. #endif
  369. #if HAS_TEMP_ADC_5
  370. case TEMP_5_PIN: pin_index = TEMP_5; break;
  371. #endif
  372. #if HAS_TEMP_ADC_6
  373. case TEMP_6_PIN: pin_index = TEMP_6; break;
  374. #endif
  375. #if HAS_TEMP_ADC_7
  376. case TEMP_7_PIN: pin_index = TEMP_7; break;
  377. #endif
  378. #if HAS_JOY_ADC_X
  379. case JOY_X_PIN: pin_index = JOY_X; break;
  380. #endif
  381. #if HAS_JOY_ADC_Y
  382. case JOY_Y_PIN: pin_index = JOY_Y; break;
  383. #endif
  384. #if HAS_JOY_ADC_Z
  385. case JOY_Z_PIN: pin_index = JOY_Z; break;
  386. #endif
  387. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  388. case FILWIDTH_PIN: pin_index = FILWIDTH; break;
  389. #endif
  390. #if HAS_ADC_BUTTONS
  391. case ADC_KEYPAD_PIN: pin_index = ADC_KEY; break;
  392. #endif
  393. #if ENABLED(POWER_MONITOR_CURRENT)
  394. case POWER_MONITOR_CURRENT_PIN: pin_index = POWERMON_CURRENT; break;
  395. #endif
  396. #if ENABLED(POWER_MONITOR_VOLTAGE)
  397. case POWER_MONITOR_VOLTAGE_PIN: pin_index = POWERMON_VOLTS; break;
  398. #endif
  399. }
  400. HAL_adc_result = (HAL_adc_results[(int)pin_index] >> 2) & 0x3FF; // shift to get 10 bits only.
  401. }
  402. uint16_t HAL_adc_get_result() { return HAL_adc_result; }
  403. uint16_t analogRead(pin_t pin) {
  404. const bool is_analog = _GET_MODE(pin) == GPIO_INPUT_ANALOG;
  405. return is_analog ? analogRead(uint8_t(pin)) : 0;
  406. }
  407. // Wrapper to maple unprotected analogWrite
  408. void analogWrite(pin_t pin, int pwm_val8) {
  409. if (PWM_PIN(pin))
  410. analogWrite(uint8_t(pin), pwm_val8);
  411. }
  412. void HAL_reboot() { nvic_sys_reset(); }
  413. void flashFirmware(const int16_t) { HAL_reboot(); }
  414. #endif // __STM32F1__