Teensy 4.1 core updated for C++20
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

507 líneas
20KB

  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(void (* const se)()) : _serialEvent(se) {}
  238. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  239. enum {CNT_HARDWARE_SERIAL = 6};
  240. #else //(__MK64FX512__) || defined(__MK66FX1M0__)
  241. enum {CNT_HARDWARE_SERIAL = 3};
  242. #endif
  243. virtual void begin(uint32_t baud);
  244. virtual void begin(uint32_t baud, uint32_t format) {
  245. serial_begin(BAUD2DIV(baud));
  246. serial_format(format); }
  247. virtual void end(void) { serial_end(); }
  248. virtual void transmitterEnable(uint8_t pin) { serial_set_transmit_pin(pin); }
  249. virtual void setRX(uint8_t pin) { serial_set_rx(pin); }
  250. virtual void setTX(uint8_t pin, bool opendrain=false) { serial_set_tx(pin, opendrain); }
  251. virtual bool attachRts(uint8_t pin) { return serial_set_rts(pin); }
  252. virtual bool attachCts(uint8_t pin) { return serial_set_cts(pin); }
  253. virtual int available(void) { return serial_available(); }
  254. virtual int peek(void) { return serial_peek(); }
  255. virtual int read(void) { return serial_getchar(); }
  256. virtual void flush(void) { serial_flush(); }
  257. virtual void clear(void) { serial_clear(); }
  258. virtual int availableForWrite(void) { return serial_write_buffer_free(); }
  259. using Print::write;
  260. virtual size_t write(uint8_t c) { serial_putchar(c); return 1; }
  261. virtual size_t write(unsigned long n) { return write((uint8_t)n); }
  262. virtual size_t write(long n) { return write((uint8_t)n); }
  263. virtual size_t write(unsigned int n) { return write((uint8_t)n); }
  264. virtual size_t write(int n) { return write((uint8_t)n); }
  265. virtual size_t write(const uint8_t *buffer, size_t size)
  266. { serial_write(buffer, size); return size; }
  267. virtual size_t write(const char *str) { size_t len = strlen(str);
  268. serial_write((const uint8_t *)str, len);
  269. return len; }
  270. virtual size_t write9bit(uint32_t c) { serial_putchar(c); return 1; }
  271. operator bool() { return true; }
  272. static inline void processSerialEventsList() {
  273. for (uint8_t i = 0; i < s_count_serials_with_serial_events; i++) {
  274. s_serials_with_serial_events[i]->doYieldCode();
  275. }
  276. }
  277. protected:
  278. static HardwareSerial *s_serials_with_serial_events[CNT_HARDWARE_SERIAL];
  279. static uint8_t s_count_serials_with_serial_events;
  280. void (* const _serialEvent)();
  281. void addToSerialEventsList();
  282. inline void doYieldCode() {
  283. if (available()) (*_serialEvent)();
  284. }
  285. };
  286. extern HardwareSerial Serial1;
  287. extern void serialEvent1(void);
  288. class HardwareSerial2 : public HardwareSerial
  289. {
  290. public:
  291. constexpr HardwareSerial2(void (* const se)()) : HardwareSerial(se) {}
  292. virtual void begin(uint32_t baud);
  293. virtual void begin(uint32_t baud, uint32_t format) {
  294. serial2_begin(BAUD2DIV2(baud));
  295. serial2_format(format); }
  296. virtual void end(void) { serial2_end(); }
  297. virtual void transmitterEnable(uint8_t pin) { serial2_set_transmit_pin(pin); }
  298. virtual void setRX(uint8_t pin) { serial2_set_rx(pin); }
  299. virtual void setTX(uint8_t pin, bool opendrain=false) { serial2_set_tx(pin, opendrain); }
  300. virtual bool attachRts(uint8_t pin) { return serial2_set_rts(pin); }
  301. virtual bool attachCts(uint8_t pin) { return serial2_set_cts(pin); }
  302. virtual int available(void) { return serial2_available(); }
  303. virtual int peek(void) { return serial2_peek(); }
  304. virtual int read(void) { return serial2_getchar(); }
  305. virtual void flush(void) { serial2_flush(); }
  306. virtual void clear(void) { serial2_clear(); }
  307. virtual int availableForWrite(void) { return serial2_write_buffer_free(); }
  308. using Print::write;
  309. virtual size_t write(uint8_t c) { serial2_putchar(c); return 1; }
  310. virtual size_t write(unsigned long n) { return write((uint8_t)n); }
  311. virtual size_t write(long n) { return write((uint8_t)n); }
  312. virtual size_t write(unsigned int n) { return write((uint8_t)n); }
  313. virtual size_t write(int n) { return write((uint8_t)n); }
  314. virtual size_t write(const uint8_t *buffer, size_t size)
  315. { serial2_write(buffer, size); return size; }
  316. virtual size_t write(const char *str) { size_t len = strlen(str);
  317. serial2_write((const uint8_t *)str, len);
  318. return len; }
  319. virtual size_t write9bit(uint32_t c) { serial2_putchar(c); return 1; }
  320. operator bool() { return true; }
  321. };
  322. extern HardwareSerial2 Serial2;
  323. extern void serialEvent2(void);
  324. class HardwareSerial3 : public HardwareSerial
  325. {
  326. public:
  327. constexpr HardwareSerial3(void (* const se)()) : HardwareSerial(se) {}
  328. virtual void begin(uint32_t baud);
  329. virtual void begin(uint32_t baud, uint32_t format) {
  330. serial3_begin(BAUD2DIV3(baud));
  331. serial3_format(format); }
  332. virtual void end(void) { serial3_end(); }
  333. virtual void transmitterEnable(uint8_t pin) { serial3_set_transmit_pin(pin); }
  334. virtual void setRX(uint8_t pin) { serial3_set_rx(pin); }
  335. virtual void setTX(uint8_t pin, bool opendrain=false) { serial3_set_tx(pin, opendrain); }
  336. virtual bool attachRts(uint8_t pin) { return serial3_set_rts(pin); }
  337. virtual bool attachCts(uint8_t pin) { return serial3_set_cts(pin); }
  338. virtual int available(void) { return serial3_available(); }
  339. virtual int peek(void) { return serial3_peek(); }
  340. virtual int read(void) { return serial3_getchar(); }
  341. virtual void flush(void) { serial3_flush(); }
  342. virtual void clear(void) { serial3_clear(); }
  343. virtual int availableForWrite(void) { return serial3_write_buffer_free(); }
  344. using Print::write;
  345. virtual size_t write(uint8_t c) { serial3_putchar(c); return 1; }
  346. virtual size_t write(unsigned long n) { return write((uint8_t)n); }
  347. virtual size_t write(long n) { return write((uint8_t)n); }
  348. virtual size_t write(unsigned int n) { return write((uint8_t)n); }
  349. virtual size_t write(int n) { return write((uint8_t)n); }
  350. virtual size_t write(const uint8_t *buffer, size_t size)
  351. { serial3_write(buffer, size); return size; }
  352. virtual size_t write(const char *str) { size_t len = strlen(str);
  353. serial3_write((const uint8_t *)str, len);
  354. return len; }
  355. virtual size_t write9bit(uint32_t c) { serial3_putchar(c); return 1; }
  356. operator bool() { return true; }
  357. };
  358. extern HardwareSerial3 Serial3;
  359. extern void serialEvent3(void);
  360. class HardwareSerial4 : public HardwareSerial
  361. {
  362. public:
  363. constexpr HardwareSerial4(void (* const se)()) : HardwareSerial(se) {}
  364. virtual void begin(uint32_t baud);
  365. virtual void begin(uint32_t baud, uint32_t format) {
  366. serial4_begin(BAUD2DIV3(baud));
  367. serial4_format(format); }
  368. virtual void end(void) { serial4_end(); }
  369. virtual void transmitterEnable(uint8_t pin) { serial4_set_transmit_pin(pin); }
  370. virtual void setRX(uint8_t pin) { serial4_set_rx(pin); }
  371. virtual void setTX(uint8_t pin, bool opendrain=false) { serial4_set_tx(pin, opendrain); }
  372. virtual bool attachRts(uint8_t pin) { return serial4_set_rts(pin); }
  373. virtual bool attachCts(uint8_t pin) { return serial4_set_cts(pin); }
  374. virtual int available(void) { return serial4_available(); }
  375. virtual int peek(void) { return serial4_peek(); }
  376. virtual int read(void) { return serial4_getchar(); }
  377. virtual void flush(void) { serial4_flush(); }
  378. virtual void clear(void) { serial4_clear(); }
  379. virtual int availableForWrite(void) { return serial4_write_buffer_free(); }
  380. using Print::write;
  381. virtual size_t write(uint8_t c) { serial4_putchar(c); return 1; }
  382. virtual size_t write(unsigned long n) { return write((uint8_t)n); }
  383. virtual size_t write(long n) { return write((uint8_t)n); }
  384. virtual size_t write(unsigned int n) { return write((uint8_t)n); }
  385. virtual size_t write(int n) { return write((uint8_t)n); }
  386. virtual size_t write(const uint8_t *buffer, size_t size)
  387. { serial4_write(buffer, size); return size; }
  388. virtual size_t write(const char *str) { size_t len = strlen(str);
  389. serial4_write((const uint8_t *)str, len);
  390. return len; }
  391. virtual size_t write9bit(uint32_t c) { serial4_putchar(c); return 1; }
  392. operator bool() { return true; }
  393. };
  394. extern HardwareSerial4 Serial4;
  395. extern void serialEvent4(void);
  396. class HardwareSerial5 : public HardwareSerial
  397. {
  398. public:
  399. constexpr HardwareSerial5(void (* const se)()) : HardwareSerial(se) {}
  400. virtual void begin(uint32_t baud);
  401. virtual void begin(uint32_t baud, uint32_t format) {
  402. serial5_begin(BAUD2DIV3(baud));
  403. serial5_format(format); }
  404. virtual void end(void) { serial5_end(); }
  405. virtual void transmitterEnable(uint8_t pin) { serial5_set_transmit_pin(pin); }
  406. virtual void setRX(uint8_t pin) { serial5_set_rx(pin); }
  407. virtual void setTX(uint8_t pin, bool opendrain=false) { serial5_set_tx(pin, opendrain); }
  408. virtual bool attachRts(uint8_t pin) { return serial5_set_rts(pin); }
  409. virtual bool attachCts(uint8_t pin) { return serial5_set_cts(pin); }
  410. virtual int available(void) { return serial5_available(); }
  411. virtual int peek(void) { return serial5_peek(); }
  412. virtual int read(void) { return serial5_getchar(); }
  413. virtual void flush(void) { serial5_flush(); }
  414. virtual void clear(void) { serial5_clear(); }
  415. virtual int availableForWrite(void) { return serial5_write_buffer_free(); }
  416. using Print::write;
  417. virtual size_t write(uint8_t c) { serial5_putchar(c); return 1; }
  418. virtual size_t write(unsigned long n) { return write((uint8_t)n); }
  419. virtual size_t write(long n) { return write((uint8_t)n); }
  420. virtual size_t write(unsigned int n) { return write((uint8_t)n); }
  421. virtual size_t write(int n) { return write((uint8_t)n); }
  422. virtual size_t write(const uint8_t *buffer, size_t size)
  423. { serial5_write(buffer, size); return size; }
  424. virtual size_t write(const char *str) { size_t len = strlen(str);
  425. serial5_write((const uint8_t *)str, len);
  426. return len; }
  427. virtual size_t write9bit(uint32_t c) { serial5_putchar(c); return 1; }
  428. operator bool() { return true; }
  429. };
  430. extern HardwareSerial5 Serial5;
  431. extern void serialEvent5(void);
  432. class HardwareSerial6 : public HardwareSerial
  433. {
  434. public:
  435. constexpr HardwareSerial6(void (* const se)()) : HardwareSerial(se) {}
  436. #if defined(__MK66FX1M0__) // For LPUART just pass baud straight in.
  437. virtual void begin(uint32_t baud);
  438. virtual void begin(uint32_t baud, uint32_t format) {
  439. serial6_begin(baud);
  440. serial6_format(format); }
  441. #else
  442. virtual void begin(uint32_t baud);
  443. virtual void begin(uint32_t baud, uint32_t format) {
  444. serial6_begin(BAUD2DIV3(baud));
  445. serial6_format(format); }
  446. #endif
  447. virtual void end(void) { serial6_end(); }
  448. virtual void transmitterEnable(uint8_t pin) { serial6_set_transmit_pin(pin); }
  449. virtual void setRX(uint8_t pin) { serial6_set_rx(pin); }
  450. virtual void setTX(uint8_t pin, bool opendrain=false) { serial6_set_tx(pin, opendrain); }
  451. virtual bool attachRts(uint8_t pin) { return serial6_set_rts(pin); }
  452. virtual bool attachCts(uint8_t pin) { return serial6_set_cts(pin); }
  453. virtual int available(void) { return serial6_available(); }
  454. virtual int peek(void) { return serial6_peek(); }
  455. virtual int read(void) { return serial6_getchar(); }
  456. virtual void flush(void) { serial6_flush(); }
  457. virtual void clear(void) { serial6_clear(); }
  458. virtual int availableForWrite(void) { return serial6_write_buffer_free(); }
  459. using Print::write;
  460. virtual size_t write(uint8_t c) { serial6_putchar(c); return 1; }
  461. virtual size_t write(unsigned long n) { return write((uint8_t)n); }
  462. virtual size_t write(long n) { return write((uint8_t)n); }
  463. virtual size_t write(unsigned int n) { return write((uint8_t)n); }
  464. virtual size_t write(int n) { return write((uint8_t)n); }
  465. virtual size_t write(const uint8_t *buffer, size_t size)
  466. { serial6_write(buffer, size); return size; }
  467. virtual size_t write(const char *str) { size_t len = strlen(str);
  468. serial6_write((const uint8_t *)str, len);
  469. return len; }
  470. virtual size_t write9bit(uint32_t c) { serial6_putchar(c); return 1; }
  471. operator bool() { return true; }
  472. };
  473. extern HardwareSerial6 Serial6;
  474. extern void serialEvent6(void);
  475. #endif
  476. #endif