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.

MarlinSerial.cpp 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. #include "../../inc/MarlinConfigPre.h"
  23. #include "MarlinSerial.h"
  24. #include <libmaple/usart.h>
  25. // Copied from ~/.platformio/packages/framework-arduinoststm32-maple/STM32F1/system/libmaple/usart_private.h
  26. // Changed to handle Emergency Parser
  27. static inline __always_inline void my_usart_irq(ring_buffer *rb, ring_buffer *wb, usart_reg_map *regs, MarlinSerial &serial) {
  28. /* Handle RXNEIE and TXEIE interrupts.
  29. * RXNE signifies availability of a byte in DR.
  30. *
  31. * See table 198 (sec 27.4, p809) in STM document RM0008 rev 15.
  32. * We enable RXNEIE.
  33. */
  34. if ((regs->CR1 & USART_CR1_RXNEIE) && (regs->SR & USART_SR_RXNE)) {
  35. uint8_t c = (uint8)regs->DR;
  36. #ifdef USART_SAFE_INSERT
  37. // If the buffer is full and the user defines USART_SAFE_INSERT,
  38. // ignore new bytes.
  39. rb_safe_insert(rb, c);
  40. #else
  41. // By default, push bytes around in the ring buffer.
  42. rb_push_insert(rb, c);
  43. #endif
  44. #if ENABLED(EMERGENCY_PARSER)
  45. emergency_parser.update(serial.emergency_state, c);
  46. #endif
  47. }
  48. // TXE signifies readiness to send a byte to DR.
  49. if ((regs->CR1 & USART_CR1_TXEIE) && (regs->SR & USART_SR_TXE)) {
  50. if (!rb_is_empty(wb))
  51. regs->DR=rb_remove(wb);
  52. else
  53. regs->CR1 &= ~((uint32)USART_CR1_TXEIE); // disable TXEIE
  54. }
  55. }
  56. #define DEFINE_HWSERIAL_MARLIN(name, n) \
  57. MarlinSerial name(USART##n, \
  58. BOARD_USART##n##_TX_PIN, \
  59. BOARD_USART##n##_RX_PIN); \
  60. extern "C" void __irq_usart##n(void) { \
  61. my_usart_irq(USART##n->rb, USART##n->wb, USART##n##_BASE, MSerial##n); \
  62. }
  63. #define DEFINE_HWSERIAL_UART_MARLIN(name, n) \
  64. MarlinSerial name(UART##n, \
  65. BOARD_USART##n##_TX_PIN, \
  66. BOARD_USART##n##_RX_PIN); \
  67. extern "C" void __irq_usart##n(void) { \
  68. my_usart_irq(USART##n->rb, USART##n->wb, USART##n##_BASE, MSerial##n); \
  69. }
  70. #if SERIAL_PORT == 1 || SERIAL_PORT_2 == 1 || DGUS_SERIAL_PORT == 1
  71. DEFINE_HWSERIAL_MARLIN(MSerial1, 1);
  72. #endif
  73. #if SERIAL_PORT == 2 || SERIAL_PORT_2 == 2 || DGUS_SERIAL_PORT == 2
  74. DEFINE_HWSERIAL_MARLIN(MSerial2, 2);
  75. #endif
  76. #if SERIAL_PORT == 3 || SERIAL_PORT_2 == 3 || DGUS_SERIAL_PORT == 3
  77. DEFINE_HWSERIAL_MARLIN(MSerial3, 3);
  78. #endif
  79. #if SERIAL_PORT == 4 || SERIAL_PORT_2 == 4 || DGUS_SERIAL_PORT == 4
  80. DEFINE_HWSERIAL_UART_MARLIN(MSerial4, 4);
  81. #endif
  82. #if SERIAL_PORT == 5 || SERIAL_PORT_2 == 5 || DGUS_SERIAL_PORT == 5
  83. DEFINE_HWSERIAL_UART_MARLIN(MSerial5, 5);
  84. #endif