Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

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