No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

508 líneas
14KB

  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. // alternate version supporting RTS by disable of receive interrupt
  31. // https://forum.pjrc.com/threads/30536-Complete-implementation-of-UART-hardware-flow-control
  32. // https://forum.pjrc.com/threads/29446-Teensy-Hardware-Flow-Control-RTS-CTS/page2
  33. #include "kinetis.h"
  34. #include "core_pins.h"
  35. #include "HardwareSerial.h"
  36. ////////////////////////////////////////////////////////////////
  37. // Tunable parameters (relatively safe to edit these numbers)
  38. ////////////////////////////////////////////////////////////////
  39. #define TX_BUFFER_SIZE 64 // number of outgoing bytes to buffer
  40. #define RX_BUFFER_SIZE 64 // number of incoming bytes to buffer
  41. #define IRQ_PRIORITY 64 // 0 = highest priority, 255 = lowest
  42. ////////////////////////////////////////////////////////////////
  43. // changes not recommended below this point....
  44. ////////////////////////////////////////////////////////////////
  45. #ifdef SERIAL_9BIT_SUPPORT
  46. static uint8_t use9Bits = 0;
  47. #define BUFTYPE uint16_t
  48. #else
  49. #define BUFTYPE uint8_t
  50. #define use9Bits 0
  51. #endif
  52. static volatile BUFTYPE tx_buffer[TX_BUFFER_SIZE];
  53. static volatile BUFTYPE rx_buffer[RX_BUFFER_SIZE];
  54. static volatile uint8_t transmitting = 0;
  55. #if defined(KINETISK)
  56. static volatile uint8_t *transmit_pin=NULL;
  57. #define transmit_assert() *transmit_pin = 1
  58. #define transmit_deassert() *transmit_pin = 0
  59. #elif defined(KINETISL)
  60. static volatile uint8_t *transmit_pin=NULL;
  61. static uint8_t transmit_mask=0;
  62. #define transmit_assert() *(transmit_pin+4) = transmit_mask;
  63. #define transmit_deassert() *(transmit_pin+8) = transmit_mask;
  64. #endif
  65. #if TX_BUFFER_SIZE > 255
  66. static volatile uint16_t tx_buffer_head = 0;
  67. static volatile uint16_t tx_buffer_tail = 0;
  68. #else
  69. static volatile uint8_t tx_buffer_head = 0;
  70. static volatile uint8_t tx_buffer_tail = 0;
  71. #endif
  72. #if RX_BUFFER_SIZE > 255
  73. static volatile uint16_t rx_buffer_head = 0;
  74. static volatile uint16_t rx_buffer_tail = 0;
  75. #else
  76. static volatile uint8_t rx_buffer_head = 0;
  77. static volatile uint8_t rx_buffer_tail = 0;
  78. #endif
  79. // UART0 and UART1 are clocked by F_CPU, UART2 is clocked by F_BUS
  80. // UART0 has 8 byte fifo, UART1 and UART2 have 1 byte buffer
  81. void serial_begin(uint32_t divisor)
  82. {
  83. SIM_SCGC4 |= SIM_SCGC4_UART0; // turn on clock, TODO: use bitband
  84. rx_buffer_head = 0;
  85. rx_buffer_tail = 0;
  86. tx_buffer_head = 0;
  87. tx_buffer_tail = 0;
  88. transmitting = 0;
  89. CORE_PIN0_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3);
  90. CORE_PIN1_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3);
  91. #if defined(HAS_KINETISK_UART0)
  92. UART0_BDH = (divisor >> 13) & 0x1F;
  93. UART0_BDL = (divisor >> 5) & 0xFF;
  94. UART0_C4 = divisor & 0x1F;
  95. #ifdef HAS_KINETISK_UART0_FIFO
  96. UART0_C1 = UART_C1_ILT;
  97. UART0_TWFIFO = 2; // tx watermark, causes S1_TDRE to set
  98. UART0_RWFIFO = 4; // rx watermark, causes S1_RDRF to set
  99. UART0_PFIFO = UART_PFIFO_TXFE | UART_PFIFO_RXFE;
  100. #else
  101. UART0_C1 = 0;
  102. UART0_PFIFO = 0;
  103. #endif
  104. #elif defined(HAS_KINETISL_UART0)
  105. UART0_BDH = (divisor >> 8) & 0x1F;
  106. UART0_BDL = divisor & 0xFF;
  107. UART0_C1 = 0;
  108. #endif
  109. UART0_C2 = UART_C2_TE | UART_C2_RE | UART_C2_RIE | UART_C2_ILIE;
  110. NVIC_SET_PRIORITY(IRQ_UART0_STATUS, IRQ_PRIORITY);
  111. NVIC_ENABLE_IRQ(IRQ_UART0_STATUS);
  112. }
  113. void serial_format(uint32_t format)
  114. {
  115. uint8_t c;
  116. c = UART0_C1;
  117. c = (c & ~0x13) | (format & 0x03); // configure parity
  118. if (format & 0x04) c |= 0x10; // 9 bits (might include parity)
  119. UART0_C1 = c;
  120. if ((format & 0x0F) == 0x04) UART0_C3 |= 0x40; // 8N2 is 9 bit with 9th bit always 1
  121. c = UART0_S2 & ~0x10;
  122. if (format & 0x10) c |= 0x10; // rx invert
  123. UART0_S2 = c;
  124. c = UART0_C3 & ~0x10;
  125. if (format & 0x20) c |= 0x10; // tx invert
  126. UART0_C3 = c;
  127. #ifdef SERIAL_9BIT_SUPPORT
  128. c = UART0_C4 & 0x1F;
  129. if (format & 0x08) c |= 0x20; // 9 bit mode with parity (requires 10 bits)
  130. UART0_C4 = c;
  131. use9Bits = format & 0x80;
  132. #endif
  133. }
  134. void serial_end(void)
  135. {
  136. if (!(SIM_SCGC4 & SIM_SCGC4_UART0)) return;
  137. while (transmitting) yield(); // wait for buffered data to send
  138. NVIC_DISABLE_IRQ(IRQ_UART0_STATUS);
  139. UART0_C2 = 0;
  140. CORE_PIN0_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1);
  141. CORE_PIN1_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1);
  142. rx_buffer_head = 0;
  143. rx_buffer_tail = 0;
  144. }
  145. void serial_set_transmit_pin(uint8_t pin)
  146. {
  147. while (transmitting) ;
  148. pinMode(pin, OUTPUT);
  149. digitalWrite(pin, LOW);
  150. transmit_pin = portOutputRegister(pin);
  151. #if defined(KINETISL)
  152. transmit_mask = digitalPinToBitMask(pin);
  153. #endif
  154. }
  155. int serial_set_rts(uint8_t pin)
  156. {
  157. if (!(SIM_SCGC4 & SIM_SCGC4_UART0)) return 0;
  158. if (pin == 6) {
  159. CORE_PIN6_CONFIG = PORT_PCR_MUX(3);
  160. } else if (pin == 19) {
  161. CORE_PIN19_CONFIG = PORT_PCR_MUX(3);
  162. } else {
  163. UART0_MODEM &= ~UART_MODEM_RXRTSE;
  164. return 0;
  165. }
  166. UART0_MODEM |= UART_MODEM_RXRTSE;
  167. return 1;
  168. }
  169. int serial_set_cts(uint8_t pin)
  170. {
  171. if (!(SIM_SCGC4 & SIM_SCGC4_UART0)) return 0;
  172. if (pin == 18) {
  173. CORE_PIN18_CONFIG = PORT_PCR_MUX(3); // TODO: weak pullup or pulldown?
  174. } else if (pin == 20) {
  175. CORE_PIN20_CONFIG = PORT_PCR_MUX(3); // TODO: weak pullup or pulldown?
  176. } else {
  177. UART0_MODEM &= ~UART_MODEM_TXCTSE;
  178. return 0;
  179. }
  180. UART0_MODEM |= UART_MODEM_TXCTSE;
  181. return 1;
  182. }
  183. void serial_putchar(uint32_t c)
  184. {
  185. uint32_t head, n;
  186. if (!(SIM_SCGC4 & SIM_SCGC4_UART0)) return;
  187. if (transmit_pin) transmit_assert();
  188. head = tx_buffer_head;
  189. if (++head >= TX_BUFFER_SIZE) head = 0;
  190. while (tx_buffer_tail == head) {
  191. int priority = nvic_execution_priority();
  192. if (priority <= IRQ_PRIORITY) {
  193. if ((UART0_S1 & UART_S1_TDRE)) {
  194. uint32_t tail = tx_buffer_tail;
  195. if (++tail >= TX_BUFFER_SIZE) tail = 0;
  196. n = tx_buffer[tail];
  197. if (use9Bits) UART0_C3 = (UART0_C3 & ~0x40) | ((n & 0x100) >> 2);
  198. UART0_D = n;
  199. tx_buffer_tail = tail;
  200. }
  201. } else if (priority >= 256) {
  202. yield();
  203. }
  204. }
  205. tx_buffer[head] = c;
  206. transmitting = 1;
  207. tx_buffer_head = head;
  208. UART0_C2 |= UART_C2_TIE;
  209. UART0_C2 &= ~UART_C2_TCIE;
  210. }
  211. #ifdef HAS_KINETISK_UART0_FIFO
  212. void serial_write(const void *buf, unsigned int count)
  213. {
  214. const uint8_t *p = (const uint8_t *)buf;
  215. const uint8_t *end = p + count;
  216. uint32_t head, n;
  217. if (!(SIM_SCGC4 & SIM_SCGC4_UART0)) return;
  218. if (transmit_pin) transmit_assert();
  219. while (p < end) {
  220. head = tx_buffer_head;
  221. if (++head >= TX_BUFFER_SIZE) head = 0;
  222. if (tx_buffer_tail == head) {
  223. UART0_C2 |= UART_C2_TIE;
  224. UART0_C2 &= ~UART_C2_TCIE;
  225. do {
  226. int priority = nvic_execution_priority();
  227. if (priority <= IRQ_PRIORITY) {
  228. if ((UART0_S1 & UART_S1_TDRE)) {
  229. uint32_t tail = tx_buffer_tail;
  230. if (++tail >= TX_BUFFER_SIZE) tail = 0;
  231. n = tx_buffer[tail];
  232. if (use9Bits) UART0_C3 = (UART0_C3 & ~0x40) | ((n & 0x100) >> 2);
  233. UART0_D = n;
  234. tx_buffer_tail = tail;
  235. }
  236. } else if (priority >= 256) {
  237. yield();
  238. }
  239. } while (tx_buffer_tail == head);
  240. }
  241. tx_buffer[head] = *p++;
  242. transmitting = 1;
  243. tx_buffer_head = head;
  244. }
  245. UART0_C2 |= UART_C2_TIE;
  246. UART0_C2 &= ~UART_C2_TCIE;
  247. }
  248. #else
  249. void serial_write(const void *buf, unsigned int count)
  250. {
  251. const uint8_t *p = (const uint8_t *)buf;
  252. while (count-- > 0) serial_putchar(*p++);
  253. }
  254. #endif
  255. void serial_flush(void)
  256. {
  257. while (transmitting) yield(); // wait
  258. }
  259. int serial_write_buffer_free(void)
  260. {
  261. uint32_t head, tail;
  262. head = tx_buffer_head;
  263. tail = tx_buffer_tail;
  264. if (head >= tail) return TX_BUFFER_SIZE - 1 - head + tail;
  265. return tail - head - 1;
  266. }
  267. int serial_available(void)
  268. {
  269. uint32_t head, tail;
  270. head = rx_buffer_head;
  271. tail = rx_buffer_tail;
  272. if (head >= tail) return head - tail;
  273. return RX_BUFFER_SIZE + head - tail;
  274. }
  275. int serial_getchar(void)
  276. {
  277. uint32_t head, tail;
  278. int c;
  279. head = rx_buffer_head;
  280. tail = rx_buffer_tail;
  281. if (head == tail) return -1;
  282. if (++tail >= RX_BUFFER_SIZE) tail = 0;
  283. c = rx_buffer[tail];
  284. rx_buffer_tail = tail;
  285. #ifdef HAS_KINETISK_UART0_FIFO
  286. if ((UART0_C2 & (UART_C2_RIE | UART_C2_ILIE))==0) {//rx interrupt currently disabled
  287. int freespace;
  288. if (head >= tail) //rx head and tail would be unchanged from above if interrupts were disabled
  289. freespace = RX_BUFFER_SIZE -1 + tail - head;
  290. else
  291. freespace = tail - head - 1;
  292. if (freespace >= UART0_RCFIFO) {
  293. UART0_C2 |= (UART_C2_RIE | UART_C2_ILIE);//enable rx interrupts
  294. }
  295. }
  296. #else
  297. UART0_C2 |= UART_C2_RIE;
  298. #endif
  299. return c;
  300. }
  301. int serial_peek(void)
  302. {
  303. uint32_t head, tail;
  304. head = rx_buffer_head;
  305. tail = rx_buffer_tail;
  306. if (head == tail) return -1;
  307. if (++tail >= RX_BUFFER_SIZE) tail = 0;
  308. return rx_buffer[tail];
  309. }
  310. void serial_clear(void)
  311. {
  312. #ifdef HAS_KINETISK_UART0_FIFO
  313. if (!(SIM_SCGC4 & SIM_SCGC4_UART0)) return;
  314. UART0_C2 &= ~(UART_C2_RE | UART_C2_RIE | UART_C2_ILIE);
  315. UART0_CFIFO = UART_CFIFO_RXFLUSH;
  316. UART0_C2 |= (UART_C2_RE | UART_C2_RIE | UART_C2_ILIE);
  317. #endif
  318. rx_buffer_head = rx_buffer_tail;
  319. }
  320. // status interrupt combines
  321. // Transmit data below watermark UART_S1_TDRE
  322. // Transmit complete UART_S1_TC
  323. // Idle line UART_S1_IDLE
  324. // Receive data above watermark UART_S1_RDRF
  325. // LIN break detect UART_S2_LBKDIF
  326. // RxD pin active edge UART_S2_RXEDGIF
  327. void uart0_status_isr(void)
  328. {
  329. uint32_t head, tail, n;
  330. uint8_t c;
  331. #ifdef HAS_KINETISK_UART0_FIFO
  332. uint32_t newhead;
  333. if (UART0_S1 & (UART_S1_RDRF | UART_S1_IDLE)) {
  334. if (UART0_RCFIFO == 0) {
  335. // The only way to clear the IDLE interrupt flag is
  336. // to read the data register. But reading with no
  337. // data causes a FIFO underrun, which causes the
  338. // FIFO to return corrupted data. If anyone from
  339. // Freescale reads this, what a poor design! There
  340. // write should be a write-1-to-clear for IDLE.
  341. c = UART0_D;
  342. // flushing the fifo recovers from the underrun,
  343. // but there's a possible race condition where a
  344. // new character could be received between reading
  345. // RCFIFO == 0 and flushing the FIFO. To minimize
  346. // the chance, interrupts are disabled so a higher
  347. // priority interrupt (hopefully) doesn't delay.
  348. // TODO: change this to disabling the IDLE interrupt
  349. // which won't be simple, since we already manage
  350. // which transmit interrupts are enabled.
  351. __disable_irq();
  352. UART0_CFIFO = UART_CFIFO_RXFLUSH;
  353. __enable_irq();
  354. } else {
  355. head = rx_buffer_head;
  356. tail = rx_buffer_tail;
  357. do {
  358. newhead = head + 1;
  359. if (newhead >= RX_BUFFER_SIZE) newhead = 0;
  360. if (UART0_MODEM & UART_MODEM_RXRTSE) {
  361. if (newhead == tail) {
  362. UART0_C2 &= ~(UART_C2_RIE | UART_C2_ILIE);//disable rx interrupts
  363. break;
  364. }
  365. }
  366. if (UART0_RCFIFO==1) UART0_S1; //as per page 1214 of datasheet regarding resetting of RDRF flag
  367. if (use9Bits && (UART0_C3 & 0x80)) {
  368. n = UART0_D | 0x100;
  369. } else {
  370. n = UART0_D;
  371. }
  372. head = newhead;
  373. rx_buffer[head] = n;
  374. } while (UART0_RCFIFO);
  375. rx_buffer_head = head;
  376. }
  377. }
  378. c = UART0_C2;
  379. if ((c & UART_C2_TIE) && (UART0_S1 & UART_S1_TDRE)) {
  380. head = tx_buffer_head;
  381. tail = tx_buffer_tail;
  382. do {
  383. if (tail == head) break;
  384. if (++tail >= TX_BUFFER_SIZE) tail = 0;
  385. UART0_S1;
  386. n = tx_buffer[tail];
  387. if (use9Bits) UART0_C3 = (UART0_C3 & ~0x40) | ((n & 0x100) >> 2);
  388. UART0_D = n;
  389. } while (UART0_TCFIFO < 8);
  390. tx_buffer_tail = tail;
  391. if (UART0_S1 & UART_S1_TDRE) {
  392. UART0_C2 |= UART_C2_TCIE;
  393. UART0_C2 &= ~UART_C2_TIE;
  394. }
  395. }
  396. #else
  397. if (UART0_S1 & UART_S1_RDRF) {
  398. do {
  399. head = rx_buffer_head + 1;
  400. if (head >= RX_BUFFER_SIZE) head = 0;
  401. if (UART0_MODEM & UART_MODEM_RXRTSE) {
  402. if (head == rx_buffer_tail) {
  403. UART0_C2 &= ~(UART_C2_RIE);//disable rx interrupts
  404. break;
  405. }
  406. }
  407. n = UART0_D;
  408. if (use9Bits && (UART0_C3 & 0x80)) n |= 0x100;
  409. rx_buffer[head] = n;
  410. rx_buffer_head = head;
  411. break;
  412. } while (true);
  413. }
  414. c = UART0_C2;
  415. if ((c & UART_C2_TIE) && (UART0_S1 & UART_S1_TDRE)) {
  416. head = tx_buffer_head;
  417. tail = tx_buffer_tail;
  418. if (head == tail) {
  419. UART0_C2 |= UART_C2_TCIE;
  420. UART0_C2 &= ~UART_C2_TIE;
  421. } else {
  422. if (++tail >= TX_BUFFER_SIZE) tail = 0;
  423. n = tx_buffer[tail];
  424. if (use9Bits) UART0_C3 = (UART0_C3 & ~0x40) | ((n & 0x100) >> 2);
  425. UART0_D = n;
  426. tx_buffer_tail = tail;
  427. }
  428. }
  429. #endif
  430. if ((c & UART_C2_TCIE) && (UART0_S1 & UART_S1_TC)) {
  431. transmitting = 0;
  432. if (transmit_pin) transmit_deassert();
  433. UART0_C2 &= ~(UART_C2_TCIE | UART_C2_TIE);
  434. }
  435. }
  436. void serial_print(const char *p)
  437. {
  438. while (*p) {
  439. char c = *p++;
  440. if (c == '\n') serial_putchar('\r');
  441. serial_putchar(c);
  442. }
  443. }
  444. static void serial_phex1(uint32_t n)
  445. {
  446. n &= 15;
  447. if (n < 10) {
  448. serial_putchar('0' + n);
  449. } else {
  450. serial_putchar('A' - 10 + n);
  451. }
  452. }
  453. void serial_phex(uint32_t n)
  454. {
  455. serial_phex1(n >> 4);
  456. serial_phex1(n);
  457. }
  458. void serial_phex16(uint32_t n)
  459. {
  460. serial_phex(n >> 8);
  461. serial_phex(n);
  462. }
  463. void serial_phex32(uint32_t n)
  464. {
  465. serial_phex(n >> 24);
  466. serial_phex(n >> 16);
  467. serial_phex(n >> 8);
  468. serial_phex(n);
  469. }