Skip to content

Commit a208924

Browse files
Fix FlashIAP test failure
1 parent f7d1aef commit a208924

File tree

3 files changed

+33
-14
lines changed

3 files changed

+33
-14
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ cmake_install.cmake
103103
CMakeFiles/
104104
cmake_build/
105105
Testing/
106+
cmake-variants.yaml
107+
CMakeUserPresets.json
106108

107109
# CLion
108110
cmake-build-*/

drivers/tests/TESTS/mbed_drivers/flashiap/main.cpp

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -249,11 +249,11 @@ void flashiap_timing_test()
249249
std::chrono::microseconds curr_time{};
250250
std::chrono::microseconds avg_erase_time{};
251251
unsigned int num_write_sizes;
252-
unsigned int byte_usec_ratio;
252+
float byte_sec_ratio;
253253
std::chrono::microseconds max_erase_time(0), min_erase_time(-1);
254254
const unsigned int max_writes = 128;
255255
const unsigned int max_write_sizes = 6;
256-
const unsigned int max_byte_usec_ratio = 200;
256+
const unsigned int max_byte_sec_ratio = 200000000;
257257

258258
uint32_t page_size = flash_device.get_page_size();
259259
uint32_t write_size = page_size;
@@ -294,28 +294,34 @@ void flashiap_timing_test()
294294
}
295295
timer.reset();
296296
ret = flash_device.program(buf, address, write_size);
297+
298+
if(ret)
299+
{
300+
printf("Failed programming %" PRIu32 " bytes at address 0x%" PRIx32 "\n!", write_size, address);
301+
TEST_FAIL();
302+
}
303+
297304
curr_time = timer.elapsed_time();
298305
avg_write_time += curr_time;
299-
TEST_ASSERT_EQUAL_INT32(0, ret);
300306
max_write_time = us_max(max_write_time, curr_time);
301307
min_write_time = us_min(min_write_time, curr_time);
302308
address += write_size;
303309
}
304310
delete[] buf;
305311
avg_write_time /= num_writes;
306-
byte_usec_ratio = write_size / avg_write_time.count();
307-
utest_printf("Write size %6u bytes: avg %10" PRIi64 ", min %10" PRIi64 ", max %10" PRIi64 " (usec), rate %10u bytes/usec\n",
308-
write_size, avg_write_time.count(), min_write_time.count(), max_write_time.count(), byte_usec_ratio);
309-
TEST_ASSERT(byte_usec_ratio < max_byte_usec_ratio);
312+
byte_sec_ratio = write_size / std::chrono::duration_cast<std::chrono::duration<float>>(avg_write_time).count();
313+
utest_printf("Write size %6u bytes: avg %10" PRIi64 ", min %10" PRIi64 ", max %10" PRIi64 " (usec), rate %.01f bytes/sec\n",
314+
write_size, avg_write_time.count(), min_write_time.count(), max_write_time.count(), byte_sec_ratio);
315+
TEST_ASSERT(byte_sec_ratio < max_byte_sec_ratio);
310316
write_size *= 4;
311317
}
312318

313319
if (num_write_sizes) {
314320
avg_erase_time /= num_write_sizes;
315-
byte_usec_ratio = sector_size / avg_erase_time.count();
316-
utest_printf("\nErase size %6u bytes: avg %10" PRIi64 ", min %10" PRIi64 ", max %10" PRIi64" (usec), rate %10u bytes/usec\n\n",
317-
sector_size, avg_erase_time.count(), min_erase_time.count(), max_erase_time.count(), byte_usec_ratio);
318-
TEST_ASSERT(byte_usec_ratio < max_byte_usec_ratio);
321+
byte_sec_ratio = sector_size / std::chrono::duration_cast<std::chrono::duration<float>>(avg_erase_time).count();
322+
utest_printf("\nErase size %6u bytes: avg %10" PRIi64 ", min %10" PRIi64 ", max %10" PRIi64" (usec), rate %.01f bytes/sec\n\n",
323+
sector_size, avg_erase_time.count(), min_erase_time.count(), max_erase_time.count(), byte_sec_ratio);
324+
TEST_ASSERT(byte_sec_ratio < max_byte_sec_ratio);
319325
}
320326

321327
ret = flash_device.deinit();

targets/TARGET_NXP/TARGET_LPC176X/device/flash_api.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#include "cmsis.h"
2323
#include <stdlib.h>
2424
#include <string.h>
25+
#include <stdio.h>
26+
#include <inttypes.h>
2527

2628
#define MEMMAP (*((volatile unsigned long *) 0x400FC040))
2729

@@ -122,10 +124,18 @@ int32_t flash_program_page(flash_t *obj, uint32_t address,
122124
// On LPC1768, the first RAM bank starts at 0x1000000, so anywhere below that has to be flash.
123125
// The IAP firmware does not support flash to flash copies, so if the source data is in flash
124126
// it must be buffered in RAM.
125-
bool isFlashToFlashCopy = (ptrdiff_t)(data) < 0x10000000;
127+
// Additionally, there seems to be an issue where the IAP ROM won't operate if a source page to be copied
128+
// crosses the boundary between AHBSRAM0 and AHBSRAM1
129+
const bool startsInAHBSRAM1 = ((uint32_t)data) >= MBED_CONFIGURED_RAM_BANK_IRAM2_START && ((uint32_t)data) < (MBED_CONFIGURED_RAM_BANK_IRAM2_START + 16384);
130+
const bool endsInAHBSRAM2 = startsInAHBSRAM1 && ((((uint32_t)data) + size) >= MBED_CONFIGURED_RAM_BANK_IRAM2_START + 16384);
131+
const bool sourceAddressPageAligned = (((uint32_t)data) % pageSize) == 0;
132+
bool mustBuffer = ((ptrdiff_t)(data) < MBED_CONFIGURED_RAM_BANK_IRAM1_START) || (startsInAHBSRAM1 && endsInAHBSRAM2 && !sourceAddressPageAligned);
133+
134+
// printf("Flash program: flash address = 0x%" PRIx32 ", source data = 0x%" PRIx32 ", size = 0x%" PRIx32 ", startsInAHBSRAM1 = %d, endsInAHBSRAM2=%d, mustBuffer=%d\n",
135+
// address, (ptrdiff_t)data, size, !!startsInAHBSRAM1, !!endsInAHBSRAM2, !!mustBuffer);
126136

127137
// check word boundary
128-
if (isFlashToFlashCopy) {
138+
if (mustBuffer) {
129139
// always malloc outside critical section
130140
tempBuffer = malloc(pageSize);
131141
if (tempBuffer == 0) {
@@ -140,7 +150,7 @@ int32_t flash_program_page(flash_t *obj, uint32_t address,
140150
for(size_t pageIdx = 0; pageIdx < (size / pageSize); ++pageIdx)
141151
{
142152
uint8_t * pageSourceAddr = source + (pageIdx * pageSize);
143-
if (isFlashToFlashCopy) {
153+
if (mustBuffer) {
144154
memcpy(tempBuffer, pageSourceAddr, pageSize);
145155
pageSourceAddr = tempBuffer;
146156
}
@@ -166,6 +176,7 @@ int32_t flash_program_page(flash_t *obj, uint32_t address,
166176
IAP_Call (&IAP.cmd, &IAP.stat); // Call IAP Command
167177
if (IAP.stat) {
168178
core_util_critical_section_exit();
179+
//printf("IAP copy failed, address=0x%lx, pageSourceAddr=0x%lx, pageSize=%u\n", address, pageSourceAddr, pageSize);
169180
return -1; // Command Failed
170181
}
171182

0 commit comments

Comments
 (0)