Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

541 rinda
23KB

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