Teensy 4.1 core updated for C++20
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

452 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. c = LPUART0_CTRL;
  172. c = (c & ~0x13) | (format & 0x03); // configure parity
  173. if (format & 0x04) c |= 0x10; // 9 bits (might include parity)
  174. LPUART0_CTRL = c;
  175. if ((format & 0x0F) == 0x04) LPUART0_CTRL |= LPUART_CTRL_T8; // 8N2 is 9 bit with 9th bit always 1
  176. // c = UART5_S2 & ~0x10;
  177. // if (format & 0x10) c |= 0x10; // rx invert
  178. // UART5_S2 = c;
  179. c = LPUART0_CTRL & ~LPUART_CTRL_TXINV;
  180. if (format & 0x20) c |= LPUART_CTRL_TXINV; // tx invert
  181. LPUART0_CTRL = c;
  182. #if 0
  183. c = UART5_C4 & 0x1F;
  184. if (format & 0x08) c |= 0x20; // 9 bit mode with parity (requires 10 bits)
  185. UART5_C4 = c;
  186. use9Bits = format & 0x80;
  187. #endif
  188. }
  189. void serial6_end(void)
  190. {
  191. if (!(SIM_SCGC2 & SIM_SCGC2_LPUART0)) return;
  192. while (transmitting) yield(); // wait for buffered data to send
  193. NVIC_DISABLE_IRQ(IRQ_LPUART0);
  194. LPUART0_CTRL = 0;
  195. CORE_PIN47_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1);
  196. CORE_PIN48_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1);
  197. rx_buffer_head = 0;
  198. rx_buffer_tail = 0;
  199. if (rts_pin) rts_deassert();
  200. }
  201. void serial6_set_transmit_pin(uint8_t pin)
  202. {
  203. while (transmitting) ;
  204. pinMode(pin, OUTPUT);
  205. digitalWrite(pin, LOW);
  206. transmit_pin = portOutputRegister(pin);
  207. }
  208. void serial6_set_tx(uint8_t pin, uint8_t opendrain)
  209. {
  210. uint32_t cfg;
  211. if (opendrain) pin |= 128;
  212. if (pin == tx_pin_num) return;
  213. if ((SIM_SCGC4 & SIM_SCGC4_UART2)) {
  214. switch (tx_pin_num & 127) {
  215. case 48: CORE_PIN48_CONFIG = 0; break; // PTE24
  216. }
  217. if (opendrain) {
  218. cfg = PORT_PCR_DSE | PORT_PCR_ODE;
  219. } else {
  220. cfg = PORT_PCR_DSE | PORT_PCR_SRE;
  221. }
  222. switch (pin & 127) {
  223. case 48: CORE_PIN48_CONFIG = cfg | PORT_PCR_MUX(3); break;
  224. }
  225. }
  226. tx_pin_num = pin;
  227. }
  228. void serial6_set_rx(uint8_t pin)
  229. {
  230. }
  231. int serial6_set_rts(uint8_t pin)
  232. {
  233. if (!(SIM_SCGC2 & SIM_SCGC2_LPUART0)) return 0;
  234. if (pin < CORE_NUM_DIGITAL) {
  235. rts_pin = portOutputRegister(pin);
  236. pinMode(pin, OUTPUT);
  237. rts_assert();
  238. } else {
  239. rts_pin = NULL;
  240. return 0;
  241. }
  242. return 1;
  243. }
  244. int serial6_set_cts(uint8_t pin)
  245. {
  246. if (!(SIM_SCGC2 & SIM_SCGC2_LPUART0)) return 0;
  247. if (pin == 56) {
  248. CORE_PIN56_CONFIG = PORT_PCR_MUX(3) | PORT_PCR_PE; // weak pulldown
  249. } else {
  250. UART5_MODEM &= ~UART_MODEM_TXCTSE;
  251. return 0;
  252. }
  253. UART5_MODEM |= UART_MODEM_TXCTSE;
  254. return 1;
  255. }
  256. void serial6_putchar(uint32_t c)
  257. {
  258. uint32_t head, n;
  259. if (!(SIM_SCGC2 & SIM_SCGC2_LPUART0)) return;
  260. if (transmit_pin) transmit_assert();
  261. head = tx_buffer_head;
  262. if (++head >= TX_BUFFER_SIZE) head = 0;
  263. while (tx_buffer_tail == head) {
  264. int priority = nvic_execution_priority();
  265. if (priority <= IRQ_PRIORITY) {
  266. if ((LPUART0_STAT & LPUART_STAT_TDRE)) {
  267. uint32_t tail = tx_buffer_tail;
  268. if (++tail >= TX_BUFFER_SIZE) tail = 0;
  269. n = tx_buffer[tail];
  270. //if (use9Bits) UART5_C3 = (UART5_C3 & ~0x40) | ((n & 0x100) >> 2);
  271. LPUART0_DATA = n;
  272. tx_buffer_tail = tail;
  273. }
  274. } else if (priority >= 256) {
  275. yield(); // wait
  276. }
  277. }
  278. tx_buffer[head] = c;
  279. transmitting = 1;
  280. tx_buffer_head = head;
  281. //LPUART0_CTRL |= LPUART_CTRL_TIE; // enable the transmit interrupt
  282. BITBAND_SET_BIT(LPUART0_CTRL, TIE_BIT);
  283. }
  284. void serial6_write(const void *buf, unsigned int count)
  285. {
  286. const uint8_t *p = (const uint8_t *)buf;
  287. while (count-- > 0) serial6_putchar(*p++);
  288. }
  289. void serial6_flush(void)
  290. {
  291. while (transmitting) yield(); // wait
  292. }
  293. int serial6_write_buffer_free(void)
  294. {
  295. uint32_t head, tail;
  296. head = tx_buffer_head;
  297. tail = tx_buffer_tail;
  298. if (head >= tail) return TX_BUFFER_SIZE - 1 - head + tail;
  299. return tail - head - 1;
  300. }
  301. int serial6_available(void)
  302. {
  303. uint32_t head, tail;
  304. head = rx_buffer_head;
  305. tail = rx_buffer_tail;
  306. if (head >= tail) return head - tail;
  307. return RX_BUFFER_SIZE + head - tail;
  308. }
  309. int serial6_getchar(void)
  310. {
  311. uint32_t head, tail;
  312. int c;
  313. head = rx_buffer_head;
  314. tail = rx_buffer_tail;
  315. if (head == tail) return -1;
  316. if (++tail >= RX_BUFFER_SIZE) tail = 0;
  317. c = rx_buffer[tail];
  318. rx_buffer_tail = tail;
  319. if (rts_pin) {
  320. int avail;
  321. if (head >= tail) avail = head - tail;
  322. else avail = RX_BUFFER_SIZE + head - tail;
  323. if (avail <= RTS_LOW_WATERMARK) rts_assert();
  324. }
  325. return c;
  326. }
  327. int serial6_peek(void)
  328. {
  329. uint32_t head, tail;
  330. head = rx_buffer_head;
  331. tail = rx_buffer_tail;
  332. if (head == tail) return -1;
  333. if (++tail >= RX_BUFFER_SIZE) tail = 0;
  334. return rx_buffer[tail];
  335. }
  336. void serial6_clear(void)
  337. {
  338. rx_buffer_head = rx_buffer_tail;
  339. if (rts_pin) rts_assert();
  340. }
  341. // status interrupt combines
  342. // Transmit data below watermark LPUART_STAT_TDRE
  343. // Transmit complete LPUART_STAT_TC
  344. // Idle line LPUART_STAT_IDLE
  345. // Receive data above watermark LPUART_STAT_RDRF
  346. // LIN break detect UART_S2_LBKDIF
  347. // RxD pin active edge UART_S2_RXEDGIF
  348. void lpuart0_status_isr(void)
  349. {
  350. uint32_t head, tail, n;
  351. uint32_t c;
  352. if (LPUART0_STAT & LPUART_STAT_RDRF) {
  353. // if (use9Bits && (UART5_C3 & 0x80)) {
  354. // n = UART5_D | 0x100;
  355. // } else {
  356. // n = UART5_D;
  357. // }
  358. n = LPUART0_DATA & 0x3ff; // use only the 10 data bits
  359. head = rx_buffer_head + 1;
  360. if (head >= RX_BUFFER_SIZE) head = 0;
  361. if (head != rx_buffer_tail) {
  362. rx_buffer[head] = n;
  363. rx_buffer_head = head;
  364. }
  365. if (rts_pin) {
  366. int avail;
  367. tail = tx_buffer_tail;
  368. if (head >= tail) avail = head - tail;
  369. else avail = RX_BUFFER_SIZE + head - tail;
  370. if (avail >= RTS_HIGH_WATERMARK) rts_deassert();
  371. }
  372. }
  373. c = LPUART0_CTRL;
  374. if ((c & LPUART_CTRL_TIE) && (LPUART0_STAT & LPUART_STAT_TDRE)) {
  375. head = tx_buffer_head;
  376. tail = tx_buffer_tail;
  377. if (head == tail) {
  378. BITBAND_CLR_BIT(LPUART0_CTRL, TIE_BIT);
  379. BITBAND_SET_BIT(LPUART0_CTRL, TCIE_BIT);
  380. //LPUART0_CTRL &= ~LPUART_CTRL_TIE;
  381. //LPUART0_CTRL |= LPUART_CTRL_TCIE; // Actually wondering if we can just leave this one on...
  382. } else {
  383. if (++tail >= TX_BUFFER_SIZE) tail = 0;
  384. n = tx_buffer[tail];
  385. //if (use9Bits) UART5_C3 = (UART5_C3 & ~0x40) | ((n & 0x100) >> 2);
  386. LPUART0_DATA = n;
  387. tx_buffer_tail = tail;
  388. }
  389. }
  390. if ((c & LPUART_CTRL_TCIE) && (LPUART0_STAT & LPUART_STAT_TC)) {
  391. transmitting = 0;
  392. if (transmit_pin) transmit_deassert();
  393. BITBAND_CLR_BIT(LPUART0_CTRL, TCIE_BIT);
  394. // LPUART0_CTRL &= ~LPUART_CTRL_TCIE; // Actually wondering if we can just leave this one on...
  395. }
  396. }
  397. #endif // HAS_KINETISK_UART4