Kiibohd Controller
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

uart_serial.c 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /* Copyright (C) 2014-2015 by Jacob Alexander
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to deal
  5. * in the Software without restriction, including without limitation the rights
  6. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. * copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. * THE SOFTWARE.
  20. */
  21. // ----- Includes -----
  22. // Compiler Includes
  23. #include <string.h> // For memcpy
  24. // Project Includes
  25. #include <Lib/OutputLib.h>
  26. #include <Lib/Interrupts.h>
  27. #include <print.h>
  28. #include <kll_defs.h>
  29. // Local Includes
  30. #include "uart_serial.h"
  31. // ----- Defines -----
  32. // UART Configuration
  33. #if defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_) // UART0 Debug
  34. #define UART_BDH UART0_BDH
  35. #define UART_BDL UART0_BDL
  36. #define UART_C1 UART0_C1
  37. #define UART_C2 UART0_C2
  38. #define UART_C3 UART0_C3
  39. #define UART_C4 UART0_C4
  40. #define UART_CFIFO UART0_CFIFO
  41. #define UART_D UART0_D
  42. #define UART_PFIFO UART0_PFIFO
  43. #define UART_RCFIFO UART0_RCFIFO
  44. #define UART_RWFIFO UART0_RWFIFO
  45. #define UART_S1 UART0_S1
  46. #define UART_S2 UART0_S2
  47. #define UART_SFIFO UART0_SFIFO
  48. #define UART_TWFIFO UART0_TWFIFO
  49. #define SIM_SCGC4_UART SIM_SCGC4_UART0
  50. #define IRQ_UART_STATUS IRQ_UART0_STATUS
  51. #elif defined(_mk20dx256vlh7_) // UART2 Debug
  52. #define UART_BDH UART2_BDH
  53. #define UART_BDL UART2_BDL
  54. #define UART_C1 UART2_C1
  55. #define UART_C2 UART2_C2
  56. #define UART_C3 UART2_C3
  57. #define UART_C4 UART2_C4
  58. #define UART_CFIFO UART2_CFIFO
  59. #define UART_D UART2_D
  60. #define UART_PFIFO UART2_PFIFO
  61. #define UART_RCFIFO UART2_RCFIFO
  62. #define UART_RWFIFO UART2_RWFIFO
  63. #define UART_S1 UART2_S1
  64. #define UART_S2 UART2_S2
  65. #define UART_SFIFO UART2_SFIFO
  66. #define UART_TWFIFO UART2_TWFIFO
  67. #define SIM_SCGC4_UART SIM_SCGC4_UART2
  68. #define IRQ_UART_STATUS IRQ_UART2_STATUS
  69. #endif
  70. // ----- Variables -----
  71. #define uart_buffer_size 128 // 128 byte buffer
  72. volatile uint8_t uart_buffer_head = 0;
  73. volatile uint8_t uart_buffer_tail = 0;
  74. volatile uint8_t uart_buffer_items = 0;
  75. volatile uint8_t uart_buffer[uart_buffer_size];
  76. volatile uint8_t uart_configured = 0;
  77. // ----- Interrupt Functions -----
  78. #if defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_) // UART0 Debug
  79. void uart0_status_isr()
  80. #elif defined(_mk20dx256vlh7_) // UART2 Debug
  81. void uart2_status_isr()
  82. #endif
  83. {
  84. cli(); // Disable Interrupts
  85. // UART0_S1 must be read for the interrupt to be cleared
  86. if ( UART_S1 & ( UART_S1_RDRF | UART_S1_IDLE ) )
  87. {
  88. uint8_t available = UART_RCFIFO;
  89. // If there was actually nothing
  90. if ( available == 0 )
  91. {
  92. // Cleanup
  93. available = UART_D;
  94. UART_CFIFO = UART_CFIFO_RXFLUSH;
  95. goto done;
  96. }
  97. // Read UART0 into buffer until FIFO is empty
  98. while ( available-- > 0 )
  99. {
  100. uart_buffer[uart_buffer_tail++] = UART_D;
  101. uart_buffer_items++;
  102. // Wrap-around of tail pointer
  103. if ( uart_buffer_tail >= uart_buffer_size )
  104. {
  105. uart_buffer_tail = 0;
  106. }
  107. // Make sure the head pointer also moves if circular buffer is overwritten
  108. if ( uart_buffer_head == uart_buffer_tail )
  109. {
  110. uart_buffer_head++;
  111. }
  112. // Wrap-around of head pointer
  113. if ( uart_buffer_head >= uart_buffer_size )
  114. {
  115. uart_buffer_head = 0;
  116. }
  117. }
  118. }
  119. done:
  120. sei(); // Re-enable Interrupts
  121. }
  122. // ----- Functions -----
  123. void uart_serial_setup()
  124. {
  125. // Indication that the UART is not ready yet
  126. uart_configured = 0;
  127. // Setup the the UART interface for keyboard data input
  128. SIM_SCGC4 |= SIM_SCGC4_UART; // Disable clock gating
  129. // MCHCK / Kiibohd-dfu
  130. #if defined(_mk20dx128vlf5_)
  131. // Pin Setup for UART0
  132. PORTA_PCR1 = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(2); // RX Pin
  133. PORTA_PCR2 = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(2); // TX Pin
  134. // Kiibohd-dfu
  135. #elif defined(_mk20dx256vlh7_)
  136. // Pin Setup for UART2
  137. PORTD_PCR2 = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); // RX Pin
  138. PORTD_PCR3 = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3); // TX Pin
  139. // Teensy
  140. #else
  141. // Pin Setup for UART0
  142. PORTB_PCR16 = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); // RX Pin
  143. PORTB_PCR17 = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3); // TX Pin
  144. #endif
  145. #if defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_) // UART0 Debug
  146. // Setup baud rate - 115200 Baud
  147. // 48 MHz / ( 16 * Baud ) = BDH/L
  148. // Baud: 115200 -> 48 MHz / ( 16 * 115200 ) = 26.0416667
  149. // Thus baud setting = 26
  150. // NOTE: If finer baud adjustment is needed see UARTx_C4 -> BRFA in the datasheet
  151. uint16_t baud = 26; // Max setting of 8191
  152. UART_BDH = (uint8_t)(baud >> 8);
  153. UART_BDL = (uint8_t)baud;
  154. UART_C4 = 0x02;
  155. #elif defined(_mk20dx256vlh7_) // UART2 Debug
  156. // Setup baud rate - 115200 Baud
  157. // Uses Bus Clock
  158. // 36 MHz / ( 16 * Baud ) = BDH/L
  159. // Baud: 115200 -> 36 MHz / ( 16 * 115200 ) = 19.53125
  160. // Thus baud setting = 19
  161. // NOTE: If finer baud adjustment is needed see UARTx_C4 -> BRFA in the datasheet
  162. uint16_t baud = 19; // Max setting of 8191
  163. UART_BDH = (uint8_t)(baud >> 8);
  164. UART_BDL = (uint8_t)baud;
  165. UART_C4 = 0x11;
  166. #endif
  167. // 8 bit, No Parity, Idle Character bit after stop
  168. UART_C1 = UART_C1_ILT;
  169. // Interrupt notification watermarks
  170. #if defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_) // UART0 Debug
  171. UART_TWFIFO = 2;
  172. UART_RWFIFO = 4;
  173. #elif defined(_mk20dx256vlh7_) // UART2 Debug
  174. // UART2 has a single byte FIFO
  175. UART_TWFIFO = 1;
  176. UART_RWFIFO = 1;
  177. #endif
  178. // TX FIFO Enabled, TX FIFO Size 1 (Max 8 datawords), RX FIFO Enabled, RX FIFO Size 1 (Max 8 datawords)
  179. // TX/RX FIFO Size:
  180. // 0x0 - 1 dataword
  181. // 0x1 - 4 dataword
  182. // 0x2 - 8 dataword
  183. UART_PFIFO = UART_PFIFO_TXFE | UART_PFIFO_RXFE;
  184. // Reciever Inversion Disabled, LSBF
  185. // UART_S2_RXINV UART_S2_MSBF
  186. UART_S2 |= 0x00;
  187. // Transmit Inversion Disabled
  188. // UART_C3_TXINV
  189. UART_C3 |= 0x00;
  190. // TX Enabled, RX Enabled, RX Interrupt Enabled, Generate idles
  191. // UART_C2_TE UART_C2_RE UART_C2_RIE UART_C2_ILIE
  192. UART_C2 = UART_C2_TE | UART_C2_RE | UART_C2_RIE | UART_C2_ILIE;
  193. // Add interrupt to the vector table
  194. NVIC_ENABLE_IRQ( IRQ_UART_STATUS );
  195. // UART is now ready to use
  196. uart_configured = 1;
  197. }
  198. // Get the next character, or -1 if nothing received
  199. int uart_serial_getchar()
  200. {
  201. if ( !uart_configured )
  202. return -1;
  203. unsigned int value = -1;
  204. // Check to see if the FIFO has characters
  205. if ( uart_buffer_items > 0 )
  206. {
  207. value = uart_buffer[uart_buffer_head++];
  208. uart_buffer_items--;
  209. // Wrap-around of head pointer
  210. if ( uart_buffer_head >= uart_buffer_size )
  211. {
  212. uart_buffer_head = 0;
  213. }
  214. }
  215. return value;
  216. }
  217. // Number of bytes available in the receive buffer
  218. int uart_serial_available()
  219. {
  220. return uart_buffer_items;
  221. }
  222. // Discard any buffered input
  223. void uart_serial_flush_input()
  224. {
  225. uart_buffer_head = 0;
  226. uart_buffer_tail = 0;
  227. uart_buffer_items = 0;
  228. }
  229. // Transmit a character. 0 returned on success, -1 on error
  230. int uart_serial_putchar( uint8_t c )
  231. {
  232. if ( !uart_configured )
  233. return -1;
  234. while ( !( UART_SFIFO & UART_SFIFO_TXEMPT ) ); // Wait till there is room to send
  235. UART_D = c;
  236. return 0;
  237. }
  238. int uart_serial_write( const void *buffer, uint32_t size )
  239. {
  240. if ( !uart_configured )
  241. return -1;
  242. const uint8_t *data = (const uint8_t *)buffer;
  243. uint32_t position = 0;
  244. // While buffer is not empty and transmit buffer is
  245. while ( position < size )
  246. {
  247. while ( !( UART_SFIFO & UART_SFIFO_TXEMPT ) ); // Wait till there is room to send
  248. UART_D = data[position++];
  249. }
  250. return 0;
  251. }
  252. void uart_serial_flush_output()
  253. {
  254. // Delay until buffer has been sent
  255. while ( !( UART_SFIFO & UART_SFIFO_TXEMPT ) ); // Wait till there is room to send
  256. }
  257. void uart_device_reload()
  258. {
  259. if ( flashModeEnabled_define == 0 )
  260. {
  261. print( NL );
  262. warn_print("flashModeEnabled not set, cancelling firmware reload...");
  263. info_msg("Set flashModeEnabled to 1 in your kll configuration.");
  264. return;
  265. }
  266. // MCHCK
  267. #if defined(_mk20dx128vlf5_)
  268. // MCHCK Kiibohd Variant
  269. // Check to see if PTA3 (has a pull-up) is connected to GND (usually via jumper)
  270. // Only allow reload if the jumper is present (security)
  271. GPIOA_PDDR &= ~(1<<3); // Input
  272. PORTA_PCR3 = PORT_PCR_PFE | PORT_PCR_MUX(1); // Internal pull-up
  273. // Check for jumper
  274. if ( GPIOA_PDIR & (1<<3) && flashModeEnabled_define != 0 )
  275. {
  276. print( NL );
  277. warn_print("Security jumper not present, cancelling firmware reload...");
  278. info_msg("Replace jumper on middle 2 pins, or manually press the firmware reload button.");
  279. }
  280. else
  281. {
  282. // Copies variable into the VBAT register, must be identical to the variable in the bootloader to jump to the bootloader flash mode
  283. for ( int pos = 0; pos < sizeof(sys_reset_to_loader_magic); pos++ )
  284. (&VBAT)[ pos ] = sys_reset_to_loader_magic[ pos ];
  285. SOFTWARE_RESET();
  286. }
  287. // Kiibohd mk20dx256vlh7
  288. #elif defined(_mk20dx256vlh7_)
  289. // Copies variable into the VBAT register, must be identical to the variable in the bootloader to jump to the bootloader flash mode
  290. for ( int pos = 0; pos < sizeof(sys_reset_to_loader_magic); pos++ )
  291. (&VBAT)[ pos ] = sys_reset_to_loader_magic[ pos ];
  292. SOFTWARE_RESET();
  293. // Teensy 3.0 and 3.1
  294. #else
  295. asm volatile("bkpt");
  296. #endif
  297. }