Teensy 4.1 core updated for C++20
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

439 行
13KB

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