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

📄 term1.c

📁 ARM入门的好帮手.包含了从简单到相对较复杂的程序.
💻 C
字号:
//*----------------------------------------------------------------------------
//*         ATMEL Microcontroller Software Support  -  ROUSSET  -
//*----------------------------------------------------------------------------
//* The software is delivered "AS IS" without warranty or condition of any
//* kind, either express, implied or statutory. This includes without
//* limitation any warranty or condition with respect to merchantability or
//* fitness for any particular purpose, or against the infringements of
//* intellectual property rights of others.
//*----------------------------------------------------------------------------
//* File Name           : term1.c
//* Object              : Terminal functions Library.
//* 17/04/00    EL      : Creation
//*----------------------------------------------------------------------------

#include    "term1.h"
#include    "protocol.h"

u_char  command_cnt, command_lng, next_command_lng;
extern char NewCommand;

//*----------------------------------------------------------------------------
//* Function Name       : at91_term1_c_handler
//* Object              : terminal interrupt handler
//* Input Parameters    : terminal descriptor
//* Output Parameters   : None
//*----------------------------------------------------------------------------
void at91_term1_c_handler ( TerminalDesc *term_desc )
//* Begin
{
    u_int   status ;
    u_int   x ;
    StructUSART         *usart = term_desc->usart_desc->usart_base ;
    TerminalDataDesc    *data = term_desc->term_data ;

    while (( status = ( usart->US_CSR & usart->US_IMR )) != 0 )
    {
        //* If a reception error occured
        if (( status & US_MASK_IRQ_ERROR ) != 0 )
        {
            status &= ~US_RXRDY ;
            usart->US_CR = US_RSTSTA ;
            x = usart->US_RHR ;
        }

        //* RXRDY interrupt
        if (( status & US_RXRDY ) != 0 )
        {
            if ((u_int)data->rx_cnt == TERMINAL_SIZE_BUFFER)
            {
                data->error |= TERMINAL_ERROR_RX_OVF ;
            }
            else
            {
                //* reception
                *(data->rx_in_pt) = usart->US_RHR ;

		if (data->rx_cnt == 0)
		{
		    //* New command
		    switch(*(data->rx_in_pt))
		    {
			case DATA : command_cnt = command_lng;
				    next_command_lng = FALSE;
				    break;

			case SYNC : command_cnt = LEN_SYNC_COMMAND;
				    next_command_lng = FALSE;
				    break;

			case ACK :  command_cnt = LEN_ACK_COMMAND;
				    next_command_lng = FALSE;
				    break;

			case NACK : command_cnt = LEN_NACK_COMMAND;
				    next_command_lng = FALSE;
				    break;

			case SPEED : command_cnt = LEN_SPEED_COMMAND;
				    next_command_lng = FALSE;
				    break;

			case ERASE : command_cnt = LEN_ERASE_COMMAND;
				    next_command_lng = FALSE;
				    break;

			case WRITE : command_cnt = LEN_WRITE_COMMAND;
				    next_command_lng = TRUE;	
				    break;

			case READ : command_cnt = LEN_READ_COMMAND;
				    next_command_lng = TRUE;	
				    break;

			case VERIFY : command_cnt = LEN_VERIFY_COMMAND;
				    next_command_lng = FALSE;
				    break;

			case RESET : command_cnt = LEN_RESET_COMMAND;
				    next_command_lng = FALSE;
				    break;

			default :   command_cnt = 1;
				    next_command_lng = FALSE;
				    break;

		    }
		}

		command_cnt--;
		data->rx_cnt++;

		if (command_cnt == 0)
		{
		    NewCommand = TRUE;
		    data->rx_cnt = 0;
		    if (next_command_lng == TRUE)
			command_lng = *(data->rx_in_pt)+2;	
		    //* reset pointer
	 	    data->rx_in_pt = data->rx_out_pt;  
		} 
		else
	            data->rx_in_pt++;
            }
        }

        //* TXRDY interrupt
        if (( status & US_TXRDY ) != 0 )
        {
            if ((u_int)data->tx_cnt > 0)
            {
                data->tx_cnt--;

                //* transmission
                usart->US_THR = *(data->tx_out_pt)++ ;
            }
            else
            {
	        //* Disable the TXRDY interrupt
                usart->US_IDR = 0x02;
            }
        }
    }
}
//* End

//*----------------------------------------------------------------------------
//* Function Name       : at91_term1_open
//* Object              : open the terminal
//* Input Parameters    : terminal descriptor
//* Output Parameters   : None
//*----------------------------------------------------------------------------
void at91_term1_open ( TerminalDesc *term_desc )
//* Begin
{
    TerminalDataDesc    *data = term_desc->term_data ;

    at91_usart_open ( term_desc->usart_desc, term_desc->format,term_desc->baud_rate, 0 ) ;

    data->rx_in_pt = data->buffer1 ;
    data->tx_in_pt = data->buffer2 ;
    data->rx_out_pt = data->buffer1 ;
    data->tx_out_pt = data->buffer2 ;
    data->rx_cnt = 0 ;
    data->tx_cnt = 0 ;

}
//* End

//*----------------------------------------------------------------------------
//* Function Name       : at91_term1_close
//* Object              : close the terminal
//* Input Parameters    : terminal descriptor
//* Output Parameters   : None
//*----------------------------------------------------------------------------
void at91_term1_close ( TerminalDesc *term_desc )
//* Begin
{
    at91_usart_close(term_desc->usart_desc);
}
//* End

//* End

⌨️ 快捷键说明

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