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.

wifiSerial_STM32F1.cpp 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. *
  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 __STM32F1__
  23. #include "../../../inc/MarlinConfigPre.h"
  24. #if BOTH(HAS_TFT_LVGL_UI, MKS_WIFI_MODULE)
  25. #include "tft_lvgl_configuration.h"
  26. #include "draw_ui.h"
  27. #include "wifiSerial.h"
  28. #include <libmaple/libmaple.h>
  29. #include <libmaple/gpio.h>
  30. #include <libmaple/timer.h>
  31. #include <libmaple/usart.h>
  32. #include <libmaple/ring_buffer.h>
  33. #include "../../../MarlinCore.h"
  34. DEFINE_WFSERIAL(WifiSerial1, 1);
  35. WifiSerial::WifiSerial(usart_dev *usart_device, uint8 tx_pin, uint8 rx_pin) {
  36. this->usart_device = usart_device;
  37. this->tx_pin = tx_pin;
  38. this->rx_pin = rx_pin;
  39. }
  40. /**
  41. * Set up / tear down
  42. */
  43. #if STM32_MCU_SERIES == STM32_SERIES_F1
  44. /* F1 MCUs have no GPIO_AFR[HL], so turn off PWM if there's a conflict
  45. * on this GPIO bit. */
  46. static void disable_timer_if_necessary(timer_dev *dev, uint8 ch) {
  47. if (dev) timer_set_mode(dev, ch, TIMER_DISABLED);
  48. }
  49. static void usart_enable_no_irq(usart_dev *usart_device, bool with_irq) {
  50. if (with_irq) usart_enable(usart_device);
  51. else {
  52. usart_reg_map *regs = usart_device->regs;
  53. regs->CR1 |= (USART_CR1_TE | USART_CR1_RE); // Preserve word length, etc. Use 'or' to preserve USART_CR1_M_8N1
  54. regs->CR1 |= USART_CR1_UE;
  55. }
  56. }
  57. #elif STM32_MCU_SERIES == STM32_SERIES_F2 || STM32_MCU_SERIES == STM32_SERIES_F4
  58. #define disable_timer_if_necessary(dev, ch) NOOP
  59. static void usart_enable_no_irq(usart_dev *usart_device, bool with_irq) {
  60. if (with_irq)
  61. usart_enable(usart_device);
  62. else {
  63. usart_reg_map *regs = usart_device->regs;
  64. regs->CR1 |= (USART_CR1_TE | USART_CR1_RE); // Preserve word length, etc. Use 'or' to preserve USART_CR1_M_8N1
  65. regs->CR1 |= USART_CR1_UE;
  66. }
  67. }
  68. #else
  69. #warning "Unsupported STM32 series; timer conflicts are possible"
  70. #define usart_enable_no_irq(X, Y) usart_enable(X)
  71. #endif
  72. void WifiSerial::begin(uint32 baud) { begin(baud, SERIAL_8N1); }
  73. /**
  74. * Roger Clark.
  75. * Note. The config parameter is not currently used. This is a work in progress.
  76. * Code needs to be written to set the config of the hardware serial control register in question.
  77. */
  78. void WifiSerial::begin(uint32 baud, uint8_t config) {
  79. //ASSERT(baud <= this->usart_device->max_baud); // Roger Clark. Assert doesn't do anything useful, we may as well save the space in flash and ram etc
  80. if (baud > this->usart_device->max_baud) return;
  81. const stm32_pin_info *txi = &PIN_MAP[this->tx_pin],
  82. *rxi = &PIN_MAP[this->rx_pin];
  83. disable_timer_if_necessary(txi->timer_device, txi->timer_channel);
  84. usart_init(this->usart_device);
  85. // Reinitialize the receive buffer, mks_esp8266 fixed data frame length is 1k bytes
  86. rb_init(this->usart_device->rb, WIFI_RX_BUF_SIZE, wifiRxBuf);
  87. usart_config_gpios_async(this->usart_device,
  88. rxi->gpio_device, rxi->gpio_bit,
  89. txi->gpio_device, txi->gpio_bit,
  90. config);
  91. usart_set_baud_rate(this->usart_device, USART_USE_PCLK, baud);
  92. usart_enable_no_irq(this->usart_device, baud == WIFI_BAUDRATE);
  93. }
  94. void WifiSerial::end() {
  95. usart_disable(this->usart_device);
  96. }
  97. int WifiSerial::available() {
  98. return usart_data_available(this->usart_device);
  99. }
  100. //
  101. // I/O
  102. //
  103. int WifiSerial::read() {
  104. if (usart_data_available(usart_device) <= 0) return -1;
  105. return usart_getc(usart_device);
  106. }
  107. int WifiSerial::write(unsigned char ch) {
  108. usart_putc(this->usart_device, ch);
  109. return 1;
  110. }
  111. int WifiSerial::wifi_rb_is_full() {
  112. return rb_is_full(this->usart_device->rb);
  113. }
  114. #endif // HAS_TFT_LVGL_UI && MKS_WIFI_MODULE
  115. #endif // __STM32F1__