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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /* Copyright (C) 2014 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. #include "uart_serial.h"
  22. #include <Lib/OutputLib.h>
  23. #include <Lib/Interrupts.h>
  24. #include <string.h> // For memcpy
  25. // ----- Variables -----
  26. #define uart0_buffer_size 128 // 128 byte buffer
  27. volatile uint8_t uart0_buffer_head = 0;
  28. volatile uint8_t uart0_buffer_tail = 0;
  29. volatile uint8_t uart0_buffer_items = 0;
  30. volatile uint8_t uart0_buffer[uart0_buffer_size];
  31. volatile uint8_t uart_configured = 0;
  32. // ----- Interrupt Functions -----
  33. void uart0_status_isr()
  34. {
  35. cli(); // Disable Interrupts
  36. // UART0_S1 must be read for the interrupt to be cleared
  37. if ( UART0_S1 & ( UART_S1_RDRF | UART_S1_IDLE ) )
  38. {
  39. uint8_t available = UART0_RCFIFO;
  40. // If there was actually nothing
  41. if ( available == 0 )
  42. {
  43. // Cleanup
  44. available = UART0_D;
  45. UART0_CFIFO = UART_CFIFO_RXFLUSH;
  46. sei();
  47. return;
  48. }
  49. // Read UART0 into buffer until FIFO is empty
  50. while ( available-- > 0 )
  51. {
  52. uart0_buffer[uart0_buffer_tail++] = UART0_D;
  53. uart0_buffer_items++;
  54. // Wrap-around of tail pointer
  55. if ( uart0_buffer_tail >= uart0_buffer_size )
  56. {
  57. uart0_buffer_tail = 0;
  58. }
  59. // Make sure the head pointer also moves if circular buffer is overwritten
  60. if ( uart0_buffer_head == uart0_buffer_tail )
  61. {
  62. uart0_buffer_head++;
  63. }
  64. // Wrap-around of head pointer
  65. if ( uart0_buffer_head >= uart0_buffer_size )
  66. {
  67. uart0_buffer_head = 0;
  68. }
  69. }
  70. }
  71. sei(); // Re-enable Interrupts
  72. }
  73. // ----- Functions -----
  74. void uart_serial_setup()
  75. {
  76. // Indication that the UART is not ready yet
  77. uart_configured = 0;
  78. // Setup the the UART interface for keyboard data input
  79. SIM_SCGC4 |= SIM_SCGC4_UART0; // Disable clock gating
  80. // MCHCK
  81. #if defined(_mk20dx128vlf5_)
  82. // Pin Setup for UART0
  83. PORTA_PCR1 = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(2); // RX Pin
  84. PORTA_PCR2 = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(2); // TX Pin
  85. // Teensy
  86. #else
  87. // Pin Setup for UART0
  88. PORTB_PCR16 = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); // RX Pin
  89. PORTB_PCR17 = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3); // TX Pin
  90. #endif
  91. // Setup baud rate - 115200 Baud
  92. // 48 MHz / ( 16 * Baud ) = BDH/L
  93. // Baud: 115200 -> 48 MHz / ( 16 * 115200 ) = 26.0416667
  94. // Thus baud setting = 26
  95. // NOTE: If finer baud adjustment is needed see UARTx_C4 -> BRFA in the datasheet
  96. uint16_t baud = 26; // Max setting of 8191
  97. UART0_BDH = (uint8_t)(baud >> 8);
  98. UART0_BDL = (uint8_t)baud;
  99. UART0_C4 = 0x02;
  100. // 8 bit, No Parity, Idle Character bit after stop
  101. UART0_C1 = UART_C1_ILT;
  102. // Interrupt notification watermarks
  103. UART0_TWFIFO = 2;
  104. UART0_RWFIFO = 4;
  105. // TX FIFO Disabled, TX FIFO Size 1 (Max 8 datawords), RX FIFO Enabled, RX FIFO Size 1 (Max 8 datawords)
  106. // TX/RX FIFO Size:
  107. // 0x0 - 1 dataword
  108. // 0x1 - 4 dataword
  109. // 0x2 - 8 dataword
  110. UART0_PFIFO = UART_PFIFO_TXFE | UART_PFIFO_RXFE;
  111. // Reciever Inversion Disabled, LSBF
  112. // UART_S2_RXINV UART_S2_MSBF
  113. UART0_S2 |= 0x00;
  114. // Transmit Inversion Disabled
  115. // UART_C3_TXINV
  116. UART0_C3 |= 0x00;
  117. // TX Enabled, RX Enabled, RX Interrupt Enabled, Generate idles
  118. // UART_C2_TE UART_C2_RE UART_C2_RIE UART_C2_ILIE
  119. UART0_C2 = UART_C2_TE | UART_C2_RE | UART_C2_RIE | UART_C2_ILIE;
  120. // Add interrupt to the vector table
  121. NVIC_ENABLE_IRQ( IRQ_UART0_STATUS );
  122. // UART is now ready to use
  123. uart_configured = 1;
  124. }
  125. // Get the next character, or -1 if nothing received
  126. int uart_serial_getchar()
  127. {
  128. if ( !uart_configured )
  129. return -1;
  130. unsigned int value = -1;
  131. // Check to see if the FIFO has characters
  132. if ( uart0_buffer_items > 0 )
  133. {
  134. value = uart0_buffer[uart0_buffer_head++];
  135. uart0_buffer_items--;
  136. // Wrap-around of head pointer
  137. if ( uart0_buffer_head >= uart0_buffer_size )
  138. {
  139. uart0_buffer_head = 0;
  140. }
  141. }
  142. return value;
  143. }
  144. // Number of bytes available in the receive buffer
  145. int uart_serial_available()
  146. {
  147. return uart0_buffer_items;
  148. }
  149. // Discard any buffered input
  150. void uart_serial_flush_input()
  151. {
  152. uart0_buffer_head = 0;
  153. uart0_buffer_tail = 0;
  154. uart0_buffer_items = 0;
  155. }
  156. // Transmit a character. 0 returned on success, -1 on error
  157. int uart_serial_putchar( uint8_t c )
  158. {
  159. if ( !uart_configured )
  160. return -1;
  161. while ( !( UART0_SFIFO & UART_SFIFO_TXEMPT ) ); // Wait till there is room to send
  162. UART0_D = c;
  163. return 0;
  164. }
  165. int uart_serial_write( const void *buffer, uint32_t size )
  166. {
  167. if ( !uart_configured )
  168. return -1;
  169. const uint8_t *data = (const uint8_t *)buffer;
  170. uint32_t position = 0;
  171. // While buffer is not empty and transmit buffer is
  172. while ( position < size )
  173. {
  174. while ( !( UART0_SFIFO & UART_SFIFO_TXEMPT ) ); // Wait till there is room to send
  175. UART0_D = data[position++];
  176. }
  177. return 0;
  178. }
  179. void uart_serial_flush_output()
  180. {
  181. // Delay until buffer has been sent
  182. while ( !( UART0_SFIFO & UART_SFIFO_TXEMPT ) ); // Wait till there is room to send
  183. }
  184. void uart_device_reload()
  185. {
  186. asm volatile("bkpt");
  187. }