Teensy 4.1 core updated for C++20
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

10 лет назад
10 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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 "kinetis.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 3.x boards support 9 bit mode on all their serial ports
  42. // Teensy LC only supports 9 bit mode on Serial1. Serial2 & Serial3 can't use 9 bits.
  43. #define SERIAL_7E1 0x02
  44. #define SERIAL_7O1 0x03
  45. #define SERIAL_8N1 0x00
  46. #define SERIAL_8E1 0x06
  47. #define SERIAL_8O1 0x07
  48. #define SERIAL_7E1_RXINV 0x12
  49. #define SERIAL_7O1_RXINV 0x13
  50. #define SERIAL_8N1_RXINV 0x10
  51. #define SERIAL_8E1_RXINV 0x16
  52. #define SERIAL_8O1_RXINV 0x17
  53. #define SERIAL_7E1_TXINV 0x22
  54. #define SERIAL_7O1_TXINV 0x23
  55. #define SERIAL_8N1_TXINV 0x20
  56. #define SERIAL_8E1_TXINV 0x26
  57. #define SERIAL_8O1_TXINV 0x27
  58. #define SERIAL_7E1_RXINV_TXINV 0x32
  59. #define SERIAL_7O1_RXINV_TXINV 0x33
  60. #define SERIAL_8N1_RXINV_TXINV 0x30
  61. #define SERIAL_8E1_RXINV_TXINV 0x36
  62. #define SERIAL_8O1_RXINV_TXINV 0x37
  63. #ifdef SERIAL_9BIT_SUPPORT
  64. #define SERIAL_9N1 0x84
  65. #define SERIAL_9E1 0x8E
  66. #define SERIAL_9O1 0x8F
  67. #define SERIAL_9N1_RXINV 0x94
  68. #define SERIAL_9E1_RXINV 0x9E
  69. #define SERIAL_9O1_RXINV 0x9F
  70. #define SERIAL_9N1_TXINV 0xA4
  71. #define SERIAL_9E1_TXINV 0xAE
  72. #define SERIAL_9O1_TXINV 0xAF
  73. #define SERIAL_9N1_RXINV_TXINV 0xB4
  74. #define SERIAL_9E1_RXINV_TXINV 0xBE
  75. #define SERIAL_9O1_RXINV_TXINV 0xBF
  76. #endif
  77. // Teensy LC and 3.5 and 3.6 Uarts have 1/2 bit stop setting
  78. #if defined(__MK64FX512__) || defined(__MK66FX1M0__) || defined(KINETISL)
  79. #define SERIAL_2STOP_BITS 0x100
  80. #define SERIAL_8E2 (SERIAL_8E1 | SERIAL_2STOP_BITS)
  81. #define SERIAL_8O2 (SERIAL_8O1 | SERIAL_2STOP_BITS)
  82. #define SERIAL_8E2_RXINV (SERIAL_8E1_RXINV | SERIAL_2STOP_BITS)
  83. #define SERIAL_8O2_RXINV (SERIAL_8O1_RXINV | SERIAL_2STOP_BITS)
  84. #define SERIAL_8E2_TXINV (SERIAL_8E1_TXINV | SERIAL_2STOP_BITS)
  85. #define SERIAL_8O2_TXINV (SERIAL_8O1_TXINV | SERIAL_2STOP_BITS)
  86. #define SERIAL_8E2_RXINV_TXINV (SERIAL_8E1_RXINV_TXINV | SERIAL_2STOP_BITS)
  87. #define SERIAL_8O2_RXINV_TXINV (SERIAL_8O1_RXINV_TXINV | SERIAL_2STOP_BITS)
  88. #define SERIAL_8N2 (SERIAL_8N1 | SERIAL_2STOP_BITS)
  89. #define SERIAL_8N2_RXINV (SERIAL_8N1_RXINV | SERIAL_2STOP_BITS)
  90. #define SERIAL_8N2_TXINV (SERIAL_8N1_TXINV | SERIAL_2STOP_BITS)
  91. #define SERIAL_8N2_RXINV_TXINV (SERIAL_8N1_RXINV_TXINV | SERIAL_2STOP_BITS)
  92. #else
  93. // for Teensy 3.0-3.2 we can fake 2 stop bits by using 9 bit mode
  94. #define SERIAL_8N2 0x04
  95. #define SERIAL_8N2_RXINV 0x14
  96. #define SERIAL_8N2_TXINV 0x24
  97. #define SERIAL_8N2_RXINV_TXINV 0x34
  98. #endif
  99. // bit0: parity, 0=even, 1=odd
  100. // bit1: parity, 0=disable, 1=enable
  101. // bit2: mode, 1=9bit, 0=8bit
  102. // bit3: mode10: 1=10bit, 0=8bit
  103. // bit4: rxinv, 0=normal, 1=inverted
  104. // bit5: txinv, 0=normal, 1=inverted
  105. // bit6: unused
  106. // bit7: actual data goes into 9th bit
  107. #if defined(KINETISK)
  108. #define BAUD2DIV(baud) (((F_CPU * 2) + ((baud) >> 1)) / (baud))
  109. #define BAUD2DIV2(baud) (((F_CPU * 2) + ((baud) >> 1)) / (baud))
  110. #define BAUD2DIV3(baud) (((F_BUS * 2) + ((baud) >> 1)) / (baud))
  111. #elif defined(KINETISL)
  112. #if F_CPU <= 2000000
  113. #define BAUD2DIV(baud) (((F_PLL / 16 ) + ((baud) >> 1)) / (baud))
  114. #elif F_CPU <= 16000000
  115. #define BAUD2DIV(baud) (((F_PLL / (F_PLL / 1000000)) + ((baud) >> 1)) / (baud))
  116. #else
  117. #define BAUD2DIV(baud) (((F_PLL / 2 / 16) + ((baud) >> 1)) / (baud))
  118. #endif
  119. #define BAUD2DIV2(baud) (((F_BUS / 16) + ((baud) >> 1)) / (baud))
  120. #define BAUD2DIV3(baud) (((F_BUS / 16) + ((baud) >> 1)) / (baud))
  121. #endif
  122. // C language implementation
  123. //
  124. #ifdef __cplusplus
  125. extern "C" {
  126. #endif
  127. void serial_begin(uint32_t divisor);
  128. void serial_format(uint32_t format);
  129. void serial_end(void);
  130. void serial_set_transmit_pin(uint8_t pin);
  131. void serial_set_rx(uint8_t pin);
  132. void serial_set_tx(uint8_t pin, uint8_t opendrain);
  133. int serial_set_rts(uint8_t pin);
  134. int serial_set_cts(uint8_t pin);
  135. void serial_putchar(uint32_t c);
  136. void serial_write(const void *buf, unsigned int count);
  137. void serial_flush(void);
  138. int serial_write_buffer_free(void);
  139. int serial_available(void);
  140. int serial_getchar(void);
  141. int serial_peek(void);
  142. void serial_clear(void);
  143. void serial_print(const char *p);
  144. void serial_phex(uint32_t n);
  145. void serial_phex16(uint32_t n);
  146. void serial_phex32(uint32_t n);
  147. void serial2_begin(uint32_t divisor);
  148. void serial2_format(uint32_t format);
  149. void serial2_end(void);
  150. void serial2_set_transmit_pin(uint8_t pin);
  151. void serial2_set_rx(uint8_t pin);
  152. void serial2_set_tx(uint8_t pin, uint8_t opendrain);
  153. int serial2_set_rts(uint8_t pin);
  154. int serial2_set_cts(uint8_t pin);
  155. void serial2_putchar(uint32_t c);
  156. void serial2_write(const void *buf, unsigned int count);
  157. void serial2_flush(void);
  158. int serial2_write_buffer_free(void);
  159. int serial2_available(void);
  160. int serial2_getchar(void);
  161. int serial2_peek(void);
  162. void serial2_clear(void);
  163. void serial3_begin(uint32_t divisor);
  164. void serial3_format(uint32_t format);
  165. void serial3_end(void);
  166. void serial3_set_transmit_pin(uint8_t pin);
  167. void serial3_set_rx(uint8_t pin);
  168. void serial3_set_tx(uint8_t pin, uint8_t opendrain);
  169. int serial3_set_rts(uint8_t pin);
  170. int serial3_set_cts(uint8_t pin);
  171. void serial3_putchar(uint32_t c);
  172. void serial3_write(const void *buf, unsigned int count);
  173. void serial3_flush(void);
  174. int serial3_write_buffer_free(void);
  175. int serial3_available(void);
  176. int serial3_getchar(void);
  177. int serial3_peek(void);
  178. void serial3_clear(void);
  179. void serial4_begin(uint32_t divisor);
  180. void serial4_format(uint32_t format);
  181. void serial4_end(void);
  182. void serial4_set_transmit_pin(uint8_t pin);
  183. void serial4_set_rx(uint8_t pin);
  184. void serial4_set_tx(uint8_t pin, uint8_t opendrain);
  185. int serial4_set_rts(uint8_t pin);
  186. int serial4_set_cts(uint8_t pin);
  187. void serial4_putchar(uint32_t c);
  188. void serial4_write(const void *buf, unsigned int count);
  189. void serial4_flush(void);
  190. int serial4_write_buffer_free(void);
  191. int serial4_available(void);
  192. int serial4_getchar(void);
  193. int serial4_peek(void);
  194. void serial4_clear(void);
  195. void serial5_begin(uint32_t divisor);
  196. void serial5_format(uint32_t format);
  197. void serial5_end(void);
  198. void serial5_set_transmit_pin(uint8_t pin);
  199. void serial5_set_rx(uint8_t pin);
  200. void serial5_set_tx(uint8_t pin, uint8_t opendrain);
  201. int serial5_set_rts(uint8_t pin);
  202. int serial5_set_cts(uint8_t pin);
  203. void serial5_putchar(uint32_t c);
  204. void serial5_write(const void *buf, unsigned int count);
  205. void serial5_flush(void);
  206. int serial5_write_buffer_free(void);
  207. int serial5_available(void);
  208. int serial5_getchar(void);
  209. int serial5_peek(void);
  210. void serial5_clear(void);
  211. void serial6_begin(uint32_t divisor);
  212. void serial6_format(uint32_t format);
  213. void serial6_end(void);
  214. void serial6_set_transmit_pin(uint8_t pin);
  215. void serial6_set_rx(uint8_t pin);
  216. void serial6_set_tx(uint8_t pin, uint8_t opendrain);
  217. int serial6_set_rts(uint8_t pin);
  218. int serial6_set_cts(uint8_t pin);
  219. void serial6_putchar(uint32_t c);
  220. void serial6_write(const void *buf, unsigned int count);
  221. void serial6_flush(void);
  222. int serial6_write_buffer_free(void);
  223. int serial6_available(void);
  224. int serial6_getchar(void);
  225. int serial6_peek(void);
  226. void serial6_clear(void);
  227. #ifdef __cplusplus
  228. }
  229. #endif
  230. // C++ interface
  231. //
  232. #ifdef __cplusplus
  233. #include "Stream.h"
  234. class HardwareSerial : public Stream
  235. {
  236. public:
  237. constexpr HardwareSerial() {}
  238. virtual void begin(uint32_t baud) { serial_begin(BAUD2DIV(baud)); }
  239. virtual void begin(uint32_t baud, uint32_t format) {
  240. serial_begin(BAUD2DIV(baud));
  241. serial_format(format); }
  242. virtual void end(void) { serial_end(); }
  243. virtual void transmitterEnable(uint8_t pin) { serial_set_transmit_pin(pin); }
  244. virtual void setRX(uint8_t pin) { serial_set_rx(pin); }
  245. virtual void setTX(uint8_t pin, bool opendrain=false) { serial_set_tx(pin, opendrain); }
  246. virtual bool attachRts(uint8_t pin) { return serial_set_rts(pin); }
  247. virtual bool attachCts(uint8_t pin) { return serial_set_cts(pin); }
  248. virtual int available(void) { return serial_available(); }
  249. virtual int peek(void) { return serial_peek(); }
  250. virtual int read(void) { return serial_getchar(); }
  251. virtual void flush(void) { serial_flush(); }
  252. virtual void clear(void) { serial_clear(); }
  253. virtual int availableForWrite(void) { return serial_write_buffer_free(); }
  254. using Print::write;
  255. virtual size_t write(uint8_t c) { serial_putchar(c); return 1; }
  256. virtual size_t write(unsigned long n) { return write((uint8_t)n); }
  257. virtual size_t write(long n) { return write((uint8_t)n); }
  258. virtual size_t write(unsigned int n) { return write((uint8_t)n); }
  259. virtual size_t write(int n) { return write((uint8_t)n); }
  260. virtual size_t write(const uint8_t *buffer, size_t size)
  261. { serial_write(buffer, size); return size; }
  262. virtual size_t write(const char *str) { size_t len = strlen(str);
  263. serial_write((const uint8_t *)str, len);
  264. return len; }
  265. virtual size_t write9bit(uint32_t c) { serial_putchar(c); return 1; }
  266. operator bool() { return true; }
  267. };
  268. extern HardwareSerial Serial1;
  269. extern void serialEvent1(void);
  270. class HardwareSerial2 : public HardwareSerial
  271. {
  272. public:
  273. constexpr HardwareSerial2() {}
  274. virtual void begin(uint32_t baud) { serial2_begin(BAUD2DIV2(baud)); }
  275. virtual void begin(uint32_t baud, uint32_t format) {
  276. serial2_begin(BAUD2DIV2(baud));
  277. serial2_format(format); }
  278. virtual void end(void) { serial2_end(); }
  279. virtual void transmitterEnable(uint8_t pin) { serial2_set_transmit_pin(pin); }
  280. virtual void setRX(uint8_t pin) { serial2_set_rx(pin); }
  281. virtual void setTX(uint8_t pin, bool opendrain=false) { serial2_set_tx(pin, opendrain); }
  282. virtual bool attachRts(uint8_t pin) { return serial2_set_rts(pin); }
  283. virtual bool attachCts(uint8_t pin) { return serial2_set_cts(pin); }
  284. virtual int available(void) { return serial2_available(); }
  285. virtual int peek(void) { return serial2_peek(); }
  286. virtual int read(void) { return serial2_getchar(); }
  287. virtual void flush(void) { serial2_flush(); }
  288. virtual void clear(void) { serial2_clear(); }
  289. virtual int availableForWrite(void) { return serial2_write_buffer_free(); }
  290. using Print::write;
  291. virtual size_t write(uint8_t c) { serial2_putchar(c); return 1; }
  292. virtual size_t write(unsigned long n) { return write((uint8_t)n); }
  293. virtual size_t write(long n) { return write((uint8_t)n); }
  294. virtual size_t write(unsigned int n) { return write((uint8_t)n); }
  295. virtual size_t write(int n) { return write((uint8_t)n); }
  296. virtual size_t write(const uint8_t *buffer, size_t size)
  297. { serial2_write(buffer, size); return size; }
  298. virtual size_t write(const char *str) { size_t len = strlen(str);
  299. serial2_write((const uint8_t *)str, len);
  300. return len; }
  301. virtual size_t write9bit(uint32_t c) { serial2_putchar(c); return 1; }
  302. operator bool() { return true; }
  303. };
  304. extern HardwareSerial2 Serial2;
  305. extern void serialEvent2(void);
  306. class HardwareSerial3 : public HardwareSerial
  307. {
  308. public:
  309. constexpr HardwareSerial3() {}
  310. virtual void begin(uint32_t baud) { serial3_begin(BAUD2DIV3(baud)); }
  311. virtual void begin(uint32_t baud, uint32_t format) {
  312. serial3_begin(BAUD2DIV3(baud));
  313. serial3_format(format); }
  314. virtual void end(void) { serial3_end(); }
  315. virtual void transmitterEnable(uint8_t pin) { serial3_set_transmit_pin(pin); }
  316. virtual void setRX(uint8_t pin) { serial3_set_rx(pin); }
  317. virtual void setTX(uint8_t pin, bool opendrain=false) { serial3_set_tx(pin, opendrain); }
  318. virtual bool attachRts(uint8_t pin) { return serial3_set_rts(pin); }
  319. virtual bool attachCts(uint8_t pin) { return serial3_set_cts(pin); }
  320. virtual int available(void) { return serial3_available(); }
  321. virtual int peek(void) { return serial3_peek(); }
  322. virtual int read(void) { return serial3_getchar(); }
  323. virtual void flush(void) { serial3_flush(); }
  324. virtual void clear(void) { serial3_clear(); }
  325. virtual int availableForWrite(void) { return serial3_write_buffer_free(); }
  326. using Print::write;
  327. virtual size_t write(uint8_t c) { serial3_putchar(c); return 1; }
  328. virtual size_t write(unsigned long n) { return write((uint8_t)n); }
  329. virtual size_t write(long n) { return write((uint8_t)n); }
  330. virtual size_t write(unsigned int n) { return write((uint8_t)n); }
  331. virtual size_t write(int n) { return write((uint8_t)n); }
  332. virtual size_t write(const uint8_t *buffer, size_t size)
  333. { serial3_write(buffer, size); return size; }
  334. virtual size_t write(const char *str) { size_t len = strlen(str);
  335. serial3_write((const uint8_t *)str, len);
  336. return len; }
  337. virtual size_t write9bit(uint32_t c) { serial3_putchar(c); return 1; }
  338. operator bool() { return true; }
  339. };
  340. extern HardwareSerial3 Serial3;
  341. extern void serialEvent3(void);
  342. class HardwareSerial4 : public HardwareSerial
  343. {
  344. public:
  345. constexpr HardwareSerial4() {}
  346. virtual void begin(uint32_t baud) { serial4_begin(BAUD2DIV3(baud)); }
  347. virtual void begin(uint32_t baud, uint32_t format) {
  348. serial4_begin(BAUD2DIV3(baud));
  349. serial4_format(format); }
  350. virtual void end(void) { serial4_end(); }
  351. virtual void transmitterEnable(uint8_t pin) { serial4_set_transmit_pin(pin); }
  352. virtual void setRX(uint8_t pin) { serial4_set_rx(pin); }
  353. virtual void setTX(uint8_t pin, bool opendrain=false) { serial4_set_tx(pin, opendrain); }
  354. virtual bool attachRts(uint8_t pin) { return serial4_set_rts(pin); }
  355. virtual bool attachCts(uint8_t pin) { return serial4_set_cts(pin); }
  356. virtual int available(void) { return serial4_available(); }
  357. virtual int peek(void) { return serial4_peek(); }
  358. virtual int read(void) { return serial4_getchar(); }
  359. virtual void flush(void) { serial4_flush(); }
  360. virtual void clear(void) { serial4_clear(); }
  361. virtual int availableForWrite(void) { return serial4_write_buffer_free(); }
  362. using Print::write;
  363. virtual size_t write(uint8_t c) { serial4_putchar(c); return 1; }
  364. virtual size_t write(unsigned long n) { return write((uint8_t)n); }
  365. virtual size_t write(long n) { return write((uint8_t)n); }
  366. virtual size_t write(unsigned int n) { return write((uint8_t)n); }
  367. virtual size_t write(int n) { return write((uint8_t)n); }
  368. virtual size_t write(const uint8_t *buffer, size_t size)
  369. { serial4_write(buffer, size); return size; }
  370. virtual size_t write(const char *str) { size_t len = strlen(str);
  371. serial4_write((const uint8_t *)str, len);
  372. return len; }
  373. virtual size_t write9bit(uint32_t c) { serial4_putchar(c); return 1; }
  374. operator bool() { return true; }
  375. };
  376. extern HardwareSerial4 Serial4;
  377. extern void serialEvent4(void);
  378. class HardwareSerial5 : public HardwareSerial
  379. {
  380. public:
  381. constexpr HardwareSerial5() {}
  382. virtual void begin(uint32_t baud) { serial5_begin(BAUD2DIV3(baud)); }
  383. virtual void begin(uint32_t baud, uint32_t format) {
  384. serial5_begin(BAUD2DIV3(baud));
  385. serial5_format(format); }
  386. virtual void end(void) { serial5_end(); }
  387. virtual void transmitterEnable(uint8_t pin) { serial5_set_transmit_pin(pin); }
  388. virtual void setRX(uint8_t pin) { serial5_set_rx(pin); }
  389. virtual void setTX(uint8_t pin, bool opendrain=false) { serial5_set_tx(pin, opendrain); }
  390. virtual bool attachRts(uint8_t pin) { return serial5_set_rts(pin); }
  391. virtual bool attachCts(uint8_t pin) { return serial5_set_cts(pin); }
  392. virtual int available(void) { return serial5_available(); }
  393. virtual int peek(void) { return serial5_peek(); }
  394. virtual int read(void) { return serial5_getchar(); }
  395. virtual void flush(void) { serial5_flush(); }
  396. virtual void clear(void) { serial5_clear(); }
  397. virtual int availableForWrite(void) { return serial5_write_buffer_free(); }
  398. using Print::write;
  399. virtual size_t write(uint8_t c) { serial5_putchar(c); return 1; }
  400. virtual size_t write(unsigned long n) { return write((uint8_t)n); }
  401. virtual size_t write(long n) { return write((uint8_t)n); }
  402. virtual size_t write(unsigned int n) { return write((uint8_t)n); }
  403. virtual size_t write(int n) { return write((uint8_t)n); }
  404. virtual size_t write(const uint8_t *buffer, size_t size)
  405. { serial5_write(buffer, size); return size; }
  406. virtual size_t write(const char *str) { size_t len = strlen(str);
  407. serial5_write((const uint8_t *)str, len);
  408. return len; }
  409. virtual size_t write9bit(uint32_t c) { serial5_putchar(c); return 1; }
  410. operator bool() { return true; }
  411. };
  412. extern HardwareSerial5 Serial5;
  413. extern void serialEvent5(void);
  414. class HardwareSerial6 : public HardwareSerial
  415. {
  416. public:
  417. constexpr HardwareSerial6() {}
  418. #if defined(__MK66FX1M0__) // For LPUART just pass baud straight in.
  419. virtual void begin(uint32_t baud) { serial6_begin(baud); }
  420. virtual void begin(uint32_t baud, uint32_t format) {
  421. serial6_begin(baud);
  422. serial6_format(format); }
  423. #else
  424. virtual void begin(uint32_t baud) { serial6_begin(BAUD2DIV3(baud)); }
  425. virtual void begin(uint32_t baud, uint32_t format) {
  426. serial6_begin(BAUD2DIV3(baud));
  427. serial6_format(format); }
  428. #endif
  429. virtual void end(void) { serial6_end(); }
  430. virtual void transmitterEnable(uint8_t pin) { serial6_set_transmit_pin(pin); }
  431. virtual void setRX(uint8_t pin) { serial6_set_rx(pin); }
  432. virtual void setTX(uint8_t pin, bool opendrain=false) { serial6_set_tx(pin, opendrain); }
  433. virtual bool attachRts(uint8_t pin) { return serial6_set_rts(pin); }
  434. virtual bool attachCts(uint8_t pin) { return serial6_set_cts(pin); }
  435. virtual int available(void) { return serial6_available(); }
  436. virtual int peek(void) { return serial6_peek(); }
  437. virtual int read(void) { return serial6_getchar(); }
  438. virtual void flush(void) { serial6_flush(); }
  439. virtual void clear(void) { serial6_clear(); }
  440. virtual int availableForWrite(void) { return serial6_write_buffer_free(); }
  441. using Print::write;
  442. virtual size_t write(uint8_t c) { serial6_putchar(c); return 1; }
  443. virtual size_t write(unsigned long n) { return write((uint8_t)n); }
  444. virtual size_t write(long n) { return write((uint8_t)n); }
  445. virtual size_t write(unsigned int n) { return write((uint8_t)n); }
  446. virtual size_t write(int n) { return write((uint8_t)n); }
  447. virtual size_t write(const uint8_t *buffer, size_t size)
  448. { serial6_write(buffer, size); return size; }
  449. virtual size_t write(const char *str) { size_t len = strlen(str);
  450. serial6_write((const uint8_t *)str, len);
  451. return len; }
  452. virtual size_t write9bit(uint32_t c) { serial6_putchar(c); return 1; }
  453. operator bool() { return true; }
  454. };
  455. extern HardwareSerial6 Serial6;
  456. extern void serialEvent6(void);
  457. #endif
  458. #endif