Files
atomthreads/tests/timer2.c
2010-01-14 01:53:45 +00:00

176 lines
5.0 KiB
C

/*
* Copyright (c) 2010, Kelvin Lawson. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. No personal names or organizations' names associated with the
* Atomthreads project may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE ATOMTHREADS PROJECT AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "atom.h"
#include "atomtimer.h"
#include "atomtests.h"
/* Test period (in seconds) */
#define TEST_PERIOD_SECS 10
/* Test OS objects */
static ATOM_TCB tcb1, tcb2, tcb3;
static uint8_t test1_thread_stack[TEST_THREAD_STACK_SIZE];
static uint8_t test2_thread_stack[TEST_THREAD_STACK_SIZE];
static uint8_t test3_thread_stack[TEST_THREAD_STACK_SIZE];
/* Per-thread failure counts */
static volatile int g_failure_cnt[3];
/* Forward declarations */
static void test_thread_func (uint32_t data);
/**
* \b test_start
*
* Start timer test.
*
* Tests that atomTimerDelay() delays for the correct time
* period when used by three threads simultanously.
*
* @retval Number of failures
*/
uint32_t test_start (void)
{
int failures;
/* Default to zero failures */
failures = 0;
g_failure_cnt[0] = g_failure_cnt[1] = g_failure_cnt[2] = 0;
/* Create Thread 1 */
if (atomThreadCreate(&tcb1, TEST_THREAD_PRIO, test_thread_func, 1,
&test1_thread_stack[TEST_THREAD_STACK_SIZE - 1]) != ATOM_OK)
{
/* Fail */
ATOMLOG (_STR("Thread1\n"));
failures++;
}
/* Create Thread 2 */
if (atomThreadCreate(&tcb2, TEST_THREAD_PRIO, test_thread_func, 2,
&test2_thread_stack[TEST_THREAD_STACK_SIZE - 1]) != ATOM_OK)
{
/* Fail */
ATOMLOG (_STR("Thread2\n"));
failures++;
}
/* Create Thread 3 */
if (atomThreadCreate(&tcb3, TEST_THREAD_PRIO, test_thread_func, 3,
&test3_thread_stack[TEST_THREAD_STACK_SIZE - 1]) != ATOM_OK)
{
/* Fail */
ATOMLOG (_STR("Thread3\n"));
failures++;
}
/* Sleep for 10 seconds allowing the three threads to run tests */
if (atomTimerDelay(TEST_PERIOD_SECS * SYSTEM_TICKS_PER_SEC) != ATOM_OK)
{
ATOMLOG (_STR("Period\n"));
failures++;
}
/* Add the per-thread failure count to the main count */
failures += g_failure_cnt[0] + g_failure_cnt[1] + g_failure_cnt[2];
/* Log final status */
if (failures == 0)
{
ATOMLOG (_STR("Pass\n"));
}
else
{
ATOMLOG (_STR("Fail(%d)\n"), failures);
}
/* Quit */
return failures;
}
/**
* \b test_thread_func
*
* Entry point for test thread. The same thread entry point is used for all
* three test threads, with the thread number/ID (1-3) passed as the entry
* point parameter.
*
* @param[in] data Thread number (1,2,3)
*
* @return None
*/
static void test_thread_func (uint32_t data)
{
uint8_t thread_id;
uint32_t start_time, end_time;
/* Thread ID is passed through the function parameter */
thread_id = (uint8_t)data;
/*
* Sleep for 1 tick to ensure that the thread starts near
* a timer tick boundary. This ensures that the system
* tick does not advance between the atomTimeGet() call
* and the actual atomTimerDelay() call being made.
*/
atomTimerDelay (1);
/* Loop running the test forever */
while (1)
{
/* Record the start time */
start_time = atomTimeGet();
/* Sleep for n ticks, where n is the thread ID */
if (atomTimerDelay(thread_id) != ATOM_OK)
{
g_failure_cnt[thread_id-1]++;
}
else
{
/* Record the time we woke up */
end_time = atomTimeGet();
/* Check that time has advanced by exactly n ticks */
if ((end_time - start_time) != thread_id)
{
g_failure_cnt[thread_id-1]++;
}
}
}
}