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.

serial2.c 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. /* Teensyduino Core Library
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2013 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. #define TX_BUFFER_SIZE 40
  37. #define RX_BUFFER_SIZE 64
  38. #define IRQ_PRIORITY 64 // 0 = highest priority, 255 = lowest
  39. ////////////////////////////////////////////////////////////////
  40. // changes not recommended below this point....
  41. ////////////////////////////////////////////////////////////////
  42. #ifdef SERIAL_9BIT_SUPPORT
  43. static uint8_t use9Bits = 0;
  44. #define BUFTYPE uint16_t
  45. #else
  46. #define BUFTYPE uint8_t
  47. #define use9Bits 0
  48. #endif
  49. static volatile BUFTYPE tx_buffer[TX_BUFFER_SIZE];
  50. static volatile BUFTYPE rx_buffer[RX_BUFFER_SIZE];
  51. static volatile uint8_t transmitting = 0;
  52. #if defined(KINETISK)
  53. static volatile uint8_t *transmit_pin=NULL;
  54. #define transmit_assert() *transmit_pin = 1
  55. #define transmit_deassert() *transmit_pin = 0
  56. #elif defined(KINETISL)
  57. static volatile uint8_t *transmit_pin=NULL;
  58. static uint8_t transmit_mask=0;
  59. #define transmit_assert() *(transmit_pin+4) = transmit_mask;
  60. #define transmit_deassert() *(transmit_pin+8) = transmit_mask;
  61. #endif
  62. #if TX_BUFFER_SIZE > 255
  63. static volatile uint16_t tx_buffer_head = 0;
  64. static volatile uint16_t tx_buffer_tail = 0;
  65. #else
  66. static volatile uint8_t tx_buffer_head = 0;
  67. static volatile uint8_t tx_buffer_tail = 0;
  68. #endif
  69. #if RX_BUFFER_SIZE > 255
  70. static volatile uint16_t rx_buffer_head = 0;
  71. static volatile uint16_t rx_buffer_tail = 0;
  72. #else
  73. static volatile uint8_t rx_buffer_head = 0;
  74. static volatile uint8_t rx_buffer_tail = 0;
  75. #endif
  76. // UART0 and UART1 are clocked by F_CPU, UART2 is clocked by F_BUS
  77. // UART0 has 8 byte fifo, UART1 and UART2 have 1 byte buffer
  78. #ifdef HAS_KINETISK_UART1_FIFO
  79. #define C2_ENABLE UART_C2_TE | UART_C2_RE | UART_C2_RIE | UART_C2_ILIE
  80. #else
  81. #define C2_ENABLE UART_C2_TE | UART_C2_RE | UART_C2_RIE
  82. #endif
  83. #define C2_TX_ACTIVE C2_ENABLE | UART_C2_TIE
  84. #define C2_TX_COMPLETING C2_ENABLE | UART_C2_TCIE
  85. #define C2_TX_INACTIVE C2_ENABLE
  86. void serial2_begin(uint32_t divisor)
  87. {
  88. SIM_SCGC4 |= SIM_SCGC4_UART1; // turn on clock, TODO: use bitband
  89. rx_buffer_head = 0;
  90. rx_buffer_tail = 0;
  91. tx_buffer_head = 0;
  92. tx_buffer_tail = 0;
  93. transmitting = 0;
  94. CORE_PIN9_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3);
  95. CORE_PIN10_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3);
  96. #if defined(HAS_KINETISK_UART1)
  97. UART1_BDH = (divisor >> 13) & 0x1F;
  98. UART1_BDL = (divisor >> 5) & 0xFF;
  99. UART1_C4 = divisor & 0x1F;
  100. #ifdef HAS_KINETISK_UART1_FIFO
  101. UART1_C1 = UART_C1_ILT;
  102. UART1_TWFIFO = 2; // tx watermark, causes S1_TDRE to set
  103. UART1_RWFIFO = 4; // rx watermark, causes S1_RDRF to set
  104. UART1_PFIFO = UART_PFIFO_TXFE | UART_PFIFO_RXFE;
  105. #else
  106. UART1_C1 = 0;
  107. UART1_PFIFO = 0;
  108. #endif
  109. #elif defined(HAS_KINETISL_UART1)
  110. UART1_BDH = (divisor >> 8) & 0x1F;
  111. UART1_BDL = divisor & 0xFF;
  112. UART1_C1 = 0;
  113. #endif
  114. UART1_C2 = C2_TX_INACTIVE;
  115. NVIC_SET_PRIORITY(IRQ_UART1_STATUS, IRQ_PRIORITY);
  116. NVIC_ENABLE_IRQ(IRQ_UART1_STATUS);
  117. }
  118. void serial2_format(uint32_t format)
  119. {
  120. uint8_t c;
  121. c = UART1_C1;
  122. c = (c & ~0x13) | (format & 0x03); // configure parity
  123. if (format & 0x04) c |= 0x10; // 9 bits (might include parity)
  124. UART1_C1 = c;
  125. if ((format & 0x0F) == 0x04) UART1_C3 |= 0x40; // 8N2 is 9 bit with 9th bit always 1
  126. c = UART1_S2 & ~0x10;
  127. if (format & 0x10) c |= 0x10; // rx invert
  128. UART1_S2 = c;
  129. c = UART1_C3 & ~0x10;
  130. if (format & 0x20) c |= 0x10; // tx invert
  131. UART1_C3 = c;
  132. #ifdef SERIAL_9BIT_SUPPORT
  133. c = UART1_C4 & 0x1F;
  134. if (format & 0x08) c |= 0x20; // 9 bit mode with parity (requires 10 bits)
  135. UART1_C4 = c;
  136. use9Bits = format & 0x80;
  137. #endif
  138. // UART1_C1.0 = parity, 0=even, 1=odd
  139. // UART1_C1.1 = parity, 0=disable, 1=enable
  140. // UART1_C1.4 = mode, 1=9bit, 0=8bit
  141. // UART1_C4.5 = mode, 1=10bit, 0=8bit
  142. // UART1_C3.4 = txinv, 0=normal, 1=inverted
  143. // UART1_S2.4 = rxinv, 0=normal, 1=inverted
  144. }
  145. void serial2_end(void)
  146. {
  147. if (!(SIM_SCGC4 & SIM_SCGC4_UART1)) return;
  148. while (transmitting) yield(); // wait for buffered data to send
  149. NVIC_DISABLE_IRQ(IRQ_UART1_STATUS);
  150. UART1_C2 = 0;
  151. CORE_PIN9_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1);
  152. CORE_PIN10_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1);
  153. rx_buffer_head = 0;
  154. rx_buffer_tail = 0;
  155. }
  156. void serial2_set_transmit_pin(uint8_t pin)
  157. {
  158. while (transmitting) ;
  159. pinMode(pin, OUTPUT);
  160. digitalWrite(pin, LOW);
  161. transmit_pin = portOutputRegister(pin);
  162. #if defined(KINETISL)
  163. transmit_mask = digitalPinToBitMask(pin);
  164. #endif
  165. }
  166. int serial2_set_rts(uint8_t pin)
  167. {
  168. if (!(SIM_SCGC4 & SIM_SCGC4_UART1)) return 0;
  169. if (pin == 22) {
  170. CORE_PIN22_CONFIG = PORT_PCR_MUX(3);
  171. } else {
  172. UART1_MODEM &= ~UART_MODEM_RXRTSE;
  173. return 0;
  174. }
  175. UART1_MODEM |= UART_MODEM_RXRTSE;
  176. return 1;
  177. }
  178. int serial2_set_cts(uint8_t pin)
  179. {
  180. if (!(SIM_SCGC4 & SIM_SCGC4_UART1)) return 0;
  181. if (pin == 23) {
  182. CORE_PIN23_CONFIG = PORT_PCR_MUX(3); // TODO: weak pullup or pulldown?
  183. } else {
  184. UART1_MODEM &= ~UART_MODEM_TXCTSE;
  185. return 0;
  186. }
  187. UART1_MODEM |= UART_MODEM_TXCTSE;
  188. return 1;
  189. }
  190. void serial2_putchar(uint32_t c)
  191. {
  192. uint32_t head, n;
  193. if (!(SIM_SCGC4 & SIM_SCGC4_UART1)) return;
  194. if (transmit_pin) transmit_assert();
  195. head = tx_buffer_head;
  196. if (++head >= TX_BUFFER_SIZE) head = 0;
  197. while (tx_buffer_tail == head) {
  198. int priority = nvic_execution_priority();
  199. if (priority <= IRQ_PRIORITY) {
  200. if ((UART1_S1 & UART_S1_TDRE)) {
  201. uint32_t tail = tx_buffer_tail;
  202. if (++tail >= TX_BUFFER_SIZE) tail = 0;
  203. n = tx_buffer[tail];
  204. if (use9Bits) UART1_C3 = (UART1_C3 & ~0x40) | ((n & 0x100) >> 2);
  205. UART1_D = n;
  206. tx_buffer_tail = tail;
  207. }
  208. } else if (priority >= 256) {
  209. yield(); // wait
  210. }
  211. }
  212. tx_buffer[head] = c;
  213. transmitting = 1;
  214. tx_buffer_head = head;
  215. UART1_C2 = C2_TX_ACTIVE;
  216. }
  217. #ifdef HAS_KINETISK_UART1_FIFO
  218. void serial2_write(const void *buf, unsigned int count)
  219. {
  220. const uint8_t *p = (const uint8_t *)buf;
  221. const uint8_t *end = p + count;
  222. uint32_t head, n;
  223. if (!(SIM_SCGC4 & SIM_SCGC4_UART1)) return;
  224. if (transmit_pin) transmit_assert();
  225. while (p < end) {
  226. head = tx_buffer_head;
  227. if (++head >= TX_BUFFER_SIZE) head = 0;
  228. if (tx_buffer_tail == head) {
  229. UART1_C2 = C2_TX_ACTIVE;
  230. do {
  231. int priority = nvic_execution_priority();
  232. if (priority <= IRQ_PRIORITY) {
  233. if ((UART1_S1 & UART_S1_TDRE)) {
  234. uint32_t tail = tx_buffer_tail;
  235. if (++tail >= TX_BUFFER_SIZE) tail = 0;
  236. n = tx_buffer[tail];
  237. if (use9Bits) UART1_C3 = (UART1_C3 & ~0x40) | ((n & 0x100) >> 2);
  238. UART1_D = n;
  239. tx_buffer_tail = tail;
  240. }
  241. } else if (priority >= 256) {
  242. yield();
  243. }
  244. } while (tx_buffer_tail == head);
  245. }
  246. tx_buffer[head] = *p++;
  247. transmitting = 1;
  248. tx_buffer_head = head;
  249. }
  250. UART1_C2 = C2_TX_ACTIVE;
  251. }
  252. #else
  253. void serial2_write(const void *buf, unsigned int count)
  254. {
  255. const uint8_t *p = (const uint8_t *)buf;
  256. while (count-- > 0) serial2_putchar(*p++);
  257. }
  258. #endif
  259. void serial2_flush(void)
  260. {
  261. while (transmitting) yield(); // wait
  262. }
  263. int serial2_write_buffer_free(void)
  264. {
  265. uint32_t head, tail;
  266. head = tx_buffer_head;
  267. tail = tx_buffer_tail;
  268. if (head >= tail) return TX_BUFFER_SIZE - 1 - head + tail;
  269. return tail - head - 1;
  270. }
  271. int serial2_available(void)
  272. {
  273. uint32_t head, tail;
  274. head = rx_buffer_head;
  275. tail = rx_buffer_tail;
  276. if (head >= tail) return head - tail;
  277. return RX_BUFFER_SIZE + head - tail;
  278. }
  279. int serial2_getchar(void)
  280. {
  281. uint32_t head, tail;
  282. int c;
  283. head = rx_buffer_head;
  284. tail = rx_buffer_tail;
  285. if (head == tail) return -1;
  286. if (++tail >= RX_BUFFER_SIZE) tail = 0;
  287. c = rx_buffer[tail];
  288. rx_buffer_tail = tail;
  289. return c;
  290. }
  291. int serial2_peek(void)
  292. {
  293. uint32_t head, tail;
  294. head = rx_buffer_head;
  295. tail = rx_buffer_tail;
  296. if (head == tail) return -1;
  297. if (++tail >= RX_BUFFER_SIZE) tail = 0;
  298. return rx_buffer[tail];
  299. }
  300. void serial2_clear(void)
  301. {
  302. #ifdef HAS_KINETISK_UART1_FIFO
  303. if (!(SIM_SCGC4 & SIM_SCGC4_UART1)) return;
  304. UART1_C2 &= ~(UART_C2_RE | UART_C2_RIE | UART_C2_ILIE);
  305. UART1_CFIFO = UART_CFIFO_RXFLUSH;
  306. UART1_C2 |= (UART_C2_RE | UART_C2_RIE | UART_C2_ILIE);
  307. #endif
  308. rx_buffer_head = rx_buffer_tail;
  309. }
  310. // status interrupt combines
  311. // Transmit data below watermark UART_S1_TDRE
  312. // Transmit complete UART_S1_TC
  313. // Idle line UART_S1_IDLE
  314. // Receive data above watermark UART_S1_RDRF
  315. // LIN break detect UART_S2_LBKDIF
  316. // RxD pin active edge UART_S2_RXEDGIF
  317. void uart1_status_isr(void)
  318. {
  319. uint32_t head, tail, n;
  320. uint8_t c;
  321. #ifdef HAS_KINETISK_UART1_FIFO
  322. uint32_t newhead;
  323. uint8_t avail;
  324. if (UART1_S1 & (UART_S1_RDRF | UART_S1_IDLE)) {
  325. __disable_irq();
  326. avail = UART1_RCFIFO;
  327. if (avail == 0) {
  328. // The only way to clear the IDLE interrupt flag is
  329. // to read the data register. But reading with no
  330. // data causes a FIFO underrun, which causes the
  331. // FIFO to return corrupted data. If anyone from
  332. // Freescale reads this, what a poor design! There
  333. // write should be a write-1-to-clear for IDLE.
  334. c = UART1_D;
  335. // flushing the fifo recovers from the underrun,
  336. // but there's a possible race condition where a
  337. // new character could be received between reading
  338. // RCFIFO == 0 and flushing the FIFO. To minimize
  339. // the chance, interrupts are disabled so a higher
  340. // priority interrupt (hopefully) doesn't delay.
  341. // TODO: change this to disabling the IDLE interrupt
  342. // which won't be simple, since we already manage
  343. // which transmit interrupts are enabled.
  344. UART1_CFIFO = UART_CFIFO_RXFLUSH;
  345. __enable_irq();
  346. } else {
  347. __enable_irq();
  348. head = rx_buffer_head;
  349. tail = rx_buffer_tail;
  350. do {
  351. if (use9Bits && (UART1_C3 & 0x80)) {
  352. n = UART1_D | 0x100;
  353. } else {
  354. n = UART1_D;
  355. }
  356. newhead = head + 1;
  357. if (newhead >= RX_BUFFER_SIZE) newhead = 0;
  358. if (newhead != tail) {
  359. head = newhead;
  360. rx_buffer[head] = n;
  361. }
  362. } while (--avail > 0);
  363. rx_buffer_head = head;
  364. }
  365. }
  366. c = UART1_C2;
  367. if ((c & UART_C2_TIE) && (UART1_S1 & UART_S1_TDRE)) {
  368. head = tx_buffer_head;
  369. tail = tx_buffer_tail;
  370. do {
  371. if (tail == head) break;
  372. if (++tail >= TX_BUFFER_SIZE) tail = 0;
  373. avail = UART1_S1;
  374. n = tx_buffer[tail];
  375. if (use9Bits) UART1_C3 = (UART1_C3 & ~0x40) | ((n & 0x100) >> 2);
  376. UART1_D = n;
  377. } while (UART1_TCFIFO < 8);
  378. tx_buffer_tail = tail;
  379. if (UART1_S1 & UART_S1_TDRE) UART1_C2 = C2_TX_COMPLETING;
  380. }
  381. #else
  382. if (UART1_S1 & UART_S1_RDRF) {
  383. n = UART1_D;
  384. if (use9Bits && (UART1_C3 & 0x80)) n |= 0x100;
  385. head = rx_buffer_head + 1;
  386. if (head >= RX_BUFFER_SIZE) head = 0;
  387. if (head != rx_buffer_tail) {
  388. rx_buffer[head] = n;
  389. rx_buffer_head = head;
  390. }
  391. }
  392. c = UART1_C2;
  393. if ((c & UART_C2_TIE) && (UART1_S1 & UART_S1_TDRE)) {
  394. head = tx_buffer_head;
  395. tail = tx_buffer_tail;
  396. if (head == tail) {
  397. UART1_C2 = C2_TX_COMPLETING;
  398. } else {
  399. if (++tail >= TX_BUFFER_SIZE) tail = 0;
  400. n = tx_buffer[tail];
  401. if (use9Bits) UART1_C3 = (UART1_C3 & ~0x40) | ((n & 0x100) >> 2);
  402. UART1_D = n;
  403. tx_buffer_tail = tail;
  404. }
  405. }
  406. #endif
  407. if ((c & UART_C2_TCIE) && (UART1_S1 & UART_S1_TC)) {
  408. transmitting = 0;
  409. if (transmit_pin) transmit_deassert();
  410. UART1_C2 = C2_TX_INACTIVE;
  411. }
  412. }