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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. // Pin Setup for UART0
  81. PORTB_PCR16 = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); // RX Pin
  82. PORTB_PCR17 = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3); // TX Pin
  83. // Setup baud rate - 115200 Baud
  84. // 48 MHz / ( 16 * Baud ) = BDH/L
  85. // Baud: 115200 -> 48 MHz / ( 16 * 115200 ) = 26.0416667
  86. // Thus baud setting = 26
  87. // NOTE: If finer baud adjustment is needed see UARTx_C4 -> BRFA in the datasheet
  88. uint16_t baud = 26; // Max setting of 8191
  89. UART0_BDH = (uint8_t)(baud >> 8);
  90. UART0_BDL = (uint8_t)baud;
  91. UART0_C4 = 0x02;
  92. // 8 bit, No Parity, Idle Character bit after stop
  93. UART0_C1 = UART_C1_ILT;
  94. // Interrupt notification watermark
  95. UART0_TWFIFO = 2;
  96. UART0_RWFIFO = 4;
  97. // TX FIFO Disabled, TX FIFO Size 1 (Max 8 datawords), RX FIFO Enabled, RX FIFO Size 1 (Max 8 datawords)
  98. // TX/RX FIFO Size:
  99. // 0x0 - 1 dataword
  100. // 0x1 - 4 dataword
  101. // 0x2 - 8 dataword
  102. UART0_PFIFO = UART_PFIFO_TXFE | UART_PFIFO_RXFE;
  103. // Reciever Inversion Disabled, LSBF
  104. // UART_S2_RXINV UART_S2_MSBF
  105. UART0_S2 |= 0x00;
  106. // Transmit Inversion Disabled
  107. // UART_C3_TXINV
  108. UART0_C3 |= 0x00;
  109. // TX Enabled, RX Enabled, RX Interrupt Enabled, Generate idles
  110. // UART_C2_TE UART_C2_RE UART_C2_RIE UART_C2_ILIE
  111. UART0_C2 = UART_C2_TE | UART_C2_RE | UART_C2_RIE | UART_C2_ILIE;
  112. // Add interrupt to the vector table
  113. NVIC_ENABLE_IRQ( IRQ_UART0_STATUS );
  114. // UART is now ready to use
  115. uart_configured = 1;
  116. }
  117. // Get the next character, or -1 if nothing received
  118. int uart_serial_getchar()
  119. {
  120. if ( !uart_configured )
  121. return -1;
  122. unsigned int value = -1;
  123. // Check to see if the FIFO has characters
  124. if ( uart0_buffer_items > 0 )
  125. {
  126. value = uart0_buffer[uart0_buffer_head++];
  127. uart0_buffer_items--;
  128. // Wrap-around of head pointer
  129. if ( uart0_buffer_head >= uart0_buffer_size )
  130. {
  131. uart0_buffer_head = 0;
  132. }
  133. }
  134. return value;
  135. }
  136. // Number of bytes available in the receive buffer
  137. int uart_serial_available()
  138. {
  139. return uart0_buffer_items;
  140. }
  141. // Discard any buffered input
  142. void uart_serial_flush_input()
  143. {
  144. uart0_buffer_head = 0;
  145. uart0_buffer_tail = 0;
  146. uart0_buffer_items = 0;
  147. }
  148. // Transmit a character. 0 returned on success, -1 on error
  149. int uart_serial_putchar( uint8_t c )
  150. {
  151. if ( !uart_configured )
  152. return -1;
  153. while ( !( UART0_SFIFO & UART_SFIFO_TXEMPT ) ); // Wait till there is room to send
  154. UART0_D = c;
  155. return 0;
  156. }
  157. int uart_serial_write( const void *buffer, uint32_t size )
  158. {
  159. if ( !uart_configured )
  160. return -1;
  161. const uint8_t *data = (const uint8_t *)buffer;
  162. uint32_t position = 0;
  163. // While buffer is not empty and transmit buffer is
  164. while ( position < size )
  165. {
  166. while ( !( UART0_SFIFO & UART_SFIFO_TXEMPT ) ); // Wait till there is room to send
  167. UART0_D = data[position++];
  168. }
  169. return 0;
  170. }
  171. void uart_serial_flush_output()
  172. {
  173. // Delay until buffer has been sent
  174. while ( !( UART0_SFIFO & UART_SFIFO_TXEMPT ) ); // Wait till there is room to send
  175. }
  176. void uart_device_reload()
  177. {
  178. asm volatile("bkpt");
  179. }