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.

MinSerial.cpp 4.4KB

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