timer1.c 4.44 KB
#include <os.h>
#include <rmon.h>
#include <R4300.h>
#include "osprof.h"

/*
 * timer1
 *
 * Tests for:
 *	osSetTimer
 *	osStopTimer
 *
 * Assumed working:
 *	osGetIntMask
 *	osCreateMesgQueue
 *	osRecvMessage
 *
 * Description:
 *	Setup couple of countdown timers and check that the messages are 
 * 		delivered when timers expire
 *	Check that countdown timers are not on the timer list anymore.
 *	Setup couple of interval timers and check that the messages are
 *		delivered every interval time
 *	Stop interval timer
 *	Check that message queue is empty
 *	Setup couple of interval and countdown timers and check that the
 *		message is delivered when countdown timer expire and the
 *		new messages are delivered every interval time
 *	Stop interval timer
 *	Check that message queue is empty
 */

#define	NUM_MESSAGE	4
#define	NUMBER_TIMER	3

static OSMesgQueue	timerMesgQueue;
static OSMesg		timerMesgBuf[NUM_MESSAGE];
static OSTimer		mytimer[NUMBER_TIMER];

int
timer1(void)
{
    OSMesg actualMesg, expectedMesg[NUMBER_TIMER];
    int i = 0;
    int j = 0;
    int retValue;
    int numFailures = 0;
    u32 count1, count2;
    


    /*
     * Check out the Countdown timer.
     */
    osCreateMesgQueue(&timerMesgQueue, timerMesgBuf, NUM_MESSAGE);

    for (i= 0; i < NUMBER_TIMER; i++) 
  	expectedMesg[0] = (OSMesg)i;

    /* setup countdown timer */

    rmonPrintf("Test countdown timer\n");
    for (i= 0; i < NUMBER_TIMER; i++) {
	osSetTimer(&mytimer[i], OS_USEC_TO_CYCLES(i*10+200), 0, 
	&timerMesgQueue, &expectedMesg[i]);
	}

    for (i= 0; i < NUMBER_TIMER; i++) {
	    (void)osRecvMesg(&timerMesgQueue, &actualMesg, OS_MESG_BLOCK);
	    if (&expectedMesg[i] != actualMesg) {
		rmonPrintf("timer1: expected message 0x%x, actual 0x%x\n",
			&expectedMesg[i], actualMesg);
		numFailures++;
	    }
    }

    for (i= 0; i < NUMBER_TIMER; i++) {
	   retValue =  osStopTimer(&mytimer[i]);
	   if (retValue != -1) {
		rmonPrintf("timer1: Countdown timer should be removed after expired\n");
		numFailures++;
      	   }

    }

    /* setup interval timer */

    rmonPrintf("Test interval timer\n");
    for (i= 0; i < NUMBER_TIMER; i++) {
	osSetTimer(&mytimer[i], 0, OS_USEC_TO_CYCLES(i*10+200), 
		   &timerMesgQueue, &expectedMesg[i]);
	}

    for (i= 0,j=0; j < NUMBER_TIMER * 5 ; j++) {
	    (void)osRecvMesg(&timerMesgQueue, &actualMesg, OS_MESG_BLOCK);
	    if (&expectedMesg[i] != actualMesg) {
		rmonPrintf("timer1: expected message 0x%x, actual 0x%x\n",
			&expectedMesg[i], actualMesg);
		numFailures++;
	    }
	    i++;
	    i = i % NUMBER_TIMER;
    }

    for (i= 0; i < NUMBER_TIMER; i++) {
	   retValue =  osStopTimer(&mytimer[i]);
	   if (retValue) {
		rmonPrintf("timer1: Interval timer should on the timer list\n");
		numFailures++;
      	   }

    }

    /*
     * Verify that the message queue is now empty.
     */
    retValue = osRecvMesg(&timerMesgQueue, &actualMesg, OS_MESG_NOBLOCK);

    if (retValue != -1) {
        rmonPrintf("timer1: expected return value %d, actual %d\n",
                -1, retValue);
        numFailures++;
    }

    /* setup interval timer and countdown timer */

    rmonPrintf(" Test interval timer and countdown timer\n");
    for (i= 0; i < NUMBER_TIMER; i++) {
	osSetTimer(&mytimer[i], OS_USEC_TO_CYCLES((NUMBER_TIMER  - i) *100), 
		   i*10000+200000, &timerMesgQueue, &expectedMesg[i]);
	}

    /* check countdown timer first */

    for (i= NUMBER_TIMER-1; i >= 0 ; i--) {
	    (void)osRecvMesg(&timerMesgQueue, &actualMesg, OS_MESG_BLOCK);
	    if (&expectedMesg[i] != actualMesg) {
		rmonPrintf("timer1: expected message 0x%x, actual 0x%x\n",
			&expectedMesg[i], actualMesg);
		numFailures++;
	    }
    }

    for (i= 0,j=0; j < NUMBER_TIMER * 5 ; j++) {
	    (void)osRecvMesg(&timerMesgQueue, &actualMesg, OS_MESG_BLOCK);
	    if (&expectedMesg[i] != actualMesg) {
		rmonPrintf("timer1: expected message 0x%x, actual 0x%x\n",
			&expectedMesg[i], actualMesg);
		numFailures++;
	    }
	    i++;
	    i = i % NUMBER_TIMER;
    }

    for (i= 0; i < NUMBER_TIMER; i++) {
	   retValue =  osStopTimer(&mytimer[i]);
	   if (retValue) {
		rmonPrintf("timer1: Interval timer should on the timer list\n");
		numFailures++;
      	   }

    }

    /*
     * Verify that the message queue is now empty.
     */
    retValue = osRecvMesg(&timerMesgQueue, &actualMesg, OS_MESG_NOBLOCK);

    if (retValue != -1) {
        rmonPrintf("timer1: expected return value %d, actual %d\n",
                -1, retValue);
        numFailures++;
    }
    return(numFailures);
}