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.

574 lines
16KB

  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. #include <stddef.h>
  34. ////////////////////////////////////////////////////////////////
  35. // Tunable parameters (relatively safe to edit these numbers)
  36. ////////////////////////////////////////////////////////////////
  37. #ifndef SERIAL3_TX_BUFFER_SIZE
  38. #define SERIAL3_TX_BUFFER_SIZE 40 // number of outgoing bytes to buffer
  39. #endif
  40. #ifndef SERIAL3_RX_BUFFER_SIZE
  41. #define SERIAL3_RX_BUFFER_SIZE 64 // number of incoming bytes to buffer
  42. #endif
  43. #define RTS_HIGH_WATERMARK (SERIAL3_RX_BUFFER_SIZE-24) // RTS requests sender to pause
  44. #define RTS_LOW_WATERMARK (SERIAL3_RX_BUFFER_SIZE-38) // RTS allows sender to resume
  45. #define IRQ_PRIORITY 64 // 0 = highest priority, 255 = lowest
  46. ////////////////////////////////////////////////////////////////
  47. // changes not recommended below this point....
  48. ////////////////////////////////////////////////////////////////
  49. #ifdef SERIAL_9BIT_SUPPORT
  50. static uint8_t use9Bits = 0;
  51. #define BUFTYPE uint16_t
  52. #else
  53. #define BUFTYPE uint8_t
  54. #define use9Bits 0
  55. #endif
  56. static volatile BUFTYPE tx_buffer[SERIAL3_TX_BUFFER_SIZE];
  57. static volatile BUFTYPE rx_buffer[SERIAL3_RX_BUFFER_SIZE];
  58. static volatile BUFTYPE *rx_buffer_storage_ = NULL;
  59. static volatile BUFTYPE *tx_buffer_storage_ = NULL;
  60. static size_t tx_buffer_total_size_ = SERIAL3_TX_BUFFER_SIZE;
  61. static size_t rx_buffer_total_size_ = SERIAL3_RX_BUFFER_SIZE;
  62. static size_t rts_low_watermark_ = RTS_LOW_WATERMARK;
  63. static size_t rts_high_watermark_ = RTS_HIGH_WATERMARK;
  64. static volatile uint8_t transmitting = 0;
  65. #if defined(KINETISK)
  66. static volatile uint8_t *transmit_pin=NULL;
  67. #define transmit_assert() *transmit_pin = 1
  68. #define transmit_deassert() *transmit_pin = 0
  69. static volatile uint8_t *rts_pin=NULL;
  70. #define rts_assert() *rts_pin = 0
  71. #define rts_deassert() *rts_pin = 1
  72. #elif defined(KINETISL)
  73. static volatile uint8_t *transmit_pin=NULL;
  74. static uint8_t transmit_mask=0;
  75. #define transmit_assert() *(transmit_pin+4) = transmit_mask;
  76. #define transmit_deassert() *(transmit_pin+8) = transmit_mask;
  77. static volatile uint8_t *rts_pin=NULL;
  78. static uint8_t rts_mask=0;
  79. #define rts_assert() *(rts_pin+8) = rts_mask;
  80. #define rts_deassert() *(rts_pin+4) = rts_mask;
  81. #endif
  82. #if SERIAL3_TX_BUFFER_SIZE > 65535
  83. static volatile uint32_t tx_buffer_head = 0;
  84. static volatile uint32_t tx_buffer_tail = 0;
  85. #elif SERIAL3_TX_BUFFER_SIZE > 255
  86. static volatile uint16_t tx_buffer_head = 0;
  87. static volatile uint16_t tx_buffer_tail = 0;
  88. #else
  89. static volatile uint8_t tx_buffer_head = 0;
  90. static volatile uint8_t tx_buffer_tail = 0;
  91. #endif
  92. #if SERIAL3_RX_BUFFER_SIZE > 65535
  93. static volatile uint32_t rx_buffer_head = 0;
  94. static volatile uint32_t rx_buffer_tail = 0;
  95. #elif SERIAL3_RX_BUFFER_SIZE > 255
  96. static volatile uint16_t rx_buffer_head = 0;
  97. static volatile uint16_t rx_buffer_tail = 0;
  98. #else
  99. static volatile uint8_t rx_buffer_head = 0;
  100. static volatile uint8_t rx_buffer_tail = 0;
  101. #endif
  102. #if defined(KINETISL)
  103. static uint8_t rx_pin_num = 7;
  104. #endif
  105. static uint8_t tx_pin_num = 8;
  106. #if defined(KINETISL)
  107. static uint8_t half_duplex_mode = 0;
  108. #endif
  109. // UART0 and UART1 are clocked by F_CPU, UART2 is clocked by F_BUS
  110. // UART0 has 8 byte fifo, UART1 and UART2 have 1 byte buffer
  111. #define C2_ENABLE UART_C2_TE | UART_C2_RE | UART_C2_RIE
  112. #define C2_TX_ACTIVE C2_ENABLE | UART_C2_TIE
  113. #define C2_TX_COMPLETING C2_ENABLE | UART_C2_TCIE
  114. #define C2_TX_INACTIVE C2_ENABLE
  115. // BITBAND Support
  116. #define GPIO_BITBAND_ADDR(reg, bit) (((uint32_t)&(reg) - 0x40000000) * 32 + (bit) * 4 + 0x42000000)
  117. #define GPIO_BITBAND_PTR(reg, bit) ((uint32_t *)GPIO_BITBAND_ADDR((reg), (bit)))
  118. #define C3_TXDIR_BIT 5
  119. void serial3_begin(uint32_t divisor)
  120. {
  121. SIM_SCGC4 |= SIM_SCGC4_UART2; // turn on clock, TODO: use bitband
  122. rx_buffer_head = 0;
  123. rx_buffer_tail = 0;
  124. tx_buffer_head = 0;
  125. tx_buffer_tail = 0;
  126. transmitting = 0;
  127. #if defined(KINETISK)
  128. CORE_PIN7_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3);
  129. CORE_PIN8_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3);
  130. #elif defined(KINETISL)
  131. switch (rx_pin_num) {
  132. case 7: CORE_PIN7_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  133. case 6: CORE_PIN6_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  134. }
  135. switch (tx_pin_num) {
  136. case 8: CORE_PIN8_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3); break;
  137. case 20: CORE_PIN20_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3); break;
  138. }
  139. #endif
  140. #if defined(HAS_KINETISK_UART2)
  141. if (divisor < 32) divisor = 32;
  142. UART2_BDH = (divisor >> 13) & 0x1F;
  143. UART2_BDL = (divisor >> 5) & 0xFF;
  144. UART2_C4 = divisor & 0x1F;
  145. UART2_C1 = 0;
  146. UART2_PFIFO = 0;
  147. #elif defined(HAS_KINETISL_UART2)
  148. if (divisor < 1) divisor = 1;
  149. UART2_BDH = (divisor >> 8) & 0x1F;
  150. UART2_BDL = divisor & 0xFF;
  151. UART2_C1 = 0;
  152. #endif
  153. UART2_C2 = C2_TX_INACTIVE;
  154. NVIC_SET_PRIORITY(IRQ_UART2_STATUS, IRQ_PRIORITY);
  155. NVIC_ENABLE_IRQ(IRQ_UART2_STATUS);
  156. }
  157. void serial3_format(uint32_t format)
  158. {
  159. uint8_t c;
  160. c = UART2_C1;
  161. c = (c & ~0x13) | (format & 0x03); // configure parity
  162. if (format & 0x04) c |= 0x10; // 9 bits (might include parity)
  163. UART2_C1 = c;
  164. if ((format & 0x0F) == 0x04) UART2_C3 |= 0x40; // 8N2 is 9 bit with 9th bit always 1
  165. c = UART2_S2 & ~0x10;
  166. if (format & 0x10) c |= 0x10; // rx invert
  167. UART2_S2 = c;
  168. c = UART2_C3 & ~0x10;
  169. if (format & 0x20) c |= 0x10; // tx invert
  170. UART2_C3 = c;
  171. #if defined(SERIAL_9BIT_SUPPORT) && !defined(KINETISL)
  172. c = UART2_C4 & 0x1F;
  173. if (format & 0x08) c |= 0x20; // 9 bit mode with parity (requires 10 bits)
  174. UART2_C4 = c;
  175. use9Bits = format & 0x80;
  176. #endif
  177. #if defined(__MK64FX512__) || defined(__MK66FX1M0__) || defined(KINETISL)
  178. // For T3.5/T3.6/TLC See about turning on 2 stop bit mode
  179. if ( format & 0x100) {
  180. uint8_t bdl = UART2_BDL;
  181. UART2_BDH |= UART_BDH_SBNS; // Turn on 2 stop bits - was turned off by set baud
  182. UART2_BDL = bdl; // Says BDH not acted on until BDL is written
  183. }
  184. #endif
  185. // process request for half duplex.
  186. if ((format & SERIAL_HALF_DUPLEX) != 0) {
  187. c = UART2_C1;
  188. c |= UART_C1_LOOPS | UART_C1_RSRC;
  189. UART2_C1 = c;
  190. // Lets try to make use of bitband address to set the direction for ue...
  191. #if defined(KINETISL)
  192. switch (tx_pin_num) {
  193. case 8: CORE_PIN8_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  194. case 20: CORE_PIN20_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  195. }
  196. half_duplex_mode = 1;
  197. #else
  198. volatile uint32_t *reg = portConfigRegister(tx_pin_num);
  199. *reg = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3) | PORT_PCR_PE | PORT_PCR_PS; // pullup on output pin;
  200. transmit_pin = (uint8_t*)GPIO_BITBAND_PTR(UART2_C3, C3_TXDIR_BIT);
  201. #endif
  202. } else {
  203. #if defined(KINETISL)
  204. half_duplex_mode = 0;
  205. #else
  206. if (transmit_pin == (uint8_t*)GPIO_BITBAND_PTR(UART2_C3, C3_TXDIR_BIT)) transmit_pin = NULL;
  207. #endif
  208. }
  209. }
  210. void serial3_end(void)
  211. {
  212. if (!(SIM_SCGC4 & SIM_SCGC4_UART2)) return;
  213. while (transmitting) yield(); // wait for buffered data to send
  214. NVIC_DISABLE_IRQ(IRQ_UART2_STATUS);
  215. UART2_C2 = 0;
  216. #if defined(KINETISK)
  217. CORE_PIN7_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1);
  218. CORE_PIN8_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1);
  219. #elif defined(KINETISL)
  220. switch (rx_pin_num) {
  221. case 7: CORE_PIN7_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); break;
  222. case 6: CORE_PIN6_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); break;
  223. }
  224. switch (tx_pin_num & 127) {
  225. case 8: CORE_PIN8_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); break;
  226. case 20: CORE_PIN20_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); break;
  227. }
  228. #endif
  229. UART2_S1;
  230. UART2_D; // clear leftover error status
  231. rx_buffer_head = 0;
  232. rx_buffer_tail = 0;
  233. if (rts_pin) rts_deassert();
  234. }
  235. void serial3_set_transmit_pin(uint8_t pin)
  236. {
  237. while (transmitting) ;
  238. pinMode(pin, OUTPUT);
  239. digitalWrite(pin, LOW);
  240. transmit_pin = portOutputRegister(pin);
  241. #if defined(KINETISL)
  242. transmit_mask = digitalPinToBitMask(pin);
  243. #endif
  244. }
  245. void serial3_set_tx(uint8_t pin, uint8_t opendrain)
  246. {
  247. uint32_t cfg;
  248. if (opendrain) pin |= 128;
  249. if (pin == tx_pin_num) return;
  250. if ((SIM_SCGC4 & SIM_SCGC4_UART2)) {
  251. switch (tx_pin_num & 127) {
  252. case 8: CORE_PIN8_CONFIG = 0; break; // PTD3
  253. #if defined(KINETISL)
  254. case 20: CORE_PIN20_CONFIG = 0; break; // PTD5
  255. #endif
  256. }
  257. if (opendrain) {
  258. cfg = PORT_PCR_DSE | PORT_PCR_ODE;
  259. } else {
  260. cfg = PORT_PCR_DSE | PORT_PCR_SRE;
  261. }
  262. switch (pin & 127) {
  263. case 8: CORE_PIN8_CONFIG = cfg | PORT_PCR_MUX(3); break;
  264. #if defined(KINETISL)
  265. case 20: CORE_PIN20_CONFIG = cfg | PORT_PCR_MUX(3); break;
  266. #endif
  267. }
  268. }
  269. tx_pin_num = pin;
  270. }
  271. void serial3_set_rx(uint8_t pin)
  272. {
  273. #if defined(KINETISL)
  274. if (pin == rx_pin_num) return;
  275. if ((SIM_SCGC4 & SIM_SCGC4_UART2)) {
  276. switch (rx_pin_num) {
  277. case 7: CORE_PIN7_CONFIG = 0; break; // PTD2
  278. case 6: CORE_PIN6_CONFIG = 0; break; // PTD4
  279. }
  280. switch (pin) {
  281. case 7: CORE_PIN7_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  282. case 6: CORE_PIN6_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  283. }
  284. }
  285. rx_pin_num = pin;
  286. #endif
  287. }
  288. int serial3_set_rts(uint8_t pin)
  289. {
  290. if (!(SIM_SCGC4 & SIM_SCGC4_UART2)) return 0;
  291. if (pin < CORE_NUM_DIGITAL) {
  292. rts_pin = portOutputRegister(pin);
  293. #if defined(KINETISL)
  294. rts_mask = digitalPinToBitMask(pin);
  295. #endif
  296. pinMode(pin, OUTPUT);
  297. rts_assert();
  298. } else {
  299. rts_pin = NULL;
  300. return 0;
  301. }
  302. /*
  303. if (pin == 2) {
  304. CORE_PIN2_CONFIG = PORT_PCR_MUX(3);
  305. } else {
  306. UART2_MODEM &= ~UART_MODEM_RXRTSE;
  307. return 0;
  308. }
  309. UART2_MODEM |= UART_MODEM_RXRTSE;
  310. */
  311. return 1;
  312. }
  313. int serial3_set_cts(uint8_t pin)
  314. {
  315. #if defined(KINETISK)
  316. if (!(SIM_SCGC4 & SIM_SCGC4_UART2)) return 0;
  317. if (pin == 14) {
  318. CORE_PIN14_CONFIG = PORT_PCR_MUX(3) | PORT_PCR_PE; // weak pulldown
  319. } else {
  320. UART2_MODEM &= ~UART_MODEM_TXCTSE;
  321. return 0;
  322. }
  323. UART2_MODEM |= UART_MODEM_TXCTSE;
  324. return 1;
  325. #else
  326. return 0;
  327. #endif
  328. }
  329. void serial3_putchar(uint32_t c)
  330. {
  331. uint32_t head, n;
  332. if (!(SIM_SCGC4 & SIM_SCGC4_UART2)) return;
  333. if (transmit_pin) transmit_assert();
  334. #if defined(KINETISL)
  335. if (half_duplex_mode) {
  336. __disable_irq();
  337. volatile uint32_t reg = UART2_C3;
  338. reg |= UART_C3_TXDIR;
  339. UART2_C3 = reg;
  340. __enable_irq();
  341. }
  342. #endif
  343. head = tx_buffer_head;
  344. if (++head >= tx_buffer_total_size_) head = 0;
  345. while (tx_buffer_tail == head) {
  346. int priority = nvic_execution_priority();
  347. if (priority <= IRQ_PRIORITY) {
  348. if ((UART2_S1 & UART_S1_TDRE)) {
  349. uint32_t tail = tx_buffer_tail;
  350. if (++tail >= tx_buffer_total_size_) tail = 0;
  351. if (tail < SERIAL3_TX_BUFFER_SIZE) {
  352. n = tx_buffer[tail];
  353. } else {
  354. n = tx_buffer_storage_[tail-SERIAL3_TX_BUFFER_SIZE];
  355. }
  356. if (use9Bits) UART2_C3 = (UART2_C3 & ~0x40) | ((n & 0x100) >> 2);
  357. UART2_D = n;
  358. tx_buffer_tail = tail;
  359. }
  360. } else if (priority >= 256) {
  361. yield(); // wait
  362. }
  363. }
  364. if (head < SERIAL3_TX_BUFFER_SIZE) {
  365. tx_buffer[head] = c;
  366. } else {
  367. tx_buffer_storage_[head - SERIAL3_TX_BUFFER_SIZE] = c;
  368. }
  369. transmitting = 1;
  370. tx_buffer_head = head;
  371. UART2_C2 = C2_TX_ACTIVE;
  372. }
  373. void serial3_write(const void *buf, unsigned int count)
  374. {
  375. const uint8_t *p = (const uint8_t *)buf;
  376. while (count-- > 0) serial3_putchar(*p++);
  377. }
  378. void serial3_flush(void)
  379. {
  380. while (transmitting) yield(); // wait
  381. }
  382. int serial3_write_buffer_free(void)
  383. {
  384. uint32_t head, tail;
  385. head = tx_buffer_head;
  386. tail = tx_buffer_tail;
  387. if (head >= tail) return tx_buffer_total_size_ - 1 - head + tail;
  388. return tail - head - 1;
  389. }
  390. int serial3_available(void)
  391. {
  392. uint32_t head, tail;
  393. head = rx_buffer_head;
  394. tail = rx_buffer_tail;
  395. if (head >= tail) return head - tail;
  396. return rx_buffer_total_size_ + head - tail;
  397. }
  398. int serial3_getchar(void)
  399. {
  400. uint32_t head, tail;
  401. int c;
  402. head = rx_buffer_head;
  403. tail = rx_buffer_tail;
  404. if (head == tail) return -1;
  405. if (++tail >= rx_buffer_total_size_) tail = 0;
  406. if (tail < SERIAL3_RX_BUFFER_SIZE) {
  407. c = rx_buffer[tail];
  408. } else {
  409. c = rx_buffer_storage_[tail-SERIAL3_RX_BUFFER_SIZE];
  410. }
  411. rx_buffer_tail = tail;
  412. if (rts_pin) {
  413. int avail;
  414. if (head >= tail) avail = head - tail;
  415. else avail = rx_buffer_total_size_ + head - tail;
  416. if (avail <= rts_low_watermark_) rts_assert();
  417. }
  418. return c;
  419. }
  420. int serial3_peek(void)
  421. {
  422. uint32_t head, tail;
  423. head = rx_buffer_head;
  424. tail = rx_buffer_tail;
  425. if (head == tail) return -1;
  426. if (++tail >= rx_buffer_total_size_) tail = 0;
  427. if (tail < SERIAL3_RX_BUFFER_SIZE) {
  428. return rx_buffer[tail];
  429. }
  430. return rx_buffer_storage_[tail-SERIAL3_RX_BUFFER_SIZE];
  431. }
  432. void serial3_clear(void)
  433. {
  434. rx_buffer_head = rx_buffer_tail;
  435. if (rts_pin) rts_assert();
  436. }
  437. // status interrupt combines
  438. // Transmit data below watermark UART_S1_TDRE
  439. // Transmit complete UART_S1_TC
  440. // Idle line UART_S1_IDLE
  441. // Receive data above watermark UART_S1_RDRF
  442. // LIN break detect UART_S2_LBKDIF
  443. // RxD pin active edge UART_S2_RXEDGIF
  444. void uart2_status_isr(void)
  445. {
  446. uint32_t head, tail, n;
  447. uint8_t c;
  448. if (UART2_S1 & UART_S1_RDRF) {
  449. if (use9Bits && (UART2_C3 & 0x80)) {
  450. n = UART2_D | 0x100;
  451. } else {
  452. n = UART2_D;
  453. }
  454. head = rx_buffer_head + 1;
  455. if (head >= rx_buffer_total_size_) head = 0;
  456. if (head != rx_buffer_tail) {
  457. if (head < SERIAL3_RX_BUFFER_SIZE) {
  458. rx_buffer[head] = n;
  459. } else {
  460. rx_buffer_storage_[head-SERIAL3_RX_BUFFER_SIZE] = n;
  461. }
  462. rx_buffer_head = head;
  463. }
  464. if (rts_pin) {
  465. int avail;
  466. tail = tx_buffer_tail;
  467. if (head >= tail) avail = head - tail;
  468. else avail = rx_buffer_total_size_ + head - tail;
  469. if (avail >= rts_high_watermark_) rts_deassert();
  470. }
  471. }
  472. c = UART2_C2;
  473. if ((c & UART_C2_TIE) && (UART2_S1 & UART_S1_TDRE)) {
  474. head = tx_buffer_head;
  475. tail = tx_buffer_tail;
  476. if (head == tail) {
  477. UART2_C2 = C2_TX_COMPLETING;
  478. } else {
  479. if (++tail >= tx_buffer_total_size_) tail = 0;
  480. if (tail < SERIAL3_TX_BUFFER_SIZE) {
  481. n = tx_buffer[tail];
  482. } else {
  483. n = tx_buffer_storage_[tail-SERIAL3_TX_BUFFER_SIZE];
  484. }
  485. if (use9Bits) UART2_C3 = (UART2_C3 & ~0x40) | ((n & 0x100) >> 2);
  486. UART2_D = n;
  487. tx_buffer_tail = tail;
  488. }
  489. }
  490. if ((c & UART_C2_TCIE) && (UART2_S1 & UART_S1_TC)) {
  491. transmitting = 0;
  492. if (transmit_pin) transmit_deassert();
  493. #if defined(KINETISL)
  494. if (transmit_pin) transmit_deassert();
  495. if (half_duplex_mode) {
  496. __disable_irq();
  497. volatile uint32_t reg = UART2_C3;
  498. reg &= ~UART_C3_TXDIR;
  499. UART2_C3 = reg;
  500. __enable_irq();
  501. }
  502. #endif
  503. UART2_C2 = C2_TX_INACTIVE;
  504. }
  505. }
  506. void serial3_add_memory_for_read(void *buffer, size_t length)
  507. {
  508. rx_buffer_storage_ = (BUFTYPE*)buffer;
  509. if (buffer) {
  510. rx_buffer_total_size_ = SERIAL3_RX_BUFFER_SIZE + length;
  511. } else {
  512. rx_buffer_total_size_ = SERIAL3_RX_BUFFER_SIZE;
  513. }
  514. rts_low_watermark_ = RTS_LOW_WATERMARK + length;
  515. rts_high_watermark_ = RTS_HIGH_WATERMARK + length;
  516. }
  517. void serial3_add_memory_for_write(void *buffer, size_t length)
  518. {
  519. tx_buffer_storage_ = (BUFTYPE*)buffer;
  520. if (buffer) {
  521. tx_buffer_total_size_ = SERIAL3_TX_BUFFER_SIZE + length;
  522. } else {
  523. tx_buffer_total_size_ = SERIAL3_TX_BUFFER_SIZE;
  524. }
  525. }