BA / Mbed OS BaBoRo1
Committer:
borlanic
Date:
Thu Mar 29 07:02:09 2018 +0000
Revision:
0:380207fcb5c1
Encoder, IMU --> OK; Controller --> in bearbeitung

Who changed what in which revision?

UserRevisionLine numberNew contents of line
borlanic 0:380207fcb5c1 1 /*
borlanic 0:380207fcb5c1 2 * Copyright (c) 2016-2017, ARM Limited, All Rights Reserved
borlanic 0:380207fcb5c1 3 * SPDX-License-Identifier: Apache-2.0
borlanic 0:380207fcb5c1 4 *
borlanic 0:380207fcb5c1 5 * Licensed under the Apache License, Version 2.0 (the "License"); you may
borlanic 0:380207fcb5c1 6 * not use this file except in compliance with the License.
borlanic 0:380207fcb5c1 7 * You may obtain a copy of the License at
borlanic 0:380207fcb5c1 8 *
borlanic 0:380207fcb5c1 9 * http://www.apache.org/licenses/LICENSE-2.0
borlanic 0:380207fcb5c1 10 *
borlanic 0:380207fcb5c1 11 * Unless required by applicable law or agreed to in writing, software
borlanic 0:380207fcb5c1 12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
borlanic 0:380207fcb5c1 13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
borlanic 0:380207fcb5c1 14 * See the License for the specific language governing permissions and
borlanic 0:380207fcb5c1 15 * limitations under the License.
borlanic 0:380207fcb5c1 16 */
borlanic 0:380207fcb5c1 17
borlanic 0:380207fcb5c1 18 #if defined(TARGET_CORTEX_A)
borlanic 0:380207fcb5c1 19 #error [NOT_SUPPORTED] This function not supported for this target
borlanic 0:380207fcb5c1 20 #endif
borlanic 0:380207fcb5c1 21
borlanic 0:380207fcb5c1 22 #include <stdio.h>
borlanic 0:380207fcb5c1 23 #include <stdlib.h>
borlanic 0:380207fcb5c1 24 #include <string.h>
borlanic 0:380207fcb5c1 25
borlanic 0:380207fcb5c1 26 #include "mbed.h"
borlanic 0:380207fcb5c1 27 #include "cmsis.h"
borlanic 0:380207fcb5c1 28 #include "greentea-client/test_env.h"
borlanic 0:380207fcb5c1 29 #include "utest/utest.h"
borlanic 0:380207fcb5c1 30 #include "unity/unity.h"
borlanic 0:380207fcb5c1 31
borlanic 0:380207fcb5c1 32 using utest::v1::Case;
borlanic 0:380207fcb5c1 33
borlanic 0:380207fcb5c1 34 static const int test_timeout = 30;
borlanic 0:380207fcb5c1 35
borlanic 0:380207fcb5c1 36
borlanic 0:380207fcb5c1 37 // Amount to malloc for each iteration
borlanic 0:380207fcb5c1 38 #define MALLOC_TEST_SIZE 256
borlanic 0:380207fcb5c1 39 // Malloc fill pattern
borlanic 0:380207fcb5c1 40 #define MALLOC_FILL 0x55
borlanic 0:380207fcb5c1 41
borlanic 0:380207fcb5c1 42 extern uint32_t mbed_heap_start;
borlanic 0:380207fcb5c1 43 extern uint32_t mbed_heap_size;
borlanic 0:380207fcb5c1 44 extern uint32_t mbed_stack_isr_start;
borlanic 0:380207fcb5c1 45 extern uint32_t mbed_stack_isr_size;
borlanic 0:380207fcb5c1 46
borlanic 0:380207fcb5c1 47
borlanic 0:380207fcb5c1 48 struct linked_list {
borlanic 0:380207fcb5c1 49 linked_list * next;
borlanic 0:380207fcb5c1 50 uint8_t data[MALLOC_TEST_SIZE];
borlanic 0:380207fcb5c1 51 };
borlanic 0:380207fcb5c1 52
borlanic 0:380207fcb5c1 53
borlanic 0:380207fcb5c1 54
borlanic 0:380207fcb5c1 55 /* TODO: add memory layout test.
borlanic 0:380207fcb5c1 56 *
borlanic 0:380207fcb5c1 57 * The test was skipped for now since not all devices seems to comply with Mbed OS memory.
borlanic 0:380207fcb5c1 58 *
borlanic 0:380207fcb5c1 59 * @note Mbed OS memory model: https://os.mbed.com/docs/latest/reference/memory.html
borlanic 0:380207fcb5c1 60 *
borlanic 0:380207fcb5c1 61 */
borlanic 0:380207fcb5c1 62
borlanic 0:380207fcb5c1 63
borlanic 0:380207fcb5c1 64 /*
borlanic 0:380207fcb5c1 65 * Return true if addr is in range [start:start+size)
borlanic 0:380207fcb5c1 66 */
borlanic 0:380207fcb5c1 67 static bool inrange(uint32_t addr, uint32_t start, uint32_t size)
borlanic 0:380207fcb5c1 68 {
borlanic 0:380207fcb5c1 69 return (addr >= start) && (addr < (start + size));
borlanic 0:380207fcb5c1 70 }
borlanic 0:380207fcb5c1 71
borlanic 0:380207fcb5c1 72 /*
borlanic 0:380207fcb5c1 73 * Return true if [addr:addr+size] is inside [start:start+len]
borlanic 0:380207fcb5c1 74 */
borlanic 0:380207fcb5c1 75 static bool rangeinrange(uint32_t addr, uint32_t size, uint32_t start, uint32_t len)
borlanic 0:380207fcb5c1 76 {
borlanic 0:380207fcb5c1 77 if ((addr + size) > (start + len)) {
borlanic 0:380207fcb5c1 78 return false;
borlanic 0:380207fcb5c1 79 }
borlanic 0:380207fcb5c1 80 if (addr < start) {
borlanic 0:380207fcb5c1 81 return false;
borlanic 0:380207fcb5c1 82 }
borlanic 0:380207fcb5c1 83 return true;
borlanic 0:380207fcb5c1 84 }
borlanic 0:380207fcb5c1 85
borlanic 0:380207fcb5c1 86 /*
borlanic 0:380207fcb5c1 87 * Return true if the region is filled only with the specified value
borlanic 0:380207fcb5c1 88 */
borlanic 0:380207fcb5c1 89 static bool valid_fill(uint8_t * data, uint32_t size, uint8_t fill)
borlanic 0:380207fcb5c1 90 {
borlanic 0:380207fcb5c1 91 for (uint32_t i = 0; i < size; i++) {
borlanic 0:380207fcb5c1 92 if (data[i] != fill) {
borlanic 0:380207fcb5c1 93 return false;
borlanic 0:380207fcb5c1 94 }
borlanic 0:380207fcb5c1 95 }
borlanic 0:380207fcb5c1 96 return true;
borlanic 0:380207fcb5c1 97 }
borlanic 0:380207fcb5c1 98
borlanic 0:380207fcb5c1 99 static void allocate_and_fill_heap(linked_list *&head)
borlanic 0:380207fcb5c1 100 {
borlanic 0:380207fcb5c1 101 linked_list *current;
borlanic 0:380207fcb5c1 102
borlanic 0:380207fcb5c1 103 current = (linked_list*) malloc(sizeof(linked_list));
borlanic 0:380207fcb5c1 104 TEST_ASSERT_NOT_NULL(current);
borlanic 0:380207fcb5c1 105
borlanic 0:380207fcb5c1 106 current->next = NULL;
borlanic 0:380207fcb5c1 107 memset((void*) current->data, MALLOC_FILL, sizeof(current->data));
borlanic 0:380207fcb5c1 108
borlanic 0:380207fcb5c1 109 // Allocate until malloc returns NULL
borlanic 0:380207fcb5c1 110 head = current;
borlanic 0:380207fcb5c1 111 while (true) {
borlanic 0:380207fcb5c1 112
borlanic 0:380207fcb5c1 113 // Allocate
borlanic 0:380207fcb5c1 114 linked_list *temp = (linked_list*) malloc(sizeof(linked_list));
borlanic 0:380207fcb5c1 115
borlanic 0:380207fcb5c1 116 if (NULL == temp) {
borlanic 0:380207fcb5c1 117 break;
borlanic 0:380207fcb5c1 118 }
borlanic 0:380207fcb5c1 119 bool result = rangeinrange((uint32_t) temp, sizeof(linked_list), mbed_heap_start, mbed_heap_size);
borlanic 0:380207fcb5c1 120
borlanic 0:380207fcb5c1 121 TEST_ASSERT_TRUE_MESSAGE(result, "Memory allocation out of range");
borlanic 0:380207fcb5c1 122
borlanic 0:380207fcb5c1 123 // Init
borlanic 0:380207fcb5c1 124 temp->next = NULL;
borlanic 0:380207fcb5c1 125 memset((void*) temp->data, MALLOC_FILL, sizeof(current->data));
borlanic 0:380207fcb5c1 126
borlanic 0:380207fcb5c1 127 // Add to list
borlanic 0:380207fcb5c1 128 current->next = temp;
borlanic 0:380207fcb5c1 129 current = temp;
borlanic 0:380207fcb5c1 130 }
borlanic 0:380207fcb5c1 131 }
borlanic 0:380207fcb5c1 132
borlanic 0:380207fcb5c1 133 static void check_and_free_heap(linked_list *head, uint32_t &max_allocation_size)
borlanic 0:380207fcb5c1 134 {
borlanic 0:380207fcb5c1 135 uint32_t total_size = 0;
borlanic 0:380207fcb5c1 136 linked_list * current = head;
borlanic 0:380207fcb5c1 137
borlanic 0:380207fcb5c1 138 while (current != NULL) {
borlanic 0:380207fcb5c1 139 total_size += sizeof(linked_list);
borlanic 0:380207fcb5c1 140 bool result = valid_fill(current->data, sizeof(current->data), MALLOC_FILL);
borlanic 0:380207fcb5c1 141
borlanic 0:380207fcb5c1 142 TEST_ASSERT_TRUE_MESSAGE(result, "Memory fill check failed");
borlanic 0:380207fcb5c1 143
borlanic 0:380207fcb5c1 144 linked_list * next = current->next;
borlanic 0:380207fcb5c1 145 free(current);
borlanic 0:380207fcb5c1 146 current = next;
borlanic 0:380207fcb5c1 147 }
borlanic 0:380207fcb5c1 148
borlanic 0:380207fcb5c1 149 max_allocation_size = total_size;
borlanic 0:380207fcb5c1 150 }
borlanic 0:380207fcb5c1 151
borlanic 0:380207fcb5c1 152 /** Test heap allocation
borlanic 0:380207fcb5c1 153
borlanic 0:380207fcb5c1 154 Given a heap
borlanic 0:380207fcb5c1 155 When memory is allocated from heap
borlanic 0:380207fcb5c1 156 Then the memory is within heap boundary
borlanic 0:380207fcb5c1 157
borlanic 0:380207fcb5c1 158 */
borlanic 0:380207fcb5c1 159 void test_heap_in_range(void)
borlanic 0:380207fcb5c1 160 {
borlanic 0:380207fcb5c1 161 char *initial_heap;
borlanic 0:380207fcb5c1 162
borlanic 0:380207fcb5c1 163 // Sanity check malloc
borlanic 0:380207fcb5c1 164 initial_heap = (char*) malloc(1);
borlanic 0:380207fcb5c1 165 TEST_ASSERT_NOT_NULL(initial_heap);
borlanic 0:380207fcb5c1 166
borlanic 0:380207fcb5c1 167 bool result = inrange((uint32_t) initial_heap, mbed_heap_start, mbed_heap_size);
borlanic 0:380207fcb5c1 168
borlanic 0:380207fcb5c1 169 TEST_ASSERT_TRUE_MESSAGE(result, "Heap in wrong location");
borlanic 0:380207fcb5c1 170 free(initial_heap);
borlanic 0:380207fcb5c1 171 }
borlanic 0:380207fcb5c1 172
borlanic 0:380207fcb5c1 173 /** Test for Main thread stack
borlanic 0:380207fcb5c1 174
borlanic 0:380207fcb5c1 175 Given a Main thread and its stack
borlanic 0:380207fcb5c1 176 When check Main thread stack pointer
borlanic 0:380207fcb5c1 177 Then the SP is within Main stack boundary
borlanic 0:380207fcb5c1 178 */
borlanic 0:380207fcb5c1 179 void test_main_stack_in_range(void)
borlanic 0:380207fcb5c1 180 {
borlanic 0:380207fcb5c1 181 os_thread_t *thread = (os_thread_t*) osThreadGetId();
borlanic 0:380207fcb5c1 182
borlanic 0:380207fcb5c1 183 uint32_t psp = __get_PSP();
borlanic 0:380207fcb5c1 184 uint8_t *stack_mem = (uint8_t*) thread->stack_mem;
borlanic 0:380207fcb5c1 185 uint32_t stack_size = thread->stack_size;
borlanic 0:380207fcb5c1 186
borlanic 0:380207fcb5c1 187 // PSP stack should be somewhere in the middle
borlanic 0:380207fcb5c1 188 bool result = inrange(psp, (uint32_t) stack_mem, stack_size);
borlanic 0:380207fcb5c1 189
borlanic 0:380207fcb5c1 190 TEST_ASSERT_TRUE_MESSAGE(result, "Main stack in wrong location");
borlanic 0:380207fcb5c1 191 }
borlanic 0:380207fcb5c1 192
borlanic 0:380207fcb5c1 193 /** Test for Scheduler/ISR thread stack
borlanic 0:380207fcb5c1 194
borlanic 0:380207fcb5c1 195 Given a Scheduler/ISR thread and its stack
borlanic 0:380207fcb5c1 196 When check Scheduler/ISR thread stack pointer
borlanic 0:380207fcb5c1 197 Then the SP is within Scheduler/ISR stack boundary
borlanic 0:380207fcb5c1 198 */
borlanic 0:380207fcb5c1 199 void test_isr_stack_in_range(void)
borlanic 0:380207fcb5c1 200 {
borlanic 0:380207fcb5c1 201 // MSP stack should be very near end (test using within 128 bytes)
borlanic 0:380207fcb5c1 202 uint32_t msp = __get_MSP();
borlanic 0:380207fcb5c1 203 bool result = inrange(msp, mbed_stack_isr_start + mbed_stack_isr_size - 128, 128);
borlanic 0:380207fcb5c1 204
borlanic 0:380207fcb5c1 205 TEST_ASSERT_TRUE_MESSAGE(result, "Interrupt stack in wrong location");
borlanic 0:380207fcb5c1 206 }
borlanic 0:380207fcb5c1 207
borlanic 0:380207fcb5c1 208 /** Test full heap allocation
borlanic 0:380207fcb5c1 209
borlanic 0:380207fcb5c1 210 Given a heap and linked_list data structure
borlanic 0:380207fcb5c1 211 When linked_list is filled till run out of heap memory
borlanic 0:380207fcb5c1 212 Then the memory is properly initialised and freed
borlanic 0:380207fcb5c1 213 */
borlanic 0:380207fcb5c1 214 void test_heap_allocation_free(void)
borlanic 0:380207fcb5c1 215 {
borlanic 0:380207fcb5c1 216 linked_list *head = NULL;
borlanic 0:380207fcb5c1 217 uint32_t max_allocation_size = 0;
borlanic 0:380207fcb5c1 218
borlanic 0:380207fcb5c1 219 // Fully allocate the heap and stack
borlanic 0:380207fcb5c1 220 allocate_and_fill_heap(head);
borlanic 0:380207fcb5c1 221
borlanic 0:380207fcb5c1 222 check_and_free_heap(head, max_allocation_size);
borlanic 0:380207fcb5c1 223
borlanic 0:380207fcb5c1 224 // Force a task switch so a stack check is performed
borlanic 0:380207fcb5c1 225 Thread::wait(10);
borlanic 0:380207fcb5c1 226
borlanic 0:380207fcb5c1 227 printf("Total size dynamically allocated: %luB\n", max_allocation_size);
borlanic 0:380207fcb5c1 228 }
borlanic 0:380207fcb5c1 229
borlanic 0:380207fcb5c1 230
borlanic 0:380207fcb5c1 231 // Test cases
borlanic 0:380207fcb5c1 232 Case cases[] = {
borlanic 0:380207fcb5c1 233 Case("Test heap in range", test_heap_in_range),
borlanic 0:380207fcb5c1 234 Case("Test main stack in range", test_main_stack_in_range),
borlanic 0:380207fcb5c1 235 Case("Test isr stack in range", test_isr_stack_in_range),
borlanic 0:380207fcb5c1 236 Case("Test heap allocation and free", test_heap_allocation_free)
borlanic 0:380207fcb5c1 237 };
borlanic 0:380207fcb5c1 238
borlanic 0:380207fcb5c1 239 utest::v1::status_t greentea_test_setup(const size_t number_of_cases)
borlanic 0:380207fcb5c1 240 {
borlanic 0:380207fcb5c1 241 GREENTEA_SETUP(test_timeout, "default_auto");
borlanic 0:380207fcb5c1 242 return utest::v1::greentea_test_setup_handler(number_of_cases);
borlanic 0:380207fcb5c1 243 }
borlanic 0:380207fcb5c1 244
borlanic 0:380207fcb5c1 245 utest::v1::Specification specification(greentea_test_setup, cases);
borlanic 0:380207fcb5c1 246
borlanic 0:380207fcb5c1 247 int main()
borlanic 0:380207fcb5c1 248 {
borlanic 0:380207fcb5c1 249 return !utest::v1::Harness::run(specification);
borlanic 0:380207fcb5c1 250 }