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

📄 led_blink.c

📁 uC/OS 使用gcc进行开发的例子,可以参考一下
💻 C
📖 第 1 页 / 共 3 页
字号:
    tc_pt->TC_CCR = SWTRG ;

    //* Return True
    return ( TRUE ) ;
}
//* End
#endif

//*P
//*----------------------------------------------------------------------------
//* Function Name       : read_timer_status
//* Object              : Read the Status of a Timer Counter Channel
//* Input Parameters    : <timer_id> = the channel number
//* Output Parameters   : the status value, or 0 if the timer id is out of range
//* Functions called    : none
//*----------------------------------------------------------------------------
_REFERENCE (INT32U my_read_timer_status ( INT32U timer_id ))
#ifdef CORPS
//* Begin
{
    //* If timer_id is out of range, return False
    if ( timer_id >= NB_TIMER ) return ( FALSE ) ;

    //* Return the value of the Status Register
    return ( ConstTimer[timer_id].TCBBase->
                TC[ConstTimer[timer_id].ChannelId].TC_SR ) ;
}
//* End
#endif

//*P
//*----------------------------------------------------------------------------
//* Function Name       : init_timer_irq
//* Object              : Initialize the Timer Counter Handlers
//* Input Parameters    : none
//* Output Parameters   : none
//* Functions called    : none
//*----------------------------------------------------------------------------
_REFERENCE (void my_init_timer_2_irq ( void ))
#ifdef CORPS
//* Begin
{
//    INT32U       tc_id=2 ;

    //* For each timer counter channel
//    for ( tc_id = 0 ; tc_id < NB_TIMER ; tc_id ++ )
//    {
        //* Initialise the default handler address
//        TCHandlerTable[tc_id] = no_handler_tc ;
        //* Enable the interrupt on the Interrupt controller for Timer Counter
        init_interrupt ( (INT8U)TC2IRQ,
                         (INT8U)TC2_IRQ_PRIORITY,
                         (INT8U)EdgeTriggered ,
                         timer_2_isr ) ;
//    }
    //* EndFor
}
//* End
#endif

//*P
//*-----------------------------------------------------------------------------
//* Function Name       : define_as_pio
//* Object              : Define pins as managed by the PIO controller
//* Input Parameters    : <mask> defines the pins to managed by the PIO
//* Output Parameters   : the value of the PIO Status Register
//* Functions called    : none
//*-----------------------------------------------------------------------------
_REFERENCE (INT32U my_define_as_pio ( INT32U pio_ctrl_id, INT32U mask ))
#ifdef CORPS
//* Begin
{
    //* If PIO Controller index is correct
    if ( pio_ctrl_id > NB_PIO_CTRL ) return ( FALSE ) ;
    //* Write the argument mask in the PIO Enable Register
    ConstPio[pio_ctrl_id].PioBase->PIO_PER = mask ;
    //* Return the value of the PIO Status Register
    return ( ConstPio[pio_ctrl_id].PioBase->PIO_PSR ) ;
}
//* End
#endif

//*P
//*-----------------------------------------------------------------------------
//* Function Name       : define_as_output
//* Object              : Define pins as output if managed by the parallel IO
//* Input Parameters    : <mask> defines the pins to be defined as output
//* Output Parameters   : a mask showing the pins really defined as output
//*                     : bit is set if corresponding pin is an output
//* Functions called    : none
//*-----------------------------------------------------------------------------
_REFERENCE (INT32U my_define_as_output ( INT32U pio_ctrl_id, INT32U mask ))
#ifdef CORPS
//* Begin
{
    //* If PIO Controller index is correct
    if ( pio_ctrl_id > NB_PIO_CTRL ) return ( FALSE ) ;
    //* Write the argument mask in the Output Enable Register
    ConstPio[pio_ctrl_id].PioBase->PIO_OER = mask ;
    //* Return PIO Status Register AND PIO Direction Status Register
    return ( ConstPio[pio_ctrl_id].PioBase->PIO_PSR &
             ConstPio[pio_ctrl_id].PioBase->PIO_OSR ) ;
}
//* End
#endif

//*P
//*-----------------------------------------------------------------------------
//* Function Name       : set_pio_output
//* Object              : Set an output if mnaged by the PIO and defined as
//*                     : output
//* Input Parameters    : <mask> defines the pins to be set
//* Output Parameters   : the state of the 32 pins ( on the pads )
//* Functions called    : none
//*-----------------------------------------------------------------------------
_REFERENCE (INT32U my_set_pio_output ( INT32U pio_ctrl_id, INT32U mask ))
#ifdef CORPS
//* Begin
{
    //* If PIO Controller index is correct
    if ( pio_ctrl_id > NB_PIO_CTRL ) return ( FALSE ) ;
    //* Write the argument mask in the Set Output Data Register
    ConstPio[pio_ctrl_id].PioBase->PIO_SODR = mask ;
    //* Return the Pin Data Status Register
    return ( ConstPio[pio_ctrl_id].PioBase->PIO_PDSR ) ;
}
//* End
#endif

//*P
//*-----------------------------------------------------------------------------
//* Function Name       : clear_pio_output
//* Object              : Clear an output if managed by the PIO and defined as
//*                     : output
//* Input Parameters    : <mask> defines the pins to be cleared
//* Output Parameters   : the state of the 32 pins ( on the pads )
//* Functions called    : none
//*-----------------------------------------------------------------------------
_REFERENCE (INT32U my_clear_pio_output ( INT32U pio_ctrl_id, INT32U mask ))
#ifdef CORPS
//* Begin
{
    //* If PIO Controller index is correct
    if ( pio_ctrl_id > NB_PIO_CTRL ) return ( FALSE ) ;
    //* Write the argument mask in the Clear Output Data Register
    ConstPio[pio_ctrl_id].PioBase->PIO_CODR = mask ;
    //* Return the Pin Data Status Register
    return ( ConstPio[pio_ctrl_id].PioBase->PIO_PDSR ) ;
}
//* End
#endif


extern	void Init_UART0( void );
extern	void SendByte0( INT8U value);
extern	INT8U ReceiveByte0( void );
extern	void DispHex32( INT32U value);
/*
Configure the USART 0 of the AT91 to send and receive bytes on the channel 1
set with normal mode.
The channel status is polled to detect the TX and Rx ready.
The byte format is : start + 8 data (without parity) + 1 stop
The baud rate is counter is 53 : 38400 bauds with MCKI = 32.768 MHz
The byte transferred is incremented by one
*/
void	Init_UART0(void)
{
    unsigned int    loop_count;

    //* Stop channel
    //* . disable Tx and Rx and clear errors
    AT91_REG(0xFFFD0000) =
        (1 << 7) |          // TXDIS : Transmitter disable
        (1 << 5) |          // RXDIS : Receiver disable
        (1 << 3) |          // RSTTX : Reset Transmitter
        (1 << 2) ;          // RSTRX : Reset Receiver
    for (loop_count = 1000 ; loop_count > 0 ; loop_count--);
    //* . clear PDC counts (Rx and Tx)
    AT91_REG(0xFFFD0034) = AT91_REG(0xFFFD003C) = 0x00000000;
    //* . Disable ITs
    AT91_REG(0xFFFD000C) = 0xFFFFFFFF;

    //* Initialize the channel
    //* . Tx and Rx as peripheral
    AT91_REG(0xFFFF0004) =
        (1 << 14) |         // P14 - TXD0
        (1 << 15) ;         // P15 - TRD0
    //* . US_MR - Mode
    AT91_REG(0xFFFD0004) =
        (0 << 14) |         // CHMODE = Normal Mode
        (0 << 12) |         // NBSTOP = 1 stop bit
        (4 <<  9) |         // PAR = No parity
        (0 <<  8) |         // SYNC = asynchronous mode
        (3 <<  6) |         // CHRL = 8 bits
        (0 <<  4) ;         // USCLKS = MCKI
    //* . US_BRGR - Baud Rate Generator
    AT91_REG(0xFFFD0020) = 53;
    //* Start channel
    //* . enable Tx and Rx
    AT91_REG(0xFFFD0000) =
        (1 << 6) |          // TXEN : Transmitter enable
        (1 << 4) ;          // RXEN : Receiver enable
}
    
void SendByte0 ( INT8U value )
{    
	//* Wait Tx ready
    for ( ; (AT91_REG(0xFFFD0014) & (1 << 1)) == 0 ; );
    
    //* Send byte
    AT91_REG(0xFFFD001C) = value;

}

INT8U ReceiveByte0 ( void )
{
    //* Wait Rx ready
	for ( ; (AT91_REG(0xFFFD0014) & (1 << 0)) == 0 ; );
    
    //* Receive and check byte
    return AT91_REG(0xFFFD0018);

}

void DispHex32( INT32U value)
{
	INT8U	temp,i;
	
	for(i=0;i<8;i++)
	{
		temp = ( value & 0xf0000000 ) / 0x10000000;
		if ( temp <=0x9 )
			temp += 0x30;
		else
			temp += 0x41 - 0x0a;
		SendByte0(temp);
		value = value << 4;
	}
}


/*
 * Task running at the highest priority. 
 */
void
 Task1(void *i)
{
    INT8U Reply;
//	INT32U	m,n;

    my_delay_microsec ( 2, 100000, TRUE ); //, timer_irq ) ;

	ARMEnableInt();

    for (;;)
    {

//	OSTimeDly(8);

	if(FlagTask1==TRUE)
	{
		my_clear_pio_output ( 0, LED3 ) ;
		FlagTask1=FALSE;
	}
	else
	{
		my_set_pio_output ( 0, LED3 ) ;
		FlagTask1=TRUE;
	}
        /* wait for the semaphore  */
        OSSemPend(Sem2, 0, &Reply);


        /* wait a short while */
//	for(m=0;m<1000;m++)
//		for(n=0;n<100;n++);

        /* signal the semaphore */
//        OSSemPost(Sem1);
    }
}

void
 Task2(void *i)
{
//    INT8U Reply;
//	INT32U	m,n;
	
	debug_led();

    for (;;)
    {

	if(FlagSwi==TRUE)
	{
		my_clear_pio_output ( 0, LED2 ) ;
		FlagSwi=FALSE;
	}
	else
	{
		my_set_pio_output ( 0, LED2 ) ;
		FlagSwi=TRUE;
	}


//	for(m=0;m<1000;m++)
//		for(n=0;n<100;n++);

        /* wait for the semaphore */
//        OSSemPend(Sem1, 0, &Reply);

//        uHALr_printf("[");

        /* wait a short while */
	OSTimeDly(16);

//        uHALr_printf("2]");

        /* signal the semaphore */
        OSSemPost(Sem2);


    }
}

void timer_irq ( StructTC *tc_pt )
{
	
    my_read_timer_status ( 2 ) ;
	if(FlagIrq==TRUE)
	{
		my_clear_pio_output ( 0, LED1 ) ;
		FlagIrq=FALSE;
	}
	else
	{
		my_set_pio_output ( 0, LED1 ) ;
		FlagIrq=TRUE;
	}

}

void swi_irq ( void )
{
	if(FlagSwi==TRUE)
	{
		my_clear_pio_output ( 0, LED2 ) ;
		FlagSwi=FALSE;
	}
	else
	{
		my_set_pio_output ( 0, LED2 ) ;
		FlagSwi=TRUE;
	}

}


void debug_led(void)
{
    my_set_pio_output ( 0, LED3  );
	my_clear_pio_output ( 0, LED2|LED1 );
}	

//*P
//*-----------------------------------------------------------------------------
//* Function Name       : main
//* Object              : Main function of the test "delay"
//* Input Parameters    : none
//* Output Parameters   :
//* Functions called    :
//*-----------------------------------------------------------------------------
_REFERENCE (int MainApplication( void ))
#ifdef CORPS
    //* Begin
{
//	int i,j;

	need_to_swap_context = 0 ;
	
	FlagIrq=TRUE;
	FlagSwi=TRUE;
    FlagTask1=TRUE;
    my_define_as_pio ( 0 , LED1|LED2|LED3 );
    my_define_as_output ( 0, LED1|LED2|LED3 );
    my_set_pio_output ( 0, LED1  );
	my_clear_pio_output ( 0, LED2|LED3 );
	
	Init_UART0();
	DispHex32(0x44440000);

    my_read_timer_status ( 2 ) ;

//    init_timer_irq () ;
	
	my_init_timer_2_irq();

/*	init_interrupt ( SWIRQ,
                     SW_IRQ_PRIORITY,
                     EdgeTriggered ,
//                     swi_interrupt_handler) ;
					OS_TASK_SW);
*/					
    /* needed by uC/OS */
    OSInit();

    my_set_pio_output ( 0, LED2  );
	my_clear_pio_output ( 0, LED1|LED3 );


    /* 
     * create the tasks in uC/OS and assign decreasing
     * priority to them 
     */
	Sem2 = OSSemCreate(0);                            /* Random number semaphore                  */
    OSTaskCreate(Task1, (void*)1, (OS_STK *)&Stack1[STACKSIZE - 1], 21);
    OSTaskCreate(Task2, (void*)0, (OS_STK *)&Stack2[STACKSIZE - 1], 22);

/*    my_set_pio_output ( 0, LED3  );
	my_clear_pio_output ( 0, LED2|LED1 );

    my_delay_microsec ( 2, 100000, TRUE ); //, timer_irq ) ;

	ARMEnableInt();

	while(TRUE)
	{
	}
*/	
    /* start the game */
    OSStart();

	return	0;

//* End
}
#endif

⌨️ 快捷键说明

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