Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. ////////////////////////////////////////////////////////////////
  34. // Tunable parameters (relatively safe to edit these numbers)
  35. ////////////////////////////////////////////////////////////////
  36. #define TX_BUFFER_SIZE 40 // number of outgoing bytes to buffer
  37. #define RX_BUFFER_SIZE 64 // number of incoming bytes to buffer
  38. #define RTS_HIGH_WATERMARK 40 // RTS requests sender to pause
  39. #define RTS_LOW_WATERMARK 26 // RTS allows sender to resume
  40. #define IRQ_PRIORITY 64 // 0 = highest priority, 255 = lowest
  41. ////////////////////////////////////////////////////////////////
  42. // changes not recommended below this point....
  43. ////////////////////////////////////////////////////////////////
  44. #ifdef SERIAL_9BIT_SUPPORT
  45. static uint8_t use9Bits = 0;
  46. #define BUFTYPE uint16_t
  47. #else
  48. #define BUFTYPE uint8_t
  49. #define use9Bits 0
  50. #endif
  51. static volatile BUFTYPE tx_buffer[TX_BUFFER_SIZE];
  52. static volatile BUFTYPE rx_buffer[RX_BUFFER_SIZE];
  53. static volatile uint8_t transmitting = 0;
  54. #if defined(KINETISK)
  55. static volatile uint8_t *transmit_pin=NULL;
  56. #define transmit_assert() *transmit_pin = 1
  57. #define transmit_deassert() *transmit_pin = 0
  58. static volatile uint8_t *rts_pin=NULL;
  59. #define rts_assert() *rts_pin = 0
  60. #define rts_deassert() *rts_pin = 1
  61. #elif defined(KINETISL)
  62. static volatile uint8_t *transmit_pin=NULL;
  63. static uint8_t transmit_mask=0;
  64. #define transmit_assert() *(transmit_pin+4) = transmit_mask;
  65. #define transmit_deassert() *(transmit_pin+8) = transmit_mask;
  66. static volatile uint8_t *rts_pin=NULL;
  67. static uint8_t rts_mask=0;
  68. #define rts_assert() *(rts_pin+8) = rts_mask;
  69. #define rts_deassert() *(rts_pin+4) = rts_mask;
  70. #endif
  71. #if TX_BUFFER_SIZE > 255
  72. static volatile uint16_t tx_buffer_head = 0;
  73. static volatile uint16_t tx_buffer_tail = 0;
  74. #else
  75. static volatile uint8_t tx_buffer_head = 0;
  76. static volatile uint8_t tx_buffer_tail = 0;
  77. #endif
  78. #if RX_BUFFER_SIZE > 255
  79. static volatile uint16_t rx_buffer_head = 0;
  80. static volatile uint16_t rx_buffer_tail = 0;
  81. #else
  82. static volatile uint8_t rx_buffer_head = 0;
  83. static volatile uint8_t rx_buffer_tail = 0;
  84. #endif
  85. #if defined(KINETISL)
  86. static uint8_t rx_pin_num = 7;
  87. static uint8_t tx_pin_num = 8;
  88. #endif
  89. // UART0 and UART1 are clocked by F_CPU, UART2 is clocked by F_BUS
  90. // UART0 has 8 byte fifo, UART1 and UART2 have 1 byte buffer
  91. #define C2_ENABLE UART_C2_TE | UART_C2_RE | UART_C2_RIE
  92. #define C2_TX_ACTIVE C2_ENABLE | UART_C2_TIE
  93. #define C2_TX_COMPLETING C2_ENABLE | UART_C2_TCIE
  94. #define C2_TX_INACTIVE C2_ENABLE
  95. void serial3_begin(uint32_t divisor)
  96. {
  97. SIM_SCGC4 |= SIM_SCGC4_UART2; // turn on clock, TODO: use bitband
  98. rx_buffer_head = 0;
  99. rx_buffer_tail = 0;
  100. tx_buffer_head = 0;
  101. tx_buffer_tail = 0;
  102. transmitting = 0;
  103. #if defined(KINETISK)
  104. CORE_PIN7_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3);
  105. CORE_PIN8_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3);
  106. #elif defined(KINETISL)
  107. switch (rx_pin_num) {
  108. case 7: CORE_PIN7_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  109. case 6: CORE_PIN6_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  110. }
  111. switch (tx_pin_num) {
  112. case 8: CORE_PIN8_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3); break;
  113. case 20: CORE_PIN20_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3); break;
  114. }
  115. #endif
  116. #if defined(HAS_KINETISK_UART2)
  117. UART2_BDH = (divisor >> 13) & 0x1F;
  118. UART2_BDL = (divisor >> 5) & 0xFF;
  119. UART2_C4 = divisor & 0x1F;
  120. UART2_C1 = 0;
  121. UART2_PFIFO = 0;
  122. #elif defined(HAS_KINETISL_UART2)
  123. UART2_BDH = (divisor >> 8) & 0x1F;
  124. UART2_BDL = divisor & 0xFF;
  125. UART2_C1 = 0;
  126. #endif
  127. UART2_C2 = C2_TX_INACTIVE;
  128. NVIC_SET_PRIORITY(IRQ_UART2_STATUS, IRQ_PRIORITY);
  129. NVIC_ENABLE_IRQ(IRQ_UART2_STATUS);
  130. }
  131. void serial3_format(uint32_t format)
  132. {
  133. uint8_t c;
  134. c = UART2_C1;
  135. c = (c & ~0x13) | (format & 0x03); // configure parity
  136. if (format & 0x04) c |= 0x10; // 9 bits (might include parity)
  137. UART2_C1 = c;
  138. if ((format & 0x0F) == 0x04) UART2_C3 |= 0x40; // 8N2 is 9 bit with 9th bit always 1
  139. c = UART2_S2 & ~0x10;
  140. if (format & 0x10) c |= 0x10; // rx invert
  141. UART2_S2 = c;
  142. c = UART2_C3 & ~0x10;
  143. if (format & 0x20) c |= 0x10; // tx invert
  144. UART2_C3 = c;
  145. #ifdef SERIAL_9BIT_SUPPORT
  146. c = UART2_C4 & 0x1F;
  147. if (format & 0x08) c |= 0x20; // 9 bit mode with parity (requires 10 bits)
  148. UART2_C4 = c;
  149. use9Bits = format & 0x80;
  150. #endif
  151. }
  152. void serial3_end(void)
  153. {
  154. if (!(SIM_SCGC4 & SIM_SCGC4_UART2)) return;
  155. while (transmitting) yield(); // wait for buffered data to send
  156. NVIC_DISABLE_IRQ(IRQ_UART2_STATUS);
  157. UART2_C2 = 0;
  158. CORE_PIN7_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1);
  159. CORE_PIN8_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1);
  160. rx_buffer_head = 0;
  161. rx_buffer_tail = 0;
  162. if (rts_pin) rts_deassert();
  163. }
  164. void serial3_set_transmit_pin(uint8_t pin)
  165. {
  166. while (transmitting) ;
  167. pinMode(pin, OUTPUT);
  168. digitalWrite(pin, LOW);
  169. transmit_pin = portOutputRegister(pin);
  170. #if defined(KINETISL)
  171. transmit_mask = digitalPinToBitMask(pin);
  172. #endif
  173. }
  174. void serial3_set_tx(uint8_t pin, uint8_t opendrain)
  175. {
  176. #if defined(KINETISL)
  177. uint32_t cfg;
  178. if (opendrain) pin |= 128;
  179. if (pin == tx_pin_num) return;
  180. if ((SIM_SCGC4 & SIM_SCGC4_UART2)) {
  181. switch (tx_pin_num & 127) {
  182. case 8: CORE_PIN8_CONFIG = 0; break; // PTD3
  183. case 20: CORE_PIN20_CONFIG = 0; break; // PTD5
  184. }
  185. if (opendrain) {
  186. cfg = PORT_PCR_DSE | PORT_PCR_ODE;
  187. } else {
  188. cfg = PORT_PCR_DSE | PORT_PCR_SRE;
  189. }
  190. switch (pin & 127) {
  191. case 8: CORE_PIN8_CONFIG = cfg | PORT_PCR_MUX(3); break;
  192. case 20: CORE_PIN20_CONFIG = cfg | PORT_PCR_MUX(3); break;
  193. }
  194. }
  195. tx_pin_num = pin;
  196. #endif
  197. }
  198. void serial3_set_rx(uint8_t pin)
  199. {
  200. #if defined(KINETISL)
  201. if (pin == rx_pin_num) return;
  202. if ((SIM_SCGC4 & SIM_SCGC4_UART2)) {
  203. switch (rx_pin_num) {
  204. case 7: CORE_PIN7_CONFIG = 0; break; // PTD2
  205. case 6: CORE_PIN6_CONFIG = 0; break; // PTD4
  206. }
  207. switch (pin) {
  208. case 7: CORE_PIN7_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  209. case 6: CORE_PIN6_CONFIG = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); break;
  210. }
  211. }
  212. rx_pin_num = pin;
  213. #endif
  214. }
  215. int serial3_set_rts(uint8_t pin)
  216. {
  217. if (!(SIM_SCGC4 & SIM_SCGC4_UART2)) return 0;
  218. if (pin < CORE_NUM_DIGITAL) {
  219. rts_pin = portOutputRegister(pin);
  220. #if defined(KINETISL)
  221. rts_mask = digitalPinToBitMask(pin);
  222. #endif
  223. pinMode(pin, OUTPUT);
  224. rts_assert();
  225. } else {
  226. rts_pin = NULL;
  227. return 0;
  228. }
  229. /*
  230. if (pin == 2) {
  231. CORE_PIN2_CONFIG = PORT_PCR_MUX(3);
  232. } else {
  233. UART2_MODEM &= ~UART_MODEM_RXRTSE;
  234. return 0;
  235. }
  236. UART2_MODEM |= UART_MODEM_RXRTSE;
  237. */
  238. return 1;
  239. }
  240. int serial3_set_cts(uint8_t pin)
  241. {
  242. #if defined(KINETISK)
  243. if (!(SIM_SCGC4 & SIM_SCGC4_UART2)) return 0;
  244. if (pin == 14) {
  245. CORE_PIN14_CONFIG = PORT_PCR_MUX(3) | PORT_PCR_PE; // weak pulldown
  246. } else {
  247. UART2_MODEM &= ~UART_MODEM_TXCTSE;
  248. return 0;
  249. }
  250. UART2_MODEM |= UART_MODEM_TXCTSE;
  251. return 1;
  252. #else
  253. return 0;
  254. #endif
  255. }
  256. void serial3_putchar(uint32_t c)
  257. {
  258. uint32_t head, n;
  259. if (!(SIM_SCGC4 & SIM_SCGC4_UART2)) 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 ((UART2_S1 & UART_S1_TDRE)) {
  267. uint32_t tail = tx_buffer_tail;
  268. if (++tail >= TX_BUFFER_SIZE) tail = 0;
  269. n = tx_buffer[tail];
  270. if (use9Bits) UART2_C3 = (UART2_C3 & ~0x40) | ((n & 0x100) >> 2);
  271. UART2_D = 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. UART2_C2 = C2_TX_ACTIVE;
  282. }
  283. void serial3_write(const void *buf, unsigned int count)
  284. {
  285. const uint8_t *p = (const uint8_t *)buf;
  286. while (count-- > 0) serial3_putchar(*p++);
  287. }
  288. void serial3_flush(void)
  289. {
  290. while (transmitting) yield(); // wait
  291. }
  292. int serial3_write_buffer_free(void)
  293. {
  294. uint32_t head, tail;
  295. head = tx_buffer_head;
  296. tail = tx_buffer_tail;
  297. if (head >= tail) return TX_BUFFER_SIZE - 1 - head + tail;
  298. return tail - head - 1;
  299. }
  300. int serial3_available(void)
  301. {
  302. uint32_t head, tail;
  303. head = rx_buffer_head;
  304. tail = rx_buffer_tail;
  305. if (head >= tail) return head - tail;
  306. return RX_BUFFER_SIZE + head - tail;
  307. }
  308. int serial3_getchar(void)
  309. {
  310. uint32_t head, tail;
  311. int c;
  312. head = rx_buffer_head;
  313. tail = rx_buffer_tail;
  314. if (head == tail) return -1;
  315. if (++tail >= RX_BUFFER_SIZE) tail = 0;
  316. c = rx_buffer[tail];
  317. rx_buffer_tail = tail;
  318. if (rts_pin) {
  319. int avail;
  320. if (head >= tail) avail = head - tail;
  321. else avail = RX_BUFFER_SIZE + head - tail;
  322. if (avail <= RTS_LOW_WATERMARK) rts_assert();
  323. }
  324. return c;
  325. }
  326. int serial3_peek(void)
  327. {
  328. uint32_t head, tail;
  329. head = rx_buffer_head;
  330. tail = rx_buffer_tail;
  331. if (head == tail) return -1;
  332. if (++tail >= RX_BUFFER_SIZE) tail = 0;
  333. return rx_buffer[tail];
  334. }
  335. void serial3_clear(void)
  336. {
  337. rx_buffer_head = rx_buffer_tail;
  338. if (rts_pin) rts_assert();
  339. }
  340. // status interrupt combines
  341. // Transmit data below watermark UART_S1_TDRE
  342. // Transmit complete UART_S1_TC
  343. // Idle line UART_S1_IDLE
  344. // Receive data above watermark UART_S1_RDRF
  345. // LIN break detect UART_S2_LBKDIF
  346. // RxD pin active edge UART_S2_RXEDGIF
  347. void uart2_status_isr(void)
  348. {
  349. uint32_t head, tail, n;
  350. uint8_t c;
  351. if (UART2_S1 & UART_S1_RDRF) {
  352. if (use9Bits && (UART2_C3 & 0x80)) {
  353. n = UART2_D | 0x100;
  354. } else {
  355. n = UART2_D;
  356. }
  357. head = rx_buffer_head + 1;
  358. if (head >= RX_BUFFER_SIZE) head = 0;
  359. if (head != rx_buffer_tail) {
  360. rx_buffer[head] = n;
  361. rx_buffer_head = head;
  362. }
  363. if (rts_pin) {
  364. int avail;
  365. tail = tx_buffer_tail;
  366. if (head >= tail) avail = head - tail;
  367. else avail = RX_BUFFER_SIZE + head - tail;
  368. if (avail >= RTS_HIGH_WATERMARK) rts_deassert();
  369. }
  370. }
  371. c = UART2_C2;
  372. if ((c & UART_C2_TIE) && (UART2_S1 & UART_S1_TDRE)) {
  373. head = tx_buffer_head;
  374. tail = tx_buffer_tail;
  375. if (head == tail) {
  376. UART2_C2 = C2_TX_COMPLETING;
  377. } else {
  378. if (++tail >= TX_BUFFER_SIZE) tail = 0;
  379. n = tx_buffer[tail];
  380. if (use9Bits) UART2_C3 = (UART2_C3 & ~0x40) | ((n & 0x100) >> 2);
  381. UART2_D = n;
  382. tx_buffer_tail = tail;
  383. }
  384. }
  385. if ((c & UART_C2_TCIE) && (UART2_S1 & UART_S1_TC)) {
  386. transmitting = 0;
  387. if (transmit_pin) transmit_deassert();
  388. UART2_C2 = C2_TX_INACTIVE;
  389. }
  390. }