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

📄 lib_at91.c

📁 ARM入门的好帮手.包含了从简单到相对较复杂的程序.
💻 C
📖 第 1 页 / 共 2 页
字号:
    //* If External clock used
    if (( mode & SCK_USED ) != 0 )
    {
        //* Define RXD, TXD and SCK as peripheral
        at91_pio_close ( usart_pt->pio_ctrl,
                         (1 << usart_pt->pin_txd) |
                         (1 << usart_pt->pin_rxd) |
                         (1 << usart_pt->pin_sck) ) ;
    }
    //* Else
    else
    {
        //* Define RXD and TXD as peripheral
        at91_pio_close ( usart_pt->pio_ctrl,
                         (1 << usart_pt->pin_txd) |
                         (1 << usart_pt->pin_rxd) ) ;
    //* EndIf
    }

    //* Reset receiver and transmitter
    usart_pt->usart_base->US_CR = US_RSTRX | US_RSTTX | US_RXDIS | US_TXDIS ;

    //* Clear Transmit and Receive Counters
    usart_pt->usart_base->US_RCR = 0 ;
    usart_pt->usart_base->US_TCR = 0 ;

    //* Define the baud rate divisor register
    usart_pt->usart_base->US_BRGR = speed ;

    //* Define the USART mode
    usart_pt->usart_base->US_MR = mode  ;

    //* Write the Timeguard Register
    usart_pt->usart_base->US_TTGR = timeguard ;

    //* Enable receiver and transmitter
    usart_pt->usart_base->US_CR = US_RXEN | US_TXEN ;

//* End
}

//*----------------------------------------------------------------------------
//* Function Name       : at91_usart_close
//* Object              : Disable an USART.
//* Input Parameters    : <usart_pt> = USART Descriptor pointer
//* Output Parameters   : none
//* Functions called    : at91_clock_close, at91_pio_open
//*----------------------------------------------------------------------------
void at91_usart_close ( const UsartDesc *usart_pt )
//* Begin
{
    //* Disable the clock on the Peripheral
    at91_clock_close ( usart_pt->periph_id ) ;

    //* Define all USARTs pins as pio
    at91_pio_open ( usart_pt->pio_ctrl,
                    (1 << usart_pt->pin_txd) |
                    (1 << usart_pt->pin_rxd) |
                    (1 << usart_pt->pin_sck),
                    RESET_PIO_CONF ) ;

    //* Disable all interrupts
    usart_pt->usart_base->US_IDR = 0xFFFFFFFF ;

    //* Abort the Peripheral Data Transfers
    usart_pt->usart_base->US_RCR = 0 ;
    usart_pt->usart_base->US_TCR = 0 ;

    //* Disable receiver and transmitter and stop any activity immediately
    usart_pt->usart_base->US_CR = US_TXDIS | US_RXDIS | US_RSTTX | US_RSTRX ;

//* End
}

//*----------------------------------------------------------------------------
//* Function Name       : at91_usart_get_status
//* Object              : Read the Status Register of an USART.
//* Input Parameters    : <usart_pt> = USART Descriptor pointer
//* Output Parameters   : USART Status Register
//* Functions called    : none
//*----------------------------------------------------------------------------
u_int at91_usart_get_status ( const UsartDesc *usart_pt )
//* Begin
{
    //* Return the Control Status Register Value
    return ( usart_pt->usart_base->US_CSR ) ;

//* End
}

//*----------------------------------------------------------------------------
//* Function Name       : at91_usart_trig_cmd
//* Object              : Reset the Status Bits of an USART.
//* Input Parameters    : <usart_pt> = USART Descriptor pointer
//*                     : <cmd>      = command mask
//* Output Parameters   : none
//* Functions called    : none
//*----------------------------------------------------------------------------
void at91_usart_trig_cmd ( const UsartDesc *usart_pt, u_int cmd )
//* Begin
{
    //* Write the command in the Control Register
    usart_pt->usart_base->US_CR = cmd ;

//* End
}

//*----------------------------------------------------------------------------
//* Function Name       : at91_usart_write
//* Object              : Store a character in the Transmit hold Register.
//* Input Parameters    : <usart_pt>  = USART Descriptor pointer
//*                     : <character> = character to transmit
//* Output Parameters   : none
//* Functions called    : none
//*----------------------------------------------------------------------------
void at91_usart_write ( const UsartDesc *usart_pt, u_int character )
//* Begin
{
    //* Write the character in the Transmit Holding Register
    usart_pt->usart_base->US_THR = character ;

//* End
}

//*----------------------------------------------------------------------------
//* Function Name       : at91_usart_read
//* Object              : Read a character from the Receive Register
//* Input Parameters    : <usart_pt> = USART Descriptor pointer
//*                     : <pt_char>  = point where to save the received character
//* Output Parameters   : none
//* Functions called    : none
//*----------------------------------------------------------------------------
void at91_usart_read ( const UsartDesc *usart_pt, u_int *pt_char )
//* Begin
{
    //* Read the received character and store it
    *pt_char = usart_pt->usart_base->US_RHR ;

//* End
}

//*-----------------------------------------------------------------------------
//* Function Name       : at91_usart_send_frame
//* Object              : Transmit a complete frame.
//* Input Parameters    : <usart_pt> =  USART pointer
//*                     : <pt_buffer> = the address of the receive buffer
//*                     : <max_size> = the maximum number of bytes to be
//*                     :              received
//*                     : <timeout> = the inter-character time delay in number
//*                     :             of byte
//* Output Parameters   :
//* Functions called    : none
//*-----------------------------------------------------------------------------
u_int at91_usart_send_frame ( const UsartDesc *usart_pt, char *pt_buffer, u_int size_buf )
//* Begin
{

    //* Wait for previous transfer finished
    while (( usart_pt->usart_base->US_CSR & US_ENDTX ) == 0 ) ;

    //* Store the address of the buffer
    usart_pt->usart_base->US_TPR = (u_int) pt_buffer ;

    //* Store the number of bytes to transmit
    usart_pt->usart_base->US_TCR = size_buf ;

    //* Return true
    return ( TRUE ) ;
}
//* End

//*-----------------------------------------------------------------------------
//* Function Name       : at91_usart_receive_frame
//* Object              : Receive a complete frame.
//* Input Parameters    : <usart_pt> =  USART pointer
//*                     : <pt_buffer> = the address of the receive buffer
//*                     : <max_size> = the maximum number of bytes to be
//*                     :              received
//*                     : <timeout> = the inter-character time delay in number
//*                     :             of byte
//* Output Parameters   :
//* Functions called    : none
//*-----------------------------------------------------------------------------
u_int at91_usart_receive_frame ( const UsartDesc *usart_pt, char *pt_buffer, u_int max_size , u_int timeout )
//* Begin
{

    //* Store the timeout value
    usart_pt->usart_base->US_RTOR = (timeout * 10 / 4) ;

    //* Restart the timeout logic
    usart_pt->usart_base->US_CR = US_STTTO ;

    //* Store the address of the buffer
    usart_pt->usart_base->US_RPR = (u_int) pt_buffer ;

    //* Store the number of bytes to receive
    usart_pt->usart_base->US_RCR = max_size ;

    //* Return true
    return ( TRUE ) ;
}
//* End
//*----------------------------------------------------------------------------
//* Function Name       : at91_tc_open
//* Object              : Initialize Timer Counter Channel and enable is clock
//* Input Parameters    : <tc_pt> = TC Channel Descriptor Pointer
//*                       <mode> = Timer Counter Mode
//*                     : <tioa> = TIOA enabled as peripheral if non null
//*                     : <tiob> = TIOB enabled as peripheral if non null
//* Output Parameters   : None
//* Functions called    : at91_clock_open, at91_pio_close
//*----------------------------------------------------------------------------
void at91_tc_open ( const TCDesc *tc_pt, u_int mode, u_int tioa, u_int tiob )
//* Begin
{
    u_int pio = 0 ;
    //* Start the Clock of the Timer
    at91_clock_open ( tc_pt->periph_id ) ;

    pio = 1<<tc_pt->pin_tclk ;
    //* Enable TIOA pin if requested
    if ( tioa == TRUE )
    {
        pio |= 1<<tc_pt->pin_tioa ;
    }
    //* Enable TIOB pin if requested
    if ( tiob == TRUE )
    {
        pio |= 1<<tc_pt->pin_tiob ;
    }
    at91_pio_close ( tc_pt->pio_ctrl, pio ) ;

    //* Disable the clock and the interrupts
    tc_pt->tc_base->TC_CCR = TC_CLKDIS ;
    tc_pt->tc_base->TC_IDR = 0xFFFFFFFF ;
    pio = tc_pt->tc_base->TC_SR ;

    //* Set the Mode of the Timer Counter
    tc_pt->tc_base->TC_CMR = mode ;

    //* Enable the clock
    tc_pt->tc_base->TC_CCR = TC_CLKEN ;
//* End
}

//*----------------------------------------------------------------------------
//* Function Name       : at91_tc_close
//* Object              : Stop a Timer Counter Channel and disable is clock
//* Input Parameters    : <tc_pt> = the channel number
//* Output Parameters   : None
//* Functions called    : at91_clock_close
//*----------------------------------------------------------------------------
void at91_tc_close ( const TCDesc *tc_pt )
//* Begin
{
    //* Disable the clock and interrupts
    tc_pt->tc_base->TC_CCR = TC_CLKDIS ;
    tc_pt->tc_base->TC_IDR = 0xFFFFFFFF ;

    //* Stop the Clock of the Timer
    at91_clock_close ( tc_pt->periph_id ) ;
//* End
}

//*----------------------------------------------------------------------------
//* Function Name       : at91_tc_get_status
//* Object              : Read the Status of a Timer Counter Channel
//* Input Parameters    : <tc_pt> = the channel number
//* Output Parameters   : the status value
//* Functions called    : None
//*----------------------------------------------------------------------------
u_int at91_tc_get_status ( const TCDesc *tc_pt )
//* Begin
{
    //* Return the value of the Status Register
    return ( tc_pt->tc_base->TC_SR ) ;

//* End
}

//*----------------------------------------------------------------------------
//* Function Name       : at91_tc_trig_cmd
//* Object              : Generate a software trigger on a TC channel
//* Input Parameters    : <tc_pt> = the channel number to trig
//* Output Parameters   : None
//* Functions called    : at91_error
//*----------------------------------------------------------------------------
void at91_tc_trig_cmd ( const TCDesc *tc_pt, u_int cmd )
//* Begin
{
    //* Depending on the command
    switch (cmd)
    {
        //* Case Channel Trigger
        case TC_TRIG_CHANNEL:
            //* Perform a Software trigger on the corresponding channel
            tc_pt->tc_base->TC_CCR = TC_SWTRG ;
            break ;

        //* Case Synchronization Trigger
        case TC_TRIG_BLOCK:
            //* Perform a synchronization trigger
            ((StructTCBlock *) ((u_int)tc_pt->tc_base & 0xF0))->TC_BCR = TC_SYNC ;
            break ;

        //* Unkonwn
      //  default :
            //* Run the AT91 Library Error Macro
       //$$$$     at91_error ("__FILE__","__LINE__") ;
    //* End Switch
    }
//* End
}

//*----------------------------------------------------------------------------
//* Function Name       : at91_tc_set_mode
//* Object              : Update Timer Counter Mode Register with mask
//* Input Parameters    : <tc_pt> = the channel number
//*                     : <mask> = bit to modify in the mode register
//*                     : <data> = set/clear bits in the mode register
//* Output Parameters   : none
//* Functions called    : none
//*----------------------------------------------------------------------------
void at91_tc_set_mode ( const TCDesc *tc_pt, u_int mask, u_int data )
//* Begin
{
    //* If data is not null
    if (data != 0)
        //* Set bits in the Mode Register corresponding to the mask
        tc_pt->tc_base->TC_CMR |= mask ;
    //* Else
    else
        //* Clear bits in the Mode Register corresponding to the mask
        tc_pt->tc_base->TC_CMR &= ~mask ;
    //* EndIf
}
//* End

//*----------------------------------------------------------------------------
//* Function Name       : at91_tc_read
//* Object              : Read all Timer Counter Register
//* Input Parameters    : <tc_pt> = Channel Descriptor Pointer
//*                     : <reg> = Destination Register Value Table Pointer
//* Output Parameters   : None
//* Functions called    : None
//*----------------------------------------------------------------------------
void at91_tc_read ( const TCDesc *tc_pt, u_int reg[] )
//* Begin
{
    reg[RA] = tc_pt->tc_base->TC_RA ;
    reg[RB] = tc_pt->tc_base->TC_RB ;
    reg[RC] = tc_pt->tc_base->TC_RC ;
    reg[CV] = tc_pt->tc_base->TC_CV ;
}
//* End

//*----------------------------------------------------------------------------
//* Function Name       : at91_tc_write
//* Object              : Write Timer Counter Register
//* Input Parameters    : <tc_pt> = Timer Counter Channel Descriptor Pointer
//*                     : <reg> = Source Register Value Table Pointer
//* Output Parameters   : None
//* Functions called    : None
//*----------------------------------------------------------------------------
void at91_tc_write ( const TCDesc *tc_pt, u_int reg[] )
//* Begin
{
    //* First Value -> Register A
    tc_pt->tc_base->TC_RA = reg[RA] ;
    //* Second Value -> Register B
    tc_pt->tc_base->TC_RB = reg[RB] ;
    //* Third Value -> Register C
    tc_pt->tc_base->TC_RC = reg[RC] ;
}
//* End
//*----------------------------------------------------------------------------
//* Function Name       : Interrupt Handlig
//* Output Parameters   : None
//* Functions called    : None
//*----------------------------------------------------------------------------

void at91_default_fiq_handler  (void)
{
	for(;;);
}
void  at91_default_irq_handler (void)
{
	for(;;);
}

void  at91_spurious_handler (void)
{
	for(;;);
}


⌨️ 快捷键说明

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