Skip to content

Feature: Flash IAP c++ class addition #3700

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
201 changes: 201 additions & 0 deletions TESTS/mbed_drivers/flashiap/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@

/* mbed Microcontroller Library
* Copyright (c) 2017 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#if !DEVICE_FLASH
#error [NOT_SUPPORTED] Flash API not supported for this target
#endif

#include "utest/utest.h"
#include "unity/unity.h"
#include "greentea-client/test_env.h"

#include "mbed.h"

using namespace utest::v1;

void flashiap_init_test()
{
FlashIAP flash_device;
uint32_t ret = flash_device.init();
TEST_ASSERT_EQUAL_INT32(ret, 0);
ret = flash_device.deinit();
TEST_ASSERT_EQUAL_INT32(ret, 0);
}

void flashiap_program_test()
{
FlashIAP flash_device;
uint32_t ret = flash_device.init();
TEST_ASSERT_EQUAL_INT32(ret, 0);

// get the last sector size (flash size - 1)
uint32_t sector_size = flash_device.get_sector_size(flash_device.get_flash_start() + flash_device.get_flash_size() - 1UL);
TEST_ASSERT_NOT_EQUAL(sector_size, 0);
const uint8_t test_value = 0xCE;
uint8_t *data = new uint8_t[sector_size];
for (uint32_t i = 0; i < sector_size; i++) {
data[i] = test_value;
}

// the one before the last page in the system
uint32_t address = (flash_device.get_flash_start() + flash_device.get_flash_size()) - (sector_size);
TEST_ASSERT_TRUE(address != 0UL);
ret = flash_device.erase(address, sector_size);
TEST_ASSERT_EQUAL_INT32(ret, 0);

ret = flash_device.program(data, address, sector_size);
TEST_ASSERT_EQUAL_INT32(ret, 0);

uint8_t *data_flashed = new uint8_t[sector_size];
ret = flash_device.read(data_flashed, address, sector_size);
TEST_ASSERT_EQUAL_INT32(ret, 0);
TEST_ASSERT_EQUAL_UINT8_ARRAY(data, data_flashed, sector_size);
delete[] data;
delete[] data_flashed;

ret = flash_device.deinit();
TEST_ASSERT_EQUAL_INT32(ret, 0);
}

void flashiap_write_test()
{
FlashIAP flash_device;
uint32_t ret = flash_device.init();
TEST_ASSERT_EQUAL_INT32(ret, 0);

// get the last sector size
uint32_t sector_size = flash_device.get_sector_size(flash_device.get_flash_start() + flash_device.get_flash_size() - 1UL);
TEST_ASSERT_NOT_EQUAL(sector_size, 0);
const uint8_t test_value = 0xCE;
uint8_t *data = new uint8_t[sector_size];
for (uint32_t i = 0; i < sector_size; i++) {
data[i] = test_value;
}

// the one before the last page in the system
uint32_t address = (flash_device.get_flash_start() + flash_device.get_flash_size()) - (sector_size);
TEST_ASSERT_TRUE(address != 0UL);
ret = flash_device.write(data, address, sector_size);
TEST_ASSERT_EQUAL_INT32(ret, 0);

uint8_t *data_flashed = new uint8_t[sector_size];
ret = flash_device.read(data_flashed, address, sector_size);
TEST_ASSERT_EQUAL_INT32(ret, 0);
TEST_ASSERT_EQUAL_UINT8_ARRAY(data, data_flashed, sector_size);
delete[] data;
delete[] data_flashed;

ret = flash_device.deinit();
TEST_ASSERT_EQUAL_INT32(ret, 0);
}

void flashiap_program_error_test()
{
FlashIAP flash_device;
uint32_t ret = flash_device.init();
TEST_ASSERT_EQUAL_INT32(ret, 0);

// get the last sector size (flash size - 1)
uint32_t sector_size = flash_device.get_sector_size(flash_device.get_flash_start() + flash_device.get_flash_size() - 1UL);
TEST_ASSERT_NOT_EQUAL(sector_size, 0);
const uint8_t test_value = 0xCE;
uint8_t *data = new uint8_t[sector_size];
for (uint32_t i = 0; i < sector_size; i++) {
data[i] = test_value;
}

// the one before the last page in the system
uint32_t address = (flash_device.get_flash_start() + flash_device.get_flash_size()) - (sector_size);
TEST_ASSERT_TRUE(address != 0UL);

// unaligned address
address += 1;
ret = flash_device.erase(address, sector_size);
TEST_ASSERT_EQUAL_INT32(ret, -1);
ret = flash_device.program(data, address, sector_size);
TEST_ASSERT_EQUAL_INT32(ret, -1);

// unaligned sector size
sector_size += 1;
ret = flash_device.program(data, address, sector_size);
TEST_ASSERT_EQUAL_INT32(ret, -1);

delete[] data;

ret = flash_device.deinit();
TEST_ASSERT_EQUAL_INT32(ret, 0);
}

void flashiap_write_error_test()
{
FlashIAP flash_device;
uint32_t ret = flash_device.init();
TEST_ASSERT_EQUAL_INT32(ret, 0);

// get the last sector size
uint32_t sector_size = flash_device.get_sector_size(flash_device.get_flash_start() + flash_device.get_flash_size() - 1UL);
TEST_ASSERT_NOT_EQUAL(sector_size, 0);
const uint8_t test_value = 0xCE;
uint8_t *data = new uint8_t[sector_size];
for (uint32_t i = 0; i < sector_size; i++) {
data[i] = test_value;
}

// the one before the last page in the system
uint32_t address = (flash_device.get_flash_start() + flash_device.get_flash_size()) - (sector_size);
TEST_ASSERT_TRUE(address != 0UL);

// unaligned address
address += 1;
ret = flash_device.write(data, address, sector_size);
TEST_ASSERT_EQUAL_INT32(ret, -1);

// unaligned sector size
address -= 1;
sector_size += 1;
ret = flash_device.write(data, address, sector_size);
TEST_ASSERT_EQUAL_INT32(ret, -1);

delete[] data;

ret = flash_device.deinit();
TEST_ASSERT_EQUAL_INT32(ret, 0);
}

utest::v1::status_t greentea_failure_handler(const Case *const source, const failure_t reason) {
greentea_case_failure_abort_handler(source, reason);
return STATUS_CONTINUE;
}

Case cases[] = {
Case("FlashIAP - init", flashiap_init_test, greentea_failure_handler),
Case("FlashIAP - program", flashiap_program_test, greentea_failure_handler),
Case("FlashIAP - write", flashiap_write_test, greentea_failure_handler),
Case("FlashIAP - program errors", flashiap_program_error_test, greentea_failure_handler),
Case("FlashIAP - write errors", flashiap_write_error_test, greentea_failure_handler),
};

utest::v1::status_t greentea_test_setup(const size_t number_of_cases) {
GREENTEA_SETUP(20, "default_auto");
return greentea_test_setup_handler(number_of_cases);
}

Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler);

int main() {
Harness::run(specification);
}
8 changes: 4 additions & 4 deletions TESTS/mbed_hal/flash/functional_tests/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ void flash_program_page_test()
}

// the one before the last page in the system
uint32_t address = (test_flash.target_config->flash_start + test_flash.target_config->flash_size) - (2*test_size);
uint32_t address = flash_get_start_address(&test_flash) + flash_get_size(&test_flash) - (2*test_size);

// sector size might not be same as page size
uint32_t erase_sector_boundary = ALIGN_DOWN(address, flash_get_sector_size(&test_flash, address));
Expand Down Expand Up @@ -97,7 +97,7 @@ void flash_erase_sector_test()
TEST_ASSERT_EQUAL_INT32(ret, 0);

uint32_t sector_size = 0x1000;
uint32_t address = (test_flash.target_config->flash_start + test_flash.target_config->flash_size) - (4*sector_size);
uint32_t address = flash_get_start_address(&test_flash) + flash_get_size(&test_flash) - (4*sector_size);
// sector size might not be same as page size
uint32_t erase_sector_boundary = ALIGN_DOWN(address, flash_get_sector_size(&test_flash, address));
ret = flash_erase_sector(&test_flash, erase_sector_boundary);
Expand All @@ -115,7 +115,7 @@ void flash_erase_sector_error_test()

// most common sector size to get an sector address
uint32_t sector_size = 0x1000;
uint32_t address = (test_flash.target_config->flash_start + test_flash.target_config->flash_size) - (4*sector_size);
uint32_t address = flash_get_start_address(&test_flash) + flash_get_size(&test_flash) - (4*sector_size);
uint32_t erase_sector_boundary = ALIGN_DOWN(address, flash_get_sector_size(&test_flash, address));

// unaligned address
Expand All @@ -136,7 +136,7 @@ void flash_program_page_error_test()

uint32_t test_size = flash_get_page_size(&test_flash);
// the one before the last page in the system
uint32_t address = (test_flash.target_config->flash_start + test_flash.target_config->flash_size) - (2*test_size);
uint32_t address = flash_get_start_address(&test_flash) + flash_get_size(&test_flash) - (2*test_size);

// sector size might not be same as page size
uint32_t erase_sector_boundary = ALIGN_DOWN(address, flash_get_sector_size(&test_flash, address));
Expand Down
Loading