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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 + off;
  74. // Calculate length of transfer
  75. *len = *buf + USB_DFU_TRANSFER_SIZE > (void*)(&_app_rom_end)
  76. ? (void*)(&_app_rom_end) - *buf + 1
  77. : USB_DFU_TRANSFER_SIZE;
  78. return (DFU_STATUS_OK);
  79. }
  80. static enum dfu_status setup_write( size_t off, size_t len, void **buf )
  81. {
  82. static int last = 0;
  83. #ifdef FLASH_DEBUG
  84. // Debug
  85. print("Setup Write: offset(");
  86. printHex( off );
  87. print(") len(");
  88. printHex( len );
  89. print(") last(");
  90. printHex( last );
  91. printNL(")");
  92. #endif
  93. if ( len > sizeof(staging) )
  94. return (DFU_STATUS_errADDRESS);
  95. // We only allow the last write to be less than one sector size.
  96. if ( off == 0 )
  97. last = 0;
  98. if ( last && len != 0 )
  99. return (DFU_STATUS_errADDRESS);
  100. if ( len != USB_DFU_TRANSFER_SIZE )
  101. {
  102. last = 1;
  103. memset( staging, 0xff, sizeof(staging) );
  104. }
  105. *buf = staging;
  106. return (DFU_STATUS_OK);
  107. }
  108. static enum dfu_status finish_write( void *buf, size_t off, size_t len )
  109. {
  110. void *target;
  111. // If nothing left to flash, this is still ok
  112. if ( len == 0 )
  113. return (DFU_STATUS_OK);
  114. // If the binary is larger than the internal flash, error
  115. if ( off + (uintptr_t)&_app_rom + len > (uintptr_t)&_app_rom_end )
  116. return (DFU_STATUS_errADDRESS);
  117. target = flash_get_staging_area( off + (uintptr_t)&_app_rom, USB_DFU_TRANSFER_SIZE );
  118. if ( !target )
  119. return (DFU_STATUS_errADDRESS);
  120. memcpy( target, buf, len );
  121. // Depending on the error return a different status
  122. switch ( flash_program_sector( off + (uintptr_t)&_app_rom, USB_DFU_TRANSFER_SIZE ) )
  123. {
  124. /*
  125. case FTFL_FSTAT_RDCOLERR: // Flash Read Collision Error
  126. case FTFL_FSTAT_ACCERR: // Flash Access Error
  127. case FTFL_FSTAT_FPVIOL: // Flash Protection Violation Error
  128. return (DFU_STATUS_errADDRESS);
  129. case FTFL_FSTAT_MGSTAT0: // Memory Controller Command Completion Error
  130. return (DFU_STATUS_errADDRESS);
  131. */
  132. case 0:
  133. default: // No error
  134. return (DFU_STATUS_OK);
  135. }
  136. }
  137. static struct dfu_ctx dfu_ctx;
  138. void init_usb_bootloader( int config )
  139. {
  140. dfu_init( setup_read, setup_write, finish_write, &dfu_ctx );
  141. }
  142. void main()
  143. {
  144. #if defined(_mk20dx128vlf5_) // Kiibohd-dfu / Infinity
  145. // XXX McHCK uses B16 instead of A19
  146. // Enabling LED to indicate we are in the bootloader
  147. GPIOA_PDDR |= (1<<19);
  148. // Setup pin - A19 - See Lib/pin_map.mchck for more details on pins
  149. PORTA_PCR19 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  150. GPIOA_PSOR |= (1<<19);
  151. #elif defined(_mk20dx256vlh7_) // Kiibohd-dfu
  152. // Enabling LED to indicate we are in the bootloader
  153. GPIOA_PDDR |= (1<<5);
  154. // Setup pin - A5 - See Lib/pin_map.mchck for more details on pins
  155. PORTA_PCR5 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  156. GPIOA_PSOR |= (1<<5);
  157. // TODO Add CMake configuration for disabling
  158. // Set LCD backlight on ICED to Red
  159. GPIOC_PDDR |= (1<<1);
  160. PORTC_PCR1 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  161. GPIOC_PCOR |= (1<<1);
  162. #else
  163. #error "Incompatible chip for bootloader"
  164. #endif
  165. uart_serial_setup();
  166. printNL( NL "Bootloader DFU-Mode" );
  167. // Bootloader Enter Reasons
  168. print(" RCM_SRS0 - ");
  169. printHex( RCM_SRS0 & 0x60 );
  170. print( NL " RCM_SRS1 - ");
  171. printHex( RCM_SRS1 & 0x02 );
  172. print( NL " _app_rom - ");
  173. printHex( (uint32_t)_app_rom );
  174. print( NL " Soft Rst - " );
  175. printHex( memcmp( (uint8_t*)&VBAT, sys_reset_to_loader_magic, sizeof(sys_reset_to_loader_magic) ) == 0 );
  176. print( NL );
  177. #ifdef FLASH_DEBUG
  178. for ( uint8_t sector = 0; sector < 3; sector++ )
  179. sector_print( &_app_rom, sector, 16 );
  180. print( NL );
  181. #endif
  182. flash_prepare_flashing();
  183. usb_init( &dfu_device );
  184. for (;;)
  185. {
  186. usb_poll();
  187. }
  188. }