您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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