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.

debug.c 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /* Copyright (C) 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. // Local Includes
  23. #include "mchck.h"
  24. // ----- Defines -----
  25. // UART Configuration
  26. #if defined(_mk20dx256vlh7_) // UART2 Debug
  27. #define UART_BDH UART2_BDH
  28. #define UART_BDL UART2_BDL
  29. #define UART_C1 UART2_C1
  30. #define UART_C2 UART2_C2
  31. #define UART_C3 UART2_C3
  32. #define UART_C4 UART2_C4
  33. #define UART_CFIFO UART2_CFIFO
  34. #define UART_D UART2_D
  35. #define UART_PFIFO UART2_PFIFO
  36. #define UART_RCFIFO UART2_RCFIFO
  37. #define UART_RWFIFO UART2_RWFIFO
  38. #define UART_S1 UART2_S1
  39. #define UART_S2 UART2_S2
  40. #define UART_SFIFO UART2_SFIFO
  41. #define UART_TWFIFO UART2_TWFIFO
  42. #define SIM_SCGC4_UART SIM_SCGC4_UART2
  43. #define IRQ_UART_STATUS IRQ_UART2_STATUS
  44. #else
  45. #error "Bootloader UART Debug unsupported"
  46. #endif
  47. // ----- Functions -----
  48. #if defined(_mk20dx256vlh7_)
  49. void uart_serial_setup()
  50. {
  51. // Setup the the UART interface for keyboard data input
  52. SIM_SCGC4 |= SIM_SCGC4_UART; // Disable clock gating
  53. // Kiibohd-dfu
  54. #if defined(_mk20dx256vlh7_)
  55. // Pin Setup for UART2
  56. PORTD_PCR3 = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3); // TX Pin
  57. #endif
  58. #if defined(_mk20dx256vlh7_) // UART2 Debug
  59. // Setup baud rate - 115200 Baud
  60. // Uses Bus Clock
  61. // 36 MHz / ( 16 * Baud ) = BDH/L
  62. // Baud: 115200 -> 36 MHz / ( 16 * 115200 ) = 19.53125
  63. // Thus baud setting = 19
  64. // NOTE: If finer baud adjustment is needed see UARTx_C4 -> BRFA in the datasheet
  65. uint16_t baud = 19; // Max setting of 8191
  66. UART_BDH = (uint8_t)(baud >> 8);
  67. UART_BDL = (uint8_t)baud;
  68. UART_C4 = 0x11;
  69. #endif
  70. // 8 bit, No Parity, Idle Character bit after stop
  71. UART_C1 = UART_C1_ILT;
  72. // TX FIFO Enabled, TX FIFO Size 1 (Max 8 datawords)
  73. // TX/RX FIFO Size:
  74. // 0x0 - 1 dataword
  75. // 0x1 - 4 dataword
  76. // 0x2 - 8 dataword
  77. UART_PFIFO = UART_PFIFO_TXFE;
  78. // TX Enabled, RX Enabled, RX Interrupt Enabled, Generate idles
  79. // UART_C2_TE UART_C2_RE UART_C2_RIE UART_C2_ILIE
  80. UART_C2 = UART_C2_TE | UART_C2_ILIE;
  81. }
  82. int uart_serial_write( const void *buffer, uint32_t size )
  83. {
  84. const uint8_t *data = (const uint8_t *)buffer;
  85. uint32_t position = 0;
  86. // While buffer is not empty and transmit buffer is
  87. while ( position < size )
  88. {
  89. while ( !( UART_SFIFO & UART_SFIFO_TXEMPT ) ); // Wait till there is room to send
  90. UART_D = data[position++];
  91. }
  92. return 0;
  93. }
  94. int Output_putstr( char* str )
  95. {
  96. uint32_t count = 0;
  97. // Count characters until NULL character, then send the amount counted
  98. while ( str[count] != '\0' )
  99. count++;
  100. return uart_serial_write( str, count );
  101. }
  102. uint16_t lenStr( char* in )
  103. {
  104. // Iterator
  105. char *pos;
  106. // Loop until null is found
  107. for ( pos = in; *pos; pos++ );
  108. // Return the difference between the pointers of in and pos (which is the string length)
  109. return (pos - in);
  110. }
  111. void revsStr( char* in )
  112. {
  113. // Iterators
  114. int i, j;
  115. // Temp storage
  116. char c;
  117. // Loop through the string, and reverse the order of the characters
  118. for ( i = 0, j = lenStr( in ) - 1; i < j; i++, j-- )
  119. {
  120. c = in[i];
  121. in[i] = in[j];
  122. in[j] = c;
  123. }
  124. }
  125. void hexToStr_op( uint32_t in, char* out, uint8_t op )
  126. {
  127. // Position container
  128. uint32_t pos = 0;
  129. // Evaluate through digits as hex
  130. do
  131. {
  132. uint32_t cur = in % 16;
  133. out[pos++] = cur + (( cur < 10 ) ? '0' : 'A' - 10);
  134. }
  135. while ( (in /= 16) > 0 );
  136. // Output formatting options
  137. switch ( op )
  138. {
  139. case 1: // Add 0x
  140. out[pos++] = 'x';
  141. out[pos++] = '0';
  142. break;
  143. case 2: // 8-bit padding
  144. case 4: // 16-bit padding
  145. case 8: // 32-bit padding
  146. while ( pos < op )
  147. out[pos++] = '0';
  148. break;
  149. }
  150. // Append null
  151. out[pos] = '\0';
  152. // Reverse the string to the correct order
  153. revsStr( out );
  154. }
  155. void printHex_op( uint32_t in, uint8_t op )
  156. {
  157. // With an op of 1, the max number of characters is 6 + 1 for null
  158. // e.g. "0xFFFF\0"
  159. // op 2 and 4 require fewer characters (2+1 and 4+1 respectively)
  160. char tmpStr[7];
  161. // Convert number
  162. hexToStr_op( in, tmpStr, op );
  163. // Print number
  164. Output_putstr( tmpStr );
  165. }
  166. #endif