Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

serial2.c 11KB

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