Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

HardwareSerial.h 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /* Teensyduino Core Library
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2017 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. #ifdef __cplusplus
  34. #include "Stream.h"
  35. #include "core_pins.h"
  36. #ifdef SERIAL_9BIT_SUPPORT
  37. #define BUFTYPE uint16_t
  38. #else
  39. #define BUFTYPE uint8_t
  40. #endif
  41. extern "C" {
  42. extern void IRQHandler_Serial1();
  43. extern void IRQHandler_Serial2();
  44. extern void IRQHandler_Serial3();
  45. extern void IRQHandler_Serial4();
  46. extern void IRQHandler_Serial5();
  47. extern void IRQHandler_Serial6();
  48. extern void IRQHandler_Serial7();
  49. extern void IRQHandler_Serial8();
  50. }
  51. class HardwareSerial : public Stream
  52. {
  53. public:
  54. typedef struct {
  55. IRQ_NUMBER_t irq;
  56. void (*irq_handler)(void);
  57. volatile uint32_t &ccm_register;
  58. const uint32_t ccm_value;
  59. const uint8_t rx_pin;
  60. const uint8_t tx_pin;
  61. //volatile uint32_t &rx_mux_register;
  62. //volatile uint32_t &tx_mux_register;
  63. volatile uint32_t &rx_select_input_register;
  64. const uint8_t rx_mux_val;
  65. const uint8_t tx_mux_val;
  66. const uint8_t rx_select_val;
  67. } hardware_t;
  68. public:
  69. constexpr HardwareSerial(IMXRT_LPUART_t *myport, const hardware_t *myhardware,
  70. volatile BUFTYPE *_tx_buffer, size_t _tx_buffer_size,
  71. volatile BUFTYPE *_rx_buffer, size_t _rx_buffer_size) :
  72. port(myport), hardware(myhardware),
  73. tx_buffer_(_tx_buffer), rx_buffer_(_rx_buffer), tx_buffer_size_(_tx_buffer_size), rx_buffer_size_(_rx_buffer_size),
  74. tx_buffer_total_size_(_tx_buffer_size), rx_buffer_total_size_(_rx_buffer_size) {
  75. }
  76. void begin(uint32_t baud, uint8_t format=0);
  77. void end(void);
  78. virtual int available(void);
  79. virtual int peek(void);
  80. virtual void flush(void);
  81. virtual size_t write(uint8_t c);
  82. virtual int read(void);
  83. void transmitterEnable(uint8_t pin);
  84. void setRX(uint8_t pin);
  85. void setTX(uint8_t pin, bool opendrain=false);
  86. bool attachRts(uint8_t pin);
  87. bool attachCts(uint8_t pin);
  88. void clear(void);
  89. int availableForWrite(void);
  90. size_t write9bit(uint32_t c);
  91. using Print::write;
  92. // Only overwrite some of the virtualWrite functions if we are going to optimize them over Print version
  93. /*
  94. virtual void begin(uint32_t baud) { serial_begin(BAUD2DIV(baud)); }
  95. virtual void begin(uint32_t baud, uint32_t format) {
  96. serial_begin(BAUD2DIV(baud));
  97. serial_format(format); }
  98. */
  99. operator bool() { return true; }
  100. private:
  101. IMXRT_LPUART_t * const port;
  102. const hardware_t * const hardware;
  103. volatile BUFTYPE *tx_buffer_;
  104. volatile BUFTYPE *rx_buffer_;
  105. volatile BUFTYPE *rx_buffer_storage_ = nullptr;
  106. volatile BUFTYPE *tx_buffer_storage_ = nullptr;
  107. size_t tx_buffer_size_;
  108. size_t rx_buffer_size_;
  109. size_t tx_buffer_total_size_;
  110. size_t rx_buffer_total_size_;
  111. volatile uint8_t transmitting_ = 0;
  112. volatile uint16_t tx_buffer_head_ = 0;
  113. volatile uint16_t tx_buffer_tail_ = 0;
  114. volatile uint16_t rx_buffer_head_ = 0;
  115. volatile uint16_t rx_buffer_tail_ = 0;
  116. // Currently using digitalWWrite...
  117. int transmit_pin_=-1;
  118. int rts_pin_=-1;
  119. inline void transmit_assert() {digitalWrite(transmit_pin_, 1);}
  120. inline void transmit_deassert() {digitalWrite(transmit_pin_, 0);}
  121. inline void rts_assert() {digitalWrite(rts_pin_ , 0); }
  122. inline void rts_deassert() {digitalWrite(rts_pin_ , 1); }
  123. void IRQHandler();
  124. friend void IRQHandler_Serial1();
  125. friend void IRQHandler_Serial2();
  126. friend void IRQHandler_Serial3();
  127. friend void IRQHandler_Serial4();
  128. friend void IRQHandler_Serial5();
  129. friend void IRQHandler_Serial6();
  130. friend void IRQHandler_Serial7();
  131. friend void IRQHandler_Serial8();
  132. };
  133. extern HardwareSerial Serial1;
  134. extern HardwareSerial Serial2;
  135. extern HardwareSerial Serial3;
  136. extern HardwareSerial Serial4;
  137. extern HardwareSerial Serial5;
  138. extern HardwareSerial Serial6;
  139. extern HardwareSerial Serial7;
  140. extern HardwareSerial Serial8;
  141. //extern void serialEvent1(void);
  142. #endif // __cplusplus
  143. // Uncomment to enable 9 bit formats. These are default disabled to save memory.
  144. //#define SERIAL_9BIT_SUPPORT
  145. //
  146. // On Windows & Linux, this file is in Arduino's hardware/teensy/avr/cores/teensy3
  147. // folder. The Windows installer puts Arduino in C:\Program Files (x86)\Arduino
  148. // On Macintosh, you must control-click Arduino and select "Show Package Contents", then
  149. // look in Contents/Java/hardware/teensy/avr/cores/teensy3 to find this file.
  150. //
  151. // Teensy 3.x boards support 9 bit mode on all their serial ports
  152. // Teensy LC only supports 9 bit mode on Serial1. Serial2 & Serial3 can't use 9 bits.
  153. #define SERIAL_7E1 0x02
  154. #define SERIAL_7O1 0x03
  155. #define SERIAL_8N1 0x00
  156. #define SERIAL_8E1 0x06
  157. #define SERIAL_8O1 0x07
  158. #define SERIAL_7E1_RXINV 0x12
  159. #define SERIAL_7O1_RXINV 0x13
  160. #define SERIAL_8N1_RXINV 0x10
  161. #define SERIAL_8E1_RXINV 0x16
  162. #define SERIAL_8O1_RXINV 0x17
  163. #define SERIAL_7E1_TXINV 0x22
  164. #define SERIAL_7O1_TXINV 0x23
  165. #define SERIAL_8N1_TXINV 0x20
  166. #define SERIAL_8E1_TXINV 0x26
  167. #define SERIAL_8O1_TXINV 0x27
  168. #define SERIAL_7E1_RXINV_TXINV 0x32
  169. #define SERIAL_7O1_RXINV_TXINV 0x33
  170. #define SERIAL_8N1_RXINV_TXINV 0x30
  171. #define SERIAL_8E1_RXINV_TXINV 0x36
  172. #define SERIAL_8O1_RXINV_TXINV 0x37
  173. #ifdef SERIAL_9BIT_SUPPORT
  174. #define SERIAL_9N1 0x84
  175. #define SERIAL_9E1 0x8E
  176. #define SERIAL_9O1 0x8F
  177. #define SERIAL_9N1_RXINV 0x94
  178. #define SERIAL_9E1_RXINV 0x9E
  179. #define SERIAL_9O1_RXINV 0x9F
  180. #define SERIAL_9N1_TXINV 0xA4
  181. #define SERIAL_9E1_TXINV 0xAE
  182. #define SERIAL_9O1_TXINV 0xAF
  183. #define SERIAL_9N1_RXINV_TXINV 0xB4
  184. #define SERIAL_9E1_RXINV_TXINV 0xBE
  185. #define SERIAL_9O1_RXINV_TXINV 0xBF
  186. #endif
  187. // Teensy LC and 3.5 and 3.6 Uarts have 1/2 bit stop setting
  188. #if defined(__MK64FX512__) || defined(__MK66FX1M0__) || defined(KINETISL)
  189. #define SERIAL_2STOP_BITS 0x100
  190. #define SERIAL_8E2 (SERIAL_8E1 | SERIAL_2STOP_BITS)
  191. #define SERIAL_8O2 (SERIAL_8O1 | SERIAL_2STOP_BITS)
  192. #define SERIAL_8E2_RXINV (SERIAL_8E1_RXINV | SERIAL_2STOP_BITS)
  193. #define SERIAL_8O2_RXINV (SERIAL_8O1_RXINV | SERIAL_2STOP_BITS)
  194. #define SERIAL_8E2_TXINV (SERIAL_8E1_TXINV | SERIAL_2STOP_BITS)
  195. #define SERIAL_8O2_TXINV (SERIAL_8O1_TXINV | SERIAL_2STOP_BITS)
  196. #define SERIAL_8E2_RXINV_TXINV (SERIAL_8E1_RXINV_TXINV | SERIAL_2STOP_BITS)
  197. #define SERIAL_8O2_RXINV_TXINV (SERIAL_8O1_RXINV_TXINV | SERIAL_2STOP_BITS)
  198. #define SERIAL_8N2 (SERIAL_8N1 | SERIAL_2STOP_BITS)
  199. #define SERIAL_8N2_RXINV (SERIAL_8N1_RXINV | SERIAL_2STOP_BITS)
  200. #define SERIAL_8N2_TXINV (SERIAL_8N1_TXINV | SERIAL_2STOP_BITS)
  201. #define SERIAL_8N2_RXINV_TXINV (SERIAL_8N1_RXINV_TXINV | SERIAL_2STOP_BITS)
  202. #else
  203. // for Teensy 3.0-3.2 we can fake 2 stop bits by using 9 bit mode
  204. #define SERIAL_8N2 0x04
  205. #define SERIAL_8N2_RXINV 0x14
  206. #define SERIAL_8N2_TXINV 0x24
  207. #define SERIAL_8N2_RXINV_TXINV 0x34
  208. #endif
  209. // bit0: parity, 0=even, 1=odd
  210. // bit1: parity, 0=disable, 1=enable
  211. // bit2: mode, 1=9bit, 0=8bit
  212. // bit3: mode10: 1=10bit, 0=8bit
  213. // bit4: rxinv, 0=normal, 1=inverted
  214. // bit5: txinv, 0=normal, 1=inverted
  215. // bit6: unused
  216. // bit7: actual data goes into 9th bit
  217. // TODO: replace with proper divisor+oversample calculation
  218. #define BAUD2DIV(baud) (24000000/16/(baud))
  219. /*
  220. #if defined(KINETISK)
  221. #define BAUD2DIV(baud) (((F_CPU * 2) + ((baud) >> 1)) / (baud))
  222. #define BAUD2DIV2(baud) (((F_CPU * 2) + ((baud) >> 1)) / (baud))
  223. #define BAUD2DIV3(baud) (((F_BUS * 2) + ((baud) >> 1)) / (baud))
  224. #elif defined(KINETISL)
  225. #if F_CPU <= 2000000
  226. #define BAUD2DIV(baud) (((F_PLL / 16 ) + ((baud) >> 1)) / (baud))
  227. #elif F_CPU <= 16000000
  228. #define BAUD2DIV(baud) (((F_PLL / (F_PLL / 1000000)) + ((baud) >> 1)) / (baud))
  229. #else
  230. #define BAUD2DIV(baud) (((F_PLL / 2 / 16) + ((baud) >> 1)) / (baud))
  231. #endif
  232. #define BAUD2DIV2(baud) (((F_BUS / 16) + ((baud) >> 1)) / (baud))
  233. #define BAUD2DIV3(baud) (((F_BUS / 16) + ((baud) >> 1)) / (baud))
  234. #endif
  235. */
  236. #endif