Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

HardwareSerial.h 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. // Half duplex support
  91. #define SERIAL_HALF_DUPLEX 0x200
  92. #define SERIAL_7E1_HALF_DUPLEX (SERIAL_7E1 | SERIAL_HALF_DUPLEX)
  93. #define SERIAL_7O1_HALF_DUPLEX (SERIAL_7O1 | SERIAL_HALF_DUPLEX)
  94. #define SERIAL_8N1_HALF_DUPLEX (SERIAL_8N1 | SERIAL_HALF_DUPLEX)
  95. // bit0: parity, 0=even, 1=odd
  96. // bit1: parity, 0=disable, 1=enable
  97. // bit2: mode, 1=9bit, 0=8bit
  98. // bit3: mode10: 1=10bit, 0=8bit
  99. // bit4: rxinv, 0=normal, 1=inverted
  100. // bit5: txinv, 0=normal, 1=inverted
  101. // bit6: unused
  102. // bit7: actual data goes into 9th bit
  103. // bit8: 2 stop bits
  104. // bit9: Half Duplex Mode
  105. #ifdef __cplusplus
  106. #include "Stream.h"
  107. #include "core_pins.h"
  108. #ifdef SERIAL_9BIT_SUPPORT
  109. #define BUFTYPE uint16_t
  110. #else
  111. #define BUFTYPE uint8_t
  112. #endif
  113. extern "C" {
  114. extern void IRQHandler_Serial1();
  115. extern void IRQHandler_Serial2();
  116. extern void IRQHandler_Serial3();
  117. extern void IRQHandler_Serial4();
  118. extern void IRQHandler_Serial5();
  119. extern void IRQHandler_Serial6();
  120. extern void IRQHandler_Serial7();
  121. #if defined(ARDUINO_TEENSY41)
  122. extern void IRQHandler_Serial8();
  123. #endif
  124. }
  125. //===================================================================
  126. // Should find a good home for this
  127. // Map IO pin to XBar pin...
  128. //===================================================================
  129. // BUGBUG - find a good home
  130. typedef struct _pin_to_xbar_info{
  131. const uint8_t pin; // The pin number
  132. const uint8_t xbar_in_index; // What XBar input index.
  133. const uint32_t mux_val; // Value to set for mux;
  134. volatile uint32_t *select_input_register; // Which register controls the selection
  135. const uint32_t select_val; // Value for that selection
  136. } pin_to_xbar_info_t;
  137. extern const pin_to_xbar_info_t pin_to_xbar_info[];
  138. extern const uint8_t count_pin_to_xbar_info;
  139. class HardwareSerial : public Stream
  140. {
  141. public:
  142. static const uint8_t cnt_tx_pins = 2;
  143. static const uint8_t cnt_rx_pins = 2;
  144. typedef struct {
  145. const uint8_t pin; // The pin number
  146. const uint32_t mux_val; // Value to set for mux;
  147. volatile uint32_t *select_input_register; // Which register controls the selection
  148. const uint32_t select_val; // Value for that selection
  149. } pin_info_t;
  150. typedef struct {
  151. uint8_t serial_index; // which object are we? 0 based
  152. IRQ_NUMBER_t irq;
  153. void (*irq_handler)(void);
  154. void (* _serialEvent)(void);
  155. const uint8_t *serial_event_handler_default;
  156. volatile uint32_t &ccm_register;
  157. const uint32_t ccm_value;
  158. pin_info_t rx_pins[cnt_rx_pins];
  159. pin_info_t tx_pins[cnt_tx_pins];
  160. const uint8_t cts_pin;
  161. const uint8_t cts_mux_val;
  162. const uint16_t irq_priority;
  163. const uint16_t rts_low_watermark;
  164. const uint16_t rts_high_watermark;
  165. const uint8_t xbar_out_lpuartX_trig_input;
  166. } hardware_t;
  167. public:
  168. constexpr HardwareSerial(IMXRT_LPUART_t *myport, const hardware_t *myhardware,
  169. volatile BUFTYPE *_tx_buffer, size_t _tx_buffer_size,
  170. volatile BUFTYPE *_rx_buffer, size_t _rx_buffer_size) :
  171. port(myport), hardware(myhardware),
  172. tx_buffer_(_tx_buffer), rx_buffer_(_rx_buffer), tx_buffer_size_(_tx_buffer_size), rx_buffer_size_(_rx_buffer_size),
  173. tx_buffer_total_size_(_tx_buffer_size), rx_buffer_total_size_(_rx_buffer_size) {
  174. }
  175. void begin(uint32_t baud, uint16_t format=0);
  176. void end(void);
  177. virtual int available(void);
  178. virtual int peek(void);
  179. virtual void flush(void);
  180. virtual size_t write(uint8_t c);
  181. virtual int read(void);
  182. void transmitterEnable(uint8_t pin);
  183. void setRX(uint8_t pin);
  184. void setTX(uint8_t pin, bool opendrain=false);
  185. bool attachRts(uint8_t pin);
  186. bool attachCts(uint8_t pin);
  187. void clear(void);
  188. int availableForWrite(void);
  189. void addMemoryForRead(void *buffer, size_t length);
  190. void addMemoryForWrite(void *buffer, size_t length);
  191. void addStorageForRead(void *buffer, size_t length) __attribute__((deprecated("addStorageForRead was renamed to addMemoryForRead"))){
  192. addMemoryForRead(buffer, length);
  193. }
  194. void addStorageForWrite(void *buffer, size_t length) __attribute__((deprecated("addStorageForWrite was renamed to addMemoryForWrite"))){
  195. addMemoryForWrite(buffer, length);
  196. }
  197. size_t write9bit(uint32_t c);
  198. // Event Handler functions and data
  199. static uint8_t serial_event_handlers_active;
  200. using Print::write;
  201. size_t write(unsigned long n) { return write((uint8_t)n); }
  202. size_t write(long n) { return write((uint8_t)n); }
  203. size_t write(unsigned int n) { return write((uint8_t)n); }
  204. size_t write(int n) { return write((uint8_t)n); }
  205. // Only overwrite some of the virtualWrite functions if we are going to optimize them over Print version
  206. /*
  207. virtual void begin(uint32_t baud) { serial_begin(BAUD2DIV(baud)); }
  208. virtual void begin(uint32_t baud, uint32_t format) {
  209. serial_begin(BAUD2DIV(baud));
  210. serial_format(format); }
  211. */
  212. operator bool() { return true; }
  213. static inline void processSerialEventsList() {
  214. for (uint8_t i = 0; i < s_count_serials_with_serial_events; i++) {
  215. s_serials_with_serial_events[i]->doYieldCode();
  216. }
  217. }
  218. private:
  219. IMXRT_LPUART_t * const port;
  220. const hardware_t * const hardware;
  221. uint8_t rx_pin_index_ = 0x0; // default is always first item
  222. uint8_t tx_pin_index_ = 0x0;
  223. uint8_t half_duplex_mode_ = 0; // are we in half duplex mode?
  224. volatile BUFTYPE *tx_buffer_;
  225. volatile BUFTYPE *rx_buffer_;
  226. volatile BUFTYPE *rx_buffer_storage_ = nullptr;
  227. volatile BUFTYPE *tx_buffer_storage_ = nullptr;
  228. size_t tx_buffer_size_;
  229. size_t rx_buffer_size_;
  230. size_t tx_buffer_total_size_;
  231. size_t rx_buffer_total_size_;
  232. size_t rts_low_watermark_ = 0;
  233. size_t rts_high_watermark_ = 0;
  234. volatile uint8_t transmitting_ = 0;
  235. volatile uint16_t tx_buffer_head_ = 0;
  236. volatile uint16_t tx_buffer_tail_ = 0;
  237. volatile uint16_t rx_buffer_head_ = 0;
  238. volatile uint16_t rx_buffer_tail_ = 0;
  239. volatile uint32_t *transmit_pin_baseReg_ = 0;
  240. uint32_t transmit_pin_bitmask_ = 0;
  241. volatile uint32_t *rts_pin_baseReg_ = 0;
  242. uint32_t rts_pin_bitmask_ = 0;
  243. inline void rts_assert();
  244. inline void rts_deassert();
  245. void IRQHandler();
  246. friend void IRQHandler_Serial1();
  247. friend void IRQHandler_Serial2();
  248. friend void IRQHandler_Serial3();
  249. friend void IRQHandler_Serial4();
  250. friend void IRQHandler_Serial5();
  251. friend void IRQHandler_Serial6();
  252. friend void IRQHandler_Serial7();
  253. #if defined(ARDUINO_TEENSY41)
  254. friend void IRQHandler_Serial8();
  255. static HardwareSerial *s_serials_with_serial_events[8];
  256. #else
  257. static HardwareSerial *s_serials_with_serial_events[7];
  258. #endif
  259. static uint8_t s_count_serials_with_serial_events;
  260. void addToSerialEventsList();
  261. inline void doYieldCode() {
  262. if (available()) (*hardware->_serialEvent)();
  263. }
  264. };
  265. extern HardwareSerial Serial1;
  266. extern HardwareSerial Serial2;
  267. extern HardwareSerial Serial3;
  268. extern HardwareSerial Serial4;
  269. extern HardwareSerial Serial5;
  270. extern HardwareSerial Serial6;
  271. extern HardwareSerial Serial7;
  272. extern void serialEvent1(void);
  273. extern void serialEvent2(void);
  274. extern void serialEvent3(void);
  275. extern void serialEvent4(void);
  276. extern void serialEvent5(void);
  277. extern void serialEvent6(void);
  278. extern void serialEvent7(void);
  279. #if defined(ARDUINO_TEENSY41)
  280. extern HardwareSerial Serial8;
  281. extern void serialEvent8(void);
  282. #endif
  283. #endif // __cplusplus
  284. // c functions to call c++ code in case some programs call the old functions
  285. // Defined under extern "C" {}
  286. #ifdef __cplusplus
  287. extern "C" {
  288. #endif
  289. extern void serial_print(const char *p);
  290. extern void serial_phex(uint32_t n);
  291. extern void serial_phex16(uint32_t n);
  292. extern void serial_phex32(uint32_t n);
  293. #ifdef __cplusplus
  294. }
  295. #endif
  296. // TODO: replace with proper divisor+oversample calculation
  297. #define BAUD2DIV(baud) (24000000/16/(baud))
  298. /*
  299. #if defined(KINETISK)
  300. #define BAUD2DIV(baud) (((F_CPU * 2) + ((baud) >> 1)) / (baud))
  301. #define BAUD2DIV2(baud) (((F_CPU * 2) + ((baud) >> 1)) / (baud))
  302. #define BAUD2DIV3(baud) (((F_BUS * 2) + ((baud) >> 1)) / (baud))
  303. #elif defined(KINETISL)
  304. #if F_CPU <= 2000000
  305. #define BAUD2DIV(baud) (((F_PLL / 16 ) + ((baud) >> 1)) / (baud))
  306. #elif F_CPU <= 16000000
  307. #define BAUD2DIV(baud) (((F_PLL / (F_PLL / 1000000)) + ((baud) >> 1)) / (baud))
  308. #else
  309. #define BAUD2DIV(baud) (((F_PLL / 2 / 16) + ((baud) >> 1)) / (baud))
  310. #endif
  311. #define BAUD2DIV2(baud) (((F_BUS / 16) + ((baud) >> 1)) / (baud))
  312. #define BAUD2DIV3(baud) (((F_BUS / 16) + ((baud) >> 1)) / (baud))
  313. #endif
  314. */
  315. #endif