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.

442 lines
18KB

  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. void serial4_begin(uint32_t divisor);
  154. void serial4_format(uint32_t format);
  155. void serial4_end(void);
  156. void serial4_set_transmit_pin(uint8_t pin);
  157. void serial4_set_rx(uint8_t pin);
  158. void serial4_set_tx(uint8_t pin, uint8_t opendrain);
  159. int serial4_set_rts(uint8_t pin);
  160. int serial4_set_cts(uint8_t pin);
  161. void serial4_putchar(uint32_t c);
  162. void serial4_write(const void *buf, unsigned int count);
  163. void serial4_flush(void);
  164. int serial4_write_buffer_free(void);
  165. int serial4_available(void);
  166. int serial4_getchar(void);
  167. int serial4_peek(void);
  168. void serial4_clear(void);
  169. void serial5_begin(uint32_t divisor);
  170. void serial5_format(uint32_t format);
  171. void serial5_end(void);
  172. void serial5_set_transmit_pin(uint8_t pin);
  173. void serial5_set_rx(uint8_t pin);
  174. void serial5_set_tx(uint8_t pin, uint8_t opendrain);
  175. int serial5_set_rts(uint8_t pin);
  176. int serial5_set_cts(uint8_t pin);
  177. void serial5_putchar(uint32_t c);
  178. void serial5_write(const void *buf, unsigned int count);
  179. void serial5_flush(void);
  180. int serial5_write_buffer_free(void);
  181. int serial5_available(void);
  182. int serial5_getchar(void);
  183. int serial5_peek(void);
  184. void serial5_clear(void);
  185. void serial6_begin(uint32_t divisor);
  186. void serial6_format(uint32_t format);
  187. void serial6_end(void);
  188. void serial6_set_transmit_pin(uint8_t pin);
  189. void serial6_set_rx(uint8_t pin);
  190. void serial6_set_tx(uint8_t pin, uint8_t opendrain);
  191. int serial6_set_rts(uint8_t pin);
  192. int serial6_set_cts(uint8_t pin);
  193. void serial6_putchar(uint32_t c);
  194. void serial6_write(const void *buf, unsigned int count);
  195. void serial6_flush(void);
  196. int serial6_write_buffer_free(void);
  197. int serial6_available(void);
  198. int serial6_getchar(void);
  199. int serial6_peek(void);
  200. void serial6_clear(void);
  201. #ifdef __cplusplus
  202. }
  203. #endif
  204. // C++ interface
  205. //
  206. #ifdef __cplusplus
  207. #include "Stream.h"
  208. class HardwareSerial : public Stream
  209. {
  210. public:
  211. virtual void begin(uint32_t baud) { serial_begin(BAUD2DIV(baud)); }
  212. virtual void begin(uint32_t baud, uint32_t format) {
  213. serial_begin(BAUD2DIV(baud));
  214. serial_format(format); }
  215. virtual void end(void) { serial_end(); }
  216. virtual void transmitterEnable(uint8_t pin) { serial_set_transmit_pin(pin); }
  217. virtual void setRX(uint8_t pin) { serial_set_rx(pin); }
  218. virtual void setTX(uint8_t pin, bool opendrain=false) { serial_set_tx(pin, opendrain); }
  219. virtual bool attachRts(uint8_t pin) { return serial_set_rts(pin); }
  220. virtual bool attachCts(uint8_t pin) { return serial_set_cts(pin); }
  221. virtual int available(void) { return serial_available(); }
  222. virtual int peek(void) { return serial_peek(); }
  223. virtual int read(void) { return serial_getchar(); }
  224. virtual void flush(void) { serial_flush(); }
  225. virtual void clear(void) { serial_clear(); }
  226. virtual int availableForWrite(void) { return serial_write_buffer_free(); }
  227. virtual size_t write(uint8_t c) { serial_putchar(c); return 1; }
  228. virtual size_t write(unsigned long n) { return write((uint8_t)n); }
  229. virtual size_t write(long n) { return write((uint8_t)n); }
  230. virtual size_t write(unsigned int n) { return write((uint8_t)n); }
  231. virtual size_t write(int n) { return write((uint8_t)n); }
  232. virtual size_t write(const uint8_t *buffer, size_t size)
  233. { serial_write(buffer, size); return size; }
  234. virtual size_t write(const char *str) { size_t len = strlen(str);
  235. serial_write((const uint8_t *)str, len);
  236. return len; }
  237. virtual size_t write9bit(uint32_t c) { serial_putchar(c); return 1; }
  238. operator bool() { return true; }
  239. };
  240. extern HardwareSerial Serial1;
  241. extern void serialEvent1(void);
  242. class HardwareSerial2 : public HardwareSerial
  243. {
  244. public:
  245. virtual void begin(uint32_t baud) { serial2_begin(BAUD2DIV2(baud)); }
  246. virtual void begin(uint32_t baud, uint32_t format) {
  247. serial2_begin(BAUD2DIV2(baud));
  248. serial2_format(format); }
  249. virtual void end(void) { serial2_end(); }
  250. virtual void transmitterEnable(uint8_t pin) { serial2_set_transmit_pin(pin); }
  251. virtual void setRX(uint8_t pin) { serial2_set_rx(pin); }
  252. virtual void setTX(uint8_t pin, bool opendrain=false) { serial2_set_tx(pin, opendrain); }
  253. virtual bool attachRts(uint8_t pin) { return serial2_set_rts(pin); }
  254. virtual bool attachCts(uint8_t pin) { return serial2_set_cts(pin); }
  255. virtual int available(void) { return serial2_available(); }
  256. virtual int peek(void) { return serial2_peek(); }
  257. virtual int read(void) { return serial2_getchar(); }
  258. virtual void flush(void) { serial2_flush(); }
  259. virtual void clear(void) { serial2_clear(); }
  260. virtual int availableForWrite(void) { return serial2_write_buffer_free(); }
  261. virtual size_t write(uint8_t c) { serial2_putchar(c); return 1; }
  262. virtual size_t write(unsigned long n) { return write((uint8_t)n); }
  263. virtual size_t write(long n) { return write((uint8_t)n); }
  264. virtual size_t write(unsigned int n) { return write((uint8_t)n); }
  265. virtual size_t write(int n) { return write((uint8_t)n); }
  266. virtual size_t write(const uint8_t *buffer, size_t size)
  267. { serial2_write(buffer, size); return size; }
  268. virtual size_t write(const char *str) { size_t len = strlen(str);
  269. serial2_write((const uint8_t *)str, len);
  270. return len; }
  271. virtual size_t write9bit(uint32_t c) { serial2_putchar(c); return 1; }
  272. operator bool() { return true; }
  273. };
  274. extern HardwareSerial2 Serial2;
  275. extern void serialEvent2(void);
  276. class HardwareSerial3 : public HardwareSerial
  277. {
  278. public:
  279. virtual void begin(uint32_t baud) { serial3_begin(BAUD2DIV3(baud)); }
  280. virtual void begin(uint32_t baud, uint32_t format) {
  281. serial3_begin(BAUD2DIV3(baud));
  282. serial3_format(format); }
  283. virtual void end(void) { serial3_end(); }
  284. virtual void transmitterEnable(uint8_t pin) { serial3_set_transmit_pin(pin); }
  285. virtual void setRX(uint8_t pin) { serial3_set_rx(pin); }
  286. virtual void setTX(uint8_t pin, bool opendrain=false) { serial3_set_tx(pin, opendrain); }
  287. virtual bool attachRts(uint8_t pin) { return serial3_set_rts(pin); }
  288. virtual bool attachCts(uint8_t pin) { return serial3_set_cts(pin); }
  289. virtual int available(void) { return serial3_available(); }
  290. virtual int peek(void) { return serial3_peek(); }
  291. virtual int read(void) { return serial3_getchar(); }
  292. virtual void flush(void) { serial3_flush(); }
  293. virtual void clear(void) { serial3_clear(); }
  294. virtual int availableForWrite(void) { return serial3_write_buffer_free(); }
  295. virtual size_t write(uint8_t c) { serial3_putchar(c); return 1; }
  296. virtual size_t write(unsigned long n) { return write((uint8_t)n); }
  297. virtual size_t write(long n) { return write((uint8_t)n); }
  298. virtual size_t write(unsigned int n) { return write((uint8_t)n); }
  299. virtual size_t write(int n) { return write((uint8_t)n); }
  300. virtual size_t write(const uint8_t *buffer, size_t size)
  301. { serial3_write(buffer, size); return size; }
  302. virtual size_t write(const char *str) { size_t len = strlen(str);
  303. serial3_write((const uint8_t *)str, len);
  304. return len; }
  305. virtual size_t write9bit(uint32_t c) { serial3_putchar(c); return 1; }
  306. operator bool() { return true; }
  307. };
  308. extern HardwareSerial3 Serial3;
  309. extern void serialEvent3(void);
  310. class HardwareSerial4 : public HardwareSerial
  311. {
  312. public:
  313. virtual void begin(uint32_t baud) { serial4_begin(BAUD2DIV3(baud)); }
  314. virtual void begin(uint32_t baud, uint32_t format) {
  315. serial4_begin(BAUD2DIV3(baud));
  316. serial4_format(format); }
  317. virtual void end(void) { serial4_end(); }
  318. virtual void transmitterEnable(uint8_t pin) { serial4_set_transmit_pin(pin); }
  319. virtual void setRX(uint8_t pin) { serial4_set_rx(pin); }
  320. virtual void setTX(uint8_t pin, bool opendrain=false) { serial4_set_tx(pin, opendrain); }
  321. virtual bool attachRts(uint8_t pin) { return serial4_set_rts(pin); }
  322. virtual bool attachCts(uint8_t pin) { return serial4_set_cts(pin); }
  323. virtual int available(void) { return serial4_available(); }
  324. virtual int peek(void) { return serial4_peek(); }
  325. virtual int read(void) { return serial4_getchar(); }
  326. virtual void flush(void) { serial4_flush(); }
  327. virtual void clear(void) { serial4_clear(); }
  328. virtual int availableForWrite(void) { return serial4_write_buffer_free(); }
  329. virtual size_t write(uint8_t c) { serial4_putchar(c); return 1; }
  330. virtual size_t write(unsigned long n) { return write((uint8_t)n); }
  331. virtual size_t write(long n) { return write((uint8_t)n); }
  332. virtual size_t write(unsigned int n) { return write((uint8_t)n); }
  333. virtual size_t write(int n) { return write((uint8_t)n); }
  334. virtual size_t write(const uint8_t *buffer, size_t size)
  335. { serial4_write(buffer, size); return size; }
  336. virtual size_t write(const char *str) { size_t len = strlen(str);
  337. serial4_write((const uint8_t *)str, len);
  338. return len; }
  339. virtual size_t write9bit(uint32_t c) { serial4_putchar(c); return 1; }
  340. operator bool() { return true; }
  341. };
  342. extern HardwareSerial4 Serial4;
  343. extern void serialEvent4(void);
  344. class HardwareSerial5 : public HardwareSerial
  345. {
  346. public:
  347. virtual void begin(uint32_t baud) { serial5_begin(BAUD2DIV3(baud)); }
  348. virtual void begin(uint32_t baud, uint32_t format) {
  349. serial5_begin(BAUD2DIV3(baud));
  350. serial5_format(format); }
  351. virtual void end(void) { serial5_end(); }
  352. virtual void transmitterEnable(uint8_t pin) { serial5_set_transmit_pin(pin); }
  353. virtual void setRX(uint8_t pin) { serial5_set_rx(pin); }
  354. virtual void setTX(uint8_t pin, bool opendrain=false) { serial5_set_tx(pin, opendrain); }
  355. virtual bool attachRts(uint8_t pin) { return serial5_set_rts(pin); }
  356. virtual bool attachCts(uint8_t pin) { return serial5_set_cts(pin); }
  357. virtual int available(void) { return serial5_available(); }
  358. virtual int peek(void) { return serial5_peek(); }
  359. virtual int read(void) { return serial5_getchar(); }
  360. virtual void flush(void) { serial5_flush(); }
  361. virtual void clear(void) { serial5_clear(); }
  362. virtual int availableForWrite(void) { return serial5_write_buffer_free(); }
  363. virtual size_t write(uint8_t c) { serial5_putchar(c); return 1; }
  364. virtual size_t write(unsigned long n) { return write((uint8_t)n); }
  365. virtual size_t write(long n) { return write((uint8_t)n); }
  366. virtual size_t write(unsigned int n) { return write((uint8_t)n); }
  367. virtual size_t write(int n) { return write((uint8_t)n); }
  368. virtual size_t write(const uint8_t *buffer, size_t size)
  369. { serial5_write(buffer, size); return size; }
  370. virtual size_t write(const char *str) { size_t len = strlen(str);
  371. serial5_write((const uint8_t *)str, len);
  372. return len; }
  373. virtual size_t write9bit(uint32_t c) { serial5_putchar(c); return 1; }
  374. operator bool() { return true; }
  375. };
  376. extern HardwareSerial5 Serial5;
  377. extern void serialEvent5(void);
  378. class HardwareSerial6 : public HardwareSerial
  379. {
  380. public:
  381. virtual void begin(uint32_t baud) { serial6_begin(BAUD2DIV3(baud)); }
  382. virtual void begin(uint32_t baud, uint32_t format) {
  383. serial6_begin(BAUD2DIV3(baud));
  384. serial6_format(format); }
  385. virtual void end(void) { serial6_end(); }
  386. virtual void transmitterEnable(uint8_t pin) { serial6_set_transmit_pin(pin); }
  387. virtual void setRX(uint8_t pin) { serial6_set_rx(pin); }
  388. virtual void setTX(uint8_t pin, bool opendrain=false) { serial6_set_tx(pin, opendrain); }
  389. virtual bool attachRts(uint8_t pin) { return serial6_set_rts(pin); }
  390. virtual bool attachCts(uint8_t pin) { return serial6_set_cts(pin); }
  391. virtual int available(void) { return serial6_available(); }
  392. virtual int peek(void) { return serial6_peek(); }
  393. virtual int read(void) { return serial6_getchar(); }
  394. virtual void flush(void) { serial6_flush(); }
  395. virtual void clear(void) { serial6_clear(); }
  396. virtual int availableForWrite(void) { return serial6_write_buffer_free(); }
  397. virtual size_t write(uint8_t c) { serial6_putchar(c); return 1; }
  398. virtual size_t write(unsigned long n) { return write((uint8_t)n); }
  399. virtual size_t write(long n) { return write((uint8_t)n); }
  400. virtual size_t write(unsigned int n) { return write((uint8_t)n); }
  401. virtual size_t write(int n) { return write((uint8_t)n); }
  402. virtual size_t write(const uint8_t *buffer, size_t size)
  403. { serial6_write(buffer, size); return size; }
  404. virtual size_t write(const char *str) { size_t len = strlen(str);
  405. serial6_write((const uint8_t *)str, len);
  406. return len; }
  407. virtual size_t write9bit(uint32_t c) { serial6_putchar(c); return 1; }
  408. operator bool() { return true; }
  409. };
  410. extern HardwareSerial6 Serial6;
  411. extern void serialEvent6(void);
  412. #endif
  413. #endif