| 109 | } |
| 110 | |
| 111 | int flash_program_sector( uintptr_t addr, size_t len ) |
| 112 | { |
| 113 | if ( len != USB_DFU_TRANSFER_SIZE ) |
| 114 | return 1; |
| 115 | |
| 116 | #if defined(_mk20dx128vlf5_) || defined(_mk20dx128vlh7_) |
| 117 | // Check if this is the beginning of a sector |
| 118 | // Only erase if necessary |
| 119 | if ( (addr & (FLASH_SECTOR_SIZE - 1)) == 0 |
| 120 | && flash_read_1s_sector( addr, FLASH_SECTOR_SIZE / 4 ) |
| 121 | && flash_erase_sector( addr ) ) |
| 122 | return 1; |
| 123 | |
| 124 | // Program sector (longword) |
| 125 | return flash_program_section( addr, FLASH_SECTOR_SIZE / 4 ); |
| 126 | #elif defined(_mk20dx256vlh7_) |
| 127 | // Check if beginning of sector and erase if not empty |
| 128 | // Each sector is 2 kB in length, but we can only write to half a sector at a time |
| 129 | // We can only erase an entire sector at a time |
| 130 | if ( (addr & (FLASH_SECTOR_SIZE - 1)) == 0 |
| 131 | && flash_read_1s_sector( addr, FLASH_SECTOR_SIZE / 8 ) |
| 132 | && flash_erase_sector( addr ) ) |
| 133 | return 1; |
| 134 | |
| 135 | // Program half-sector (phrases) |
| 136 | return flash_program_section( addr, FLASH_SECTOR_SIZE / 16 ); |
| 137 | #elif defined(_mk22fx512avlh12_) |
| 138 | // Check if beginning of sector and erase if not empty |
| 139 | // Each sector is 4 kB in length, but we can only write to half a sector at a time |
| 140 | // We can only erase an entire sector at a time |
| 141 | if ( (addr & (FLASH_SECTOR_SIZE - 1)) == 0 |
| 142 | && flash_read_1s_sector( addr, FLASH_SECTOR_SIZE / 16 ) |
| 143 | && flash_erase_sector( addr ) ) |
| 144 | return 1; |
| 145 | |
| 146 | // Program half-sector (double phrases) |
| 147 | return flash_program_section( addr, FLASH_SECTOR_SIZE / 32 ); |
| 148 | #endif |
| 149 | } |
| 150 | |
| 151 | int flash_prepare_reading() |
| 152 | { |
no test coverage detected
searching dependent graphs…