Kiibohd Controller
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
Este repositório está arquivado. Você pode visualizar os arquivos e realizar clone, mas não poderá realizar push nem abrir issues e pull requests.

uart_serial.c 4.9KB

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