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.

HAL.cpp 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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. // Serial ports
  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. // ------------------------
  97. // Watchdog Timer
  98. // ------------------------
  99. #if ENABLED(USE_WATCHDOG)
  100. #include <libmaple/iwdg.h>
  101. void watchdogSetup() {
  102. // do whatever. don't remove this function.
  103. }
  104. /**
  105. * The watchdog clock is 40Khz. So for a 4s or 8s interval use a /256 preescaler and 625 or 1250 reload value (counts down to 0).
  106. */
  107. #define STM32F1_WD_RELOAD TERN(WATCHDOG_DURATION_8S, 1250, 625) // 4 or 8 second timeout
  108. /**
  109. * @brief Initialize the independent hardware watchdog.
  110. *
  111. * @return No return
  112. *
  113. * @details The watchdog clock is 40Khz. So for a 4s or 8s interval use a /256 preescaler and 625 or 1250 reload value (counts down to 0).
  114. */
  115. void MarlinHAL::watchdog_init() {
  116. #if DISABLED(DISABLE_WATCHDOG_INIT)
  117. iwdg_init(IWDG_PRE_256, STM32F1_WD_RELOAD);
  118. #endif
  119. }
  120. // Reset watchdog. MUST be called every 4 or 8 seconds after the
  121. // first watchdog_init or the STM32F1 will reset.
  122. void MarlinHAL::watchdog_refresh() {
  123. #if DISABLED(PINS_DEBUGGING) && PIN_EXISTS(LED)
  124. TOGGLE(LED_PIN); // heartbeat indicator
  125. #endif
  126. iwdg_feed();
  127. }
  128. #endif // USE_WATCHDOG
  129. // ------------------------
  130. // ADC
  131. // ------------------------
  132. // Watch out for recursion here! Our pin_t is signed, so pass through to Arduino -> analogRead(uint8_t)
  133. uint16_t analogRead(const pin_t pin) {
  134. const bool is_analog = _GET_MODE(pin) == GPIO_INPUT_ANALOG;
  135. return is_analog ? analogRead(uint8_t(pin)) : 0;
  136. }
  137. // Wrapper to maple unprotected analogWrite
  138. void analogWrite(const pin_t pin, int pwm_val8) {
  139. if (PWM_PIN(pin)) analogWrite(uint8_t(pin), pwm_val8);
  140. }
  141. uint16_t MarlinHAL::adc_result;
  142. // ------------------------
  143. // Private functions
  144. // ------------------------
  145. static void NVIC_SetPriorityGrouping(uint32_t PriorityGroup) {
  146. uint32_t reg_value;
  147. uint32_t PriorityGroupTmp = (PriorityGroup & (uint32_t)0x07); // only values 0..7 are used
  148. reg_value = SCB->AIRCR; // read old register configuration
  149. reg_value &= ~(SCB_AIRCR_VECTKEY_Msk | SCB_AIRCR_PRIGROUP_Msk); // clear bits to change
  150. reg_value = (reg_value |
  151. ((uint32_t)0x5FA << SCB_AIRCR_VECTKEY_Pos) |
  152. (PriorityGroupTmp << 8)); // Insert write key & priority group
  153. SCB->AIRCR = reg_value;
  154. }
  155. // ------------------------
  156. // Public functions
  157. // ------------------------
  158. void flashFirmware(const int16_t) { hal.reboot(); }
  159. //
  160. // Leave PA11/PA12 intact if USBSerial is not used
  161. //
  162. #if SERIAL_USB
  163. namespace wirish { namespace priv {
  164. #if SERIAL_PORT > 0
  165. #if SERIAL_PORT2
  166. #if SERIAL_PORT2 > 0
  167. void board_setup_usb() {}
  168. #endif
  169. #else
  170. void board_setup_usb() {}
  171. #endif
  172. #endif
  173. } }
  174. #endif
  175. TERN_(POSTMORTEM_DEBUGGING, extern void install_min_serial());
  176. // ------------------------
  177. // MarlinHAL class
  178. // ------------------------
  179. void MarlinHAL::init() {
  180. NVIC_SetPriorityGrouping(0x3);
  181. #if PIN_EXISTS(LED)
  182. OUT_WRITE(LED_PIN, LOW);
  183. #endif
  184. #if HAS_SD_HOST_DRIVE
  185. MSC_SD_init();
  186. #elif BOTH(SERIAL_USB, EMERGENCY_PARSER)
  187. usb_cdcacm_set_hooks(USB_CDCACM_HOOK_RX, my_rx_callback);
  188. #endif
  189. #if PIN_EXISTS(USB_CONNECT)
  190. OUT_WRITE(USB_CONNECT_PIN, !USB_CONNECT_INVERTING); // USB clear connection
  191. delay(1000); // Give OS time to notice
  192. WRITE(USB_CONNECT_PIN, USB_CONNECT_INVERTING);
  193. #endif
  194. TERN_(POSTMORTEM_DEBUGGING, install_min_serial()); // Install the minimal serial handler
  195. }
  196. // HAL idle task
  197. void MarlinHAL::idletask() {
  198. #if HAS_SHARED_MEDIA
  199. // If Marlin is using the SD card we need to lock it to prevent access from
  200. // a PC via USB.
  201. // Other HALs use IS_SD_PRINTING() and IS_SD_FILE_OPEN() to check for access but
  202. // this will not reliably detect delete operations. To be safe we will lock
  203. // the disk if Marlin has it mounted. Unfortunately there is currently no way
  204. // to unmount the disk from the LCD menu.
  205. // if (IS_SD_PRINTING() || IS_SD_FILE_OPEN())
  206. /* copy from lpc1768 framework, should be fixed later for process HAS_SD_HOST_DRIVE*/
  207. // process USB mass storage device class loop
  208. MarlinMSC.loop();
  209. #endif
  210. }
  211. void MarlinHAL::reboot() { nvic_sys_reset(); }
  212. // ------------------------
  213. // Free Memory Accessor
  214. // ------------------------
  215. extern "C" {
  216. extern unsigned int _ebss; // end of bss section
  217. }
  218. /**
  219. * TODO: Change this to correct it for libmaple
  220. */
  221. // return free memory between end of heap (or end bss) and whatever is current
  222. /*
  223. #include <wirish/syscalls.c>
  224. //extern caddr_t _sbrk(int incr);
  225. #ifndef CONFIG_HEAP_END
  226. extern char _lm_heap_end;
  227. #define CONFIG_HEAP_END ((caddr_t)&_lm_heap_end)
  228. #endif
  229. extern "C" {
  230. static int freeMemory() {
  231. char top = 't';
  232. return &top - reinterpret_cast<char*>(sbrk(0));
  233. }
  234. int freeMemory() {
  235. int free_memory;
  236. int heap_end = (int)_sbrk(0);
  237. free_memory = ((int)&free_memory) - ((int)heap_end);
  238. return free_memory;
  239. }
  240. }
  241. */
  242. // ------------------------
  243. // ADC
  244. // ------------------------
  245. enum ADCIndex : uint8_t {
  246. OPTITEM(HAS_TEMP_ADC_0, TEMP_0)
  247. OPTITEM(HAS_TEMP_ADC_1, TEMP_1)
  248. OPTITEM(HAS_TEMP_ADC_2, TEMP_2)
  249. OPTITEM(HAS_TEMP_ADC_3, TEMP_3)
  250. OPTITEM(HAS_TEMP_ADC_4, TEMP_4)
  251. OPTITEM(HAS_TEMP_ADC_5, TEMP_5)
  252. OPTITEM(HAS_TEMP_ADC_6, TEMP_6)
  253. OPTITEM(HAS_TEMP_ADC_7, TEMP_7)
  254. OPTITEM(HAS_HEATED_BED, TEMP_BED)
  255. OPTITEM(HAS_TEMP_CHAMBER, TEMP_CHAMBER)
  256. OPTITEM(HAS_TEMP_ADC_PROBE, TEMP_PROBE)
  257. OPTITEM(HAS_TEMP_COOLER, TEMP_COOLER)
  258. OPTITEM(HAS_TEMP_BOARD, TEMP_BOARD)
  259. OPTITEM(FILAMENT_WIDTH_SENSOR, FILWIDTH)
  260. OPTITEM(HAS_ADC_BUTTONS, ADC_KEY)
  261. OPTITEM(HAS_JOY_ADC_X, JOY_X)
  262. OPTITEM(HAS_JOY_ADC_Y, JOY_Y)
  263. OPTITEM(HAS_JOY_ADC_Z, JOY_Z)
  264. OPTITEM(POWER_MONITOR_CURRENT, POWERMON_CURRENT)
  265. OPTITEM(POWER_MONITOR_VOLTAGE, POWERMON_VOLTS)
  266. ADC_COUNT
  267. };
  268. static uint16_t adc_results[ADC_COUNT];
  269. // Init the AD in continuous capture mode
  270. void MarlinHAL::adc_init() {
  271. static const uint8_t adc_pins[] = {
  272. OPTITEM(HAS_TEMP_ADC_0, TEMP_0_PIN)
  273. OPTITEM(HAS_TEMP_ADC_1, TEMP_1_PIN)
  274. OPTITEM(HAS_TEMP_ADC_2, TEMP_2_PIN)
  275. OPTITEM(HAS_TEMP_ADC_3, TEMP_3_PIN)
  276. OPTITEM(HAS_TEMP_ADC_4, TEMP_4_PIN)
  277. OPTITEM(HAS_TEMP_ADC_5, TEMP_5_PIN)
  278. OPTITEM(HAS_TEMP_ADC_6, TEMP_6_PIN)
  279. OPTITEM(HAS_TEMP_ADC_7, TEMP_7_PIN)
  280. OPTITEM(HAS_HEATED_BED, TEMP_BED_PIN)
  281. OPTITEM(HAS_TEMP_CHAMBER, TEMP_CHAMBER_PIN)
  282. OPTITEM(HAS_TEMP_ADC_PROBE, TEMP_PROBE_PIN)
  283. OPTITEM(HAS_TEMP_COOLER, TEMP_COOLER_PIN)
  284. OPTITEM(HAS_TEMP_BOARD, TEMP_BOARD_PIN)
  285. OPTITEM(FILAMENT_WIDTH_SENSOR, FILWIDTH_PIN)
  286. OPTITEM(HAS_ADC_BUTTONS, ADC_KEYPAD_PIN)
  287. OPTITEM(HAS_JOY_ADC_X, JOY_X_PIN)
  288. OPTITEM(HAS_JOY_ADC_Y, JOY_Y_PIN)
  289. OPTITEM(HAS_JOY_ADC_Z, JOY_Z_PIN)
  290. OPTITEM(POWER_MONITOR_CURRENT, POWER_MONITOR_CURRENT_PIN)
  291. OPTITEM(POWER_MONITOR_VOLTAGE, POWER_MONITOR_VOLTAGE_PIN)
  292. };
  293. static STM32ADC adc(ADC1);
  294. // configure the ADC
  295. adc.calibrate();
  296. adc.setSampleRate((F_CPU > 72000000) ? ADC_SMPR_71_5 : ADC_SMPR_41_5); // 71.5 or 41.5 ADC cycles
  297. adc.setPins((uint8_t *)adc_pins, ADC_COUNT);
  298. adc.setDMA(adc_results, uint16_t(ADC_COUNT), uint32_t(DMA_MINC_MODE | DMA_CIRC_MODE), nullptr);
  299. adc.setScanMode();
  300. adc.setContinuous();
  301. adc.startConversion();
  302. }
  303. void MarlinHAL::adc_start(const pin_t pin) {
  304. #define __TCASE(N,I) case N: pin_index = I; break;
  305. #define _TCASE(C,N,I) TERN_(C, __TCASE(N, I))
  306. ADCIndex pin_index;
  307. switch (pin) {
  308. default: return;
  309. _TCASE(HAS_TEMP_ADC_0, TEMP_0_PIN, TEMP_0)
  310. _TCASE(HAS_TEMP_ADC_1, TEMP_1_PIN, TEMP_1)
  311. _TCASE(HAS_TEMP_ADC_2, TEMP_2_PIN, TEMP_2)
  312. _TCASE(HAS_TEMP_ADC_3, TEMP_3_PIN, TEMP_3)
  313. _TCASE(HAS_TEMP_ADC_4, TEMP_4_PIN, TEMP_4)
  314. _TCASE(HAS_TEMP_ADC_5, TEMP_5_PIN, TEMP_5)
  315. _TCASE(HAS_TEMP_ADC_6, TEMP_6_PIN, TEMP_6)
  316. _TCASE(HAS_TEMP_ADC_7, TEMP_7_PIN, TEMP_7)
  317. _TCASE(HAS_HEATED_BED, TEMP_BED_PIN, TEMP_BED)
  318. _TCASE(HAS_TEMP_CHAMBER, TEMP_CHAMBER_PIN, TEMP_CHAMBER)
  319. _TCASE(HAS_TEMP_ADC_PROBE, TEMP_PROBE_PIN, TEMP_PROBE)
  320. _TCASE(HAS_TEMP_COOLER, TEMP_COOLER_PIN, TEMP_COOLER)
  321. _TCASE(HAS_TEMP_BOARD, TEMP_BOARD_PIN, TEMP_BOARD)
  322. _TCASE(HAS_JOY_ADC_X, JOY_X_PIN, JOY_X)
  323. _TCASE(HAS_JOY_ADC_Y, JOY_Y_PIN, JOY_Y)
  324. _TCASE(HAS_JOY_ADC_Z, JOY_Z_PIN, JOY_Z)
  325. _TCASE(FILAMENT_WIDTH_SENSOR, FILWIDTH_PIN, FILWIDTH)
  326. _TCASE(HAS_ADC_BUTTONS, ADC_KEYPAD_PIN, ADC_KEY)
  327. _TCASE(POWER_MONITOR_CURRENT, POWER_MONITOR_CURRENT_PIN, POWERMON_CURRENT)
  328. _TCASE(POWER_MONITOR_VOLTAGE, POWER_MONITOR_VOLTAGE_PIN, POWERMON_VOLTS)
  329. }
  330. adc_result = (adc_results[(int)pin_index] & 0xFFF) >> (12 - HAL_ADC_RESOLUTION); // shift out unused bits
  331. }
  332. #endif // __STM32F1__