/* ==================================================================================
File name:      F280XHALL3_GPIO.H                     
Target	 : 		F2803x family             
=====================================================================================

-------------------------------------------------------------------------------------
 Hall logics are always read, not only when Hall edges are detected. 
 All Hall bits are also read at once instead of individual Hall bit be read. 
 This Hall drive use GPIOs only for Piccolo instead of eCAP
-----------------------------------------------------------------------------------*/

#ifndef __F2803X_HALL3_GPIO_H__
#define __F2803X_HALL3_GPIO_H__

#include "f2803xbmsk.h"
#include "f2803xhall_gpio_struct.h"
/*-----------------------------------------------------------------------------
 Initialization states for ECAP Control Registers 1 and 2 for ECAP1/2/3
------------------------------------------------------------------------------*/


#define HALL3_ECCTL2_INIT_STATE   ( CONTINUOUS_MODE + \
                                    TSCNTSTP_FREE + \
                                    SYNCI_DISABLE + \
                                    SYNCO_DISABLE + \
                                    CAPTURE_MODE )


/* Note:
- When HallMapPointer is as an input, it is defined by MOD6_CNT.
- When HallMapPointer is as an output, during hall map creation, 
  this variable points to the current commutation state.  
  After map creation, it points to the next commutation state.
*/

/*------------------------------------------------------------------------------
Default Initializer for the F2803X HALL3 Object
------------------------------------------------------------------------------*/
#define F2803XHALL3      {0,  \
                          0,  \
                          0,  \
                         3,  \
                          0,  \
                          0,  \
                          0,  \
                     0xFFFF,  \
              {0,0,0,0,0,0},  \
                          0,  \
                     0x3FFF,  \
                          0,  \
                        -10,  \
                        0,    \
					    }                         

/*------------------------------------------------------------------------------
Target Independent Default Initializer HALL3 Object
------------------------------------------------------------------------------*/
#define HALL3_DEFAULTS F2803XHALL3
void HALL3_DETERMINE_STATE_MACRO(HALL3 *v);
void HALL3_DEBOUNCE_MACRO(HALL3 *v);
void HALL3_NEXT_STATE_MACRO(HALL3 *v);
void HALL3_DETERMINE_STATE_MACRO(HALL3 *v);
void HALL3_CREATE_MAP(HALL3 *v);
/*----------------------------------------------------------------------------------------------
	 HALL3_INIT Macro Definition
----------------------------------------------------------------------------------------------*/
void HALL3_INIT_MACRO(HALL3 *v)	{

	EALLOW;                       /* Enable EALLOW*/
	/* Configure ECAP1/2/3 as GPIO-inputs (GPIO24/25/26 or GPIO24/13/14) for Piccolo B & A*/
	GpioCtrlRegs.GPAMUX2.bit.GPIO24 = 0;  /* GPIO24 is ECAP1 - A */
	GpioCtrlRegs.GPAMUX2.bit.GPIO25 = 0;  /* GPIO25 is ECAP2 - B */
	GpioCtrlRegs.GPAMUX2.bit.GPIO26 = 0;  /* GPIO26 is ECAP3 - C */

	/* Config GPIO24/25/26/13/14 as inputs*/
	GpioCtrlRegs.GPADIR.bit.GPIO24 = 0;   /* GPIO24 is ECAP1 - A */
	GpioCtrlRegs.GPADIR.bit.GPIO25 = 0;   /* GPIO25 is ECAP2 - B */
	GpioCtrlRegs.GPADIR.bit.GPIO26 = 0;   /* GPIO26 is ECAP3 - C */
	EDIS;                         /* Disable EALLOW*/

	HALL3_DETERMINE_STATE_MACRO(v);
	v->HallGpioBuffer = v->HallGpio;       /* Init with current ECAP/GPIO logic levels*/
	v->HallGpioAccepted = v->HallGpio;     /* Init with current ECAP/GPIO logic levels*/
}
/*----------------------------------------------------------------------------------------------
	 HALL3_DETERMINE_STATE Macro Definition
----------------------------------------------------------------------------------------------*/
	Uint32 temp;
	Uint16 HallGpioBitA,HallGpioBitB,HallGpioBitC;

void HALL3_DETERMINE_STATE_MACRO(HALL3 *v){

	/* temp.2-0 = GPIO26.GPIO25.GPIO24 */
	temp = (GpioDataRegs.GPADAT.all>>24)&0x00000007; /* read all three GPIOs at once*/

	HallGpioBitA = (temp&0x00000001);			/* save GPIO24 - A*/
	HallGpioBitB = (temp&0x00000002)>>1;		/* save GPIO25 - B*/
	HallGpioBitC = (temp&0x00000004)>>2;		/* save GPIO26 - C*/
	HallGpioBitA = HallGpioBitA;
	HallGpioBitB = HallGpioBitB<<1;
	HallGpioBitC = HallGpioBitC<<2;

	v->HallGpio = HallGpioBitA + HallGpioBitB + HallGpioBitC;
}
/*----------------------------------------------------------------------------------------------
	 HALL3_READ Macro Definition 防抖换相
----------------------------------------------------------------------------------------------*/

void HALL3_READ_MACRO(HALL3 *v){
        v->CmtnTrigHall = 0;		/* Reset trigger, it only handshakes with calling program.*/
	if (v->EdgeDebounced==0)	/* Debounce current position. */
	{
		HALL3_DEBOUNCE_MACRO(v);
		v->CmtnTrigHall = v->EdgeDebounced;      /* Set Commutation trigger here*/
	}
	else							/* If current position is debounced, find match in table */
		HALL3_NEXT_STATE_MACRO(v);	/* and return pointer to current state.  Ptr to be incremented*/
								    /* by MOD6CNT after RET.*/

	v->EdgeDebounced = 0;            /* Reset trigger*/
}

/*----------------------------------------------------------------------------------------------
	 HALL3_DEBOUNCE Macro Definition
----------------------------------------------------------------------------------------------*/


void HALL3_DEBOUNCE_MACRO(HALL3 *v)	{

	HALL3_DETERMINE_STATE_MACRO(v);			/* read HallGpio*/

	if (v->HallGpio == v->HallGpioAccepted)	/* GPIO_UNCHANGED: Current GPIO reading == debounced 防抖 ..*/												/*..GPIO reading, no change in state (no edge yet)	*/
	{
		if (v->Revolutions <= 0)         	/* Only create hall map during initial Revolutions*/
			HALL3_CREATE_MAP(v);

		v->StallCount -= 1;            		/* Decrement stall counter*/
		if (v->StallCount == 0)
		{
		    v->EdgeDebounced = 0x7FFF;		/* 换相0x7FFF If motor has stalled, then user trigger to commutate*/
		    v->StallCount = 0x3FFF;    		/* Reset counter to starting value*/
		    v->Stall_status = 1;
		}

		v->DebounceCount = 0;
	}
	else          							/* GPIO_CHANGED: Motor might have moved to a new position.*/
	{
		if (v->HallGpio == v->HallGpioBuffer)	/* Current GPIO reading == previous GPIO reading?*/
		{
			if (v->DebounceCount >= v->DebounceAmount)	/* If equal, is current GPIO reading debounced?*/
			{
				v->HallGpioAccepted = v->HallGpioBuffer;	/* Current GPIO reading is now debounced*/
				v->EdgeDebounced = 0x7FFF;        		/*换相 Edge/position debounced, trigger commutation*/

				v->StallCount = 0x7FFF;         		    /* On new edge, reset stall counter*/
				 v->Stall_status = 0;
				v->CapCounter += 1;                   	/* Increment running edge detection counter*/

				v->DebounceCount = 0;             		/* Reset debounce counter*/

				if (v->HallMapPointer==0)
					v->Revolutions += 1;          		/* Increment on every rev (HallMapPointer = 0)*/
			}
			else     									/* DEBOUNCE_MORE*/
				v->DebounceCount += 1;            		/* 防抖计数 Increment debounce counter*/
		}
		else       										/* NEW_READING*/
		{
			v->HallGpioBuffer = v->HallGpio;  		/* Save new reading and reset debounce counter*/
			v->DebounceCount = 0;
		}
	}
		}
/*----------------------------------------------------------------------------------------------
	 HALL3_NEXT Macro Definition
----------------------------------------------------------------------------------------------*/

	int16 i, HallPointer;
void HALL3_NEXT_STATE_MACRO(HALL3 *v){

	if (v->Revolutions>0)      			/* Only run function after map has been created.*/
	{
		for (i=0;i<=5;i++)          	/* Search for a match of current debounced GPIO position*/
		{                           	/* and the table entries.*/
			if (v->HallMap[i] == v->HallGpioAccepted)		/* Match_Found*/
				HallPointer = i;
		}

		v->HallMapPointer = HallPointer; /* On match, save pointer position. Pointer will be incremented */
	}	                            	/* by 1 since MOD6CNT will receive a positive trigger*/
}
/*----------------------------------------------------------------------------------------------
	 HALL3_CREATE_MAP Macro Definition
----------------------------------------------------------------------------------------------*/

void HALL3_CREATE_MAP(HALL3 *v){

	v->HallMap[v->HallMapPointer] = v->HallGpioAccepted;  /* Save debounced GPIO to table.*/

}


#endif // __F2803X_HALL3_H__ 






