/*
 * bank_sel.c
 *
 *  Created on: 13-12-2016
 *      Author: KJ
 */



#include "tdefs.h"
#include "misc.h"
#include "helper.h"
#include "bank_sel.h"
#include "comm.h"
#include "config.h"

#include <ti/ipc/Notify.h>


int bank_sel_initlog(void *arguments, void *logic)
{
	struct bank_sel_args *args = (struct bank_sel_args *)arguments;
	struct bank_sel_logic *log = (struct bank_sel_logic *)logic;

	if(set_bit_ptr_mask(args->io.bank0_in,&log->bank0_ptr,&log->bank0_bit_mask))
		return -1;

	if(set_bit_ptr_mask(args->io.bank1_in,&log->bank1_ptr,&log->bank1_bit_mask))
		return -1;

	if(set_bit_ptr_mask(args->io.bank2_in,&log->bank2_ptr,&log->bank2_bit_mask))
		return -1;

	if(set_bit_ptr_mask(args->io.bank3_in,&log->bank3_ptr,&log->bank3_bit_mask))
		return -1;

	if(set_bit_ptr_mask(args->io.bank4_in,&log->bank4_ptr,&log->bank4_bit_mask))
		return -1;

	if(set_bit_ptr_mask(args->io.bank5_in,&log->bank5_ptr,&log->bank5_bit_mask))
		return -1;

	if(set_bit_ptr_mask(args->io.blok_in,&log->blok_in,&log->blok_in_bit_mask))
		return -1;

	if(set_bit_ptr_mask(args->io.bank0_out,&log->bank0_out,&log->bank0_out_bit_mask))
		return -1;
	if(set_bit_ptr_mask(args->io.bank1_out,&log->bank1_out,&log->bank1_out_bit_mask))
		return -1;
	if(set_bit_ptr_mask(args->io.bank2_out,&log->bank2_out,&log->bank2_out_bit_mask))
		return -1;
	if(set_bit_ptr_mask(args->io.bank3_out,&log->bank3_out,&log->bank3_out_bit_mask))
		return -1;
	if(set_bit_ptr_mask(args->io.bank4_out,&log->bank4_out,&log->bank4_out_bit_mask))
		return -1;
	if(set_bit_ptr_mask(args->io.bank5_out,&log->bank5_out,&log->bank5_out_bit_mask))
		return -1;

	return 0;
}

void bank_sel(void *arguments, void *logic)
{
	struct bank_sel_logic *log = (struct bank_sel_logic *)logic;

	if(!(*log->blok_in & log->blok_in_bit_mask))
	{
		if(*log->bank0_ptr & log->bank0_bit_mask)
			log->sel_bank=0;
		else if(*log->bank1_ptr & log->bank1_bit_mask)
			log->sel_bank=1;
		else if(*log->bank2_ptr & log->bank2_bit_mask)
			log->sel_bank=2;
		else if(*log->bank3_ptr & log->bank3_bit_mask)
			log->sel_bank=3;
		else if(*log->bank4_ptr & log->bank4_bit_mask)
			log->sel_bank=4;
		else if(*log->bank5_ptr & log->bank5_bit_mask)
			log->sel_bank=5;
		else
			log->sel_bank=0;
	}

	check_and_set_mask(log->sel_bank==0,log->bank0_out,log->bank0_out_bit_mask);
	check_and_set_mask(log->sel_bank==1,log->bank1_out,log->bank1_out_bit_mask);
	check_and_set_mask(log->sel_bank==2,log->bank2_out,log->bank2_out_bit_mask);
	check_and_set_mask(log->sel_bank==3,log->bank3_out,log->bank3_out_bit_mask);
	check_and_set_mask(log->sel_bank==4,log->bank4_out,log->bank4_out_bit_mask);
	check_and_set_mask(log->sel_bank==5,log->bank5_out,log->bank5_out_bit_mask);

	if(log->t_cnt++ > (1200/LOOP_CYCLE_MS)) // every 1.2 secs
	{

		logman_notify|=(LOGMAN_NOTIFY_BANK0<<log->sel_bank) | LOGMAN_NOTIFY_NEW_EVENTS;
//		Notify_sendEvent(notify.remoteProcId,notify.lineId,notify.eventId, NOTIFY_SEL_BANK0+log->sel_bank, TRUE);
		log->t_cnt=0;
	}
}
