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.

159 lines
5.9KB

  1. /* Teensyduino Core Library
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2013 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 "mk20dx128.h"
  33. #include <inttypes.h>
  34. #define BAUD2DIV(baud) (((F_CPU * 2) + ((baud) >> 1)) / (baud))
  35. #define BAUD2DIV3(baud) (((F_BUS * 2) + ((baud) >> 1)) / (baud))
  36. // C language implementation
  37. //
  38. #ifdef __cplusplus
  39. extern "C" {
  40. #endif
  41. void serial_begin(uint32_t divisor);
  42. void serial_end(void);
  43. void serial_putchar(uint8_t c);
  44. void serial_write(const void *buf, unsigned int count);
  45. void serial_flush(void);
  46. int serial_available(void);
  47. int serial_getchar(void);
  48. int serial_peek(void);
  49. void serial_clear(void);
  50. void serial_print(const char *p);
  51. void serial_phex(uint32_t n);
  52. void serial_phex16(uint32_t n);
  53. void serial_phex32(uint32_t n);
  54. void serial2_begin(uint32_t divisor);
  55. void serial2_end(void);
  56. void serial2_putchar(uint8_t c);
  57. void serial2_write(const void *buf, unsigned int count);
  58. void serial2_flush(void);
  59. int serial2_available(void);
  60. int serial2_getchar(void);
  61. int serial2_peek(void);
  62. void serial2_clear(void);
  63. void serial3_begin(uint32_t divisor);
  64. void serial3_end(void);
  65. void serial3_putchar(uint8_t c);
  66. void serial3_write(const void *buf, unsigned int count);
  67. void serial3_flush(void);
  68. int serial3_available(void);
  69. int serial3_getchar(void);
  70. int serial3_peek(void);
  71. void serial3_clear(void);
  72. #ifdef __cplusplus
  73. }
  74. #endif
  75. // C++ interface
  76. //
  77. #ifdef __cplusplus
  78. #include "Stream.h"
  79. class HardwareSerial : public Stream
  80. {
  81. public:
  82. void begin(uint32_t baud) { serial_begin(BAUD2DIV(baud)); }
  83. void end(void) { serial_end(); }
  84. virtual int available(void) { return serial_available(); }
  85. virtual int peek(void) { return serial_peek(); }
  86. virtual int read(void) { return serial_getchar(); }
  87. virtual void flush(void) { serial_flush(); }
  88. void clear(void) { serial_clear(); }
  89. virtual size_t write(uint8_t c) { serial_putchar(c); return 1; }
  90. size_t write(unsigned long n) { return write((uint8_t)n); }
  91. size_t write(long n) { return write((uint8_t)n); }
  92. size_t write(unsigned int n) { return write((uint8_t)n); }
  93. size_t write(int n) { return write((uint8_t)n); }
  94. virtual size_t write(const uint8_t *buffer, size_t size)
  95. { serial_write(buffer, size); return size; }
  96. size_t write(const char *str) { size_t len = strlen(str);
  97. serial_write((const uint8_t *)str, len);
  98. return len; }
  99. };
  100. extern HardwareSerial Serial1;
  101. class HardwareSerial2 : public HardwareSerial
  102. {
  103. public:
  104. void begin(uint32_t baud) { serial2_begin(BAUD2DIV(baud)); }
  105. void end(void) { serial2_end(); }
  106. virtual int available(void) { return serial2_available(); }
  107. virtual int peek(void) { return serial2_peek(); }
  108. virtual int read(void) { return serial2_getchar(); }
  109. virtual void flush(void) { serial2_flush(); }
  110. void clear(void) { serial2_clear(); }
  111. virtual size_t write(uint8_t c) { serial2_putchar(c); return 1; }
  112. size_t write(unsigned long n) { return write((uint8_t)n); }
  113. size_t write(long n) { return write((uint8_t)n); }
  114. size_t write(unsigned int n) { return write((uint8_t)n); }
  115. size_t write(int n) { return write((uint8_t)n); }
  116. virtual size_t write(const uint8_t *buffer, size_t size)
  117. { serial2_write(buffer, size); return size; }
  118. size_t write(const char *str) { size_t len = strlen(str);
  119. serial2_write((const uint8_t *)str, len);
  120. return len; }
  121. };
  122. extern HardwareSerial2 Serial2;
  123. class HardwareSerial3 : public HardwareSerial
  124. {
  125. public:
  126. void begin(uint32_t baud) { serial3_begin(BAUD2DIV3(baud)); }
  127. void end(void) { serial3_end(); }
  128. virtual int available(void) { return serial3_available(); }
  129. virtual int peek(void) { return serial3_peek(); }
  130. virtual int read(void) { return serial3_getchar(); }
  131. virtual void flush(void) { serial3_flush(); }
  132. void clear(void) { serial3_clear(); }
  133. virtual size_t write(uint8_t c) { serial3_putchar(c); return 1; }
  134. size_t write(unsigned long n) { return write((uint8_t)n); }
  135. size_t write(long n) { return write((uint8_t)n); }
  136. size_t write(unsigned int n) { return write((uint8_t)n); }
  137. size_t write(int n) { return write((uint8_t)n); }
  138. virtual size_t write(const uint8_t *buffer, size_t size)
  139. { serial3_write(buffer, size); return size; }
  140. size_t write(const char *str) { size_t len = strlen(str);
  141. serial3_write((const uint8_t *)str, len);
  142. return len; }
  143. };
  144. extern HardwareSerial3 Serial3;
  145. #endif
  146. #endif