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

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