/*
 *  Copyright 2010 by Texas Instruments Incorporated.
 *  All rights reserved. Property of Texas Instruments Incorporated.
 *  Restricted rights to use, duplicate or disclose this code are
 *  granted through contract.
 *
 */
/***************************************************************************/
/*                                                                         */
/*     H E L L O . C                                                       */
/*                                                                         */
/*     Basic LOG event operation from main.                                */
/*                                                                         */
/***************************************************************************/

#include <std.h>

#include <log.h>
#include <sem.h>
#include <tsk.h>
#include <clk.h>

#include "hellocfg.h"


int N;
int n_loop;
int sum;

int sum_loop( int loop){
	 int i = 0, sum = 0;
	sum = 0;
	for(i = 0; i <= loop; i ++){
		sum = sum + i;
	}
	return sum;
}

void InitTsk(int n){
	N = n;
	LOG_printf(&trace, "InitTsk() Run  at time 0x%x \n", CLK_gethtime());
	LOG_printf(&trace, "Init N with val %d \n",  N);
}

void Sink(){
	while(1){
		SEM_pend(&SEM0, SYS_FOREVER);
		sum = sum_loop(n_loop);
		LOG_printf(&trace, "Sink() Run  at time 0x%x , sum is %d", CLK_gethtime(), sum);
	}
}


void Source() {
	static int cnt = 0;
	while (1) {
		if (cnt < N) {
			n_loop = cnt;
			cnt ++;
			LOG_printf(&trace, "Source() Run  at time 0x%x , set n_loop as %d", CLK_gethtime(), n_loop);
			SEM_post(&SEM0);
			TSK_yield();
		}
		else return;
	}
}


/*
 *  ======== main ========
 */
Void main()
{
	int N_init = 9;

    LOG_printf(&trace, "Main Run at time 0x%x \n", CLK_gethtime());


    InitTsk(N_init);

    /* fall into DSP/BIOS idle loop */
    return;
}


