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.

преди 11 години
преди 10 години
преди 11 години
преди 10 години
преди 11 години
преди 11 години
преди 9 години
преди 11 години
преди 9 години
преди 11 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 9 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 9 години
преди 11 години
преди 11 години
преди 9 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 9 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 9 години
преди 11 години
преди 11 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 "kinetis.h"
  33. // uncomment to enable 9 bit formats
  34. //#define SERIAL_9BIT_SUPPORT
  35. #define SERIAL_7E1 0x02
  36. #define SERIAL_7O1 0x03
  37. #define SERIAL_8N1 0x00
  38. #define SERIAL_8N2 0x04
  39. #define SERIAL_8E1 0x06
  40. #define SERIAL_8O1 0x07
  41. #define SERIAL_7E1_RXINV 0x12
  42. #define SERIAL_7O1_RXINV 0x13
  43. #define SERIAL_8N1_RXINV 0x10
  44. #define SERIAL_8N2_RXINV 0x14
  45. #define SERIAL_8E1_RXINV 0x16
  46. #define SERIAL_8O1_RXINV 0x17
  47. #define SERIAL_7E1_TXINV 0x22
  48. #define SERIAL_7O1_TXINV 0x23
  49. #define SERIAL_8N1_TXINV 0x20
  50. #define SERIAL_8N2_TXINV 0x24
  51. #define SERIAL_8E1_TXINV 0x26
  52. #define SERIAL_8O1_TXINV 0x27
  53. #define SERIAL_7E1_RXINV_TXINV 0x32
  54. #define SERIAL_7O1_RXINV_TXINV 0x33
  55. #define SERIAL_8N1_RXINV_TXINV 0x30
  56. #define SERIAL_8N2_RXINV_TXINV 0x34
  57. #define SERIAL_8E1_RXINV_TXINV 0x36
  58. #define SERIAL_8O1_RXINV_TXINV 0x37
  59. #ifdef SERIAL_9BIT_SUPPORT
  60. #define SERIAL_9N1 0x84
  61. #define SERIAL_9E1 0x8E
  62. #define SERIAL_9O1 0x8F
  63. #define SERIAL_9N1_RXINV 0x94
  64. #define SERIAL_9E1_RXINV 0x9E
  65. #define SERIAL_9O1_RXINV 0x9F
  66. #define SERIAL_9N1_TXINV 0xA4
  67. #define SERIAL_9E1_TXINV 0xAE
  68. #define SERIAL_9O1_TXINV 0xAF
  69. #define SERIAL_9N1_RXINV_TXINV 0xB4
  70. #define SERIAL_9E1_RXINV_TXINV 0xBE
  71. #define SERIAL_9O1_RXINV_TXINV 0xBF
  72. #endif
  73. // bit0: parity, 0=even, 1=odd
  74. // bit1: parity, 0=disable, 1=enable
  75. // bit2: mode, 1=9bit, 0=8bit
  76. // bit3: mode10: 1=10bit, 0=8bit
  77. // bit4: rxinv, 0=normal, 1=inverted
  78. // bit5: txinv, 0=normal, 1=inverted
  79. // bit6: unused
  80. // bit7: actual data goes into 9th bit
  81. #if defined(KINETISK)
  82. #define BAUD2DIV(baud) (((F_CPU * 2) + ((baud) >> 1)) / (baud))
  83. #define BAUD2DIV2(baud) (((F_CPU * 2) + ((baud) >> 1)) / (baud))
  84. #define BAUD2DIV3(baud) (((F_BUS * 2) + ((baud) >> 1)) / (baud))
  85. #elif defined(KINETISL)
  86. #if F_CPU <= 2000000
  87. #define BAUD2DIV(baud) (((F_PLL / 16 ) + ((baud) >> 1)) / (baud))
  88. #elif F_CPU <= 16000000
  89. #define BAUD2DIV(baud) (((F_PLL / (F_PLL / 1000000)) + ((baud) >> 1)) / (baud))
  90. #else
  91. #define BAUD2DIV(baud) (((F_PLL / 2 / 16) + ((baud) >> 1)) / (baud))
  92. #endif
  93. #define BAUD2DIV2(baud) (((F_BUS / 16) + ((baud) >> 1)) / (baud))
  94. #define BAUD2DIV3(baud) (((F_BUS / 16) + ((baud) >> 1)) / (baud))
  95. #endif
  96. // C language implementation
  97. //
  98. #ifdef __cplusplus
  99. extern "C" {
  100. #endif
  101. void serial_begin(uint32_t divisor);
  102. void serial_format(uint32_t format);
  103. void serial_end(void);
  104. void serial_set_transmit_pin(uint8_t pin);
  105. void serial_set_rx(uint8_t pin);
  106. void serial_set_tx(uint8_t pin, uint8_t opendrain);
  107. int serial_set_rts(uint8_t pin);
  108. int serial_set_cts(uint8_t pin);
  109. void serial_putchar(uint32_t c);
  110. void serial_write(const void *buf, unsigned int count);
  111. void serial_flush(void);
  112. int serial_write_buffer_free(void);
  113. int serial_available(void);
  114. int serial_getchar(void);
  115. int serial_peek(void);
  116. void serial_clear(void);
  117. void serial_print(const char *p);
  118. void serial_phex(uint32_t n);
  119. void serial_phex16(uint32_t n);
  120. void serial_phex32(uint32_t n);
  121. void serial2_begin(uint32_t divisor);
  122. void serial2_format(uint32_t format);
  123. void serial2_end(void);
  124. void serial2_set_transmit_pin(uint8_t pin);
  125. void serial2_set_rx(uint8_t pin);
  126. void serial2_set_tx(uint8_t pin, uint8_t opendrain);
  127. int serial2_set_rts(uint8_t pin);
  128. int serial2_set_cts(uint8_t pin);
  129. void serial2_putchar(uint32_t c);
  130. void serial2_write(const void *buf, unsigned int count);
  131. void serial2_flush(void);
  132. int serial2_write_buffer_free(void);
  133. int serial2_available(void);
  134. int serial2_getchar(void);
  135. int serial2_peek(void);
  136. void serial2_clear(void);
  137. void serial3_begin(uint32_t divisor);
  138. void serial3_format(uint32_t format);
  139. void serial3_end(void);
  140. void serial3_set_transmit_pin(uint8_t pin);
  141. void serial3_set_rx(uint8_t pin);
  142. void serial3_set_tx(uint8_t pin, uint8_t opendrain);
  143. int serial3_set_rts(uint8_t pin);
  144. int serial3_set_cts(uint8_t pin);
  145. void serial3_putchar(uint32_t c);
  146. void serial3_write(const void *buf, unsigned int count);
  147. void serial3_flush(void);
  148. int serial3_write_buffer_free(void);
  149. int serial3_available(void);
  150. int serial3_getchar(void);
  151. int serial3_peek(void);
  152. void serial3_clear(void);
  153. #ifdef __cplusplus
  154. }
  155. #endif
  156. // C++ interface
  157. //
  158. #ifdef __cplusplus
  159. #include "Stream.h"
  160. class HardwareSerial : public Stream
  161. {
  162. public:
  163. virtual void begin(uint32_t baud) { serial_begin(BAUD2DIV(baud)); }
  164. virtual void begin(uint32_t baud, uint32_t format) {
  165. serial_begin(BAUD2DIV(baud));
  166. serial_format(format); }
  167. virtual void end(void) { serial_end(); }
  168. virtual void transmitterEnable(uint8_t pin) { serial_set_transmit_pin(pin); }
  169. virtual void setRX(uint8_t pin) { serial_set_rx(pin); }
  170. virtual void setTX(uint8_t pin, bool opendrain=false) { serial_set_tx(pin, opendrain); }
  171. virtual bool attachRts(uint8_t pin) { return serial_set_rts(pin); }
  172. virtual bool attachCts(uint8_t pin) { return serial_set_cts(pin); }
  173. virtual int available(void) { return serial_available(); }
  174. virtual int peek(void) { return serial_peek(); }
  175. virtual int read(void) { return serial_getchar(); }
  176. virtual void flush(void) { serial_flush(); }
  177. virtual void clear(void) { serial_clear(); }
  178. virtual int availableForWrite(void) { return serial_write_buffer_free(); }
  179. virtual size_t write(uint8_t c) { serial_putchar(c); return 1; }
  180. virtual size_t write(unsigned long n) { return write((uint8_t)n); }
  181. virtual size_t write(long n) { return write((uint8_t)n); }
  182. virtual size_t write(unsigned int n) { return write((uint8_t)n); }
  183. virtual size_t write(int n) { return write((uint8_t)n); }
  184. virtual size_t write(const uint8_t *buffer, size_t size)
  185. { serial_write(buffer, size); return size; }
  186. virtual size_t write(const char *str) { size_t len = strlen(str);
  187. serial_write((const uint8_t *)str, len);
  188. return len; }
  189. virtual size_t write9bit(uint32_t c) { serial_putchar(c); return 1; }
  190. operator bool() { return true; }
  191. };
  192. extern HardwareSerial Serial1;
  193. extern void serialEvent1(void);
  194. class HardwareSerial2 : public HardwareSerial
  195. {
  196. public:
  197. virtual void begin(uint32_t baud) { serial2_begin(BAUD2DIV2(baud)); }
  198. virtual void begin(uint32_t baud, uint32_t format) {
  199. serial2_begin(BAUD2DIV2(baud));
  200. serial2_format(format); }
  201. virtual void end(void) { serial2_end(); }
  202. virtual void transmitterEnable(uint8_t pin) { serial2_set_transmit_pin(pin); }
  203. virtual void setRX(uint8_t pin) { serial2_set_rx(pin); }
  204. virtual void setTX(uint8_t pin, bool opendrain=false) { serial2_set_tx(pin, opendrain); }
  205. virtual bool attachRts(uint8_t pin) { return serial2_set_rts(pin); }
  206. virtual bool attachCts(uint8_t pin) { return serial2_set_cts(pin); }
  207. virtual int available(void) { return serial2_available(); }
  208. virtual int peek(void) { return serial2_peek(); }
  209. virtual int read(void) { return serial2_getchar(); }
  210. virtual void flush(void) { serial2_flush(); }
  211. virtual void clear(void) { serial2_clear(); }
  212. virtual int availableForWrite(void) { return serial2_write_buffer_free(); }
  213. virtual size_t write(uint8_t c) { serial2_putchar(c); return 1; }
  214. virtual size_t write(unsigned long n) { return write((uint8_t)n); }
  215. virtual size_t write(long n) { return write((uint8_t)n); }
  216. virtual size_t write(unsigned int n) { return write((uint8_t)n); }
  217. virtual size_t write(int n) { return write((uint8_t)n); }
  218. virtual size_t write(const uint8_t *buffer, size_t size)
  219. { serial2_write(buffer, size); return size; }
  220. virtual size_t write(const char *str) { size_t len = strlen(str);
  221. serial2_write((const uint8_t *)str, len);
  222. return len; }
  223. virtual size_t write9bit(uint32_t c) { serial2_putchar(c); return 1; }
  224. operator bool() { return true; }
  225. };
  226. extern HardwareSerial2 Serial2;
  227. extern void serialEvent2(void);
  228. class HardwareSerial3 : public HardwareSerial
  229. {
  230. public:
  231. virtual void begin(uint32_t baud) { serial3_begin(BAUD2DIV3(baud)); }
  232. virtual void begin(uint32_t baud, uint32_t format) {
  233. serial3_begin(BAUD2DIV3(baud));
  234. serial3_format(format); }
  235. virtual void end(void) { serial3_end(); }
  236. virtual void transmitterEnable(uint8_t pin) { serial3_set_transmit_pin(pin); }
  237. virtual void setRX(uint8_t pin) { serial3_set_rx(pin); }
  238. virtual void setTX(uint8_t pin, bool opendrain=false) { serial3_set_tx(pin, opendrain); }
  239. virtual bool attachRts(uint8_t pin) { return serial3_set_rts(pin); }
  240. virtual bool attachCts(uint8_t pin) { return serial3_set_cts(pin); }
  241. virtual int available(void) { return serial3_available(); }
  242. virtual int peek(void) { return serial3_peek(); }
  243. virtual int read(void) { return serial3_getchar(); }
  244. virtual void flush(void) { serial3_flush(); }
  245. virtual void clear(void) { serial3_clear(); }
  246. virtual int availableForWrite(void) { return serial3_write_buffer_free(); }
  247. virtual size_t write(uint8_t c) { serial3_putchar(c); return 1; }
  248. virtual size_t write(unsigned long n) { return write((uint8_t)n); }
  249. virtual size_t write(long n) { return write((uint8_t)n); }
  250. virtual size_t write(unsigned int n) { return write((uint8_t)n); }
  251. virtual size_t write(int n) { return write((uint8_t)n); }
  252. virtual size_t write(const uint8_t *buffer, size_t size)
  253. { serial3_write(buffer, size); return size; }
  254. virtual size_t write(const char *str) { size_t len = strlen(str);
  255. serial3_write((const uint8_t *)str, len);
  256. return len; }
  257. virtual size_t write9bit(uint32_t c) { serial3_putchar(c); return 1; }
  258. operator bool() { return true; }
  259. };
  260. extern HardwareSerial3 Serial3;
  261. extern void serialEvent3(void);
  262. #endif
  263. #endif