Teensy 4.1 core updated for C++20
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.

516 lines
21KB

  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. // Half duplex support
  100. #define SERIAL_HALF_DUPLEX 0x200
  101. #define SERIAL_7E1_HALF_DUPLEX (SERIAL_7E1 | SERIAL_HALF_DUPLEX)
  102. #define SERIAL_7O1_HALF_DUPLEX (SERIAL_7O1 | SERIAL_HALF_DUPLEX)
  103. #define SERIAL_8N1_HALF_DUPLEX (SERIAL_8N1 | SERIAL_HALF_DUPLEX)
  104. // bit0: parity, 0=even, 1=odd
  105. // bit1: parity, 0=disable, 1=enable
  106. // bit2: mode, 1=9bit, 0=8bit
  107. // bit3: mode10: 1=10bit, 0=8bit
  108. // bit4: rxinv, 0=normal, 1=inverted
  109. // bit5: txinv, 0=normal, 1=inverted
  110. // bit6: unused
  111. // bit7: actual data goes into 9th bit
  112. // bit8: 2 stop bits (T3.5/3.6 and LC)
  113. // bit9: Half duplex
  114. #if defined(KINETISK)
  115. #define BAUD2DIV(baud) (((F_CPU * 2) + ((baud) >> 1)) / (baud))
  116. #define BAUD2DIV2(baud) (((F_CPU * 2) + ((baud) >> 1)) / (baud))
  117. #define BAUD2DIV3(baud) (((F_BUS * 2) + ((baud) >> 1)) / (baud))
  118. #elif defined(KINETISL)
  119. #if F_CPU <= 2000000
  120. #define BAUD2DIV(baud) (((F_PLL / 16 ) + ((baud) >> 1)) / (baud))
  121. #elif F_CPU <= 16000000
  122. #define BAUD2DIV(baud) (((F_PLL / (F_PLL / 1000000)) + ((baud) >> 1)) / (baud))
  123. #else
  124. #define BAUD2DIV(baud) (((F_PLL / 2 / 16) + ((baud) >> 1)) / (baud))
  125. #endif
  126. #define BAUD2DIV2(baud) (((F_BUS / 16) + ((baud) >> 1)) / (baud))
  127. #define BAUD2DIV3(baud) (((F_BUS / 16) + ((baud) >> 1)) / (baud))
  128. #endif
  129. // C language implementation
  130. //
  131. #ifdef __cplusplus
  132. extern "C" {
  133. #endif
  134. void serial_begin(uint32_t divisor);
  135. void serial_format(uint32_t format);
  136. void serial_end(void);
  137. void serial_set_transmit_pin(uint8_t pin);
  138. void serial_set_rx(uint8_t pin);
  139. void serial_set_tx(uint8_t pin, uint8_t opendrain);
  140. int serial_set_rts(uint8_t pin);
  141. int serial_set_cts(uint8_t pin);
  142. void serial_putchar(uint32_t c);
  143. void serial_write(const void *buf, unsigned int count);
  144. void serial_flush(void);
  145. int serial_write_buffer_free(void);
  146. int serial_available(void);
  147. int serial_getchar(void);
  148. int serial_peek(void);
  149. void serial_clear(void);
  150. void serial_print(const char *p);
  151. void serial_phex(uint32_t n);
  152. void serial_phex16(uint32_t n);
  153. void serial_phex32(uint32_t n);
  154. void serial2_begin(uint32_t divisor);
  155. void serial2_format(uint32_t format);
  156. void serial2_end(void);
  157. void serial2_set_transmit_pin(uint8_t pin);
  158. void serial2_set_rx(uint8_t pin);
  159. void serial2_set_tx(uint8_t pin, uint8_t opendrain);
  160. int serial2_set_rts(uint8_t pin);
  161. int serial2_set_cts(uint8_t pin);
  162. void serial2_putchar(uint32_t c);
  163. void serial2_write(const void *buf, unsigned int count);
  164. void serial2_flush(void);
  165. int serial2_write_buffer_free(void);
  166. int serial2_available(void);
  167. int serial2_getchar(void);
  168. int serial2_peek(void);
  169. void serial2_clear(void);
  170. void serial3_begin(uint32_t divisor);
  171. void serial3_format(uint32_t format);
  172. void serial3_end(void);
  173. void serial3_set_transmit_pin(uint8_t pin);
  174. void serial3_set_rx(uint8_t pin);
  175. void serial3_set_tx(uint8_t pin, uint8_t opendrain);
  176. int serial3_set_rts(uint8_t pin);
  177. int serial3_set_cts(uint8_t pin);
  178. void serial3_putchar(uint32_t c);
  179. void serial3_write(const void *buf, unsigned int count);
  180. void serial3_flush(void);
  181. int serial3_write_buffer_free(void);
  182. int serial3_available(void);
  183. int serial3_getchar(void);
  184. int serial3_peek(void);
  185. void serial3_clear(void);
  186. void serial4_begin(uint32_t divisor);
  187. void serial4_format(uint32_t format);
  188. void serial4_end(void);
  189. void serial4_set_transmit_pin(uint8_t pin);
  190. void serial4_set_rx(uint8_t pin);
  191. void serial4_set_tx(uint8_t pin, uint8_t opendrain);
  192. int serial4_set_rts(uint8_t pin);
  193. int serial4_set_cts(uint8_t pin);
  194. void serial4_putchar(uint32_t c);
  195. void serial4_write(const void *buf, unsigned int count);
  196. void serial4_flush(void);
  197. int serial4_write_buffer_free(void);
  198. int serial4_available(void);
  199. int serial4_getchar(void);
  200. int serial4_peek(void);
  201. void serial4_clear(void);
  202. void serial5_begin(uint32_t divisor);
  203. void serial5_format(uint32_t format);
  204. void serial5_end(void);
  205. void serial5_set_transmit_pin(uint8_t pin);
  206. void serial5_set_rx(uint8_t pin);
  207. void serial5_set_tx(uint8_t pin, uint8_t opendrain);
  208. int serial5_set_rts(uint8_t pin);
  209. int serial5_set_cts(uint8_t pin);
  210. void serial5_putchar(uint32_t c);
  211. void serial5_write(const void *buf, unsigned int count);
  212. void serial5_flush(void);
  213. int serial5_write_buffer_free(void);
  214. int serial5_available(void);
  215. int serial5_getchar(void);
  216. int serial5_peek(void);
  217. void serial5_clear(void);
  218. void serial6_begin(uint32_t divisor);
  219. void serial6_format(uint32_t format);
  220. void serial6_end(void);
  221. void serial6_set_transmit_pin(uint8_t pin);
  222. void serial6_set_rx(uint8_t pin);
  223. void serial6_set_tx(uint8_t pin, uint8_t opendrain);
  224. int serial6_set_rts(uint8_t pin);
  225. int serial6_set_cts(uint8_t pin);
  226. void serial6_putchar(uint32_t c);
  227. void serial6_write(const void *buf, unsigned int count);
  228. void serial6_flush(void);
  229. int serial6_write_buffer_free(void);
  230. int serial6_available(void);
  231. int serial6_getchar(void);
  232. int serial6_peek(void);
  233. void serial6_clear(void);
  234. #ifdef __cplusplus
  235. }
  236. #endif
  237. // C++ interface
  238. //
  239. #ifdef __cplusplus
  240. #include "Stream.h"
  241. class HardwareSerial : public Stream
  242. {
  243. public:
  244. constexpr HardwareSerial(void (* const se)()) : _serialEvent(se) {}
  245. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  246. enum {CNT_HARDWARE_SERIAL = 6};
  247. #else //(__MK64FX512__) || defined(__MK66FX1M0__)
  248. enum {CNT_HARDWARE_SERIAL = 3};
  249. #endif
  250. virtual void begin(uint32_t baud);
  251. virtual void begin(uint32_t baud, uint32_t format) {
  252. serial_begin(BAUD2DIV(baud));
  253. serial_format(format); }
  254. virtual void end(void) { serial_end(); }
  255. virtual void transmitterEnable(uint8_t pin) { serial_set_transmit_pin(pin); }
  256. virtual void setRX(uint8_t pin) { serial_set_rx(pin); }
  257. virtual void setTX(uint8_t pin, bool opendrain=false) { serial_set_tx(pin, opendrain); }
  258. virtual bool attachRts(uint8_t pin) { return serial_set_rts(pin); }
  259. virtual bool attachCts(uint8_t pin) { return serial_set_cts(pin); }
  260. virtual int available(void) { return serial_available(); }
  261. virtual int peek(void) { return serial_peek(); }
  262. virtual int read(void) { return serial_getchar(); }
  263. virtual void flush(void) { serial_flush(); }
  264. virtual void clear(void) { serial_clear(); }
  265. virtual int availableForWrite(void) { return serial_write_buffer_free(); }
  266. using Print::write;
  267. virtual size_t write(uint8_t c) { serial_putchar(c); return 1; }
  268. virtual size_t write(unsigned long n) { return write((uint8_t)n); }
  269. virtual size_t write(long n) { return write((uint8_t)n); }
  270. virtual size_t write(unsigned int n) { return write((uint8_t)n); }
  271. virtual size_t write(int n) { return write((uint8_t)n); }
  272. virtual size_t write(const uint8_t *buffer, size_t size)
  273. { serial_write(buffer, size); return size; }
  274. virtual size_t write(const char *str) { size_t len = strlen(str);
  275. serial_write((const uint8_t *)str, len);
  276. return len; }
  277. virtual size_t write9bit(uint32_t c) { serial_putchar(c); return 1; }
  278. operator bool() { return true; }
  279. static inline void processSerialEventsList() {
  280. for (uint8_t i = 0; i < s_count_serials_with_serial_events; i++) {
  281. s_serials_with_serial_events[i]->doYieldCode();
  282. }
  283. }
  284. protected:
  285. static HardwareSerial *s_serials_with_serial_events[CNT_HARDWARE_SERIAL];
  286. static uint8_t s_count_serials_with_serial_events;
  287. void (* const _serialEvent)();
  288. void addToSerialEventsList();
  289. inline void doYieldCode() {
  290. if (available()) (*_serialEvent)();
  291. }
  292. };
  293. extern HardwareSerial Serial1;
  294. extern void serialEvent1(void);
  295. class HardwareSerial2 : public HardwareSerial
  296. {
  297. public:
  298. constexpr HardwareSerial2(void (* const se)()) : HardwareSerial(se) {}
  299. virtual void begin(uint32_t baud);
  300. virtual void begin(uint32_t baud, uint32_t format) {
  301. serial2_begin(BAUD2DIV2(baud));
  302. serial2_format(format); }
  303. virtual void end(void) { serial2_end(); }
  304. virtual void transmitterEnable(uint8_t pin) { serial2_set_transmit_pin(pin); }
  305. virtual void setRX(uint8_t pin) { serial2_set_rx(pin); }
  306. virtual void setTX(uint8_t pin, bool opendrain=false) { serial2_set_tx(pin, opendrain); }
  307. virtual bool attachRts(uint8_t pin) { return serial2_set_rts(pin); }
  308. virtual bool attachCts(uint8_t pin) { return serial2_set_cts(pin); }
  309. virtual int available(void) { return serial2_available(); }
  310. virtual int peek(void) { return serial2_peek(); }
  311. virtual int read(void) { return serial2_getchar(); }
  312. virtual void flush(void) { serial2_flush(); }
  313. virtual void clear(void) { serial2_clear(); }
  314. virtual int availableForWrite(void) { return serial2_write_buffer_free(); }
  315. using Print::write;
  316. virtual size_t write(uint8_t c) { serial2_putchar(c); return 1; }
  317. virtual size_t write(unsigned long n) { return write((uint8_t)n); }
  318. virtual size_t write(long n) { return write((uint8_t)n); }
  319. virtual size_t write(unsigned int n) { return write((uint8_t)n); }
  320. virtual size_t write(int n) { return write((uint8_t)n); }
  321. virtual size_t write(const uint8_t *buffer, size_t size)
  322. { serial2_write(buffer, size); return size; }
  323. virtual size_t write(const char *str) { size_t len = strlen(str);
  324. serial2_write((const uint8_t *)str, len);
  325. return len; }
  326. virtual size_t write9bit(uint32_t c) { serial2_putchar(c); return 1; }
  327. operator bool() { return true; }
  328. };
  329. extern HardwareSerial2 Serial2;
  330. extern void serialEvent2(void);
  331. class HardwareSerial3 : public HardwareSerial
  332. {
  333. public:
  334. constexpr HardwareSerial3(void (* const se)()) : HardwareSerial(se) {}
  335. virtual void begin(uint32_t baud);
  336. virtual void begin(uint32_t baud, uint32_t format) {
  337. serial3_begin(BAUD2DIV3(baud));
  338. serial3_format(format); }
  339. virtual void end(void) { serial3_end(); }
  340. virtual void transmitterEnable(uint8_t pin) { serial3_set_transmit_pin(pin); }
  341. virtual void setRX(uint8_t pin) { serial3_set_rx(pin); }
  342. virtual void setTX(uint8_t pin, bool opendrain=false) { serial3_set_tx(pin, opendrain); }
  343. virtual bool attachRts(uint8_t pin) { return serial3_set_rts(pin); }
  344. virtual bool attachCts(uint8_t pin) { return serial3_set_cts(pin); }
  345. virtual int available(void) { return serial3_available(); }
  346. virtual int peek(void) { return serial3_peek(); }
  347. virtual int read(void) { return serial3_getchar(); }
  348. virtual void flush(void) { serial3_flush(); }
  349. virtual void clear(void) { serial3_clear(); }
  350. virtual int availableForWrite(void) { return serial3_write_buffer_free(); }
  351. using Print::write;
  352. virtual size_t write(uint8_t c) { serial3_putchar(c); return 1; }
  353. virtual size_t write(unsigned long n) { return write((uint8_t)n); }
  354. virtual size_t write(long n) { return write((uint8_t)n); }
  355. virtual size_t write(unsigned int n) { return write((uint8_t)n); }
  356. virtual size_t write(int n) { return write((uint8_t)n); }
  357. virtual size_t write(const uint8_t *buffer, size_t size)
  358. { serial3_write(buffer, size); return size; }
  359. virtual size_t write(const char *str) { size_t len = strlen(str);
  360. serial3_write((const uint8_t *)str, len);
  361. return len; }
  362. virtual size_t write9bit(uint32_t c) { serial3_putchar(c); return 1; }
  363. operator bool() { return true; }
  364. };
  365. extern HardwareSerial3 Serial3;
  366. extern void serialEvent3(void);
  367. class HardwareSerial4 : public HardwareSerial
  368. {
  369. public:
  370. constexpr HardwareSerial4(void (* const se)()) : HardwareSerial(se) {}
  371. virtual void begin(uint32_t baud);
  372. virtual void begin(uint32_t baud, uint32_t format) {
  373. serial4_begin(BAUD2DIV3(baud));
  374. serial4_format(format); }
  375. virtual void end(void) { serial4_end(); }
  376. virtual void transmitterEnable(uint8_t pin) { serial4_set_transmit_pin(pin); }
  377. virtual void setRX(uint8_t pin) { serial4_set_rx(pin); }
  378. virtual void setTX(uint8_t pin, bool opendrain=false) { serial4_set_tx(pin, opendrain); }
  379. virtual bool attachRts(uint8_t pin) { return serial4_set_rts(pin); }
  380. virtual bool attachCts(uint8_t pin) { return serial4_set_cts(pin); }
  381. virtual int available(void) { return serial4_available(); }
  382. virtual int peek(void) { return serial4_peek(); }
  383. virtual int read(void) { return serial4_getchar(); }
  384. virtual void flush(void) { serial4_flush(); }
  385. virtual void clear(void) { serial4_clear(); }
  386. virtual int availableForWrite(void) { return serial4_write_buffer_free(); }
  387. using Print::write;
  388. virtual size_t write(uint8_t c) { serial4_putchar(c); return 1; }
  389. virtual size_t write(unsigned long n) { return write((uint8_t)n); }
  390. virtual size_t write(long n) { return write((uint8_t)n); }
  391. virtual size_t write(unsigned int n) { return write((uint8_t)n); }
  392. virtual size_t write(int n) { return write((uint8_t)n); }
  393. virtual size_t write(const uint8_t *buffer, size_t size)
  394. { serial4_write(buffer, size); return size; }
  395. virtual size_t write(const char *str) { size_t len = strlen(str);
  396. serial4_write((const uint8_t *)str, len);
  397. return len; }
  398. virtual size_t write9bit(uint32_t c) { serial4_putchar(c); return 1; }
  399. operator bool() { return true; }
  400. };
  401. extern HardwareSerial4 Serial4;
  402. extern void serialEvent4(void);
  403. class HardwareSerial5 : public HardwareSerial
  404. {
  405. public:
  406. constexpr HardwareSerial5(void (* const se)()) : HardwareSerial(se) {}
  407. virtual void begin(uint32_t baud);
  408. virtual void begin(uint32_t baud, uint32_t format) {
  409. serial5_begin(BAUD2DIV3(baud));
  410. serial5_format(format); }
  411. virtual void end(void) { serial5_end(); }
  412. virtual void transmitterEnable(uint8_t pin) { serial5_set_transmit_pin(pin); }
  413. virtual void setRX(uint8_t pin) { serial5_set_rx(pin); }
  414. virtual void setTX(uint8_t pin, bool opendrain=false) { serial5_set_tx(pin, opendrain); }
  415. virtual bool attachRts(uint8_t pin) { return serial5_set_rts(pin); }
  416. virtual bool attachCts(uint8_t pin) { return serial5_set_cts(pin); }
  417. virtual int available(void) { return serial5_available(); }
  418. virtual int peek(void) { return serial5_peek(); }
  419. virtual int read(void) { return serial5_getchar(); }
  420. virtual void flush(void) { serial5_flush(); }
  421. virtual void clear(void) { serial5_clear(); }
  422. virtual int availableForWrite(void) { return serial5_write_buffer_free(); }
  423. using Print::write;
  424. virtual size_t write(uint8_t c) { serial5_putchar(c); return 1; }
  425. virtual size_t write(unsigned long n) { return write((uint8_t)n); }
  426. virtual size_t write(long n) { return write((uint8_t)n); }
  427. virtual size_t write(unsigned int n) { return write((uint8_t)n); }
  428. virtual size_t write(int n) { return write((uint8_t)n); }
  429. virtual size_t write(const uint8_t *buffer, size_t size)
  430. { serial5_write(buffer, size); return size; }
  431. virtual size_t write(const char *str) { size_t len = strlen(str);
  432. serial5_write((const uint8_t *)str, len);
  433. return len; }
  434. virtual size_t write9bit(uint32_t c) { serial5_putchar(c); return 1; }
  435. operator bool() { return true; }
  436. };
  437. extern HardwareSerial5 Serial5;
  438. extern void serialEvent5(void);
  439. class HardwareSerial6 : public HardwareSerial
  440. {
  441. public:
  442. constexpr HardwareSerial6(void (* const se)()) : HardwareSerial(se) {}
  443. #if defined(__MK66FX1M0__) // For LPUART just pass baud straight in.
  444. virtual void begin(uint32_t baud);
  445. virtual void begin(uint32_t baud, uint32_t format) {
  446. serial6_begin(baud);
  447. serial6_format(format); }
  448. #else
  449. virtual void begin(uint32_t baud);
  450. virtual void begin(uint32_t baud, uint32_t format) {
  451. serial6_begin(BAUD2DIV3(baud));
  452. serial6_format(format); }
  453. #endif
  454. virtual void end(void) { serial6_end(); }
  455. virtual void transmitterEnable(uint8_t pin) { serial6_set_transmit_pin(pin); }
  456. virtual void setRX(uint8_t pin) { serial6_set_rx(pin); }
  457. virtual void setTX(uint8_t pin, bool opendrain=false) { serial6_set_tx(pin, opendrain); }
  458. virtual bool attachRts(uint8_t pin) { return serial6_set_rts(pin); }
  459. virtual bool attachCts(uint8_t pin) { return serial6_set_cts(pin); }
  460. virtual int available(void) { return serial6_available(); }
  461. virtual int peek(void) { return serial6_peek(); }
  462. virtual int read(void) { return serial6_getchar(); }
  463. virtual void flush(void) { serial6_flush(); }
  464. virtual void clear(void) { serial6_clear(); }
  465. virtual int availableForWrite(void) { return serial6_write_buffer_free(); }
  466. using Print::write;
  467. virtual size_t write(uint8_t c) { serial6_putchar(c); return 1; }
  468. virtual size_t write(unsigned long n) { return write((uint8_t)n); }
  469. virtual size_t write(long n) { return write((uint8_t)n); }
  470. virtual size_t write(unsigned int n) { return write((uint8_t)n); }
  471. virtual size_t write(int n) { return write((uint8_t)n); }
  472. virtual size_t write(const uint8_t *buffer, size_t size)
  473. { serial6_write(buffer, size); return size; }
  474. virtual size_t write(const char *str) { size_t len = strlen(str);
  475. serial6_write((const uint8_t *)str, len);
  476. return len; }
  477. virtual size_t write9bit(uint32_t c) { serial6_putchar(c); return 1; }
  478. operator bool() { return true; }
  479. };
  480. extern HardwareSerial6 Serial6;
  481. extern void serialEvent6(void);
  482. #endif
  483. #endif