|
| 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(0, ret); |
| 35 | + ret = flash_device.deinit(); |
| 36 | + TEST_ASSERT_EQUAL_INT32(0, ret); |
| 37 | +} |
| 38 | + |
| 39 | +void flashiap_program_test() |
| 40 | +{ |
| 41 | + FlashIAP flash_device; |
| 42 | + uint32_t ret = flash_device.init(); |
| 43 | + TEST_ASSERT_EQUAL_INT32(0, ret); |
| 44 | + |
| 45 | + // get the last sector size (flash size - 1) |
| 46 | + uint32_t sector_size = flash_device.get_sector_size(flash_device.get_flash_start() + flash_device.get_flash_size() - 1UL); |
| 47 | + uint32_t page_size = flash_device.get_page_size(); |
| 48 | + TEST_ASSERT_NOT_EQUAL(0, sector_size); |
| 49 | + TEST_ASSERT_NOT_EQUAL(0, page_size); |
| 50 | + TEST_ASSERT_TRUE(sector_size % page_size == 0); |
| 51 | + const uint8_t test_value = 0xCE; |
| 52 | + uint8_t *data = new uint8_t[page_size]; |
| 53 | + for (uint32_t i = 0; i < page_size; i++) { |
| 54 | + data[i] = test_value; |
| 55 | + } |
| 56 | + |
| 57 | + // the one before the last sector in the system |
| 58 | + uint32_t address = (flash_device.get_flash_start() + flash_device.get_flash_size()) - (sector_size); |
| 59 | + TEST_ASSERT_TRUE(address != 0UL); |
| 60 | + ret = flash_device.erase(address, sector_size); |
| 61 | + TEST_ASSERT_EQUAL_INT32(0, ret); |
| 62 | + |
| 63 | + |
| 64 | + for (uint32_t i = 0; i < sector_size / page_size; i++) { |
| 65 | + uint32_t page_addr = address + i * page_size; |
| 66 | + ret = flash_device.program(data, page_addr, page_size); |
| 67 | + TEST_ASSERT_EQUAL_INT32(0, ret); |
| 68 | + } |
| 69 | + |
| 70 | + uint8_t *data_flashed = new uint8_t[page_size]; |
| 71 | + for (uint32_t i = 0; i < sector_size / page_size; i++) { |
| 72 | + uint32_t page_addr = address + i * page_size; |
| 73 | + ret = flash_device.read(data_flashed, page_addr, page_size); |
| 74 | + TEST_ASSERT_EQUAL_INT32(0, ret); |
| 75 | + TEST_ASSERT_EQUAL_UINT8_ARRAY(data, data_flashed, page_size); |
| 76 | + } |
| 77 | + delete[] data; |
| 78 | + delete[] data_flashed; |
| 79 | + |
| 80 | + ret = flash_device.deinit(); |
| 81 | + TEST_ASSERT_EQUAL_INT32(0, ret); |
| 82 | +} |
| 83 | + |
| 84 | +void flashiap_program_error_test() |
| 85 | +{ |
| 86 | + FlashIAP flash_device; |
| 87 | + uint32_t ret = flash_device.init(); |
| 88 | + TEST_ASSERT_EQUAL_INT32(0, ret); |
| 89 | + |
| 90 | + // get the last sector size (flash size - 1) |
| 91 | + uint32_t sector_size = flash_device.get_sector_size(flash_device.get_flash_start() + flash_device.get_flash_size() - 1UL); |
| 92 | + uint32_t page_size = flash_device.get_page_size(); |
| 93 | + TEST_ASSERT_NOT_EQUAL(0, sector_size); |
| 94 | + TEST_ASSERT_NOT_EQUAL(0, page_size); |
| 95 | + TEST_ASSERT_TRUE(sector_size % page_size == 0); |
| 96 | + const uint8_t test_value = 0xCE; |
| 97 | + uint8_t *data = new uint8_t[page_size]; |
| 98 | + for (uint32_t i = 0; i < page_size; i++) { |
| 99 | + data[i] = test_value; |
| 100 | + } |
| 101 | + |
| 102 | + // the one before the last page in the system |
| 103 | + uint32_t address = (flash_device.get_flash_start() + flash_device.get_flash_size()) - (sector_size); |
| 104 | + TEST_ASSERT_TRUE(address != 0UL); |
| 105 | + |
| 106 | + // unaligned address |
| 107 | + ret = flash_device.erase(address + 1, sector_size); |
| 108 | + TEST_ASSERT_EQUAL_INT32(-1, ret); |
| 109 | + ret = flash_device.program(data, address + 1, page_size); |
| 110 | + TEST_ASSERT_EQUAL_INT32(-1, ret); |
| 111 | + |
| 112 | + // unaligned page size |
| 113 | + ret = flash_device.program(data, address, page_size + 1); |
| 114 | + TEST_ASSERT_EQUAL_INT32(-1, ret); |
| 115 | + |
| 116 | + delete[] data; |
| 117 | + |
| 118 | + ret = flash_device.deinit(); |
| 119 | + TEST_ASSERT_EQUAL_INT32(0, ret); |
| 120 | +} |
| 121 | + |
| 122 | +Case cases[] = { |
| 123 | + Case("FlashIAP - init", flashiap_init_test), |
| 124 | + Case("FlashIAP - program", flashiap_program_test), |
| 125 | + Case("FlashIAP - program errors", flashiap_program_error_test), |
| 126 | +}; |
| 127 | + |
| 128 | +utest::v1::status_t greentea_test_setup(const size_t number_of_cases) { |
| 129 | + GREENTEA_SETUP(20, "default_auto"); |
| 130 | + return greentea_test_setup_handler(number_of_cases); |
| 131 | +} |
| 132 | + |
| 133 | +Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler); |
| 134 | + |
| 135 | +int main() { |
| 136 | + Harness::run(specification); |
| 137 | +} |
0 commit comments