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 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. #ifdef TARGET_LPC1768
  23. #include "../../inc/MarlinConfig.h"
  24. #include "../shared/Delay.h"
  25. #include "../../../gcode/parser.h"
  26. DefaultSerial1 USBSerial(false, UsbSerial);
  27. uint32_t MarlinHAL::adc_result = 0;
  28. // U8glib required functions
  29. extern "C" {
  30. void u8g_xMicroDelay(uint16_t val) { DELAY_US(val); }
  31. void u8g_MicroDelay() { u8g_xMicroDelay(1); }
  32. void u8g_10MicroDelay() { u8g_xMicroDelay(10); }
  33. void u8g_Delay(uint16_t val) { delay(val); }
  34. }
  35. // return free heap space
  36. int freeMemory() {
  37. char stack_end;
  38. void *heap_start = malloc(sizeof(uint32_t));
  39. if (heap_start == 0) return 0;
  40. uint32_t result = (uint32_t)&stack_end - (uint32_t)heap_start;
  41. free(heap_start);
  42. return result;
  43. }
  44. void MarlinHAL::reboot() { NVIC_SystemReset(); }
  45. uint8_t MarlinHAL::get_reset_source() {
  46. #if ENABLED(USE_WATCHDOG)
  47. if (watchdog_timed_out()) return RST_WATCHDOG;
  48. #endif
  49. return RST_POWER_ON;
  50. }
  51. void MarlinHAL::clear_reset_source() { watchdog_clear_timeout_flag(); }
  52. void flashFirmware(const int16_t) {
  53. delay(500); // Give OS time to disconnect
  54. USB_Connect(false); // USB clear connection
  55. delay(1000); // Give OS time to notice
  56. hal.reboot();
  57. }
  58. #if ENABLED(USE_WATCHDOG)
  59. #include <lpc17xx_wdt.h>
  60. #define WDT_TIMEOUT_US TERN(WATCHDOG_DURATION_8S, 8000000, 4000000) // 4 or 8 second timeout
  61. void MarlinHAL::watchdog_init() {
  62. #if ENABLED(WATCHDOG_RESET_MANUAL)
  63. // We enable the watchdog timer, but only for the interrupt.
  64. // Configure WDT to only trigger an interrupt
  65. // Disable WDT interrupt (just in case, to avoid triggering it!)
  66. NVIC_DisableIRQ(WDT_IRQn);
  67. // We NEED memory barriers to ensure Interrupts are actually disabled!
  68. // ( https://dzone.com/articles/nvic-disabling-interrupts-on-arm-cortex-m-and-the )
  69. __DSB();
  70. __ISB();
  71. // Configure WDT to only trigger an interrupt
  72. // Initialize WDT with the given parameters
  73. WDT_Init(WDT_CLKSRC_IRC, WDT_MODE_INT_ONLY);
  74. // Configure and enable WDT interrupt.
  75. NVIC_ClearPendingIRQ(WDT_IRQn);
  76. NVIC_SetPriority(WDT_IRQn, 0); // Use highest priority, so we detect all kinds of lockups
  77. NVIC_EnableIRQ(WDT_IRQn);
  78. #else
  79. WDT_Init(WDT_CLKSRC_IRC, WDT_MODE_RESET);
  80. #endif
  81. WDT_Start(WDT_TIMEOUT_US);
  82. }
  83. void MarlinHAL::watchdog_refresh() {
  84. WDT_Feed();
  85. #if DISABLED(PINS_DEBUGGING) && PIN_EXISTS(LED)
  86. TOGGLE(LED_PIN); // heartbeat indicator
  87. #endif
  88. }
  89. // Timeout state
  90. bool MarlinHAL::watchdog_timed_out() { return TEST(WDT_ReadTimeOutFlag(), 0); }
  91. void MarlinHAL::watchdog_clear_timeout_flag() { WDT_ClrTimeOutFlag(); }
  92. #endif // USE_WATCHDOG
  93. // For M42/M43, scan command line for pin code
  94. // return index into pin map array if found and the pin is valid.
  95. // return dval if not found or not a valid pin.
  96. int16_t PARSED_PIN_INDEX(const char code, const int16_t dval) {
  97. const uint16_t val = (uint16_t)parser.intval(code, -1), port = val / 100, pin = val % 100;
  98. const int16_t ind = (port < ((NUM_DIGITAL_PINS) >> 5) && pin < 32) ? ((port << 5) | pin) : -2;
  99. return ind > -1 ? ind : dval;
  100. }
  101. #endif // TARGET_LPC1768