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.

789 lines
23KB

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