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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. // ----- Variables -----
  22. /**
  23. * Unfortunately we can't DMA directly to FlexRAM, so we'll have to stage here.
  24. */
  25. static char staging[ FLASH_SECTOR_SIZE ];
  26. // ----- Functions -----
  27. static enum dfu_status setup_write( size_t off, size_t len, void **buf )
  28. {
  29. GPIOA_PCOR |= (1<<5);
  30. static int last = 0;
  31. if ( len > sizeof(staging) )
  32. return (DFU_STATUS_errADDRESS);
  33. // We only allow the last write to be less than one sector size.
  34. if ( off == 0 )
  35. last = 0;
  36. if ( last && len != 0 )
  37. return (DFU_STATUS_errADDRESS);
  38. if ( len != FLASH_SECTOR_SIZE )
  39. {
  40. last = 1;
  41. memset( staging, 0xff, sizeof(staging) );
  42. }
  43. *buf = staging;
  44. return (DFU_STATUS_OK);
  45. }
  46. static enum dfu_status finish_write( void *buf, size_t off, size_t len )
  47. {
  48. void *target;
  49. if ( len == 0 )
  50. return (DFU_STATUS_OK);
  51. target = flash_get_staging_area(off + (uintptr_t)&_app_rom, FLASH_SECTOR_SIZE);
  52. if ( !target )
  53. return (DFU_STATUS_errADDRESS);
  54. memcpy( target, buf, len );
  55. // Depending on the error return a different status
  56. switch ( flash_program_sector(off + (uintptr_t)&_app_rom, FLASH_SECTOR_SIZE) )
  57. {
  58. /*
  59. case FTFL_FSTAT_RDCOLERR: // Flash Read Collision Error
  60. case FTFL_FSTAT_ACCERR: // Flash Access Error
  61. case FTFL_FSTAT_FPVIOL: // Flash Protection Violation Error
  62. return (DFU_STATUS_errADDRESS);
  63. case FTFL_FSTAT_MGSTAT0: // Memory Controller Command Completion Error
  64. return (DFU_STATUS_errADDRESS);
  65. */
  66. case 0:
  67. default: // No error
  68. return (DFU_STATUS_OK);
  69. }
  70. }
  71. static struct dfu_ctx dfu_ctx;
  72. void init_usb_bootloader( int config )
  73. {
  74. dfu_init(setup_write, finish_write, &dfu_ctx);
  75. }
  76. void main()
  77. {
  78. #if defined(_mk20dx128vlf5_) // Kiibohd-dfu / Infinity
  79. // XXX McHCK uses B16 instead of A19
  80. // Enabling LED to indicate we are in the bootloader
  81. GPIOA_PDDR |= (1<<19);
  82. // Setup pin - A19 - See Lib/pin_map.mchck for more details on pins
  83. PORTA_PCR19 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  84. GPIOA_PSOR |= (1<<19);
  85. #elif defined(_mk20dx256vlh7_) // Kiibohd-dfu
  86. // Enabling LED to indicate we are in the bootloader
  87. GPIOA_PDDR |= (1<<5);
  88. // Setup pin - A5 - See Lib/pin_map.mchck for more details on pins
  89. PORTA_PCR5 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  90. GPIOA_PSOR |= (1<<5);
  91. #else
  92. #error "Incompatible chip for bootloader"
  93. #endif
  94. //for (uint8_t c = 0; c < 20; c++)
  95. /*
  96. while( 1 )
  97. {
  98. GPIOA_PTOR |= (1<<5);
  99. for (uint32_t d = 0; d < 7200000; d++ );
  100. }
  101. */
  102. // XXX REMOVEME
  103. /*
  104. GPIOB_PDDR |= (1<<16);
  105. PORTB_PCR16 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  106. GPIOB_PSOR |= (1<<16);
  107. */
  108. flash_prepare_flashing();
  109. uint32_t *position = &_app_rom;
  110. usb_init( &dfu_device );
  111. for (;;)
  112. {
  113. usb_poll();
  114. /*
  115. for ( ; position < &_app_rom + 0x201; position++ )
  116. //for ( ; position < &_app_rom + 0x800; position++ )
  117. {
  118. if ( *position != 0xFFFFFFFF )
  119. {
  120. while( 1 )
  121. {
  122. GPIOA_PTOR |= (1<<5);
  123. for (uint32_t d = 0; d < 7200000; d++ );
  124. }
  125. }
  126. }
  127. */
  128. }
  129. }