Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

610 lines
17KB

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