Teensy 4.1 core updated for C++20
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.

684 lines
20KB

  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 SERIAL2_TX_BUFFER_SIZE
  38. #define SERIAL2_TX_BUFFER_SIZE 40 // number of outgoing bytes to buffer
  39. #endif
  40. #ifndef SERIAL2_RX_BUFFER_SIZE
  41. #define SERIAL2_RX_BUFFER_SIZE 64 // number of incoming bytes to buffer
  42. #endif
  43. #define RTS_HIGH_WATERMARK (SERIAL2_RX_BUFFER_SIZE-24) // RTS requests sender to pause
  44. #define RTS_LOW_WATERMARK (SERIAL2_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[SERIAL2_TX_BUFFER_SIZE];
  57. static volatile BUFTYPE rx_buffer[SERIAL2_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_ = SERIAL2_TX_BUFFER_SIZE;
  61. static size_t rx_buffer_total_size_ = SERIAL2_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 SERIAL2_TX_BUFFER_SIZE > 65535
  83. static volatile uint32_t tx_buffer_head = 0;
  84. static volatile uint32_t tx_buffer_tail = 0;
  85. #elif SERIAL2_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 SERIAL2_RX_BUFFER_SIZE > 65535
  93. static volatile uint32_t rx_buffer_head = 0;
  94. static volatile uint32_t rx_buffer_tail = 0;
  95. #elif SERIAL2_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(KINETISK)
  103. static uint8_t rx_pin_num = 9;
  104. static uint8_t tx_pin_num = 10;
  105. #endif
  106. // UART0 and UART1 are clocked by F_CPU, UART2 is clocked by F_BUS
  107. // UART0 has 8 byte fifo, UART1 and UART2 have 1 byte buffer
  108. #ifdef HAS_KINETISK_UART1_FIFO
  109. #define C2_ENABLE UART_C2_TE | UART_C2_RE | UART_C2_RIE | UART_C2_ILIE
  110. #else
  111. #define C2_ENABLE UART_C2_TE | UART_C2_RE | UART_C2_RIE
  112. #endif
  113. #define C2_TX_ACTIVE C2_ENABLE | UART_C2_TIE
  114. #define C2_TX_COMPLETING C2_ENABLE | UART_C2_TCIE
  115. #define C2_TX_INACTIVE C2_ENABLE
  116. void serial2_begin(uint32_t divisor)
  117. {
  118. SIM_SCGC4 |= SIM_SCGC4_UART1; // turn on clock, TODO: use bitband
  119. rx_buffer_head = 0;
  120. rx_buffer_tail = 0;
  121. tx_buffer_head = 0;
  122. tx_buffer_tail = 0;
  123. transmitting = 0;
  124. #if defined(KINETISK)
  125. switch (rx_pin_num) {
  126. case 9: CORE_PIN9_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  127. #if defined(__MK20DX128__) || defined(__MK20DX256__) // T3.0, T3.1, T3.2
  128. case 26: CORE_PIN26_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  129. #elif defined(__MK64FX512__) || defined(__MK66FX1M0__) // T3.5 or T3.6
  130. case 59: CORE_PIN59_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  131. #endif
  132. }
  133. switch (tx_pin_num) {
  134. case 10: CORE_PIN10_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3); break;
  135. #if defined(__MK20DX128__) || defined(__MK20DX256__) // T3.0, T3.1, T3.2
  136. case 31: CORE_PIN31_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3); break;
  137. #elif defined(__MK64FX512__) || defined(__MK66FX1M0__) // T3.5 or T3.6
  138. case 58: CORE_PIN58_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3); break;
  139. #endif
  140. }
  141. #elif defined(KINETISL)
  142. CORE_PIN9_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3);
  143. CORE_PIN10_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3);
  144. #endif
  145. #if defined(HAS_KINETISK_UART1)
  146. if (divisor < 32) divisor = 32;
  147. UART1_BDH = (divisor >> 13) & 0x1F;
  148. UART1_BDL = (divisor >> 5) & 0xFF;
  149. UART1_C4 = divisor & 0x1F;
  150. #ifdef HAS_KINETISK_UART1_FIFO
  151. UART1_C1 = UART_C1_ILT;
  152. UART1_TWFIFO = 2; // tx watermark, causes S1_TDRE to set
  153. UART1_RWFIFO = 4; // rx watermark, causes S1_RDRF to set
  154. UART1_PFIFO = UART_PFIFO_TXFE | UART_PFIFO_RXFE;
  155. #else
  156. UART1_C1 = 0;
  157. UART1_PFIFO = 0;
  158. #endif
  159. #elif defined(HAS_KINETISL_UART1)
  160. if (divisor < 1) divisor = 1;
  161. UART1_BDH = (divisor >> 8) & 0x1F;
  162. UART1_BDL = divisor & 0xFF;
  163. UART1_C1 = 0;
  164. #endif
  165. UART1_C2 = C2_TX_INACTIVE;
  166. NVIC_SET_PRIORITY(IRQ_UART1_STATUS, IRQ_PRIORITY);
  167. NVIC_ENABLE_IRQ(IRQ_UART1_STATUS);
  168. }
  169. void serial2_format(uint32_t format)
  170. {
  171. uint8_t c;
  172. c = UART1_C1;
  173. c = (c & ~0x13) | (format & 0x03); // configure parity
  174. if (format & 0x04) c |= 0x10; // 9 bits (might include parity)
  175. UART1_C1 = c;
  176. if ((format & 0x0F) == 0x04) UART1_C3 |= 0x40; // 8N2 is 9 bit with 9th bit always 1
  177. c = UART1_S2 & ~0x10;
  178. if (format & 0x10) c |= 0x10; // rx invert
  179. UART1_S2 = c;
  180. c = UART1_C3 & ~0x10;
  181. if (format & 0x20) c |= 0x10; // tx invert
  182. UART1_C3 = c;
  183. #if defined(SERIAL_9BIT_SUPPORT) && !defined(KINETISL)
  184. c = UART1_C4 & 0x1F;
  185. if (format & 0x08) c |= 0x20; // 9 bit mode with parity (requires 10 bits)
  186. UART1_C4 = c;
  187. use9Bits = format & 0x80;
  188. #endif
  189. #if defined(__MK64FX512__) || defined(__MK66FX1M0__) || defined(KINETISL)
  190. // For T3.5/T3.6/TLC See about turning on 2 stop bit mode
  191. if ( format & 0x100) {
  192. uint8_t bdl = UART1_BDL;
  193. UART1_BDH |= UART_BDH_SBNS; // Turn on 2 stop bits - was turned off by set baud
  194. UART1_BDL = bdl; // Says BDH not acted on until BDL is written
  195. }
  196. #endif
  197. }
  198. void serial2_end(void)
  199. {
  200. if (!(SIM_SCGC4 & SIM_SCGC4_UART1)) return;
  201. while (transmitting) yield(); // wait for buffered data to send
  202. NVIC_DISABLE_IRQ(IRQ_UART1_STATUS);
  203. UART1_C2 = 0;
  204. #if defined(KINETISK)
  205. switch (rx_pin_num) {
  206. case 9: CORE_PIN9_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); break; // PTC3
  207. #if defined(__MK20DX128__) || defined(__MK20DX256__) // T3.0, T3.1, T3.2
  208. case 26: CORE_PIN26_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); break; // PTE1
  209. #elif defined(__MK64FX512__) || defined(__MK66FX1M0__) // T3.5, T3.6
  210. case 59: CORE_PIN59_CONFIG = 0; break;
  211. #endif
  212. }
  213. switch (tx_pin_num & 127) {
  214. case 10: CORE_PIN10_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); break; // PTC4
  215. #if defined(__MK20DX128__) || defined(__MK20DX256__) // T3.0, T3.1, T3.2
  216. case 31: CORE_PIN31_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); break; // PTE0
  217. #elif defined(__MK64FX512__) || defined(__MK66FX1M0__) // T3.5, T3.6
  218. case 58: CORE_PIN58_CONFIG = 0; break;
  219. #endif
  220. }
  221. #elif defined(KINETISL)
  222. CORE_PIN9_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); // PTC3
  223. CORE_PIN10_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); // PTC4
  224. #endif
  225. UART1_S1;
  226. UART1_D; // clear leftover error status
  227. rx_buffer_head = 0;
  228. rx_buffer_tail = 0;
  229. if (rts_pin) rts_deassert();
  230. }
  231. void serial2_set_transmit_pin(uint8_t pin)
  232. {
  233. while (transmitting) ;
  234. pinMode(pin, OUTPUT);
  235. digitalWrite(pin, LOW);
  236. transmit_pin = portOutputRegister(pin);
  237. #if defined(KINETISL)
  238. transmit_mask = digitalPinToBitMask(pin);
  239. #endif
  240. }
  241. void serial2_set_tx(uint8_t pin, uint8_t opendrain)
  242. {
  243. #if defined(KINETISK)
  244. uint32_t cfg;
  245. if (opendrain) pin |= 128;
  246. if (pin == tx_pin_num) return;
  247. if ((SIM_SCGC4 & SIM_SCGC4_UART1)) {
  248. switch (tx_pin_num & 127) {
  249. case 10: CORE_PIN10_CONFIG = 0; break; // PTC4
  250. #if defined(__MK20DX128__) || defined(__MK20DX256__) // T3.0, T3.1, T3.2
  251. case 31: CORE_PIN31_CONFIG = 0; break; // PTE0
  252. #elif defined(__MK64FX512__) || defined(__MK66FX1M0__) // T3.5, T3.6
  253. case 58: CORE_PIN58_CONFIG = 0; break;
  254. #endif
  255. }
  256. if (opendrain) {
  257. cfg = PORT_PCR_DSE | PORT_PCR_ODE;
  258. } else {
  259. cfg = PORT_PCR_DSE | PORT_PCR_SRE;
  260. }
  261. switch (pin & 127) {
  262. case 10: CORE_PIN10_CONFIG = cfg | PORT_PCR_MUX(3); break;
  263. #if defined(__MK20DX128__) || defined(__MK20DX256__) // T3.0, T3.1, T3.2
  264. case 31: CORE_PIN31_CONFIG = cfg | PORT_PCR_MUX(3); break;
  265. #elif defined(__MK64FX512__) || defined(__MK66FX1M0__) // T3.5, T3.6
  266. case 58: CORE_PIN58_CONFIG = cfg | PORT_PCR_MUX(3); break;
  267. #endif
  268. }
  269. }
  270. tx_pin_num = pin;
  271. #endif
  272. }
  273. void serial2_set_rx(uint8_t pin)
  274. {
  275. #if defined(KINETISK)
  276. if (pin == rx_pin_num) return;
  277. if ((SIM_SCGC4 & SIM_SCGC4_UART1)) {
  278. switch (rx_pin_num) {
  279. case 9: CORE_PIN9_CONFIG = 0; break; // PTC3
  280. #if defined(__MK20DX128__) || defined(__MK20DX256__) // T3.0, T3.1, T3.2
  281. case 26: CORE_PIN26_CONFIG = 0; break; // PTE1
  282. #elif defined(__MK64FX512__) || defined(__MK66FX1M0__) // T3.5, T3.6
  283. case 59: CORE_PIN59_CONFIG = 0; break;
  284. #endif
  285. }
  286. switch (pin) {
  287. case 9: CORE_PIN9_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  288. #if defined(__MK20DX128__) || defined(__MK20DX256__) // T3.0, T3.1, T3.2
  289. case 26: CORE_PIN26_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  290. #elif defined(__MK64FX512__) || defined(__MK66FX1M0__) // T3.5, T3.6
  291. case 59: CORE_PIN59_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  292. #endif
  293. }
  294. }
  295. rx_pin_num = pin;
  296. #endif
  297. }
  298. int serial2_set_rts(uint8_t pin)
  299. {
  300. if (!(SIM_SCGC4 & SIM_SCGC4_UART1)) return 0;
  301. if (pin < CORE_NUM_DIGITAL) {
  302. rts_pin = portOutputRegister(pin);
  303. #if defined(KINETISL)
  304. rts_mask = digitalPinToBitMask(pin);
  305. #endif
  306. pinMode(pin, OUTPUT);
  307. rts_assert();
  308. } else {
  309. rts_pin = NULL;
  310. return 0;
  311. }
  312. /*
  313. if (!(SIM_SCGC4 & SIM_SCGC4_UART1)) return 0;
  314. if (pin == 22) {
  315. CORE_PIN22_CONFIG = PORT_PCR_MUX(3);
  316. } else {
  317. UART1_MODEM &= ~UART_MODEM_RXRTSE;
  318. return 0;
  319. }
  320. UART1_MODEM |= UART_MODEM_RXRTSE;
  321. */
  322. return 1;
  323. }
  324. int serial2_set_cts(uint8_t pin)
  325. {
  326. #if defined(KINETISK)
  327. if (!(SIM_SCGC4 & SIM_SCGC4_UART1)) return 0;
  328. if (pin == 23) {
  329. CORE_PIN23_CONFIG = PORT_PCR_MUX(3) | PORT_PCR_PE; // weak pulldown
  330. #if defined(__MK64FX512__) || defined(__MK66FX1M0__) // on T3.5 or T3.6
  331. } else if (pin == 60) {
  332. CORE_PIN60_CONFIG = PORT_PCR_MUX(3) | PORT_PCR_PE; // weak pulldown
  333. #endif
  334. } else {
  335. UART1_MODEM &= ~UART_MODEM_TXCTSE;
  336. return 0;
  337. }
  338. UART1_MODEM |= UART_MODEM_TXCTSE;
  339. return 1;
  340. #else
  341. return 0;
  342. #endif
  343. }
  344. void serial2_putchar(uint32_t c)
  345. {
  346. uint32_t head, n;
  347. if (!(SIM_SCGC4 & SIM_SCGC4_UART1)) return;
  348. if (transmit_pin) transmit_assert();
  349. head = tx_buffer_head;
  350. if (++head >= tx_buffer_total_size_) head = 0;
  351. while (tx_buffer_tail == head) {
  352. int priority = nvic_execution_priority();
  353. if (priority <= IRQ_PRIORITY) {
  354. if ((UART1_S1 & UART_S1_TDRE)) {
  355. uint32_t tail = tx_buffer_tail;
  356. if (++tail >= tx_buffer_total_size_) tail = 0;
  357. if (tail < SERIAL2_TX_BUFFER_SIZE) {
  358. n = tx_buffer[tail];
  359. } else {
  360. n = tx_buffer_storage_[tail-SERIAL2_TX_BUFFER_SIZE];
  361. }
  362. if (use9Bits) UART1_C3 = (UART1_C3 & ~0x40) | ((n & 0x100) >> 2);
  363. UART1_D = n;
  364. tx_buffer_tail = tail;
  365. }
  366. } else if (priority >= 256) {
  367. yield(); // wait
  368. }
  369. }
  370. if (head < SERIAL2_TX_BUFFER_SIZE) {
  371. tx_buffer[head] = c;
  372. } else {
  373. tx_buffer_storage_[head - SERIAL2_TX_BUFFER_SIZE] = c;
  374. }
  375. transmitting = 1;
  376. tx_buffer_head = head;
  377. UART1_C2 = C2_TX_ACTIVE;
  378. }
  379. #ifdef HAS_KINETISK_UART1_FIFO
  380. void serial2_write(const void *buf, unsigned int count)
  381. {
  382. const uint8_t *p = (const uint8_t *)buf;
  383. const uint8_t *end = p + count;
  384. uint32_t head, n;
  385. if (!(SIM_SCGC4 & SIM_SCGC4_UART1)) return;
  386. if (transmit_pin) transmit_assert();
  387. while (p < end) {
  388. head = tx_buffer_head;
  389. if (++head >= tx_buffer_total_size_) head = 0;
  390. if (tx_buffer_tail == head) {
  391. UART1_C2 = C2_TX_ACTIVE;
  392. do {
  393. int priority = nvic_execution_priority();
  394. if (priority <= IRQ_PRIORITY) {
  395. if ((UART1_S1 & UART_S1_TDRE)) {
  396. uint32_t tail = tx_buffer_tail;
  397. if (++tail >= tx_buffer_total_size_) tail = 0;
  398. if (tail < SERIAL2_TX_BUFFER_SIZE) {
  399. n = tx_buffer[tail];
  400. } else {
  401. n = tx_buffer_storage_[tail-SERIAL2_TX_BUFFER_SIZE];
  402. }
  403. if (use9Bits) UART1_C3 = (UART1_C3 & ~0x40) | ((n & 0x100) >> 2);
  404. UART1_D = n;
  405. tx_buffer_tail = tail;
  406. }
  407. } else if (priority >= 256) {
  408. yield();
  409. }
  410. } while (tx_buffer_tail == head);
  411. }
  412. if (head < SERIAL2_TX_BUFFER_SIZE) {
  413. tx_buffer[head] = *p++;
  414. } else {
  415. tx_buffer_storage_[head - SERIAL2_TX_BUFFER_SIZE] = *p++;
  416. }
  417. transmitting = 1;
  418. tx_buffer_head = head;
  419. }
  420. UART1_C2 = C2_TX_ACTIVE;
  421. }
  422. #else
  423. void serial2_write(const void *buf, unsigned int count)
  424. {
  425. const uint8_t *p = (const uint8_t *)buf;
  426. while (count-- > 0) serial2_putchar(*p++);
  427. }
  428. #endif
  429. void serial2_flush(void)
  430. {
  431. while (transmitting) yield(); // wait
  432. }
  433. int serial2_write_buffer_free(void)
  434. {
  435. uint32_t head, tail;
  436. head = tx_buffer_head;
  437. tail = tx_buffer_tail;
  438. if (head >= tail) return tx_buffer_total_size_ - 1 - head + tail;
  439. return tail - head - 1;
  440. }
  441. int serial2_available(void)
  442. {
  443. uint32_t head, tail;
  444. head = rx_buffer_head;
  445. tail = rx_buffer_tail;
  446. if (head >= tail) return head - tail;
  447. return rx_buffer_total_size_ + head - tail;
  448. }
  449. int serial2_getchar(void)
  450. {
  451. uint32_t head, tail;
  452. int c;
  453. head = rx_buffer_head;
  454. tail = rx_buffer_tail;
  455. if (head == tail) return -1;
  456. if (++tail >= rx_buffer_total_size_) tail = 0;
  457. if (tail < SERIAL2_RX_BUFFER_SIZE) {
  458. c = rx_buffer[tail];
  459. } else {
  460. c = rx_buffer_storage_[tail-SERIAL2_RX_BUFFER_SIZE];
  461. }
  462. rx_buffer_tail = tail;
  463. if (rts_pin) {
  464. int avail;
  465. if (head >= tail) avail = head - tail;
  466. else avail = rx_buffer_total_size_ + head - tail;
  467. if (avail <= rts_low_watermark_) rts_assert();
  468. }
  469. return c;
  470. }
  471. int serial2_peek(void)
  472. {
  473. uint32_t head, tail;
  474. head = rx_buffer_head;
  475. tail = rx_buffer_tail;
  476. if (head == tail) return -1;
  477. if (++tail >= rx_buffer_total_size_) tail = 0;
  478. if (tail < SERIAL2_RX_BUFFER_SIZE) {
  479. return rx_buffer[tail];
  480. }
  481. return rx_buffer_storage_[tail-SERIAL2_RX_BUFFER_SIZE];
  482. }
  483. void serial2_clear(void)
  484. {
  485. #ifdef HAS_KINETISK_UART1_FIFO
  486. if (!(SIM_SCGC4 & SIM_SCGC4_UART1)) return;
  487. UART1_C2 &= ~(UART_C2_RE | UART_C2_RIE | UART_C2_ILIE);
  488. UART1_CFIFO = UART_CFIFO_RXFLUSH;
  489. UART1_C2 |= (UART_C2_RE | UART_C2_RIE | UART_C2_ILIE);
  490. #endif
  491. rx_buffer_head = rx_buffer_tail;
  492. if (rts_pin) rts_assert();
  493. }
  494. // status interrupt combines
  495. // Transmit data below watermark UART_S1_TDRE
  496. // Transmit complete UART_S1_TC
  497. // Idle line UART_S1_IDLE
  498. // Receive data above watermark UART_S1_RDRF
  499. // LIN break detect UART_S2_LBKDIF
  500. // RxD pin active edge UART_S2_RXEDGIF
  501. void uart1_status_isr(void)
  502. {
  503. uint32_t head, tail, n;
  504. uint8_t c;
  505. #ifdef HAS_KINETISK_UART1_FIFO
  506. uint32_t newhead;
  507. uint8_t avail;
  508. if (UART1_S1 & (UART_S1_RDRF | UART_S1_IDLE)) {
  509. __disable_irq();
  510. avail = UART1_RCFIFO;
  511. if (avail == 0) {
  512. // The only way to clear the IDLE interrupt flag is
  513. // to read the data register. But reading with no
  514. // data causes a FIFO underrun, which causes the
  515. // FIFO to return corrupted data. If anyone from
  516. // Freescale reads this, what a poor design! There
  517. // write should be a write-1-to-clear for IDLE.
  518. c = UART1_D;
  519. // flushing the fifo recovers from the underrun,
  520. // but there's a possible race condition where a
  521. // new character could be received between reading
  522. // RCFIFO == 0 and flushing the FIFO. To minimize
  523. // the chance, interrupts are disabled so a higher
  524. // priority interrupt (hopefully) doesn't delay.
  525. // TODO: change this to disabling the IDLE interrupt
  526. // which won't be simple, since we already manage
  527. // which transmit interrupts are enabled.
  528. UART1_CFIFO = UART_CFIFO_RXFLUSH;
  529. __enable_irq();
  530. } else {
  531. __enable_irq();
  532. head = rx_buffer_head;
  533. tail = rx_buffer_tail;
  534. do {
  535. if (use9Bits && (UART1_C3 & 0x80)) {
  536. n = UART1_D | 0x100;
  537. } else {
  538. n = UART1_D;
  539. }
  540. newhead = head + 1;
  541. if (newhead >= rx_buffer_total_size_) newhead = 0;
  542. if (newhead != tail) {
  543. head = newhead;
  544. if (newhead < SERIAL2_RX_BUFFER_SIZE) {
  545. rx_buffer[head] = n;
  546. } else {
  547. rx_buffer_storage_[head-SERIAL2_RX_BUFFER_SIZE] = n;
  548. }
  549. }
  550. } while (--avail > 0);
  551. rx_buffer_head = head;
  552. if (rts_pin) {
  553. int avail;
  554. if (head >= tail) avail = head - tail;
  555. else avail = rx_buffer_total_size_ + head - tail;
  556. if (avail >= rts_high_watermark_) rts_deassert();
  557. }
  558. }
  559. }
  560. c = UART1_C2;
  561. if ((c & UART_C2_TIE) && (UART1_S1 & UART_S1_TDRE)) {
  562. head = tx_buffer_head;
  563. tail = tx_buffer_tail;
  564. do {
  565. if (tail == head) break;
  566. if (++tail >= tx_buffer_total_size_) tail = 0;
  567. avail = UART1_S1;
  568. if (tail < SERIAL2_TX_BUFFER_SIZE) {
  569. n = tx_buffer[tail];
  570. } else {
  571. n = tx_buffer_storage_[tail-SERIAL2_TX_BUFFER_SIZE];
  572. }
  573. if (use9Bits) UART1_C3 = (UART1_C3 & ~0x40) | ((n & 0x100) >> 2);
  574. UART1_D = n;
  575. } while (UART1_TCFIFO < 8);
  576. tx_buffer_tail = tail;
  577. if (UART1_S1 & UART_S1_TDRE) UART1_C2 = C2_TX_COMPLETING;
  578. }
  579. #else
  580. if (UART1_S1 & UART_S1_RDRF) {
  581. if (use9Bits && (UART1_C3 & 0x80)) {
  582. n = UART1_D | 0x100;
  583. } else {
  584. n = UART1_D;
  585. }
  586. head = rx_buffer_head + 1;
  587. if (head >= rx_buffer_total_size_) head = 0;
  588. if (head != rx_buffer_tail) {
  589. if (head < SERIAL2_RX_BUFFER_SIZE) {
  590. rx_buffer[head] = n;
  591. } else {
  592. rx_buffer_storage_[head-SERIAL2_RX_BUFFER_SIZE] = n;
  593. }
  594. rx_buffer_head = head;
  595. }
  596. }
  597. c = UART1_C2;
  598. if ((c & UART_C2_TIE) && (UART1_S1 & UART_S1_TDRE)) {
  599. head = tx_buffer_head;
  600. tail = tx_buffer_tail;
  601. if (head == tail) {
  602. UART1_C2 = C2_TX_COMPLETING;
  603. } else {
  604. if (++tail >= tx_buffer_total_size_) tail = 0;
  605. if (tail < SERIAL2_TX_BUFFER_SIZE) {
  606. n = tx_buffer[tail];
  607. } else {
  608. n = tx_buffer_storage_[tail-SERIAL2_TX_BUFFER_SIZE];
  609. }
  610. if (use9Bits) UART1_C3 = (UART1_C3 & ~0x40) | ((n & 0x100) >> 2);
  611. UART1_D = n;
  612. tx_buffer_tail = tail;
  613. }
  614. }
  615. #endif
  616. if ((c & UART_C2_TCIE) && (UART1_S1 & UART_S1_TC)) {
  617. transmitting = 0;
  618. if (transmit_pin) transmit_deassert();
  619. UART1_C2 = C2_TX_INACTIVE;
  620. }
  621. }
  622. void serial2_add_memory_for_read(void *buffer, size_t length)
  623. {
  624. rx_buffer_storage_ = (BUFTYPE*)buffer;
  625. if (buffer) {
  626. rx_buffer_total_size_ = SERIAL2_RX_BUFFER_SIZE + length;
  627. } else {
  628. rx_buffer_total_size_ = SERIAL2_RX_BUFFER_SIZE;
  629. }
  630. rts_low_watermark_ = RTS_LOW_WATERMARK + length;
  631. rts_high_watermark_ = RTS_HIGH_WATERMARK + length;
  632. }
  633. void serial2_add_memory_for_write(void *buffer, size_t length)
  634. {
  635. tx_buffer_storage_ = (BUFTYPE*)buffer;
  636. if (buffer) {
  637. tx_buffer_total_size_ = SERIAL2_TX_BUFFER_SIZE + length;
  638. } else {
  639. tx_buffer_total_size_ = SERIAL2_TX_BUFFER_SIZE;
  640. }
  641. }