選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

522 行
14KB

  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. #include "kinetis.h"
  31. #include "core_pins.h"
  32. #include "HardwareSerial.h"
  33. ////////////////////////////////////////////////////////////////
  34. // Tunable parameters (relatively safe to edit these numbers)
  35. ////////////////////////////////////////////////////////////////
  36. #define TX_BUFFER_SIZE 64 // number of outgoing bytes to buffer
  37. #define RX_BUFFER_SIZE 64 // number of incoming bytes to buffer
  38. #define RTS_HIGH_WATERMARK 40 // RTS requests sender to pause
  39. #define RTS_LOW_WATERMARK 26 // RTS allows sender to resume
  40. #define IRQ_PRIORITY 64 // 0 = highest priority, 255 = lowest
  41. ////////////////////////////////////////////////////////////////
  42. // changes not recommended below this point....
  43. ////////////////////////////////////////////////////////////////
  44. #ifdef SERIAL_9BIT_SUPPORT
  45. static uint8_t use9Bits = 0;
  46. #define BUFTYPE uint16_t
  47. #else
  48. #define BUFTYPE uint8_t
  49. #define use9Bits 0
  50. #endif
  51. static volatile BUFTYPE tx_buffer[TX_BUFFER_SIZE];
  52. static volatile BUFTYPE rx_buffer[RX_BUFFER_SIZE];
  53. static volatile uint8_t transmitting = 0;
  54. #if defined(KINETISK)
  55. static volatile uint8_t *transmit_pin=NULL;
  56. #define transmit_assert() *transmit_pin = 1
  57. #define transmit_deassert() *transmit_pin = 0
  58. static volatile uint8_t *rts_pin=NULL;
  59. #define rts_assert() *rts_pin = 0
  60. #define rts_deassert() *rts_pin = 1
  61. #elif defined(KINETISL)
  62. static volatile uint8_t *transmit_pin=NULL;
  63. static uint8_t transmit_mask=0;
  64. #define transmit_assert() *(transmit_pin+4) = transmit_mask;
  65. #define transmit_deassert() *(transmit_pin+8) = transmit_mask;
  66. static volatile uint8_t *rts_pin=NULL;
  67. static uint8_t rts_mask=0;
  68. #define rts_assert() *(rts_pin+8) = rts_mask;
  69. #define rts_deassert() *(rts_pin+4) = rts_mask;
  70. #endif
  71. #if TX_BUFFER_SIZE > 255
  72. static volatile uint16_t tx_buffer_head = 0;
  73. static volatile uint16_t tx_buffer_tail = 0;
  74. #else
  75. static volatile uint8_t tx_buffer_head = 0;
  76. static volatile uint8_t tx_buffer_tail = 0;
  77. #endif
  78. #if RX_BUFFER_SIZE > 255
  79. static volatile uint16_t rx_buffer_head = 0;
  80. static volatile uint16_t rx_buffer_tail = 0;
  81. #else
  82. static volatile uint8_t rx_buffer_head = 0;
  83. static volatile uint8_t rx_buffer_tail = 0;
  84. #endif
  85. // UART0 and UART1 are clocked by F_CPU, UART2 is clocked by F_BUS
  86. // UART0 has 8 byte fifo, UART1 and UART2 have 1 byte buffer
  87. #ifdef HAS_KINETISK_UART0_FIFO
  88. #define C2_ENABLE UART_C2_TE | UART_C2_RE | UART_C2_RIE | UART_C2_ILIE
  89. #else
  90. #define C2_ENABLE UART_C2_TE | UART_C2_RE | UART_C2_RIE
  91. #endif
  92. #define C2_TX_ACTIVE C2_ENABLE | UART_C2_TIE
  93. #define C2_TX_COMPLETING C2_ENABLE | UART_C2_TCIE
  94. #define C2_TX_INACTIVE C2_ENABLE
  95. void serial_begin(uint32_t divisor)
  96. {
  97. SIM_SCGC4 |= SIM_SCGC4_UART0; // turn on clock, TODO: use bitband
  98. rx_buffer_head = 0;
  99. rx_buffer_tail = 0;
  100. tx_buffer_head = 0;
  101. tx_buffer_tail = 0;
  102. transmitting = 0;
  103. CORE_PIN0_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3);
  104. CORE_PIN1_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3);
  105. #if defined(HAS_KINETISK_UART0)
  106. UART0_BDH = (divisor >> 13) & 0x1F;
  107. UART0_BDL = (divisor >> 5) & 0xFF;
  108. UART0_C4 = divisor & 0x1F;
  109. #ifdef HAS_KINETISK_UART0_FIFO
  110. UART0_C1 = UART_C1_ILT;
  111. UART0_TWFIFO = 2; // tx watermark, causes S1_TDRE to set
  112. UART0_RWFIFO = 4; // rx watermark, causes S1_RDRF to set
  113. UART0_PFIFO = UART_PFIFO_TXFE | UART_PFIFO_RXFE;
  114. #else
  115. UART0_C1 = 0;
  116. UART0_PFIFO = 0;
  117. #endif
  118. #elif defined(HAS_KINETISL_UART0)
  119. UART0_BDH = (divisor >> 8) & 0x1F;
  120. UART0_BDL = divisor & 0xFF;
  121. UART0_C1 = 0;
  122. #endif
  123. UART0_C2 = C2_TX_INACTIVE;
  124. NVIC_SET_PRIORITY(IRQ_UART0_STATUS, IRQ_PRIORITY);
  125. NVIC_ENABLE_IRQ(IRQ_UART0_STATUS);
  126. }
  127. void serial_format(uint32_t format)
  128. {
  129. uint8_t c;
  130. c = UART0_C1;
  131. c = (c & ~0x13) | (format & 0x03); // configure parity
  132. if (format & 0x04) c |= 0x10; // 9 bits (might include parity)
  133. UART0_C1 = c;
  134. if ((format & 0x0F) == 0x04) UART0_C3 |= 0x40; // 8N2 is 9 bit with 9th bit always 1
  135. c = UART0_S2 & ~0x10;
  136. if (format & 0x10) c |= 0x10; // rx invert
  137. UART0_S2 = c;
  138. c = UART0_C3 & ~0x10;
  139. if (format & 0x20) c |= 0x10; // tx invert
  140. UART0_C3 = c;
  141. #ifdef SERIAL_9BIT_SUPPORT
  142. c = UART0_C4 & 0x1F;
  143. if (format & 0x08) c |= 0x20; // 9 bit mode with parity (requires 10 bits)
  144. UART0_C4 = c;
  145. use9Bits = format & 0x80;
  146. #endif
  147. }
  148. void serial_end(void)
  149. {
  150. if (!(SIM_SCGC4 & SIM_SCGC4_UART0)) return;
  151. while (transmitting) yield(); // wait for buffered data to send
  152. NVIC_DISABLE_IRQ(IRQ_UART0_STATUS);
  153. UART0_C2 = 0;
  154. CORE_PIN0_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1);
  155. CORE_PIN1_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1);
  156. rx_buffer_head = 0;
  157. rx_buffer_tail = 0;
  158. if (rts_pin) rts_deassert();
  159. }
  160. void serial_set_transmit_pin(uint8_t pin)
  161. {
  162. while (transmitting) ;
  163. pinMode(pin, OUTPUT);
  164. digitalWrite(pin, LOW);
  165. transmit_pin = portOutputRegister(pin);
  166. #if defined(KINETISL)
  167. transmit_mask = digitalPinToBitMask(pin);
  168. #endif
  169. }
  170. int serial_set_rts(uint8_t pin)
  171. {
  172. if (!(SIM_SCGC4 & SIM_SCGC4_UART0)) return 0;
  173. if (pin < CORE_NUM_DIGITAL) {
  174. rts_pin = portOutputRegister(pin);
  175. #if defined(KINETISL)
  176. rts_mask = digitalPinToBitMask(pin);
  177. #endif
  178. pinMode(pin, OUTPUT);
  179. rts_assert();
  180. } else {
  181. rts_pin = NULL;
  182. return 0;
  183. }
  184. /*
  185. if (pin == 6) {
  186. CORE_PIN6_CONFIG = PORT_PCR_MUX(3);
  187. } else if (pin == 19) {
  188. CORE_PIN19_CONFIG = PORT_PCR_MUX(3);
  189. } else {
  190. UART0_MODEM &= ~UART_MODEM_RXRTSE;
  191. return 0;
  192. }
  193. UART0_MODEM |= UART_MODEM_RXRTSE;
  194. */
  195. return 1;
  196. }
  197. int serial_set_cts(uint8_t pin)
  198. {
  199. if (!(SIM_SCGC4 & SIM_SCGC4_UART0)) return 0;
  200. if (pin == 18) {
  201. CORE_PIN18_CONFIG = PORT_PCR_MUX(3) | PORT_PCR_PE; // weak pulldown
  202. } else if (pin == 20) {
  203. CORE_PIN20_CONFIG = PORT_PCR_MUX(3) | PORT_PCR_PE; // weak pulldown
  204. } else {
  205. UART0_MODEM &= ~UART_MODEM_TXCTSE;
  206. return 0;
  207. }
  208. UART0_MODEM |= UART_MODEM_TXCTSE;
  209. return 1;
  210. }
  211. void serial_putchar(uint32_t c)
  212. {
  213. uint32_t head, n;
  214. if (!(SIM_SCGC4 & SIM_SCGC4_UART0)) return;
  215. if (transmit_pin) transmit_assert();
  216. head = tx_buffer_head;
  217. if (++head >= TX_BUFFER_SIZE) head = 0;
  218. while (tx_buffer_tail == head) {
  219. int priority = nvic_execution_priority();
  220. if (priority <= IRQ_PRIORITY) {
  221. if ((UART0_S1 & UART_S1_TDRE)) {
  222. uint32_t tail = tx_buffer_tail;
  223. if (++tail >= TX_BUFFER_SIZE) tail = 0;
  224. n = tx_buffer[tail];
  225. if (use9Bits) UART0_C3 = (UART0_C3 & ~0x40) | ((n & 0x100) >> 2);
  226. UART0_D = n;
  227. tx_buffer_tail = tail;
  228. }
  229. } else if (priority >= 256) {
  230. yield();
  231. }
  232. }
  233. tx_buffer[head] = c;
  234. transmitting = 1;
  235. tx_buffer_head = head;
  236. UART0_C2 = C2_TX_ACTIVE;
  237. }
  238. #ifdef HAS_KINETISK_UART0_FIFO
  239. void serial_write(const void *buf, unsigned int count)
  240. {
  241. const uint8_t *p = (const uint8_t *)buf;
  242. const uint8_t *end = p + count;
  243. uint32_t head, n;
  244. if (!(SIM_SCGC4 & SIM_SCGC4_UART0)) return;
  245. if (transmit_pin) transmit_assert();
  246. while (p < end) {
  247. head = tx_buffer_head;
  248. if (++head >= TX_BUFFER_SIZE) head = 0;
  249. if (tx_buffer_tail == head) {
  250. UART0_C2 = C2_TX_ACTIVE;
  251. do {
  252. int priority = nvic_execution_priority();
  253. if (priority <= IRQ_PRIORITY) {
  254. if ((UART0_S1 & UART_S1_TDRE)) {
  255. uint32_t tail = tx_buffer_tail;
  256. if (++tail >= TX_BUFFER_SIZE) tail = 0;
  257. n = tx_buffer[tail];
  258. if (use9Bits) UART0_C3 = (UART0_C3 & ~0x40) | ((n & 0x100) >> 2);
  259. UART0_D = n;
  260. tx_buffer_tail = tail;
  261. }
  262. } else if (priority >= 256) {
  263. yield();
  264. }
  265. } while (tx_buffer_tail == head);
  266. }
  267. tx_buffer[head] = *p++;
  268. transmitting = 1;
  269. tx_buffer_head = head;
  270. }
  271. UART0_C2 = C2_TX_ACTIVE;
  272. }
  273. #else
  274. void serial_write(const void *buf, unsigned int count)
  275. {
  276. const uint8_t *p = (const uint8_t *)buf;
  277. while (count-- > 0) serial_putchar(*p++);
  278. }
  279. #endif
  280. void serial_flush(void)
  281. {
  282. while (transmitting) yield(); // wait
  283. }
  284. int serial_write_buffer_free(void)
  285. {
  286. uint32_t head, tail;
  287. head = tx_buffer_head;
  288. tail = tx_buffer_tail;
  289. if (head >= tail) return TX_BUFFER_SIZE - 1 - head + tail;
  290. return tail - head - 1;
  291. }
  292. int serial_available(void)
  293. {
  294. uint32_t head, tail;
  295. head = rx_buffer_head;
  296. tail = rx_buffer_tail;
  297. if (head >= tail) return head - tail;
  298. return RX_BUFFER_SIZE + head - tail;
  299. }
  300. int serial_getchar(void)
  301. {
  302. uint32_t head, tail;
  303. int c;
  304. head = rx_buffer_head;
  305. tail = rx_buffer_tail;
  306. if (head == tail) return -1;
  307. if (++tail >= RX_BUFFER_SIZE) tail = 0;
  308. c = rx_buffer[tail];
  309. rx_buffer_tail = tail;
  310. if (rts_pin) {
  311. int avail;
  312. if (head >= tail) avail = head - tail;
  313. else avail = RX_BUFFER_SIZE + head - tail;
  314. if (avail <= RTS_LOW_WATERMARK) rts_assert();
  315. }
  316. return c;
  317. }
  318. int serial_peek(void)
  319. {
  320. uint32_t head, tail;
  321. head = rx_buffer_head;
  322. tail = rx_buffer_tail;
  323. if (head == tail) return -1;
  324. if (++tail >= RX_BUFFER_SIZE) tail = 0;
  325. return rx_buffer[tail];
  326. }
  327. void serial_clear(void)
  328. {
  329. #ifdef HAS_KINETISK_UART0_FIFO
  330. if (!(SIM_SCGC4 & SIM_SCGC4_UART0)) return;
  331. UART0_C2 &= ~(UART_C2_RE | UART_C2_RIE | UART_C2_ILIE);
  332. UART0_CFIFO = UART_CFIFO_RXFLUSH;
  333. UART0_C2 |= (UART_C2_RE | UART_C2_RIE | UART_C2_ILIE);
  334. #endif
  335. rx_buffer_head = rx_buffer_tail;
  336. if (rts_pin) rts_assert();
  337. }
  338. // status interrupt combines
  339. // Transmit data below watermark UART_S1_TDRE
  340. // Transmit complete UART_S1_TC
  341. // Idle line UART_S1_IDLE
  342. // Receive data above watermark UART_S1_RDRF
  343. // LIN break detect UART_S2_LBKDIF
  344. // RxD pin active edge UART_S2_RXEDGIF
  345. void uart0_status_isr(void)
  346. {
  347. uint32_t head, tail, n;
  348. uint8_t c;
  349. #ifdef HAS_KINETISK_UART0_FIFO
  350. uint32_t newhead;
  351. uint8_t avail;
  352. if (UART0_S1 & (UART_S1_RDRF | UART_S1_IDLE)) {
  353. __disable_irq();
  354. avail = UART0_RCFIFO;
  355. if (avail == 0) {
  356. // The only way to clear the IDLE interrupt flag is
  357. // to read the data register. But reading with no
  358. // data causes a FIFO underrun, which causes the
  359. // FIFO to return corrupted data. If anyone from
  360. // Freescale reads this, what a poor design! There
  361. // write should be a write-1-to-clear for IDLE.
  362. c = UART0_D;
  363. // flushing the fifo recovers from the underrun,
  364. // but there's a possible race condition where a
  365. // new character could be received between reading
  366. // RCFIFO == 0 and flushing the FIFO. To minimize
  367. // the chance, interrupts are disabled so a higher
  368. // priority interrupt (hopefully) doesn't delay.
  369. // TODO: change this to disabling the IDLE interrupt
  370. // which won't be simple, since we already manage
  371. // which transmit interrupts are enabled.
  372. UART0_CFIFO = UART_CFIFO_RXFLUSH;
  373. __enable_irq();
  374. } else {
  375. __enable_irq();
  376. head = rx_buffer_head;
  377. tail = rx_buffer_tail;
  378. do {
  379. if (use9Bits && (UART0_C3 & 0x80)) {
  380. n = UART0_D | 0x100;
  381. } else {
  382. n = UART0_D;
  383. }
  384. newhead = head + 1;
  385. if (newhead >= RX_BUFFER_SIZE) newhead = 0;
  386. if (newhead != tail) {
  387. head = newhead;
  388. rx_buffer[head] = n;
  389. }
  390. } while (--avail > 0);
  391. rx_buffer_head = head;
  392. if (rts_pin) {
  393. int avail;
  394. if (head >= tail) avail = head - tail;
  395. else avail = RX_BUFFER_SIZE + head - tail;
  396. if (avail >= RTS_HIGH_WATERMARK) rts_deassert();
  397. }
  398. }
  399. }
  400. c = UART0_C2;
  401. if ((c & UART_C2_TIE) && (UART0_S1 & UART_S1_TDRE)) {
  402. head = tx_buffer_head;
  403. tail = tx_buffer_tail;
  404. do {
  405. if (tail == head) break;
  406. if (++tail >= TX_BUFFER_SIZE) tail = 0;
  407. avail = UART0_S1;
  408. n = tx_buffer[tail];
  409. if (use9Bits) UART0_C3 = (UART0_C3 & ~0x40) | ((n & 0x100) >> 2);
  410. UART0_D = n;
  411. } while (UART0_TCFIFO < 8);
  412. tx_buffer_tail = tail;
  413. if (UART0_S1 & UART_S1_TDRE) UART0_C2 = C2_TX_COMPLETING;
  414. }
  415. #else
  416. if (UART0_S1 & UART_S1_RDRF) {
  417. n = UART0_D;
  418. if (use9Bits && (UART0_C3 & 0x80)) n |= 0x100;
  419. head = rx_buffer_head + 1;
  420. if (head >= RX_BUFFER_SIZE) head = 0;
  421. if (head != rx_buffer_tail) {
  422. rx_buffer[head] = n;
  423. rx_buffer_head = head;
  424. }
  425. }
  426. c = UART0_C2;
  427. if ((c & UART_C2_TIE) && (UART0_S1 & UART_S1_TDRE)) {
  428. head = tx_buffer_head;
  429. tail = tx_buffer_tail;
  430. if (head == tail) {
  431. UART0_C2 = C2_TX_COMPLETING;
  432. } else {
  433. if (++tail >= TX_BUFFER_SIZE) tail = 0;
  434. n = tx_buffer[tail];
  435. if (use9Bits) UART0_C3 = (UART0_C3 & ~0x40) | ((n & 0x100) >> 2);
  436. UART0_D = n;
  437. tx_buffer_tail = tail;
  438. }
  439. }
  440. #endif
  441. if ((c & UART_C2_TCIE) && (UART0_S1 & UART_S1_TC)) {
  442. transmitting = 0;
  443. if (transmit_pin) transmit_deassert();
  444. UART0_C2 = C2_TX_INACTIVE;
  445. }
  446. }
  447. void serial_print(const char *p)
  448. {
  449. while (*p) {
  450. char c = *p++;
  451. if (c == '\n') serial_putchar('\r');
  452. serial_putchar(c);
  453. }
  454. }
  455. static void serial_phex1(uint32_t n)
  456. {
  457. n &= 15;
  458. if (n < 10) {
  459. serial_putchar('0' + n);
  460. } else {
  461. serial_putchar('A' - 10 + n);
  462. }
  463. }
  464. void serial_phex(uint32_t n)
  465. {
  466. serial_phex1(n >> 4);
  467. serial_phex1(n);
  468. }
  469. void serial_phex16(uint32_t n)
  470. {
  471. serial_phex(n >> 8);
  472. serial_phex(n);
  473. }
  474. void serial_phex32(uint32_t n)
  475. {
  476. serial_phex(n >> 24);
  477. serial_phex(n >> 16);
  478. serial_phex(n >> 8);
  479. serial_phex(n);
  480. }