/*
 *               Copyright (C) 2005, BroadOn Communications Corp.
 *
 *  These coded instructions, statements, and computer programs contain
 *  unpublished  proprietary information of BroadOn Communications Corp.,
 *  and  are protected by Federal copyright law. They may not be disclosed
 *  to  third  parties or copied or duplicated in any form, in whole or in
 *  part, without the prior written consent of BroadOn Communications Corp.
 *
 */
#include <types.h>
#include <sys.h>
#include <ioslibc.h>
#include <ios.h>
#include <iop.h>
#define STACK_SIZE 1024
const u8 _initStack[STACK_SIZE];
const u32 _initStackSize = sizeof(_initStack);
const u32 _initPriority = 10;

#define WAIT_T 3000 /* us */
#define NITER  100
void
main(u32 arg)
{
//  u32 n = NITER;
  u32  i = 0;
  u32 end_time;
  u32 start_time;

  IOSMessageQueueId mq;
  IOSMessage array[4];
  IOSMessage m;
  IOSTimerId t1;

  /* Initialize timer */
  mq = IOS_CreateMessageQueue(array, 4);
  t1 = IOS_CreateTimer(0, 0, mq, 0xdeadbeef);
 
  IOS_StopTimer(t1);

  start_time = busRd32(SYS_TIMER_REG);
  for (i = 0; i < NITER; ++i ) {
      IOS_RestartTimer(t1, WAIT_T, 0);
      IOS_ReceiveMessage(mq, &m, IOS_MESSAGE_BLOCK);
      IOS_StopTimer(t1);
  }
  end_time = busRd32(SYS_TIMER_REG);
  
  printf("udelay(%u x %u): start_ticks: %08x end_ticks: %08x delta: %u\n", 
          WAIT_T, NITER, start_time, end_time, end_time - start_time);
  printf("Expected ticks = %u (+ delays from 3*(%u)  syscalls)\n", NITER*US_TO_TICKS_243(WAIT_T), NITER);
  printf("Error: %d\n", end_time - start_time - NITER*US_TO_TICKS_243(WAIT_T));


}
