Skip to content

Commit 819ae95

Browse files
committed
QSPI: limit page-write loop to remaining bytes
F/221
1 parent 1bb35e9 commit 819ae95

3 files changed

Lines changed: 121 additions & 2 deletions

File tree

src/qspi_flash.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ int spi_flash_write(uint32_t address, const void *data, int len)
431431
ret = qspi_write_enable();
432432
if (ret == 0) {
433433
uint8_t* ptr;
434-
xferSz = len;
434+
xferSz = len - (page * FLASH_PAGE_SIZE);
435435
if (xferSz > FLASH_PAGE_SIZE)
436436
xferSz = FLASH_PAGE_SIZE;
437437

tools/unit-tests/Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ TESTS:=unit-parser unit-extflash unit-string unit-spi-flash unit-aes128 \
3838
unit-image unit-image-rsa unit-nvm unit-nvm-flagshome unit-enc-nvm \
3939
unit-enc-nvm-flagshome unit-delta unit-update-flash \
4040
unit-update-flash-enc unit-update-ram unit-pkcs11_store unit-psa_store unit-disk \
41-
unit-multiboot unit-boot-x86-fsp
41+
unit-multiboot unit-boot-x86-fsp unit-qspi-flash
4242

4343
all: $(TESTS)
4444

@@ -106,6 +106,9 @@ unit-extflash: ../../include/target.h unit-extflash.c
106106
unit-spi-flash: ../../include/target.h unit-spi-flash.c
107107
gcc -o $@ $^ $(CFLAGS) $(LDFLAGS)
108108

109+
unit-qspi-flash: ../../include/target.h unit-qspi-flash.c
110+
gcc -o $@ $^ $(CFLAGS) $(LDFLAGS)
111+
109112
unit-string: ../../include/target.h unit-string.c
110113
gcc -o $@ $^ $(CFLAGS) -DDEBUG_UART -DPRINTF_ENABLED $(LDFLAGS)
111114

tools/unit-tests/unit-qspi-flash.c

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/* unit-qspi-flash.c
2+
*
3+
* Unit tests for qspi_flash.c.
4+
*/
5+
6+
#define QSPI_FLASH
7+
8+
#include <check.h>
9+
#include <stdint.h>
10+
#include <string.h>
11+
12+
static int program_call_count;
13+
static uint32_t program_sizes[8];
14+
static const uint8_t *program_ptrs[8];
15+
static uint32_t program_addrs[8];
16+
17+
void spi_init(int polarity, int phase)
18+
{
19+
(void)polarity;
20+
(void)phase;
21+
}
22+
23+
void spi_release(void)
24+
{
25+
}
26+
27+
#include "../../src/qspi_flash.c"
28+
29+
int qspi_transfer(uint8_t fmode, const uint8_t cmd,
30+
uint32_t addr, uint32_t addrSz, uint32_t addrMode,
31+
uint32_t alt, uint32_t altSz, uint32_t altMode,
32+
uint32_t dummySz,
33+
uint8_t* data, uint32_t dataSz, uint32_t dataMode)
34+
{
35+
(void)fmode;
36+
(void)addrSz;
37+
(void)addrMode;
38+
(void)alt;
39+
(void)altSz;
40+
(void)altMode;
41+
(void)dummySz;
42+
(void)dataMode;
43+
44+
if (cmd == READ_SR_CMD) {
45+
ck_assert_ptr_nonnull(data);
46+
ck_assert_uint_ge(dataSz, 1);
47+
data[0] = FLASH_SR_WRITE_EN;
48+
return 0;
49+
}
50+
51+
if (cmd == FLASH_WRITE_CMD) {
52+
ck_assert_int_lt(program_call_count, (int)(sizeof(program_sizes) / sizeof(program_sizes[0])));
53+
program_sizes[program_call_count] = dataSz;
54+
program_ptrs[program_call_count] = data;
55+
program_addrs[program_call_count] = addr;
56+
program_call_count++;
57+
return 0;
58+
}
59+
60+
return 0;
61+
}
62+
63+
static void setup(void)
64+
{
65+
program_call_count = 0;
66+
memset(program_sizes, 0, sizeof(program_sizes));
67+
memset(program_ptrs, 0, sizeof(program_ptrs));
68+
memset(program_addrs, 0, sizeof(program_addrs));
69+
}
70+
71+
START_TEST(test_qspi_write_splits_last_page_to_remaining_bytes)
72+
{
73+
uint8_t buf[300];
74+
int ret;
75+
76+
memset(buf, 0xA5, sizeof(buf));
77+
78+
ret = spi_flash_write(0x1000, buf, sizeof(buf));
79+
80+
ck_assert_int_eq(ret, 0);
81+
ck_assert_int_eq(program_call_count, 2);
82+
ck_assert_uint_eq(program_sizes[0], FLASH_PAGE_SIZE);
83+
ck_assert_ptr_eq(program_ptrs[0], buf);
84+
ck_assert_uint_eq(program_addrs[0], 0x1000);
85+
ck_assert_uint_eq(program_sizes[1], sizeof(buf) - FLASH_PAGE_SIZE);
86+
ck_assert_ptr_eq(program_ptrs[1], buf + FLASH_PAGE_SIZE);
87+
ck_assert_uint_eq(program_addrs[1], 0x1000 + FLASH_PAGE_SIZE);
88+
}
89+
END_TEST
90+
91+
static Suite *qspi_flash_suite(void)
92+
{
93+
Suite *s;
94+
TCase *tc;
95+
96+
s = suite_create("QSPI Flash");
97+
tc = tcase_create("Write");
98+
tcase_add_checked_fixture(tc, setup, NULL);
99+
tcase_add_test(tc, test_qspi_write_splits_last_page_to_remaining_bytes);
100+
suite_add_tcase(s, tc);
101+
return s;
102+
}
103+
104+
int main(void)
105+
{
106+
Suite *s;
107+
SRunner *sr;
108+
int failed;
109+
110+
s = qspi_flash_suite();
111+
sr = srunner_create(s);
112+
srunner_run_all(sr, CK_NORMAL);
113+
failed = srunner_ntests_failed(sr);
114+
srunner_free(sr);
115+
return failed == 0 ? 0 : 1;
116+
}

0 commit comments

Comments
 (0)