Teensy 4.1 core updated for C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

532 satır
22KB

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