/****************************************************************************/
/* MCF8316A.c                                                                 */
/* @Author : afly.sun                                                       */
/* Replace PMD1000 solution                                 */
/****************************************************************************/
#ifndef _MCF8316A_C_
#define _MCF8316A_C_

#ifdef __SUPPORT_MCF8316A

#include "ddp442x_rtos_include.h"

#include "dvMCF8316A.h"
#include "I2C.h"
#include "dbmessage.h"
#include "gpiofunction.h"
#include "hali2cbus.h"
#include "rta_tsk.h"
#include "tmr.h"


#define MCF8316A_I2C_PORT 	I2C_PORT0
#define MCF8316A_I2C_ADDR 	0xC0
#define MCF8316A_RETRY 		5


static DataFormat_32bit gRegisterAddressTable[RegisterMax] =
{
    {0x10, 0x00, 0x80, 0x20, 0x8C, 0x63, 0x44}, // 0x80 : init speed detect config
    {0x10, 0x00, 0x82, 0x64, 0xF0, 0x3A, 0x28}, // 0x82 : reverse driver config

    {0x10, 0x00, 0x84, 0xD0, 0x07, 0x68, 0x0B}, // 0x84 : motor start setting1
    {0x10, 0x00, 0x86, 0x04, 0x60, 0x06, 0x23}, // 0x86 : motor start setting2

    {0x10, 0x00, 0x88, 0xB0, 0x01, 0x32, 0x0D}, // 0x88 : close loop setting1
    {0x10, 0x00, 0x8A, 0x00, 0x00, 0xAD, 0x1A}, // 0x8A : close loop setting2
    {0x10, 0x00, 0x8C, 0x00, 0x00, 0x00, 0x00}, // 0x8C : close loop setting3
    {0x10, 0x00, 0x8E, 0x2C, 0x01, 0x00, 0x00}, // 0x8E : close loop setting4

    {0x10, 0x00, 0x94, 0x00, 0x00, 0x00, 0x00}, // 0x94 : config speed profile1
    {0x10, 0x00, 0x96, 0x00, 0x00, 0x00, 0x00}, // 0x96 : config speed profile2
    {0x10, 0x00, 0x98, 0x00, 0x00, 0x00, 0x00}, // 0x98 : config speed profile3
    {0x10, 0x00, 0x9A, 0x00, 0x00, 0x00, 0x00}, // 0x9A : config speed profile4
    {0x10, 0x00, 0x9C, 0x00, 0x00, 0x00, 0x00}, // 0x9C : config speed profile5
    {0x10, 0x00, 0x9E, 0x00, 0x00, 0x00, 0x00}, // 0x9E : config speed profile6

    {0x10, 0x00, 0x90, 0x0E, 0x82, 0xE8, 0x5F}, // 0x90 : config fault setting1
    {0x10, 0x00, 0x92, 0x00, 0x80, 0xC0, 0x74}, // 0x92 : config fault setting2

    {0x10, 0x00, 0xA4, 0x05, 0x00, 0x00, 0x00}, // 0xA4 : config hardware pin

    {0x10, 0x00, 0xA6, 0x00, 0x00, 0x00, 0x08}, // 0xA6 : config device1
    {0x10, 0x00, 0xA8, 0x00, 0x30, 0x00, 0x00}, // 0xA6 : config device2 ----crystal OS

    {0x10, 0x00, 0xAA, 0x00, 0x00, 0x00, 0x40}, // 0xAA : config peri

    {0x10, 0x00, 0xAC, 0x00, 0x01, 0x40, 0x1C}, // 0xAC : config gated driver setting1
    {0x10, 0x00, 0xAE, 0x00, 0x00, 0x20, 0x00}, // 0xAE : config gated driver setting2

    {0x10, 0x00, 0xEA, 0x00, 0x00, 0x00, 0x00}, // 0xEA : config algo

    {0x10, 0x00, 0xA0, 0x7D, 0x40, 0xB3, 0x04}, // 0xA0 : config init algo setting1
    {0x10, 0x00, 0xA2, 0xA7, 0x01, 0x00, 0x00}, // 0xA2 : config init algo setting2
};



/**
* Function   : dvMCF8316A_ByteWrite
* Description:
* 
*
* Input      :
* Output     :
* @return    : 
* Others     :
*/
int08 dvMCF8316A_ByteWrite(uint32  byte_count, uint08 *data_buffer_ptr)
{
	int08 cc = PASS;
	uint32 SemID, wNumWritten;
	uint08 retry = MCF8316A_RETRY, temp[8];

	I2C_GetSemaphoreID(MCF8316A_I2C_PORT, &SemID);

	if (RTA_SUCCESS != (cc = RTA_SemReserve(SemID, 1000)))
	{
		dbmsg_ftrace(DBM_ALWAYS, "MCF8316A: SemReserve failed! cc = %d\r\n", cc);
		return cc;
	}

	dbmsg_ftrace(DBM_ALWAYS, "MCF8316A: byte_count= %d\r\n", byte_count);
	temp[0] = data_buffer_ptr[0];
	dbmsg_ftrace(DBM_ALWAYS, "MCF8316A: temp[0]= %x\r\n", temp[0]);
	temp[1] = data_buffer_ptr[1];
	dbmsg_ftrace(DBM_ALWAYS, "MCF8316A: temp[1]= %x\r\n", temp[1]);
	temp[2] = data_buffer_ptr[2];
	dbmsg_ftrace(DBM_ALWAYS, "MCF8316A: temp[2]= %x\r\n", temp[2]);
	temp[3] = data_buffer_ptr[3];
	dbmsg_ftrace(DBM_ALWAYS, "MCF8316A: temp[3]= %x\r\n", temp[3]);
	temp[4] = data_buffer_ptr[4];
	dbmsg_ftrace(DBM_ALWAYS, "MCF8316A: temp[4]= %x\r\n", temp[4]);
	temp[5] = data_buffer_ptr[5];
	dbmsg_ftrace(DBM_ALWAYS, "MCF8316A: temp[5]= %x\r\n", temp[5]);
	temp[6] = data_buffer_ptr[6];
	dbmsg_ftrace(DBM_ALWAYS, "MCF8316A: temp[6]= %x\r\n", temp[6]);
	temp[7] = data_buffer_ptr[7];
	dbmsg_ftrace(DBM_ALWAYS, "MCF8316A: temp[7]= %x\r\n", temp[7]);
	

	while (retry--) // max 5 times to retry while I2C write fail
	{
		cc = I2C_MasterWrite(MCF8316A_I2C_PORT,
					MCF8316A_I2C_ADDR,
					byte_count, 					/* num write bytes */
					&temp[0],
					0, 					/* byte delay, 0 : run at once */
					1000, 				/* timeout */
					&wNumWritten );

		if (PASS != cc) 						/* if an API error occurred */
		{
			dbmsg_ftrace(DBM_ALWAYS, "NO.%d MCF8316A write byte error!!! cc = %d\r\n", MCF8316A_RETRY - retry, cc);
		}
		else
		{
			break;
		}
	}

	RTA_SemRelease(SemID);

	return cc;
}

/**
* Function   : dvMCF8316A_Init
* Description:
* 
*
* Input      :
* Output     :
* @return    : 
* Others     :
*/
int08 dvMCF8316A_Init(void)
{
	uint08 i, temp[8];
	int08 cc = PASS;

	for (i = 0; i < RegisterMax; i++)
	{
		dbmsg_ftrace(DBM_ALWAYS, "MCF8316A: [%d] = %x, %x, %x, %x, %x, %x, %x\r\n", i, gRegisterAddressTable[i].Control_0, gRegisterAddressTable[i].Control_1, gRegisterAddressTable[i].Control_2, gRegisterAddressTable[i].Data_0, gRegisterAddressTable[i].Data_1, gRegisterAddressTable[i].Data_2, gRegisterAddressTable[i].Data_3);
		temp[0] = gRegisterAddressTable[i].Control_0;
		temp[1] = gRegisterAddressTable[i].Control_1;
		temp[2] = gRegisterAddressTable[i].Control_2;
		temp[3] = gRegisterAddressTable[i].Data_0;
		temp[4] = gRegisterAddressTable[i].Data_1;
		temp[5] = gRegisterAddressTable[i].Data_2;
		temp[6] = gRegisterAddressTable[i].Data_3;

		//if (PASS != (cc = dvMCF8316A_ByteWrite(6, temp)))
		cc = dvMCF8316A_ByteWrite(7, temp);
		{
			dbmsg_ftrace(DBM_ALWAYS, "MCF8316A: Initial FAIL! cc = %d\r\n", cc);
		}
		RTA_TaskDelay(TMR_ConvertMSToTicks(100));
	}

    return cc;
}

#endif

#endif //_MCF8316A_C_

