Teensy 4.1 core updated for C++20
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

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