Teensy 4.1 core updated for C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

718 lines
21KB

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