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.

flash.c 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. // ----- Local Includes -----
  18. #include "mchck.h"
  19. #include "debug.h"
  20. // ----- Variables -----
  21. uint32_t flash_ALLOW_BRICKABLE_ADDRESSES;
  22. // ----- Functions -----
  23. /* This will have to live in SRAM. */
  24. __attribute__((section(".ramtext.ftfl_submit_cmd"), long_call))
  25. int ftfl_submit_cmd()
  26. {
  27. FTFL.fstat.raw = ((struct FTFL_FSTAT_t){
  28. .ccif = 1,
  29. //.rdcolerr = 1,
  30. .accerr = 1,
  31. .fpviol = 1
  32. }).raw;
  33. // Wait for the operation to complete
  34. struct FTFL_FSTAT_t stat;
  35. while (!(stat = FTFL.fstat).ccif); // XXX maybe WFI?
  36. // Mask error bits
  37. return stat.raw & (FTFL_FSTAT_RDCOLERR | FTFL_FSTAT_ACCERR | FTFL_FSTAT_FPVIOL | FTFL_FSTAT_MGSTAT0);
  38. //return (!!stat.mgstat0);
  39. }
  40. int flash_prepare_flashing()
  41. {
  42. /* switch to FlexRAM */
  43. if ( !FTFL.fcnfg.ramrdy )
  44. {
  45. FTFL.fccob.set_flexram.fcmd = FTFL_FCMD_SET_FLEXRAM;
  46. FTFL.fccob.set_flexram.flexram_function = FTFL_FLEXRAM_RAM;
  47. return (ftfl_submit_cmd());
  48. }
  49. return (0);
  50. }
  51. int flash_read_1s_sector( uintptr_t addr, size_t num )
  52. {
  53. FTFL.fccob.read_1s_section.fcmd = FTFL_FCMD_READ_1s_SECTION;
  54. FTFL.fccob.read_1s_section.addr = addr;
  55. FTFL.fccob.read_1s_section.margin = FTFL_MARGIN_NORMAL;
  56. FTFL.fccob.read_1s_section.num_words = num;
  57. return ftfl_submit_cmd();
  58. }
  59. int flash_erase_sector( uintptr_t addr )
  60. {
  61. #ifdef FLASH_DEBUG
  62. // Debug
  63. print("Erasing Sector: address(");
  64. printHex( addr );
  65. printNL(")");
  66. #endif
  67. if ( addr < (uintptr_t)&_app_rom && flash_ALLOW_BRICKABLE_ADDRESSES != 0x00023420 )
  68. return (-1);
  69. FTFL.fccob.erase.fcmd = FTFL_FCMD_ERASE_SECTOR;
  70. FTFL.fccob.erase.addr = addr;
  71. return ftfl_submit_cmd();
  72. }
  73. int flash_program_section_longwords( uintptr_t addr, size_t num_words )
  74. {
  75. #ifdef FLASH_DEBUG
  76. // Debug
  77. print("Programming Sector: address(");
  78. printHex( addr );
  79. print(") longwords(");
  80. printHex( num_words );
  81. printNL(")");
  82. #endif
  83. FTFL.fccob.program_section.fcmd = FTFL_FCMD_PROGRAM_SECTION;
  84. FTFL.fccob.program_section.addr = addr;
  85. FTFL.fccob.program_section.num_words = num_words;
  86. return ftfl_submit_cmd();
  87. }
  88. int flash_program_section_phrases( uintptr_t addr, size_t num_phrases )
  89. {
  90. #ifdef FLASH_DEBUG
  91. // Debug
  92. print("Programming Sector: address(");
  93. printHex( addr );
  94. print(") phrases(");
  95. printHex( num_phrases );
  96. printNL(")");
  97. #endif
  98. FTFL.fccob.program_section.fcmd = FTFL_FCMD_PROGRAM_SECTION;
  99. FTFL.fccob.program_section.addr = addr;
  100. FTFL.fccob.program_section.num_words = num_phrases;
  101. return ftfl_submit_cmd();
  102. }
  103. int flash_program_sector( uintptr_t addr, size_t len )
  104. {
  105. if ( len != USB_DFU_TRANSFER_SIZE )
  106. return 1;
  107. #if defined(_mk20dx128vlf5_)
  108. // Check if this is the beginning of a sector
  109. // Only erase if necessary
  110. if ( (addr & (FLASH_SECTOR_SIZE - 1)) == 0
  111. && flash_read_1s_sector( addr, FLASH_SECTOR_SIZE / 4 )
  112. && flash_erase_sector( addr ) )
  113. return 1;
  114. // Program sector
  115. return flash_program_section_longwords( addr, FLASH_SECTOR_SIZE / 4 );
  116. #elif defined(_mk20dx256vlh7_)
  117. // Check if beginning of sector and erase if not empty
  118. // Each sector is 2 kB in length, but we can only write to half a sector at a time
  119. // We can only erase an entire sector at a time
  120. if ( (addr & (FLASH_SECTOR_SIZE - 1)) == 0
  121. && flash_read_1s_sector( addr, FLASH_SECTOR_SIZE / 8 )
  122. && flash_erase_sector( addr ) )
  123. return 1;
  124. // Program half-sector
  125. return flash_program_section_phrases( addr, FLASH_SECTOR_SIZE / 16 );
  126. #endif
  127. }
  128. int flash_prepare_reading()
  129. {
  130. return (0);
  131. }
  132. int flash_read_sector( uintptr_t addr, size_t len )
  133. {
  134. return (0);
  135. }
  136. void *flash_get_staging_area( uintptr_t addr, size_t len )
  137. {
  138. if ( (addr & (USB_DFU_TRANSFER_SIZE - 1)) != 0 || len != USB_DFU_TRANSFER_SIZE )
  139. return (NULL);
  140. return (FlexRAM);
  141. }