Skip to content

Commit fa68774

Browse files
committed
spi_flash:
1. add test code for new api 2. resolve some comments
1 parent 7555031 commit fa68774

File tree

4 files changed

+63
-15
lines changed

4 files changed

+63
-15
lines changed

components/bootloader_support/src/esp_image_format.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ static const char *TAG = "esp_image";
3434
/* Headroom to ensure between stack SP (at time of checking) and data loaded from flash */
3535
#define STACK_LOAD_HEADROOM 32768
3636

37+
/* Mmap source address mask */
38+
#define MMAP_ALIGNED_MASK 0x0000FFFF
39+
3740
#ifdef BOOTLOADER_BUILD
3841
/* 64 bits of random data to obfuscate loaded RAM with, until verification is complete
3942
(Means loaded code isn't executable until after the secure boot check.)
@@ -296,15 +299,17 @@ static esp_err_t process_segment(int index, uint32_t flash_addr, esp_image_segme
296299
}
297300
}
298301
#ifndef BOOTLOADER_BUILD
299-
uint32_t page_count = spi_flash_mmap_get_free_pages(SPI_FLASH_MMAP_DATA);
300-
ESP_LOGI(TAG, "free data page_count 0x%08x",page_count);
301-
while (data_len >= page_count * SPI_FLASH_MMU_PAGE_SIZE) {
302-
err = process_segment_data(load_addr, data_addr, page_count * SPI_FLASH_MMU_PAGE_SIZE, do_load, sha_handle, checksum);
302+
uint32_t free_page_count = spi_flash_mmap_get_free_pages(SPI_FLASH_MMAP_DATA);
303+
ESP_LOGD(TAG, "free data page_count 0x%08x",free_page_count);
304+
uint32_t offset_page = 0;
305+
while (data_len >= free_page_count * SPI_FLASH_MMU_PAGE_SIZE) {
306+
offset_page = ((data_addr & MMAP_ALIGNED_MASK) != 0)?1:0;
307+
err = process_segment_data(load_addr, data_addr, (free_page_count - offset_page) * SPI_FLASH_MMU_PAGE_SIZE, do_load, sha_handle, checksum);
303308
if (err != ESP_OK) {
304309
return err;
305310
}
306-
data_addr += page_count * SPI_FLASH_MMU_PAGE_SIZE;
307-
data_len -= page_count * SPI_FLASH_MMU_PAGE_SIZE;
311+
data_addr += (free_page_count - offset_page) * SPI_FLASH_MMU_PAGE_SIZE;
312+
data_len -= (free_page_count - offset_page) * SPI_FLASH_MMU_PAGE_SIZE;
308313
}
309314
#endif
310315
err = process_segment_data(load_addr, data_addr, data_len, do_load, sha_handle, checksum);

components/spi_flash/flash_mmap.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,10 @@ esp_err_t IRAM_ATTR spi_flash_mmap_pages(int *pages, size_t page_count, spi_flas
180180
// Algorithm is essentially naïve strstr algorithm, except that unused MMU
181181
// entries are treated as wildcards.
182182
int start;
183-
int end = region_begin + region_size - page_count;
184-
for (start = region_begin; start <= end; ++start) {
183+
// the " + 1" is a fix when loop the MMU table pages, because the last MMU page
184+
// is valid as well if it have not been used
185+
int end = region_begin + region_size - page_count + 1;
186+
for (start = region_begin; start < end; ++start) {
185187
int pageno = 0;
186188
int pos;
187189
DPORT_STALL_OTHER_CPU_START();
@@ -199,7 +201,7 @@ esp_err_t IRAM_ATTR spi_flash_mmap_pages(int *pages, size_t page_count, spi_flas
199201
}
200202
}
201203
// checked all the region(s) and haven't found anything?
202-
if (start > end) {
204+
if (start == end) {
203205
*out_handle = 0;
204206
*out_ptr = NULL;
205207
ret = ESP_ERR_NO_MEM;
@@ -312,11 +314,7 @@ uint32_t spi_flash_mmap_get_free_pages(spi_flash_mmap_memory_t memory)
312314
}
313315
}
314316
DPORT_STALL_OTHER_CPU_END();
315-
if(count >= 1) {
316-
return count - 1; //don't sure mmap src_addr,if src_addr not align 64K bytes,max need one more pages
317-
} else {
318-
return 0;
319-
}
317+
return count;
320318
}
321319

322320
/* 256-bit (up to 16MB of 64KB pages) bitset of all flash pages

components/spi_flash/include/esp_spi_flash.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ void spi_flash_mmap_dump();
239239
/**
240240
* @brief get free pages number which can be mmap
241241
*
242-
* @note This function will return free page number of the mmu table which can mmap,
242+
* This function will return free page number of the mmu table which can mmap,
243243
* when you want to call spi_flash_mmap to mmap an ranger of flash data to Dcache or Icache
244244
* memmory region, maybe the size of MMU table will exceed,so if you are not sure the
245245
* size need mmap is ok, can call the interface and watch how many MMU table page can be

components/spi_flash/test/test_mmap.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,51 @@ TEST_CASE("flash_mmap invalidates just-written data", "[spi_flash]")
276276
handle1 = 0;
277277
}
278278

279+
TEST_CASE("flash_mmap can mmap after get enough free MMU pages", "[spi_flash]")
280+
{
281+
//this test case should make flash size >= 4MB, because max size of Dcache can mapped is 4MB
282+
setup_mmap_tests();
283+
284+
printf("Mapping %x (+%x)\n", start, end - start);
285+
const void *ptr1;
286+
ESP_ERROR_CHECK( spi_flash_mmap(start, end - start, SPI_FLASH_MMAP_DATA, &ptr1, &handle1) );
287+
printf("mmap_res: handle=%d ptr=%p\n", handle1, ptr1);
288+
289+
spi_flash_mmap_dump();
290+
291+
srand(0);
292+
const uint32_t *data = (const uint32_t *) ptr1;
293+
for (int block = 0; block < (end - start) / 0x10000; ++block) {
294+
printf("block %d\n", block);
295+
for (int sector = 0; sector < 16; ++sector) {
296+
printf("sector %d\n", sector);
297+
for (uint32_t word = 0; word < 1024; ++word) {
298+
TEST_ASSERT_EQUAL_HEX32(rand(), data[(block * 16 + sector) * 1024 + word]);
299+
}
300+
}
301+
}
302+
uint32_t free_pages = spi_flash_mmap_get_free_pages(SPI_FLASH_MMAP_DATA);
303+
printf("Mapping %x (+%x)\n", 0, free_pages * SPI_FLASH_MMU_PAGE_SIZE);
304+
const void *ptr2;
305+
ESP_ERROR_CHECK( spi_flash_mmap(0, free_pages * SPI_FLASH_MMU_PAGE_SIZE, SPI_FLASH_MMAP_DATA, &ptr2, &handle2) );
306+
printf("mmap_res: handle=%d ptr=%p\n", handle2, ptr2);
307+
308+
spi_flash_mmap_dump();
309+
310+
311+
printf("Unmapping handle1\n");
312+
spi_flash_munmap(handle1);
313+
handle1 = 0;
314+
spi_flash_mmap_dump();
315+
316+
printf("Unmapping handle2\n");
317+
spi_flash_munmap(handle2);
318+
handle2 = 0;
319+
spi_flash_mmap_dump();
320+
321+
TEST_ASSERT_EQUAL_PTR(NULL, spi_flash_phys2cache(start, SPI_FLASH_MMAP_DATA));
322+
}
323+
279324
TEST_CASE("phys2cache/cache2phys basic checks", "[spi_flash]")
280325
{
281326
uint8_t buf[64];

0 commit comments

Comments
 (0)