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.

294 line
9.4KB

  1. /* Teensyduino Core Library
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2019 PJRC.COM, LLC.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * 1. The above copyright notice and this permission notice shall be
  14. * included in all copies or substantial portions of the Software.
  15. *
  16. * 2. If the Software is incorporated into a build system that allows
  17. * selection among a list of target devices, then similar target
  18. * devices manufactured by PJRC.COM must be included in the list of
  19. * target devices and selectable in the same manner.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  25. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  26. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  27. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  28. * SOFTWARE.
  29. */
  30. #ifndef HardwareSerial_h
  31. #define HardwareSerial_h
  32. #include "imxrt.h"
  33. // Uncomment to enable 9 bit formats. These are default disabled to save memory.
  34. //#define SERIAL_9BIT_SUPPORT
  35. //
  36. // On Windows & Linux, this file is in Arduino's hardware/teensy/avr/cores/teensy3
  37. // folder. The Windows installer puts Arduino in C:\Program Files (x86)\Arduino
  38. // On Macintosh, you must control-click Arduino and select "Show Package Contents", then
  39. // look in Contents/Java/hardware/teensy/avr/cores/teensy3 to find this file.
  40. //
  41. // Teensy 4.x boards support 9 bit mode on all their serial ports
  42. #define SERIAL_7E1 0x02
  43. #define SERIAL_7O1 0x03
  44. #define SERIAL_8N1 0x00
  45. #define SERIAL_8E1 0x06
  46. #define SERIAL_8O1 0x07
  47. #define SERIAL_7E1_RXINV 0x12
  48. #define SERIAL_7O1_RXINV 0x13
  49. #define SERIAL_8N1_RXINV 0x10
  50. #define SERIAL_8E1_RXINV 0x16
  51. #define SERIAL_8O1_RXINV 0x17
  52. #define SERIAL_7E1_TXINV 0x22
  53. #define SERIAL_7O1_TXINV 0x23
  54. #define SERIAL_8N1_TXINV 0x20
  55. #define SERIAL_8E1_TXINV 0x26
  56. #define SERIAL_8O1_TXINV 0x27
  57. #define SERIAL_7E1_RXINV_TXINV 0x32
  58. #define SERIAL_7O1_RXINV_TXINV 0x33
  59. #define SERIAL_8N1_RXINV_TXINV 0x30
  60. #define SERIAL_8E1_RXINV_TXINV 0x36
  61. #define SERIAL_8O1_RXINV_TXINV 0x37
  62. #ifdef SERIAL_9BIT_SUPPORT
  63. #define SERIAL_9N1 0x84
  64. #define SERIAL_9E1 0x8E
  65. #define SERIAL_9O1 0x8F
  66. #define SERIAL_9N1_RXINV 0x94
  67. #define SERIAL_9E1_RXINV 0x9E
  68. #define SERIAL_9O1_RXINV 0x9F
  69. #define SERIAL_9N1_TXINV 0xA4
  70. #define SERIAL_9E1_TXINV 0xAE
  71. #define SERIAL_9O1_TXINV 0xAF
  72. #define SERIAL_9N1_RXINV_TXINV 0xB4
  73. #define SERIAL_9E1_RXINV_TXINV 0xBE
  74. #define SERIAL_9O1_RXINV_TXINV 0xBF
  75. #endif
  76. // We have 1/2 bit stop setting
  77. #define SERIAL_2STOP_BITS 0x100
  78. #define SERIAL_8E2 (SERIAL_8E1 | SERIAL_2STOP_BITS)
  79. #define SERIAL_8O2 (SERIAL_8O1 | SERIAL_2STOP_BITS)
  80. #define SERIAL_8E2_RXINV (SERIAL_8E1_RXINV | SERIAL_2STOP_BITS)
  81. #define SERIAL_8O2_RXINV (SERIAL_8O1_RXINV | SERIAL_2STOP_BITS)
  82. #define SERIAL_8E2_TXINV (SERIAL_8E1_TXINV | SERIAL_2STOP_BITS)
  83. #define SERIAL_8O2_TXINV (SERIAL_8O1_TXINV | SERIAL_2STOP_BITS)
  84. #define SERIAL_8E2_RXINV_TXINV (SERIAL_8E1_RXINV_TXINV | SERIAL_2STOP_BITS)
  85. #define SERIAL_8O2_RXINV_TXINV (SERIAL_8O1_RXINV_TXINV | SERIAL_2STOP_BITS)
  86. #define SERIAL_8N2 (SERIAL_8N1 | SERIAL_2STOP_BITS)
  87. #define SERIAL_8N2_RXINV (SERIAL_8N1_RXINV | SERIAL_2STOP_BITS)
  88. #define SERIAL_8N2_TXINV (SERIAL_8N1_TXINV | SERIAL_2STOP_BITS)
  89. #define SERIAL_8N2_RXINV_TXINV (SERIAL_8N1_RXINV_TXINV | SERIAL_2STOP_BITS)
  90. // bit0: parity, 0=even, 1=odd
  91. // bit1: parity, 0=disable, 1=enable
  92. // bit2: mode, 1=9bit, 0=8bit
  93. // bit3: mode10: 1=10bit, 0=8bit
  94. // bit4: rxinv, 0=normal, 1=inverted
  95. // bit5: txinv, 0=normal, 1=inverted
  96. // bit6: unused
  97. // bit7: actual data goes into 9th bit
  98. #ifdef __cplusplus
  99. #include "Stream.h"
  100. #include "core_pins.h"
  101. #ifdef SERIAL_9BIT_SUPPORT
  102. #define BUFTYPE uint16_t
  103. #else
  104. #define BUFTYPE uint8_t
  105. #endif
  106. extern "C" {
  107. extern void IRQHandler_Serial1();
  108. extern void IRQHandler_Serial2();
  109. extern void IRQHandler_Serial3();
  110. extern void IRQHandler_Serial4();
  111. extern void IRQHandler_Serial5();
  112. extern void IRQHandler_Serial6();
  113. extern void IRQHandler_Serial7();
  114. #if defined(__IMXRT1052__)
  115. extern void IRQHandler_Serial8();
  116. #endif
  117. }
  118. typedef void(*SerialEventCheckingFunctionPointer)();
  119. class HardwareSerial : public Stream
  120. {
  121. public:
  122. typedef struct {
  123. uint8_t serial_index; // which object are we? 0 based
  124. IRQ_NUMBER_t irq;
  125. void (*irq_handler)(void);
  126. void (*serial_event_handler_check)(void);
  127. volatile uint32_t &ccm_register;
  128. const uint32_t ccm_value;
  129. const uint8_t rx_pin;
  130. const uint8_t tx_pin;
  131. const uint8_t cts_pin;
  132. volatile uint32_t &rx_select_input_register;
  133. const uint8_t rx_mux_val;
  134. const uint8_t tx_mux_val;
  135. const uint8_t cts_mux_val;
  136. const uint8_t rx_select_val;
  137. const uint16_t irq_priority;
  138. const uint16_t rts_low_watermark;
  139. const uint16_t rts_high_watermark;
  140. } hardware_t;
  141. public:
  142. constexpr HardwareSerial(IMXRT_LPUART_t *myport, const hardware_t *myhardware,
  143. volatile BUFTYPE *_tx_buffer, size_t _tx_buffer_size,
  144. volatile BUFTYPE *_rx_buffer, size_t _rx_buffer_size) :
  145. port(myport), hardware(myhardware),
  146. tx_buffer_(_tx_buffer), rx_buffer_(_rx_buffer), tx_buffer_size_(_tx_buffer_size), rx_buffer_size_(_rx_buffer_size),
  147. tx_buffer_total_size_(_tx_buffer_size), rx_buffer_total_size_(_rx_buffer_size) {
  148. }
  149. void begin(uint32_t baud, uint16_t format=0);
  150. void end(void);
  151. virtual int available(void);
  152. virtual int peek(void);
  153. virtual void flush(void);
  154. virtual size_t write(uint8_t c);
  155. virtual int read(void);
  156. void transmitterEnable(uint8_t pin);
  157. void setRX(uint8_t pin);
  158. void setTX(uint8_t pin, bool opendrain=false);
  159. bool attachRts(uint8_t pin);
  160. bool attachCts(uint8_t pin);
  161. void clear(void);
  162. int availableForWrite(void);
  163. void addStorageForRead(void *buffer, size_t length);
  164. void addStorageForWrite(void *buffer, size_t length);
  165. size_t write9bit(uint32_t c);
  166. // Event Handler functions and data
  167. void enableSerialEvents();
  168. void disableSerialEvents();
  169. static void processSerialEvents();
  170. using Print::write;
  171. // Only overwrite some of the virtualWrite functions if we are going to optimize them over Print version
  172. /*
  173. virtual void begin(uint32_t baud) { serial_begin(BAUD2DIV(baud)); }
  174. virtual void begin(uint32_t baud, uint32_t format) {
  175. serial_begin(BAUD2DIV(baud));
  176. serial_format(format); }
  177. */
  178. operator bool() { return true; }
  179. private:
  180. IMXRT_LPUART_t * const port;
  181. const hardware_t * const hardware;
  182. volatile BUFTYPE *tx_buffer_;
  183. volatile BUFTYPE *rx_buffer_;
  184. volatile BUFTYPE *rx_buffer_storage_ = nullptr;
  185. volatile BUFTYPE *tx_buffer_storage_ = nullptr;
  186. size_t tx_buffer_size_;
  187. size_t rx_buffer_size_;
  188. size_t tx_buffer_total_size_;
  189. size_t rx_buffer_total_size_;
  190. size_t rts_low_watermark_ = 0;
  191. size_t rts_high_watermark_ = 0;
  192. volatile uint8_t transmitting_ = 0;
  193. volatile uint16_t tx_buffer_head_ = 0;
  194. volatile uint16_t tx_buffer_tail_ = 0;
  195. volatile uint16_t rx_buffer_head_ = 0;
  196. volatile uint16_t rx_buffer_tail_ = 0;
  197. volatile uint32_t *transmit_pin_baseReg_ = 0;
  198. uint32_t transmit_pin_bitmask_ = 0;
  199. volatile uint32_t *rts_pin_baseReg_ = 0;
  200. uint32_t rts_pin_bitmask_ = 0;
  201. inline void rts_assert();
  202. inline void rts_deassert();
  203. void IRQHandler();
  204. friend void IRQHandler_Serial1();
  205. friend void IRQHandler_Serial2();
  206. friend void IRQHandler_Serial3();
  207. friend void IRQHandler_Serial4();
  208. friend void IRQHandler_Serial5();
  209. friend void IRQHandler_Serial6();
  210. friend void IRQHandler_Serial7();
  211. #if defined(__IMXRT1052__)
  212. friend void IRQHandler_Serial8();
  213. static SerialEventCheckingFunctionPointer serial_event_handler_checks[8];
  214. #else
  215. static SerialEventCheckingFunctionPointer serial_event_handler_checks[7];
  216. #endif
  217. static uint8_t serial_event_handlers_active;
  218. };
  219. extern HardwareSerial Serial1;
  220. extern HardwareSerial Serial2;
  221. extern HardwareSerial Serial3;
  222. extern HardwareSerial Serial4;
  223. extern HardwareSerial Serial5;
  224. extern HardwareSerial Serial6;
  225. extern HardwareSerial Serial7;
  226. extern void serialEvent1(void);
  227. extern void serialEvent2(void);
  228. extern void serialEvent3(void);
  229. extern void serialEvent4(void);
  230. extern void serialEvent5(void);
  231. extern void serialEvent6(void);
  232. extern void serialEvent7(void);
  233. #if defined(__IMXRT1052__)
  234. extern HardwareSerial Serial8;
  235. extern void serialEvent8(void);
  236. #endif
  237. #endif // __cplusplus
  238. // TODO: replace with proper divisor+oversample calculation
  239. #define BAUD2DIV(baud) (24000000/16/(baud))
  240. /*
  241. #if defined(KINETISK)
  242. #define BAUD2DIV(baud) (((F_CPU * 2) + ((baud) >> 1)) / (baud))
  243. #define BAUD2DIV2(baud) (((F_CPU * 2) + ((baud) >> 1)) / (baud))
  244. #define BAUD2DIV3(baud) (((F_BUS * 2) + ((baud) >> 1)) / (baud))
  245. #elif defined(KINETISL)
  246. #if F_CPU <= 2000000
  247. #define BAUD2DIV(baud) (((F_PLL / 16 ) + ((baud) >> 1)) / (baud))
  248. #elif F_CPU <= 16000000
  249. #define BAUD2DIV(baud) (((F_PLL / (F_PLL / 1000000)) + ((baud) >> 1)) / (baud))
  250. #else
  251. #define BAUD2DIV(baud) (((F_PLL / 2 / 16) + ((baud) >> 1)) / (baud))
  252. #endif
  253. #define BAUD2DIV2(baud) (((F_BUS / 16) + ((baud) >> 1)) / (baud))
  254. #define BAUD2DIV3(baud) (((F_BUS / 16) + ((baud) >> 1)) / (baud))
  255. #endif
  256. */
  257. #endif