Teensy 4.1 core updated for C++20
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

460 Zeilen
13KB

  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. #include <stddef.h>
  34. #ifdef HAS_KINETISK_UART3
  35. ////////////////////////////////////////////////////////////////
  36. // Tunable parameters (relatively safe to edit these numbers)
  37. ////////////////////////////////////////////////////////////////
  38. #ifndef SERIAL4_TX_BUFFER_SIZE
  39. #define SERIAL4_TX_BUFFER_SIZE 40 // number of outgoing bytes to buffer
  40. #endif
  41. #ifndef SERIAL4_RX_BUFFER_SIZE
  42. #define SERIAL4_RX_BUFFER_SIZE 64 // number of incoming bytes to buffer
  43. #endif
  44. #define RTS_HIGH_WATERMARK (SERIAL4_RX_BUFFER_SIZE-24) // RTS requests sender to pause
  45. #define RTS_LOW_WATERMARK (SERIAL4_RX_BUFFER_SIZE-38) // RTS allows sender to resume
  46. #define IRQ_PRIORITY 64 // 0 = highest priority, 255 = lowest
  47. ////////////////////////////////////////////////////////////////
  48. // changes not recommended below this point....
  49. ////////////////////////////////////////////////////////////////
  50. #ifdef SERIAL_9BIT_SUPPORT
  51. static uint8_t use9Bits = 0;
  52. #define BUFTYPE uint16_t
  53. #else
  54. #define BUFTYPE uint8_t
  55. #define use9Bits 0
  56. #endif
  57. static volatile BUFTYPE tx_buffer[SERIAL4_TX_BUFFER_SIZE];
  58. static volatile BUFTYPE rx_buffer[SERIAL4_RX_BUFFER_SIZE];
  59. static volatile BUFTYPE *rx_buffer_storage_ = NULL;
  60. static volatile BUFTYPE *tx_buffer_storage_ = NULL;
  61. static size_t tx_buffer_total_size_ = SERIAL4_TX_BUFFER_SIZE;
  62. static size_t rx_buffer_total_size_ = SERIAL4_RX_BUFFER_SIZE;
  63. static size_t rts_low_watermark_ = RTS_LOW_WATERMARK;
  64. static size_t rts_high_watermark_ = RTS_HIGH_WATERMARK;
  65. static volatile uint8_t transmitting = 0;
  66. static volatile uint8_t *transmit_pin=NULL;
  67. #define transmit_assert() *transmit_pin = 1
  68. #define transmit_deassert() *transmit_pin = 0
  69. static volatile uint8_t *rts_pin=NULL;
  70. #define rts_assert() *rts_pin = 0
  71. #define rts_deassert() *rts_pin = 1
  72. #if SERIAL4_TX_BUFFER_SIZE > 65535
  73. static volatile uint32_t tx_buffer_head = 0;
  74. static volatile uint32_t tx_buffer_tail = 0;
  75. #elif SERIAL4_TX_BUFFER_SIZE > 255
  76. static volatile uint16_t tx_buffer_head = 0;
  77. static volatile uint16_t tx_buffer_tail = 0;
  78. #else
  79. static volatile uint8_t tx_buffer_head = 0;
  80. static volatile uint8_t tx_buffer_tail = 0;
  81. #endif
  82. #if SERIAL4_RX_BUFFER_SIZE > 65535
  83. static volatile uint32_t rx_buffer_head = 0;
  84. static volatile uint32_t rx_buffer_tail = 0;
  85. #elif SERIAL4_RX_BUFFER_SIZE > 255
  86. static volatile uint16_t rx_buffer_head = 0;
  87. static volatile uint16_t rx_buffer_tail = 0;
  88. #else
  89. static volatile uint8_t rx_buffer_head = 0;
  90. static volatile uint8_t rx_buffer_tail = 0;
  91. #endif
  92. static uint8_t rx_pin_num = 31;
  93. static uint8_t tx_pin_num = 32;
  94. // UART0 and UART1 are clocked by F_CPU, UART2 is clocked by F_BUS
  95. // UART0 has 8 byte fifo, UART1 and UART2 have 1 byte buffer
  96. #define C2_ENABLE UART_C2_TE | UART_C2_RE | UART_C2_RIE
  97. #define C2_TX_ACTIVE C2_ENABLE | UART_C2_TIE
  98. #define C2_TX_COMPLETING C2_ENABLE | UART_C2_TCIE
  99. #define C2_TX_INACTIVE C2_ENABLE
  100. void serial4_begin(uint32_t divisor)
  101. {
  102. SIM_SCGC4 |= SIM_SCGC4_UART3; // turn on clock, TODO: use bitband
  103. rx_buffer_head = 0;
  104. rx_buffer_tail = 0;
  105. tx_buffer_head = 0;
  106. tx_buffer_tail = 0;
  107. transmitting = 0;
  108. switch (rx_pin_num) {
  109. case 31: CORE_PIN31_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  110. case 63: CORE_PIN63_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  111. }
  112. switch (tx_pin_num) {
  113. case 32: CORE_PIN32_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3); break;
  114. case 62: CORE_PIN62_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3); break;
  115. }
  116. if (divisor < 32) divisor = 32;
  117. UART3_BDH = (divisor >> 13) & 0x1F;
  118. UART3_BDL = (divisor >> 5) & 0xFF;
  119. UART3_C4 = divisor & 0x1F;
  120. UART3_C1 = 0;
  121. UART3_PFIFO = 0;
  122. UART3_C2 = C2_TX_INACTIVE;
  123. NVIC_SET_PRIORITY(IRQ_UART3_STATUS, IRQ_PRIORITY);
  124. NVIC_ENABLE_IRQ(IRQ_UART3_STATUS);
  125. }
  126. void serial4_format(uint32_t format)
  127. {
  128. uint8_t c;
  129. c = UART3_C1;
  130. c = (c & ~0x13) | (format & 0x03); // configure parity
  131. if (format & 0x04) c |= 0x10; // 9 bits (might include parity)
  132. UART3_C1 = c;
  133. if ((format & 0x0F) == 0x04) UART3_C3 |= 0x40; // 8N2 is 9 bit with 9th bit always 1
  134. c = UART3_S2 & ~0x10;
  135. if (format & 0x10) c |= 0x10; // rx invert
  136. UART3_S2 = c;
  137. c = UART3_C3 & ~0x10;
  138. if (format & 0x20) c |= 0x10; // tx invert
  139. UART3_C3 = c;
  140. #ifdef SERIAL_9BIT_SUPPORT
  141. c = UART3_C4 & 0x1F;
  142. if (format & 0x08) c |= 0x20; // 9 bit mode with parity (requires 10 bits)
  143. UART3_C4 = c;
  144. use9Bits = format & 0x80;
  145. #endif
  146. #if defined(__MK64FX512__) || defined(__MK66FX1M0__) || defined(KINETISL)
  147. // For T3.5/T3.6/TLC See about turning on 2 stop bit mode
  148. if ( format & 0x100) {
  149. uint8_t bdl = UART3_BDL;
  150. UART3_BDH |= UART_BDH_SBNS; // Turn on 2 stop bits - was turned off by set baud
  151. UART3_BDL = bdl; // Says BDH not acted on until BDL is written
  152. }
  153. #endif
  154. }
  155. void serial4_end(void)
  156. {
  157. if (!(SIM_SCGC4 & SIM_SCGC4_UART3)) return;
  158. while (transmitting) yield(); // wait for buffered data to send
  159. NVIC_DISABLE_IRQ(IRQ_UART3_STATUS);
  160. UART3_C2 = 0;
  161. switch (rx_pin_num) {
  162. case 31: CORE_PIN31_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); break; // PTC3
  163. case 63: CORE_PIN63_CONFIG = 0; break;
  164. }
  165. switch (tx_pin_num & 127) {
  166. case 32: CORE_PIN32_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); break; // PTC4
  167. case 62: CORE_PIN62_CONFIG = 0; break;
  168. }
  169. UART3_S1;
  170. UART3_D; // clear leftover error status
  171. rx_buffer_head = 0;
  172. rx_buffer_tail = 0;
  173. if (rts_pin) rts_deassert();
  174. }
  175. void serial4_set_transmit_pin(uint8_t pin)
  176. {
  177. while (transmitting) ;
  178. pinMode(pin, OUTPUT);
  179. digitalWrite(pin, LOW);
  180. transmit_pin = portOutputRegister(pin);
  181. }
  182. void serial4_set_tx(uint8_t pin, uint8_t opendrain)
  183. {
  184. uint32_t cfg;
  185. if (opendrain) pin |= 128;
  186. if (pin == tx_pin_num) return;
  187. if ((SIM_SCGC4 & SIM_SCGC4_UART3)) {
  188. switch (tx_pin_num & 127) {
  189. case 32: CORE_PIN32_CONFIG = 0; break; // PTB11
  190. case 62: CORE_PIN62_CONFIG = 0; break;
  191. }
  192. if (opendrain) {
  193. cfg = PORT_PCR_DSE | PORT_PCR_ODE;
  194. } else {
  195. cfg = PORT_PCR_DSE | PORT_PCR_SRE;
  196. }
  197. switch (pin & 127) {
  198. case 32: CORE_PIN32_CONFIG = cfg | PORT_PCR_MUX(3); break;
  199. case 62: CORE_PIN62_CONFIG = cfg | PORT_PCR_MUX(3); break;
  200. }
  201. }
  202. tx_pin_num = pin;
  203. }
  204. void serial4_set_rx(uint8_t pin)
  205. {
  206. if (pin == rx_pin_num) return;
  207. if ((SIM_SCGC4 & SIM_SCGC4_UART3)) {
  208. switch (rx_pin_num) {
  209. case 31: CORE_PIN31_CONFIG = 0; break; // PTC3
  210. case 63: CORE_PIN63_CONFIG = 0; break;
  211. }
  212. switch (pin) {
  213. case 31: CORE_PIN31_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  214. case 63: CORE_PIN63_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  215. }
  216. }
  217. rx_pin_num = pin;
  218. }
  219. int serial4_set_rts(uint8_t pin)
  220. {
  221. if (!(SIM_SCGC4 & SIM_SCGC4_UART3)) return 0;
  222. if (pin < CORE_NUM_DIGITAL) {
  223. rts_pin = portOutputRegister(pin);
  224. pinMode(pin, OUTPUT);
  225. rts_assert();
  226. } else {
  227. rts_pin = NULL;
  228. return 0;
  229. }
  230. return 1;
  231. }
  232. int serial4_set_cts(uint8_t pin)
  233. {
  234. return 0;
  235. }
  236. void serial4_putchar(uint32_t c)
  237. {
  238. uint32_t head, n;
  239. if (!(SIM_SCGC4 & SIM_SCGC4_UART3)) return;
  240. if (transmit_pin) transmit_assert();
  241. head = tx_buffer_head;
  242. if (++head >= tx_buffer_total_size_) head = 0;
  243. while (tx_buffer_tail == head) {
  244. int priority = nvic_execution_priority();
  245. if (priority <= IRQ_PRIORITY) {
  246. if ((UART3_S1 & UART_S1_TDRE)) {
  247. uint32_t tail = tx_buffer_tail;
  248. if (++tail >= tx_buffer_total_size_) tail = 0;
  249. if (tail < SERIAL4_TX_BUFFER_SIZE) {
  250. n = tx_buffer[tail];
  251. } else {
  252. n = tx_buffer_storage_[tail-SERIAL4_TX_BUFFER_SIZE];
  253. }
  254. if (use9Bits) UART3_C3 = (UART3_C3 & ~0x40) | ((n & 0x100) >> 2);
  255. UART3_D = n;
  256. tx_buffer_tail = tail;
  257. }
  258. } else if (priority >= 256) {
  259. yield(); // wait
  260. }
  261. }
  262. if (head < SERIAL4_TX_BUFFER_SIZE) {
  263. tx_buffer[head] = c;
  264. } else {
  265. tx_buffer_storage_[head - SERIAL4_TX_BUFFER_SIZE] = c;
  266. }
  267. transmitting = 1;
  268. tx_buffer_head = head;
  269. UART3_C2 = C2_TX_ACTIVE;
  270. }
  271. void serial4_write(const void *buf, unsigned int count)
  272. {
  273. const uint8_t *p = (const uint8_t *)buf;
  274. while (count-- > 0) serial4_putchar(*p++);
  275. }
  276. void serial4_flush(void)
  277. {
  278. while (transmitting) yield(); // wait
  279. }
  280. int serial4_write_buffer_free(void)
  281. {
  282. uint32_t head, tail;
  283. head = tx_buffer_head;
  284. tail = tx_buffer_tail;
  285. if (head >= tail) return tx_buffer_total_size_ - 1 - head + tail;
  286. return tail - head - 1;
  287. }
  288. int serial4_available(void)
  289. {
  290. uint32_t head, tail;
  291. head = rx_buffer_head;
  292. tail = rx_buffer_tail;
  293. if (head >= tail) return head - tail;
  294. return rx_buffer_total_size_ + head - tail;
  295. }
  296. int serial4_getchar(void)
  297. {
  298. uint32_t head, tail;
  299. int c;
  300. head = rx_buffer_head;
  301. tail = rx_buffer_tail;
  302. if (head == tail) return -1;
  303. if (++tail >= rx_buffer_total_size_) tail = 0;
  304. if (tail < SERIAL4_RX_BUFFER_SIZE) {
  305. c = rx_buffer[tail];
  306. } else {
  307. c = rx_buffer_storage_[tail-SERIAL4_RX_BUFFER_SIZE];
  308. }
  309. rx_buffer_tail = tail;
  310. if (rts_pin) {
  311. int avail;
  312. if (head >= tail) avail = head - tail;
  313. else avail = rx_buffer_total_size_ + head - tail;
  314. if (avail <= rts_low_watermark_) rts_assert();
  315. }
  316. return c;
  317. }
  318. int serial4_peek(void)
  319. {
  320. uint32_t head, tail;
  321. head = rx_buffer_head;
  322. tail = rx_buffer_tail;
  323. if (head == tail) return -1;
  324. if (++tail >= rx_buffer_total_size_) tail = 0;
  325. if (tail < SERIAL4_RX_BUFFER_SIZE) {
  326. return rx_buffer[tail];
  327. }
  328. return rx_buffer_storage_[tail-SERIAL4_RX_BUFFER_SIZE];
  329. }
  330. void serial4_clear(void)
  331. {
  332. rx_buffer_head = rx_buffer_tail;
  333. if (rts_pin) rts_assert();
  334. }
  335. // status interrupt combines
  336. // Transmit data below watermark UART_S1_TDRE
  337. // Transmit complete UART_S1_TC
  338. // Idle line UART_S1_IDLE
  339. // Receive data above watermark UART_S1_RDRF
  340. // LIN break detect UART_S2_LBKDIF
  341. // RxD pin active edge UART_S2_RXEDGIF
  342. void uart3_status_isr(void)
  343. {
  344. uint32_t head, tail, n;
  345. uint8_t c;
  346. if (UART3_S1 & UART_S1_RDRF) {
  347. if (use9Bits && (UART3_C3 & 0x80)) {
  348. n = UART3_D | 0x100;
  349. } else {
  350. n = UART3_D;
  351. }
  352. head = rx_buffer_head + 1;
  353. if (head >= rx_buffer_total_size_) head = 0;
  354. if (head != rx_buffer_tail) {
  355. if (head < SERIAL4_RX_BUFFER_SIZE) {
  356. rx_buffer[head] = n;
  357. } else {
  358. rx_buffer_storage_[head-SERIAL4_RX_BUFFER_SIZE] = n;
  359. }
  360. rx_buffer_head = head;
  361. }
  362. if (rts_pin) {
  363. int avail;
  364. tail = tx_buffer_tail;
  365. if (head >= tail) avail = head - tail;
  366. else avail = rx_buffer_total_size_ + head - tail;
  367. if (avail >= rts_high_watermark_) rts_deassert();
  368. }
  369. }
  370. c = UART3_C2;
  371. if ((c & UART_C2_TIE) && (UART3_S1 & UART_S1_TDRE)) {
  372. head = tx_buffer_head;
  373. tail = tx_buffer_tail;
  374. if (head == tail) {
  375. UART3_C2 = C2_TX_COMPLETING;
  376. } else {
  377. if (++tail >= tx_buffer_total_size_) tail = 0;
  378. if (tail < SERIAL4_TX_BUFFER_SIZE) {
  379. n = tx_buffer[tail];
  380. } else {
  381. n = tx_buffer_storage_[tail-SERIAL4_TX_BUFFER_SIZE];
  382. }
  383. if (use9Bits) UART3_C3 = (UART3_C3 & ~0x40) | ((n & 0x100) >> 2);
  384. UART3_D = n;
  385. tx_buffer_tail = tail;
  386. }
  387. }
  388. if ((c & UART_C2_TCIE) && (UART3_S1 & UART_S1_TC)) {
  389. transmitting = 0;
  390. if (transmit_pin) transmit_deassert();
  391. UART3_C2 = C2_TX_INACTIVE;
  392. }
  393. }
  394. void serial4_add_memory_for_read(void *buffer, size_t length)
  395. {
  396. rx_buffer_storage_ = (BUFTYPE*)buffer;
  397. if (buffer) {
  398. rx_buffer_total_size_ = SERIAL4_RX_BUFFER_SIZE + length;
  399. } else {
  400. rx_buffer_total_size_ = SERIAL4_RX_BUFFER_SIZE;
  401. }
  402. rts_low_watermark_ = RTS_LOW_WATERMARK + length;
  403. rts_high_watermark_ = RTS_HIGH_WATERMARK + length;
  404. }
  405. void serial4_add_memory_for_write(void *buffer, size_t length)
  406. {
  407. tx_buffer_storage_ = (BUFTYPE*)buffer;
  408. if (buffer) {
  409. tx_buffer_total_size_ = SERIAL4_TX_BUFFER_SIZE + length;
  410. } else {
  411. tx_buffer_total_size_ = SERIAL4_TX_BUFFER_SIZE;
  412. }
  413. }
  414. #endif // HAS_KINETISK_UART3