Rtos API example
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2017 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 #include "mbed.h" 00017 #include "greentea-client/test_env.h" 00018 #include "unity.h" 00019 #include "utest.h" 00020 00021 #include "HeapBlockDevice.h" 00022 #include "MBRBlockDevice.h" 00023 #include <stdlib.h> 00024 00025 using namespace utest::v1; 00026 00027 #define BLOCK_COUNT 16 00028 #define BLOCK_SIZE 512 00029 00030 HeapBlockDevice bd(BLOCK_COUNT*BLOCK_SIZE, BLOCK_SIZE); 00031 00032 // Testing formatting of master boot record 00033 void test_mbr_format() 00034 { 00035 // Create two partitions splitting device in ~half 00036 int err = MBRBlockDevice::partition(&bd, 1, 0x83, 0, (BLOCK_COUNT/2)*BLOCK_SIZE); 00037 TEST_ASSERT_EQUAL(0, err); 00038 00039 err = MBRBlockDevice::partition(&bd, 2, 0x83, -(BLOCK_COUNT/2)*BLOCK_SIZE); 00040 TEST_ASSERT_EQUAL(0, err); 00041 00042 // Load both partitions, as well as a third to check for invalid partitions 00043 MBRBlockDevice part1(&bd, 1); 00044 err = part1.init(); 00045 TEST_ASSERT_EQUAL(0, err); 00046 00047 MBRBlockDevice part2(&bd, 2); 00048 err = part2.init(); 00049 TEST_ASSERT_EQUAL(0, err); 00050 00051 MBRBlockDevice part3(&bd, 3); 00052 err = part3.init(); 00053 TEST_ASSERT_EQUAL(BD_ERROR_INVALID_PARTITION, err); 00054 00055 // Deinit partitions 00056 err = part1.deinit(); 00057 TEST_ASSERT_EQUAL(0, err); 00058 00059 err = part2.deinit(); 00060 TEST_ASSERT_EQUAL(0, err); 00061 } 00062 00063 // Testing mbr attributes 00064 void test_mbr_attr() 00065 { 00066 // Load partitions 00067 MBRBlockDevice part1(&bd, 1); 00068 int err = part1.init(); 00069 TEST_ASSERT_EQUAL(0, err); 00070 00071 MBRBlockDevice part2(&bd, 2); 00072 err = part2.init(); 00073 TEST_ASSERT_EQUAL(0, err); 00074 00075 // Test attributes on partitions 00076 printf("partition 1 partition number: %d\n", part1.get_partition_number()); 00077 printf("partition 1 partition start: 0x%llx\n", part1.get_partition_start()); 00078 printf("partition 1 partition stop: 0x%llx\n", part1.get_partition_stop()); 00079 printf("partition 1 partition type: 0x%02x\n", part1.get_partition_type()); 00080 printf("partition 1 read size: %llu bytes\n", part1.get_read_size()); 00081 printf("partition 1 program size: %llu bytes\n", part1.get_program_size()); 00082 printf("partition 1 erase size: %llu bytes\n", part1.get_erase_size()); 00083 printf("partition 1 size: %llu bytes\n", part1.size()); 00084 TEST_ASSERT_EQUAL(1, part1.get_partition_number()); 00085 TEST_ASSERT_EQUAL(1*BLOCK_SIZE, part1.get_partition_start()); 00086 TEST_ASSERT_EQUAL((BLOCK_COUNT/2)*BLOCK_SIZE, part1.get_partition_stop()); 00087 TEST_ASSERT_EQUAL(0x83, part1.get_partition_type()); 00088 TEST_ASSERT_EQUAL(BLOCK_SIZE, part1.get_read_size()); 00089 TEST_ASSERT_EQUAL(BLOCK_SIZE, part1.get_program_size()); 00090 TEST_ASSERT_EQUAL(BLOCK_SIZE, part1.get_erase_size()); 00091 TEST_ASSERT_EQUAL(((BLOCK_COUNT/2)-1)*BLOCK_SIZE, part1.size()); 00092 00093 printf("partition 2 partition number: %d\n", part2.get_partition_number()); 00094 printf("partition 2 partition start: 0x%llx\n", part2.get_partition_start()); 00095 printf("partition 2 partition stop: 0x%llx\n", part2.get_partition_stop()); 00096 printf("partition 2 partition type: 0x%02x\n", part2.get_partition_type()); 00097 printf("partition 2 read size: %llu bytes\n", part2.get_read_size()); 00098 printf("partition 2 program size: %llu bytes\n", part2.get_program_size()); 00099 printf("partition 2 erase size: %llu bytes\n", part2.get_erase_size()); 00100 printf("partition 2 size: %llu bytes\n", part2.size()); 00101 TEST_ASSERT_EQUAL(2, part2.get_partition_number()); 00102 TEST_ASSERT_EQUAL((BLOCK_COUNT/2)*BLOCK_SIZE, part2.get_partition_start()); 00103 TEST_ASSERT_EQUAL(BLOCK_COUNT*BLOCK_SIZE, part2.get_partition_stop()); 00104 TEST_ASSERT_EQUAL(0x83, part2.get_partition_type()); 00105 TEST_ASSERT_EQUAL(BLOCK_SIZE, part2.get_read_size()); 00106 TEST_ASSERT_EQUAL(BLOCK_SIZE, part2.get_program_size()); 00107 TEST_ASSERT_EQUAL(BLOCK_SIZE, part2.get_erase_size()); 00108 TEST_ASSERT_EQUAL((BLOCK_COUNT/2)*BLOCK_SIZE, part2.size()); 00109 00110 // Deinit partitions 00111 err = part1.deinit(); 00112 TEST_ASSERT_EQUAL(0, err); 00113 00114 err = part2.deinit(); 00115 TEST_ASSERT_EQUAL(0, err); 00116 } 00117 00118 // Testing mbr read write 00119 void test_mbr_read_write() 00120 { 00121 // Load partitions 00122 MBRBlockDevice part1(&bd, 1); 00123 int err = part1.init(); 00124 TEST_ASSERT_EQUAL(0, err); 00125 00126 MBRBlockDevice part2(&bd, 2); 00127 err = part2.init(); 00128 TEST_ASSERT_EQUAL(0, err); 00129 00130 // Test reading/writing the partitions 00131 uint8_t *write_block = new uint8_t[BLOCK_SIZE]; 00132 uint8_t *read_block = new uint8_t[BLOCK_SIZE]; 00133 00134 // Fill with random sequence 00135 srand(1); 00136 for (int i = 0; i < BLOCK_SIZE; i++) { 00137 write_block[i] = 0xff & rand(); 00138 } 00139 00140 // Write, sync, and read the block 00141 err = part1.erase(0, BLOCK_SIZE); 00142 TEST_ASSERT_EQUAL(0, err); 00143 00144 err = part1.program(write_block, 0, BLOCK_SIZE); 00145 TEST_ASSERT_EQUAL(0, err); 00146 00147 err = part1.read(read_block, 0, BLOCK_SIZE); 00148 TEST_ASSERT_EQUAL(0, err); 00149 00150 // Check that the data was unmodified 00151 srand(1); 00152 for (int i = 0; i < BLOCK_SIZE; i++) { 00153 TEST_ASSERT_EQUAL(0xff & rand(), read_block[i]); 00154 } 00155 00156 // Check with original block device 00157 err = bd.read(read_block, 1*BLOCK_SIZE, BLOCK_SIZE); 00158 TEST_ASSERT_EQUAL(0, err); 00159 00160 // Check that the data was unmodified 00161 srand(1); 00162 for (int i = 0; i < BLOCK_SIZE; i++) { 00163 TEST_ASSERT_EQUAL(0xff & rand(), read_block[i]); 00164 } 00165 00166 // Test with second slice of block device 00167 srand(1); 00168 for (int i = 0; i < BLOCK_SIZE; i++) { 00169 write_block[i] = 0xff & rand(); 00170 } 00171 00172 // Write, sync, and read the block 00173 err = part2.erase(0, BLOCK_SIZE); 00174 TEST_ASSERT_EQUAL(0, err); 00175 00176 err = part2.program(write_block, 0, BLOCK_SIZE); 00177 TEST_ASSERT_EQUAL(0, err); 00178 00179 err = part2.read(read_block, 0, BLOCK_SIZE); 00180 TEST_ASSERT_EQUAL(0, err); 00181 00182 // Check that the data was unmodified 00183 srand(1); 00184 for (int i = 0; i < BLOCK_SIZE; i++) { 00185 TEST_ASSERT_EQUAL(0xff & rand(), read_block[i]); 00186 } 00187 00188 // Check with original block device 00189 err = bd.read(read_block, (BLOCK_COUNT/2)*BLOCK_SIZE, BLOCK_SIZE); 00190 TEST_ASSERT_EQUAL(0, err); 00191 00192 // Check that the data was unmodified 00193 srand(1); 00194 for (int i = 0; i < BLOCK_SIZE; i++) { 00195 TEST_ASSERT_EQUAL(0xff & rand(), read_block[i]); 00196 } 00197 00198 // Clean up 00199 delete[] write_block; 00200 delete[] read_block; 00201 00202 err = part1.deinit(); 00203 TEST_ASSERT_EQUAL(0, err); 00204 00205 err = part2.deinit(); 00206 TEST_ASSERT_EQUAL(0, err); 00207 } 00208 00209 00210 // Test setup 00211 utest::v1::status_t test_setup(const size_t number_of_cases) { 00212 GREENTEA_SETUP(10, "default_auto"); 00213 return verbose_test_setup_handler(number_of_cases); 00214 } 00215 00216 Case cases[] = { 00217 Case("Testing formatting of master boot record", test_mbr_format), 00218 Case("Testing mbr attributes", test_mbr_attr), 00219 Case("Testing mbr read write", test_mbr_read_write), 00220 }; 00221 00222 Specification specification(test_setup, cases); 00223 00224 int main() { 00225 return !Harness::run(specification); 00226 }
Generated on Sun Jul 17 2022 08:25:26 by
1.7.2