Teensy 4.1 core updated for C++20
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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