Teensy 4.1 core updated for C++20
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

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