No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

653 líneas
19KB

  1. /* Teensyduino Core Library
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2017 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. #ifndef SERIAL1_TX_BUFFER_SIZE
  37. #define SERIAL1_TX_BUFFER_SIZE 64 // number of outgoing bytes to buffer
  38. #endif
  39. #ifndef SERIAL1_RX_BUFFER_SIZE
  40. #define SERIAL1_RX_BUFFER_SIZE 64 // number of incoming bytes to buffer
  41. #endif
  42. #define RTS_HIGH_WATERMARK (SERIAL1_RX_BUFFER_SIZE-24) // RTS requests sender to pause
  43. #define RTS_LOW_WATERMARK (SERIAL1_RX_BUFFER_SIZE-38) // RTS allows sender to resume
  44. #define IRQ_PRIORITY 64 // 0 = highest priority, 255 = lowest
  45. ////////////////////////////////////////////////////////////////
  46. // changes not recommended below this point....
  47. ////////////////////////////////////////////////////////////////
  48. #ifdef SERIAL_9BIT_SUPPORT
  49. static uint8_t use9Bits = 0;
  50. #define BUFTYPE uint16_t
  51. #else
  52. #define BUFTYPE uint8_t
  53. #define use9Bits 0
  54. #endif
  55. static volatile BUFTYPE tx_buffer[SERIAL1_TX_BUFFER_SIZE];
  56. static volatile BUFTYPE rx_buffer[SERIAL1_RX_BUFFER_SIZE];
  57. static volatile uint8_t transmitting = 0;
  58. #if defined(KINETISK)
  59. static volatile uint8_t *transmit_pin=NULL;
  60. #define transmit_assert() *transmit_pin = 1
  61. #define transmit_deassert() *transmit_pin = 0
  62. static volatile uint8_t *rts_pin=NULL;
  63. #define rts_assert() *rts_pin = 0
  64. #define rts_deassert() *rts_pin = 1
  65. #elif defined(KINETISL)
  66. static volatile uint8_t *transmit_pin=NULL;
  67. static uint8_t transmit_mask=0;
  68. #define transmit_assert() *(transmit_pin+4) = transmit_mask;
  69. #define transmit_deassert() *(transmit_pin+8) = transmit_mask;
  70. static volatile uint8_t *rts_pin=NULL;
  71. static uint8_t rts_mask=0;
  72. #define rts_assert() *(rts_pin+8) = rts_mask;
  73. #define rts_deassert() *(rts_pin+4) = rts_mask;
  74. #endif
  75. #if SERIAL1_TX_BUFFER_SIZE > 255
  76. static volatile uint16_t tx_buffer_head = 0;
  77. static volatile uint16_t tx_buffer_tail = 0;
  78. #else
  79. static volatile uint8_t tx_buffer_head = 0;
  80. static volatile uint8_t tx_buffer_tail = 0;
  81. #endif
  82. #if SERIAL1_RX_BUFFER_SIZE > 255
  83. static volatile uint16_t rx_buffer_head = 0;
  84. static volatile uint16_t rx_buffer_tail = 0;
  85. #else
  86. static volatile uint8_t rx_buffer_head = 0;
  87. static volatile uint8_t rx_buffer_tail = 0;
  88. #endif
  89. static uint8_t rx_pin_num = 0;
  90. static uint8_t tx_pin_num = 1;
  91. // UART0 and UART1 are clocked by F_CPU, UART2 is clocked by F_BUS
  92. // UART0 has 8 byte fifo, UART1 and UART2 have 1 byte buffer
  93. #ifdef HAS_KINETISK_UART0_FIFO
  94. #define C2_ENABLE UART_C2_TE | UART_C2_RE | UART_C2_RIE | UART_C2_ILIE
  95. #else
  96. #define C2_ENABLE UART_C2_TE | UART_C2_RE | UART_C2_RIE
  97. #endif
  98. #define C2_TX_ACTIVE C2_ENABLE | UART_C2_TIE
  99. #define C2_TX_COMPLETING C2_ENABLE | UART_C2_TCIE
  100. #define C2_TX_INACTIVE C2_ENABLE
  101. void serial_begin(uint32_t divisor)
  102. {
  103. SIM_SCGC4 |= SIM_SCGC4_UART0; // turn on clock, TODO: use bitband
  104. rx_buffer_head = 0;
  105. rx_buffer_tail = 0;
  106. tx_buffer_head = 0;
  107. tx_buffer_tail = 0;
  108. transmitting = 0;
  109. switch (rx_pin_num) {
  110. case 0: CORE_PIN0_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  111. case 21: CORE_PIN21_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  112. #if defined(KINETISL)
  113. case 3: CORE_PIN3_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(2); break;
  114. case 25: CORE_PIN25_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(4); break;
  115. #endif
  116. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  117. case 27: CORE_PIN27_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  118. #endif
  119. }
  120. switch (tx_pin_num) {
  121. case 1: CORE_PIN1_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3); break;
  122. case 5: CORE_PIN5_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3); break;
  123. #if defined(KINETISL)
  124. case 4: CORE_PIN4_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(2); break;
  125. case 24: CORE_PIN24_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(4); break;
  126. #endif
  127. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  128. case 26: CORE_PIN26_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3); break;
  129. #endif
  130. }
  131. #if defined(HAS_KINETISK_UART0)
  132. UART0_BDH = (divisor >> 13) & 0x1F;
  133. UART0_BDL = (divisor >> 5) & 0xFF;
  134. UART0_C4 = divisor & 0x1F;
  135. #ifdef HAS_KINETISK_UART0_FIFO
  136. UART0_C1 = UART_C1_ILT;
  137. UART0_TWFIFO = 2; // tx watermark, causes S1_TDRE to set
  138. UART0_RWFIFO = 4; // rx watermark, causes S1_RDRF to set
  139. UART0_PFIFO = UART_PFIFO_TXFE | UART_PFIFO_RXFE;
  140. #else
  141. UART0_C1 = 0;
  142. UART0_PFIFO = 0;
  143. #endif
  144. #elif defined(HAS_KINETISL_UART0)
  145. UART0_BDH = (divisor >> 8) & 0x1F;
  146. UART0_BDL = divisor & 0xFF;
  147. UART0_C1 = 0;
  148. #endif
  149. UART0_C2 = C2_TX_INACTIVE;
  150. NVIC_SET_PRIORITY(IRQ_UART0_STATUS, IRQ_PRIORITY);
  151. NVIC_ENABLE_IRQ(IRQ_UART0_STATUS);
  152. }
  153. void serial_format(uint32_t format)
  154. {
  155. uint8_t c;
  156. c = UART0_C1;
  157. c = (c & ~0x13) | (format & 0x03); // configure parity
  158. if (format & 0x04) c |= 0x10; // 9 bits (might include parity)
  159. UART0_C1 = c;
  160. if ((format & 0x0F) == 0x04) UART0_C3 |= 0x40; // 8N2 is 9 bit with 9th bit always 1
  161. c = UART0_S2 & ~0x10;
  162. if (format & 0x10) c |= 0x10; // rx invert
  163. UART0_S2 = c;
  164. c = UART0_C3 & ~0x10;
  165. if (format & 0x20) c |= 0x10; // tx invert
  166. UART0_C3 = c;
  167. #ifdef SERIAL_9BIT_SUPPORT
  168. c = UART0_C4 & 0x1F;
  169. if (format & 0x08) c |= 0x20; // 9 bit mode with parity (requires 10 bits)
  170. UART0_C4 = c;
  171. use9Bits = format & 0x80;
  172. #endif
  173. #if defined(__MK64FX512__) || defined(__MK66FX1M0__) || defined(KINETISL)
  174. // For T3.5/T3.6/TLC See about turning on 2 stop bit mode
  175. if ( format & 0x100) {
  176. uint8_t bdl = UART0_BDL;
  177. UART0_BDH |= UART_BDH_SBNS; // Turn on 2 stop bits - was turned off by set baud
  178. UART0_BDL = bdl; // Says BDH not acted on until BDL is written
  179. }
  180. #endif
  181. }
  182. void serial_end(void)
  183. {
  184. if (!(SIM_SCGC4 & SIM_SCGC4_UART0)) return;
  185. while (transmitting) yield(); // wait for buffered data to send
  186. NVIC_DISABLE_IRQ(IRQ_UART0_STATUS);
  187. UART0_C2 = 0;
  188. switch (rx_pin_num) {
  189. case 0: CORE_PIN0_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); break;
  190. case 21: CORE_PIN21_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); break;
  191. #if defined(KINETISL)
  192. case 3: CORE_PIN3_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); break;
  193. case 25: CORE_PIN25_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); break;
  194. #endif
  195. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  196. case 27: CORE_PIN27_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); break;
  197. #endif
  198. }
  199. switch (tx_pin_num & 127) {
  200. case 1: CORE_PIN1_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); break;
  201. case 5: CORE_PIN5_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); break;
  202. #if defined(KINETISL)
  203. case 4: CORE_PIN4_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); break;
  204. case 24: CORE_PIN24_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); break;
  205. #endif
  206. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  207. case 26: CORE_PIN26_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); break;
  208. #endif
  209. }
  210. rx_buffer_head = 0;
  211. rx_buffer_tail = 0;
  212. if (rts_pin) rts_deassert();
  213. }
  214. void serial_set_transmit_pin(uint8_t pin)
  215. {
  216. while (transmitting) ;
  217. pinMode(pin, OUTPUT);
  218. digitalWrite(pin, LOW);
  219. transmit_pin = portOutputRegister(pin);
  220. #if defined(KINETISL)
  221. transmit_mask = digitalPinToBitMask(pin);
  222. #endif
  223. }
  224. void serial_set_tx(uint8_t pin, uint8_t opendrain)
  225. {
  226. uint32_t cfg;
  227. if (opendrain) pin |= 128;
  228. if (pin == tx_pin_num) return;
  229. if ((SIM_SCGC4 & SIM_SCGC4_UART0)) {
  230. switch (tx_pin_num & 127) {
  231. case 1: CORE_PIN1_CONFIG = 0; break; // PTB17
  232. case 5: CORE_PIN5_CONFIG = 0; break; // PTD7
  233. #if defined(KINETISL)
  234. case 4: CORE_PIN4_CONFIG = 0; break; // PTA2
  235. case 24: CORE_PIN24_CONFIG = 0; break; // PTE20
  236. #endif
  237. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  238. case 26: CORE_PIN26_CONFIG = 0; break; //PTA14
  239. #endif
  240. }
  241. if (opendrain) {
  242. cfg = PORT_PCR_DSE | PORT_PCR_ODE;
  243. } else {
  244. cfg = PORT_PCR_DSE | PORT_PCR_SRE;
  245. }
  246. switch (pin & 127) {
  247. case 1: CORE_PIN1_CONFIG = cfg | PORT_PCR_MUX(3); break;
  248. case 5: CORE_PIN5_CONFIG = cfg | PORT_PCR_MUX(3); break;
  249. #if defined(KINETISL)
  250. case 4: CORE_PIN4_CONFIG = cfg | PORT_PCR_MUX(2); break;
  251. case 24: CORE_PIN24_CONFIG = cfg | PORT_PCR_MUX(4); break;
  252. #endif
  253. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  254. case 26: CORE_PIN26_CONFIG = cfg | PORT_PCR_MUX(3); break;
  255. #endif
  256. }
  257. }
  258. tx_pin_num = pin;
  259. }
  260. void serial_set_rx(uint8_t pin)
  261. {
  262. if (pin == rx_pin_num) return;
  263. if ((SIM_SCGC4 & SIM_SCGC4_UART0)) {
  264. switch (rx_pin_num) {
  265. case 0: CORE_PIN0_CONFIG = 0; break; // PTB16
  266. case 21: CORE_PIN21_CONFIG = 0; break; // PTD6
  267. #if defined(KINETISL)
  268. case 3: CORE_PIN3_CONFIG = 0; break; // PTA1
  269. case 25: CORE_PIN25_CONFIG = 0; break; // PTE21
  270. #endif
  271. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  272. case 27: CORE_PIN27_CONFIG = 0; break; // PTA15
  273. #endif
  274. }
  275. switch (pin) {
  276. case 0: CORE_PIN0_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  277. case 21: CORE_PIN21_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  278. #if defined(KINETISL)
  279. case 3: CORE_PIN3_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(2); break;
  280. case 25: CORE_PIN25_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(4); break;
  281. #endif
  282. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  283. case 27: CORE_PIN27_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  284. #endif
  285. }
  286. }
  287. rx_pin_num = pin;
  288. }
  289. int serial_set_rts(uint8_t pin)
  290. {
  291. if (!(SIM_SCGC4 & SIM_SCGC4_UART0)) return 0;
  292. if (pin < CORE_NUM_DIGITAL) {
  293. rts_pin = portOutputRegister(pin);
  294. #if defined(KINETISL)
  295. rts_mask = digitalPinToBitMask(pin);
  296. #endif
  297. pinMode(pin, OUTPUT);
  298. rts_assert();
  299. } else {
  300. rts_pin = NULL;
  301. return 0;
  302. }
  303. /*
  304. if (pin == 6) {
  305. CORE_PIN6_CONFIG = PORT_PCR_MUX(3);
  306. } else if (pin == 19) {
  307. CORE_PIN19_CONFIG = PORT_PCR_MUX(3);
  308. } else {
  309. UART0_MODEM &= ~UART_MODEM_RXRTSE;
  310. return 0;
  311. }
  312. UART0_MODEM |= UART_MODEM_RXRTSE;
  313. */
  314. return 1;
  315. }
  316. int serial_set_cts(uint8_t pin)
  317. {
  318. #if defined(KINETISK)
  319. if (!(SIM_SCGC4 & SIM_SCGC4_UART0)) return 0;
  320. if (pin == 18) {
  321. CORE_PIN18_CONFIG = PORT_PCR_MUX(3) | PORT_PCR_PE; // weak pulldown
  322. } else if (pin == 20) {
  323. CORE_PIN20_CONFIG = PORT_PCR_MUX(3) | PORT_PCR_PE; // weak pulldown
  324. } else {
  325. UART0_MODEM &= ~UART_MODEM_TXCTSE;
  326. return 0;
  327. }
  328. UART0_MODEM |= UART_MODEM_TXCTSE;
  329. return 1;
  330. #else
  331. return 0;
  332. #endif
  333. }
  334. void serial_putchar(uint32_t c)
  335. {
  336. uint32_t head, n;
  337. if (!(SIM_SCGC4 & SIM_SCGC4_UART0)) return;
  338. if (transmit_pin) transmit_assert();
  339. head = tx_buffer_head;
  340. if (++head >= SERIAL1_TX_BUFFER_SIZE) head = 0;
  341. while (tx_buffer_tail == head) {
  342. int priority = nvic_execution_priority();
  343. if (priority <= IRQ_PRIORITY) {
  344. if ((UART0_S1 & UART_S1_TDRE)) {
  345. uint32_t tail = tx_buffer_tail;
  346. if (++tail >= SERIAL1_TX_BUFFER_SIZE) tail = 0;
  347. n = tx_buffer[tail];
  348. if (use9Bits) UART0_C3 = (UART0_C3 & ~0x40) | ((n & 0x100) >> 2);
  349. UART0_D = n;
  350. tx_buffer_tail = tail;
  351. }
  352. } else if (priority >= 256) {
  353. yield();
  354. }
  355. }
  356. tx_buffer[head] = c;
  357. transmitting = 1;
  358. tx_buffer_head = head;
  359. UART0_C2 = C2_TX_ACTIVE;
  360. }
  361. #ifdef HAS_KINETISK_UART0_FIFO
  362. void serial_write(const void *buf, unsigned int count)
  363. {
  364. const uint8_t *p = (const uint8_t *)buf;
  365. const uint8_t *end = p + count;
  366. uint32_t head, n;
  367. if (!(SIM_SCGC4 & SIM_SCGC4_UART0)) return;
  368. if (transmit_pin) transmit_assert();
  369. while (p < end) {
  370. head = tx_buffer_head;
  371. if (++head >= SERIAL1_TX_BUFFER_SIZE) head = 0;
  372. if (tx_buffer_tail == head) {
  373. UART0_C2 = C2_TX_ACTIVE;
  374. do {
  375. int priority = nvic_execution_priority();
  376. if (priority <= IRQ_PRIORITY) {
  377. if ((UART0_S1 & UART_S1_TDRE)) {
  378. uint32_t tail = tx_buffer_tail;
  379. if (++tail >= SERIAL1_TX_BUFFER_SIZE) tail = 0;
  380. n = tx_buffer[tail];
  381. if (use9Bits) UART0_C3 = (UART0_C3 & ~0x40) | ((n & 0x100) >> 2);
  382. UART0_D = n;
  383. tx_buffer_tail = tail;
  384. }
  385. } else if (priority >= 256) {
  386. yield();
  387. }
  388. } while (tx_buffer_tail == head);
  389. }
  390. tx_buffer[head] = *p++;
  391. transmitting = 1;
  392. tx_buffer_head = head;
  393. }
  394. UART0_C2 = C2_TX_ACTIVE;
  395. }
  396. #else
  397. void serial_write(const void *buf, unsigned int count)
  398. {
  399. const uint8_t *p = (const uint8_t *)buf;
  400. while (count-- > 0) serial_putchar(*p++);
  401. }
  402. #endif
  403. void serial_flush(void)
  404. {
  405. while (transmitting) yield(); // wait
  406. }
  407. int serial_write_buffer_free(void)
  408. {
  409. uint32_t head, tail;
  410. head = tx_buffer_head;
  411. tail = tx_buffer_tail;
  412. if (head >= tail) return SERIAL1_TX_BUFFER_SIZE - 1 - head + tail;
  413. return tail - head - 1;
  414. }
  415. int serial_available(void)
  416. {
  417. uint32_t head, tail;
  418. head = rx_buffer_head;
  419. tail = rx_buffer_tail;
  420. if (head >= tail) return head - tail;
  421. return SERIAL1_RX_BUFFER_SIZE + head - tail;
  422. }
  423. int serial_getchar(void)
  424. {
  425. uint32_t head, tail;
  426. int c;
  427. head = rx_buffer_head;
  428. tail = rx_buffer_tail;
  429. if (head == tail) return -1;
  430. if (++tail >= SERIAL1_RX_BUFFER_SIZE) tail = 0;
  431. c = rx_buffer[tail];
  432. rx_buffer_tail = tail;
  433. if (rts_pin) {
  434. int avail;
  435. if (head >= tail) avail = head - tail;
  436. else avail = SERIAL1_RX_BUFFER_SIZE + head - tail;
  437. if (avail <= RTS_LOW_WATERMARK) rts_assert();
  438. }
  439. return c;
  440. }
  441. int serial_peek(void)
  442. {
  443. uint32_t head, tail;
  444. head = rx_buffer_head;
  445. tail = rx_buffer_tail;
  446. if (head == tail) return -1;
  447. if (++tail >= SERIAL1_RX_BUFFER_SIZE) tail = 0;
  448. return rx_buffer[tail];
  449. }
  450. void serial_clear(void)
  451. {
  452. #ifdef HAS_KINETISK_UART0_FIFO
  453. if (!(SIM_SCGC4 & SIM_SCGC4_UART0)) return;
  454. UART0_C2 &= ~(UART_C2_RE | UART_C2_RIE | UART_C2_ILIE);
  455. UART0_CFIFO = UART_CFIFO_RXFLUSH;
  456. UART0_C2 |= (UART_C2_RE | UART_C2_RIE | UART_C2_ILIE);
  457. #endif
  458. rx_buffer_head = rx_buffer_tail;
  459. if (rts_pin) rts_assert();
  460. }
  461. // status interrupt combines
  462. // Transmit data below watermark UART_S1_TDRE
  463. // Transmit complete UART_S1_TC
  464. // Idle line UART_S1_IDLE
  465. // Receive data above watermark UART_S1_RDRF
  466. // LIN break detect UART_S2_LBKDIF
  467. // RxD pin active edge UART_S2_RXEDGIF
  468. void uart0_status_isr(void)
  469. {
  470. uint32_t head, tail, n;
  471. uint8_t c;
  472. #ifdef HAS_KINETISK_UART0_FIFO
  473. uint32_t newhead;
  474. uint8_t avail;
  475. if (UART0_S1 & (UART_S1_RDRF | UART_S1_IDLE)) {
  476. __disable_irq();
  477. avail = UART0_RCFIFO;
  478. if (avail == 0) {
  479. // The only way to clear the IDLE interrupt flag is
  480. // to read the data register. But reading with no
  481. // data causes a FIFO underrun, which causes the
  482. // FIFO to return corrupted data. If anyone from
  483. // Freescale reads this, what a poor design! There
  484. // write should be a write-1-to-clear for IDLE.
  485. c = UART0_D;
  486. // flushing the fifo recovers from the underrun,
  487. // but there's a possible race condition where a
  488. // new character could be received between reading
  489. // RCFIFO == 0 and flushing the FIFO. To minimize
  490. // the chance, interrupts are disabled so a higher
  491. // priority interrupt (hopefully) doesn't delay.
  492. // TODO: change this to disabling the IDLE interrupt
  493. // which won't be simple, since we already manage
  494. // which transmit interrupts are enabled.
  495. UART0_CFIFO = UART_CFIFO_RXFLUSH;
  496. __enable_irq();
  497. } else {
  498. __enable_irq();
  499. head = rx_buffer_head;
  500. tail = rx_buffer_tail;
  501. do {
  502. if (use9Bits && (UART0_C3 & 0x80)) {
  503. n = UART0_D | 0x100;
  504. } else {
  505. n = UART0_D;
  506. }
  507. newhead = head + 1;
  508. if (newhead >= SERIAL1_RX_BUFFER_SIZE) newhead = 0;
  509. if (newhead != tail) {
  510. head = newhead;
  511. rx_buffer[head] = n;
  512. }
  513. } while (--avail > 0);
  514. rx_buffer_head = head;
  515. if (rts_pin) {
  516. int avail;
  517. if (head >= tail) avail = head - tail;
  518. else avail = SERIAL1_RX_BUFFER_SIZE + head - tail;
  519. if (avail >= RTS_HIGH_WATERMARK) rts_deassert();
  520. }
  521. }
  522. }
  523. c = UART0_C2;
  524. if ((c & UART_C2_TIE) && (UART0_S1 & UART_S1_TDRE)) {
  525. head = tx_buffer_head;
  526. tail = tx_buffer_tail;
  527. do {
  528. if (tail == head) break;
  529. if (++tail >= SERIAL1_TX_BUFFER_SIZE) tail = 0;
  530. avail = UART0_S1;
  531. n = tx_buffer[tail];
  532. if (use9Bits) UART0_C3 = (UART0_C3 & ~0x40) | ((n & 0x100) >> 2);
  533. UART0_D = n;
  534. } while (UART0_TCFIFO < 8);
  535. tx_buffer_tail = tail;
  536. if (UART0_S1 & UART_S1_TDRE) UART0_C2 = C2_TX_COMPLETING;
  537. }
  538. #else
  539. if (UART0_S1 & UART_S1_RDRF) {
  540. if (use9Bits && (UART0_C3 & 0x80)) {
  541. n = UART0_D | 0x100;
  542. } else {
  543. n = UART0_D;
  544. }
  545. head = rx_buffer_head + 1;
  546. if (head >= SERIAL1_RX_BUFFER_SIZE) head = 0;
  547. if (head != rx_buffer_tail) {
  548. rx_buffer[head] = n;
  549. rx_buffer_head = head;
  550. }
  551. }
  552. c = UART0_C2;
  553. if ((c & UART_C2_TIE) && (UART0_S1 & UART_S1_TDRE)) {
  554. head = tx_buffer_head;
  555. tail = tx_buffer_tail;
  556. if (head == tail) {
  557. UART0_C2 = C2_TX_COMPLETING;
  558. } else {
  559. if (++tail >= SERIAL1_TX_BUFFER_SIZE) tail = 0;
  560. n = tx_buffer[tail];
  561. if (use9Bits) UART0_C3 = (UART0_C3 & ~0x40) | ((n & 0x100) >> 2);
  562. UART0_D = n;
  563. tx_buffer_tail = tail;
  564. }
  565. }
  566. #endif
  567. if ((c & UART_C2_TCIE) && (UART0_S1 & UART_S1_TC)) {
  568. transmitting = 0;
  569. if (transmit_pin) transmit_deassert();
  570. UART0_C2 = C2_TX_INACTIVE;
  571. }
  572. }
  573. void serial_print(const char *p)
  574. {
  575. while (*p) {
  576. char c = *p++;
  577. if (c == '\n') serial_putchar('\r');
  578. serial_putchar(c);
  579. }
  580. }
  581. static void serial_phex1(uint32_t n)
  582. {
  583. n &= 15;
  584. if (n < 10) {
  585. serial_putchar('0' + n);
  586. } else {
  587. serial_putchar('A' - 10 + n);
  588. }
  589. }
  590. void serial_phex(uint32_t n)
  591. {
  592. serial_phex1(n >> 4);
  593. serial_phex1(n);
  594. }
  595. void serial_phex16(uint32_t n)
  596. {
  597. serial_phex(n >> 8);
  598. serial_phex(n);
  599. }
  600. void serial_phex32(uint32_t n)
  601. {
  602. serial_phex(n >> 24);
  603. serial_phex(n >> 16);
  604. serial_phex(n >> 8);
  605. serial_phex(n);
  606. }