Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 128 additions & 0 deletions TESTS/mbed_timing_fpga_ci_test_shield/basic/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
* Copyright (c) 2013-2019, ARM Limited, All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*
* 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 "mbed.h"
#include "greentea-client/test_env.h"
#include "utest/utest.h"
#include "unity/unity.h"
#include "MbedTester.h"

#if !COMPONENT_FPGA_CI_TEST_SHIELD
#error [NOT_SUPPORTED] FPGA CI Test Shield is needed to run this test
#endif

#if defined(MBED_RTOS_SINGLE_THREAD)
#error [NOT_SUPPORTED] test not supported
#endif

#if !DEVICE_USTICKER
#error [NOT_SUPPORTED] test not supported
#endif

using utest::v1::Case;

#if defined(__CORTEX_M23) || defined(__CORTEX_M33)
#define TEST_STACK_SIZE 512
#else
#define TEST_STACK_SIZE 256
#endif
#define ONE_MILLI_SEC 1000
#define DELAY_MS 1000 // 1 sec

volatile uint32_t elapsed_time_ms = 0;
static const int test_timeout = 40;

const PinList *form_factor = pinmap_ff_default_pins();
const PinList *restricted = pinmap_restricted_pins();
MbedTester tester(form_factor, restricted);

void update_tick_thread(Mutex *mutex)
{
while (true) {
ThisThread::sleep_for(1);
mutex->lock();
++elapsed_time_ms;
mutex->unlock();
}
}

/** Tests is to measure the accuracy of ThisThread::sleep_for() over a period of time

Given
a thread updating elapsed_time_ms every milli sec
and host script for time measurement accuracy check (More details on tests can be found in timing_drift_auto.py)
When host query what is current count base_time
Then Device responds by the elapsed_time_ms
When host query what is current count final_time
Then Device responds by the elapsed_time_ms
When host computes the drift considering base_time, final_time, transport delay and measurement stretch
Then host send the results back to device pass/fail based on tolerance
*/
void test(void)
{
char _key[11] = { };
char _value[128] = { };
int expected_key = 1;
Mutex mutex;
uint32_t elapsed_time;
uint32_t start, stop;

tester.reset();

tester.timer_init();

tester.peripherals_reset();

tester.select_peripheral(MbedTester::PeripheralTimer);

tester.timer_set_mode(MbedTester::TimerModeCountDownTimer);

tester.timer_set_delay_ms(DELAY_MS);

tester.timer_reset();

Thread tick_thread(osPriorityHigh, TEST_STACK_SIZE);
tick_thread.start(callback(update_tick_thread, &mutex));

mutex.lock();
start = elapsed_time_ms;
mutex.unlock();

tester.timer_delay();

mutex.lock();
stop = elapsed_time_ms;
mutex.unlock();

TEST_ASSERT_UINT32_WITHIN(1, DELAY_MS, stop - start);
}

Case cases[] = {
Case("Test ThisThread::sleep_for accuracy", test)
};

utest::v1::status_t greentea_test_setup(const size_t number_of_cases)
{
GREENTEA_SETUP(test_timeout, "default_auto");
return utest::v1::greentea_test_setup_handler(number_of_cases);
}

utest::v1::Specification specification(greentea_test_setup, cases);

int main()
{
utest::v1::Harness::run(specification);
}
211 changes: 211 additions & 0 deletions TESTS/mbed_timing_fpga_ci_test_shield/common_tickers_freq/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
/*
* Copyright (c) 2013-2019, ARM Limited, All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*
* 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.
*/

/*
* This is the mbed device part of the test to verify if mbed board ticker
* freqency is valid.
*/

#include "mbed.h"
#include "greentea-client/test_env.h"
#include "utest/utest.h"
#include "unity/unity.h"
#include "ticker_api_test_freq.h"
#include "hal/us_ticker_api.h"
#include "hal/lp_ticker_api.h"
#include "hal/mbed_lp_ticker_wrapper.h"
#include "MbedTester.h"

#if !COMPONENT_FPGA_CI_TEST_SHIELD
#error [NOT_SUPPORTED] FPGA CI Test Shield is needed to run this test
#endif

#if !DEVICE_USTICKER
#error [NOT_SUPPORTED] test not supported
#endif

#define US_PER_S 1000000
#define DELTA_FREQ(freq) ((freq) / 10) // 10% delta

using namespace utest::v1;

const ticker_interface_t *intf;
uint32_t intf_mask;
uint32_t intf_last_tick;
uint32_t intf_elapsed_ticks;
ticker_irq_handler_type prev_handler;

const PinList *form_factor = pinmap_ff_default_pins();
const PinList *restricted = pinmap_restricted_pins();
MbedTester tester(form_factor, restricted);

uint32_t ticks_to_us(uint32_t ticks, uint32_t freq)
{
return (uint32_t)((uint64_t)ticks * US_PER_S / freq);
}

void elapsed_ticks_reset()
{
core_util_critical_section_enter();

const uint32_t ticker_bits = intf->get_info()->bits;

intf_mask = (1 << ticker_bits) - 1;
intf_last_tick = intf->read();
intf_elapsed_ticks = 0;

core_util_critical_section_exit();
}

uint32_t elapsed_ticks_update()
{
core_util_critical_section_enter();

const uint32_t current_tick = intf->read();
intf_elapsed_ticks += (current_tick - intf_last_tick) & intf_mask;
intf_last_tick = current_tick;

/* Schedule next interrupt half way to overflow */
uint32_t next = (current_tick + intf_mask / 2) & intf_mask;
intf->set_interrupt(next);

core_util_critical_section_exit();
return intf_elapsed_ticks;
}

void ticker_event_handler_stub(const ticker_data_t *const ticker)
{
intf->clear_interrupt();
elapsed_ticks_update();
}

/* Test that the ticker is operating at the frequency it specifies. */
void ticker_frequency_test()
{
const uint32_t ticker_freq = intf->get_info()->frequency;

intf->init();

/* Detect overflow for tickers with lower counters width. */
elapsed_ticks_update();

tester.reset();

tester.timer_init();

tester.peripherals_reset();

tester.select_peripheral(MbedTester::PeripheralTimer);

tester.timer_set_mode(MbedTester::TimerModeCountDownTimer);

tester.timer_set_delay_ms(1000);

tester.timer_reset();

elapsed_ticks_reset();

const uint32_t begin_ticks = elapsed_ticks_update();

tester.timer_delay();

const uint32_t end_ticks = elapsed_ticks_update();

const uint32_t diff_ticks = end_ticks - begin_ticks;

tester.timer_free();

intf->disable_interrupt();

TEST_ASSERT_UINT32_WITHIN(DELTA_FREQ(ticker_freq), ticker_freq, diff_ticks);
}

utest::v1::status_t us_ticker_case_setup_handler_t(const Case *const source, const size_t index_of_case)
{
#if DEVICE_LPTICKER && (LPTICKER_DELAY_TICKS > 0)
/* Suspend the lp ticker wrapper since it makes use of the us ticker */
ticker_suspend(get_lp_ticker_data());
lp_ticker_wrapper_suspend();
#endif
ticker_suspend(get_us_ticker_data());
intf = get_us_ticker_data()->interface;
prev_handler = set_us_ticker_irq_handler(ticker_event_handler_stub);
return greentea_case_setup_handler(source, index_of_case);
}

utest::v1::status_t us_ticker_case_teardown_handler_t(const Case *const source, const size_t passed, const size_t failed,
const failure_t reason)
{
set_us_ticker_irq_handler(prev_handler);
ticker_resume(get_us_ticker_data());
#if DEVICE_LPTICKER && (LPTICKER_DELAY_TICKS > 0)
lp_ticker_wrapper_resume();
ticker_resume(get_lp_ticker_data());
#endif
return greentea_case_teardown_handler(source, passed, failed, reason);
}

#if DEVICE_LPTICKER
utest::v1::status_t lp_ticker_case_setup_handler_t(const Case *const source, const size_t index_of_case)
{
ticker_suspend(get_lp_ticker_data());
intf = get_lp_ticker_data()->interface;
prev_handler = set_lp_ticker_irq_handler(ticker_event_handler_stub);
return greentea_case_setup_handler(source, index_of_case);
}

utest::v1::status_t lp_ticker_case_teardown_handler_t(const Case *const source, const size_t passed, const size_t failed,
const failure_t reason)
{
set_lp_ticker_irq_handler(prev_handler);
ticker_resume(get_lp_ticker_data());
return greentea_case_teardown_handler(source, passed, failed, reason);
}
#endif

// Test cases
Case cases[] = {
Case("Microsecond ticker frequency test", us_ticker_case_setup_handler_t, ticker_frequency_test,
us_ticker_case_teardown_handler_t),
#if DEVICE_LPTICKER
Case("Low power ticker frequency test", lp_ticker_case_setup_handler_t, ticker_frequency_test,
lp_ticker_case_teardown_handler_t),
#endif
};

utest::v1::status_t greentea_test_setup(const size_t number_of_cases)
{
/* Suspend RTOS Kernel so the timers are not in use. */
osKernelSuspend();

GREENTEA_SETUP(120, "default_auto");
return greentea_test_setup_handler(number_of_cases);
}

void greentea_test_teardown(const size_t passed, const size_t failed, const failure_t failure)
{
osKernelResume(0);

greentea_test_teardown_handler(passed, failed, failure);
}

Specification specification(greentea_test_setup, cases, greentea_test_teardown);

int main()
{
Harness::run(specification);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* mbed Microcontroller Library
* Copyright (c) 2017-2019 ARM Limited
* SPDX-License-Identifier: Apache-2.0
*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add SPDX identifiers to new files

* 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_ticker_tests */
/** @{*/

#ifndef TICKER_API_TEST_FREQ_H
#define TICKER_API_TEST_FREQ_H

#include "device.h"


#ifdef __cplusplus
extern "C" {
#endif

/** Test that the ticker is operating at the frequency it specifies.
*
* Given ticker is available.
* When ticker specifies its frequency.
* Then the specified frequency is valid.
*/
void ticker_frequency_test(void);


/**@}*/

#ifdef __cplusplus
}
#endif

#endif

/**@}*/
Loading