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.

228 lines
8.0KB

  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. // uncomment to enable 9 bit formats
  35. //#define SERIAL_9BIT_SUPPORT
  36. #define SERIAL_7E1 0x02
  37. #define SERIAL_7O1 0x03
  38. #define SERIAL_8N1 0x00
  39. #define SERIAL_8N2 0x04
  40. #define SERIAL_8E1 0x06
  41. #define SERIAL_8O1 0x07
  42. #define SERIAL_7E1_RXINV 0x12
  43. #define SERIAL_7O1_RXINV 0x13
  44. #define SERIAL_8N1_RXINV 0x10
  45. #define SERIAL_8N2_RXINV 0x14
  46. #define SERIAL_8E1_RXINV 0x16
  47. #define SERIAL_8O1_RXINV 0x17
  48. #define SERIAL_7E1_TXINV 0x22
  49. #define SERIAL_7O1_TXINV 0x23
  50. #define SERIAL_8N1_TXINV 0x20
  51. #define SERIAL_8N2_TXINV 0x24
  52. #define SERIAL_8E1_TXINV 0x26
  53. #define SERIAL_8O1_TXINV 0x27
  54. #define SERIAL_7E1_RXINV_TXINV 0x32
  55. #define SERIAL_7O1_RXINV_TXINV 0x33
  56. #define SERIAL_8N1_RXINV_TXINV 0x30
  57. #define SERIAL_8N2_RXINV_TXINV 0x34
  58. #define SERIAL_8E1_RXINV_TXINV 0x36
  59. #define SERIAL_8O1_RXINV_TXINV 0x37
  60. #ifdef SERIAL_9BIT_SUPPORT
  61. #define SERIAL_9N1 0x84
  62. #define SERIAL_9E1 0x8E
  63. #define SERIAL_9O1 0x8F
  64. #define SERIAL_9N1_RXINV 0x94
  65. #define SERIAL_9E1_RXINV 0x9E
  66. #define SERIAL_9O1_RXINV 0x9F
  67. #define SERIAL_9N1_TXINV 0xA4
  68. #define SERIAL_9E1_TXINV 0xAE
  69. #define SERIAL_9O1_TXINV 0xAF
  70. #define SERIAL_9N1_RXINV_TXINV 0xB4
  71. #define SERIAL_9E1_RXINV_TXINV 0xBE
  72. #define SERIAL_9O1_RXINV_TXINV 0xBF
  73. #endif
  74. // bit0: parity, 0=even, 1=odd
  75. // bit1: parity, 0=disable, 1=enable
  76. // bit2: mode, 1=9bit, 0=8bit
  77. // bit3: mode10: 1=10bit, 0=8bit
  78. // bit4: rxinv, 0=normal, 1=inverted
  79. // bit5: txinv, 0=normal, 1=inverted
  80. // bit6: unused
  81. // bit7: actual data goes into 9th bit
  82. #define BAUD2DIV(baud) (((F_CPU * 2) + ((baud) >> 1)) / (baud))
  83. #define BAUD2DIV3(baud) (((F_BUS * 2) + ((baud) >> 1)) / (baud))
  84. // C language implementation
  85. //
  86. #ifdef __cplusplus
  87. extern "C" {
  88. #endif
  89. void serial_begin(uint32_t divisor);
  90. void serial_format(uint32_t format);
  91. void serial_end(void);
  92. void serial_set_transmit_pin(uint8_t pin);
  93. void serial_putchar(uint32_t c);
  94. void serial_write(const void *buf, unsigned int count);
  95. void serial_flush(void);
  96. int serial_available(void);
  97. int serial_getchar(void);
  98. int serial_peek(void);
  99. void serial_clear(void);
  100. void serial_print(const char *p);
  101. void serial_phex(uint32_t n);
  102. void serial_phex16(uint32_t n);
  103. void serial_phex32(uint32_t n);
  104. void serial2_begin(uint32_t divisor);
  105. void serial2_format(uint32_t format);
  106. void serial2_end(void);
  107. void serial2_putchar(uint32_t c);
  108. void serial2_write(const void *buf, unsigned int count);
  109. void serial2_flush(void);
  110. int serial2_available(void);
  111. int serial2_getchar(void);
  112. int serial2_peek(void);
  113. void serial2_clear(void);
  114. void serial3_begin(uint32_t divisor);
  115. void serial3_format(uint32_t format);
  116. void serial3_end(void);
  117. void serial3_putchar(uint32_t c);
  118. void serial3_write(const void *buf, unsigned int count);
  119. void serial3_flush(void);
  120. int serial3_available(void);
  121. int serial3_getchar(void);
  122. int serial3_peek(void);
  123. void serial3_clear(void);
  124. #ifdef __cplusplus
  125. }
  126. #endif
  127. // C++ interface
  128. //
  129. #ifdef __cplusplus
  130. #include "Stream.h"
  131. class HardwareSerial : public Stream
  132. {
  133. public:
  134. virtual void begin(uint32_t baud) { serial_begin(BAUD2DIV(baud)); }
  135. virtual void begin(uint32_t baud, uint32_t format) {
  136. serial_begin(BAUD2DIV(baud));
  137. serial_format(format); }
  138. virtual void end(void) { serial_end(); }
  139. virtual void transmitterEnable(uint8_t pin) { serial_set_transmit_pin(pin); }
  140. virtual int available(void) { return serial_available(); }
  141. virtual int peek(void) { return serial_peek(); }
  142. virtual int read(void) { return serial_getchar(); }
  143. virtual void flush(void) { serial_flush(); }
  144. virtual void clear(void) { serial_clear(); }
  145. virtual size_t write(uint8_t c) { serial_putchar(c); return 1; }
  146. size_t write(unsigned long n) { return write((uint8_t)n); }
  147. size_t write(long n) { return write((uint8_t)n); }
  148. size_t write(unsigned int n) { return write((uint8_t)n); }
  149. size_t write(int n) { return write((uint8_t)n); }
  150. virtual size_t write(const uint8_t *buffer, size_t size)
  151. { serial_write(buffer, size); return size; }
  152. size_t write(const char *str) { size_t len = strlen(str);
  153. serial_write((const uint8_t *)str, len);
  154. return len; }
  155. size_t write9bit(uint32_t c) { serial_putchar(c); return 1; }
  156. };
  157. extern HardwareSerial Serial1;
  158. class HardwareSerial2 : public HardwareSerial
  159. {
  160. public:
  161. virtual void begin(uint32_t baud) { serial2_begin(BAUD2DIV(baud)); }
  162. virtual void begin(uint32_t baud, uint32_t format) {
  163. serial2_begin(BAUD2DIV(baud));
  164. serial2_format(format); }
  165. virtual void end(void) { serial2_end(); }
  166. virtual int available(void) { return serial2_available(); }
  167. virtual int peek(void) { return serial2_peek(); }
  168. virtual int read(void) { return serial2_getchar(); }
  169. virtual void flush(void) { serial2_flush(); }
  170. virtual void clear(void) { serial2_clear(); }
  171. virtual size_t write(uint8_t c) { serial2_putchar(c); return 1; }
  172. size_t write(unsigned long n) { return write((uint8_t)n); }
  173. size_t write(long n) { return write((uint8_t)n); }
  174. size_t write(unsigned int n) { return write((uint8_t)n); }
  175. size_t write(int n) { return write((uint8_t)n); }
  176. virtual size_t write(const uint8_t *buffer, size_t size)
  177. { serial2_write(buffer, size); return size; }
  178. size_t write(const char *str) { size_t len = strlen(str);
  179. serial2_write((const uint8_t *)str, len);
  180. return len; }
  181. size_t write9bit(uint32_t c) { serial2_putchar(c); return 1; }
  182. };
  183. extern HardwareSerial2 Serial2;
  184. class HardwareSerial3 : public HardwareSerial
  185. {
  186. public:
  187. virtual void begin(uint32_t baud) { serial3_begin(BAUD2DIV3(baud)); }
  188. virtual void begin(uint32_t baud, uint32_t format) {
  189. serial3_begin(BAUD2DIV3(baud));
  190. serial3_format(format); }
  191. virtual void end(void) { serial3_end(); }
  192. virtual int available(void) { return serial3_available(); }
  193. virtual int peek(void) { return serial3_peek(); }
  194. virtual int read(void) { return serial3_getchar(); }
  195. virtual void flush(void) { serial3_flush(); }
  196. virtual void clear(void) { serial3_clear(); }
  197. virtual size_t write(uint8_t c) { serial3_putchar(c); return 1; }
  198. size_t write(unsigned long n) { return write((uint8_t)n); }
  199. size_t write(long n) { return write((uint8_t)n); }
  200. size_t write(unsigned int n) { return write((uint8_t)n); }
  201. size_t write(int n) { return write((uint8_t)n); }
  202. virtual size_t write(const uint8_t *buffer, size_t size)
  203. { serial3_write(buffer, size); return size; }
  204. size_t write(const char *str) { size_t len = strlen(str);
  205. serial3_write((const uint8_t *)str, len);
  206. return len; }
  207. size_t write9bit(uint32_t c) { serial3_putchar(c); return 1; }
  208. };
  209. extern HardwareSerial3 Serial3;
  210. #endif
  211. #endif