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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /* Copyright (c) 2011,2012 Simon Schubert <[email protected]>.
  2. * Modifications by Jacob Alexander 2014-2016 <[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[ USB_DFU_TRANSFER_SIZE ];
  27. // ----- Functions -----
  28. int 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. // Verify if sector erased
  34. FTFL.fccob.read_1s_section.fcmd = FTFL_FCMD_READ_1s_SECTION;
  35. FTFL.fccob.read_1s_section.addr = (uintptr_t)start;
  36. FTFL.fccob.read_1s_section.margin = FTFL_MARGIN_NORMAL;
  37. FTFL.fccob.read_1s_section.num_words = 250; // 2000 kB / 64 bits
  38. int retval = ftfl_submit_cmd();
  39. #ifdef FLASH_DEBUG
  40. print( NL );
  41. print("Block ");
  42. printHex( sector );
  43. print(" ");
  44. printHex( (size_t)start );
  45. print(" -> ");
  46. printHex( (size_t)end );
  47. print(" Erased: ");
  48. printHex( retval );
  49. print( NL );
  50. #endif
  51. // Display sector
  52. for ( size_t line = 0; pos < end - 24; line++ )
  53. {
  54. // Each Line
  55. printHex_op( (size_t)pos, 4 );
  56. print(": ");
  57. // Each 2 byte chunk
  58. for ( size_t chunk = 0; chunk < chunks; chunk++ )
  59. {
  60. // Print out the two bytes (second one first)
  61. printHex_op( *(pos + 1), 2 );
  62. printHex_op( *pos, 2 );
  63. print(" ");
  64. pos += 2;
  65. }
  66. print( NL );
  67. }
  68. return retval;
  69. }
  70. static enum dfu_status setup_read( size_t off, size_t *len, void **buf )
  71. {
  72. // Calculate starting address from offset
  73. *buf = (void*)&_app_rom + (USB_DFU_TRANSFER_SIZE / 4) * off;
  74. // Calculate length of transfer
  75. /*
  76. *len = *buf > (void*)(&_app_rom_end) - USB_DFU_TRANSFER_SIZE
  77. ? 0 : USB_DFU_TRANSFER_SIZE;
  78. */
  79. // Check for error
  80. /*
  81. if ( *buf > (void*)&_app_rom_end )
  82. return (DFU_STATUS_errADDRESS);
  83. */
  84. return (DFU_STATUS_OK);
  85. }
  86. static enum dfu_status setup_write( size_t off, size_t len, void **buf )
  87. {
  88. static int last = 0;
  89. #ifdef FLASH_DEBUG
  90. // Debug
  91. print("Setup Write: offset(");
  92. printHex( off );
  93. print(") len(");
  94. printHex( len );
  95. print(") last(");
  96. printHex( last );
  97. printNL(")");
  98. #endif
  99. if ( len > sizeof(staging) )
  100. return (DFU_STATUS_errADDRESS);
  101. // We only allow the last write to be less than one sector size.
  102. if ( off == 0 )
  103. last = 0;
  104. if ( last && len != 0 )
  105. return (DFU_STATUS_errADDRESS);
  106. if ( len != USB_DFU_TRANSFER_SIZE )
  107. {
  108. last = 1;
  109. memset( staging, 0xff, sizeof(staging) );
  110. }
  111. *buf = staging;
  112. return (DFU_STATUS_OK);
  113. }
  114. static enum dfu_status finish_write( void *buf, size_t off, size_t len )
  115. {
  116. void *target;
  117. // If nothing left to flash, this is still ok
  118. if ( len == 0 )
  119. return (DFU_STATUS_OK);
  120. // If the binary is larger than the internal flash, error
  121. if ( off + (uintptr_t)&_app_rom + len > (uintptr_t)&_app_rom_end )
  122. return (DFU_STATUS_errADDRESS);
  123. target = flash_get_staging_area( off + (uintptr_t)&_app_rom, USB_DFU_TRANSFER_SIZE );
  124. if ( !target )
  125. return (DFU_STATUS_errADDRESS);
  126. memcpy( target, buf, len );
  127. // Depending on the error return a different status
  128. switch ( flash_program_sector( off + (uintptr_t)&_app_rom, USB_DFU_TRANSFER_SIZE ) )
  129. {
  130. /*
  131. case FTFL_FSTAT_RDCOLERR: // Flash Read Collision Error
  132. case FTFL_FSTAT_ACCERR: // Flash Access Error
  133. case FTFL_FSTAT_FPVIOL: // Flash Protection Violation Error
  134. return (DFU_STATUS_errADDRESS);
  135. case FTFL_FSTAT_MGSTAT0: // Memory Controller Command Completion Error
  136. return (DFU_STATUS_errADDRESS);
  137. */
  138. case 0:
  139. default: // No error
  140. return (DFU_STATUS_OK);
  141. }
  142. }
  143. static struct dfu_ctx dfu_ctx;
  144. void init_usb_bootloader( int config )
  145. {
  146. dfu_init( setup_read, setup_write, finish_write, &dfu_ctx );
  147. }
  148. void main()
  149. {
  150. #if defined(_mk20dx128vlf5_) // Kiibohd-dfu / Infinity
  151. // XXX McHCK uses B16 instead of A19
  152. // Enabling LED to indicate we are in the bootloader
  153. GPIOA_PDDR |= (1<<19);
  154. // Setup pin - A19 - See Lib/pin_map.mchck for more details on pins
  155. PORTA_PCR19 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  156. GPIOA_PSOR |= (1<<19);
  157. #elif defined(_mk20dx256vlh7_) // Kiibohd-dfu
  158. // Enabling LED to indicate we are in the bootloader
  159. GPIOA_PDDR |= (1<<5);
  160. // Setup pin - A5 - See Lib/pin_map.mchck for more details on pins
  161. PORTA_PCR5 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  162. GPIOA_PSOR |= (1<<5);
  163. // TODO Add CMake configuration for disabling
  164. // Set LCD backlight on ICED to Red
  165. GPIOC_PDDR |= (1<<1);
  166. PORTC_PCR1 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  167. GPIOC_PCOR |= (1<<1);
  168. #else
  169. #error "Incompatible chip for bootloader"
  170. #endif
  171. uart_serial_setup();
  172. printNL( NL "Bootloader DFU-Mode" );
  173. // Bootloader Enter Reasons
  174. print(" RCM_SRS0 - ");
  175. printHex( RCM_SRS0 & 0x60 );
  176. print( NL " RCM_SRS1 - ");
  177. printHex( RCM_SRS1 & 0x02 );
  178. print( NL " _app_rom - ");
  179. printHex( (uint32_t)_app_rom );
  180. print( NL " Soft Rst - " );
  181. printHex( memcmp( (uint8_t*)&VBAT, sys_reset_to_loader_magic, sizeof(sys_reset_to_loader_magic) ) == 0 );
  182. print( NL );
  183. #ifdef FLASH_DEBUG
  184. for ( uint8_t sector = 0; sector < 3; sector++ )
  185. sector_print( &_app_rom, sector, 16 );
  186. print( NL );
  187. #endif
  188. flash_prepare_flashing();
  189. usb_init( &dfu_device );
  190. for (;;)
  191. {
  192. usb_poll();
  193. }
  194. }