aboutsummaryrefslogtreecommitdiff
path: root/Marlin/src/lcd/extui/lib/mks_ui/wifiSerial.h
blob: cd11d6b5d9bcca526c855476a5c5a4a15d241db4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/**
 * Marlin 3D Printer Firmware
 * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
 *
 * Based on Sprinter and grbl.
 * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 */
#pragma once

#include "tft_lvgl_configuration.h"

#if ENABLED(MKS_WIFI_MODULE)

#ifdef SERIAL_PORT_2
  #error "SERIAL_PORT_2 must be disabled with TFT_LVGL_UI* and MKS_WIFI_MODULE."
#endif

#define WIFI_BAUDRATE          115200
#define WIFI_UPLOAD_BAUDRATE  1958400
#define USART_SAFE_INSERT

#define WIFI_RX_BUF_SIZE  (1024)
#define WIFI_TX_BUF_SIZE  (64)

#ifdef __STM32F1__

  #include <libmaple/libmaple_types.h>
  #include <libmaple/usart.h>
  #include <libmaple/libmaple.h>
  #include <libmaple/gpio.h>
  #include <libmaple/timer.h>
  #include <libmaple/ring_buffer.h>

  #define DEFINE_WFSERIAL(name, n)\
    WifiSerial name(USART##n, \
    BOARD_USART##n##_TX_PIN,  \
    BOARD_USART##n##_RX_PIN)

  class WifiSerial {
    public:
      uint8 wifiRxBuf[WIFI_RX_BUF_SIZE];

    public:
      WifiSerial(struct usart_dev *usart_device, uint8 tx_pin, uint8 rx_pin);

      /* Set up/tear down */
      void begin(uint32 baud);
      void begin(uint32 baud,uint8_t config);
      void end();
      int available(void);
      int read(void);
      int write(uint8_t);
      inline void wifi_usart_irq(usart_reg_map *regs) {
        /* Handling RXNEIE and TXEIE interrupts.
        * RXNE signifies availability of a byte in DR.
        *
        * See table 198 (sec 27.4, p809) in STM document RM0008 rev 15.
        * We enable RXNEIE.
        */
        if ((regs->CR1 & USART_CR1_RXNEIE) && (regs->SR & USART_SR_RXNE)) {
          #ifdef USART_SAFE_INSERT
            /* If the buffer is full and the user defines USART_SAFE_INSERT,
            * ignore new bytes. */
            rb_safe_insert(this->usart_device->rb, (uint8)regs->DR);
          #else
            /* By default, push bytes around in the ring buffer. */
            rb_push_insert(this->usart_device->rb, (uint8)regs->DR);
          #endif
        }
        /* TXE signifies readiness to send a byte to DR. */
        if ((regs->CR1 & USART_CR1_TXEIE) && (regs->SR & USART_SR_TXE)) {
            if (!rb_is_empty(this->usart_device->wb))
                regs->DR=rb_remove(this->usart_device->wb);
            else
                regs->CR1 &= ~((uint32)USART_CR1_TXEIE); // disable TXEIE
        }
      }
      int wifi_rb_is_full(void);
      struct usart_dev *usart_device;
      private:
      uint8 tx_pin;
      uint8 rx_pin;
  };
  extern WifiSerial WifiSerial1;
  #define WIFISERIAL  WifiSerial1
#else

  #include <inttypes.h>
  #include "Stream.h"
  #include "uart.h"

  class WifiSerial {
    protected:
      // Has any byte been written to the UART since begin()
      bool _written;
      serial_t _serial;
    public:
      uint8_t wifiRxBuf[WIFI_RX_BUF_SIZE];
      uint8_t wifiTxBuf[WIFI_TX_BUF_SIZE];
      WifiSerial(void *peripheral);

      /* Set up/tear down */
      void begin(uint32_t baud);
      void begin(uint32_t baud,uint8_t config);
      void end();
      int available(void);
      int read(void);
      int write(uint8_t);
      
      // Interrupt handlers
      static int _tx_complete_irq(serial_t *obj);
      static void _rx_complete_irq(serial_t *obj);

      void flush(void);
      bool isHalfDuplex(void) const;
      void enableHalfDuplexRx(void);

      private:
        void setRx(uint32_t _rx);
        void setTx(uint32_t _tx);
        void setRx(PinName _rx);
        void setTx(PinName _tx);
        void init(PinName _rx, PinName _tx);
        bool _rx_enabled;
        uint8_t _config;
        unsigned long _baud;
  };
  extern WifiSerial WifiSerial1;
  #define WIFISERIAL  WifiSerial1
#endif // __STM32F1__
#endif // MKS_WIFI_MODULE