Color Oled(SSD1331) connect to STMicroelectronics Nucleo-F466

Dependencies:   ssd1331

Committer:
kadonotakashi
Date:
Wed Oct 10 00:33:53 2018 +0000
Revision:
0:8fdf9a60065b
how to make mbed librry

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kadonotakashi 0:8fdf9a60065b 1 /*
kadonotakashi 0:8fdf9a60065b 2 * Copyright (c) 2013-2017, ARM Limited, All Rights Reserved
kadonotakashi 0:8fdf9a60065b 3 * SPDX-License-Identifier: Apache-2.0
kadonotakashi 0:8fdf9a60065b 4 *
kadonotakashi 0:8fdf9a60065b 5 * Licensed under the Apache License, Version 2.0 (the "License"); you may
kadonotakashi 0:8fdf9a60065b 6 * not use this file except in compliance with the License.
kadonotakashi 0:8fdf9a60065b 7 * You may obtain a copy of the License at
kadonotakashi 0:8fdf9a60065b 8 *
kadonotakashi 0:8fdf9a60065b 9 * http://www.apache.org/licenses/LICENSE-2.0
kadonotakashi 0:8fdf9a60065b 10 *
kadonotakashi 0:8fdf9a60065b 11 * Unless required by applicable law or agreed to in writing, software
kadonotakashi 0:8fdf9a60065b 12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
kadonotakashi 0:8fdf9a60065b 13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
kadonotakashi 0:8fdf9a60065b 14 * See the License for the specific language governing permissions and
kadonotakashi 0:8fdf9a60065b 15 * limitations under the License.
kadonotakashi 0:8fdf9a60065b 16 */
kadonotakashi 0:8fdf9a60065b 17
kadonotakashi 0:8fdf9a60065b 18 #include "mbed.h"
kadonotakashi 0:8fdf9a60065b 19 #include "greentea-client/test_env.h"
kadonotakashi 0:8fdf9a60065b 20 #include "unity/unity.h"
kadonotakashi 0:8fdf9a60065b 21 #include "utest/utest.h"
kadonotakashi 0:8fdf9a60065b 22
kadonotakashi 0:8fdf9a60065b 23 using utest::v1::Case;
kadonotakashi 0:8fdf9a60065b 24
kadonotakashi 0:8fdf9a60065b 25 #if defined(MBED_RTOS_SINGLE_THREAD)
kadonotakashi 0:8fdf9a60065b 26 #error [NOT_SUPPORTED] test not supported
kadonotakashi 0:8fdf9a60065b 27 #endif
kadonotakashi 0:8fdf9a60065b 28
kadonotakashi 0:8fdf9a60065b 29 #if !DEVICE_USTICKER
kadonotakashi 0:8fdf9a60065b 30 #error [NOT_SUPPORTED] test not supported
kadonotakashi 0:8fdf9a60065b 31 #endif
kadonotakashi 0:8fdf9a60065b 32
kadonotakashi 0:8fdf9a60065b 33 #if defined(__CORTEX_M23) || defined(__CORTEX_M33)
kadonotakashi 0:8fdf9a60065b 34 #define THREAD_STACK_SIZE 512
kadonotakashi 0:8fdf9a60065b 35 #else
kadonotakashi 0:8fdf9a60065b 36 #define THREAD_STACK_SIZE 320 /* 512B stack on GCC_ARM compiler cause out of memory on some 16kB RAM boards e.g. NUCLEO_F070RB */
kadonotakashi 0:8fdf9a60065b 37 #endif
kadonotakashi 0:8fdf9a60065b 38
kadonotakashi 0:8fdf9a60065b 39 #define MAX_FLAG_POS 30
kadonotakashi 0:8fdf9a60065b 40 #define PROHIBITED_FLAG_POS 31
kadonotakashi 0:8fdf9a60065b 41
kadonotakashi 0:8fdf9a60065b 42 /* flags */
kadonotakashi 0:8fdf9a60065b 43 #define FLAG01 0x1FFF /* 00000000000000000001111111111111 */
kadonotakashi 0:8fdf9a60065b 44 #define FLAG02 0x3FFE000 /* 00000011111111111110000000000000 */
kadonotakashi 0:8fdf9a60065b 45 #define FLAG03 0x7C000000 /* 01111100000000000000000000000000 */
kadonotakashi 0:8fdf9a60065b 46 #define PROHIBITED_FLAG 0x80000000 /* 10000000000000000000000000000000 */
kadonotakashi 0:8fdf9a60065b 47 #define NO_FLAGS 0x0
kadonotakashi 0:8fdf9a60065b 48
kadonotakashi 0:8fdf9a60065b 49 Semaphore sync_sem(0, 1);
kadonotakashi 0:8fdf9a60065b 50
kadonotakashi 0:8fdf9a60065b 51 /* In order to successfully run this test suite when compiled with --profile=debug
kadonotakashi 0:8fdf9a60065b 52 * error() has to be redefined as noop.
kadonotakashi 0:8fdf9a60065b 53 *
kadonotakashi 0:8fdf9a60065b 54 * EventFlags calls RTX API which uses Event Recorder functionality. When compiled
kadonotakashi 0:8fdf9a60065b 55 * with MBED_TRAP_ERRORS_ENABLED=1 (set in debug profile) EvrRtxEventFlagsError() calls error()
kadonotakashi 0:8fdf9a60065b 56 * which aborts test program.
kadonotakashi 0:8fdf9a60065b 57 */
kadonotakashi 0:8fdf9a60065b 58 #if defined(MBED_TRAP_ERRORS_ENABLED) && MBED_TRAP_ERRORS_ENABLED
kadonotakashi 0:8fdf9a60065b 59 void error(const char *format, ...)
kadonotakashi 0:8fdf9a60065b 60 {
kadonotakashi 0:8fdf9a60065b 61 (void) format;
kadonotakashi 0:8fdf9a60065b 62 }
kadonotakashi 0:8fdf9a60065b 63
kadonotakashi 0:8fdf9a60065b 64 //Override the set_error function to trap the errors
kadonotakashi 0:8fdf9a60065b 65 mbed_error_status_t mbed_error(mbed_error_status_t error_status, const char *error_msg, unsigned int error_value, const char *filename, int line_number)
kadonotakashi 0:8fdf9a60065b 66 {
kadonotakashi 0:8fdf9a60065b 67 return MBED_SUCCESS;
kadonotakashi 0:8fdf9a60065b 68 }
kadonotakashi 0:8fdf9a60065b 69 #endif
kadonotakashi 0:8fdf9a60065b 70
kadonotakashi 0:8fdf9a60065b 71 template<uint32_t flags, uint32_t wait_ms>
kadonotakashi 0:8fdf9a60065b 72 void send_thread(EventFlags *ef)
kadonotakashi 0:8fdf9a60065b 73 {
kadonotakashi 0:8fdf9a60065b 74 for (uint32_t i = 0; i <= MAX_FLAG_POS; i++) {
kadonotakashi 0:8fdf9a60065b 75 const uint32_t flag = flags & (1 << i);
kadonotakashi 0:8fdf9a60065b 76 if (flag) {
kadonotakashi 0:8fdf9a60065b 77 ef->set(flag);
kadonotakashi 0:8fdf9a60065b 78 Thread::wait(wait_ms);
kadonotakashi 0:8fdf9a60065b 79 }
kadonotakashi 0:8fdf9a60065b 80 }
kadonotakashi 0:8fdf9a60065b 81 }
kadonotakashi 0:8fdf9a60065b 82
kadonotakashi 0:8fdf9a60065b 83 template<uint32_t flags, uint32_t wait_ms>
kadonotakashi 0:8fdf9a60065b 84 void send_thread_sync(EventFlags *ef)
kadonotakashi 0:8fdf9a60065b 85 {
kadonotakashi 0:8fdf9a60065b 86 for (uint32_t i = 0; i <= MAX_FLAG_POS; i++) {
kadonotakashi 0:8fdf9a60065b 87 const uint32_t flag = flags & (1 << i);
kadonotakashi 0:8fdf9a60065b 88 if (flag) {
kadonotakashi 0:8fdf9a60065b 89 sync_sem.wait();
kadonotakashi 0:8fdf9a60065b 90 ef->set(flag);
kadonotakashi 0:8fdf9a60065b 91 Thread::wait(wait_ms);
kadonotakashi 0:8fdf9a60065b 92 }
kadonotakashi 0:8fdf9a60065b 93 }
kadonotakashi 0:8fdf9a60065b 94 }
kadonotakashi 0:8fdf9a60065b 95
kadonotakashi 0:8fdf9a60065b 96 template<uint32_t flags>
kadonotakashi 0:8fdf9a60065b 97 void wait_thread_all(EventFlags *ef)
kadonotakashi 0:8fdf9a60065b 98 {
kadonotakashi 0:8fdf9a60065b 99 uint32_t ret, flags_after_clear;
kadonotakashi 0:8fdf9a60065b 100 ret = ef->wait_all(flags);
kadonotakashi 0:8fdf9a60065b 101 flags_after_clear = ef->get();
kadonotakashi 0:8fdf9a60065b 102 TEST_ASSERT(flags | ret);
kadonotakashi 0:8fdf9a60065b 103 TEST_ASSERT(flags | ~flags_after_clear);
kadonotakashi 0:8fdf9a60065b 104 }
kadonotakashi 0:8fdf9a60065b 105
kadonotakashi 0:8fdf9a60065b 106
kadonotakashi 0:8fdf9a60065b 107 /** Test if get on empty EventFlags object return NO_FLAGS
kadonotakashi 0:8fdf9a60065b 108
kadonotakashi 0:8fdf9a60065b 109 Given a empty EventFlags object
kadonotakashi 0:8fdf9a60065b 110 When call @a get
kadonotakashi 0:8fdf9a60065b 111 Then @a get return status is NO_FLAGS
kadonotakashi 0:8fdf9a60065b 112 */
kadonotakashi 0:8fdf9a60065b 113 void test_empty_get(void)
kadonotakashi 0:8fdf9a60065b 114 {
kadonotakashi 0:8fdf9a60065b 115 EventFlags ev;
kadonotakashi 0:8fdf9a60065b 116 uint32_t flags;
kadonotakashi 0:8fdf9a60065b 117
kadonotakashi 0:8fdf9a60065b 118 flags = ev.get();
kadonotakashi 0:8fdf9a60065b 119 TEST_ASSERT_EQUAL(NO_FLAGS, flags);
kadonotakashi 0:8fdf9a60065b 120 }
kadonotakashi 0:8fdf9a60065b 121
kadonotakashi 0:8fdf9a60065b 122 /** Test if clear on empty EventFlags object return NO_FLAGS
kadonotakashi 0:8fdf9a60065b 123
kadonotakashi 0:8fdf9a60065b 124 Given a empty EventFlags object
kadonotakashi 0:8fdf9a60065b 125 When call @a clear(NO_FLAGS)
kadonotakashi 0:8fdf9a60065b 126 Then @a clear return status is NO_FLAGS
kadonotakashi 0:8fdf9a60065b 127 */
kadonotakashi 0:8fdf9a60065b 128 void test_empty_clear(void)
kadonotakashi 0:8fdf9a60065b 129 {
kadonotakashi 0:8fdf9a60065b 130 EventFlags ev;
kadonotakashi 0:8fdf9a60065b 131 uint32_t flags;
kadonotakashi 0:8fdf9a60065b 132
kadonotakashi 0:8fdf9a60065b 133 flags = ev.clear(NO_FLAGS);
kadonotakashi 0:8fdf9a60065b 134 TEST_ASSERT_EQUAL(NO_FLAGS, flags);
kadonotakashi 0:8fdf9a60065b 135 }
kadonotakashi 0:8fdf9a60065b 136
kadonotakashi 0:8fdf9a60065b 137 /** Test if set on empty EventFlags object return NO_FLAGS
kadonotakashi 0:8fdf9a60065b 138
kadonotakashi 0:8fdf9a60065b 139 Given a empty EventFlags object
kadonotakashi 0:8fdf9a60065b 140 When call @a set(NO_FLAGS)
kadonotakashi 0:8fdf9a60065b 141 Then @a set return status is NO_FLAGS
kadonotakashi 0:8fdf9a60065b 142 */
kadonotakashi 0:8fdf9a60065b 143 void test_empty_set(void)
kadonotakashi 0:8fdf9a60065b 144 {
kadonotakashi 0:8fdf9a60065b 145 EventFlags ev;
kadonotakashi 0:8fdf9a60065b 146 uint32_t flags;
kadonotakashi 0:8fdf9a60065b 147
kadonotakashi 0:8fdf9a60065b 148 flags = ev.set(NO_FLAGS);
kadonotakashi 0:8fdf9a60065b 149 TEST_ASSERT_EQUAL(NO_FLAGS, flags);
kadonotakashi 0:8fdf9a60065b 150 }
kadonotakashi 0:8fdf9a60065b 151
kadonotakashi 0:8fdf9a60065b 152 /** Test if call of set/clean with PROHIBITED_FLAG doesn't invalidates object flags
kadonotakashi 0:8fdf9a60065b 153
kadonotakashi 0:8fdf9a60065b 154 Given a EventFlags object with all flags already set
kadonotakashi 0:8fdf9a60065b 155 When call @a clear(PROHIBITED_FLAG) with prohibited flag
kadonotakashi 0:8fdf9a60065b 156 Then @a clear return status is osFlagsErrorParameter and object flags stays unchanged
kadonotakashi 0:8fdf9a60065b 157 When call @a set(PROHIBITED_FLAG) with prohibited flag
kadonotakashi 0:8fdf9a60065b 158 Then @a set return status is osFlagsErrorParameter and object flags stays unchanged
kadonotakashi 0:8fdf9a60065b 159
kadonotakashi 0:8fdf9a60065b 160 @note Each signal has up to 31 event flags 0x1, 0x2, 0x4, 0x8, ..., 0x40000000
kadonotakashi 0:8fdf9a60065b 161 Most significant bit is reserved and thereby flag 0x80000000 is prohibited
kadonotakashi 0:8fdf9a60065b 162 */
kadonotakashi 0:8fdf9a60065b 163 void test_prohibited(void)
kadonotakashi 0:8fdf9a60065b 164 {
kadonotakashi 0:8fdf9a60065b 165 EventFlags ev;
kadonotakashi 0:8fdf9a60065b 166 uint32_t flags;
kadonotakashi 0:8fdf9a60065b 167
kadonotakashi 0:8fdf9a60065b 168 ev.set(FLAG01 | FLAG02 | FLAG03);
kadonotakashi 0:8fdf9a60065b 169
kadonotakashi 0:8fdf9a60065b 170 flags = ev.clear(PROHIBITED_FLAG);
kadonotakashi 0:8fdf9a60065b 171 TEST_ASSERT_EQUAL(osFlagsErrorParameter, flags);
kadonotakashi 0:8fdf9a60065b 172
kadonotakashi 0:8fdf9a60065b 173 flags = ev.get();
kadonotakashi 0:8fdf9a60065b 174 TEST_ASSERT_EQUAL(FLAG01 | FLAG02 | FLAG03, flags);
kadonotakashi 0:8fdf9a60065b 175
kadonotakashi 0:8fdf9a60065b 176 flags = ev.set(PROHIBITED_FLAG);
kadonotakashi 0:8fdf9a60065b 177 TEST_ASSERT_EQUAL(osFlagsErrorParameter, flags);
kadonotakashi 0:8fdf9a60065b 178
kadonotakashi 0:8fdf9a60065b 179 flags = ev.get();
kadonotakashi 0:8fdf9a60065b 180 TEST_ASSERT_EQUAL(FLAG01 | FLAG02 | FLAG03, flags);
kadonotakashi 0:8fdf9a60065b 181 }
kadonotakashi 0:8fdf9a60065b 182
kadonotakashi 0:8fdf9a60065b 183 /** Test set/get/clear for full flag range
kadonotakashi 0:8fdf9a60065b 184
kadonotakashi 0:8fdf9a60065b 185 Given a EventFlags object
kadonotakashi 0:8fdf9a60065b 186 When call @a clear
kadonotakashi 0:8fdf9a60065b 187 Then @a clear return status is already set flags
kadonotakashi 0:8fdf9a60065b 188 When call @a set with specified flag
kadonotakashi 0:8fdf9a60065b 189 Then @a set return status is flags after setting
kadonotakashi 0:8fdf9a60065b 190 When call @a get
kadonotakashi 0:8fdf9a60065b 191 Then @a get return status is set flags
kadonotakashi 0:8fdf9a60065b 192 */
kadonotakashi 0:8fdf9a60065b 193 void test_set_get_clear_full_flag_range(void)
kadonotakashi 0:8fdf9a60065b 194 {
kadonotakashi 0:8fdf9a60065b 195 EventFlags ev;
kadonotakashi 0:8fdf9a60065b 196 uint32_t flag, flags, ret;
kadonotakashi 0:8fdf9a60065b 197
kadonotakashi 0:8fdf9a60065b 198 flags = NO_FLAGS;
kadonotakashi 0:8fdf9a60065b 199 for (int i = 0; i <= MAX_FLAG_POS; i++) {
kadonotakashi 0:8fdf9a60065b 200 ret = ev.clear();
kadonotakashi 0:8fdf9a60065b 201 TEST_ASSERT_EQUAL(flags, ret);
kadonotakashi 0:8fdf9a60065b 202 flags = 1 << i;
kadonotakashi 0:8fdf9a60065b 203 ret = ev.set(flags);
kadonotakashi 0:8fdf9a60065b 204 TEST_ASSERT_EQUAL(flags, ret);
kadonotakashi 0:8fdf9a60065b 205 ret = ev.get();
kadonotakashi 0:8fdf9a60065b 206 TEST_ASSERT_EQUAL(flags, ret);
kadonotakashi 0:8fdf9a60065b 207 }
kadonotakashi 0:8fdf9a60065b 208
kadonotakashi 0:8fdf9a60065b 209 ev.clear();
kadonotakashi 0:8fdf9a60065b 210 flags = NO_FLAGS;
kadonotakashi 0:8fdf9a60065b 211 for (int i = 0; i <= MAX_FLAG_POS; i++) {
kadonotakashi 0:8fdf9a60065b 212 ret = ev.clear(NO_FLAGS);
kadonotakashi 0:8fdf9a60065b 213 TEST_ASSERT_EQUAL(flags, ret);
kadonotakashi 0:8fdf9a60065b 214 flag = 1 << i;
kadonotakashi 0:8fdf9a60065b 215 flags |= flag;
kadonotakashi 0:8fdf9a60065b 216 ret = ev.set(flag);
kadonotakashi 0:8fdf9a60065b 217 TEST_ASSERT_EQUAL(flags, ret);
kadonotakashi 0:8fdf9a60065b 218 ret = ev.get();
kadonotakashi 0:8fdf9a60065b 219 TEST_ASSERT_EQUAL(flags, ret);
kadonotakashi 0:8fdf9a60065b 220 }
kadonotakashi 0:8fdf9a60065b 221 }
kadonotakashi 0:8fdf9a60065b 222
kadonotakashi 0:8fdf9a60065b 223 /** Test if multi-threaded flag set cause wait_all to return
kadonotakashi 0:8fdf9a60065b 224
kadonotakashi 0:8fdf9a60065b 225 Given a EventFlags object and three threads are started in parallel
kadonotakashi 0:8fdf9a60065b 226 When threads set specified flags
kadonotakashi 0:8fdf9a60065b 227 Then main thread waits until receive all of them
kadonotakashi 0:8fdf9a60065b 228 */
kadonotakashi 0:8fdf9a60065b 229 void test_multi_thread_all(void)
kadonotakashi 0:8fdf9a60065b 230 {
kadonotakashi 0:8fdf9a60065b 231 EventFlags ef;
kadonotakashi 0:8fdf9a60065b 232 Thread thread1(osPriorityNormal, THREAD_STACK_SIZE);
kadonotakashi 0:8fdf9a60065b 233 Thread thread2(osPriorityNormal, THREAD_STACK_SIZE);
kadonotakashi 0:8fdf9a60065b 234 Thread thread3(osPriorityNormal, THREAD_STACK_SIZE);
kadonotakashi 0:8fdf9a60065b 235 thread1.start(callback(send_thread<FLAG01, 1>, &ef));
kadonotakashi 0:8fdf9a60065b 236 thread2.start(callback(send_thread<FLAG02, 2>, &ef));
kadonotakashi 0:8fdf9a60065b 237 thread3.start(callback(send_thread<FLAG03, 3>, &ef));
kadonotakashi 0:8fdf9a60065b 238
kadonotakashi 0:8fdf9a60065b 239 uint32_t ret = ef.wait_all(FLAG01 | FLAG02 | FLAG03);
kadonotakashi 0:8fdf9a60065b 240 TEST_ASSERT_EQUAL(FLAG01 | FLAG02 | FLAG03, ret);
kadonotakashi 0:8fdf9a60065b 241 }
kadonotakashi 0:8fdf9a60065b 242
kadonotakashi 0:8fdf9a60065b 243 /** Test if multi-threaded flag set cause wait_any to return
kadonotakashi 0:8fdf9a60065b 244
kadonotakashi 0:8fdf9a60065b 245 Given a EventFlags object and three threads are started in parallel
kadonotakashi 0:8fdf9a60065b 246 When threads set specified flags
kadonotakashi 0:8fdf9a60065b 247 Then main thread waits until receive all of them
kadonotakashi 0:8fdf9a60065b 248 */
kadonotakashi 0:8fdf9a60065b 249 void test_multi_thread_any(void)
kadonotakashi 0:8fdf9a60065b 250 {
kadonotakashi 0:8fdf9a60065b 251 EventFlags ef;
kadonotakashi 0:8fdf9a60065b 252 uint32_t ret;
kadonotakashi 0:8fdf9a60065b 253 Thread thread1(osPriorityNormal, THREAD_STACK_SIZE);
kadonotakashi 0:8fdf9a60065b 254 Thread thread2(osPriorityNormal, THREAD_STACK_SIZE);
kadonotakashi 0:8fdf9a60065b 255 Thread thread3(osPriorityNormal, THREAD_STACK_SIZE);
kadonotakashi 0:8fdf9a60065b 256 thread1.start(callback(send_thread<FLAG01, 1>, &ef));
kadonotakashi 0:8fdf9a60065b 257 thread2.start(callback(send_thread<FLAG02, 1>, &ef));
kadonotakashi 0:8fdf9a60065b 258 thread3.start(callback(send_thread<FLAG03, 1>, &ef));
kadonotakashi 0:8fdf9a60065b 259
kadonotakashi 0:8fdf9a60065b 260 for (int i = 0; i <= MAX_FLAG_POS; i++) {
kadonotakashi 0:8fdf9a60065b 261 uint32_t flag = 1 << i;
kadonotakashi 0:8fdf9a60065b 262 ret = ef.wait_any(flag);
kadonotakashi 0:8fdf9a60065b 263 TEST_ASSERT(flag | ret);
kadonotakashi 0:8fdf9a60065b 264 }
kadonotakashi 0:8fdf9a60065b 265 ret = ef.get();
kadonotakashi 0:8fdf9a60065b 266 TEST_ASSERT_EQUAL(NO_FLAGS, ret);
kadonotakashi 0:8fdf9a60065b 267 }
kadonotakashi 0:8fdf9a60065b 268
kadonotakashi 0:8fdf9a60065b 269 /** Test if multi-threaded flag set cause wait_any(with timeout) to return
kadonotakashi 0:8fdf9a60065b 270
kadonotakashi 0:8fdf9a60065b 271 Given a EventFlags object and thread is running
kadonotakashi 0:8fdf9a60065b 272 When main thread call @ wait_any with timeout
kadonotakashi 0:8fdf9a60065b 273 Then when timeout expires @ wait_any return status is osFlagsErrorTimeout
kadonotakashi 0:8fdf9a60065b 274 When main thread call @ wait_any with timeout and thread set specified flags
kadonotakashi 0:8fdf9a60065b 275 Then main thread waits until receive all of them and @ wait_any return status is wait flag
kadonotakashi 0:8fdf9a60065b 276 */
kadonotakashi 0:8fdf9a60065b 277 void test_multi_thread_any_timeout(void)
kadonotakashi 0:8fdf9a60065b 278 {
kadonotakashi 0:8fdf9a60065b 279 EventFlags ef;
kadonotakashi 0:8fdf9a60065b 280 uint32_t ret;
kadonotakashi 0:8fdf9a60065b 281 Thread thread(osPriorityNormal, THREAD_STACK_SIZE);
kadonotakashi 0:8fdf9a60065b 282 thread.start(callback(send_thread_sync < FLAG01 | FLAG02 | FLAG03, 1 >, &ef));
kadonotakashi 0:8fdf9a60065b 283
kadonotakashi 0:8fdf9a60065b 284 for (int i = 0; i <= MAX_FLAG_POS; i++) {
kadonotakashi 0:8fdf9a60065b 285 uint32_t flag = 1 << i;
kadonotakashi 0:8fdf9a60065b 286
kadonotakashi 0:8fdf9a60065b 287 ret = ef.wait_any(flag, 10);
kadonotakashi 0:8fdf9a60065b 288 TEST_ASSERT_EQUAL(osFlagsErrorTimeout, ret);
kadonotakashi 0:8fdf9a60065b 289
kadonotakashi 0:8fdf9a60065b 290 sync_sem.release();
kadonotakashi 0:8fdf9a60065b 291 ret = ef.wait_any(flag, 10);
kadonotakashi 0:8fdf9a60065b 292 TEST_ASSERT_EQUAL(flag, ret);
kadonotakashi 0:8fdf9a60065b 293 }
kadonotakashi 0:8fdf9a60065b 294 ret = ef.get();
kadonotakashi 0:8fdf9a60065b 295 TEST_ASSERT_EQUAL(NO_FLAGS, ret);
kadonotakashi 0:8fdf9a60065b 296 }
kadonotakashi 0:8fdf9a60065b 297
kadonotakashi 0:8fdf9a60065b 298 /** Test if multi-threaded flag set cause wait_any(without clear) to return
kadonotakashi 0:8fdf9a60065b 299
kadonotakashi 0:8fdf9a60065b 300 Given a EventFlags object and three threads are started in parallel
kadonotakashi 0:8fdf9a60065b 301 When threads set specified flags
kadonotakashi 0:8fdf9a60065b 302 Then main thread waits until receive all of them
kadonotakashi 0:8fdf9a60065b 303 */
kadonotakashi 0:8fdf9a60065b 304 void test_multi_thread_any_no_clear(void)
kadonotakashi 0:8fdf9a60065b 305 {
kadonotakashi 0:8fdf9a60065b 306 EventFlags ef;
kadonotakashi 0:8fdf9a60065b 307 uint32_t ret;
kadonotakashi 0:8fdf9a60065b 308 Thread thread1(osPriorityNormal, THREAD_STACK_SIZE);
kadonotakashi 0:8fdf9a60065b 309 Thread thread2(osPriorityNormal, THREAD_STACK_SIZE);
kadonotakashi 0:8fdf9a60065b 310 Thread thread3(osPriorityNormal, THREAD_STACK_SIZE);
kadonotakashi 0:8fdf9a60065b 311 thread1.start(callback(send_thread<FLAG01, 1>, &ef));
kadonotakashi 0:8fdf9a60065b 312 thread2.start(callback(send_thread<FLAG02, 1>, &ef));
kadonotakashi 0:8fdf9a60065b 313 thread3.start(callback(send_thread<FLAG03, 1>, &ef));
kadonotakashi 0:8fdf9a60065b 314
kadonotakashi 0:8fdf9a60065b 315 for (int i = 0; i <= MAX_FLAG_POS; i++) {
kadonotakashi 0:8fdf9a60065b 316 uint32_t flag = 1 << i;
kadonotakashi 0:8fdf9a60065b 317 ret = ef.wait_any(flag, osWaitForever, false);
kadonotakashi 0:8fdf9a60065b 318 TEST_ASSERT(flag | ret);
kadonotakashi 0:8fdf9a60065b 319 ret = ef.clear(flag);
kadonotakashi 0:8fdf9a60065b 320 TEST_ASSERT(ret < osFlagsError);
kadonotakashi 0:8fdf9a60065b 321 }
kadonotakashi 0:8fdf9a60065b 322 ret = ef.get();
kadonotakashi 0:8fdf9a60065b 323 TEST_ASSERT_EQUAL(NO_FLAGS, ret);
kadonotakashi 0:8fdf9a60065b 324 }
kadonotakashi 0:8fdf9a60065b 325
kadonotakashi 0:8fdf9a60065b 326 /** Test multi-threaded wait_any
kadonotakashi 0:8fdf9a60065b 327
kadonotakashi 0:8fdf9a60065b 328 Given a EventFlags object and three threads are started in parallel
kadonotakashi 0:8fdf9a60065b 329 When flags are set in main thread
kadonotakashi 0:8fdf9a60065b 330 Then other threads waits until receive all of them
kadonotakashi 0:8fdf9a60065b 331 */
kadonotakashi 0:8fdf9a60065b 332 void test_multi_thread_all_many_wait(void)
kadonotakashi 0:8fdf9a60065b 333 {
kadonotakashi 0:8fdf9a60065b 334 EventFlags ef;
kadonotakashi 0:8fdf9a60065b 335 {
kadonotakashi 0:8fdf9a60065b 336 Thread thread1(osPriorityNormal, THREAD_STACK_SIZE);
kadonotakashi 0:8fdf9a60065b 337 Thread thread2(osPriorityNormal, THREAD_STACK_SIZE);
kadonotakashi 0:8fdf9a60065b 338 Thread thread3(osPriorityNormal, THREAD_STACK_SIZE);
kadonotakashi 0:8fdf9a60065b 339 thread1.start(callback(wait_thread_all<FLAG01>, &ef));
kadonotakashi 0:8fdf9a60065b 340 thread2.start(callback(wait_thread_all<FLAG02>, &ef));
kadonotakashi 0:8fdf9a60065b 341 thread3.start(callback(wait_thread_all<FLAG03>, &ef));
kadonotakashi 0:8fdf9a60065b 342
kadonotakashi 0:8fdf9a60065b 343 ef.set(FLAG01 | FLAG02 | FLAG03);
kadonotakashi 0:8fdf9a60065b 344 thread1.join();
kadonotakashi 0:8fdf9a60065b 345 thread2.join();
kadonotakashi 0:8fdf9a60065b 346 thread3.join();
kadonotakashi 0:8fdf9a60065b 347 TEST_ASSERT_EQUAL(NO_FLAGS, ef.get());
kadonotakashi 0:8fdf9a60065b 348 }
kadonotakashi 0:8fdf9a60065b 349
kadonotakashi 0:8fdf9a60065b 350 {
kadonotakashi 0:8fdf9a60065b 351 Thread thread1(osPriorityNormal, THREAD_STACK_SIZE);
kadonotakashi 0:8fdf9a60065b 352 Thread thread2(osPriorityNormal, THREAD_STACK_SIZE);
kadonotakashi 0:8fdf9a60065b 353 Thread thread3(osPriorityNormal, THREAD_STACK_SIZE);
kadonotakashi 0:8fdf9a60065b 354 thread1.start(callback(wait_thread_all<FLAG01>, &ef));
kadonotakashi 0:8fdf9a60065b 355 thread2.start(callback(wait_thread_all<FLAG02>, &ef));
kadonotakashi 0:8fdf9a60065b 356 thread3.start(callback(wait_thread_all<FLAG03>, &ef));
kadonotakashi 0:8fdf9a60065b 357
kadonotakashi 0:8fdf9a60065b 358 ef.set(FLAG01);
kadonotakashi 0:8fdf9a60065b 359 thread1.join();
kadonotakashi 0:8fdf9a60065b 360 ef.set(FLAG02);
kadonotakashi 0:8fdf9a60065b 361 thread2.join();
kadonotakashi 0:8fdf9a60065b 362 ef.set(FLAG03);
kadonotakashi 0:8fdf9a60065b 363 thread3.join();
kadonotakashi 0:8fdf9a60065b 364 TEST_ASSERT_EQUAL(NO_FLAGS, ef.get());
kadonotakashi 0:8fdf9a60065b 365 }
kadonotakashi 0:8fdf9a60065b 366 }
kadonotakashi 0:8fdf9a60065b 367
kadonotakashi 0:8fdf9a60065b 368 utest::v1::status_t test_setup(const size_t number_of_cases)
kadonotakashi 0:8fdf9a60065b 369 {
kadonotakashi 0:8fdf9a60065b 370 GREENTEA_SETUP(10, "default_auto");
kadonotakashi 0:8fdf9a60065b 371 return utest::v1::verbose_test_setup_handler(number_of_cases);
kadonotakashi 0:8fdf9a60065b 372 }
kadonotakashi 0:8fdf9a60065b 373
kadonotakashi 0:8fdf9a60065b 374 Case cases[] = {
kadonotakashi 0:8fdf9a60065b 375 Case("Test empty clear", test_empty_clear),
kadonotakashi 0:8fdf9a60065b 376 Case("Test empty get", test_empty_get),
kadonotakashi 0:8fdf9a60065b 377 Case("Test empty set", test_empty_set),
kadonotakashi 0:8fdf9a60065b 378 Case("Test clear/set with prohibited flag", test_prohibited),
kadonotakashi 0:8fdf9a60065b 379 Case("Test set/get/clear for full flag range", test_set_get_clear_full_flag_range),
kadonotakashi 0:8fdf9a60065b 380 Case("Test multi-threaded wait_all", test_multi_thread_all),
kadonotakashi 0:8fdf9a60065b 381 Case("Test multi-threaded wait_any", test_multi_thread_any),
kadonotakashi 0:8fdf9a60065b 382 Case("Test multi-threaded wait_all many wait", test_multi_thread_all_many_wait),
kadonotakashi 0:8fdf9a60065b 383 Case("Test multi-threaded wait_any timeout", test_multi_thread_any_timeout),
kadonotakashi 0:8fdf9a60065b 384 Case("Test multi-threaded wait_any no clear", test_multi_thread_any_no_clear)
kadonotakashi 0:8fdf9a60065b 385 };
kadonotakashi 0:8fdf9a60065b 386
kadonotakashi 0:8fdf9a60065b 387 utest::v1::Specification specification(test_setup, cases);
kadonotakashi 0:8fdf9a60065b 388
kadonotakashi 0:8fdf9a60065b 389 int main()
kadonotakashi 0:8fdf9a60065b 390 {
kadonotakashi 0:8fdf9a60065b 391 return !utest::v1::Harness::run(specification);
kadonotakashi 0:8fdf9a60065b 392 }