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_MinSerial.cpp 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2021 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  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. #ifdef __STM32F1__
  24. #include "../../inc/MarlinConfigPre.h"
  25. #if ENABLED(POSTMORTEM_DEBUGGING)
  26. #include "../shared/HAL_MinSerial.h"
  27. #include "watchdog.h"
  28. #include <libmaple/usart.h>
  29. #include <libmaple/rcc.h>
  30. #include <libmaple/nvic.h>
  31. /* Instruction Synchronization Barrier */
  32. #define isb() __asm__ __volatile__ ("isb" : : : "memory")
  33. /* Data Synchronization Barrier */
  34. #define dsb() __asm__ __volatile__ ("dsb" : : : "memory")
  35. static void TXBegin() {
  36. #if !WITHIN(SERIAL_PORT, 1, 6)
  37. #warning "Using POSTMORTEM_DEBUGGING requires a physical U(S)ART hardware in case of severe error."
  38. #warning "Disabling the severe error reporting feature currently because the used serial port is not a HW port."
  39. #else
  40. // We use MYSERIAL1 here, so we need to figure out how to get the linked register
  41. struct usart_dev* dev = MYSERIAL1.c_dev();
  42. // Or use this if removing libmaple
  43. // int irq = dev->irq_num;
  44. // int nvicUART[] = { NVIC_USART1 /* = 37 */, NVIC_USART2 /* = 38 */, NVIC_USART3 /* = 39 */, NVIC_UART4 /* = 52 */, NVIC_UART5 /* = 53 */ };
  45. // Disabling irq means setting the bit in the NVIC ICER register located at
  46. // Disable UART interrupt in NVIC
  47. nvic_irq_disable(dev->irq_num);
  48. // Use this if removing libmaple
  49. //NVIC_BASE->ICER[1] |= _BV(irq - 32);
  50. // We NEED memory barriers to ensure Interrupts are actually disabled!
  51. // ( https://dzone.com/articles/nvic-disabling-interrupts-on-arm-cortex-m-and-the )
  52. dsb();
  53. isb();
  54. rcc_clk_disable(dev->clk_id);
  55. rcc_clk_enable(dev->clk_id);
  56. usart_reg_map *regs = dev->regs;
  57. regs->CR1 = 0; // Reset the USART
  58. regs->CR2 = 0; // 1 stop bit
  59. // If we don't touch the BRR (baudrate register), we don't need to recompute. Else we would need to call
  60. usart_set_baud_rate(dev, 0, BAUDRATE);
  61. regs->CR1 = (USART_CR1_TE | USART_CR1_UE); // 8 bits, no parity, 1 stop bit
  62. #endif
  63. }
  64. // A SW memory barrier, to ensure GCC does not overoptimize loops
  65. #define sw_barrier() __asm__ volatile("": : :"memory");
  66. static void TX(char c) {
  67. #if WITHIN(SERIAL_PORT, 1, 6)
  68. struct usart_dev* dev = MYSERIAL1.c_dev();
  69. while (!(dev->regs->SR & USART_SR_TXE)) {
  70. TERN_(USE_WATCHDOG, HAL_watchdog_refresh());
  71. sw_barrier();
  72. }
  73. dev->regs->DR = c;
  74. #endif
  75. }
  76. void install_min_serial() {
  77. HAL_min_serial_init = &TXBegin;
  78. HAL_min_serial_out = &TX;
  79. }
  80. #if DISABLED(DYNAMIC_VECTORTABLE) && DISABLED(STM32F0xx) // Cortex M0 can't branch to a symbol that's too far, so we have a specific hack for them
  81. extern "C" {
  82. __attribute__((naked)) void JumpHandler_ASM() {
  83. __asm__ __volatile__ (
  84. "b CommonHandler_ASM\n"
  85. );
  86. }
  87. void __attribute__((naked, alias("JumpHandler_ASM"), nothrow)) __exc_hardfault();
  88. void __attribute__((naked, alias("JumpHandler_ASM"), nothrow)) __exc_busfault();
  89. void __attribute__((naked, alias("JumpHandler_ASM"), nothrow)) __exc_usagefault();
  90. void __attribute__((naked, alias("JumpHandler_ASM"), nothrow)) __exc_memmanage();
  91. void __attribute__((naked, alias("JumpHandler_ASM"), nothrow)) __exc_nmi();
  92. void __attribute__((naked, alias("JumpHandler_ASM"), nothrow)) __stm32reservedexception7();
  93. void __attribute__((naked, alias("JumpHandler_ASM"), nothrow)) __stm32reservedexception8();
  94. void __attribute__((naked, alias("JumpHandler_ASM"), nothrow)) __stm32reservedexception9();
  95. void __attribute__((naked, alias("JumpHandler_ASM"), nothrow)) __stm32reservedexception10();
  96. void __attribute__((naked, alias("JumpHandler_ASM"), nothrow)) __stm32reservedexception13();
  97. }
  98. #endif
  99. #endif // POSTMORTEM_DEBUGGING
  100. #endif // __STM32F1__