Teensy 4.1 core updated for C++20
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

663 rindas
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. ////////////////////////////////////////////////////////////////
  34. // Tunable parameters (relatively safe to edit these numbers)
  35. ////////////////////////////////////////////////////////////////
  36. #ifndef SERIAL2_TX_BUFFER_SIZE
  37. #define SERIAL2_TX_BUFFER_SIZE 40 // number of outgoing bytes to buffer
  38. #endif
  39. #ifndef SERIAL2_RX_BUFFER_SIZE
  40. #define SERIAL2_RX_BUFFER_SIZE 64 // number of incoming bytes to buffer
  41. #endif
  42. #define RTS_HIGH_WATERMARK (SERIAL2_RX_BUFFER_SIZE-24) // RTS requests sender to pause
  43. #define RTS_LOW_WATERMARK (SERIAL2_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[SERIAL2_TX_BUFFER_SIZE];
  56. static volatile BUFTYPE rx_buffer[SERIAL2_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 SERIAL2_TX_BUFFER_SIZE > 65535
  76. static volatile uint32_t tx_buffer_head = 0;
  77. static volatile uint32_t tx_buffer_tail = 0;
  78. #elif SERIAL2_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 SERIAL2_RX_BUFFER_SIZE > 65535
  86. static volatile uint32_t rx_buffer_head = 0;
  87. static volatile uint32_t rx_buffer_tail = 0;
  88. #elif SERIAL2_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. #if defined(KINETISK)
  96. static uint8_t rx_pin_num = 9;
  97. static uint8_t tx_pin_num = 10;
  98. #endif
  99. #if defined(KINETISL)
  100. static uint8_t half_duplex_mode = 0;
  101. #endif
  102. // UART0 and UART1 are clocked by F_CPU, UART2 is clocked by F_BUS
  103. // UART0 has 8 byte fifo, UART1 and UART2 have 1 byte buffer
  104. #ifdef HAS_KINETISK_UART1_FIFO
  105. #define C2_ENABLE UART_C2_TE | UART_C2_RE | UART_C2_RIE | UART_C2_ILIE
  106. #else
  107. #define C2_ENABLE UART_C2_TE | UART_C2_RE | UART_C2_RIE
  108. #endif
  109. #define C2_TX_ACTIVE C2_ENABLE | UART_C2_TIE
  110. #define C2_TX_COMPLETING C2_ENABLE | UART_C2_TCIE
  111. #define C2_TX_INACTIVE C2_ENABLE
  112. // BITBAND Support
  113. #define GPIO_BITBAND_ADDR(reg, bit) (((uint32_t)&(reg) - 0x40000000) * 32 + (bit) * 4 + 0x42000000)
  114. #define GPIO_BITBAND_PTR(reg, bit) ((uint32_t *)GPIO_BITBAND_ADDR((reg), (bit)))
  115. #define C3_TXDIR_BIT 5
  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. // process request for half duplex.
  198. if ((format & SERIAL_HALF_DUPLEX) != 0) {
  199. c = UART1_C1;
  200. c |= UART_C1_LOOPS | UART_C1_RSRC;
  201. UART1_C1 = c;
  202. // Lets try to make use of bitband address to set the direction for ue...
  203. #if defined(KINETISL)
  204. //CORE_PIN10_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(1) | PORT_PCR_PE | PORT_PCR_PS;
  205. CORE_PIN10_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3);
  206. half_duplex_mode = 1;
  207. #else
  208. volatile uint32_t *reg = portConfigRegister(tx_pin_num);
  209. *reg = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3) | PORT_PCR_PE | PORT_PCR_PS; // pullup on output pin;
  210. transmit_pin = (uint8_t*)GPIO_BITBAND_PTR(UART1_C3, C3_TXDIR_BIT);
  211. #endif
  212. } else {
  213. #if defined(KINETISL)
  214. half_duplex_mode = 0;
  215. #else
  216. if (transmit_pin == (uint8_t*)GPIO_BITBAND_PTR(UART1_C3, C3_TXDIR_BIT)) transmit_pin = NULL;
  217. #endif
  218. }
  219. }
  220. void serial2_end(void)
  221. {
  222. if (!(SIM_SCGC4 & SIM_SCGC4_UART1)) return;
  223. while (transmitting) yield(); // wait for buffered data to send
  224. NVIC_DISABLE_IRQ(IRQ_UART1_STATUS);
  225. UART1_C2 = 0;
  226. #if defined(KINETISK)
  227. switch (rx_pin_num) {
  228. case 9: CORE_PIN9_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); break; // PTC3
  229. #if defined(__MK20DX128__) || defined(__MK20DX256__) // T3.0, T3.1, T3.2
  230. case 26: CORE_PIN26_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); break; // PTE1
  231. #elif defined(__MK64FX512__) || defined(__MK66FX1M0__) // T3.5, T3.6
  232. case 59: CORE_PIN59_CONFIG = 0; break;
  233. #endif
  234. }
  235. switch (tx_pin_num & 127) {
  236. case 10: CORE_PIN10_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); break; // PTC4
  237. #if defined(__MK20DX128__) || defined(__MK20DX256__) // T3.0, T3.1, T3.2
  238. case 31: CORE_PIN31_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); break; // PTE0
  239. #elif defined(__MK64FX512__) || defined(__MK66FX1M0__) // T3.5, T3.6
  240. case 58: CORE_PIN58_CONFIG = 0; break;
  241. #endif
  242. }
  243. #elif defined(KINETISL)
  244. CORE_PIN9_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); // PTC3
  245. CORE_PIN10_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); // PTC4
  246. #endif
  247. UART1_S1;
  248. UART1_D; // clear leftover error status
  249. rx_buffer_head = 0;
  250. rx_buffer_tail = 0;
  251. if (rts_pin) rts_deassert();
  252. }
  253. void serial2_set_transmit_pin(uint8_t pin)
  254. {
  255. while (transmitting) ;
  256. pinMode(pin, OUTPUT);
  257. digitalWrite(pin, LOW);
  258. transmit_pin = portOutputRegister(pin);
  259. #if defined(KINETISL)
  260. transmit_mask = digitalPinToBitMask(pin);
  261. #endif
  262. }
  263. void serial2_set_tx(uint8_t pin, uint8_t opendrain)
  264. {
  265. #if defined(KINETISK)
  266. uint32_t cfg;
  267. if (opendrain) pin |= 128;
  268. if (pin == tx_pin_num) return;
  269. if ((SIM_SCGC4 & SIM_SCGC4_UART1)) {
  270. switch (tx_pin_num & 127) {
  271. case 10: CORE_PIN10_CONFIG = 0; break; // PTC4
  272. #if defined(__MK20DX128__) || defined(__MK20DX256__) // T3.0, T3.1, T3.2
  273. case 31: CORE_PIN31_CONFIG = 0; break; // PTE0
  274. #elif defined(__MK64FX512__) || defined(__MK66FX1M0__) // T3.5, T3.6
  275. case 58: CORE_PIN58_CONFIG = 0; break;
  276. #endif
  277. }
  278. if (opendrain) {
  279. cfg = PORT_PCR_DSE | PORT_PCR_ODE;
  280. } else {
  281. cfg = PORT_PCR_DSE | PORT_PCR_SRE;
  282. }
  283. switch (pin & 127) {
  284. case 10: CORE_PIN10_CONFIG = cfg | PORT_PCR_MUX(3); break;
  285. #if defined(__MK20DX128__) || defined(__MK20DX256__) // T3.0, T3.1, T3.2
  286. case 31: CORE_PIN31_CONFIG = cfg | PORT_PCR_MUX(3); break;
  287. #elif defined(__MK64FX512__) || defined(__MK66FX1M0__) // T3.5, T3.6
  288. case 58: CORE_PIN58_CONFIG = cfg | PORT_PCR_MUX(3); break;
  289. #endif
  290. }
  291. }
  292. tx_pin_num = pin;
  293. #endif
  294. }
  295. void serial2_set_rx(uint8_t pin)
  296. {
  297. #if defined(KINETISK)
  298. if (pin == rx_pin_num) return;
  299. if ((SIM_SCGC4 & SIM_SCGC4_UART1)) {
  300. switch (rx_pin_num) {
  301. case 9: CORE_PIN9_CONFIG = 0; break; // PTC3
  302. #if defined(__MK20DX128__) || defined(__MK20DX256__) // T3.0, T3.1, T3.2
  303. case 26: CORE_PIN26_CONFIG = 0; break; // PTE1
  304. #elif defined(__MK64FX512__) || defined(__MK66FX1M0__) // T3.5, T3.6
  305. case 59: CORE_PIN59_CONFIG = 0; break;
  306. #endif
  307. }
  308. switch (pin) {
  309. case 9: CORE_PIN9_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  310. #if defined(__MK20DX128__) || defined(__MK20DX256__) // T3.0, T3.1, T3.2
  311. case 26: CORE_PIN26_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  312. #elif defined(__MK64FX512__) || defined(__MK66FX1M0__) // T3.5, T3.6
  313. case 59: CORE_PIN59_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  314. #endif
  315. }
  316. }
  317. rx_pin_num = pin;
  318. #endif
  319. }
  320. int serial2_set_rts(uint8_t pin)
  321. {
  322. if (!(SIM_SCGC4 & SIM_SCGC4_UART1)) return 0;
  323. if (pin < CORE_NUM_DIGITAL) {
  324. rts_pin = portOutputRegister(pin);
  325. #if defined(KINETISL)
  326. rts_mask = digitalPinToBitMask(pin);
  327. #endif
  328. pinMode(pin, OUTPUT);
  329. rts_assert();
  330. } else {
  331. rts_pin = NULL;
  332. return 0;
  333. }
  334. /*
  335. if (!(SIM_SCGC4 & SIM_SCGC4_UART1)) return 0;
  336. if (pin == 22) {
  337. CORE_PIN22_CONFIG = PORT_PCR_MUX(3);
  338. } else {
  339. UART1_MODEM &= ~UART_MODEM_RXRTSE;
  340. return 0;
  341. }
  342. UART1_MODEM |= UART_MODEM_RXRTSE;
  343. */
  344. return 1;
  345. }
  346. int serial2_set_cts(uint8_t pin)
  347. {
  348. #if defined(KINETISK)
  349. if (!(SIM_SCGC4 & SIM_SCGC4_UART1)) return 0;
  350. if (pin == 23) {
  351. CORE_PIN23_CONFIG = PORT_PCR_MUX(3) | PORT_PCR_PE; // weak pulldown
  352. #if defined(__MK64FX512__) || defined(__MK66FX1M0__) // on T3.5 or T3.6
  353. } else if (pin == 60) {
  354. CORE_PIN60_CONFIG = PORT_PCR_MUX(3) | PORT_PCR_PE; // weak pulldown
  355. #endif
  356. } else {
  357. UART1_MODEM &= ~UART_MODEM_TXCTSE;
  358. return 0;
  359. }
  360. UART1_MODEM |= UART_MODEM_TXCTSE;
  361. return 1;
  362. #else
  363. return 0;
  364. #endif
  365. }
  366. void serial2_putchar(uint32_t c)
  367. {
  368. uint32_t head, n;
  369. if (!(SIM_SCGC4 & SIM_SCGC4_UART1)) return;
  370. if (transmit_pin) transmit_assert();
  371. #if defined(KINETISL)
  372. if (half_duplex_mode) {
  373. __disable_irq();
  374. volatile uint32_t reg = UART1_C3;
  375. reg |= UART_C3_TXDIR;
  376. UART1_C3 = reg;
  377. __enable_irq();
  378. }
  379. #endif
  380. head = tx_buffer_head;
  381. if (++head >= SERIAL2_TX_BUFFER_SIZE) head = 0;
  382. while (tx_buffer_tail == head) {
  383. int priority = nvic_execution_priority();
  384. if (priority <= IRQ_PRIORITY) {
  385. if ((UART1_S1 & UART_S1_TDRE)) {
  386. uint32_t tail = tx_buffer_tail;
  387. if (++tail >= SERIAL2_TX_BUFFER_SIZE) tail = 0;
  388. n = tx_buffer[tail];
  389. if (use9Bits) UART1_C3 = (UART1_C3 & ~0x40) | ((n & 0x100) >> 2);
  390. UART1_D = n;
  391. tx_buffer_tail = tail;
  392. }
  393. } else if (priority >= 256) {
  394. yield(); // wait
  395. }
  396. }
  397. tx_buffer[head] = c;
  398. transmitting = 1;
  399. tx_buffer_head = head;
  400. UART1_C2 = C2_TX_ACTIVE;
  401. }
  402. #ifdef HAS_KINETISK_UART1_FIFO
  403. void serial2_write(const void *buf, unsigned int count)
  404. {
  405. const uint8_t *p = (const uint8_t *)buf;
  406. const uint8_t *end = p + count;
  407. uint32_t head, n;
  408. if (!(SIM_SCGC4 & SIM_SCGC4_UART1)) return;
  409. if (transmit_pin) transmit_assert();
  410. while (p < end) {
  411. head = tx_buffer_head;
  412. if (++head >= SERIAL2_TX_BUFFER_SIZE) head = 0;
  413. if (tx_buffer_tail == head) {
  414. UART1_C2 = C2_TX_ACTIVE;
  415. do {
  416. int priority = nvic_execution_priority();
  417. if (priority <= IRQ_PRIORITY) {
  418. if ((UART1_S1 & UART_S1_TDRE)) {
  419. uint32_t tail = tx_buffer_tail;
  420. if (++tail >= SERIAL2_TX_BUFFER_SIZE) tail = 0;
  421. n = tx_buffer[tail];
  422. if (use9Bits) UART1_C3 = (UART1_C3 & ~0x40) | ((n & 0x100) >> 2);
  423. UART1_D = n;
  424. tx_buffer_tail = tail;
  425. }
  426. } else if (priority >= 256) {
  427. yield();
  428. }
  429. } while (tx_buffer_tail == head);
  430. }
  431. tx_buffer[head] = *p++;
  432. transmitting = 1;
  433. tx_buffer_head = head;
  434. }
  435. UART1_C2 = C2_TX_ACTIVE;
  436. }
  437. #else
  438. void serial2_write(const void *buf, unsigned int count)
  439. {
  440. const uint8_t *p = (const uint8_t *)buf;
  441. while (count-- > 0) serial2_putchar(*p++);
  442. }
  443. #endif
  444. void serial2_flush(void)
  445. {
  446. while (transmitting) yield(); // wait
  447. }
  448. int serial2_write_buffer_free(void)
  449. {
  450. uint32_t head, tail;
  451. head = tx_buffer_head;
  452. tail = tx_buffer_tail;
  453. if (head >= tail) return SERIAL2_TX_BUFFER_SIZE - 1 - head + tail;
  454. return tail - head - 1;
  455. }
  456. int serial2_available(void)
  457. {
  458. uint32_t head, tail;
  459. head = rx_buffer_head;
  460. tail = rx_buffer_tail;
  461. if (head >= tail) return head - tail;
  462. return SERIAL2_RX_BUFFER_SIZE + head - tail;
  463. }
  464. int serial2_getchar(void)
  465. {
  466. uint32_t head, tail;
  467. int c;
  468. head = rx_buffer_head;
  469. tail = rx_buffer_tail;
  470. if (head == tail) return -1;
  471. if (++tail >= SERIAL2_RX_BUFFER_SIZE) tail = 0;
  472. c = rx_buffer[tail];
  473. rx_buffer_tail = tail;
  474. if (rts_pin) {
  475. int avail;
  476. if (head >= tail) avail = head - tail;
  477. else avail = SERIAL2_RX_BUFFER_SIZE + head - tail;
  478. if (avail <= RTS_LOW_WATERMARK) rts_assert();
  479. }
  480. return c;
  481. }
  482. int serial2_peek(void)
  483. {
  484. uint32_t head, tail;
  485. head = rx_buffer_head;
  486. tail = rx_buffer_tail;
  487. if (head == tail) return -1;
  488. if (++tail >= SERIAL2_RX_BUFFER_SIZE) tail = 0;
  489. return rx_buffer[tail];
  490. }
  491. void serial2_clear(void)
  492. {
  493. #ifdef HAS_KINETISK_UART1_FIFO
  494. if (!(SIM_SCGC4 & SIM_SCGC4_UART1)) return;
  495. UART1_C2 &= ~(UART_C2_RE | UART_C2_RIE | UART_C2_ILIE);
  496. UART1_CFIFO = UART_CFIFO_RXFLUSH;
  497. UART1_C2 |= (UART_C2_RE | UART_C2_RIE | UART_C2_ILIE);
  498. #endif
  499. rx_buffer_head = rx_buffer_tail;
  500. if (rts_pin) rts_assert();
  501. }
  502. // status interrupt combines
  503. // Transmit data below watermark UART_S1_TDRE
  504. // Transmit complete UART_S1_TC
  505. // Idle line UART_S1_IDLE
  506. // Receive data above watermark UART_S1_RDRF
  507. // LIN break detect UART_S2_LBKDIF
  508. // RxD pin active edge UART_S2_RXEDGIF
  509. void uart1_status_isr(void)
  510. {
  511. uint32_t head, tail, n;
  512. uint8_t c;
  513. #ifdef HAS_KINETISK_UART1_FIFO
  514. uint32_t newhead;
  515. uint8_t avail;
  516. if (UART1_S1 & (UART_S1_RDRF | UART_S1_IDLE)) {
  517. __disable_irq();
  518. avail = UART1_RCFIFO;
  519. if (avail == 0) {
  520. // The only way to clear the IDLE interrupt flag is
  521. // to read the data register. But reading with no
  522. // data causes a FIFO underrun, which causes the
  523. // FIFO to return corrupted data. If anyone from
  524. // Freescale reads this, what a poor design! There
  525. // write should be a write-1-to-clear for IDLE.
  526. c = UART1_D;
  527. // flushing the fifo recovers from the underrun,
  528. // but there's a possible race condition where a
  529. // new character could be received between reading
  530. // RCFIFO == 0 and flushing the FIFO. To minimize
  531. // the chance, interrupts are disabled so a higher
  532. // priority interrupt (hopefully) doesn't delay.
  533. // TODO: change this to disabling the IDLE interrupt
  534. // which won't be simple, since we already manage
  535. // which transmit interrupts are enabled.
  536. UART1_CFIFO = UART_CFIFO_RXFLUSH;
  537. __enable_irq();
  538. } else {
  539. __enable_irq();
  540. head = rx_buffer_head;
  541. tail = rx_buffer_tail;
  542. do {
  543. if (use9Bits && (UART1_C3 & 0x80)) {
  544. n = UART1_D | 0x100;
  545. } else {
  546. n = UART1_D;
  547. }
  548. newhead = head + 1;
  549. if (newhead >= SERIAL2_RX_BUFFER_SIZE) newhead = 0;
  550. if (newhead != tail) {
  551. head = newhead;
  552. rx_buffer[head] = n;
  553. }
  554. } while (--avail > 0);
  555. rx_buffer_head = head;
  556. if (rts_pin) {
  557. int avail;
  558. if (head >= tail) avail = head - tail;
  559. else avail = SERIAL2_RX_BUFFER_SIZE + head - tail;
  560. if (avail >= RTS_HIGH_WATERMARK) rts_deassert();
  561. }
  562. }
  563. }
  564. c = UART1_C2;
  565. if ((c & UART_C2_TIE) && (UART1_S1 & UART_S1_TDRE)) {
  566. head = tx_buffer_head;
  567. tail = tx_buffer_tail;
  568. do {
  569. if (tail == head) break;
  570. if (++tail >= SERIAL2_TX_BUFFER_SIZE) tail = 0;
  571. avail = UART1_S1;
  572. n = tx_buffer[tail];
  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 >= SERIAL2_RX_BUFFER_SIZE) head = 0;
  588. if (head != rx_buffer_tail) {
  589. rx_buffer[head] = n;
  590. rx_buffer_head = head;
  591. }
  592. }
  593. c = UART1_C2;
  594. if ((c & UART_C2_TIE) && (UART1_S1 & UART_S1_TDRE)) {
  595. head = tx_buffer_head;
  596. tail = tx_buffer_tail;
  597. if (head == tail) {
  598. UART1_C2 = C2_TX_COMPLETING;
  599. } else {
  600. if (++tail >= SERIAL2_TX_BUFFER_SIZE) tail = 0;
  601. n = tx_buffer[tail];
  602. if (use9Bits) UART1_C3 = (UART1_C3 & ~0x40) | ((n & 0x100) >> 2);
  603. UART1_D = n;
  604. tx_buffer_tail = tail;
  605. }
  606. }
  607. #endif
  608. if ((c & UART_C2_TCIE) && (UART1_S1 & UART_S1_TC)) {
  609. transmitting = 0;
  610. if (transmit_pin) transmit_deassert();
  611. #if defined(KINETISL)
  612. if (half_duplex_mode) {
  613. __disable_irq();
  614. volatile uint32_t reg = UART1_C3;
  615. reg &= ~UART_C3_TXDIR;
  616. UART1_C3 = reg;
  617. __enable_irq();
  618. }
  619. #endif
  620. UART1_C2 = C2_TX_INACTIVE;
  621. }
  622. }