Teensy 4.1 core updated for C++20
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.

483 lines
14KB

  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. #ifdef HAS_KINETISK_LPUART0
  34. #define GPIO_BITBAND_ADDR(reg, bit) (((uint32_t)&(reg) - 0x40000000) * 32 + (bit) * 4 + 0x42000000)
  35. #define GPIO_BITBAND_PTR(reg, bit) ((uint32_t *)GPIO_BITBAND_ADDR((reg), (bit)))
  36. #define BITBAND_SET_BIT(reg, bit) (*GPIO_BITBAND_PTR((reg), (bit)) = 1)
  37. #define BITBAND_CLR_BIT(reg, bit) (*GPIO_BITBAND_PTR((reg), (bit)) = 0)
  38. #define CTRL_TXDIR_BIT 29
  39. #define CTRL_TIE_BIT 23
  40. #define CTRL_TCIE_BIT 22
  41. #define CTRL_TE_BIT 19
  42. #define CTRL_RE_BIT 18
  43. #define CTRL_LOOPS_BIT 7
  44. #define CTRL_RSRC_BIT 5
  45. ////////////////////////////////////////////////////////////////
  46. // Tunable parameters (relatively safe to edit these numbers)
  47. ////////////////////////////////////////////////////////////////
  48. #ifndef SERIAL6_TX_BUFFER_SIZE
  49. #define SERIAL6_TX_BUFFER_SIZE 40 // number of outgoing bytes to buffer
  50. #endif
  51. #ifndef SERIAL6_RX_BUFFER_SIZE
  52. #define SERIAL6_RX_BUFFER_SIZE 64 // number of incoming bytes to buffer
  53. #endif
  54. #define RTS_HIGH_WATERMARK (SERIAL6_RX_BUFFER_SIZE-24) // RTS requests sender to pause
  55. #define RTS_LOW_WATERMARK (SERIAL6_RX_BUFFER_SIZE-38) // RTS allows sender to resume
  56. #define IRQ_PRIORITY 64 // 0 = highest priority, 255 = lowest
  57. ////////////////////////////////////////////////////////////////
  58. // changes not recommended below this point....
  59. ////////////////////////////////////////////////////////////////
  60. #ifdef SERIAL_9BIT_SUPPORT
  61. static uint8_t use9Bits = 0;
  62. #define BUFTYPE uint16_t
  63. #else
  64. #define BUFTYPE uint8_t
  65. #define use9Bits 0
  66. #endif
  67. static volatile BUFTYPE tx_buffer[SERIAL6_TX_BUFFER_SIZE];
  68. static volatile BUFTYPE rx_buffer[SERIAL6_RX_BUFFER_SIZE];
  69. static volatile uint8_t transmitting = 0;
  70. static volatile uint8_t *transmit_pin=NULL;
  71. #define transmit_assert() *transmit_pin = 1
  72. #define transmit_deassert() *transmit_pin = 0
  73. static volatile uint8_t *rts_pin=NULL;
  74. #define rts_assert() *rts_pin = 0
  75. #define rts_deassert() *rts_pin = 1
  76. #if SERIAL6_TX_BUFFER_SIZE > 255
  77. static volatile uint16_t tx_buffer_head = 0;
  78. static volatile uint16_t tx_buffer_tail = 0;
  79. #else
  80. static volatile uint8_t tx_buffer_head = 0;
  81. static volatile uint8_t tx_buffer_tail = 0;
  82. #endif
  83. #if SERIAL6_RX_BUFFER_SIZE > 255
  84. static volatile uint16_t rx_buffer_head = 0;
  85. static volatile uint16_t rx_buffer_tail = 0;
  86. #else
  87. static volatile uint8_t rx_buffer_head = 0;
  88. static volatile uint8_t rx_buffer_tail = 0;
  89. #endif
  90. static uint8_t tx_pin_num = 48;
  91. void serial6_begin(uint32_t desiredBaudRate)
  92. {
  93. #define F_LPUART_CLOCK_SPEED 48000000 //F_BUS
  94. // Make sure the clock for this uart is enabled, else the registers are not
  95. // vailable.
  96. SIM_SCGC2 |= SIM_SCGC2_LPUART0; // Turn on the clock
  97. // Convert the baud rate to best divisor and OSR, based off of code I found in posting
  98. // try to find an OSR > 4 with the minimum difference from the actual disired baud rate.
  99. uint16_t sbr, sbrTemp, osrCheck;
  100. uint32_t osr, baudDiffCheck, calculatedBaud, baudDiff;
  101. uint32_t clockSpeed;
  102. // First lets figure out what the LPUART Clock speed is.
  103. uint32_t PLLFLLSEL = SIM_SOPT2 & SIM_SOPT2_IRC48SEL; // Note: Bot bits on here
  104. if (PLLFLLSEL == SIM_SOPT2_IRC48SEL)
  105. clockSpeed = 48000000; // Fixed to 48mhz
  106. else if (PLLFLLSEL == SIM_SOPT2_PLLFLLSEL)
  107. clockSpeed = F_PLL; // Using PLL clock
  108. else
  109. clockSpeed = F_CPU/4; // FLL clock, guessing
  110. osr = 4;
  111. sbr = (clockSpeed/(desiredBaudRate * osr));
  112. /*set sbr to 1 if the clockSpeed can not satisfy the desired baud rate*/
  113. if(sbr == 0) {
  114. // Maybe print something.
  115. return; // can not initialize
  116. }
  117. // With integer math the divide*muliply implies the calculated baud will be >= desired baud
  118. calculatedBaud = (clockSpeed / (osr * sbr));
  119. baudDiff = calculatedBaud - desiredBaudRate;
  120. // Check if better off with sbr+1
  121. if (baudDiff != 0) {
  122. calculatedBaud = (clockSpeed / (osr * (sbr + 1)));
  123. baudDiffCheck = desiredBaudRate - calculatedBaud ;
  124. if (baudDiffCheck < baudDiff) {
  125. sbr++; // use the higher sbr
  126. baudDiff = baudDiffCheck;
  127. }
  128. }
  129. // loop to find the best osr value possible, one that generates minimum baudDiff
  130. for (osrCheck = 5; osrCheck <= 32; osrCheck++) {
  131. sbrTemp = (clockSpeed/(desiredBaudRate * osrCheck));
  132. if(sbrTemp == 0)
  133. break; // higher divisor returns 0 so can not use...
  134. // Remember integer math so (X/Y)*Y will always be <=X
  135. calculatedBaud = (clockSpeed / (osrCheck * sbrTemp));
  136. baudDiffCheck = calculatedBaud - desiredBaudRate;
  137. if (baudDiffCheck <= baudDiff) {
  138. baudDiff = baudDiffCheck;
  139. osr = osrCheck;
  140. sbr = sbrTemp;
  141. }
  142. // Lets try the rounded up one as well
  143. if (baudDiffCheck) {
  144. calculatedBaud = (clockSpeed / (osrCheck * ++sbrTemp));
  145. baudDiffCheck = desiredBaudRate - calculatedBaud;
  146. if (baudDiffCheck <= baudDiff) {
  147. baudDiff = baudDiffCheck;
  148. osr = osrCheck;
  149. sbr = sbrTemp;
  150. }
  151. }
  152. }
  153. // for lower OSR <= 7x turn on both edge sampling
  154. uint32_t lpb = LPUART_BAUD_OSR(osr-1) | LPUART_BAUD_SBR(sbr);
  155. if (osr < 8) {
  156. lpb |= LPUART_BAUD_BOTHEDGE;
  157. }
  158. LPUART0_BAUD = lpb;
  159. SIM_SOPT2 |= SIM_SOPT2_LPUARTSRC(1); // Lets use PLL?
  160. rx_buffer_head = 0;
  161. rx_buffer_tail = 0;
  162. tx_buffer_head = 0;
  163. tx_buffer_tail = 0;
  164. transmitting = 0;
  165. CORE_PIN47_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(5);
  166. CORE_PIN48_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(5);
  167. LPUART0_CTRL = 0;
  168. LPUART0_MATCH = 0;
  169. LPUART0_STAT = 0;
  170. // Enable the transmitter, receiver and enable receiver interrupt
  171. LPUART0_CTRL |= LPUART_CTRL_RIE | LPUART_CTRL_TE | LPUART_CTRL_RE;
  172. NVIC_SET_PRIORITY(IRQ_LPUART0, IRQ_PRIORITY);
  173. NVIC_ENABLE_IRQ(IRQ_LPUART0);
  174. }
  175. void serial6_format(uint32_t format)
  176. {
  177. uint32_t c;
  178. // Bits 0-2 - Parity plus 9 bit.
  179. c = LPUART0_CTRL;
  180. //c = (c & ~(LPUART_CTRL_M | LPUART_CTRL_PE | LPUART_CTRL_PT)) | (format & (LPUART_CTRL_PE | LPUART_CTRL_PT)); // configure parity
  181. //if (format & 0x04) c |= LPUART_CTRL_M; // 9 bits (might include parity)
  182. c = (c & ~0x13) | (format & 0x03); // configure parity
  183. if (format & 0x04) c |= 0x10; // 9 bits (might include parity)
  184. LPUART0_CTRL = c;
  185. if ((format & 0x0F) == 0x04) LPUART0_CTRL |= LPUART_CTRL_T8; // 8N2 is 9 bit with 9th bit always 1
  186. // Bit 3 10 bit - Will assume that begin already cleared it.
  187. if (format & 0x08)
  188. LPUART0_BAUD |= LPUART_BAUD_M10;
  189. // Bit 4 RXINVERT
  190. c = LPUART0_STAT & ~LPUART_STAT_RXINV;
  191. if (format & 0x10) c |= LPUART_STAT_RXINV; // rx invert
  192. LPUART0_STAT = c;
  193. // Bit 5 TXINVERT
  194. c = LPUART0_CTRL & ~LPUART_CTRL_TXINV;
  195. if (format & 0x20) c |= LPUART_CTRL_TXINV; // tx invert
  196. LPUART0_CTRL = c;
  197. // For T3.6 See about turning on 2 stop bit mode
  198. if ( format & 0x100) LPUART0_BAUD |= LPUART_BAUD_SBNS;
  199. // process request for half duplex.
  200. if ((format & SERIAL_HALF_DUPLEX) != 0) {
  201. BITBAND_SET_BIT(LPUART0_CTRL, CTRL_LOOPS_BIT);
  202. BITBAND_SET_BIT(LPUART0_CTRL, CTRL_RSRC_BIT);
  203. CORE_PIN48_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(5);
  204. // Lets try to make use of bitband address to set the direction for ue...
  205. transmit_pin = (uint8_t*)GPIO_BITBAND_PTR(LPUART0_CTRL, CTRL_TXDIR_BIT);
  206. } else {
  207. if (transmit_pin == (uint8_t*)GPIO_BITBAND_PTR(LPUART0_CTRL, CTRL_TXDIR_BIT)) transmit_pin = NULL;
  208. BITBAND_CLR_BIT(LPUART0_CTRL, CTRL_LOOPS_BIT);
  209. BITBAND_CLR_BIT(LPUART0_CTRL, CTRL_RSRC_BIT);
  210. }
  211. }
  212. void serial6_end(void)
  213. {
  214. if (!(SIM_SCGC2 & SIM_SCGC2_LPUART0)) return;
  215. while (transmitting) yield(); // wait for buffered data to send
  216. NVIC_DISABLE_IRQ(IRQ_LPUART0);
  217. LPUART0_CTRL = 0;
  218. CORE_PIN47_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1);
  219. CORE_PIN48_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1);
  220. rx_buffer_head = 0;
  221. rx_buffer_tail = 0;
  222. if (rts_pin) rts_deassert();
  223. }
  224. void serial6_set_transmit_pin(uint8_t pin)
  225. {
  226. while (transmitting) ;
  227. pinMode(pin, OUTPUT);
  228. digitalWrite(pin, LOW);
  229. transmit_pin = portOutputRegister(pin);
  230. }
  231. void serial6_set_tx(uint8_t pin, uint8_t opendrain)
  232. {
  233. uint32_t cfg;
  234. if (opendrain) pin |= 128;
  235. if (pin == tx_pin_num) return;
  236. if ((SIM_SCGC2 & SIM_SCGC2_LPUART0)) {
  237. switch (tx_pin_num & 127) {
  238. case 48: CORE_PIN48_CONFIG = 0; break; // PTE24
  239. }
  240. if (opendrain) {
  241. cfg = PORT_PCR_DSE | PORT_PCR_ODE;
  242. } else {
  243. cfg = PORT_PCR_DSE | PORT_PCR_SRE;
  244. }
  245. switch (pin & 127) {
  246. case 48: CORE_PIN48_CONFIG = cfg | PORT_PCR_MUX(5); break;
  247. }
  248. }
  249. tx_pin_num = pin;
  250. }
  251. void serial6_set_rx(uint8_t pin)
  252. {
  253. }
  254. int serial6_set_rts(uint8_t pin)
  255. {
  256. if (!(SIM_SCGC2 & SIM_SCGC2_LPUART0)) return 0;
  257. if (pin < CORE_NUM_DIGITAL) {
  258. rts_pin = portOutputRegister(pin);
  259. pinMode(pin, OUTPUT);
  260. rts_assert();
  261. } else {
  262. rts_pin = NULL;
  263. return 0;
  264. }
  265. return 1;
  266. }
  267. int serial6_set_cts(uint8_t pin)
  268. {
  269. if (!(SIM_SCGC2 & SIM_SCGC2_LPUART0)) return 0;
  270. if (pin == 56) {
  271. CORE_PIN56_CONFIG = PORT_PCR_MUX(5) | PORT_PCR_PE; // weak pulldown
  272. } else {
  273. UART5_MODEM &= ~UART_MODEM_TXCTSE;
  274. return 0;
  275. }
  276. UART5_MODEM |= UART_MODEM_TXCTSE;
  277. return 1;
  278. }
  279. void serial6_putchar(uint32_t c)
  280. {
  281. uint32_t head, n;
  282. if (!(SIM_SCGC2 & SIM_SCGC2_LPUART0)) return;
  283. if (transmit_pin) transmit_assert();
  284. head = tx_buffer_head;
  285. if (++head >= SERIAL6_TX_BUFFER_SIZE) head = 0;
  286. while (tx_buffer_tail == head) {
  287. int priority = nvic_execution_priority();
  288. if (priority <= IRQ_PRIORITY) {
  289. if ((LPUART0_STAT & LPUART_STAT_TDRE)) {
  290. uint32_t tail = tx_buffer_tail;
  291. if (++tail >= SERIAL6_TX_BUFFER_SIZE) tail = 0;
  292. n = tx_buffer[tail];
  293. //if (use9Bits) UART5_C3 = (UART5_C3 & ~0x40) | ((n & 0x100) >> 2);
  294. LPUART0_DATA = n;
  295. tx_buffer_tail = tail;
  296. }
  297. } else if (priority >= 256) {
  298. yield(); // wait
  299. }
  300. }
  301. tx_buffer[head] = c;
  302. transmitting = 1;
  303. tx_buffer_head = head;
  304. //LPUART0_CTRL |= LPUART_CTRL_TIE; // enable the transmit interrupt
  305. BITBAND_SET_BIT(LPUART0_CTRL, CTRL_TIE_BIT);
  306. }
  307. void serial6_write(const void *buf, unsigned int count)
  308. {
  309. const uint8_t *p = (const uint8_t *)buf;
  310. while (count-- > 0) serial6_putchar(*p++);
  311. }
  312. void serial6_flush(void)
  313. {
  314. while (transmitting) yield(); // wait
  315. }
  316. int serial6_write_buffer_free(void)
  317. {
  318. uint32_t head, tail;
  319. head = tx_buffer_head;
  320. tail = tx_buffer_tail;
  321. if (head >= tail) return SERIAL6_TX_BUFFER_SIZE - 1 - head + tail;
  322. return tail - head - 1;
  323. }
  324. int serial6_available(void)
  325. {
  326. uint32_t head, tail;
  327. head = rx_buffer_head;
  328. tail = rx_buffer_tail;
  329. if (head >= tail) return head - tail;
  330. return SERIAL6_RX_BUFFER_SIZE + head - tail;
  331. }
  332. int serial6_getchar(void)
  333. {
  334. uint32_t head, tail;
  335. int c;
  336. head = rx_buffer_head;
  337. tail = rx_buffer_tail;
  338. if (head == tail) return -1;
  339. if (++tail >= SERIAL6_RX_BUFFER_SIZE) tail = 0;
  340. c = rx_buffer[tail];
  341. rx_buffer_tail = tail;
  342. if (rts_pin) {
  343. int avail;
  344. if (head >= tail) avail = head - tail;
  345. else avail = SERIAL6_RX_BUFFER_SIZE + head - tail;
  346. if (avail <= RTS_LOW_WATERMARK) rts_assert();
  347. }
  348. return c;
  349. }
  350. int serial6_peek(void)
  351. {
  352. uint32_t head, tail;
  353. head = rx_buffer_head;
  354. tail = rx_buffer_tail;
  355. if (head == tail) return -1;
  356. if (++tail >= SERIAL6_RX_BUFFER_SIZE) tail = 0;
  357. return rx_buffer[tail];
  358. }
  359. void serial6_clear(void)
  360. {
  361. rx_buffer_head = rx_buffer_tail;
  362. if (rts_pin) rts_assert();
  363. }
  364. // status interrupt combines
  365. // Transmit data below watermark LPUART_STAT_TDRE
  366. // Transmit complete LPUART_STAT_TC
  367. // Idle line LPUART_STAT_IDLE
  368. // Receive data above watermark LPUART_STAT_RDRF
  369. // LIN break detect UART_S2_LBKDIF
  370. // RxD pin active edge UART_S2_RXEDGIF
  371. void lpuart0_status_isr(void)
  372. {
  373. uint32_t head, tail, n;
  374. uint32_t c;
  375. if (LPUART0_STAT & LPUART_STAT_RDRF) {
  376. // if (use9Bits && (UART5_C3 & 0x80)) {
  377. // n = UART5_D | 0x100;
  378. // } else {
  379. // n = UART5_D;
  380. // }
  381. n = LPUART0_DATA & 0x3ff; // use only the 10 data bits
  382. head = rx_buffer_head + 1;
  383. if (head >= SERIAL6_RX_BUFFER_SIZE) head = 0;
  384. if (head != rx_buffer_tail) {
  385. rx_buffer[head] = n;
  386. rx_buffer_head = head;
  387. }
  388. if (rts_pin) {
  389. int avail;
  390. tail = tx_buffer_tail;
  391. if (head >= tail) avail = head - tail;
  392. else avail = SERIAL6_RX_BUFFER_SIZE + head - tail;
  393. if (avail >= RTS_HIGH_WATERMARK) rts_deassert();
  394. }
  395. }
  396. c = LPUART0_CTRL;
  397. if ((c & LPUART_CTRL_TIE) && (LPUART0_STAT & LPUART_STAT_TDRE)) {
  398. head = tx_buffer_head;
  399. tail = tx_buffer_tail;
  400. if (head == tail) {
  401. BITBAND_CLR_BIT(LPUART0_CTRL, CTRL_TIE_BIT);
  402. BITBAND_SET_BIT(LPUART0_CTRL, CTRL_TCIE_BIT);
  403. //LPUART0_CTRL &= ~LPUART_CTRL_TIE;
  404. //LPUART0_CTRL |= LPUART_CTRL_TCIE; // Actually wondering if we can just leave this one on...
  405. } else {
  406. if (++tail >= SERIAL6_TX_BUFFER_SIZE) tail = 0;
  407. n = tx_buffer[tail];
  408. //if (use9Bits) UART5_C3 = (UART5_C3 & ~0x40) | ((n & 0x100) >> 2);
  409. LPUART0_DATA = n;
  410. tx_buffer_tail = tail;
  411. }
  412. }
  413. if ((c & LPUART_CTRL_TCIE) && (LPUART0_STAT & LPUART_STAT_TC)) {
  414. transmitting = 0;
  415. if (transmit_pin) transmit_deassert();
  416. BITBAND_CLR_BIT(LPUART0_CTRL, CTRL_TCIE_BIT);
  417. // LPUART0_CTRL &= ~LPUART_CTRL_TCIE; // Actually wondering if we can just leave this one on...
  418. }
  419. }
  420. #endif // HAS_KINETISK_LPUART0