BA / SerialCom

Fork of OmniWheels by Gustav Atmel

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2018 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 "critical_section_test.h"
00017 #include "hal/critical_section_api.h"
00018 #include "utest/utest.h"
00019 #include "unity/unity.h"
00020 #include "greentea-client/test_env.h"
00021 #include "mbed.h"
00022 #include "cmsis.h"
00023 #ifdef TARGET_NRF5 // for all NRF5x targets
00024 #include "nrf_nvic.h" // for __NRF_NVIC_APP_IRQS_0 / __NRF_NVIC_APP_IRQS_1
00025 #endif
00026 
00027 using utest::v1::Case;
00028 
00029 bool test_are_interrupts_enabled(void)
00030 {
00031     // NRF5x targets don't disable interrupts when in critical section, instead they mask application interrupts this is due to BLE stack
00032     // (BLE to be operational requires some interrupts to be always enabled)
00033 #ifdef TARGET_NRF52_DK
00034     // check if APP interrupts are masked for NRF52_DK board
00035     return (((NVIC->ISER[0] & __NRF_NVIC_APP_IRQS_0) != 0) || ((NVIC->ISER[1] & __NRF_NVIC_APP_IRQS_1) != 0));
00036 #elif TARGET_NRF5
00037     // check if APP interrupts are masked for other NRF5 boards
00038     return ((NVIC->ISER[0] & __NRF_NVIC_APP_IRQS_0) != 0);
00039 #else
00040 #if defined(__CORTEX_A9)
00041     return ((__get_CPSR() & 0x80) == 0);
00042 #else
00043     return ((__get_PRIMASK() & 0x1) == 0);
00044 #endif
00045 #endif
00046 }
00047 
00048 
00049 template<int N>
00050 void test_critical_section()
00051 {
00052     TEST_ASSERT_FALSE(hal_in_critical_section());
00053     TEST_ASSERT_TRUE(test_are_interrupts_enabled());
00054 
00055     for (int i = 0; i < N; i++) {
00056         hal_critical_section_enter();
00057         TEST_ASSERT_TRUE(hal_in_critical_section());
00058         TEST_ASSERT_FALSE(test_are_interrupts_enabled());
00059     }
00060 
00061     // assumed to be called once (according API)
00062     hal_critical_section_exit();
00063 
00064     TEST_ASSERT_FALSE(hal_in_critical_section());
00065     TEST_ASSERT_TRUE(test_are_interrupts_enabled());
00066 }
00067 
00068 Case cases[] = {
00069     Case("Test critical section single lock", test_critical_section<1>),
00070     Case("Test critical section nested lock", test_critical_section<10>)
00071 };
00072 
00073 utest::v1::status_t greentea_test_setup(const size_t number_of_cases)
00074 {
00075     GREENTEA_SETUP(10, "timing_drift_auto");
00076     return utest::v1::greentea_test_setup_handler(number_of_cases);
00077 }
00078 
00079 utest::v1::Specification specification(greentea_test_setup, cases);
00080 
00081 int main()
00082 {
00083     return !utest::v1::Harness::run(specification);
00084 }