Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

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