⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pbled_test.c

📁 ADI Blackfin BF51X 範例程式
💻 C
字号:
/*******************************************************************
Analog Devices, Inc. All Rights Reserved.

This software is proprietary and confidential.  By using this software
you agree to the terms of the associated Analog Devices License Agreement.

Project Name:  	Power_On_Self_Test

Hardware:		ADSP-BF518F EZ-Board

Description:	This examples performs pushbutton and LED tests on the EZ-Board.
*******************************************************************/

#include "post_common.h"
#include "PBLED_test.h"
#include "Timer_ISR.h"

/*******************************************************************
*  function prototypes
*******************************************************************/

/*******************************************************************
*  global variables
*******************************************************************/

#define delay 0x400000

int g_LEDs[] = {LED1, LED2, LED3};

int g_nPORTHIO = 0x0;
bool g_button_1_pushed = false;
bool g_button_2_pushed = false;
bool g_button_pd_pushed = false;
bool g_button_wake_pushed = false;
	
extern bool g_bIsTestChosen;
extern int g_nInitialTest;
extern int g_nPostState;


/*******************************************************************
*   Function:    Init_LEDs
*   Description: This function configures PORTH for LEDs.
*******************************************************************/
void Init_LEDs(void)
{
	*pPORTH_FER			&= (~0x0068);	/* Setup for LEDs */
	*pPORTHIO_DIR		|= 0x0068;		/* Setup port for output */
	*pPORTHIO_CLEAR		= 0x0068;		/* Turn all LEDs off */
}

/*******************************************************************
*   Function:    Init_PushButtons
*   Description: This function configures PORTH and interrupts
*					for push buttons.
*******************************************************************/
void Init_PushButtons(void)
{
	int iar3 = *pSIC_IAR3;
	int imask0 = *pSIC_IMASK0;

	/* PB1 uses PH0
	   PB2 uses PH1 */
	
	/* setup PB1 and PB2 only for now */
	*pPORTHIO_INEN		|= (PH0 | PH1);	/* enable*/
	*pPORTHIO_DIR		&= (~(PH0 | PH1));	/* input */

	/* init the global mirror register, any buttons pushed at
		startup will get lost once we configure for edges */
	g_nPORTHIO = *pPORTHIO;

	/* setup interrupts */
	*pPORTHIO_EDGE	|= (PH0 | PH1);		/* edge sensitive */
	*pPORTHIO_POLAR	&= (~(PH0 | PH1));		/* rising edge */
    *pPORTHIO_MASKA_SET	|= (PH0 | PH1);	/* enable pbs */

    /* port H interrupt A is bits 20-23 of IAR3, setup for IVG11 */
    iar3 |= 0x00400000;
	iar3 &= 0xff4fffff;
    *pSIC_IAR3 = iar3;

    /* register ISR and enable port H interrupt A */
    register_handler(ik_ivg11, Pushbutton_ISR);
    imask0 = *pSIC_IMASK0;
    imask0 |= 0x20000000;
    *pSIC_IMASK0 = imask0;
    
    /* clear the LEDs */
    ClearSet_LED_Bank( (-1), 0x0000);
}


/*******************************************************************
*   Function:    LED_Bar
*   Description: Display a blinking LED bar
*******************************************************************/
void LED_Bar(const int iSpeed)
{
	int n,j;

	for( n = 0; n <= 2; n++ )
	{
		ClearSet_LED(g_LEDs[n], 3);
		Delay(iSpeed);
	}
}


/*******************************************************************
*   Function:    LED_Bar_Reverse
*   Description: Display a blinking LED bar in reverse
*******************************************************************/
void LED_Bar_Reverse(const int iSpeed)
{
	int n,j;

	for( n = 2; n >= 0; n-- )
	{
		ClearSet_LED(g_LEDs[n], 3);
		Delay(iSpeed);
	}
}

/*******************************************************************
*   Function:    LED_Bar_BacknForth
*   Description: Display a blinking LED bar that goes back and forth.
*******************************************************************/
void LED_Bar_BacknForth(const int iSpeed)
{
	static unsigned int state = 0; /* remember the state */

	if ( (state % 4) == 0 )
	{
		ClearSet_LED(LED1, 1);
		ClearSet_LED(LED2, 0);
		ClearSet_LED(LED3, 0);
	}
	else if ( (state % 4) == 1 )
	{
		ClearSet_LED(LED1, 0);
		ClearSet_LED(LED2, 1);
		ClearSet_LED(LED3, 0);
	}
	else if ( (state % 4) == 2 )
	{
		ClearSet_LED(LED1, 0);
		ClearSet_LED(LED2, 0);
		ClearSet_LED(LED3, 1);
	}
	else
	{
		ClearSet_LED(LED1, 0);
		ClearSet_LED(LED2, 1);
		ClearSet_LED(LED3, 0);
	}

	Delay(iSpeed);
	state++;
}


/*******************************************************************
*   Function:    Blink_LED
*   Description: Blink various LEDs
*******************************************************************/
void Blink_LED(const int enleds, const int iSpeed)
{
	int n, j;

	while( 1 )
	{
		for( n = 0; n <= 2; n++ )
		{	
			ClearSet_LED(g_LEDs[n], 3);
		}

		Delay(iSpeed);
	}
}


/*******************************************************************
*   Function:    ClearSet_LED_Bank
*   Description: Clear or set a particular LED or group of LEDs
*******************************************************************/
void ClearSet_LED_Bank(const int enleds, const int iState)
{
	int n; 
	int nTempState = iState;


	for( n = 0; n <= 2; n++ )
	{
		ClearSet_LED(g_LEDs[n], (nTempState & 0x3) );

		nTempState >>= 2;
	}
}


/*******************************************************************
*   Function:    ClearSet_LED
*   Description: Clear or set a particular LED (NOT A GROUP).
*******************************************************************/
void ClearSet_LED(const int led, const int bState)
{
	/* if bState is 0 we clear the LED,
	   if bState is 1 we set the LED,
	   else we toggle the LED */

	if( led == LED1 )
	{
	   	if( 0 == bState )
		{
			*pPORTHIO_CLEAR = 0x0008;
		}
		else if( 1 == bState )
		{
			*pPORTHIO_SET = 0x0008;
		}
		else
		{
			*pPORTHIO_TOGGLE = 0x0008;
		}
	}
	else if( led == LED2 )
	{
		if( 0 == bState )
		{
			*pPORTHIO_CLEAR = 0x0020;
		}
		else if( 1 == bState )
		{
			*pPORTHIO_SET = 0x0020;
		}
		else
		{
			*pPORTHIO_TOGGLE = 0x0020;
		}
	}
	else
	{
		if( 0 == bState )
		{
			*pPORTHIO_CLEAR = 0x0040;
		}
		else if( 1 == bState )
		{
			*pPORTHIO_SET = 0x0040;
		}
		else
		{
			*pPORTHIO_TOGGLE = 0x0040;
		}
	}

}

/*******************************************************************
*   Function:    TEST_PBLED
*   Description: test to make sure LEDs and PBs are working correctly
*******************************************************************/
int TEST_PBLED(void)
{
	int temp = 0;
	bool bPB1 = false, bPB2 = false;	/* flags to see if button was pushed */

	/* init globals, do this each time because we may be called more than once */
	g_nPORTHIO = 0x0;

	/* enable push buttons pb1 and pb2 */
	Init_PushButtons();
	
	ClearSet_LED( LED1, 0x0);
	ClearSet_LED( LED2, 0x1);
	ClearSet_LED( LED3, 0x0);

	/* loop until both pbs are pushed */
	while( !bPB1 || !bPB2 )
	{
		/* PB ISR will update this when a pb is pushed */
		temp = g_nPORTHIO;

		/* was pb1 pushed? */
		if( (PH0 == (temp & PH0)) && !bPB1 )
		{
			/* pb1 was pressed, toggle all LEDs */
			bPB1 = true;
			ClearSet_LED_Bank( (LED1|LED2|LED3), 0x2a );
		}

		/* was pb2 pushed? */
		if( (PH1 == (temp & PH1)) && !bPB2 )
		{
			/* pb2 was pressed, toggle all LEDs */
			bPB2 = true;
			ClearSet_LED_Bank( (LED1|LED2|LED3), 0x2a );
		}
	}

	/* return status */
	return (int)(bPB1 && bPB2);
}


/*******************************************************************
*   Function:    Pushbutton_ISR
*   Description: Called each time PB1 or PB2 are pressed.
*******************************************************************/
EX_INTERRUPT_HANDLER(Pushbutton_ISR)
{
	int pb_status = *pPORTHIO;
	static int pb1_ctr = 0;
	static int pb2_ctr = 0;
	static int unknown_ctr = 0;
	static bool bDone = false;

	/* if we are running tests already this may be the PB test so
	   we update the global mirror register, clear it, and return */
	if ( STATE_SELECTED == g_nPostState )
	{
		g_nPORTHIO |= *pPORTHIO;
		*pPORTHIO_CLEAR = 0x003;
		return;
	}

	/* if PB1 pushed */
	if ( 0x0001 == (pb_status & PH0) )
	{
		pb1_ctr++;
		*pPORTHIO_CLEAR = PH0;
		g_button_1_pushed = true;
	}

	/* if PB2 pushed */
	else if ( 0x0002 == (pb_status & PH1) )
	{
		pb2_ctr++;
		*pPORTHIO_CLEAR = PH1;
		g_button_2_pushed = true;
	}

	/* some other interrupt got us here */
	else
	{
		unknown_ctr++;
	}

	/* update the state machine */
	switch (g_nPostState)
	{
		case STATE_START:
		case STATE_1:
		case STATE_2:
		case STATE_3:
		case STATE_4:

			/* pushing a 0 */
			if (g_button_1_pushed)
			{
				g_nPostState += 1;
				g_nInitialTest <<= 1;
				g_button_1_pushed = false;
			}

			/* pushing a 1 */
			if (g_button_2_pushed)
			{
				g_nPostState += 1;
				g_nInitialTest <<= 1;
				g_nInitialTest |= 0x1;
				g_button_2_pushed = false;
			}

		break;
	}

	/* when we get to this state a test has been sucessfully entered */
	if ( STATE_SELECTED == g_nPostState )
	{
		g_bIsTestChosen = true;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -