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 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. // Local Includes
  28. #include "uart_serial.h"
  29. // ----- Defines -----
  30. // UART Configuration
  31. #if defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_) // UART0 Debug
  32. #define UART_BDH UART0_BDH
  33. #define UART_BDL UART0_BDL
  34. #define UART_C1 UART0_C1
  35. #define UART_C2 UART0_C2
  36. #define UART_C3 UART0_C3
  37. #define UART_C4 UART0_C4
  38. #define UART_CFIFO UART0_CFIFO
  39. #define UART_D UART0_D
  40. #define UART_PFIFO UART0_PFIFO
  41. #define UART_RCFIFO UART0_RCFIFO
  42. #define UART_RWFIFO UART0_RWFIFO
  43. #define UART_S1 UART0_S1
  44. #define UART_S2 UART0_S2
  45. #define UART_SFIFO UART0_SFIFO
  46. #define UART_TWFIFO UART0_TWFIFO
  47. #define SIM_SCGC4_UART SIM_SCGC4_UART0
  48. #define IRQ_UART_STATUS IRQ_UART0_STATUS
  49. #elif defined(_mk20dx256vlh7_) // UART2 Debug
  50. #define UART_BDH UART2_BDH
  51. #define UART_BDL UART2_BDL
  52. #define UART_C1 UART2_C1
  53. #define UART_C2 UART2_C2
  54. #define UART_C3 UART2_C3
  55. #define UART_C4 UART2_C4
  56. #define UART_CFIFO UART2_CFIFO
  57. #define UART_D UART2_D
  58. #define UART_PFIFO UART2_PFIFO
  59. #define UART_RCFIFO UART2_RCFIFO
  60. #define UART_RWFIFO UART2_RWFIFO
  61. #define UART_S1 UART2_S1
  62. #define UART_S2 UART2_S2
  63. #define UART_SFIFO UART2_SFIFO
  64. #define UART_TWFIFO UART2_TWFIFO
  65. #define SIM_SCGC4_UART SIM_SCGC4_UART2
  66. #define IRQ_UART_STATUS IRQ_UART2_STATUS
  67. #endif
  68. // ----- Variables -----
  69. #define uart_buffer_size 128 // 128 byte buffer
  70. volatile uint8_t uart_buffer_head = 0;
  71. volatile uint8_t uart_buffer_tail = 0;
  72. volatile uint8_t uart_buffer_items = 0;
  73. volatile uint8_t uart_buffer[uart_buffer_size];
  74. volatile uint8_t uart_configured = 0;
  75. // ----- Interrupt Functions -----
  76. #if defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_) // UART0 Debug
  77. void uart0_status_isr()
  78. #elif defined(_mk20dx256vlh7_) // UART2 Debug
  79. void uart2_status_isr()
  80. #endif
  81. {
  82. cli(); // Disable Interrupts
  83. // UART0_S1 must be read for the interrupt to be cleared
  84. if ( UART_S1 & ( UART_S1_RDRF | UART_S1_IDLE ) )
  85. {
  86. uint8_t available = UART_RCFIFO;
  87. // If there was actually nothing
  88. if ( available == 0 )
  89. {
  90. // Cleanup
  91. available = UART_D;
  92. UART_CFIFO = UART_CFIFO_RXFLUSH;
  93. sei();
  94. return;
  95. }
  96. // Read UART0 into buffer until FIFO is empty
  97. while ( available-- > 0 )
  98. {
  99. uart_buffer[uart_buffer_tail++] = UART_D;
  100. uart_buffer_items++;
  101. // Wrap-around of tail pointer
  102. if ( uart_buffer_tail >= uart_buffer_size )
  103. {
  104. uart_buffer_tail = 0;
  105. }
  106. // Make sure the head pointer also moves if circular buffer is overwritten
  107. if ( uart_buffer_head == uart_buffer_tail )
  108. {
  109. uart_buffer_head++;
  110. }
  111. // Wrap-around of head pointer
  112. if ( uart_buffer_head >= uart_buffer_size )
  113. {
  114. uart_buffer_head = 0;
  115. }
  116. }
  117. }
  118. sei(); // Re-enable Interrupts
  119. }
  120. // ----- Functions -----
  121. void uart_serial_setup()
  122. {
  123. // Indication that the UART is not ready yet
  124. uart_configured = 0;
  125. // Setup the the UART interface for keyboard data input
  126. SIM_SCGC4 |= SIM_SCGC4_UART; // Disable clock gating
  127. // MCHCK / Kiibohd-dfu
  128. #if defined(_mk20dx128vlf5_)
  129. // Pin Setup for UART0
  130. PORTA_PCR1 = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(2); // RX Pin
  131. PORTA_PCR2 = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(2); // TX Pin
  132. // Kiibohd-dfu
  133. #elif defined(_mk20dx256vlh7_)
  134. // Pin Setup for UART2
  135. PORTD_PCR2 = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); // RX Pin
  136. PORTD_PCR3 = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3); // TX Pin
  137. // Teensy
  138. #else
  139. // Pin Setup for UART0
  140. PORTB_PCR16 = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); // RX Pin
  141. PORTB_PCR17 = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3); // TX Pin
  142. #endif
  143. #if defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_) // UART0 Debug
  144. // Setup baud rate - 115200 Baud
  145. // 48 MHz / ( 16 * Baud ) = BDH/L
  146. // Baud: 115200 -> 48 MHz / ( 16 * 115200 ) = 26.0416667
  147. // Thus baud setting = 26
  148. // NOTE: If finer baud adjustment is needed see UARTx_C4 -> BRFA in the datasheet
  149. uint16_t baud = 26; // Max setting of 8191
  150. UART_BDH = (uint8_t)(baud >> 8);
  151. UART_BDL = (uint8_t)baud;
  152. UART_C4 = 0x02;
  153. #elif defined(_mk20dx256vlh7_) // UART2 Debug
  154. // Setup baud rate - 115200 Baud
  155. // Uses Bus Clock
  156. // 24 MHz / ( 16 * Baud ) = BDH/L
  157. // Baud: 115200 -> 24 MHz / ( 16 * 115200 ) = 13.021
  158. // Thus baud setting = 13
  159. // NOTE: If finer baud adjustment is needed see UARTx_C4 -> BRFA in the datasheet
  160. uint16_t baud = 13; // Max setting of 8191
  161. UART_BDH = (uint8_t)(baud >> 8);
  162. UART_BDL = (uint8_t)baud;
  163. UART_C4 = 0x01;
  164. #endif
  165. // 8 bit, No Parity, Idle Character bit after stop
  166. UART_C1 = UART_C1_ILT;
  167. // Interrupt notification watermarks
  168. #if defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_) // UART0 Debug
  169. UART_TWFIFO = 2;
  170. UART_RWFIFO = 4;
  171. #elif defined(_mk20dx256vlh7_) // UART2 Debug
  172. // UART2 has a single byte FIFO
  173. UART_TWFIFO = 1;
  174. UART_RWFIFO = 1;
  175. #endif
  176. // TX FIFO Enabled, TX FIFO Size 1 (Max 8 datawords), RX FIFO Enabled, RX FIFO Size 1 (Max 8 datawords)
  177. // TX/RX FIFO Size:
  178. // 0x0 - 1 dataword
  179. // 0x1 - 4 dataword
  180. // 0x2 - 8 dataword
  181. UART_PFIFO = UART_PFIFO_TXFE | UART_PFIFO_RXFE;
  182. // Reciever Inversion Disabled, LSBF
  183. // UART_S2_RXINV UART_S2_MSBF
  184. UART_S2 |= 0x00;
  185. // Transmit Inversion Disabled
  186. // UART_C3_TXINV
  187. UART_C3 |= 0x00;
  188. // TX Enabled, RX Enabled, RX Interrupt Enabled, Generate idles
  189. // UART_C2_TE UART_C2_RE UART_C2_RIE UART_C2_ILIE
  190. UART_C2 = UART_C2_TE | UART_C2_RE | UART_C2_RIE | UART_C2_ILIE;
  191. // Add interrupt to the vector table
  192. NVIC_ENABLE_IRQ( IRQ_UART_STATUS );
  193. // UART is now ready to use
  194. uart_configured = 1;
  195. }
  196. // Get the next character, or -1 if nothing received
  197. int uart_serial_getchar()
  198. {
  199. if ( !uart_configured )
  200. return -1;
  201. unsigned int value = -1;
  202. // Check to see if the FIFO has characters
  203. if ( uart_buffer_items > 0 )
  204. {
  205. value = uart_buffer[uart_buffer_head++];
  206. uart_buffer_items--;
  207. // Wrap-around of head pointer
  208. if ( uart_buffer_head >= uart_buffer_size )
  209. {
  210. uart_buffer_head = 0;
  211. }
  212. }
  213. return value;
  214. }
  215. // Number of bytes available in the receive buffer
  216. int uart_serial_available()
  217. {
  218. return uart_buffer_items;
  219. }
  220. // Discard any buffered input
  221. void uart_serial_flush_input()
  222. {
  223. uart_buffer_head = 0;
  224. uart_buffer_tail = 0;
  225. uart_buffer_items = 0;
  226. }
  227. // Transmit a character. 0 returned on success, -1 on error
  228. int uart_serial_putchar( uint8_t c )
  229. {
  230. if ( !uart_configured )
  231. return -1;
  232. while ( !( UART_SFIFO & UART_SFIFO_TXEMPT ) ); // Wait till there is room to send
  233. UART_D = c;
  234. return 0;
  235. }
  236. int uart_serial_write( const void *buffer, uint32_t size )
  237. {
  238. if ( !uart_configured )
  239. return -1;
  240. const uint8_t *data = (const uint8_t *)buffer;
  241. uint32_t position = 0;
  242. // While buffer is not empty and transmit buffer is
  243. while ( position < size )
  244. {
  245. while ( !( UART_SFIFO & UART_SFIFO_TXEMPT ) ); // Wait till there is room to send
  246. UART_D = data[position++];
  247. }
  248. return 0;
  249. }
  250. void uart_serial_flush_output()
  251. {
  252. // Delay until buffer has been sent
  253. while ( !( UART_SFIFO & UART_SFIFO_TXEMPT ) ); // Wait till there is room to send
  254. }
  255. void uart_device_reload()
  256. {
  257. asm volatile("bkpt");
  258. }