Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

il y a 7 ans
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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. // BITBAND Support
  101. #define GPIO_BITBAND_ADDR(reg, bit) (((uint32_t)&(reg) - 0x40000000) * 32 + (bit) * 4 + 0x42000000)
  102. #define GPIO_BITBAND_PTR(reg, bit) ((uint32_t *)GPIO_BITBAND_ADDR((reg), (bit)))
  103. #define C3_TXDIR_BIT 5
  104. void serial4_begin(uint32_t divisor)
  105. {
  106. SIM_SCGC4 |= SIM_SCGC4_UART3; // turn on clock, TODO: use bitband
  107. rx_buffer_head = 0;
  108. rx_buffer_tail = 0;
  109. tx_buffer_head = 0;
  110. tx_buffer_tail = 0;
  111. transmitting = 0;
  112. switch (rx_pin_num) {
  113. case 31: CORE_PIN31_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  114. case 63: CORE_PIN63_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  115. }
  116. switch (tx_pin_num) {
  117. case 32: CORE_PIN32_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3); break;
  118. case 62: CORE_PIN62_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3); break;
  119. }
  120. if (divisor < 32) divisor = 32;
  121. UART3_BDH = (divisor >> 13) & 0x1F;
  122. UART3_BDL = (divisor >> 5) & 0xFF;
  123. UART3_C4 = divisor & 0x1F;
  124. UART3_C1 = 0;
  125. UART3_PFIFO = 0;
  126. UART3_C2 = C2_TX_INACTIVE;
  127. NVIC_SET_PRIORITY(IRQ_UART3_STATUS, IRQ_PRIORITY);
  128. NVIC_ENABLE_IRQ(IRQ_UART3_STATUS);
  129. }
  130. void serial4_format(uint32_t format)
  131. {
  132. uint8_t c;
  133. c = UART3_C1;
  134. c = (c & ~0x13) | (format & 0x03); // configure parity
  135. if (format & 0x04) c |= 0x10; // 9 bits (might include parity)
  136. UART3_C1 = c;
  137. if ((format & 0x0F) == 0x04) UART3_C3 |= 0x40; // 8N2 is 9 bit with 9th bit always 1
  138. c = UART3_S2 & ~0x10;
  139. if (format & 0x10) c |= 0x10; // rx invert
  140. UART3_S2 = c;
  141. c = UART3_C3 & ~0x10;
  142. if (format & 0x20) c |= 0x10; // tx invert
  143. UART3_C3 = c;
  144. #ifdef SERIAL_9BIT_SUPPORT
  145. c = UART3_C4 & 0x1F;
  146. if (format & 0x08) c |= 0x20; // 9 bit mode with parity (requires 10 bits)
  147. UART3_C4 = c;
  148. use9Bits = format & 0x80;
  149. #endif
  150. #if defined(__MK64FX512__) || defined(__MK66FX1M0__) || defined(KINETISL)
  151. // For T3.5/T3.6/TLC See about turning on 2 stop bit mode
  152. if ( format & 0x100) {
  153. uint8_t bdl = UART3_BDL;
  154. UART3_BDH |= UART_BDH_SBNS; // Turn on 2 stop bits - was turned off by set baud
  155. UART3_BDL = bdl; // Says BDH not acted on until BDL is written
  156. }
  157. #endif
  158. // process request for half duplex.
  159. if ((format & SERIAL_HALF_DUPLEX) != 0) {
  160. UART3_C1 |= UART_C1_LOOPS | UART_C1_RSRC;
  161. volatile uint32_t *reg = portConfigRegister(tx_pin_num);
  162. *reg = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3) | PORT_PCR_PE | PORT_PCR_PS; // pullup on output pin;
  163. // Lets try to make use of bitband address to set the direction for ue...
  164. #if defined(KINETISL)
  165. transmit_pin = &UART3_C3;
  166. transmit_mask = UART_C3_TXDIR;
  167. #else
  168. transmit_pin = (uint8_t*)GPIO_BITBAND_PTR(UART3_C3, C3_TXDIR_BIT);
  169. #endif
  170. } else {
  171. #if defined(KINETISL)
  172. if (transmit_pin == &UART3_C3) transmit_pin = NULL;
  173. #else
  174. if (transmit_pin == (uint8_t*)GPIO_BITBAND_PTR(UART3_C3, C3_TXDIR_BIT)) transmit_pin = NULL;
  175. #endif
  176. }
  177. }
  178. void serial4_end(void)
  179. {
  180. if (!(SIM_SCGC4 & SIM_SCGC4_UART3)) return;
  181. while (transmitting) yield(); // wait for buffered data to send
  182. NVIC_DISABLE_IRQ(IRQ_UART3_STATUS);
  183. UART3_C2 = 0;
  184. switch (rx_pin_num) {
  185. case 31: CORE_PIN31_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); break; // PTC3
  186. case 63: CORE_PIN63_CONFIG = 0; break;
  187. }
  188. switch (tx_pin_num & 127) {
  189. case 32: CORE_PIN32_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); break; // PTC4
  190. case 62: CORE_PIN62_CONFIG = 0; break;
  191. }
  192. UART3_S1;
  193. UART3_D; // clear leftover error status
  194. rx_buffer_head = 0;
  195. rx_buffer_tail = 0;
  196. if (rts_pin) rts_deassert();
  197. }
  198. void serial4_set_transmit_pin(uint8_t pin)
  199. {
  200. while (transmitting) ;
  201. pinMode(pin, OUTPUT);
  202. digitalWrite(pin, LOW);
  203. transmit_pin = portOutputRegister(pin);
  204. }
  205. void serial4_set_tx(uint8_t pin, uint8_t opendrain)
  206. {
  207. uint32_t cfg;
  208. if (opendrain) pin |= 128;
  209. if (pin == tx_pin_num) return;
  210. if ((SIM_SCGC4 & SIM_SCGC4_UART3)) {
  211. switch (tx_pin_num & 127) {
  212. case 32: CORE_PIN32_CONFIG = 0; break; // PTB11
  213. case 62: CORE_PIN62_CONFIG = 0; break;
  214. }
  215. if (opendrain) {
  216. cfg = PORT_PCR_DSE | PORT_PCR_ODE;
  217. } else {
  218. cfg = PORT_PCR_DSE | PORT_PCR_SRE;
  219. }
  220. switch (pin & 127) {
  221. case 32: CORE_PIN32_CONFIG = cfg | PORT_PCR_MUX(3); break;
  222. case 62: CORE_PIN62_CONFIG = cfg | PORT_PCR_MUX(3); break;
  223. }
  224. }
  225. tx_pin_num = pin;
  226. }
  227. void serial4_set_rx(uint8_t pin)
  228. {
  229. if (pin == rx_pin_num) return;
  230. if ((SIM_SCGC4 & SIM_SCGC4_UART3)) {
  231. switch (rx_pin_num) {
  232. case 31: CORE_PIN31_CONFIG = 0; break; // PTC3
  233. case 63: CORE_PIN63_CONFIG = 0; break;
  234. }
  235. switch (pin) {
  236. case 31: CORE_PIN31_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  237. case 63: CORE_PIN63_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  238. }
  239. }
  240. rx_pin_num = pin;
  241. }
  242. int serial4_set_rts(uint8_t pin)
  243. {
  244. if (!(SIM_SCGC4 & SIM_SCGC4_UART3)) return 0;
  245. if (pin < CORE_NUM_DIGITAL) {
  246. rts_pin = portOutputRegister(pin);
  247. pinMode(pin, OUTPUT);
  248. rts_assert();
  249. } else {
  250. rts_pin = NULL;
  251. return 0;
  252. }
  253. return 1;
  254. }
  255. int serial4_set_cts(uint8_t pin)
  256. {
  257. return 0;
  258. }
  259. void serial4_putchar(uint32_t c)
  260. {
  261. uint32_t head, n;
  262. if (!(SIM_SCGC4 & SIM_SCGC4_UART3)) return;
  263. if (transmit_pin) transmit_assert();
  264. head = tx_buffer_head;
  265. if (++head >= tx_buffer_total_size_) head = 0;
  266. while (tx_buffer_tail == head) {
  267. int priority = nvic_execution_priority();
  268. if (priority <= IRQ_PRIORITY) {
  269. if ((UART3_S1 & UART_S1_TDRE)) {
  270. uint32_t tail = tx_buffer_tail;
  271. if (++tail >= tx_buffer_total_size_) tail = 0;
  272. if (tail < SERIAL4_TX_BUFFER_SIZE) {
  273. n = tx_buffer[tail];
  274. } else {
  275. n = tx_buffer_storage_[tail-SERIAL4_TX_BUFFER_SIZE];
  276. }
  277. if (use9Bits) UART3_C3 = (UART3_C3 & ~0x40) | ((n & 0x100) >> 2);
  278. UART3_D = n;
  279. tx_buffer_tail = tail;
  280. }
  281. } else if (priority >= 256) {
  282. yield(); // wait
  283. }
  284. }
  285. if (head < SERIAL4_TX_BUFFER_SIZE) {
  286. tx_buffer[head] = c;
  287. } else {
  288. tx_buffer_storage_[head - SERIAL4_TX_BUFFER_SIZE] = c;
  289. }
  290. transmitting = 1;
  291. tx_buffer_head = head;
  292. UART3_C2 = C2_TX_ACTIVE;
  293. }
  294. void serial4_write(const void *buf, unsigned int count)
  295. {
  296. const uint8_t *p = (const uint8_t *)buf;
  297. while (count-- > 0) serial4_putchar(*p++);
  298. }
  299. void serial4_flush(void)
  300. {
  301. while (transmitting) yield(); // wait
  302. }
  303. int serial4_write_buffer_free(void)
  304. {
  305. uint32_t head, tail;
  306. head = tx_buffer_head;
  307. tail = tx_buffer_tail;
  308. if (head >= tail) return tx_buffer_total_size_ - 1 - head + tail;
  309. return tail - head - 1;
  310. }
  311. int serial4_available(void)
  312. {
  313. uint32_t head, tail;
  314. head = rx_buffer_head;
  315. tail = rx_buffer_tail;
  316. if (head >= tail) return head - tail;
  317. return rx_buffer_total_size_ + head - tail;
  318. }
  319. int serial4_getchar(void)
  320. {
  321. uint32_t head, tail;
  322. int c;
  323. head = rx_buffer_head;
  324. tail = rx_buffer_tail;
  325. if (head == tail) return -1;
  326. if (++tail >= rx_buffer_total_size_) tail = 0;
  327. if (tail < SERIAL4_RX_BUFFER_SIZE) {
  328. c = rx_buffer[tail];
  329. } else {
  330. c = rx_buffer_storage_[tail-SERIAL4_RX_BUFFER_SIZE];
  331. }
  332. rx_buffer_tail = tail;
  333. if (rts_pin) {
  334. int avail;
  335. if (head >= tail) avail = head - tail;
  336. else avail = rx_buffer_total_size_ + head - tail;
  337. if (avail <= rts_low_watermark_) rts_assert();
  338. }
  339. return c;
  340. }
  341. int serial4_peek(void)
  342. {
  343. uint32_t head, tail;
  344. head = rx_buffer_head;
  345. tail = rx_buffer_tail;
  346. if (head == tail) return -1;
  347. if (++tail >= rx_buffer_total_size_) tail = 0;
  348. if (tail < SERIAL4_RX_BUFFER_SIZE) {
  349. return rx_buffer[tail];
  350. }
  351. return rx_buffer_storage_[tail-SERIAL4_RX_BUFFER_SIZE];
  352. }
  353. void serial4_clear(void)
  354. {
  355. rx_buffer_head = rx_buffer_tail;
  356. if (rts_pin) rts_assert();
  357. }
  358. // status interrupt combines
  359. // Transmit data below watermark UART_S1_TDRE
  360. // Transmit complete UART_S1_TC
  361. // Idle line UART_S1_IDLE
  362. // Receive data above watermark UART_S1_RDRF
  363. // LIN break detect UART_S2_LBKDIF
  364. // RxD pin active edge UART_S2_RXEDGIF
  365. void uart3_status_isr(void)
  366. {
  367. uint32_t head, tail, n;
  368. uint8_t c;
  369. if (UART3_S1 & UART_S1_RDRF) {
  370. if (use9Bits && (UART3_C3 & 0x80)) {
  371. n = UART3_D | 0x100;
  372. } else {
  373. n = UART3_D;
  374. }
  375. head = rx_buffer_head + 1;
  376. if (head >= rx_buffer_total_size_) head = 0;
  377. if (head != rx_buffer_tail) {
  378. if (head < SERIAL4_RX_BUFFER_SIZE) {
  379. rx_buffer[head] = n;
  380. } else {
  381. rx_buffer_storage_[head-SERIAL4_RX_BUFFER_SIZE] = n;
  382. }
  383. rx_buffer_head = head;
  384. }
  385. if (rts_pin) {
  386. int avail;
  387. tail = tx_buffer_tail;
  388. if (head >= tail) avail = head - tail;
  389. else avail = rx_buffer_total_size_ + head - tail;
  390. if (avail >= rts_high_watermark_) rts_deassert();
  391. }
  392. }
  393. c = UART3_C2;
  394. if ((c & UART_C2_TIE) && (UART3_S1 & UART_S1_TDRE)) {
  395. head = tx_buffer_head;
  396. tail = tx_buffer_tail;
  397. if (head == tail) {
  398. UART3_C2 = C2_TX_COMPLETING;
  399. } else {
  400. if (++tail >= tx_buffer_total_size_) tail = 0;
  401. if (tail < SERIAL4_TX_BUFFER_SIZE) {
  402. n = tx_buffer[tail];
  403. } else {
  404. n = tx_buffer_storage_[tail-SERIAL4_TX_BUFFER_SIZE];
  405. }
  406. if (use9Bits) UART3_C3 = (UART3_C3 & ~0x40) | ((n & 0x100) >> 2);
  407. UART3_D = n;
  408. tx_buffer_tail = tail;
  409. }
  410. }
  411. if ((c & UART_C2_TCIE) && (UART3_S1 & UART_S1_TC)) {
  412. transmitting = 0;
  413. if (transmit_pin) transmit_deassert();
  414. UART3_C2 = C2_TX_INACTIVE;
  415. }
  416. }
  417. void serial4_add_memory_for_read(void *buffer, size_t length)
  418. {
  419. rx_buffer_storage_ = (BUFTYPE*)buffer;
  420. if (buffer) {
  421. rx_buffer_total_size_ = SERIAL4_RX_BUFFER_SIZE + length;
  422. } else {
  423. rx_buffer_total_size_ = SERIAL4_RX_BUFFER_SIZE;
  424. }
  425. rts_low_watermark_ = RTS_LOW_WATERMARK + length;
  426. rts_high_watermark_ = RTS_HIGH_WATERMARK + length;
  427. }
  428. void serial4_add_memory_for_write(void *buffer, size_t length)
  429. {
  430. tx_buffer_storage_ = (BUFTYPE*)buffer;
  431. if (buffer) {
  432. tx_buffer_total_size_ = SERIAL4_TX_BUFFER_SIZE + length;
  433. } else {
  434. tx_buffer_total_size_ = SERIAL4_TX_BUFFER_SIZE;
  435. }
  436. }
  437. #endif // HAS_KINETISK_UART3