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

456 lines
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_UART5
  35. ////////////////////////////////////////////////////////////////
  36. // Tunable parameters (relatively safe to edit these numbers)
  37. ////////////////////////////////////////////////////////////////
  38. #ifndef SERIAL6_TX_BUFFER_SIZE
  39. #define SERIAL6_TX_BUFFER_SIZE 40 // number of outgoing bytes to buffer
  40. #endif
  41. #ifndef SERIAL6_RX_BUFFER_SIZE
  42. #define SERIAL6_RX_BUFFER_SIZE 64 // number of incoming bytes to buffer
  43. #endif
  44. #define RTS_HIGH_WATERMARK (SERIAL6_RX_BUFFER_SIZE-24) // RTS requests sender to pause
  45. #define RTS_LOW_WATERMARK (SERIAL6_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[SERIAL6_TX_BUFFER_SIZE];
  58. static volatile BUFTYPE rx_buffer[SERIAL6_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_ = SERIAL6_TX_BUFFER_SIZE;
  62. static size_t rx_buffer_total_size_ = SERIAL6_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 SERIAL6_TX_BUFFER_SIZE > 65535
  73. static volatile uint32_t tx_buffer_head = 0;
  74. static volatile uint32_t tx_buffer_tail = 0;
  75. #elif SERIAL6_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 SERIAL6_RX_BUFFER_SIZE > 65535
  83. static volatile uint32_t rx_buffer_head = 0;
  84. static volatile uint32_t rx_buffer_tail = 0;
  85. #elif SERIAL6_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 tx_pin_num = 48;
  93. // UART0 and UART1 are clocked by F_CPU, UART2 is clocked by F_BUS
  94. // UART0 has 8 byte fifo, UART1 and UART2 have 1 byte buffer
  95. #define C2_ENABLE UART_C2_TE | UART_C2_RE | UART_C2_RIE
  96. #define C2_TX_ACTIVE C2_ENABLE | UART_C2_TIE
  97. #define C2_TX_COMPLETING C2_ENABLE | UART_C2_TCIE
  98. #define C2_TX_INACTIVE C2_ENABLE
  99. // BITBAND Support
  100. #define GPIO_BITBAND_ADDR(reg, bit) (((uint32_t)&(reg) - 0x40000000) * 32 + (bit) * 4 + 0x42000000)
  101. #define GPIO_BITBAND_PTR(reg, bit) ((uint32_t *)GPIO_BITBAND_ADDR((reg), (bit)))
  102. #define C3_TXDIR_BIT 5
  103. void serial6_begin(uint32_t divisor)
  104. {
  105. SIM_SCGC1 |= SIM_SCGC1_UART5; // turn on clock, TODO: use bitband
  106. rx_buffer_head = 0;
  107. rx_buffer_tail = 0;
  108. tx_buffer_head = 0;
  109. tx_buffer_tail = 0;
  110. transmitting = 0;
  111. CORE_PIN47_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3);
  112. CORE_PIN48_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3);
  113. if (divisor < 32) divisor = 32;
  114. UART5_BDH = (divisor >> 13) & 0x1F;
  115. UART5_BDL = (divisor >> 5) & 0xFF;
  116. UART5_C4 = divisor & 0x1F;
  117. UART5_C1 = 0;
  118. UART5_PFIFO = 0;
  119. UART5_C2 = C2_TX_INACTIVE;
  120. NVIC_SET_PRIORITY(IRQ_UART5_STATUS, IRQ_PRIORITY);
  121. NVIC_ENABLE_IRQ(IRQ_UART5_STATUS);
  122. }
  123. void serial6_format(uint32_t format)
  124. {
  125. uint8_t c;
  126. c = UART5_C1;
  127. c = (c & ~0x13) | (format & 0x03); // configure parity
  128. if (format & 0x04) c |= 0x10; // 9 bits (might include parity)
  129. UART5_C1 = c;
  130. if ((format & 0x0F) == 0x04) UART5_C3 |= 0x40; // 8N2 is 9 bit with 9th bit always 1
  131. c = UART5_S2 & ~0x10;
  132. if (format & 0x10) c |= 0x10; // rx invert
  133. UART5_S2 = c;
  134. c = UART5_C3 & ~0x10;
  135. if (format & 0x20) c |= 0x10; // tx invert
  136. UART5_C3 = c;
  137. #ifdef SERIAL_9BIT_SUPPORT
  138. c = UART5_C4 & 0x1F;
  139. if (format & 0x08) c |= 0x20; // 9 bit mode with parity (requires 10 bits)
  140. UART5_C4 = c;
  141. use9Bits = format & 0x80;
  142. #endif
  143. // For T3.5 See about turning on 2 stop bit mode
  144. if ( format & 0x100) {
  145. uint8_t bdl = UART5_BDL;
  146. UART5_BDH |= UART_BDH_SBNS; // Turn on 2 stop bits - was turned off by set baud
  147. UART5_BDL = bdl; // Says BDH not acted on until BDL is written
  148. }
  149. // process request for half duplex.
  150. if ((format & SERIAL_HALF_DUPLEX) != 0) {
  151. UART5_C1 |= UART_C1_LOOPS | UART_C1_RSRC;
  152. volatile uint32_t *reg = portConfigRegister(tx_pin_num);
  153. *reg = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3) | PORT_PCR_PE | PORT_PCR_PS; // pullup on output pin;
  154. // Lets try to make use of bitband address to set the direction for ue...
  155. transmit_pin = (uint8_t*)GPIO_BITBAND_PTR(UART5_C3, C3_TXDIR_BIT);
  156. } else {
  157. if (transmit_pin == (uint8_t*)GPIO_BITBAND_PTR(UART5_C3, C3_TXDIR_BIT)) transmit_pin = NULL;
  158. }
  159. }
  160. void serial6_end(void)
  161. {
  162. if (!(SIM_SCGC1 & SIM_SCGC1_UART5)) return;
  163. while (transmitting) yield(); // wait for buffered data to send
  164. NVIC_DISABLE_IRQ(IRQ_UART5_STATUS);
  165. UART5_C2 = 0;
  166. CORE_PIN47_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1);
  167. CORE_PIN48_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1);
  168. UART5_S1;
  169. UART5_D; // clear leftover error status
  170. rx_buffer_head = 0;
  171. rx_buffer_tail = 0;
  172. if (rts_pin) rts_deassert();
  173. }
  174. void serial6_set_transmit_pin(uint8_t pin)
  175. {
  176. while (transmitting) ;
  177. pinMode(pin, OUTPUT);
  178. digitalWrite(pin, LOW);
  179. transmit_pin = portOutputRegister(pin);
  180. }
  181. void serial6_set_tx(uint8_t pin, uint8_t opendrain)
  182. {
  183. uint32_t cfg;
  184. if (opendrain) pin |= 128;
  185. if (pin == tx_pin_num) return;
  186. if ((SIM_SCGC1 & SIM_SCGC1_UART5)) {
  187. switch (tx_pin_num & 127) {
  188. case 48: CORE_PIN48_CONFIG = 0; break; // PTE24
  189. }
  190. if (opendrain) {
  191. cfg = PORT_PCR_DSE | PORT_PCR_ODE;
  192. } else {
  193. cfg = PORT_PCR_DSE | PORT_PCR_SRE;
  194. }
  195. switch (pin & 127) {
  196. case 48: CORE_PIN48_CONFIG = cfg | PORT_PCR_MUX(3); break;
  197. }
  198. }
  199. tx_pin_num = pin;
  200. }
  201. void serial6_set_rx(uint8_t pin)
  202. {
  203. }
  204. int serial6_set_rts(uint8_t pin)
  205. {
  206. if (!(SIM_SCGC1 & SIM_SCGC1_UART5)) return 0;
  207. if (pin < CORE_NUM_DIGITAL) {
  208. rts_pin = portOutputRegister(pin);
  209. pinMode(pin, OUTPUT);
  210. rts_assert();
  211. } else {
  212. rts_pin = NULL;
  213. return 0;
  214. }
  215. return 1;
  216. }
  217. int serial6_set_cts(uint8_t pin)
  218. {
  219. if (!(SIM_SCGC1 & SIM_SCGC1_UART5)) return 0;
  220. if (pin == 56) {
  221. CORE_PIN56_CONFIG = PORT_PCR_MUX(3) | PORT_PCR_PE; // weak pulldown
  222. } else {
  223. UART5_MODEM &= ~UART_MODEM_TXCTSE;
  224. return 0;
  225. }
  226. UART5_MODEM |= UART_MODEM_TXCTSE;
  227. return 1;
  228. }
  229. void serial6_putchar(uint32_t c)
  230. {
  231. uint32_t head, n;
  232. if (!(SIM_SCGC1 & SIM_SCGC1_UART5)) return;
  233. if (transmit_pin) transmit_assert();
  234. head = tx_buffer_head;
  235. if (++head >= tx_buffer_total_size_) head = 0;
  236. while (tx_buffer_tail == head) {
  237. int priority = nvic_execution_priority();
  238. if (priority <= IRQ_PRIORITY) {
  239. if ((UART5_S1 & UART_S1_TDRE)) {
  240. uint32_t tail = tx_buffer_tail;
  241. if (++tail >= tx_buffer_total_size_) tail = 0;
  242. if (tail < SERIAL6_TX_BUFFER_SIZE) {
  243. n = tx_buffer[tail];
  244. } else {
  245. n = tx_buffer_storage_[tail-SERIAL6_TX_BUFFER_SIZE];
  246. }
  247. if (use9Bits) UART5_C3 = (UART5_C3 & ~0x40) | ((n & 0x100) >> 2);
  248. UART5_D = n;
  249. tx_buffer_tail = tail;
  250. }
  251. } else if (priority >= 256) {
  252. yield(); // wait
  253. }
  254. }
  255. if (head < SERIAL6_TX_BUFFER_SIZE) {
  256. tx_buffer[head] = c;
  257. } else {
  258. tx_buffer_storage_[head - SERIAL6_TX_BUFFER_SIZE] = c;
  259. }
  260. transmitting = 1;
  261. tx_buffer_head = head;
  262. UART5_C2 = C2_TX_ACTIVE;
  263. }
  264. void serial6_write(const void *buf, unsigned int count)
  265. {
  266. const uint8_t *p = (const uint8_t *)buf;
  267. while (count-- > 0) serial6_putchar(*p++);
  268. }
  269. void serial6_flush(void)
  270. {
  271. while (transmitting) yield(); // wait
  272. }
  273. int serial6_write_buffer_free(void)
  274. {
  275. uint32_t head, tail;
  276. head = tx_buffer_head;
  277. tail = tx_buffer_tail;
  278. if (head >= tail) return tx_buffer_total_size_ - 1 - head + tail;
  279. return tail - head - 1;
  280. }
  281. int serial6_available(void)
  282. {
  283. uint32_t head, tail;
  284. head = rx_buffer_head;
  285. tail = rx_buffer_tail;
  286. if (head >= tail) return head - tail;
  287. return rx_buffer_total_size_ + head - tail;
  288. }
  289. int serial6_getchar(void)
  290. {
  291. uint32_t head, tail;
  292. int c;
  293. head = rx_buffer_head;
  294. tail = rx_buffer_tail;
  295. if (head == tail) return -1;
  296. if (++tail >= rx_buffer_total_size_) tail = 0;
  297. if (tail < SERIAL6_RX_BUFFER_SIZE) {
  298. c = rx_buffer[tail];
  299. } else {
  300. c = rx_buffer_storage_[tail-SERIAL6_RX_BUFFER_SIZE];
  301. }
  302. rx_buffer_tail = tail;
  303. if (rts_pin) {
  304. int avail;
  305. if (head >= tail) avail = head - tail;
  306. else avail = rx_buffer_total_size_ + head - tail;
  307. if (avail <= rts_low_watermark_) rts_assert();
  308. }
  309. return c;
  310. }
  311. int serial6_peek(void)
  312. {
  313. uint32_t head, tail;
  314. head = rx_buffer_head;
  315. tail = rx_buffer_tail;
  316. if (head == tail) return -1;
  317. if (++tail >= rx_buffer_total_size_) tail = 0;
  318. if (tail < SERIAL6_RX_BUFFER_SIZE) {
  319. return rx_buffer[tail];
  320. }
  321. return rx_buffer_storage_[tail-SERIAL6_RX_BUFFER_SIZE];
  322. }
  323. void serial6_clear(void)
  324. {
  325. rx_buffer_head = rx_buffer_tail;
  326. if (rts_pin) rts_assert();
  327. }
  328. // status interrupt combines
  329. // Transmit data below watermark UART_S1_TDRE
  330. // Transmit complete UART_S1_TC
  331. // Idle line UART_S1_IDLE
  332. // Receive data above watermark UART_S1_RDRF
  333. // LIN break detect UART_S2_LBKDIF
  334. // RxD pin active edge UART_S2_RXEDGIF
  335. void uart5_status_isr(void)
  336. {
  337. uint32_t head, tail, n;
  338. uint8_t c;
  339. if (UART5_S1 & UART_S1_RDRF) {
  340. if (use9Bits && (UART5_C3 & 0x80)) {
  341. n = UART5_D | 0x100;
  342. } else {
  343. n = UART5_D;
  344. }
  345. head = rx_buffer_head + 1;
  346. if (head >= rx_buffer_total_size_) head = 0;
  347. if (head != rx_buffer_tail) {
  348. if (head < SERIAL6_RX_BUFFER_SIZE) {
  349. rx_buffer[head] = n;
  350. } else {
  351. rx_buffer_storage_[head-SERIAL6_RX_BUFFER_SIZE] = n;
  352. }
  353. rx_buffer_head = head;
  354. }
  355. if (rts_pin) {
  356. int avail;
  357. tail = tx_buffer_tail;
  358. if (head >= tail) avail = head - tail;
  359. else avail = rx_buffer_total_size_ + head - tail;
  360. if (avail >= rts_high_watermark_) rts_deassert();
  361. }
  362. }
  363. c = UART5_C2;
  364. if ((c & UART_C2_TIE) && (UART5_S1 & UART_S1_TDRE)) {
  365. head = tx_buffer_head;
  366. tail = tx_buffer_tail;
  367. if (head == tail) {
  368. UART5_C2 = C2_TX_COMPLETING;
  369. } else {
  370. if (++tail >= tx_buffer_total_size_) tail = 0;
  371. if (tail < SERIAL6_TX_BUFFER_SIZE) {
  372. n = tx_buffer[tail];
  373. } else {
  374. n = tx_buffer_storage_[tail-SERIAL6_TX_BUFFER_SIZE];
  375. }
  376. if (use9Bits) UART5_C3 = (UART5_C3 & ~0x40) | ((n & 0x100) >> 2);
  377. UART5_D = n;
  378. tx_buffer_tail = tail;
  379. }
  380. }
  381. if ((c & UART_C2_TCIE) && (UART5_S1 & UART_S1_TC)) {
  382. transmitting = 0;
  383. if (transmit_pin) transmit_deassert();
  384. UART5_C2 = C2_TX_INACTIVE;
  385. }
  386. }
  387. void serial6_add_memory_for_read(void *buffer, size_t length)
  388. {
  389. rx_buffer_storage_ = (BUFTYPE*)buffer;
  390. if (buffer) {
  391. rx_buffer_total_size_ = SERIAL6_RX_BUFFER_SIZE + length;
  392. } else {
  393. rx_buffer_total_size_ = SERIAL6_RX_BUFFER_SIZE;
  394. }
  395. rts_low_watermark_ = RTS_LOW_WATERMARK + length;
  396. rts_high_watermark_ = RTS_HIGH_WATERMARK + length;
  397. }
  398. void serial6_add_memory_for_write(void *buffer, size_t length)
  399. {
  400. tx_buffer_storage_ = (BUFTYPE*)buffer;
  401. if (buffer) {
  402. tx_buffer_total_size_ = SERIAL6_TX_BUFFER_SIZE + length;
  403. } else {
  404. tx_buffer_total_size_ = SERIAL6_TX_BUFFER_SIZE;
  405. }
  406. }
  407. #endif // HAS_KINETISK_UART5