-
Notifications
You must be signed in to change notification settings - Fork 3k
Add tests for critical section HAL API #5789
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
cmonr
merged 1 commit into
ARMmbed:master
from
maciejbocianski:hal_critical_section_tests
Jan 26, 2018
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* mbed Microcontroller Library | ||
* Copyright (c) 2018 ARM Limited | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/** \addtogroup hal_critical_tests | ||
* @{ | ||
*/ | ||
|
||
#ifndef MBED_CRITICAL_SECTION_TEST_H | ||
#define MBED_CRITICAL_SECTION_TEST_H | ||
|
||
/** Template for HAL critical section tests | ||
* | ||
* Test critical section | ||
* Given a critical section HAL mechanism | ||
* When before critical section | ||
* Then interrupts are enabled | ||
* When inside critical section | ||
* Then interrupts are disabled | ||
* When after critical section | ||
* Then interrupts are enabled again | ||
* | ||
* Test critical section - nested lock | ||
* Given a critical section HAL mechanism | ||
* When before critical section | ||
* Then interrupts are enabled | ||
* When inside nested critical section | ||
* Then interrupts are disabled | ||
* When after nested critical section | ||
* Then interrupts are enabled again | ||
* | ||
*/ | ||
template <int N> | ||
void test_critical_section(); | ||
|
||
|
||
/**@}*/ | ||
|
||
#endif // MBED_CRITICAL_SECTION_TEST_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* mbed Microcontroller Library | ||
* Copyright (c) 2018 ARM Limited | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
#include "critical_section_test.h" | ||
#include "hal/critical_section_api.h" | ||
#include "utest/utest.h" | ||
#include "unity/unity.h" | ||
#include "greentea-client/test_env.h" | ||
#include "mbed.h" | ||
#include "cmsis.h" | ||
#ifdef TARGET_NRF5 // for all NRF5x targets | ||
#include "nrf_nvic.h" // for __NRF_NVIC_APP_IRQS_0 / __NRF_NVIC_APP_IRQS_1 | ||
#endif | ||
|
||
using utest::v1::Case; | ||
|
||
bool test_are_interrupts_enabled(void) | ||
{ | ||
// NRF5x targets don't disable interrupts when in critical section, instead they mask application interrupts this is due to BLE stack | ||
// (BLE to be operational requires some interrupts to be always enabled) | ||
#ifdef TARGET_NRF52_DK | ||
// check if APP interrupts are masked for NRF52_DK board | ||
return (((NVIC->ISER[0] & __NRF_NVIC_APP_IRQS_0) != 0) || ((NVIC->ISER[1] & __NRF_NVIC_APP_IRQS_1) != 0)); | ||
#elif TARGET_NRF5 | ||
// check if APP interrupts are masked for other NRF5 boards | ||
return ((NVIC->ISER[0] & __NRF_NVIC_APP_IRQS_0) != 0); | ||
#else | ||
#if defined(__CORTEX_A9) | ||
return ((__get_CPSR() & 0x80) == 0); | ||
#else | ||
return ((__get_PRIMASK() & 0x1) == 0); | ||
#endif | ||
#endif | ||
} | ||
|
||
|
||
template<int N> | ||
void test_critical_section() | ||
{ | ||
TEST_ASSERT_FALSE(hal_in_critical_section()); | ||
TEST_ASSERT_TRUE(test_are_interrupts_enabled()); | ||
|
||
for (int i = 0; i < N; i++) { | ||
hal_critical_section_enter(); | ||
TEST_ASSERT_TRUE(hal_in_critical_section()); | ||
TEST_ASSERT_FALSE(test_are_interrupts_enabled()); | ||
} | ||
|
||
// assumed to be called once (according API) | ||
hal_critical_section_exit(); | ||
|
||
TEST_ASSERT_FALSE(hal_in_critical_section()); | ||
TEST_ASSERT_TRUE(test_are_interrupts_enabled()); | ||
} | ||
|
||
Case cases[] = { | ||
Case("Test critical section single lock", test_critical_section<1>), | ||
Case("Test critical section nested lock", test_critical_section<10>) | ||
}; | ||
|
||
utest::v1::status_t greentea_test_setup(const size_t number_of_cases) | ||
{ | ||
GREENTEA_SETUP(10, "timing_drift_auto"); | ||
return utest::v1::greentea_test_setup_handler(number_of_cases); | ||
} | ||
|
||
utest::v1::Specification specification(greentea_test_setup, cases); | ||
|
||
int main() | ||
{ | ||
return !utest::v1::Harness::run(specification); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wasn't there somewhere a request for having function to check if interrupts are enabled? Or even an implementation?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
indeed, there is function
hal_in_critical_section()
, but it would be nice to verify API functions by something form outside critical section API.us_ticker
could be used to check interrupts but unfortunately it utilisescore_util_critical_section_
(which also call HAL critical API ) and it brakes the test. Generly we should always use platformcore_util_critical_section
not HAL, in particular we cannot use both in the same timeThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was concerned about the portability (how this function can grow in time).
Looks fine to test this here.