Skip to content

Commit 5fb9662

Browse files
0xc0170c1728p9
authored andcommitted
tests: add FlashIAP functional test
It currently tests functionality provided by flash IAP API like write/read/program/erase and getters.
1 parent c3951d8 commit 5fb9662

File tree

1 file changed

+201
-0
lines changed

1 file changed

+201
-0
lines changed

TESTS/mbed_drivers/flashiap/main.cpp

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
2+
/* mbed Microcontroller Library
3+
* Copyright (c) 2017 ARM Limited
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#if !DEVICE_FLASH
19+
#error [NOT_SUPPORTED] Flash API not supported for this target
20+
#endif
21+
22+
#include "utest/utest.h"
23+
#include "unity/unity.h"
24+
#include "greentea-client/test_env.h"
25+
26+
#include "mbed.h"
27+
28+
using namespace utest::v1;
29+
30+
void flashiap_init_test()
31+
{
32+
FlashIAP flash_device;
33+
uint32_t ret = flash_device.init();
34+
TEST_ASSERT_EQUAL_INT32(ret, 0);
35+
ret = flash_device.deinit();
36+
TEST_ASSERT_EQUAL_INT32(ret, 0);
37+
}
38+
39+
void flashiap_program_test()
40+
{
41+
FlashIAP flash_device;
42+
uint32_t ret = flash_device.init();
43+
TEST_ASSERT_EQUAL_INT32(ret, 0);
44+
45+
// get the last sector size (flash size - 1)
46+
uint32_t sector_size = flash_device.get_sector_size(flash_device.get_flash_size() - 1UL);
47+
TEST_ASSERT_NOT_EQUAL(sector_size, 0);
48+
const uint8_t test_value = 0xCE;
49+
uint8_t *data = new uint8_t[sector_size];
50+
for (uint32_t i = 0; i < sector_size; i++) {
51+
data[i] = test_value;
52+
}
53+
54+
// the one before the last page in the system
55+
uint32_t address = (flash_device.get_flash_size()) - (sector_size);
56+
TEST_ASSERT_TRUE(address != 0UL);
57+
ret = flash_device.erase(address, sector_size);
58+
TEST_ASSERT_EQUAL_INT32(ret, 0);
59+
60+
ret = flash_device.program(data, address, sector_size);
61+
TEST_ASSERT_EQUAL_INT32(ret, 0);
62+
63+
uint8_t *data_flashed = new uint8_t[sector_size];
64+
ret = flash_device.read(data_flashed, address, sector_size);
65+
TEST_ASSERT_EQUAL_INT32(ret, 0);
66+
TEST_ASSERT_EQUAL_UINT8_ARRAY(data, data_flashed, sector_size);
67+
delete[] data;
68+
delete[] data_flashed;
69+
70+
ret = flash_device.deinit();
71+
TEST_ASSERT_EQUAL_INT32(ret, 0);
72+
}
73+
74+
void flashiap_write_test()
75+
{
76+
FlashIAP flash_device;
77+
uint32_t ret = flash_device.init();
78+
TEST_ASSERT_EQUAL_INT32(ret, 0);
79+
80+
// get the last sector size
81+
uint32_t sector_size = flash_device.get_sector_size(flash_device.get_flash_size() - 1UL);
82+
TEST_ASSERT_NOT_EQUAL(sector_size, 0);
83+
const uint8_t test_value = 0xCE;
84+
uint8_t *data = new uint8_t[sector_size];
85+
for (uint32_t i = 0; i < sector_size; i++) {
86+
data[i] = test_value;
87+
}
88+
89+
// the one before the last page in the system
90+
uint32_t address = (flash_device.get_flash_size()) - (sector_size);
91+
TEST_ASSERT_TRUE(address != 0UL);
92+
ret = flash_device.write(data, address, sector_size);
93+
TEST_ASSERT_EQUAL_INT32(ret, 0);
94+
95+
uint8_t *data_flashed = new uint8_t[sector_size];
96+
ret = flash_device.read(data_flashed, address, sector_size);
97+
TEST_ASSERT_EQUAL_INT32(ret, 0);
98+
TEST_ASSERT_EQUAL_UINT8_ARRAY(data, data_flashed, sector_size);
99+
delete[] data;
100+
delete[] data_flashed;
101+
102+
ret = flash_device.deinit();
103+
TEST_ASSERT_EQUAL_INT32(ret, 0);
104+
}
105+
106+
void flashiap_program_error_test()
107+
{
108+
FlashIAP flash_device;
109+
uint32_t ret = flash_device.init();
110+
TEST_ASSERT_EQUAL_INT32(ret, 0);
111+
112+
// get the last sector size (flash size - 1)
113+
uint32_t sector_size = flash_device.get_sector_size(flash_device.get_flash_size() - 1UL);
114+
TEST_ASSERT_NOT_EQUAL(sector_size, 0);
115+
const uint8_t test_value = 0xCE;
116+
uint8_t *data = new uint8_t[sector_size];
117+
for (uint32_t i = 0; i < sector_size; i++) {
118+
data[i] = test_value;
119+
}
120+
121+
// the one before the last page in the system
122+
uint32_t address = (flash_device.get_flash_size()) - (sector_size);
123+
TEST_ASSERT_TRUE(address != 0UL);
124+
125+
// unaligned address
126+
address += 1;
127+
ret = flash_device.erase(address, sector_size);
128+
TEST_ASSERT_EQUAL_INT32(ret, -1);
129+
ret = flash_device.program(data, address, sector_size);
130+
TEST_ASSERT_EQUAL_INT32(ret, -1);
131+
132+
// unaligned sector size
133+
sector_size += 1;
134+
ret = flash_device.program(data, address, sector_size);
135+
TEST_ASSERT_EQUAL_INT32(ret, -1);
136+
137+
delete[] data;
138+
139+
ret = flash_device.deinit();
140+
TEST_ASSERT_EQUAL_INT32(ret, 0);
141+
}
142+
143+
void flashiap_write_error_test()
144+
{
145+
FlashIAP flash_device;
146+
uint32_t ret = flash_device.init();
147+
TEST_ASSERT_EQUAL_INT32(ret, 0);
148+
149+
// get the last sector size
150+
uint32_t sector_size = flash_device.get_sector_size(flash_device.get_flash_size() - 1UL);
151+
TEST_ASSERT_NOT_EQUAL(sector_size, 0);
152+
const uint8_t test_value = 0xCE;
153+
uint8_t *data = new uint8_t[sector_size];
154+
for (uint32_t i = 0; i < sector_size; i++) {
155+
data[i] = test_value;
156+
}
157+
158+
// the one before the last page in the system
159+
uint32_t address = (flash_device.get_flash_size()) - (sector_size);
160+
TEST_ASSERT_TRUE(address != 0UL);
161+
162+
// unaligned address
163+
address += 1;
164+
ret = flash_device.write(data, address, sector_size);
165+
TEST_ASSERT_EQUAL_INT32(ret, -1);
166+
167+
// unaligned sector size
168+
address -= 1;
169+
sector_size += 1;
170+
ret = flash_device.write(data, address, sector_size);
171+
TEST_ASSERT_EQUAL_INT32(ret, -1);
172+
173+
delete[] data;
174+
175+
ret = flash_device.deinit();
176+
TEST_ASSERT_EQUAL_INT32(ret, 0);
177+
}
178+
179+
utest::v1::status_t greentea_failure_handler(const Case *const source, const failure_t reason) {
180+
greentea_case_failure_abort_handler(source, reason);
181+
return STATUS_CONTINUE;
182+
}
183+
184+
Case cases[] = {
185+
Case("FlashIAP - init", flashiap_init_test, greentea_failure_handler),
186+
Case("FlashIAP - program", flashiap_program_test, greentea_failure_handler),
187+
Case("FlashIAP - write", flashiap_write_test, greentea_failure_handler),
188+
Case("FlashIAP - program errors", flashiap_program_error_test, greentea_failure_handler),
189+
Case("FlashIAP - write errors", flashiap_write_error_test, greentea_failure_handler),
190+
};
191+
192+
utest::v1::status_t greentea_test_setup(const size_t number_of_cases) {
193+
GREENTEA_SETUP(20, "default_auto");
194+
return greentea_test_setup_handler(number_of_cases);
195+
}
196+
197+
Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler);
198+
199+
int main() {
200+
Harness::run(specification);
201+
}

0 commit comments

Comments
 (0)