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.

main.c 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /* Copyright (c) 2011,2012 Simon Schubert <[email protected]>.
  2. * Modifications by Jacob Alexander 2014-2015 <[email protected]>
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. // ----- Includes -----
  18. // Local Includes
  19. #include "mchck.h"
  20. #include "dfu.desc.h"
  21. #include "debug.h"
  22. // ----- Variables -----
  23. /**
  24. * Unfortunately we can't DMA directly to FlexRAM, so we'll have to stage here.
  25. */
  26. static char staging[ FLASH_SECTOR_SIZE ];
  27. // ----- Functions -----
  28. void sector_print( void* buf, size_t sector, size_t chunks )
  29. {
  30. uint8_t* start = (uint8_t*)buf + sector * USB_DFU_TRANSFER_SIZE;
  31. uint8_t* end = (uint8_t*)buf + (sector + 1) * USB_DFU_TRANSFER_SIZE;
  32. uint8_t* pos = start;
  33. print( NL );
  34. print("Block ");
  35. printHex( sector );
  36. print(" ");
  37. printHex( (size_t)start );
  38. print(" -> ");
  39. printHex( (size_t)end );
  40. print( NL );
  41. // Display sector
  42. for ( size_t line = 0; pos < end - 24; line++ )
  43. {
  44. // Each Line
  45. printHex_op( (size_t)pos, 4 );
  46. print(" ");
  47. // Each 2 byte chunk
  48. for ( size_t chunk = 0; chunk < chunks; chunk++ )
  49. {
  50. // Print out the two bytes (second one first)
  51. printHex_op( *(pos + 1), 2 );
  52. printHex_op( *pos, 2 );
  53. print(" ");
  54. pos += 2;
  55. }
  56. print( NL );
  57. }
  58. }
  59. static enum dfu_status setup_read( size_t off, size_t *len, void **buf )
  60. {
  61. // Calculate starting address from offset
  62. *buf = (void*)&_app_rom + (USB_DFU_TRANSFER_SIZE / 4) * off;
  63. // Calculate length of transfer
  64. *len = *buf > (void*)(&_app_rom_end) - USB_DFU_TRANSFER_SIZE
  65. ? 0 : USB_DFU_TRANSFER_SIZE;
  66. // Check for error
  67. if ( *buf > (void*)&_app_rom_end )
  68. return (DFU_STATUS_errADDRESS);
  69. return (DFU_STATUS_OK);
  70. }
  71. static enum dfu_status setup_write( size_t off, size_t len, void **buf )
  72. {
  73. static int last = 0;
  74. if ( len > sizeof(staging) )
  75. return (DFU_STATUS_errADDRESS);
  76. // We only allow the last write to be less than one sector size.
  77. if ( off == 0 )
  78. last = 0;
  79. if ( last && len != 0 )
  80. return (DFU_STATUS_errADDRESS);
  81. if ( len != FLASH_SECTOR_SIZE )
  82. {
  83. last = 1;
  84. memset( staging, 0xff, sizeof(staging) );
  85. }
  86. *buf = staging;
  87. return (DFU_STATUS_OK);
  88. }
  89. static enum dfu_status finish_write( void *buf, size_t off, size_t len )
  90. {
  91. void *target;
  92. if ( len == 0 )
  93. return (DFU_STATUS_OK);
  94. target = flash_get_staging_area(off + (uintptr_t)&_app_rom, FLASH_SECTOR_SIZE);
  95. if ( !target )
  96. return (DFU_STATUS_errADDRESS);
  97. memcpy( target, buf, len );
  98. print("BUF: ");
  99. printHex( off );
  100. sector_print( target, 0, 16 );
  101. // Depending on the error return a different status
  102. switch ( flash_program_sector(off + (uintptr_t)&_app_rom, FLASH_SECTOR_SIZE) )
  103. {
  104. /*
  105. case FTFL_FSTAT_RDCOLERR: // Flash Read Collision Error
  106. case FTFL_FSTAT_ACCERR: // Flash Access Error
  107. case FTFL_FSTAT_FPVIOL: // Flash Protection Violation Error
  108. return (DFU_STATUS_errADDRESS);
  109. case FTFL_FSTAT_MGSTAT0: // Memory Controller Command Completion Error
  110. return (DFU_STATUS_errADDRESS);
  111. */
  112. case 0:
  113. default: // No error
  114. return (DFU_STATUS_OK);
  115. }
  116. }
  117. static struct dfu_ctx dfu_ctx;
  118. void init_usb_bootloader( int config )
  119. {
  120. dfu_init( setup_read, setup_write, finish_write, &dfu_ctx );
  121. }
  122. void main()
  123. {
  124. #if defined(_mk20dx128vlf5_) // Kiibohd-dfu / Infinity
  125. // XXX McHCK uses B16 instead of A19
  126. // Enabling LED to indicate we are in the bootloader
  127. GPIOA_PDDR |= (1<<19);
  128. // Setup pin - A19 - See Lib/pin_map.mchck for more details on pins
  129. PORTA_PCR19 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  130. GPIOA_PSOR |= (1<<19);
  131. #elif defined(_mk20dx256vlh7_) // Kiibohd-dfu
  132. // Enabling LED to indicate we are in the bootloader
  133. GPIOA_PDDR |= (1<<5);
  134. // Setup pin - A5 - See Lib/pin_map.mchck for more details on pins
  135. PORTA_PCR5 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  136. GPIOA_PSOR |= (1<<5);
  137. #else
  138. #error "Incompatible chip for bootloader"
  139. #endif
  140. uart_serial_setup();
  141. printNL( NL "Bootloader DFU-Mode" );
  142. // TODO REMOVEME
  143. for ( uint8_t sector = 0; sector < 3; sector++ )
  144. sector_print( &_app_rom, sector, 16 );
  145. print( NL );
  146. // XXX REMOVEME
  147. /*
  148. GPIOB_PDDR |= (1<<16);
  149. PORTB_PCR16 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  150. GPIOB_PSOR |= (1<<16);
  151. // RST
  152. GPIOC_PDDR |= (1<<8);
  153. PORTC_PCR8 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  154. GPIOC_PSOR |= (1<<8);
  155. // CS1B
  156. GPIOC_PDDR |= (1<<4);
  157. PORTC_PCR4 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  158. GPIOC_PCOR |= (1<<4);
  159. */
  160. // Backlight
  161. GPIOC_PDDR |= (1<<1);
  162. PORTC_PCR1 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  163. GPIOC_PCOR |= (1<<1);
  164. GPIOC_PDDR |= (1<<2);
  165. PORTC_PCR2 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  166. GPIOC_PCOR |= (1<<2);
  167. GPIOC_PDDR |= (1<<3);
  168. PORTC_PCR3 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  169. GPIOC_PCOR |= (1<<3);
  170. flash_prepare_flashing();
  171. //uint32_t *position = &_app_rom;
  172. usb_init( &dfu_device );
  173. for (;;)
  174. {
  175. usb_poll();
  176. }
  177. }