/*
 * configuration.c
 *
 *  Created on: Aug 1, 2012
 *      Author: naraharid
 */

#include <stdio.h>
#include "configuration.h"
#include "drivers/mss_uart/mss_uart.h"


#define DEFAULT_WEIGHT0_VALUE 0X00000000           //0X00000000  is the value if all the masters weight(default) is 32
#define DEFAULT_WEIGHT1_VALUE 0X000				   //0X00000000  is the value if all the masters weight(default) is 32

uint32_t weight_value;
void AHBBus_init()
{

*(volatile uint32_t *)ESRAM_MAX_LAT = 0x3F;
*(volatile uint32_t *)MASTER_WEIGHT0_CR = DEFAULT_WEIGHT0_VALUE;
*(volatile uint32_t *)MASTER_WEIGHT1_CR = DEFAULT_WEIGHT1_VALUE;
*(volatile uint32_t *)MSS_EXTERNAL_SR = 0;

}

/***************************************************************
 * See "configuration.h" for details of how to use this function.
 ***************************************************************/
void master_select(uint32_t *master, uint32_t *weight, int mast_no)
{

	int i,config_reg;

	for(i=0;i<mast_no;i++)
	{
	//	weight_value = 0;
		if(weight[i]==32)
		weight[i]=0;
	switch (master[i])
		{

		case 1:  weight_value = (weight[i]<<15); //FIC_0
				config_reg = 0;

				break;
		case 2: weight_value = (weight[i]<<20);   //FIC1
				config_reg = 0;

				break;
		case 3:  weight_value = (weight[i]<<25);   //PDMA
				 config_reg = 0;
				 break;
		case 4: weight_value = (weight[i]);   //IC
				config_reg = 0;

				break;
		case 5: weight_value =  (weight[i]<<5);   //S
				config_reg = 0;

				break;
		case 6: weight_value = (weight[i]<<10);   //GIGE
				config_reg = 0;

				break;
		case 7: weight_value =  (weight[i]);   //HPDMA
				config_reg = 1;
				break;
		case 8:
				weight_value =  (weight[i]<<5);   //USB
				config_reg = 1;
				break;
		case 9:
				weight_value =  (weight[i]<<10);   //G
				config_reg = 1;

				break;

		}
		set_weight(weight_value,config_reg);
	}
}





/***************************************************************
 * See "configuration.h" for details of how to use this function.
 ***************************************************************/

void set_weight(uint32_t weight, int config_reg)
{
	if(config_reg == 0)
	{
		*(volatile uint32_t *)MASTER_WEIGHT0_CR = ((*(volatile uint32_t *)MASTER_WEIGHT0_CR) |weight);
	}
	else
	{
		*(volatile uint32_t *)MASTER_WEIGHT1_CR|= weight;
	}
}


/***************************************************************
 * See "configuration.h" for details of how to use this function.
 ***************************************************************/

void set_latency(uint32_t latency,int slave)
{
	if(slave == 2)
	{
		if (latency == 8)
			*(volatile uint32_t *)ESRAM_MAX_LAT = 0;
		else
			*(volatile uint32_t *)ESRAM_MAX_LAT &= (latency << 3);
	}

	if(slave == 1)
	{
		if (latency == 8)
			*(volatile uint32_t *)ESRAM_MAX_LAT = 0;
		else
			*(volatile uint32_t *)ESRAM_MAX_LAT &= latency ;
	}

}











