Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 00002 /* mbed Microcontroller Library 00003 * Copyright (c) 2017 ARM Limited 00004 * 00005 * Licensed under the Apache License, Version 2.0 (the "License"); 00006 * you may not use this file except in compliance with the License. 00007 * You may obtain a copy of the License at 00008 * 00009 * http://www.apache.org/licenses/LICENSE-2.0 00010 * 00011 * Unless required by applicable law or agreed to in writing, software 00012 * distributed under the License is distributed on an "AS IS" BASIS, 00013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00014 * See the License for the specific language governing permissions and 00015 * limitations under the License. 00016 */ 00017 00018 #if !DEVICE_FLASH 00019 #error [NOT_SUPPORTED] Flash API not supported for this target 00020 #endif 00021 00022 #include "utest/utest.h" 00023 #include "unity/unity.h" 00024 #include "greentea-client/test_env.h" 00025 00026 #include "mbed.h" 00027 00028 using namespace utest::v1; 00029 00030 void flashiap_init_test() 00031 { 00032 FlashIAP flash_device; 00033 uint32_t ret = flash_device.init(); 00034 TEST_ASSERT_EQUAL_INT32(0, ret); 00035 ret = flash_device.deinit(); 00036 TEST_ASSERT_EQUAL_INT32(0, ret); 00037 } 00038 00039 void flashiap_program_test() 00040 { 00041 FlashIAP flash_device; 00042 uint32_t ret = flash_device.init(); 00043 TEST_ASSERT_EQUAL_INT32(0, ret); 00044 00045 // get the last sector size (flash size - 1) 00046 uint32_t sector_size = flash_device.get_sector_size(flash_device.get_flash_start() + flash_device.get_flash_size() - 1UL); 00047 uint32_t page_size = flash_device.get_page_size(); 00048 TEST_ASSERT_NOT_EQUAL(0, sector_size); 00049 TEST_ASSERT_NOT_EQUAL(0, page_size); 00050 TEST_ASSERT_TRUE(sector_size % page_size == 0); 00051 const uint8_t test_value = 0xCE; 00052 uint8_t *data = new uint8_t[page_size]; 00053 for (uint32_t i = 0; i < page_size; i++) { 00054 data[i] = test_value; 00055 } 00056 00057 // the one before the last sector in the system 00058 uint32_t address = (flash_device.get_flash_start() + flash_device.get_flash_size()) - (sector_size); 00059 TEST_ASSERT_TRUE(address != 0UL); 00060 ret = flash_device.erase(address, sector_size); 00061 TEST_ASSERT_EQUAL_INT32(0, ret); 00062 00063 00064 for (uint32_t i = 0; i < sector_size / page_size; i++) { 00065 uint32_t page_addr = address + i * page_size; 00066 ret = flash_device.program(data, page_addr, page_size); 00067 TEST_ASSERT_EQUAL_INT32(0, ret); 00068 } 00069 00070 uint8_t *data_flashed = new uint8_t[page_size]; 00071 for (uint32_t i = 0; i < sector_size / page_size; i++) { 00072 uint32_t page_addr = address + i * page_size; 00073 ret = flash_device.read(data_flashed, page_addr, page_size); 00074 TEST_ASSERT_EQUAL_INT32(0, ret); 00075 TEST_ASSERT_EQUAL_UINT8_ARRAY(data, data_flashed, page_size); 00076 } 00077 delete[] data; 00078 delete[] data_flashed; 00079 00080 ret = flash_device.deinit(); 00081 TEST_ASSERT_EQUAL_INT32(0, ret); 00082 } 00083 00084 void flashiap_cross_sector_program_test() 00085 { 00086 FlashIAP flash_device; 00087 uint32_t ret = flash_device.init(); 00088 TEST_ASSERT_EQUAL_INT32(0, ret); 00089 00090 uint32_t page_size = flash_device.get_page_size(); 00091 00092 // Erase last two sectors 00093 uint32_t address = flash_device.get_flash_start() + flash_device.get_flash_size(); 00094 uint32_t sector_size, agg_size = 0; 00095 for (uint32_t i = 0; i < 2; i++) { 00096 sector_size = flash_device.get_sector_size(address - 1UL); 00097 TEST_ASSERT_NOT_EQUAL(0, sector_size); 00098 TEST_ASSERT_TRUE(sector_size % page_size == 0); 00099 agg_size += sector_size; 00100 address -= sector_size; 00101 } 00102 ret = flash_device.erase(address, agg_size); 00103 TEST_ASSERT_EQUAL_INT32(0, ret); 00104 00105 address += sector_size - page_size; 00106 uint32_t aligned_prog_size = 2 * page_size; 00107 uint32_t prog_size = aligned_prog_size; 00108 if (page_size > 1) { 00109 prog_size--; 00110 } 00111 uint8_t *data = new uint8_t[aligned_prog_size]; 00112 for (uint32_t i = 0; i < prog_size; i++) { 00113 data[i] = rand() % 256; 00114 } 00115 for (uint32_t i = prog_size; i < aligned_prog_size; i++) { 00116 data[i] = 0xFF; 00117 } 00118 00119 ret = flash_device.program(data, address, prog_size); 00120 TEST_ASSERT_EQUAL_INT32(0, ret); 00121 00122 uint8_t *data_flashed = new uint8_t[aligned_prog_size]; 00123 ret = flash_device.read(data_flashed, address, aligned_prog_size); 00124 TEST_ASSERT_EQUAL_INT32(0, ret); 00125 TEST_ASSERT_EQUAL_UINT8_ARRAY(data, data_flashed, aligned_prog_size); 00126 00127 delete[] data; 00128 delete[] data_flashed; 00129 00130 ret = flash_device.deinit(); 00131 TEST_ASSERT_EQUAL_INT32(0, ret); 00132 } 00133 00134 void flashiap_program_error_test() 00135 { 00136 FlashIAP flash_device; 00137 uint32_t ret = flash_device.init(); 00138 TEST_ASSERT_EQUAL_INT32(0, ret); 00139 00140 // get the last sector size (flash size - 1) 00141 uint32_t sector_size = flash_device.get_sector_size(flash_device.get_flash_start() + flash_device.get_flash_size() - 1UL); 00142 uint32_t page_size = flash_device.get_page_size(); 00143 TEST_ASSERT_NOT_EQUAL(0, sector_size); 00144 TEST_ASSERT_NOT_EQUAL(0, page_size); 00145 TEST_ASSERT_TRUE(sector_size % page_size == 0); 00146 const uint8_t test_value = 0xCE; 00147 uint8_t *data = new uint8_t[page_size]; 00148 for (uint32_t i = 0; i < page_size; i++) { 00149 data[i] = test_value; 00150 } 00151 00152 // the one before the last page in the system 00153 uint32_t address = (flash_device.get_flash_start() + flash_device.get_flash_size()) - (sector_size); 00154 TEST_ASSERT_TRUE(address != 0UL); 00155 00156 // unaligned address 00157 ret = flash_device.erase(address + 1, sector_size); 00158 TEST_ASSERT_EQUAL_INT32(-1, ret); 00159 if (flash_device.get_page_size() > 1) { 00160 ret = flash_device.program(data, address + 1, page_size); 00161 TEST_ASSERT_EQUAL_INT32(-1, ret); 00162 } 00163 00164 delete[] data; 00165 00166 ret = flash_device.deinit(); 00167 TEST_ASSERT_EQUAL_INT32(0, ret); 00168 } 00169 00170 Case cases[] = { 00171 Case("FlashIAP - init", flashiap_init_test), 00172 Case("FlashIAP - program", flashiap_program_test), 00173 Case("FlashIAP - program across sectors", flashiap_cross_sector_program_test), 00174 Case("FlashIAP - program errors", flashiap_program_error_test), 00175 }; 00176 00177 utest::v1::status_t greentea_test_setup(const size_t number_of_cases) { 00178 GREENTEA_SETUP(20, "default_auto"); 00179 return greentea_test_setup_handler(number_of_cases); 00180 } 00181 00182 Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler); 00183 00184 int main() { 00185 Harness::run(specification); 00186 }
Generated on Tue Jul 12 2022 12:22:06 by
1.7.2